blob: 6db01417d39a1839fd5b43280db6fbf57a4b4c80 [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>
33#include <linux/fs.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100034#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/kernel.h>
36#include <linux/list.h>
37#include <linux/mm.h>
38#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070044#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/seq_file.h>
46#include <linux/slab.h>
47#include <linux/magic.h>
48#include <linux/spinlock.h>
49#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080052#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070055#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040056#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070057#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070058#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070059#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080060#include <linux/eventfd.h>
61#include <linux/poll.h>
Ben Blumd8466872011-05-26 16:25:21 -070062#include <linux/flex_array.h> /* used in cgroup_attach_proc */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020063#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heo28b4c272012-04-01 12:09:56 -070067/* css deactivation bias, makes css->refcnt negative to deny new trygets */
68#define CSS_DEACT_BIAS INT_MIN
69
Tejun Heoe25e2cb2011-12-12 18:12:21 -080070/*
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
73 *
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
79 *
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
82 *
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
84 * breaks it.
85 */
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -080087static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070088
Ben Blumaae8aab2010-03-10 15:22:07 -080089/*
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020091 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080092 * registered after that. The mutable section of this array is protected by
93 * cgroup_mutex.
94 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020095#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020096#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -080097static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070098#include <linux/cgroup_subsys.h>
99};
100
Paul Menagec6d57f32009-09-23 15:56:19 -0700101#define MAX_CGROUP_ROOT_NAMELEN 64
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
106 * hierarchy
107 */
108struct cgroupfs_root {
109 struct super_block *sb;
110
111 /*
112 * The bitmask of subsystems intended to be attached to this
113 * hierarchy
114 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400115 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700117 /* Unique id for this hierarchy. */
118 int hierarchy_id;
119
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400121 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122
123 /* A list running through the attached subsystems */
124 struct list_head subsys_list;
125
126 /* The root cgroup for this hierarchy */
127 struct cgroup top_cgroup;
128
129 /* Tracks how many cgroups are currently defined in hierarchy.*/
130 int number_of_cgroups;
131
Li Zefane5f6a862009-01-07 18:07:41 -0800132 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133 struct list_head root_list;
134
Tejun Heob0ca5a82012-04-01 12:09:54 -0700135 /* All cgroups on this root, cgroup_mutex protected */
136 struct list_head allcg_list;
137
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138 /* Hierarchy-specific flags */
139 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700140
Tejun Heo0a950f62012-11-19 09:02:12 -0800141 /* IDs for cgroups in this hierarchy */
142 struct ida cgroup_ida;
143
Paul Menagee788e062008-07-25 01:46:59 -0700144 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700145 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700146
147 /* The name for this hierarchy - may be empty */
148 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700149};
150
Paul Menageddbcc7e2007-10-18 23:39:30 -0700151/*
152 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
153 * subsystems that are otherwise unattached - it never has more than a
154 * single cgroup, and all tasks are part of that cgroup.
155 */
156static struct cgroupfs_root rootnode;
157
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700158/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700159 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
160 */
161struct cfent {
162 struct list_head node;
163 struct dentry *dentry;
164 struct cftype *type;
165};
166
167/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700168 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
169 * cgroup_subsys->use_id != 0.
170 */
171#define CSS_ID_MAX (65535)
172struct css_id {
173 /*
174 * The css to which this ID points. This pointer is set to valid value
175 * after cgroup is populated. If cgroup is removed, this will be NULL.
176 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800177 * is called after synchronize_rcu(). But for safe use, css_tryget()
178 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700179 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100180 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700181 /*
182 * ID of this css.
183 */
184 unsigned short id;
185 /*
186 * Depth in hierarchy which this ID belongs to.
187 */
188 unsigned short depth;
189 /*
190 * ID is freed by RCU. (and lookup routine is RCU safe.)
191 */
192 struct rcu_head rcu_head;
193 /*
194 * Hierarchy of CSS ID belongs to.
195 */
196 unsigned short stack[0]; /* Array of Length (depth+1) */
197};
198
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800199/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300200 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800201 */
202struct cgroup_event {
203 /*
204 * Cgroup which the event belongs to.
205 */
206 struct cgroup *cgrp;
207 /*
208 * Control file which the event associated.
209 */
210 struct cftype *cft;
211 /*
212 * eventfd to signal userspace about the event.
213 */
214 struct eventfd_ctx *eventfd;
215 /*
216 * Each of these stored in a list by the cgroup.
217 */
218 struct list_head list;
219 /*
220 * All fields below needed to unregister event when
221 * userspace closes eventfd.
222 */
223 poll_table pt;
224 wait_queue_head_t *wqh;
225 wait_queue_t wait;
226 struct work_struct remove;
227};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700228
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229/* The list of hierarchy roots */
230
231static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700232static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700233
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700234static DEFINE_IDA(hierarchy_ida);
235static int next_hierarchy_id;
236static DEFINE_SPINLOCK(hierarchy_id_lock);
237
Paul Menageddbcc7e2007-10-18 23:39:30 -0700238/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
239#define dummytop (&rootnode.top_cgroup)
240
241/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800242 * check for fork/exit handlers to call. This avoids us having to do
243 * extra work in the fork/exit path if none of the subsystems need to
244 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700245 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700246static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700247
Tejun Heo42809dd2012-11-19 08:13:37 -0800248static int cgroup_destroy_locked(struct cgroup *cgrp);
249
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800250#ifdef CONFIG_PROVE_LOCKING
251int cgroup_lock_is_held(void)
252{
253 return lockdep_is_held(&cgroup_mutex);
254}
255#else /* #ifdef CONFIG_PROVE_LOCKING */
256int cgroup_lock_is_held(void)
257{
258 return mutex_is_locked(&cgroup_mutex);
259}
260#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
261
262EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
263
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700264static int css_unbias_refcnt(int refcnt)
265{
266 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
267}
268
Tejun Heo28b4c272012-04-01 12:09:56 -0700269/* the current nr of refs, always >= 0 whether @css is deactivated or not */
270static int css_refcnt(struct cgroup_subsys_state *css)
271{
272 int v = atomic_read(&css->refcnt);
273
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700274 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700275}
276
Paul Menageddbcc7e2007-10-18 23:39:30 -0700277/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700278inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700279{
Paul Menagebd89aab2007-10-18 23:40:44 -0700280 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700281}
282
283/* bits in struct cgroupfs_root flags field */
284enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400285 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
286 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700287};
288
Adrian Bunke9685a02008-02-07 00:13:46 -0800289static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700290{
291 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700292 (1 << CGRP_RELEASABLE) |
293 (1 << CGRP_NOTIFY_ON_RELEASE);
294 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700295}
296
Adrian Bunke9685a02008-02-07 00:13:46 -0800297static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700298{
Paul Menagebd89aab2007-10-18 23:40:44 -0700299 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700300}
301
Paul Menageddbcc7e2007-10-18 23:39:30 -0700302/*
303 * for_each_subsys() allows you to iterate on each subsystem attached to
304 * an active hierarchy
305 */
306#define for_each_subsys(_root, _ss) \
307list_for_each_entry(_ss, &_root->subsys_list, sibling)
308
Li Zefane5f6a862009-01-07 18:07:41 -0800309/* for_each_active_root() allows you to iterate across the active hierarchies */
310#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700311list_for_each_entry(_root, &roots, root_list)
312
Tejun Heof6ea9372012-04-01 12:09:55 -0700313static inline struct cgroup *__d_cgrp(struct dentry *dentry)
314{
315 return dentry->d_fsdata;
316}
317
Tejun Heo05ef1d72012-04-01 12:09:56 -0700318static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700319{
320 return dentry->d_fsdata;
321}
322
Tejun Heo05ef1d72012-04-01 12:09:56 -0700323static inline struct cftype *__d_cft(struct dentry *dentry)
324{
325 return __d_cfe(dentry)->type;
326}
327
Paul Menage81a6a5c2007-10-18 23:39:38 -0700328/* the list of cgroups eligible for automatic release. Protected by
329 * release_list_lock */
330static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200331static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700332static void cgroup_release_agent(struct work_struct *work);
333static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700334static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335
Paul Menage817929e2007-10-18 23:39:36 -0700336/* Link structure for associating css_set objects with cgroups */
337struct cg_cgroup_link {
338 /*
339 * List running through cg_cgroup_links associated with a
340 * cgroup, anchored on cgroup->css_sets
341 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700342 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700343 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700344 /*
345 * List running through cg_cgroup_links pointing at a
346 * single css_set object, anchored on css_set->cg_links
347 */
348 struct list_head cg_link_list;
349 struct css_set *cg;
350};
351
352/* The default css_set - used by init and its children prior to any
353 * hierarchies being mounted. It contains a pointer to the root state
354 * for each subsystem. Also used to anchor the list of css_sets. Not
355 * reference-counted, to improve performance when child cgroups
356 * haven't been created.
357 */
358
359static struct css_set init_css_set;
360static struct cg_cgroup_link init_css_set_link;
361
Ben Blume6a11052010-03-10 15:22:09 -0800362static int cgroup_init_idr(struct cgroup_subsys *ss,
363 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700364
Paul Menage817929e2007-10-18 23:39:36 -0700365/* css_set_lock protects the list of css_set objects, and the
366 * chain of tasks off each css_set. Nests outside task->alloc_lock
367 * due to cgroup_iter_start() */
368static DEFINE_RWLOCK(css_set_lock);
369static int css_set_count;
370
Paul Menage7717f7b2009-09-23 15:56:22 -0700371/*
372 * hash table for cgroup groups. This improves the performance to find
373 * an existing css_set. This hash doesn't (currently) take into
374 * account cgroups in empty hierarchies.
375 */
Li Zefan472b1052008-04-29 01:00:11 -0700376#define CSS_SET_HASH_BITS 7
377#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
378static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
379
380static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
381{
382 int i;
383 int index;
384 unsigned long tmp = 0UL;
385
386 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
387 tmp += (unsigned long)css[i];
388 tmp = (tmp >> 16) ^ tmp;
389
390 index = hash_long(tmp, CSS_SET_HASH_BITS);
391
392 return &css_set_table[index];
393}
394
Paul Menage817929e2007-10-18 23:39:36 -0700395/* We don't maintain the lists running through each css_set to its
396 * task until after the first call to cgroup_iter_start(). This
397 * reduces the fork()/exit() overhead for people who have cgroups
398 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700399static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700400
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700401static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700402{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700403 struct cg_cgroup_link *link;
404 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700405 /*
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
408 * rwlock
409 */
410 if (atomic_add_unless(&cg->refcount, -1, 1))
411 return;
412 write_lock(&css_set_lock);
413 if (!atomic_dec_and_test(&cg->refcount)) {
414 write_unlock(&css_set_lock);
415 return;
416 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700417
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700418 /* This css_set is dead. unlink it and release cgroup refcounts */
419 hlist_del(&cg->hlist);
420 css_set_count--;
421
422 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
423 cg_link_list) {
424 struct cgroup *cgrp = link->cgrp;
425 list_del(&link->cg_link_list);
426 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700427 if (atomic_dec_and_test(&cgrp->count) &&
428 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700429 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700430 set_bit(CGRP_RELEASABLE, &cgrp->flags);
431 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700433
434 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436
437 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800438 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700439}
440
441/*
442 * refcounted get/put for css_set objects
443 */
444static inline void get_css_set(struct css_set *cg)
445{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700446 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700447}
448
449static inline void put_css_set(struct css_set *cg)
450{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700451 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700452}
453
Paul Menage81a6a5c2007-10-18 23:39:38 -0700454static inline void put_css_set_taskexit(struct css_set *cg)
455{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700456 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700457}
458
Paul Menage817929e2007-10-18 23:39:36 -0700459/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700460 * compare_css_sets - helper function for find_existing_css_set().
461 * @cg: candidate css_set being tested
462 * @old_cg: existing css_set for a task
463 * @new_cgrp: cgroup that's being entered by the task
464 * @template: desired set of css pointers in css_set (pre-calculated)
465 *
466 * Returns true if "cg" matches "old_cg" except for the hierarchy
467 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
468 */
469static bool compare_css_sets(struct css_set *cg,
470 struct css_set *old_cg,
471 struct cgroup *new_cgrp,
472 struct cgroup_subsys_state *template[])
473{
474 struct list_head *l1, *l2;
475
476 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
477 /* Not all subsystems matched */
478 return false;
479 }
480
481 /*
482 * Compare cgroup pointers in order to distinguish between
483 * different cgroups in heirarchies with no subsystems. We
484 * could get by with just this check alone (and skip the
485 * memcmp above) but on most setups the memcmp check will
486 * avoid the need for this more expensive check on almost all
487 * candidates.
488 */
489
490 l1 = &cg->cg_links;
491 l2 = &old_cg->cg_links;
492 while (1) {
493 struct cg_cgroup_link *cgl1, *cgl2;
494 struct cgroup *cg1, *cg2;
495
496 l1 = l1->next;
497 l2 = l2->next;
498 /* See if we reached the end - both lists are equal length. */
499 if (l1 == &cg->cg_links) {
500 BUG_ON(l2 != &old_cg->cg_links);
501 break;
502 } else {
503 BUG_ON(l2 == &old_cg->cg_links);
504 }
505 /* Locate the cgroups associated with these links. */
506 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
507 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
508 cg1 = cgl1->cgrp;
509 cg2 = cgl2->cgrp;
510 /* Hierarchies should be linked in the same order. */
511 BUG_ON(cg1->root != cg2->root);
512
513 /*
514 * If this hierarchy is the hierarchy of the cgroup
515 * that's changing, then we need to check that this
516 * css_set points to the new cgroup; if it's any other
517 * hierarchy, then this css_set should point to the
518 * same cgroup as the old css_set.
519 */
520 if (cg1->root == new_cgrp->root) {
521 if (cg1 != new_cgrp)
522 return false;
523 } else {
524 if (cg1 != cg2)
525 return false;
526 }
527 }
528 return true;
529}
530
531/*
Paul Menage817929e2007-10-18 23:39:36 -0700532 * find_existing_css_set() is a helper for
533 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700534 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700535 *
536 * oldcg: the cgroup group that we're using before the cgroup
537 * transition
538 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700539 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700540 *
541 * template: location in which to build the desired set of subsystem
542 * state objects for the new cgroup group
543 */
Paul Menage817929e2007-10-18 23:39:36 -0700544static struct css_set *find_existing_css_set(
545 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700546 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700547 struct cgroup_subsys_state *template[])
548{
549 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700550 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700551 struct hlist_head *hhead;
552 struct hlist_node *node;
553 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700554
Ben Blumaae8aab2010-03-10 15:22:07 -0800555 /*
556 * Build the set of subsystem state objects that we want to see in the
557 * new css_set. while subsystems can change globally, the entries here
558 * won't change, so no need for locking.
559 */
Paul Menage817929e2007-10-18 23:39:36 -0700560 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400561 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700562 /* Subsystem is in this hierarchy. So we want
563 * the subsystem state from the new
564 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700565 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700566 } else {
567 /* Subsystem is not in this hierarchy, so we
568 * don't want to change the subsystem state */
569 template[i] = oldcg->subsys[i];
570 }
571 }
572
Li Zefan472b1052008-04-29 01:00:11 -0700573 hhead = css_set_hash(template);
574 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700575 if (!compare_css_sets(cg, oldcg, cgrp, template))
576 continue;
577
578 /* This css_set matches what we need */
579 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700580 }
Paul Menage817929e2007-10-18 23:39:36 -0700581
582 /* No existing cgroup group matched */
583 return NULL;
584}
585
Paul Menage817929e2007-10-18 23:39:36 -0700586static void free_cg_links(struct list_head *tmp)
587{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700588 struct cg_cgroup_link *link;
589 struct cg_cgroup_link *saved_link;
590
591 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700592 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700593 kfree(link);
594 }
595}
596
597/*
Li Zefan36553432008-07-29 22:33:19 -0700598 * allocate_cg_links() allocates "count" cg_cgroup_link structures
599 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
600 * success or a negative error
601 */
602static int allocate_cg_links(int count, struct list_head *tmp)
603{
604 struct cg_cgroup_link *link;
605 int i;
606 INIT_LIST_HEAD(tmp);
607 for (i = 0; i < count; i++) {
608 link = kmalloc(sizeof(*link), GFP_KERNEL);
609 if (!link) {
610 free_cg_links(tmp);
611 return -ENOMEM;
612 }
613 list_add(&link->cgrp_link_list, tmp);
614 }
615 return 0;
616}
617
Li Zefanc12f65d2009-01-07 18:07:42 -0800618/**
619 * link_css_set - a helper function to link a css_set to a cgroup
620 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
621 * @cg: the css_set to be linked
622 * @cgrp: the destination cgroup
623 */
624static void link_css_set(struct list_head *tmp_cg_links,
625 struct css_set *cg, struct cgroup *cgrp)
626{
627 struct cg_cgroup_link *link;
628
629 BUG_ON(list_empty(tmp_cg_links));
630 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
631 cgrp_link_list);
632 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700633 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700634 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800635 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700636 /*
637 * Always add links to the tail of the list so that the list
638 * is sorted by order of hierarchy creation
639 */
640 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800641}
642
Li Zefan36553432008-07-29 22:33:19 -0700643/*
Paul Menage817929e2007-10-18 23:39:36 -0700644 * find_css_set() takes an existing cgroup group and a
645 * cgroup object, and returns a css_set object that's
646 * equivalent to the old group, but with the given cgroup
647 * substituted into the appropriate hierarchy. Must be called with
648 * cgroup_mutex held
649 */
Paul Menage817929e2007-10-18 23:39:36 -0700650static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700651 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700652{
653 struct css_set *res;
654 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700655
656 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700657
Li Zefan472b1052008-04-29 01:00:11 -0700658 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700659 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700660
Paul Menage817929e2007-10-18 23:39:36 -0700661 /* First see if we already have a cgroup group that matches
662 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700663 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700664 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700665 if (res)
666 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700667 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700668
669 if (res)
670 return res;
671
672 res = kmalloc(sizeof(*res), GFP_KERNEL);
673 if (!res)
674 return NULL;
675
676 /* Allocate all the cg_cgroup_link objects that we'll need */
677 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
678 kfree(res);
679 return NULL;
680 }
681
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700682 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700683 INIT_LIST_HEAD(&res->cg_links);
684 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700685 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700686
687 /* Copy the set of subsystem state objects generated in
688 * find_existing_css_set() */
689 memcpy(res->subsys, template, sizeof(res->subsys));
690
691 write_lock(&css_set_lock);
692 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700693 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
694 struct cgroup *c = link->cgrp;
695 if (c->root == cgrp->root)
696 c = cgrp;
697 link_css_set(&tmp_cg_links, res, c);
698 }
Paul Menage817929e2007-10-18 23:39:36 -0700699
700 BUG_ON(!list_empty(&tmp_cg_links));
701
Paul Menage817929e2007-10-18 23:39:36 -0700702 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700703
704 /* Add this cgroup group to the hash table */
705 hhead = css_set_hash(res->subsys);
706 hlist_add_head(&res->hlist, hhead);
707
Paul Menage817929e2007-10-18 23:39:36 -0700708 write_unlock(&css_set_lock);
709
710 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700711}
712
Paul Menageddbcc7e2007-10-18 23:39:30 -0700713/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700714 * Return the cgroup for "task" from the given hierarchy. Must be
715 * called with cgroup_mutex held.
716 */
717static struct cgroup *task_cgroup_from_root(struct task_struct *task,
718 struct cgroupfs_root *root)
719{
720 struct css_set *css;
721 struct cgroup *res = NULL;
722
723 BUG_ON(!mutex_is_locked(&cgroup_mutex));
724 read_lock(&css_set_lock);
725 /*
726 * No need to lock the task - since we hold cgroup_mutex the
727 * task can't change groups, so the only thing that can happen
728 * is that it exits and its css is set back to init_css_set.
729 */
730 css = task->cgroups;
731 if (css == &init_css_set) {
732 res = &root->top_cgroup;
733 } else {
734 struct cg_cgroup_link *link;
735 list_for_each_entry(link, &css->cg_links, cg_link_list) {
736 struct cgroup *c = link->cgrp;
737 if (c->root == root) {
738 res = c;
739 break;
740 }
741 }
742 }
743 read_unlock(&css_set_lock);
744 BUG_ON(!res);
745 return res;
746}
747
748/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700749 * There is one global cgroup mutex. We also require taking
750 * task_lock() when dereferencing a task's cgroup subsys pointers.
751 * See "The task_lock() exception", at the end of this comment.
752 *
753 * A task must hold cgroup_mutex to modify cgroups.
754 *
755 * Any task can increment and decrement the count field without lock.
756 * So in general, code holding cgroup_mutex can't rely on the count
757 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800758 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700759 * means that no tasks are currently attached, therefore there is no
760 * way a task attached to that cgroup can fork (the other way to
761 * increment the count). So code holding cgroup_mutex can safely
762 * assume that if the count is zero, it will stay zero. Similarly, if
763 * a task holds cgroup_mutex on a cgroup with zero count, it
764 * knows that the cgroup won't be removed, as cgroup_rmdir()
765 * needs that mutex.
766 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700767 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
768 * (usually) take cgroup_mutex. These are the two most performance
769 * critical pieces of code here. The exception occurs on cgroup_exit(),
770 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
771 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800772 * to the release agent with the name of the cgroup (path relative to
773 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700774 *
775 * A cgroup can only be deleted if both its 'count' of using tasks
776 * is zero, and its list of 'children' cgroups is empty. Since all
777 * tasks in the system use _some_ cgroup, and since there is always at
778 * least one task in the system (init, pid == 1), therefore, top_cgroup
779 * always has either children cgroups and/or using tasks. So we don't
780 * need a special hack to ensure that top_cgroup cannot be deleted.
781 *
782 * The task_lock() exception
783 *
784 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800785 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800786 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700787 * several performance critical places that need to reference
788 * task->cgroup without the expense of grabbing a system global
789 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800790 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
792 * the task_struct routinely used for such matters.
793 *
794 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800795 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 */
797
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798/**
799 * cgroup_lock - lock out any changes to cgroup structures
800 *
801 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700802void cgroup_lock(void)
803{
804 mutex_lock(&cgroup_mutex);
805}
Ben Blum67523c42010-03-10 15:22:11 -0800806EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807
808/**
809 * cgroup_unlock - release lock on cgroup changes
810 *
811 * Undo the lock taken in a previous cgroup_lock() call.
812 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700813void cgroup_unlock(void)
814{
815 mutex_unlock(&cgroup_mutex);
816}
Ben Blum67523c42010-03-10 15:22:11 -0800817EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700818
819/*
820 * A couple of forward declarations required, due to cyclic reference loop:
821 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
822 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
823 * -> cgroup_mkdir.
824 */
825
Al Viro18bb1db2011-07-26 01:41:39 -0400826static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400827static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700828static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400829static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
830 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700831static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700832static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700833
834static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200835 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700836 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700837};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700838
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700839static int alloc_css_id(struct cgroup_subsys *ss,
840 struct cgroup *parent, struct cgroup *child);
841
Al Viroa5e7ed32011-07-26 01:55:55 -0400842static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700843{
844 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845
846 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400847 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100849 inode->i_uid = current_fsuid();
850 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700851 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
852 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
853 }
854 return inode;
855}
856
857static void cgroup_diput(struct dentry *dentry, struct inode *inode)
858{
859 /* is dentry a directory ? if so, kfree() associated cgroup */
860 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700861 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800862 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700863 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700864 /* It's possible for external users to be holding css
865 * reference counts on a cgroup; css_put() needs to
866 * be able to access the cgroup after decrementing
867 * the reference count in order to know if it needs to
868 * queue the cgroup to be handled by the release
869 * agent */
870 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800871
872 mutex_lock(&cgroup_mutex);
873 /*
874 * Release the subsystem state objects.
875 */
Li Zefan75139b82009-01-07 18:07:33 -0800876 for_each_subsys(cgrp->root, ss)
Tejun Heo92fb9742012-11-19 08:13:38 -0800877 ss->css_free(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800878
879 cgrp->root->number_of_cgroups--;
880 mutex_unlock(&cgroup_mutex);
881
Paul Menagea47295e2009-01-07 18:07:44 -0800882 /*
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700883 * Drop the active superblock reference that we took when we
884 * created the cgroup
Paul Menagea47295e2009-01-07 18:07:44 -0800885 */
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700886 deactivate_super(cgrp->root->sb);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800887
Ben Blum72a8cb32009-09-23 15:56:27 -0700888 /*
889 * if we're getting rid of the cgroup, refcount should ensure
890 * that there are no pidlists left.
891 */
892 BUG_ON(!list_empty(&cgrp->pidlists));
893
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400894 simple_xattrs_free(&cgrp->xattrs);
895
Tejun Heo0a950f62012-11-19 09:02:12 -0800896 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800897 kfree_rcu(cgrp, rcu_head);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700898 } else {
899 struct cfent *cfe = __d_cfe(dentry);
900 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400901 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700902
903 WARN_ONCE(!list_empty(&cfe->node) &&
904 cgrp != &cgrp->root->top_cgroup,
905 "cfe still linked for %s\n", cfe->type->name);
906 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400907 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700908 }
909 iput(inode);
910}
911
Al Viroc72a04e2011-01-14 05:31:45 +0000912static int cgroup_delete(const struct dentry *d)
913{
914 return 1;
915}
916
Paul Menageddbcc7e2007-10-18 23:39:30 -0700917static void remove_dir(struct dentry *d)
918{
919 struct dentry *parent = dget(d->d_parent);
920
921 d_delete(d);
922 simple_rmdir(parent->d_inode, d);
923 dput(parent);
924}
925
Tejun Heo05ef1d72012-04-01 12:09:56 -0700926static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700927{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700928 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700929
Tejun Heo05ef1d72012-04-01 12:09:56 -0700930 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
931 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100932
Tejun Heo05ef1d72012-04-01 12:09:56 -0700933 list_for_each_entry(cfe, &cgrp->files, node) {
934 struct dentry *d = cfe->dentry;
935
936 if (cft && cfe->type != cft)
937 continue;
938
939 dget(d);
940 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700941 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942 list_del_init(&cfe->node);
943 dput(d);
944
945 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700946 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700947 return -ENOENT;
948}
949
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400950/**
951 * cgroup_clear_directory - selective removal of base and subsystem files
952 * @dir: directory containing the files
953 * @base_files: true if the base files should be removed
954 * @subsys_mask: mask of the subsystem ids whose files should be removed
955 */
956static void cgroup_clear_directory(struct dentry *dir, bool base_files,
957 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700958{
959 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400960 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700961
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400962 for_each_subsys(cgrp->root, ss) {
963 struct cftype_set *set;
964 if (!test_bit(ss->subsys_id, &subsys_mask))
965 continue;
966 list_for_each_entry(set, &ss->cftsets, node)
967 cgroup_rm_file(cgrp, set->cfts);
968 }
969 if (base_files) {
970 while (!list_empty(&cgrp->files))
971 cgroup_rm_file(cgrp, NULL);
972 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973}
974
975/*
976 * NOTE : the dentry must have been dget()'ed
977 */
978static void cgroup_d_remove_dir(struct dentry *dentry)
979{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100980 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400981 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100982
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400983 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100985 parent = dentry->d_parent;
986 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800987 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100989 spin_unlock(&dentry->d_lock);
990 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 remove_dir(dentry);
992}
993
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700994/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800995 * Call with cgroup_mutex held. Drops reference counts on modules, including
996 * any duplicate ones that parse_cgroupfs_options took. If this function
997 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800998 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001000 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001002 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001003 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004 int i;
1005
Ben Blumaae8aab2010-03-10 15:22:07 -08001006 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001007 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001008
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001009 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1010 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001011 /* Check that any added subsystems are currently free */
1012 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001013 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001015 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001017 /*
1018 * Nobody should tell us to do a subsys that doesn't exist:
1019 * parse_cgroupfs_options should catch that case and refcounts
1020 * ensure that subsystems won't disappear once selected.
1021 */
1022 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023 if (ss->root != &rootnode) {
1024 /* Subsystem isn't free */
1025 return -EBUSY;
1026 }
1027 }
1028
1029 /* Currently we don't handle adding/removing subsystems when
1030 * any child cgroups exist. This is theoretically supportable
1031 * but involves complex error handling, so it's being left until
1032 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001033 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034 return -EBUSY;
1035
1036 /* Process each subsystem */
1037 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1038 struct cgroup_subsys *ss = subsys[i];
1039 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001040 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001041 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001042 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001043 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044 BUG_ON(!dummytop->subsys[i]);
1045 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001046 cgrp->subsys[i] = dummytop->subsys[i];
1047 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001048 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001049 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001051 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001052 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001053 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001055 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001056 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1057 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001059 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001061 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001062 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001063 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001064 /* subsystem is now free - drop reference on module */
1065 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001066 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001068 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001069 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001070 /*
1071 * a refcount was taken, but we already had one, so
1072 * drop the extra reference.
1073 */
1074 module_put(ss->module);
1075#ifdef CONFIG_MODULE_UNLOAD
1076 BUG_ON(ss->module && !module_refcount(ss->module));
1077#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 } else {
1079 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001080 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 }
1082 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001083 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084 synchronize_rcu();
1085
1086 return 0;
1087}
1088
Al Viro34c80b12011-12-08 21:32:45 -05001089static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090{
Al Viro34c80b12011-12-08 21:32:45 -05001091 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 struct cgroup_subsys *ss;
1093
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001094 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 for_each_subsys(root, ss)
1096 seq_printf(seq, ",%s", ss->name);
1097 if (test_bit(ROOT_NOPREFIX, &root->flags))
1098 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001099 if (test_bit(ROOT_XATTR, &root->flags))
1100 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001101 if (strlen(root->release_agent_path))
1102 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001103 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001104 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001105 if (strlen(root->name))
1106 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001107 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108 return 0;
1109}
1110
1111struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001112 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001113 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001114 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001115 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001116 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001117 /* User explicitly requested empty subsystem */
1118 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001119
1120 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001121
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122};
1123
Ben Blumaae8aab2010-03-10 15:22:07 -08001124/*
1125 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001126 * with cgroup_mutex held to protect the subsys[] array. This function takes
1127 * refcounts on subsystems to be used, unless it returns error, in which case
1128 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001129 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001130static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001132 char *token, *o = data;
1133 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001134 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001135 int i;
1136 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001137
Ben Blumaae8aab2010-03-10 15:22:07 -08001138 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1139
Li Zefanf9ab5b52009-06-17 16:26:33 -07001140#ifdef CONFIG_CPUSETS
1141 mask = ~(1UL << cpuset_subsys_id);
1142#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143
Paul Menagec6d57f32009-09-23 15:56:19 -07001144 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001145
1146 while ((token = strsep(&o, ",")) != NULL) {
1147 if (!*token)
1148 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001149 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001150 /* Explicitly have no subsystems */
1151 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001152 continue;
1153 }
1154 if (!strcmp(token, "all")) {
1155 /* Mutually exclusive option 'all' + subsystem name */
1156 if (one_ss)
1157 return -EINVAL;
1158 all_ss = true;
1159 continue;
1160 }
1161 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001162 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 continue;
1164 }
1165 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001166 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001167 continue;
1168 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001169 if (!strcmp(token, "xattr")) {
1170 set_bit(ROOT_XATTR, &opts->flags);
1171 continue;
1172 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001173 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001174 /* Specifying two release agents is forbidden */
1175 if (opts->release_agent)
1176 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001177 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001178 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001179 if (!opts->release_agent)
1180 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001181 continue;
1182 }
1183 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001184 const char *name = token + 5;
1185 /* Can't specify an empty name */
1186 if (!strlen(name))
1187 return -EINVAL;
1188 /* Must match [\w.-]+ */
1189 for (i = 0; i < strlen(name); i++) {
1190 char c = name[i];
1191 if (isalnum(c))
1192 continue;
1193 if ((c == '.') || (c == '-') || (c == '_'))
1194 continue;
1195 return -EINVAL;
1196 }
1197 /* Specifying two names is forbidden */
1198 if (opts->name)
1199 return -EINVAL;
1200 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001201 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001202 GFP_KERNEL);
1203 if (!opts->name)
1204 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001205
1206 continue;
1207 }
1208
1209 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1210 struct cgroup_subsys *ss = subsys[i];
1211 if (ss == NULL)
1212 continue;
1213 if (strcmp(token, ss->name))
1214 continue;
1215 if (ss->disabled)
1216 continue;
1217
1218 /* Mutually exclusive option 'all' + subsystem name */
1219 if (all_ss)
1220 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001221 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001222 one_ss = true;
1223
1224 break;
1225 }
1226 if (i == CGROUP_SUBSYS_COUNT)
1227 return -ENOENT;
1228 }
1229
1230 /*
1231 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001232 * otherwise if 'none', 'name=' and a subsystem name options
1233 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001234 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001235 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001236 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1237 struct cgroup_subsys *ss = subsys[i];
1238 if (ss == NULL)
1239 continue;
1240 if (ss->disabled)
1241 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001242 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243 }
1244 }
1245
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001246 /* Consistency checks */
1247
Li Zefanf9ab5b52009-06-17 16:26:33 -07001248 /*
1249 * Option noprefix was introduced just for backward compatibility
1250 * with the old cpuset, so we allow noprefix only if mounting just
1251 * the cpuset subsystem.
1252 */
1253 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001254 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001255 return -EINVAL;
1256
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001257
1258 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001259 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001260 return -EINVAL;
1261
1262 /*
1263 * We either have to specify by name or by subsystems. (So all
1264 * empty hierarchies must have a name).
1265 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001266 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001267 return -EINVAL;
1268
Ben Blumcf5d5942010-03-10 15:22:09 -08001269 /*
1270 * Grab references on all the modules we'll need, so the subsystems
1271 * don't dance around before rebind_subsystems attaches them. This may
1272 * take duplicate reference counts on a subsystem that's already used,
1273 * but rebind_subsystems handles this case.
1274 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001275 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001276 unsigned long bit = 1UL << i;
1277
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001278 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001279 continue;
1280 if (!try_module_get(subsys[i]->module)) {
1281 module_pin_failed = true;
1282 break;
1283 }
1284 }
1285 if (module_pin_failed) {
1286 /*
1287 * oops, one of the modules was going away. this means that we
1288 * raced with a module_delete call, and to the user this is
1289 * essentially a "subsystem doesn't exist" case.
1290 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001291 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001292 /* drop refcounts only on the ones we took */
1293 unsigned long bit = 1UL << i;
1294
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001295 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001296 continue;
1297 module_put(subsys[i]->module);
1298 }
1299 return -ENOENT;
1300 }
1301
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302 return 0;
1303}
1304
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001305static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001306{
1307 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001308 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 unsigned long bit = 1UL << i;
1310
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001311 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001312 continue;
1313 module_put(subsys[i]->module);
1314 }
1315}
1316
Paul Menageddbcc7e2007-10-18 23:39:30 -07001317static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1318{
1319 int ret = 0;
1320 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001321 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001323 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324
Paul Menagebd89aab2007-10-18 23:40:44 -07001325 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001327 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328
1329 /* See what subsystems are wanted */
1330 ret = parse_cgroupfs_options(data, &opts);
1331 if (ret)
1332 goto out_unlock;
1333
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001334 /* See feature-removal-schedule.txt */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001335 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001336 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1337 task_tgid_nr(current), current->comm);
1338
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001339 added_mask = opts.subsys_mask & ~root->subsys_mask;
1340 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001341
Ben Blumcf5d5942010-03-10 15:22:09 -08001342 /* Don't allow flags or name to change at remount */
1343 if (opts.flags != root->flags ||
1344 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001345 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001346 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001347 goto out_unlock;
1348 }
1349
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001350 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001351 if (ret) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001352 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001353 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001354 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001355
Tejun Heoff4c8d52012-04-01 12:09:54 -07001356 /* clear out any existing files and repopulate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001357 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001358 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001359 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001360
Paul Menage81a6a5c2007-10-18 23:39:38 -07001361 if (opts.release_agent)
1362 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001364 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001365 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001366 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001368 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369 return ret;
1370}
1371
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001372static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001373 .statfs = simple_statfs,
1374 .drop_inode = generic_delete_inode,
1375 .show_options = cgroup_show_options,
1376 .remount_fs = cgroup_remount,
1377};
1378
Paul Menagecc31edc2008-10-18 20:28:04 -07001379static void init_cgroup_housekeeping(struct cgroup *cgrp)
1380{
1381 INIT_LIST_HEAD(&cgrp->sibling);
1382 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001383 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001384 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001385 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001386 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001387 INIT_LIST_HEAD(&cgrp->pidlists);
1388 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001389 INIT_LIST_HEAD(&cgrp->event_list);
1390 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001391 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001392}
Paul Menagec6d57f32009-09-23 15:56:19 -07001393
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394static void init_cgroup_root(struct cgroupfs_root *root)
1395{
Paul Menagebd89aab2007-10-18 23:40:44 -07001396 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001397
Paul Menageddbcc7e2007-10-18 23:39:30 -07001398 INIT_LIST_HEAD(&root->subsys_list);
1399 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001400 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001402 cgrp->root = root;
1403 cgrp->top_cgroup = cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001404 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menagecc31edc2008-10-18 20:28:04 -07001405 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406}
1407
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001408static bool init_root_id(struct cgroupfs_root *root)
1409{
1410 int ret = 0;
1411
1412 do {
1413 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1414 return false;
1415 spin_lock(&hierarchy_id_lock);
1416 /* Try to allocate the next unused ID */
1417 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1418 &root->hierarchy_id);
1419 if (ret == -ENOSPC)
1420 /* Try again starting from 0 */
1421 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1422 if (!ret) {
1423 next_hierarchy_id = root->hierarchy_id + 1;
1424 } else if (ret != -EAGAIN) {
1425 /* Can only get here if the 31-bit IDR is full ... */
1426 BUG_ON(ret);
1427 }
1428 spin_unlock(&hierarchy_id_lock);
1429 } while (ret);
1430 return true;
1431}
1432
Paul Menageddbcc7e2007-10-18 23:39:30 -07001433static int cgroup_test_super(struct super_block *sb, void *data)
1434{
Paul Menagec6d57f32009-09-23 15:56:19 -07001435 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436 struct cgroupfs_root *root = sb->s_fs_info;
1437
Paul Menagec6d57f32009-09-23 15:56:19 -07001438 /* If we asked for a name then it must match */
1439 if (opts->name && strcmp(opts->name, root->name))
1440 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001441
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001442 /*
1443 * If we asked for subsystems (or explicitly for no
1444 * subsystems) then they must match
1445 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001446 if ((opts->subsys_mask || opts->none)
1447 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001448 return 0;
1449
1450 return 1;
1451}
1452
Paul Menagec6d57f32009-09-23 15:56:19 -07001453static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1454{
1455 struct cgroupfs_root *root;
1456
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001457 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001458 return NULL;
1459
1460 root = kzalloc(sizeof(*root), GFP_KERNEL);
1461 if (!root)
1462 return ERR_PTR(-ENOMEM);
1463
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001464 if (!init_root_id(root)) {
1465 kfree(root);
1466 return ERR_PTR(-ENOMEM);
1467 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001468 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001469
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001470 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001471 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001472 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001473 if (opts->release_agent)
1474 strcpy(root->release_agent_path, opts->release_agent);
1475 if (opts->name)
1476 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001477 if (opts->cpuset_clone_children)
1478 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001479 return root;
1480}
1481
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001482static void cgroup_drop_root(struct cgroupfs_root *root)
1483{
1484 if (!root)
1485 return;
1486
1487 BUG_ON(!root->hierarchy_id);
1488 spin_lock(&hierarchy_id_lock);
1489 ida_remove(&hierarchy_ida, root->hierarchy_id);
1490 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001491 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001492 kfree(root);
1493}
1494
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495static int cgroup_set_super(struct super_block *sb, void *data)
1496{
1497 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001498 struct cgroup_sb_opts *opts = data;
1499
1500 /* If we don't have a new root, we can't set up a new sb */
1501 if (!opts->new_root)
1502 return -EINVAL;
1503
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001504 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505
1506 ret = set_anon_super(sb, NULL);
1507 if (ret)
1508 return ret;
1509
Paul Menagec6d57f32009-09-23 15:56:19 -07001510 sb->s_fs_info = opts->new_root;
1511 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512
1513 sb->s_blocksize = PAGE_CACHE_SIZE;
1514 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1515 sb->s_magic = CGROUP_SUPER_MAGIC;
1516 sb->s_op = &cgroup_ops;
1517
1518 return 0;
1519}
1520
1521static int cgroup_get_rootdir(struct super_block *sb)
1522{
Al Viro0df6a632010-12-21 13:29:29 -05001523 static const struct dentry_operations cgroup_dops = {
1524 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001525 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001526 };
1527
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528 struct inode *inode =
1529 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530
1531 if (!inode)
1532 return -ENOMEM;
1533
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534 inode->i_fop = &simple_dir_operations;
1535 inode->i_op = &cgroup_dir_inode_operations;
1536 /* directories start off with i_nlink == 2 (for "." entry) */
1537 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001538 sb->s_root = d_make_root(inode);
1539 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001540 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001541 /* for everything else we want ->d_op set */
1542 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 return 0;
1544}
1545
Al Virof7e83572010-07-26 13:23:11 +04001546static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001548 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549{
1550 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001551 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 int ret = 0;
1553 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001554 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001555 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556
1557 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001558 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001560 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001561 if (ret)
1562 goto out_err;
1563
1564 /*
1565 * Allocate a new cgroup root. We may not need it if we're
1566 * reusing an existing hierarchy.
1567 */
1568 new_root = cgroup_root_from_opts(&opts);
1569 if (IS_ERR(new_root)) {
1570 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001571 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001572 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001573 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574
Paul Menagec6d57f32009-09-23 15:56:19 -07001575 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001576 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001578 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001579 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001580 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581 }
1582
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 root = sb->s_fs_info;
1584 BUG_ON(!root);
1585 if (root == opts.new_root) {
1586 /* We used the new root structure, so this is a new hierarchy */
1587 struct list_head tmp_cg_links;
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;
eparis@redhat2ce97382011-06-02 21:20:51 +10001590 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001591 int i;
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
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001604 /* Check for name clashes with existing mounts */
1605 ret = -EBUSY;
1606 if (strlen(root->name))
1607 for_each_active_root(existing_root)
1608 if (!strcmp(existing_root->name, root->name))
1609 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001610
Paul Menage817929e2007-10-18 23:39:36 -07001611 /*
1612 * We're accessing css_set_count without locking
1613 * css_set_lock here, but that's OK - it can only be
1614 * increased by someone holding cgroup_lock, and
1615 * that's us. The worst that can happen is that we
1616 * have some link structures left over
1617 */
1618 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001619 if (ret)
1620 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001621
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001622 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001624 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001625 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001627 /*
1628 * There must be no failure case after here, since rebinding
1629 * takes care of subsystems' refcounts, which are explicitly
1630 * dropped in the failure exit path.
1631 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632
1633 /* EBUSY should be the only error here */
1634 BUG_ON(ret);
1635
1636 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001637 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638
Li Zefanc12f65d2009-01-07 18:07:42 -08001639 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640 root->top_cgroup.dentry = sb->s_root;
1641
Paul Menage817929e2007-10-18 23:39:36 -07001642 /* Link the top cgroup in this hierarchy into all
1643 * the css_set objects */
1644 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001645 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1646 struct hlist_head *hhead = &css_set_table[i];
1647 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001648 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001649
Li Zefanc12f65d2009-01-07 18:07:42 -08001650 hlist_for_each_entry(cg, node, hhead, hlist)
1651 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001652 }
Paul Menage817929e2007-10-18 23:39:36 -07001653 write_unlock(&css_set_lock);
1654
1655 free_cg_links(&tmp_cg_links);
1656
Li Zefanc12f65d2009-01-07 18:07:42 -08001657 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001658 BUG_ON(root->number_of_cgroups != 1);
1659
eparis@redhat2ce97382011-06-02 21:20:51 +10001660 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001661 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001662 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001663 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001665 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001666 } else {
1667 /*
1668 * We re-used an existing hierarchy - the new root (if
1669 * any) is not needed
1670 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001671 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001672 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001673 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 }
1675
Paul Menagec6d57f32009-09-23 15:56:19 -07001676 kfree(opts.release_agent);
1677 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001678 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001679
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001680 unlock_drop:
1681 mutex_unlock(&cgroup_root_mutex);
1682 mutex_unlock(&cgroup_mutex);
1683 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001685 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001686 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001687 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001688 out_err:
1689 kfree(opts.release_agent);
1690 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001691 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692}
1693
1694static void cgroup_kill_sb(struct super_block *sb) {
1695 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001696 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001698 struct cg_cgroup_link *link;
1699 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700
1701 BUG_ON(!root);
1702
1703 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001704 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705
1706 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001707 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708
1709 /* Rebind all subsystems back to the default hierarchy */
1710 ret = rebind_subsystems(root, 0);
1711 /* Shouldn't be able to fail ... */
1712 BUG_ON(ret);
1713
Paul Menage817929e2007-10-18 23:39:36 -07001714 /*
1715 * Release all the links from css_sets to this hierarchy's
1716 * root cgroup
1717 */
1718 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001719
1720 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1721 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001722 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001723 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001724 kfree(link);
1725 }
1726 write_unlock(&css_set_lock);
1727
Paul Menage839ec542009-01-29 14:25:22 -08001728 if (!list_empty(&root->root_list)) {
1729 list_del(&root->root_list);
1730 root_count--;
1731 }
Li Zefane5f6a862009-01-07 18:07:41 -08001732
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001733 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734 mutex_unlock(&cgroup_mutex);
1735
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001736 simple_xattrs_free(&cgrp->xattrs);
1737
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001739 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740}
1741
1742static struct file_system_type cgroup_fs_type = {
1743 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001744 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001745 .kill_sb = cgroup_kill_sb,
1746};
1747
Greg KH676db4a2010-08-05 13:53:35 -07001748static struct kobject *cgroup_kobj;
1749
Li Zefana043e3b2008-02-23 15:24:09 -08001750/**
1751 * cgroup_path - generate the path of a cgroup
1752 * @cgrp: the cgroup in question
1753 * @buf: the buffer to write the path into
1754 * @buflen: the length of the buffer
1755 *
Paul Menagea47295e2009-01-07 18:07:44 -08001756 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1757 * reference. Writes path of cgroup into buf. Returns 0 on success,
1758 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001760int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761{
Tejun Heofebfcef2012-11-19 08:13:36 -08001762 struct dentry *dentry = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001764
1765 rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1766 "cgroup_path() called without proper locking");
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767
Paul Menagea47295e2009-01-07 18:07:44 -08001768 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769 /*
1770 * Inactive subsystems have no dentry for their root
1771 * cgroup
1772 */
1773 strcpy(buf, "/");
1774 return 0;
1775 }
1776
Tao Ma316eb662012-11-08 21:36:38 +08001777 start = buf + buflen - 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778
Tao Ma316eb662012-11-08 21:36:38 +08001779 *start = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001781 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001782
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783 if ((start -= len) < buf)
1784 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001785 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001786 cgrp = cgrp->parent;
1787 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001789
Tejun Heofebfcef2012-11-19 08:13:36 -08001790 dentry = cgrp->dentry;
Paul Menagebd89aab2007-10-18 23:40:44 -07001791 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792 continue;
1793 if (--start < buf)
1794 return -ENAMETOOLONG;
1795 *start = '/';
1796 }
1797 memmove(buf, start, buf + buflen - start);
1798 return 0;
1799}
Ben Blum67523c42010-03-10 15:22:11 -08001800EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801
Ben Blum74a11662011-05-26 16:25:20 -07001802/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001803 * Control Group taskset
1804 */
Tejun Heo134d3372011-12-12 18:12:21 -08001805struct task_and_cgroup {
1806 struct task_struct *task;
1807 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001808 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001809};
1810
Tejun Heo2f7ee562011-12-12 18:12:21 -08001811struct cgroup_taskset {
1812 struct task_and_cgroup single;
1813 struct flex_array *tc_array;
1814 int tc_array_len;
1815 int idx;
1816 struct cgroup *cur_cgrp;
1817};
1818
1819/**
1820 * cgroup_taskset_first - reset taskset and return the first task
1821 * @tset: taskset of interest
1822 *
1823 * @tset iteration is initialized and the first task is returned.
1824 */
1825struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1826{
1827 if (tset->tc_array) {
1828 tset->idx = 0;
1829 return cgroup_taskset_next(tset);
1830 } else {
1831 tset->cur_cgrp = tset->single.cgrp;
1832 return tset->single.task;
1833 }
1834}
1835EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1836
1837/**
1838 * cgroup_taskset_next - iterate to the next task in taskset
1839 * @tset: taskset of interest
1840 *
1841 * Return the next task in @tset. Iteration must have been initialized
1842 * with cgroup_taskset_first().
1843 */
1844struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1845{
1846 struct task_and_cgroup *tc;
1847
1848 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1849 return NULL;
1850
1851 tc = flex_array_get(tset->tc_array, tset->idx++);
1852 tset->cur_cgrp = tc->cgrp;
1853 return tc->task;
1854}
1855EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1856
1857/**
1858 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1859 * @tset: taskset of interest
1860 *
1861 * Return the cgroup for the current (last returned) task of @tset. This
1862 * function must be preceded by either cgroup_taskset_first() or
1863 * cgroup_taskset_next().
1864 */
1865struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1866{
1867 return tset->cur_cgrp;
1868}
1869EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1870
1871/**
1872 * cgroup_taskset_size - return the number of tasks in taskset
1873 * @tset: taskset of interest
1874 */
1875int cgroup_taskset_size(struct cgroup_taskset *tset)
1876{
1877 return tset->tc_array ? tset->tc_array_len : 1;
1878}
1879EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1880
1881
Ben Blum74a11662011-05-26 16:25:20 -07001882/*
1883 * cgroup_task_migrate - move a task from one cgroup to another.
1884 *
1885 * 'guarantee' is set if the caller promises that a new css_set for the task
1886 * will already exist. If not set, this function might sleep, and can fail with
Tejun Heocd3d0952011-12-12 18:12:21 -08001887 * -ENOMEM. Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001888 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001889static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1890 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001891{
1892 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001893
1894 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001895 * We are synchronized through threadgroup_lock() against PF_EXITING
1896 * setting such that we can't race against cgroup_exit() changing the
1897 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001898 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001899 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001900 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001901
Ben Blum74a11662011-05-26 16:25:20 -07001902 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001903 rcu_assign_pointer(tsk->cgroups, newcg);
1904 task_unlock(tsk);
1905
1906 /* Update the css_set linked lists if we're using them */
1907 write_lock(&css_set_lock);
1908 if (!list_empty(&tsk->cg_list))
1909 list_move(&tsk->cg_list, &newcg->tasks);
1910 write_unlock(&css_set_lock);
1911
1912 /*
1913 * We just gained a reference on oldcg by taking it from the task. As
1914 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1915 * it here; it will be freed under RCU.
1916 */
Ben Blum74a11662011-05-26 16:25:20 -07001917 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001918 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001919}
1920
Li Zefana043e3b2008-02-23 15:24:09 -08001921/**
1922 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1923 * @cgrp: the cgroup the task is attaching to
1924 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001925 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001926 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1927 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001928 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001929int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001930{
Tejun Heo8f121912012-03-29 22:03:33 -07001931 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001932 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001933 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001934 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001935 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001936 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001937
Tejun Heocd3d0952011-12-12 18:12:21 -08001938 /* @tsk either already exited or can't exit until the end */
1939 if (tsk->flags & PF_EXITING)
1940 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001941
1942 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001943 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001944 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001945 return 0;
1946
Tejun Heo2f7ee562011-12-12 18:12:21 -08001947 tset.single.task = tsk;
1948 tset.single.cgrp = oldcgrp;
1949
Paul Menagebbcb81d2007-10-18 23:39:32 -07001950 for_each_subsys(root, ss) {
1951 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001952 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001953 if (retval) {
1954 /*
1955 * Remember on which subsystem the can_attach()
1956 * failed, so that we only call cancel_attach()
1957 * against the subsystems whose can_attach()
1958 * succeeded. (See below)
1959 */
1960 failed_ss = ss;
1961 goto out;
1962 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001963 }
1964 }
1965
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001966 newcg = find_css_set(tsk->cgroups, cgrp);
1967 if (!newcg) {
1968 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001969 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001970 }
1971
1972 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001973
Paul Menagebbcb81d2007-10-18 23:39:32 -07001974 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001975 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001976 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001977 }
Ben Blum74a11662011-05-26 16:25:20 -07001978
Paul Menagebbcb81d2007-10-18 23:39:32 -07001979 synchronize_rcu();
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001980out:
1981 if (retval) {
1982 for_each_subsys(root, ss) {
1983 if (ss == failed_ss)
1984 /*
1985 * This subsystem was the one that failed the
1986 * can_attach() check earlier, so we don't need
1987 * to call cancel_attach() against it or any
1988 * remaining subsystems.
1989 */
1990 break;
1991 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001992 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001993 }
1994 }
1995 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001996}
1997
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001998/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001999 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2000 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002001 * @tsk: the task to be attached
2002 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002003int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002004{
2005 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002006 int retval = 0;
2007
2008 cgroup_lock();
2009 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002010 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2011
2012 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002013 if (retval)
2014 break;
2015 }
2016 cgroup_unlock();
2017
2018 return retval;
2019}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002020EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002021
Ben Blum74a11662011-05-26 16:25:20 -07002022/**
2023 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2024 * @cgrp: the cgroup to attach to
2025 * @leader: the threadgroup leader task_struct of the group to be attached
2026 *
Tejun Heo257058a2011-12-12 18:12:21 -08002027 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2028 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002029 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002030static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002031{
2032 int retval, i, group_size;
2033 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002034 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002035 struct cgroupfs_root *root = cgrp->root;
2036 /* threadgroup list cursor and array */
2037 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002038 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002039 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002040 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002041
2042 /*
2043 * step 0: in order to do expensive, possibly blocking operations for
2044 * every thread, we cannot iterate the thread group list, since it needs
2045 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002046 * group - group_rwsem prevents new threads from appearing, and if
2047 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002048 */
2049 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002050 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002051 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002052 if (!group)
2053 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002054 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2055 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2056 if (retval)
2057 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002058
Ben Blum74a11662011-05-26 16:25:20 -07002059 tsk = leader;
2060 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002061 /*
2062 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2063 * already PF_EXITING could be freed from underneath us unless we
2064 * take an rcu_read_lock.
2065 */
2066 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002067 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002068 struct task_and_cgroup ent;
2069
Tejun Heocd3d0952011-12-12 18:12:21 -08002070 /* @tsk either already exited or can't exit until the end */
2071 if (tsk->flags & PF_EXITING)
2072 continue;
2073
Ben Blum74a11662011-05-26 16:25:20 -07002074 /* as per above, nr_threads may decrease, but not increase. */
2075 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002076 ent.task = tsk;
2077 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002078 /* nothing to do if this task is already in the cgroup */
2079 if (ent.cgrp == cgrp)
2080 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002081 /*
2082 * saying GFP_ATOMIC has no effect here because we did prealloc
2083 * earlier, but it's good form to communicate our expectations.
2084 */
Tejun Heo134d3372011-12-12 18:12:21 -08002085 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002086 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002087 i++;
2088 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002089 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002090 /* remember the number of threads in the array for later. */
2091 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002092 tset.tc_array = group;
2093 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002094
Tejun Heo134d3372011-12-12 18:12:21 -08002095 /* methods shouldn't be called if no task is actually migrating */
2096 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002097 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002098 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002099
Ben Blum74a11662011-05-26 16:25:20 -07002100 /*
2101 * step 1: check that we can legitimately attach to the cgroup.
2102 */
2103 for_each_subsys(root, ss) {
2104 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002105 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002106 if (retval) {
2107 failed_ss = ss;
2108 goto out_cancel_attach;
2109 }
2110 }
Ben Blum74a11662011-05-26 16:25:20 -07002111 }
2112
2113 /*
2114 * step 2: make sure css_sets exist for all threads to be migrated.
2115 * we use find_css_set, which allocates a new one if necessary.
2116 */
Ben Blum74a11662011-05-26 16:25:20 -07002117 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002118 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002119 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2120 if (!tc->cg) {
2121 retval = -ENOMEM;
2122 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002123 }
2124 }
2125
2126 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002127 * step 3: now that we're guaranteed success wrt the css_sets,
2128 * proceed to move all tasks to the new cgroup. There are no
2129 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002130 */
Ben Blum74a11662011-05-26 16:25:20 -07002131 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002132 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002133 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002134 }
2135 /* nothing is sensitive to fork() after this point. */
2136
2137 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002138 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002139 */
2140 for_each_subsys(root, ss) {
2141 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002142 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002143 }
2144
2145 /*
2146 * step 5: success! and cleanup
2147 */
2148 synchronize_rcu();
Ben Blum74a11662011-05-26 16:25:20 -07002149 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002150out_put_css_set_refs:
2151 if (retval) {
2152 for (i = 0; i < group_size; i++) {
2153 tc = flex_array_get(group, i);
2154 if (!tc->cg)
2155 break;
2156 put_css_set(tc->cg);
2157 }
Ben Blum74a11662011-05-26 16:25:20 -07002158 }
2159out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002160 if (retval) {
2161 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002162 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002163 break;
Ben Blum74a11662011-05-26 16:25:20 -07002164 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002165 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002166 }
2167 }
Ben Blum74a11662011-05-26 16:25:20 -07002168out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002169 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002170 return retval;
2171}
2172
2173/*
2174 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002175 * function to attach either it or all tasks in its threadgroup. Will lock
2176 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002177 */
2178static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002179{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002180 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002181 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002182 int ret;
2183
Ben Blum74a11662011-05-26 16:25:20 -07002184 if (!cgroup_lock_live_group(cgrp))
2185 return -ENODEV;
2186
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002187retry_find_task:
2188 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002189 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002190 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002191 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002192 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002193 ret= -ESRCH;
2194 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002195 }
Ben Blum74a11662011-05-26 16:25:20 -07002196 /*
2197 * even if we're attaching all tasks in the thread group, we
2198 * only need to check permissions on one of them.
2199 */
David Howellsc69e8d92008-11-14 10:39:19 +11002200 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002201 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2202 !uid_eq(cred->euid, tcred->uid) &&
2203 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002204 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002205 ret = -EACCES;
2206 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002207 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002208 } else
2209 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002210
2211 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002212 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002213
2214 /*
2215 * Workqueue threads may acquire PF_THREAD_BOUND and become
2216 * trapped in a cpuset, or RT worker may be born in a cgroup
2217 * with no rt_runtime allocated. Just say no.
2218 */
2219 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2220 ret = -EINVAL;
2221 rcu_read_unlock();
2222 goto out_unlock_cgroup;
2223 }
2224
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002225 get_task_struct(tsk);
2226 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002227
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002228 threadgroup_lock(tsk);
2229 if (threadgroup) {
2230 if (!thread_group_leader(tsk)) {
2231 /*
2232 * a race with de_thread from another thread's exec()
2233 * may strip us of our leadership, if this happens,
2234 * there is no choice but to throw this task away and
2235 * try again; this is
2236 * "double-double-toil-and-trouble-check locking".
2237 */
2238 threadgroup_unlock(tsk);
2239 put_task_struct(tsk);
2240 goto retry_find_task;
2241 }
2242 ret = cgroup_attach_proc(cgrp, tsk);
2243 } else
2244 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002245 threadgroup_unlock(tsk);
2246
Paul Menagebbcb81d2007-10-18 23:39:32 -07002247 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002248out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002249 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002250 return ret;
2251}
2252
Paul Menageaf351022008-07-25 01:47:01 -07002253static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2254{
Ben Blum74a11662011-05-26 16:25:20 -07002255 return attach_task_by_pid(cgrp, pid, false);
2256}
2257
2258static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2259{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002260 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002261}
2262
Paul Menagee788e062008-07-25 01:46:59 -07002263/**
2264 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2265 * @cgrp: the cgroup to be checked for liveness
2266 *
Paul Menage84eea842008-07-25 01:47:00 -07002267 * On success, returns true; the lock should be later released with
2268 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002269 */
Paul Menage84eea842008-07-25 01:47:00 -07002270bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002271{
2272 mutex_lock(&cgroup_mutex);
2273 if (cgroup_is_removed(cgrp)) {
2274 mutex_unlock(&cgroup_mutex);
2275 return false;
2276 }
2277 return true;
2278}
Ben Blum67523c42010-03-10 15:22:11 -08002279EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002280
2281static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2282 const char *buffer)
2283{
2284 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002285 if (strlen(buffer) >= PATH_MAX)
2286 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002287 if (!cgroup_lock_live_group(cgrp))
2288 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002289 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002290 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002291 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002292 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002293 return 0;
2294}
2295
2296static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2297 struct seq_file *seq)
2298{
2299 if (!cgroup_lock_live_group(cgrp))
2300 return -ENODEV;
2301 seq_puts(seq, cgrp->root->release_agent_path);
2302 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002303 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002304 return 0;
2305}
2306
Paul Menage84eea842008-07-25 01:47:00 -07002307/* A buffer size big enough for numbers or short strings */
2308#define CGROUP_LOCAL_BUFFER_SIZE 64
2309
Paul Menagee73d2c62008-04-29 01:00:06 -07002310static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002311 struct file *file,
2312 const char __user *userbuf,
2313 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002314{
Paul Menage84eea842008-07-25 01:47:00 -07002315 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002316 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002317 char *end;
2318
2319 if (!nbytes)
2320 return -EINVAL;
2321 if (nbytes >= sizeof(buffer))
2322 return -E2BIG;
2323 if (copy_from_user(buffer, userbuf, nbytes))
2324 return -EFAULT;
2325
2326 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002327 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002328 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002329 if (*end)
2330 return -EINVAL;
2331 retval = cft->write_u64(cgrp, cft, val);
2332 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002333 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002334 if (*end)
2335 return -EINVAL;
2336 retval = cft->write_s64(cgrp, cft, val);
2337 }
Paul Menage355e0c42007-10-18 23:39:33 -07002338 if (!retval)
2339 retval = nbytes;
2340 return retval;
2341}
2342
Paul Menagedb3b1492008-07-25 01:46:58 -07002343static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2344 struct file *file,
2345 const char __user *userbuf,
2346 size_t nbytes, loff_t *unused_ppos)
2347{
Paul Menage84eea842008-07-25 01:47:00 -07002348 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002349 int retval = 0;
2350 size_t max_bytes = cft->max_write_len;
2351 char *buffer = local_buffer;
2352
2353 if (!max_bytes)
2354 max_bytes = sizeof(local_buffer) - 1;
2355 if (nbytes >= max_bytes)
2356 return -E2BIG;
2357 /* Allocate a dynamic buffer if we need one */
2358 if (nbytes >= sizeof(local_buffer)) {
2359 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2360 if (buffer == NULL)
2361 return -ENOMEM;
2362 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002363 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2364 retval = -EFAULT;
2365 goto out;
2366 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002367
2368 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002369 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002370 if (!retval)
2371 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002372out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002373 if (buffer != local_buffer)
2374 kfree(buffer);
2375 return retval;
2376}
2377
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2379 size_t nbytes, loff_t *ppos)
2380{
2381 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002382 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002383
Li Zefan75139b82009-01-07 18:07:33 -08002384 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002386 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002387 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002388 if (cft->write_u64 || cft->write_s64)
2389 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002390 if (cft->write_string)
2391 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002392 if (cft->trigger) {
2393 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2394 return ret ? ret : nbytes;
2395 }
Paul Menage355e0c42007-10-18 23:39:33 -07002396 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397}
2398
Paul Menagef4c753b2008-04-29 00:59:56 -07002399static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2400 struct file *file,
2401 char __user *buf, size_t nbytes,
2402 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002403{
Paul Menage84eea842008-07-25 01:47:00 -07002404 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002405 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002406 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2407
2408 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2409}
2410
Paul Menagee73d2c62008-04-29 01:00:06 -07002411static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2412 struct file *file,
2413 char __user *buf, size_t nbytes,
2414 loff_t *ppos)
2415{
Paul Menage84eea842008-07-25 01:47:00 -07002416 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002417 s64 val = cft->read_s64(cgrp, cft);
2418 int len = sprintf(tmp, "%lld\n", (long long) val);
2419
2420 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2421}
2422
Paul Menageddbcc7e2007-10-18 23:39:30 -07002423static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2424 size_t nbytes, loff_t *ppos)
2425{
2426 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002427 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428
Li Zefan75139b82009-01-07 18:07:33 -08002429 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430 return -ENODEV;
2431
2432 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002433 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002434 if (cft->read_u64)
2435 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002436 if (cft->read_s64)
2437 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002438 return -EINVAL;
2439}
2440
Paul Menage91796562008-04-29 01:00:01 -07002441/*
2442 * seqfile ops/methods for returning structured data. Currently just
2443 * supports string->u64 maps, but can be extended in future.
2444 */
2445
2446struct cgroup_seqfile_state {
2447 struct cftype *cft;
2448 struct cgroup *cgroup;
2449};
2450
2451static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2452{
2453 struct seq_file *sf = cb->state;
2454 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2455}
2456
2457static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2458{
2459 struct cgroup_seqfile_state *state = m->private;
2460 struct cftype *cft = state->cft;
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 };
2466 return cft->read_map(state->cgroup, cft, &cb);
2467 }
2468 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002469}
2470
Adrian Bunk96930a62008-07-25 19:46:21 -07002471static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002472{
2473 struct seq_file *seq = file->private_data;
2474 kfree(seq->private);
2475 return single_release(inode, file);
2476}
2477
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002478static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002479 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002480 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002481 .llseek = seq_lseek,
2482 .release = cgroup_seqfile_release,
2483};
2484
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485static int cgroup_file_open(struct inode *inode, struct file *file)
2486{
2487 int err;
2488 struct cftype *cft;
2489
2490 err = generic_file_open(inode, file);
2491 if (err)
2492 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002493 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002494
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002495 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002496 struct cgroup_seqfile_state *state =
2497 kzalloc(sizeof(*state), GFP_USER);
2498 if (!state)
2499 return -ENOMEM;
2500 state->cft = cft;
2501 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2502 file->f_op = &cgroup_seqfile_operations;
2503 err = single_open(file, cgroup_seqfile_show, state);
2504 if (err < 0)
2505 kfree(state);
2506 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002507 err = cft->open(inode, file);
2508 else
2509 err = 0;
2510
2511 return err;
2512}
2513
2514static int cgroup_file_release(struct inode *inode, struct file *file)
2515{
2516 struct cftype *cft = __d_cft(file->f_dentry);
2517 if (cft->release)
2518 return cft->release(inode, file);
2519 return 0;
2520}
2521
2522/*
2523 * cgroup_rename - Only allow simple rename of directories in place.
2524 */
2525static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2526 struct inode *new_dir, struct dentry *new_dentry)
2527{
2528 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2529 return -ENOTDIR;
2530 if (new_dentry->d_inode)
2531 return -EEXIST;
2532 if (old_dir != new_dir)
2533 return -EIO;
2534 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2535}
2536
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002537static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2538{
2539 if (S_ISDIR(dentry->d_inode->i_mode))
2540 return &__d_cgrp(dentry)->xattrs;
2541 else
2542 return &__d_cft(dentry)->xattrs;
2543}
2544
2545static inline int xattr_enabled(struct dentry *dentry)
2546{
2547 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2548 return test_bit(ROOT_XATTR, &root->flags);
2549}
2550
2551static bool is_valid_xattr(const char *name)
2552{
2553 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2554 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2555 return true;
2556 return false;
2557}
2558
2559static int cgroup_setxattr(struct dentry *dentry, const char *name,
2560 const void *val, size_t size, int flags)
2561{
2562 if (!xattr_enabled(dentry))
2563 return -EOPNOTSUPP;
2564 if (!is_valid_xattr(name))
2565 return -EINVAL;
2566 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2567}
2568
2569static int cgroup_removexattr(struct dentry *dentry, const char *name)
2570{
2571 if (!xattr_enabled(dentry))
2572 return -EOPNOTSUPP;
2573 if (!is_valid_xattr(name))
2574 return -EINVAL;
2575 return simple_xattr_remove(__d_xattrs(dentry), name);
2576}
2577
2578static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2579 void *buf, size_t size)
2580{
2581 if (!xattr_enabled(dentry))
2582 return -EOPNOTSUPP;
2583 if (!is_valid_xattr(name))
2584 return -EINVAL;
2585 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2586}
2587
2588static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2589{
2590 if (!xattr_enabled(dentry))
2591 return -EOPNOTSUPP;
2592 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2593}
2594
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002595static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002596 .read = cgroup_file_read,
2597 .write = cgroup_file_write,
2598 .llseek = generic_file_llseek,
2599 .open = cgroup_file_open,
2600 .release = cgroup_file_release,
2601};
2602
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002603static const struct inode_operations cgroup_file_inode_operations = {
2604 .setxattr = cgroup_setxattr,
2605 .getxattr = cgroup_getxattr,
2606 .listxattr = cgroup_listxattr,
2607 .removexattr = cgroup_removexattr,
2608};
2609
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002610static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002611 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002612 .mkdir = cgroup_mkdir,
2613 .rmdir = cgroup_rmdir,
2614 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002615 .setxattr = cgroup_setxattr,
2616 .getxattr = cgroup_getxattr,
2617 .listxattr = cgroup_listxattr,
2618 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002619};
2620
Al Viro00cd8dd2012-06-10 17:13:09 -04002621static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002622{
2623 if (dentry->d_name.len > NAME_MAX)
2624 return ERR_PTR(-ENAMETOOLONG);
2625 d_add(dentry, NULL);
2626 return NULL;
2627}
2628
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002629/*
2630 * Check if a file is a control file
2631 */
2632static inline struct cftype *__file_cft(struct file *file)
2633{
2634 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2635 return ERR_PTR(-EINVAL);
2636 return __d_cft(file->f_dentry);
2637}
2638
Al Viroa5e7ed32011-07-26 01:55:55 -04002639static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002640 struct super_block *sb)
2641{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002642 struct inode *inode;
2643
2644 if (!dentry)
2645 return -ENOENT;
2646 if (dentry->d_inode)
2647 return -EEXIST;
2648
2649 inode = cgroup_new_inode(mode, sb);
2650 if (!inode)
2651 return -ENOMEM;
2652
2653 if (S_ISDIR(mode)) {
2654 inode->i_op = &cgroup_dir_inode_operations;
2655 inode->i_fop = &simple_dir_operations;
2656
2657 /* start off with i_nlink == 2 (for "." entry) */
2658 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002659 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002660
Tejun Heob8a2df62012-11-19 08:13:37 -08002661 /*
2662 * Control reaches here with cgroup_mutex held.
2663 * @inode->i_mutex should nest outside cgroup_mutex but we
2664 * want to populate it immediately without releasing
2665 * cgroup_mutex. As @inode isn't visible to anyone else
2666 * yet, trylock will always succeed without affecting
2667 * lockdep checks.
2668 */
2669 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002670 } else if (S_ISREG(mode)) {
2671 inode->i_size = 0;
2672 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002673 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002675 d_instantiate(dentry, inode);
2676 dget(dentry); /* Extra count - pin the dentry in core */
2677 return 0;
2678}
2679
Li Zefan099fca32009-04-02 16:57:29 -07002680/**
2681 * cgroup_file_mode - deduce file mode of a control file
2682 * @cft: the control file in question
2683 *
2684 * returns cft->mode if ->mode is not 0
2685 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2686 * returns S_IRUGO if it has only a read handler
2687 * returns S_IWUSR if it has only a write hander
2688 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002689static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002690{
Al Viroa5e7ed32011-07-26 01:55:55 -04002691 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002692
2693 if (cft->mode)
2694 return cft->mode;
2695
2696 if (cft->read || cft->read_u64 || cft->read_s64 ||
2697 cft->read_map || cft->read_seq_string)
2698 mode |= S_IRUGO;
2699
2700 if (cft->write || cft->write_u64 || cft->write_s64 ||
2701 cft->write_string || cft->trigger)
2702 mode |= S_IWUSR;
2703
2704 return mode;
2705}
2706
Tejun Heodb0416b2012-04-01 12:09:55 -07002707static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002708 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709{
Paul Menagebd89aab2007-10-18 23:40:44 -07002710 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002711 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002713 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002714 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002715 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002717
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002718 simple_xattrs_init(&cft->xattrs);
2719
Tejun Heo8e3f6542012-04-01 12:09:55 -07002720 /* does @cft->flags tell us to skip creation on @cgrp? */
2721 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2722 return 0;
2723 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2724 return 0;
2725
Paul Menagebd89aab2007-10-18 23:40:44 -07002726 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 strcpy(name, subsys->name);
2728 strcat(name, ".");
2729 }
2730 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002731
Paul Menageddbcc7e2007-10-18 23:39:30 -07002732 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002733
2734 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2735 if (!cfe)
2736 return -ENOMEM;
2737
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002739 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002740 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002741 goto out;
2742 }
2743
2744 mode = cgroup_file_mode(cft);
2745 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2746 if (!error) {
2747 cfe->type = (void *)cft;
2748 cfe->dentry = dentry;
2749 dentry->d_fsdata = cfe;
2750 list_add_tail(&cfe->node, &parent->files);
2751 cfe = NULL;
2752 }
2753 dput(dentry);
2754out:
2755 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756 return error;
2757}
2758
Tejun Heo79578622012-04-01 12:09:56 -07002759static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002760 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002761{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002762 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002763 int err, ret = 0;
2764
2765 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Tejun Heo79578622012-04-01 12:09:56 -07002766 if (is_add)
2767 err = cgroup_add_file(cgrp, subsys, cft);
2768 else
2769 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002770 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002771 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2772 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002773 ret = err;
2774 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002775 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002776 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002777}
2778
Tejun Heo8e3f6542012-04-01 12:09:55 -07002779static DEFINE_MUTEX(cgroup_cft_mutex);
2780
2781static void cgroup_cfts_prepare(void)
2782 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2783{
2784 /*
2785 * Thanks to the entanglement with vfs inode locking, we can't walk
2786 * the existing cgroups under cgroup_mutex and create files.
2787 * Instead, we increment reference on all cgroups and build list of
2788 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2789 * exclusive access to the field.
2790 */
2791 mutex_lock(&cgroup_cft_mutex);
2792 mutex_lock(&cgroup_mutex);
2793}
2794
2795static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002796 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002797 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2798{
2799 LIST_HEAD(pending);
2800 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002801
2802 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2803 if (cfts && ss->root != &rootnode) {
2804 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2805 dget(cgrp->dentry);
2806 list_add_tail(&cgrp->cft_q_node, &pending);
2807 }
2808 }
2809
2810 mutex_unlock(&cgroup_mutex);
2811
2812 /*
2813 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2814 * files for all cgroups which were created before.
2815 */
2816 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2817 struct inode *inode = cgrp->dentry->d_inode;
2818
2819 mutex_lock(&inode->i_mutex);
2820 mutex_lock(&cgroup_mutex);
2821 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002822 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002823 mutex_unlock(&cgroup_mutex);
2824 mutex_unlock(&inode->i_mutex);
2825
2826 list_del_init(&cgrp->cft_q_node);
2827 dput(cgrp->dentry);
2828 }
2829
2830 mutex_unlock(&cgroup_cft_mutex);
2831}
2832
2833/**
2834 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2835 * @ss: target cgroup subsystem
2836 * @cfts: zero-length name terminated array of cftypes
2837 *
2838 * Register @cfts to @ss. Files described by @cfts are created for all
2839 * existing cgroups to which @ss is attached and all future cgroups will
2840 * have them too. This function can be called anytime whether @ss is
2841 * attached or not.
2842 *
2843 * Returns 0 on successful registration, -errno on failure. Note that this
2844 * function currently returns 0 as long as @cfts registration is successful
2845 * even if some file creation attempts on existing cgroups fail.
2846 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002847int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002848{
2849 struct cftype_set *set;
2850
2851 set = kzalloc(sizeof(*set), GFP_KERNEL);
2852 if (!set)
2853 return -ENOMEM;
2854
2855 cgroup_cfts_prepare();
2856 set->cfts = cfts;
2857 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002858 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002859
2860 return 0;
2861}
2862EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2863
Li Zefana043e3b2008-02-23 15:24:09 -08002864/**
Tejun Heo79578622012-04-01 12:09:56 -07002865 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2866 * @ss: target cgroup subsystem
2867 * @cfts: zero-length name terminated array of cftypes
2868 *
2869 * Unregister @cfts from @ss. Files described by @cfts are removed from
2870 * all existing cgroups to which @ss is attached and all future cgroups
2871 * won't have them either. This function can be called anytime whether @ss
2872 * is attached or not.
2873 *
2874 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2875 * registered with @ss.
2876 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002877int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002878{
2879 struct cftype_set *set;
2880
2881 cgroup_cfts_prepare();
2882
2883 list_for_each_entry(set, &ss->cftsets, node) {
2884 if (set->cfts == cfts) {
2885 list_del_init(&set->node);
2886 cgroup_cfts_commit(ss, cfts, false);
2887 return 0;
2888 }
2889 }
2890
2891 cgroup_cfts_commit(ss, NULL, false);
2892 return -ENOENT;
2893}
2894
2895/**
Li Zefana043e3b2008-02-23 15:24:09 -08002896 * cgroup_task_count - count the number of tasks in a cgroup.
2897 * @cgrp: the cgroup in question
2898 *
2899 * Return the number of tasks in the cgroup.
2900 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002901int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002902{
2903 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002904 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002905
Paul Menage817929e2007-10-18 23:39:36 -07002906 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002907 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002908 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002909 }
2910 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002911 return count;
2912}
2913
2914/*
Paul Menage817929e2007-10-18 23:39:36 -07002915 * Advance a list_head iterator. The iterator should be positioned at
2916 * the start of a css_set
2917 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002918static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002919 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002920{
2921 struct list_head *l = it->cg_link;
2922 struct cg_cgroup_link *link;
2923 struct css_set *cg;
2924
2925 /* Advance to the next non-empty css_set */
2926 do {
2927 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002928 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002929 it->cg_link = NULL;
2930 return;
2931 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002932 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002933 cg = link->cg;
2934 } while (list_empty(&cg->tasks));
2935 it->cg_link = l;
2936 it->task = cg->tasks.next;
2937}
2938
Cliff Wickman31a7df02008-02-07 00:14:42 -08002939/*
2940 * To reduce the fork() overhead for systems that are not actually
2941 * using their cgroups capability, we don't maintain the lists running
2942 * through each css_set to its tasks until we see the list actually
2943 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002944 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002945static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002946{
2947 struct task_struct *p, *g;
2948 write_lock(&css_set_lock);
2949 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002950 /*
2951 * We need tasklist_lock because RCU is not safe against
2952 * while_each_thread(). Besides, a forking task that has passed
2953 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2954 * is not guaranteed to have its child immediately visible in the
2955 * tasklist if we walk through it with RCU.
2956 */
2957 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002958 do_each_thread(g, p) {
2959 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002960 /*
2961 * We should check if the process is exiting, otherwise
2962 * it will race with cgroup_exit() in that the list
2963 * entry won't be deleted though the process has exited.
2964 */
2965 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002966 list_add(&p->cg_list, &p->cgroups->tasks);
2967 task_unlock(p);
2968 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002969 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002970 write_unlock(&css_set_lock);
2971}
2972
Tejun Heo574bd9f2012-11-09 09:12:29 -08002973/**
2974 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2975 * @pos: the current position (%NULL to initiate traversal)
2976 * @cgroup: cgroup whose descendants to walk
2977 *
2978 * To be used by cgroup_for_each_descendant_pre(). Find the next
2979 * descendant to visit for pre-order traversal of @cgroup's descendants.
2980 */
2981struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2982 struct cgroup *cgroup)
2983{
2984 struct cgroup *next;
2985
2986 WARN_ON_ONCE(!rcu_read_lock_held());
2987
2988 /* if first iteration, pretend we just visited @cgroup */
2989 if (!pos) {
2990 if (list_empty(&cgroup->children))
2991 return NULL;
2992 pos = cgroup;
2993 }
2994
2995 /* visit the first child if exists */
2996 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2997 if (next)
2998 return next;
2999
3000 /* no child, visit my or the closest ancestor's next sibling */
3001 do {
3002 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3003 sibling);
3004 if (&next->sibling != &pos->parent->children)
3005 return next;
3006
3007 pos = pos->parent;
3008 } while (pos != cgroup);
3009
3010 return NULL;
3011}
3012EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3013
3014static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3015{
3016 struct cgroup *last;
3017
3018 do {
3019 last = pos;
3020 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3021 sibling);
3022 } while (pos);
3023
3024 return last;
3025}
3026
3027/**
3028 * cgroup_next_descendant_post - find the next descendant for post-order walk
3029 * @pos: the current position (%NULL to initiate traversal)
3030 * @cgroup: cgroup whose descendants to walk
3031 *
3032 * To be used by cgroup_for_each_descendant_post(). Find the next
3033 * descendant to visit for post-order traversal of @cgroup's descendants.
3034 */
3035struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3036 struct cgroup *cgroup)
3037{
3038 struct cgroup *next;
3039
3040 WARN_ON_ONCE(!rcu_read_lock_held());
3041
3042 /* if first iteration, visit the leftmost descendant */
3043 if (!pos) {
3044 next = cgroup_leftmost_descendant(cgroup);
3045 return next != cgroup ? next : NULL;
3046 }
3047
3048 /* if there's an unvisited sibling, visit its leftmost descendant */
3049 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3050 if (&next->sibling != &pos->parent->children)
3051 return cgroup_leftmost_descendant(next);
3052
3053 /* no sibling left, visit parent */
3054 next = pos->parent;
3055 return next != cgroup ? next : NULL;
3056}
3057EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3058
Paul Menagebd89aab2007-10-18 23:40:44 -07003059void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003060 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003061{
3062 /*
3063 * The first time anyone tries to iterate across a cgroup,
3064 * we need to enable the list linking each css_set to its
3065 * tasks, and fix up all existing tasks.
3066 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003067 if (!use_task_css_set_links)
3068 cgroup_enable_task_cg_lists();
3069
Paul Menage817929e2007-10-18 23:39:36 -07003070 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003071 it->cg_link = &cgrp->css_sets;
3072 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003073}
3074
Paul Menagebd89aab2007-10-18 23:40:44 -07003075struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003076 struct cgroup_iter *it)
3077{
3078 struct task_struct *res;
3079 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003080 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003081
3082 /* If the iterator cg is NULL, we have no tasks */
3083 if (!it->cg_link)
3084 return NULL;
3085 res = list_entry(l, struct task_struct, cg_list);
3086 /* Advance iterator to find next entry */
3087 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003088 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3089 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003090 /* We reached the end of this task list - move on to
3091 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003092 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003093 } else {
3094 it->task = l;
3095 }
3096 return res;
3097}
3098
Paul Menagebd89aab2007-10-18 23:40:44 -07003099void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003100 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003101{
3102 read_unlock(&css_set_lock);
3103}
3104
Cliff Wickman31a7df02008-02-07 00:14:42 -08003105static inline int started_after_time(struct task_struct *t1,
3106 struct timespec *time,
3107 struct task_struct *t2)
3108{
3109 int start_diff = timespec_compare(&t1->start_time, time);
3110 if (start_diff > 0) {
3111 return 1;
3112 } else if (start_diff < 0) {
3113 return 0;
3114 } else {
3115 /*
3116 * Arbitrarily, if two processes started at the same
3117 * time, we'll say that the lower pointer value
3118 * started first. Note that t2 may have exited by now
3119 * so this may not be a valid pointer any longer, but
3120 * that's fine - it still serves to distinguish
3121 * between two tasks started (effectively) simultaneously.
3122 */
3123 return t1 > t2;
3124 }
3125}
3126
3127/*
3128 * This function is a callback from heap_insert() and is used to order
3129 * the heap.
3130 * In this case we order the heap in descending task start time.
3131 */
3132static inline int started_after(void *p1, void *p2)
3133{
3134 struct task_struct *t1 = p1;
3135 struct task_struct *t2 = p2;
3136 return started_after_time(t1, &t2->start_time, t2);
3137}
3138
3139/**
3140 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3141 * @scan: struct cgroup_scanner containing arguments for the scan
3142 *
3143 * Arguments include pointers to callback functions test_task() and
3144 * process_task().
3145 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3146 * and if it returns true, call process_task() for it also.
3147 * The test_task pointer may be NULL, meaning always true (select all tasks).
3148 * Effectively duplicates cgroup_iter_{start,next,end}()
3149 * but does not lock css_set_lock for the call to process_task().
3150 * The struct cgroup_scanner may be embedded in any structure of the caller's
3151 * creation.
3152 * It is guaranteed that process_task() will act on every task that
3153 * is a member of the cgroup for the duration of this call. This
3154 * function may or may not call process_task() for tasks that exit
3155 * or move to a different cgroup during the call, or are forked or
3156 * move into the cgroup during the call.
3157 *
3158 * Note that test_task() may be called with locks held, and may in some
3159 * situations be called multiple times for the same task, so it should
3160 * be cheap.
3161 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3162 * pre-allocated and will be used for heap operations (and its "gt" member will
3163 * be overwritten), else a temporary heap will be used (allocation of which
3164 * may cause this function to fail).
3165 */
3166int cgroup_scan_tasks(struct cgroup_scanner *scan)
3167{
3168 int retval, i;
3169 struct cgroup_iter it;
3170 struct task_struct *p, *dropped;
3171 /* Never dereference latest_task, since it's not refcounted */
3172 struct task_struct *latest_task = NULL;
3173 struct ptr_heap tmp_heap;
3174 struct ptr_heap *heap;
3175 struct timespec latest_time = { 0, 0 };
3176
3177 if (scan->heap) {
3178 /* The caller supplied our heap and pre-allocated its memory */
3179 heap = scan->heap;
3180 heap->gt = &started_after;
3181 } else {
3182 /* We need to allocate our own heap memory */
3183 heap = &tmp_heap;
3184 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3185 if (retval)
3186 /* cannot allocate the heap */
3187 return retval;
3188 }
3189
3190 again:
3191 /*
3192 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3193 * to determine which are of interest, and using the scanner's
3194 * "process_task" callback to process any of them that need an update.
3195 * Since we don't want to hold any locks during the task updates,
3196 * gather tasks to be processed in a heap structure.
3197 * The heap is sorted by descending task start time.
3198 * If the statically-sized heap fills up, we overflow tasks that
3199 * started later, and in future iterations only consider tasks that
3200 * started after the latest task in the previous pass. This
3201 * guarantees forward progress and that we don't miss any tasks.
3202 */
3203 heap->size = 0;
3204 cgroup_iter_start(scan->cg, &it);
3205 while ((p = cgroup_iter_next(scan->cg, &it))) {
3206 /*
3207 * Only affect tasks that qualify per the caller's callback,
3208 * if he provided one
3209 */
3210 if (scan->test_task && !scan->test_task(p, scan))
3211 continue;
3212 /*
3213 * Only process tasks that started after the last task
3214 * we processed
3215 */
3216 if (!started_after_time(p, &latest_time, latest_task))
3217 continue;
3218 dropped = heap_insert(heap, p);
3219 if (dropped == NULL) {
3220 /*
3221 * The new task was inserted; the heap wasn't
3222 * previously full
3223 */
3224 get_task_struct(p);
3225 } else if (dropped != p) {
3226 /*
3227 * The new task was inserted, and pushed out a
3228 * different task
3229 */
3230 get_task_struct(p);
3231 put_task_struct(dropped);
3232 }
3233 /*
3234 * Else the new task was newer than anything already in
3235 * the heap and wasn't inserted
3236 */
3237 }
3238 cgroup_iter_end(scan->cg, &it);
3239
3240 if (heap->size) {
3241 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003242 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003243 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003244 latest_time = q->start_time;
3245 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003246 }
3247 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003248 scan->process_task(q, scan);
3249 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003250 }
3251 /*
3252 * If we had to process any tasks at all, scan again
3253 * in case some of them were in the middle of forking
3254 * children that didn't get processed.
3255 * Not the most efficient way to do it, but it avoids
3256 * having to take callback_mutex in the fork path
3257 */
3258 goto again;
3259 }
3260 if (heap == &tmp_heap)
3261 heap_free(&tmp_heap);
3262 return 0;
3263}
3264
Paul Menage817929e2007-10-18 23:39:36 -07003265/*
Ben Blum102a7752009-09-23 15:56:26 -07003266 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003267 *
3268 * Reading this file can return large amounts of data if a cgroup has
3269 * *lots* of attached tasks. So it may need several calls to read(),
3270 * but we cannot guarantee that the information we produce is correct
3271 * unless we produce it entirely atomically.
3272 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003273 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003274
Li Zefan24528252012-01-20 11:58:43 +08003275/* which pidlist file are we talking about? */
3276enum cgroup_filetype {
3277 CGROUP_FILE_PROCS,
3278 CGROUP_FILE_TASKS,
3279};
3280
3281/*
3282 * A pidlist is a list of pids that virtually represents the contents of one
3283 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3284 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3285 * to the cgroup.
3286 */
3287struct cgroup_pidlist {
3288 /*
3289 * used to find which pidlist is wanted. doesn't change as long as
3290 * this particular list stays in the list.
3291 */
3292 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3293 /* array of xids */
3294 pid_t *list;
3295 /* how many elements the above list has */
3296 int length;
3297 /* how many files are using the current array */
3298 int use_count;
3299 /* each of these stored in a list by its cgroup */
3300 struct list_head links;
3301 /* pointer to the cgroup we belong to, for list removal purposes */
3302 struct cgroup *owner;
3303 /* protects the other fields */
3304 struct rw_semaphore mutex;
3305};
3306
Paul Menagebbcb81d2007-10-18 23:39:32 -07003307/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003308 * The following two functions "fix" the issue where there are more pids
3309 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3310 * TODO: replace with a kernel-wide solution to this problem
3311 */
3312#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3313static void *pidlist_allocate(int count)
3314{
3315 if (PIDLIST_TOO_LARGE(count))
3316 return vmalloc(count * sizeof(pid_t));
3317 else
3318 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3319}
3320static void pidlist_free(void *p)
3321{
3322 if (is_vmalloc_addr(p))
3323 vfree(p);
3324 else
3325 kfree(p);
3326}
3327static void *pidlist_resize(void *p, int newcount)
3328{
3329 void *newlist;
3330 /* note: if new alloc fails, old p will still be valid either way */
3331 if (is_vmalloc_addr(p)) {
3332 newlist = vmalloc(newcount * sizeof(pid_t));
3333 if (!newlist)
3334 return NULL;
3335 memcpy(newlist, p, newcount * sizeof(pid_t));
3336 vfree(p);
3337 } else {
3338 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3339 }
3340 return newlist;
3341}
3342
3343/*
Ben Blum102a7752009-09-23 15:56:26 -07003344 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3345 * If the new stripped list is sufficiently smaller and there's enough memory
3346 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3347 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003348 */
Ben Blum102a7752009-09-23 15:56:26 -07003349/* is the size difference enough that we should re-allocate the array? */
3350#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3351static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003352{
Ben Blum102a7752009-09-23 15:56:26 -07003353 int src, dest = 1;
3354 pid_t *list = *p;
3355 pid_t *newlist;
3356
3357 /*
3358 * we presume the 0th element is unique, so i starts at 1. trivial
3359 * edge cases first; no work needs to be done for either
3360 */
3361 if (length == 0 || length == 1)
3362 return length;
3363 /* src and dest walk down the list; dest counts unique elements */
3364 for (src = 1; src < length; src++) {
3365 /* find next unique element */
3366 while (list[src] == list[src-1]) {
3367 src++;
3368 if (src == length)
3369 goto after;
3370 }
3371 /* dest always points to where the next unique element goes */
3372 list[dest] = list[src];
3373 dest++;
3374 }
3375after:
3376 /*
3377 * if the length difference is large enough, we want to allocate a
3378 * smaller buffer to save memory. if this fails due to out of memory,
3379 * we'll just stay with what we've got.
3380 */
3381 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003382 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003383 if (newlist)
3384 *p = newlist;
3385 }
3386 return dest;
3387}
3388
3389static int cmppid(const void *a, const void *b)
3390{
3391 return *(pid_t *)a - *(pid_t *)b;
3392}
3393
3394/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003395 * find the appropriate pidlist for our purpose (given procs vs tasks)
3396 * returns with the lock on that pidlist already held, and takes care
3397 * of the use count, or returns NULL with no locks held if we're out of
3398 * memory.
3399 */
3400static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3401 enum cgroup_filetype type)
3402{
3403 struct cgroup_pidlist *l;
3404 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003405 struct pid_namespace *ns = current->nsproxy->pid_ns;
3406
Ben Blum72a8cb32009-09-23 15:56:27 -07003407 /*
3408 * We can't drop the pidlist_mutex before taking the l->mutex in case
3409 * the last ref-holder is trying to remove l from the list at the same
3410 * time. Holding the pidlist_mutex precludes somebody taking whichever
3411 * list we find out from under us - compare release_pid_array().
3412 */
3413 mutex_lock(&cgrp->pidlist_mutex);
3414 list_for_each_entry(l, &cgrp->pidlists, links) {
3415 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003416 /* make sure l doesn't vanish out from under us */
3417 down_write(&l->mutex);
3418 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003419 return l;
3420 }
3421 }
3422 /* entry not found; create a new one */
3423 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3424 if (!l) {
3425 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003426 return l;
3427 }
3428 init_rwsem(&l->mutex);
3429 down_write(&l->mutex);
3430 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003431 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003432 l->use_count = 0; /* don't increment here */
3433 l->list = NULL;
3434 l->owner = cgrp;
3435 list_add(&l->links, &cgrp->pidlists);
3436 mutex_unlock(&cgrp->pidlist_mutex);
3437 return l;
3438}
3439
3440/*
Ben Blum102a7752009-09-23 15:56:26 -07003441 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3442 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003443static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3444 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003445{
3446 pid_t *array;
3447 int length;
3448 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003449 struct cgroup_iter it;
3450 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003451 struct cgroup_pidlist *l;
3452
3453 /*
3454 * If cgroup gets more users after we read count, we won't have
3455 * enough space - tough. This race is indistinguishable to the
3456 * caller from the case that the additional cgroup users didn't
3457 * show up until sometime later on.
3458 */
3459 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003460 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003461 if (!array)
3462 return -ENOMEM;
3463 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003464 cgroup_iter_start(cgrp, &it);
3465 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003466 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003467 break;
Ben Blum102a7752009-09-23 15:56:26 -07003468 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003469 if (type == CGROUP_FILE_PROCS)
3470 pid = task_tgid_vnr(tsk);
3471 else
3472 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003473 if (pid > 0) /* make sure to only use valid results */
3474 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003475 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003476 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003477 length = n;
3478 /* now sort & (if procs) strip out duplicates */
3479 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003480 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003481 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003482 l = cgroup_pidlist_find(cgrp, type);
3483 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003484 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003485 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003486 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003487 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003488 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003489 l->list = array;
3490 l->length = length;
3491 l->use_count++;
3492 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003493 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003494 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003495}
3496
Balbir Singh846c7bb2007-10-18 23:39:44 -07003497/**
Li Zefana043e3b2008-02-23 15:24:09 -08003498 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003499 * @stats: cgroupstats to fill information into
3500 * @dentry: A dentry entry belonging to the cgroup for which stats have
3501 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003502 *
3503 * Build and fill cgroupstats so that taskstats can export it to user
3504 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003505 */
3506int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3507{
3508 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003509 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003510 struct cgroup_iter it;
3511 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003512
Balbir Singh846c7bb2007-10-18 23:39:44 -07003513 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003514 * Validate dentry by checking the superblock operations,
3515 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003516 */
Li Zefan33d283b2008-11-19 15:36:48 -08003517 if (dentry->d_sb->s_op != &cgroup_ops ||
3518 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003519 goto err;
3520
3521 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003522 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003523
Paul Menagebd89aab2007-10-18 23:40:44 -07003524 cgroup_iter_start(cgrp, &it);
3525 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003526 switch (tsk->state) {
3527 case TASK_RUNNING:
3528 stats->nr_running++;
3529 break;
3530 case TASK_INTERRUPTIBLE:
3531 stats->nr_sleeping++;
3532 break;
3533 case TASK_UNINTERRUPTIBLE:
3534 stats->nr_uninterruptible++;
3535 break;
3536 case TASK_STOPPED:
3537 stats->nr_stopped++;
3538 break;
3539 default:
3540 if (delayacct_is_task_waiting_on_io(tsk))
3541 stats->nr_io_wait++;
3542 break;
3543 }
3544 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003545 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003546
Balbir Singh846c7bb2007-10-18 23:39:44 -07003547err:
3548 return ret;
3549}
3550
Paul Menage8f3ff202009-09-23 15:56:25 -07003551
Paul Menagecc31edc2008-10-18 20:28:04 -07003552/*
Ben Blum102a7752009-09-23 15:56:26 -07003553 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003554 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003555 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003556 */
3557
Ben Blum102a7752009-09-23 15:56:26 -07003558static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003559{
3560 /*
3561 * Initially we receive a position value that corresponds to
3562 * one more than the last pid shown (or 0 on the first call or
3563 * after a seek to the start). Use a binary-search to find the
3564 * next pid to display, if any
3565 */
Ben Blum102a7752009-09-23 15:56:26 -07003566 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003567 int index = 0, pid = *pos;
3568 int *iter;
3569
Ben Blum102a7752009-09-23 15:56:26 -07003570 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003571 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003572 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003573
Paul Menagecc31edc2008-10-18 20:28:04 -07003574 while (index < end) {
3575 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003576 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003577 index = mid;
3578 break;
Ben Blum102a7752009-09-23 15:56:26 -07003579 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003580 index = mid + 1;
3581 else
3582 end = mid;
3583 }
3584 }
3585 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003586 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003587 return NULL;
3588 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003589 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003590 *pos = *iter;
3591 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003592}
3593
Ben Blum102a7752009-09-23 15:56:26 -07003594static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003595{
Ben Blum102a7752009-09-23 15:56:26 -07003596 struct cgroup_pidlist *l = s->private;
3597 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003598}
3599
Ben Blum102a7752009-09-23 15:56:26 -07003600static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003601{
Ben Blum102a7752009-09-23 15:56:26 -07003602 struct cgroup_pidlist *l = s->private;
3603 pid_t *p = v;
3604 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003605 /*
3606 * Advance to the next pid in the array. If this goes off the
3607 * end, we're done
3608 */
3609 p++;
3610 if (p >= end) {
3611 return NULL;
3612 } else {
3613 *pos = *p;
3614 return p;
3615 }
3616}
3617
Ben Blum102a7752009-09-23 15:56:26 -07003618static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003619{
3620 return seq_printf(s, "%d\n", *(int *)v);
3621}
3622
Ben Blum102a7752009-09-23 15:56:26 -07003623/*
3624 * seq_operations functions for iterating on pidlists through seq_file -
3625 * independent of whether it's tasks or procs
3626 */
3627static const struct seq_operations cgroup_pidlist_seq_operations = {
3628 .start = cgroup_pidlist_start,
3629 .stop = cgroup_pidlist_stop,
3630 .next = cgroup_pidlist_next,
3631 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003632};
3633
Ben Blum102a7752009-09-23 15:56:26 -07003634static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003635{
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 /*
3637 * the case where we're the last user of this particular pidlist will
3638 * have us remove it from the cgroup's list, which entails taking the
3639 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3640 * pidlist_mutex, we have to take pidlist_mutex first.
3641 */
3642 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003643 down_write(&l->mutex);
3644 BUG_ON(!l->use_count);
3645 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003646 /* we're the last user if refcount is 0; remove and free */
3647 list_del(&l->links);
3648 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003649 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003650 put_pid_ns(l->key.ns);
3651 up_write(&l->mutex);
3652 kfree(l);
3653 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003654 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003655 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003656 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003657}
3658
Ben Blum102a7752009-09-23 15:56:26 -07003659static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003660{
Ben Blum102a7752009-09-23 15:56:26 -07003661 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003662 if (!(file->f_mode & FMODE_READ))
3663 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003664 /*
3665 * the seq_file will only be initialized if the file was opened for
3666 * reading; hence we check if it's not null only in that case.
3667 */
3668 l = ((struct seq_file *)file->private_data)->private;
3669 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003670 return seq_release(inode, file);
3671}
3672
Ben Blum102a7752009-09-23 15:56:26 -07003673static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003674 .read = seq_read,
3675 .llseek = seq_lseek,
3676 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003677 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003678};
3679
3680/*
Ben Blum102a7752009-09-23 15:56:26 -07003681 * The following functions handle opens on a file that displays a pidlist
3682 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3683 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003684 */
Ben Blum102a7752009-09-23 15:56:26 -07003685/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003686static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003687{
3688 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003689 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003690 int retval;
3691
3692 /* Nothing to do for write-only files */
3693 if (!(file->f_mode & FMODE_READ))
3694 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003695
Ben Blum102a7752009-09-23 15:56:26 -07003696 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003697 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003698 if (retval)
3699 return retval;
3700 /* configure file information */
3701 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003702
Ben Blum102a7752009-09-23 15:56:26 -07003703 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003704 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003705 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003706 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003707 }
Ben Blum102a7752009-09-23 15:56:26 -07003708 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003709 return 0;
3710}
Ben Blum102a7752009-09-23 15:56:26 -07003711static int cgroup_tasks_open(struct inode *unused, struct file *file)
3712{
Ben Blum72a8cb32009-09-23 15:56:27 -07003713 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003714}
3715static int cgroup_procs_open(struct inode *unused, struct file *file)
3716{
Ben Blum72a8cb32009-09-23 15:56:27 -07003717 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003718}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003719
Paul Menagebd89aab2007-10-18 23:40:44 -07003720static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003721 struct cftype *cft)
3722{
Paul Menagebd89aab2007-10-18 23:40:44 -07003723 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003724}
3725
Paul Menage6379c102008-07-25 01:47:01 -07003726static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3727 struct cftype *cft,
3728 u64 val)
3729{
3730 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3731 if (val)
3732 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3733 else
3734 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3735 return 0;
3736}
3737
Paul Menagebbcb81d2007-10-18 23:39:32 -07003738/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003739 * Unregister event and free resources.
3740 *
3741 * Gets called from workqueue.
3742 */
3743static void cgroup_event_remove(struct work_struct *work)
3744{
3745 struct cgroup_event *event = container_of(work, struct cgroup_event,
3746 remove);
3747 struct cgroup *cgrp = event->cgrp;
3748
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003749 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3750
3751 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003752 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003753 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003754}
3755
3756/*
3757 * Gets called on POLLHUP on eventfd when user closes it.
3758 *
3759 * Called with wqh->lock held and interrupts disabled.
3760 */
3761static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3762 int sync, void *key)
3763{
3764 struct cgroup_event *event = container_of(wait,
3765 struct cgroup_event, wait);
3766 struct cgroup *cgrp = event->cgrp;
3767 unsigned long flags = (unsigned long)key;
3768
3769 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003770 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003771 spin_lock(&cgrp->event_list_lock);
3772 list_del(&event->list);
3773 spin_unlock(&cgrp->event_list_lock);
3774 /*
3775 * We are in atomic context, but cgroup_event_remove() may
3776 * sleep, so we have to call it in workqueue.
3777 */
3778 schedule_work(&event->remove);
3779 }
3780
3781 return 0;
3782}
3783
3784static void cgroup_event_ptable_queue_proc(struct file *file,
3785 wait_queue_head_t *wqh, poll_table *pt)
3786{
3787 struct cgroup_event *event = container_of(pt,
3788 struct cgroup_event, pt);
3789
3790 event->wqh = wqh;
3791 add_wait_queue(wqh, &event->wait);
3792}
3793
3794/*
3795 * Parse input and register new cgroup event handler.
3796 *
3797 * Input must be in format '<event_fd> <control_fd> <args>'.
3798 * Interpretation of args is defined by control file implementation.
3799 */
3800static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3801 const char *buffer)
3802{
3803 struct cgroup_event *event = NULL;
3804 unsigned int efd, cfd;
3805 struct file *efile = NULL;
3806 struct file *cfile = NULL;
3807 char *endp;
3808 int ret;
3809
3810 efd = simple_strtoul(buffer, &endp, 10);
3811 if (*endp != ' ')
3812 return -EINVAL;
3813 buffer = endp + 1;
3814
3815 cfd = simple_strtoul(buffer, &endp, 10);
3816 if ((*endp != ' ') && (*endp != '\0'))
3817 return -EINVAL;
3818 buffer = endp + 1;
3819
3820 event = kzalloc(sizeof(*event), GFP_KERNEL);
3821 if (!event)
3822 return -ENOMEM;
3823 event->cgrp = cgrp;
3824 INIT_LIST_HEAD(&event->list);
3825 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3826 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3827 INIT_WORK(&event->remove, cgroup_event_remove);
3828
3829 efile = eventfd_fget(efd);
3830 if (IS_ERR(efile)) {
3831 ret = PTR_ERR(efile);
3832 goto fail;
3833 }
3834
3835 event->eventfd = eventfd_ctx_fileget(efile);
3836 if (IS_ERR(event->eventfd)) {
3837 ret = PTR_ERR(event->eventfd);
3838 goto fail;
3839 }
3840
3841 cfile = fget(cfd);
3842 if (!cfile) {
3843 ret = -EBADF;
3844 goto fail;
3845 }
3846
3847 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003848 /* AV: shouldn't we check that it's been opened for read instead? */
3849 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003850 if (ret < 0)
3851 goto fail;
3852
3853 event->cft = __file_cft(cfile);
3854 if (IS_ERR(event->cft)) {
3855 ret = PTR_ERR(event->cft);
3856 goto fail;
3857 }
3858
3859 if (!event->cft->register_event || !event->cft->unregister_event) {
3860 ret = -EINVAL;
3861 goto fail;
3862 }
3863
3864 ret = event->cft->register_event(cgrp, event->cft,
3865 event->eventfd, buffer);
3866 if (ret)
3867 goto fail;
3868
3869 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3870 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3871 ret = 0;
3872 goto fail;
3873 }
3874
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003875 /*
3876 * Events should be removed after rmdir of cgroup directory, but before
3877 * destroying subsystem state objects. Let's take reference to cgroup
3878 * directory dentry to do that.
3879 */
3880 dget(cgrp->dentry);
3881
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003882 spin_lock(&cgrp->event_list_lock);
3883 list_add(&event->list, &cgrp->event_list);
3884 spin_unlock(&cgrp->event_list_lock);
3885
3886 fput(cfile);
3887 fput(efile);
3888
3889 return 0;
3890
3891fail:
3892 if (cfile)
3893 fput(cfile);
3894
3895 if (event && event->eventfd && !IS_ERR(event->eventfd))
3896 eventfd_ctx_put(event->eventfd);
3897
3898 if (!IS_ERR_OR_NULL(efile))
3899 fput(efile);
3900
3901 kfree(event);
3902
3903 return ret;
3904}
3905
Daniel Lezcano97978e62010-10-27 15:33:35 -07003906static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3907 struct cftype *cft)
3908{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003909 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003910}
3911
3912static int cgroup_clone_children_write(struct cgroup *cgrp,
3913 struct cftype *cft,
3914 u64 val)
3915{
3916 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003917 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003918 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003919 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003920 return 0;
3921}
3922
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003923/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003924 * for the common functions, 'private' gives the type of file
3925 */
Ben Blum102a7752009-09-23 15:56:26 -07003926/* for hysterical raisins, we can't put this on the older files */
3927#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003928static struct cftype files[] = {
3929 {
3930 .name = "tasks",
3931 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003932 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003933 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003934 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003935 },
Ben Blum102a7752009-09-23 15:56:26 -07003936 {
3937 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3938 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003939 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003940 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003941 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003942 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003943 {
3944 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003945 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003946 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003947 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003948 {
3949 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3950 .write_string = cgroup_write_event_control,
3951 .mode = S_IWUGO,
3952 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003953 {
3954 .name = "cgroup.clone_children",
3955 .read_u64 = cgroup_clone_children_read,
3956 .write_u64 = cgroup_clone_children_write,
3957 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003958 {
3959 .name = "release_agent",
3960 .flags = CFTYPE_ONLY_ON_ROOT,
3961 .read_seq_string = cgroup_release_agent_show,
3962 .write_string = cgroup_release_agent_write,
3963 .max_write_len = PATH_MAX,
3964 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003965 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003966};
3967
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003968/**
3969 * cgroup_populate_dir - selectively creation of files in a directory
3970 * @cgrp: target cgroup
3971 * @base_files: true if the base files should be added
3972 * @subsys_mask: mask of the subsystem ids whose files should be added
3973 */
3974static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3975 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003976{
3977 int err;
3978 struct cgroup_subsys *ss;
3979
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003980 if (base_files) {
3981 err = cgroup_addrm_files(cgrp, NULL, files, true);
3982 if (err < 0)
3983 return err;
3984 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003985
Tejun Heo8e3f6542012-04-01 12:09:55 -07003986 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003987 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003988 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003989 if (!test_bit(ss->subsys_id, &subsys_mask))
3990 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003991
Tejun Heodb0416b2012-04-01 12:09:55 -07003992 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07003993 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003994 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07003995
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003996 /* This cgroup is ready now */
3997 for_each_subsys(cgrp->root, ss) {
3998 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3999 /*
4000 * Update id->css pointer and make this css visible from
4001 * CSS ID functions. This pointer will be dereferened
4002 * from RCU-read-side without locks.
4003 */
4004 if (css->id)
4005 rcu_assign_pointer(css->id->css, css);
4006 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004007
4008 return 0;
4009}
4010
Tejun Heo48ddbe12012-04-01 12:09:56 -07004011static void css_dput_fn(struct work_struct *work)
4012{
4013 struct cgroup_subsys_state *css =
4014 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004015 struct dentry *dentry = css->cgroup->dentry;
4016 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004017
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004018 atomic_inc(&sb->s_active);
4019 dput(dentry);
4020 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004021}
4022
Paul Menageddbcc7e2007-10-18 23:39:30 -07004023static void init_cgroup_css(struct cgroup_subsys_state *css,
4024 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004025 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004026{
Paul Menagebd89aab2007-10-18 23:40:44 -07004027 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004028 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004029 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004030 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004031 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004032 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004033 BUG_ON(cgrp->subsys[ss->subsys_id]);
4034 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004035
4036 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004037 * css holds an extra ref to @cgrp->dentry which is put on the last
4038 * css_put(). dput() requires process context, which css_put() may
4039 * be called without. @css->dput_work will be used to invoke
4040 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004041 */
4042 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004043}
4044
Tejun Heob1929db2012-11-19 08:13:38 -08004045/* invoke ->post_create() on a new CSS and mark it online if successful */
4046static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004047{
Tejun Heob1929db2012-11-19 08:13:38 -08004048 int ret = 0;
4049
Tejun Heoa31f2d32012-11-19 08:13:37 -08004050 lockdep_assert_held(&cgroup_mutex);
4051
Tejun Heo92fb9742012-11-19 08:13:38 -08004052 if (ss->css_online)
4053 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004054 if (!ret)
4055 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4056 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004057}
4058
4059/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4060static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4061 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4062{
4063 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4064
4065 lockdep_assert_held(&cgroup_mutex);
4066
4067 if (!(css->flags & CSS_ONLINE))
4068 return;
4069
4070 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004071 * css_offline() should be called with cgroup_mutex unlocked. See
Tejun Heoa31f2d32012-11-19 08:13:37 -08004072 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4073 * details. This temporary unlocking should go away once
4074 * cgroup_mutex is unexported from controllers.
4075 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004076 if (ss->css_offline) {
Tejun Heoa31f2d32012-11-19 08:13:37 -08004077 mutex_unlock(&cgroup_mutex);
Tejun Heo92fb9742012-11-19 08:13:38 -08004078 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004079 mutex_lock(&cgroup_mutex);
4080 }
4081
4082 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4083}
4084
Paul Menageddbcc7e2007-10-18 23:39:30 -07004085/*
Li Zefana043e3b2008-02-23 15:24:09 -08004086 * cgroup_create - create a cgroup
4087 * @parent: cgroup that will be parent of the new cgroup
4088 * @dentry: dentry of the new cgroup
4089 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004090 *
Li Zefana043e3b2008-02-23 15:24:09 -08004091 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004092 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004093static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004094 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004095{
Paul Menagebd89aab2007-10-18 23:40:44 -07004096 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004097 struct cgroupfs_root *root = parent->root;
4098 int err = 0;
4099 struct cgroup_subsys *ss;
4100 struct super_block *sb = root->sb;
4101
Tejun Heo0a950f62012-11-19 09:02:12 -08004102 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004103 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4104 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004105 return -ENOMEM;
4106
Tejun Heo0a950f62012-11-19 09:02:12 -08004107 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4108 if (cgrp->id < 0)
4109 goto err_free_cgrp;
4110
Tejun Heo976c06b2012-11-05 09:16:59 -08004111 /*
4112 * Only live parents can have children. Note that the liveliness
4113 * check isn't strictly necessary because cgroup_mkdir() and
4114 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4115 * anyway so that locking is contained inside cgroup proper and we
4116 * don't get nasty surprises if we ever grow another caller.
4117 */
4118 if (!cgroup_lock_live_group(parent)) {
4119 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004120 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004121 }
4122
Paul Menageddbcc7e2007-10-18 23:39:30 -07004123 /* Grab a reference on the superblock so the hierarchy doesn't
4124 * get deleted on unmount if there are child cgroups. This
4125 * can be done outside cgroup_mutex, since the sb can't
4126 * disappear while someone has an open control file on the
4127 * fs */
4128 atomic_inc(&sb->s_active);
4129
Paul Menagecc31edc2008-10-18 20:28:04 -07004130 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004131
Paul Menagebd89aab2007-10-18 23:40:44 -07004132 cgrp->parent = parent;
4133 cgrp->root = parent->root;
4134 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004135
Li Zefanb6abdb02008-03-04 14:28:19 -08004136 if (notify_on_release(parent))
4137 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4138
Tejun Heo2260e7f2012-11-19 08:13:38 -08004139 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4140 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004141
Paul Menageddbcc7e2007-10-18 23:39:30 -07004142 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004143 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004144
Tejun Heo92fb9742012-11-19 08:13:38 -08004145 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004146 if (IS_ERR(css)) {
4147 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004148 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004149 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004150 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004151 if (ss->use_id) {
4152 err = alloc_css_id(ss, parent, cgrp);
4153 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004154 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004155 }
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004156
4157 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4158 parent->parent) {
4159 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",
4160 current->comm, current->pid, ss->name);
4161 if (!strcmp(ss->name, "memory"))
4162 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4163 ss->warned_broken_hierarchy = true;
4164 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004165 }
4166
Tejun Heo4e139af2012-11-19 08:13:36 -08004167 /*
4168 * Create directory. cgroup_create_file() returns with the new
4169 * directory locked on success so that it can be populated without
4170 * dropping cgroup_mutex.
4171 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004172 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004174 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004175 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004176
Tejun Heo4e139af2012-11-19 08:13:36 -08004177 /* allocation complete, commit to creation */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004178 dentry->d_fsdata = cgrp;
Tejun Heofebfcef2012-11-19 08:13:36 -08004179 cgrp->dentry = dentry;
Tejun Heo4e139af2012-11-19 08:13:36 -08004180 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4181 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4182 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004183
Tejun Heob1929db2012-11-19 08:13:38 -08004184 /* each css holds a ref to the cgroup's dentry */
4185 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004186 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004187
Tejun Heob1929db2012-11-19 08:13:38 -08004188 /* creation succeeded, notify subsystems */
4189 for_each_subsys(root, ss) {
4190 err = online_css(ss, cgrp);
4191 if (err)
4192 goto err_destroy;
Tejun Heoa8638032012-11-09 09:12:29 -08004193 }
4194
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004195 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004196 if (err)
4197 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198
4199 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004200 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004201
4202 return 0;
4203
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004204err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004205 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004206 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004207 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004209 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004210 /* Release the reference count that we took on the superblock */
4211 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004212err_free_id:
4213 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004214err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004215 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004217
4218err_destroy:
4219 cgroup_destroy_locked(cgrp);
4220 mutex_unlock(&cgroup_mutex);
4221 mutex_unlock(&dentry->d_inode->i_mutex);
4222 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223}
4224
Al Viro18bb1db2011-07-26 01:41:39 -04004225static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004226{
4227 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4228
4229 /* the vfs holds inode->i_mutex already */
4230 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4231}
4232
Tejun Heo28b4c272012-04-01 12:09:56 -07004233/*
4234 * Check the reference count on each subsystem. Since we already
4235 * established that there are no tasks in the cgroup, if the css refcount
4236 * is also 1, then there should be no outstanding references, so the
4237 * subsystem is safe to destroy. We scan across all subsystems rather than
4238 * using the per-hierarchy linked list of mounted subsystems since we can
4239 * be called via check_for_release() with no synchronization other than
4240 * RCU, and the subsystem linked list isn't RCU-safe.
4241 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004242static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004243{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004244 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004245
Ben Blumaae8aab2010-03-10 15:22:07 -08004246 /*
4247 * We won't need to lock the subsys array, because the subsystems
4248 * we're concerned about aren't going anywhere since our cgroup root
4249 * has a reference on them.
4250 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004251 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4252 struct cgroup_subsys *ss = subsys[i];
4253 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004254
Ben Blumaae8aab2010-03-10 15:22:07 -08004255 /* Skip subsystems not present or not in this hierarchy */
4256 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004257 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004258
Paul Menagebd89aab2007-10-18 23:40:44 -07004259 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004260 /*
4261 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004262 * that by this point the cgroup has been removed
4263 * and the css deleted. But a false-positive doesn't
4264 * matter, since it can only happen if the cgroup
4265 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004266 * release agent to be called anyway.
4267 */
4268 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004269 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004270 }
4271 return 0;
4272}
4273
Tejun Heo42809dd2012-11-19 08:13:37 -08004274static int cgroup_destroy_locked(struct cgroup *cgrp)
4275 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004276{
Tejun Heo42809dd2012-11-19 08:13:37 -08004277 struct dentry *d = cgrp->dentry;
4278 struct cgroup *parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004279 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004280 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004281 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004282
Tejun Heo42809dd2012-11-19 08:13:37 -08004283 lockdep_assert_held(&d->d_inode->i_mutex);
4284 lockdep_assert_held(&cgroup_mutex);
4285
4286 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004287 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004288
Tejun Heo1a90dd52012-11-05 09:16:59 -08004289 /*
4290 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4291 * removed. This makes future css_tryget() and child creation
4292 * attempts fail thus maintaining the removal conditions verified
4293 * above.
4294 */
Tejun Heoed9577932012-11-05 09:16:58 -08004295 for_each_subsys(cgrp->root, ss) {
4296 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4297
4298 WARN_ON(atomic_read(&css->refcnt) < 0);
4299 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004300 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004301 set_bit(CGRP_REMOVED, &cgrp->flags);
4302
Tejun Heoa31f2d32012-11-19 08:13:37 -08004303 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004304 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004305 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004306
4307 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004308 * Put all the base refs. Each css holds an extra reference to the
4309 * cgroup's dentry and cgroup removal proceeds regardless of css
4310 * refs. On the last put of each css, whenever that may be, the
4311 * extra dentry ref is put so that dentry destruction happens only
4312 * after all css's are released.
4313 */
Tejun Heoe9316082012-11-05 09:16:58 -08004314 for_each_subsys(cgrp->root, ss)
4315 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004316
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004317 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004318 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004319 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004320 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004321
Paul Menage999cd8a2009-01-07 18:08:36 -08004322 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004323 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004324 list_del_init(&cgrp->allcg_node);
4325
Tejun Heo42809dd2012-11-19 08:13:37 -08004326 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004327 cgroup_d_remove_dir(d);
4328 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329
Paul Menagebd89aab2007-10-18 23:40:44 -07004330 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004331 check_for_release(parent);
4332
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004333 /*
4334 * Unregister events and notify userspace.
4335 * Notify userspace about cgroup removing only after rmdir of cgroup
4336 * directory to avoid race between userspace and kernelspace
4337 */
4338 spin_lock(&cgrp->event_list_lock);
4339 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4340 list_del(&event->list);
4341 remove_wait_queue(event->wqh, &event->wait);
4342 eventfd_signal(event->eventfd, 1);
4343 schedule_work(&event->remove);
4344 }
4345 spin_unlock(&cgrp->event_list_lock);
4346
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347 return 0;
4348}
4349
Tejun Heo42809dd2012-11-19 08:13:37 -08004350static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4351{
4352 int ret;
4353
4354 mutex_lock(&cgroup_mutex);
4355 ret = cgroup_destroy_locked(dentry->d_fsdata);
4356 mutex_unlock(&cgroup_mutex);
4357
4358 return ret;
4359}
4360
Tejun Heo8e3f6542012-04-01 12:09:55 -07004361static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4362{
4363 INIT_LIST_HEAD(&ss->cftsets);
4364
4365 /*
4366 * base_cftset is embedded in subsys itself, no need to worry about
4367 * deregistration.
4368 */
4369 if (ss->base_cftypes) {
4370 ss->base_cftset.cfts = ss->base_cftypes;
4371 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4372 }
4373}
4374
Li Zefan06a11922008-04-29 01:00:07 -07004375static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004376{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004377 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004378
4379 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004380
Tejun Heo648bb562012-11-19 08:13:36 -08004381 mutex_lock(&cgroup_mutex);
4382
Tejun Heo8e3f6542012-04-01 12:09:55 -07004383 /* init base cftset */
4384 cgroup_init_cftsets(ss);
4385
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004387 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004388 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004389 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390 /* We don't handle early failures gracefully */
4391 BUG_ON(IS_ERR(css));
4392 init_cgroup_css(css, ss, dummytop);
4393
Li Zefane8d55fd2008-04-29 01:00:13 -07004394 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004395 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004396 * newly registered, all tasks and hence the
4397 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004398 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004399
4400 need_forkexit_callback |= ss->fork || ss->exit;
4401
Li Zefane8d55fd2008-04-29 01:00:13 -07004402 /* At system boot, before all subsystems have been
4403 * registered, no tasks have been forked, so we don't
4404 * need to invoke fork callbacks here. */
4405 BUG_ON(!list_empty(&init_task.tasks));
4406
Paul Menageddbcc7e2007-10-18 23:39:30 -07004407 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004408 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004409
Tejun Heo648bb562012-11-19 08:13:36 -08004410 mutex_unlock(&cgroup_mutex);
4411
Ben Blume6a11052010-03-10 15:22:09 -08004412 /* this function shouldn't be used with modular subsystems, since they
4413 * need to register a subsys_id, among other things */
4414 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004415}
4416
4417/**
Ben Blume6a11052010-03-10 15:22:09 -08004418 * cgroup_load_subsys: load and register a modular subsystem at runtime
4419 * @ss: the subsystem to load
4420 *
4421 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004422 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004423 * up for use. If the subsystem is built-in anyway, work is delegated to the
4424 * simpler cgroup_init_subsys.
4425 */
4426int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4427{
Ben Blume6a11052010-03-10 15:22:09 -08004428 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004429 int i, ret;
Ben Blume6a11052010-03-10 15:22:09 -08004430
4431 /* check name and function validity */
4432 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004433 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004434 return -EINVAL;
4435
4436 /*
4437 * we don't support callbacks in modular subsystems. this check is
4438 * before the ss->module check for consistency; a subsystem that could
4439 * be a module should still have no callbacks even if the user isn't
4440 * compiling it as one.
4441 */
4442 if (ss->fork || ss->exit)
4443 return -EINVAL;
4444
4445 /*
4446 * an optionally modular subsystem is built-in: we want to do nothing,
4447 * since cgroup_init_subsys will have already taken care of it.
4448 */
4449 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004450 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004451 BUG_ON(subsys[ss->subsys_id] != ss);
4452 return 0;
4453 }
4454
Tejun Heo8e3f6542012-04-01 12:09:55 -07004455 /* init base cftset */
4456 cgroup_init_cftsets(ss);
4457
Ben Blume6a11052010-03-10 15:22:09 -08004458 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004459 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004460
4461 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004462 * no ss->css_alloc seems to need anything important in the ss
4463 * struct, so this can happen first (i.e. before the rootnode
4464 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004465 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004466 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004467 if (IS_ERR(css)) {
4468 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004469 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004470 mutex_unlock(&cgroup_mutex);
4471 return PTR_ERR(css);
4472 }
4473
4474 list_add(&ss->sibling, &rootnode.subsys_list);
4475 ss->root = &rootnode;
4476
4477 /* our new subsystem will be attached to the dummy hierarchy. */
4478 init_cgroup_css(css, ss, dummytop);
4479 /* init_idr must be after init_cgroup_css because it sets css->id. */
4480 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004481 ret = cgroup_init_idr(ss, css);
4482 if (ret)
4483 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004484 }
4485
4486 /*
4487 * Now we need to entangle the css into the existing css_sets. unlike
4488 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4489 * will need a new pointer to it; done by iterating the css_set_table.
4490 * furthermore, modifying the existing css_sets will corrupt the hash
4491 * table state, so each changed css_set will need its hash recomputed.
4492 * this is all done under the css_set_lock.
4493 */
4494 write_lock(&css_set_lock);
4495 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4496 struct css_set *cg;
4497 struct hlist_node *node, *tmp;
4498 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4499
4500 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4501 /* skip entries that we already rehashed */
4502 if (cg->subsys[ss->subsys_id])
4503 continue;
4504 /* remove existing entry */
4505 hlist_del(&cg->hlist);
4506 /* set new value */
4507 cg->subsys[ss->subsys_id] = css;
4508 /* recompute hash and restore entry */
4509 new_bucket = css_set_hash(cg->subsys);
4510 hlist_add_head(&cg->hlist, new_bucket);
4511 }
4512 }
4513 write_unlock(&css_set_lock);
4514
Ben Blume6a11052010-03-10 15:22:09 -08004515 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004516 ret = online_css(ss, dummytop);
4517 if (ret)
4518 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004519
Ben Blume6a11052010-03-10 15:22:09 -08004520 /* success! */
4521 mutex_unlock(&cgroup_mutex);
4522 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004523
4524err_unload:
4525 mutex_unlock(&cgroup_mutex);
4526 /* @ss can't be mounted here as try_module_get() would fail */
4527 cgroup_unload_subsys(ss);
4528 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004529}
4530EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4531
4532/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004533 * cgroup_unload_subsys: unload a modular subsystem
4534 * @ss: the subsystem to unload
4535 *
4536 * This function should be called in a modular subsystem's exitcall. When this
4537 * function is invoked, the refcount on the subsystem's module will be 0, so
4538 * the subsystem will not be attached to any hierarchy.
4539 */
4540void cgroup_unload_subsys(struct cgroup_subsys *ss)
4541{
4542 struct cg_cgroup_link *link;
4543 struct hlist_head *hhead;
4544
4545 BUG_ON(ss->module == NULL);
4546
4547 /*
4548 * we shouldn't be called if the subsystem is in use, and the use of
4549 * try_module_get in parse_cgroupfs_options should ensure that it
4550 * doesn't start being used while we're killing it off.
4551 */
4552 BUG_ON(ss->root != &rootnode);
4553
4554 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004555
Tejun Heoa31f2d32012-11-19 08:13:37 -08004556 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004557 ss->active = 0;
4558
4559 if (ss->use_id) {
4560 idr_remove_all(&ss->idr);
4561 idr_destroy(&ss->idr);
4562 }
4563
Ben Blumcf5d5942010-03-10 15:22:09 -08004564 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004565 subsys[ss->subsys_id] = NULL;
4566
4567 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004568 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004569
4570 /*
4571 * disentangle the css from all css_sets attached to the dummytop. as
4572 * in loading, we need to pay our respects to the hashtable gods.
4573 */
4574 write_lock(&css_set_lock);
4575 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4576 struct css_set *cg = link->cg;
4577
4578 hlist_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004579 cg->subsys[ss->subsys_id] = NULL;
4580 hhead = css_set_hash(cg->subsys);
4581 hlist_add_head(&cg->hlist, hhead);
4582 }
4583 write_unlock(&css_set_lock);
4584
4585 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004586 * remove subsystem's css from the dummytop and free it - need to
4587 * free before marking as null because ss->css_free needs the
4588 * cgrp->subsys pointer to find their state. note that this also
4589 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004590 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004591 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004592 dummytop->subsys[ss->subsys_id] = NULL;
4593
4594 mutex_unlock(&cgroup_mutex);
4595}
4596EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4597
4598/**
Li Zefana043e3b2008-02-23 15:24:09 -08004599 * cgroup_init_early - cgroup initialization at system boot
4600 *
4601 * Initialize cgroups at system boot, and initialize any
4602 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004603 */
4604int __init cgroup_init_early(void)
4605{
4606 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004607 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004608 INIT_LIST_HEAD(&init_css_set.cg_links);
4609 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004610 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004611 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004612 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004613 root_count = 1;
4614 init_task.cgroups = &init_css_set;
4615
4616 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004617 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004618 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004619 &rootnode.top_cgroup.css_sets);
4620 list_add(&init_css_set_link.cg_link_list,
4621 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004622
Li Zefan472b1052008-04-29 01:00:11 -07004623 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4624 INIT_HLIST_HEAD(&css_set_table[i]);
4625
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004626 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004627 struct cgroup_subsys *ss = subsys[i];
4628
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004629 /* at bootup time, we don't worry about modular subsystems */
4630 if (!ss || ss->module)
4631 continue;
4632
Paul Menageddbcc7e2007-10-18 23:39:30 -07004633 BUG_ON(!ss->name);
4634 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004635 BUG_ON(!ss->css_alloc);
4636 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004637 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004638 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639 ss->name, ss->subsys_id);
4640 BUG();
4641 }
4642
4643 if (ss->early_init)
4644 cgroup_init_subsys(ss);
4645 }
4646 return 0;
4647}
4648
4649/**
Li Zefana043e3b2008-02-23 15:24:09 -08004650 * cgroup_init - cgroup initialization
4651 *
4652 * Register cgroup filesystem and /proc file, and initialize
4653 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004654 */
4655int __init cgroup_init(void)
4656{
4657 int err;
4658 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004659 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004660
4661 err = bdi_init(&cgroup_backing_dev_info);
4662 if (err)
4663 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004664
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004665 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004666 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004667
4668 /* at bootup time, we don't worry about modular subsystems */
4669 if (!ss || ss->module)
4670 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004671 if (!ss->early_init)
4672 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004673 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004674 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004675 }
4676
Li Zefan472b1052008-04-29 01:00:11 -07004677 /* Add init_css_set to the hash table */
4678 hhead = css_set_hash(init_css_set.subsys);
4679 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004680 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004681
4682 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4683 if (!cgroup_kobj) {
4684 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004685 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004686 }
4687
4688 err = register_filesystem(&cgroup_fs_type);
4689 if (err < 0) {
4690 kobject_put(cgroup_kobj);
4691 goto out;
4692 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004693
Li Zefan46ae2202008-04-29 01:00:08 -07004694 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004695
Paul Menageddbcc7e2007-10-18 23:39:30 -07004696out:
Paul Menagea4243162007-10-18 23:39:35 -07004697 if (err)
4698 bdi_destroy(&cgroup_backing_dev_info);
4699
Paul Menageddbcc7e2007-10-18 23:39:30 -07004700 return err;
4701}
Paul Menageb4f48b62007-10-18 23:39:33 -07004702
Paul Menagea4243162007-10-18 23:39:35 -07004703/*
4704 * proc_cgroup_show()
4705 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4706 * - Used for /proc/<pid>/cgroup.
4707 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4708 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004709 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004710 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4711 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4712 * cgroup to top_cgroup.
4713 */
4714
4715/* TODO: Use a proper seq_file iterator */
4716static int proc_cgroup_show(struct seq_file *m, void *v)
4717{
4718 struct pid *pid;
4719 struct task_struct *tsk;
4720 char *buf;
4721 int retval;
4722 struct cgroupfs_root *root;
4723
4724 retval = -ENOMEM;
4725 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4726 if (!buf)
4727 goto out;
4728
4729 retval = -ESRCH;
4730 pid = m->private;
4731 tsk = get_pid_task(pid, PIDTYPE_PID);
4732 if (!tsk)
4733 goto out_free;
4734
4735 retval = 0;
4736
4737 mutex_lock(&cgroup_mutex);
4738
Li Zefane5f6a862009-01-07 18:07:41 -08004739 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004740 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004741 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004742 int count = 0;
4743
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004744 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004745 for_each_subsys(root, ss)
4746 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004747 if (strlen(root->name))
4748 seq_printf(m, "%sname=%s", count ? "," : "",
4749 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004750 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004751 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004752 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004753 if (retval < 0)
4754 goto out_unlock;
4755 seq_puts(m, buf);
4756 seq_putc(m, '\n');
4757 }
4758
4759out_unlock:
4760 mutex_unlock(&cgroup_mutex);
4761 put_task_struct(tsk);
4762out_free:
4763 kfree(buf);
4764out:
4765 return retval;
4766}
4767
4768static int cgroup_open(struct inode *inode, struct file *file)
4769{
4770 struct pid *pid = PROC_I(inode)->pid;
4771 return single_open(file, proc_cgroup_show, pid);
4772}
4773
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004774const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004775 .open = cgroup_open,
4776 .read = seq_read,
4777 .llseek = seq_lseek,
4778 .release = single_release,
4779};
4780
4781/* Display information about each subsystem and each hierarchy */
4782static int proc_cgroupstats_show(struct seq_file *m, void *v)
4783{
4784 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004785
Paul Menage8bab8dd2008-04-04 14:29:57 -07004786 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004787 /*
4788 * ideally we don't want subsystems moving around while we do this.
4789 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4790 * subsys/hierarchy state.
4791 */
Paul Menagea4243162007-10-18 23:39:35 -07004792 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004793 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4794 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004795 if (ss == NULL)
4796 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004797 seq_printf(m, "%s\t%d\t%d\t%d\n",
4798 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004799 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004800 }
4801 mutex_unlock(&cgroup_mutex);
4802 return 0;
4803}
4804
4805static int cgroupstats_open(struct inode *inode, struct file *file)
4806{
Al Viro9dce07f2008-03-29 03:07:28 +00004807 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004808}
4809
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004810static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004811 .open = cgroupstats_open,
4812 .read = seq_read,
4813 .llseek = seq_lseek,
4814 .release = single_release,
4815};
4816
Paul Menageb4f48b62007-10-18 23:39:33 -07004817/**
4818 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004819 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004820 *
4821 * Description: A task inherits its parent's cgroup at fork().
4822 *
4823 * A pointer to the shared css_set was automatically copied in
4824 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004825 * it was not made under the protection of RCU or cgroup_mutex, so
4826 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4827 * have already changed current->cgroups, allowing the previously
4828 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004829 *
4830 * At the point that cgroup_fork() is called, 'current' is the parent
4831 * task, and the passed argument 'child' points to the child task.
4832 */
4833void cgroup_fork(struct task_struct *child)
4834{
Tejun Heo9bb71302012-10-18 17:52:07 -07004835 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004836 child->cgroups = current->cgroups;
4837 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004838 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004839 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004840}
4841
4842/**
Li Zefana043e3b2008-02-23 15:24:09 -08004843 * cgroup_post_fork - called on a new task after adding it to the task list
4844 * @child: the task in question
4845 *
Tejun Heo5edee612012-10-16 15:03:14 -07004846 * Adds the task to the list running through its css_set if necessary and
4847 * call the subsystem fork() callbacks. Has to be after the task is
4848 * visible on the task list in case we race with the first call to
4849 * cgroup_iter_start() - to guarantee that the new task ends up on its
4850 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004851 */
Paul Menage817929e2007-10-18 23:39:36 -07004852void cgroup_post_fork(struct task_struct *child)
4853{
Tejun Heo5edee612012-10-16 15:03:14 -07004854 int i;
4855
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004856 /*
4857 * use_task_css_set_links is set to 1 before we walk the tasklist
4858 * under the tasklist_lock and we read it here after we added the child
4859 * to the tasklist under the tasklist_lock as well. If the child wasn't
4860 * yet in the tasklist when we walked through it from
4861 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4862 * should be visible now due to the paired locking and barriers implied
4863 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4864 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4865 * lock on fork.
4866 */
Paul Menage817929e2007-10-18 23:39:36 -07004867 if (use_task_css_set_links) {
4868 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004869 task_lock(child);
4870 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004871 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004872 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004873 write_unlock(&css_set_lock);
4874 }
Tejun Heo5edee612012-10-16 15:03:14 -07004875
4876 /*
4877 * Call ss->fork(). This must happen after @child is linked on
4878 * css_set; otherwise, @child might change state between ->fork()
4879 * and addition to css_set.
4880 */
4881 if (need_forkexit_callback) {
4882 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4883 struct cgroup_subsys *ss = subsys[i];
4884
4885 /*
4886 * fork/exit callbacks are supported only for
4887 * builtin subsystems and we don't need further
4888 * synchronization as they never go away.
4889 */
4890 if (!ss || ss->module)
4891 continue;
4892
4893 if (ss->fork)
4894 ss->fork(child);
4895 }
4896 }
Paul Menage817929e2007-10-18 23:39:36 -07004897}
Tejun Heo5edee612012-10-16 15:03:14 -07004898
Paul Menage817929e2007-10-18 23:39:36 -07004899/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004900 * cgroup_exit - detach cgroup from exiting task
4901 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004902 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004903 *
4904 * Description: Detach cgroup from @tsk and release it.
4905 *
4906 * Note that cgroups marked notify_on_release force every task in
4907 * them to take the global cgroup_mutex mutex when exiting.
4908 * This could impact scaling on very large systems. Be reluctant to
4909 * use notify_on_release cgroups where very high task exit scaling
4910 * is required on large systems.
4911 *
4912 * the_top_cgroup_hack:
4913 *
4914 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4915 *
4916 * We call cgroup_exit() while the task is still competent to
4917 * handle notify_on_release(), then leave the task attached to the
4918 * root cgroup in each hierarchy for the remainder of its exit.
4919 *
4920 * To do this properly, we would increment the reference count on
4921 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4922 * code we would add a second cgroup function call, to drop that
4923 * reference. This would just create an unnecessary hot spot on
4924 * the top_cgroup reference count, to no avail.
4925 *
4926 * Normally, holding a reference to a cgroup without bumping its
4927 * count is unsafe. The cgroup could go away, or someone could
4928 * attach us to a different cgroup, decrementing the count on
4929 * the first cgroup that we never incremented. But in this case,
4930 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004931 * which wards off any cgroup_attach_task() attempts, or task is a failed
4932 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004933 */
4934void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4935{
Paul Menage817929e2007-10-18 23:39:36 -07004936 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004937 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004938
4939 /*
4940 * Unlink from the css_set task list if necessary.
4941 * Optimistically check cg_list before taking
4942 * css_set_lock
4943 */
4944 if (!list_empty(&tsk->cg_list)) {
4945 write_lock(&css_set_lock);
4946 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004947 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004948 write_unlock(&css_set_lock);
4949 }
4950
Paul Menageb4f48b62007-10-18 23:39:33 -07004951 /* Reassign the task to the init_css_set. */
4952 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004953 cg = tsk->cgroups;
4954 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004955
4956 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004957 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004958 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004959
4960 /* modular subsystems can't use callbacks */
4961 if (!ss || ss->module)
4962 continue;
4963
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004964 if (ss->exit) {
4965 struct cgroup *old_cgrp =
4966 rcu_dereference_raw(cg->subsys[i])->cgroup;
4967 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004968 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004969 }
4970 }
4971 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004972 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004973
Paul Menage817929e2007-10-18 23:39:36 -07004974 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004975 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004976}
Paul Menage697f4162007-10-18 23:39:34 -07004977
4978/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004979 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004980 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004981 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004982 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004983 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4984 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004985 *
4986 * If we are sending in dummytop, then presumably we are creating
4987 * the top cgroup in the subsystem.
4988 *
4989 * Called only by the ns (nsproxy) cgroup.
4990 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004991int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004992{
4993 int ret;
4994 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004995
Paul Menagebd89aab2007-10-18 23:40:44 -07004996 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004997 return 1;
4998
Paul Menage7717f7b2009-09-23 15:56:22 -07004999 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005000 while (cgrp != target && cgrp!= cgrp->top_cgroup)
5001 cgrp = cgrp->parent;
5002 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07005003 return ret;
5004}
Paul Menage81a6a5c2007-10-18 23:39:38 -07005005
Paul Menagebd89aab2007-10-18 23:40:44 -07005006static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005007{
5008 /* All of these checks rely on RCU to keep the cgroup
5009 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07005010 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5011 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005012 /* Control Group is currently removeable. If it's not
5013 * already queued for a userspace notification, queue
5014 * it now */
5015 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005016 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005017 if (!cgroup_is_removed(cgrp) &&
5018 list_empty(&cgrp->release_list)) {
5019 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005020 need_schedule_work = 1;
5021 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005022 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005023 if (need_schedule_work)
5024 schedule_work(&release_agent_work);
5025 }
5026}
5027
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005028/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005029bool __css_tryget(struct cgroup_subsys_state *css)
5030{
Tejun Heoe9316082012-11-05 09:16:58 -08005031 while (true) {
5032 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005033
Tejun Heoe9316082012-11-05 09:16:58 -08005034 v = css_refcnt(css);
5035 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5036 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005037 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005038 else if (t < 0)
5039 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005040 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005041 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005042}
5043EXPORT_SYMBOL_GPL(__css_tryget);
5044
5045/* Caller must verify that the css is not for root cgroup */
5046void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005047{
Paul Menagebd89aab2007-10-18 23:40:44 -07005048 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005049 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005050
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005052 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5053
5054 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005055 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005056 if (notify_on_release(cgrp)) {
5057 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5058 check_for_release(cgrp);
5059 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005060 break;
5061 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005062 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005063 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005064 }
5065 rcu_read_unlock();
5066}
Ben Blum67523c42010-03-10 15:22:11 -08005067EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005068
5069/*
5070 * Notify userspace when a cgroup is released, by running the
5071 * configured release agent with the name of the cgroup (path
5072 * relative to the root of cgroup file system) as the argument.
5073 *
5074 * Most likely, this user command will try to rmdir this cgroup.
5075 *
5076 * This races with the possibility that some other task will be
5077 * attached to this cgroup before it is removed, or that some other
5078 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5079 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5080 * unused, and this cgroup will be reprieved from its death sentence,
5081 * to continue to serve a useful existence. Next time it's released,
5082 * we will get notified again, if it still has 'notify_on_release' set.
5083 *
5084 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5085 * means only wait until the task is successfully execve()'d. The
5086 * separate release agent task is forked by call_usermodehelper(),
5087 * then control in this thread returns here, without waiting for the
5088 * release agent task. We don't bother to wait because the caller of
5089 * this routine has no use for the exit status of the release agent
5090 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005091 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005092static void cgroup_release_agent(struct work_struct *work)
5093{
5094 BUG_ON(work != &release_agent_work);
5095 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005096 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005097 while (!list_empty(&release_list)) {
5098 char *argv[3], *envp[3];
5099 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005100 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005101 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005102 struct cgroup,
5103 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005104 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005105 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005106 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005107 if (!pathbuf)
5108 goto continue_free;
5109 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5110 goto continue_free;
5111 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5112 if (!agentbuf)
5113 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005114
5115 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005116 argv[i++] = agentbuf;
5117 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005118 argv[i] = NULL;
5119
5120 i = 0;
5121 /* minimal command environment */
5122 envp[i++] = "HOME=/";
5123 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5124 envp[i] = NULL;
5125
5126 /* Drop the lock while we invoke the usermode helper,
5127 * since the exec could involve hitting disk and hence
5128 * be a slow process */
5129 mutex_unlock(&cgroup_mutex);
5130 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005131 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005132 continue_free:
5133 kfree(pathbuf);
5134 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005135 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005136 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005137 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005138 mutex_unlock(&cgroup_mutex);
5139}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005140
5141static int __init cgroup_disable(char *str)
5142{
5143 int i;
5144 char *token;
5145
5146 while ((token = strsep(&str, ",")) != NULL) {
5147 if (!*token)
5148 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005149 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005150 struct cgroup_subsys *ss = subsys[i];
5151
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005152 /*
5153 * cgroup_disable, being at boot time, can't
5154 * know about module subsystems, so we don't
5155 * worry about them.
5156 */
5157 if (!ss || ss->module)
5158 continue;
5159
Paul Menage8bab8dd2008-04-04 14:29:57 -07005160 if (!strcmp(token, ss->name)) {
5161 ss->disabled = 1;
5162 printk(KERN_INFO "Disabling %s control group"
5163 " subsystem\n", ss->name);
5164 break;
5165 }
5166 }
5167 }
5168 return 1;
5169}
5170__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005171
5172/*
5173 * Functons for CSS ID.
5174 */
5175
5176/*
5177 *To get ID other than 0, this should be called when !cgroup_is_removed().
5178 */
5179unsigned short css_id(struct cgroup_subsys_state *css)
5180{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005181 struct css_id *cssid;
5182
5183 /*
5184 * This css_id() can return correct value when somone has refcnt
5185 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5186 * it's unchanged until freed.
5187 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005188 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005189
5190 if (cssid)
5191 return cssid->id;
5192 return 0;
5193}
Ben Blum67523c42010-03-10 15:22:11 -08005194EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005195
5196unsigned short css_depth(struct cgroup_subsys_state *css)
5197{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005198 struct css_id *cssid;
5199
Tejun Heo28b4c272012-04-01 12:09:56 -07005200 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005201
5202 if (cssid)
5203 return cssid->depth;
5204 return 0;
5205}
Ben Blum67523c42010-03-10 15:22:11 -08005206EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005207
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005208/**
5209 * css_is_ancestor - test "root" css is an ancestor of "child"
5210 * @child: the css to be tested.
5211 * @root: the css supporsed to be an ancestor of the child.
5212 *
5213 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005214 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005215 * But, considering usual usage, the csses should be valid objects after test.
5216 * Assuming that the caller will do some action to the child if this returns
5217 * returns true, the caller must take "child";s reference count.
5218 * If "child" is valid object and this returns true, "root" is valid, too.
5219 */
5220
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005221bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005222 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005223{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005224 struct css_id *child_id;
5225 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005226
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005227 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005228 if (!child_id)
5229 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005230 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005231 if (!root_id)
5232 return false;
5233 if (child_id->depth < root_id->depth)
5234 return false;
5235 if (child_id->stack[root_id->depth] != root_id->id)
5236 return false;
5237 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005238}
5239
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005240void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5241{
5242 struct css_id *id = css->id;
5243 /* When this is called before css_id initialization, id can be NULL */
5244 if (!id)
5245 return;
5246
5247 BUG_ON(!ss->use_id);
5248
5249 rcu_assign_pointer(id->css, NULL);
5250 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005251 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005252 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005253 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005254 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005255}
Ben Blum67523c42010-03-10 15:22:11 -08005256EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005257
5258/*
5259 * This is called by init or create(). Then, calls to this function are
5260 * always serialized (By cgroup_mutex() at create()).
5261 */
5262
5263static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5264{
5265 struct css_id *newid;
5266 int myid, error, size;
5267
5268 BUG_ON(!ss->use_id);
5269
5270 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5271 newid = kzalloc(size, GFP_KERNEL);
5272 if (!newid)
5273 return ERR_PTR(-ENOMEM);
5274 /* get id */
5275 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5276 error = -ENOMEM;
5277 goto err_out;
5278 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005279 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005280 /* Don't use 0. allocates an ID of 1-65535 */
5281 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005282 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005283
5284 /* Returns error when there are no free spaces for new ID.*/
5285 if (error) {
5286 error = -ENOSPC;
5287 goto err_out;
5288 }
5289 if (myid > CSS_ID_MAX)
5290 goto remove_idr;
5291
5292 newid->id = myid;
5293 newid->depth = depth;
5294 return newid;
5295remove_idr:
5296 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005297 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005298 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005299 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005300err_out:
5301 kfree(newid);
5302 return ERR_PTR(error);
5303
5304}
5305
Ben Blume6a11052010-03-10 15:22:09 -08005306static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5307 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005308{
5309 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005310
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005311 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005312 idr_init(&ss->idr);
5313
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005314 newid = get_new_cssid(ss, 0);
5315 if (IS_ERR(newid))
5316 return PTR_ERR(newid);
5317
5318 newid->stack[0] = newid->id;
5319 newid->css = rootcss;
5320 rootcss->id = newid;
5321 return 0;
5322}
5323
5324static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5325 struct cgroup *child)
5326{
5327 int subsys_id, i, depth = 0;
5328 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005329 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005330
5331 subsys_id = ss->subsys_id;
5332 parent_css = parent->subsys[subsys_id];
5333 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005334 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005335 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005336
5337 child_id = get_new_cssid(ss, depth);
5338 if (IS_ERR(child_id))
5339 return PTR_ERR(child_id);
5340
5341 for (i = 0; i < depth; i++)
5342 child_id->stack[i] = parent_id->stack[i];
5343 child_id->stack[depth] = child_id->id;
5344 /*
5345 * child_id->css pointer will be set after this cgroup is available
5346 * see cgroup_populate_dir()
5347 */
5348 rcu_assign_pointer(child_css->id, child_id);
5349
5350 return 0;
5351}
5352
5353/**
5354 * css_lookup - lookup css by id
5355 * @ss: cgroup subsys to be looked into.
5356 * @id: the id
5357 *
5358 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5359 * NULL if not. Should be called under rcu_read_lock()
5360 */
5361struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5362{
5363 struct css_id *cssid = NULL;
5364
5365 BUG_ON(!ss->use_id);
5366 cssid = idr_find(&ss->idr, id);
5367
5368 if (unlikely(!cssid))
5369 return NULL;
5370
5371 return rcu_dereference(cssid->css);
5372}
Ben Blum67523c42010-03-10 15:22:11 -08005373EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005374
5375/**
5376 * css_get_next - lookup next cgroup under specified hierarchy.
5377 * @ss: pointer to subsystem
5378 * @id: current position of iteration.
5379 * @root: pointer to css. search tree under this.
5380 * @foundid: position of found object.
5381 *
5382 * Search next css under the specified hierarchy of rootid. Calling under
5383 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5384 */
5385struct cgroup_subsys_state *
5386css_get_next(struct cgroup_subsys *ss, int id,
5387 struct cgroup_subsys_state *root, int *foundid)
5388{
5389 struct cgroup_subsys_state *ret = NULL;
5390 struct css_id *tmp;
5391 int tmpid;
5392 int rootid = css_id(root);
5393 int depth = css_depth(root);
5394
5395 if (!rootid)
5396 return NULL;
5397
5398 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005399 WARN_ON_ONCE(!rcu_read_lock_held());
5400
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005401 /* fill start point for scan */
5402 tmpid = id;
5403 while (1) {
5404 /*
5405 * scan next entry from bitmap(tree), tmpid is updated after
5406 * idr_get_next().
5407 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005408 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005409 if (!tmp)
5410 break;
5411 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5412 ret = rcu_dereference(tmp->css);
5413 if (ret) {
5414 *foundid = tmpid;
5415 break;
5416 }
5417 }
5418 /* continue to scan from next id */
5419 tmpid = tmpid + 1;
5420 }
5421 return ret;
5422}
5423
Stephane Eraniane5d13672011-02-14 11:20:01 +02005424/*
5425 * get corresponding css from file open on cgroupfs directory
5426 */
5427struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5428{
5429 struct cgroup *cgrp;
5430 struct inode *inode;
5431 struct cgroup_subsys_state *css;
5432
5433 inode = f->f_dentry->d_inode;
5434 /* check in cgroup filesystem dir */
5435 if (inode->i_op != &cgroup_dir_inode_operations)
5436 return ERR_PTR(-EBADF);
5437
5438 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5439 return ERR_PTR(-EINVAL);
5440
5441 /* get cgroup */
5442 cgrp = __d_cgrp(f->f_dentry);
5443 css = cgrp->subsys[id];
5444 return css ? css : ERR_PTR(-ENOENT);
5445}
5446
Paul Menagefe693432009-09-23 15:56:20 -07005447#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005448static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005449{
5450 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5451
5452 if (!css)
5453 return ERR_PTR(-ENOMEM);
5454
5455 return css;
5456}
5457
Tejun Heo92fb9742012-11-19 08:13:38 -08005458static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005459{
5460 kfree(cont->subsys[debug_subsys_id]);
5461}
5462
5463static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5464{
5465 return atomic_read(&cont->count);
5466}
5467
5468static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5469{
5470 return cgroup_task_count(cont);
5471}
5472
5473static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5474{
5475 return (u64)(unsigned long)current->cgroups;
5476}
5477
5478static u64 current_css_set_refcount_read(struct cgroup *cont,
5479 struct cftype *cft)
5480{
5481 u64 count;
5482
5483 rcu_read_lock();
5484 count = atomic_read(&current->cgroups->refcount);
5485 rcu_read_unlock();
5486 return count;
5487}
5488
Paul Menage7717f7b2009-09-23 15:56:22 -07005489static int current_css_set_cg_links_read(struct cgroup *cont,
5490 struct cftype *cft,
5491 struct seq_file *seq)
5492{
5493 struct cg_cgroup_link *link;
5494 struct css_set *cg;
5495
5496 read_lock(&css_set_lock);
5497 rcu_read_lock();
5498 cg = rcu_dereference(current->cgroups);
5499 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5500 struct cgroup *c = link->cgrp;
5501 const char *name;
5502
5503 if (c->dentry)
5504 name = c->dentry->d_name.name;
5505 else
5506 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005507 seq_printf(seq, "Root %d group %s\n",
5508 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005509 }
5510 rcu_read_unlock();
5511 read_unlock(&css_set_lock);
5512 return 0;
5513}
5514
5515#define MAX_TASKS_SHOWN_PER_CSS 25
5516static int cgroup_css_links_read(struct cgroup *cont,
5517 struct cftype *cft,
5518 struct seq_file *seq)
5519{
5520 struct cg_cgroup_link *link;
5521
5522 read_lock(&css_set_lock);
5523 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5524 struct css_set *cg = link->cg;
5525 struct task_struct *task;
5526 int count = 0;
5527 seq_printf(seq, "css_set %p\n", cg);
5528 list_for_each_entry(task, &cg->tasks, cg_list) {
5529 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5530 seq_puts(seq, " ...\n");
5531 break;
5532 } else {
5533 seq_printf(seq, " task %d\n",
5534 task_pid_vnr(task));
5535 }
5536 }
5537 }
5538 read_unlock(&css_set_lock);
5539 return 0;
5540}
5541
Paul Menagefe693432009-09-23 15:56:20 -07005542static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5543{
5544 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5545}
5546
5547static struct cftype debug_files[] = {
5548 {
5549 .name = "cgroup_refcount",
5550 .read_u64 = cgroup_refcount_read,
5551 },
5552 {
5553 .name = "taskcount",
5554 .read_u64 = debug_taskcount_read,
5555 },
5556
5557 {
5558 .name = "current_css_set",
5559 .read_u64 = current_css_set_read,
5560 },
5561
5562 {
5563 .name = "current_css_set_refcount",
5564 .read_u64 = current_css_set_refcount_read,
5565 },
5566
5567 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005568 .name = "current_css_set_cg_links",
5569 .read_seq_string = current_css_set_cg_links_read,
5570 },
5571
5572 {
5573 .name = "cgroup_css_links",
5574 .read_seq_string = cgroup_css_links_read,
5575 },
5576
5577 {
Paul Menagefe693432009-09-23 15:56:20 -07005578 .name = "releasable",
5579 .read_u64 = releasable_read,
5580 },
Paul Menagefe693432009-09-23 15:56:20 -07005581
Tejun Heo4baf6e32012-04-01 12:09:55 -07005582 { } /* terminate */
5583};
Paul Menagefe693432009-09-23 15:56:20 -07005584
5585struct cgroup_subsys debug_subsys = {
5586 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005587 .css_alloc = debug_css_alloc,
5588 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005589 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005590 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005591};
5592#endif /* CONFIG_CGROUP_DEBUG */