blob: 55a0a770a5a237645ec72a2706116742f2ddc454 [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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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
Tao Mad0b2fdd2012-11-20 22:06:18 +0800785 * cgroup_attach_task(), which overwrites one task's 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
Tao Mad0b2fdd2012-11-20 22:06:18 +0800790 * in cgroup_attach_task(), modifying a task's 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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-08-23 16:53:31 -04001000 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001{
Aristeu Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-08-23 16:53:31 -04001350 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001351 if (ret) {
Aristeu Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-08-23 16:53:31 -04001446 if ((opts->subsys_mask || opts->none)
1447 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001448 return 0;
1449
1450 return 1;
1451}
1452
Paul Menagec6d57f32009-09-23 15:56:19 -07001453static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1454{
1455 struct cgroupfs_root *root;
1456
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001457 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001458 return NULL;
1459
1460 root = kzalloc(sizeof(*root), GFP_KERNEL);
1461 if (!root)
1462 return ERR_PTR(-ENOMEM);
1463
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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 Rozanskia1a71b452012-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 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001885 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001886 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001887static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1888 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001889{
1890 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001891
1892 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001893 * We are synchronized through threadgroup_lock() against PF_EXITING
1894 * setting such that we can't race against cgroup_exit() changing the
1895 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001896 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001897 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001898 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001899
Ben Blum74a11662011-05-26 16:25:20 -07001900 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001901 rcu_assign_pointer(tsk->cgroups, newcg);
1902 task_unlock(tsk);
1903
1904 /* Update the css_set linked lists if we're using them */
1905 write_lock(&css_set_lock);
1906 if (!list_empty(&tsk->cg_list))
1907 list_move(&tsk->cg_list, &newcg->tasks);
1908 write_unlock(&css_set_lock);
1909
1910 /*
1911 * We just gained a reference on oldcg by taking it from the task. As
1912 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1913 * it here; it will be freed under RCU.
1914 */
Ben Blum74a11662011-05-26 16:25:20 -07001915 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001916 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001917}
1918
Li Zefana043e3b2008-02-23 15:24:09 -08001919/**
1920 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1921 * @cgrp: the cgroup the task is attaching to
1922 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001923 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001924 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1925 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001926 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001927int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001928{
Tejun Heo8f121912012-03-29 22:03:33 -07001929 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001930 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001931 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001932 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001933 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001934 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001935
Tejun Heocd3d0952011-12-12 18:12:21 -08001936 /* @tsk either already exited or can't exit until the end */
1937 if (tsk->flags & PF_EXITING)
1938 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001939
1940 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001941 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001942 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001943 return 0;
1944
Tejun Heo2f7ee562011-12-12 18:12:21 -08001945 tset.single.task = tsk;
1946 tset.single.cgrp = oldcgrp;
1947
Paul Menagebbcb81d2007-10-18 23:39:32 -07001948 for_each_subsys(root, ss) {
1949 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001950 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001951 if (retval) {
1952 /*
1953 * Remember on which subsystem the can_attach()
1954 * failed, so that we only call cancel_attach()
1955 * against the subsystems whose can_attach()
1956 * succeeded. (See below)
1957 */
1958 failed_ss = ss;
1959 goto out;
1960 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001961 }
1962 }
1963
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001964 newcg = find_css_set(tsk->cgroups, cgrp);
1965 if (!newcg) {
1966 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001967 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001968 }
1969
1970 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001971
Paul Menagebbcb81d2007-10-18 23:39:32 -07001972 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001973 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001974 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001975 }
Ben Blum74a11662011-05-26 16:25:20 -07001976
Paul Menagebbcb81d2007-10-18 23:39:32 -07001977 synchronize_rcu();
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001978out:
1979 if (retval) {
1980 for_each_subsys(root, ss) {
1981 if (ss == failed_ss)
1982 /*
1983 * This subsystem was the one that failed the
1984 * can_attach() check earlier, so we don't need
1985 * to call cancel_attach() against it or any
1986 * remaining subsystems.
1987 */
1988 break;
1989 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08001990 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001991 }
1992 }
1993 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001994}
1995
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001996/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001997 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1998 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001999 * @tsk: the task to be attached
2000 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002001int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002002{
2003 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002004 int retval = 0;
2005
2006 cgroup_lock();
2007 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002008 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2009
2010 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002011 if (retval)
2012 break;
2013 }
2014 cgroup_unlock();
2015
2016 return retval;
2017}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002018EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002019
Ben Blum74a11662011-05-26 16:25:20 -07002020/**
2021 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2022 * @cgrp: the cgroup to attach to
2023 * @leader: the threadgroup leader task_struct of the group to be attached
2024 *
Tejun Heo257058a2011-12-12 18:12:21 -08002025 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2026 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002027 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002028static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002029{
2030 int retval, i, group_size;
2031 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002032 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002033 struct cgroupfs_root *root = cgrp->root;
2034 /* threadgroup list cursor and array */
2035 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002036 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002037 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002038 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002039
2040 /*
2041 * step 0: in order to do expensive, possibly blocking operations for
2042 * every thread, we cannot iterate the thread group list, since it needs
2043 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002044 * group - group_rwsem prevents new threads from appearing, and if
2045 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002046 */
2047 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002048 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002049 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002050 if (!group)
2051 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002052 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2053 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2054 if (retval)
2055 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002056
Ben Blum74a11662011-05-26 16:25:20 -07002057 tsk = leader;
2058 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002059 /*
2060 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2061 * already PF_EXITING could be freed from underneath us unless we
2062 * take an rcu_read_lock.
2063 */
2064 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002065 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002066 struct task_and_cgroup ent;
2067
Tejun Heocd3d0952011-12-12 18:12:21 -08002068 /* @tsk either already exited or can't exit until the end */
2069 if (tsk->flags & PF_EXITING)
2070 continue;
2071
Ben Blum74a11662011-05-26 16:25:20 -07002072 /* as per above, nr_threads may decrease, but not increase. */
2073 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002074 ent.task = tsk;
2075 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002076 /* nothing to do if this task is already in the cgroup */
2077 if (ent.cgrp == cgrp)
2078 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002079 /*
2080 * saying GFP_ATOMIC has no effect here because we did prealloc
2081 * earlier, but it's good form to communicate our expectations.
2082 */
Tejun Heo134d3372011-12-12 18:12:21 -08002083 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002084 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002085 i++;
2086 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002087 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002088 /* remember the number of threads in the array for later. */
2089 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002090 tset.tc_array = group;
2091 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002092
Tejun Heo134d3372011-12-12 18:12:21 -08002093 /* methods shouldn't be called if no task is actually migrating */
2094 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002095 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002096 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002097
Ben Blum74a11662011-05-26 16:25:20 -07002098 /*
2099 * step 1: check that we can legitimately attach to the cgroup.
2100 */
2101 for_each_subsys(root, ss) {
2102 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002103 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002104 if (retval) {
2105 failed_ss = ss;
2106 goto out_cancel_attach;
2107 }
2108 }
Ben Blum74a11662011-05-26 16:25:20 -07002109 }
2110
2111 /*
2112 * step 2: make sure css_sets exist for all threads to be migrated.
2113 * we use find_css_set, which allocates a new one if necessary.
2114 */
Ben Blum74a11662011-05-26 16:25:20 -07002115 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002116 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002117 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2118 if (!tc->cg) {
2119 retval = -ENOMEM;
2120 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002121 }
2122 }
2123
2124 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002125 * step 3: now that we're guaranteed success wrt the css_sets,
2126 * proceed to move all tasks to the new cgroup. There are no
2127 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002128 */
Ben Blum74a11662011-05-26 16:25:20 -07002129 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002130 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002131 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002132 }
2133 /* nothing is sensitive to fork() after this point. */
2134
2135 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002136 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002137 */
2138 for_each_subsys(root, ss) {
2139 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002140 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002141 }
2142
2143 /*
2144 * step 5: success! and cleanup
2145 */
2146 synchronize_rcu();
Ben Blum74a11662011-05-26 16:25:20 -07002147 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002148out_put_css_set_refs:
2149 if (retval) {
2150 for (i = 0; i < group_size; i++) {
2151 tc = flex_array_get(group, i);
2152 if (!tc->cg)
2153 break;
2154 put_css_set(tc->cg);
2155 }
Ben Blum74a11662011-05-26 16:25:20 -07002156 }
2157out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002158 if (retval) {
2159 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002160 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002161 break;
Ben Blum74a11662011-05-26 16:25:20 -07002162 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002163 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002164 }
2165 }
Ben Blum74a11662011-05-26 16:25:20 -07002166out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002167 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002168 return retval;
2169}
2170
2171/*
2172 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002173 * function to attach either it or all tasks in its threadgroup. Will lock
2174 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002175 */
2176static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002177{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002178 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002179 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002180 int ret;
2181
Ben Blum74a11662011-05-26 16:25:20 -07002182 if (!cgroup_lock_live_group(cgrp))
2183 return -ENODEV;
2184
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002185retry_find_task:
2186 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002188 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002189 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002190 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002191 ret= -ESRCH;
2192 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002193 }
Ben Blum74a11662011-05-26 16:25:20 -07002194 /*
2195 * even if we're attaching all tasks in the thread group, we
2196 * only need to check permissions on one of them.
2197 */
David Howellsc69e8d92008-11-14 10:39:19 +11002198 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002199 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2200 !uid_eq(cred->euid, tcred->uid) &&
2201 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002202 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002203 ret = -EACCES;
2204 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002205 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002206 } else
2207 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002208
2209 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002210 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002211
2212 /*
2213 * Workqueue threads may acquire PF_THREAD_BOUND and become
2214 * trapped in a cpuset, or RT worker may be born in a cgroup
2215 * with no rt_runtime allocated. Just say no.
2216 */
2217 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2218 ret = -EINVAL;
2219 rcu_read_unlock();
2220 goto out_unlock_cgroup;
2221 }
2222
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002223 get_task_struct(tsk);
2224 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002225
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002226 threadgroup_lock(tsk);
2227 if (threadgroup) {
2228 if (!thread_group_leader(tsk)) {
2229 /*
2230 * a race with de_thread from another thread's exec()
2231 * may strip us of our leadership, if this happens,
2232 * there is no choice but to throw this task away and
2233 * try again; this is
2234 * "double-double-toil-and-trouble-check locking".
2235 */
2236 threadgroup_unlock(tsk);
2237 put_task_struct(tsk);
2238 goto retry_find_task;
2239 }
2240 ret = cgroup_attach_proc(cgrp, tsk);
2241 } else
2242 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002243 threadgroup_unlock(tsk);
2244
Paul Menagebbcb81d2007-10-18 23:39:32 -07002245 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002246out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002247 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002248 return ret;
2249}
2250
Paul Menageaf351022008-07-25 01:47:01 -07002251static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2252{
Ben Blum74a11662011-05-26 16:25:20 -07002253 return attach_task_by_pid(cgrp, pid, false);
2254}
2255
2256static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2257{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002258 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002259}
2260
Paul Menagee788e062008-07-25 01:46:59 -07002261/**
2262 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2263 * @cgrp: the cgroup to be checked for liveness
2264 *
Paul Menage84eea842008-07-25 01:47:00 -07002265 * On success, returns true; the lock should be later released with
2266 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002267 */
Paul Menage84eea842008-07-25 01:47:00 -07002268bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002269{
2270 mutex_lock(&cgroup_mutex);
2271 if (cgroup_is_removed(cgrp)) {
2272 mutex_unlock(&cgroup_mutex);
2273 return false;
2274 }
2275 return true;
2276}
Ben Blum67523c42010-03-10 15:22:11 -08002277EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002278
2279static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2280 const char *buffer)
2281{
2282 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002283 if (strlen(buffer) >= PATH_MAX)
2284 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002285 if (!cgroup_lock_live_group(cgrp))
2286 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002287 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002288 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002289 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002290 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002291 return 0;
2292}
2293
2294static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2295 struct seq_file *seq)
2296{
2297 if (!cgroup_lock_live_group(cgrp))
2298 return -ENODEV;
2299 seq_puts(seq, cgrp->root->release_agent_path);
2300 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002301 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002302 return 0;
2303}
2304
Paul Menage84eea842008-07-25 01:47:00 -07002305/* A buffer size big enough for numbers or short strings */
2306#define CGROUP_LOCAL_BUFFER_SIZE 64
2307
Paul Menagee73d2c62008-04-29 01:00:06 -07002308static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002309 struct file *file,
2310 const char __user *userbuf,
2311 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002312{
Paul Menage84eea842008-07-25 01:47:00 -07002313 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002314 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002315 char *end;
2316
2317 if (!nbytes)
2318 return -EINVAL;
2319 if (nbytes >= sizeof(buffer))
2320 return -E2BIG;
2321 if (copy_from_user(buffer, userbuf, nbytes))
2322 return -EFAULT;
2323
2324 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002325 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002326 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002327 if (*end)
2328 return -EINVAL;
2329 retval = cft->write_u64(cgrp, cft, val);
2330 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002331 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002332 if (*end)
2333 return -EINVAL;
2334 retval = cft->write_s64(cgrp, cft, val);
2335 }
Paul Menage355e0c42007-10-18 23:39:33 -07002336 if (!retval)
2337 retval = nbytes;
2338 return retval;
2339}
2340
Paul Menagedb3b1492008-07-25 01:46:58 -07002341static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2342 struct file *file,
2343 const char __user *userbuf,
2344 size_t nbytes, loff_t *unused_ppos)
2345{
Paul Menage84eea842008-07-25 01:47:00 -07002346 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002347 int retval = 0;
2348 size_t max_bytes = cft->max_write_len;
2349 char *buffer = local_buffer;
2350
2351 if (!max_bytes)
2352 max_bytes = sizeof(local_buffer) - 1;
2353 if (nbytes >= max_bytes)
2354 return -E2BIG;
2355 /* Allocate a dynamic buffer if we need one */
2356 if (nbytes >= sizeof(local_buffer)) {
2357 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2358 if (buffer == NULL)
2359 return -ENOMEM;
2360 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002361 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2362 retval = -EFAULT;
2363 goto out;
2364 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002365
2366 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002367 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002368 if (!retval)
2369 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002370out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002371 if (buffer != local_buffer)
2372 kfree(buffer);
2373 return retval;
2374}
2375
Paul Menageddbcc7e2007-10-18 23:39:30 -07002376static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2377 size_t nbytes, loff_t *ppos)
2378{
2379 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002380 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002381
Li Zefan75139b82009-01-07 18:07:33 -08002382 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002383 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002384 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002385 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002386 if (cft->write_u64 || cft->write_s64)
2387 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002388 if (cft->write_string)
2389 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002390 if (cft->trigger) {
2391 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2392 return ret ? ret : nbytes;
2393 }
Paul Menage355e0c42007-10-18 23:39:33 -07002394 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002395}
2396
Paul Menagef4c753b2008-04-29 00:59:56 -07002397static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2398 struct file *file,
2399 char __user *buf, size_t nbytes,
2400 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401{
Paul Menage84eea842008-07-25 01:47:00 -07002402 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002403 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2405
2406 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2407}
2408
Paul Menagee73d2c62008-04-29 01:00:06 -07002409static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2410 struct file *file,
2411 char __user *buf, size_t nbytes,
2412 loff_t *ppos)
2413{
Paul Menage84eea842008-07-25 01:47:00 -07002414 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002415 s64 val = cft->read_s64(cgrp, cft);
2416 int len = sprintf(tmp, "%lld\n", (long long) val);
2417
2418 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2419}
2420
Paul Menageddbcc7e2007-10-18 23:39:30 -07002421static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2422 size_t nbytes, loff_t *ppos)
2423{
2424 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002425 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002426
Li Zefan75139b82009-01-07 18:07:33 -08002427 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428 return -ENODEV;
2429
2430 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002431 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002432 if (cft->read_u64)
2433 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002434 if (cft->read_s64)
2435 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002436 return -EINVAL;
2437}
2438
Paul Menage91796562008-04-29 01:00:01 -07002439/*
2440 * seqfile ops/methods for returning structured data. Currently just
2441 * supports string->u64 maps, but can be extended in future.
2442 */
2443
2444struct cgroup_seqfile_state {
2445 struct cftype *cft;
2446 struct cgroup *cgroup;
2447};
2448
2449static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2450{
2451 struct seq_file *sf = cb->state;
2452 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2453}
2454
2455static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2456{
2457 struct cgroup_seqfile_state *state = m->private;
2458 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002459 if (cft->read_map) {
2460 struct cgroup_map_cb cb = {
2461 .fill = cgroup_map_add,
2462 .state = m,
2463 };
2464 return cft->read_map(state->cgroup, cft, &cb);
2465 }
2466 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002467}
2468
Adrian Bunk96930a62008-07-25 19:46:21 -07002469static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002470{
2471 struct seq_file *seq = file->private_data;
2472 kfree(seq->private);
2473 return single_release(inode, file);
2474}
2475
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002476static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002477 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002478 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002479 .llseek = seq_lseek,
2480 .release = cgroup_seqfile_release,
2481};
2482
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483static int cgroup_file_open(struct inode *inode, struct file *file)
2484{
2485 int err;
2486 struct cftype *cft;
2487
2488 err = generic_file_open(inode, file);
2489 if (err)
2490 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002491 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002492
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002493 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002494 struct cgroup_seqfile_state *state =
2495 kzalloc(sizeof(*state), GFP_USER);
2496 if (!state)
2497 return -ENOMEM;
2498 state->cft = cft;
2499 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2500 file->f_op = &cgroup_seqfile_operations;
2501 err = single_open(file, cgroup_seqfile_show, state);
2502 if (err < 0)
2503 kfree(state);
2504 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002505 err = cft->open(inode, file);
2506 else
2507 err = 0;
2508
2509 return err;
2510}
2511
2512static int cgroup_file_release(struct inode *inode, struct file *file)
2513{
2514 struct cftype *cft = __d_cft(file->f_dentry);
2515 if (cft->release)
2516 return cft->release(inode, file);
2517 return 0;
2518}
2519
2520/*
2521 * cgroup_rename - Only allow simple rename of directories in place.
2522 */
2523static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2524 struct inode *new_dir, struct dentry *new_dentry)
2525{
2526 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2527 return -ENOTDIR;
2528 if (new_dentry->d_inode)
2529 return -EEXIST;
2530 if (old_dir != new_dir)
2531 return -EIO;
2532 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2533}
2534
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002535static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2536{
2537 if (S_ISDIR(dentry->d_inode->i_mode))
2538 return &__d_cgrp(dentry)->xattrs;
2539 else
2540 return &__d_cft(dentry)->xattrs;
2541}
2542
2543static inline int xattr_enabled(struct dentry *dentry)
2544{
2545 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2546 return test_bit(ROOT_XATTR, &root->flags);
2547}
2548
2549static bool is_valid_xattr(const char *name)
2550{
2551 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2552 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2553 return true;
2554 return false;
2555}
2556
2557static int cgroup_setxattr(struct dentry *dentry, const char *name,
2558 const void *val, size_t size, int flags)
2559{
2560 if (!xattr_enabled(dentry))
2561 return -EOPNOTSUPP;
2562 if (!is_valid_xattr(name))
2563 return -EINVAL;
2564 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2565}
2566
2567static int cgroup_removexattr(struct dentry *dentry, const char *name)
2568{
2569 if (!xattr_enabled(dentry))
2570 return -EOPNOTSUPP;
2571 if (!is_valid_xattr(name))
2572 return -EINVAL;
2573 return simple_xattr_remove(__d_xattrs(dentry), name);
2574}
2575
2576static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2577 void *buf, size_t size)
2578{
2579 if (!xattr_enabled(dentry))
2580 return -EOPNOTSUPP;
2581 if (!is_valid_xattr(name))
2582 return -EINVAL;
2583 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2584}
2585
2586static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2587{
2588 if (!xattr_enabled(dentry))
2589 return -EOPNOTSUPP;
2590 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2591}
2592
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002593static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002594 .read = cgroup_file_read,
2595 .write = cgroup_file_write,
2596 .llseek = generic_file_llseek,
2597 .open = cgroup_file_open,
2598 .release = cgroup_file_release,
2599};
2600
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002601static const struct inode_operations cgroup_file_inode_operations = {
2602 .setxattr = cgroup_setxattr,
2603 .getxattr = cgroup_getxattr,
2604 .listxattr = cgroup_listxattr,
2605 .removexattr = cgroup_removexattr,
2606};
2607
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002608static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002609 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002610 .mkdir = cgroup_mkdir,
2611 .rmdir = cgroup_rmdir,
2612 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002613 .setxattr = cgroup_setxattr,
2614 .getxattr = cgroup_getxattr,
2615 .listxattr = cgroup_listxattr,
2616 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002617};
2618
Al Viro00cd8dd2012-06-10 17:13:09 -04002619static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002620{
2621 if (dentry->d_name.len > NAME_MAX)
2622 return ERR_PTR(-ENAMETOOLONG);
2623 d_add(dentry, NULL);
2624 return NULL;
2625}
2626
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002627/*
2628 * Check if a file is a control file
2629 */
2630static inline struct cftype *__file_cft(struct file *file)
2631{
2632 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2633 return ERR_PTR(-EINVAL);
2634 return __d_cft(file->f_dentry);
2635}
2636
Al Viroa5e7ed32011-07-26 01:55:55 -04002637static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002638 struct super_block *sb)
2639{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002640 struct inode *inode;
2641
2642 if (!dentry)
2643 return -ENOENT;
2644 if (dentry->d_inode)
2645 return -EEXIST;
2646
2647 inode = cgroup_new_inode(mode, sb);
2648 if (!inode)
2649 return -ENOMEM;
2650
2651 if (S_ISDIR(mode)) {
2652 inode->i_op = &cgroup_dir_inode_operations;
2653 inode->i_fop = &simple_dir_operations;
2654
2655 /* start off with i_nlink == 2 (for "." entry) */
2656 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002657 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002658
Tejun Heob8a2df62012-11-19 08:13:37 -08002659 /*
2660 * Control reaches here with cgroup_mutex held.
2661 * @inode->i_mutex should nest outside cgroup_mutex but we
2662 * want to populate it immediately without releasing
2663 * cgroup_mutex. As @inode isn't visible to anyone else
2664 * yet, trylock will always succeed without affecting
2665 * lockdep checks.
2666 */
2667 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002668 } else if (S_ISREG(mode)) {
2669 inode->i_size = 0;
2670 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002671 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002672 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 d_instantiate(dentry, inode);
2674 dget(dentry); /* Extra count - pin the dentry in core */
2675 return 0;
2676}
2677
Li Zefan099fca32009-04-02 16:57:29 -07002678/**
2679 * cgroup_file_mode - deduce file mode of a control file
2680 * @cft: the control file in question
2681 *
2682 * returns cft->mode if ->mode is not 0
2683 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2684 * returns S_IRUGO if it has only a read handler
2685 * returns S_IWUSR if it has only a write hander
2686 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002687static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002688{
Al Viroa5e7ed32011-07-26 01:55:55 -04002689 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002690
2691 if (cft->mode)
2692 return cft->mode;
2693
2694 if (cft->read || cft->read_u64 || cft->read_s64 ||
2695 cft->read_map || cft->read_seq_string)
2696 mode |= S_IRUGO;
2697
2698 if (cft->write || cft->write_u64 || cft->write_s64 ||
2699 cft->write_string || cft->trigger)
2700 mode |= S_IWUSR;
2701
2702 return mode;
2703}
2704
Tejun Heodb0416b2012-04-01 12:09:55 -07002705static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002706 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002707{
Paul Menagebd89aab2007-10-18 23:40:44 -07002708 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002709 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002710 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002711 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002713 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002714 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002715
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002716 simple_xattrs_init(&cft->xattrs);
2717
Tejun Heo8e3f6542012-04-01 12:09:55 -07002718 /* does @cft->flags tell us to skip creation on @cgrp? */
2719 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2720 return 0;
2721 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2722 return 0;
2723
Paul Menagebd89aab2007-10-18 23:40:44 -07002724 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002725 strcpy(name, subsys->name);
2726 strcat(name, ".");
2727 }
2728 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002729
Paul Menageddbcc7e2007-10-18 23:39:30 -07002730 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002731
2732 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2733 if (!cfe)
2734 return -ENOMEM;
2735
Paul Menageddbcc7e2007-10-18 23:39:30 -07002736 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002737 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002739 goto out;
2740 }
2741
2742 mode = cgroup_file_mode(cft);
2743 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2744 if (!error) {
2745 cfe->type = (void *)cft;
2746 cfe->dentry = dentry;
2747 dentry->d_fsdata = cfe;
2748 list_add_tail(&cfe->node, &parent->files);
2749 cfe = NULL;
2750 }
2751 dput(dentry);
2752out:
2753 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754 return error;
2755}
2756
Tejun Heo79578622012-04-01 12:09:56 -07002757static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002758 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002759{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002760 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002761 int err, ret = 0;
2762
2763 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Tejun Heo79578622012-04-01 12:09:56 -07002764 if (is_add)
2765 err = cgroup_add_file(cgrp, subsys, cft);
2766 else
2767 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002768 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002769 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2770 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002771 ret = err;
2772 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002773 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002774 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002775}
2776
Tejun Heo8e3f6542012-04-01 12:09:55 -07002777static DEFINE_MUTEX(cgroup_cft_mutex);
2778
2779static void cgroup_cfts_prepare(void)
2780 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2781{
2782 /*
2783 * Thanks to the entanglement with vfs inode locking, we can't walk
2784 * the existing cgroups under cgroup_mutex and create files.
2785 * Instead, we increment reference on all cgroups and build list of
2786 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2787 * exclusive access to the field.
2788 */
2789 mutex_lock(&cgroup_cft_mutex);
2790 mutex_lock(&cgroup_mutex);
2791}
2792
2793static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002794 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002795 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2796{
2797 LIST_HEAD(pending);
2798 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002799
2800 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2801 if (cfts && ss->root != &rootnode) {
2802 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2803 dget(cgrp->dentry);
2804 list_add_tail(&cgrp->cft_q_node, &pending);
2805 }
2806 }
2807
2808 mutex_unlock(&cgroup_mutex);
2809
2810 /*
2811 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2812 * files for all cgroups which were created before.
2813 */
2814 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2815 struct inode *inode = cgrp->dentry->d_inode;
2816
2817 mutex_lock(&inode->i_mutex);
2818 mutex_lock(&cgroup_mutex);
2819 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002820 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002821 mutex_unlock(&cgroup_mutex);
2822 mutex_unlock(&inode->i_mutex);
2823
2824 list_del_init(&cgrp->cft_q_node);
2825 dput(cgrp->dentry);
2826 }
2827
2828 mutex_unlock(&cgroup_cft_mutex);
2829}
2830
2831/**
2832 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2833 * @ss: target cgroup subsystem
2834 * @cfts: zero-length name terminated array of cftypes
2835 *
2836 * Register @cfts to @ss. Files described by @cfts are created for all
2837 * existing cgroups to which @ss is attached and all future cgroups will
2838 * have them too. This function can be called anytime whether @ss is
2839 * attached or not.
2840 *
2841 * Returns 0 on successful registration, -errno on failure. Note that this
2842 * function currently returns 0 as long as @cfts registration is successful
2843 * even if some file creation attempts on existing cgroups fail.
2844 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002845int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002846{
2847 struct cftype_set *set;
2848
2849 set = kzalloc(sizeof(*set), GFP_KERNEL);
2850 if (!set)
2851 return -ENOMEM;
2852
2853 cgroup_cfts_prepare();
2854 set->cfts = cfts;
2855 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002856 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002857
2858 return 0;
2859}
2860EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2861
Li Zefana043e3b2008-02-23 15:24:09 -08002862/**
Tejun Heo79578622012-04-01 12:09:56 -07002863 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2864 * @ss: target cgroup subsystem
2865 * @cfts: zero-length name terminated array of cftypes
2866 *
2867 * Unregister @cfts from @ss. Files described by @cfts are removed from
2868 * all existing cgroups to which @ss is attached and all future cgroups
2869 * won't have them either. This function can be called anytime whether @ss
2870 * is attached or not.
2871 *
2872 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2873 * registered with @ss.
2874 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002875int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002876{
2877 struct cftype_set *set;
2878
2879 cgroup_cfts_prepare();
2880
2881 list_for_each_entry(set, &ss->cftsets, node) {
2882 if (set->cfts == cfts) {
2883 list_del_init(&set->node);
2884 cgroup_cfts_commit(ss, cfts, false);
2885 return 0;
2886 }
2887 }
2888
2889 cgroup_cfts_commit(ss, NULL, false);
2890 return -ENOENT;
2891}
2892
2893/**
Li Zefana043e3b2008-02-23 15:24:09 -08002894 * cgroup_task_count - count the number of tasks in a cgroup.
2895 * @cgrp: the cgroup in question
2896 *
2897 * Return the number of tasks in the cgroup.
2898 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002899int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002900{
2901 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002902 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002903
Paul Menage817929e2007-10-18 23:39:36 -07002904 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002905 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002906 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002907 }
2908 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002909 return count;
2910}
2911
2912/*
Paul Menage817929e2007-10-18 23:39:36 -07002913 * Advance a list_head iterator. The iterator should be positioned at
2914 * the start of a css_set
2915 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002916static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002917 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002918{
2919 struct list_head *l = it->cg_link;
2920 struct cg_cgroup_link *link;
2921 struct css_set *cg;
2922
2923 /* Advance to the next non-empty css_set */
2924 do {
2925 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002926 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002927 it->cg_link = NULL;
2928 return;
2929 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002930 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002931 cg = link->cg;
2932 } while (list_empty(&cg->tasks));
2933 it->cg_link = l;
2934 it->task = cg->tasks.next;
2935}
2936
Cliff Wickman31a7df02008-02-07 00:14:42 -08002937/*
2938 * To reduce the fork() overhead for systems that are not actually
2939 * using their cgroups capability, we don't maintain the lists running
2940 * through each css_set to its tasks until we see the list actually
2941 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002942 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002943static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002944{
2945 struct task_struct *p, *g;
2946 write_lock(&css_set_lock);
2947 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002948 /*
2949 * We need tasklist_lock because RCU is not safe against
2950 * while_each_thread(). Besides, a forking task that has passed
2951 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2952 * is not guaranteed to have its child immediately visible in the
2953 * tasklist if we walk through it with RCU.
2954 */
2955 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002956 do_each_thread(g, p) {
2957 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002958 /*
2959 * We should check if the process is exiting, otherwise
2960 * it will race with cgroup_exit() in that the list
2961 * entry won't be deleted though the process has exited.
2962 */
2963 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002964 list_add(&p->cg_list, &p->cgroups->tasks);
2965 task_unlock(p);
2966 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002967 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002968 write_unlock(&css_set_lock);
2969}
2970
Tejun Heo574bd9f2012-11-09 09:12:29 -08002971/**
2972 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2973 * @pos: the current position (%NULL to initiate traversal)
2974 * @cgroup: cgroup whose descendants to walk
2975 *
2976 * To be used by cgroup_for_each_descendant_pre(). Find the next
2977 * descendant to visit for pre-order traversal of @cgroup's descendants.
2978 */
2979struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2980 struct cgroup *cgroup)
2981{
2982 struct cgroup *next;
2983
2984 WARN_ON_ONCE(!rcu_read_lock_held());
2985
2986 /* if first iteration, pretend we just visited @cgroup */
2987 if (!pos) {
2988 if (list_empty(&cgroup->children))
2989 return NULL;
2990 pos = cgroup;
2991 }
2992
2993 /* visit the first child if exists */
2994 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2995 if (next)
2996 return next;
2997
2998 /* no child, visit my or the closest ancestor's next sibling */
2999 do {
3000 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3001 sibling);
3002 if (&next->sibling != &pos->parent->children)
3003 return next;
3004
3005 pos = pos->parent;
3006 } while (pos != cgroup);
3007
3008 return NULL;
3009}
3010EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3011
3012static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3013{
3014 struct cgroup *last;
3015
3016 do {
3017 last = pos;
3018 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3019 sibling);
3020 } while (pos);
3021
3022 return last;
3023}
3024
3025/**
3026 * cgroup_next_descendant_post - find the next descendant for post-order walk
3027 * @pos: the current position (%NULL to initiate traversal)
3028 * @cgroup: cgroup whose descendants to walk
3029 *
3030 * To be used by cgroup_for_each_descendant_post(). Find the next
3031 * descendant to visit for post-order traversal of @cgroup's descendants.
3032 */
3033struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3034 struct cgroup *cgroup)
3035{
3036 struct cgroup *next;
3037
3038 WARN_ON_ONCE(!rcu_read_lock_held());
3039
3040 /* if first iteration, visit the leftmost descendant */
3041 if (!pos) {
3042 next = cgroup_leftmost_descendant(cgroup);
3043 return next != cgroup ? next : NULL;
3044 }
3045
3046 /* if there's an unvisited sibling, visit its leftmost descendant */
3047 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3048 if (&next->sibling != &pos->parent->children)
3049 return cgroup_leftmost_descendant(next);
3050
3051 /* no sibling left, visit parent */
3052 next = pos->parent;
3053 return next != cgroup ? next : NULL;
3054}
3055EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3056
Paul Menagebd89aab2007-10-18 23:40:44 -07003057void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003058 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003059{
3060 /*
3061 * The first time anyone tries to iterate across a cgroup,
3062 * we need to enable the list linking each css_set to its
3063 * tasks, and fix up all existing tasks.
3064 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003065 if (!use_task_css_set_links)
3066 cgroup_enable_task_cg_lists();
3067
Paul Menage817929e2007-10-18 23:39:36 -07003068 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003069 it->cg_link = &cgrp->css_sets;
3070 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003071}
3072
Paul Menagebd89aab2007-10-18 23:40:44 -07003073struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003074 struct cgroup_iter *it)
3075{
3076 struct task_struct *res;
3077 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003078 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003079
3080 /* If the iterator cg is NULL, we have no tasks */
3081 if (!it->cg_link)
3082 return NULL;
3083 res = list_entry(l, struct task_struct, cg_list);
3084 /* Advance iterator to find next entry */
3085 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003086 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3087 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003088 /* We reached the end of this task list - move on to
3089 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003090 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003091 } else {
3092 it->task = l;
3093 }
3094 return res;
3095}
3096
Paul Menagebd89aab2007-10-18 23:40:44 -07003097void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003098 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003099{
3100 read_unlock(&css_set_lock);
3101}
3102
Cliff Wickman31a7df02008-02-07 00:14:42 -08003103static inline int started_after_time(struct task_struct *t1,
3104 struct timespec *time,
3105 struct task_struct *t2)
3106{
3107 int start_diff = timespec_compare(&t1->start_time, time);
3108 if (start_diff > 0) {
3109 return 1;
3110 } else if (start_diff < 0) {
3111 return 0;
3112 } else {
3113 /*
3114 * Arbitrarily, if two processes started at the same
3115 * time, we'll say that the lower pointer value
3116 * started first. Note that t2 may have exited by now
3117 * so this may not be a valid pointer any longer, but
3118 * that's fine - it still serves to distinguish
3119 * between two tasks started (effectively) simultaneously.
3120 */
3121 return t1 > t2;
3122 }
3123}
3124
3125/*
3126 * This function is a callback from heap_insert() and is used to order
3127 * the heap.
3128 * In this case we order the heap in descending task start time.
3129 */
3130static inline int started_after(void *p1, void *p2)
3131{
3132 struct task_struct *t1 = p1;
3133 struct task_struct *t2 = p2;
3134 return started_after_time(t1, &t2->start_time, t2);
3135}
3136
3137/**
3138 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3139 * @scan: struct cgroup_scanner containing arguments for the scan
3140 *
3141 * Arguments include pointers to callback functions test_task() and
3142 * process_task().
3143 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3144 * and if it returns true, call process_task() for it also.
3145 * The test_task pointer may be NULL, meaning always true (select all tasks).
3146 * Effectively duplicates cgroup_iter_{start,next,end}()
3147 * but does not lock css_set_lock for the call to process_task().
3148 * The struct cgroup_scanner may be embedded in any structure of the caller's
3149 * creation.
3150 * It is guaranteed that process_task() will act on every task that
3151 * is a member of the cgroup for the duration of this call. This
3152 * function may or may not call process_task() for tasks that exit
3153 * or move to a different cgroup during the call, or are forked or
3154 * move into the cgroup during the call.
3155 *
3156 * Note that test_task() may be called with locks held, and may in some
3157 * situations be called multiple times for the same task, so it should
3158 * be cheap.
3159 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3160 * pre-allocated and will be used for heap operations (and its "gt" member will
3161 * be overwritten), else a temporary heap will be used (allocation of which
3162 * may cause this function to fail).
3163 */
3164int cgroup_scan_tasks(struct cgroup_scanner *scan)
3165{
3166 int retval, i;
3167 struct cgroup_iter it;
3168 struct task_struct *p, *dropped;
3169 /* Never dereference latest_task, since it's not refcounted */
3170 struct task_struct *latest_task = NULL;
3171 struct ptr_heap tmp_heap;
3172 struct ptr_heap *heap;
3173 struct timespec latest_time = { 0, 0 };
3174
3175 if (scan->heap) {
3176 /* The caller supplied our heap and pre-allocated its memory */
3177 heap = scan->heap;
3178 heap->gt = &started_after;
3179 } else {
3180 /* We need to allocate our own heap memory */
3181 heap = &tmp_heap;
3182 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3183 if (retval)
3184 /* cannot allocate the heap */
3185 return retval;
3186 }
3187
3188 again:
3189 /*
3190 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3191 * to determine which are of interest, and using the scanner's
3192 * "process_task" callback to process any of them that need an update.
3193 * Since we don't want to hold any locks during the task updates,
3194 * gather tasks to be processed in a heap structure.
3195 * The heap is sorted by descending task start time.
3196 * If the statically-sized heap fills up, we overflow tasks that
3197 * started later, and in future iterations only consider tasks that
3198 * started after the latest task in the previous pass. This
3199 * guarantees forward progress and that we don't miss any tasks.
3200 */
3201 heap->size = 0;
3202 cgroup_iter_start(scan->cg, &it);
3203 while ((p = cgroup_iter_next(scan->cg, &it))) {
3204 /*
3205 * Only affect tasks that qualify per the caller's callback,
3206 * if he provided one
3207 */
3208 if (scan->test_task && !scan->test_task(p, scan))
3209 continue;
3210 /*
3211 * Only process tasks that started after the last task
3212 * we processed
3213 */
3214 if (!started_after_time(p, &latest_time, latest_task))
3215 continue;
3216 dropped = heap_insert(heap, p);
3217 if (dropped == NULL) {
3218 /*
3219 * The new task was inserted; the heap wasn't
3220 * previously full
3221 */
3222 get_task_struct(p);
3223 } else if (dropped != p) {
3224 /*
3225 * The new task was inserted, and pushed out a
3226 * different task
3227 */
3228 get_task_struct(p);
3229 put_task_struct(dropped);
3230 }
3231 /*
3232 * Else the new task was newer than anything already in
3233 * the heap and wasn't inserted
3234 */
3235 }
3236 cgroup_iter_end(scan->cg, &it);
3237
3238 if (heap->size) {
3239 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003240 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003241 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003242 latest_time = q->start_time;
3243 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003244 }
3245 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003246 scan->process_task(q, scan);
3247 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003248 }
3249 /*
3250 * If we had to process any tasks at all, scan again
3251 * in case some of them were in the middle of forking
3252 * children that didn't get processed.
3253 * Not the most efficient way to do it, but it avoids
3254 * having to take callback_mutex in the fork path
3255 */
3256 goto again;
3257 }
3258 if (heap == &tmp_heap)
3259 heap_free(&tmp_heap);
3260 return 0;
3261}
3262
Paul Menage817929e2007-10-18 23:39:36 -07003263/*
Ben Blum102a7752009-09-23 15:56:26 -07003264 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003265 *
3266 * Reading this file can return large amounts of data if a cgroup has
3267 * *lots* of attached tasks. So it may need several calls to read(),
3268 * but we cannot guarantee that the information we produce is correct
3269 * unless we produce it entirely atomically.
3270 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003271 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003272
Li Zefan24528252012-01-20 11:58:43 +08003273/* which pidlist file are we talking about? */
3274enum cgroup_filetype {
3275 CGROUP_FILE_PROCS,
3276 CGROUP_FILE_TASKS,
3277};
3278
3279/*
3280 * A pidlist is a list of pids that virtually represents the contents of one
3281 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3282 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3283 * to the cgroup.
3284 */
3285struct cgroup_pidlist {
3286 /*
3287 * used to find which pidlist is wanted. doesn't change as long as
3288 * this particular list stays in the list.
3289 */
3290 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3291 /* array of xids */
3292 pid_t *list;
3293 /* how many elements the above list has */
3294 int length;
3295 /* how many files are using the current array */
3296 int use_count;
3297 /* each of these stored in a list by its cgroup */
3298 struct list_head links;
3299 /* pointer to the cgroup we belong to, for list removal purposes */
3300 struct cgroup *owner;
3301 /* protects the other fields */
3302 struct rw_semaphore mutex;
3303};
3304
Paul Menagebbcb81d2007-10-18 23:39:32 -07003305/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003306 * The following two functions "fix" the issue where there are more pids
3307 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3308 * TODO: replace with a kernel-wide solution to this problem
3309 */
3310#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3311static void *pidlist_allocate(int count)
3312{
3313 if (PIDLIST_TOO_LARGE(count))
3314 return vmalloc(count * sizeof(pid_t));
3315 else
3316 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3317}
3318static void pidlist_free(void *p)
3319{
3320 if (is_vmalloc_addr(p))
3321 vfree(p);
3322 else
3323 kfree(p);
3324}
3325static void *pidlist_resize(void *p, int newcount)
3326{
3327 void *newlist;
3328 /* note: if new alloc fails, old p will still be valid either way */
3329 if (is_vmalloc_addr(p)) {
3330 newlist = vmalloc(newcount * sizeof(pid_t));
3331 if (!newlist)
3332 return NULL;
3333 memcpy(newlist, p, newcount * sizeof(pid_t));
3334 vfree(p);
3335 } else {
3336 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3337 }
3338 return newlist;
3339}
3340
3341/*
Ben Blum102a7752009-09-23 15:56:26 -07003342 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3343 * If the new stripped list is sufficiently smaller and there's enough memory
3344 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3345 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003346 */
Ben Blum102a7752009-09-23 15:56:26 -07003347/* is the size difference enough that we should re-allocate the array? */
3348#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3349static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003350{
Ben Blum102a7752009-09-23 15:56:26 -07003351 int src, dest = 1;
3352 pid_t *list = *p;
3353 pid_t *newlist;
3354
3355 /*
3356 * we presume the 0th element is unique, so i starts at 1. trivial
3357 * edge cases first; no work needs to be done for either
3358 */
3359 if (length == 0 || length == 1)
3360 return length;
3361 /* src and dest walk down the list; dest counts unique elements */
3362 for (src = 1; src < length; src++) {
3363 /* find next unique element */
3364 while (list[src] == list[src-1]) {
3365 src++;
3366 if (src == length)
3367 goto after;
3368 }
3369 /* dest always points to where the next unique element goes */
3370 list[dest] = list[src];
3371 dest++;
3372 }
3373after:
3374 /*
3375 * if the length difference is large enough, we want to allocate a
3376 * smaller buffer to save memory. if this fails due to out of memory,
3377 * we'll just stay with what we've got.
3378 */
3379 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003380 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003381 if (newlist)
3382 *p = newlist;
3383 }
3384 return dest;
3385}
3386
3387static int cmppid(const void *a, const void *b)
3388{
3389 return *(pid_t *)a - *(pid_t *)b;
3390}
3391
3392/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003393 * find the appropriate pidlist for our purpose (given procs vs tasks)
3394 * returns with the lock on that pidlist already held, and takes care
3395 * of the use count, or returns NULL with no locks held if we're out of
3396 * memory.
3397 */
3398static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3399 enum cgroup_filetype type)
3400{
3401 struct cgroup_pidlist *l;
3402 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003403 struct pid_namespace *ns = current->nsproxy->pid_ns;
3404
Ben Blum72a8cb32009-09-23 15:56:27 -07003405 /*
3406 * We can't drop the pidlist_mutex before taking the l->mutex in case
3407 * the last ref-holder is trying to remove l from the list at the same
3408 * time. Holding the pidlist_mutex precludes somebody taking whichever
3409 * list we find out from under us - compare release_pid_array().
3410 */
3411 mutex_lock(&cgrp->pidlist_mutex);
3412 list_for_each_entry(l, &cgrp->pidlists, links) {
3413 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003414 /* make sure l doesn't vanish out from under us */
3415 down_write(&l->mutex);
3416 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003417 return l;
3418 }
3419 }
3420 /* entry not found; create a new one */
3421 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3422 if (!l) {
3423 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003424 return l;
3425 }
3426 init_rwsem(&l->mutex);
3427 down_write(&l->mutex);
3428 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003429 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003430 l->use_count = 0; /* don't increment here */
3431 l->list = NULL;
3432 l->owner = cgrp;
3433 list_add(&l->links, &cgrp->pidlists);
3434 mutex_unlock(&cgrp->pidlist_mutex);
3435 return l;
3436}
3437
3438/*
Ben Blum102a7752009-09-23 15:56:26 -07003439 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3440 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003441static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3442 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003443{
3444 pid_t *array;
3445 int length;
3446 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003447 struct cgroup_iter it;
3448 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003449 struct cgroup_pidlist *l;
3450
3451 /*
3452 * If cgroup gets more users after we read count, we won't have
3453 * enough space - tough. This race is indistinguishable to the
3454 * caller from the case that the additional cgroup users didn't
3455 * show up until sometime later on.
3456 */
3457 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003458 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003459 if (!array)
3460 return -ENOMEM;
3461 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003462 cgroup_iter_start(cgrp, &it);
3463 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003464 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003465 break;
Ben Blum102a7752009-09-23 15:56:26 -07003466 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003467 if (type == CGROUP_FILE_PROCS)
3468 pid = task_tgid_vnr(tsk);
3469 else
3470 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003471 if (pid > 0) /* make sure to only use valid results */
3472 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003473 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003474 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003475 length = n;
3476 /* now sort & (if procs) strip out duplicates */
3477 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003478 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003479 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003480 l = cgroup_pidlist_find(cgrp, type);
3481 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003482 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003483 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003484 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003485 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003486 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003487 l->list = array;
3488 l->length = length;
3489 l->use_count++;
3490 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003491 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003492 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003493}
3494
Balbir Singh846c7bb2007-10-18 23:39:44 -07003495/**
Li Zefana043e3b2008-02-23 15:24:09 -08003496 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003497 * @stats: cgroupstats to fill information into
3498 * @dentry: A dentry entry belonging to the cgroup for which stats have
3499 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003500 *
3501 * Build and fill cgroupstats so that taskstats can export it to user
3502 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003503 */
3504int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3505{
3506 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003507 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003508 struct cgroup_iter it;
3509 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003510
Balbir Singh846c7bb2007-10-18 23:39:44 -07003511 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003512 * Validate dentry by checking the superblock operations,
3513 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003514 */
Li Zefan33d283b2008-11-19 15:36:48 -08003515 if (dentry->d_sb->s_op != &cgroup_ops ||
3516 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003517 goto err;
3518
3519 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003520 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003521
Paul Menagebd89aab2007-10-18 23:40:44 -07003522 cgroup_iter_start(cgrp, &it);
3523 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003524 switch (tsk->state) {
3525 case TASK_RUNNING:
3526 stats->nr_running++;
3527 break;
3528 case TASK_INTERRUPTIBLE:
3529 stats->nr_sleeping++;
3530 break;
3531 case TASK_UNINTERRUPTIBLE:
3532 stats->nr_uninterruptible++;
3533 break;
3534 case TASK_STOPPED:
3535 stats->nr_stopped++;
3536 break;
3537 default:
3538 if (delayacct_is_task_waiting_on_io(tsk))
3539 stats->nr_io_wait++;
3540 break;
3541 }
3542 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003543 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003544
Balbir Singh846c7bb2007-10-18 23:39:44 -07003545err:
3546 return ret;
3547}
3548
Paul Menage8f3ff202009-09-23 15:56:25 -07003549
Paul Menagecc31edc2008-10-18 20:28:04 -07003550/*
Ben Blum102a7752009-09-23 15:56:26 -07003551 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003552 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003553 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003554 */
3555
Ben Blum102a7752009-09-23 15:56:26 -07003556static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003557{
3558 /*
3559 * Initially we receive a position value that corresponds to
3560 * one more than the last pid shown (or 0 on the first call or
3561 * after a seek to the start). Use a binary-search to find the
3562 * next pid to display, if any
3563 */
Ben Blum102a7752009-09-23 15:56:26 -07003564 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003565 int index = 0, pid = *pos;
3566 int *iter;
3567
Ben Blum102a7752009-09-23 15:56:26 -07003568 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003569 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003570 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003571
Paul Menagecc31edc2008-10-18 20:28:04 -07003572 while (index < end) {
3573 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003574 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003575 index = mid;
3576 break;
Ben Blum102a7752009-09-23 15:56:26 -07003577 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003578 index = mid + 1;
3579 else
3580 end = mid;
3581 }
3582 }
3583 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003584 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003585 return NULL;
3586 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003587 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003588 *pos = *iter;
3589 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003590}
3591
Ben Blum102a7752009-09-23 15:56:26 -07003592static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003593{
Ben Blum102a7752009-09-23 15:56:26 -07003594 struct cgroup_pidlist *l = s->private;
3595 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003596}
3597
Ben Blum102a7752009-09-23 15:56:26 -07003598static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003599{
Ben Blum102a7752009-09-23 15:56:26 -07003600 struct cgroup_pidlist *l = s->private;
3601 pid_t *p = v;
3602 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003603 /*
3604 * Advance to the next pid in the array. If this goes off the
3605 * end, we're done
3606 */
3607 p++;
3608 if (p >= end) {
3609 return NULL;
3610 } else {
3611 *pos = *p;
3612 return p;
3613 }
3614}
3615
Ben Blum102a7752009-09-23 15:56:26 -07003616static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003617{
3618 return seq_printf(s, "%d\n", *(int *)v);
3619}
3620
Ben Blum102a7752009-09-23 15:56:26 -07003621/*
3622 * seq_operations functions for iterating on pidlists through seq_file -
3623 * independent of whether it's tasks or procs
3624 */
3625static const struct seq_operations cgroup_pidlist_seq_operations = {
3626 .start = cgroup_pidlist_start,
3627 .stop = cgroup_pidlist_stop,
3628 .next = cgroup_pidlist_next,
3629 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003630};
3631
Ben Blum102a7752009-09-23 15:56:26 -07003632static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003633{
Ben Blum72a8cb32009-09-23 15:56:27 -07003634 /*
3635 * the case where we're the last user of this particular pidlist will
3636 * have us remove it from the cgroup's list, which entails taking the
3637 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3638 * pidlist_mutex, we have to take pidlist_mutex first.
3639 */
3640 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003641 down_write(&l->mutex);
3642 BUG_ON(!l->use_count);
3643 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003644 /* we're the last user if refcount is 0; remove and free */
3645 list_del(&l->links);
3646 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003647 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003648 put_pid_ns(l->key.ns);
3649 up_write(&l->mutex);
3650 kfree(l);
3651 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003652 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003653 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003654 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003655}
3656
Ben Blum102a7752009-09-23 15:56:26 -07003657static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003658{
Ben Blum102a7752009-09-23 15:56:26 -07003659 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003660 if (!(file->f_mode & FMODE_READ))
3661 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003662 /*
3663 * the seq_file will only be initialized if the file was opened for
3664 * reading; hence we check if it's not null only in that case.
3665 */
3666 l = ((struct seq_file *)file->private_data)->private;
3667 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003668 return seq_release(inode, file);
3669}
3670
Ben Blum102a7752009-09-23 15:56:26 -07003671static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003672 .read = seq_read,
3673 .llseek = seq_lseek,
3674 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003675 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003676};
3677
3678/*
Ben Blum102a7752009-09-23 15:56:26 -07003679 * The following functions handle opens on a file that displays a pidlist
3680 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3681 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003682 */
Ben Blum102a7752009-09-23 15:56:26 -07003683/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003684static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003685{
3686 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003687 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003688 int retval;
3689
3690 /* Nothing to do for write-only files */
3691 if (!(file->f_mode & FMODE_READ))
3692 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003693
Ben Blum102a7752009-09-23 15:56:26 -07003694 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003696 if (retval)
3697 return retval;
3698 /* configure file information */
3699 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003700
Ben Blum102a7752009-09-23 15:56:26 -07003701 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003702 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003703 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003704 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003705 }
Ben Blum102a7752009-09-23 15:56:26 -07003706 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003707 return 0;
3708}
Ben Blum102a7752009-09-23 15:56:26 -07003709static int cgroup_tasks_open(struct inode *unused, struct file *file)
3710{
Ben Blum72a8cb32009-09-23 15:56:27 -07003711 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003712}
3713static int cgroup_procs_open(struct inode *unused, struct file *file)
3714{
Ben Blum72a8cb32009-09-23 15:56:27 -07003715 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003716}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003717
Paul Menagebd89aab2007-10-18 23:40:44 -07003718static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003719 struct cftype *cft)
3720{
Paul Menagebd89aab2007-10-18 23:40:44 -07003721 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003722}
3723
Paul Menage6379c102008-07-25 01:47:01 -07003724static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3725 struct cftype *cft,
3726 u64 val)
3727{
3728 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3729 if (val)
3730 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3731 else
3732 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3733 return 0;
3734}
3735
Paul Menagebbcb81d2007-10-18 23:39:32 -07003736/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003737 * Unregister event and free resources.
3738 *
3739 * Gets called from workqueue.
3740 */
3741static void cgroup_event_remove(struct work_struct *work)
3742{
3743 struct cgroup_event *event = container_of(work, struct cgroup_event,
3744 remove);
3745 struct cgroup *cgrp = event->cgrp;
3746
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003747 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3748
3749 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003750 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003751 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003752}
3753
3754/*
3755 * Gets called on POLLHUP on eventfd when user closes it.
3756 *
3757 * Called with wqh->lock held and interrupts disabled.
3758 */
3759static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3760 int sync, void *key)
3761{
3762 struct cgroup_event *event = container_of(wait,
3763 struct cgroup_event, wait);
3764 struct cgroup *cgrp = event->cgrp;
3765 unsigned long flags = (unsigned long)key;
3766
3767 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003768 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003769 spin_lock(&cgrp->event_list_lock);
3770 list_del(&event->list);
3771 spin_unlock(&cgrp->event_list_lock);
3772 /*
3773 * We are in atomic context, but cgroup_event_remove() may
3774 * sleep, so we have to call it in workqueue.
3775 */
3776 schedule_work(&event->remove);
3777 }
3778
3779 return 0;
3780}
3781
3782static void cgroup_event_ptable_queue_proc(struct file *file,
3783 wait_queue_head_t *wqh, poll_table *pt)
3784{
3785 struct cgroup_event *event = container_of(pt,
3786 struct cgroup_event, pt);
3787
3788 event->wqh = wqh;
3789 add_wait_queue(wqh, &event->wait);
3790}
3791
3792/*
3793 * Parse input and register new cgroup event handler.
3794 *
3795 * Input must be in format '<event_fd> <control_fd> <args>'.
3796 * Interpretation of args is defined by control file implementation.
3797 */
3798static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3799 const char *buffer)
3800{
3801 struct cgroup_event *event = NULL;
3802 unsigned int efd, cfd;
3803 struct file *efile = NULL;
3804 struct file *cfile = NULL;
3805 char *endp;
3806 int ret;
3807
3808 efd = simple_strtoul(buffer, &endp, 10);
3809 if (*endp != ' ')
3810 return -EINVAL;
3811 buffer = endp + 1;
3812
3813 cfd = simple_strtoul(buffer, &endp, 10);
3814 if ((*endp != ' ') && (*endp != '\0'))
3815 return -EINVAL;
3816 buffer = endp + 1;
3817
3818 event = kzalloc(sizeof(*event), GFP_KERNEL);
3819 if (!event)
3820 return -ENOMEM;
3821 event->cgrp = cgrp;
3822 INIT_LIST_HEAD(&event->list);
3823 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3824 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3825 INIT_WORK(&event->remove, cgroup_event_remove);
3826
3827 efile = eventfd_fget(efd);
3828 if (IS_ERR(efile)) {
3829 ret = PTR_ERR(efile);
3830 goto fail;
3831 }
3832
3833 event->eventfd = eventfd_ctx_fileget(efile);
3834 if (IS_ERR(event->eventfd)) {
3835 ret = PTR_ERR(event->eventfd);
3836 goto fail;
3837 }
3838
3839 cfile = fget(cfd);
3840 if (!cfile) {
3841 ret = -EBADF;
3842 goto fail;
3843 }
3844
3845 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003846 /* AV: shouldn't we check that it's been opened for read instead? */
3847 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003848 if (ret < 0)
3849 goto fail;
3850
3851 event->cft = __file_cft(cfile);
3852 if (IS_ERR(event->cft)) {
3853 ret = PTR_ERR(event->cft);
3854 goto fail;
3855 }
3856
3857 if (!event->cft->register_event || !event->cft->unregister_event) {
3858 ret = -EINVAL;
3859 goto fail;
3860 }
3861
3862 ret = event->cft->register_event(cgrp, event->cft,
3863 event->eventfd, buffer);
3864 if (ret)
3865 goto fail;
3866
3867 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3868 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3869 ret = 0;
3870 goto fail;
3871 }
3872
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003873 /*
3874 * Events should be removed after rmdir of cgroup directory, but before
3875 * destroying subsystem state objects. Let's take reference to cgroup
3876 * directory dentry to do that.
3877 */
3878 dget(cgrp->dentry);
3879
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003880 spin_lock(&cgrp->event_list_lock);
3881 list_add(&event->list, &cgrp->event_list);
3882 spin_unlock(&cgrp->event_list_lock);
3883
3884 fput(cfile);
3885 fput(efile);
3886
3887 return 0;
3888
3889fail:
3890 if (cfile)
3891 fput(cfile);
3892
3893 if (event && event->eventfd && !IS_ERR(event->eventfd))
3894 eventfd_ctx_put(event->eventfd);
3895
3896 if (!IS_ERR_OR_NULL(efile))
3897 fput(efile);
3898
3899 kfree(event);
3900
3901 return ret;
3902}
3903
Daniel Lezcano97978e62010-10-27 15:33:35 -07003904static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3905 struct cftype *cft)
3906{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003907 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003908}
3909
3910static int cgroup_clone_children_write(struct cgroup *cgrp,
3911 struct cftype *cft,
3912 u64 val)
3913{
3914 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003915 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003916 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003917 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003918 return 0;
3919}
3920
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003921/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003922 * for the common functions, 'private' gives the type of file
3923 */
Ben Blum102a7752009-09-23 15:56:26 -07003924/* for hysterical raisins, we can't put this on the older files */
3925#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003926static struct cftype files[] = {
3927 {
3928 .name = "tasks",
3929 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003930 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003931 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003932 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003933 },
Ben Blum102a7752009-09-23 15:56:26 -07003934 {
3935 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3936 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003937 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003938 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003939 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003940 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003941 {
3942 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003943 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003944 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003945 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003946 {
3947 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3948 .write_string = cgroup_write_event_control,
3949 .mode = S_IWUGO,
3950 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003951 {
3952 .name = "cgroup.clone_children",
3953 .read_u64 = cgroup_clone_children_read,
3954 .write_u64 = cgroup_clone_children_write,
3955 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003956 {
3957 .name = "release_agent",
3958 .flags = CFTYPE_ONLY_ON_ROOT,
3959 .read_seq_string = cgroup_release_agent_show,
3960 .write_string = cgroup_release_agent_write,
3961 .max_write_len = PATH_MAX,
3962 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003963 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003964};
3965
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003966/**
3967 * cgroup_populate_dir - selectively creation of files in a directory
3968 * @cgrp: target cgroup
3969 * @base_files: true if the base files should be added
3970 * @subsys_mask: mask of the subsystem ids whose files should be added
3971 */
3972static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3973 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003974{
3975 int err;
3976 struct cgroup_subsys *ss;
3977
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003978 if (base_files) {
3979 err = cgroup_addrm_files(cgrp, NULL, files, true);
3980 if (err < 0)
3981 return err;
3982 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003983
Tejun Heo8e3f6542012-04-01 12:09:55 -07003984 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003985 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003986 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003987 if (!test_bit(ss->subsys_id, &subsys_mask))
3988 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003989
Tejun Heodb0416b2012-04-01 12:09:55 -07003990 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07003991 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003992 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07003993
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003994 /* This cgroup is ready now */
3995 for_each_subsys(cgrp->root, ss) {
3996 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3997 /*
3998 * Update id->css pointer and make this css visible from
3999 * CSS ID functions. This pointer will be dereferened
4000 * from RCU-read-side without locks.
4001 */
4002 if (css->id)
4003 rcu_assign_pointer(css->id->css, css);
4004 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004005
4006 return 0;
4007}
4008
Tejun Heo48ddbe12012-04-01 12:09:56 -07004009static void css_dput_fn(struct work_struct *work)
4010{
4011 struct cgroup_subsys_state *css =
4012 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004013 struct dentry *dentry = css->cgroup->dentry;
4014 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004015
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004016 atomic_inc(&sb->s_active);
4017 dput(dentry);
4018 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004019}
4020
Paul Menageddbcc7e2007-10-18 23:39:30 -07004021static void init_cgroup_css(struct cgroup_subsys_state *css,
4022 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004023 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004024{
Paul Menagebd89aab2007-10-18 23:40:44 -07004025 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004026 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004027 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004028 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004029 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004030 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004031 BUG_ON(cgrp->subsys[ss->subsys_id]);
4032 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004033
4034 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004035 * css holds an extra ref to @cgrp->dentry which is put on the last
4036 * css_put(). dput() requires process context, which css_put() may
4037 * be called without. @css->dput_work will be used to invoke
4038 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004039 */
4040 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041}
4042
Tejun Heob1929db2012-11-19 08:13:38 -08004043/* invoke ->post_create() on a new CSS and mark it online if successful */
4044static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004045{
Tejun Heob1929db2012-11-19 08:13:38 -08004046 int ret = 0;
4047
Tejun Heoa31f2d32012-11-19 08:13:37 -08004048 lockdep_assert_held(&cgroup_mutex);
4049
Tejun Heo92fb9742012-11-19 08:13:38 -08004050 if (ss->css_online)
4051 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004052 if (!ret)
4053 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4054 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004055}
4056
4057/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4058static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4059 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4060{
4061 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4062
4063 lockdep_assert_held(&cgroup_mutex);
4064
4065 if (!(css->flags & CSS_ONLINE))
4066 return;
4067
4068 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004069 * css_offline() should be called with cgroup_mutex unlocked. See
Tejun Heoa31f2d32012-11-19 08:13:37 -08004070 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4071 * details. This temporary unlocking should go away once
4072 * cgroup_mutex is unexported from controllers.
4073 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004074 if (ss->css_offline) {
Tejun Heoa31f2d32012-11-19 08:13:37 -08004075 mutex_unlock(&cgroup_mutex);
Tejun Heo92fb9742012-11-19 08:13:38 -08004076 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004077 mutex_lock(&cgroup_mutex);
4078 }
4079
4080 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4081}
4082
Paul Menageddbcc7e2007-10-18 23:39:30 -07004083/*
Li Zefana043e3b2008-02-23 15:24:09 -08004084 * cgroup_create - create a cgroup
4085 * @parent: cgroup that will be parent of the new cgroup
4086 * @dentry: dentry of the new cgroup
4087 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004088 *
Li Zefana043e3b2008-02-23 15:24:09 -08004089 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004090 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004091static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004092 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004093{
Paul Menagebd89aab2007-10-18 23:40:44 -07004094 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004095 struct cgroupfs_root *root = parent->root;
4096 int err = 0;
4097 struct cgroup_subsys *ss;
4098 struct super_block *sb = root->sb;
4099
Tejun Heo0a950f62012-11-19 09:02:12 -08004100 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004101 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4102 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004103 return -ENOMEM;
4104
Tejun Heo0a950f62012-11-19 09:02:12 -08004105 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4106 if (cgrp->id < 0)
4107 goto err_free_cgrp;
4108
Tejun Heo976c06b2012-11-05 09:16:59 -08004109 /*
4110 * Only live parents can have children. Note that the liveliness
4111 * check isn't strictly necessary because cgroup_mkdir() and
4112 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4113 * anyway so that locking is contained inside cgroup proper and we
4114 * don't get nasty surprises if we ever grow another caller.
4115 */
4116 if (!cgroup_lock_live_group(parent)) {
4117 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004118 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004119 }
4120
Paul Menageddbcc7e2007-10-18 23:39:30 -07004121 /* Grab a reference on the superblock so the hierarchy doesn't
4122 * get deleted on unmount if there are child cgroups. This
4123 * can be done outside cgroup_mutex, since the sb can't
4124 * disappear while someone has an open control file on the
4125 * fs */
4126 atomic_inc(&sb->s_active);
4127
Paul Menagecc31edc2008-10-18 20:28:04 -07004128 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004129
Paul Menagebd89aab2007-10-18 23:40:44 -07004130 cgrp->parent = parent;
4131 cgrp->root = parent->root;
4132 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004133
Li Zefanb6abdb02008-03-04 14:28:19 -08004134 if (notify_on_release(parent))
4135 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4136
Tejun Heo2260e7f2012-11-19 08:13:38 -08004137 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4138 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004139
Paul Menageddbcc7e2007-10-18 23:39:30 -07004140 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004141 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004142
Tejun Heo92fb9742012-11-19 08:13:38 -08004143 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004144 if (IS_ERR(css)) {
4145 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004146 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004147 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004148 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004149 if (ss->use_id) {
4150 err = alloc_css_id(ss, parent, cgrp);
4151 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004152 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004153 }
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004154
4155 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4156 parent->parent) {
4157 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",
4158 current->comm, current->pid, ss->name);
4159 if (!strcmp(ss->name, "memory"))
4160 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4161 ss->warned_broken_hierarchy = true;
4162 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004163 }
4164
Tejun Heo4e139af2012-11-19 08:13:36 -08004165 /*
4166 * Create directory. cgroup_create_file() returns with the new
4167 * directory locked on success so that it can be populated without
4168 * dropping cgroup_mutex.
4169 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004170 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004171 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004172 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004173 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004174
Tejun Heo4e139af2012-11-19 08:13:36 -08004175 /* allocation complete, commit to creation */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004176 dentry->d_fsdata = cgrp;
Tejun Heofebfcef2012-11-19 08:13:36 -08004177 cgrp->dentry = dentry;
Tejun Heo4e139af2012-11-19 08:13:36 -08004178 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4179 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4180 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004181
Tejun Heob1929db2012-11-19 08:13:38 -08004182 /* each css holds a ref to the cgroup's dentry */
4183 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004184 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004185
Tejun Heob1929db2012-11-19 08:13:38 -08004186 /* creation succeeded, notify subsystems */
4187 for_each_subsys(root, ss) {
4188 err = online_css(ss, cgrp);
4189 if (err)
4190 goto err_destroy;
Tejun Heoa8638032012-11-09 09:12:29 -08004191 }
4192
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004193 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004194 if (err)
4195 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004196
4197 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004198 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199
4200 return 0;
4201
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004202err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004203 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004204 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004205 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004206 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004207 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208 /* Release the reference count that we took on the superblock */
4209 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004210err_free_id:
4211 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004212err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004213 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004214 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004215
4216err_destroy:
4217 cgroup_destroy_locked(cgrp);
4218 mutex_unlock(&cgroup_mutex);
4219 mutex_unlock(&dentry->d_inode->i_mutex);
4220 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004221}
4222
Al Viro18bb1db2011-07-26 01:41:39 -04004223static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004224{
4225 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4226
4227 /* the vfs holds inode->i_mutex already */
4228 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4229}
4230
Tejun Heo28b4c272012-04-01 12:09:56 -07004231/*
4232 * Check the reference count on each subsystem. Since we already
4233 * established that there are no tasks in the cgroup, if the css refcount
4234 * is also 1, then there should be no outstanding references, so the
4235 * subsystem is safe to destroy. We scan across all subsystems rather than
4236 * using the per-hierarchy linked list of mounted subsystems since we can
4237 * be called via check_for_release() with no synchronization other than
4238 * RCU, and the subsystem linked list isn't RCU-safe.
4239 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004240static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004241{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004242 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004243
Ben Blumaae8aab2010-03-10 15:22:07 -08004244 /*
4245 * We won't need to lock the subsys array, because the subsystems
4246 * we're concerned about aren't going anywhere since our cgroup root
4247 * has a reference on them.
4248 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004249 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4250 struct cgroup_subsys *ss = subsys[i];
4251 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004252
Ben Blumaae8aab2010-03-10 15:22:07 -08004253 /* Skip subsystems not present or not in this hierarchy */
4254 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004255 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004256
Paul Menagebd89aab2007-10-18 23:40:44 -07004257 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004258 /*
4259 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004260 * that by this point the cgroup has been removed
4261 * and the css deleted. But a false-positive doesn't
4262 * matter, since it can only happen if the cgroup
4263 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004264 * release agent to be called anyway.
4265 */
4266 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004267 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004268 }
4269 return 0;
4270}
4271
Tejun Heo42809dd2012-11-19 08:13:37 -08004272static int cgroup_destroy_locked(struct cgroup *cgrp)
4273 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004274{
Tejun Heo42809dd2012-11-19 08:13:37 -08004275 struct dentry *d = cgrp->dentry;
4276 struct cgroup *parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004277 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004278 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004279 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280
Tejun Heo42809dd2012-11-19 08:13:37 -08004281 lockdep_assert_held(&d->d_inode->i_mutex);
4282 lockdep_assert_held(&cgroup_mutex);
4283
4284 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004285 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004286
Tejun Heo1a90dd52012-11-05 09:16:59 -08004287 /*
4288 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4289 * removed. This makes future css_tryget() and child creation
4290 * attempts fail thus maintaining the removal conditions verified
4291 * above.
4292 */
Tejun Heoed9577932012-11-05 09:16:58 -08004293 for_each_subsys(cgrp->root, ss) {
4294 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4295
4296 WARN_ON(atomic_read(&css->refcnt) < 0);
4297 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004298 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004299 set_bit(CGRP_REMOVED, &cgrp->flags);
4300
Tejun Heoa31f2d32012-11-19 08:13:37 -08004301 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004302 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004303 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004304
4305 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004306 * Put all the base refs. Each css holds an extra reference to the
4307 * cgroup's dentry and cgroup removal proceeds regardless of css
4308 * refs. On the last put of each css, whenever that may be, the
4309 * extra dentry ref is put so that dentry destruction happens only
4310 * after all css's are released.
4311 */
Tejun Heoe9316082012-11-05 09:16:58 -08004312 for_each_subsys(cgrp->root, ss)
4313 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004314
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004315 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004316 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004317 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004318 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004319
Paul Menage999cd8a2009-01-07 18:08:36 -08004320 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004321 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004322 list_del_init(&cgrp->allcg_node);
4323
Tejun Heo42809dd2012-11-19 08:13:37 -08004324 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004325 cgroup_d_remove_dir(d);
4326 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004327
Paul Menagebd89aab2007-10-18 23:40:44 -07004328 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004329 check_for_release(parent);
4330
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004331 /*
4332 * Unregister events and notify userspace.
4333 * Notify userspace about cgroup removing only after rmdir of cgroup
4334 * directory to avoid race between userspace and kernelspace
4335 */
4336 spin_lock(&cgrp->event_list_lock);
4337 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4338 list_del(&event->list);
4339 remove_wait_queue(event->wqh, &event->wait);
4340 eventfd_signal(event->eventfd, 1);
4341 schedule_work(&event->remove);
4342 }
4343 spin_unlock(&cgrp->event_list_lock);
4344
Paul Menageddbcc7e2007-10-18 23:39:30 -07004345 return 0;
4346}
4347
Tejun Heo42809dd2012-11-19 08:13:37 -08004348static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4349{
4350 int ret;
4351
4352 mutex_lock(&cgroup_mutex);
4353 ret = cgroup_destroy_locked(dentry->d_fsdata);
4354 mutex_unlock(&cgroup_mutex);
4355
4356 return ret;
4357}
4358
Tejun Heo8e3f6542012-04-01 12:09:55 -07004359static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4360{
4361 INIT_LIST_HEAD(&ss->cftsets);
4362
4363 /*
4364 * base_cftset is embedded in subsys itself, no need to worry about
4365 * deregistration.
4366 */
4367 if (ss->base_cftypes) {
4368 ss->base_cftset.cfts = ss->base_cftypes;
4369 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4370 }
4371}
4372
Li Zefan06a11922008-04-29 01:00:07 -07004373static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004374{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004375 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004376
4377 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004378
Tejun Heo648bb562012-11-19 08:13:36 -08004379 mutex_lock(&cgroup_mutex);
4380
Tejun Heo8e3f6542012-04-01 12:09:55 -07004381 /* init base cftset */
4382 cgroup_init_cftsets(ss);
4383
Paul Menageddbcc7e2007-10-18 23:39:30 -07004384 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004385 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004387 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004388 /* We don't handle early failures gracefully */
4389 BUG_ON(IS_ERR(css));
4390 init_cgroup_css(css, ss, dummytop);
4391
Li Zefane8d55fd2008-04-29 01:00:13 -07004392 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004393 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004394 * newly registered, all tasks and hence the
4395 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004396 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004397
4398 need_forkexit_callback |= ss->fork || ss->exit;
4399
Li Zefane8d55fd2008-04-29 01:00:13 -07004400 /* At system boot, before all subsystems have been
4401 * registered, no tasks have been forked, so we don't
4402 * need to invoke fork callbacks here. */
4403 BUG_ON(!list_empty(&init_task.tasks));
4404
Paul Menageddbcc7e2007-10-18 23:39:30 -07004405 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004406 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004407
Tejun Heo648bb562012-11-19 08:13:36 -08004408 mutex_unlock(&cgroup_mutex);
4409
Ben Blume6a11052010-03-10 15:22:09 -08004410 /* this function shouldn't be used with modular subsystems, since they
4411 * need to register a subsys_id, among other things */
4412 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004413}
4414
4415/**
Ben Blume6a11052010-03-10 15:22:09 -08004416 * cgroup_load_subsys: load and register a modular subsystem at runtime
4417 * @ss: the subsystem to load
4418 *
4419 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004420 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004421 * up for use. If the subsystem is built-in anyway, work is delegated to the
4422 * simpler cgroup_init_subsys.
4423 */
4424int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4425{
Ben Blume6a11052010-03-10 15:22:09 -08004426 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004427 int i, ret;
Ben Blume6a11052010-03-10 15:22:09 -08004428
4429 /* check name and function validity */
4430 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004431 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004432 return -EINVAL;
4433
4434 /*
4435 * we don't support callbacks in modular subsystems. this check is
4436 * before the ss->module check for consistency; a subsystem that could
4437 * be a module should still have no callbacks even if the user isn't
4438 * compiling it as one.
4439 */
4440 if (ss->fork || ss->exit)
4441 return -EINVAL;
4442
4443 /*
4444 * an optionally modular subsystem is built-in: we want to do nothing,
4445 * since cgroup_init_subsys will have already taken care of it.
4446 */
4447 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004448 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004449 BUG_ON(subsys[ss->subsys_id] != ss);
4450 return 0;
4451 }
4452
Tejun Heo8e3f6542012-04-01 12:09:55 -07004453 /* init base cftset */
4454 cgroup_init_cftsets(ss);
4455
Ben Blume6a11052010-03-10 15:22:09 -08004456 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004457 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004458
4459 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004460 * no ss->css_alloc seems to need anything important in the ss
4461 * struct, so this can happen first (i.e. before the rootnode
4462 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004463 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004464 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004465 if (IS_ERR(css)) {
4466 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004467 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004468 mutex_unlock(&cgroup_mutex);
4469 return PTR_ERR(css);
4470 }
4471
4472 list_add(&ss->sibling, &rootnode.subsys_list);
4473 ss->root = &rootnode;
4474
4475 /* our new subsystem will be attached to the dummy hierarchy. */
4476 init_cgroup_css(css, ss, dummytop);
4477 /* init_idr must be after init_cgroup_css because it sets css->id. */
4478 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004479 ret = cgroup_init_idr(ss, css);
4480 if (ret)
4481 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004482 }
4483
4484 /*
4485 * Now we need to entangle the css into the existing css_sets. unlike
4486 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4487 * will need a new pointer to it; done by iterating the css_set_table.
4488 * furthermore, modifying the existing css_sets will corrupt the hash
4489 * table state, so each changed css_set will need its hash recomputed.
4490 * this is all done under the css_set_lock.
4491 */
4492 write_lock(&css_set_lock);
4493 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4494 struct css_set *cg;
4495 struct hlist_node *node, *tmp;
4496 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4497
4498 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4499 /* skip entries that we already rehashed */
4500 if (cg->subsys[ss->subsys_id])
4501 continue;
4502 /* remove existing entry */
4503 hlist_del(&cg->hlist);
4504 /* set new value */
4505 cg->subsys[ss->subsys_id] = css;
4506 /* recompute hash and restore entry */
4507 new_bucket = css_set_hash(cg->subsys);
4508 hlist_add_head(&cg->hlist, new_bucket);
4509 }
4510 }
4511 write_unlock(&css_set_lock);
4512
Ben Blume6a11052010-03-10 15:22:09 -08004513 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004514 ret = online_css(ss, dummytop);
4515 if (ret)
4516 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004517
Ben Blume6a11052010-03-10 15:22:09 -08004518 /* success! */
4519 mutex_unlock(&cgroup_mutex);
4520 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004521
4522err_unload:
4523 mutex_unlock(&cgroup_mutex);
4524 /* @ss can't be mounted here as try_module_get() would fail */
4525 cgroup_unload_subsys(ss);
4526 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004527}
4528EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4529
4530/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004531 * cgroup_unload_subsys: unload a modular subsystem
4532 * @ss: the subsystem to unload
4533 *
4534 * This function should be called in a modular subsystem's exitcall. When this
4535 * function is invoked, the refcount on the subsystem's module will be 0, so
4536 * the subsystem will not be attached to any hierarchy.
4537 */
4538void cgroup_unload_subsys(struct cgroup_subsys *ss)
4539{
4540 struct cg_cgroup_link *link;
4541 struct hlist_head *hhead;
4542
4543 BUG_ON(ss->module == NULL);
4544
4545 /*
4546 * we shouldn't be called if the subsystem is in use, and the use of
4547 * try_module_get in parse_cgroupfs_options should ensure that it
4548 * doesn't start being used while we're killing it off.
4549 */
4550 BUG_ON(ss->root != &rootnode);
4551
4552 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004553
Tejun Heoa31f2d32012-11-19 08:13:37 -08004554 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004555 ss->active = 0;
4556
4557 if (ss->use_id) {
4558 idr_remove_all(&ss->idr);
4559 idr_destroy(&ss->idr);
4560 }
4561
Ben Blumcf5d5942010-03-10 15:22:09 -08004562 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004563 subsys[ss->subsys_id] = NULL;
4564
4565 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004566 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004567
4568 /*
4569 * disentangle the css from all css_sets attached to the dummytop. as
4570 * in loading, we need to pay our respects to the hashtable gods.
4571 */
4572 write_lock(&css_set_lock);
4573 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4574 struct css_set *cg = link->cg;
4575
4576 hlist_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004577 cg->subsys[ss->subsys_id] = NULL;
4578 hhead = css_set_hash(cg->subsys);
4579 hlist_add_head(&cg->hlist, hhead);
4580 }
4581 write_unlock(&css_set_lock);
4582
4583 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004584 * remove subsystem's css from the dummytop and free it - need to
4585 * free before marking as null because ss->css_free needs the
4586 * cgrp->subsys pointer to find their state. note that this also
4587 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004588 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004589 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004590 dummytop->subsys[ss->subsys_id] = NULL;
4591
4592 mutex_unlock(&cgroup_mutex);
4593}
4594EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4595
4596/**
Li Zefana043e3b2008-02-23 15:24:09 -08004597 * cgroup_init_early - cgroup initialization at system boot
4598 *
4599 * Initialize cgroups at system boot, and initialize any
4600 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004601 */
4602int __init cgroup_init_early(void)
4603{
4604 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004605 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004606 INIT_LIST_HEAD(&init_css_set.cg_links);
4607 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004608 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004609 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004611 root_count = 1;
4612 init_task.cgroups = &init_css_set;
4613
4614 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004615 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004616 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004617 &rootnode.top_cgroup.css_sets);
4618 list_add(&init_css_set_link.cg_link_list,
4619 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004620
Li Zefan472b1052008-04-29 01:00:11 -07004621 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4622 INIT_HLIST_HEAD(&css_set_table[i]);
4623
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004624 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625 struct cgroup_subsys *ss = subsys[i];
4626
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004627 /* at bootup time, we don't worry about modular subsystems */
4628 if (!ss || ss->module)
4629 continue;
4630
Paul Menageddbcc7e2007-10-18 23:39:30 -07004631 BUG_ON(!ss->name);
4632 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004633 BUG_ON(!ss->css_alloc);
4634 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004636 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004637 ss->name, ss->subsys_id);
4638 BUG();
4639 }
4640
4641 if (ss->early_init)
4642 cgroup_init_subsys(ss);
4643 }
4644 return 0;
4645}
4646
4647/**
Li Zefana043e3b2008-02-23 15:24:09 -08004648 * cgroup_init - cgroup initialization
4649 *
4650 * Register cgroup filesystem and /proc file, and initialize
4651 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004652 */
4653int __init cgroup_init(void)
4654{
4655 int err;
4656 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004657 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004658
4659 err = bdi_init(&cgroup_backing_dev_info);
4660 if (err)
4661 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004662
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004663 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004664 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004665
4666 /* at bootup time, we don't worry about modular subsystems */
4667 if (!ss || ss->module)
4668 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004669 if (!ss->early_init)
4670 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004671 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004672 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004673 }
4674
Li Zefan472b1052008-04-29 01:00:11 -07004675 /* Add init_css_set to the hash table */
4676 hhead = css_set_hash(init_css_set.subsys);
4677 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004678 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004679
4680 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4681 if (!cgroup_kobj) {
4682 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004683 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004684 }
4685
4686 err = register_filesystem(&cgroup_fs_type);
4687 if (err < 0) {
4688 kobject_put(cgroup_kobj);
4689 goto out;
4690 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004691
Li Zefan46ae2202008-04-29 01:00:08 -07004692 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004693
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694out:
Paul Menagea4243162007-10-18 23:39:35 -07004695 if (err)
4696 bdi_destroy(&cgroup_backing_dev_info);
4697
Paul Menageddbcc7e2007-10-18 23:39:30 -07004698 return err;
4699}
Paul Menageb4f48b62007-10-18 23:39:33 -07004700
Paul Menagea4243162007-10-18 23:39:35 -07004701/*
4702 * proc_cgroup_show()
4703 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4704 * - Used for /proc/<pid>/cgroup.
4705 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4706 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004707 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004708 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4709 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4710 * cgroup to top_cgroup.
4711 */
4712
4713/* TODO: Use a proper seq_file iterator */
4714static int proc_cgroup_show(struct seq_file *m, void *v)
4715{
4716 struct pid *pid;
4717 struct task_struct *tsk;
4718 char *buf;
4719 int retval;
4720 struct cgroupfs_root *root;
4721
4722 retval = -ENOMEM;
4723 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4724 if (!buf)
4725 goto out;
4726
4727 retval = -ESRCH;
4728 pid = m->private;
4729 tsk = get_pid_task(pid, PIDTYPE_PID);
4730 if (!tsk)
4731 goto out_free;
4732
4733 retval = 0;
4734
4735 mutex_lock(&cgroup_mutex);
4736
Li Zefane5f6a862009-01-07 18:07:41 -08004737 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004738 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004739 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004740 int count = 0;
4741
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004742 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004743 for_each_subsys(root, ss)
4744 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004745 if (strlen(root->name))
4746 seq_printf(m, "%sname=%s", count ? "," : "",
4747 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004748 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004749 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004750 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004751 if (retval < 0)
4752 goto out_unlock;
4753 seq_puts(m, buf);
4754 seq_putc(m, '\n');
4755 }
4756
4757out_unlock:
4758 mutex_unlock(&cgroup_mutex);
4759 put_task_struct(tsk);
4760out_free:
4761 kfree(buf);
4762out:
4763 return retval;
4764}
4765
4766static int cgroup_open(struct inode *inode, struct file *file)
4767{
4768 struct pid *pid = PROC_I(inode)->pid;
4769 return single_open(file, proc_cgroup_show, pid);
4770}
4771
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004772const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004773 .open = cgroup_open,
4774 .read = seq_read,
4775 .llseek = seq_lseek,
4776 .release = single_release,
4777};
4778
4779/* Display information about each subsystem and each hierarchy */
4780static int proc_cgroupstats_show(struct seq_file *m, void *v)
4781{
4782 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004783
Paul Menage8bab8dd2008-04-04 14:29:57 -07004784 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004785 /*
4786 * ideally we don't want subsystems moving around while we do this.
4787 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4788 * subsys/hierarchy state.
4789 */
Paul Menagea4243162007-10-18 23:39:35 -07004790 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004791 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4792 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004793 if (ss == NULL)
4794 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004795 seq_printf(m, "%s\t%d\t%d\t%d\n",
4796 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004797 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004798 }
4799 mutex_unlock(&cgroup_mutex);
4800 return 0;
4801}
4802
4803static int cgroupstats_open(struct inode *inode, struct file *file)
4804{
Al Viro9dce07f2008-03-29 03:07:28 +00004805 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004806}
4807
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004808static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004809 .open = cgroupstats_open,
4810 .read = seq_read,
4811 .llseek = seq_lseek,
4812 .release = single_release,
4813};
4814
Paul Menageb4f48b62007-10-18 23:39:33 -07004815/**
4816 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004817 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004818 *
4819 * Description: A task inherits its parent's cgroup at fork().
4820 *
4821 * A pointer to the shared css_set was automatically copied in
4822 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004823 * it was not made under the protection of RCU or cgroup_mutex, so
4824 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4825 * have already changed current->cgroups, allowing the previously
4826 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004827 *
4828 * At the point that cgroup_fork() is called, 'current' is the parent
4829 * task, and the passed argument 'child' points to the child task.
4830 */
4831void cgroup_fork(struct task_struct *child)
4832{
Tejun Heo9bb71302012-10-18 17:52:07 -07004833 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004834 child->cgroups = current->cgroups;
4835 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004836 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004837 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004838}
4839
4840/**
Li Zefana043e3b2008-02-23 15:24:09 -08004841 * cgroup_post_fork - called on a new task after adding it to the task list
4842 * @child: the task in question
4843 *
Tejun Heo5edee612012-10-16 15:03:14 -07004844 * Adds the task to the list running through its css_set if necessary and
4845 * call the subsystem fork() callbacks. Has to be after the task is
4846 * visible on the task list in case we race with the first call to
4847 * cgroup_iter_start() - to guarantee that the new task ends up on its
4848 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004849 */
Paul Menage817929e2007-10-18 23:39:36 -07004850void cgroup_post_fork(struct task_struct *child)
4851{
Tejun Heo5edee612012-10-16 15:03:14 -07004852 int i;
4853
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004854 /*
4855 * use_task_css_set_links is set to 1 before we walk the tasklist
4856 * under the tasklist_lock and we read it here after we added the child
4857 * to the tasklist under the tasklist_lock as well. If the child wasn't
4858 * yet in the tasklist when we walked through it from
4859 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4860 * should be visible now due to the paired locking and barriers implied
4861 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4862 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4863 * lock on fork.
4864 */
Paul Menage817929e2007-10-18 23:39:36 -07004865 if (use_task_css_set_links) {
4866 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004867 task_lock(child);
4868 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004869 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004870 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004871 write_unlock(&css_set_lock);
4872 }
Tejun Heo5edee612012-10-16 15:03:14 -07004873
4874 /*
4875 * Call ss->fork(). This must happen after @child is linked on
4876 * css_set; otherwise, @child might change state between ->fork()
4877 * and addition to css_set.
4878 */
4879 if (need_forkexit_callback) {
4880 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4881 struct cgroup_subsys *ss = subsys[i];
4882
4883 /*
4884 * fork/exit callbacks are supported only for
4885 * builtin subsystems and we don't need further
4886 * synchronization as they never go away.
4887 */
4888 if (!ss || ss->module)
4889 continue;
4890
4891 if (ss->fork)
4892 ss->fork(child);
4893 }
4894 }
Paul Menage817929e2007-10-18 23:39:36 -07004895}
Tejun Heo5edee612012-10-16 15:03:14 -07004896
Paul Menage817929e2007-10-18 23:39:36 -07004897/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004898 * cgroup_exit - detach cgroup from exiting task
4899 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004900 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004901 *
4902 * Description: Detach cgroup from @tsk and release it.
4903 *
4904 * Note that cgroups marked notify_on_release force every task in
4905 * them to take the global cgroup_mutex mutex when exiting.
4906 * This could impact scaling on very large systems. Be reluctant to
4907 * use notify_on_release cgroups where very high task exit scaling
4908 * is required on large systems.
4909 *
4910 * the_top_cgroup_hack:
4911 *
4912 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4913 *
4914 * We call cgroup_exit() while the task is still competent to
4915 * handle notify_on_release(), then leave the task attached to the
4916 * root cgroup in each hierarchy for the remainder of its exit.
4917 *
4918 * To do this properly, we would increment the reference count on
4919 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4920 * code we would add a second cgroup function call, to drop that
4921 * reference. This would just create an unnecessary hot spot on
4922 * the top_cgroup reference count, to no avail.
4923 *
4924 * Normally, holding a reference to a cgroup without bumping its
4925 * count is unsafe. The cgroup could go away, or someone could
4926 * attach us to a different cgroup, decrementing the count on
4927 * the first cgroup that we never incremented. But in this case,
4928 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004929 * which wards off any cgroup_attach_task() attempts, or task is a failed
4930 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004931 */
4932void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4933{
Paul Menage817929e2007-10-18 23:39:36 -07004934 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004935 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004936
4937 /*
4938 * Unlink from the css_set task list if necessary.
4939 * Optimistically check cg_list before taking
4940 * css_set_lock
4941 */
4942 if (!list_empty(&tsk->cg_list)) {
4943 write_lock(&css_set_lock);
4944 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004945 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004946 write_unlock(&css_set_lock);
4947 }
4948
Paul Menageb4f48b62007-10-18 23:39:33 -07004949 /* Reassign the task to the init_css_set. */
4950 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004951 cg = tsk->cgroups;
4952 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004953
4954 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004955 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004956 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004957
4958 /* modular subsystems can't use callbacks */
4959 if (!ss || ss->module)
4960 continue;
4961
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004962 if (ss->exit) {
4963 struct cgroup *old_cgrp =
4964 rcu_dereference_raw(cg->subsys[i])->cgroup;
4965 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004966 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004967 }
4968 }
4969 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004970 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004971
Paul Menage817929e2007-10-18 23:39:36 -07004972 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004973 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004974}
Paul Menage697f4162007-10-18 23:39:34 -07004975
4976/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004977 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004978 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004979 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004980 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004981 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4982 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004983 *
4984 * If we are sending in dummytop, then presumably we are creating
4985 * the top cgroup in the subsystem.
4986 *
4987 * Called only by the ns (nsproxy) cgroup.
4988 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004989int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004990{
4991 int ret;
4992 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004993
Paul Menagebd89aab2007-10-18 23:40:44 -07004994 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004995 return 1;
4996
Paul Menage7717f7b2009-09-23 15:56:22 -07004997 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004998 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4999 cgrp = cgrp->parent;
5000 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07005001 return ret;
5002}
Paul Menage81a6a5c2007-10-18 23:39:38 -07005003
Paul Menagebd89aab2007-10-18 23:40:44 -07005004static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005005{
5006 /* All of these checks rely on RCU to keep the cgroup
5007 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07005008 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5009 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005010 /* Control Group is currently removeable. If it's not
5011 * already queued for a userspace notification, queue
5012 * it now */
5013 int need_schedule_work = 0;
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005014 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005015 if (!cgroup_is_removed(cgrp) &&
5016 list_empty(&cgrp->release_list)) {
5017 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005018 need_schedule_work = 1;
5019 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005020 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005021 if (need_schedule_work)
5022 schedule_work(&release_agent_work);
5023 }
5024}
5025
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005026/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005027bool __css_tryget(struct cgroup_subsys_state *css)
5028{
Tejun Heoe9316082012-11-05 09:16:58 -08005029 while (true) {
5030 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005031
Tejun Heoe9316082012-11-05 09:16:58 -08005032 v = css_refcnt(css);
5033 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5034 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005035 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005036 else if (t < 0)
5037 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005038 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005039 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005040}
5041EXPORT_SYMBOL_GPL(__css_tryget);
5042
5043/* Caller must verify that the css is not for root cgroup */
5044void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005045{
Paul Menagebd89aab2007-10-18 23:40:44 -07005046 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005047 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005048
Paul Menage81a6a5c2007-10-18 23:39:38 -07005049 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005050 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5051
5052 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005053 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005054 if (notify_on_release(cgrp)) {
5055 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5056 check_for_release(cgrp);
5057 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005058 break;
5059 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005060 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005061 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005062 }
5063 rcu_read_unlock();
5064}
Ben Blum67523c42010-03-10 15:22:11 -08005065EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005066
5067/*
5068 * Notify userspace when a cgroup is released, by running the
5069 * configured release agent with the name of the cgroup (path
5070 * relative to the root of cgroup file system) as the argument.
5071 *
5072 * Most likely, this user command will try to rmdir this cgroup.
5073 *
5074 * This races with the possibility that some other task will be
5075 * attached to this cgroup before it is removed, or that some other
5076 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5077 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5078 * unused, and this cgroup will be reprieved from its death sentence,
5079 * to continue to serve a useful existence. Next time it's released,
5080 * we will get notified again, if it still has 'notify_on_release' set.
5081 *
5082 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5083 * means only wait until the task is successfully execve()'d. The
5084 * separate release agent task is forked by call_usermodehelper(),
5085 * then control in this thread returns here, without waiting for the
5086 * release agent task. We don't bother to wait because the caller of
5087 * this routine has no use for the exit status of the release agent
5088 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005089 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005090static void cgroup_release_agent(struct work_struct *work)
5091{
5092 BUG_ON(work != &release_agent_work);
5093 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005094 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005095 while (!list_empty(&release_list)) {
5096 char *argv[3], *envp[3];
5097 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005098 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005099 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005100 struct cgroup,
5101 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005102 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005103 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005104 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005105 if (!pathbuf)
5106 goto continue_free;
5107 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5108 goto continue_free;
5109 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5110 if (!agentbuf)
5111 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005112
5113 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005114 argv[i++] = agentbuf;
5115 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005116 argv[i] = NULL;
5117
5118 i = 0;
5119 /* minimal command environment */
5120 envp[i++] = "HOME=/";
5121 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5122 envp[i] = NULL;
5123
5124 /* Drop the lock while we invoke the usermode helper,
5125 * since the exec could involve hitting disk and hence
5126 * be a slow process */
5127 mutex_unlock(&cgroup_mutex);
5128 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005129 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005130 continue_free:
5131 kfree(pathbuf);
5132 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005133 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005134 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005135 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005136 mutex_unlock(&cgroup_mutex);
5137}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005138
5139static int __init cgroup_disable(char *str)
5140{
5141 int i;
5142 char *token;
5143
5144 while ((token = strsep(&str, ",")) != NULL) {
5145 if (!*token)
5146 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005147 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005148 struct cgroup_subsys *ss = subsys[i];
5149
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005150 /*
5151 * cgroup_disable, being at boot time, can't
5152 * know about module subsystems, so we don't
5153 * worry about them.
5154 */
5155 if (!ss || ss->module)
5156 continue;
5157
Paul Menage8bab8dd2008-04-04 14:29:57 -07005158 if (!strcmp(token, ss->name)) {
5159 ss->disabled = 1;
5160 printk(KERN_INFO "Disabling %s control group"
5161 " subsystem\n", ss->name);
5162 break;
5163 }
5164 }
5165 }
5166 return 1;
5167}
5168__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005169
5170/*
5171 * Functons for CSS ID.
5172 */
5173
5174/*
5175 *To get ID other than 0, this should be called when !cgroup_is_removed().
5176 */
5177unsigned short css_id(struct cgroup_subsys_state *css)
5178{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005179 struct css_id *cssid;
5180
5181 /*
5182 * This css_id() can return correct value when somone has refcnt
5183 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5184 * it's unchanged until freed.
5185 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005186 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005187
5188 if (cssid)
5189 return cssid->id;
5190 return 0;
5191}
Ben Blum67523c42010-03-10 15:22:11 -08005192EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005193
5194unsigned short css_depth(struct cgroup_subsys_state *css)
5195{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005196 struct css_id *cssid;
5197
Tejun Heo28b4c272012-04-01 12:09:56 -07005198 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005199
5200 if (cssid)
5201 return cssid->depth;
5202 return 0;
5203}
Ben Blum67523c42010-03-10 15:22:11 -08005204EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005205
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005206/**
5207 * css_is_ancestor - test "root" css is an ancestor of "child"
5208 * @child: the css to be tested.
5209 * @root: the css supporsed to be an ancestor of the child.
5210 *
5211 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005212 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005213 * But, considering usual usage, the csses should be valid objects after test.
5214 * Assuming that the caller will do some action to the child if this returns
5215 * returns true, the caller must take "child";s reference count.
5216 * If "child" is valid object and this returns true, "root" is valid, too.
5217 */
5218
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005219bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005220 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005221{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005222 struct css_id *child_id;
5223 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005224
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005225 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005226 if (!child_id)
5227 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005228 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005229 if (!root_id)
5230 return false;
5231 if (child_id->depth < root_id->depth)
5232 return false;
5233 if (child_id->stack[root_id->depth] != root_id->id)
5234 return false;
5235 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005236}
5237
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005238void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5239{
5240 struct css_id *id = css->id;
5241 /* When this is called before css_id initialization, id can be NULL */
5242 if (!id)
5243 return;
5244
5245 BUG_ON(!ss->use_id);
5246
5247 rcu_assign_pointer(id->css, NULL);
5248 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005249 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005250 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005251 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005252 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005253}
Ben Blum67523c42010-03-10 15:22:11 -08005254EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005255
5256/*
5257 * This is called by init or create(). Then, calls to this function are
5258 * always serialized (By cgroup_mutex() at create()).
5259 */
5260
5261static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5262{
5263 struct css_id *newid;
5264 int myid, error, size;
5265
5266 BUG_ON(!ss->use_id);
5267
5268 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5269 newid = kzalloc(size, GFP_KERNEL);
5270 if (!newid)
5271 return ERR_PTR(-ENOMEM);
5272 /* get id */
5273 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5274 error = -ENOMEM;
5275 goto err_out;
5276 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005277 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005278 /* Don't use 0. allocates an ID of 1-65535 */
5279 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005280 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005281
5282 /* Returns error when there are no free spaces for new ID.*/
5283 if (error) {
5284 error = -ENOSPC;
5285 goto err_out;
5286 }
5287 if (myid > CSS_ID_MAX)
5288 goto remove_idr;
5289
5290 newid->id = myid;
5291 newid->depth = depth;
5292 return newid;
5293remove_idr:
5294 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005295 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005296 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005297 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005298err_out:
5299 kfree(newid);
5300 return ERR_PTR(error);
5301
5302}
5303
Ben Blume6a11052010-03-10 15:22:09 -08005304static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5305 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005306{
5307 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005308
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005309 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005310 idr_init(&ss->idr);
5311
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005312 newid = get_new_cssid(ss, 0);
5313 if (IS_ERR(newid))
5314 return PTR_ERR(newid);
5315
5316 newid->stack[0] = newid->id;
5317 newid->css = rootcss;
5318 rootcss->id = newid;
5319 return 0;
5320}
5321
5322static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5323 struct cgroup *child)
5324{
5325 int subsys_id, i, depth = 0;
5326 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005327 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005328
5329 subsys_id = ss->subsys_id;
5330 parent_css = parent->subsys[subsys_id];
5331 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005332 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005333 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005334
5335 child_id = get_new_cssid(ss, depth);
5336 if (IS_ERR(child_id))
5337 return PTR_ERR(child_id);
5338
5339 for (i = 0; i < depth; i++)
5340 child_id->stack[i] = parent_id->stack[i];
5341 child_id->stack[depth] = child_id->id;
5342 /*
5343 * child_id->css pointer will be set after this cgroup is available
5344 * see cgroup_populate_dir()
5345 */
5346 rcu_assign_pointer(child_css->id, child_id);
5347
5348 return 0;
5349}
5350
5351/**
5352 * css_lookup - lookup css by id
5353 * @ss: cgroup subsys to be looked into.
5354 * @id: the id
5355 *
5356 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5357 * NULL if not. Should be called under rcu_read_lock()
5358 */
5359struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5360{
5361 struct css_id *cssid = NULL;
5362
5363 BUG_ON(!ss->use_id);
5364 cssid = idr_find(&ss->idr, id);
5365
5366 if (unlikely(!cssid))
5367 return NULL;
5368
5369 return rcu_dereference(cssid->css);
5370}
Ben Blum67523c42010-03-10 15:22:11 -08005371EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005372
5373/**
5374 * css_get_next - lookup next cgroup under specified hierarchy.
5375 * @ss: pointer to subsystem
5376 * @id: current position of iteration.
5377 * @root: pointer to css. search tree under this.
5378 * @foundid: position of found object.
5379 *
5380 * Search next css under the specified hierarchy of rootid. Calling under
5381 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5382 */
5383struct cgroup_subsys_state *
5384css_get_next(struct cgroup_subsys *ss, int id,
5385 struct cgroup_subsys_state *root, int *foundid)
5386{
5387 struct cgroup_subsys_state *ret = NULL;
5388 struct css_id *tmp;
5389 int tmpid;
5390 int rootid = css_id(root);
5391 int depth = css_depth(root);
5392
5393 if (!rootid)
5394 return NULL;
5395
5396 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005397 WARN_ON_ONCE(!rcu_read_lock_held());
5398
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005399 /* fill start point for scan */
5400 tmpid = id;
5401 while (1) {
5402 /*
5403 * scan next entry from bitmap(tree), tmpid is updated after
5404 * idr_get_next().
5405 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005406 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005407 if (!tmp)
5408 break;
5409 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5410 ret = rcu_dereference(tmp->css);
5411 if (ret) {
5412 *foundid = tmpid;
5413 break;
5414 }
5415 }
5416 /* continue to scan from next id */
5417 tmpid = tmpid + 1;
5418 }
5419 return ret;
5420}
5421
Stephane Eraniane5d13672011-02-14 11:20:01 +02005422/*
5423 * get corresponding css from file open on cgroupfs directory
5424 */
5425struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5426{
5427 struct cgroup *cgrp;
5428 struct inode *inode;
5429 struct cgroup_subsys_state *css;
5430
5431 inode = f->f_dentry->d_inode;
5432 /* check in cgroup filesystem dir */
5433 if (inode->i_op != &cgroup_dir_inode_operations)
5434 return ERR_PTR(-EBADF);
5435
5436 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5437 return ERR_PTR(-EINVAL);
5438
5439 /* get cgroup */
5440 cgrp = __d_cgrp(f->f_dentry);
5441 css = cgrp->subsys[id];
5442 return css ? css : ERR_PTR(-ENOENT);
5443}
5444
Paul Menagefe693432009-09-23 15:56:20 -07005445#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005446static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005447{
5448 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5449
5450 if (!css)
5451 return ERR_PTR(-ENOMEM);
5452
5453 return css;
5454}
5455
Tejun Heo92fb9742012-11-19 08:13:38 -08005456static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005457{
5458 kfree(cont->subsys[debug_subsys_id]);
5459}
5460
5461static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5462{
5463 return atomic_read(&cont->count);
5464}
5465
5466static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5467{
5468 return cgroup_task_count(cont);
5469}
5470
5471static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5472{
5473 return (u64)(unsigned long)current->cgroups;
5474}
5475
5476static u64 current_css_set_refcount_read(struct cgroup *cont,
5477 struct cftype *cft)
5478{
5479 u64 count;
5480
5481 rcu_read_lock();
5482 count = atomic_read(&current->cgroups->refcount);
5483 rcu_read_unlock();
5484 return count;
5485}
5486
Paul Menage7717f7b2009-09-23 15:56:22 -07005487static int current_css_set_cg_links_read(struct cgroup *cont,
5488 struct cftype *cft,
5489 struct seq_file *seq)
5490{
5491 struct cg_cgroup_link *link;
5492 struct css_set *cg;
5493
5494 read_lock(&css_set_lock);
5495 rcu_read_lock();
5496 cg = rcu_dereference(current->cgroups);
5497 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5498 struct cgroup *c = link->cgrp;
5499 const char *name;
5500
5501 if (c->dentry)
5502 name = c->dentry->d_name.name;
5503 else
5504 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005505 seq_printf(seq, "Root %d group %s\n",
5506 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005507 }
5508 rcu_read_unlock();
5509 read_unlock(&css_set_lock);
5510 return 0;
5511}
5512
5513#define MAX_TASKS_SHOWN_PER_CSS 25
5514static int cgroup_css_links_read(struct cgroup *cont,
5515 struct cftype *cft,
5516 struct seq_file *seq)
5517{
5518 struct cg_cgroup_link *link;
5519
5520 read_lock(&css_set_lock);
5521 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5522 struct css_set *cg = link->cg;
5523 struct task_struct *task;
5524 int count = 0;
5525 seq_printf(seq, "css_set %p\n", cg);
5526 list_for_each_entry(task, &cg->tasks, cg_list) {
5527 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5528 seq_puts(seq, " ...\n");
5529 break;
5530 } else {
5531 seq_printf(seq, " task %d\n",
5532 task_pid_vnr(task));
5533 }
5534 }
5535 }
5536 read_unlock(&css_set_lock);
5537 return 0;
5538}
5539
Paul Menagefe693432009-09-23 15:56:20 -07005540static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5541{
5542 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5543}
5544
5545static struct cftype debug_files[] = {
5546 {
5547 .name = "cgroup_refcount",
5548 .read_u64 = cgroup_refcount_read,
5549 },
5550 {
5551 .name = "taskcount",
5552 .read_u64 = debug_taskcount_read,
5553 },
5554
5555 {
5556 .name = "current_css_set",
5557 .read_u64 = current_css_set_read,
5558 },
5559
5560 {
5561 .name = "current_css_set_refcount",
5562 .read_u64 = current_css_set_refcount_read,
5563 },
5564
5565 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005566 .name = "current_css_set_cg_links",
5567 .read_seq_string = current_css_set_cg_links_read,
5568 },
5569
5570 {
5571 .name = "cgroup_css_links",
5572 .read_seq_string = cgroup_css_links_read,
5573 },
5574
5575 {
Paul Menagefe693432009-09-23 15:56:20 -07005576 .name = "releasable",
5577 .read_u64 = releasable_read,
5578 },
Paul Menagefe693432009-09-23 15:56:20 -07005579
Tejun Heo4baf6e32012-04-01 12:09:55 -07005580 { } /* terminate */
5581};
Paul Menagefe693432009-09-23 15:56:20 -07005582
5583struct cgroup_subsys debug_subsys = {
5584 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005585 .css_alloc = debug_css_alloc,
5586 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005587 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005588 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005589};
5590#endif /* CONFIG_CGROUP_DEBUG */