blob: df6814706cca03d2af1ac68bf453aece8f4bf8da [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heo28b4c272012-04-01 12:09:56 -070066/* css deactivation bias, makes css->refcnt negative to deny new trygets */
67#define CSS_DEACT_BIAS INT_MIN
68
Tejun Heoe25e2cb2011-12-12 18:12:21 -080069/*
70 * cgroup_mutex is the master lock. Any modification to cgroup or its
71 * hierarchy must be performed while holding it.
72 *
73 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
74 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
75 * release_agent_path and so on. Modifying requires both cgroup_mutex and
76 * cgroup_root_mutex. Readers can acquire either of the two. This is to
77 * break the following locking order cycle.
78 *
79 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
80 * B. namespace_sem -> cgroup_mutex
81 *
82 * B happens only through cgroup_show_options() and using cgroup_root_mutex
83 * breaks it.
84 */
Tejun Heo22194492013-04-07 09:29:51 -070085#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
87EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
88#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070089static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070090#endif
91
Tejun Heoe25e2cb2011-12-12 18:12:21 -080092static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070093
Ben Blumaae8aab2010-03-10 15:22:07 -080094/*
95 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020096 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080097 * registered after that. The mutable section of this array is protected by
98 * cgroup_mutex.
99 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200100#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200101#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -0800102static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103#include <linux/cgroup_subsys.h>
104};
105
Paul Menageddbcc7e2007-10-18 23:39:30 -0700106/*
107 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
108 * subsystems that are otherwise unattached - it never has more than a
109 * single cgroup, and all tasks are part of that cgroup.
110 */
111static struct cgroupfs_root rootnode;
112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
189static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700190static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Paul Menageddbcc7e2007-10-18 23:39:30 -0700199/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
200#define dummytop (&rootnode.top_cgroup)
201
Li Zefan65dff752013-03-01 15:01:56 +0800202static struct cgroup_name root_cgroup_name = { .name = "/" };
203
Paul Menageddbcc7e2007-10-18 23:39:30 -0700204/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800205 * check for fork/exit handlers to call. This avoids us having to do
206 * extra work in the fork/exit path if none of the subsystems need to
207 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700208 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700209static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210
Tejun Heoea15f8c2013-06-13 19:27:42 -0700211static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800212static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800213static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
214 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800215
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700216static int css_unbias_refcnt(int refcnt)
217{
218 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
219}
220
Tejun Heo28b4c272012-04-01 12:09:56 -0700221/* the current nr of refs, always >= 0 whether @css is deactivated or not */
222static int css_refcnt(struct cgroup_subsys_state *css)
223{
224 int v = atomic_read(&css->refcnt);
225
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700226 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700227}
228
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700230static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700231{
Tejun Heo54766d42013-06-12 21:04:53 -0700232 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700233}
234
Li Zefan78574cf2013-04-08 19:00:38 -0700235/**
236 * cgroup_is_descendant - test ancestry
237 * @cgrp: the cgroup to be tested
238 * @ancestor: possible ancestor of @cgrp
239 *
240 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
241 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
242 * and @ancestor are accessible.
243 */
244bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
245{
246 while (cgrp) {
247 if (cgrp == ancestor)
248 return true;
249 cgrp = cgrp->parent;
250 }
251 return false;
252}
253EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700254
Adrian Bunke9685a02008-02-07 00:13:46 -0800255static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700256{
257 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700258 (1 << CGRP_RELEASABLE) |
259 (1 << CGRP_NOTIFY_ON_RELEASE);
260 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700261}
262
Adrian Bunke9685a02008-02-07 00:13:46 -0800263static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700264{
Paul Menagebd89aab2007-10-18 23:40:44 -0700265 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700266}
267
Paul Menageddbcc7e2007-10-18 23:39:30 -0700268/*
269 * for_each_subsys() allows you to iterate on each subsystem attached to
270 * an active hierarchy
271 */
272#define for_each_subsys(_root, _ss) \
273list_for_each_entry(_ss, &_root->subsys_list, sibling)
274
Li Zefane5f6a862009-01-07 18:07:41 -0800275/* for_each_active_root() allows you to iterate across the active hierarchies */
276#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700277list_for_each_entry(_root, &roots, root_list)
278
Tejun Heof6ea9372012-04-01 12:09:55 -0700279static inline struct cgroup *__d_cgrp(struct dentry *dentry)
280{
281 return dentry->d_fsdata;
282}
283
Tejun Heo05ef1d72012-04-01 12:09:56 -0700284static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700285{
286 return dentry->d_fsdata;
287}
288
Tejun Heo05ef1d72012-04-01 12:09:56 -0700289static inline struct cftype *__d_cft(struct dentry *dentry)
290{
291 return __d_cfe(dentry)->type;
292}
293
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700294/**
295 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
296 * @cgrp: the cgroup to be checked for liveness
297 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700298 * On success, returns true; the mutex should be later unlocked. On
299 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700300 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700301static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700302{
303 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700304 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700305 mutex_unlock(&cgroup_mutex);
306 return false;
307 }
308 return true;
309}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700310
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311/* the list of cgroups eligible for automatic release. Protected by
312 * release_list_lock */
313static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200314static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700315static void cgroup_release_agent(struct work_struct *work);
316static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700317static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700318
Tejun Heo69d02062013-06-12 21:04:50 -0700319/*
320 * A cgroup can be associated with multiple css_sets as different tasks may
321 * belong to different cgroups on different hierarchies. In the other
322 * direction, a css_set is naturally associated with multiple cgroups.
323 * This M:N relationship is represented by the following link structure
324 * which exists for each association and allows traversing the associations
325 * from both sides.
326 */
327struct cgrp_cset_link {
328 /* the cgroup and css_set this link associates */
329 struct cgroup *cgrp;
330 struct css_set *cset;
331
332 /* list of cgrp_cset_links anchored at cgrp->cset_links */
333 struct list_head cset_link;
334
335 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
336 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700337};
338
339/* The default css_set - used by init and its children prior to any
340 * hierarchies being mounted. It contains a pointer to the root state
341 * for each subsystem. Also used to anchor the list of css_sets. Not
342 * reference-counted, to improve performance when child cgroups
343 * haven't been created.
344 */
345
346static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700347static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700348
Ben Blume6a11052010-03-10 15:22:09 -0800349static int cgroup_init_idr(struct cgroup_subsys *ss,
350 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700351
Paul Menage817929e2007-10-18 23:39:36 -0700352/* css_set_lock protects the list of css_set objects, and the
353 * chain of tasks off each css_set. Nests outside task->alloc_lock
354 * due to cgroup_iter_start() */
355static DEFINE_RWLOCK(css_set_lock);
356static int css_set_count;
357
Paul Menage7717f7b2009-09-23 15:56:22 -0700358/*
359 * hash table for cgroup groups. This improves the performance to find
360 * an existing css_set. This hash doesn't (currently) take into
361 * account cgroups in empty hierarchies.
362 */
Li Zefan472b1052008-04-29 01:00:11 -0700363#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800364static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700365
Li Zefan0ac801f2013-01-10 11:49:27 +0800366static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700367{
368 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800369 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700370
371 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800372 key += (unsigned long)css[i];
373 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700374
Li Zefan0ac801f2013-01-10 11:49:27 +0800375 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700376}
377
Paul Menage817929e2007-10-18 23:39:36 -0700378/* We don't maintain the lists running through each css_set to its
379 * task until after the first call to cgroup_iter_start(). This
380 * reduces the fork()/exit() overhead for people who have cgroups
381 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700382static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700383
Tejun Heo5abb8852013-06-12 21:04:49 -0700384static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700385{
Tejun Heo69d02062013-06-12 21:04:50 -0700386 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700387
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700388 /*
389 * Ensure that the refcount doesn't hit zero while any readers
390 * can see it. Similar to atomic_dec_and_lock(), but for an
391 * rwlock
392 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700393 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700394 return;
395 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700396 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700397 write_unlock(&css_set_lock);
398 return;
399 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700400
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700401 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700402 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700403 css_set_count--;
404
Tejun Heo69d02062013-06-12 21:04:50 -0700405 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700406 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700407
Tejun Heo69d02062013-06-12 21:04:50 -0700408 list_del(&link->cset_link);
409 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800410
Tejun Heoddd69142013-06-12 21:04:54 -0700411 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700412 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700413 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700414 set_bit(CGRP_RELEASABLE, &cgrp->flags);
415 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700416 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700417
418 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420
421 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700422 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700423}
424
425/*
426 * refcounted get/put for css_set objects
427 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700428static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700429{
Tejun Heo5abb8852013-06-12 21:04:49 -0700430 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700431}
432
Tejun Heo5abb8852013-06-12 21:04:49 -0700433static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700434{
Tejun Heo5abb8852013-06-12 21:04:49 -0700435 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700436}
437
Tejun Heo5abb8852013-06-12 21:04:49 -0700438static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700439{
Tejun Heo5abb8852013-06-12 21:04:49 -0700440 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700441}
442
Paul Menage817929e2007-10-18 23:39:36 -0700443/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700444 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700445 * @cset: candidate css_set being tested
446 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700447 * @new_cgrp: cgroup that's being entered by the task
448 * @template: desired set of css pointers in css_set (pre-calculated)
449 *
450 * Returns true if "cg" matches "old_cg" except for the hierarchy
451 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
452 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700453static bool compare_css_sets(struct css_set *cset,
454 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700455 struct cgroup *new_cgrp,
456 struct cgroup_subsys_state *template[])
457{
458 struct list_head *l1, *l2;
459
Tejun Heo5abb8852013-06-12 21:04:49 -0700460 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700461 /* Not all subsystems matched */
462 return false;
463 }
464
465 /*
466 * Compare cgroup pointers in order to distinguish between
467 * different cgroups in heirarchies with no subsystems. We
468 * could get by with just this check alone (and skip the
469 * memcmp above) but on most setups the memcmp check will
470 * avoid the need for this more expensive check on almost all
471 * candidates.
472 */
473
Tejun Heo69d02062013-06-12 21:04:50 -0700474 l1 = &cset->cgrp_links;
475 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700476 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700477 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700478 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700479
480 l1 = l1->next;
481 l2 = l2->next;
482 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700483 if (l1 == &cset->cgrp_links) {
484 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700485 break;
486 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700487 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700488 }
489 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700490 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
491 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
492 cgrp1 = link1->cgrp;
493 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700494 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700495 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700496
497 /*
498 * If this hierarchy is the hierarchy of the cgroup
499 * that's changing, then we need to check that this
500 * css_set points to the new cgroup; if it's any other
501 * hierarchy, then this css_set should point to the
502 * same cgroup as the old css_set.
503 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700504 if (cgrp1->root == new_cgrp->root) {
505 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700506 return false;
507 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700508 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700509 return false;
510 }
511 }
512 return true;
513}
514
515/*
Paul Menage817929e2007-10-18 23:39:36 -0700516 * find_existing_css_set() is a helper for
517 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700518 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700519 *
520 * oldcg: the cgroup group that we're using before the cgroup
521 * transition
522 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700523 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700524 *
525 * template: location in which to build the desired set of subsystem
526 * state objects for the new cgroup group
527 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700528static struct css_set *find_existing_css_set(struct css_set *old_cset,
529 struct cgroup *cgrp,
530 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700531{
532 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700533 struct cgroupfs_root *root = cgrp->root;
Tejun Heo5abb8852013-06-12 21:04:49 -0700534 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800535 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700536
Ben Blumaae8aab2010-03-10 15:22:07 -0800537 /*
538 * Build the set of subsystem state objects that we want to see in the
539 * new css_set. while subsystems can change globally, the entries here
540 * won't change, so no need for locking.
541 */
Paul Menage817929e2007-10-18 23:39:36 -0700542 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400543 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700544 /* Subsystem is in this hierarchy. So we want
545 * the subsystem state from the new
546 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700547 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700548 } else {
549 /* Subsystem is not in this hierarchy, so we
550 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700551 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700552 }
553 }
554
Li Zefan0ac801f2013-01-10 11:49:27 +0800555 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700556 hash_for_each_possible(css_set_table, cset, hlist, key) {
557 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700558 continue;
559
560 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700561 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700562 }
Paul Menage817929e2007-10-18 23:39:36 -0700563
564 /* No existing cgroup group matched */
565 return NULL;
566}
567
Tejun Heo69d02062013-06-12 21:04:50 -0700568static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700569{
Tejun Heo69d02062013-06-12 21:04:50 -0700570 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700571
Tejun Heo69d02062013-06-12 21:04:50 -0700572 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
573 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700574 kfree(link);
575 }
576}
577
Tejun Heo69d02062013-06-12 21:04:50 -0700578/**
579 * allocate_cgrp_cset_links - allocate cgrp_cset_links
580 * @count: the number of links to allocate
581 * @tmp_links: list_head the allocated links are put on
582 *
583 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
584 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700585 */
Tejun Heo69d02062013-06-12 21:04:50 -0700586static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700587{
Tejun Heo69d02062013-06-12 21:04:50 -0700588 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700589 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700590
591 INIT_LIST_HEAD(tmp_links);
592
Li Zefan36553432008-07-29 22:33:19 -0700593 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700594 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700595 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700596 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700597 return -ENOMEM;
598 }
Tejun Heo69d02062013-06-12 21:04:50 -0700599 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700600 }
601 return 0;
602}
603
Li Zefanc12f65d2009-01-07 18:07:42 -0800604/**
605 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700606 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700607 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800608 * @cgrp: the destination cgroup
609 */
Tejun Heo69d02062013-06-12 21:04:50 -0700610static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
611 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800612{
Tejun Heo69d02062013-06-12 21:04:50 -0700613 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800614
Tejun Heo69d02062013-06-12 21:04:50 -0700615 BUG_ON(list_empty(tmp_links));
616 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
617 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700618 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700619 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700620 /*
621 * Always add links to the tail of the list so that the list
622 * is sorted by order of hierarchy creation
623 */
Tejun Heo69d02062013-06-12 21:04:50 -0700624 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800625}
626
Li Zefan36553432008-07-29 22:33:19 -0700627/*
Paul Menage817929e2007-10-18 23:39:36 -0700628 * find_css_set() takes an existing cgroup group and a
629 * cgroup object, and returns a css_set object that's
630 * equivalent to the old group, but with the given cgroup
631 * substituted into the appropriate hierarchy. Must be called with
632 * cgroup_mutex held
633 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700634static struct css_set *find_css_set(struct css_set *old_cset,
635 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700636{
Tejun Heo5abb8852013-06-12 21:04:49 -0700637 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -0700638 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Tejun Heo69d02062013-06-12 21:04:50 -0700639 struct list_head tmp_links;
640 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800641 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700642
Paul Menage817929e2007-10-18 23:39:36 -0700643 /* First see if we already have a cgroup group that matches
644 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700645 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700646 cset = find_existing_css_set(old_cset, cgrp, template);
647 if (cset)
648 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700649 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700650
Tejun Heo5abb8852013-06-12 21:04:49 -0700651 if (cset)
652 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700653
Tejun Heof4f4be22013-06-12 21:04:51 -0700654 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700655 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700656 return NULL;
657
Tejun Heo69d02062013-06-12 21:04:50 -0700658 /* Allocate all the cgrp_cset_link objects that we'll need */
659 if (allocate_cgrp_cset_links(root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700660 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700661 return NULL;
662 }
663
Tejun Heo5abb8852013-06-12 21:04:49 -0700664 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700665 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700666 INIT_LIST_HEAD(&cset->tasks);
667 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700668
669 /* Copy the set of subsystem state objects generated in
670 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700671 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700672
673 write_lock(&css_set_lock);
674 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700675 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700676 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700677
Paul Menage7717f7b2009-09-23 15:56:22 -0700678 if (c->root == cgrp->root)
679 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700680 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700681 }
Paul Menage817929e2007-10-18 23:39:36 -0700682
Tejun Heo69d02062013-06-12 21:04:50 -0700683 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700684
Paul Menage817929e2007-10-18 23:39:36 -0700685 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700686
687 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 key = css_set_hash(cset->subsys);
689 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700690
Paul Menage817929e2007-10-18 23:39:36 -0700691 write_unlock(&css_set_lock);
692
Tejun Heo5abb8852013-06-12 21:04:49 -0700693 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700694}
695
Paul Menageddbcc7e2007-10-18 23:39:30 -0700696/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700697 * Return the cgroup for "task" from the given hierarchy. Must be
698 * called with cgroup_mutex held.
699 */
700static struct cgroup *task_cgroup_from_root(struct task_struct *task,
701 struct cgroupfs_root *root)
702{
Tejun Heo5abb8852013-06-12 21:04:49 -0700703 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700704 struct cgroup *res = NULL;
705
706 BUG_ON(!mutex_is_locked(&cgroup_mutex));
707 read_lock(&css_set_lock);
708 /*
709 * No need to lock the task - since we hold cgroup_mutex the
710 * task can't change groups, so the only thing that can happen
711 * is that it exits and its css is set back to init_css_set.
712 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700713 cset = task->cgroups;
714 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700715 res = &root->top_cgroup;
716 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700717 struct cgrp_cset_link *link;
718
719 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700720 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700721
Paul Menage7717f7b2009-09-23 15:56:22 -0700722 if (c->root == root) {
723 res = c;
724 break;
725 }
726 }
727 }
728 read_unlock(&css_set_lock);
729 BUG_ON(!res);
730 return res;
731}
732
733/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700734 * There is one global cgroup mutex. We also require taking
735 * task_lock() when dereferencing a task's cgroup subsys pointers.
736 * See "The task_lock() exception", at the end of this comment.
737 *
738 * A task must hold cgroup_mutex to modify cgroups.
739 *
740 * Any task can increment and decrement the count field without lock.
741 * So in general, code holding cgroup_mutex can't rely on the count
742 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800743 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700744 * means that no tasks are currently attached, therefore there is no
745 * way a task attached to that cgroup can fork (the other way to
746 * increment the count). So code holding cgroup_mutex can safely
747 * assume that if the count is zero, it will stay zero. Similarly, if
748 * a task holds cgroup_mutex on a cgroup with zero count, it
749 * knows that the cgroup won't be removed, as cgroup_rmdir()
750 * needs that mutex.
751 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700752 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
753 * (usually) take cgroup_mutex. These are the two most performance
754 * critical pieces of code here. The exception occurs on cgroup_exit(),
755 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
756 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800757 * to the release agent with the name of the cgroup (path relative to
758 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700759 *
760 * A cgroup can only be deleted if both its 'count' of using tasks
761 * is zero, and its list of 'children' cgroups is empty. Since all
762 * tasks in the system use _some_ cgroup, and since there is always at
763 * least one task in the system (init, pid == 1), therefore, top_cgroup
764 * always has either children cgroups and/or using tasks. So we don't
765 * need a special hack to ensure that top_cgroup cannot be deleted.
766 *
767 * The task_lock() exception
768 *
769 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800770 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800771 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772 * several performance critical places that need to reference
773 * task->cgroup without the expense of grabbing a system global
774 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800775 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
777 * the task_struct routinely used for such matters.
778 *
779 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800780 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781 */
782
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783/*
784 * A couple of forward declarations required, due to cyclic reference loop:
785 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
786 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
787 * -> cgroup_mkdir.
788 */
789
Al Viro18bb1db2011-07-26 01:41:39 -0400790static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400791static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400793static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
794 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700795static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700796static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700797
798static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200799 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700800 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700801};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700802
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700803static int alloc_css_id(struct cgroup_subsys *ss,
804 struct cgroup *parent, struct cgroup *child);
805
Al Viroa5e7ed32011-07-26 01:55:55 -0400806static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807{
808 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809
810 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400811 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100813 inode->i_uid = current_fsuid();
814 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
816 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
817 }
818 return inode;
819}
820
Li Zefan65dff752013-03-01 15:01:56 +0800821static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
822{
823 struct cgroup_name *name;
824
825 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
826 if (!name)
827 return NULL;
828 strcpy(name->name, dentry->d_name.name);
829 return name;
830}
831
Li Zefanbe445622013-01-24 14:31:42 +0800832static void cgroup_free_fn(struct work_struct *work)
833{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700834 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800835 struct cgroup_subsys *ss;
836
837 mutex_lock(&cgroup_mutex);
838 /*
839 * Release the subsystem state objects.
840 */
841 for_each_subsys(cgrp->root, ss)
842 ss->css_free(cgrp);
843
844 cgrp->root->number_of_cgroups--;
845 mutex_unlock(&cgroup_mutex);
846
847 /*
Li Zefan415cf072013-04-08 14:35:02 +0800848 * We get a ref to the parent's dentry, and put the ref when
849 * this cgroup is being freed, so it's guaranteed that the
850 * parent won't be destroyed before its children.
851 */
852 dput(cgrp->parent->dentry);
853
Li Zefancc20e012013-04-26 11:58:02 -0700854 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
855
Li Zefan415cf072013-04-08 14:35:02 +0800856 /*
Li Zefanbe445622013-01-24 14:31:42 +0800857 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700858 * created the cgroup. This will free cgrp->root, if we are
859 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800860 */
861 deactivate_super(cgrp->root->sb);
862
863 /*
864 * if we're getting rid of the cgroup, refcount should ensure
865 * that there are no pidlists left.
866 */
867 BUG_ON(!list_empty(&cgrp->pidlists));
868
869 simple_xattrs_free(&cgrp->xattrs);
870
Li Zefan65dff752013-03-01 15:01:56 +0800871 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800872 kfree(cgrp);
873}
874
875static void cgroup_free_rcu(struct rcu_head *head)
876{
877 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
878
Tejun Heoea15f8c2013-06-13 19:27:42 -0700879 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
880 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800881}
882
Paul Menageddbcc7e2007-10-18 23:39:30 -0700883static void cgroup_diput(struct dentry *dentry, struct inode *inode)
884{
885 /* is dentry a directory ? if so, kfree() associated cgroup */
886 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700887 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800888
Tejun Heo54766d42013-06-12 21:04:53 -0700889 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800890 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700891 } else {
892 struct cfent *cfe = __d_cfe(dentry);
893 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
894
895 WARN_ONCE(!list_empty(&cfe->node) &&
896 cgrp != &cgrp->root->top_cgroup,
897 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700898 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700899 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700900 }
901 iput(inode);
902}
903
Al Viroc72a04e2011-01-14 05:31:45 +0000904static int cgroup_delete(const struct dentry *d)
905{
906 return 1;
907}
908
Paul Menageddbcc7e2007-10-18 23:39:30 -0700909static void remove_dir(struct dentry *d)
910{
911 struct dentry *parent = dget(d->d_parent);
912
913 d_delete(d);
914 simple_rmdir(parent->d_inode, d);
915 dput(parent);
916}
917
Li Zefan2739d3c2013-01-21 18:18:33 +0800918static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700919{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700920 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700921
Tejun Heo05ef1d72012-04-01 12:09:56 -0700922 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
923 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100924
Li Zefan2739d3c2013-01-21 18:18:33 +0800925 /*
926 * If we're doing cleanup due to failure of cgroup_create(),
927 * the corresponding @cfe may not exist.
928 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700929 list_for_each_entry(cfe, &cgrp->files, node) {
930 struct dentry *d = cfe->dentry;
931
932 if (cft && cfe->type != cft)
933 continue;
934
935 dget(d);
936 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700937 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700938 list_del_init(&cfe->node);
939 dput(d);
940
Li Zefan2739d3c2013-01-21 18:18:33 +0800941 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700942 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943}
944
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400945/**
946 * cgroup_clear_directory - selective removal of base and subsystem files
947 * @dir: directory containing the files
948 * @base_files: true if the base files should be removed
949 * @subsys_mask: mask of the subsystem ids whose files should be removed
950 */
951static void cgroup_clear_directory(struct dentry *dir, bool base_files,
952 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700953{
954 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400955 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700956
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400957 for_each_subsys(cgrp->root, ss) {
958 struct cftype_set *set;
959 if (!test_bit(ss->subsys_id, &subsys_mask))
960 continue;
961 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800962 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400963 }
964 if (base_files) {
965 while (!list_empty(&cgrp->files))
966 cgroup_rm_file(cgrp, NULL);
967 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968}
969
970/*
971 * NOTE : the dentry must have been dget()'ed
972 */
973static void cgroup_d_remove_dir(struct dentry *dentry)
974{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100975 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100977
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400978 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100980 parent = dentry->d_parent;
981 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800982 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100984 spin_unlock(&dentry->d_lock);
985 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986 remove_dir(dentry);
987}
988
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700989/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800990 * Call with cgroup_mutex held. Drops reference counts on modules, including
991 * any duplicate ones that parse_cgroupfs_options took. If this function
992 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800993 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400995 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400997 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -0700998 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999 int i;
1000
Ben Blumaae8aab2010-03-10 15:22:07 -08001001 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001002 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001003
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001004 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1005 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 /* Check that any added subsystems are currently free */
1007 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001008 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001010 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001011 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001012 /*
1013 * Nobody should tell us to do a subsys that doesn't exist:
1014 * parse_cgroupfs_options should catch that case and refcounts
1015 * ensure that subsystems won't disappear once selected.
1016 */
1017 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 if (ss->root != &rootnode) {
1019 /* Subsystem isn't free */
1020 return -EBUSY;
1021 }
1022 }
1023
1024 /* Currently we don't handle adding/removing subsystems when
1025 * any child cgroups exist. This is theoretically supportable
1026 * but involves complex error handling, so it's being left until
1027 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001028 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 return -EBUSY;
1030
1031 /* Process each subsystem */
1032 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1033 struct cgroup_subsys *ss = subsys[i];
1034 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001035 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001037 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001038 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 BUG_ON(!dummytop->subsys[i]);
1040 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001041 cgrp->subsys[i] = dummytop->subsys[i];
1042 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001043 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001044 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001046 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001047 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001048 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001050 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001051 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1052 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001054 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001056 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001057 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001058 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001059 /* subsystem is now free - drop reference on module */
1060 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001061 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001063 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001064 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001065 /*
1066 * a refcount was taken, but we already had one, so
1067 * drop the extra reference.
1068 */
1069 module_put(ss->module);
1070#ifdef CONFIG_MODULE_UNLOAD
1071 BUG_ON(ss->module && !module_refcount(ss->module));
1072#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 } else {
1074 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001075 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076 }
1077 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001078 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079
1080 return 0;
1081}
1082
Al Viro34c80b12011-12-08 21:32:45 -05001083static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084{
Al Viro34c80b12011-12-08 21:32:45 -05001085 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086 struct cgroup_subsys *ss;
1087
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001088 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089 for_each_subsys(root, ss)
1090 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001091 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1092 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001093 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001095 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001096 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001097 if (strlen(root->release_agent_path))
1098 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001099 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001100 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001101 if (strlen(root->name))
1102 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001103 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104 return 0;
1105}
1106
1107struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001108 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001110 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001111 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001112 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001113 /* User explicitly requested empty subsystem */
1114 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001115
1116 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001117
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118};
1119
Ben Blumaae8aab2010-03-10 15:22:07 -08001120/*
1121 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001122 * with cgroup_mutex held to protect the subsys[] array. This function takes
1123 * refcounts on subsystems to be used, unless it returns error, in which case
1124 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001125 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001126static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001128 char *token, *o = data;
1129 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001130 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001131 int i;
1132 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001133
Ben Blumaae8aab2010-03-10 15:22:07 -08001134 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1135
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136#ifdef CONFIG_CPUSETS
1137 mask = ~(1UL << cpuset_subsys_id);
1138#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139
Paul Menagec6d57f32009-09-23 15:56:19 -07001140 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141
1142 while ((token = strsep(&o, ",")) != NULL) {
1143 if (!*token)
1144 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001145 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001146 /* Explicitly have no subsystems */
1147 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001148 continue;
1149 }
1150 if (!strcmp(token, "all")) {
1151 /* Mutually exclusive option 'all' + subsystem name */
1152 if (one_ss)
1153 return -EINVAL;
1154 all_ss = true;
1155 continue;
1156 }
Tejun Heo873fe092013-04-14 20:15:26 -07001157 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1158 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1159 continue;
1160 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001162 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 continue;
1164 }
1165 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001166 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001167 continue;
1168 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001169 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001170 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001171 continue;
1172 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001173 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001174 /* Specifying two release agents is forbidden */
1175 if (opts->release_agent)
1176 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001177 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001178 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001179 if (!opts->release_agent)
1180 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001181 continue;
1182 }
1183 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001184 const char *name = token + 5;
1185 /* Can't specify an empty name */
1186 if (!strlen(name))
1187 return -EINVAL;
1188 /* Must match [\w.-]+ */
1189 for (i = 0; i < strlen(name); i++) {
1190 char c = name[i];
1191 if (isalnum(c))
1192 continue;
1193 if ((c == '.') || (c == '-') || (c == '_'))
1194 continue;
1195 return -EINVAL;
1196 }
1197 /* Specifying two names is forbidden */
1198 if (opts->name)
1199 return -EINVAL;
1200 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001201 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001202 GFP_KERNEL);
1203 if (!opts->name)
1204 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001205
1206 continue;
1207 }
1208
1209 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1210 struct cgroup_subsys *ss = subsys[i];
1211 if (ss == NULL)
1212 continue;
1213 if (strcmp(token, ss->name))
1214 continue;
1215 if (ss->disabled)
1216 continue;
1217
1218 /* Mutually exclusive option 'all' + subsystem name */
1219 if (all_ss)
1220 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001221 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001222 one_ss = true;
1223
1224 break;
1225 }
1226 if (i == CGROUP_SUBSYS_COUNT)
1227 return -ENOENT;
1228 }
1229
1230 /*
1231 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001232 * otherwise if 'none', 'name=' and a subsystem name options
1233 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001234 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001235 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001236 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1237 struct cgroup_subsys *ss = subsys[i];
1238 if (ss == NULL)
1239 continue;
1240 if (ss->disabled)
1241 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001242 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243 }
1244 }
1245
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001246 /* Consistency checks */
1247
Tejun Heo873fe092013-04-14 20:15:26 -07001248 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1249 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1250
1251 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1252 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1253 return -EINVAL;
1254 }
1255
1256 if (opts->cpuset_clone_children) {
1257 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1258 return -EINVAL;
1259 }
1260 }
1261
Li Zefanf9ab5b52009-06-17 16:26:33 -07001262 /*
1263 * Option noprefix was introduced just for backward compatibility
1264 * with the old cpuset, so we allow noprefix only if mounting just
1265 * the cpuset subsystem.
1266 */
Tejun Heo93438622013-04-14 20:15:25 -07001267 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001268 return -EINVAL;
1269
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001270
1271 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001272 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001273 return -EINVAL;
1274
1275 /*
1276 * We either have to specify by name or by subsystems. (So all
1277 * empty hierarchies must have a name).
1278 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001279 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001280 return -EINVAL;
1281
Ben Blumcf5d5942010-03-10 15:22:09 -08001282 /*
1283 * Grab references on all the modules we'll need, so the subsystems
1284 * don't dance around before rebind_subsystems attaches them. This may
1285 * take duplicate reference counts on a subsystem that's already used,
1286 * but rebind_subsystems handles this case.
1287 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001288 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001289 unsigned long bit = 1UL << i;
1290
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001291 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001292 continue;
1293 if (!try_module_get(subsys[i]->module)) {
1294 module_pin_failed = true;
1295 break;
1296 }
1297 }
1298 if (module_pin_failed) {
1299 /*
1300 * oops, one of the modules was going away. this means that we
1301 * raced with a module_delete call, and to the user this is
1302 * essentially a "subsystem doesn't exist" case.
1303 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001304 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001305 /* drop refcounts only on the ones we took */
1306 unsigned long bit = 1UL << i;
1307
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001308 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 continue;
1310 module_put(subsys[i]->module);
1311 }
1312 return -ENOENT;
1313 }
1314
Paul Menageddbcc7e2007-10-18 23:39:30 -07001315 return 0;
1316}
1317
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001318static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001319{
1320 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001321 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001322 unsigned long bit = 1UL << i;
1323
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001324 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001325 continue;
1326 module_put(subsys[i]->module);
1327 }
1328}
1329
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1331{
1332 int ret = 0;
1333 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001334 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001336 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001337
Tejun Heo873fe092013-04-14 20:15:26 -07001338 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1339 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1340 return -EINVAL;
1341 }
1342
Paul Menagebd89aab2007-10-18 23:40:44 -07001343 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001345 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346
1347 /* See what subsystems are wanted */
1348 ret = parse_cgroupfs_options(data, &opts);
1349 if (ret)
1350 goto out_unlock;
1351
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001352 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001353 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1354 task_tgid_nr(current), current->comm);
1355
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001356 added_mask = opts.subsys_mask & ~root->subsys_mask;
1357 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001358
Ben Blumcf5d5942010-03-10 15:22:09 -08001359 /* Don't allow flags or name to change at remount */
1360 if (opts.flags != root->flags ||
1361 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001362 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001363 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001364 goto out_unlock;
1365 }
1366
Gao feng7083d032012-12-03 09:28:18 +08001367 /*
1368 * Clear out the files of subsystems that should be removed, do
1369 * this before rebind_subsystems, since rebind_subsystems may
1370 * change this hierarchy's subsys_list.
1371 */
1372 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1373
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001374 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001375 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001376 /* rebind_subsystems failed, re-populate the removed files */
1377 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001378 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001379 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001380 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001382 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001383 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384
Paul Menage81a6a5c2007-10-18 23:39:38 -07001385 if (opts.release_agent)
1386 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001388 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001389 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001390 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001392 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393 return ret;
1394}
1395
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001396static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397 .statfs = simple_statfs,
1398 .drop_inode = generic_delete_inode,
1399 .show_options = cgroup_show_options,
1400 .remount_fs = cgroup_remount,
1401};
1402
Paul Menagecc31edc2008-10-18 20:28:04 -07001403static void init_cgroup_housekeeping(struct cgroup *cgrp)
1404{
1405 INIT_LIST_HEAD(&cgrp->sibling);
1406 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001407 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001408 INIT_LIST_HEAD(&cgrp->cset_links);
Tejun Heo22430762012-11-19 08:13:35 -08001409 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001410 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001411 INIT_LIST_HEAD(&cgrp->pidlists);
1412 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001413 INIT_LIST_HEAD(&cgrp->event_list);
1414 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001415 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001416}
Paul Menagec6d57f32009-09-23 15:56:19 -07001417
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418static void init_cgroup_root(struct cgroupfs_root *root)
1419{
Paul Menagebd89aab2007-10-18 23:40:44 -07001420 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001421
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422 INIT_LIST_HEAD(&root->subsys_list);
1423 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001424 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001425 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001426 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001427 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001428 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001429 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430}
1431
Tejun Heofa3ca072013-04-14 11:36:56 -07001432static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001433{
Tejun Heo1a574232013-04-14 11:36:58 -07001434 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001435
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001436 lockdep_assert_held(&cgroup_mutex);
1437 lockdep_assert_held(&cgroup_root_mutex);
1438
Tejun Heo1a574232013-04-14 11:36:58 -07001439 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
1440 if (id < 0)
1441 return id;
1442
1443 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001444 return 0;
1445}
1446
1447static void cgroup_exit_root_id(struct cgroupfs_root *root)
1448{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001449 lockdep_assert_held(&cgroup_mutex);
1450 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001451
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001452 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001453 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001454 root->hierarchy_id = 0;
1455 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001456}
1457
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458static int cgroup_test_super(struct super_block *sb, void *data)
1459{
Paul Menagec6d57f32009-09-23 15:56:19 -07001460 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461 struct cgroupfs_root *root = sb->s_fs_info;
1462
Paul Menagec6d57f32009-09-23 15:56:19 -07001463 /* If we asked for a name then it must match */
1464 if (opts->name && strcmp(opts->name, root->name))
1465 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001467 /*
1468 * If we asked for subsystems (or explicitly for no
1469 * subsystems) then they must match
1470 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001471 if ((opts->subsys_mask || opts->none)
1472 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473 return 0;
1474
1475 return 1;
1476}
1477
Paul Menagec6d57f32009-09-23 15:56:19 -07001478static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1479{
1480 struct cgroupfs_root *root;
1481
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001482 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001483 return NULL;
1484
1485 root = kzalloc(sizeof(*root), GFP_KERNEL);
1486 if (!root)
1487 return ERR_PTR(-ENOMEM);
1488
1489 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001490
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001491 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001493 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001494 if (opts->release_agent)
1495 strcpy(root->release_agent_path, opts->release_agent);
1496 if (opts->name)
1497 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001498 if (opts->cpuset_clone_children)
1499 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001500 return root;
1501}
1502
Tejun Heofa3ca072013-04-14 11:36:56 -07001503static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001504{
Tejun Heofa3ca072013-04-14 11:36:56 -07001505 if (root) {
1506 /* hierarhcy ID shoulid already have been released */
1507 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001508
Tejun Heofa3ca072013-04-14 11:36:56 -07001509 ida_destroy(&root->cgroup_ida);
1510 kfree(root);
1511 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001512}
1513
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514static int cgroup_set_super(struct super_block *sb, void *data)
1515{
1516 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001517 struct cgroup_sb_opts *opts = data;
1518
1519 /* If we don't have a new root, we can't set up a new sb */
1520 if (!opts->new_root)
1521 return -EINVAL;
1522
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001523 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524
1525 ret = set_anon_super(sb, NULL);
1526 if (ret)
1527 return ret;
1528
Paul Menagec6d57f32009-09-23 15:56:19 -07001529 sb->s_fs_info = opts->new_root;
1530 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531
1532 sb->s_blocksize = PAGE_CACHE_SIZE;
1533 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1534 sb->s_magic = CGROUP_SUPER_MAGIC;
1535 sb->s_op = &cgroup_ops;
1536
1537 return 0;
1538}
1539
1540static int cgroup_get_rootdir(struct super_block *sb)
1541{
Al Viro0df6a632010-12-21 13:29:29 -05001542 static const struct dentry_operations cgroup_dops = {
1543 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001544 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001545 };
1546
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547 struct inode *inode =
1548 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549
1550 if (!inode)
1551 return -ENOMEM;
1552
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553 inode->i_fop = &simple_dir_operations;
1554 inode->i_op = &cgroup_dir_inode_operations;
1555 /* directories start off with i_nlink == 2 (for "." entry) */
1556 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001557 sb->s_root = d_make_root(inode);
1558 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001560 /* for everything else we want ->d_op set */
1561 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 return 0;
1563}
1564
Al Virof7e83572010-07-26 13:23:11 +04001565static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001567 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568{
1569 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001570 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 int ret = 0;
1572 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001573 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001574 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575
1576 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001577 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001579 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001580 if (ret)
1581 goto out_err;
1582
1583 /*
1584 * Allocate a new cgroup root. We may not need it if we're
1585 * reusing an existing hierarchy.
1586 */
1587 new_root = cgroup_root_from_opts(&opts);
1588 if (IS_ERR(new_root)) {
1589 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001590 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001591 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001592 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593
Paul Menagec6d57f32009-09-23 15:56:19 -07001594 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001595 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001597 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001598 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001599 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600 }
1601
Paul Menagec6d57f32009-09-23 15:56:19 -07001602 root = sb->s_fs_info;
1603 BUG_ON(!root);
1604 if (root == opts.new_root) {
1605 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001606 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001607 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001608 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001609 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001610 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001611 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612
1613 BUG_ON(sb->s_root != NULL);
1614
1615 ret = cgroup_get_rootdir(sb);
1616 if (ret)
1617 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001618 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619
Paul Menage817929e2007-10-18 23:39:36 -07001620 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001622 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001624 /* Check for name clashes with existing mounts */
1625 ret = -EBUSY;
1626 if (strlen(root->name))
1627 for_each_active_root(existing_root)
1628 if (!strcmp(existing_root->name, root->name))
1629 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001630
Paul Menage817929e2007-10-18 23:39:36 -07001631 /*
1632 * We're accessing css_set_count without locking
1633 * css_set_lock here, but that's OK - it can only be
1634 * increased by someone holding cgroup_lock, and
1635 * that's us. The worst that can happen is that we
1636 * have some link structures left over
1637 */
Tejun Heo69d02062013-06-12 21:04:50 -07001638 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001639 if (ret)
1640 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001641
Tejun Heofa3ca072013-04-14 11:36:56 -07001642 ret = cgroup_init_root_id(root);
1643 if (ret)
1644 goto unlock_drop;
1645
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001646 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001648 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001649 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001650 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001651 /*
1652 * There must be no failure case after here, since rebinding
1653 * takes care of subsystems' refcounts, which are explicitly
1654 * dropped in the failure exit path.
1655 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001656
1657 /* EBUSY should be the only error here */
1658 BUG_ON(ret);
1659
1660 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001661 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662
Li Zefanc12f65d2009-01-07 18:07:42 -08001663 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664 root->top_cgroup.dentry = sb->s_root;
1665
Paul Menage817929e2007-10-18 23:39:36 -07001666 /* Link the top cgroup in this hierarchy into all
1667 * the css_set objects */
1668 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001669 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001670 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001671 write_unlock(&css_set_lock);
1672
Tejun Heo69d02062013-06-12 21:04:50 -07001673 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001674
Li Zefanc12f65d2009-01-07 18:07:42 -08001675 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 BUG_ON(root->number_of_cgroups != 1);
1677
eparis@redhat2ce97382011-06-02 21:20:51 +10001678 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001679 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001680 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001681 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001683 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001684 } else {
1685 /*
1686 * We re-used an existing hierarchy - the new root (if
1687 * any) is not needed
1688 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001689 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001690
1691 if (((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) &&
1692 root->flags != opts.flags) {
1693 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1694 ret = -EINVAL;
1695 goto drop_new_super;
1696 }
1697
Ben Blumcf5d5942010-03-10 15:22:09 -08001698 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001699 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700 }
1701
Paul Menagec6d57f32009-09-23 15:56:19 -07001702 kfree(opts.release_agent);
1703 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001704 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001706 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001707 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001708 mutex_unlock(&cgroup_root_mutex);
1709 mutex_unlock(&cgroup_mutex);
1710 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001712 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001713 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001714 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001715 out_err:
1716 kfree(opts.release_agent);
1717 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001718 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719}
1720
1721static void cgroup_kill_sb(struct super_block *sb) {
1722 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001723 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001724 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001725 int ret;
1726
1727 BUG_ON(!root);
1728
1729 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001730 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001731
1732 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001733 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734
1735 /* Rebind all subsystems back to the default hierarchy */
1736 ret = rebind_subsystems(root, 0);
1737 /* Shouldn't be able to fail ... */
1738 BUG_ON(ret);
1739
Paul Menage817929e2007-10-18 23:39:36 -07001740 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001741 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001742 * root cgroup
1743 */
1744 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001745
Tejun Heo69d02062013-06-12 21:04:50 -07001746 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1747 list_del(&link->cset_link);
1748 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001749 kfree(link);
1750 }
1751 write_unlock(&css_set_lock);
1752
Paul Menage839ec542009-01-29 14:25:22 -08001753 if (!list_empty(&root->root_list)) {
1754 list_del(&root->root_list);
1755 root_count--;
1756 }
Li Zefane5f6a862009-01-07 18:07:41 -08001757
Tejun Heofa3ca072013-04-14 11:36:56 -07001758 cgroup_exit_root_id(root);
1759
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001760 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 mutex_unlock(&cgroup_mutex);
1762
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001763 simple_xattrs_free(&cgrp->xattrs);
1764
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001766 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767}
1768
1769static struct file_system_type cgroup_fs_type = {
1770 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001771 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 .kill_sb = cgroup_kill_sb,
1773};
1774
Greg KH676db4a2010-08-05 13:53:35 -07001775static struct kobject *cgroup_kobj;
1776
Li Zefana043e3b2008-02-23 15:24:09 -08001777/**
1778 * cgroup_path - generate the path of a cgroup
1779 * @cgrp: the cgroup in question
1780 * @buf: the buffer to write the path into
1781 * @buflen: the length of the buffer
1782 *
Li Zefan65dff752013-03-01 15:01:56 +08001783 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1784 *
1785 * We can't generate cgroup path using dentry->d_name, as accessing
1786 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1787 * inode's i_mutex, while on the other hand cgroup_path() can be called
1788 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001790int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791{
Li Zefan65dff752013-03-01 15:01:56 +08001792 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001794
Tejun Heoda1f2962013-04-14 10:32:19 -07001795 if (!cgrp->parent) {
1796 if (strlcpy(buf, "/", buflen) >= buflen)
1797 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798 return 0;
1799 }
1800
Tao Ma316eb662012-11-08 21:36:38 +08001801 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001802 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001803
Li Zefan65dff752013-03-01 15:01:56 +08001804 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001805 do {
Li Zefan65dff752013-03-01 15:01:56 +08001806 const char *name = cgroup_name(cgrp);
1807 int len;
1808
1809 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001811 goto out;
1812 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001813
Paul Menageddbcc7e2007-10-18 23:39:30 -07001814 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001815 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001817
1818 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001819 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001820 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001821 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001822out:
1823 rcu_read_unlock();
1824 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001825}
Ben Blum67523c42010-03-10 15:22:11 -08001826EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001827
Tejun Heo857a2be2013-04-14 20:50:08 -07001828/**
1829 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1830 * @task: target task
1831 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1832 * @buf: the buffer to write the path into
1833 * @buflen: the length of the buffer
1834 *
1835 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1836 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1837 * be used inside locks used by cgroup controller callbacks.
1838 */
1839int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1840 char *buf, size_t buflen)
1841{
1842 struct cgroupfs_root *root;
1843 struct cgroup *cgrp = NULL;
1844 int ret = -ENOENT;
1845
1846 mutex_lock(&cgroup_mutex);
1847
1848 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1849 if (root) {
1850 cgrp = task_cgroup_from_root(task, root);
1851 ret = cgroup_path(cgrp, buf, buflen);
1852 }
1853
1854 mutex_unlock(&cgroup_mutex);
1855
1856 return ret;
1857}
1858EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1859
Ben Blum74a11662011-05-26 16:25:20 -07001860/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001861 * Control Group taskset
1862 */
Tejun Heo134d3372011-12-12 18:12:21 -08001863struct task_and_cgroup {
1864 struct task_struct *task;
1865 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001866 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001867};
1868
Tejun Heo2f7ee562011-12-12 18:12:21 -08001869struct cgroup_taskset {
1870 struct task_and_cgroup single;
1871 struct flex_array *tc_array;
1872 int tc_array_len;
1873 int idx;
1874 struct cgroup *cur_cgrp;
1875};
1876
1877/**
1878 * cgroup_taskset_first - reset taskset and return the first task
1879 * @tset: taskset of interest
1880 *
1881 * @tset iteration is initialized and the first task is returned.
1882 */
1883struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1884{
1885 if (tset->tc_array) {
1886 tset->idx = 0;
1887 return cgroup_taskset_next(tset);
1888 } else {
1889 tset->cur_cgrp = tset->single.cgrp;
1890 return tset->single.task;
1891 }
1892}
1893EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1894
1895/**
1896 * cgroup_taskset_next - iterate to the next task in taskset
1897 * @tset: taskset of interest
1898 *
1899 * Return the next task in @tset. Iteration must have been initialized
1900 * with cgroup_taskset_first().
1901 */
1902struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1903{
1904 struct task_and_cgroup *tc;
1905
1906 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1907 return NULL;
1908
1909 tc = flex_array_get(tset->tc_array, tset->idx++);
1910 tset->cur_cgrp = tc->cgrp;
1911 return tc->task;
1912}
1913EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1914
1915/**
1916 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1917 * @tset: taskset of interest
1918 *
1919 * Return the cgroup for the current (last returned) task of @tset. This
1920 * function must be preceded by either cgroup_taskset_first() or
1921 * cgroup_taskset_next().
1922 */
1923struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1924{
1925 return tset->cur_cgrp;
1926}
1927EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1928
1929/**
1930 * cgroup_taskset_size - return the number of tasks in taskset
1931 * @tset: taskset of interest
1932 */
1933int cgroup_taskset_size(struct cgroup_taskset *tset)
1934{
1935 return tset->tc_array ? tset->tc_array_len : 1;
1936}
1937EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1938
1939
Ben Blum74a11662011-05-26 16:25:20 -07001940/*
1941 * cgroup_task_migrate - move a task from one cgroup to another.
1942 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001943 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001944 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001945static void cgroup_task_migrate(struct cgroup *old_cgrp,
1946 struct task_struct *tsk,
1947 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001948{
Tejun Heo5abb8852013-06-12 21:04:49 -07001949 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001950
1951 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001952 * We are synchronized through threadgroup_lock() against PF_EXITING
1953 * setting such that we can't race against cgroup_exit() changing the
1954 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001955 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001956 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heo5abb8852013-06-12 21:04:49 -07001957 old_cset = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001958
Ben Blum74a11662011-05-26 16:25:20 -07001959 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001960 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001961 task_unlock(tsk);
1962
1963 /* Update the css_set linked lists if we're using them */
1964 write_lock(&css_set_lock);
1965 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001966 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001967 write_unlock(&css_set_lock);
1968
1969 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001970 * We just gained a reference on old_cset by taking it from the
1971 * task. As trading it for new_cset is protected by cgroup_mutex,
1972 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001973 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001974 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1975 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001976}
1977
Li Zefana043e3b2008-02-23 15:24:09 -08001978/**
Li Zefan081aa452013-03-13 09:17:09 +08001979 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001980 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001981 * @tsk: the task or the leader of the threadgroup to be attached
1982 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001983 *
Tejun Heo257058a2011-12-12 18:12:21 -08001984 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001985 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001986 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001987static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1988 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001989{
1990 int retval, i, group_size;
1991 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001992 struct cgroupfs_root *root = cgrp->root;
1993 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001994 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001995 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001996 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001997 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001998
1999 /*
2000 * step 0: in order to do expensive, possibly blocking operations for
2001 * every thread, we cannot iterate the thread group list, since it needs
2002 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002003 * group - group_rwsem prevents new threads from appearing, and if
2004 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002005 */
Li Zefan081aa452013-03-13 09:17:09 +08002006 if (threadgroup)
2007 group_size = get_nr_threads(tsk);
2008 else
2009 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002010 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002011 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002012 if (!group)
2013 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002014 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002015 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002016 if (retval)
2017 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002018
Ben Blum74a11662011-05-26 16:25:20 -07002019 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002020 /*
2021 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2022 * already PF_EXITING could be freed from underneath us unless we
2023 * take an rcu_read_lock.
2024 */
2025 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002026 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002027 struct task_and_cgroup ent;
2028
Tejun Heocd3d0952011-12-12 18:12:21 -08002029 /* @tsk either already exited or can't exit until the end */
2030 if (tsk->flags & PF_EXITING)
2031 continue;
2032
Ben Blum74a11662011-05-26 16:25:20 -07002033 /* as per above, nr_threads may decrease, but not increase. */
2034 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002035 ent.task = tsk;
2036 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002037 /* nothing to do if this task is already in the cgroup */
2038 if (ent.cgrp == cgrp)
2039 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002040 /*
2041 * saying GFP_ATOMIC has no effect here because we did prealloc
2042 * earlier, but it's good form to communicate our expectations.
2043 */
Tejun Heo134d3372011-12-12 18:12:21 -08002044 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002045 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002046 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002047
2048 if (!threadgroup)
2049 break;
Ben Blum74a11662011-05-26 16:25:20 -07002050 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002051 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002052 /* remember the number of threads in the array for later. */
2053 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002054 tset.tc_array = group;
2055 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002056
Tejun Heo134d3372011-12-12 18:12:21 -08002057 /* methods shouldn't be called if no task is actually migrating */
2058 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002059 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002060 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002061
Ben Blum74a11662011-05-26 16:25:20 -07002062 /*
2063 * step 1: check that we can legitimately attach to the cgroup.
2064 */
2065 for_each_subsys(root, ss) {
2066 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002067 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002068 if (retval) {
2069 failed_ss = ss;
2070 goto out_cancel_attach;
2071 }
2072 }
Ben Blum74a11662011-05-26 16:25:20 -07002073 }
2074
2075 /*
2076 * step 2: make sure css_sets exist for all threads to be migrated.
2077 * we use find_css_set, which allocates a new one if necessary.
2078 */
Ben Blum74a11662011-05-26 16:25:20 -07002079 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002080 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002081 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2082 if (!tc->cg) {
2083 retval = -ENOMEM;
2084 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002085 }
2086 }
2087
2088 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002089 * step 3: now that we're guaranteed success wrt the css_sets,
2090 * proceed to move all tasks to the new cgroup. There are no
2091 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002092 */
Ben Blum74a11662011-05-26 16:25:20 -07002093 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002094 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002095 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002096 }
2097 /* nothing is sensitive to fork() after this point. */
2098
2099 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002100 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002101 */
2102 for_each_subsys(root, ss) {
2103 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002104 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002105 }
2106
2107 /*
2108 * step 5: success! and cleanup
2109 */
Ben Blum74a11662011-05-26 16:25:20 -07002110 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002111out_put_css_set_refs:
2112 if (retval) {
2113 for (i = 0; i < group_size; i++) {
2114 tc = flex_array_get(group, i);
2115 if (!tc->cg)
2116 break;
2117 put_css_set(tc->cg);
2118 }
Ben Blum74a11662011-05-26 16:25:20 -07002119 }
2120out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002121 if (retval) {
2122 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002123 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002124 break;
Ben Blum74a11662011-05-26 16:25:20 -07002125 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002126 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002127 }
2128 }
Ben Blum74a11662011-05-26 16:25:20 -07002129out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002130 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002131 return retval;
2132}
2133
2134/*
2135 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002136 * function to attach either it or all tasks in its threadgroup. Will lock
2137 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002138 */
2139static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002140{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002141 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002142 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002143 int ret;
2144
Ben Blum74a11662011-05-26 16:25:20 -07002145 if (!cgroup_lock_live_group(cgrp))
2146 return -ENODEV;
2147
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002148retry_find_task:
2149 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002150 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002151 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002152 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002153 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002154 ret= -ESRCH;
2155 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002156 }
Ben Blum74a11662011-05-26 16:25:20 -07002157 /*
2158 * even if we're attaching all tasks in the thread group, we
2159 * only need to check permissions on one of them.
2160 */
David Howellsc69e8d92008-11-14 10:39:19 +11002161 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002162 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2163 !uid_eq(cred->euid, tcred->uid) &&
2164 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002165 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002166 ret = -EACCES;
2167 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002168 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002169 } else
2170 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002171
2172 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002173 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002174
2175 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002176 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002177 * trapped in a cpuset, or RT worker may be born in a cgroup
2178 * with no rt_runtime allocated. Just say no.
2179 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002180 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002181 ret = -EINVAL;
2182 rcu_read_unlock();
2183 goto out_unlock_cgroup;
2184 }
2185
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002186 get_task_struct(tsk);
2187 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002188
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002189 threadgroup_lock(tsk);
2190 if (threadgroup) {
2191 if (!thread_group_leader(tsk)) {
2192 /*
2193 * a race with de_thread from another thread's exec()
2194 * may strip us of our leadership, if this happens,
2195 * there is no choice but to throw this task away and
2196 * try again; this is
2197 * "double-double-toil-and-trouble-check locking".
2198 */
2199 threadgroup_unlock(tsk);
2200 put_task_struct(tsk);
2201 goto retry_find_task;
2202 }
Li Zefan081aa452013-03-13 09:17:09 +08002203 }
2204
2205 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2206
Tejun Heocd3d0952011-12-12 18:12:21 -08002207 threadgroup_unlock(tsk);
2208
Paul Menagebbcb81d2007-10-18 23:39:32 -07002209 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002210out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002211 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002212 return ret;
2213}
2214
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002215/**
2216 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2217 * @from: attach to all cgroups of a given task
2218 * @tsk: the task to be attached
2219 */
2220int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2221{
2222 struct cgroupfs_root *root;
2223 int retval = 0;
2224
Tejun Heo47cfcd02013-04-07 09:29:51 -07002225 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002226 for_each_active_root(root) {
2227 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2228
2229 retval = cgroup_attach_task(from_cg, tsk, false);
2230 if (retval)
2231 break;
2232 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002233 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002234
2235 return retval;
2236}
2237EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2238
Paul Menageaf351022008-07-25 01:47:01 -07002239static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2240{
Ben Blum74a11662011-05-26 16:25:20 -07002241 return attach_task_by_pid(cgrp, pid, false);
2242}
2243
2244static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2245{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002246 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002247}
2248
Paul Menagee788e062008-07-25 01:46:59 -07002249static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2250 const char *buffer)
2251{
2252 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002253 if (strlen(buffer) >= PATH_MAX)
2254 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002255 if (!cgroup_lock_live_group(cgrp))
2256 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002257 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002258 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002259 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002260 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002261 return 0;
2262}
2263
2264static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2265 struct seq_file *seq)
2266{
2267 if (!cgroup_lock_live_group(cgrp))
2268 return -ENODEV;
2269 seq_puts(seq, cgrp->root->release_agent_path);
2270 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002271 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002272 return 0;
2273}
2274
Tejun Heo873fe092013-04-14 20:15:26 -07002275static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2276 struct seq_file *seq)
2277{
2278 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002279 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002280}
2281
Paul Menage84eea842008-07-25 01:47:00 -07002282/* A buffer size big enough for numbers or short strings */
2283#define CGROUP_LOCAL_BUFFER_SIZE 64
2284
Paul Menagee73d2c62008-04-29 01:00:06 -07002285static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002286 struct file *file,
2287 const char __user *userbuf,
2288 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002289{
Paul Menage84eea842008-07-25 01:47:00 -07002290 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002291 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002292 char *end;
2293
2294 if (!nbytes)
2295 return -EINVAL;
2296 if (nbytes >= sizeof(buffer))
2297 return -E2BIG;
2298 if (copy_from_user(buffer, userbuf, nbytes))
2299 return -EFAULT;
2300
2301 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002302 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002303 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002304 if (*end)
2305 return -EINVAL;
2306 retval = cft->write_u64(cgrp, cft, val);
2307 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002308 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002309 if (*end)
2310 return -EINVAL;
2311 retval = cft->write_s64(cgrp, cft, val);
2312 }
Paul Menage355e0c42007-10-18 23:39:33 -07002313 if (!retval)
2314 retval = nbytes;
2315 return retval;
2316}
2317
Paul Menagedb3b1492008-07-25 01:46:58 -07002318static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2319 struct file *file,
2320 const char __user *userbuf,
2321 size_t nbytes, loff_t *unused_ppos)
2322{
Paul Menage84eea842008-07-25 01:47:00 -07002323 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002324 int retval = 0;
2325 size_t max_bytes = cft->max_write_len;
2326 char *buffer = local_buffer;
2327
2328 if (!max_bytes)
2329 max_bytes = sizeof(local_buffer) - 1;
2330 if (nbytes >= max_bytes)
2331 return -E2BIG;
2332 /* Allocate a dynamic buffer if we need one */
2333 if (nbytes >= sizeof(local_buffer)) {
2334 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2335 if (buffer == NULL)
2336 return -ENOMEM;
2337 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002338 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2339 retval = -EFAULT;
2340 goto out;
2341 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002342
2343 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002344 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002345 if (!retval)
2346 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002347out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002348 if (buffer != local_buffer)
2349 kfree(buffer);
2350 return retval;
2351}
2352
Paul Menageddbcc7e2007-10-18 23:39:30 -07002353static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2354 size_t nbytes, loff_t *ppos)
2355{
2356 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002357 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002358
Tejun Heo54766d42013-06-12 21:04:53 -07002359 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002360 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002361 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002362 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002363 if (cft->write_u64 || cft->write_s64)
2364 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002365 if (cft->write_string)
2366 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002367 if (cft->trigger) {
2368 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2369 return ret ? ret : nbytes;
2370 }
Paul Menage355e0c42007-10-18 23:39:33 -07002371 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002372}
2373
Paul Menagef4c753b2008-04-29 00:59:56 -07002374static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2375 struct file *file,
2376 char __user *buf, size_t nbytes,
2377 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378{
Paul Menage84eea842008-07-25 01:47:00 -07002379 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002380 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002381 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2382
2383 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2384}
2385
Paul Menagee73d2c62008-04-29 01:00:06 -07002386static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2387 struct file *file,
2388 char __user *buf, size_t nbytes,
2389 loff_t *ppos)
2390{
Paul Menage84eea842008-07-25 01:47:00 -07002391 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002392 s64 val = cft->read_s64(cgrp, cft);
2393 int len = sprintf(tmp, "%lld\n", (long long) val);
2394
2395 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2396}
2397
Paul Menageddbcc7e2007-10-18 23:39:30 -07002398static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2399 size_t nbytes, loff_t *ppos)
2400{
2401 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002402 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002403
Tejun Heo54766d42013-06-12 21:04:53 -07002404 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002405 return -ENODEV;
2406
2407 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002408 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002409 if (cft->read_u64)
2410 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002411 if (cft->read_s64)
2412 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002413 return -EINVAL;
2414}
2415
Paul Menage91796562008-04-29 01:00:01 -07002416/*
2417 * seqfile ops/methods for returning structured data. Currently just
2418 * supports string->u64 maps, but can be extended in future.
2419 */
2420
2421struct cgroup_seqfile_state {
2422 struct cftype *cft;
2423 struct cgroup *cgroup;
2424};
2425
2426static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2427{
2428 struct seq_file *sf = cb->state;
2429 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2430}
2431
2432static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2433{
2434 struct cgroup_seqfile_state *state = m->private;
2435 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002436 if (cft->read_map) {
2437 struct cgroup_map_cb cb = {
2438 .fill = cgroup_map_add,
2439 .state = m,
2440 };
2441 return cft->read_map(state->cgroup, cft, &cb);
2442 }
2443 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002444}
2445
Adrian Bunk96930a62008-07-25 19:46:21 -07002446static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002447{
2448 struct seq_file *seq = file->private_data;
2449 kfree(seq->private);
2450 return single_release(inode, file);
2451}
2452
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002453static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002454 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002455 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002456 .llseek = seq_lseek,
2457 .release = cgroup_seqfile_release,
2458};
2459
Paul Menageddbcc7e2007-10-18 23:39:30 -07002460static int cgroup_file_open(struct inode *inode, struct file *file)
2461{
2462 int err;
2463 struct cftype *cft;
2464
2465 err = generic_file_open(inode, file);
2466 if (err)
2467 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002468 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002469
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002470 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002471 struct cgroup_seqfile_state *state;
2472
2473 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002474 if (!state)
2475 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002476
Paul Menage91796562008-04-29 01:00:01 -07002477 state->cft = cft;
2478 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2479 file->f_op = &cgroup_seqfile_operations;
2480 err = single_open(file, cgroup_seqfile_show, state);
2481 if (err < 0)
2482 kfree(state);
2483 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002484 err = cft->open(inode, file);
2485 else
2486 err = 0;
2487
2488 return err;
2489}
2490
2491static int cgroup_file_release(struct inode *inode, struct file *file)
2492{
2493 struct cftype *cft = __d_cft(file->f_dentry);
2494 if (cft->release)
2495 return cft->release(inode, file);
2496 return 0;
2497}
2498
2499/*
2500 * cgroup_rename - Only allow simple rename of directories in place.
2501 */
2502static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2503 struct inode *new_dir, struct dentry *new_dentry)
2504{
Li Zefan65dff752013-03-01 15:01:56 +08002505 int ret;
2506 struct cgroup_name *name, *old_name;
2507 struct cgroup *cgrp;
2508
2509 /*
2510 * It's convinient to use parent dir's i_mutex to protected
2511 * cgrp->name.
2512 */
2513 lockdep_assert_held(&old_dir->i_mutex);
2514
Paul Menageddbcc7e2007-10-18 23:39:30 -07002515 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2516 return -ENOTDIR;
2517 if (new_dentry->d_inode)
2518 return -EEXIST;
2519 if (old_dir != new_dir)
2520 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002521
2522 cgrp = __d_cgrp(old_dentry);
2523
2524 name = cgroup_alloc_name(new_dentry);
2525 if (!name)
2526 return -ENOMEM;
2527
2528 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2529 if (ret) {
2530 kfree(name);
2531 return ret;
2532 }
2533
2534 old_name = cgrp->name;
2535 rcu_assign_pointer(cgrp->name, name);
2536
2537 kfree_rcu(old_name, rcu_head);
2538 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002539}
2540
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002541static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2542{
2543 if (S_ISDIR(dentry->d_inode->i_mode))
2544 return &__d_cgrp(dentry)->xattrs;
2545 else
Li Zefan712317a2013-04-18 23:09:52 -07002546 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002547}
2548
2549static inline int xattr_enabled(struct dentry *dentry)
2550{
2551 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002552 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002553}
2554
2555static bool is_valid_xattr(const char *name)
2556{
2557 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2558 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2559 return true;
2560 return false;
2561}
2562
2563static int cgroup_setxattr(struct dentry *dentry, const char *name,
2564 const void *val, size_t size, int flags)
2565{
2566 if (!xattr_enabled(dentry))
2567 return -EOPNOTSUPP;
2568 if (!is_valid_xattr(name))
2569 return -EINVAL;
2570 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2571}
2572
2573static int cgroup_removexattr(struct dentry *dentry, const char *name)
2574{
2575 if (!xattr_enabled(dentry))
2576 return -EOPNOTSUPP;
2577 if (!is_valid_xattr(name))
2578 return -EINVAL;
2579 return simple_xattr_remove(__d_xattrs(dentry), name);
2580}
2581
2582static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2583 void *buf, size_t size)
2584{
2585 if (!xattr_enabled(dentry))
2586 return -EOPNOTSUPP;
2587 if (!is_valid_xattr(name))
2588 return -EINVAL;
2589 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2590}
2591
2592static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2593{
2594 if (!xattr_enabled(dentry))
2595 return -EOPNOTSUPP;
2596 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2597}
2598
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002599static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002600 .read = cgroup_file_read,
2601 .write = cgroup_file_write,
2602 .llseek = generic_file_llseek,
2603 .open = cgroup_file_open,
2604 .release = cgroup_file_release,
2605};
2606
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002607static const struct inode_operations cgroup_file_inode_operations = {
2608 .setxattr = cgroup_setxattr,
2609 .getxattr = cgroup_getxattr,
2610 .listxattr = cgroup_listxattr,
2611 .removexattr = cgroup_removexattr,
2612};
2613
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002614static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002615 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002616 .mkdir = cgroup_mkdir,
2617 .rmdir = cgroup_rmdir,
2618 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002619 .setxattr = cgroup_setxattr,
2620 .getxattr = cgroup_getxattr,
2621 .listxattr = cgroup_listxattr,
2622 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002623};
2624
Al Viro00cd8dd2012-06-10 17:13:09 -04002625static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002626{
2627 if (dentry->d_name.len > NAME_MAX)
2628 return ERR_PTR(-ENAMETOOLONG);
2629 d_add(dentry, NULL);
2630 return NULL;
2631}
2632
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002633/*
2634 * Check if a file is a control file
2635 */
2636static inline struct cftype *__file_cft(struct file *file)
2637{
Al Viro496ad9a2013-01-23 17:07:38 -05002638 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002639 return ERR_PTR(-EINVAL);
2640 return __d_cft(file->f_dentry);
2641}
2642
Al Viroa5e7ed32011-07-26 01:55:55 -04002643static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002644 struct super_block *sb)
2645{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002646 struct inode *inode;
2647
2648 if (!dentry)
2649 return -ENOENT;
2650 if (dentry->d_inode)
2651 return -EEXIST;
2652
2653 inode = cgroup_new_inode(mode, sb);
2654 if (!inode)
2655 return -ENOMEM;
2656
2657 if (S_ISDIR(mode)) {
2658 inode->i_op = &cgroup_dir_inode_operations;
2659 inode->i_fop = &simple_dir_operations;
2660
2661 /* start off with i_nlink == 2 (for "." entry) */
2662 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002663 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002664
Tejun Heob8a2df62012-11-19 08:13:37 -08002665 /*
2666 * Control reaches here with cgroup_mutex held.
2667 * @inode->i_mutex should nest outside cgroup_mutex but we
2668 * want to populate it immediately without releasing
2669 * cgroup_mutex. As @inode isn't visible to anyone else
2670 * yet, trylock will always succeed without affecting
2671 * lockdep checks.
2672 */
2673 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 } else if (S_ISREG(mode)) {
2675 inode->i_size = 0;
2676 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002677 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002679 d_instantiate(dentry, inode);
2680 dget(dentry); /* Extra count - pin the dentry in core */
2681 return 0;
2682}
2683
Li Zefan099fca32009-04-02 16:57:29 -07002684/**
2685 * cgroup_file_mode - deduce file mode of a control file
2686 * @cft: the control file in question
2687 *
2688 * returns cft->mode if ->mode is not 0
2689 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2690 * returns S_IRUGO if it has only a read handler
2691 * returns S_IWUSR if it has only a write hander
2692 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002693static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002694{
Al Viroa5e7ed32011-07-26 01:55:55 -04002695 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002696
2697 if (cft->mode)
2698 return cft->mode;
2699
2700 if (cft->read || cft->read_u64 || cft->read_s64 ||
2701 cft->read_map || cft->read_seq_string)
2702 mode |= S_IRUGO;
2703
2704 if (cft->write || cft->write_u64 || cft->write_s64 ||
2705 cft->write_string || cft->trigger)
2706 mode |= S_IWUSR;
2707
2708 return mode;
2709}
2710
Tejun Heodb0416b2012-04-01 12:09:55 -07002711static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002712 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713{
Paul Menagebd89aab2007-10-18 23:40:44 -07002714 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002715 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002717 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002718 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002719 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002720 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002721
Tejun Heo93438622013-04-14 20:15:25 -07002722 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002723 strcpy(name, subsys->name);
2724 strcat(name, ".");
2725 }
2726 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002727
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002729
2730 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2731 if (!cfe)
2732 return -ENOMEM;
2733
Paul Menageddbcc7e2007-10-18 23:39:30 -07002734 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002735 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002736 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002737 goto out;
2738 }
2739
Li Zefand6cbf352013-05-14 19:44:20 +08002740 cfe->type = (void *)cft;
2741 cfe->dentry = dentry;
2742 dentry->d_fsdata = cfe;
2743 simple_xattrs_init(&cfe->xattrs);
2744
Tejun Heo05ef1d72012-04-01 12:09:56 -07002745 mode = cgroup_file_mode(cft);
2746 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2747 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002748 list_add_tail(&cfe->node, &parent->files);
2749 cfe = NULL;
2750 }
2751 dput(dentry);
2752out:
2753 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754 return error;
2755}
2756
Tejun Heo79578622012-04-01 12:09:56 -07002757static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002758 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002759{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002760 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002761 int err, ret = 0;
2762
2763 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002764 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002765 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2766 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002767 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2768 continue;
2769 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2770 continue;
2771
Li Zefan2739d3c2013-01-21 18:18:33 +08002772 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002773 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002774 if (err)
2775 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2776 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002777 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002778 } else {
2779 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002780 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002781 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002782 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002783}
2784
Tejun Heo8e3f6542012-04-01 12:09:55 -07002785static DEFINE_MUTEX(cgroup_cft_mutex);
2786
2787static void cgroup_cfts_prepare(void)
2788 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2789{
2790 /*
2791 * Thanks to the entanglement with vfs inode locking, we can't walk
2792 * the existing cgroups under cgroup_mutex and create files.
2793 * Instead, we increment reference on all cgroups and build list of
2794 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2795 * exclusive access to the field.
2796 */
2797 mutex_lock(&cgroup_cft_mutex);
2798 mutex_lock(&cgroup_mutex);
2799}
2800
2801static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002802 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002803 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2804{
2805 LIST_HEAD(pending);
2806 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002807
2808 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2809 if (cfts && ss->root != &rootnode) {
2810 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2811 dget(cgrp->dentry);
2812 list_add_tail(&cgrp->cft_q_node, &pending);
2813 }
2814 }
2815
2816 mutex_unlock(&cgroup_mutex);
2817
2818 /*
2819 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2820 * files for all cgroups which were created before.
2821 */
2822 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2823 struct inode *inode = cgrp->dentry->d_inode;
2824
2825 mutex_lock(&inode->i_mutex);
2826 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -07002827 if (!cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002828 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002829 mutex_unlock(&cgroup_mutex);
2830 mutex_unlock(&inode->i_mutex);
2831
2832 list_del_init(&cgrp->cft_q_node);
2833 dput(cgrp->dentry);
2834 }
2835
2836 mutex_unlock(&cgroup_cft_mutex);
2837}
2838
2839/**
2840 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2841 * @ss: target cgroup subsystem
2842 * @cfts: zero-length name terminated array of cftypes
2843 *
2844 * Register @cfts to @ss. Files described by @cfts are created for all
2845 * existing cgroups to which @ss is attached and all future cgroups will
2846 * have them too. This function can be called anytime whether @ss is
2847 * attached or not.
2848 *
2849 * Returns 0 on successful registration, -errno on failure. Note that this
2850 * function currently returns 0 as long as @cfts registration is successful
2851 * even if some file creation attempts on existing cgroups fail.
2852 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002853int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854{
2855 struct cftype_set *set;
2856
2857 set = kzalloc(sizeof(*set), GFP_KERNEL);
2858 if (!set)
2859 return -ENOMEM;
2860
2861 cgroup_cfts_prepare();
2862 set->cfts = cfts;
2863 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002864 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002865
2866 return 0;
2867}
2868EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2869
Li Zefana043e3b2008-02-23 15:24:09 -08002870/**
Tejun Heo79578622012-04-01 12:09:56 -07002871 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2872 * @ss: target cgroup subsystem
2873 * @cfts: zero-length name terminated array of cftypes
2874 *
2875 * Unregister @cfts from @ss. Files described by @cfts are removed from
2876 * all existing cgroups to which @ss is attached and all future cgroups
2877 * won't have them either. This function can be called anytime whether @ss
2878 * is attached or not.
2879 *
2880 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2881 * registered with @ss.
2882 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002883int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002884{
2885 struct cftype_set *set;
2886
2887 cgroup_cfts_prepare();
2888
2889 list_for_each_entry(set, &ss->cftsets, node) {
2890 if (set->cfts == cfts) {
2891 list_del_init(&set->node);
2892 cgroup_cfts_commit(ss, cfts, false);
2893 return 0;
2894 }
2895 }
2896
2897 cgroup_cfts_commit(ss, NULL, false);
2898 return -ENOENT;
2899}
2900
2901/**
Li Zefana043e3b2008-02-23 15:24:09 -08002902 * cgroup_task_count - count the number of tasks in a cgroup.
2903 * @cgrp: the cgroup in question
2904 *
2905 * Return the number of tasks in the cgroup.
2906 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002907int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002908{
2909 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002910 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002911
Paul Menage817929e2007-10-18 23:39:36 -07002912 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002913 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2914 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002915 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002916 return count;
2917}
2918
2919/*
Paul Menage817929e2007-10-18 23:39:36 -07002920 * Advance a list_head iterator. The iterator should be positioned at
2921 * the start of a css_set
2922 */
Tejun Heo69d02062013-06-12 21:04:50 -07002923static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002924{
Tejun Heo69d02062013-06-12 21:04:50 -07002925 struct list_head *l = it->cset_link;
2926 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002927 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002928
2929 /* Advance to the next non-empty css_set */
2930 do {
2931 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002932 if (l == &cgrp->cset_links) {
2933 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002934 return;
2935 }
Tejun Heo69d02062013-06-12 21:04:50 -07002936 link = list_entry(l, struct cgrp_cset_link, cset_link);
2937 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002938 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002939 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002940 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002941}
2942
Cliff Wickman31a7df02008-02-07 00:14:42 -08002943/*
2944 * To reduce the fork() overhead for systems that are not actually
2945 * using their cgroups capability, we don't maintain the lists running
2946 * through each css_set to its tasks until we see the list actually
2947 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002948 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002949static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002950{
2951 struct task_struct *p, *g;
2952 write_lock(&css_set_lock);
2953 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002954 /*
2955 * We need tasklist_lock because RCU is not safe against
2956 * while_each_thread(). Besides, a forking task that has passed
2957 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2958 * is not guaranteed to have its child immediately visible in the
2959 * tasklist if we walk through it with RCU.
2960 */
2961 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002962 do_each_thread(g, p) {
2963 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002964 /*
2965 * We should check if the process is exiting, otherwise
2966 * it will race with cgroup_exit() in that the list
2967 * entry won't be deleted though the process has exited.
2968 */
2969 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002970 list_add(&p->cg_list, &p->cgroups->tasks);
2971 task_unlock(p);
2972 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002973 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002974 write_unlock(&css_set_lock);
2975}
2976
Tejun Heo574bd9f2012-11-09 09:12:29 -08002977/**
Tejun Heo53fa5262013-05-24 10:55:38 +09002978 * cgroup_next_sibling - find the next sibling of a given cgroup
2979 * @pos: the current cgroup
2980 *
2981 * This function returns the next sibling of @pos and should be called
2982 * under RCU read lock. The only requirement is that @pos is accessible.
2983 * The next sibling is guaranteed to be returned regardless of @pos's
2984 * state.
2985 */
2986struct cgroup *cgroup_next_sibling(struct cgroup *pos)
2987{
2988 struct cgroup *next;
2989
2990 WARN_ON_ONCE(!rcu_read_lock_held());
2991
2992 /*
2993 * @pos could already have been removed. Once a cgroup is removed,
2994 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002995 * changes. As CGRP_DEAD assertion is serialized and happens
2996 * before the cgroup is taken off the ->sibling list, if we see it
2997 * unasserted, it's guaranteed that the next sibling hasn't
2998 * finished its grace period even if it's already removed, and thus
2999 * safe to dereference from this RCU critical section. If
3000 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3001 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003002 */
Tejun Heo54766d42013-06-12 21:04:53 -07003003 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003004 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3005 if (&next->sibling != &pos->parent->children)
3006 return next;
3007 return NULL;
3008 }
3009
3010 /*
3011 * Can't dereference the next pointer. Each cgroup is given a
3012 * monotonically increasing unique serial number and always
3013 * appended to the sibling list, so the next one can be found by
3014 * walking the parent's children until we see a cgroup with higher
3015 * serial number than @pos's.
3016 *
3017 * While this path can be slow, it's taken only when either the
3018 * current cgroup is removed or iteration and removal race.
3019 */
3020 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3021 if (next->serial_nr > pos->serial_nr)
3022 return next;
3023 return NULL;
3024}
3025EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3026
3027/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003028 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3029 * @pos: the current position (%NULL to initiate traversal)
3030 * @cgroup: cgroup whose descendants to walk
3031 *
3032 * To be used by cgroup_for_each_descendant_pre(). Find the next
3033 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003034 *
3035 * While this function requires RCU read locking, it doesn't require the
3036 * whole traversal to be contained in a single RCU critical section. This
3037 * function will return the correct next descendant as long as both @pos
3038 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003039 */
3040struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3041 struct cgroup *cgroup)
3042{
3043 struct cgroup *next;
3044
3045 WARN_ON_ONCE(!rcu_read_lock_held());
3046
3047 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003048 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003049 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003050
3051 /* visit the first child if exists */
3052 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3053 if (next)
3054 return next;
3055
3056 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003057 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003058 next = cgroup_next_sibling(pos);
3059 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003060 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003061 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003062 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003063
3064 return NULL;
3065}
3066EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3067
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003068/**
3069 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3070 * @pos: cgroup of interest
3071 *
3072 * Return the rightmost descendant of @pos. If there's no descendant,
3073 * @pos is returned. This can be used during pre-order traversal to skip
3074 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003075 *
3076 * While this function requires RCU read locking, it doesn't require the
3077 * whole traversal to be contained in a single RCU critical section. This
3078 * function will return the correct rightmost descendant as long as @pos is
3079 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003080 */
3081struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3082{
3083 struct cgroup *last, *tmp;
3084
3085 WARN_ON_ONCE(!rcu_read_lock_held());
3086
3087 do {
3088 last = pos;
3089 /* ->prev isn't RCU safe, walk ->next till the end */
3090 pos = NULL;
3091 list_for_each_entry_rcu(tmp, &last->children, sibling)
3092 pos = tmp;
3093 } while (pos);
3094
3095 return last;
3096}
3097EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3098
Tejun Heo574bd9f2012-11-09 09:12:29 -08003099static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3100{
3101 struct cgroup *last;
3102
3103 do {
3104 last = pos;
3105 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3106 sibling);
3107 } while (pos);
3108
3109 return last;
3110}
3111
3112/**
3113 * cgroup_next_descendant_post - find the next descendant for post-order walk
3114 * @pos: the current position (%NULL to initiate traversal)
3115 * @cgroup: cgroup whose descendants to walk
3116 *
3117 * To be used by cgroup_for_each_descendant_post(). Find the next
3118 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003119 *
3120 * While this function requires RCU read locking, it doesn't require the
3121 * whole traversal to be contained in a single RCU critical section. This
3122 * function will return the correct next descendant as long as both @pos
3123 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003124 */
3125struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3126 struct cgroup *cgroup)
3127{
3128 struct cgroup *next;
3129
3130 WARN_ON_ONCE(!rcu_read_lock_held());
3131
3132 /* if first iteration, visit the leftmost descendant */
3133 if (!pos) {
3134 next = cgroup_leftmost_descendant(cgroup);
3135 return next != cgroup ? next : NULL;
3136 }
3137
3138 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003139 next = cgroup_next_sibling(pos);
3140 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003141 return cgroup_leftmost_descendant(next);
3142
3143 /* no sibling left, visit parent */
3144 next = pos->parent;
3145 return next != cgroup ? next : NULL;
3146}
3147EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3148
Paul Menagebd89aab2007-10-18 23:40:44 -07003149void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003150 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003151{
3152 /*
3153 * The first time anyone tries to iterate across a cgroup,
3154 * we need to enable the list linking each css_set to its
3155 * tasks, and fix up all existing tasks.
3156 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003157 if (!use_task_css_set_links)
3158 cgroup_enable_task_cg_lists();
3159
Paul Menage817929e2007-10-18 23:39:36 -07003160 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003161 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003162 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003163}
3164
Paul Menagebd89aab2007-10-18 23:40:44 -07003165struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003166 struct cgroup_iter *it)
3167{
3168 struct task_struct *res;
3169 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003170 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003171
3172 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003173 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003174 return NULL;
3175 res = list_entry(l, struct task_struct, cg_list);
3176 /* Advance iterator to find next entry */
3177 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003178 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3179 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003180 /* We reached the end of this task list - move on to
3181 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003182 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003183 } else {
3184 it->task = l;
3185 }
3186 return res;
3187}
3188
Paul Menagebd89aab2007-10-18 23:40:44 -07003189void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003190 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003191{
3192 read_unlock(&css_set_lock);
3193}
3194
Cliff Wickman31a7df02008-02-07 00:14:42 -08003195static inline int started_after_time(struct task_struct *t1,
3196 struct timespec *time,
3197 struct task_struct *t2)
3198{
3199 int start_diff = timespec_compare(&t1->start_time, time);
3200 if (start_diff > 0) {
3201 return 1;
3202 } else if (start_diff < 0) {
3203 return 0;
3204 } else {
3205 /*
3206 * Arbitrarily, if two processes started at the same
3207 * time, we'll say that the lower pointer value
3208 * started first. Note that t2 may have exited by now
3209 * so this may not be a valid pointer any longer, but
3210 * that's fine - it still serves to distinguish
3211 * between two tasks started (effectively) simultaneously.
3212 */
3213 return t1 > t2;
3214 }
3215}
3216
3217/*
3218 * This function is a callback from heap_insert() and is used to order
3219 * the heap.
3220 * In this case we order the heap in descending task start time.
3221 */
3222static inline int started_after(void *p1, void *p2)
3223{
3224 struct task_struct *t1 = p1;
3225 struct task_struct *t2 = p2;
3226 return started_after_time(t1, &t2->start_time, t2);
3227}
3228
3229/**
3230 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3231 * @scan: struct cgroup_scanner containing arguments for the scan
3232 *
3233 * Arguments include pointers to callback functions test_task() and
3234 * process_task().
3235 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3236 * and if it returns true, call process_task() for it also.
3237 * The test_task pointer may be NULL, meaning always true (select all tasks).
3238 * Effectively duplicates cgroup_iter_{start,next,end}()
3239 * but does not lock css_set_lock for the call to process_task().
3240 * The struct cgroup_scanner may be embedded in any structure of the caller's
3241 * creation.
3242 * It is guaranteed that process_task() will act on every task that
3243 * is a member of the cgroup for the duration of this call. This
3244 * function may or may not call process_task() for tasks that exit
3245 * or move to a different cgroup during the call, or are forked or
3246 * move into the cgroup during the call.
3247 *
3248 * Note that test_task() may be called with locks held, and may in some
3249 * situations be called multiple times for the same task, so it should
3250 * be cheap.
3251 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3252 * pre-allocated and will be used for heap operations (and its "gt" member will
3253 * be overwritten), else a temporary heap will be used (allocation of which
3254 * may cause this function to fail).
3255 */
3256int cgroup_scan_tasks(struct cgroup_scanner *scan)
3257{
3258 int retval, i;
3259 struct cgroup_iter it;
3260 struct task_struct *p, *dropped;
3261 /* Never dereference latest_task, since it's not refcounted */
3262 struct task_struct *latest_task = NULL;
3263 struct ptr_heap tmp_heap;
3264 struct ptr_heap *heap;
3265 struct timespec latest_time = { 0, 0 };
3266
3267 if (scan->heap) {
3268 /* The caller supplied our heap and pre-allocated its memory */
3269 heap = scan->heap;
3270 heap->gt = &started_after;
3271 } else {
3272 /* We need to allocate our own heap memory */
3273 heap = &tmp_heap;
3274 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3275 if (retval)
3276 /* cannot allocate the heap */
3277 return retval;
3278 }
3279
3280 again:
3281 /*
3282 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3283 * to determine which are of interest, and using the scanner's
3284 * "process_task" callback to process any of them that need an update.
3285 * Since we don't want to hold any locks during the task updates,
3286 * gather tasks to be processed in a heap structure.
3287 * The heap is sorted by descending task start time.
3288 * If the statically-sized heap fills up, we overflow tasks that
3289 * started later, and in future iterations only consider tasks that
3290 * started after the latest task in the previous pass. This
3291 * guarantees forward progress and that we don't miss any tasks.
3292 */
3293 heap->size = 0;
3294 cgroup_iter_start(scan->cg, &it);
3295 while ((p = cgroup_iter_next(scan->cg, &it))) {
3296 /*
3297 * Only affect tasks that qualify per the caller's callback,
3298 * if he provided one
3299 */
3300 if (scan->test_task && !scan->test_task(p, scan))
3301 continue;
3302 /*
3303 * Only process tasks that started after the last task
3304 * we processed
3305 */
3306 if (!started_after_time(p, &latest_time, latest_task))
3307 continue;
3308 dropped = heap_insert(heap, p);
3309 if (dropped == NULL) {
3310 /*
3311 * The new task was inserted; the heap wasn't
3312 * previously full
3313 */
3314 get_task_struct(p);
3315 } else if (dropped != p) {
3316 /*
3317 * The new task was inserted, and pushed out a
3318 * different task
3319 */
3320 get_task_struct(p);
3321 put_task_struct(dropped);
3322 }
3323 /*
3324 * Else the new task was newer than anything already in
3325 * the heap and wasn't inserted
3326 */
3327 }
3328 cgroup_iter_end(scan->cg, &it);
3329
3330 if (heap->size) {
3331 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003332 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003333 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003334 latest_time = q->start_time;
3335 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003336 }
3337 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003338 scan->process_task(q, scan);
3339 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003340 }
3341 /*
3342 * If we had to process any tasks at all, scan again
3343 * in case some of them were in the middle of forking
3344 * children that didn't get processed.
3345 * Not the most efficient way to do it, but it avoids
3346 * having to take callback_mutex in the fork path
3347 */
3348 goto again;
3349 }
3350 if (heap == &tmp_heap)
3351 heap_free(&tmp_heap);
3352 return 0;
3353}
3354
Tejun Heo8cc99342013-04-07 09:29:50 -07003355static void cgroup_transfer_one_task(struct task_struct *task,
3356 struct cgroup_scanner *scan)
3357{
3358 struct cgroup *new_cgroup = scan->data;
3359
Tejun Heo47cfcd02013-04-07 09:29:51 -07003360 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003361 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003362 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003363}
3364
3365/**
3366 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3367 * @to: cgroup to which the tasks will be moved
3368 * @from: cgroup in which the tasks currently reside
3369 */
3370int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3371{
3372 struct cgroup_scanner scan;
3373
3374 scan.cg = from;
3375 scan.test_task = NULL; /* select all tasks in cgroup */
3376 scan.process_task = cgroup_transfer_one_task;
3377 scan.heap = NULL;
3378 scan.data = to;
3379
3380 return cgroup_scan_tasks(&scan);
3381}
3382
Paul Menage817929e2007-10-18 23:39:36 -07003383/*
Ben Blum102a7752009-09-23 15:56:26 -07003384 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003385 *
3386 * Reading this file can return large amounts of data if a cgroup has
3387 * *lots* of attached tasks. So it may need several calls to read(),
3388 * but we cannot guarantee that the information we produce is correct
3389 * unless we produce it entirely atomically.
3390 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003391 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003392
Li Zefan24528252012-01-20 11:58:43 +08003393/* which pidlist file are we talking about? */
3394enum cgroup_filetype {
3395 CGROUP_FILE_PROCS,
3396 CGROUP_FILE_TASKS,
3397};
3398
3399/*
3400 * A pidlist is a list of pids that virtually represents the contents of one
3401 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3402 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3403 * to the cgroup.
3404 */
3405struct cgroup_pidlist {
3406 /*
3407 * used to find which pidlist is wanted. doesn't change as long as
3408 * this particular list stays in the list.
3409 */
3410 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3411 /* array of xids */
3412 pid_t *list;
3413 /* how many elements the above list has */
3414 int length;
3415 /* how many files are using the current array */
3416 int use_count;
3417 /* each of these stored in a list by its cgroup */
3418 struct list_head links;
3419 /* pointer to the cgroup we belong to, for list removal purposes */
3420 struct cgroup *owner;
3421 /* protects the other fields */
3422 struct rw_semaphore mutex;
3423};
3424
Paul Menagebbcb81d2007-10-18 23:39:32 -07003425/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003426 * The following two functions "fix" the issue where there are more pids
3427 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3428 * TODO: replace with a kernel-wide solution to this problem
3429 */
3430#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3431static void *pidlist_allocate(int count)
3432{
3433 if (PIDLIST_TOO_LARGE(count))
3434 return vmalloc(count * sizeof(pid_t));
3435 else
3436 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3437}
3438static void pidlist_free(void *p)
3439{
3440 if (is_vmalloc_addr(p))
3441 vfree(p);
3442 else
3443 kfree(p);
3444}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003445
3446/*
Ben Blum102a7752009-09-23 15:56:26 -07003447 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003448 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003449 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003450static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003451{
Ben Blum102a7752009-09-23 15:56:26 -07003452 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003453
3454 /*
3455 * we presume the 0th element is unique, so i starts at 1. trivial
3456 * edge cases first; no work needs to be done for either
3457 */
3458 if (length == 0 || length == 1)
3459 return length;
3460 /* src and dest walk down the list; dest counts unique elements */
3461 for (src = 1; src < length; src++) {
3462 /* find next unique element */
3463 while (list[src] == list[src-1]) {
3464 src++;
3465 if (src == length)
3466 goto after;
3467 }
3468 /* dest always points to where the next unique element goes */
3469 list[dest] = list[src];
3470 dest++;
3471 }
3472after:
Ben Blum102a7752009-09-23 15:56:26 -07003473 return dest;
3474}
3475
3476static int cmppid(const void *a, const void *b)
3477{
3478 return *(pid_t *)a - *(pid_t *)b;
3479}
3480
3481/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003482 * find the appropriate pidlist for our purpose (given procs vs tasks)
3483 * returns with the lock on that pidlist already held, and takes care
3484 * of the use count, or returns NULL with no locks held if we're out of
3485 * memory.
3486 */
3487static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3488 enum cgroup_filetype type)
3489{
3490 struct cgroup_pidlist *l;
3491 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003492 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003493
Ben Blum72a8cb32009-09-23 15:56:27 -07003494 /*
3495 * We can't drop the pidlist_mutex before taking the l->mutex in case
3496 * the last ref-holder is trying to remove l from the list at the same
3497 * time. Holding the pidlist_mutex precludes somebody taking whichever
3498 * list we find out from under us - compare release_pid_array().
3499 */
3500 mutex_lock(&cgrp->pidlist_mutex);
3501 list_for_each_entry(l, &cgrp->pidlists, links) {
3502 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003503 /* make sure l doesn't vanish out from under us */
3504 down_write(&l->mutex);
3505 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003506 return l;
3507 }
3508 }
3509 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003510 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003511 if (!l) {
3512 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003513 return l;
3514 }
3515 init_rwsem(&l->mutex);
3516 down_write(&l->mutex);
3517 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003518 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003519 l->owner = cgrp;
3520 list_add(&l->links, &cgrp->pidlists);
3521 mutex_unlock(&cgrp->pidlist_mutex);
3522 return l;
3523}
3524
3525/*
Ben Blum102a7752009-09-23 15:56:26 -07003526 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3527 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003528static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3529 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003530{
3531 pid_t *array;
3532 int length;
3533 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003534 struct cgroup_iter it;
3535 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003536 struct cgroup_pidlist *l;
3537
3538 /*
3539 * If cgroup gets more users after we read count, we won't have
3540 * enough space - tough. This race is indistinguishable to the
3541 * caller from the case that the additional cgroup users didn't
3542 * show up until sometime later on.
3543 */
3544 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003545 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003546 if (!array)
3547 return -ENOMEM;
3548 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003549 cgroup_iter_start(cgrp, &it);
3550 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003551 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003552 break;
Ben Blum102a7752009-09-23 15:56:26 -07003553 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003554 if (type == CGROUP_FILE_PROCS)
3555 pid = task_tgid_vnr(tsk);
3556 else
3557 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003558 if (pid > 0) /* make sure to only use valid results */
3559 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003560 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003561 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003562 length = n;
3563 /* now sort & (if procs) strip out duplicates */
3564 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003565 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003566 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003567 l = cgroup_pidlist_find(cgrp, type);
3568 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003569 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003570 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003571 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003572 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003573 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003574 l->list = array;
3575 l->length = length;
3576 l->use_count++;
3577 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003578 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003579 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003580}
3581
Balbir Singh846c7bb2007-10-18 23:39:44 -07003582/**
Li Zefana043e3b2008-02-23 15:24:09 -08003583 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003584 * @stats: cgroupstats to fill information into
3585 * @dentry: A dentry entry belonging to the cgroup for which stats have
3586 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003587 *
3588 * Build and fill cgroupstats so that taskstats can export it to user
3589 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003590 */
3591int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3592{
3593 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003594 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003595 struct cgroup_iter it;
3596 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003597
Balbir Singh846c7bb2007-10-18 23:39:44 -07003598 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003599 * Validate dentry by checking the superblock operations,
3600 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003601 */
Li Zefan33d283b2008-11-19 15:36:48 -08003602 if (dentry->d_sb->s_op != &cgroup_ops ||
3603 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003604 goto err;
3605
3606 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003607 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003608
Paul Menagebd89aab2007-10-18 23:40:44 -07003609 cgroup_iter_start(cgrp, &it);
3610 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003611 switch (tsk->state) {
3612 case TASK_RUNNING:
3613 stats->nr_running++;
3614 break;
3615 case TASK_INTERRUPTIBLE:
3616 stats->nr_sleeping++;
3617 break;
3618 case TASK_UNINTERRUPTIBLE:
3619 stats->nr_uninterruptible++;
3620 break;
3621 case TASK_STOPPED:
3622 stats->nr_stopped++;
3623 break;
3624 default:
3625 if (delayacct_is_task_waiting_on_io(tsk))
3626 stats->nr_io_wait++;
3627 break;
3628 }
3629 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003630 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003631
Balbir Singh846c7bb2007-10-18 23:39:44 -07003632err:
3633 return ret;
3634}
3635
Paul Menage8f3ff202009-09-23 15:56:25 -07003636
Paul Menagecc31edc2008-10-18 20:28:04 -07003637/*
Ben Blum102a7752009-09-23 15:56:26 -07003638 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003639 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003640 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003641 */
3642
Ben Blum102a7752009-09-23 15:56:26 -07003643static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003644{
3645 /*
3646 * Initially we receive a position value that corresponds to
3647 * one more than the last pid shown (or 0 on the first call or
3648 * after a seek to the start). Use a binary-search to find the
3649 * next pid to display, if any
3650 */
Ben Blum102a7752009-09-23 15:56:26 -07003651 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003652 int index = 0, pid = *pos;
3653 int *iter;
3654
Ben Blum102a7752009-09-23 15:56:26 -07003655 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003656 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003657 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003658
Paul Menagecc31edc2008-10-18 20:28:04 -07003659 while (index < end) {
3660 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003661 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003662 index = mid;
3663 break;
Ben Blum102a7752009-09-23 15:56:26 -07003664 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003665 index = mid + 1;
3666 else
3667 end = mid;
3668 }
3669 }
3670 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003671 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003672 return NULL;
3673 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003674 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003675 *pos = *iter;
3676 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003677}
3678
Ben Blum102a7752009-09-23 15:56:26 -07003679static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003680{
Ben Blum102a7752009-09-23 15:56:26 -07003681 struct cgroup_pidlist *l = s->private;
3682 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003683}
3684
Ben Blum102a7752009-09-23 15:56:26 -07003685static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003686{
Ben Blum102a7752009-09-23 15:56:26 -07003687 struct cgroup_pidlist *l = s->private;
3688 pid_t *p = v;
3689 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003690 /*
3691 * Advance to the next pid in the array. If this goes off the
3692 * end, we're done
3693 */
3694 p++;
3695 if (p >= end) {
3696 return NULL;
3697 } else {
3698 *pos = *p;
3699 return p;
3700 }
3701}
3702
Ben Blum102a7752009-09-23 15:56:26 -07003703static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003704{
3705 return seq_printf(s, "%d\n", *(int *)v);
3706}
3707
Ben Blum102a7752009-09-23 15:56:26 -07003708/*
3709 * seq_operations functions for iterating on pidlists through seq_file -
3710 * independent of whether it's tasks or procs
3711 */
3712static const struct seq_operations cgroup_pidlist_seq_operations = {
3713 .start = cgroup_pidlist_start,
3714 .stop = cgroup_pidlist_stop,
3715 .next = cgroup_pidlist_next,
3716 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003717};
3718
Ben Blum102a7752009-09-23 15:56:26 -07003719static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003720{
Ben Blum72a8cb32009-09-23 15:56:27 -07003721 /*
3722 * the case where we're the last user of this particular pidlist will
3723 * have us remove it from the cgroup's list, which entails taking the
3724 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3725 * pidlist_mutex, we have to take pidlist_mutex first.
3726 */
3727 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003728 down_write(&l->mutex);
3729 BUG_ON(!l->use_count);
3730 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003731 /* we're the last user if refcount is 0; remove and free */
3732 list_del(&l->links);
3733 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003734 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003735 put_pid_ns(l->key.ns);
3736 up_write(&l->mutex);
3737 kfree(l);
3738 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003739 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003740 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003741 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003742}
3743
Ben Blum102a7752009-09-23 15:56:26 -07003744static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003745{
Ben Blum102a7752009-09-23 15:56:26 -07003746 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003747 if (!(file->f_mode & FMODE_READ))
3748 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003749 /*
3750 * the seq_file will only be initialized if the file was opened for
3751 * reading; hence we check if it's not null only in that case.
3752 */
3753 l = ((struct seq_file *)file->private_data)->private;
3754 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003755 return seq_release(inode, file);
3756}
3757
Ben Blum102a7752009-09-23 15:56:26 -07003758static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003759 .read = seq_read,
3760 .llseek = seq_lseek,
3761 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003762 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003763};
3764
3765/*
Ben Blum102a7752009-09-23 15:56:26 -07003766 * The following functions handle opens on a file that displays a pidlist
3767 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3768 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003769 */
Ben Blum102a7752009-09-23 15:56:26 -07003770/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003771static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003772{
3773 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003774 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003775 int retval;
3776
3777 /* Nothing to do for write-only files */
3778 if (!(file->f_mode & FMODE_READ))
3779 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003780
Ben Blum102a7752009-09-23 15:56:26 -07003781 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003782 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003783 if (retval)
3784 return retval;
3785 /* configure file information */
3786 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003787
Ben Blum102a7752009-09-23 15:56:26 -07003788 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003789 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003790 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003791 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003792 }
Ben Blum102a7752009-09-23 15:56:26 -07003793 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003794 return 0;
3795}
Ben Blum102a7752009-09-23 15:56:26 -07003796static int cgroup_tasks_open(struct inode *unused, struct file *file)
3797{
Ben Blum72a8cb32009-09-23 15:56:27 -07003798 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003799}
3800static int cgroup_procs_open(struct inode *unused, struct file *file)
3801{
Ben Blum72a8cb32009-09-23 15:56:27 -07003802 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003803}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003804
Paul Menagebd89aab2007-10-18 23:40:44 -07003805static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003806 struct cftype *cft)
3807{
Paul Menagebd89aab2007-10-18 23:40:44 -07003808 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003809}
3810
Paul Menage6379c102008-07-25 01:47:01 -07003811static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3812 struct cftype *cft,
3813 u64 val)
3814{
3815 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3816 if (val)
3817 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3818 else
3819 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3820 return 0;
3821}
3822
Paul Menagebbcb81d2007-10-18 23:39:32 -07003823/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003824 * Unregister event and free resources.
3825 *
3826 * Gets called from workqueue.
3827 */
3828static void cgroup_event_remove(struct work_struct *work)
3829{
3830 struct cgroup_event *event = container_of(work, struct cgroup_event,
3831 remove);
3832 struct cgroup *cgrp = event->cgrp;
3833
Li Zefan810cbee2013-02-18 18:56:14 +08003834 remove_wait_queue(event->wqh, &event->wait);
3835
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003836 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3837
Li Zefan810cbee2013-02-18 18:56:14 +08003838 /* Notify userspace the event is going away. */
3839 eventfd_signal(event->eventfd, 1);
3840
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003841 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003842 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003843 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003844}
3845
3846/*
3847 * Gets called on POLLHUP on eventfd when user closes it.
3848 *
3849 * Called with wqh->lock held and interrupts disabled.
3850 */
3851static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3852 int sync, void *key)
3853{
3854 struct cgroup_event *event = container_of(wait,
3855 struct cgroup_event, wait);
3856 struct cgroup *cgrp = event->cgrp;
3857 unsigned long flags = (unsigned long)key;
3858
3859 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003860 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003861 * If the event has been detached at cgroup removal, we
3862 * can simply return knowing the other side will cleanup
3863 * for us.
3864 *
3865 * We can't race against event freeing since the other
3866 * side will require wqh->lock via remove_wait_queue(),
3867 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003868 */
Li Zefan810cbee2013-02-18 18:56:14 +08003869 spin_lock(&cgrp->event_list_lock);
3870 if (!list_empty(&event->list)) {
3871 list_del_init(&event->list);
3872 /*
3873 * We are in atomic context, but cgroup_event_remove()
3874 * may sleep, so we have to call it in workqueue.
3875 */
3876 schedule_work(&event->remove);
3877 }
3878 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003879 }
3880
3881 return 0;
3882}
3883
3884static void cgroup_event_ptable_queue_proc(struct file *file,
3885 wait_queue_head_t *wqh, poll_table *pt)
3886{
3887 struct cgroup_event *event = container_of(pt,
3888 struct cgroup_event, pt);
3889
3890 event->wqh = wqh;
3891 add_wait_queue(wqh, &event->wait);
3892}
3893
3894/*
3895 * Parse input and register new cgroup event handler.
3896 *
3897 * Input must be in format '<event_fd> <control_fd> <args>'.
3898 * Interpretation of args is defined by control file implementation.
3899 */
3900static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3901 const char *buffer)
3902{
3903 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003904 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003905 unsigned int efd, cfd;
3906 struct file *efile = NULL;
3907 struct file *cfile = NULL;
3908 char *endp;
3909 int ret;
3910
3911 efd = simple_strtoul(buffer, &endp, 10);
3912 if (*endp != ' ')
3913 return -EINVAL;
3914 buffer = endp + 1;
3915
3916 cfd = simple_strtoul(buffer, &endp, 10);
3917 if ((*endp != ' ') && (*endp != '\0'))
3918 return -EINVAL;
3919 buffer = endp + 1;
3920
3921 event = kzalloc(sizeof(*event), GFP_KERNEL);
3922 if (!event)
3923 return -ENOMEM;
3924 event->cgrp = cgrp;
3925 INIT_LIST_HEAD(&event->list);
3926 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3927 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3928 INIT_WORK(&event->remove, cgroup_event_remove);
3929
3930 efile = eventfd_fget(efd);
3931 if (IS_ERR(efile)) {
3932 ret = PTR_ERR(efile);
3933 goto fail;
3934 }
3935
3936 event->eventfd = eventfd_ctx_fileget(efile);
3937 if (IS_ERR(event->eventfd)) {
3938 ret = PTR_ERR(event->eventfd);
3939 goto fail;
3940 }
3941
3942 cfile = fget(cfd);
3943 if (!cfile) {
3944 ret = -EBADF;
3945 goto fail;
3946 }
3947
3948 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003949 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003950 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003951 if (ret < 0)
3952 goto fail;
3953
3954 event->cft = __file_cft(cfile);
3955 if (IS_ERR(event->cft)) {
3956 ret = PTR_ERR(event->cft);
3957 goto fail;
3958 }
3959
Li Zefanf1690072013-02-18 14:13:35 +08003960 /*
3961 * The file to be monitored must be in the same cgroup as
3962 * cgroup.event_control is.
3963 */
3964 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3965 if (cgrp_cfile != cgrp) {
3966 ret = -EINVAL;
3967 goto fail;
3968 }
3969
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003970 if (!event->cft->register_event || !event->cft->unregister_event) {
3971 ret = -EINVAL;
3972 goto fail;
3973 }
3974
3975 ret = event->cft->register_event(cgrp, event->cft,
3976 event->eventfd, buffer);
3977 if (ret)
3978 goto fail;
3979
Li Zefan7ef70e42013-04-26 11:58:03 -07003980 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003981
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003982 /*
3983 * Events should be removed after rmdir of cgroup directory, but before
3984 * destroying subsystem state objects. Let's take reference to cgroup
3985 * directory dentry to do that.
3986 */
3987 dget(cgrp->dentry);
3988
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003989 spin_lock(&cgrp->event_list_lock);
3990 list_add(&event->list, &cgrp->event_list);
3991 spin_unlock(&cgrp->event_list_lock);
3992
3993 fput(cfile);
3994 fput(efile);
3995
3996 return 0;
3997
3998fail:
3999 if (cfile)
4000 fput(cfile);
4001
4002 if (event && event->eventfd && !IS_ERR(event->eventfd))
4003 eventfd_ctx_put(event->eventfd);
4004
4005 if (!IS_ERR_OR_NULL(efile))
4006 fput(efile);
4007
4008 kfree(event);
4009
4010 return ret;
4011}
4012
Daniel Lezcano97978e62010-10-27 15:33:35 -07004013static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4014 struct cftype *cft)
4015{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004016 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004017}
4018
4019static int cgroup_clone_children_write(struct cgroup *cgrp,
4020 struct cftype *cft,
4021 u64 val)
4022{
4023 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004024 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004025 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004026 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004027 return 0;
4028}
4029
Tejun Heod5c56ce2013-06-03 19:14:34 -07004030static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004031 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004032 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004033 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004034 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004035 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004036 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004037 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004038 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004039 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004040 .write_string = cgroup_write_event_control,
4041 .mode = S_IWUGO,
4042 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004043 {
4044 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004045 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004046 .read_u64 = cgroup_clone_children_read,
4047 .write_u64 = cgroup_clone_children_write,
4048 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004049 {
Tejun Heo873fe092013-04-14 20:15:26 -07004050 .name = "cgroup.sane_behavior",
4051 .flags = CFTYPE_ONLY_ON_ROOT,
4052 .read_seq_string = cgroup_sane_behavior_show,
4053 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004054
4055 /*
4056 * Historical crazy stuff. These don't have "cgroup." prefix and
4057 * don't exist if sane_behavior. If you're depending on these, be
4058 * prepared to be burned.
4059 */
4060 {
4061 .name = "tasks",
4062 .flags = CFTYPE_INSANE, /* use "procs" instead */
4063 .open = cgroup_tasks_open,
4064 .write_u64 = cgroup_tasks_write,
4065 .release = cgroup_pidlist_release,
4066 .mode = S_IRUGO | S_IWUSR,
4067 },
4068 {
4069 .name = "notify_on_release",
4070 .flags = CFTYPE_INSANE,
4071 .read_u64 = cgroup_read_notify_on_release,
4072 .write_u64 = cgroup_write_notify_on_release,
4073 },
Tejun Heo873fe092013-04-14 20:15:26 -07004074 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004075 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004076 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004077 .read_seq_string = cgroup_release_agent_show,
4078 .write_string = cgroup_release_agent_write,
4079 .max_write_len = PATH_MAX,
4080 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004081 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004082};
4083
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004084/**
4085 * cgroup_populate_dir - selectively creation of files in a directory
4086 * @cgrp: target cgroup
4087 * @base_files: true if the base files should be added
4088 * @subsys_mask: mask of the subsystem ids whose files should be added
4089 */
4090static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4091 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004092{
4093 int err;
4094 struct cgroup_subsys *ss;
4095
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004096 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004097 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004098 if (err < 0)
4099 return err;
4100 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004101
Tejun Heo8e3f6542012-04-01 12:09:55 -07004102 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004103 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004104 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004105 if (!test_bit(ss->subsys_id, &subsys_mask))
4106 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004107
Tejun Heodb0416b2012-04-01 12:09:55 -07004108 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004109 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004110 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004111
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004112 /* This cgroup is ready now */
4113 for_each_subsys(cgrp->root, ss) {
4114 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4115 /*
4116 * Update id->css pointer and make this css visible from
4117 * CSS ID functions. This pointer will be dereferened
4118 * from RCU-read-side without locks.
4119 */
4120 if (css->id)
4121 rcu_assign_pointer(css->id->css, css);
4122 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004123
4124 return 0;
4125}
4126
Tejun Heo48ddbe12012-04-01 12:09:56 -07004127static void css_dput_fn(struct work_struct *work)
4128{
4129 struct cgroup_subsys_state *css =
4130 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004131 struct dentry *dentry = css->cgroup->dentry;
4132 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004133
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004134 atomic_inc(&sb->s_active);
4135 dput(dentry);
4136 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004137}
4138
Paul Menageddbcc7e2007-10-18 23:39:30 -07004139static void init_cgroup_css(struct cgroup_subsys_state *css,
4140 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004141 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004142{
Paul Menagebd89aab2007-10-18 23:40:44 -07004143 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004144 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004145 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004146 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004147 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004148 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004149 BUG_ON(cgrp->subsys[ss->subsys_id]);
4150 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004151
4152 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004153 * css holds an extra ref to @cgrp->dentry which is put on the last
4154 * css_put(). dput() requires process context, which css_put() may
4155 * be called without. @css->dput_work will be used to invoke
4156 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004157 */
4158 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004159}
4160
Tejun Heob1929db2012-11-19 08:13:38 -08004161/* invoke ->post_create() on a new CSS and mark it online if successful */
4162static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004163{
Tejun Heob1929db2012-11-19 08:13:38 -08004164 int ret = 0;
4165
Tejun Heoa31f2d32012-11-19 08:13:37 -08004166 lockdep_assert_held(&cgroup_mutex);
4167
Tejun Heo92fb9742012-11-19 08:13:38 -08004168 if (ss->css_online)
4169 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004170 if (!ret)
4171 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4172 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004173}
4174
4175/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4176static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4177 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4178{
4179 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4180
4181 lockdep_assert_held(&cgroup_mutex);
4182
4183 if (!(css->flags & CSS_ONLINE))
4184 return;
4185
Li Zefand7eeac12013-03-12 15:35:59 -07004186 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004187 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004188
4189 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4190}
4191
Paul Menageddbcc7e2007-10-18 23:39:30 -07004192/*
Li Zefana043e3b2008-02-23 15:24:09 -08004193 * cgroup_create - create a cgroup
4194 * @parent: cgroup that will be parent of the new cgroup
4195 * @dentry: dentry of the new cgroup
4196 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004197 *
Li Zefana043e3b2008-02-23 15:24:09 -08004198 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004201 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202{
Tejun Heo53fa5262013-05-24 10:55:38 +09004203 static atomic64_t serial_nr_cursor = ATOMIC64_INIT(0);
Paul Menagebd89aab2007-10-18 23:40:44 -07004204 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004205 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004206 struct cgroupfs_root *root = parent->root;
4207 int err = 0;
4208 struct cgroup_subsys *ss;
4209 struct super_block *sb = root->sb;
4210
Tejun Heo0a950f62012-11-19 09:02:12 -08004211 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004212 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4213 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004214 return -ENOMEM;
4215
Li Zefan65dff752013-03-01 15:01:56 +08004216 name = cgroup_alloc_name(dentry);
4217 if (!name)
4218 goto err_free_cgrp;
4219 rcu_assign_pointer(cgrp->name, name);
4220
Tejun Heo0a950f62012-11-19 09:02:12 -08004221 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4222 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004223 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004224
Tejun Heo976c06b2012-11-05 09:16:59 -08004225 /*
4226 * Only live parents can have children. Note that the liveliness
4227 * check isn't strictly necessary because cgroup_mkdir() and
4228 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4229 * anyway so that locking is contained inside cgroup proper and we
4230 * don't get nasty surprises if we ever grow another caller.
4231 */
4232 if (!cgroup_lock_live_group(parent)) {
4233 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004234 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004235 }
4236
Paul Menageddbcc7e2007-10-18 23:39:30 -07004237 /* Grab a reference on the superblock so the hierarchy doesn't
4238 * get deleted on unmount if there are child cgroups. This
4239 * can be done outside cgroup_mutex, since the sb can't
4240 * disappear while someone has an open control file on the
4241 * fs */
4242 atomic_inc(&sb->s_active);
4243
Paul Menagecc31edc2008-10-18 20:28:04 -07004244 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004245
Li Zefanfe1c06c2013-01-24 14:30:22 +08004246 dentry->d_fsdata = cgrp;
4247 cgrp->dentry = dentry;
4248
Paul Menagebd89aab2007-10-18 23:40:44 -07004249 cgrp->parent = parent;
4250 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004251
Li Zefanb6abdb02008-03-04 14:28:19 -08004252 if (notify_on_release(parent))
4253 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4254
Tejun Heo2260e7f2012-11-19 08:13:38 -08004255 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4256 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004257
Paul Menageddbcc7e2007-10-18 23:39:30 -07004258 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004259 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004260
Tejun Heo92fb9742012-11-19 08:13:38 -08004261 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004262 if (IS_ERR(css)) {
4263 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004264 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004265 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004266 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004267 if (ss->use_id) {
4268 err = alloc_css_id(ss, parent, cgrp);
4269 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004270 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004271 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272 }
4273
Tejun Heo4e139af2012-11-19 08:13:36 -08004274 /*
4275 * Create directory. cgroup_create_file() returns with the new
4276 * directory locked on success so that it can be populated without
4277 * dropping cgroup_mutex.
4278 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004279 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004281 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004282 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283
Tejun Heo53fa5262013-05-24 10:55:38 +09004284 /*
4285 * Assign a monotonically increasing serial number. With the list
4286 * appending below, it guarantees that sibling cgroups are always
4287 * sorted in the ascending serial number order on the parent's
4288 * ->children.
4289 */
4290 cgrp->serial_nr = atomic64_inc_return(&serial_nr_cursor);
4291
Tejun Heo4e139af2012-11-19 08:13:36 -08004292 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004293 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4294 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4295 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004296
Tejun Heob1929db2012-11-19 08:13:38 -08004297 /* each css holds a ref to the cgroup's dentry */
4298 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004299 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004300
Li Zefan415cf072013-04-08 14:35:02 +08004301 /* hold a ref to the parent's dentry */
4302 dget(parent->dentry);
4303
Tejun Heob1929db2012-11-19 08:13:38 -08004304 /* creation succeeded, notify subsystems */
4305 for_each_subsys(root, ss) {
4306 err = online_css(ss, cgrp);
4307 if (err)
4308 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004309
4310 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4311 parent->parent) {
4312 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",
4313 current->comm, current->pid, ss->name);
4314 if (!strcmp(ss->name, "memory"))
4315 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4316 ss->warned_broken_hierarchy = true;
4317 }
Tejun Heoa8638032012-11-09 09:12:29 -08004318 }
4319
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004320 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004321 if (err)
4322 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004323
4324 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004325 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004326
4327 return 0;
4328
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004329err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004330 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004331 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004332 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004333 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004334 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335 /* Release the reference count that we took on the superblock */
4336 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004337err_free_id:
4338 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004339err_free_name:
4340 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004341err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004342 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004343 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004344
4345err_destroy:
4346 cgroup_destroy_locked(cgrp);
4347 mutex_unlock(&cgroup_mutex);
4348 mutex_unlock(&dentry->d_inode->i_mutex);
4349 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004350}
4351
Al Viro18bb1db2011-07-26 01:41:39 -04004352static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004353{
4354 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4355
4356 /* the vfs holds inode->i_mutex already */
4357 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4358}
4359
Tejun Heo42809dd2012-11-19 08:13:37 -08004360static int cgroup_destroy_locked(struct cgroup *cgrp)
4361 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004362{
Tejun Heo42809dd2012-11-19 08:13:37 -08004363 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004364 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004365 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004366 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004367
Tejun Heo42809dd2012-11-19 08:13:37 -08004368 lockdep_assert_held(&d->d_inode->i_mutex);
4369 lockdep_assert_held(&cgroup_mutex);
4370
Tejun Heoddd69142013-06-12 21:04:54 -07004371 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004372 * css_set_lock synchronizes access to ->cset_links and prevents
4373 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004374 */
4375 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004376 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004377 read_unlock(&css_set_lock);
4378 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004380
Tejun Heo1a90dd52012-11-05 09:16:59 -08004381 /*
4382 * Block new css_tryget() by deactivating refcnt and mark @cgrp
Tejun Heo455050d2013-06-13 19:27:41 -07004383 * removed. This makes future css_tryget() attempts fail which we
4384 * guarantee to ->css_offline() callbacks.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004385 */
Tejun Heoed9577932012-11-05 09:16:58 -08004386 for_each_subsys(cgrp->root, ss) {
4387 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4388
4389 WARN_ON(atomic_read(&css->refcnt) < 0);
4390 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004391 }
Tejun Heo455050d2013-06-13 19:27:41 -07004392
4393 /*
4394 * Mark @cgrp dead. This prevents further task migration and child
4395 * creation by disabling cgroup_lock_live_group(). Note that
4396 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4397 * resume iteration after dropping RCU read lock. See
4398 * cgroup_next_sibling() for details.
4399 */
Tejun Heo54766d42013-06-12 21:04:53 -07004400 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004401
Tejun Heo455050d2013-06-13 19:27:41 -07004402 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4403 raw_spin_lock(&release_list_lock);
4404 if (!list_empty(&cgrp->release_list))
4405 list_del_init(&cgrp->release_list);
4406 raw_spin_unlock(&release_list_lock);
4407
4408 /*
4409 * Remove @cgrp directory. The removal puts the base ref but we
4410 * aren't quite done with @cgrp yet, so hold onto it.
4411 */
4412 dget(d);
4413 cgroup_d_remove_dir(d);
4414
4415 /*
4416 * Unregister events and notify userspace.
4417 * Notify userspace about cgroup removing only after rmdir of cgroup
4418 * directory to avoid race between userspace and kernelspace.
4419 */
4420 spin_lock(&cgrp->event_list_lock);
4421 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4422 list_del_init(&event->list);
4423 schedule_work(&event->remove);
4424 }
4425 spin_unlock(&cgrp->event_list_lock);
4426
Tejun Heoea15f8c2013-06-13 19:27:42 -07004427 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4428 schedule_work(&cgrp->destroy_work);
4429
4430 return 0;
4431};
4432
4433static void cgroup_offline_fn(struct work_struct *work)
4434{
4435 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4436 struct cgroup *parent = cgrp->parent;
4437 struct dentry *d = cgrp->dentry;
4438 struct cgroup_subsys *ss;
4439
4440 mutex_lock(&cgroup_mutex);
4441
Tejun Heoa31f2d32012-11-19 08:13:37 -08004442 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004443 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004444 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004445
4446 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004447 * Put all the base refs. Each css holds an extra reference to the
4448 * cgroup's dentry and cgroup removal proceeds regardless of css
4449 * refs. On the last put of each css, whenever that may be, the
4450 * extra dentry ref is put so that dentry destruction happens only
4451 * after all css's are released.
4452 */
Tejun Heoe9316082012-11-05 09:16:58 -08004453 for_each_subsys(cgrp->root, ss)
4454 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004455
Paul Menage999cd8a2009-01-07 18:08:36 -08004456 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004457 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004458 list_del_init(&cgrp->allcg_node);
4459
Paul Menageddbcc7e2007-10-18 23:39:30 -07004460 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004461
Paul Menagebd89aab2007-10-18 23:40:44 -07004462 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004463 check_for_release(parent);
4464
Tejun Heoea15f8c2013-06-13 19:27:42 -07004465 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004466}
4467
Tejun Heo42809dd2012-11-19 08:13:37 -08004468static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4469{
4470 int ret;
4471
4472 mutex_lock(&cgroup_mutex);
4473 ret = cgroup_destroy_locked(dentry->d_fsdata);
4474 mutex_unlock(&cgroup_mutex);
4475
4476 return ret;
4477}
4478
Tejun Heo8e3f6542012-04-01 12:09:55 -07004479static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4480{
4481 INIT_LIST_HEAD(&ss->cftsets);
4482
4483 /*
4484 * base_cftset is embedded in subsys itself, no need to worry about
4485 * deregistration.
4486 */
4487 if (ss->base_cftypes) {
4488 ss->base_cftset.cfts = ss->base_cftypes;
4489 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4490 }
4491}
4492
Li Zefan06a11922008-04-29 01:00:07 -07004493static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004494{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004495 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004496
4497 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004498
Tejun Heo648bb562012-11-19 08:13:36 -08004499 mutex_lock(&cgroup_mutex);
4500
Tejun Heo8e3f6542012-04-01 12:09:55 -07004501 /* init base cftset */
4502 cgroup_init_cftsets(ss);
4503
Paul Menageddbcc7e2007-10-18 23:39:30 -07004504 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004505 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004506 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004507 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004508 /* We don't handle early failures gracefully */
4509 BUG_ON(IS_ERR(css));
4510 init_cgroup_css(css, ss, dummytop);
4511
Li Zefane8d55fd2008-04-29 01:00:13 -07004512 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004513 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004514 * newly registered, all tasks and hence the
4515 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004516 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004517
4518 need_forkexit_callback |= ss->fork || ss->exit;
4519
Li Zefane8d55fd2008-04-29 01:00:13 -07004520 /* At system boot, before all subsystems have been
4521 * registered, no tasks have been forked, so we don't
4522 * need to invoke fork callbacks here. */
4523 BUG_ON(!list_empty(&init_task.tasks));
4524
Tejun Heob1929db2012-11-19 08:13:38 -08004525 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004526
Tejun Heo648bb562012-11-19 08:13:36 -08004527 mutex_unlock(&cgroup_mutex);
4528
Ben Blume6a11052010-03-10 15:22:09 -08004529 /* this function shouldn't be used with modular subsystems, since they
4530 * need to register a subsys_id, among other things */
4531 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004532}
4533
4534/**
Ben Blume6a11052010-03-10 15:22:09 -08004535 * cgroup_load_subsys: load and register a modular subsystem at runtime
4536 * @ss: the subsystem to load
4537 *
4538 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004539 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004540 * up for use. If the subsystem is built-in anyway, work is delegated to the
4541 * simpler cgroup_init_subsys.
4542 */
4543int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4544{
Ben Blume6a11052010-03-10 15:22:09 -08004545 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004546 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004547 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004548 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004549 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004550
4551 /* check name and function validity */
4552 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004553 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004554 return -EINVAL;
4555
4556 /*
4557 * we don't support callbacks in modular subsystems. this check is
4558 * before the ss->module check for consistency; a subsystem that could
4559 * be a module should still have no callbacks even if the user isn't
4560 * compiling it as one.
4561 */
4562 if (ss->fork || ss->exit)
4563 return -EINVAL;
4564
4565 /*
4566 * an optionally modular subsystem is built-in: we want to do nothing,
4567 * since cgroup_init_subsys will have already taken care of it.
4568 */
4569 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004570 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004571 BUG_ON(subsys[ss->subsys_id] != ss);
4572 return 0;
4573 }
4574
Tejun Heo8e3f6542012-04-01 12:09:55 -07004575 /* init base cftset */
4576 cgroup_init_cftsets(ss);
4577
Ben Blume6a11052010-03-10 15:22:09 -08004578 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004579 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004580
4581 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004582 * no ss->css_alloc seems to need anything important in the ss
4583 * struct, so this can happen first (i.e. before the rootnode
4584 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004585 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004586 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004587 if (IS_ERR(css)) {
4588 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004589 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004590 mutex_unlock(&cgroup_mutex);
4591 return PTR_ERR(css);
4592 }
4593
4594 list_add(&ss->sibling, &rootnode.subsys_list);
4595 ss->root = &rootnode;
4596
4597 /* our new subsystem will be attached to the dummy hierarchy. */
4598 init_cgroup_css(css, ss, dummytop);
4599 /* init_idr must be after init_cgroup_css because it sets css->id. */
4600 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004601 ret = cgroup_init_idr(ss, css);
4602 if (ret)
4603 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004604 }
4605
4606 /*
4607 * Now we need to entangle the css into the existing css_sets. unlike
4608 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4609 * will need a new pointer to it; done by iterating the css_set_table.
4610 * furthermore, modifying the existing css_sets will corrupt the hash
4611 * table state, so each changed css_set will need its hash recomputed.
4612 * this is all done under the css_set_lock.
4613 */
4614 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004615 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004616 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004617 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004618 continue;
4619 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004620 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004621 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004622 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004623 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004624 key = css_set_hash(cset->subsys);
4625 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004626 }
4627 write_unlock(&css_set_lock);
4628
Tejun Heob1929db2012-11-19 08:13:38 -08004629 ret = online_css(ss, dummytop);
4630 if (ret)
4631 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004632
Ben Blume6a11052010-03-10 15:22:09 -08004633 /* success! */
4634 mutex_unlock(&cgroup_mutex);
4635 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004636
4637err_unload:
4638 mutex_unlock(&cgroup_mutex);
4639 /* @ss can't be mounted here as try_module_get() would fail */
4640 cgroup_unload_subsys(ss);
4641 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004642}
4643EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4644
4645/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004646 * cgroup_unload_subsys: unload a modular subsystem
4647 * @ss: the subsystem to unload
4648 *
4649 * This function should be called in a modular subsystem's exitcall. When this
4650 * function is invoked, the refcount on the subsystem's module will be 0, so
4651 * the subsystem will not be attached to any hierarchy.
4652 */
4653void cgroup_unload_subsys(struct cgroup_subsys *ss)
4654{
Tejun Heo69d02062013-06-12 21:04:50 -07004655 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004656
4657 BUG_ON(ss->module == NULL);
4658
4659 /*
4660 * we shouldn't be called if the subsystem is in use, and the use of
4661 * try_module_get in parse_cgroupfs_options should ensure that it
4662 * doesn't start being used while we're killing it off.
4663 */
4664 BUG_ON(ss->root != &rootnode);
4665
4666 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004667
Tejun Heoa31f2d32012-11-19 08:13:37 -08004668 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004669
Tejun Heoc897ff62013-02-27 17:03:49 -08004670 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004671 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004672
Ben Blumcf5d5942010-03-10 15:22:09 -08004673 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004674 subsys[ss->subsys_id] = NULL;
4675
4676 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004677 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004678
4679 /*
4680 * disentangle the css from all css_sets attached to the dummytop. as
4681 * in loading, we need to pay our respects to the hashtable gods.
4682 */
4683 write_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07004684 list_for_each_entry(link, &dummytop->cset_links, cset_link) {
4685 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004686 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004687
Tejun Heo5abb8852013-06-12 21:04:49 -07004688 hash_del(&cset->hlist);
4689 cset->subsys[ss->subsys_id] = NULL;
4690 key = css_set_hash(cset->subsys);
4691 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004692 }
4693 write_unlock(&css_set_lock);
4694
4695 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004696 * remove subsystem's css from the dummytop and free it - need to
4697 * free before marking as null because ss->css_free needs the
4698 * cgrp->subsys pointer to find their state. note that this also
4699 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004700 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004701 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004702 dummytop->subsys[ss->subsys_id] = NULL;
4703
4704 mutex_unlock(&cgroup_mutex);
4705}
4706EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4707
4708/**
Li Zefana043e3b2008-02-23 15:24:09 -08004709 * cgroup_init_early - cgroup initialization at system boot
4710 *
4711 * Initialize cgroups at system boot, and initialize any
4712 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004713 */
4714int __init cgroup_init_early(void)
4715{
4716 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004717 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004718 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004719 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004720 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004721 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004722 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004723 root_count = 1;
4724 init_task.cgroups = &init_css_set;
4725
Tejun Heo69d02062013-06-12 21:04:50 -07004726 init_cgrp_cset_link.cset = &init_css_set;
4727 init_cgrp_cset_link.cgrp = dummytop;
4728 list_add(&init_cgrp_cset_link.cset_link, &rootnode.top_cgroup.cset_links);
4729 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004730
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004731 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004732 struct cgroup_subsys *ss = subsys[i];
4733
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004734 /* at bootup time, we don't worry about modular subsystems */
4735 if (!ss || ss->module)
4736 continue;
4737
Paul Menageddbcc7e2007-10-18 23:39:30 -07004738 BUG_ON(!ss->name);
4739 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004740 BUG_ON(!ss->css_alloc);
4741 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004742 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004743 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004744 ss->name, ss->subsys_id);
4745 BUG();
4746 }
4747
4748 if (ss->early_init)
4749 cgroup_init_subsys(ss);
4750 }
4751 return 0;
4752}
4753
4754/**
Li Zefana043e3b2008-02-23 15:24:09 -08004755 * cgroup_init - cgroup initialization
4756 *
4757 * Register cgroup filesystem and /proc file, and initialize
4758 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004759 */
4760int __init cgroup_init(void)
4761{
4762 int err;
4763 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004764 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004765
4766 err = bdi_init(&cgroup_backing_dev_info);
4767 if (err)
4768 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004769
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004770 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004771 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004772
4773 /* at bootup time, we don't worry about modular subsystems */
4774 if (!ss || ss->module)
4775 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004776 if (!ss->early_init)
4777 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004778 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004779 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004780 }
4781
Li Zefan472b1052008-04-29 01:00:11 -07004782 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004783 key = css_set_hash(init_css_set.subsys);
4784 hash_add(css_set_table, &init_css_set.hlist, key);
Tejun Heofa3ca072013-04-14 11:36:56 -07004785
4786 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004787 mutex_lock(&cgroup_mutex);
4788 mutex_lock(&cgroup_root_mutex);
4789
Tejun Heofa3ca072013-04-14 11:36:56 -07004790 BUG_ON(cgroup_init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004791
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004792 mutex_unlock(&cgroup_root_mutex);
4793 mutex_unlock(&cgroup_mutex);
4794
Greg KH676db4a2010-08-05 13:53:35 -07004795 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4796 if (!cgroup_kobj) {
4797 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004798 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004799 }
4800
4801 err = register_filesystem(&cgroup_fs_type);
4802 if (err < 0) {
4803 kobject_put(cgroup_kobj);
4804 goto out;
4805 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004806
Li Zefan46ae2202008-04-29 01:00:08 -07004807 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004808
Paul Menageddbcc7e2007-10-18 23:39:30 -07004809out:
Paul Menagea4243162007-10-18 23:39:35 -07004810 if (err)
4811 bdi_destroy(&cgroup_backing_dev_info);
4812
Paul Menageddbcc7e2007-10-18 23:39:30 -07004813 return err;
4814}
Paul Menageb4f48b62007-10-18 23:39:33 -07004815
Paul Menagea4243162007-10-18 23:39:35 -07004816/*
4817 * proc_cgroup_show()
4818 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4819 * - Used for /proc/<pid>/cgroup.
4820 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4821 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004822 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004823 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4824 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4825 * cgroup to top_cgroup.
4826 */
4827
4828/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004829int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004830{
4831 struct pid *pid;
4832 struct task_struct *tsk;
4833 char *buf;
4834 int retval;
4835 struct cgroupfs_root *root;
4836
4837 retval = -ENOMEM;
4838 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4839 if (!buf)
4840 goto out;
4841
4842 retval = -ESRCH;
4843 pid = m->private;
4844 tsk = get_pid_task(pid, PIDTYPE_PID);
4845 if (!tsk)
4846 goto out_free;
4847
4848 retval = 0;
4849
4850 mutex_lock(&cgroup_mutex);
4851
Li Zefane5f6a862009-01-07 18:07:41 -08004852 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004853 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004854 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004855 int count = 0;
4856
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004857 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004858 for_each_subsys(root, ss)
4859 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004860 if (strlen(root->name))
4861 seq_printf(m, "%sname=%s", count ? "," : "",
4862 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004863 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004864 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004865 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004866 if (retval < 0)
4867 goto out_unlock;
4868 seq_puts(m, buf);
4869 seq_putc(m, '\n');
4870 }
4871
4872out_unlock:
4873 mutex_unlock(&cgroup_mutex);
4874 put_task_struct(tsk);
4875out_free:
4876 kfree(buf);
4877out:
4878 return retval;
4879}
4880
Paul Menagea4243162007-10-18 23:39:35 -07004881/* Display information about each subsystem and each hierarchy */
4882static int proc_cgroupstats_show(struct seq_file *m, void *v)
4883{
4884 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004885
Paul Menage8bab8dd2008-04-04 14:29:57 -07004886 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004887 /*
4888 * ideally we don't want subsystems moving around while we do this.
4889 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4890 * subsys/hierarchy state.
4891 */
Paul Menagea4243162007-10-18 23:39:35 -07004892 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004893 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4894 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004895 if (ss == NULL)
4896 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004897 seq_printf(m, "%s\t%d\t%d\t%d\n",
4898 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004899 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004900 }
4901 mutex_unlock(&cgroup_mutex);
4902 return 0;
4903}
4904
4905static int cgroupstats_open(struct inode *inode, struct file *file)
4906{
Al Viro9dce07f2008-03-29 03:07:28 +00004907 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004908}
4909
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004910static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004911 .open = cgroupstats_open,
4912 .read = seq_read,
4913 .llseek = seq_lseek,
4914 .release = single_release,
4915};
4916
Paul Menageb4f48b62007-10-18 23:39:33 -07004917/**
4918 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004919 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004920 *
4921 * Description: A task inherits its parent's cgroup at fork().
4922 *
4923 * A pointer to the shared css_set was automatically copied in
4924 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004925 * it was not made under the protection of RCU or cgroup_mutex, so
4926 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4927 * have already changed current->cgroups, allowing the previously
4928 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004929 *
4930 * At the point that cgroup_fork() is called, 'current' is the parent
4931 * task, and the passed argument 'child' points to the child task.
4932 */
4933void cgroup_fork(struct task_struct *child)
4934{
Tejun Heo9bb71302012-10-18 17:52:07 -07004935 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004936 child->cgroups = current->cgroups;
4937 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004938 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004939 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004940}
4941
4942/**
Li Zefana043e3b2008-02-23 15:24:09 -08004943 * cgroup_post_fork - called on a new task after adding it to the task list
4944 * @child: the task in question
4945 *
Tejun Heo5edee612012-10-16 15:03:14 -07004946 * Adds the task to the list running through its css_set if necessary and
4947 * call the subsystem fork() callbacks. Has to be after the task is
4948 * visible on the task list in case we race with the first call to
4949 * cgroup_iter_start() - to guarantee that the new task ends up on its
4950 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004951 */
Paul Menage817929e2007-10-18 23:39:36 -07004952void cgroup_post_fork(struct task_struct *child)
4953{
Tejun Heo5edee612012-10-16 15:03:14 -07004954 int i;
4955
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004956 /*
4957 * use_task_css_set_links is set to 1 before we walk the tasklist
4958 * under the tasklist_lock and we read it here after we added the child
4959 * to the tasklist under the tasklist_lock as well. If the child wasn't
4960 * yet in the tasklist when we walked through it from
4961 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4962 * should be visible now due to the paired locking and barriers implied
4963 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4964 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4965 * lock on fork.
4966 */
Paul Menage817929e2007-10-18 23:39:36 -07004967 if (use_task_css_set_links) {
4968 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004969 task_lock(child);
4970 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004971 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004972 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004973 write_unlock(&css_set_lock);
4974 }
Tejun Heo5edee612012-10-16 15:03:14 -07004975
4976 /*
4977 * Call ss->fork(). This must happen after @child is linked on
4978 * css_set; otherwise, @child might change state between ->fork()
4979 * and addition to css_set.
4980 */
4981 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004982 /*
4983 * fork/exit callbacks are supported only for builtin
4984 * subsystems, and the builtin section of the subsys
4985 * array is immutable, so we don't need to lock the
4986 * subsys array here. On the other hand, modular section
4987 * of the array can be freed at module unload, so we
4988 * can't touch that.
4989 */
4990 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004991 struct cgroup_subsys *ss = subsys[i];
4992
Tejun Heo5edee612012-10-16 15:03:14 -07004993 if (ss->fork)
4994 ss->fork(child);
4995 }
4996 }
Paul Menage817929e2007-10-18 23:39:36 -07004997}
Tejun Heo5edee612012-10-16 15:03:14 -07004998
Paul Menage817929e2007-10-18 23:39:36 -07004999/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005000 * cgroup_exit - detach cgroup from exiting task
5001 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005002 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005003 *
5004 * Description: Detach cgroup from @tsk and release it.
5005 *
5006 * Note that cgroups marked notify_on_release force every task in
5007 * them to take the global cgroup_mutex mutex when exiting.
5008 * This could impact scaling on very large systems. Be reluctant to
5009 * use notify_on_release cgroups where very high task exit scaling
5010 * is required on large systems.
5011 *
5012 * the_top_cgroup_hack:
5013 *
5014 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5015 *
5016 * We call cgroup_exit() while the task is still competent to
5017 * handle notify_on_release(), then leave the task attached to the
5018 * root cgroup in each hierarchy for the remainder of its exit.
5019 *
5020 * To do this properly, we would increment the reference count on
5021 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5022 * code we would add a second cgroup function call, to drop that
5023 * reference. This would just create an unnecessary hot spot on
5024 * the top_cgroup reference count, to no avail.
5025 *
5026 * Normally, holding a reference to a cgroup without bumping its
5027 * count is unsafe. The cgroup could go away, or someone could
5028 * attach us to a different cgroup, decrementing the count on
5029 * the first cgroup that we never incremented. But in this case,
5030 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005031 * which wards off any cgroup_attach_task() attempts, or task is a failed
5032 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005033 */
5034void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5035{
Tejun Heo5abb8852013-06-12 21:04:49 -07005036 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005037 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005038
5039 /*
5040 * Unlink from the css_set task list if necessary.
5041 * Optimistically check cg_list before taking
5042 * css_set_lock
5043 */
5044 if (!list_empty(&tsk->cg_list)) {
5045 write_lock(&css_set_lock);
5046 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005047 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005048 write_unlock(&css_set_lock);
5049 }
5050
Paul Menageb4f48b62007-10-18 23:39:33 -07005051 /* Reassign the task to the init_css_set. */
5052 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07005053 cset = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07005054 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005055
5056 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005057 /*
5058 * fork/exit callbacks are supported only for builtin
5059 * subsystems, see cgroup_post_fork() for details.
5060 */
5061 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005062 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005063
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005064 if (ss->exit) {
5065 struct cgroup *old_cgrp =
Tejun Heo5abb8852013-06-12 21:04:49 -07005066 rcu_dereference_raw(cset->subsys[i])->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005067 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08005068 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005069 }
5070 }
5071 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005072 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005073
Tejun Heo5abb8852013-06-12 21:04:49 -07005074 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005075}
Paul Menage697f4162007-10-18 23:39:34 -07005076
Paul Menagebd89aab2007-10-18 23:40:44 -07005077static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005078{
Li Zefanf50daa72013-03-01 15:06:07 +08005079 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005080 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005081 /*
5082 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005083 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005084 * it now
5085 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005086 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005087
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005088 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005089 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005090 list_empty(&cgrp->release_list)) {
5091 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005092 need_schedule_work = 1;
5093 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005094 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005095 if (need_schedule_work)
5096 schedule_work(&release_agent_work);
5097 }
5098}
5099
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005100/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005101bool __css_tryget(struct cgroup_subsys_state *css)
5102{
Tejun Heoe9316082012-11-05 09:16:58 -08005103 while (true) {
5104 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005105
Tejun Heoe9316082012-11-05 09:16:58 -08005106 v = css_refcnt(css);
5107 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5108 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005109 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005110 else if (t < 0)
5111 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005112 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005113 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005114}
5115EXPORT_SYMBOL_GPL(__css_tryget);
5116
5117/* Caller must verify that the css is not for root cgroup */
5118void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005119{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005120 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005121
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005122 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08005123 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08005124 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005125}
Ben Blum67523c42010-03-10 15:22:11 -08005126EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005127
5128/*
5129 * Notify userspace when a cgroup is released, by running the
5130 * configured release agent with the name of the cgroup (path
5131 * relative to the root of cgroup file system) as the argument.
5132 *
5133 * Most likely, this user command will try to rmdir this cgroup.
5134 *
5135 * This races with the possibility that some other task will be
5136 * attached to this cgroup before it is removed, or that some other
5137 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5138 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5139 * unused, and this cgroup will be reprieved from its death sentence,
5140 * to continue to serve a useful existence. Next time it's released,
5141 * we will get notified again, if it still has 'notify_on_release' set.
5142 *
5143 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5144 * means only wait until the task is successfully execve()'d. The
5145 * separate release agent task is forked by call_usermodehelper(),
5146 * then control in this thread returns here, without waiting for the
5147 * release agent task. We don't bother to wait because the caller of
5148 * this routine has no use for the exit status of the release agent
5149 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005150 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005151static void cgroup_release_agent(struct work_struct *work)
5152{
5153 BUG_ON(work != &release_agent_work);
5154 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005155 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005156 while (!list_empty(&release_list)) {
5157 char *argv[3], *envp[3];
5158 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005159 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005160 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005161 struct cgroup,
5162 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005163 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005164 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005165 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005166 if (!pathbuf)
5167 goto continue_free;
5168 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5169 goto continue_free;
5170 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5171 if (!agentbuf)
5172 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005173
5174 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005175 argv[i++] = agentbuf;
5176 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005177 argv[i] = NULL;
5178
5179 i = 0;
5180 /* minimal command environment */
5181 envp[i++] = "HOME=/";
5182 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5183 envp[i] = NULL;
5184
5185 /* Drop the lock while we invoke the usermode helper,
5186 * since the exec could involve hitting disk and hence
5187 * be a slow process */
5188 mutex_unlock(&cgroup_mutex);
5189 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005190 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005191 continue_free:
5192 kfree(pathbuf);
5193 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005194 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005195 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005196 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005197 mutex_unlock(&cgroup_mutex);
5198}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005199
5200static int __init cgroup_disable(char *str)
5201{
5202 int i;
5203 char *token;
5204
5205 while ((token = strsep(&str, ",")) != NULL) {
5206 if (!*token)
5207 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005208 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005209 struct cgroup_subsys *ss = subsys[i];
5210
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005211 /*
5212 * cgroup_disable, being at boot time, can't
5213 * know about module subsystems, so we don't
5214 * worry about them.
5215 */
5216 if (!ss || ss->module)
5217 continue;
5218
Paul Menage8bab8dd2008-04-04 14:29:57 -07005219 if (!strcmp(token, ss->name)) {
5220 ss->disabled = 1;
5221 printk(KERN_INFO "Disabling %s control group"
5222 " subsystem\n", ss->name);
5223 break;
5224 }
5225 }
5226 }
5227 return 1;
5228}
5229__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005230
5231/*
5232 * Functons for CSS ID.
5233 */
5234
Tejun Heo54766d42013-06-12 21:04:53 -07005235/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005236unsigned short css_id(struct cgroup_subsys_state *css)
5237{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005238 struct css_id *cssid;
5239
5240 /*
5241 * This css_id() can return correct value when somone has refcnt
5242 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5243 * it's unchanged until freed.
5244 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005245 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005246
5247 if (cssid)
5248 return cssid->id;
5249 return 0;
5250}
Ben Blum67523c42010-03-10 15:22:11 -08005251EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005252
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005253/**
5254 * css_is_ancestor - test "root" css is an ancestor of "child"
5255 * @child: the css to be tested.
5256 * @root: the css supporsed to be an ancestor of the child.
5257 *
5258 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005259 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005260 * But, considering usual usage, the csses should be valid objects after test.
5261 * Assuming that the caller will do some action to the child if this returns
5262 * returns true, the caller must take "child";s reference count.
5263 * If "child" is valid object and this returns true, "root" is valid, too.
5264 */
5265
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005266bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005267 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005268{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005269 struct css_id *child_id;
5270 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005271
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005272 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005273 if (!child_id)
5274 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005275 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005276 if (!root_id)
5277 return false;
5278 if (child_id->depth < root_id->depth)
5279 return false;
5280 if (child_id->stack[root_id->depth] != root_id->id)
5281 return false;
5282 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005283}
5284
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005285void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5286{
5287 struct css_id *id = css->id;
5288 /* When this is called before css_id initialization, id can be NULL */
5289 if (!id)
5290 return;
5291
5292 BUG_ON(!ss->use_id);
5293
5294 rcu_assign_pointer(id->css, NULL);
5295 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005296 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005297 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005298 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005299 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005300}
Ben Blum67523c42010-03-10 15:22:11 -08005301EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005302
5303/*
5304 * This is called by init or create(). Then, calls to this function are
5305 * always serialized (By cgroup_mutex() at create()).
5306 */
5307
5308static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5309{
5310 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005311 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005312
5313 BUG_ON(!ss->use_id);
5314
5315 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5316 newid = kzalloc(size, GFP_KERNEL);
5317 if (!newid)
5318 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005319
5320 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005321 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005322 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005323 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005324 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005325 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005326
5327 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005328 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005329 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005330
Tejun Heod228d9e2013-02-27 17:04:54 -08005331 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005332 newid->depth = depth;
5333 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005334err_out:
5335 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005336 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005337
5338}
5339
Ben Blume6a11052010-03-10 15:22:09 -08005340static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5341 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005342{
5343 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005344
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005345 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005346 idr_init(&ss->idr);
5347
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005348 newid = get_new_cssid(ss, 0);
5349 if (IS_ERR(newid))
5350 return PTR_ERR(newid);
5351
5352 newid->stack[0] = newid->id;
5353 newid->css = rootcss;
5354 rootcss->id = newid;
5355 return 0;
5356}
5357
5358static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5359 struct cgroup *child)
5360{
5361 int subsys_id, i, depth = 0;
5362 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005363 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005364
5365 subsys_id = ss->subsys_id;
5366 parent_css = parent->subsys[subsys_id];
5367 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005368 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005369 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005370
5371 child_id = get_new_cssid(ss, depth);
5372 if (IS_ERR(child_id))
5373 return PTR_ERR(child_id);
5374
5375 for (i = 0; i < depth; i++)
5376 child_id->stack[i] = parent_id->stack[i];
5377 child_id->stack[depth] = child_id->id;
5378 /*
5379 * child_id->css pointer will be set after this cgroup is available
5380 * see cgroup_populate_dir()
5381 */
5382 rcu_assign_pointer(child_css->id, child_id);
5383
5384 return 0;
5385}
5386
5387/**
5388 * css_lookup - lookup css by id
5389 * @ss: cgroup subsys to be looked into.
5390 * @id: the id
5391 *
5392 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5393 * NULL if not. Should be called under rcu_read_lock()
5394 */
5395struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5396{
5397 struct css_id *cssid = NULL;
5398
5399 BUG_ON(!ss->use_id);
5400 cssid = idr_find(&ss->idr, id);
5401
5402 if (unlikely(!cssid))
5403 return NULL;
5404
5405 return rcu_dereference(cssid->css);
5406}
Ben Blum67523c42010-03-10 15:22:11 -08005407EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005408
Stephane Eraniane5d13672011-02-14 11:20:01 +02005409/*
5410 * get corresponding css from file open on cgroupfs directory
5411 */
5412struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5413{
5414 struct cgroup *cgrp;
5415 struct inode *inode;
5416 struct cgroup_subsys_state *css;
5417
Al Viro496ad9a2013-01-23 17:07:38 -05005418 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005419 /* check in cgroup filesystem dir */
5420 if (inode->i_op != &cgroup_dir_inode_operations)
5421 return ERR_PTR(-EBADF);
5422
5423 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5424 return ERR_PTR(-EINVAL);
5425
5426 /* get cgroup */
5427 cgrp = __d_cgrp(f->f_dentry);
5428 css = cgrp->subsys[id];
5429 return css ? css : ERR_PTR(-ENOENT);
5430}
5431
Paul Menagefe693432009-09-23 15:56:20 -07005432#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005433static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005434{
5435 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5436
5437 if (!css)
5438 return ERR_PTR(-ENOMEM);
5439
5440 return css;
5441}
5442
Tejun Heo92fb9742012-11-19 08:13:38 -08005443static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005444{
5445 kfree(cont->subsys[debug_subsys_id]);
5446}
5447
Paul Menagefe693432009-09-23 15:56:20 -07005448static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5449{
5450 return cgroup_task_count(cont);
5451}
5452
5453static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5454{
5455 return (u64)(unsigned long)current->cgroups;
5456}
5457
5458static u64 current_css_set_refcount_read(struct cgroup *cont,
5459 struct cftype *cft)
5460{
5461 u64 count;
5462
5463 rcu_read_lock();
5464 count = atomic_read(&current->cgroups->refcount);
5465 rcu_read_unlock();
5466 return count;
5467}
5468
Paul Menage7717f7b2009-09-23 15:56:22 -07005469static int current_css_set_cg_links_read(struct cgroup *cont,
5470 struct cftype *cft,
5471 struct seq_file *seq)
5472{
Tejun Heo69d02062013-06-12 21:04:50 -07005473 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005474 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005475
5476 read_lock(&css_set_lock);
5477 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005478 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005479 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005480 struct cgroup *c = link->cgrp;
5481 const char *name;
5482
5483 if (c->dentry)
5484 name = c->dentry->d_name.name;
5485 else
5486 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005487 seq_printf(seq, "Root %d group %s\n",
5488 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005489 }
5490 rcu_read_unlock();
5491 read_unlock(&css_set_lock);
5492 return 0;
5493}
5494
5495#define MAX_TASKS_SHOWN_PER_CSS 25
5496static int cgroup_css_links_read(struct cgroup *cont,
5497 struct cftype *cft,
5498 struct seq_file *seq)
5499{
Tejun Heo69d02062013-06-12 21:04:50 -07005500 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005501
5502 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07005503 list_for_each_entry(link, &cont->cset_links, cset_link) {
5504 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005505 struct task_struct *task;
5506 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005507 seq_printf(seq, "css_set %p\n", cset);
5508 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005509 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5510 seq_puts(seq, " ...\n");
5511 break;
5512 } else {
5513 seq_printf(seq, " task %d\n",
5514 task_pid_vnr(task));
5515 }
5516 }
5517 }
5518 read_unlock(&css_set_lock);
5519 return 0;
5520}
5521
Paul Menagefe693432009-09-23 15:56:20 -07005522static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5523{
5524 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5525}
5526
5527static struct cftype debug_files[] = {
5528 {
Paul Menagefe693432009-09-23 15:56:20 -07005529 .name = "taskcount",
5530 .read_u64 = debug_taskcount_read,
5531 },
5532
5533 {
5534 .name = "current_css_set",
5535 .read_u64 = current_css_set_read,
5536 },
5537
5538 {
5539 .name = "current_css_set_refcount",
5540 .read_u64 = current_css_set_refcount_read,
5541 },
5542
5543 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005544 .name = "current_css_set_cg_links",
5545 .read_seq_string = current_css_set_cg_links_read,
5546 },
5547
5548 {
5549 .name = "cgroup_css_links",
5550 .read_seq_string = cgroup_css_links_read,
5551 },
5552
5553 {
Paul Menagefe693432009-09-23 15:56:20 -07005554 .name = "releasable",
5555 .read_u64 = releasable_read,
5556 },
Paul Menagefe693432009-09-23 15:56:20 -07005557
Tejun Heo4baf6e32012-04-01 12:09:55 -07005558 { } /* terminate */
5559};
Paul Menagefe693432009-09-23 15:56:20 -07005560
5561struct cgroup_subsys debug_subsys = {
5562 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005563 .css_alloc = debug_css_alloc,
5564 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005565 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005566 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005567};
5568#endif /* CONFIG_CGROUP_DEBUG */