blob: 921b1387c944d562c8c6dd7bf885d07e44b991e2 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040084EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070085#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -070099static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroupfs_root cgroup_dummy_root;
109
110/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Tejun Heo105347b2013-08-13 11:01:55 -0400120 struct cgroup_subsys_state *css;
Li Zefan712317a2013-04-18 23:09:52 -0700121
122 /* file xattrs */
123 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700124};
125
126/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700127 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
128 * cgroup_subsys->use_id != 0.
129 */
130#define CSS_ID_MAX (65535)
131struct css_id {
132 /*
133 * The css to which this ID points. This pointer is set to valid value
134 * after cgroup is populated. If cgroup is removed, this will be NULL.
135 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800136 * is called after synchronize_rcu(). But for safe use, css_tryget()
137 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700138 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100139 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700140 /*
141 * ID of this css.
142 */
143 unsigned short id;
144 /*
145 * Depth in hierarchy which this ID belongs to.
146 */
147 unsigned short depth;
148 /*
149 * ID is freed by RCU. (and lookup routine is RCU safe.)
150 */
151 struct rcu_head rcu_head;
152 /*
153 * Hierarchy of CSS ID belongs to.
154 */
155 unsigned short stack[0]; /* Array of Length (depth+1) */
156};
157
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800158/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300159 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800160 */
161struct cgroup_event {
162 /*
Tejun Heo81eeaf02013-08-08 20:11:26 -0400163 * css which the event belongs to.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800164 */
Tejun Heo81eeaf02013-08-08 20:11:26 -0400165 struct cgroup_subsys_state *css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800166 /*
167 * Control file which the event associated.
168 */
169 struct cftype *cft;
170 /*
171 * eventfd to signal userspace about the event.
172 */
173 struct eventfd_ctx *eventfd;
174 /*
175 * Each of these stored in a list by the cgroup.
176 */
177 struct list_head list;
178 /*
179 * All fields below needed to unregister event when
180 * userspace closes eventfd.
181 */
182 poll_table pt;
183 wait_queue_head_t *wqh;
184 wait_queue_t wait;
185 struct work_struct remove;
186};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700187
Paul Menageddbcc7e2007-10-18 23:39:30 -0700188/* The list of hierarchy roots */
189
Tejun Heo9871bf92013-06-24 15:21:47 -0700190static LIST_HEAD(cgroup_roots);
191static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700192
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700193/*
194 * Hierarchy ID allocation and mapping. It follows the same exclusion
195 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
196 * writes, either for reads.
197 */
Tejun Heo1a574232013-04-14 11:36:58 -0700198static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700199
Li Zefan65dff752013-03-01 15:01:56 +0800200static struct cgroup_name root_cgroup_name = { .name = "/" };
201
Li Zefan794611a2013-06-18 18:53:53 +0800202/*
203 * Assign a monotonically increasing serial number to cgroups. It
204 * guarantees cgroups with bigger numbers are newer than those with smaller
205 * numbers. Also, as cgroups are always appended to the parent's
206 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700207 * the ascending serial number order on the list. Protected by
208 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800209 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700210static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800211
Paul Menageddbcc7e2007-10-18 23:39:30 -0700212/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800213 * check for fork/exit handlers to call. This avoids us having to do
214 * extra work in the fork/exit path if none of the subsystems need to
215 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700216 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700217static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700218
Tejun Heo628f7cd2013-06-28 16:24:11 -0700219static struct cftype cgroup_base_files[];
220
Tejun Heof20104d2013-08-13 20:22:50 -0400221static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800222static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400223static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
224 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800225
Tejun Heo95109b62013-08-08 20:11:27 -0400226/**
227 * cgroup_css - obtain a cgroup's css for the specified subsystem
228 * @cgrp: the cgroup of interest
229 * @subsys_id: the subsystem of interest
230 *
231 * Return @cgrp's css (cgroup_subsys_state) associated with @subsys_id.
Tejun Heo73e80ed2013-08-13 11:01:55 -0400232 * This function must be called either under cgroup_mutex or
233 * rcu_read_lock() and the caller is responsible for pinning the returned
234 * css if it wants to keep accessing it outside the said locks. This
235 * function may return %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400236 */
237static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
238 int subsys_id)
239{
Tejun Heo73e80ed2013-08-13 11:01:55 -0400240 return rcu_dereference_check(cgrp->subsys[subsys_id],
241 lockdep_is_held(&cgroup_mutex));
Tejun Heo95109b62013-08-08 20:11:27 -0400242}
243
Paul Menageddbcc7e2007-10-18 23:39:30 -0700244/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700245static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700246{
Tejun Heo54766d42013-06-12 21:04:53 -0700247 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700248}
249
Li Zefan78574cf2013-04-08 19:00:38 -0700250/**
251 * cgroup_is_descendant - test ancestry
252 * @cgrp: the cgroup to be tested
253 * @ancestor: possible ancestor of @cgrp
254 *
255 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
256 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
257 * and @ancestor are accessible.
258 */
259bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
260{
261 while (cgrp) {
262 if (cgrp == ancestor)
263 return true;
264 cgrp = cgrp->parent;
265 }
266 return false;
267}
268EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700269
Adrian Bunke9685a02008-02-07 00:13:46 -0800270static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700271{
272 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700273 (1 << CGRP_RELEASABLE) |
274 (1 << CGRP_NOTIFY_ON_RELEASE);
275 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700276}
277
Adrian Bunke9685a02008-02-07 00:13:46 -0800278static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700279{
Paul Menagebd89aab2007-10-18 23:40:44 -0700280 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700281}
282
Tejun Heo30159ec2013-06-25 11:53:37 -0700283/**
284 * for_each_subsys - iterate all loaded cgroup subsystems
285 * @ss: the iteration cursor
286 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
287 *
288 * Should be called under cgroup_mutex.
289 */
290#define for_each_subsys(ss, i) \
291 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
292 if (({ lockdep_assert_held(&cgroup_mutex); \
293 !((ss) = cgroup_subsys[i]); })) { } \
294 else
295
296/**
297 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
298 * @ss: the iteration cursor
299 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
300 *
301 * Bulit-in subsystems are always present and iteration itself doesn't
302 * require any synchronization.
303 */
304#define for_each_builtin_subsys(ss, i) \
305 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
306 (((ss) = cgroup_subsys[i]) || true); (i)++)
307
Tejun Heo5549c492013-06-24 15:21:48 -0700308/* iterate each subsystem attached to a hierarchy */
309#define for_each_root_subsys(root, ss) \
310 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700311
Tejun Heo5549c492013-06-24 15:21:48 -0700312/* iterate across the active hierarchies */
313#define for_each_active_root(root) \
314 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700315
Tejun Heof6ea9372012-04-01 12:09:55 -0700316static inline struct cgroup *__d_cgrp(struct dentry *dentry)
317{
318 return dentry->d_fsdata;
319}
320
Tejun Heo05ef1d72012-04-01 12:09:56 -0700321static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700322{
323 return dentry->d_fsdata;
324}
325
Tejun Heo05ef1d72012-04-01 12:09:56 -0700326static inline struct cftype *__d_cft(struct dentry *dentry)
327{
328 return __d_cfe(dentry)->type;
329}
330
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700331/**
332 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
333 * @cgrp: the cgroup to be checked for liveness
334 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700335 * On success, returns true; the mutex should be later unlocked. On
336 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700337 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700338static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700339{
340 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700341 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700342 mutex_unlock(&cgroup_mutex);
343 return false;
344 }
345 return true;
346}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700347
Paul Menage81a6a5c2007-10-18 23:39:38 -0700348/* the list of cgroups eligible for automatic release. Protected by
349 * release_list_lock */
350static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200351static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700352static void cgroup_release_agent(struct work_struct *work);
353static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700354static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700355
Tejun Heo69d02062013-06-12 21:04:50 -0700356/*
357 * A cgroup can be associated with multiple css_sets as different tasks may
358 * belong to different cgroups on different hierarchies. In the other
359 * direction, a css_set is naturally associated with multiple cgroups.
360 * This M:N relationship is represented by the following link structure
361 * which exists for each association and allows traversing the associations
362 * from both sides.
363 */
364struct cgrp_cset_link {
365 /* the cgroup and css_set this link associates */
366 struct cgroup *cgrp;
367 struct css_set *cset;
368
369 /* list of cgrp_cset_links anchored at cgrp->cset_links */
370 struct list_head cset_link;
371
372 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
373 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700374};
375
376/* The default css_set - used by init and its children prior to any
377 * hierarchies being mounted. It contains a pointer to the root state
378 * for each subsystem. Also used to anchor the list of css_sets. Not
379 * reference-counted, to improve performance when child cgroups
380 * haven't been created.
381 */
382
383static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700384static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700385
Ben Blume6a11052010-03-10 15:22:09 -0800386static int cgroup_init_idr(struct cgroup_subsys *ss,
387 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700388
Tejun Heo0942eee2013-08-08 20:11:26 -0400389/*
390 * css_set_lock protects the list of css_set objects, and the chain of
391 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400392 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400393 */
Paul Menage817929e2007-10-18 23:39:36 -0700394static DEFINE_RWLOCK(css_set_lock);
395static int css_set_count;
396
Paul Menage7717f7b2009-09-23 15:56:22 -0700397/*
398 * hash table for cgroup groups. This improves the performance to find
399 * an existing css_set. This hash doesn't (currently) take into
400 * account cgroups in empty hierarchies.
401 */
Li Zefan472b1052008-04-29 01:00:11 -0700402#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800403static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700404
Li Zefan0ac801f2013-01-10 11:49:27 +0800405static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700406{
Li Zefan0ac801f2013-01-10 11:49:27 +0800407 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700408 struct cgroup_subsys *ss;
409 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700410
Tejun Heo30159ec2013-06-25 11:53:37 -0700411 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800412 key += (unsigned long)css[i];
413 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700414
Li Zefan0ac801f2013-01-10 11:49:27 +0800415 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700416}
417
Tejun Heo0942eee2013-08-08 20:11:26 -0400418/*
419 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400420 * until after the first call to css_task_iter_start(). This reduces the
421 * fork()/exit() overhead for people who have cgroups compiled into their
422 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400423 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700424static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700425
Tejun Heo5abb8852013-06-12 21:04:49 -0700426static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700427{
Tejun Heo69d02062013-06-12 21:04:50 -0700428 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700429
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700430 /*
431 * Ensure that the refcount doesn't hit zero while any readers
432 * can see it. Similar to atomic_dec_and_lock(), but for an
433 * rwlock
434 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700435 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700436 return;
437 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700438 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700439 write_unlock(&css_set_lock);
440 return;
441 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700442
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700443 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700444 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700445 css_set_count--;
446
Tejun Heo69d02062013-06-12 21:04:50 -0700447 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700448 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700449
Tejun Heo69d02062013-06-12 21:04:50 -0700450 list_del(&link->cset_link);
451 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800452
Tejun Heoddd69142013-06-12 21:04:54 -0700453 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700454 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700455 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700456 set_bit(CGRP_RELEASABLE, &cgrp->flags);
457 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700459
460 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700461 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700462
463 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700464 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700465}
466
467/*
468 * refcounted get/put for css_set objects
469 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700470static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700471{
Tejun Heo5abb8852013-06-12 21:04:49 -0700472 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700473}
474
Tejun Heo5abb8852013-06-12 21:04:49 -0700475static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700476{
Tejun Heo5abb8852013-06-12 21:04:49 -0700477 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700478}
479
Tejun Heo5abb8852013-06-12 21:04:49 -0700480static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700481{
Tejun Heo5abb8852013-06-12 21:04:49 -0700482 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700483}
484
Tejun Heob326f9d2013-06-24 15:21:48 -0700485/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700486 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700487 * @cset: candidate css_set being tested
488 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700489 * @new_cgrp: cgroup that's being entered by the task
490 * @template: desired set of css pointers in css_set (pre-calculated)
491 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800492 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700493 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
494 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700495static bool compare_css_sets(struct css_set *cset,
496 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700497 struct cgroup *new_cgrp,
498 struct cgroup_subsys_state *template[])
499{
500 struct list_head *l1, *l2;
501
Tejun Heo5abb8852013-06-12 21:04:49 -0700502 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700503 /* Not all subsystems matched */
504 return false;
505 }
506
507 /*
508 * Compare cgroup pointers in order to distinguish between
509 * different cgroups in heirarchies with no subsystems. We
510 * could get by with just this check alone (and skip the
511 * memcmp above) but on most setups the memcmp check will
512 * avoid the need for this more expensive check on almost all
513 * candidates.
514 */
515
Tejun Heo69d02062013-06-12 21:04:50 -0700516 l1 = &cset->cgrp_links;
517 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700518 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700519 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700520 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700521
522 l1 = l1->next;
523 l2 = l2->next;
524 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700525 if (l1 == &cset->cgrp_links) {
526 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700527 break;
528 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700529 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700530 }
531 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700532 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
533 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
534 cgrp1 = link1->cgrp;
535 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700536 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700537 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700538
539 /*
540 * If this hierarchy is the hierarchy of the cgroup
541 * that's changing, then we need to check that this
542 * css_set points to the new cgroup; if it's any other
543 * hierarchy, then this css_set should point to the
544 * same cgroup as the old css_set.
545 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700546 if (cgrp1->root == new_cgrp->root) {
547 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700548 return false;
549 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700551 return false;
552 }
553 }
554 return true;
555}
556
Tejun Heob326f9d2013-06-24 15:21:48 -0700557/**
558 * find_existing_css_set - init css array and find the matching css_set
559 * @old_cset: the css_set that we're using before the cgroup transition
560 * @cgrp: the cgroup that we're moving into
561 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700562 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700563static struct css_set *find_existing_css_set(struct css_set *old_cset,
564 struct cgroup *cgrp,
565 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700566{
Paul Menagebd89aab2007-10-18 23:40:44 -0700567 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700568 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700569 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800570 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700571 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700572
Ben Blumaae8aab2010-03-10 15:22:07 -0800573 /*
574 * Build the set of subsystem state objects that we want to see in the
575 * new css_set. while subsystems can change globally, the entries here
576 * won't change, so no need for locking.
577 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700578 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400579 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700580 /* Subsystem is in this hierarchy. So we want
581 * the subsystem state from the new
582 * cgroup */
Tejun Heo40e93b32013-08-13 11:01:53 -0400583 template[i] = cgroup_css(cgrp, i);
Paul Menage817929e2007-10-18 23:39:36 -0700584 } else {
585 /* Subsystem is not in this hierarchy, so we
586 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700587 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700588 }
589 }
590
Li Zefan0ac801f2013-01-10 11:49:27 +0800591 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700592 hash_for_each_possible(css_set_table, cset, hlist, key) {
593 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700594 continue;
595
596 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700597 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700598 }
Paul Menage817929e2007-10-18 23:39:36 -0700599
600 /* No existing cgroup group matched */
601 return NULL;
602}
603
Tejun Heo69d02062013-06-12 21:04:50 -0700604static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700605{
Tejun Heo69d02062013-06-12 21:04:50 -0700606 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700607
Tejun Heo69d02062013-06-12 21:04:50 -0700608 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
609 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700610 kfree(link);
611 }
612}
613
Tejun Heo69d02062013-06-12 21:04:50 -0700614/**
615 * allocate_cgrp_cset_links - allocate cgrp_cset_links
616 * @count: the number of links to allocate
617 * @tmp_links: list_head the allocated links are put on
618 *
619 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
620 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700621 */
Tejun Heo69d02062013-06-12 21:04:50 -0700622static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700623{
Tejun Heo69d02062013-06-12 21:04:50 -0700624 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700625 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700626
627 INIT_LIST_HEAD(tmp_links);
628
Li Zefan36553432008-07-29 22:33:19 -0700629 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700630 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700631 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700632 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700633 return -ENOMEM;
634 }
Tejun Heo69d02062013-06-12 21:04:50 -0700635 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700636 }
637 return 0;
638}
639
Li Zefanc12f65d2009-01-07 18:07:42 -0800640/**
641 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700642 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700643 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800644 * @cgrp: the destination cgroup
645 */
Tejun Heo69d02062013-06-12 21:04:50 -0700646static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
647 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800648{
Tejun Heo69d02062013-06-12 21:04:50 -0700649 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800650
Tejun Heo69d02062013-06-12 21:04:50 -0700651 BUG_ON(list_empty(tmp_links));
652 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
653 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700654 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700655 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700656 /*
657 * Always add links to the tail of the list so that the list
658 * is sorted by order of hierarchy creation
659 */
Tejun Heo69d02062013-06-12 21:04:50 -0700660 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800661}
662
Tejun Heob326f9d2013-06-24 15:21:48 -0700663/**
664 * find_css_set - return a new css_set with one cgroup updated
665 * @old_cset: the baseline css_set
666 * @cgrp: the cgroup to be updated
667 *
668 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
669 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700670 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700671static struct css_set *find_css_set(struct css_set *old_cset,
672 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700673{
Tejun Heob326f9d2013-06-24 15:21:48 -0700674 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700675 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700676 struct list_head tmp_links;
677 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800678 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700679
Tejun Heob326f9d2013-06-24 15:21:48 -0700680 lockdep_assert_held(&cgroup_mutex);
681
Paul Menage817929e2007-10-18 23:39:36 -0700682 /* First see if we already have a cgroup group that matches
683 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700684 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700685 cset = find_existing_css_set(old_cset, cgrp, template);
686 if (cset)
687 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700688 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700689
Tejun Heo5abb8852013-06-12 21:04:49 -0700690 if (cset)
691 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700692
Tejun Heof4f4be22013-06-12 21:04:51 -0700693 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700694 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700695 return NULL;
696
Tejun Heo69d02062013-06-12 21:04:50 -0700697 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700698 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700699 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700700 return NULL;
701 }
702
Tejun Heo5abb8852013-06-12 21:04:49 -0700703 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700704 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700705 INIT_LIST_HEAD(&cset->tasks);
706 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700707
708 /* Copy the set of subsystem state objects generated in
709 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700710 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700711
712 write_lock(&css_set_lock);
713 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700714 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700715 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700716
Paul Menage7717f7b2009-09-23 15:56:22 -0700717 if (c->root == cgrp->root)
718 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700719 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700720 }
Paul Menage817929e2007-10-18 23:39:36 -0700721
Tejun Heo69d02062013-06-12 21:04:50 -0700722 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700723
Paul Menage817929e2007-10-18 23:39:36 -0700724 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700725
726 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700727 key = css_set_hash(cset->subsys);
728 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700729
Paul Menage817929e2007-10-18 23:39:36 -0700730 write_unlock(&css_set_lock);
731
Tejun Heo5abb8852013-06-12 21:04:49 -0700732 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700733}
734
Paul Menageddbcc7e2007-10-18 23:39:30 -0700735/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700736 * Return the cgroup for "task" from the given hierarchy. Must be
737 * called with cgroup_mutex held.
738 */
739static struct cgroup *task_cgroup_from_root(struct task_struct *task,
740 struct cgroupfs_root *root)
741{
Tejun Heo5abb8852013-06-12 21:04:49 -0700742 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700743 struct cgroup *res = NULL;
744
745 BUG_ON(!mutex_is_locked(&cgroup_mutex));
746 read_lock(&css_set_lock);
747 /*
748 * No need to lock the task - since we hold cgroup_mutex the
749 * task can't change groups, so the only thing that can happen
750 * is that it exits and its css is set back to init_css_set.
751 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700752 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700753 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700754 res = &root->top_cgroup;
755 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700756 struct cgrp_cset_link *link;
757
758 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700759 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700760
Paul Menage7717f7b2009-09-23 15:56:22 -0700761 if (c->root == root) {
762 res = c;
763 break;
764 }
765 }
766 }
767 read_unlock(&css_set_lock);
768 BUG_ON(!res);
769 return res;
770}
771
772/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 * There is one global cgroup mutex. We also require taking
774 * task_lock() when dereferencing a task's cgroup subsys pointers.
775 * See "The task_lock() exception", at the end of this comment.
776 *
777 * A task must hold cgroup_mutex to modify cgroups.
778 *
779 * Any task can increment and decrement the count field without lock.
780 * So in general, code holding cgroup_mutex can't rely on the count
781 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800782 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783 * means that no tasks are currently attached, therefore there is no
784 * way a task attached to that cgroup can fork (the other way to
785 * increment the count). So code holding cgroup_mutex can safely
786 * assume that if the count is zero, it will stay zero. Similarly, if
787 * a task holds cgroup_mutex on a cgroup with zero count, it
788 * knows that the cgroup won't be removed, as cgroup_rmdir()
789 * needs that mutex.
790 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
792 * (usually) take cgroup_mutex. These are the two most performance
793 * critical pieces of code here. The exception occurs on cgroup_exit(),
794 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
795 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800796 * to the release agent with the name of the cgroup (path relative to
797 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798 *
799 * A cgroup can only be deleted if both its 'count' of using tasks
800 * is zero, and its list of 'children' cgroups is empty. Since all
801 * tasks in the system use _some_ cgroup, and since there is always at
802 * least one task in the system (init, pid == 1), therefore, top_cgroup
803 * always has either children cgroups and/or using tasks. So we don't
804 * need a special hack to ensure that top_cgroup cannot be deleted.
805 *
806 * The task_lock() exception
807 *
808 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800809 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800810 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700811 * several performance critical places that need to reference
812 * task->cgroup without the expense of grabbing a system global
813 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800814 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
816 * the task_struct routinely used for such matters.
817 *
818 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800819 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820 */
821
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822/*
823 * A couple of forward declarations required, due to cyclic reference loop:
824 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
825 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
826 * -> cgroup_mkdir.
827 */
828
Al Viro18bb1db2011-07-26 01:41:39 -0400829static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400830static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700831static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700832static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700833static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700834static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700835
836static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200837 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700838 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700839};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700840
Tejun Heo623f9262013-08-13 11:01:55 -0400841static int alloc_css_id(struct cgroup_subsys_state *child_css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700842
Al Viroa5e7ed32011-07-26 01:55:55 -0400843static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700844{
845 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846
847 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400848 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100850 inode->i_uid = current_fsuid();
851 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700852 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
853 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
854 }
855 return inode;
856}
857
Li Zefan65dff752013-03-01 15:01:56 +0800858static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
859{
860 struct cgroup_name *name;
861
862 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
863 if (!name)
864 return NULL;
865 strcpy(name->name, dentry->d_name.name);
866 return name;
867}
868
Li Zefanbe445622013-01-24 14:31:42 +0800869static void cgroup_free_fn(struct work_struct *work)
870{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700871 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800872
873 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800874 cgrp->root->number_of_cgroups--;
875 mutex_unlock(&cgroup_mutex);
876
877 /*
Li Zefan415cf072013-04-08 14:35:02 +0800878 * We get a ref to the parent's dentry, and put the ref when
879 * this cgroup is being freed, so it's guaranteed that the
880 * parent won't be destroyed before its children.
881 */
882 dput(cgrp->parent->dentry);
883
884 /*
Li Zefanbe445622013-01-24 14:31:42 +0800885 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700886 * created the cgroup. This will free cgrp->root, if we are
887 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800888 */
889 deactivate_super(cgrp->root->sb);
890
891 /*
892 * if we're getting rid of the cgroup, refcount should ensure
893 * that there are no pidlists left.
894 */
895 BUG_ON(!list_empty(&cgrp->pidlists));
896
897 simple_xattrs_free(&cgrp->xattrs);
898
Li Zefan65dff752013-03-01 15:01:56 +0800899 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800900 kfree(cgrp);
901}
902
903static void cgroup_free_rcu(struct rcu_head *head)
904{
905 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
906
Tejun Heoea15f8c2013-06-13 19:27:42 -0700907 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
908 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800909}
910
Paul Menageddbcc7e2007-10-18 23:39:30 -0700911static void cgroup_diput(struct dentry *dentry, struct inode *inode)
912{
913 /* is dentry a directory ? if so, kfree() associated cgroup */
914 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700915 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800916
Tejun Heo54766d42013-06-12 21:04:53 -0700917 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800918 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700919 } else {
920 struct cfent *cfe = __d_cfe(dentry);
921 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
922
923 WARN_ONCE(!list_empty(&cfe->node) &&
924 cgrp != &cgrp->root->top_cgroup,
925 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700926 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700927 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700928 }
929 iput(inode);
930}
931
Al Viroc72a04e2011-01-14 05:31:45 +0000932static int cgroup_delete(const struct dentry *d)
933{
934 return 1;
935}
936
Paul Menageddbcc7e2007-10-18 23:39:30 -0700937static void remove_dir(struct dentry *d)
938{
939 struct dentry *parent = dget(d->d_parent);
940
941 d_delete(d);
942 simple_rmdir(parent->d_inode, d);
943 dput(parent);
944}
945
Li Zefan2739d3c2013-01-21 18:18:33 +0800946static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700948 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700949
Tejun Heo05ef1d72012-04-01 12:09:56 -0700950 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
951 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100952
Li Zefan2739d3c2013-01-21 18:18:33 +0800953 /*
954 * If we're doing cleanup due to failure of cgroup_create(),
955 * the corresponding @cfe may not exist.
956 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700957 list_for_each_entry(cfe, &cgrp->files, node) {
958 struct dentry *d = cfe->dentry;
959
960 if (cft && cfe->type != cft)
961 continue;
962
963 dget(d);
964 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700965 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700966 list_del_init(&cfe->node);
967 dput(d);
968
Li Zefan2739d3c2013-01-21 18:18:33 +0800969 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700971}
972
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400973/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700974 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700975 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976 * @subsys_mask: mask of the subsystem ids whose files should be removed
977 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700978static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700979{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400980 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700981 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700982
Tejun Heob420ba72013-07-12 12:34:02 -0700983 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400984 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700985
986 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400987 continue;
988 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400989 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400990 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991}
992
993/*
994 * NOTE : the dentry must have been dget()'ed
995 */
996static void cgroup_d_remove_dir(struct dentry *dentry)
997{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100998 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001000 parent = dentry->d_parent;
1001 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001002 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001004 spin_unlock(&dentry->d_lock);
1005 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 remove_dir(dentry);
1007}
1008
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001009/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001010 * Call with cgroup_mutex held. Drops reference counts on modules, including
1011 * any duplicate ones that parse_cgroupfs_options took. If this function
1012 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001013 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001015 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016{
Paul Menagebd89aab2007-10-18 23:40:44 -07001017 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001018 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001019 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001020 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001021
Ben Blumaae8aab2010-03-10 15:22:07 -08001022 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001023 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001024
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001026 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001027 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001029
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001030 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001031 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001032 ret = -EBUSY;
1033 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001035
1036 /* pin the module */
1037 if (!try_module_get(ss->module)) {
1038 ret = -ENOENT;
1039 goto out_put;
1040 }
1041 pinned |= 1 << i;
1042 }
1043
1044 /* subsys could be missing if unloaded between parsing and here */
1045 if (added_mask != pinned) {
1046 ret = -ENOENT;
1047 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 }
1049
Tejun Heo31261212013-06-28 17:07:30 -07001050 ret = cgroup_populate_dir(cgrp, added_mask);
1051 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001052 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001053
1054 /*
1055 * Nothing can fail from this point on. Remove files for the
1056 * removed subsystems and rebind each subsystem.
1057 */
1058 cgroup_clear_dir(cgrp, removed_mask);
1059
Tejun Heo30159ec2013-06-25 11:53:37 -07001060 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001062
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001063 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064 /* We're binding this subsystem to this hierarchy */
Tejun Heo40e93b32013-08-13 11:01:53 -04001065 BUG_ON(cgroup_css(cgrp, i));
1066 BUG_ON(!cgroup_css(cgroup_dummy_top, i));
1067 BUG_ON(cgroup_css(cgroup_dummy_top, i)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001068
Tejun Heo73e80ed2013-08-13 11:01:55 -04001069 rcu_assign_pointer(cgrp->subsys[i],
1070 cgroup_css(cgroup_dummy_top, i));
Tejun Heo40e93b32013-08-13 11:01:53 -04001071 cgroup_css(cgrp, i)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001072
Li Zefan33a68ac2009-01-07 18:07:42 -08001073 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001074 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 if (ss->bind)
Tejun Heo40e93b32013-08-13 11:01:53 -04001076 ss->bind(cgroup_css(cgrp, i));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001077
Ben Blumcf5d5942010-03-10 15:22:09 -08001078 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001079 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001080 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 /* We're removing this subsystem */
Tejun Heo40e93b32013-08-13 11:01:53 -04001082 BUG_ON(cgroup_css(cgrp, i) != cgroup_css(cgroup_dummy_top, i));
1083 BUG_ON(cgroup_css(cgrp, i)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001084
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 if (ss->bind)
Tejun Heo40e93b32013-08-13 11:01:53 -04001086 ss->bind(cgroup_css(cgroup_dummy_top, i));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001087
Tejun Heo40e93b32013-08-13 11:01:53 -04001088 cgroup_css(cgroup_dummy_top, i)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001089 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1090
Tejun Heo9871bf92013-06-24 15:21:47 -07001091 cgroup_subsys[i]->root = &cgroup_dummy_root;
1092 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001093
Ben Blumcf5d5942010-03-10 15:22:09 -08001094 /* subsystem is now free - drop reference on module */
1095 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001096 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 }
1098 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099
Tejun Heo1672d042013-06-25 18:04:54 -07001100 /*
1101 * Mark @root has finished binding subsystems. @root->subsys_mask
1102 * now matches the bound subsystems.
1103 */
1104 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1105
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001107
1108out_put:
1109 for_each_subsys(ss, i)
1110 if (pinned & (1 << i))
1111 module_put(ss->module);
1112 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001113}
1114
Al Viro34c80b12011-12-08 21:32:45 -05001115static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116{
Al Viro34c80b12011-12-08 21:32:45 -05001117 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118 struct cgroup_subsys *ss;
1119
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001120 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001121 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001123 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1124 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001125 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001127 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001128 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001129 if (strlen(root->release_agent_path))
1130 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001131 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001132 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001133 if (strlen(root->name))
1134 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001135 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136 return 0;
1137}
1138
1139struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001140 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001142 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001143 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001144 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001145 /* User explicitly requested empty subsystem */
1146 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001147
1148 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001149
Paul Menageddbcc7e2007-10-18 23:39:30 -07001150};
1151
Ben Blumaae8aab2010-03-10 15:22:07 -08001152/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001153 * Convert a hierarchy specifier into a bitmask of subsystems and
1154 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1155 * array. This function takes refcounts on subsystems to be used, unless it
1156 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001157 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001158static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001160 char *token, *o = data;
1161 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001162 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001163 struct cgroup_subsys *ss;
1164 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001165
Ben Blumaae8aab2010-03-10 15:22:07 -08001166 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1167
Li Zefanf9ab5b52009-06-17 16:26:33 -07001168#ifdef CONFIG_CPUSETS
1169 mask = ~(1UL << cpuset_subsys_id);
1170#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001171
Paul Menagec6d57f32009-09-23 15:56:19 -07001172 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001173
1174 while ((token = strsep(&o, ",")) != NULL) {
1175 if (!*token)
1176 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001178 /* Explicitly have no subsystems */
1179 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001180 continue;
1181 }
1182 if (!strcmp(token, "all")) {
1183 /* Mutually exclusive option 'all' + subsystem name */
1184 if (one_ss)
1185 return -EINVAL;
1186 all_ss = true;
1187 continue;
1188 }
Tejun Heo873fe092013-04-14 20:15:26 -07001189 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1190 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1191 continue;
1192 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001193 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001194 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001195 continue;
1196 }
1197 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001198 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001199 continue;
1200 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001201 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001202 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001203 continue;
1204 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001205 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001206 /* Specifying two release agents is forbidden */
1207 if (opts->release_agent)
1208 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001209 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001210 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001211 if (!opts->release_agent)
1212 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001213 continue;
1214 }
1215 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001216 const char *name = token + 5;
1217 /* Can't specify an empty name */
1218 if (!strlen(name))
1219 return -EINVAL;
1220 /* Must match [\w.-]+ */
1221 for (i = 0; i < strlen(name); i++) {
1222 char c = name[i];
1223 if (isalnum(c))
1224 continue;
1225 if ((c == '.') || (c == '-') || (c == '_'))
1226 continue;
1227 return -EINVAL;
1228 }
1229 /* Specifying two names is forbidden */
1230 if (opts->name)
1231 return -EINVAL;
1232 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001233 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001234 GFP_KERNEL);
1235 if (!opts->name)
1236 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001237
1238 continue;
1239 }
1240
Tejun Heo30159ec2013-06-25 11:53:37 -07001241 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001242 if (strcmp(token, ss->name))
1243 continue;
1244 if (ss->disabled)
1245 continue;
1246
1247 /* Mutually exclusive option 'all' + subsystem name */
1248 if (all_ss)
1249 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001250 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001251 one_ss = true;
1252
1253 break;
1254 }
1255 if (i == CGROUP_SUBSYS_COUNT)
1256 return -ENOENT;
1257 }
1258
1259 /*
1260 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001261 * otherwise if 'none', 'name=' and a subsystem name options
1262 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001263 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001264 if (all_ss || (!one_ss && !opts->none && !opts->name))
1265 for_each_subsys(ss, i)
1266 if (!ss->disabled)
1267 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001268
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001269 /* Consistency checks */
1270
Tejun Heo873fe092013-04-14 20:15:26 -07001271 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1272 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1273
1274 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1275 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1276 return -EINVAL;
1277 }
1278
1279 if (opts->cpuset_clone_children) {
1280 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1281 return -EINVAL;
1282 }
1283 }
1284
Li Zefanf9ab5b52009-06-17 16:26:33 -07001285 /*
1286 * Option noprefix was introduced just for backward compatibility
1287 * with the old cpuset, so we allow noprefix only if mounting just
1288 * the cpuset subsystem.
1289 */
Tejun Heo93438622013-04-14 20:15:25 -07001290 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001291 return -EINVAL;
1292
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001293
1294 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001295 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001296 return -EINVAL;
1297
1298 /*
1299 * We either have to specify by name or by subsystems. (So all
1300 * empty hierarchies must have a name).
1301 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001302 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001303 return -EINVAL;
1304
1305 return 0;
1306}
1307
1308static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1309{
1310 int ret = 0;
1311 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001312 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001314 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001315
Tejun Heo873fe092013-04-14 20:15:26 -07001316 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1317 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1318 return -EINVAL;
1319 }
1320
Paul Menagebd89aab2007-10-18 23:40:44 -07001321 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001323 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324
1325 /* See what subsystems are wanted */
1326 ret = parse_cgroupfs_options(data, &opts);
1327 if (ret)
1328 goto out_unlock;
1329
Tejun Heoa8a648c2013-06-24 15:21:47 -07001330 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001331 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1332 task_tgid_nr(current), current->comm);
1333
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001334 added_mask = opts.subsys_mask & ~root->subsys_mask;
1335 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001336
Ben Blumcf5d5942010-03-10 15:22:09 -08001337 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001338 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001339 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001340 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1341 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1342 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001343 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001344 goto out_unlock;
1345 }
1346
Tejun Heof172e672013-06-28 17:07:30 -07001347 /* remounting is not allowed for populated hierarchies */
1348 if (root->number_of_cgroups > 1) {
1349 ret = -EBUSY;
1350 goto out_unlock;
1351 }
1352
Tejun Heoa8a648c2013-06-24 15:21:47 -07001353 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001354 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001355 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356
Paul Menage81a6a5c2007-10-18 23:39:38 -07001357 if (opts.release_agent)
1358 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001360 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001361 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001362 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001364 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365 return ret;
1366}
1367
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001368static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369 .statfs = simple_statfs,
1370 .drop_inode = generic_delete_inode,
1371 .show_options = cgroup_show_options,
1372 .remount_fs = cgroup_remount,
1373};
1374
Paul Menagecc31edc2008-10-18 20:28:04 -07001375static void init_cgroup_housekeeping(struct cgroup *cgrp)
1376{
1377 INIT_LIST_HEAD(&cgrp->sibling);
1378 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001379 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001380 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001381 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001382 INIT_LIST_HEAD(&cgrp->pidlists);
1383 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001384 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001385 INIT_LIST_HEAD(&cgrp->event_list);
1386 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001387 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001388}
Paul Menagec6d57f32009-09-23 15:56:19 -07001389
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390static void init_cgroup_root(struct cgroupfs_root *root)
1391{
Paul Menagebd89aab2007-10-18 23:40:44 -07001392 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001393
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394 INIT_LIST_HEAD(&root->subsys_list);
1395 INIT_LIST_HEAD(&root->root_list);
1396 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001397 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001398 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001399 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001400 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401}
1402
Tejun Heofc76df72013-06-25 11:53:37 -07001403static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001404{
Tejun Heo1a574232013-04-14 11:36:58 -07001405 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001406
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001407 lockdep_assert_held(&cgroup_mutex);
1408 lockdep_assert_held(&cgroup_root_mutex);
1409
Tejun Heofc76df72013-06-25 11:53:37 -07001410 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1411 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001412 if (id < 0)
1413 return id;
1414
1415 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001416 return 0;
1417}
1418
1419static void cgroup_exit_root_id(struct cgroupfs_root *root)
1420{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001421 lockdep_assert_held(&cgroup_mutex);
1422 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001423
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001424 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001425 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001426 root->hierarchy_id = 0;
1427 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001428}
1429
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430static int cgroup_test_super(struct super_block *sb, void *data)
1431{
Paul Menagec6d57f32009-09-23 15:56:19 -07001432 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001433 struct cgroupfs_root *root = sb->s_fs_info;
1434
Paul Menagec6d57f32009-09-23 15:56:19 -07001435 /* If we asked for a name then it must match */
1436 if (opts->name && strcmp(opts->name, root->name))
1437 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001438
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001439 /*
1440 * If we asked for subsystems (or explicitly for no
1441 * subsystems) then they must match
1442 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001443 if ((opts->subsys_mask || opts->none)
1444 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445 return 0;
1446
1447 return 1;
1448}
1449
Paul Menagec6d57f32009-09-23 15:56:19 -07001450static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1451{
1452 struct cgroupfs_root *root;
1453
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001454 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001455 return NULL;
1456
1457 root = kzalloc(sizeof(*root), GFP_KERNEL);
1458 if (!root)
1459 return ERR_PTR(-ENOMEM);
1460
1461 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001462
Tejun Heo1672d042013-06-25 18:04:54 -07001463 /*
1464 * We need to set @root->subsys_mask now so that @root can be
1465 * matched by cgroup_test_super() before it finishes
1466 * initialization; otherwise, competing mounts with the same
1467 * options may try to bind the same subsystems instead of waiting
1468 * for the first one leading to unexpected mount errors.
1469 * SUBSYS_BOUND will be set once actual binding is complete.
1470 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001471 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001472 root->flags = opts->flags;
1473 if (opts->release_agent)
1474 strcpy(root->release_agent_path, opts->release_agent);
1475 if (opts->name)
1476 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001477 if (opts->cpuset_clone_children)
1478 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001479 return root;
1480}
1481
Tejun Heofa3ca072013-04-14 11:36:56 -07001482static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001483{
Tejun Heofa3ca072013-04-14 11:36:56 -07001484 if (root) {
1485 /* hierarhcy ID shoulid already have been released */
1486 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001487
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001488 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001489 kfree(root);
1490 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001491}
1492
Paul Menageddbcc7e2007-10-18 23:39:30 -07001493static int cgroup_set_super(struct super_block *sb, void *data)
1494{
1495 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001496 struct cgroup_sb_opts *opts = data;
1497
1498 /* If we don't have a new root, we can't set up a new sb */
1499 if (!opts->new_root)
1500 return -EINVAL;
1501
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001502 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503
1504 ret = set_anon_super(sb, NULL);
1505 if (ret)
1506 return ret;
1507
Paul Menagec6d57f32009-09-23 15:56:19 -07001508 sb->s_fs_info = opts->new_root;
1509 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510
1511 sb->s_blocksize = PAGE_CACHE_SIZE;
1512 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1513 sb->s_magic = CGROUP_SUPER_MAGIC;
1514 sb->s_op = &cgroup_ops;
1515
1516 return 0;
1517}
1518
1519static int cgroup_get_rootdir(struct super_block *sb)
1520{
Al Viro0df6a632010-12-21 13:29:29 -05001521 static const struct dentry_operations cgroup_dops = {
1522 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001523 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001524 };
1525
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526 struct inode *inode =
1527 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528
1529 if (!inode)
1530 return -ENOMEM;
1531
Paul Menageddbcc7e2007-10-18 23:39:30 -07001532 inode->i_fop = &simple_dir_operations;
1533 inode->i_op = &cgroup_dir_inode_operations;
1534 /* directories start off with i_nlink == 2 (for "." entry) */
1535 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001536 sb->s_root = d_make_root(inode);
1537 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001539 /* for everything else we want ->d_op set */
1540 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541 return 0;
1542}
1543
Al Virof7e83572010-07-26 13:23:11 +04001544static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001545 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001546 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547{
1548 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001549 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550 int ret = 0;
1551 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001552 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001553 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001554 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001555 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556
1557 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001558 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001560 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001561 if (ret)
1562 goto out_err;
1563
1564 /*
1565 * Allocate a new cgroup root. We may not need it if we're
1566 * reusing an existing hierarchy.
1567 */
1568 new_root = cgroup_root_from_opts(&opts);
1569 if (IS_ERR(new_root)) {
1570 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001571 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001572 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001573 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574
Paul Menagec6d57f32009-09-23 15:56:19 -07001575 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001576 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001578 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001579 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001580 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581 }
1582
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 root = sb->s_fs_info;
1584 BUG_ON(!root);
1585 if (root == opts.new_root) {
1586 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001587 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001588 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001589 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001590 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591
1592 BUG_ON(sb->s_root != NULL);
1593
1594 ret = cgroup_get_rootdir(sb);
1595 if (ret)
1596 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001597 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598
Paul Menage817929e2007-10-18 23:39:36 -07001599 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001601 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001603 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1604 0, 1, GFP_KERNEL);
1605 if (root_cgrp->id < 0)
1606 goto unlock_drop;
1607
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001608 /* Check for name clashes with existing mounts */
1609 ret = -EBUSY;
1610 if (strlen(root->name))
1611 for_each_active_root(existing_root)
1612 if (!strcmp(existing_root->name, root->name))
1613 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001614
Paul Menage817929e2007-10-18 23:39:36 -07001615 /*
1616 * We're accessing css_set_count without locking
1617 * css_set_lock here, but that's OK - it can only be
1618 * increased by someone holding cgroup_lock, and
1619 * that's us. The worst that can happen is that we
1620 * have some link structures left over
1621 */
Tejun Heo69d02062013-06-12 21:04:50 -07001622 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001623 if (ret)
1624 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001625
Tejun Heofc76df72013-06-25 11:53:37 -07001626 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1627 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001628 if (ret)
1629 goto unlock_drop;
1630
Tejun Heo31261212013-06-28 17:07:30 -07001631 sb->s_root->d_fsdata = root_cgrp;
1632 root_cgrp->dentry = sb->s_root;
1633
1634 /*
1635 * We're inside get_sb() and will call lookup_one_len() to
1636 * create the root files, which doesn't work if SELinux is
1637 * in use. The following cred dancing somehow works around
1638 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1639 * populating new cgroupfs mount") for more details.
1640 */
1641 cred = override_creds(&init_cred);
1642
Tejun Heo2bb566c2013-08-08 20:11:23 -04001643 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001644 if (ret)
1645 goto rm_base_files;
1646
Tejun Heoa8a648c2013-06-24 15:21:47 -07001647 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001648 if (ret)
1649 goto rm_base_files;
1650
1651 revert_creds(cred);
1652
Ben Blumcf5d5942010-03-10 15:22:09 -08001653 /*
1654 * There must be no failure case after here, since rebinding
1655 * takes care of subsystems' refcounts, which are explicitly
1656 * dropped in the failure exit path.
1657 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001658
Tejun Heo9871bf92013-06-24 15:21:47 -07001659 list_add(&root->root_list, &cgroup_roots);
1660 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661
Paul Menage817929e2007-10-18 23:39:36 -07001662 /* Link the top cgroup in this hierarchy into all
1663 * the css_set objects */
1664 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001665 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001666 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001667 write_unlock(&css_set_lock);
1668
Tejun Heo69d02062013-06-12 21:04:50 -07001669 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001670
Li Zefanc12f65d2009-01-07 18:07:42 -08001671 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 BUG_ON(root->number_of_cgroups != 1);
1673
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001674 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001676 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001677 } else {
1678 /*
1679 * We re-used an existing hierarchy - the new root (if
1680 * any) is not needed
1681 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001682 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001683
Tejun Heoc7ba8282013-06-29 14:06:10 -07001684 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001685 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1686 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1687 ret = -EINVAL;
1688 goto drop_new_super;
1689 } else {
1690 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1691 }
Tejun Heo873fe092013-04-14 20:15:26 -07001692 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693 }
1694
Paul Menagec6d57f32009-09-23 15:56:19 -07001695 kfree(opts.release_agent);
1696 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001697 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698
Tejun Heo31261212013-06-28 17:07:30 -07001699 rm_base_files:
1700 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001701 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001702 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001703 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001704 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001705 mutex_unlock(&cgroup_root_mutex);
1706 mutex_unlock(&cgroup_mutex);
1707 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001709 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001710 out_err:
1711 kfree(opts.release_agent);
1712 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001713 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714}
1715
1716static void cgroup_kill_sb(struct super_block *sb) {
1717 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001718 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001719 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720 int ret;
1721
1722 BUG_ON(!root);
1723
1724 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001725 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001726
Tejun Heo31261212013-06-28 17:07:30 -07001727 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001729 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730
1731 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001732 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1733 ret = rebind_subsystems(root, 0, root->subsys_mask);
1734 /* Shouldn't be able to fail ... */
1735 BUG_ON(ret);
1736 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737
Paul Menage817929e2007-10-18 23:39:36 -07001738 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001739 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001740 * root cgroup
1741 */
1742 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001743
Tejun Heo69d02062013-06-12 21:04:50 -07001744 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1745 list_del(&link->cset_link);
1746 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001747 kfree(link);
1748 }
1749 write_unlock(&css_set_lock);
1750
Paul Menage839ec542009-01-29 14:25:22 -08001751 if (!list_empty(&root->root_list)) {
1752 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001753 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001754 }
Li Zefane5f6a862009-01-07 18:07:41 -08001755
Tejun Heofa3ca072013-04-14 11:36:56 -07001756 cgroup_exit_root_id(root);
1757
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001758 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001760 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001762 simple_xattrs_free(&cgrp->xattrs);
1763
Paul Menageddbcc7e2007-10-18 23:39:30 -07001764 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001765 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766}
1767
1768static struct file_system_type cgroup_fs_type = {
1769 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001770 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771 .kill_sb = cgroup_kill_sb,
1772};
1773
Greg KH676db4a2010-08-05 13:53:35 -07001774static struct kobject *cgroup_kobj;
1775
Li Zefana043e3b2008-02-23 15:24:09 -08001776/**
1777 * cgroup_path - generate the path of a cgroup
1778 * @cgrp: the cgroup in question
1779 * @buf: the buffer to write the path into
1780 * @buflen: the length of the buffer
1781 *
Li Zefan65dff752013-03-01 15:01:56 +08001782 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1783 *
1784 * We can't generate cgroup path using dentry->d_name, as accessing
1785 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1786 * inode's i_mutex, while on the other hand cgroup_path() can be called
1787 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001789int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790{
Li Zefan65dff752013-03-01 15:01:56 +08001791 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001793
Tejun Heoda1f2962013-04-14 10:32:19 -07001794 if (!cgrp->parent) {
1795 if (strlcpy(buf, "/", buflen) >= buflen)
1796 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 return 0;
1798 }
1799
Tao Ma316eb662012-11-08 21:36:38 +08001800 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001801 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001802
Li Zefan65dff752013-03-01 15:01:56 +08001803 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001804 do {
Li Zefan65dff752013-03-01 15:01:56 +08001805 const char *name = cgroup_name(cgrp);
1806 int len;
1807
1808 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001809 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001810 goto out;
1811 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001812
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001814 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001816
1817 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001818 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001819 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001821out:
1822 rcu_read_unlock();
1823 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824}
Ben Blum67523c42010-03-10 15:22:11 -08001825EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826
Tejun Heo857a2be2013-04-14 20:50:08 -07001827/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001828 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001829 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001830 * @buf: the buffer to write the path into
1831 * @buflen: the length of the buffer
1832 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001833 * Determine @task's cgroup on the first (the one with the lowest non-zero
1834 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1835 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1836 * cgroup controller callbacks.
1837 *
1838 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001839 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001840int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001841{
1842 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001843 struct cgroup *cgrp;
1844 int hierarchy_id = 1, ret = 0;
1845
1846 if (buflen < 2)
1847 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001848
1849 mutex_lock(&cgroup_mutex);
1850
Tejun Heo913ffdb2013-07-11 16:34:48 -07001851 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1852
Tejun Heo857a2be2013-04-14 20:50:08 -07001853 if (root) {
1854 cgrp = task_cgroup_from_root(task, root);
1855 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001856 } else {
1857 /* if no hierarchy exists, everyone is in "/" */
1858 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001859 }
1860
1861 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001862 return ret;
1863}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001864EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001865
Ben Blum74a11662011-05-26 16:25:20 -07001866/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001867 * Control Group taskset
1868 */
Tejun Heo134d3372011-12-12 18:12:21 -08001869struct task_and_cgroup {
1870 struct task_struct *task;
1871 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001872 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001873};
1874
Tejun Heo2f7ee562011-12-12 18:12:21 -08001875struct cgroup_taskset {
1876 struct task_and_cgroup single;
1877 struct flex_array *tc_array;
1878 int tc_array_len;
1879 int idx;
1880 struct cgroup *cur_cgrp;
1881};
1882
1883/**
1884 * cgroup_taskset_first - reset taskset and return the first task
1885 * @tset: taskset of interest
1886 *
1887 * @tset iteration is initialized and the first task is returned.
1888 */
1889struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1890{
1891 if (tset->tc_array) {
1892 tset->idx = 0;
1893 return cgroup_taskset_next(tset);
1894 } else {
1895 tset->cur_cgrp = tset->single.cgrp;
1896 return tset->single.task;
1897 }
1898}
1899EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1900
1901/**
1902 * cgroup_taskset_next - iterate to the next task in taskset
1903 * @tset: taskset of interest
1904 *
1905 * Return the next task in @tset. Iteration must have been initialized
1906 * with cgroup_taskset_first().
1907 */
1908struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1909{
1910 struct task_and_cgroup *tc;
1911
1912 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1913 return NULL;
1914
1915 tc = flex_array_get(tset->tc_array, tset->idx++);
1916 tset->cur_cgrp = tc->cgrp;
1917 return tc->task;
1918}
1919EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1920
1921/**
Tejun Heod99c8722013-08-08 20:11:27 -04001922 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001923 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001924 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001925 *
Tejun Heod99c8722013-08-08 20:11:27 -04001926 * Return the css for the current (last returned) task of @tset for
1927 * subsystem specified by @subsys_id. This function must be preceded by
1928 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001929 */
Tejun Heod99c8722013-08-08 20:11:27 -04001930struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1931 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001932{
Tejun Heod99c8722013-08-08 20:11:27 -04001933 return cgroup_css(tset->cur_cgrp, subsys_id);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001934}
Tejun Heod99c8722013-08-08 20:11:27 -04001935EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001936
1937/**
1938 * cgroup_taskset_size - return the number of tasks in taskset
1939 * @tset: taskset of interest
1940 */
1941int cgroup_taskset_size(struct cgroup_taskset *tset)
1942{
1943 return tset->tc_array ? tset->tc_array_len : 1;
1944}
1945EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1946
1947
Ben Blum74a11662011-05-26 16:25:20 -07001948/*
1949 * cgroup_task_migrate - move a task from one cgroup to another.
1950 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001951 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001952 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001953static void cgroup_task_migrate(struct cgroup *old_cgrp,
1954 struct task_struct *tsk,
1955 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001956{
Tejun Heo5abb8852013-06-12 21:04:49 -07001957 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001958
1959 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001960 * We are synchronized through threadgroup_lock() against PF_EXITING
1961 * setting such that we can't race against cgroup_exit() changing the
1962 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001963 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001964 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001965 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001966
Ben Blum74a11662011-05-26 16:25:20 -07001967 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001968 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001969 task_unlock(tsk);
1970
1971 /* Update the css_set linked lists if we're using them */
1972 write_lock(&css_set_lock);
1973 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001974 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001975 write_unlock(&css_set_lock);
1976
1977 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001978 * We just gained a reference on old_cset by taking it from the
1979 * task. As trading it for new_cset is protected by cgroup_mutex,
1980 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001981 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001982 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1983 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001984}
1985
Li Zefana043e3b2008-02-23 15:24:09 -08001986/**
Li Zefan081aa452013-03-13 09:17:09 +08001987 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001988 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001989 * @tsk: the task or the leader of the threadgroup to be attached
1990 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001991 *
Tejun Heo257058a2011-12-12 18:12:21 -08001992 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001993 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001994 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001995static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1996 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001997{
1998 int retval, i, group_size;
1999 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002000 struct cgroupfs_root *root = cgrp->root;
2001 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002002 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002003 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002004 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002005 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002006
2007 /*
2008 * step 0: in order to do expensive, possibly blocking operations for
2009 * every thread, we cannot iterate the thread group list, since it needs
2010 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002011 * group - group_rwsem prevents new threads from appearing, and if
2012 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002013 */
Li Zefan081aa452013-03-13 09:17:09 +08002014 if (threadgroup)
2015 group_size = get_nr_threads(tsk);
2016 else
2017 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002018 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002019 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002020 if (!group)
2021 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002022 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002023 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002024 if (retval)
2025 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002026
Ben Blum74a11662011-05-26 16:25:20 -07002027 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002028 /*
2029 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2030 * already PF_EXITING could be freed from underneath us unless we
2031 * take an rcu_read_lock.
2032 */
2033 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002034 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002035 struct task_and_cgroup ent;
2036
Tejun Heocd3d0952011-12-12 18:12:21 -08002037 /* @tsk either already exited or can't exit until the end */
2038 if (tsk->flags & PF_EXITING)
2039 continue;
2040
Ben Blum74a11662011-05-26 16:25:20 -07002041 /* as per above, nr_threads may decrease, but not increase. */
2042 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002043 ent.task = tsk;
2044 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002045 /* nothing to do if this task is already in the cgroup */
2046 if (ent.cgrp == cgrp)
2047 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002048 /*
2049 * saying GFP_ATOMIC has no effect here because we did prealloc
2050 * earlier, but it's good form to communicate our expectations.
2051 */
Tejun Heo134d3372011-12-12 18:12:21 -08002052 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002053 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002054 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002055
2056 if (!threadgroup)
2057 break;
Ben Blum74a11662011-05-26 16:25:20 -07002058 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002059 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002060 /* remember the number of threads in the array for later. */
2061 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002062 tset.tc_array = group;
2063 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002064
Tejun Heo134d3372011-12-12 18:12:21 -08002065 /* methods shouldn't be called if no task is actually migrating */
2066 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002067 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002068 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002069
Ben Blum74a11662011-05-26 16:25:20 -07002070 /*
2071 * step 1: check that we can legitimately attach to the cgroup.
2072 */
Tejun Heo5549c492013-06-24 15:21:48 -07002073 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04002074 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -04002075
Ben Blum74a11662011-05-26 16:25:20 -07002076 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002077 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002078 if (retval) {
2079 failed_ss = ss;
2080 goto out_cancel_attach;
2081 }
2082 }
Ben Blum74a11662011-05-26 16:25:20 -07002083 }
2084
2085 /*
2086 * step 2: make sure css_sets exist for all threads to be migrated.
2087 * we use find_css_set, which allocates a new one if necessary.
2088 */
Ben Blum74a11662011-05-26 16:25:20 -07002089 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002090 struct css_set *old_cset;
2091
Tejun Heo134d3372011-12-12 18:12:21 -08002092 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002093 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002094 tc->cset = find_css_set(old_cset, cgrp);
2095 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002096 retval = -ENOMEM;
2097 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002098 }
2099 }
2100
2101 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002102 * step 3: now that we're guaranteed success wrt the css_sets,
2103 * proceed to move all tasks to the new cgroup. There are no
2104 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002105 */
Ben Blum74a11662011-05-26 16:25:20 -07002106 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002107 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002108 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002109 }
2110 /* nothing is sensitive to fork() after this point. */
2111
2112 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002113 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002114 */
Tejun Heo5549c492013-06-24 15:21:48 -07002115 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04002116 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -04002117
Ben Blum74a11662011-05-26 16:25:20 -07002118 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002119 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002120 }
2121
2122 /*
2123 * step 5: success! and cleanup
2124 */
Ben Blum74a11662011-05-26 16:25:20 -07002125 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002126out_put_css_set_refs:
2127 if (retval) {
2128 for (i = 0; i < group_size; i++) {
2129 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002130 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002131 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002132 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002133 }
Ben Blum74a11662011-05-26 16:25:20 -07002134 }
2135out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002136 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002137 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04002138 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -04002139
Tejun Heo494c1672011-12-12 18:12:22 -08002140 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002141 break;
Ben Blum74a11662011-05-26 16:25:20 -07002142 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002143 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002144 }
2145 }
Ben Blum74a11662011-05-26 16:25:20 -07002146out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002147 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002148 return retval;
2149}
2150
2151/*
2152 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002153 * function to attach either it or all tasks in its threadgroup. Will lock
2154 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002155 */
2156static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002157{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002158 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002159 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002160 int ret;
2161
Ben Blum74a11662011-05-26 16:25:20 -07002162 if (!cgroup_lock_live_group(cgrp))
2163 return -ENODEV;
2164
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165retry_find_task:
2166 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002167 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002168 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002169 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002170 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002171 ret= -ESRCH;
2172 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002173 }
Ben Blum74a11662011-05-26 16:25:20 -07002174 /*
2175 * even if we're attaching all tasks in the thread group, we
2176 * only need to check permissions on one of them.
2177 */
David Howellsc69e8d92008-11-14 10:39:19 +11002178 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002179 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2180 !uid_eq(cred->euid, tcred->uid) &&
2181 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002182 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002183 ret = -EACCES;
2184 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002185 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002186 } else
2187 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002188
2189 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002190 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002191
2192 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002193 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002194 * trapped in a cpuset, or RT worker may be born in a cgroup
2195 * with no rt_runtime allocated. Just say no.
2196 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002197 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002198 ret = -EINVAL;
2199 rcu_read_unlock();
2200 goto out_unlock_cgroup;
2201 }
2202
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002203 get_task_struct(tsk);
2204 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002205
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002206 threadgroup_lock(tsk);
2207 if (threadgroup) {
2208 if (!thread_group_leader(tsk)) {
2209 /*
2210 * a race with de_thread from another thread's exec()
2211 * may strip us of our leadership, if this happens,
2212 * there is no choice but to throw this task away and
2213 * try again; this is
2214 * "double-double-toil-and-trouble-check locking".
2215 */
2216 threadgroup_unlock(tsk);
2217 put_task_struct(tsk);
2218 goto retry_find_task;
2219 }
Li Zefan081aa452013-03-13 09:17:09 +08002220 }
2221
2222 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2223
Tejun Heocd3d0952011-12-12 18:12:21 -08002224 threadgroup_unlock(tsk);
2225
Paul Menagebbcb81d2007-10-18 23:39:32 -07002226 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002227out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002228 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002229 return ret;
2230}
2231
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002232/**
2233 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2234 * @from: attach to all cgroups of a given task
2235 * @tsk: the task to be attached
2236 */
2237int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2238{
2239 struct cgroupfs_root *root;
2240 int retval = 0;
2241
Tejun Heo47cfcd02013-04-07 09:29:51 -07002242 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002243 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002244 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002245
Li Zefan6f4b7e62013-07-31 16:18:36 +08002246 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002247 if (retval)
2248 break;
2249 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002250 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002251
2252 return retval;
2253}
2254EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2255
Tejun Heo182446d2013-08-08 20:11:24 -04002256static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2257 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002258{
Tejun Heo182446d2013-08-08 20:11:24 -04002259 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002260}
2261
Tejun Heo182446d2013-08-08 20:11:24 -04002262static int cgroup_procs_write(struct cgroup_subsys_state *css,
2263 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002264{
Tejun Heo182446d2013-08-08 20:11:24 -04002265 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002266}
2267
Tejun Heo182446d2013-08-08 20:11:24 -04002268static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2269 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002270{
Tejun Heo182446d2013-08-08 20:11:24 -04002271 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002272 if (strlen(buffer) >= PATH_MAX)
2273 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002274 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002275 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002276 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002277 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002278 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002279 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002280 return 0;
2281}
2282
Tejun Heo182446d2013-08-08 20:11:24 -04002283static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2284 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002285{
Tejun Heo182446d2013-08-08 20:11:24 -04002286 struct cgroup *cgrp = css->cgroup;
2287
Paul Menagee788e062008-07-25 01:46:59 -07002288 if (!cgroup_lock_live_group(cgrp))
2289 return -ENODEV;
2290 seq_puts(seq, cgrp->root->release_agent_path);
2291 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002292 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002293 return 0;
2294}
2295
Tejun Heo182446d2013-08-08 20:11:24 -04002296static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2297 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002298{
Tejun Heo182446d2013-08-08 20:11:24 -04002299 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002300 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002301}
2302
Paul Menage84eea842008-07-25 01:47:00 -07002303/* A buffer size big enough for numbers or short strings */
2304#define CGROUP_LOCAL_BUFFER_SIZE 64
2305
Tejun Heo182446d2013-08-08 20:11:24 -04002306static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2307 struct cftype *cft, struct file *file,
2308 const char __user *userbuf, size_t nbytes,
2309 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002310{
Paul Menage84eea842008-07-25 01:47:00 -07002311 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002312 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002313 char *end;
2314
2315 if (!nbytes)
2316 return -EINVAL;
2317 if (nbytes >= sizeof(buffer))
2318 return -E2BIG;
2319 if (copy_from_user(buffer, userbuf, nbytes))
2320 return -EFAULT;
2321
2322 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002323 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002324 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002325 if (*end)
2326 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002327 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002328 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002329 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002330 if (*end)
2331 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002332 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002333 }
Paul Menage355e0c42007-10-18 23:39:33 -07002334 if (!retval)
2335 retval = nbytes;
2336 return retval;
2337}
2338
Tejun Heo182446d2013-08-08 20:11:24 -04002339static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2340 struct cftype *cft, struct file *file,
2341 const char __user *userbuf, size_t nbytes,
2342 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002343{
Paul Menage84eea842008-07-25 01:47:00 -07002344 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002345 int retval = 0;
2346 size_t max_bytes = cft->max_write_len;
2347 char *buffer = local_buffer;
2348
2349 if (!max_bytes)
2350 max_bytes = sizeof(local_buffer) - 1;
2351 if (nbytes >= max_bytes)
2352 return -E2BIG;
2353 /* Allocate a dynamic buffer if we need one */
2354 if (nbytes >= sizeof(local_buffer)) {
2355 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2356 if (buffer == NULL)
2357 return -ENOMEM;
2358 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002359 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2360 retval = -EFAULT;
2361 goto out;
2362 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002363
2364 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002365 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002366 if (!retval)
2367 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002368out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002369 if (buffer != local_buffer)
2370 kfree(buffer);
2371 return retval;
2372}
2373
Paul Menageddbcc7e2007-10-18 23:39:30 -07002374static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002375 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002376{
Tejun Heo182446d2013-08-08 20:11:24 -04002377 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002379 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380
Paul Menage355e0c42007-10-18 23:39:33 -07002381 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002382 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002383 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002384 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002385 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002386 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002387 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002388 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002389 return ret ? ret : nbytes;
2390 }
Paul Menage355e0c42007-10-18 23:39:33 -07002391 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392}
2393
Tejun Heo182446d2013-08-08 20:11:24 -04002394static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2395 struct cftype *cft, struct file *file,
2396 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397{
Paul Menage84eea842008-07-25 01:47:00 -07002398 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002399 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002400 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2401
2402 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2403}
2404
Tejun Heo182446d2013-08-08 20:11:24 -04002405static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2406 struct cftype *cft, struct file *file,
2407 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002408{
Paul Menage84eea842008-07-25 01:47:00 -07002409 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002410 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002411 int len = sprintf(tmp, "%lld\n", (long long) val);
2412
2413 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2414}
2415
Paul Menageddbcc7e2007-10-18 23:39:30 -07002416static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002417 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418{
Tejun Heo182446d2013-08-08 20:11:24 -04002419 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002421 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002422
Paul Menageddbcc7e2007-10-18 23:39:30 -07002423 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002424 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002425 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002426 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002427 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002428 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002429 return -EINVAL;
2430}
2431
Paul Menage91796562008-04-29 01:00:01 -07002432/*
2433 * seqfile ops/methods for returning structured data. Currently just
2434 * supports string->u64 maps, but can be extended in future.
2435 */
2436
Paul Menage91796562008-04-29 01:00:01 -07002437static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2438{
2439 struct seq_file *sf = cb->state;
2440 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2441}
2442
2443static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2444{
Li Zefane0798ce2013-07-31 17:36:25 +08002445 struct cfent *cfe = m->private;
2446 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002447 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002448
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002449 if (cft->read_map) {
2450 struct cgroup_map_cb cb = {
2451 .fill = cgroup_map_add,
2452 .state = m,
2453 };
Tejun Heo182446d2013-08-08 20:11:24 -04002454 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002455 }
Tejun Heo182446d2013-08-08 20:11:24 -04002456 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002457}
2458
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002459static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002460 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002461 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002462 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002463 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002464};
2465
Paul Menageddbcc7e2007-10-18 23:39:30 -07002466static int cgroup_file_open(struct inode *inode, struct file *file)
2467{
Tejun Heof7d58812013-08-08 20:11:23 -04002468 struct cfent *cfe = __d_cfe(file->f_dentry);
2469 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002470 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2471 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002472 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002473
2474 err = generic_file_open(inode, file);
2475 if (err)
2476 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002477
2478 /*
2479 * If the file belongs to a subsystem, pin the css. Will be
2480 * unpinned either on open failure or release. This ensures that
2481 * @css stays alive for all file operations.
2482 */
Tejun Heo105347b2013-08-13 11:01:55 -04002483 rcu_read_lock();
2484 if (cft->ss) {
2485 css = cgroup_css(cgrp, cft->ss->subsys_id);
2486 if (!css_tryget(css))
2487 css = NULL;
2488 } else {
2489 css = &cgrp->dummy_css;
2490 }
2491 rcu_read_unlock();
2492
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002493 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002494 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002495
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002496 /*
2497 * @cfe->css is used by read/write/close to determine the
2498 * associated css. @file->private_data would be a better place but
2499 * that's already used by seqfile. Multiple accessors may use it
2500 * simultaneously which is okay as the association never changes.
2501 */
2502 WARN_ON_ONCE(cfe->css && cfe->css != css);
2503 cfe->css = css;
2504
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002505 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002506 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002507 err = single_open(file, cgroup_seqfile_show, cfe);
2508 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002509 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002510 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002511
Tejun Heo67f4c362013-08-08 20:11:24 -04002512 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002513 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002514 return err;
2515}
2516
2517static int cgroup_file_release(struct inode *inode, struct file *file)
2518{
Tejun Heof7d58812013-08-08 20:11:23 -04002519 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002520 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002521 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002522 int ret = 0;
2523
Paul Menageddbcc7e2007-10-18 23:39:30 -07002524 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002525 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002526 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002527 css_put(css);
2528 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002529}
2530
2531/*
2532 * cgroup_rename - Only allow simple rename of directories in place.
2533 */
2534static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2535 struct inode *new_dir, struct dentry *new_dentry)
2536{
Li Zefan65dff752013-03-01 15:01:56 +08002537 int ret;
2538 struct cgroup_name *name, *old_name;
2539 struct cgroup *cgrp;
2540
2541 /*
2542 * It's convinient to use parent dir's i_mutex to protected
2543 * cgrp->name.
2544 */
2545 lockdep_assert_held(&old_dir->i_mutex);
2546
Paul Menageddbcc7e2007-10-18 23:39:30 -07002547 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2548 return -ENOTDIR;
2549 if (new_dentry->d_inode)
2550 return -EEXIST;
2551 if (old_dir != new_dir)
2552 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002553
2554 cgrp = __d_cgrp(old_dentry);
2555
Tejun Heo6db8e852013-06-14 11:18:22 -07002556 /*
2557 * This isn't a proper migration and its usefulness is very
2558 * limited. Disallow if sane_behavior.
2559 */
2560 if (cgroup_sane_behavior(cgrp))
2561 return -EPERM;
2562
Li Zefan65dff752013-03-01 15:01:56 +08002563 name = cgroup_alloc_name(new_dentry);
2564 if (!name)
2565 return -ENOMEM;
2566
2567 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2568 if (ret) {
2569 kfree(name);
2570 return ret;
2571 }
2572
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002573 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002574 rcu_assign_pointer(cgrp->name, name);
2575
2576 kfree_rcu(old_name, rcu_head);
2577 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002578}
2579
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002580static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2581{
2582 if (S_ISDIR(dentry->d_inode->i_mode))
2583 return &__d_cgrp(dentry)->xattrs;
2584 else
Li Zefan712317a2013-04-18 23:09:52 -07002585 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002586}
2587
2588static inline int xattr_enabled(struct dentry *dentry)
2589{
2590 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002591 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002592}
2593
2594static bool is_valid_xattr(const char *name)
2595{
2596 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2597 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2598 return true;
2599 return false;
2600}
2601
2602static int cgroup_setxattr(struct dentry *dentry, const char *name,
2603 const void *val, size_t size, int flags)
2604{
2605 if (!xattr_enabled(dentry))
2606 return -EOPNOTSUPP;
2607 if (!is_valid_xattr(name))
2608 return -EINVAL;
2609 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2610}
2611
2612static int cgroup_removexattr(struct dentry *dentry, const char *name)
2613{
2614 if (!xattr_enabled(dentry))
2615 return -EOPNOTSUPP;
2616 if (!is_valid_xattr(name))
2617 return -EINVAL;
2618 return simple_xattr_remove(__d_xattrs(dentry), name);
2619}
2620
2621static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2622 void *buf, size_t size)
2623{
2624 if (!xattr_enabled(dentry))
2625 return -EOPNOTSUPP;
2626 if (!is_valid_xattr(name))
2627 return -EINVAL;
2628 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2629}
2630
2631static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2632{
2633 if (!xattr_enabled(dentry))
2634 return -EOPNOTSUPP;
2635 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2636}
2637
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002638static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002639 .read = cgroup_file_read,
2640 .write = cgroup_file_write,
2641 .llseek = generic_file_llseek,
2642 .open = cgroup_file_open,
2643 .release = cgroup_file_release,
2644};
2645
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002646static const struct inode_operations cgroup_file_inode_operations = {
2647 .setxattr = cgroup_setxattr,
2648 .getxattr = cgroup_getxattr,
2649 .listxattr = cgroup_listxattr,
2650 .removexattr = cgroup_removexattr,
2651};
2652
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002653static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002654 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002655 .mkdir = cgroup_mkdir,
2656 .rmdir = cgroup_rmdir,
2657 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002658 .setxattr = cgroup_setxattr,
2659 .getxattr = cgroup_getxattr,
2660 .listxattr = cgroup_listxattr,
2661 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002662};
2663
Al Viro00cd8dd2012-06-10 17:13:09 -04002664static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002665{
2666 if (dentry->d_name.len > NAME_MAX)
2667 return ERR_PTR(-ENAMETOOLONG);
2668 d_add(dentry, NULL);
2669 return NULL;
2670}
2671
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002672/*
2673 * Check if a file is a control file
2674 */
2675static inline struct cftype *__file_cft(struct file *file)
2676{
Al Viro496ad9a2013-01-23 17:07:38 -05002677 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002678 return ERR_PTR(-EINVAL);
2679 return __d_cft(file->f_dentry);
2680}
2681
Al Viroa5e7ed32011-07-26 01:55:55 -04002682static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002683 struct super_block *sb)
2684{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002685 struct inode *inode;
2686
2687 if (!dentry)
2688 return -ENOENT;
2689 if (dentry->d_inode)
2690 return -EEXIST;
2691
2692 inode = cgroup_new_inode(mode, sb);
2693 if (!inode)
2694 return -ENOMEM;
2695
2696 if (S_ISDIR(mode)) {
2697 inode->i_op = &cgroup_dir_inode_operations;
2698 inode->i_fop = &simple_dir_operations;
2699
2700 /* start off with i_nlink == 2 (for "." entry) */
2701 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002702 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002703
Tejun Heob8a2df62012-11-19 08:13:37 -08002704 /*
2705 * Control reaches here with cgroup_mutex held.
2706 * @inode->i_mutex should nest outside cgroup_mutex but we
2707 * want to populate it immediately without releasing
2708 * cgroup_mutex. As @inode isn't visible to anyone else
2709 * yet, trylock will always succeed without affecting
2710 * lockdep checks.
2711 */
2712 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713 } else if (S_ISREG(mode)) {
2714 inode->i_size = 0;
2715 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002716 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002717 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002718 d_instantiate(dentry, inode);
2719 dget(dentry); /* Extra count - pin the dentry in core */
2720 return 0;
2721}
2722
Li Zefan099fca32009-04-02 16:57:29 -07002723/**
2724 * cgroup_file_mode - deduce file mode of a control file
2725 * @cft: the control file in question
2726 *
2727 * returns cft->mode if ->mode is not 0
2728 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2729 * returns S_IRUGO if it has only a read handler
2730 * returns S_IWUSR if it has only a write hander
2731 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002732static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002733{
Al Viroa5e7ed32011-07-26 01:55:55 -04002734 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002735
2736 if (cft->mode)
2737 return cft->mode;
2738
2739 if (cft->read || cft->read_u64 || cft->read_s64 ||
2740 cft->read_map || cft->read_seq_string)
2741 mode |= S_IRUGO;
2742
2743 if (cft->write || cft->write_u64 || cft->write_s64 ||
2744 cft->write_string || cft->trigger)
2745 mode |= S_IWUSR;
2746
2747 return mode;
2748}
2749
Tejun Heo2bb566c2013-08-08 20:11:23 -04002750static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002751{
Paul Menagebd89aab2007-10-18 23:40:44 -07002752 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002753 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002755 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002757 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002758 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002759
Tejun Heo2bb566c2013-08-08 20:11:23 -04002760 if (cft->ss && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2761 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002762 strcat(name, ".");
2763 }
2764 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002765
Paul Menageddbcc7e2007-10-18 23:39:30 -07002766 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002767
2768 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2769 if (!cfe)
2770 return -ENOMEM;
2771
Paul Menageddbcc7e2007-10-18 23:39:30 -07002772 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002773 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002774 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002775 goto out;
2776 }
2777
Li Zefand6cbf352013-05-14 19:44:20 +08002778 cfe->type = (void *)cft;
2779 cfe->dentry = dentry;
2780 dentry->d_fsdata = cfe;
2781 simple_xattrs_init(&cfe->xattrs);
2782
Tejun Heo05ef1d72012-04-01 12:09:56 -07002783 mode = cgroup_file_mode(cft);
2784 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2785 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002786 list_add_tail(&cfe->node, &parent->files);
2787 cfe = NULL;
2788 }
2789 dput(dentry);
2790out:
2791 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002792 return error;
2793}
2794
Tejun Heob1f28d32013-06-28 16:24:10 -07002795/**
2796 * cgroup_addrm_files - add or remove files to a cgroup directory
2797 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002798 * @cfts: array of cftypes to be added
2799 * @is_add: whether to add or remove
2800 *
2801 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002802 * For removals, this function never fails. If addition fails, this
2803 * function doesn't remove files already added. The caller is responsible
2804 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002805 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002806static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2807 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002808{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002809 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002810 int ret;
2811
2812 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2813 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002814
2815 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002816 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002817 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2818 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002819 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2820 continue;
2821 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2822 continue;
2823
Li Zefan2739d3c2013-01-21 18:18:33 +08002824 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002825 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002826 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002827 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002828 cft->name, ret);
2829 return ret;
2830 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002831 } else {
2832 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002833 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002834 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002835 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002836}
2837
Tejun Heo8e3f6542012-04-01 12:09:55 -07002838static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002839 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002840{
2841 /*
2842 * Thanks to the entanglement with vfs inode locking, we can't walk
2843 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002844 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2845 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002846 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002847 mutex_lock(&cgroup_mutex);
2848}
2849
Tejun Heo2bb566c2013-08-08 20:11:23 -04002850static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002851 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002852{
2853 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002854 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002855 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002856 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002857 struct dentry *prev = NULL;
2858 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002859 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002860 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002861 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002862
2863 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002864 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002865 !atomic_inc_not_zero(&sb->s_active)) {
2866 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002867 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002868 }
2869
Li Zefane8c82d22013-06-18 18:48:37 +08002870 /*
2871 * All cgroups which are created after we drop cgroup_mutex will
2872 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002873 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002874 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002875 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002876
Tejun Heo8e3f6542012-04-01 12:09:55 -07002877 mutex_unlock(&cgroup_mutex);
2878
Li Zefane8c82d22013-06-18 18:48:37 +08002879 /* add/rm files for all cgroups created before */
2880 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002881 css_for_each_descendant_pre(css, cgroup_css(root, ss->subsys_id)) {
2882 struct cgroup *cgrp = css->cgroup;
2883
Li Zefane8c82d22013-06-18 18:48:37 +08002884 if (cgroup_is_dead(cgrp))
2885 continue;
2886
2887 inode = cgrp->dentry->d_inode;
2888 dget(cgrp->dentry);
2889 rcu_read_unlock();
2890
2891 dput(prev);
2892 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002893
2894 mutex_lock(&inode->i_mutex);
2895 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002896 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002897 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002898 mutex_unlock(&cgroup_mutex);
2899 mutex_unlock(&inode->i_mutex);
2900
Li Zefane8c82d22013-06-18 18:48:37 +08002901 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002902 if (ret)
2903 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002904 }
Li Zefane8c82d22013-06-18 18:48:37 +08002905 rcu_read_unlock();
2906 dput(prev);
2907 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002908 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002909}
2910
2911/**
2912 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2913 * @ss: target cgroup subsystem
2914 * @cfts: zero-length name terminated array of cftypes
2915 *
2916 * Register @cfts to @ss. Files described by @cfts are created for all
2917 * existing cgroups to which @ss is attached and all future cgroups will
2918 * have them too. This function can be called anytime whether @ss is
2919 * attached or not.
2920 *
2921 * Returns 0 on successful registration, -errno on failure. Note that this
2922 * function currently returns 0 as long as @cfts registration is successful
2923 * even if some file creation attempts on existing cgroups fail.
2924 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002925int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002926{
2927 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002928 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002929 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002930
2931 set = kzalloc(sizeof(*set), GFP_KERNEL);
2932 if (!set)
2933 return -ENOMEM;
2934
Tejun Heo2bb566c2013-08-08 20:11:23 -04002935 for (cft = cfts; cft->name[0] != '\0'; cft++)
2936 cft->ss = ss;
2937
Tejun Heo8e3f6542012-04-01 12:09:55 -07002938 cgroup_cfts_prepare();
2939 set->cfts = cfts;
2940 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002941 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002942 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002943 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002944 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002945}
2946EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2947
Li Zefana043e3b2008-02-23 15:24:09 -08002948/**
Tejun Heo79578622012-04-01 12:09:56 -07002949 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002950 * @cfts: zero-length name terminated array of cftypes
2951 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002952 * Unregister @cfts. Files described by @cfts are removed from all
2953 * existing cgroups and all future cgroups won't have them either. This
2954 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002955 *
2956 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002957 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002958 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002959int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002960{
2961 struct cftype_set *set;
2962
Tejun Heo2bb566c2013-08-08 20:11:23 -04002963 if (!cfts || !cfts[0].ss)
2964 return -ENOENT;
2965
Tejun Heo79578622012-04-01 12:09:56 -07002966 cgroup_cfts_prepare();
2967
Tejun Heo2bb566c2013-08-08 20:11:23 -04002968 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002969 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002970 list_del(&set->node);
2971 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002972 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002973 return 0;
2974 }
2975 }
2976
Tejun Heo2bb566c2013-08-08 20:11:23 -04002977 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002978 return -ENOENT;
2979}
2980
2981/**
Li Zefana043e3b2008-02-23 15:24:09 -08002982 * cgroup_task_count - count the number of tasks in a cgroup.
2983 * @cgrp: the cgroup in question
2984 *
2985 * Return the number of tasks in the cgroup.
2986 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002987int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002988{
2989 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002990 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002991
Paul Menage817929e2007-10-18 23:39:36 -07002992 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002993 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2994 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002995 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002996 return count;
2997}
2998
2999/*
Tejun Heo0942eee2013-08-08 20:11:26 -04003000 * To reduce the fork() overhead for systems that are not actually using
3001 * their cgroups capability, we don't maintain the lists running through
3002 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04003003 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003004 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003005static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003006{
3007 struct task_struct *p, *g;
3008 write_lock(&css_set_lock);
3009 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003010 /*
3011 * We need tasklist_lock because RCU is not safe against
3012 * while_each_thread(). Besides, a forking task that has passed
3013 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3014 * is not guaranteed to have its child immediately visible in the
3015 * tasklist if we walk through it with RCU.
3016 */
3017 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003018 do_each_thread(g, p) {
3019 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003020 /*
3021 * We should check if the process is exiting, otherwise
3022 * it will race with cgroup_exit() in that the list
3023 * entry won't be deleted though the process has exited.
3024 */
3025 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003026 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003027 task_unlock(p);
3028 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003029 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003030 write_unlock(&css_set_lock);
3031}
3032
Tejun Heo574bd9f2012-11-09 09:12:29 -08003033/**
Tejun Heo492eb212013-08-08 20:11:25 -04003034 * css_next_child - find the next child of a given css
3035 * @pos_css: the current position (%NULL to initiate traversal)
3036 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003037 *
Tejun Heo492eb212013-08-08 20:11:25 -04003038 * This function returns the next child of @parent_css and should be called
3039 * under RCU read lock. The only requirement is that @parent_css and
3040 * @pos_css are accessible. The next sibling is guaranteed to be returned
3041 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003042 */
Tejun Heo492eb212013-08-08 20:11:25 -04003043struct cgroup_subsys_state *
3044css_next_child(struct cgroup_subsys_state *pos_css,
3045 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003046{
Tejun Heo492eb212013-08-08 20:11:25 -04003047 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3048 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003049 struct cgroup *next;
3050
3051 WARN_ON_ONCE(!rcu_read_lock_held());
3052
3053 /*
3054 * @pos could already have been removed. Once a cgroup is removed,
3055 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003056 * changes. As CGRP_DEAD assertion is serialized and happens
3057 * before the cgroup is taken off the ->sibling list, if we see it
3058 * unasserted, it's guaranteed that the next sibling hasn't
3059 * finished its grace period even if it's already removed, and thus
3060 * safe to dereference from this RCU critical section. If
3061 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3062 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003063 *
3064 * If @pos is dead, its next pointer can't be dereferenced;
3065 * however, as each cgroup is given a monotonically increasing
3066 * unique serial number and always appended to the sibling list,
3067 * the next one can be found by walking the parent's children until
3068 * we see a cgroup with higher serial number than @pos's. While
3069 * this path can be slower, it's taken only when either the current
3070 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003071 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003072 if (!pos) {
3073 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3074 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003075 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003076 } else {
3077 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3078 if (next->serial_nr > pos->serial_nr)
3079 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003080 }
3081
Tejun Heo492eb212013-08-08 20:11:25 -04003082 if (&next->sibling == &cgrp->children)
3083 return NULL;
3084
3085 if (parent_css->ss)
3086 return cgroup_css(next, parent_css->ss->subsys_id);
3087 else
3088 return &next->dummy_css;
Tejun Heo53fa5262013-05-24 10:55:38 +09003089}
Tejun Heo492eb212013-08-08 20:11:25 -04003090EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003091
3092/**
Tejun Heo492eb212013-08-08 20:11:25 -04003093 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003094 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003095 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003096 *
Tejun Heo492eb212013-08-08 20:11:25 -04003097 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003098 * to visit for pre-order traversal of @root's descendants. @root is
3099 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003100 *
3101 * While this function requires RCU read locking, it doesn't require the
3102 * whole traversal to be contained in a single RCU critical section. This
3103 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003104 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003105 */
Tejun Heo492eb212013-08-08 20:11:25 -04003106struct cgroup_subsys_state *
3107css_next_descendant_pre(struct cgroup_subsys_state *pos,
3108 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003109{
Tejun Heo492eb212013-08-08 20:11:25 -04003110 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003111
3112 WARN_ON_ONCE(!rcu_read_lock_held());
3113
Tejun Heobd8815a2013-08-08 20:11:27 -04003114 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003115 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003116 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003117
3118 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003119 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003120 if (next)
3121 return next;
3122
3123 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003124 while (pos != root) {
3125 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003126 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003127 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003128 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003129 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003130
3131 return NULL;
3132}
Tejun Heo492eb212013-08-08 20:11:25 -04003133EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003134
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003135/**
Tejun Heo492eb212013-08-08 20:11:25 -04003136 * css_rightmost_descendant - return the rightmost descendant of a css
3137 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003138 *
Tejun Heo492eb212013-08-08 20:11:25 -04003139 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3140 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003141 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003142 *
3143 * While this function requires RCU read locking, it doesn't require the
3144 * whole traversal to be contained in a single RCU critical section. This
3145 * function will return the correct rightmost descendant as long as @pos is
3146 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003147 */
Tejun Heo492eb212013-08-08 20:11:25 -04003148struct cgroup_subsys_state *
3149css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003150{
Tejun Heo492eb212013-08-08 20:11:25 -04003151 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003152
3153 WARN_ON_ONCE(!rcu_read_lock_held());
3154
3155 do {
3156 last = pos;
3157 /* ->prev isn't RCU safe, walk ->next till the end */
3158 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003159 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003160 pos = tmp;
3161 } while (pos);
3162
3163 return last;
3164}
Tejun Heo492eb212013-08-08 20:11:25 -04003165EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003166
Tejun Heo492eb212013-08-08 20:11:25 -04003167static struct cgroup_subsys_state *
3168css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003169{
Tejun Heo492eb212013-08-08 20:11:25 -04003170 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003171
3172 do {
3173 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003174 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003175 } while (pos);
3176
3177 return last;
3178}
3179
3180/**
Tejun Heo492eb212013-08-08 20:11:25 -04003181 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003182 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003183 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003184 *
Tejun Heo492eb212013-08-08 20:11:25 -04003185 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003186 * to visit for post-order traversal of @root's descendants. @root is
3187 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003188 *
3189 * While this function requires RCU read locking, it doesn't require the
3190 * whole traversal to be contained in a single RCU critical section. This
3191 * function will return the correct next descendant as long as both @pos
3192 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003193 */
Tejun Heo492eb212013-08-08 20:11:25 -04003194struct cgroup_subsys_state *
3195css_next_descendant_post(struct cgroup_subsys_state *pos,
3196 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003197{
Tejun Heo492eb212013-08-08 20:11:25 -04003198 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003199
3200 WARN_ON_ONCE(!rcu_read_lock_held());
3201
3202 /* if first iteration, visit the leftmost descendant */
3203 if (!pos) {
Tejun Heo492eb212013-08-08 20:11:25 -04003204 next = css_leftmost_descendant(root);
3205 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003206 }
3207
Tejun Heobd8815a2013-08-08 20:11:27 -04003208 /* if we visited @root, we're done */
3209 if (pos == root)
3210 return NULL;
3211
Tejun Heo574bd9f2012-11-09 09:12:29 -08003212 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003213 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003214 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003215 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003216
3217 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003218 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003219}
Tejun Heo492eb212013-08-08 20:11:25 -04003220EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003221
Tejun Heo0942eee2013-08-08 20:11:26 -04003222/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003223 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003224 * @it: the iterator to advance
3225 *
3226 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003227 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003228static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003229{
3230 struct list_head *l = it->cset_link;
3231 struct cgrp_cset_link *link;
3232 struct css_set *cset;
3233
3234 /* Advance to the next non-empty css_set */
3235 do {
3236 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003237 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003238 it->cset_link = NULL;
3239 return;
3240 }
3241 link = list_entry(l, struct cgrp_cset_link, cset_link);
3242 cset = link->cset;
3243 } while (list_empty(&cset->tasks));
3244 it->cset_link = l;
3245 it->task = cset->tasks.next;
3246}
3247
Tejun Heo0942eee2013-08-08 20:11:26 -04003248/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003249 * css_task_iter_start - initiate task iteration
3250 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003251 * @it: the task iterator to use
3252 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003253 * Initiate iteration through the tasks of @css. The caller can call
3254 * css_task_iter_next() to walk through the tasks until the function
3255 * returns NULL. On completion of iteration, css_task_iter_end() must be
3256 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003257 *
3258 * Note that this function acquires a lock which is released when the
3259 * iteration finishes. The caller can't sleep while iteration is in
3260 * progress.
3261 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003262void css_task_iter_start(struct cgroup_subsys_state *css,
3263 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003264 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003265{
3266 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003267 * The first time anyone tries to iterate across a css, we need to
3268 * enable the list linking each css_set to its tasks, and fix up
3269 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003270 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003271 if (!use_task_css_set_links)
3272 cgroup_enable_task_cg_lists();
3273
Paul Menage817929e2007-10-18 23:39:36 -07003274 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003275
Tejun Heo72ec7022013-08-08 20:11:26 -04003276 it->origin_css = css;
3277 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003278
Tejun Heo72ec7022013-08-08 20:11:26 -04003279 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003280}
3281
Tejun Heo0942eee2013-08-08 20:11:26 -04003282/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003283 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003284 * @it: the task iterator being iterated
3285 *
3286 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003287 * initialized via css_task_iter_start(). Returns NULL when the iteration
3288 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003289 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003290struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003291{
3292 struct task_struct *res;
3293 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003294 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003295
3296 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003297 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003298 return NULL;
3299 res = list_entry(l, struct task_struct, cg_list);
3300 /* Advance iterator to find next entry */
3301 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003302 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3303 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003304 /*
3305 * We reached the end of this task list - move on to the
3306 * next cgrp_cset_link.
3307 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003308 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003309 } else {
3310 it->task = l;
3311 }
3312 return res;
3313}
3314
Tejun Heo0942eee2013-08-08 20:11:26 -04003315/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003316 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003317 * @it: the task iterator to finish
3318 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003319 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003320 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003321void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003322 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003323{
3324 read_unlock(&css_set_lock);
3325}
3326
Cliff Wickman31a7df02008-02-07 00:14:42 -08003327static inline int started_after_time(struct task_struct *t1,
3328 struct timespec *time,
3329 struct task_struct *t2)
3330{
3331 int start_diff = timespec_compare(&t1->start_time, time);
3332 if (start_diff > 0) {
3333 return 1;
3334 } else if (start_diff < 0) {
3335 return 0;
3336 } else {
3337 /*
3338 * Arbitrarily, if two processes started at the same
3339 * time, we'll say that the lower pointer value
3340 * started first. Note that t2 may have exited by now
3341 * so this may not be a valid pointer any longer, but
3342 * that's fine - it still serves to distinguish
3343 * between two tasks started (effectively) simultaneously.
3344 */
3345 return t1 > t2;
3346 }
3347}
3348
3349/*
3350 * This function is a callback from heap_insert() and is used to order
3351 * the heap.
3352 * In this case we order the heap in descending task start time.
3353 */
3354static inline int started_after(void *p1, void *p2)
3355{
3356 struct task_struct *t1 = p1;
3357 struct task_struct *t2 = p2;
3358 return started_after_time(t1, &t2->start_time, t2);
3359}
3360
3361/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003362 * css_scan_tasks - iterate though all the tasks in a css
3363 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003364 * @test: optional test callback
3365 * @process: process callback
3366 * @data: data passed to @test and @process
3367 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003368 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003369 * Iterate through all the tasks in @css, calling @test for each, and if it
3370 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003371 *
Tejun Heoe5358372013-08-08 20:11:26 -04003372 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003373 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003374 * lock css_set_lock for the call to @process.
3375 *
3376 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003377 * of @css for the duration of this call. This function may or may not
3378 * call @process for tasks that exit or move to a different css during the
3379 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003380 *
3381 * Note that @test may be called with locks held, and may in some
3382 * situations be called multiple times for the same task, so it should be
3383 * cheap.
3384 *
3385 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3386 * heap operations (and its "gt" member will be overwritten), else a
3387 * temporary heap will be used (allocation of which may cause this function
3388 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003389 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003390int css_scan_tasks(struct cgroup_subsys_state *css,
3391 bool (*test)(struct task_struct *, void *),
3392 void (*process)(struct task_struct *, void *),
3393 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003394{
3395 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003396 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003397 struct task_struct *p, *dropped;
3398 /* Never dereference latest_task, since it's not refcounted */
3399 struct task_struct *latest_task = NULL;
3400 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003401 struct timespec latest_time = { 0, 0 };
3402
Tejun Heoe5358372013-08-08 20:11:26 -04003403 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003404 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003405 heap->gt = &started_after;
3406 } else {
3407 /* We need to allocate our own heap memory */
3408 heap = &tmp_heap;
3409 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3410 if (retval)
3411 /* cannot allocate the heap */
3412 return retval;
3413 }
3414
3415 again:
3416 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003417 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003418 * which are of interest, and invoking @process callback on the
3419 * ones which need an update. Since we don't want to hold any
3420 * locks during the task updates, gather tasks to be processed in a
3421 * heap structure. The heap is sorted by descending task start
3422 * time. If the statically-sized heap fills up, we overflow tasks
3423 * that started later, and in future iterations only consider tasks
3424 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003425 * guarantees forward progress and that we don't miss any tasks.
3426 */
3427 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003428 css_task_iter_start(css, &it);
3429 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003430 /*
3431 * Only affect tasks that qualify per the caller's callback,
3432 * if he provided one
3433 */
Tejun Heoe5358372013-08-08 20:11:26 -04003434 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003435 continue;
3436 /*
3437 * Only process tasks that started after the last task
3438 * we processed
3439 */
3440 if (!started_after_time(p, &latest_time, latest_task))
3441 continue;
3442 dropped = heap_insert(heap, p);
3443 if (dropped == NULL) {
3444 /*
3445 * The new task was inserted; the heap wasn't
3446 * previously full
3447 */
3448 get_task_struct(p);
3449 } else if (dropped != p) {
3450 /*
3451 * The new task was inserted, and pushed out a
3452 * different task
3453 */
3454 get_task_struct(p);
3455 put_task_struct(dropped);
3456 }
3457 /*
3458 * Else the new task was newer than anything already in
3459 * the heap and wasn't inserted
3460 */
3461 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003462 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003463
3464 if (heap->size) {
3465 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003466 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003467 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003468 latest_time = q->start_time;
3469 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003470 }
3471 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003472 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003473 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003474 }
3475 /*
3476 * If we had to process any tasks at all, scan again
3477 * in case some of them were in the middle of forking
3478 * children that didn't get processed.
3479 * Not the most efficient way to do it, but it avoids
3480 * having to take callback_mutex in the fork path
3481 */
3482 goto again;
3483 }
3484 if (heap == &tmp_heap)
3485 heap_free(&tmp_heap);
3486 return 0;
3487}
3488
Tejun Heoe5358372013-08-08 20:11:26 -04003489static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003490{
Tejun Heoe5358372013-08-08 20:11:26 -04003491 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003492
Tejun Heo47cfcd02013-04-07 09:29:51 -07003493 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003494 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003495 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003496}
3497
3498/**
3499 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3500 * @to: cgroup to which the tasks will be moved
3501 * @from: cgroup in which the tasks currently reside
3502 */
3503int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3504{
Tejun Heo72ec7022013-08-08 20:11:26 -04003505 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3506 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003507}
3508
Paul Menage817929e2007-10-18 23:39:36 -07003509/*
Ben Blum102a7752009-09-23 15:56:26 -07003510 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003511 *
3512 * Reading this file can return large amounts of data if a cgroup has
3513 * *lots* of attached tasks. So it may need several calls to read(),
3514 * but we cannot guarantee that the information we produce is correct
3515 * unless we produce it entirely atomically.
3516 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003517 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003518
Li Zefan24528252012-01-20 11:58:43 +08003519/* which pidlist file are we talking about? */
3520enum cgroup_filetype {
3521 CGROUP_FILE_PROCS,
3522 CGROUP_FILE_TASKS,
3523};
3524
3525/*
3526 * A pidlist is a list of pids that virtually represents the contents of one
3527 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3528 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3529 * to the cgroup.
3530 */
3531struct cgroup_pidlist {
3532 /*
3533 * used to find which pidlist is wanted. doesn't change as long as
3534 * this particular list stays in the list.
3535 */
3536 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3537 /* array of xids */
3538 pid_t *list;
3539 /* how many elements the above list has */
3540 int length;
3541 /* how many files are using the current array */
3542 int use_count;
3543 /* each of these stored in a list by its cgroup */
3544 struct list_head links;
3545 /* pointer to the cgroup we belong to, for list removal purposes */
3546 struct cgroup *owner;
3547 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003548 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003549};
3550
Paul Menagebbcb81d2007-10-18 23:39:32 -07003551/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003552 * The following two functions "fix" the issue where there are more pids
3553 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3554 * TODO: replace with a kernel-wide solution to this problem
3555 */
3556#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3557static void *pidlist_allocate(int count)
3558{
3559 if (PIDLIST_TOO_LARGE(count))
3560 return vmalloc(count * sizeof(pid_t));
3561 else
3562 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3563}
3564static void pidlist_free(void *p)
3565{
3566 if (is_vmalloc_addr(p))
3567 vfree(p);
3568 else
3569 kfree(p);
3570}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003571
3572/*
Ben Blum102a7752009-09-23 15:56:26 -07003573 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003574 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003575 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003576static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003577{
Ben Blum102a7752009-09-23 15:56:26 -07003578 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003579
3580 /*
3581 * we presume the 0th element is unique, so i starts at 1. trivial
3582 * edge cases first; no work needs to be done for either
3583 */
3584 if (length == 0 || length == 1)
3585 return length;
3586 /* src and dest walk down the list; dest counts unique elements */
3587 for (src = 1; src < length; src++) {
3588 /* find next unique element */
3589 while (list[src] == list[src-1]) {
3590 src++;
3591 if (src == length)
3592 goto after;
3593 }
3594 /* dest always points to where the next unique element goes */
3595 list[dest] = list[src];
3596 dest++;
3597 }
3598after:
Ben Blum102a7752009-09-23 15:56:26 -07003599 return dest;
3600}
3601
3602static int cmppid(const void *a, const void *b)
3603{
3604 return *(pid_t *)a - *(pid_t *)b;
3605}
3606
3607/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003608 * find the appropriate pidlist for our purpose (given procs vs tasks)
3609 * returns with the lock on that pidlist already held, and takes care
3610 * of the use count, or returns NULL with no locks held if we're out of
3611 * memory.
3612 */
3613static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3614 enum cgroup_filetype type)
3615{
3616 struct cgroup_pidlist *l;
3617 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003618 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003619
Ben Blum72a8cb32009-09-23 15:56:27 -07003620 /*
Li Zefanb3958902013-08-01 09:52:15 +08003621 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003622 * the last ref-holder is trying to remove l from the list at the same
3623 * time. Holding the pidlist_mutex precludes somebody taking whichever
3624 * list we find out from under us - compare release_pid_array().
3625 */
3626 mutex_lock(&cgrp->pidlist_mutex);
3627 list_for_each_entry(l, &cgrp->pidlists, links) {
3628 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003629 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003630 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003631 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003632 return l;
3633 }
3634 }
3635 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003636 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003637 if (!l) {
3638 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003639 return l;
3640 }
Li Zefanb3958902013-08-01 09:52:15 +08003641 init_rwsem(&l->rwsem);
3642 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003644 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003645 l->owner = cgrp;
3646 list_add(&l->links, &cgrp->pidlists);
3647 mutex_unlock(&cgrp->pidlist_mutex);
3648 return l;
3649}
3650
3651/*
Ben Blum102a7752009-09-23 15:56:26 -07003652 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3653 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003654static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3655 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003656{
3657 pid_t *array;
3658 int length;
3659 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003660 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003661 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003662 struct cgroup_pidlist *l;
3663
3664 /*
3665 * If cgroup gets more users after we read count, we won't have
3666 * enough space - tough. This race is indistinguishable to the
3667 * caller from the case that the additional cgroup users didn't
3668 * show up until sometime later on.
3669 */
3670 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003671 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003672 if (!array)
3673 return -ENOMEM;
3674 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003675 css_task_iter_start(&cgrp->dummy_css, &it);
3676 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003677 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003678 break;
Ben Blum102a7752009-09-23 15:56:26 -07003679 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003680 if (type == CGROUP_FILE_PROCS)
3681 pid = task_tgid_vnr(tsk);
3682 else
3683 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003684 if (pid > 0) /* make sure to only use valid results */
3685 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003686 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003687 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003688 length = n;
3689 /* now sort & (if procs) strip out duplicates */
3690 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003691 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003692 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003693 l = cgroup_pidlist_find(cgrp, type);
3694 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003695 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003696 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003697 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003698 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003699 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003700 l->list = array;
3701 l->length = length;
3702 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003703 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003704 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003705 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003706}
3707
Balbir Singh846c7bb2007-10-18 23:39:44 -07003708/**
Li Zefana043e3b2008-02-23 15:24:09 -08003709 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003710 * @stats: cgroupstats to fill information into
3711 * @dentry: A dentry entry belonging to the cgroup for which stats have
3712 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003713 *
3714 * Build and fill cgroupstats so that taskstats can export it to user
3715 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003716 */
3717int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3718{
3719 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003720 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003721 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003722 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003723
Balbir Singh846c7bb2007-10-18 23:39:44 -07003724 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003725 * Validate dentry by checking the superblock operations,
3726 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003727 */
Li Zefan33d283b2008-11-19 15:36:48 -08003728 if (dentry->d_sb->s_op != &cgroup_ops ||
3729 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003730 goto err;
3731
3732 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003733 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003734
Tejun Heo72ec7022013-08-08 20:11:26 -04003735 css_task_iter_start(&cgrp->dummy_css, &it);
3736 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003737 switch (tsk->state) {
3738 case TASK_RUNNING:
3739 stats->nr_running++;
3740 break;
3741 case TASK_INTERRUPTIBLE:
3742 stats->nr_sleeping++;
3743 break;
3744 case TASK_UNINTERRUPTIBLE:
3745 stats->nr_uninterruptible++;
3746 break;
3747 case TASK_STOPPED:
3748 stats->nr_stopped++;
3749 break;
3750 default:
3751 if (delayacct_is_task_waiting_on_io(tsk))
3752 stats->nr_io_wait++;
3753 break;
3754 }
3755 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003756 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003757
Balbir Singh846c7bb2007-10-18 23:39:44 -07003758err:
3759 return ret;
3760}
3761
Paul Menage8f3ff202009-09-23 15:56:25 -07003762
Paul Menagecc31edc2008-10-18 20:28:04 -07003763/*
Ben Blum102a7752009-09-23 15:56:26 -07003764 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003765 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003766 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003767 */
3768
Ben Blum102a7752009-09-23 15:56:26 -07003769static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003770{
3771 /*
3772 * Initially we receive a position value that corresponds to
3773 * one more than the last pid shown (or 0 on the first call or
3774 * after a seek to the start). Use a binary-search to find the
3775 * next pid to display, if any
3776 */
Ben Blum102a7752009-09-23 15:56:26 -07003777 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003778 int index = 0, pid = *pos;
3779 int *iter;
3780
Li Zefanb3958902013-08-01 09:52:15 +08003781 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003782 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003783 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003784
Paul Menagecc31edc2008-10-18 20:28:04 -07003785 while (index < end) {
3786 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003787 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003788 index = mid;
3789 break;
Ben Blum102a7752009-09-23 15:56:26 -07003790 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003791 index = mid + 1;
3792 else
3793 end = mid;
3794 }
3795 }
3796 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003797 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003798 return NULL;
3799 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003800 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003801 *pos = *iter;
3802 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003803}
3804
Ben Blum102a7752009-09-23 15:56:26 -07003805static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003806{
Ben Blum102a7752009-09-23 15:56:26 -07003807 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003808 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003809}
3810
Ben Blum102a7752009-09-23 15:56:26 -07003811static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003812{
Ben Blum102a7752009-09-23 15:56:26 -07003813 struct cgroup_pidlist *l = s->private;
3814 pid_t *p = v;
3815 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003816 /*
3817 * Advance to the next pid in the array. If this goes off the
3818 * end, we're done
3819 */
3820 p++;
3821 if (p >= end) {
3822 return NULL;
3823 } else {
3824 *pos = *p;
3825 return p;
3826 }
3827}
3828
Ben Blum102a7752009-09-23 15:56:26 -07003829static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003830{
3831 return seq_printf(s, "%d\n", *(int *)v);
3832}
3833
Ben Blum102a7752009-09-23 15:56:26 -07003834/*
3835 * seq_operations functions for iterating on pidlists through seq_file -
3836 * independent of whether it's tasks or procs
3837 */
3838static const struct seq_operations cgroup_pidlist_seq_operations = {
3839 .start = cgroup_pidlist_start,
3840 .stop = cgroup_pidlist_stop,
3841 .next = cgroup_pidlist_next,
3842 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003843};
3844
Ben Blum102a7752009-09-23 15:56:26 -07003845static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003846{
Ben Blum72a8cb32009-09-23 15:56:27 -07003847 /*
3848 * the case where we're the last user of this particular pidlist will
3849 * have us remove it from the cgroup's list, which entails taking the
3850 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3851 * pidlist_mutex, we have to take pidlist_mutex first.
3852 */
3853 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003854 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003855 BUG_ON(!l->use_count);
3856 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003857 /* we're the last user if refcount is 0; remove and free */
3858 list_del(&l->links);
3859 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003860 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003861 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003862 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003863 kfree(l);
3864 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003865 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003866 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003867 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003868}
3869
Ben Blum102a7752009-09-23 15:56:26 -07003870static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003871{
Ben Blum102a7752009-09-23 15:56:26 -07003872 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003873 if (!(file->f_mode & FMODE_READ))
3874 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003875 /*
3876 * the seq_file will only be initialized if the file was opened for
3877 * reading; hence we check if it's not null only in that case.
3878 */
3879 l = ((struct seq_file *)file->private_data)->private;
3880 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003881 return seq_release(inode, file);
3882}
3883
Ben Blum102a7752009-09-23 15:56:26 -07003884static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003885 .read = seq_read,
3886 .llseek = seq_lseek,
3887 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003888 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003889};
3890
3891/*
Ben Blum102a7752009-09-23 15:56:26 -07003892 * The following functions handle opens on a file that displays a pidlist
3893 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3894 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003895 */
Ben Blum102a7752009-09-23 15:56:26 -07003896/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003897static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003898{
3899 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003900 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003901 int retval;
3902
3903 /* Nothing to do for write-only files */
3904 if (!(file->f_mode & FMODE_READ))
3905 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003906
Ben Blum102a7752009-09-23 15:56:26 -07003907 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003908 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003909 if (retval)
3910 return retval;
3911 /* configure file information */
3912 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003913
Ben Blum102a7752009-09-23 15:56:26 -07003914 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003915 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003916 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003917 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003918 }
Ben Blum102a7752009-09-23 15:56:26 -07003919 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003920 return 0;
3921}
Ben Blum102a7752009-09-23 15:56:26 -07003922static int cgroup_tasks_open(struct inode *unused, struct file *file)
3923{
Ben Blum72a8cb32009-09-23 15:56:27 -07003924 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003925}
3926static int cgroup_procs_open(struct inode *unused, struct file *file)
3927{
Ben Blum72a8cb32009-09-23 15:56:27 -07003928 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003929}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003930
Tejun Heo182446d2013-08-08 20:11:24 -04003931static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3932 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003933{
Tejun Heo182446d2013-08-08 20:11:24 -04003934 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003935}
3936
Tejun Heo182446d2013-08-08 20:11:24 -04003937static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3938 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003939{
Tejun Heo182446d2013-08-08 20:11:24 -04003940 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003941 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003942 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003943 else
Tejun Heo182446d2013-08-08 20:11:24 -04003944 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003945 return 0;
3946}
3947
Paul Menagebbcb81d2007-10-18 23:39:32 -07003948/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003949 * When dput() is called asynchronously, if umount has been done and
3950 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3951 * there's a small window that vfs will see the root dentry with non-zero
3952 * refcnt and trigger BUG().
3953 *
3954 * That's why we hold a reference before dput() and drop it right after.
3955 */
3956static void cgroup_dput(struct cgroup *cgrp)
3957{
3958 struct super_block *sb = cgrp->root->sb;
3959
3960 atomic_inc(&sb->s_active);
3961 dput(cgrp->dentry);
3962 deactivate_super(sb);
3963}
3964
3965/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003966 * Unregister event and free resources.
3967 *
3968 * Gets called from workqueue.
3969 */
3970static void cgroup_event_remove(struct work_struct *work)
3971{
3972 struct cgroup_event *event = container_of(work, struct cgroup_event,
3973 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003974 struct cgroup_subsys_state *css = event->css;
3975 struct cgroup *cgrp = css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003976
Li Zefan810cbee2013-02-18 18:56:14 +08003977 remove_wait_queue(event->wqh, &event->wait);
3978
Tejun Heo81eeaf02013-08-08 20:11:26 -04003979 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003980
Li Zefan810cbee2013-02-18 18:56:14 +08003981 /* Notify userspace the event is going away. */
3982 eventfd_signal(event->eventfd, 1);
3983
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003984 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003985 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003986 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003987}
3988
3989/*
3990 * Gets called on POLLHUP on eventfd when user closes it.
3991 *
3992 * Called with wqh->lock held and interrupts disabled.
3993 */
3994static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3995 int sync, void *key)
3996{
3997 struct cgroup_event *event = container_of(wait,
3998 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003999 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004000 unsigned long flags = (unsigned long)key;
4001
4002 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004003 /*
Li Zefan810cbee2013-02-18 18:56:14 +08004004 * If the event has been detached at cgroup removal, we
4005 * can simply return knowing the other side will cleanup
4006 * for us.
4007 *
4008 * We can't race against event freeing since the other
4009 * side will require wqh->lock via remove_wait_queue(),
4010 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004011 */
Li Zefan810cbee2013-02-18 18:56:14 +08004012 spin_lock(&cgrp->event_list_lock);
4013 if (!list_empty(&event->list)) {
4014 list_del_init(&event->list);
4015 /*
4016 * We are in atomic context, but cgroup_event_remove()
4017 * may sleep, so we have to call it in workqueue.
4018 */
4019 schedule_work(&event->remove);
4020 }
4021 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004022 }
4023
4024 return 0;
4025}
4026
4027static void cgroup_event_ptable_queue_proc(struct file *file,
4028 wait_queue_head_t *wqh, poll_table *pt)
4029{
4030 struct cgroup_event *event = container_of(pt,
4031 struct cgroup_event, pt);
4032
4033 event->wqh = wqh;
4034 add_wait_queue(wqh, &event->wait);
4035}
4036
4037/*
4038 * Parse input and register new cgroup event handler.
4039 *
4040 * Input must be in format '<event_fd> <control_fd> <args>'.
4041 * Interpretation of args is defined by control file implementation.
4042 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004043static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
Tejun Heo182446d2013-08-08 20:11:24 -04004044 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004045{
Tejun Heo6e6eab02013-08-15 11:43:15 -04004046 struct cgroup *cgrp = dummy_css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004047 struct cgroup_event *event;
Li Zefanf1690072013-02-18 14:13:35 +08004048 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004049 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08004050 struct file *efile;
4051 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004052 char *endp;
4053 int ret;
4054
4055 efd = simple_strtoul(buffer, &endp, 10);
4056 if (*endp != ' ')
4057 return -EINVAL;
4058 buffer = endp + 1;
4059
4060 cfd = simple_strtoul(buffer, &endp, 10);
4061 if ((*endp != ' ') && (*endp != '\0'))
4062 return -EINVAL;
4063 buffer = endp + 1;
4064
4065 event = kzalloc(sizeof(*event), GFP_KERNEL);
4066 if (!event)
4067 return -ENOMEM;
Tejun Heo6e6eab02013-08-15 11:43:15 -04004068
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004069 INIT_LIST_HEAD(&event->list);
4070 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4071 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4072 INIT_WORK(&event->remove, cgroup_event_remove);
4073
4074 efile = eventfd_fget(efd);
4075 if (IS_ERR(efile)) {
4076 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004077 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004078 }
4079
4080 event->eventfd = eventfd_ctx_fileget(efile);
4081 if (IS_ERR(event->eventfd)) {
4082 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004083 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004084 }
4085
4086 cfile = fget(cfd);
4087 if (!cfile) {
4088 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004089 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004090 }
4091
4092 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004093 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004094 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004095 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004096 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004097
4098 event->cft = __file_cft(cfile);
4099 if (IS_ERR(event->cft)) {
4100 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004101 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004102 }
4103
Tejun Heo6e6eab02013-08-15 11:43:15 -04004104 if (!event->cft->ss) {
4105 ret = -EBADF;
4106 goto out_put_cfile;
4107 }
4108
4109 /* determine the css of @cfile and associate @event with it */
4110 rcu_read_lock();
4111
4112 ret = -EINVAL;
4113 event->css = cgroup_css(cgrp, event->cft->ss->subsys_id);
4114 if (event->css)
4115 ret = 0;
4116
4117 rcu_read_unlock();
4118 if (ret)
4119 goto out_put_cfile;
4120
Li Zefanf1690072013-02-18 14:13:35 +08004121 /*
4122 * The file to be monitored must be in the same cgroup as
4123 * cgroup.event_control is.
4124 */
4125 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4126 if (cgrp_cfile != cgrp) {
4127 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004128 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004129 }
4130
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004131 if (!event->cft->register_event || !event->cft->unregister_event) {
4132 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004133 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004134 }
4135
Tejun Heo6e6eab02013-08-15 11:43:15 -04004136 ret = event->cft->register_event(event->css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004137 event->eventfd, buffer);
4138 if (ret)
Li Zefan876ede82013-08-01 09:51:47 +08004139 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004140
Li Zefan7ef70e42013-04-26 11:58:03 -07004141 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004142
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004143 /*
4144 * Events should be removed after rmdir of cgroup directory, but before
4145 * destroying subsystem state objects. Let's take reference to cgroup
4146 * directory dentry to do that.
4147 */
4148 dget(cgrp->dentry);
4149
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004150 spin_lock(&cgrp->event_list_lock);
4151 list_add(&event->list, &cgrp->event_list);
4152 spin_unlock(&cgrp->event_list_lock);
4153
4154 fput(cfile);
4155 fput(efile);
4156
4157 return 0;
4158
Li Zefan876ede82013-08-01 09:51:47 +08004159out_put_cfile:
4160 fput(cfile);
4161out_put_eventfd:
4162 eventfd_ctx_put(event->eventfd);
4163out_put_efile:
4164 fput(efile);
4165out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004166 kfree(event);
4167
4168 return ret;
4169}
4170
Tejun Heo182446d2013-08-08 20:11:24 -04004171static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4172 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004173{
Tejun Heo182446d2013-08-08 20:11:24 -04004174 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004175}
4176
Tejun Heo182446d2013-08-08 20:11:24 -04004177static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4178 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004179{
4180 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004181 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004182 else
Tejun Heo182446d2013-08-08 20:11:24 -04004183 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004184 return 0;
4185}
4186
Tejun Heod5c56ce2013-06-03 19:14:34 -07004187static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004188 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004189 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004190 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004191 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004192 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004193 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004194 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004195 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004196 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004197 .write_string = cgroup_write_event_control,
4198 .mode = S_IWUGO,
4199 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004200 {
4201 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004202 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004203 .read_u64 = cgroup_clone_children_read,
4204 .write_u64 = cgroup_clone_children_write,
4205 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004206 {
Tejun Heo873fe092013-04-14 20:15:26 -07004207 .name = "cgroup.sane_behavior",
4208 .flags = CFTYPE_ONLY_ON_ROOT,
4209 .read_seq_string = cgroup_sane_behavior_show,
4210 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004211
4212 /*
4213 * Historical crazy stuff. These don't have "cgroup." prefix and
4214 * don't exist if sane_behavior. If you're depending on these, be
4215 * prepared to be burned.
4216 */
4217 {
4218 .name = "tasks",
4219 .flags = CFTYPE_INSANE, /* use "procs" instead */
4220 .open = cgroup_tasks_open,
4221 .write_u64 = cgroup_tasks_write,
4222 .release = cgroup_pidlist_release,
4223 .mode = S_IRUGO | S_IWUSR,
4224 },
4225 {
4226 .name = "notify_on_release",
4227 .flags = CFTYPE_INSANE,
4228 .read_u64 = cgroup_read_notify_on_release,
4229 .write_u64 = cgroup_write_notify_on_release,
4230 },
Tejun Heo873fe092013-04-14 20:15:26 -07004231 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004232 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004233 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004234 .read_seq_string = cgroup_release_agent_show,
4235 .write_string = cgroup_release_agent_write,
4236 .max_write_len = PATH_MAX,
4237 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004238 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004239};
4240
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004241/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004242 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004243 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004244 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004245 *
4246 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004247 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004248static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004251 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004252
Tejun Heo8e3f6542012-04-01 12:09:55 -07004253 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004254 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004255 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004256
4257 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004258 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004259
Tejun Heobee55092013-06-28 16:24:11 -07004260 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004261 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004262 if (ret < 0)
4263 goto err;
4264 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004265 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004266
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004267 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004268 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04004269 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004270 struct css_id *id = rcu_dereference_protected(css->id, true);
4271
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004272 /*
4273 * Update id->css pointer and make this css visible from
4274 * CSS ID functions. This pointer will be dereferened
4275 * from RCU-read-side without locks.
4276 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004277 if (id)
4278 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004279 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280
4281 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004282err:
4283 cgroup_clear_dir(cgrp, subsys_mask);
4284 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004285}
4286
Tejun Heo0c21ead2013-08-13 20:22:51 -04004287/*
4288 * css destruction is four-stage process.
4289 *
4290 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4291 * Implemented in kill_css().
4292 *
4293 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4294 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4295 * by invoking offline_css(). After offlining, the base ref is put.
4296 * Implemented in css_killed_work_fn().
4297 *
4298 * 3. When the percpu_ref reaches zero, the only possible remaining
4299 * accessors are inside RCU read sections. css_release() schedules the
4300 * RCU callback.
4301 *
4302 * 4. After the grace period, the css can be freed. Implemented in
4303 * css_free_work_fn().
4304 *
4305 * It is actually hairier because both step 2 and 4 require process context
4306 * and thus involve punting to css->destroy_work adding two additional
4307 * steps to the already complex sequence.
4308 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004309static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004310{
4311 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004312 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004313 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004314
Tejun Heo0ae78e02013-08-13 11:01:54 -04004315 if (css->parent)
4316 css_put(css->parent);
4317
Tejun Heo0c21ead2013-08-13 20:22:51 -04004318 css->ss->css_free(css);
4319 cgroup_dput(cgrp);
4320}
4321
4322static void css_free_rcu_fn(struct rcu_head *rcu_head)
4323{
4324 struct cgroup_subsys_state *css =
4325 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4326
4327 /*
4328 * css holds an extra ref to @cgrp->dentry which is put on the last
4329 * css_put(). dput() requires process context which we don't have.
4330 */
4331 INIT_WORK(&css->destroy_work, css_free_work_fn);
4332 schedule_work(&css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004333}
4334
Tejun Heod3daf282013-06-13 19:39:16 -07004335static void css_release(struct percpu_ref *ref)
4336{
4337 struct cgroup_subsys_state *css =
4338 container_of(ref, struct cgroup_subsys_state, refcnt);
4339
Tejun Heo0c21ead2013-08-13 20:22:51 -04004340 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004341}
4342
Tejun Heo623f9262013-08-13 11:01:55 -04004343static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4344 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004345{
Paul Menagebd89aab2007-10-18 23:40:44 -07004346 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004347 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004349 css->id = NULL;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004350
4351 if (cgrp->parent)
4352 css->parent = cgroup_css(cgrp->parent, ss->subsys_id);
4353 else
Tejun Heo38b53ab2012-11-19 08:13:36 -08004354 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004355
Tejun Heo40e93b32013-08-13 11:01:53 -04004356 BUG_ON(cgroup_css(cgrp, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004357}
4358
Li Zefan2a4ac632013-07-31 16:16:40 +08004359/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004360static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004361{
Tejun Heo623f9262013-08-13 11:01:55 -04004362 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004363 int ret = 0;
4364
Tejun Heoa31f2d32012-11-19 08:13:37 -08004365 lockdep_assert_held(&cgroup_mutex);
4366
Tejun Heo92fb9742012-11-19 08:13:38 -08004367 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004368 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004369 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004370 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004371 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004372 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4373 }
Tejun Heob1929db2012-11-19 08:13:38 -08004374 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004375}
4376
Li Zefan2a4ac632013-07-31 16:16:40 +08004377/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004378static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004379{
Tejun Heo623f9262013-08-13 11:01:55 -04004380 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004381
4382 lockdep_assert_held(&cgroup_mutex);
4383
4384 if (!(css->flags & CSS_ONLINE))
4385 return;
4386
Li Zefand7eeac12013-03-12 15:35:59 -07004387 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004388 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004389
Tejun Heoeb954192013-08-08 20:11:23 -04004390 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004391 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004392 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004393}
4394
Paul Menageddbcc7e2007-10-18 23:39:30 -07004395/*
Li Zefana043e3b2008-02-23 15:24:09 -08004396 * cgroup_create - create a cgroup
4397 * @parent: cgroup that will be parent of the new cgroup
4398 * @dentry: dentry of the new cgroup
4399 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004400 *
Li Zefana043e3b2008-02-23 15:24:09 -08004401 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004402 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004403static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004404 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004405{
Tejun Heoae7f1642013-08-13 20:22:50 -04004406 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004407 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004408 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004409 struct cgroupfs_root *root = parent->root;
4410 int err = 0;
4411 struct cgroup_subsys *ss;
4412 struct super_block *sb = root->sb;
4413
Tejun Heo0a950f62012-11-19 09:02:12 -08004414 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004415 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4416 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004417 return -ENOMEM;
4418
Li Zefan65dff752013-03-01 15:01:56 +08004419 name = cgroup_alloc_name(dentry);
4420 if (!name)
4421 goto err_free_cgrp;
4422 rcu_assign_pointer(cgrp->name, name);
4423
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004424 /*
4425 * Temporarily set the pointer to NULL, so idr_find() won't return
4426 * a half-baked cgroup.
4427 */
4428 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004429 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004430 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004431
Tejun Heo976c06b2012-11-05 09:16:59 -08004432 /*
4433 * Only live parents can have children. Note that the liveliness
4434 * check isn't strictly necessary because cgroup_mkdir() and
4435 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4436 * anyway so that locking is contained inside cgroup proper and we
4437 * don't get nasty surprises if we ever grow another caller.
4438 */
4439 if (!cgroup_lock_live_group(parent)) {
4440 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004441 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004442 }
4443
Paul Menageddbcc7e2007-10-18 23:39:30 -07004444 /* Grab a reference on the superblock so the hierarchy doesn't
4445 * get deleted on unmount if there are child cgroups. This
4446 * can be done outside cgroup_mutex, since the sb can't
4447 * disappear while someone has an open control file on the
4448 * fs */
4449 atomic_inc(&sb->s_active);
4450
Paul Menagecc31edc2008-10-18 20:28:04 -07004451 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452
Li Zefanfe1c06c2013-01-24 14:30:22 +08004453 dentry->d_fsdata = cgrp;
4454 cgrp->dentry = dentry;
4455
Paul Menagebd89aab2007-10-18 23:40:44 -07004456 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004457 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004458 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004459
Li Zefanb6abdb02008-03-04 14:28:19 -08004460 if (notify_on_release(parent))
4461 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4462
Tejun Heo2260e7f2012-11-19 08:13:38 -08004463 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4464 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004465
Tejun Heo5549c492013-06-24 15:21:48 -07004466 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004467 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004468
Tejun Heo40e93b32013-08-13 11:01:53 -04004469 css = ss->css_alloc(cgroup_css(parent, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004470 if (IS_ERR(css)) {
4471 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004472 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004473 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004474 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004475
4476 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004477 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004478 goto err_free_all;
4479
Tejun Heo623f9262013-08-13 11:01:55 -04004480 init_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004481
Li Zefan4528fd02010-02-02 13:44:10 -08004482 if (ss->use_id) {
Tejun Heo623f9262013-08-13 11:01:55 -04004483 err = alloc_css_id(css);
Li Zefan4528fd02010-02-02 13:44:10 -08004484 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004485 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004486 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004487 }
4488
Tejun Heo4e139af2012-11-19 08:13:36 -08004489 /*
4490 * Create directory. cgroup_create_file() returns with the new
4491 * directory locked on success so that it can be populated without
4492 * dropping cgroup_mutex.
4493 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004494 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004495 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004496 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004497 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004498
Tejun Heo00356bd2013-06-18 11:14:22 -07004499 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004500
Tejun Heo4e139af2012-11-19 08:13:36 -08004501 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004502 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4503 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004504
Tejun Heo0ae78e02013-08-13 11:01:54 -04004505 /* each css holds a ref to the cgroup's dentry and the parent css */
4506 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004507 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo0ae78e02013-08-13 11:01:54 -04004508
Tejun Heoed9577932012-11-05 09:16:58 -08004509 dget(dentry);
Li Zhong930913a2013-08-16 17:57:14 +08004510 css_get(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004511 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004512
Li Zefan415cf072013-04-08 14:35:02 +08004513 /* hold a ref to the parent's dentry */
4514 dget(parent->dentry);
4515
Tejun Heob1929db2012-11-19 08:13:38 -08004516 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004517 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004518 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004519
4520 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004521 if (err)
4522 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004523
4524 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4525 parent->parent) {
4526 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",
4527 current->comm, current->pid, ss->name);
4528 if (!strcmp(ss->name, "memory"))
4529 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4530 ss->warned_broken_hierarchy = true;
4531 }
Tejun Heoa8638032012-11-09 09:12:29 -08004532 }
4533
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004534 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4535
Tejun Heo2bb566c2013-08-08 20:11:23 -04004536 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004537 if (err)
4538 goto err_destroy;
4539
4540 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004541 if (err)
4542 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004543
4544 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004545 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004546
4547 return 0;
4548
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004549err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004550 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004551 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004552
4553 if (css) {
4554 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004555 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004556 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004557 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004558 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004559 /* Release the reference count that we took on the superblock */
4560 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004561err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004562 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004563err_free_name:
4564 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004565err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004566 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004567 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004568
4569err_destroy:
4570 cgroup_destroy_locked(cgrp);
4571 mutex_unlock(&cgroup_mutex);
4572 mutex_unlock(&dentry->d_inode->i_mutex);
4573 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004574}
4575
Al Viro18bb1db2011-07-26 01:41:39 -04004576static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004577{
4578 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4579
4580 /* the vfs holds inode->i_mutex already */
4581 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4582}
4583
Tejun Heo223dbc32013-08-13 20:22:50 -04004584/*
4585 * This is called when the refcnt of a css is confirmed to be killed.
4586 * css_tryget() is now guaranteed to fail.
4587 */
4588static void css_killed_work_fn(struct work_struct *work)
4589{
4590 struct cgroup_subsys_state *css =
4591 container_of(work, struct cgroup_subsys_state, destroy_work);
4592 struct cgroup *cgrp = css->cgroup;
4593
Tejun Heof20104d2013-08-13 20:22:50 -04004594 mutex_lock(&cgroup_mutex);
4595
4596 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004597 * css_tryget() is guaranteed to fail now. Tell subsystems to
4598 * initate destruction.
4599 */
4600 offline_css(css);
4601
4602 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004603 * If @cgrp is marked dead, it's waiting for refs of all css's to
4604 * be disabled before proceeding to the second phase of cgroup
4605 * destruction. If we are the last one, kick it off.
4606 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004607 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004608 cgroup_destroy_css_killed(cgrp);
4609
4610 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004611
4612 /*
4613 * Put the css refs from kill_css(). Each css holds an extra
4614 * reference to the cgroup's dentry and cgroup removal proceeds
4615 * regardless of css refs. On the last put of each css, whenever
4616 * that may be, the extra dentry ref is put so that dentry
4617 * destruction happens only after all css's are released.
4618 */
4619 css_put(css);
Tejun Heo223dbc32013-08-13 20:22:50 -04004620}
4621
4622/* css kill confirmation processing requires process context, bounce */
4623static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004624{
4625 struct cgroup_subsys_state *css =
4626 container_of(ref, struct cgroup_subsys_state, refcnt);
4627
Tejun Heo223dbc32013-08-13 20:22:50 -04004628 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4629 schedule_work(&css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004630}
4631
4632/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004633 * kill_css - destroy a css
4634 * @css: css to destroy
4635 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004636 * This function initiates destruction of @css by removing cgroup interface
4637 * files and putting its base reference. ->css_offline() will be invoked
4638 * asynchronously once css_tryget() is guaranteed to fail and when the
4639 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004640 */
4641static void kill_css(struct cgroup_subsys_state *css)
4642{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004643 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4644
Tejun Heoedae0c32013-08-13 20:22:51 -04004645 /*
4646 * Killing would put the base ref, but we need to keep it alive
4647 * until after ->css_offline().
4648 */
4649 css_get(css);
4650
4651 /*
4652 * cgroup core guarantees that, by the time ->css_offline() is
4653 * invoked, no new css reference will be given out via
4654 * css_tryget(). We can't simply call percpu_ref_kill() and
4655 * proceed to offlining css's because percpu_ref_kill() doesn't
4656 * guarantee that the ref is seen as killed on all CPUs on return.
4657 *
4658 * Use percpu_ref_kill_and_confirm() to get notifications as each
4659 * css is confirmed to be seen as killed on all CPUs.
4660 */
4661 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
4662}
4663
4664/**
Tejun Heod3daf282013-06-13 19:39:16 -07004665 * cgroup_destroy_locked - the first stage of cgroup destruction
4666 * @cgrp: cgroup to be destroyed
4667 *
4668 * css's make use of percpu refcnts whose killing latency shouldn't be
4669 * exposed to userland and are RCU protected. Also, cgroup core needs to
4670 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4671 * invoked. To satisfy all the requirements, destruction is implemented in
4672 * the following two steps.
4673 *
4674 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4675 * userland visible parts and start killing the percpu refcnts of
4676 * css's. Set up so that the next stage will be kicked off once all
4677 * the percpu refcnts are confirmed to be killed.
4678 *
4679 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4680 * rest of destruction. Once all cgroup references are gone, the
4681 * cgroup is RCU-freed.
4682 *
4683 * This function implements s1. After this step, @cgrp is gone as far as
4684 * the userland is concerned and a new cgroup with the same name may be
4685 * created. As cgroup doesn't care about the names internally, this
4686 * doesn't cause any problem.
4687 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004688static int cgroup_destroy_locked(struct cgroup *cgrp)
4689 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004690{
Tejun Heo42809dd2012-11-19 08:13:37 -08004691 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004692 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004693 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004694 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004695
Tejun Heo42809dd2012-11-19 08:13:37 -08004696 lockdep_assert_held(&d->d_inode->i_mutex);
4697 lockdep_assert_held(&cgroup_mutex);
4698
Tejun Heoddd69142013-06-12 21:04:54 -07004699 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004700 * css_set_lock synchronizes access to ->cset_links and prevents
4701 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004702 */
4703 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004704 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004705 read_unlock(&css_set_lock);
4706 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004707 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004708
Tejun Heo1a90dd52012-11-05 09:16:59 -08004709 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004710 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4711 * will be invoked to perform the rest of destruction once the
4712 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004713 */
Tejun Heoedae0c32013-08-13 20:22:51 -04004714 for_each_root_subsys(cgrp->root, ss)
4715 kill_css(cgroup_css(cgrp, ss->subsys_id));
Tejun Heo455050d2013-06-13 19:27:41 -07004716
4717 /*
4718 * Mark @cgrp dead. This prevents further task migration and child
4719 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004720 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004721 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004722 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004723 */
Tejun Heo54766d42013-06-12 21:04:53 -07004724 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004725
Tejun Heo455050d2013-06-13 19:27:41 -07004726 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4727 raw_spin_lock(&release_list_lock);
4728 if (!list_empty(&cgrp->release_list))
4729 list_del_init(&cgrp->release_list);
4730 raw_spin_unlock(&release_list_lock);
4731
4732 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004733 * If @cgrp has css's attached, the second stage of cgroup
4734 * destruction is kicked off from css_killed_work_fn() after the
4735 * refs of all attached css's are killed. If @cgrp doesn't have
4736 * any css, we kick it off here.
4737 */
4738 if (!cgrp->nr_css)
4739 cgroup_destroy_css_killed(cgrp);
4740
4741 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004742 * Clear the base files and remove @cgrp directory. The removal
4743 * puts the base ref but we aren't quite done with @cgrp yet, so
4744 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004745 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004746 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004747 dget(d);
4748 cgroup_d_remove_dir(d);
4749
4750 /*
4751 * Unregister events and notify userspace.
4752 * Notify userspace about cgroup removing only after rmdir of cgroup
4753 * directory to avoid race between userspace and kernelspace.
4754 */
4755 spin_lock(&cgrp->event_list_lock);
4756 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4757 list_del_init(&event->list);
4758 schedule_work(&event->remove);
4759 }
4760 spin_unlock(&cgrp->event_list_lock);
4761
Tejun Heoea15f8c2013-06-13 19:27:42 -07004762 return 0;
4763};
4764
Tejun Heod3daf282013-06-13 19:39:16 -07004765/**
Tejun Heof20104d2013-08-13 20:22:50 -04004766 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004767 * @work: cgroup->destroy_free_work
4768 *
4769 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004770 * destroyed after all css's are offlined and performs the rest of
4771 * destruction. This is the second step of destruction described in the
4772 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004773 */
Tejun Heof20104d2013-08-13 20:22:50 -04004774static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004775{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004776 struct cgroup *parent = cgrp->parent;
4777 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004778
Tejun Heof20104d2013-08-13 20:22:50 -04004779 lockdep_assert_held(&cgroup_mutex);
Tejun Heoea15f8c2013-06-13 19:27:42 -07004780
Paul Menage999cd8a2009-01-07 18:08:36 -08004781 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004782 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004783
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004784 /*
4785 * We should remove the cgroup object from idr before its grace
4786 * period starts, so we won't be looking up a cgroup while the
4787 * cgroup is being freed.
4788 */
4789 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4790 cgrp->id = -1;
4791
Paul Menageddbcc7e2007-10-18 23:39:30 -07004792 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004793
Paul Menagebd89aab2007-10-18 23:40:44 -07004794 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004795 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004796}
4797
Tejun Heo42809dd2012-11-19 08:13:37 -08004798static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4799{
4800 int ret;
4801
4802 mutex_lock(&cgroup_mutex);
4803 ret = cgroup_destroy_locked(dentry->d_fsdata);
4804 mutex_unlock(&cgroup_mutex);
4805
4806 return ret;
4807}
4808
Tejun Heo8e3f6542012-04-01 12:09:55 -07004809static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4810{
4811 INIT_LIST_HEAD(&ss->cftsets);
4812
4813 /*
4814 * base_cftset is embedded in subsys itself, no need to worry about
4815 * deregistration.
4816 */
4817 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004818 struct cftype *cft;
4819
4820 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4821 cft->ss = ss;
4822
Tejun Heo8e3f6542012-04-01 12:09:55 -07004823 ss->base_cftset.cfts = ss->base_cftypes;
4824 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4825 }
4826}
4827
Li Zefan06a11922008-04-29 01:00:07 -07004828static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004829{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004830 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004831
4832 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004833
Tejun Heo648bb562012-11-19 08:13:36 -08004834 mutex_lock(&cgroup_mutex);
4835
Tejun Heo8e3f6542012-04-01 12:09:55 -07004836 /* init base cftset */
4837 cgroup_init_cftsets(ss);
4838
Paul Menageddbcc7e2007-10-18 23:39:30 -07004839 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004840 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4841 ss->root = &cgroup_dummy_root;
Tejun Heo40e93b32013-08-13 11:01:53 -04004842 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004843 /* We don't handle early failures gracefully */
4844 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004845 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004846
Li Zefane8d55fd2008-04-29 01:00:13 -07004847 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004848 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004849 * newly registered, all tasks and hence the
4850 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004851 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004852
4853 need_forkexit_callback |= ss->fork || ss->exit;
4854
Li Zefane8d55fd2008-04-29 01:00:13 -07004855 /* At system boot, before all subsystems have been
4856 * registered, no tasks have been forked, so we don't
4857 * need to invoke fork callbacks here. */
4858 BUG_ON(!list_empty(&init_task.tasks));
4859
Tejun Heoae7f1642013-08-13 20:22:50 -04004860 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004861
Tejun Heo648bb562012-11-19 08:13:36 -08004862 mutex_unlock(&cgroup_mutex);
4863
Ben Blume6a11052010-03-10 15:22:09 -08004864 /* this function shouldn't be used with modular subsystems, since they
4865 * need to register a subsys_id, among other things */
4866 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004867}
4868
4869/**
Ben Blume6a11052010-03-10 15:22:09 -08004870 * cgroup_load_subsys: load and register a modular subsystem at runtime
4871 * @ss: the subsystem to load
4872 *
4873 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004874 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004875 * up for use. If the subsystem is built-in anyway, work is delegated to the
4876 * simpler cgroup_init_subsys.
4877 */
4878int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4879{
Ben Blume6a11052010-03-10 15:22:09 -08004880 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004881 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004882 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004883 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004884 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004885
4886 /* check name and function validity */
4887 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004888 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004889 return -EINVAL;
4890
4891 /*
4892 * we don't support callbacks in modular subsystems. this check is
4893 * before the ss->module check for consistency; a subsystem that could
4894 * be a module should still have no callbacks even if the user isn't
4895 * compiling it as one.
4896 */
4897 if (ss->fork || ss->exit)
4898 return -EINVAL;
4899
4900 /*
4901 * an optionally modular subsystem is built-in: we want to do nothing,
4902 * since cgroup_init_subsys will have already taken care of it.
4903 */
4904 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004905 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004906 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004907 return 0;
4908 }
4909
Tejun Heo8e3f6542012-04-01 12:09:55 -07004910 /* init base cftset */
4911 cgroup_init_cftsets(ss);
4912
Ben Blume6a11052010-03-10 15:22:09 -08004913 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004914 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004915
4916 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004917 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004918 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004919 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004920 */
Tejun Heo40e93b32013-08-13 11:01:53 -04004921 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Ben Blume6a11052010-03-10 15:22:09 -08004922 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004923 /* failure case - need to deassign the cgroup_subsys[] slot. */
4924 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004925 mutex_unlock(&cgroup_mutex);
4926 return PTR_ERR(css);
4927 }
4928
Tejun Heo9871bf92013-06-24 15:21:47 -07004929 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4930 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004931
4932 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004933 init_css(css, ss, cgroup_dummy_top);
4934 /* init_idr must be after init_css() because it sets css->id. */
Ben Blume6a11052010-03-10 15:22:09 -08004935 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004936 ret = cgroup_init_idr(ss, css);
4937 if (ret)
4938 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004939 }
4940
4941 /*
4942 * Now we need to entangle the css into the existing css_sets. unlike
4943 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4944 * will need a new pointer to it; done by iterating the css_set_table.
4945 * furthermore, modifying the existing css_sets will corrupt the hash
4946 * table state, so each changed css_set will need its hash recomputed.
4947 * this is all done under the css_set_lock.
4948 */
4949 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004950 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004951 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004952 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004953 continue;
4954 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004955 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004956 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004957 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004958 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004959 key = css_set_hash(cset->subsys);
4960 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004961 }
4962 write_unlock(&css_set_lock);
4963
Tejun Heoae7f1642013-08-13 20:22:50 -04004964 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004965 if (ret)
4966 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004967
Ben Blume6a11052010-03-10 15:22:09 -08004968 /* success! */
4969 mutex_unlock(&cgroup_mutex);
4970 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004971
4972err_unload:
4973 mutex_unlock(&cgroup_mutex);
4974 /* @ss can't be mounted here as try_module_get() would fail */
4975 cgroup_unload_subsys(ss);
4976 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004977}
4978EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4979
4980/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004981 * cgroup_unload_subsys: unload a modular subsystem
4982 * @ss: the subsystem to unload
4983 *
4984 * This function should be called in a modular subsystem's exitcall. When this
4985 * function is invoked, the refcount on the subsystem's module will be 0, so
4986 * the subsystem will not be attached to any hierarchy.
4987 */
4988void cgroup_unload_subsys(struct cgroup_subsys *ss)
4989{
Tejun Heo69d02062013-06-12 21:04:50 -07004990 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004991
4992 BUG_ON(ss->module == NULL);
4993
4994 /*
4995 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004996 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004997 * doesn't start being used while we're killing it off.
4998 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004999 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08005000
5001 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08005002
Tejun Heo623f9262013-08-13 11:01:55 -04005003 offline_css(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Tejun Heo02ae7482012-11-19 08:13:37 -08005004
Tejun Heoc897ff62013-02-27 17:03:49 -08005005 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08005006 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08005007
Ben Blumcf5d5942010-03-10 15:22:09 -08005008 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07005009 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08005010
Tejun Heo9871bf92013-06-24 15:21:47 -07005011 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07005012 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08005013
5014 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07005015 * disentangle the css from all css_sets attached to the dummy
5016 * top. as in loading, we need to pay our respects to the hashtable
5017 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08005018 */
5019 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07005020 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005021 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08005022 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08005023
Tejun Heo5abb8852013-06-12 21:04:49 -07005024 hash_del(&cset->hlist);
5025 cset->subsys[ss->subsys_id] = NULL;
5026 key = css_set_hash(cset->subsys);
5027 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08005028 }
5029 write_unlock(&css_set_lock);
5030
5031 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07005032 * remove subsystem's css from the cgroup_dummy_top and free it -
5033 * need to free before marking as null because ss->css_free needs
5034 * the cgrp->subsys pointer to find their state. note that this
5035 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08005036 */
Tejun Heo40e93b32013-08-13 11:01:53 -04005037 ss->css_free(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Tejun Heo73e80ed2013-08-13 11:01:55 -04005038 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08005039
5040 mutex_unlock(&cgroup_mutex);
5041}
5042EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
5043
5044/**
Li Zefana043e3b2008-02-23 15:24:09 -08005045 * cgroup_init_early - cgroup initialization at system boot
5046 *
5047 * Initialize cgroups at system boot, and initialize any
5048 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005049 */
5050int __init cgroup_init_early(void)
5051{
Tejun Heo30159ec2013-06-25 11:53:37 -07005052 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005053 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005054
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07005055 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07005056 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07005057 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07005058 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07005059 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07005060 init_cgroup_root(&cgroup_dummy_root);
5061 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005062 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005063
Tejun Heo69d02062013-06-12 21:04:50 -07005064 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07005065 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
5066 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07005067 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005068
Tejun Heo30159ec2013-06-25 11:53:37 -07005069 /* at bootup time, we don't worry about modular subsystems */
5070 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005071 BUG_ON(!ss->name);
5072 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08005073 BUG_ON(!ss->css_alloc);
5074 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005075 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08005076 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07005077 ss->name, ss->subsys_id);
5078 BUG();
5079 }
5080
5081 if (ss->early_init)
5082 cgroup_init_subsys(ss);
5083 }
5084 return 0;
5085}
5086
5087/**
Li Zefana043e3b2008-02-23 15:24:09 -08005088 * cgroup_init - cgroup initialization
5089 *
5090 * Register cgroup filesystem and /proc file, and initialize
5091 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005092 */
5093int __init cgroup_init(void)
5094{
Tejun Heo30159ec2013-06-25 11:53:37 -07005095 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005096 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005097 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005098
5099 err = bdi_init(&cgroup_backing_dev_info);
5100 if (err)
5101 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005102
Tejun Heo30159ec2013-06-25 11:53:37 -07005103 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005104 if (!ss->early_init)
5105 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005106 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08005107 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005108 }
5109
Tejun Heofa3ca072013-04-14 11:36:56 -07005110 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005111 mutex_lock(&cgroup_mutex);
5112 mutex_lock(&cgroup_root_mutex);
5113
Tejun Heo82fe9b02013-06-25 11:53:37 -07005114 /* Add init_css_set to the hash table */
5115 key = css_set_hash(init_css_set.subsys);
5116 hash_add(css_set_table, &init_css_set.hlist, key);
5117
Tejun Heofc76df72013-06-25 11:53:37 -07005118 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005119
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005120 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5121 0, 1, GFP_KERNEL);
5122 BUG_ON(err < 0);
5123
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005124 mutex_unlock(&cgroup_root_mutex);
5125 mutex_unlock(&cgroup_mutex);
5126
Greg KH676db4a2010-08-05 13:53:35 -07005127 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5128 if (!cgroup_kobj) {
5129 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005130 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005131 }
5132
5133 err = register_filesystem(&cgroup_fs_type);
5134 if (err < 0) {
5135 kobject_put(cgroup_kobj);
5136 goto out;
5137 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005138
Li Zefan46ae2202008-04-29 01:00:08 -07005139 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005140
Paul Menageddbcc7e2007-10-18 23:39:30 -07005141out:
Paul Menagea4243162007-10-18 23:39:35 -07005142 if (err)
5143 bdi_destroy(&cgroup_backing_dev_info);
5144
Paul Menageddbcc7e2007-10-18 23:39:30 -07005145 return err;
5146}
Paul Menageb4f48b62007-10-18 23:39:33 -07005147
Paul Menagea4243162007-10-18 23:39:35 -07005148/*
5149 * proc_cgroup_show()
5150 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5151 * - Used for /proc/<pid>/cgroup.
5152 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5153 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005154 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005155 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5156 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5157 * cgroup to top_cgroup.
5158 */
5159
5160/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005161int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005162{
5163 struct pid *pid;
5164 struct task_struct *tsk;
5165 char *buf;
5166 int retval;
5167 struct cgroupfs_root *root;
5168
5169 retval = -ENOMEM;
5170 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5171 if (!buf)
5172 goto out;
5173
5174 retval = -ESRCH;
5175 pid = m->private;
5176 tsk = get_pid_task(pid, PIDTYPE_PID);
5177 if (!tsk)
5178 goto out_free;
5179
5180 retval = 0;
5181
5182 mutex_lock(&cgroup_mutex);
5183
Li Zefane5f6a862009-01-07 18:07:41 -08005184 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005185 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005186 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005187 int count = 0;
5188
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005189 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005190 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005191 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005192 if (strlen(root->name))
5193 seq_printf(m, "%sname=%s", count ? "," : "",
5194 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005195 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005196 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005197 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005198 if (retval < 0)
5199 goto out_unlock;
5200 seq_puts(m, buf);
5201 seq_putc(m, '\n');
5202 }
5203
5204out_unlock:
5205 mutex_unlock(&cgroup_mutex);
5206 put_task_struct(tsk);
5207out_free:
5208 kfree(buf);
5209out:
5210 return retval;
5211}
5212
Paul Menagea4243162007-10-18 23:39:35 -07005213/* Display information about each subsystem and each hierarchy */
5214static int proc_cgroupstats_show(struct seq_file *m, void *v)
5215{
Tejun Heo30159ec2013-06-25 11:53:37 -07005216 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005217 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005218
Paul Menage8bab8dd2008-04-04 14:29:57 -07005219 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005220 /*
5221 * ideally we don't want subsystems moving around while we do this.
5222 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5223 * subsys/hierarchy state.
5224 */
Paul Menagea4243162007-10-18 23:39:35 -07005225 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005226
5227 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005228 seq_printf(m, "%s\t%d\t%d\t%d\n",
5229 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005230 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005231
Paul Menagea4243162007-10-18 23:39:35 -07005232 mutex_unlock(&cgroup_mutex);
5233 return 0;
5234}
5235
5236static int cgroupstats_open(struct inode *inode, struct file *file)
5237{
Al Viro9dce07f2008-03-29 03:07:28 +00005238 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005239}
5240
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005241static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005242 .open = cgroupstats_open,
5243 .read = seq_read,
5244 .llseek = seq_lseek,
5245 .release = single_release,
5246};
5247
Paul Menageb4f48b62007-10-18 23:39:33 -07005248/**
5249 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005250 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005251 *
5252 * Description: A task inherits its parent's cgroup at fork().
5253 *
5254 * A pointer to the shared css_set was automatically copied in
5255 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005256 * it was not made under the protection of RCU or cgroup_mutex, so
5257 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5258 * have already changed current->cgroups, allowing the previously
5259 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005260 *
5261 * At the point that cgroup_fork() is called, 'current' is the parent
5262 * task, and the passed argument 'child' points to the child task.
5263 */
5264void cgroup_fork(struct task_struct *child)
5265{
Tejun Heo9bb71302012-10-18 17:52:07 -07005266 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005267 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005268 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005269 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005270 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005271}
5272
5273/**
Li Zefana043e3b2008-02-23 15:24:09 -08005274 * cgroup_post_fork - called on a new task after adding it to the task list
5275 * @child: the task in question
5276 *
Tejun Heo5edee612012-10-16 15:03:14 -07005277 * Adds the task to the list running through its css_set if necessary and
5278 * call the subsystem fork() callbacks. Has to be after the task is
5279 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005280 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005281 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005282 */
Paul Menage817929e2007-10-18 23:39:36 -07005283void cgroup_post_fork(struct task_struct *child)
5284{
Tejun Heo30159ec2013-06-25 11:53:37 -07005285 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005286 int i;
5287
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005288 /*
5289 * use_task_css_set_links is set to 1 before we walk the tasklist
5290 * under the tasklist_lock and we read it here after we added the child
5291 * to the tasklist under the tasklist_lock as well. If the child wasn't
5292 * yet in the tasklist when we walked through it from
5293 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5294 * should be visible now due to the paired locking and barriers implied
5295 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5296 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5297 * lock on fork.
5298 */
Paul Menage817929e2007-10-18 23:39:36 -07005299 if (use_task_css_set_links) {
5300 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005301 task_lock(child);
5302 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005303 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005304 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005305 write_unlock(&css_set_lock);
5306 }
Tejun Heo5edee612012-10-16 15:03:14 -07005307
5308 /*
5309 * Call ss->fork(). This must happen after @child is linked on
5310 * css_set; otherwise, @child might change state between ->fork()
5311 * and addition to css_set.
5312 */
5313 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005314 /*
5315 * fork/exit callbacks are supported only for builtin
5316 * subsystems, and the builtin section of the subsys
5317 * array is immutable, so we don't need to lock the
5318 * subsys array here. On the other hand, modular section
5319 * of the array can be freed at module unload, so we
5320 * can't touch that.
5321 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005322 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005323 if (ss->fork)
5324 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005325 }
Paul Menage817929e2007-10-18 23:39:36 -07005326}
Tejun Heo5edee612012-10-16 15:03:14 -07005327
Paul Menage817929e2007-10-18 23:39:36 -07005328/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005329 * cgroup_exit - detach cgroup from exiting task
5330 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005331 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005332 *
5333 * Description: Detach cgroup from @tsk and release it.
5334 *
5335 * Note that cgroups marked notify_on_release force every task in
5336 * them to take the global cgroup_mutex mutex when exiting.
5337 * This could impact scaling on very large systems. Be reluctant to
5338 * use notify_on_release cgroups where very high task exit scaling
5339 * is required on large systems.
5340 *
5341 * the_top_cgroup_hack:
5342 *
5343 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5344 *
5345 * We call cgroup_exit() while the task is still competent to
5346 * handle notify_on_release(), then leave the task attached to the
5347 * root cgroup in each hierarchy for the remainder of its exit.
5348 *
5349 * To do this properly, we would increment the reference count on
5350 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5351 * code we would add a second cgroup function call, to drop that
5352 * reference. This would just create an unnecessary hot spot on
5353 * the top_cgroup reference count, to no avail.
5354 *
5355 * Normally, holding a reference to a cgroup without bumping its
5356 * count is unsafe. The cgroup could go away, or someone could
5357 * attach us to a different cgroup, decrementing the count on
5358 * the first cgroup that we never incremented. But in this case,
5359 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005360 * which wards off any cgroup_attach_task() attempts, or task is a failed
5361 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005362 */
5363void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5364{
Tejun Heo30159ec2013-06-25 11:53:37 -07005365 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005366 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005367 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005368
5369 /*
5370 * Unlink from the css_set task list if necessary.
5371 * Optimistically check cg_list before taking
5372 * css_set_lock
5373 */
5374 if (!list_empty(&tsk->cg_list)) {
5375 write_lock(&css_set_lock);
5376 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005377 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005378 write_unlock(&css_set_lock);
5379 }
5380
Paul Menageb4f48b62007-10-18 23:39:33 -07005381 /* Reassign the task to the init_css_set. */
5382 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005383 cset = task_css_set(tsk);
5384 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005385
5386 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005387 /*
5388 * fork/exit callbacks are supported only for builtin
5389 * subsystems, see cgroup_post_fork() for details.
5390 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005391 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005392 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005393 struct cgroup_subsys_state *old_css = cset->subsys[i];
5394 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005395
Tejun Heoeb954192013-08-08 20:11:23 -04005396 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005397 }
5398 }
5399 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005400 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005401
Tejun Heo5abb8852013-06-12 21:04:49 -07005402 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005403}
Paul Menage697f4162007-10-18 23:39:34 -07005404
Paul Menagebd89aab2007-10-18 23:40:44 -07005405static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005406{
Li Zefanf50daa72013-03-01 15:06:07 +08005407 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005408 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005409 /*
5410 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005411 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005412 * it now
5413 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005414 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005415
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005416 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005417 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005418 list_empty(&cgrp->release_list)) {
5419 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005420 need_schedule_work = 1;
5421 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005422 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005423 if (need_schedule_work)
5424 schedule_work(&release_agent_work);
5425 }
5426}
5427
Paul Menage81a6a5c2007-10-18 23:39:38 -07005428/*
5429 * Notify userspace when a cgroup is released, by running the
5430 * configured release agent with the name of the cgroup (path
5431 * relative to the root of cgroup file system) as the argument.
5432 *
5433 * Most likely, this user command will try to rmdir this cgroup.
5434 *
5435 * This races with the possibility that some other task will be
5436 * attached to this cgroup before it is removed, or that some other
5437 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5438 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5439 * unused, and this cgroup will be reprieved from its death sentence,
5440 * to continue to serve a useful existence. Next time it's released,
5441 * we will get notified again, if it still has 'notify_on_release' set.
5442 *
5443 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5444 * means only wait until the task is successfully execve()'d. The
5445 * separate release agent task is forked by call_usermodehelper(),
5446 * then control in this thread returns here, without waiting for the
5447 * release agent task. We don't bother to wait because the caller of
5448 * this routine has no use for the exit status of the release agent
5449 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005450 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005451static void cgroup_release_agent(struct work_struct *work)
5452{
5453 BUG_ON(work != &release_agent_work);
5454 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005455 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005456 while (!list_empty(&release_list)) {
5457 char *argv[3], *envp[3];
5458 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005459 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005460 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005461 struct cgroup,
5462 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005463 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005464 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005465 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005466 if (!pathbuf)
5467 goto continue_free;
5468 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5469 goto continue_free;
5470 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5471 if (!agentbuf)
5472 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005473
5474 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005475 argv[i++] = agentbuf;
5476 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005477 argv[i] = NULL;
5478
5479 i = 0;
5480 /* minimal command environment */
5481 envp[i++] = "HOME=/";
5482 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5483 envp[i] = NULL;
5484
5485 /* Drop the lock while we invoke the usermode helper,
5486 * since the exec could involve hitting disk and hence
5487 * be a slow process */
5488 mutex_unlock(&cgroup_mutex);
5489 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005490 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005491 continue_free:
5492 kfree(pathbuf);
5493 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005494 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005495 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005496 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005497 mutex_unlock(&cgroup_mutex);
5498}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005499
5500static int __init cgroup_disable(char *str)
5501{
Tejun Heo30159ec2013-06-25 11:53:37 -07005502 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005503 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005504 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005505
5506 while ((token = strsep(&str, ",")) != NULL) {
5507 if (!*token)
5508 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005509
Tejun Heo30159ec2013-06-25 11:53:37 -07005510 /*
5511 * cgroup_disable, being at boot time, can't know about
5512 * module subsystems, so we don't worry about them.
5513 */
5514 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005515 if (!strcmp(token, ss->name)) {
5516 ss->disabled = 1;
5517 printk(KERN_INFO "Disabling %s control group"
5518 " subsystem\n", ss->name);
5519 break;
5520 }
5521 }
5522 }
5523 return 1;
5524}
5525__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005526
5527/*
5528 * Functons for CSS ID.
5529 */
5530
Tejun Heo54766d42013-06-12 21:04:53 -07005531/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005532unsigned short css_id(struct cgroup_subsys_state *css)
5533{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005534 struct css_id *cssid;
5535
5536 /*
5537 * This css_id() can return correct value when somone has refcnt
5538 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5539 * it's unchanged until freed.
5540 */
Tejun Heod3daf282013-06-13 19:39:16 -07005541 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005542
5543 if (cssid)
5544 return cssid->id;
5545 return 0;
5546}
Ben Blum67523c42010-03-10 15:22:11 -08005547EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005548
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005549/**
5550 * css_is_ancestor - test "root" css is an ancestor of "child"
5551 * @child: the css to be tested.
5552 * @root: the css supporsed to be an ancestor of the child.
5553 *
5554 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005555 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005556 * But, considering usual usage, the csses should be valid objects after test.
5557 * Assuming that the caller will do some action to the child if this returns
5558 * returns true, the caller must take "child";s reference count.
5559 * If "child" is valid object and this returns true, "root" is valid, too.
5560 */
5561
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005562bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005563 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005564{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005565 struct css_id *child_id;
5566 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005567
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005568 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005569 if (!child_id)
5570 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005571 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005572 if (!root_id)
5573 return false;
5574 if (child_id->depth < root_id->depth)
5575 return false;
5576 if (child_id->stack[root_id->depth] != root_id->id)
5577 return false;
5578 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005579}
5580
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005581void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5582{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005583 struct css_id *id = rcu_dereference_protected(css->id, true);
5584
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005585 /* When this is called before css_id initialization, id can be NULL */
5586 if (!id)
5587 return;
5588
5589 BUG_ON(!ss->use_id);
5590
5591 rcu_assign_pointer(id->css, NULL);
5592 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005593 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005594 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005595 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005596 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005597}
Ben Blum67523c42010-03-10 15:22:11 -08005598EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005599
5600/*
5601 * This is called by init or create(). Then, calls to this function are
5602 * always serialized (By cgroup_mutex() at create()).
5603 */
5604
5605static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5606{
5607 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005608 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005609
5610 BUG_ON(!ss->use_id);
5611
5612 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5613 newid = kzalloc(size, GFP_KERNEL);
5614 if (!newid)
5615 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005616
5617 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005618 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005619 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005620 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005621 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005622 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005623
5624 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005625 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005626 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005627
Tejun Heod228d9e2013-02-27 17:04:54 -08005628 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005629 newid->depth = depth;
5630 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005631err_out:
5632 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005633 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005634
5635}
5636
Ben Blume6a11052010-03-10 15:22:09 -08005637static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5638 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005639{
5640 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005641
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005642 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005643 idr_init(&ss->idr);
5644
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005645 newid = get_new_cssid(ss, 0);
5646 if (IS_ERR(newid))
5647 return PTR_ERR(newid);
5648
5649 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005650 RCU_INIT_POINTER(newid->css, rootcss);
5651 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005652 return 0;
5653}
5654
Tejun Heo623f9262013-08-13 11:01:55 -04005655static int alloc_css_id(struct cgroup_subsys_state *child_css)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005656{
Tejun Heo623f9262013-08-13 11:01:55 -04005657 struct cgroup_subsys_state *parent_css = css_parent(child_css);
Li Zefanfae9c792010-04-22 17:30:00 +08005658 struct css_id *child_id, *parent_id;
Tejun Heo623f9262013-08-13 11:01:55 -04005659 int i, depth;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005660
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005661 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005662 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005663
Tejun Heo623f9262013-08-13 11:01:55 -04005664 child_id = get_new_cssid(child_css->ss, depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005665 if (IS_ERR(child_id))
5666 return PTR_ERR(child_id);
5667
5668 for (i = 0; i < depth; i++)
5669 child_id->stack[i] = parent_id->stack[i];
5670 child_id->stack[depth] = child_id->id;
5671 /*
5672 * child_id->css pointer will be set after this cgroup is available
5673 * see cgroup_populate_dir()
5674 */
5675 rcu_assign_pointer(child_css->id, child_id);
5676
5677 return 0;
5678}
5679
5680/**
5681 * css_lookup - lookup css by id
5682 * @ss: cgroup subsys to be looked into.
5683 * @id: the id
5684 *
5685 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5686 * NULL if not. Should be called under rcu_read_lock()
5687 */
5688struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5689{
5690 struct css_id *cssid = NULL;
5691
5692 BUG_ON(!ss->use_id);
5693 cssid = idr_find(&ss->idr, id);
5694
5695 if (unlikely(!cssid))
5696 return NULL;
5697
5698 return rcu_dereference(cssid->css);
5699}
Ben Blum67523c42010-03-10 15:22:11 -08005700EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005701
Tejun Heob77d7b62013-08-13 11:01:54 -04005702/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005703 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5704 * @dentry: directory dentry of interest
5705 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005706 *
5707 * Must be called under RCU read lock. The caller is responsible for
5708 * pinning the returned css if it needs to be accessed outside the RCU
5709 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005710 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005711struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5712 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005713{
5714 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005715
Tejun Heob77d7b62013-08-13 11:01:54 -04005716 WARN_ON_ONCE(!rcu_read_lock_held());
5717
Tejun Heo35cf0832013-08-26 18:40:56 -04005718 /* is @dentry a cgroup dir? */
5719 if (!dentry->d_inode ||
5720 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005721 return ERR_PTR(-EBADF);
5722
Tejun Heo35cf0832013-08-26 18:40:56 -04005723 cgrp = __d_cgrp(dentry);
5724 return cgroup_css(cgrp, ss->subsys_id) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005725}
5726
Li Zefan1cb650b2013-08-19 10:05:24 +08005727/**
5728 * css_from_id - lookup css by id
5729 * @id: the cgroup id
5730 * @ss: cgroup subsys to be looked into
5731 *
5732 * Returns the css if there's valid one with @id, otherwise returns NULL.
5733 * Should be called under rcu_read_lock().
5734 */
5735struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5736{
5737 struct cgroup *cgrp;
5738
5739 rcu_lockdep_assert(rcu_read_lock_held() ||
5740 lockdep_is_held(&cgroup_mutex),
5741 "css_from_id() needs proper protection");
5742
5743 cgrp = idr_find(&ss->root->cgroup_idr, id);
5744 if (cgrp)
5745 return cgroup_css(cgrp, ss->subsys_id);
5746 return NULL;
5747}
5748
Paul Menagefe693432009-09-23 15:56:20 -07005749#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005750static struct cgroup_subsys_state *
5751debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005752{
5753 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5754
5755 if (!css)
5756 return ERR_PTR(-ENOMEM);
5757
5758 return css;
5759}
5760
Tejun Heoeb954192013-08-08 20:11:23 -04005761static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005762{
Tejun Heoeb954192013-08-08 20:11:23 -04005763 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005764}
5765
Tejun Heo182446d2013-08-08 20:11:24 -04005766static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5767 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005768{
Tejun Heo182446d2013-08-08 20:11:24 -04005769 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005770}
5771
Tejun Heo182446d2013-08-08 20:11:24 -04005772static u64 current_css_set_read(struct cgroup_subsys_state *css,
5773 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005774{
5775 return (u64)(unsigned long)current->cgroups;
5776}
5777
Tejun Heo182446d2013-08-08 20:11:24 -04005778static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005779 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005780{
5781 u64 count;
5782
5783 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005784 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005785 rcu_read_unlock();
5786 return count;
5787}
5788
Tejun Heo182446d2013-08-08 20:11:24 -04005789static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005790 struct cftype *cft,
5791 struct seq_file *seq)
5792{
Tejun Heo69d02062013-06-12 21:04:50 -07005793 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005794 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005795
5796 read_lock(&css_set_lock);
5797 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005798 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005799 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005800 struct cgroup *c = link->cgrp;
5801 const char *name;
5802
5803 if (c->dentry)
5804 name = c->dentry->d_name.name;
5805 else
5806 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005807 seq_printf(seq, "Root %d group %s\n",
5808 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005809 }
5810 rcu_read_unlock();
5811 read_unlock(&css_set_lock);
5812 return 0;
5813}
5814
5815#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005816static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5817 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005818{
Tejun Heo69d02062013-06-12 21:04:50 -07005819 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005820
5821 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005822 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005823 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005824 struct task_struct *task;
5825 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005826 seq_printf(seq, "css_set %p\n", cset);
5827 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005828 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5829 seq_puts(seq, " ...\n");
5830 break;
5831 } else {
5832 seq_printf(seq, " task %d\n",
5833 task_pid_vnr(task));
5834 }
5835 }
5836 }
5837 read_unlock(&css_set_lock);
5838 return 0;
5839}
5840
Tejun Heo182446d2013-08-08 20:11:24 -04005841static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005842{
Tejun Heo182446d2013-08-08 20:11:24 -04005843 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005844}
5845
5846static struct cftype debug_files[] = {
5847 {
Paul Menagefe693432009-09-23 15:56:20 -07005848 .name = "taskcount",
5849 .read_u64 = debug_taskcount_read,
5850 },
5851
5852 {
5853 .name = "current_css_set",
5854 .read_u64 = current_css_set_read,
5855 },
5856
5857 {
5858 .name = "current_css_set_refcount",
5859 .read_u64 = current_css_set_refcount_read,
5860 },
5861
5862 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005863 .name = "current_css_set_cg_links",
5864 .read_seq_string = current_css_set_cg_links_read,
5865 },
5866
5867 {
5868 .name = "cgroup_css_links",
5869 .read_seq_string = cgroup_css_links_read,
5870 },
5871
5872 {
Paul Menagefe693432009-09-23 15:56:20 -07005873 .name = "releasable",
5874 .read_u64 = releasable_read,
5875 },
Paul Menagefe693432009-09-23 15:56:20 -07005876
Tejun Heo4baf6e32012-04-01 12:09:55 -07005877 { } /* terminate */
5878};
Paul Menagefe693432009-09-23 15:56:20 -07005879
5880struct cgroup_subsys debug_subsys = {
5881 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005882 .css_alloc = debug_css_alloc,
5883 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005884 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005885 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005886};
5887#endif /* CONFIG_CGROUP_DEBUG */