blob: 5cc37241a6fb78851c65f51e2c87bf9b93afd0b6 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
33#include <linux/fs.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100034#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/kernel.h>
36#include <linux/list.h>
37#include <linux/mm.h>
38#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070044#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/seq_file.h>
46#include <linux/slab.h>
47#include <linux/magic.h>
48#include <linux/spinlock.h>
49#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080052#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070055#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040056#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070057#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070058#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070059#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080060#include <linux/eventfd.h>
61#include <linux/poll.h>
Ben Blumd8466872011-05-26 16:25:21 -070062#include <linux/flex_array.h> /* used in cgroup_attach_proc */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020063#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heo28b4c272012-04-01 12:09:56 -070067/* css deactivation bias, makes css->refcnt negative to deny new trygets */
68#define CSS_DEACT_BIAS INT_MIN
69
Tejun Heoe25e2cb2011-12-12 18:12:21 -080070/*
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
73 *
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
79 *
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
82 *
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
84 * breaks it.
85 */
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -080087static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070088
Ben Blumaae8aab2010-03-10 15:22:07 -080089/*
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020091 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080092 * registered after that. The mutable section of this array is protected by
93 * cgroup_mutex.
94 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020095#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020096#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -080097static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070098#include <linux/cgroup_subsys.h>
99};
100
Paul Menagec6d57f32009-09-23 15:56:19 -0700101#define MAX_CGROUP_ROOT_NAMELEN 64
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
106 * hierarchy
107 */
108struct cgroupfs_root {
109 struct super_block *sb;
110
111 /*
112 * The bitmask of subsystems intended to be attached to this
113 * hierarchy
114 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400115 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700117 /* Unique id for this hierarchy. */
118 int hierarchy_id;
119
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400121 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122
123 /* A list running through the attached subsystems */
124 struct list_head subsys_list;
125
126 /* The root cgroup for this hierarchy */
127 struct cgroup top_cgroup;
128
129 /* Tracks how many cgroups are currently defined in hierarchy.*/
130 int number_of_cgroups;
131
Li Zefane5f6a862009-01-07 18:07:41 -0800132 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133 struct list_head root_list;
134
Tejun Heob0ca5a82012-04-01 12:09:54 -0700135 /* All cgroups on this root, cgroup_mutex protected */
136 struct list_head allcg_list;
137
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138 /* Hierarchy-specific flags */
139 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700140
Tejun Heo0a950f62012-11-19 09:02:12 -0800141 /* IDs for cgroups in this hierarchy */
142 struct ida cgroup_ida;
143
Paul Menagee788e0662008-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);
Gao feng879a3d92012-12-01 00:21:28 +0800249static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
250 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800251
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800252#ifdef CONFIG_PROVE_LOCKING
253int cgroup_lock_is_held(void)
254{
255 return lockdep_is_held(&cgroup_mutex);
256}
257#else /* #ifdef CONFIG_PROVE_LOCKING */
258int cgroup_lock_is_held(void)
259{
260 return mutex_is_locked(&cgroup_mutex);
261}
262#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
263
264EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
265
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700266static int css_unbias_refcnt(int refcnt)
267{
268 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
269}
270
Tejun Heo28b4c272012-04-01 12:09:56 -0700271/* the current nr of refs, always >= 0 whether @css is deactivated or not */
272static int css_refcnt(struct cgroup_subsys_state *css)
273{
274 int v = atomic_read(&css->refcnt);
275
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700276 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700277}
278
Paul Menageddbcc7e2007-10-18 23:39:30 -0700279/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700280inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700281{
Paul Menagebd89aab2007-10-18 23:40:44 -0700282 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700283}
284
285/* bits in struct cgroupfs_root flags field */
286enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400287 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
288 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700289};
290
Adrian Bunke9685a02008-02-07 00:13:46 -0800291static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700292{
293 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700294 (1 << CGRP_RELEASABLE) |
295 (1 << CGRP_NOTIFY_ON_RELEASE);
296 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700297}
298
Adrian Bunke9685a02008-02-07 00:13:46 -0800299static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700300{
Paul Menagebd89aab2007-10-18 23:40:44 -0700301 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302}
303
Paul Menageddbcc7e2007-10-18 23:39:30 -0700304/*
305 * for_each_subsys() allows you to iterate on each subsystem attached to
306 * an active hierarchy
307 */
308#define for_each_subsys(_root, _ss) \
309list_for_each_entry(_ss, &_root->subsys_list, sibling)
310
Li Zefane5f6a862009-01-07 18:07:41 -0800311/* for_each_active_root() allows you to iterate across the active hierarchies */
312#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700313list_for_each_entry(_root, &roots, root_list)
314
Tejun Heof6ea9372012-04-01 12:09:55 -0700315static inline struct cgroup *__d_cgrp(struct dentry *dentry)
316{
317 return dentry->d_fsdata;
318}
319
Tejun Heo05ef1d72012-04-01 12:09:56 -0700320static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700321{
322 return dentry->d_fsdata;
323}
324
Tejun Heo05ef1d72012-04-01 12:09:56 -0700325static inline struct cftype *__d_cft(struct dentry *dentry)
326{
327 return __d_cfe(dentry)->type;
328}
329
Paul Menage81a6a5c2007-10-18 23:39:38 -0700330/* the list of cgroups eligible for automatic release. Protected by
331 * release_list_lock */
332static LIST_HEAD(release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +0200333static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334static void cgroup_release_agent(struct work_struct *work);
335static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700336static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700337
Paul Menage817929e2007-10-18 23:39:36 -0700338/* Link structure for associating css_set objects with cgroups */
339struct cg_cgroup_link {
340 /*
341 * List running through cg_cgroup_links associated with a
342 * cgroup, anchored on cgroup->css_sets
343 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700344 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700345 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700346 /*
347 * List running through cg_cgroup_links pointing at a
348 * single css_set object, anchored on css_set->cg_links
349 */
350 struct list_head cg_link_list;
351 struct css_set *cg;
352};
353
354/* The default css_set - used by init and its children prior to any
355 * hierarchies being mounted. It contains a pointer to the root state
356 * for each subsystem. Also used to anchor the list of css_sets. Not
357 * reference-counted, to improve performance when child cgroups
358 * haven't been created.
359 */
360
361static struct css_set init_css_set;
362static struct cg_cgroup_link init_css_set_link;
363
Ben Blume6a11052010-03-10 15:22:09 -0800364static int cgroup_init_idr(struct cgroup_subsys *ss,
365 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700366
Paul Menage817929e2007-10-18 23:39:36 -0700367/* css_set_lock protects the list of css_set objects, and the
368 * chain of tasks off each css_set. Nests outside task->alloc_lock
369 * due to cgroup_iter_start() */
370static DEFINE_RWLOCK(css_set_lock);
371static int css_set_count;
372
Paul Menage7717f7b2009-09-23 15:56:22 -0700373/*
374 * hash table for cgroup groups. This improves the performance to find
375 * an existing css_set. This hash doesn't (currently) take into
376 * account cgroups in empty hierarchies.
377 */
Li Zefan472b1052008-04-29 01:00:11 -0700378#define CSS_SET_HASH_BITS 7
379#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
380static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
381
382static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
383{
384 int i;
385 int index;
386 unsigned long tmp = 0UL;
387
388 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
389 tmp += (unsigned long)css[i];
390 tmp = (tmp >> 16) ^ tmp;
391
392 index = hash_long(tmp, CSS_SET_HASH_BITS);
393
394 return &css_set_table[index];
395}
396
Paul Menage817929e2007-10-18 23:39:36 -0700397/* We don't maintain the lists running through each css_set to its
398 * task until after the first call to cgroup_iter_start(). This
399 * reduces the fork()/exit() overhead for people who have cgroups
400 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700401static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700402
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700403static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700404{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700405 struct cg_cgroup_link *link;
406 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 /*
408 * Ensure that the refcount doesn't hit zero while any readers
409 * can see it. Similar to atomic_dec_and_lock(), but for an
410 * rwlock
411 */
412 if (atomic_add_unless(&cg->refcount, -1, 1))
413 return;
414 write_lock(&css_set_lock);
415 if (!atomic_dec_and_test(&cg->refcount)) {
416 write_unlock(&css_set_lock);
417 return;
418 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 /* This css_set is dead. unlink it and release cgroup refcounts */
421 hlist_del(&cg->hlist);
422 css_set_count--;
423
424 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
425 cg_link_list) {
426 struct cgroup *cgrp = link->cgrp;
427 list_del(&link->cg_link_list);
428 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700429 if (atomic_dec_and_test(&cgrp->count) &&
430 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700431 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700432 set_bit(CGRP_RELEASABLE, &cgrp->flags);
433 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700435
436 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700437 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700438
439 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800440 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700441}
442
443/*
444 * refcounted get/put for css_set objects
445 */
446static inline void get_css_set(struct css_set *cg)
447{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700448 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700449}
450
451static inline void put_css_set(struct css_set *cg)
452{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700453 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700454}
455
Paul Menage81a6a5c2007-10-18 23:39:38 -0700456static inline void put_css_set_taskexit(struct css_set *cg)
457{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700458 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700459}
460
Paul Menage817929e2007-10-18 23:39:36 -0700461/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700462 * compare_css_sets - helper function for find_existing_css_set().
463 * @cg: candidate css_set being tested
464 * @old_cg: existing css_set for a task
465 * @new_cgrp: cgroup that's being entered by the task
466 * @template: desired set of css pointers in css_set (pre-calculated)
467 *
468 * Returns true if "cg" matches "old_cg" except for the hierarchy
469 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
470 */
471static bool compare_css_sets(struct css_set *cg,
472 struct css_set *old_cg,
473 struct cgroup *new_cgrp,
474 struct cgroup_subsys_state *template[])
475{
476 struct list_head *l1, *l2;
477
478 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
479 /* Not all subsystems matched */
480 return false;
481 }
482
483 /*
484 * Compare cgroup pointers in order to distinguish between
485 * different cgroups in heirarchies with no subsystems. We
486 * could get by with just this check alone (and skip the
487 * memcmp above) but on most setups the memcmp check will
488 * avoid the need for this more expensive check on almost all
489 * candidates.
490 */
491
492 l1 = &cg->cg_links;
493 l2 = &old_cg->cg_links;
494 while (1) {
495 struct cg_cgroup_link *cgl1, *cgl2;
496 struct cgroup *cg1, *cg2;
497
498 l1 = l1->next;
499 l2 = l2->next;
500 /* See if we reached the end - both lists are equal length. */
501 if (l1 == &cg->cg_links) {
502 BUG_ON(l2 != &old_cg->cg_links);
503 break;
504 } else {
505 BUG_ON(l2 == &old_cg->cg_links);
506 }
507 /* Locate the cgroups associated with these links. */
508 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
509 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
510 cg1 = cgl1->cgrp;
511 cg2 = cgl2->cgrp;
512 /* Hierarchies should be linked in the same order. */
513 BUG_ON(cg1->root != cg2->root);
514
515 /*
516 * If this hierarchy is the hierarchy of the cgroup
517 * that's changing, then we need to check that this
518 * css_set points to the new cgroup; if it's any other
519 * hierarchy, then this css_set should point to the
520 * same cgroup as the old css_set.
521 */
522 if (cg1->root == new_cgrp->root) {
523 if (cg1 != new_cgrp)
524 return false;
525 } else {
526 if (cg1 != cg2)
527 return false;
528 }
529 }
530 return true;
531}
532
533/*
Paul Menage817929e2007-10-18 23:39:36 -0700534 * find_existing_css_set() is a helper for
535 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700536 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700537 *
538 * oldcg: the cgroup group that we're using before the cgroup
539 * transition
540 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700541 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700542 *
543 * template: location in which to build the desired set of subsystem
544 * state objects for the new cgroup group
545 */
Paul Menage817929e2007-10-18 23:39:36 -0700546static struct css_set *find_existing_css_set(
547 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700548 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700549 struct cgroup_subsys_state *template[])
550{
551 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700552 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700553 struct hlist_head *hhead;
554 struct hlist_node *node;
555 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700556
Ben Blumaae8aab2010-03-10 15:22:07 -0800557 /*
558 * Build the set of subsystem state objects that we want to see in the
559 * new css_set. while subsystems can change globally, the entries here
560 * won't change, so no need for locking.
561 */
Paul Menage817929e2007-10-18 23:39:36 -0700562 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400563 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700564 /* Subsystem is in this hierarchy. So we want
565 * the subsystem state from the new
566 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700567 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700568 } else {
569 /* Subsystem is not in this hierarchy, so we
570 * don't want to change the subsystem state */
571 template[i] = oldcg->subsys[i];
572 }
573 }
574
Li Zefan472b1052008-04-29 01:00:11 -0700575 hhead = css_set_hash(template);
576 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700577 if (!compare_css_sets(cg, oldcg, cgrp, template))
578 continue;
579
580 /* This css_set matches what we need */
581 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700582 }
Paul Menage817929e2007-10-18 23:39:36 -0700583
584 /* No existing cgroup group matched */
585 return NULL;
586}
587
Paul Menage817929e2007-10-18 23:39:36 -0700588static void free_cg_links(struct list_head *tmp)
589{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700590 struct cg_cgroup_link *link;
591 struct cg_cgroup_link *saved_link;
592
593 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700594 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700595 kfree(link);
596 }
597}
598
599/*
Li Zefan36553432008-07-29 22:33:19 -0700600 * allocate_cg_links() allocates "count" cg_cgroup_link structures
601 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
602 * success or a negative error
603 */
604static int allocate_cg_links(int count, struct list_head *tmp)
605{
606 struct cg_cgroup_link *link;
607 int i;
608 INIT_LIST_HEAD(tmp);
609 for (i = 0; i < count; i++) {
610 link = kmalloc(sizeof(*link), GFP_KERNEL);
611 if (!link) {
612 free_cg_links(tmp);
613 return -ENOMEM;
614 }
615 list_add(&link->cgrp_link_list, tmp);
616 }
617 return 0;
618}
619
Li Zefanc12f65d2009-01-07 18:07:42 -0800620/**
621 * link_css_set - a helper function to link a css_set to a cgroup
622 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
623 * @cg: the css_set to be linked
624 * @cgrp: the destination cgroup
625 */
626static void link_css_set(struct list_head *tmp_cg_links,
627 struct css_set *cg, struct cgroup *cgrp)
628{
629 struct cg_cgroup_link *link;
630
631 BUG_ON(list_empty(tmp_cg_links));
632 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
633 cgrp_link_list);
634 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700635 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700636 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800637 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700638 /*
639 * Always add links to the tail of the list so that the list
640 * is sorted by order of hierarchy creation
641 */
642 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800643}
644
Li Zefan36553432008-07-29 22:33:19 -0700645/*
Paul Menage817929e2007-10-18 23:39:36 -0700646 * find_css_set() takes an existing cgroup group and a
647 * cgroup object, and returns a css_set object that's
648 * equivalent to the old group, but with the given cgroup
649 * substituted into the appropriate hierarchy. Must be called with
650 * cgroup_mutex held
651 */
Paul Menage817929e2007-10-18 23:39:36 -0700652static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700653 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700654{
655 struct css_set *res;
656 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700657
658 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700659
Li Zefan472b1052008-04-29 01:00:11 -0700660 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700661 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700662
Paul Menage817929e2007-10-18 23:39:36 -0700663 /* First see if we already have a cgroup group that matches
664 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700665 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700666 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700667 if (res)
668 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700669 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700670
671 if (res)
672 return res;
673
674 res = kmalloc(sizeof(*res), GFP_KERNEL);
675 if (!res)
676 return NULL;
677
678 /* Allocate all the cg_cgroup_link objects that we'll need */
679 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
680 kfree(res);
681 return NULL;
682 }
683
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700684 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700685 INIT_LIST_HEAD(&res->cg_links);
686 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700687 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700688
689 /* Copy the set of subsystem state objects generated in
690 * find_existing_css_set() */
691 memcpy(res->subsys, template, sizeof(res->subsys));
692
693 write_lock(&css_set_lock);
694 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700695 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
696 struct cgroup *c = link->cgrp;
697 if (c->root == cgrp->root)
698 c = cgrp;
699 link_css_set(&tmp_cg_links, res, c);
700 }
Paul Menage817929e2007-10-18 23:39:36 -0700701
702 BUG_ON(!list_empty(&tmp_cg_links));
703
Paul Menage817929e2007-10-18 23:39:36 -0700704 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700705
706 /* Add this cgroup group to the hash table */
707 hhead = css_set_hash(res->subsys);
708 hlist_add_head(&res->hlist, hhead);
709
Paul Menage817929e2007-10-18 23:39:36 -0700710 write_unlock(&css_set_lock);
711
712 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700713}
714
Paul Menageddbcc7e2007-10-18 23:39:30 -0700715/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700716 * Return the cgroup for "task" from the given hierarchy. Must be
717 * called with cgroup_mutex held.
718 */
719static struct cgroup *task_cgroup_from_root(struct task_struct *task,
720 struct cgroupfs_root *root)
721{
722 struct css_set *css;
723 struct cgroup *res = NULL;
724
725 BUG_ON(!mutex_is_locked(&cgroup_mutex));
726 read_lock(&css_set_lock);
727 /*
728 * No need to lock the task - since we hold cgroup_mutex the
729 * task can't change groups, so the only thing that can happen
730 * is that it exits and its css is set back to init_css_set.
731 */
732 css = task->cgroups;
733 if (css == &init_css_set) {
734 res = &root->top_cgroup;
735 } else {
736 struct cg_cgroup_link *link;
737 list_for_each_entry(link, &css->cg_links, cg_link_list) {
738 struct cgroup *c = link->cgrp;
739 if (c->root == root) {
740 res = c;
741 break;
742 }
743 }
744 }
745 read_unlock(&css_set_lock);
746 BUG_ON(!res);
747 return res;
748}
749
750/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 * There is one global cgroup mutex. We also require taking
752 * task_lock() when dereferencing a task's cgroup subsys pointers.
753 * See "The task_lock() exception", at the end of this comment.
754 *
755 * A task must hold cgroup_mutex to modify cgroups.
756 *
757 * Any task can increment and decrement the count field without lock.
758 * So in general, code holding cgroup_mutex can't rely on the count
759 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800760 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700761 * means that no tasks are currently attached, therefore there is no
762 * way a task attached to that cgroup can fork (the other way to
763 * increment the count). So code holding cgroup_mutex can safely
764 * assume that if the count is zero, it will stay zero. Similarly, if
765 * a task holds cgroup_mutex on a cgroup with zero count, it
766 * knows that the cgroup won't be removed, as cgroup_rmdir()
767 * needs that mutex.
768 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700769 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
770 * (usually) take cgroup_mutex. These are the two most performance
771 * critical pieces of code here. The exception occurs on cgroup_exit(),
772 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
773 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800774 * to the release agent with the name of the cgroup (path relative to
775 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776 *
777 * A cgroup can only be deleted if both its 'count' of using tasks
778 * is zero, and its list of 'children' cgroups is empty. Since all
779 * tasks in the system use _some_ cgroup, and since there is always at
780 * least one task in the system (init, pid == 1), therefore, top_cgroup
781 * always has either children cgroups and/or using tasks. So we don't
782 * need a special hack to ensure that top_cgroup cannot be deleted.
783 *
784 * The task_lock() exception
785 *
786 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800787 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800788 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789 * several performance critical places that need to reference
790 * task->cgroup without the expense of grabbing a system global
791 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800792 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
794 * the task_struct routinely used for such matters.
795 *
796 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800797 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798 */
799
Paul Menageddbcc7e2007-10-18 23:39:30 -0700800/**
801 * cgroup_lock - lock out any changes to cgroup structures
802 *
803 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700804void cgroup_lock(void)
805{
806 mutex_lock(&cgroup_mutex);
807}
Ben Blum67523c42010-03-10 15:22:11 -0800808EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809
810/**
811 * cgroup_unlock - release lock on cgroup changes
812 *
813 * Undo the lock taken in a previous cgroup_lock() call.
814 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815void cgroup_unlock(void)
816{
817 mutex_unlock(&cgroup_mutex);
818}
Ben Blum67523c42010-03-10 15:22:11 -0800819EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820
821/*
822 * A couple of forward declarations required, due to cyclic reference loop:
823 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
824 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
825 * -> cgroup_mkdir.
826 */
827
Al Viro18bb1db2011-07-26 01:41:39 -0400828static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400829static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700830static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400831static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
832 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700833static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700834static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700835
836static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200837 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700838 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700839};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700840
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700841static int alloc_css_id(struct cgroup_subsys *ss,
842 struct cgroup *parent, struct cgroup *child);
843
Al Viroa5e7ed32011-07-26 01:55:55 -0400844static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845{
846 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700847
848 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400849 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700850 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100851 inode->i_uid = current_fsuid();
852 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
854 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
855 }
856 return inode;
857}
858
859static void cgroup_diput(struct dentry *dentry, struct inode *inode)
860{
861 /* is dentry a directory ? if so, kfree() associated cgroup */
862 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700863 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800864 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700865 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700866 /* It's possible for external users to be holding css
867 * reference counts on a cgroup; css_put() needs to
868 * be able to access the cgroup after decrementing
869 * the reference count in order to know if it needs to
870 * queue the cgroup to be handled by the release
871 * agent */
872 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800873
874 mutex_lock(&cgroup_mutex);
875 /*
876 * Release the subsystem state objects.
877 */
Li Zefan75139b82009-01-07 18:07:33 -0800878 for_each_subsys(cgrp->root, ss)
Tejun Heo92fb9742012-11-19 08:13:38 -0800879 ss->css_free(cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800880
881 cgrp->root->number_of_cgroups--;
882 mutex_unlock(&cgroup_mutex);
883
Paul Menagea47295e2009-01-07 18:07:44 -0800884 /*
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700885 * Drop the active superblock reference that we took when we
886 * created the cgroup
Paul Menagea47295e2009-01-07 18:07:44 -0800887 */
Tejun Heo7db5b3c2012-07-07 15:55:47 -0700888 deactivate_super(cgrp->root->sb);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800889
Ben Blum72a8cb32009-09-23 15:56:27 -0700890 /*
891 * if we're getting rid of the cgroup, refcount should ensure
892 * that there are no pidlists left.
893 */
894 BUG_ON(!list_empty(&cgrp->pidlists));
895
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400896 simple_xattrs_free(&cgrp->xattrs);
897
Tejun Heo0a950f62012-11-19 09:02:12 -0800898 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800899 kfree_rcu(cgrp, rcu_head);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700900 } else {
901 struct cfent *cfe = __d_cfe(dentry);
902 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400903 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700904
905 WARN_ONCE(!list_empty(&cfe->node) &&
906 cgrp != &cgrp->root->top_cgroup,
907 "cfe still linked for %s\n", cfe->type->name);
908 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400909 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700910 }
911 iput(inode);
912}
913
Al Viroc72a04e2011-01-14 05:31:45 +0000914static int cgroup_delete(const struct dentry *d)
915{
916 return 1;
917}
918
Paul Menageddbcc7e2007-10-18 23:39:30 -0700919static void remove_dir(struct dentry *d)
920{
921 struct dentry *parent = dget(d->d_parent);
922
923 d_delete(d);
924 simple_rmdir(parent->d_inode, d);
925 dput(parent);
926}
927
Tejun Heo05ef1d72012-04-01 12:09:56 -0700928static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700929{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700930 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931
Tejun Heo05ef1d72012-04-01 12:09:56 -0700932 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
933 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100934
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935 list_for_each_entry(cfe, &cgrp->files, node) {
936 struct dentry *d = cfe->dentry;
937
938 if (cft && cfe->type != cft)
939 continue;
940
941 dget(d);
942 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700943 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700944 list_del_init(&cfe->node);
945 dput(d);
946
947 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700948 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700949 return -ENOENT;
950}
951
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400952/**
953 * cgroup_clear_directory - selective removal of base and subsystem files
954 * @dir: directory containing the files
955 * @base_files: true if the base files should be removed
956 * @subsys_mask: mask of the subsystem ids whose files should be removed
957 */
958static void cgroup_clear_directory(struct dentry *dir, bool base_files,
959 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700960{
961 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400962 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700963
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400964 for_each_subsys(cgrp->root, ss) {
965 struct cftype_set *set;
966 if (!test_bit(ss->subsys_id, &subsys_mask))
967 continue;
968 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800969 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400970 }
971 if (base_files) {
972 while (!list_empty(&cgrp->files))
973 cgroup_rm_file(cgrp, NULL);
974 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975}
976
977/*
978 * NOTE : the dentry must have been dget()'ed
979 */
980static void cgroup_d_remove_dir(struct dentry *dentry)
981{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100982 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400983 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100984
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400985 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100987 parent = dentry->d_parent;
988 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800989 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100991 spin_unlock(&dentry->d_lock);
992 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993 remove_dir(dentry);
994}
995
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700996/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800997 * Call with cgroup_mutex held. Drops reference counts on modules, including
998 * any duplicate ones that parse_cgroupfs_options took. If this function
999 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001000 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001002 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001004 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001005 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 int i;
1007
Ben Blumaae8aab2010-03-10 15:22:07 -08001008 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001009 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001010
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001011 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1012 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 /* Check that any added subsystems are currently free */
1014 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001015 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001017 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001019 /*
1020 * Nobody should tell us to do a subsys that doesn't exist:
1021 * parse_cgroupfs_options should catch that case and refcounts
1022 * ensure that subsystems won't disappear once selected.
1023 */
1024 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025 if (ss->root != &rootnode) {
1026 /* Subsystem isn't free */
1027 return -EBUSY;
1028 }
1029 }
1030
1031 /* Currently we don't handle adding/removing subsystems when
1032 * any child cgroups exist. This is theoretically supportable
1033 * but involves complex error handling, so it's being left until
1034 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001035 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 return -EBUSY;
1037
1038 /* Process each subsystem */
1039 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1040 struct cgroup_subsys *ss = subsys[i];
1041 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001042 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001044 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001045 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046 BUG_ON(!dummytop->subsys[i]);
1047 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001048 cgrp->subsys[i] = dummytop->subsys[i];
1049 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001050 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001051 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 if (ss->bind)
Li Zefan761b3ef52012-01-31 13:47:36 +08001053 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001054 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001055 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001057 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001058 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1059 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 if (ss->bind)
Li Zefan761b3ef52012-01-31 13:47:36 +08001061 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001063 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001064 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001065 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001066 /* subsystem is now free - drop reference on module */
1067 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001068 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001070 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001071 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001072 /*
1073 * a refcount was taken, but we already had one, so
1074 * drop the extra reference.
1075 */
1076 module_put(ss->module);
1077#ifdef CONFIG_MODULE_UNLOAD
1078 BUG_ON(ss->module && !module_refcount(ss->module));
1079#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 } else {
1081 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001082 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 }
1084 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001085 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086 synchronize_rcu();
1087
1088 return 0;
1089}
1090
Al Viro34c80b12011-12-08 21:32:45 -05001091static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092{
Al Viro34c80b12011-12-08 21:32:45 -05001093 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 struct cgroup_subsys *ss;
1095
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001096 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 for_each_subsys(root, ss)
1098 seq_printf(seq, ",%s", ss->name);
1099 if (test_bit(ROOT_NOPREFIX, &root->flags))
1100 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001101 if (test_bit(ROOT_XATTR, &root->flags))
1102 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001103 if (strlen(root->release_agent_path))
1104 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001105 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001106 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001107 if (strlen(root->name))
1108 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001109 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 return 0;
1111}
1112
1113struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001114 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001115 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001116 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001117 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001118 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001119 /* User explicitly requested empty subsystem */
1120 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001121
1122 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001123
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124};
1125
Ben Blumaae8aab2010-03-10 15:22:07 -08001126/*
1127 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001128 * with cgroup_mutex held to protect the subsys[] array. This function takes
1129 * refcounts on subsystems to be used, unless it returns error, in which case
1130 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001131 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001132static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001134 char *token, *o = data;
1135 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001137 int i;
1138 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001139
Ben Blumaae8aab2010-03-10 15:22:07 -08001140 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1141
Li Zefanf9ab5b52009-06-17 16:26:33 -07001142#ifdef CONFIG_CPUSETS
1143 mask = ~(1UL << cpuset_subsys_id);
1144#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001145
Paul Menagec6d57f32009-09-23 15:56:19 -07001146 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147
1148 while ((token = strsep(&o, ",")) != NULL) {
1149 if (!*token)
1150 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001151 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001152 /* Explicitly have no subsystems */
1153 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001154 continue;
1155 }
1156 if (!strcmp(token, "all")) {
1157 /* Mutually exclusive option 'all' + subsystem name */
1158 if (one_ss)
1159 return -EINVAL;
1160 all_ss = true;
1161 continue;
1162 }
1163 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001164 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001165 continue;
1166 }
1167 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001168 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001169 continue;
1170 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001171 if (!strcmp(token, "xattr")) {
1172 set_bit(ROOT_XATTR, &opts->flags);
1173 continue;
1174 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001175 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001176 /* Specifying two release agents is forbidden */
1177 if (opts->release_agent)
1178 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001179 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001180 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001181 if (!opts->release_agent)
1182 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001183 continue;
1184 }
1185 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001186 const char *name = token + 5;
1187 /* Can't specify an empty name */
1188 if (!strlen(name))
1189 return -EINVAL;
1190 /* Must match [\w.-]+ */
1191 for (i = 0; i < strlen(name); i++) {
1192 char c = name[i];
1193 if (isalnum(c))
1194 continue;
1195 if ((c == '.') || (c == '-') || (c == '_'))
1196 continue;
1197 return -EINVAL;
1198 }
1199 /* Specifying two names is forbidden */
1200 if (opts->name)
1201 return -EINVAL;
1202 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001203 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001204 GFP_KERNEL);
1205 if (!opts->name)
1206 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001207
1208 continue;
1209 }
1210
1211 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1212 struct cgroup_subsys *ss = subsys[i];
1213 if (ss == NULL)
1214 continue;
1215 if (strcmp(token, ss->name))
1216 continue;
1217 if (ss->disabled)
1218 continue;
1219
1220 /* Mutually exclusive option 'all' + subsystem name */
1221 if (all_ss)
1222 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001223 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001224 one_ss = true;
1225
1226 break;
1227 }
1228 if (i == CGROUP_SUBSYS_COUNT)
1229 return -ENOENT;
1230 }
1231
1232 /*
1233 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001234 * otherwise if 'none', 'name=' and a subsystem name options
1235 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001236 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001237 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001238 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1239 struct cgroup_subsys *ss = subsys[i];
1240 if (ss == NULL)
1241 continue;
1242 if (ss->disabled)
1243 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001244 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001245 }
1246 }
1247
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001248 /* Consistency checks */
1249
Li Zefanf9ab5b52009-06-17 16:26:33 -07001250 /*
1251 * Option noprefix was introduced just for backward compatibility
1252 * with the old cpuset, so we allow noprefix only if mounting just
1253 * the cpuset subsystem.
1254 */
1255 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001256 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001257 return -EINVAL;
1258
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001259
1260 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001261 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001262 return -EINVAL;
1263
1264 /*
1265 * We either have to specify by name or by subsystems. (So all
1266 * empty hierarchies must have a name).
1267 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001268 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001269 return -EINVAL;
1270
Ben Blumcf5d5942010-03-10 15:22:09 -08001271 /*
1272 * Grab references on all the modules we'll need, so the subsystems
1273 * don't dance around before rebind_subsystems attaches them. This may
1274 * take duplicate reference counts on a subsystem that's already used,
1275 * but rebind_subsystems handles this case.
1276 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001277 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001278 unsigned long bit = 1UL << i;
1279
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001280 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001281 continue;
1282 if (!try_module_get(subsys[i]->module)) {
1283 module_pin_failed = true;
1284 break;
1285 }
1286 }
1287 if (module_pin_failed) {
1288 /*
1289 * oops, one of the modules was going away. this means that we
1290 * raced with a module_delete call, and to the user this is
1291 * essentially a "subsystem doesn't exist" case.
1292 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001293 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001294 /* drop refcounts only on the ones we took */
1295 unsigned long bit = 1UL << i;
1296
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001297 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001298 continue;
1299 module_put(subsys[i]->module);
1300 }
1301 return -ENOENT;
1302 }
1303
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 return 0;
1305}
1306
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001307static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001308{
1309 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001310 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001311 unsigned long bit = 1UL << i;
1312
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001313 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001314 continue;
1315 module_put(subsys[i]->module);
1316 }
1317}
1318
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1320{
1321 int ret = 0;
1322 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001323 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001325 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326
Paul Menagebd89aab2007-10-18 23:40:44 -07001327 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001329 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330
1331 /* See what subsystems are wanted */
1332 ret = parse_cgroupfs_options(data, &opts);
1333 if (ret)
1334 goto out_unlock;
1335
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001336 /* See feature-removal-schedule.txt */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001337 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001338 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1339 task_tgid_nr(current), current->comm);
1340
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001341 added_mask = opts.subsys_mask & ~root->subsys_mask;
1342 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001343
Ben Blumcf5d5942010-03-10 15:22:09 -08001344 /* Don't allow flags or name to change at remount */
1345 if (opts.flags != root->flags ||
1346 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001348 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001349 goto out_unlock;
1350 }
1351
Gao feng7083d032012-12-03 09:28:18 +08001352 /*
1353 * Clear out the files of subsystems that should be removed, do
1354 * this before rebind_subsystems, since rebind_subsystems may
1355 * change this hierarchy's subsys_list.
1356 */
1357 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1358
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001359 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001360 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001361 /* rebind_subsystems failed, re-populate the removed files */
1362 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001363 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001364 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001365 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001367 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001368 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369
Paul Menage81a6a5c2007-10-18 23:39:38 -07001370 if (opts.release_agent)
1371 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001373 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001374 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001375 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001376 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001377 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378 return ret;
1379}
1380
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001381static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382 .statfs = simple_statfs,
1383 .drop_inode = generic_delete_inode,
1384 .show_options = cgroup_show_options,
1385 .remount_fs = cgroup_remount,
1386};
1387
Paul Menagecc31edc2008-10-18 20:28:04 -07001388static void init_cgroup_housekeeping(struct cgroup *cgrp)
1389{
1390 INIT_LIST_HEAD(&cgrp->sibling);
1391 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001392 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001393 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001394 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001395 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001396 INIT_LIST_HEAD(&cgrp->pidlists);
1397 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001398 INIT_LIST_HEAD(&cgrp->event_list);
1399 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001400 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001401}
Paul Menagec6d57f32009-09-23 15:56:19 -07001402
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403static void init_cgroup_root(struct cgroupfs_root *root)
1404{
Paul Menagebd89aab2007-10-18 23:40:44 -07001405 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001406
Paul Menageddbcc7e2007-10-18 23:39:30 -07001407 INIT_LIST_HEAD(&root->subsys_list);
1408 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001409 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001410 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001411 cgrp->root = root;
1412 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001413 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001414 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415}
1416
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001417static bool init_root_id(struct cgroupfs_root *root)
1418{
1419 int ret = 0;
1420
1421 do {
1422 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1423 return false;
1424 spin_lock(&hierarchy_id_lock);
1425 /* Try to allocate the next unused ID */
1426 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1427 &root->hierarchy_id);
1428 if (ret == -ENOSPC)
1429 /* Try again starting from 0 */
1430 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1431 if (!ret) {
1432 next_hierarchy_id = root->hierarchy_id + 1;
1433 } else if (ret != -EAGAIN) {
1434 /* Can only get here if the 31-bit IDR is full ... */
1435 BUG_ON(ret);
1436 }
1437 spin_unlock(&hierarchy_id_lock);
1438 } while (ret);
1439 return true;
1440}
1441
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442static int cgroup_test_super(struct super_block *sb, void *data)
1443{
Paul Menagec6d57f32009-09-23 15:56:19 -07001444 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445 struct cgroupfs_root *root = sb->s_fs_info;
1446
Paul Menagec6d57f32009-09-23 15:56:19 -07001447 /* If we asked for a name then it must match */
1448 if (opts->name && strcmp(opts->name, root->name))
1449 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001451 /*
1452 * If we asked for subsystems (or explicitly for no
1453 * subsystems) then they must match
1454 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001455 if ((opts->subsys_mask || opts->none)
1456 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457 return 0;
1458
1459 return 1;
1460}
1461
Paul Menagec6d57f32009-09-23 15:56:19 -07001462static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1463{
1464 struct cgroupfs_root *root;
1465
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001466 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001467 return NULL;
1468
1469 root = kzalloc(sizeof(*root), GFP_KERNEL);
1470 if (!root)
1471 return ERR_PTR(-ENOMEM);
1472
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001473 if (!init_root_id(root)) {
1474 kfree(root);
1475 return ERR_PTR(-ENOMEM);
1476 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001477 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001478
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001479 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001480 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001481 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 if (opts->release_agent)
1483 strcpy(root->release_agent_path, opts->release_agent);
1484 if (opts->name)
1485 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001486 if (opts->cpuset_clone_children)
1487 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001488 return root;
1489}
1490
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001491static void cgroup_drop_root(struct cgroupfs_root *root)
1492{
1493 if (!root)
1494 return;
1495
1496 BUG_ON(!root->hierarchy_id);
1497 spin_lock(&hierarchy_id_lock);
1498 ida_remove(&hierarchy_ida, root->hierarchy_id);
1499 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001500 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001501 kfree(root);
1502}
1503
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504static int cgroup_set_super(struct super_block *sb, void *data)
1505{
1506 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 struct cgroup_sb_opts *opts = data;
1508
1509 /* If we don't have a new root, we can't set up a new sb */
1510 if (!opts->new_root)
1511 return -EINVAL;
1512
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001513 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514
1515 ret = set_anon_super(sb, NULL);
1516 if (ret)
1517 return ret;
1518
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 sb->s_fs_info = opts->new_root;
1520 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521
1522 sb->s_blocksize = PAGE_CACHE_SIZE;
1523 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1524 sb->s_magic = CGROUP_SUPER_MAGIC;
1525 sb->s_op = &cgroup_ops;
1526
1527 return 0;
1528}
1529
1530static int cgroup_get_rootdir(struct super_block *sb)
1531{
Al Viro0df6a632010-12-21 13:29:29 -05001532 static const struct dentry_operations cgroup_dops = {
1533 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001534 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001535 };
1536
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537 struct inode *inode =
1538 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539
1540 if (!inode)
1541 return -ENOMEM;
1542
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 inode->i_fop = &simple_dir_operations;
1544 inode->i_op = &cgroup_dir_inode_operations;
1545 /* directories start off with i_nlink == 2 (for "." entry) */
1546 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001547 sb->s_root = d_make_root(inode);
1548 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001550 /* for everything else we want ->d_op set */
1551 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 return 0;
1553}
1554
Al Virof7e83572010-07-26 13:23:11 +04001555static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001557 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558{
1559 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001560 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 int ret = 0;
1562 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001563 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001564 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565
1566 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001567 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001569 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001570 if (ret)
1571 goto out_err;
1572
1573 /*
1574 * Allocate a new cgroup root. We may not need it if we're
1575 * reusing an existing hierarchy.
1576 */
1577 new_root = cgroup_root_from_opts(&opts);
1578 if (IS_ERR(new_root)) {
1579 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001580 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001581 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001582 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583
Paul Menagec6d57f32009-09-23 15:56:19 -07001584 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001585 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001587 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001588 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001589 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590 }
1591
Paul Menagec6d57f32009-09-23 15:56:19 -07001592 root = sb->s_fs_info;
1593 BUG_ON(!root);
1594 if (root == opts.new_root) {
1595 /* We used the new root structure, so this is a new hierarchy */
1596 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001597 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001598 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001599 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001600 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001601
1602 BUG_ON(sb->s_root != NULL);
1603
1604 ret = cgroup_get_rootdir(sb);
1605 if (ret)
1606 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001607 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608
Paul Menage817929e2007-10-18 23:39:36 -07001609 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001611 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001613 /* Check for name clashes with existing mounts */
1614 ret = -EBUSY;
1615 if (strlen(root->name))
1616 for_each_active_root(existing_root)
1617 if (!strcmp(existing_root->name, root->name))
1618 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001619
Paul Menage817929e2007-10-18 23:39:36 -07001620 /*
1621 * We're accessing css_set_count without locking
1622 * css_set_lock here, but that's OK - it can only be
1623 * increased by someone holding cgroup_lock, and
1624 * that's us. The worst that can happen is that we
1625 * have some link structures left over
1626 */
1627 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001628 if (ret)
1629 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001630
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001631 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001633 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001634 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001635 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001636 /*
1637 * There must be no failure case after here, since rebinding
1638 * takes care of subsystems' refcounts, which are explicitly
1639 * dropped in the failure exit path.
1640 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641
1642 /* EBUSY should be the only error here */
1643 BUG_ON(ret);
1644
1645 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001646 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647
Li Zefanc12f65d2009-01-07 18:07:42 -08001648 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649 root->top_cgroup.dentry = sb->s_root;
1650
Paul Menage817929e2007-10-18 23:39:36 -07001651 /* Link the top cgroup in this hierarchy into all
1652 * the css_set objects */
1653 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001654 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1655 struct hlist_head *hhead = &css_set_table[i];
1656 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001657 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001658
Li Zefanc12f65d2009-01-07 18:07:42 -08001659 hlist_for_each_entry(cg, node, hhead, hlist)
1660 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001661 }
Paul Menage817929e2007-10-18 23:39:36 -07001662 write_unlock(&css_set_lock);
1663
1664 free_cg_links(&tmp_cg_links);
1665
Li Zefanc12f65d2009-01-07 18:07:42 -08001666 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001667 BUG_ON(root->number_of_cgroups != 1);
1668
eparis@redhat2ce97382011-06-02 21:20:51 +10001669 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001670 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001671 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001672 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001674 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001675 } else {
1676 /*
1677 * We re-used an existing hierarchy - the new root (if
1678 * any) is not needed
1679 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001680 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001681 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001682 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683 }
1684
Paul Menagec6d57f32009-09-23 15:56:19 -07001685 kfree(opts.release_agent);
1686 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001687 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001689 unlock_drop:
1690 mutex_unlock(&cgroup_root_mutex);
1691 mutex_unlock(&cgroup_mutex);
1692 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001694 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001695 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001696 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001697 out_err:
1698 kfree(opts.release_agent);
1699 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001700 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701}
1702
1703static void cgroup_kill_sb(struct super_block *sb) {
1704 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001705 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001707 struct cg_cgroup_link *link;
1708 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001709
1710 BUG_ON(!root);
1711
1712 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001713 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714
1715 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001716 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717
1718 /* Rebind all subsystems back to the default hierarchy */
1719 ret = rebind_subsystems(root, 0);
1720 /* Shouldn't be able to fail ... */
1721 BUG_ON(ret);
1722
Paul Menage817929e2007-10-18 23:39:36 -07001723 /*
1724 * Release all the links from css_sets to this hierarchy's
1725 * root cgroup
1726 */
1727 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001728
1729 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1730 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001731 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001732 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001733 kfree(link);
1734 }
1735 write_unlock(&css_set_lock);
1736
Paul Menage839ec542009-01-29 14:25:22 -08001737 if (!list_empty(&root->root_list)) {
1738 list_del(&root->root_list);
1739 root_count--;
1740 }
Li Zefane5f6a862009-01-07 18:07:41 -08001741
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001742 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743 mutex_unlock(&cgroup_mutex);
1744
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001745 simple_xattrs_free(&cgrp->xattrs);
1746
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001748 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749}
1750
1751static struct file_system_type cgroup_fs_type = {
1752 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001753 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 .kill_sb = cgroup_kill_sb,
1755};
1756
Greg KH676db4a2010-08-05 13:53:35 -07001757static struct kobject *cgroup_kobj;
1758
Li Zefana043e3b2008-02-23 15:24:09 -08001759/**
1760 * cgroup_path - generate the path of a cgroup
1761 * @cgrp: the cgroup in question
1762 * @buf: the buffer to write the path into
1763 * @buflen: the length of the buffer
1764 *
Paul Menagea47295e2009-01-07 18:07:44 -08001765 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1766 * reference. Writes path of cgroup into buf. Returns 0 on success,
1767 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001769int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770{
Tejun Heofebfcef2012-11-19 08:13:36 -08001771 struct dentry *dentry = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001773
1774 rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1775 "cgroup_path() called without proper locking");
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776
Paul Menagea47295e2009-01-07 18:07:44 -08001777 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778 /*
1779 * Inactive subsystems have no dentry for their root
1780 * cgroup
1781 */
1782 strcpy(buf, "/");
1783 return 0;
1784 }
1785
Tao Ma316eb662012-11-08 21:36:38 +08001786 start = buf + buflen - 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787
Tao Ma316eb662012-11-08 21:36:38 +08001788 *start = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001790 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001791
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792 if ((start -= len) < buf)
1793 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001794 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001795 cgrp = cgrp->parent;
1796 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001798
Tejun Heofebfcef2012-11-19 08:13:36 -08001799 dentry = cgrp->dentry;
Paul Menagebd89aab2007-10-18 23:40:44 -07001800 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801 continue;
1802 if (--start < buf)
1803 return -ENAMETOOLONG;
1804 *start = '/';
1805 }
1806 memmove(buf, start, buf + buflen - start);
1807 return 0;
1808}
Ben Blum67523c42010-03-10 15:22:11 -08001809EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810
Ben Blum74a11662011-05-26 16:25:20 -07001811/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001812 * Control Group taskset
1813 */
Tejun Heo134d3372011-12-12 18:12:21 -08001814struct task_and_cgroup {
1815 struct task_struct *task;
1816 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001817 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001818};
1819
Tejun Heo2f7ee562011-12-12 18:12:21 -08001820struct cgroup_taskset {
1821 struct task_and_cgroup single;
1822 struct flex_array *tc_array;
1823 int tc_array_len;
1824 int idx;
1825 struct cgroup *cur_cgrp;
1826};
1827
1828/**
1829 * cgroup_taskset_first - reset taskset and return the first task
1830 * @tset: taskset of interest
1831 *
1832 * @tset iteration is initialized and the first task is returned.
1833 */
1834struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1835{
1836 if (tset->tc_array) {
1837 tset->idx = 0;
1838 return cgroup_taskset_next(tset);
1839 } else {
1840 tset->cur_cgrp = tset->single.cgrp;
1841 return tset->single.task;
1842 }
1843}
1844EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1845
1846/**
1847 * cgroup_taskset_next - iterate to the next task in taskset
1848 * @tset: taskset of interest
1849 *
1850 * Return the next task in @tset. Iteration must have been initialized
1851 * with cgroup_taskset_first().
1852 */
1853struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1854{
1855 struct task_and_cgroup *tc;
1856
1857 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1858 return NULL;
1859
1860 tc = flex_array_get(tset->tc_array, tset->idx++);
1861 tset->cur_cgrp = tc->cgrp;
1862 return tc->task;
1863}
1864EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1865
1866/**
1867 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1868 * @tset: taskset of interest
1869 *
1870 * Return the cgroup for the current (last returned) task of @tset. This
1871 * function must be preceded by either cgroup_taskset_first() or
1872 * cgroup_taskset_next().
1873 */
1874struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1875{
1876 return tset->cur_cgrp;
1877}
1878EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1879
1880/**
1881 * cgroup_taskset_size - return the number of tasks in taskset
1882 * @tset: taskset of interest
1883 */
1884int cgroup_taskset_size(struct cgroup_taskset *tset)
1885{
1886 return tset->tc_array ? tset->tc_array_len : 1;
1887}
1888EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1889
1890
Ben Blum74a11662011-05-26 16:25:20 -07001891/*
1892 * cgroup_task_migrate - move a task from one cgroup to another.
1893 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001894 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001895 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001896static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1897 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001898{
1899 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001900
1901 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001902 * We are synchronized through threadgroup_lock() against PF_EXITING
1903 * setting such that we can't race against cgroup_exit() changing the
1904 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001905 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001906 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001907 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001908
Ben Blum74a11662011-05-26 16:25:20 -07001909 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001910 rcu_assign_pointer(tsk->cgroups, newcg);
1911 task_unlock(tsk);
1912
1913 /* Update the css_set linked lists if we're using them */
1914 write_lock(&css_set_lock);
1915 if (!list_empty(&tsk->cg_list))
1916 list_move(&tsk->cg_list, &newcg->tasks);
1917 write_unlock(&css_set_lock);
1918
1919 /*
1920 * We just gained a reference on oldcg by taking it from the task. As
1921 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1922 * it here; it will be freed under RCU.
1923 */
Ben Blum74a11662011-05-26 16:25:20 -07001924 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001925 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001926}
1927
Li Zefana043e3b2008-02-23 15:24:09 -08001928/**
1929 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1930 * @cgrp: the cgroup the task is attaching to
1931 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001932 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001933 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1934 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001935 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001936int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001937{
Tejun Heo8f121912012-03-29 22:03:33 -07001938 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001939 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001940 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001941 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001942 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001943 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001944
Tejun Heocd3d0952011-12-12 18:12:21 -08001945 /* @tsk either already exited or can't exit until the end */
1946 if (tsk->flags & PF_EXITING)
1947 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001948
1949 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001950 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001951 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001952 return 0;
1953
Tejun Heo2f7ee562011-12-12 18:12:21 -08001954 tset.single.task = tsk;
1955 tset.single.cgrp = oldcgrp;
1956
Paul Menagebbcb81d2007-10-18 23:39:32 -07001957 for_each_subsys(root, ss) {
1958 if (ss->can_attach) {
Li Zefan761b3ef52012-01-31 13:47:36 +08001959 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001960 if (retval) {
1961 /*
1962 * Remember on which subsystem the can_attach()
1963 * failed, so that we only call cancel_attach()
1964 * against the subsystems whose can_attach()
1965 * succeeded. (See below)
1966 */
1967 failed_ss = ss;
1968 goto out;
1969 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001970 }
1971 }
1972
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001973 newcg = find_css_set(tsk->cgroups, cgrp);
1974 if (!newcg) {
1975 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001976 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001977 }
1978
1979 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001980
Paul Menagebbcb81d2007-10-18 23:39:32 -07001981 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001982 if (ss->attach)
Li Zefan761b3ef52012-01-31 13:47:36 +08001983 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001984 }
Ben Blum74a11662011-05-26 16:25:20 -07001985
Paul Menagebbcb81d2007-10-18 23:39:32 -07001986 synchronize_rcu();
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001987out:
1988 if (retval) {
1989 for_each_subsys(root, ss) {
1990 if (ss == failed_ss)
1991 /*
1992 * This subsystem was the one that failed the
1993 * can_attach() check earlier, so we don't need
1994 * to call cancel_attach() against it or any
1995 * remaining subsystems.
1996 */
1997 break;
1998 if (ss->cancel_attach)
Li Zefan761b3ef52012-01-31 13:47:36 +08001999 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08002000 }
2001 }
2002 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002003}
2004
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002005/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002006 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2007 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002008 * @tsk: the task to be attached
2009 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002010int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002011{
2012 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002013 int retval = 0;
2014
2015 cgroup_lock();
2016 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002017 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2018
2019 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002020 if (retval)
2021 break;
2022 }
2023 cgroup_unlock();
2024
2025 return retval;
2026}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002027EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002028
Ben Blum74a11662011-05-26 16:25:20 -07002029/**
2030 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2031 * @cgrp: the cgroup to attach to
2032 * @leader: the threadgroup leader task_struct of the group to be attached
2033 *
Tejun Heo257058a2011-12-12 18:12:21 -08002034 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2035 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002036 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002037static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002038{
2039 int retval, i, group_size;
2040 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002041 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002042 struct cgroupfs_root *root = cgrp->root;
2043 /* threadgroup list cursor and array */
2044 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002045 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002046 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002047 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002048
2049 /*
2050 * step 0: in order to do expensive, possibly blocking operations for
2051 * every thread, we cannot iterate the thread group list, since it needs
2052 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002053 * group - group_rwsem prevents new threads from appearing, and if
2054 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002055 */
2056 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002057 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002058 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002059 if (!group)
2060 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002061 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2062 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2063 if (retval)
2064 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002065
Ben Blum74a11662011-05-26 16:25:20 -07002066 tsk = leader;
2067 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002068 /*
2069 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2070 * already PF_EXITING could be freed from underneath us unless we
2071 * take an rcu_read_lock.
2072 */
2073 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002074 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002075 struct task_and_cgroup ent;
2076
Tejun Heocd3d0952011-12-12 18:12:21 -08002077 /* @tsk either already exited or can't exit until the end */
2078 if (tsk->flags & PF_EXITING)
2079 continue;
2080
Ben Blum74a11662011-05-26 16:25:20 -07002081 /* as per above, nr_threads may decrease, but not increase. */
2082 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002083 ent.task = tsk;
2084 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002085 /* nothing to do if this task is already in the cgroup */
2086 if (ent.cgrp == cgrp)
2087 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002088 /*
2089 * saying GFP_ATOMIC has no effect here because we did prealloc
2090 * earlier, but it's good form to communicate our expectations.
2091 */
Tejun Heo134d3372011-12-12 18:12:21 -08002092 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002093 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002094 i++;
2095 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002096 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002097 /* remember the number of threads in the array for later. */
2098 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002099 tset.tc_array = group;
2100 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002101
Tejun Heo134d3372011-12-12 18:12:21 -08002102 /* methods shouldn't be called if no task is actually migrating */
2103 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002104 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002105 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002106
Ben Blum74a11662011-05-26 16:25:20 -07002107 /*
2108 * step 1: check that we can legitimately attach to the cgroup.
2109 */
2110 for_each_subsys(root, ss) {
2111 if (ss->can_attach) {
Li Zefan761b3ef52012-01-31 13:47:36 +08002112 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002113 if (retval) {
2114 failed_ss = ss;
2115 goto out_cancel_attach;
2116 }
2117 }
Ben Blum74a11662011-05-26 16:25:20 -07002118 }
2119
2120 /*
2121 * step 2: make sure css_sets exist for all threads to be migrated.
2122 * we use find_css_set, which allocates a new one if necessary.
2123 */
Ben Blum74a11662011-05-26 16:25:20 -07002124 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002125 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002126 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2127 if (!tc->cg) {
2128 retval = -ENOMEM;
2129 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002130 }
2131 }
2132
2133 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002134 * step 3: now that we're guaranteed success wrt the css_sets,
2135 * proceed to move all tasks to the new cgroup. There are no
2136 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002137 */
Ben Blum74a11662011-05-26 16:25:20 -07002138 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002139 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002140 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002141 }
2142 /* nothing is sensitive to fork() after this point. */
2143
2144 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002145 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002146 */
2147 for_each_subsys(root, ss) {
2148 if (ss->attach)
Li Zefan761b3ef52012-01-31 13:47:36 +08002149 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002150 }
2151
2152 /*
2153 * step 5: success! and cleanup
2154 */
2155 synchronize_rcu();
Ben Blum74a11662011-05-26 16:25:20 -07002156 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002157out_put_css_set_refs:
2158 if (retval) {
2159 for (i = 0; i < group_size; i++) {
2160 tc = flex_array_get(group, i);
2161 if (!tc->cg)
2162 break;
2163 put_css_set(tc->cg);
2164 }
Ben Blum74a11662011-05-26 16:25:20 -07002165 }
2166out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002167 if (retval) {
2168 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002169 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002170 break;
Ben Blum74a11662011-05-26 16:25:20 -07002171 if (ss->cancel_attach)
Li Zefan761b3ef52012-01-31 13:47:36 +08002172 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002173 }
2174 }
Ben Blum74a11662011-05-26 16:25:20 -07002175out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002176 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002177 return retval;
2178}
2179
2180/*
2181 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002182 * function to attach either it or all tasks in its threadgroup. Will lock
2183 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002184 */
2185static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002186{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002188 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002189 int ret;
2190
Ben Blum74a11662011-05-26 16:25:20 -07002191 if (!cgroup_lock_live_group(cgrp))
2192 return -ENODEV;
2193
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002194retry_find_task:
2195 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002196 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002197 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002198 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002199 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002200 ret= -ESRCH;
2201 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002202 }
Ben Blum74a11662011-05-26 16:25:20 -07002203 /*
2204 * even if we're attaching all tasks in the thread group, we
2205 * only need to check permissions on one of them.
2206 */
David Howellsc69e8d92008-11-14 10:39:19 +11002207 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002208 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2209 !uid_eq(cred->euid, tcred->uid) &&
2210 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002211 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002212 ret = -EACCES;
2213 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002214 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002215 } else
2216 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002217
2218 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002219 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002220
2221 /*
2222 * Workqueue threads may acquire PF_THREAD_BOUND and become
2223 * trapped in a cpuset, or RT worker may be born in a cgroup
2224 * with no rt_runtime allocated. Just say no.
2225 */
2226 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2227 ret = -EINVAL;
2228 rcu_read_unlock();
2229 goto out_unlock_cgroup;
2230 }
2231
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002232 get_task_struct(tsk);
2233 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002234
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002235 threadgroup_lock(tsk);
2236 if (threadgroup) {
2237 if (!thread_group_leader(tsk)) {
2238 /*
2239 * a race with de_thread from another thread's exec()
2240 * may strip us of our leadership, if this happens,
2241 * there is no choice but to throw this task away and
2242 * try again; this is
2243 * "double-double-toil-and-trouble-check locking".
2244 */
2245 threadgroup_unlock(tsk);
2246 put_task_struct(tsk);
2247 goto retry_find_task;
2248 }
2249 ret = cgroup_attach_proc(cgrp, tsk);
2250 } else
2251 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002252 threadgroup_unlock(tsk);
2253
Paul Menagebbcb81d2007-10-18 23:39:32 -07002254 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002255out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002256 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002257 return ret;
2258}
2259
Paul Menageaf351022008-07-25 01:47:01 -07002260static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2261{
Ben Blum74a11662011-05-26 16:25:20 -07002262 return attach_task_by_pid(cgrp, pid, false);
2263}
2264
2265static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2266{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002267 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002268}
2269
Paul Menagee788e0662008-07-25 01:46:59 -07002270/**
2271 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2272 * @cgrp: the cgroup to be checked for liveness
2273 *
Paul Menage84eea842008-07-25 01:47:00 -07002274 * On success, returns true; the lock should be later released with
2275 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e0662008-07-25 01:46:59 -07002276 */
Paul Menage84eea842008-07-25 01:47:00 -07002277bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07002278{
2279 mutex_lock(&cgroup_mutex);
2280 if (cgroup_is_removed(cgrp)) {
2281 mutex_unlock(&cgroup_mutex);
2282 return false;
2283 }
2284 return true;
2285}
Ben Blum67523c42010-03-10 15:22:11 -08002286EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e0662008-07-25 01:46:59 -07002287
2288static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2289 const char *buffer)
2290{
2291 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002292 if (strlen(buffer) >= PATH_MAX)
2293 return -EINVAL;
Paul Menagee788e0662008-07-25 01:46:59 -07002294 if (!cgroup_lock_live_group(cgrp))
2295 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002296 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002297 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002298 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002299 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07002300 return 0;
2301}
2302
2303static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2304 struct seq_file *seq)
2305{
2306 if (!cgroup_lock_live_group(cgrp))
2307 return -ENODEV;
2308 seq_puts(seq, cgrp->root->release_agent_path);
2309 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002310 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07002311 return 0;
2312}
2313
Paul Menage84eea842008-07-25 01:47:00 -07002314/* A buffer size big enough for numbers or short strings */
2315#define CGROUP_LOCAL_BUFFER_SIZE 64
2316
Paul Menagee73d2c62008-04-29 01:00:06 -07002317static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002318 struct file *file,
2319 const char __user *userbuf,
2320 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002321{
Paul Menage84eea842008-07-25 01:47:00 -07002322 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002323 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002324 char *end;
2325
2326 if (!nbytes)
2327 return -EINVAL;
2328 if (nbytes >= sizeof(buffer))
2329 return -E2BIG;
2330 if (copy_from_user(buffer, userbuf, nbytes))
2331 return -EFAULT;
2332
2333 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002334 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002335 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002336 if (*end)
2337 return -EINVAL;
2338 retval = cft->write_u64(cgrp, cft, val);
2339 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002340 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002341 if (*end)
2342 return -EINVAL;
2343 retval = cft->write_s64(cgrp, cft, val);
2344 }
Paul Menage355e0c42007-10-18 23:39:33 -07002345 if (!retval)
2346 retval = nbytes;
2347 return retval;
2348}
2349
Paul Menagedb3b1492008-07-25 01:46:58 -07002350static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2351 struct file *file,
2352 const char __user *userbuf,
2353 size_t nbytes, loff_t *unused_ppos)
2354{
Paul Menage84eea842008-07-25 01:47:00 -07002355 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002356 int retval = 0;
2357 size_t max_bytes = cft->max_write_len;
2358 char *buffer = local_buffer;
2359
2360 if (!max_bytes)
2361 max_bytes = sizeof(local_buffer) - 1;
2362 if (nbytes >= max_bytes)
2363 return -E2BIG;
2364 /* Allocate a dynamic buffer if we need one */
2365 if (nbytes >= sizeof(local_buffer)) {
2366 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2367 if (buffer == NULL)
2368 return -ENOMEM;
2369 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002370 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2371 retval = -EFAULT;
2372 goto out;
2373 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002374
2375 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002376 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002377 if (!retval)
2378 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002379out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002380 if (buffer != local_buffer)
2381 kfree(buffer);
2382 return retval;
2383}
2384
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2386 size_t nbytes, loff_t *ppos)
2387{
2388 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002389 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390
Li Zefan75139b82009-01-07 18:07:33 -08002391 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002393 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002394 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002395 if (cft->write_u64 || cft->write_s64)
2396 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002397 if (cft->write_string)
2398 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002399 if (cft->trigger) {
2400 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2401 return ret ? ret : nbytes;
2402 }
Paul Menage355e0c42007-10-18 23:39:33 -07002403 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404}
2405
Paul Menagef4c753b2008-04-29 00:59:56 -07002406static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2407 struct file *file,
2408 char __user *buf, size_t nbytes,
2409 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002410{
Paul Menage84eea842008-07-25 01:47:00 -07002411 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002412 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002413 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2414
2415 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2416}
2417
Paul Menagee73d2c62008-04-29 01:00:06 -07002418static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2419 struct file *file,
2420 char __user *buf, size_t nbytes,
2421 loff_t *ppos)
2422{
Paul Menage84eea842008-07-25 01:47:00 -07002423 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002424 s64 val = cft->read_s64(cgrp, cft);
2425 int len = sprintf(tmp, "%lld\n", (long long) val);
2426
2427 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2428}
2429
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2431 size_t nbytes, loff_t *ppos)
2432{
2433 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002434 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002435
Li Zefan75139b82009-01-07 18:07:33 -08002436 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002437 return -ENODEV;
2438
2439 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002440 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002441 if (cft->read_u64)
2442 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002443 if (cft->read_s64)
2444 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002445 return -EINVAL;
2446}
2447
Paul Menage91796562008-04-29 01:00:01 -07002448/*
2449 * seqfile ops/methods for returning structured data. Currently just
2450 * supports string->u64 maps, but can be extended in future.
2451 */
2452
2453struct cgroup_seqfile_state {
2454 struct cftype *cft;
2455 struct cgroup *cgroup;
2456};
2457
2458static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2459{
2460 struct seq_file *sf = cb->state;
2461 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2462}
2463
2464static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2465{
2466 struct cgroup_seqfile_state *state = m->private;
2467 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002468 if (cft->read_map) {
2469 struct cgroup_map_cb cb = {
2470 .fill = cgroup_map_add,
2471 .state = m,
2472 };
2473 return cft->read_map(state->cgroup, cft, &cb);
2474 }
2475 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002476}
2477
Adrian Bunk96930a62008-07-25 19:46:21 -07002478static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002479{
2480 struct seq_file *seq = file->private_data;
2481 kfree(seq->private);
2482 return single_release(inode, file);
2483}
2484
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002485static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002486 .read = seq_read,
Paul Menagee788e0662008-07-25 01:46:59 -07002487 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002488 .llseek = seq_lseek,
2489 .release = cgroup_seqfile_release,
2490};
2491
Paul Menageddbcc7e2007-10-18 23:39:30 -07002492static int cgroup_file_open(struct inode *inode, struct file *file)
2493{
2494 int err;
2495 struct cftype *cft;
2496
2497 err = generic_file_open(inode, file);
2498 if (err)
2499 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002500 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002501
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002502 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002503 struct cgroup_seqfile_state *state =
2504 kzalloc(sizeof(*state), GFP_USER);
2505 if (!state)
2506 return -ENOMEM;
2507 state->cft = cft;
2508 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2509 file->f_op = &cgroup_seqfile_operations;
2510 err = single_open(file, cgroup_seqfile_show, state);
2511 if (err < 0)
2512 kfree(state);
2513 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002514 err = cft->open(inode, file);
2515 else
2516 err = 0;
2517
2518 return err;
2519}
2520
2521static int cgroup_file_release(struct inode *inode, struct file *file)
2522{
2523 struct cftype *cft = __d_cft(file->f_dentry);
2524 if (cft->release)
2525 return cft->release(inode, file);
2526 return 0;
2527}
2528
2529/*
2530 * cgroup_rename - Only allow simple rename of directories in place.
2531 */
2532static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2533 struct inode *new_dir, struct dentry *new_dentry)
2534{
2535 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2536 return -ENOTDIR;
2537 if (new_dentry->d_inode)
2538 return -EEXIST;
2539 if (old_dir != new_dir)
2540 return -EIO;
2541 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2542}
2543
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002544static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2545{
2546 if (S_ISDIR(dentry->d_inode->i_mode))
2547 return &__d_cgrp(dentry)->xattrs;
2548 else
2549 return &__d_cft(dentry)->xattrs;
2550}
2551
2552static inline int xattr_enabled(struct dentry *dentry)
2553{
2554 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2555 return test_bit(ROOT_XATTR, &root->flags);
2556}
2557
2558static bool is_valid_xattr(const char *name)
2559{
2560 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2561 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2562 return true;
2563 return false;
2564}
2565
2566static int cgroup_setxattr(struct dentry *dentry, const char *name,
2567 const void *val, size_t size, int flags)
2568{
2569 if (!xattr_enabled(dentry))
2570 return -EOPNOTSUPP;
2571 if (!is_valid_xattr(name))
2572 return -EINVAL;
2573 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2574}
2575
2576static int cgroup_removexattr(struct dentry *dentry, const char *name)
2577{
2578 if (!xattr_enabled(dentry))
2579 return -EOPNOTSUPP;
2580 if (!is_valid_xattr(name))
2581 return -EINVAL;
2582 return simple_xattr_remove(__d_xattrs(dentry), name);
2583}
2584
2585static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2586 void *buf, size_t size)
2587{
2588 if (!xattr_enabled(dentry))
2589 return -EOPNOTSUPP;
2590 if (!is_valid_xattr(name))
2591 return -EINVAL;
2592 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2593}
2594
2595static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2596{
2597 if (!xattr_enabled(dentry))
2598 return -EOPNOTSUPP;
2599 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2600}
2601
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002602static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002603 .read = cgroup_file_read,
2604 .write = cgroup_file_write,
2605 .llseek = generic_file_llseek,
2606 .open = cgroup_file_open,
2607 .release = cgroup_file_release,
2608};
2609
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002610static const struct inode_operations cgroup_file_inode_operations = {
2611 .setxattr = cgroup_setxattr,
2612 .getxattr = cgroup_getxattr,
2613 .listxattr = cgroup_listxattr,
2614 .removexattr = cgroup_removexattr,
2615};
2616
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002617static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002618 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002619 .mkdir = cgroup_mkdir,
2620 .rmdir = cgroup_rmdir,
2621 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002622 .setxattr = cgroup_setxattr,
2623 .getxattr = cgroup_getxattr,
2624 .listxattr = cgroup_listxattr,
2625 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002626};
2627
Al Viro00cd8dd2012-06-10 17:13:09 -04002628static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002629{
2630 if (dentry->d_name.len > NAME_MAX)
2631 return ERR_PTR(-ENAMETOOLONG);
2632 d_add(dentry, NULL);
2633 return NULL;
2634}
2635
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002636/*
2637 * Check if a file is a control file
2638 */
2639static inline struct cftype *__file_cft(struct file *file)
2640{
2641 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2642 return ERR_PTR(-EINVAL);
2643 return __d_cft(file->f_dentry);
2644}
2645
Al Viroa5e7ed32011-07-26 01:55:55 -04002646static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002647 struct super_block *sb)
2648{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002649 struct inode *inode;
2650
2651 if (!dentry)
2652 return -ENOENT;
2653 if (dentry->d_inode)
2654 return -EEXIST;
2655
2656 inode = cgroup_new_inode(mode, sb);
2657 if (!inode)
2658 return -ENOMEM;
2659
2660 if (S_ISDIR(mode)) {
2661 inode->i_op = &cgroup_dir_inode_operations;
2662 inode->i_fop = &simple_dir_operations;
2663
2664 /* start off with i_nlink == 2 (for "." entry) */
2665 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002666 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002667
Tejun Heob8a2df6a2012-11-19 08:13:37 -08002668 /*
2669 * Control reaches here with cgroup_mutex held.
2670 * @inode->i_mutex should nest outside cgroup_mutex but we
2671 * want to populate it immediately without releasing
2672 * cgroup_mutex. As @inode isn't visible to anyone else
2673 * yet, trylock will always succeed without affecting
2674 * lockdep checks.
2675 */
2676 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002677 } else if (S_ISREG(mode)) {
2678 inode->i_size = 0;
2679 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002680 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002681 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002682 d_instantiate(dentry, inode);
2683 dget(dentry); /* Extra count - pin the dentry in core */
2684 return 0;
2685}
2686
Li Zefan099fca32009-04-02 16:57:29 -07002687/**
2688 * cgroup_file_mode - deduce file mode of a control file
2689 * @cft: the control file in question
2690 *
2691 * returns cft->mode if ->mode is not 0
2692 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2693 * returns S_IRUGO if it has only a read handler
2694 * returns S_IWUSR if it has only a write hander
2695 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002696static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002697{
Al Viroa5e7ed32011-07-26 01:55:55 -04002698 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002699
2700 if (cft->mode)
2701 return cft->mode;
2702
2703 if (cft->read || cft->read_u64 || cft->read_s64 ||
2704 cft->read_map || cft->read_seq_string)
2705 mode |= S_IRUGO;
2706
2707 if (cft->write || cft->write_u64 || cft->write_s64 ||
2708 cft->write_string || cft->trigger)
2709 mode |= S_IWUSR;
2710
2711 return mode;
2712}
2713
Tejun Heodb0416b2012-04-01 12:09:55 -07002714static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002715 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716{
Paul Menagebd89aab2007-10-18 23:40:44 -07002717 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002718 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002720 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002722 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002723 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002724
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002725 simple_xattrs_init(&cft->xattrs);
2726
Tejun Heo8e3f6542012-04-01 12:09:55 -07002727 /* does @cft->flags tell us to skip creation on @cgrp? */
2728 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2729 return 0;
2730 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2731 return 0;
2732
Paul Menagebd89aab2007-10-18 23:40:44 -07002733 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002734 strcpy(name, subsys->name);
2735 strcat(name, ".");
2736 }
2737 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002738
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002740
2741 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2742 if (!cfe)
2743 return -ENOMEM;
2744
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002746 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002747 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002748 goto out;
2749 }
2750
2751 mode = cgroup_file_mode(cft);
2752 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2753 if (!error) {
2754 cfe->type = (void *)cft;
2755 cfe->dentry = dentry;
2756 dentry->d_fsdata = cfe;
2757 list_add_tail(&cfe->node, &parent->files);
2758 cfe = NULL;
2759 }
2760 dput(dentry);
2761out:
2762 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002763 return error;
2764}
2765
Tejun Heo79578622012-04-01 12:09:56 -07002766static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002767 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002768{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002769 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002770 int err, ret = 0;
2771
2772 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Tejun Heo79578622012-04-01 12:09:56 -07002773 if (is_add)
2774 err = cgroup_add_file(cgrp, subsys, cft);
2775 else
2776 err = cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002777 if (err) {
Tejun Heo79578622012-04-01 12:09:56 -07002778 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2779 is_add ? "add" : "remove", cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002780 ret = err;
2781 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002782 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002783 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002784}
2785
Tejun Heo8e3f6542012-04-01 12:09:55 -07002786static DEFINE_MUTEX(cgroup_cft_mutex);
2787
2788static void cgroup_cfts_prepare(void)
2789 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2790{
2791 /*
2792 * Thanks to the entanglement with vfs inode locking, we can't walk
2793 * the existing cgroups under cgroup_mutex and create files.
2794 * Instead, we increment reference on all cgroups and build list of
2795 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2796 * exclusive access to the field.
2797 */
2798 mutex_lock(&cgroup_cft_mutex);
2799 mutex_lock(&cgroup_mutex);
2800}
2801
2802static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002803 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002804 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2805{
2806 LIST_HEAD(pending);
2807 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002808
2809 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2810 if (cfts && ss->root != &rootnode) {
2811 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2812 dget(cgrp->dentry);
2813 list_add_tail(&cgrp->cft_q_node, &pending);
2814 }
2815 }
2816
2817 mutex_unlock(&cgroup_mutex);
2818
2819 /*
2820 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2821 * files for all cgroups which were created before.
2822 */
2823 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2824 struct inode *inode = cgrp->dentry->d_inode;
2825
2826 mutex_lock(&inode->i_mutex);
2827 mutex_lock(&cgroup_mutex);
2828 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002829 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002830 mutex_unlock(&cgroup_mutex);
2831 mutex_unlock(&inode->i_mutex);
2832
2833 list_del_init(&cgrp->cft_q_node);
2834 dput(cgrp->dentry);
2835 }
2836
2837 mutex_unlock(&cgroup_cft_mutex);
2838}
2839
2840/**
2841 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2842 * @ss: target cgroup subsystem
2843 * @cfts: zero-length name terminated array of cftypes
2844 *
2845 * Register @cfts to @ss. Files described by @cfts are created for all
2846 * existing cgroups to which @ss is attached and all future cgroups will
2847 * have them too. This function can be called anytime whether @ss is
2848 * attached or not.
2849 *
2850 * Returns 0 on successful registration, -errno on failure. Note that this
2851 * function currently returns 0 as long as @cfts registration is successful
2852 * even if some file creation attempts on existing cgroups fail.
2853 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002854int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002855{
2856 struct cftype_set *set;
2857
2858 set = kzalloc(sizeof(*set), GFP_KERNEL);
2859 if (!set)
2860 return -ENOMEM;
2861
2862 cgroup_cfts_prepare();
2863 set->cfts = cfts;
2864 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002865 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002866
2867 return 0;
2868}
2869EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2870
Li Zefana043e3b2008-02-23 15:24:09 -08002871/**
Tejun Heo79578622012-04-01 12:09:56 -07002872 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2873 * @ss: target cgroup subsystem
2874 * @cfts: zero-length name terminated array of cftypes
2875 *
2876 * Unregister @cfts from @ss. Files described by @cfts are removed from
2877 * all existing cgroups to which @ss is attached and all future cgroups
2878 * won't have them either. This function can be called anytime whether @ss
2879 * is attached or not.
2880 *
2881 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2882 * registered with @ss.
2883 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002884int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002885{
2886 struct cftype_set *set;
2887
2888 cgroup_cfts_prepare();
2889
2890 list_for_each_entry(set, &ss->cftsets, node) {
2891 if (set->cfts == cfts) {
2892 list_del_init(&set->node);
2893 cgroup_cfts_commit(ss, cfts, false);
2894 return 0;
2895 }
2896 }
2897
2898 cgroup_cfts_commit(ss, NULL, false);
2899 return -ENOENT;
2900}
2901
2902/**
Li Zefana043e3b2008-02-23 15:24:09 -08002903 * cgroup_task_count - count the number of tasks in a cgroup.
2904 * @cgrp: the cgroup in question
2905 *
2906 * Return the number of tasks in the cgroup.
2907 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002908int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002909{
2910 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002911 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002912
Paul Menage817929e2007-10-18 23:39:36 -07002913 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002914 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002915 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002916 }
2917 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002918 return count;
2919}
2920
2921/*
Paul Menage817929e2007-10-18 23:39:36 -07002922 * Advance a list_head iterator. The iterator should be positioned at
2923 * the start of a css_set
2924 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002925static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002926 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002927{
2928 struct list_head *l = it->cg_link;
2929 struct cg_cgroup_link *link;
2930 struct css_set *cg;
2931
2932 /* Advance to the next non-empty css_set */
2933 do {
2934 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002935 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002936 it->cg_link = NULL;
2937 return;
2938 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002939 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002940 cg = link->cg;
2941 } while (list_empty(&cg->tasks));
2942 it->cg_link = l;
2943 it->task = cg->tasks.next;
2944}
2945
Cliff Wickman31a7df02008-02-07 00:14:42 -08002946/*
2947 * To reduce the fork() overhead for systems that are not actually
2948 * using their cgroups capability, we don't maintain the lists running
2949 * through each css_set to its tasks until we see the list actually
2950 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002951 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002952static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002953{
2954 struct task_struct *p, *g;
2955 write_lock(&css_set_lock);
2956 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002957 /*
2958 * We need tasklist_lock because RCU is not safe against
2959 * while_each_thread(). Besides, a forking task that has passed
2960 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2961 * is not guaranteed to have its child immediately visible in the
2962 * tasklist if we walk through it with RCU.
2963 */
2964 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002965 do_each_thread(g, p) {
2966 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002967 /*
2968 * We should check if the process is exiting, otherwise
2969 * it will race with cgroup_exit() in that the list
2970 * entry won't be deleted though the process has exited.
2971 */
2972 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002973 list_add(&p->cg_list, &p->cgroups->tasks);
2974 task_unlock(p);
2975 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002976 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002977 write_unlock(&css_set_lock);
2978}
2979
Tejun Heo574bd9f2012-11-09 09:12:29 -08002980/**
2981 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2982 * @pos: the current position (%NULL to initiate traversal)
2983 * @cgroup: cgroup whose descendants to walk
2984 *
2985 * To be used by cgroup_for_each_descendant_pre(). Find the next
2986 * descendant to visit for pre-order traversal of @cgroup's descendants.
2987 */
2988struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2989 struct cgroup *cgroup)
2990{
2991 struct cgroup *next;
2992
2993 WARN_ON_ONCE(!rcu_read_lock_held());
2994
2995 /* if first iteration, pretend we just visited @cgroup */
2996 if (!pos) {
2997 if (list_empty(&cgroup->children))
2998 return NULL;
2999 pos = cgroup;
3000 }
3001
3002 /* visit the first child if exists */
3003 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3004 if (next)
3005 return next;
3006
3007 /* no child, visit my or the closest ancestor's next sibling */
3008 do {
3009 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3010 sibling);
3011 if (&next->sibling != &pos->parent->children)
3012 return next;
3013
3014 pos = pos->parent;
3015 } while (pos != cgroup);
3016
3017 return NULL;
3018}
3019EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3020
3021static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3022{
3023 struct cgroup *last;
3024
3025 do {
3026 last = pos;
3027 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3028 sibling);
3029 } while (pos);
3030
3031 return last;
3032}
3033
3034/**
3035 * cgroup_next_descendant_post - find the next descendant for post-order walk
3036 * @pos: the current position (%NULL to initiate traversal)
3037 * @cgroup: cgroup whose descendants to walk
3038 *
3039 * To be used by cgroup_for_each_descendant_post(). Find the next
3040 * descendant to visit for post-order traversal of @cgroup's descendants.
3041 */
3042struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3043 struct cgroup *cgroup)
3044{
3045 struct cgroup *next;
3046
3047 WARN_ON_ONCE(!rcu_read_lock_held());
3048
3049 /* if first iteration, visit the leftmost descendant */
3050 if (!pos) {
3051 next = cgroup_leftmost_descendant(cgroup);
3052 return next != cgroup ? next : NULL;
3053 }
3054
3055 /* if there's an unvisited sibling, visit its leftmost descendant */
3056 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3057 if (&next->sibling != &pos->parent->children)
3058 return cgroup_leftmost_descendant(next);
3059
3060 /* no sibling left, visit parent */
3061 next = pos->parent;
3062 return next != cgroup ? next : NULL;
3063}
3064EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3065
Paul Menagebd89aab2007-10-18 23:40:44 -07003066void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003067 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003068{
3069 /*
3070 * The first time anyone tries to iterate across a cgroup,
3071 * we need to enable the list linking each css_set to its
3072 * tasks, and fix up all existing tasks.
3073 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003074 if (!use_task_css_set_links)
3075 cgroup_enable_task_cg_lists();
3076
Paul Menage817929e2007-10-18 23:39:36 -07003077 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003078 it->cg_link = &cgrp->css_sets;
3079 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003080}
3081
Paul Menagebd89aab2007-10-18 23:40:44 -07003082struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003083 struct cgroup_iter *it)
3084{
3085 struct task_struct *res;
3086 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003087 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003088
3089 /* If the iterator cg is NULL, we have no tasks */
3090 if (!it->cg_link)
3091 return NULL;
3092 res = list_entry(l, struct task_struct, cg_list);
3093 /* Advance iterator to find next entry */
3094 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003095 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3096 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003097 /* We reached the end of this task list - move on to
3098 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003099 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003100 } else {
3101 it->task = l;
3102 }
3103 return res;
3104}
3105
Paul Menagebd89aab2007-10-18 23:40:44 -07003106void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003107 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003108{
3109 read_unlock(&css_set_lock);
3110}
3111
Cliff Wickman31a7df02008-02-07 00:14:42 -08003112static inline int started_after_time(struct task_struct *t1,
3113 struct timespec *time,
3114 struct task_struct *t2)
3115{
3116 int start_diff = timespec_compare(&t1->start_time, time);
3117 if (start_diff > 0) {
3118 return 1;
3119 } else if (start_diff < 0) {
3120 return 0;
3121 } else {
3122 /*
3123 * Arbitrarily, if two processes started at the same
3124 * time, we'll say that the lower pointer value
3125 * started first. Note that t2 may have exited by now
3126 * so this may not be a valid pointer any longer, but
3127 * that's fine - it still serves to distinguish
3128 * between two tasks started (effectively) simultaneously.
3129 */
3130 return t1 > t2;
3131 }
3132}
3133
3134/*
3135 * This function is a callback from heap_insert() and is used to order
3136 * the heap.
3137 * In this case we order the heap in descending task start time.
3138 */
3139static inline int started_after(void *p1, void *p2)
3140{
3141 struct task_struct *t1 = p1;
3142 struct task_struct *t2 = p2;
3143 return started_after_time(t1, &t2->start_time, t2);
3144}
3145
3146/**
3147 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3148 * @scan: struct cgroup_scanner containing arguments for the scan
3149 *
3150 * Arguments include pointers to callback functions test_task() and
3151 * process_task().
3152 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3153 * and if it returns true, call process_task() for it also.
3154 * The test_task pointer may be NULL, meaning always true (select all tasks).
3155 * Effectively duplicates cgroup_iter_{start,next,end}()
3156 * but does not lock css_set_lock for the call to process_task().
3157 * The struct cgroup_scanner may be embedded in any structure of the caller's
3158 * creation.
3159 * It is guaranteed that process_task() will act on every task that
3160 * is a member of the cgroup for the duration of this call. This
3161 * function may or may not call process_task() for tasks that exit
3162 * or move to a different cgroup during the call, or are forked or
3163 * move into the cgroup during the call.
3164 *
3165 * Note that test_task() may be called with locks held, and may in some
3166 * situations be called multiple times for the same task, so it should
3167 * be cheap.
3168 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3169 * pre-allocated and will be used for heap operations (and its "gt" member will
3170 * be overwritten), else a temporary heap will be used (allocation of which
3171 * may cause this function to fail).
3172 */
3173int cgroup_scan_tasks(struct cgroup_scanner *scan)
3174{
3175 int retval, i;
3176 struct cgroup_iter it;
3177 struct task_struct *p, *dropped;
3178 /* Never dereference latest_task, since it's not refcounted */
3179 struct task_struct *latest_task = NULL;
3180 struct ptr_heap tmp_heap;
3181 struct ptr_heap *heap;
3182 struct timespec latest_time = { 0, 0 };
3183
3184 if (scan->heap) {
3185 /* The caller supplied our heap and pre-allocated its memory */
3186 heap = scan->heap;
3187 heap->gt = &started_after;
3188 } else {
3189 /* We need to allocate our own heap memory */
3190 heap = &tmp_heap;
3191 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3192 if (retval)
3193 /* cannot allocate the heap */
3194 return retval;
3195 }
3196
3197 again:
3198 /*
3199 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3200 * to determine which are of interest, and using the scanner's
3201 * "process_task" callback to process any of them that need an update.
3202 * Since we don't want to hold any locks during the task updates,
3203 * gather tasks to be processed in a heap structure.
3204 * The heap is sorted by descending task start time.
3205 * If the statically-sized heap fills up, we overflow tasks that
3206 * started later, and in future iterations only consider tasks that
3207 * started after the latest task in the previous pass. This
3208 * guarantees forward progress and that we don't miss any tasks.
3209 */
3210 heap->size = 0;
3211 cgroup_iter_start(scan->cg, &it);
3212 while ((p = cgroup_iter_next(scan->cg, &it))) {
3213 /*
3214 * Only affect tasks that qualify per the caller's callback,
3215 * if he provided one
3216 */
3217 if (scan->test_task && !scan->test_task(p, scan))
3218 continue;
3219 /*
3220 * Only process tasks that started after the last task
3221 * we processed
3222 */
3223 if (!started_after_time(p, &latest_time, latest_task))
3224 continue;
3225 dropped = heap_insert(heap, p);
3226 if (dropped == NULL) {
3227 /*
3228 * The new task was inserted; the heap wasn't
3229 * previously full
3230 */
3231 get_task_struct(p);
3232 } else if (dropped != p) {
3233 /*
3234 * The new task was inserted, and pushed out a
3235 * different task
3236 */
3237 get_task_struct(p);
3238 put_task_struct(dropped);
3239 }
3240 /*
3241 * Else the new task was newer than anything already in
3242 * the heap and wasn't inserted
3243 */
3244 }
3245 cgroup_iter_end(scan->cg, &it);
3246
3247 if (heap->size) {
3248 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003249 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003250 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003251 latest_time = q->start_time;
3252 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003253 }
3254 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003255 scan->process_task(q, scan);
3256 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003257 }
3258 /*
3259 * If we had to process any tasks at all, scan again
3260 * in case some of them were in the middle of forking
3261 * children that didn't get processed.
3262 * Not the most efficient way to do it, but it avoids
3263 * having to take callback_mutex in the fork path
3264 */
3265 goto again;
3266 }
3267 if (heap == &tmp_heap)
3268 heap_free(&tmp_heap);
3269 return 0;
3270}
3271
Paul Menage817929e2007-10-18 23:39:36 -07003272/*
Ben Blum102a7752009-09-23 15:56:26 -07003273 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003274 *
3275 * Reading this file can return large amounts of data if a cgroup has
3276 * *lots* of attached tasks. So it may need several calls to read(),
3277 * but we cannot guarantee that the information we produce is correct
3278 * unless we produce it entirely atomically.
3279 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003280 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003281
Li Zefan24528252012-01-20 11:58:43 +08003282/* which pidlist file are we talking about? */
3283enum cgroup_filetype {
3284 CGROUP_FILE_PROCS,
3285 CGROUP_FILE_TASKS,
3286};
3287
3288/*
3289 * A pidlist is a list of pids that virtually represents the contents of one
3290 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3291 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3292 * to the cgroup.
3293 */
3294struct cgroup_pidlist {
3295 /*
3296 * used to find which pidlist is wanted. doesn't change as long as
3297 * this particular list stays in the list.
3298 */
3299 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3300 /* array of xids */
3301 pid_t *list;
3302 /* how many elements the above list has */
3303 int length;
3304 /* how many files are using the current array */
3305 int use_count;
3306 /* each of these stored in a list by its cgroup */
3307 struct list_head links;
3308 /* pointer to the cgroup we belong to, for list removal purposes */
3309 struct cgroup *owner;
3310 /* protects the other fields */
3311 struct rw_semaphore mutex;
3312};
3313
Paul Menagebbcb81d2007-10-18 23:39:32 -07003314/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003315 * The following two functions "fix" the issue where there are more pids
3316 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3317 * TODO: replace with a kernel-wide solution to this problem
3318 */
3319#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3320static void *pidlist_allocate(int count)
3321{
3322 if (PIDLIST_TOO_LARGE(count))
3323 return vmalloc(count * sizeof(pid_t));
3324 else
3325 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3326}
3327static void pidlist_free(void *p)
3328{
3329 if (is_vmalloc_addr(p))
3330 vfree(p);
3331 else
3332 kfree(p);
3333}
3334static void *pidlist_resize(void *p, int newcount)
3335{
3336 void *newlist;
3337 /* note: if new alloc fails, old p will still be valid either way */
3338 if (is_vmalloc_addr(p)) {
3339 newlist = vmalloc(newcount * sizeof(pid_t));
3340 if (!newlist)
3341 return NULL;
3342 memcpy(newlist, p, newcount * sizeof(pid_t));
3343 vfree(p);
3344 } else {
3345 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3346 }
3347 return newlist;
3348}
3349
3350/*
Ben Blum102a7752009-09-23 15:56:26 -07003351 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3352 * If the new stripped list is sufficiently smaller and there's enough memory
3353 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3354 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003355 */
Ben Blum102a7752009-09-23 15:56:26 -07003356/* is the size difference enough that we should re-allocate the array? */
3357#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3358static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003359{
Ben Blum102a7752009-09-23 15:56:26 -07003360 int src, dest = 1;
3361 pid_t *list = *p;
3362 pid_t *newlist;
3363
3364 /*
3365 * we presume the 0th element is unique, so i starts at 1. trivial
3366 * edge cases first; no work needs to be done for either
3367 */
3368 if (length == 0 || length == 1)
3369 return length;
3370 /* src and dest walk down the list; dest counts unique elements */
3371 for (src = 1; src < length; src++) {
3372 /* find next unique element */
3373 while (list[src] == list[src-1]) {
3374 src++;
3375 if (src == length)
3376 goto after;
3377 }
3378 /* dest always points to where the next unique element goes */
3379 list[dest] = list[src];
3380 dest++;
3381 }
3382after:
3383 /*
3384 * if the length difference is large enough, we want to allocate a
3385 * smaller buffer to save memory. if this fails due to out of memory,
3386 * we'll just stay with what we've got.
3387 */
3388 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003389 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003390 if (newlist)
3391 *p = newlist;
3392 }
3393 return dest;
3394}
3395
3396static int cmppid(const void *a, const void *b)
3397{
3398 return *(pid_t *)a - *(pid_t *)b;
3399}
3400
3401/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003402 * find the appropriate pidlist for our purpose (given procs vs tasks)
3403 * returns with the lock on that pidlist already held, and takes care
3404 * of the use count, or returns NULL with no locks held if we're out of
3405 * memory.
3406 */
3407static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3408 enum cgroup_filetype type)
3409{
3410 struct cgroup_pidlist *l;
3411 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003412 struct pid_namespace *ns = current->nsproxy->pid_ns;
3413
Ben Blum72a8cb32009-09-23 15:56:27 -07003414 /*
3415 * We can't drop the pidlist_mutex before taking the l->mutex in case
3416 * the last ref-holder is trying to remove l from the list at the same
3417 * time. Holding the pidlist_mutex precludes somebody taking whichever
3418 * list we find out from under us - compare release_pid_array().
3419 */
3420 mutex_lock(&cgrp->pidlist_mutex);
3421 list_for_each_entry(l, &cgrp->pidlists, links) {
3422 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003423 /* make sure l doesn't vanish out from under us */
3424 down_write(&l->mutex);
3425 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003426 return l;
3427 }
3428 }
3429 /* entry not found; create a new one */
3430 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3431 if (!l) {
3432 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003433 return l;
3434 }
3435 init_rwsem(&l->mutex);
3436 down_write(&l->mutex);
3437 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003438 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003439 l->use_count = 0; /* don't increment here */
3440 l->list = NULL;
3441 l->owner = cgrp;
3442 list_add(&l->links, &cgrp->pidlists);
3443 mutex_unlock(&cgrp->pidlist_mutex);
3444 return l;
3445}
3446
3447/*
Ben Blum102a7752009-09-23 15:56:26 -07003448 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3449 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003450static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3451 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003452{
3453 pid_t *array;
3454 int length;
3455 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003456 struct cgroup_iter it;
3457 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003458 struct cgroup_pidlist *l;
3459
3460 /*
3461 * If cgroup gets more users after we read count, we won't have
3462 * enough space - tough. This race is indistinguishable to the
3463 * caller from the case that the additional cgroup users didn't
3464 * show up until sometime later on.
3465 */
3466 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003467 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003468 if (!array)
3469 return -ENOMEM;
3470 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003471 cgroup_iter_start(cgrp, &it);
3472 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003473 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003474 break;
Ben Blum102a7752009-09-23 15:56:26 -07003475 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003476 if (type == CGROUP_FILE_PROCS)
3477 pid = task_tgid_vnr(tsk);
3478 else
3479 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003480 if (pid > 0) /* make sure to only use valid results */
3481 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003482 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003483 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003484 length = n;
3485 /* now sort & (if procs) strip out duplicates */
3486 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003487 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003488 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003489 l = cgroup_pidlist_find(cgrp, type);
3490 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003491 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003492 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003493 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003494 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003495 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003496 l->list = array;
3497 l->length = length;
3498 l->use_count++;
3499 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003500 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003501 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003502}
3503
Balbir Singh846c7bb2007-10-18 23:39:44 -07003504/**
Li Zefana043e3b2008-02-23 15:24:09 -08003505 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003506 * @stats: cgroupstats to fill information into
3507 * @dentry: A dentry entry belonging to the cgroup for which stats have
3508 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003509 *
3510 * Build and fill cgroupstats so that taskstats can export it to user
3511 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003512 */
3513int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3514{
3515 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003516 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003517 struct cgroup_iter it;
3518 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003519
Balbir Singh846c7bb2007-10-18 23:39:44 -07003520 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003521 * Validate dentry by checking the superblock operations,
3522 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003523 */
Li Zefan33d283b2008-11-19 15:36:48 -08003524 if (dentry->d_sb->s_op != &cgroup_ops ||
3525 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003526 goto err;
3527
3528 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003529 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003530
Paul Menagebd89aab2007-10-18 23:40:44 -07003531 cgroup_iter_start(cgrp, &it);
3532 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003533 switch (tsk->state) {
3534 case TASK_RUNNING:
3535 stats->nr_running++;
3536 break;
3537 case TASK_INTERRUPTIBLE:
3538 stats->nr_sleeping++;
3539 break;
3540 case TASK_UNINTERRUPTIBLE:
3541 stats->nr_uninterruptible++;
3542 break;
3543 case TASK_STOPPED:
3544 stats->nr_stopped++;
3545 break;
3546 default:
3547 if (delayacct_is_task_waiting_on_io(tsk))
3548 stats->nr_io_wait++;
3549 break;
3550 }
3551 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003552 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003553
Balbir Singh846c7bb2007-10-18 23:39:44 -07003554err:
3555 return ret;
3556}
3557
Paul Menage8f3ff202009-09-23 15:56:25 -07003558
Paul Menagecc31edc2008-10-18 20:28:04 -07003559/*
Ben Blum102a7752009-09-23 15:56:26 -07003560 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003561 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003562 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003563 */
3564
Ben Blum102a7752009-09-23 15:56:26 -07003565static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003566{
3567 /*
3568 * Initially we receive a position value that corresponds to
3569 * one more than the last pid shown (or 0 on the first call or
3570 * after a seek to the start). Use a binary-search to find the
3571 * next pid to display, if any
3572 */
Ben Blum102a7752009-09-23 15:56:26 -07003573 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003574 int index = 0, pid = *pos;
3575 int *iter;
3576
Ben Blum102a7752009-09-23 15:56:26 -07003577 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003578 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003579 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003580
Paul Menagecc31edc2008-10-18 20:28:04 -07003581 while (index < end) {
3582 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003583 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003584 index = mid;
3585 break;
Ben Blum102a7752009-09-23 15:56:26 -07003586 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003587 index = mid + 1;
3588 else
3589 end = mid;
3590 }
3591 }
3592 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003593 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003594 return NULL;
3595 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003596 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003597 *pos = *iter;
3598 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003599}
3600
Ben Blum102a7752009-09-23 15:56:26 -07003601static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003602{
Ben Blum102a7752009-09-23 15:56:26 -07003603 struct cgroup_pidlist *l = s->private;
3604 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003605}
3606
Ben Blum102a7752009-09-23 15:56:26 -07003607static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003608{
Ben Blum102a7752009-09-23 15:56:26 -07003609 struct cgroup_pidlist *l = s->private;
3610 pid_t *p = v;
3611 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003612 /*
3613 * Advance to the next pid in the array. If this goes off the
3614 * end, we're done
3615 */
3616 p++;
3617 if (p >= end) {
3618 return NULL;
3619 } else {
3620 *pos = *p;
3621 return p;
3622 }
3623}
3624
Ben Blum102a7752009-09-23 15:56:26 -07003625static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003626{
3627 return seq_printf(s, "%d\n", *(int *)v);
3628}
3629
Ben Blum102a7752009-09-23 15:56:26 -07003630/*
3631 * seq_operations functions for iterating on pidlists through seq_file -
3632 * independent of whether it's tasks or procs
3633 */
3634static const struct seq_operations cgroup_pidlist_seq_operations = {
3635 .start = cgroup_pidlist_start,
3636 .stop = cgroup_pidlist_stop,
3637 .next = cgroup_pidlist_next,
3638 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003639};
3640
Ben Blum102a7752009-09-23 15:56:26 -07003641static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003642{
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 /*
3644 * the case where we're the last user of this particular pidlist will
3645 * have us remove it from the cgroup's list, which entails taking the
3646 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3647 * pidlist_mutex, we have to take pidlist_mutex first.
3648 */
3649 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003650 down_write(&l->mutex);
3651 BUG_ON(!l->use_count);
3652 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003653 /* we're the last user if refcount is 0; remove and free */
3654 list_del(&l->links);
3655 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003656 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003657 put_pid_ns(l->key.ns);
3658 up_write(&l->mutex);
3659 kfree(l);
3660 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003661 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003662 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003663 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003664}
3665
Ben Blum102a7752009-09-23 15:56:26 -07003666static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003667{
Ben Blum102a7752009-09-23 15:56:26 -07003668 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003669 if (!(file->f_mode & FMODE_READ))
3670 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003671 /*
3672 * the seq_file will only be initialized if the file was opened for
3673 * reading; hence we check if it's not null only in that case.
3674 */
3675 l = ((struct seq_file *)file->private_data)->private;
3676 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003677 return seq_release(inode, file);
3678}
3679
Ben Blum102a7752009-09-23 15:56:26 -07003680static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003681 .read = seq_read,
3682 .llseek = seq_lseek,
3683 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003684 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003685};
3686
3687/*
Ben Blum102a7752009-09-23 15:56:26 -07003688 * The following functions handle opens on a file that displays a pidlist
3689 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3690 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003691 */
Ben Blum102a7752009-09-23 15:56:26 -07003692/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003693static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003694{
3695 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003696 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003697 int retval;
3698
3699 /* Nothing to do for write-only files */
3700 if (!(file->f_mode & FMODE_READ))
3701 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003702
Ben Blum102a7752009-09-23 15:56:26 -07003703 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003704 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003705 if (retval)
3706 return retval;
3707 /* configure file information */
3708 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003709
Ben Blum102a7752009-09-23 15:56:26 -07003710 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003711 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003712 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003713 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003714 }
Ben Blum102a7752009-09-23 15:56:26 -07003715 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003716 return 0;
3717}
Ben Blum102a7752009-09-23 15:56:26 -07003718static int cgroup_tasks_open(struct inode *unused, struct file *file)
3719{
Ben Blum72a8cb32009-09-23 15:56:27 -07003720 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003721}
3722static int cgroup_procs_open(struct inode *unused, struct file *file)
3723{
Ben Blum72a8cb32009-09-23 15:56:27 -07003724 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003725}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003726
Paul Menagebd89aab2007-10-18 23:40:44 -07003727static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003728 struct cftype *cft)
3729{
Paul Menagebd89aab2007-10-18 23:40:44 -07003730 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003731}
3732
Paul Menage6379c102008-07-25 01:47:01 -07003733static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3734 struct cftype *cft,
3735 u64 val)
3736{
3737 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3738 if (val)
3739 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3740 else
3741 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3742 return 0;
3743}
3744
Paul Menagebbcb81d2007-10-18 23:39:32 -07003745/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003746 * Unregister event and free resources.
3747 *
3748 * Gets called from workqueue.
3749 */
3750static void cgroup_event_remove(struct work_struct *work)
3751{
3752 struct cgroup_event *event = container_of(work, struct cgroup_event,
3753 remove);
3754 struct cgroup *cgrp = event->cgrp;
3755
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003756 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3757
3758 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003759 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003760 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003761}
3762
3763/*
3764 * Gets called on POLLHUP on eventfd when user closes it.
3765 *
3766 * Called with wqh->lock held and interrupts disabled.
3767 */
3768static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3769 int sync, void *key)
3770{
3771 struct cgroup_event *event = container_of(wait,
3772 struct cgroup_event, wait);
3773 struct cgroup *cgrp = event->cgrp;
3774 unsigned long flags = (unsigned long)key;
3775
3776 if (flags & POLLHUP) {
Changli Gaoa93d2f172010-05-07 14:33:26 +08003777 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003778 spin_lock(&cgrp->event_list_lock);
Greg Thelen9718ceb2012-11-28 13:50:45 -08003779 list_del_init(&event->list);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003780 spin_unlock(&cgrp->event_list_lock);
3781 /*
3782 * We are in atomic context, but cgroup_event_remove() may
3783 * sleep, so we have to call it in workqueue.
3784 */
3785 schedule_work(&event->remove);
3786 }
3787
3788 return 0;
3789}
3790
3791static void cgroup_event_ptable_queue_proc(struct file *file,
3792 wait_queue_head_t *wqh, poll_table *pt)
3793{
3794 struct cgroup_event *event = container_of(pt,
3795 struct cgroup_event, pt);
3796
3797 event->wqh = wqh;
3798 add_wait_queue(wqh, &event->wait);
3799}
3800
3801/*
3802 * Parse input and register new cgroup event handler.
3803 *
3804 * Input must be in format '<event_fd> <control_fd> <args>'.
3805 * Interpretation of args is defined by control file implementation.
3806 */
3807static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3808 const char *buffer)
3809{
3810 struct cgroup_event *event = NULL;
3811 unsigned int efd, cfd;
3812 struct file *efile = NULL;
3813 struct file *cfile = NULL;
3814 char *endp;
3815 int ret;
3816
3817 efd = simple_strtoul(buffer, &endp, 10);
3818 if (*endp != ' ')
3819 return -EINVAL;
3820 buffer = endp + 1;
3821
3822 cfd = simple_strtoul(buffer, &endp, 10);
3823 if ((*endp != ' ') && (*endp != '\0'))
3824 return -EINVAL;
3825 buffer = endp + 1;
3826
3827 event = kzalloc(sizeof(*event), GFP_KERNEL);
3828 if (!event)
3829 return -ENOMEM;
3830 event->cgrp = cgrp;
3831 INIT_LIST_HEAD(&event->list);
3832 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3833 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3834 INIT_WORK(&event->remove, cgroup_event_remove);
3835
3836 efile = eventfd_fget(efd);
3837 if (IS_ERR(efile)) {
3838 ret = PTR_ERR(efile);
3839 goto fail;
3840 }
3841
3842 event->eventfd = eventfd_ctx_fileget(efile);
3843 if (IS_ERR(event->eventfd)) {
3844 ret = PTR_ERR(event->eventfd);
3845 goto fail;
3846 }
3847
3848 cfile = fget(cfd);
3849 if (!cfile) {
3850 ret = -EBADF;
3851 goto fail;
3852 }
3853
3854 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003855 /* AV: shouldn't we check that it's been opened for read instead? */
3856 ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003857 if (ret < 0)
3858 goto fail;
3859
3860 event->cft = __file_cft(cfile);
3861 if (IS_ERR(event->cft)) {
3862 ret = PTR_ERR(event->cft);
3863 goto fail;
3864 }
3865
3866 if (!event->cft->register_event || !event->cft->unregister_event) {
3867 ret = -EINVAL;
3868 goto fail;
3869 }
3870
3871 ret = event->cft->register_event(cgrp, event->cft,
3872 event->eventfd, buffer);
3873 if (ret)
3874 goto fail;
3875
3876 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3877 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3878 ret = 0;
3879 goto fail;
3880 }
3881
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003882 /*
3883 * Events should be removed after rmdir of cgroup directory, but before
3884 * destroying subsystem state objects. Let's take reference to cgroup
3885 * directory dentry to do that.
3886 */
3887 dget(cgrp->dentry);
3888
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003889 spin_lock(&cgrp->event_list_lock);
3890 list_add(&event->list, &cgrp->event_list);
3891 spin_unlock(&cgrp->event_list_lock);
3892
3893 fput(cfile);
3894 fput(efile);
3895
3896 return 0;
3897
3898fail:
3899 if (cfile)
3900 fput(cfile);
3901
3902 if (event && event->eventfd && !IS_ERR(event->eventfd))
3903 eventfd_ctx_put(event->eventfd);
3904
3905 if (!IS_ERR_OR_NULL(efile))
3906 fput(efile);
3907
3908 kfree(event);
3909
3910 return ret;
3911}
3912
Daniel Lezcano97978e62010-10-27 15:33:35 -07003913static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3914 struct cftype *cft)
3915{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003916 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003917}
3918
3919static int cgroup_clone_children_write(struct cgroup *cgrp,
3920 struct cftype *cft,
3921 u64 val)
3922{
3923 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003924 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003925 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003926 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003927 return 0;
3928}
3929
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003930/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003931 * for the common functions, 'private' gives the type of file
3932 */
Ben Blum102a7752009-09-23 15:56:26 -07003933/* for hysterical raisins, we can't put this on the older files */
3934#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003935static struct cftype files[] = {
3936 {
3937 .name = "tasks",
3938 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003939 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003940 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003941 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003942 },
Ben Blum102a7752009-09-23 15:56:26 -07003943 {
3944 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3945 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003946 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003947 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003948 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003949 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003950 {
3951 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003952 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003953 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003954 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003955 {
3956 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3957 .write_string = cgroup_write_event_control,
3958 .mode = S_IWUGO,
3959 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003960 {
3961 .name = "cgroup.clone_children",
3962 .read_u64 = cgroup_clone_children_read,
3963 .write_u64 = cgroup_clone_children_write,
3964 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003965 {
3966 .name = "release_agent",
3967 .flags = CFTYPE_ONLY_ON_ROOT,
3968 .read_seq_string = cgroup_release_agent_show,
3969 .write_string = cgroup_release_agent_write,
3970 .max_write_len = PATH_MAX,
3971 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003972 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003973};
3974
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003975/**
3976 * cgroup_populate_dir - selectively creation of files in a directory
3977 * @cgrp: target cgroup
3978 * @base_files: true if the base files should be added
3979 * @subsys_mask: mask of the subsystem ids whose files should be added
3980 */
3981static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3982 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003983{
3984 int err;
3985 struct cgroup_subsys *ss;
3986
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003987 if (base_files) {
3988 err = cgroup_addrm_files(cgrp, NULL, files, true);
3989 if (err < 0)
3990 return err;
3991 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003992
Tejun Heo8e3f6542012-04-01 12:09:55 -07003993 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003994 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003995 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003996 if (!test_bit(ss->subsys_id, &subsys_mask))
3997 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003998
Tejun Heodb0416b2012-04-01 12:09:55 -07003999 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004000 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004001 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004002
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004003 /* This cgroup is ready now */
4004 for_each_subsys(cgrp->root, ss) {
4005 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4006 /*
4007 * Update id->css pointer and make this css visible from
4008 * CSS ID functions. This pointer will be dereferened
4009 * from RCU-read-side without locks.
4010 */
4011 if (css->id)
4012 rcu_assign_pointer(css->id->css, css);
4013 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004014
4015 return 0;
4016}
4017
Tejun Heo48ddbe12012-04-01 12:09:56 -07004018static void css_dput_fn(struct work_struct *work)
4019{
4020 struct cgroup_subsys_state *css =
4021 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004022 struct dentry *dentry = css->cgroup->dentry;
4023 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004024
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004025 atomic_inc(&sb->s_active);
4026 dput(dentry);
4027 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004028}
4029
Paul Menageddbcc7e2007-10-18 23:39:30 -07004030static void init_cgroup_css(struct cgroup_subsys_state *css,
4031 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004032 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004033{
Paul Menagebd89aab2007-10-18 23:40:44 -07004034 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004035 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004036 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004037 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004038 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004039 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004040 BUG_ON(cgrp->subsys[ss->subsys_id]);
4041 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004042
4043 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004044 * css holds an extra ref to @cgrp->dentry which is put on the last
4045 * css_put(). dput() requires process context, which css_put() may
4046 * be called without. @css->dput_work will be used to invoke
4047 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004048 */
4049 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050}
4051
Tejun Heob1929db2012-11-19 08:13:38 -08004052/* invoke ->post_create() on a new CSS and mark it online if successful */
4053static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004054{
Tejun Heob1929db2012-11-19 08:13:38 -08004055 int ret = 0;
4056
Tejun Heoa31f2d32012-11-19 08:13:37 -08004057 lockdep_assert_held(&cgroup_mutex);
4058
Tejun Heo92fb9742012-11-19 08:13:38 -08004059 if (ss->css_online)
4060 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004061 if (!ret)
4062 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4063 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004064}
4065
4066/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4067static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4068 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4069{
4070 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4071
4072 lockdep_assert_held(&cgroup_mutex);
4073
4074 if (!(css->flags & CSS_ONLINE))
4075 return;
4076
4077 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004078 * css_offline() should be called with cgroup_mutex unlocked. See
Tejun Heoa31f2d32012-11-19 08:13:37 -08004079 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4080 * details. This temporary unlocking should go away once
4081 * cgroup_mutex is unexported from controllers.
4082 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004083 if (ss->css_offline) {
Tejun Heoa31f2d32012-11-19 08:13:37 -08004084 mutex_unlock(&cgroup_mutex);
Tejun Heo92fb9742012-11-19 08:13:38 -08004085 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004086 mutex_lock(&cgroup_mutex);
4087 }
4088
4089 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4090}
4091
Paul Menageddbcc7e2007-10-18 23:39:30 -07004092/*
Li Zefana043e3b2008-02-23 15:24:09 -08004093 * cgroup_create - create a cgroup
4094 * @parent: cgroup that will be parent of the new cgroup
4095 * @dentry: dentry of the new cgroup
4096 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004097 *
Li Zefana043e3b2008-02-23 15:24:09 -08004098 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004099 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004100static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004101 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004102{
Paul Menagebd89aab2007-10-18 23:40:44 -07004103 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004104 struct cgroupfs_root *root = parent->root;
4105 int err = 0;
4106 struct cgroup_subsys *ss;
4107 struct super_block *sb = root->sb;
4108
Tejun Heo0a950f62012-11-19 09:02:12 -08004109 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004110 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4111 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004112 return -ENOMEM;
4113
Tejun Heo0a950f62012-11-19 09:02:12 -08004114 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4115 if (cgrp->id < 0)
4116 goto err_free_cgrp;
4117
Tejun Heo976c06b2012-11-05 09:16:59 -08004118 /*
4119 * Only live parents can have children. Note that the liveliness
4120 * check isn't strictly necessary because cgroup_mkdir() and
4121 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4122 * anyway so that locking is contained inside cgroup proper and we
4123 * don't get nasty surprises if we ever grow another caller.
4124 */
4125 if (!cgroup_lock_live_group(parent)) {
4126 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004127 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004128 }
4129
Paul Menageddbcc7e2007-10-18 23:39:30 -07004130 /* Grab a reference on the superblock so the hierarchy doesn't
4131 * get deleted on unmount if there are child cgroups. This
4132 * can be done outside cgroup_mutex, since the sb can't
4133 * disappear while someone has an open control file on the
4134 * fs */
4135 atomic_inc(&sb->s_active);
4136
Paul Menagecc31edc2008-10-18 20:28:04 -07004137 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138
Paul Menagebd89aab2007-10-18 23:40:44 -07004139 cgrp->parent = parent;
4140 cgrp->root = parent->root;
4141 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004142
Li Zefanb6abdb02008-03-04 14:28:19 -08004143 if (notify_on_release(parent))
4144 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4145
Tejun Heo2260e7f2012-11-19 08:13:38 -08004146 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4147 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004148
Paul Menageddbcc7e2007-10-18 23:39:30 -07004149 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004150 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004151
Tejun Heo92fb9742012-11-19 08:13:38 -08004152 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004153 if (IS_ERR(css)) {
4154 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004155 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004156 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004157 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004158 if (ss->use_id) {
4159 err = alloc_css_id(ss, parent, cgrp);
4160 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004161 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004162 }
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;
Glauber Costa1f869e82012-11-30 17:31:23 +04004191
4192 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4193 parent->parent) {
4194 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",
4195 current->comm, current->pid, ss->name);
4196 if (!strcmp(ss->name, "memory"))
4197 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4198 ss->warned_broken_hierarchy = true;
4199 }
Tejun Heoa8638032012-11-09 09:12:29 -08004200 }
4201
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004202 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004203 if (err)
4204 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004205
4206 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004207 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208
4209 return 0;
4210
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004211err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004212 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004213 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004214 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004215 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004217 /* Release the reference count that we took on the superblock */
4218 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004219err_free_id:
4220 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004221err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004222 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004224
4225err_destroy:
4226 cgroup_destroy_locked(cgrp);
4227 mutex_unlock(&cgroup_mutex);
4228 mutex_unlock(&dentry->d_inode->i_mutex);
4229 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004230}
4231
Al Viro18bb1db2011-07-26 01:41:39 -04004232static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004233{
4234 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4235
4236 /* the vfs holds inode->i_mutex already */
4237 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4238}
4239
Tejun Heo28b4c272012-04-01 12:09:56 -07004240/*
4241 * Check the reference count on each subsystem. Since we already
4242 * established that there are no tasks in the cgroup, if the css refcount
4243 * is also 1, then there should be no outstanding references, so the
4244 * subsystem is safe to destroy. We scan across all subsystems rather than
4245 * using the per-hierarchy linked list of mounted subsystems since we can
4246 * be called via check_for_release() with no synchronization other than
4247 * RCU, and the subsystem linked list isn't RCU-safe.
4248 */
Li Zefan55b6fd02008-07-29 22:33:20 -07004249static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004250{
Paul Menage81a6a5c2007-10-18 23:39:38 -07004251 int i;
Tejun Heo28b4c272012-04-01 12:09:56 -07004252
Ben Blumaae8aab2010-03-10 15:22:07 -08004253 /*
4254 * We won't need to lock the subsys array, because the subsystems
4255 * we're concerned about aren't going anywhere since our cgroup root
4256 * has a reference on them.
4257 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004258 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4259 struct cgroup_subsys *ss = subsys[i];
4260 struct cgroup_subsys_state *css;
Tejun Heo28b4c272012-04-01 12:09:56 -07004261
Ben Blumaae8aab2010-03-10 15:22:07 -08004262 /* Skip subsystems not present or not in this hierarchy */
4263 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004264 continue;
Tejun Heo28b4c272012-04-01 12:09:56 -07004265
Paul Menagebd89aab2007-10-18 23:40:44 -07004266 css = cgrp->subsys[ss->subsys_id];
Tejun Heo28b4c272012-04-01 12:09:56 -07004267 /*
4268 * When called from check_for_release() it's possible
Paul Menage81a6a5c2007-10-18 23:39:38 -07004269 * that by this point the cgroup has been removed
4270 * and the css deleted. But a false-positive doesn't
4271 * matter, since it can only happen if the cgroup
4272 * has been deleted and hence no longer needs the
Tejun Heo28b4c272012-04-01 12:09:56 -07004273 * release agent to be called anyway.
4274 */
4275 if (css && css_refcnt(css) > 1)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004276 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004277 }
4278 return 0;
4279}
4280
Tejun Heo42809dd2012-11-19 08:13:37 -08004281static int cgroup_destroy_locked(struct cgroup *cgrp)
4282 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283{
Tejun Heo42809dd2012-11-19 08:13:37 -08004284 struct dentry *d = cgrp->dentry;
4285 struct cgroup *parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004286 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004287 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004288 struct cgroup_subsys *ss;
Greg Thelen205a8722012-11-28 13:50:44 -08004289 LIST_HEAD(tmp_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004290
Tejun Heo42809dd2012-11-19 08:13:37 -08004291 lockdep_assert_held(&d->d_inode->i_mutex);
4292 lockdep_assert_held(&cgroup_mutex);
4293
4294 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004295 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004296
Tejun Heo1a90dd52012-11-05 09:16:59 -08004297 /*
4298 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4299 * removed. This makes future css_tryget() and child creation
4300 * attempts fail thus maintaining the removal conditions verified
4301 * above.
4302 */
Tejun Heoed9577932012-11-05 09:16:58 -08004303 for_each_subsys(cgrp->root, ss) {
4304 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4305
4306 WARN_ON(atomic_read(&css->refcnt) < 0);
4307 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004308 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004309 set_bit(CGRP_REMOVED, &cgrp->flags);
4310
Tejun Heoa31f2d32012-11-19 08:13:37 -08004311 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004312 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004313 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004314
4315 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004316 * Put all the base refs. Each css holds an extra reference to the
4317 * cgroup's dentry and cgroup removal proceeds regardless of css
4318 * refs. On the last put of each css, whenever that may be, the
4319 * extra dentry ref is put so that dentry destruction happens only
4320 * after all css's are released.
4321 */
Tejun Heoe9316082012-11-05 09:16:58 -08004322 for_each_subsys(cgrp->root, ss)
4323 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004325 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004326 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004327 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004328 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004329
Paul Menage999cd8a2009-01-07 18:08:36 -08004330 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004331 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004332 list_del_init(&cgrp->allcg_node);
4333
Tejun Heo42809dd2012-11-19 08:13:37 -08004334 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335 cgroup_d_remove_dir(d);
4336 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004337
Paul Menagebd89aab2007-10-18 23:40:44 -07004338 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004339 check_for_release(parent);
4340
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004341 /*
4342 * Unregister events and notify userspace.
4343 * Notify userspace about cgroup removing only after rmdir of cgroup
Greg Thelen205a8722012-11-28 13:50:44 -08004344 * directory to avoid race between userspace and kernelspace. Use
4345 * a temporary list to avoid a deadlock with cgroup_event_wake(). Since
4346 * cgroup_event_wake() is called with the wait queue head locked,
4347 * remove_wait_queue() cannot be called while holding event_list_lock.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004348 */
4349 spin_lock(&cgrp->event_list_lock);
Greg Thelen205a8722012-11-28 13:50:44 -08004350 list_splice_init(&cgrp->event_list, &tmp_list);
4351 spin_unlock(&cgrp->event_list_lock);
4352 list_for_each_entry_safe(event, tmp, &tmp_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004353 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004354 remove_wait_queue(event->wqh, &event->wait);
4355 eventfd_signal(event->eventfd, 1);
4356 schedule_work(&event->remove);
4357 }
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004358
Paul Menageddbcc7e2007-10-18 23:39:30 -07004359 return 0;
4360}
4361
Tejun Heo42809dd2012-11-19 08:13:37 -08004362static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4363{
4364 int ret;
4365
4366 mutex_lock(&cgroup_mutex);
4367 ret = cgroup_destroy_locked(dentry->d_fsdata);
4368 mutex_unlock(&cgroup_mutex);
4369
4370 return ret;
4371}
4372
Tejun Heo8e3f6542012-04-01 12:09:55 -07004373static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4374{
4375 INIT_LIST_HEAD(&ss->cftsets);
4376
4377 /*
4378 * base_cftset is embedded in subsys itself, no need to worry about
4379 * deregistration.
4380 */
4381 if (ss->base_cftypes) {
4382 ss->base_cftset.cfts = ss->base_cftypes;
4383 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4384 }
4385}
4386
Li Zefan06a11922008-04-29 01:00:07 -07004387static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004388{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004389 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004390
4391 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004392
Tejun Heo648bb562012-11-19 08:13:36 -08004393 mutex_lock(&cgroup_mutex);
4394
Tejun Heo8e3f6542012-04-01 12:09:55 -07004395 /* init base cftset */
4396 cgroup_init_cftsets(ss);
4397
Paul Menageddbcc7e2007-10-18 23:39:30 -07004398 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004399 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004400 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004401 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004402 /* We don't handle early failures gracefully */
4403 BUG_ON(IS_ERR(css));
4404 init_cgroup_css(css, ss, dummytop);
4405
Li Zefane8d55fd2008-04-29 01:00:13 -07004406 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004407 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004408 * newly registered, all tasks and hence the
4409 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004410 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004411
4412 need_forkexit_callback |= ss->fork || ss->exit;
4413
Li Zefane8d55fd2008-04-29 01:00:13 -07004414 /* At system boot, before all subsystems have been
4415 * registered, no tasks have been forked, so we don't
4416 * need to invoke fork callbacks here. */
4417 BUG_ON(!list_empty(&init_task.tasks));
4418
Paul Menageddbcc7e2007-10-18 23:39:30 -07004419 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004420 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004421
Tejun Heo648bb562012-11-19 08:13:36 -08004422 mutex_unlock(&cgroup_mutex);
4423
Ben Blume6a11052010-03-10 15:22:09 -08004424 /* this function shouldn't be used with modular subsystems, since they
4425 * need to register a subsys_id, among other things */
4426 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004427}
4428
4429/**
Ben Blume6a11052010-03-10 15:22:09 -08004430 * cgroup_load_subsys: load and register a modular subsystem at runtime
4431 * @ss: the subsystem to load
4432 *
4433 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004434 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004435 * up for use. If the subsystem is built-in anyway, work is delegated to the
4436 * simpler cgroup_init_subsys.
4437 */
4438int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4439{
Ben Blume6a11052010-03-10 15:22:09 -08004440 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004441 int i, ret;
Ben Blume6a11052010-03-10 15:22:09 -08004442
4443 /* check name and function validity */
4444 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004445 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004446 return -EINVAL;
4447
4448 /*
4449 * we don't support callbacks in modular subsystems. this check is
4450 * before the ss->module check for consistency; a subsystem that could
4451 * be a module should still have no callbacks even if the user isn't
4452 * compiling it as one.
4453 */
4454 if (ss->fork || ss->exit)
4455 return -EINVAL;
4456
4457 /*
4458 * an optionally modular subsystem is built-in: we want to do nothing,
4459 * since cgroup_init_subsys will have already taken care of it.
4460 */
4461 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004462 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004463 BUG_ON(subsys[ss->subsys_id] != ss);
4464 return 0;
4465 }
4466
Tejun Heo8e3f6542012-04-01 12:09:55 -07004467 /* init base cftset */
4468 cgroup_init_cftsets(ss);
4469
Ben Blume6a11052010-03-10 15:22:09 -08004470 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004471 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004472
4473 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004474 * no ss->css_alloc seems to need anything important in the ss
4475 * struct, so this can happen first (i.e. before the rootnode
4476 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004477 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004478 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004479 if (IS_ERR(css)) {
4480 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004481 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004482 mutex_unlock(&cgroup_mutex);
4483 return PTR_ERR(css);
4484 }
4485
4486 list_add(&ss->sibling, &rootnode.subsys_list);
4487 ss->root = &rootnode;
4488
4489 /* our new subsystem will be attached to the dummy hierarchy. */
4490 init_cgroup_css(css, ss, dummytop);
4491 /* init_idr must be after init_cgroup_css because it sets css->id. */
4492 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004493 ret = cgroup_init_idr(ss, css);
4494 if (ret)
4495 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004496 }
4497
4498 /*
4499 * Now we need to entangle the css into the existing css_sets. unlike
4500 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4501 * will need a new pointer to it; done by iterating the css_set_table.
4502 * furthermore, modifying the existing css_sets will corrupt the hash
4503 * table state, so each changed css_set will need its hash recomputed.
4504 * this is all done under the css_set_lock.
4505 */
4506 write_lock(&css_set_lock);
4507 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4508 struct css_set *cg;
4509 struct hlist_node *node, *tmp;
4510 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4511
4512 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4513 /* skip entries that we already rehashed */
4514 if (cg->subsys[ss->subsys_id])
4515 continue;
4516 /* remove existing entry */
4517 hlist_del(&cg->hlist);
4518 /* set new value */
4519 cg->subsys[ss->subsys_id] = css;
4520 /* recompute hash and restore entry */
4521 new_bucket = css_set_hash(cg->subsys);
4522 hlist_add_head(&cg->hlist, new_bucket);
4523 }
4524 }
4525 write_unlock(&css_set_lock);
4526
Ben Blume6a11052010-03-10 15:22:09 -08004527 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004528 ret = online_css(ss, dummytop);
4529 if (ret)
4530 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004531
Ben Blume6a11052010-03-10 15:22:09 -08004532 /* success! */
4533 mutex_unlock(&cgroup_mutex);
4534 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004535
4536err_unload:
4537 mutex_unlock(&cgroup_mutex);
4538 /* @ss can't be mounted here as try_module_get() would fail */
4539 cgroup_unload_subsys(ss);
4540 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004541}
4542EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4543
4544/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004545 * cgroup_unload_subsys: unload a modular subsystem
4546 * @ss: the subsystem to unload
4547 *
4548 * This function should be called in a modular subsystem's exitcall. When this
4549 * function is invoked, the refcount on the subsystem's module will be 0, so
4550 * the subsystem will not be attached to any hierarchy.
4551 */
4552void cgroup_unload_subsys(struct cgroup_subsys *ss)
4553{
4554 struct cg_cgroup_link *link;
4555 struct hlist_head *hhead;
4556
4557 BUG_ON(ss->module == NULL);
4558
4559 /*
4560 * we shouldn't be called if the subsystem is in use, and the use of
4561 * try_module_get in parse_cgroupfs_options should ensure that it
4562 * doesn't start being used while we're killing it off.
4563 */
4564 BUG_ON(ss->root != &rootnode);
4565
4566 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004567
Tejun Heoa31f2d32012-11-19 08:13:37 -08004568 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004569 ss->active = 0;
4570
4571 if (ss->use_id) {
4572 idr_remove_all(&ss->idr);
4573 idr_destroy(&ss->idr);
4574 }
4575
Ben Blumcf5d5942010-03-10 15:22:09 -08004576 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004577 subsys[ss->subsys_id] = NULL;
4578
4579 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004580 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004581
4582 /*
4583 * disentangle the css from all css_sets attached to the dummytop. as
4584 * in loading, we need to pay our respects to the hashtable gods.
4585 */
4586 write_lock(&css_set_lock);
4587 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4588 struct css_set *cg = link->cg;
4589
4590 hlist_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004591 cg->subsys[ss->subsys_id] = NULL;
4592 hhead = css_set_hash(cg->subsys);
4593 hlist_add_head(&cg->hlist, hhead);
4594 }
4595 write_unlock(&css_set_lock);
4596
4597 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004598 * remove subsystem's css from the dummytop and free it - need to
4599 * free before marking as null because ss->css_free needs the
4600 * cgrp->subsys pointer to find their state. note that this also
4601 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004602 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004603 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004604 dummytop->subsys[ss->subsys_id] = NULL;
4605
4606 mutex_unlock(&cgroup_mutex);
4607}
4608EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4609
4610/**
Li Zefana043e3b2008-02-23 15:24:09 -08004611 * cgroup_init_early - cgroup initialization at system boot
4612 *
4613 * Initialize cgroups at system boot, and initialize any
4614 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004615 */
4616int __init cgroup_init_early(void)
4617{
4618 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004619 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004620 INIT_LIST_HEAD(&init_css_set.cg_links);
4621 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004622 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004623 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004624 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004625 root_count = 1;
4626 init_task.cgroups = &init_css_set;
4627
4628 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004629 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004630 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004631 &rootnode.top_cgroup.css_sets);
4632 list_add(&init_css_set_link.cg_link_list,
4633 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004634
Li Zefan472b1052008-04-29 01:00:11 -07004635 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4636 INIT_HLIST_HEAD(&css_set_table[i]);
4637
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004638 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639 struct cgroup_subsys *ss = subsys[i];
4640
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004641 /* at bootup time, we don't worry about modular subsystems */
4642 if (!ss || ss->module)
4643 continue;
4644
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645 BUG_ON(!ss->name);
4646 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004647 BUG_ON(!ss->css_alloc);
4648 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004649 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004650 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004651 ss->name, ss->subsys_id);
4652 BUG();
4653 }
4654
4655 if (ss->early_init)
4656 cgroup_init_subsys(ss);
4657 }
4658 return 0;
4659}
4660
4661/**
Li Zefana043e3b2008-02-23 15:24:09 -08004662 * cgroup_init - cgroup initialization
4663 *
4664 * Register cgroup filesystem and /proc file, and initialize
4665 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004666 */
4667int __init cgroup_init(void)
4668{
4669 int err;
4670 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004671 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004672
4673 err = bdi_init(&cgroup_backing_dev_info);
4674 if (err)
4675 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004676
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004677 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004678 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004679
4680 /* at bootup time, we don't worry about modular subsystems */
4681 if (!ss || ss->module)
4682 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004683 if (!ss->early_init)
4684 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004685 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004686 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004687 }
4688
Li Zefan472b1052008-04-29 01:00:11 -07004689 /* Add init_css_set to the hash table */
4690 hhead = css_set_hash(init_css_set.subsys);
4691 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004692 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004693
4694 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4695 if (!cgroup_kobj) {
4696 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004697 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004698 }
4699
4700 err = register_filesystem(&cgroup_fs_type);
4701 if (err < 0) {
4702 kobject_put(cgroup_kobj);
4703 goto out;
4704 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004705
Li Zefan46ae2202008-04-29 01:00:08 -07004706 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004707
Paul Menageddbcc7e2007-10-18 23:39:30 -07004708out:
Paul Menagea4243162007-10-18 23:39:35 -07004709 if (err)
4710 bdi_destroy(&cgroup_backing_dev_info);
4711
Paul Menageddbcc7e2007-10-18 23:39:30 -07004712 return err;
4713}
Paul Menageb4f48b62007-10-18 23:39:33 -07004714
Paul Menagea4243162007-10-18 23:39:35 -07004715/*
4716 * proc_cgroup_show()
4717 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4718 * - Used for /proc/<pid>/cgroup.
4719 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4720 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004721 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004722 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4723 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4724 * cgroup to top_cgroup.
4725 */
4726
4727/* TODO: Use a proper seq_file iterator */
4728static int proc_cgroup_show(struct seq_file *m, void *v)
4729{
4730 struct pid *pid;
4731 struct task_struct *tsk;
4732 char *buf;
4733 int retval;
4734 struct cgroupfs_root *root;
4735
4736 retval = -ENOMEM;
4737 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4738 if (!buf)
4739 goto out;
4740
4741 retval = -ESRCH;
4742 pid = m->private;
4743 tsk = get_pid_task(pid, PIDTYPE_PID);
4744 if (!tsk)
4745 goto out_free;
4746
4747 retval = 0;
4748
4749 mutex_lock(&cgroup_mutex);
4750
Li Zefane5f6a862009-01-07 18:07:41 -08004751 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004752 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004753 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004754 int count = 0;
4755
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004756 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004757 for_each_subsys(root, ss)
4758 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004759 if (strlen(root->name))
4760 seq_printf(m, "%sname=%s", count ? "," : "",
4761 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004762 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004763 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004764 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004765 if (retval < 0)
4766 goto out_unlock;
4767 seq_puts(m, buf);
4768 seq_putc(m, '\n');
4769 }
4770
4771out_unlock:
4772 mutex_unlock(&cgroup_mutex);
4773 put_task_struct(tsk);
4774out_free:
4775 kfree(buf);
4776out:
4777 return retval;
4778}
4779
4780static int cgroup_open(struct inode *inode, struct file *file)
4781{
4782 struct pid *pid = PROC_I(inode)->pid;
4783 return single_open(file, proc_cgroup_show, pid);
4784}
4785
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004786const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004787 .open = cgroup_open,
4788 .read = seq_read,
4789 .llseek = seq_lseek,
4790 .release = single_release,
4791};
4792
4793/* Display information about each subsystem and each hierarchy */
4794static int proc_cgroupstats_show(struct seq_file *m, void *v)
4795{
4796 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004797
Paul Menage8bab8dd2008-04-04 14:29:57 -07004798 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004799 /*
4800 * ideally we don't want subsystems moving around while we do this.
4801 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4802 * subsys/hierarchy state.
4803 */
Paul Menagea4243162007-10-18 23:39:35 -07004804 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004805 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4806 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004807 if (ss == NULL)
4808 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004809 seq_printf(m, "%s\t%d\t%d\t%d\n",
4810 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004811 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004812 }
4813 mutex_unlock(&cgroup_mutex);
4814 return 0;
4815}
4816
4817static int cgroupstats_open(struct inode *inode, struct file *file)
4818{
Al Viro9dce07f2008-03-29 03:07:28 +00004819 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004820}
4821
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004822static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004823 .open = cgroupstats_open,
4824 .read = seq_read,
4825 .llseek = seq_lseek,
4826 .release = single_release,
4827};
4828
Paul Menageb4f48b62007-10-18 23:39:33 -07004829/**
4830 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004831 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004832 *
4833 * Description: A task inherits its parent's cgroup at fork().
4834 *
4835 * A pointer to the shared css_set was automatically copied in
4836 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004837 * it was not made under the protection of RCU or cgroup_mutex, so
4838 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4839 * have already changed current->cgroups, allowing the previously
4840 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004841 *
4842 * At the point that cgroup_fork() is called, 'current' is the parent
4843 * task, and the passed argument 'child' points to the child task.
4844 */
4845void cgroup_fork(struct task_struct *child)
4846{
Tejun Heo9bb71302012-10-18 17:52:07 -07004847 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004848 child->cgroups = current->cgroups;
4849 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004850 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004851 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004852}
4853
4854/**
Li Zefana043e3b2008-02-23 15:24:09 -08004855 * cgroup_post_fork - called on a new task after adding it to the task list
4856 * @child: the task in question
4857 *
Tejun Heo5edee612012-10-16 15:03:14 -07004858 * Adds the task to the list running through its css_set if necessary and
4859 * call the subsystem fork() callbacks. Has to be after the task is
4860 * visible on the task list in case we race with the first call to
4861 * cgroup_iter_start() - to guarantee that the new task ends up on its
4862 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004863 */
Paul Menage817929e2007-10-18 23:39:36 -07004864void cgroup_post_fork(struct task_struct *child)
4865{
Tejun Heo5edee612012-10-16 15:03:14 -07004866 int i;
4867
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004868 /*
4869 * use_task_css_set_links is set to 1 before we walk the tasklist
4870 * under the tasklist_lock and we read it here after we added the child
4871 * to the tasklist under the tasklist_lock as well. If the child wasn't
4872 * yet in the tasklist when we walked through it from
4873 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4874 * should be visible now due to the paired locking and barriers implied
4875 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4876 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4877 * lock on fork.
4878 */
Paul Menage817929e2007-10-18 23:39:36 -07004879 if (use_task_css_set_links) {
4880 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004881 task_lock(child);
4882 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004883 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004884 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004885 write_unlock(&css_set_lock);
4886 }
Tejun Heo5edee612012-10-16 15:03:14 -07004887
4888 /*
4889 * Call ss->fork(). This must happen after @child is linked on
4890 * css_set; otherwise, @child might change state between ->fork()
4891 * and addition to css_set.
4892 */
4893 if (need_forkexit_callback) {
4894 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4895 struct cgroup_subsys *ss = subsys[i];
4896
4897 /*
4898 * fork/exit callbacks are supported only for
4899 * builtin subsystems and we don't need further
4900 * synchronization as they never go away.
4901 */
4902 if (!ss || ss->module)
4903 continue;
4904
4905 if (ss->fork)
4906 ss->fork(child);
4907 }
4908 }
Paul Menage817929e2007-10-18 23:39:36 -07004909}
Tejun Heo5edee612012-10-16 15:03:14 -07004910
Paul Menage817929e2007-10-18 23:39:36 -07004911/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004912 * cgroup_exit - detach cgroup from exiting task
4913 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004914 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004915 *
4916 * Description: Detach cgroup from @tsk and release it.
4917 *
4918 * Note that cgroups marked notify_on_release force every task in
4919 * them to take the global cgroup_mutex mutex when exiting.
4920 * This could impact scaling on very large systems. Be reluctant to
4921 * use notify_on_release cgroups where very high task exit scaling
4922 * is required on large systems.
4923 *
4924 * the_top_cgroup_hack:
4925 *
4926 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4927 *
4928 * We call cgroup_exit() while the task is still competent to
4929 * handle notify_on_release(), then leave the task attached to the
4930 * root cgroup in each hierarchy for the remainder of its exit.
4931 *
4932 * To do this properly, we would increment the reference count on
4933 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4934 * code we would add a second cgroup function call, to drop that
4935 * reference. This would just create an unnecessary hot spot on
4936 * the top_cgroup reference count, to no avail.
4937 *
4938 * Normally, holding a reference to a cgroup without bumping its
4939 * count is unsafe. The cgroup could go away, or someone could
4940 * attach us to a different cgroup, decrementing the count on
4941 * the first cgroup that we never incremented. But in this case,
4942 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004943 * which wards off any cgroup_attach_task() attempts, or task is a failed
4944 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004945 */
4946void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4947{
Paul Menage817929e2007-10-18 23:39:36 -07004948 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004949 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004950
4951 /*
4952 * Unlink from the css_set task list if necessary.
4953 * Optimistically check cg_list before taking
4954 * css_set_lock
4955 */
4956 if (!list_empty(&tsk->cg_list)) {
4957 write_lock(&css_set_lock);
4958 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004959 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004960 write_unlock(&css_set_lock);
4961 }
4962
Paul Menageb4f48b62007-10-18 23:39:33 -07004963 /* Reassign the task to the init_css_set. */
4964 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004965 cg = tsk->cgroups;
4966 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004967
4968 if (run_callbacks && need_forkexit_callback) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004969 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004970 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004971
4972 /* modular subsystems can't use callbacks */
4973 if (!ss || ss->module)
4974 continue;
4975
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004976 if (ss->exit) {
4977 struct cgroup *old_cgrp =
4978 rcu_dereference_raw(cg->subsys[i])->cgroup;
4979 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef52012-01-31 13:47:36 +08004980 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004981 }
4982 }
4983 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004984 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004985
Paul Menage817929e2007-10-18 23:39:36 -07004986 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004987 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004988}
Paul Menage697f4162007-10-18 23:39:34 -07004989
4990/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004991 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004992 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004993 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004994 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004995 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4996 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004997 *
4998 * If we are sending in dummytop, then presumably we are creating
4999 * the top cgroup in the subsystem.
5000 *
5001 * Called only by the ns (nsproxy) cgroup.
5002 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07005003int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07005004{
5005 int ret;
5006 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07005007
Paul Menagebd89aab2007-10-18 23:40:44 -07005008 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07005009 return 1;
5010
Paul Menage7717f7b2009-09-23 15:56:22 -07005011 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005012 while (cgrp != target && cgrp!= cgrp->top_cgroup)
5013 cgrp = cgrp->parent;
5014 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07005015 return ret;
5016}
Paul Menage81a6a5c2007-10-18 23:39:38 -07005017
Paul Menagebd89aab2007-10-18 23:40:44 -07005018static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005019{
5020 /* All of these checks rely on RCU to keep the cgroup
5021 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07005022 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5023 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07005024 /* Control Group is currently removeable. If it's not
5025 * already queued for a userspace notification, queue
5026 * it now */
5027 int need_schedule_work = 0;
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005028 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005029 if (!cgroup_is_removed(cgrp) &&
5030 list_empty(&cgrp->release_list)) {
5031 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005032 need_schedule_work = 1;
5033 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005034 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005035 if (need_schedule_work)
5036 schedule_work(&release_agent_work);
5037 }
5038}
5039
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005040/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005041bool __css_tryget(struct cgroup_subsys_state *css)
5042{
Tejun Heoe9316082012-11-05 09:16:58 -08005043 while (true) {
5044 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005045
Tejun Heoe9316082012-11-05 09:16:58 -08005046 v = css_refcnt(css);
5047 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5048 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005049 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005050 else if (t < 0)
5051 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005052 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005053 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005054}
5055EXPORT_SYMBOL_GPL(__css_tryget);
5056
5057/* Caller must verify that the css is not for root cgroup */
5058void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005059{
Paul Menagebd89aab2007-10-18 23:40:44 -07005060 struct cgroup *cgrp = css->cgroup;
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005061 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005062
Paul Menage81a6a5c2007-10-18 23:39:38 -07005063 rcu_read_lock();
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005064 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5065
5066 switch (v) {
Tejun Heo48ddbe12012-04-01 12:09:56 -07005067 case 1:
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005068 if (notify_on_release(cgrp)) {
5069 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5070 check_for_release(cgrp);
5071 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07005072 break;
5073 case 0:
Tejun Heoed9577932012-11-05 09:16:58 -08005074 schedule_work(&css->dput_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005075 break;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005076 }
5077 rcu_read_unlock();
5078}
Ben Blum67523c42010-03-10 15:22:11 -08005079EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005080
5081/*
5082 * Notify userspace when a cgroup is released, by running the
5083 * configured release agent with the name of the cgroup (path
5084 * relative to the root of cgroup file system) as the argument.
5085 *
5086 * Most likely, this user command will try to rmdir this cgroup.
5087 *
5088 * This races with the possibility that some other task will be
5089 * attached to this cgroup before it is removed, or that some other
5090 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5091 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5092 * unused, and this cgroup will be reprieved from its death sentence,
5093 * to continue to serve a useful existence. Next time it's released,
5094 * we will get notified again, if it still has 'notify_on_release' set.
5095 *
5096 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5097 * means only wait until the task is successfully execve()'d. The
5098 * separate release agent task is forked by call_usermodehelper(),
5099 * then control in this thread returns here, without waiting for the
5100 * release agent task. We don't bother to wait because the caller of
5101 * this routine has no use for the exit status of the release agent
5102 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005103 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005104static void cgroup_release_agent(struct work_struct *work)
5105{
5106 BUG_ON(work != &release_agent_work);
5107 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005108 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005109 while (!list_empty(&release_list)) {
5110 char *argv[3], *envp[3];
5111 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07005112 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005113 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005114 struct cgroup,
5115 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005116 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005117 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005118 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07005119 if (!pathbuf)
5120 goto continue_free;
5121 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5122 goto continue_free;
5123 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5124 if (!agentbuf)
5125 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005126
5127 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07005128 argv[i++] = agentbuf;
5129 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005130 argv[i] = NULL;
5131
5132 i = 0;
5133 /* minimal command environment */
5134 envp[i++] = "HOME=/";
5135 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5136 envp[i] = NULL;
5137
5138 /* Drop the lock while we invoke the usermode helper,
5139 * since the exec could involve hitting disk and hence
5140 * be a slow process */
5141 mutex_unlock(&cgroup_mutex);
5142 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005143 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07005144 continue_free:
5145 kfree(pathbuf);
5146 kfree(agentbuf);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005147 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005148 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005149 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005150 mutex_unlock(&cgroup_mutex);
5151}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005152
5153static int __init cgroup_disable(char *str)
5154{
5155 int i;
5156 char *token;
5157
5158 while ((token = strsep(&str, ",")) != NULL) {
5159 if (!*token)
5160 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005161 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005162 struct cgroup_subsys *ss = subsys[i];
5163
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005164 /*
5165 * cgroup_disable, being at boot time, can't
5166 * know about module subsystems, so we don't
5167 * worry about them.
5168 */
5169 if (!ss || ss->module)
5170 continue;
5171
Paul Menage8bab8dd2008-04-04 14:29:57 -07005172 if (!strcmp(token, ss->name)) {
5173 ss->disabled = 1;
5174 printk(KERN_INFO "Disabling %s control group"
5175 " subsystem\n", ss->name);
5176 break;
5177 }
5178 }
5179 }
5180 return 1;
5181}
5182__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005183
5184/*
5185 * Functons for CSS ID.
5186 */
5187
5188/*
5189 *To get ID other than 0, this should be called when !cgroup_is_removed().
5190 */
5191unsigned short css_id(struct cgroup_subsys_state *css)
5192{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005193 struct css_id *cssid;
5194
5195 /*
5196 * This css_id() can return correct value when somone has refcnt
5197 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5198 * it's unchanged until freed.
5199 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005200 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005201
5202 if (cssid)
5203 return cssid->id;
5204 return 0;
5205}
Ben Blum67523c42010-03-10 15:22:11 -08005206EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005207
5208unsigned short css_depth(struct cgroup_subsys_state *css)
5209{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005210 struct css_id *cssid;
5211
Tejun Heo28b4c272012-04-01 12:09:56 -07005212 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005213
5214 if (cssid)
5215 return cssid->depth;
5216 return 0;
5217}
Ben Blum67523c42010-03-10 15:22:11 -08005218EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005219
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005220/**
5221 * css_is_ancestor - test "root" css is an ancestor of "child"
5222 * @child: the css to be tested.
5223 * @root: the css supporsed to be an ancestor of the child.
5224 *
5225 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005226 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005227 * But, considering usual usage, the csses should be valid objects after test.
5228 * Assuming that the caller will do some action to the child if this returns
5229 * returns true, the caller must take "child";s reference count.
5230 * If "child" is valid object and this returns true, "root" is valid, too.
5231 */
5232
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005233bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005234 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005235{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005236 struct css_id *child_id;
5237 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005238
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005239 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005240 if (!child_id)
5241 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005242 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005243 if (!root_id)
5244 return false;
5245 if (child_id->depth < root_id->depth)
5246 return false;
5247 if (child_id->stack[root_id->depth] != root_id->id)
5248 return false;
5249 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005250}
5251
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005252void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5253{
5254 struct css_id *id = css->id;
5255 /* When this is called before css_id initialization, id can be NULL */
5256 if (!id)
5257 return;
5258
5259 BUG_ON(!ss->use_id);
5260
5261 rcu_assign_pointer(id->css, NULL);
5262 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005263 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005264 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005265 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005266 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005267}
Ben Blum67523c42010-03-10 15:22:11 -08005268EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005269
5270/*
5271 * This is called by init or create(). Then, calls to this function are
5272 * always serialized (By cgroup_mutex() at create()).
5273 */
5274
5275static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5276{
5277 struct css_id *newid;
5278 int myid, error, size;
5279
5280 BUG_ON(!ss->use_id);
5281
5282 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5283 newid = kzalloc(size, GFP_KERNEL);
5284 if (!newid)
5285 return ERR_PTR(-ENOMEM);
5286 /* get id */
5287 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
5288 error = -ENOMEM;
5289 goto err_out;
5290 }
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005291 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005292 /* Don't use 0. allocates an ID of 1-65535 */
5293 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005294 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005295
5296 /* Returns error when there are no free spaces for new ID.*/
5297 if (error) {
5298 error = -ENOSPC;
5299 goto err_out;
5300 }
5301 if (myid > CSS_ID_MAX)
5302 goto remove_idr;
5303
5304 newid->id = myid;
5305 newid->depth = depth;
5306 return newid;
5307remove_idr:
5308 error = -ENOSPC;
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005309 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005310 idr_remove(&ss->idr, myid);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005311 spin_unlock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005312err_out:
5313 kfree(newid);
5314 return ERR_PTR(error);
5315
5316}
5317
Ben Blume6a11052010-03-10 15:22:09 -08005318static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5319 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005320{
5321 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005322
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005323 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005324 idr_init(&ss->idr);
5325
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005326 newid = get_new_cssid(ss, 0);
5327 if (IS_ERR(newid))
5328 return PTR_ERR(newid);
5329
5330 newid->stack[0] = newid->id;
5331 newid->css = rootcss;
5332 rootcss->id = newid;
5333 return 0;
5334}
5335
5336static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5337 struct cgroup *child)
5338{
5339 int subsys_id, i, depth = 0;
5340 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005341 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005342
5343 subsys_id = ss->subsys_id;
5344 parent_css = parent->subsys[subsys_id];
5345 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005346 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005347 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005348
5349 child_id = get_new_cssid(ss, depth);
5350 if (IS_ERR(child_id))
5351 return PTR_ERR(child_id);
5352
5353 for (i = 0; i < depth; i++)
5354 child_id->stack[i] = parent_id->stack[i];
5355 child_id->stack[depth] = child_id->id;
5356 /*
5357 * child_id->css pointer will be set after this cgroup is available
5358 * see cgroup_populate_dir()
5359 */
5360 rcu_assign_pointer(child_css->id, child_id);
5361
5362 return 0;
5363}
5364
5365/**
5366 * css_lookup - lookup css by id
5367 * @ss: cgroup subsys to be looked into.
5368 * @id: the id
5369 *
5370 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5371 * NULL if not. Should be called under rcu_read_lock()
5372 */
5373struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5374{
5375 struct css_id *cssid = NULL;
5376
5377 BUG_ON(!ss->use_id);
5378 cssid = idr_find(&ss->idr, id);
5379
5380 if (unlikely(!cssid))
5381 return NULL;
5382
5383 return rcu_dereference(cssid->css);
5384}
Ben Blum67523c42010-03-10 15:22:11 -08005385EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005386
5387/**
5388 * css_get_next - lookup next cgroup under specified hierarchy.
5389 * @ss: pointer to subsystem
5390 * @id: current position of iteration.
5391 * @root: pointer to css. search tree under this.
5392 * @foundid: position of found object.
5393 *
5394 * Search next css under the specified hierarchy of rootid. Calling under
5395 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5396 */
5397struct cgroup_subsys_state *
5398css_get_next(struct cgroup_subsys *ss, int id,
5399 struct cgroup_subsys_state *root, int *foundid)
5400{
5401 struct cgroup_subsys_state *ret = NULL;
5402 struct css_id *tmp;
5403 int tmpid;
5404 int rootid = css_id(root);
5405 int depth = css_depth(root);
5406
5407 if (!rootid)
5408 return NULL;
5409
5410 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005411 WARN_ON_ONCE(!rcu_read_lock_held());
5412
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005413 /* fill start point for scan */
5414 tmpid = id;
5415 while (1) {
5416 /*
5417 * scan next entry from bitmap(tree), tmpid is updated after
5418 * idr_get_next().
5419 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005420 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005421 if (!tmp)
5422 break;
5423 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5424 ret = rcu_dereference(tmp->css);
5425 if (ret) {
5426 *foundid = tmpid;
5427 break;
5428 }
5429 }
5430 /* continue to scan from next id */
5431 tmpid = tmpid + 1;
5432 }
5433 return ret;
5434}
5435
Stephane Eraniane5d13672011-02-14 11:20:01 +02005436/*
5437 * get corresponding css from file open on cgroupfs directory
5438 */
5439struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5440{
5441 struct cgroup *cgrp;
5442 struct inode *inode;
5443 struct cgroup_subsys_state *css;
5444
5445 inode = f->f_dentry->d_inode;
5446 /* check in cgroup filesystem dir */
5447 if (inode->i_op != &cgroup_dir_inode_operations)
5448 return ERR_PTR(-EBADF);
5449
5450 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5451 return ERR_PTR(-EINVAL);
5452
5453 /* get cgroup */
5454 cgrp = __d_cgrp(f->f_dentry);
5455 css = cgrp->subsys[id];
5456 return css ? css : ERR_PTR(-ENOENT);
5457}
5458
Paul Menagefe693432009-09-23 15:56:20 -07005459#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005460static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005461{
5462 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5463
5464 if (!css)
5465 return ERR_PTR(-ENOMEM);
5466
5467 return css;
5468}
5469
Tejun Heo92fb9742012-11-19 08:13:38 -08005470static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005471{
5472 kfree(cont->subsys[debug_subsys_id]);
5473}
5474
5475static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5476{
5477 return atomic_read(&cont->count);
5478}
5479
5480static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5481{
5482 return cgroup_task_count(cont);
5483}
5484
5485static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5486{
5487 return (u64)(unsigned long)current->cgroups;
5488}
5489
5490static u64 current_css_set_refcount_read(struct cgroup *cont,
5491 struct cftype *cft)
5492{
5493 u64 count;
5494
5495 rcu_read_lock();
5496 count = atomic_read(&current->cgroups->refcount);
5497 rcu_read_unlock();
5498 return count;
5499}
5500
Paul Menage7717f7b2009-09-23 15:56:22 -07005501static int current_css_set_cg_links_read(struct cgroup *cont,
5502 struct cftype *cft,
5503 struct seq_file *seq)
5504{
5505 struct cg_cgroup_link *link;
5506 struct css_set *cg;
5507
5508 read_lock(&css_set_lock);
5509 rcu_read_lock();
5510 cg = rcu_dereference(current->cgroups);
5511 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5512 struct cgroup *c = link->cgrp;
5513 const char *name;
5514
5515 if (c->dentry)
5516 name = c->dentry->d_name.name;
5517 else
5518 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005519 seq_printf(seq, "Root %d group %s\n",
5520 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005521 }
5522 rcu_read_unlock();
5523 read_unlock(&css_set_lock);
5524 return 0;
5525}
5526
5527#define MAX_TASKS_SHOWN_PER_CSS 25
5528static int cgroup_css_links_read(struct cgroup *cont,
5529 struct cftype *cft,
5530 struct seq_file *seq)
5531{
5532 struct cg_cgroup_link *link;
5533
5534 read_lock(&css_set_lock);
5535 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5536 struct css_set *cg = link->cg;
5537 struct task_struct *task;
5538 int count = 0;
5539 seq_printf(seq, "css_set %p\n", cg);
5540 list_for_each_entry(task, &cg->tasks, cg_list) {
5541 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5542 seq_puts(seq, " ...\n");
5543 break;
5544 } else {
5545 seq_printf(seq, " task %d\n",
5546 task_pid_vnr(task));
5547 }
5548 }
5549 }
5550 read_unlock(&css_set_lock);
5551 return 0;
5552}
5553
Paul Menagefe693432009-09-23 15:56:20 -07005554static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5555{
5556 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5557}
5558
5559static struct cftype debug_files[] = {
5560 {
5561 .name = "cgroup_refcount",
5562 .read_u64 = cgroup_refcount_read,
5563 },
5564 {
5565 .name = "taskcount",
5566 .read_u64 = debug_taskcount_read,
5567 },
5568
5569 {
5570 .name = "current_css_set",
5571 .read_u64 = current_css_set_read,
5572 },
5573
5574 {
5575 .name = "current_css_set_refcount",
5576 .read_u64 = current_css_set_refcount_read,
5577 },
5578
5579 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005580 .name = "current_css_set_cg_links",
5581 .read_seq_string = current_css_set_cg_links_read,
5582 },
5583
5584 {
5585 .name = "cgroup_css_links",
5586 .read_seq_string = cgroup_css_links_read,
5587 },
5588
5589 {
Paul Menagefe693432009-09-23 15:56:20 -07005590 .name = "releasable",
5591 .read_u64 = releasable_read,
5592 },
Paul Menagefe693432009-09-23 15:56:20 -07005593
Tejun Heo4baf6e32012-04-01 12:09:55 -07005594 { } /* terminate */
5595};
Paul Menagefe693432009-09-23 15:56:20 -07005596
5597struct cgroup_subsys debug_subsys = {
5598 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005599 .css_alloc = debug_css_alloc,
5600 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005601 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005602 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005603};
5604#endif /* CONFIG_CGROUP_DEBUG */