blob: c271016225676bcb646010be9f3e1c419e6d7159 [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 Heoea15f8c2013-06-13 19:27:42 -0700221static void cgroup_offline_fn(struct work_struct *work);
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
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700841static int alloc_css_id(struct cgroup_subsys *ss,
842 struct cgroup *parent, struct cgroup *child);
843
Al Viroa5e7ed32011-07-26 01:55:55 -0400844static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845{
846 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700847
848 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400849 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700850 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100851 inode->i_uid = current_fsuid();
852 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
854 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
855 }
856 return inode;
857}
858
Li Zefan65dff752013-03-01 15:01:56 +0800859static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
860{
861 struct cgroup_name *name;
862
863 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
864 if (!name)
865 return NULL;
866 strcpy(name->name, dentry->d_name.name);
867 return name;
868}
869
Li Zefanbe445622013-01-24 14:31:42 +0800870static void cgroup_free_fn(struct work_struct *work)
871{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700872 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800873 struct cgroup_subsys *ss;
874
875 mutex_lock(&cgroup_mutex);
876 /*
877 * Release the subsystem state objects.
878 */
Tejun Heoeb954192013-08-08 20:11:23 -0400879 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -0400880 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -0400881
882 ss->css_free(css);
883 }
Li Zefanbe445622013-01-24 14:31:42 +0800884
885 cgrp->root->number_of_cgroups--;
886 mutex_unlock(&cgroup_mutex);
887
888 /*
Li Zefan415cf072013-04-08 14:35:02 +0800889 * We get a ref to the parent's dentry, and put the ref when
890 * this cgroup is being freed, so it's guaranteed that the
891 * parent won't be destroyed before its children.
892 */
893 dput(cgrp->parent->dentry);
894
895 /*
Li Zefanbe445622013-01-24 14:31:42 +0800896 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700897 * created the cgroup. This will free cgrp->root, if we are
898 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800899 */
900 deactivate_super(cgrp->root->sb);
901
902 /*
903 * if we're getting rid of the cgroup, refcount should ensure
904 * that there are no pidlists left.
905 */
906 BUG_ON(!list_empty(&cgrp->pidlists));
907
908 simple_xattrs_free(&cgrp->xattrs);
909
Li Zefan65dff752013-03-01 15:01:56 +0800910 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800911 kfree(cgrp);
912}
913
914static void cgroup_free_rcu(struct rcu_head *head)
915{
916 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
917
Tejun Heoea15f8c2013-06-13 19:27:42 -0700918 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
919 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800920}
921
Paul Menageddbcc7e2007-10-18 23:39:30 -0700922static void cgroup_diput(struct dentry *dentry, struct inode *inode)
923{
924 /* is dentry a directory ? if so, kfree() associated cgroup */
925 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700926 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800927
Tejun Heo54766d42013-06-12 21:04:53 -0700928 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800929 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700930 } else {
931 struct cfent *cfe = __d_cfe(dentry);
932 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
933
934 WARN_ONCE(!list_empty(&cfe->node) &&
935 cgrp != &cgrp->root->top_cgroup,
936 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700937 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700938 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939 }
940 iput(inode);
941}
942
Al Viroc72a04e2011-01-14 05:31:45 +0000943static int cgroup_delete(const struct dentry *d)
944{
945 return 1;
946}
947
Paul Menageddbcc7e2007-10-18 23:39:30 -0700948static void remove_dir(struct dentry *d)
949{
950 struct dentry *parent = dget(d->d_parent);
951
952 d_delete(d);
953 simple_rmdir(parent->d_inode, d);
954 dput(parent);
955}
956
Li Zefan2739d3c2013-01-21 18:18:33 +0800957static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700959 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700960
Tejun Heo05ef1d72012-04-01 12:09:56 -0700961 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
962 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100963
Li Zefan2739d3c2013-01-21 18:18:33 +0800964 /*
965 * If we're doing cleanup due to failure of cgroup_create(),
966 * the corresponding @cfe may not exist.
967 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700968 list_for_each_entry(cfe, &cgrp->files, node) {
969 struct dentry *d = cfe->dentry;
970
971 if (cft && cfe->type != cft)
972 continue;
973
974 dget(d);
975 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700976 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700977 list_del_init(&cfe->node);
978 dput(d);
979
Li Zefan2739d3c2013-01-21 18:18:33 +0800980 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700982}
983
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400984/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700985 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700986 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400987 * @subsys_mask: mask of the subsystem ids whose files should be removed
988 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700989static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700990{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400991 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700992 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700993
Tejun Heob420ba72013-07-12 12:34:02 -0700994 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400995 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700996
997 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400998 continue;
999 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -04001000 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001001 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002}
1003
1004/*
1005 * NOTE : the dentry must have been dget()'ed
1006 */
1007static void cgroup_d_remove_dir(struct dentry *dentry)
1008{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001009 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001011 parent = dentry->d_parent;
1012 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001013 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001015 spin_unlock(&dentry->d_lock);
1016 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 remove_dir(dentry);
1018}
1019
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001020/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001021 * Call with cgroup_mutex held. Drops reference counts on modules, including
1022 * any duplicate ones that parse_cgroupfs_options took. If this function
1023 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001024 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001026 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001027{
Paul Menagebd89aab2007-10-18 23:40:44 -07001028 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001029 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001030 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001031 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032
Ben Blumaae8aab2010-03-10 15:22:07 -08001033 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001034 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001035
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001037 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001038 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001040
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001041 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001042 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001043 ret = -EBUSY;
1044 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001046
1047 /* pin the module */
1048 if (!try_module_get(ss->module)) {
1049 ret = -ENOENT;
1050 goto out_put;
1051 }
1052 pinned |= 1 << i;
1053 }
1054
1055 /* subsys could be missing if unloaded between parsing and here */
1056 if (added_mask != pinned) {
1057 ret = -ENOENT;
1058 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 }
1060
Tejun Heo31261212013-06-28 17:07:30 -07001061 ret = cgroup_populate_dir(cgrp, added_mask);
1062 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001063 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001064
1065 /*
1066 * Nothing can fail from this point on. Remove files for the
1067 * removed subsystems and rebind each subsystem.
1068 */
1069 cgroup_clear_dir(cgrp, removed_mask);
1070
Tejun Heo30159ec2013-06-25 11:53:37 -07001071 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001073
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001074 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 /* We're binding this subsystem to this hierarchy */
Tejun Heo40e93b32013-08-13 11:01:53 -04001076 BUG_ON(cgroup_css(cgrp, i));
1077 BUG_ON(!cgroup_css(cgroup_dummy_top, i));
1078 BUG_ON(cgroup_css(cgroup_dummy_top, i)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001079
Tejun Heo73e80ed2013-08-13 11:01:55 -04001080 rcu_assign_pointer(cgrp->subsys[i],
1081 cgroup_css(cgroup_dummy_top, i));
Tejun Heo40e93b32013-08-13 11:01:53 -04001082 cgroup_css(cgrp, i)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001083
Li Zefan33a68ac2009-01-07 18:07:42 -08001084 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001085 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086 if (ss->bind)
Tejun Heo40e93b32013-08-13 11:01:53 -04001087 ss->bind(cgroup_css(cgrp, i));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001088
Ben Blumcf5d5942010-03-10 15:22:09 -08001089 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001090 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001091 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 /* We're removing this subsystem */
Tejun Heo40e93b32013-08-13 11:01:53 -04001093 BUG_ON(cgroup_css(cgrp, i) != cgroup_css(cgroup_dummy_top, i));
1094 BUG_ON(cgroup_css(cgrp, i)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001095
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096 if (ss->bind)
Tejun Heo40e93b32013-08-13 11:01:53 -04001097 ss->bind(cgroup_css(cgroup_dummy_top, i));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001098
Tejun Heo40e93b32013-08-13 11:01:53 -04001099 cgroup_css(cgroup_dummy_top, i)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001100 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1101
Tejun Heo9871bf92013-06-24 15:21:47 -07001102 cgroup_subsys[i]->root = &cgroup_dummy_root;
1103 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001104
Ben Blumcf5d5942010-03-10 15:22:09 -08001105 /* subsystem is now free - drop reference on module */
1106 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001107 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108 }
1109 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110
Tejun Heo1672d042013-06-25 18:04:54 -07001111 /*
1112 * Mark @root has finished binding subsystems. @root->subsys_mask
1113 * now matches the bound subsystems.
1114 */
1115 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1116
Paul Menageddbcc7e2007-10-18 23:39:30 -07001117 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001118
1119out_put:
1120 for_each_subsys(ss, i)
1121 if (pinned & (1 << i))
1122 module_put(ss->module);
1123 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124}
1125
Al Viro34c80b12011-12-08 21:32:45 -05001126static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127{
Al Viro34c80b12011-12-08 21:32:45 -05001128 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129 struct cgroup_subsys *ss;
1130
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001131 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001132 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001134 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1135 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001136 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001138 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001139 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001140 if (strlen(root->release_agent_path))
1141 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001142 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001143 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001144 if (strlen(root->name))
1145 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001146 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147 return 0;
1148}
1149
1150struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001151 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001153 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001154 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001155 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001156 /* User explicitly requested empty subsystem */
1157 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001158
1159 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001160
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161};
1162
Ben Blumaae8aab2010-03-10 15:22:07 -08001163/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001164 * Convert a hierarchy specifier into a bitmask of subsystems and
1165 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1166 * array. This function takes refcounts on subsystems to be used, unless it
1167 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001168 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001169static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001170{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001171 char *token, *o = data;
1172 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001173 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001174 struct cgroup_subsys *ss;
1175 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001176
Ben Blumaae8aab2010-03-10 15:22:07 -08001177 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1178
Li Zefanf9ab5b52009-06-17 16:26:33 -07001179#ifdef CONFIG_CPUSETS
1180 mask = ~(1UL << cpuset_subsys_id);
1181#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001182
Paul Menagec6d57f32009-09-23 15:56:19 -07001183 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001184
1185 while ((token = strsep(&o, ",")) != NULL) {
1186 if (!*token)
1187 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001188 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001189 /* Explicitly have no subsystems */
1190 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001191 continue;
1192 }
1193 if (!strcmp(token, "all")) {
1194 /* Mutually exclusive option 'all' + subsystem name */
1195 if (one_ss)
1196 return -EINVAL;
1197 all_ss = true;
1198 continue;
1199 }
Tejun Heo873fe092013-04-14 20:15:26 -07001200 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1201 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1202 continue;
1203 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001204 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001205 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001206 continue;
1207 }
1208 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001209 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001210 continue;
1211 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001212 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001213 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001214 continue;
1215 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001216 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001217 /* Specifying two release agents is forbidden */
1218 if (opts->release_agent)
1219 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001220 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001221 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001222 if (!opts->release_agent)
1223 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001224 continue;
1225 }
1226 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001227 const char *name = token + 5;
1228 /* Can't specify an empty name */
1229 if (!strlen(name))
1230 return -EINVAL;
1231 /* Must match [\w.-]+ */
1232 for (i = 0; i < strlen(name); i++) {
1233 char c = name[i];
1234 if (isalnum(c))
1235 continue;
1236 if ((c == '.') || (c == '-') || (c == '_'))
1237 continue;
1238 return -EINVAL;
1239 }
1240 /* Specifying two names is forbidden */
1241 if (opts->name)
1242 return -EINVAL;
1243 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001244 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001245 GFP_KERNEL);
1246 if (!opts->name)
1247 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001248
1249 continue;
1250 }
1251
Tejun Heo30159ec2013-06-25 11:53:37 -07001252 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001253 if (strcmp(token, ss->name))
1254 continue;
1255 if (ss->disabled)
1256 continue;
1257
1258 /* Mutually exclusive option 'all' + subsystem name */
1259 if (all_ss)
1260 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001261 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001262 one_ss = true;
1263
1264 break;
1265 }
1266 if (i == CGROUP_SUBSYS_COUNT)
1267 return -ENOENT;
1268 }
1269
1270 /*
1271 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001272 * otherwise if 'none', 'name=' and a subsystem name options
1273 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001274 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001275 if (all_ss || (!one_ss && !opts->none && !opts->name))
1276 for_each_subsys(ss, i)
1277 if (!ss->disabled)
1278 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001279
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001280 /* Consistency checks */
1281
Tejun Heo873fe092013-04-14 20:15:26 -07001282 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1283 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1284
1285 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1286 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1287 return -EINVAL;
1288 }
1289
1290 if (opts->cpuset_clone_children) {
1291 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1292 return -EINVAL;
1293 }
1294 }
1295
Li Zefanf9ab5b52009-06-17 16:26:33 -07001296 /*
1297 * Option noprefix was introduced just for backward compatibility
1298 * with the old cpuset, so we allow noprefix only if mounting just
1299 * the cpuset subsystem.
1300 */
Tejun Heo93438622013-04-14 20:15:25 -07001301 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001302 return -EINVAL;
1303
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001304
1305 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001306 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001307 return -EINVAL;
1308
1309 /*
1310 * We either have to specify by name or by subsystems. (So all
1311 * empty hierarchies must have a name).
1312 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001313 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314 return -EINVAL;
1315
1316 return 0;
1317}
1318
1319static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1320{
1321 int ret = 0;
1322 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001323 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001325 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326
Tejun Heo873fe092013-04-14 20:15:26 -07001327 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1328 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1329 return -EINVAL;
1330 }
1331
Paul Menagebd89aab2007-10-18 23:40:44 -07001332 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001334 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335
1336 /* See what subsystems are wanted */
1337 ret = parse_cgroupfs_options(data, &opts);
1338 if (ret)
1339 goto out_unlock;
1340
Tejun Heoa8a648c2013-06-24 15:21:47 -07001341 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001342 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1343 task_tgid_nr(current), current->comm);
1344
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001345 added_mask = opts.subsys_mask & ~root->subsys_mask;
1346 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001347
Ben Blumcf5d5942010-03-10 15:22:09 -08001348 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001349 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001350 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001351 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1352 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1353 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001354 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001355 goto out_unlock;
1356 }
1357
Tejun Heof172e672013-06-28 17:07:30 -07001358 /* remounting is not allowed for populated hierarchies */
1359 if (root->number_of_cgroups > 1) {
1360 ret = -EBUSY;
1361 goto out_unlock;
1362 }
1363
Tejun Heoa8a648c2013-06-24 15:21:47 -07001364 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001365 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001366 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367
Paul Menage81a6a5c2007-10-18 23:39:38 -07001368 if (opts.release_agent)
1369 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001371 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001372 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001373 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001375 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001376 return ret;
1377}
1378
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001379static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380 .statfs = simple_statfs,
1381 .drop_inode = generic_delete_inode,
1382 .show_options = cgroup_show_options,
1383 .remount_fs = cgroup_remount,
1384};
1385
Paul Menagecc31edc2008-10-18 20:28:04 -07001386static void init_cgroup_housekeeping(struct cgroup *cgrp)
1387{
1388 INIT_LIST_HEAD(&cgrp->sibling);
1389 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001390 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001391 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001392 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001393 INIT_LIST_HEAD(&cgrp->pidlists);
1394 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001395 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001396 INIT_LIST_HEAD(&cgrp->event_list);
1397 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001398 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001399}
Paul Menagec6d57f32009-09-23 15:56:19 -07001400
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401static void init_cgroup_root(struct cgroupfs_root *root)
1402{
Paul Menagebd89aab2007-10-18 23:40:44 -07001403 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001404
Paul Menageddbcc7e2007-10-18 23:39:30 -07001405 INIT_LIST_HEAD(&root->subsys_list);
1406 INIT_LIST_HEAD(&root->root_list);
1407 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001408 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001409 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001410 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001411 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412}
1413
Tejun Heofc76df72013-06-25 11:53:37 -07001414static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001415{
Tejun Heo1a574232013-04-14 11:36:58 -07001416 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001417
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001418 lockdep_assert_held(&cgroup_mutex);
1419 lockdep_assert_held(&cgroup_root_mutex);
1420
Tejun Heofc76df72013-06-25 11:53:37 -07001421 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1422 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001423 if (id < 0)
1424 return id;
1425
1426 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001427 return 0;
1428}
1429
1430static void cgroup_exit_root_id(struct cgroupfs_root *root)
1431{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001432 lockdep_assert_held(&cgroup_mutex);
1433 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001434
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001435 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001436 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001437 root->hierarchy_id = 0;
1438 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001439}
1440
Paul Menageddbcc7e2007-10-18 23:39:30 -07001441static int cgroup_test_super(struct super_block *sb, void *data)
1442{
Paul Menagec6d57f32009-09-23 15:56:19 -07001443 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444 struct cgroupfs_root *root = sb->s_fs_info;
1445
Paul Menagec6d57f32009-09-23 15:56:19 -07001446 /* If we asked for a name then it must match */
1447 if (opts->name && strcmp(opts->name, root->name))
1448 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001449
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001450 /*
1451 * If we asked for subsystems (or explicitly for no
1452 * subsystems) then they must match
1453 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001454 if ((opts->subsys_mask || opts->none)
1455 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001456 return 0;
1457
1458 return 1;
1459}
1460
Paul Menagec6d57f32009-09-23 15:56:19 -07001461static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1462{
1463 struct cgroupfs_root *root;
1464
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001465 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001466 return NULL;
1467
1468 root = kzalloc(sizeof(*root), GFP_KERNEL);
1469 if (!root)
1470 return ERR_PTR(-ENOMEM);
1471
1472 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001473
Tejun Heo1672d042013-06-25 18:04:54 -07001474 /*
1475 * We need to set @root->subsys_mask now so that @root can be
1476 * matched by cgroup_test_super() before it finishes
1477 * initialization; otherwise, competing mounts with the same
1478 * options may try to bind the same subsystems instead of waiting
1479 * for the first one leading to unexpected mount errors.
1480 * SUBSYS_BOUND will be set once actual binding is complete.
1481 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001482 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001483 root->flags = opts->flags;
1484 if (opts->release_agent)
1485 strcpy(root->release_agent_path, opts->release_agent);
1486 if (opts->name)
1487 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001488 if (opts->cpuset_clone_children)
1489 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 return root;
1491}
1492
Tejun Heofa3ca072013-04-14 11:36:56 -07001493static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001494{
Tejun Heofa3ca072013-04-14 11:36:56 -07001495 if (root) {
1496 /* hierarhcy ID shoulid already have been released */
1497 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001498
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001499 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001500 kfree(root);
1501 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001502}
1503
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504static int cgroup_set_super(struct super_block *sb, void *data)
1505{
1506 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 struct cgroup_sb_opts *opts = data;
1508
1509 /* If we don't have a new root, we can't set up a new sb */
1510 if (!opts->new_root)
1511 return -EINVAL;
1512
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001513 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514
1515 ret = set_anon_super(sb, NULL);
1516 if (ret)
1517 return ret;
1518
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 sb->s_fs_info = opts->new_root;
1520 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521
1522 sb->s_blocksize = PAGE_CACHE_SIZE;
1523 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1524 sb->s_magic = CGROUP_SUPER_MAGIC;
1525 sb->s_op = &cgroup_ops;
1526
1527 return 0;
1528}
1529
1530static int cgroup_get_rootdir(struct super_block *sb)
1531{
Al Viro0df6a632010-12-21 13:29:29 -05001532 static const struct dentry_operations cgroup_dops = {
1533 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001534 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001535 };
1536
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537 struct inode *inode =
1538 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539
1540 if (!inode)
1541 return -ENOMEM;
1542
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 inode->i_fop = &simple_dir_operations;
1544 inode->i_op = &cgroup_dir_inode_operations;
1545 /* directories start off with i_nlink == 2 (for "." entry) */
1546 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001547 sb->s_root = d_make_root(inode);
1548 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001550 /* for everything else we want ->d_op set */
1551 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 return 0;
1553}
1554
Al Virof7e83572010-07-26 13:23:11 +04001555static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001557 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558{
1559 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001560 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 int ret = 0;
1562 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001563 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001564 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001565 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001566 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567
1568 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001569 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001571 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001572 if (ret)
1573 goto out_err;
1574
1575 /*
1576 * Allocate a new cgroup root. We may not need it if we're
1577 * reusing an existing hierarchy.
1578 */
1579 new_root = cgroup_root_from_opts(&opts);
1580 if (IS_ERR(new_root)) {
1581 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001582 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001583 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001584 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001585
Paul Menagec6d57f32009-09-23 15:56:19 -07001586 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001587 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001589 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001590 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001591 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592 }
1593
Paul Menagec6d57f32009-09-23 15:56:19 -07001594 root = sb->s_fs_info;
1595 BUG_ON(!root);
1596 if (root == opts.new_root) {
1597 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001598 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001599 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001600 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001601 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602
1603 BUG_ON(sb->s_root != NULL);
1604
1605 ret = cgroup_get_rootdir(sb);
1606 if (ret)
1607 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001608 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609
Paul Menage817929e2007-10-18 23:39:36 -07001610 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001611 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001612 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001614 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1615 0, 1, GFP_KERNEL);
1616 if (root_cgrp->id < 0)
1617 goto unlock_drop;
1618
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001619 /* Check for name clashes with existing mounts */
1620 ret = -EBUSY;
1621 if (strlen(root->name))
1622 for_each_active_root(existing_root)
1623 if (!strcmp(existing_root->name, root->name))
1624 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001625
Paul Menage817929e2007-10-18 23:39:36 -07001626 /*
1627 * We're accessing css_set_count without locking
1628 * css_set_lock here, but that's OK - it can only be
1629 * increased by someone holding cgroup_lock, and
1630 * that's us. The worst that can happen is that we
1631 * have some link structures left over
1632 */
Tejun Heo69d02062013-06-12 21:04:50 -07001633 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001634 if (ret)
1635 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001636
Tejun Heofc76df72013-06-25 11:53:37 -07001637 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1638 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001639 if (ret)
1640 goto unlock_drop;
1641
Tejun Heo31261212013-06-28 17:07:30 -07001642 sb->s_root->d_fsdata = root_cgrp;
1643 root_cgrp->dentry = sb->s_root;
1644
1645 /*
1646 * We're inside get_sb() and will call lookup_one_len() to
1647 * create the root files, which doesn't work if SELinux is
1648 * in use. The following cred dancing somehow works around
1649 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1650 * populating new cgroupfs mount") for more details.
1651 */
1652 cred = override_creds(&init_cred);
1653
Tejun Heo2bb566c2013-08-08 20:11:23 -04001654 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001655 if (ret)
1656 goto rm_base_files;
1657
Tejun Heoa8a648c2013-06-24 15:21:47 -07001658 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001659 if (ret)
1660 goto rm_base_files;
1661
1662 revert_creds(cred);
1663
Ben Blumcf5d5942010-03-10 15:22:09 -08001664 /*
1665 * There must be no failure case after here, since rebinding
1666 * takes care of subsystems' refcounts, which are explicitly
1667 * dropped in the failure exit path.
1668 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669
Tejun Heo9871bf92013-06-24 15:21:47 -07001670 list_add(&root->root_list, &cgroup_roots);
1671 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672
Paul Menage817929e2007-10-18 23:39:36 -07001673 /* Link the top cgroup in this hierarchy into all
1674 * the css_set objects */
1675 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001676 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001677 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001678 write_unlock(&css_set_lock);
1679
Tejun Heo69d02062013-06-12 21:04:50 -07001680 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001681
Li Zefanc12f65d2009-01-07 18:07:42 -08001682 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683 BUG_ON(root->number_of_cgroups != 1);
1684
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001685 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001687 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001688 } else {
1689 /*
1690 * We re-used an existing hierarchy - the new root (if
1691 * any) is not needed
1692 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001693 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001694
Tejun Heoc7ba8282013-06-29 14:06:10 -07001695 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001696 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1697 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1698 ret = -EINVAL;
1699 goto drop_new_super;
1700 } else {
1701 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1702 }
Tejun Heo873fe092013-04-14 20:15:26 -07001703 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704 }
1705
Paul Menagec6d57f32009-09-23 15:56:19 -07001706 kfree(opts.release_agent);
1707 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001708 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001709
Tejun Heo31261212013-06-28 17:07:30 -07001710 rm_base_files:
1711 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001712 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001713 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001714 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001715 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001716 mutex_unlock(&cgroup_root_mutex);
1717 mutex_unlock(&cgroup_mutex);
1718 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001720 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001721 out_err:
1722 kfree(opts.release_agent);
1723 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001724 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001725}
1726
1727static void cgroup_kill_sb(struct super_block *sb) {
1728 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001729 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001730 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001731 int ret;
1732
1733 BUG_ON(!root);
1734
1735 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001736 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737
Tejun Heo31261212013-06-28 17:07:30 -07001738 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001740 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741
1742 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001743 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1744 ret = rebind_subsystems(root, 0, root->subsys_mask);
1745 /* Shouldn't be able to fail ... */
1746 BUG_ON(ret);
1747 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001748
Paul Menage817929e2007-10-18 23:39:36 -07001749 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001750 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001751 * root cgroup
1752 */
1753 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001754
Tejun Heo69d02062013-06-12 21:04:50 -07001755 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1756 list_del(&link->cset_link);
1757 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001758 kfree(link);
1759 }
1760 write_unlock(&css_set_lock);
1761
Paul Menage839ec542009-01-29 14:25:22 -08001762 if (!list_empty(&root->root_list)) {
1763 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001764 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001765 }
Li Zefane5f6a862009-01-07 18:07:41 -08001766
Tejun Heofa3ca072013-04-14 11:36:56 -07001767 cgroup_exit_root_id(root);
1768
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001769 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001771 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001773 simple_xattrs_free(&cgrp->xattrs);
1774
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001776 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777}
1778
1779static struct file_system_type cgroup_fs_type = {
1780 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001781 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 .kill_sb = cgroup_kill_sb,
1783};
1784
Greg KH676db4a2010-08-05 13:53:35 -07001785static struct kobject *cgroup_kobj;
1786
Li Zefana043e3b2008-02-23 15:24:09 -08001787/**
1788 * cgroup_path - generate the path of a cgroup
1789 * @cgrp: the cgroup in question
1790 * @buf: the buffer to write the path into
1791 * @buflen: the length of the buffer
1792 *
Li Zefan65dff752013-03-01 15:01:56 +08001793 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1794 *
1795 * We can't generate cgroup path using dentry->d_name, as accessing
1796 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1797 * inode's i_mutex, while on the other hand cgroup_path() can be called
1798 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001800int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801{
Li Zefan65dff752013-03-01 15:01:56 +08001802 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001804
Tejun Heoda1f2962013-04-14 10:32:19 -07001805 if (!cgrp->parent) {
1806 if (strlcpy(buf, "/", buflen) >= buflen)
1807 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808 return 0;
1809 }
1810
Tao Ma316eb662012-11-08 21:36:38 +08001811 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001812 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001813
Li Zefan65dff752013-03-01 15:01:56 +08001814 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001815 do {
Li Zefan65dff752013-03-01 15:01:56 +08001816 const char *name = cgroup_name(cgrp);
1817 int len;
1818
1819 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001821 goto out;
1822 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001823
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001825 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001827
1828 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001829 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001830 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001831 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001832out:
1833 rcu_read_unlock();
1834 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835}
Ben Blum67523c42010-03-10 15:22:11 -08001836EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001837
Tejun Heo857a2be2013-04-14 20:50:08 -07001838/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001839 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001840 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001841 * @buf: the buffer to write the path into
1842 * @buflen: the length of the buffer
1843 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001844 * Determine @task's cgroup on the first (the one with the lowest non-zero
1845 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1846 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1847 * cgroup controller callbacks.
1848 *
1849 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001850 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001851int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001852{
1853 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001854 struct cgroup *cgrp;
1855 int hierarchy_id = 1, ret = 0;
1856
1857 if (buflen < 2)
1858 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001859
1860 mutex_lock(&cgroup_mutex);
1861
Tejun Heo913ffdb2013-07-11 16:34:48 -07001862 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1863
Tejun Heo857a2be2013-04-14 20:50:08 -07001864 if (root) {
1865 cgrp = task_cgroup_from_root(task, root);
1866 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001867 } else {
1868 /* if no hierarchy exists, everyone is in "/" */
1869 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001870 }
1871
1872 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001873 return ret;
1874}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001875EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001876
Ben Blum74a11662011-05-26 16:25:20 -07001877/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001878 * Control Group taskset
1879 */
Tejun Heo134d3372011-12-12 18:12:21 -08001880struct task_and_cgroup {
1881 struct task_struct *task;
1882 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001883 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001884};
1885
Tejun Heo2f7ee562011-12-12 18:12:21 -08001886struct cgroup_taskset {
1887 struct task_and_cgroup single;
1888 struct flex_array *tc_array;
1889 int tc_array_len;
1890 int idx;
1891 struct cgroup *cur_cgrp;
1892};
1893
1894/**
1895 * cgroup_taskset_first - reset taskset and return the first task
1896 * @tset: taskset of interest
1897 *
1898 * @tset iteration is initialized and the first task is returned.
1899 */
1900struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1901{
1902 if (tset->tc_array) {
1903 tset->idx = 0;
1904 return cgroup_taskset_next(tset);
1905 } else {
1906 tset->cur_cgrp = tset->single.cgrp;
1907 return tset->single.task;
1908 }
1909}
1910EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1911
1912/**
1913 * cgroup_taskset_next - iterate to the next task in taskset
1914 * @tset: taskset of interest
1915 *
1916 * Return the next task in @tset. Iteration must have been initialized
1917 * with cgroup_taskset_first().
1918 */
1919struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1920{
1921 struct task_and_cgroup *tc;
1922
1923 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1924 return NULL;
1925
1926 tc = flex_array_get(tset->tc_array, tset->idx++);
1927 tset->cur_cgrp = tc->cgrp;
1928 return tc->task;
1929}
1930EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1931
1932/**
Tejun Heod99c8722013-08-08 20:11:27 -04001933 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001934 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001935 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001936 *
Tejun Heod99c8722013-08-08 20:11:27 -04001937 * Return the css for the current (last returned) task of @tset for
1938 * subsystem specified by @subsys_id. This function must be preceded by
1939 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001940 */
Tejun Heod99c8722013-08-08 20:11:27 -04001941struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1942 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001943{
Tejun Heod99c8722013-08-08 20:11:27 -04001944 return cgroup_css(tset->cur_cgrp, subsys_id);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001945}
Tejun Heod99c8722013-08-08 20:11:27 -04001946EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001947
1948/**
1949 * cgroup_taskset_size - return the number of tasks in taskset
1950 * @tset: taskset of interest
1951 */
1952int cgroup_taskset_size(struct cgroup_taskset *tset)
1953{
1954 return tset->tc_array ? tset->tc_array_len : 1;
1955}
1956EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1957
1958
Ben Blum74a11662011-05-26 16:25:20 -07001959/*
1960 * cgroup_task_migrate - move a task from one cgroup to another.
1961 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001962 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001963 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001964static void cgroup_task_migrate(struct cgroup *old_cgrp,
1965 struct task_struct *tsk,
1966 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001967{
Tejun Heo5abb8852013-06-12 21:04:49 -07001968 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001969
1970 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001971 * We are synchronized through threadgroup_lock() against PF_EXITING
1972 * setting such that we can't race against cgroup_exit() changing the
1973 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001974 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001975 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001976 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001977
Ben Blum74a11662011-05-26 16:25:20 -07001978 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001979 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001980 task_unlock(tsk);
1981
1982 /* Update the css_set linked lists if we're using them */
1983 write_lock(&css_set_lock);
1984 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001985 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001986 write_unlock(&css_set_lock);
1987
1988 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001989 * We just gained a reference on old_cset by taking it from the
1990 * task. As trading it for new_cset is protected by cgroup_mutex,
1991 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001992 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001993 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1994 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001995}
1996
Li Zefana043e3b2008-02-23 15:24:09 -08001997/**
Li Zefan081aa452013-03-13 09:17:09 +08001998 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001999 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08002000 * @tsk: the task or the leader of the threadgroup to be attached
2001 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07002002 *
Tejun Heo257058a2011-12-12 18:12:21 -08002003 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08002004 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002005 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07002006static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2007 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002008{
2009 int retval, i, group_size;
2010 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002011 struct cgroupfs_root *root = cgrp->root;
2012 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002013 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002014 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002015 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002016 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002017
2018 /*
2019 * step 0: in order to do expensive, possibly blocking operations for
2020 * every thread, we cannot iterate the thread group list, since it needs
2021 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002022 * group - group_rwsem prevents new threads from appearing, and if
2023 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002024 */
Li Zefan081aa452013-03-13 09:17:09 +08002025 if (threadgroup)
2026 group_size = get_nr_threads(tsk);
2027 else
2028 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002029 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002030 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002031 if (!group)
2032 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002033 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002034 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002035 if (retval)
2036 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002037
Ben Blum74a11662011-05-26 16:25:20 -07002038 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002039 /*
2040 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2041 * already PF_EXITING could be freed from underneath us unless we
2042 * take an rcu_read_lock.
2043 */
2044 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002045 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002046 struct task_and_cgroup ent;
2047
Tejun Heocd3d0952011-12-12 18:12:21 -08002048 /* @tsk either already exited or can't exit until the end */
2049 if (tsk->flags & PF_EXITING)
2050 continue;
2051
Ben Blum74a11662011-05-26 16:25:20 -07002052 /* as per above, nr_threads may decrease, but not increase. */
2053 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002054 ent.task = tsk;
2055 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002056 /* nothing to do if this task is already in the cgroup */
2057 if (ent.cgrp == cgrp)
2058 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002059 /*
2060 * saying GFP_ATOMIC has no effect here because we did prealloc
2061 * earlier, but it's good form to communicate our expectations.
2062 */
Tejun Heo134d3372011-12-12 18:12:21 -08002063 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002064 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002065 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002066
2067 if (!threadgroup)
2068 break;
Ben Blum74a11662011-05-26 16:25:20 -07002069 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002070 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002071 /* remember the number of threads in the array for later. */
2072 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002073 tset.tc_array = group;
2074 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002075
Tejun Heo134d3372011-12-12 18:12:21 -08002076 /* methods shouldn't be called if no task is actually migrating */
2077 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002078 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002079 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002080
Ben Blum74a11662011-05-26 16:25:20 -07002081 /*
2082 * step 1: check that we can legitimately attach to the cgroup.
2083 */
Tejun Heo5549c492013-06-24 15:21:48 -07002084 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04002085 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -04002086
Ben Blum74a11662011-05-26 16:25:20 -07002087 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002088 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002089 if (retval) {
2090 failed_ss = ss;
2091 goto out_cancel_attach;
2092 }
2093 }
Ben Blum74a11662011-05-26 16:25:20 -07002094 }
2095
2096 /*
2097 * step 2: make sure css_sets exist for all threads to be migrated.
2098 * we use find_css_set, which allocates a new one if necessary.
2099 */
Ben Blum74a11662011-05-26 16:25:20 -07002100 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002101 struct css_set *old_cset;
2102
Tejun Heo134d3372011-12-12 18:12:21 -08002103 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002104 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002105 tc->cset = find_css_set(old_cset, cgrp);
2106 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002107 retval = -ENOMEM;
2108 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002109 }
2110 }
2111
2112 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002113 * step 3: now that we're guaranteed success wrt the css_sets,
2114 * proceed to move all tasks to the new cgroup. There are no
2115 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002116 */
Ben Blum74a11662011-05-26 16:25:20 -07002117 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002118 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002119 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002120 }
2121 /* nothing is sensitive to fork() after this point. */
2122
2123 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002124 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002125 */
Tejun Heo5549c492013-06-24 15:21:48 -07002126 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04002127 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -04002128
Ben Blum74a11662011-05-26 16:25:20 -07002129 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002130 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002131 }
2132
2133 /*
2134 * step 5: success! and cleanup
2135 */
Ben Blum74a11662011-05-26 16:25:20 -07002136 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002137out_put_css_set_refs:
2138 if (retval) {
2139 for (i = 0; i < group_size; i++) {
2140 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002141 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002142 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002143 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002144 }
Ben Blum74a11662011-05-26 16:25:20 -07002145 }
2146out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002147 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002148 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04002149 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -04002150
Tejun Heo494c1672011-12-12 18:12:22 -08002151 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002152 break;
Ben Blum74a11662011-05-26 16:25:20 -07002153 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002154 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002155 }
2156 }
Ben Blum74a11662011-05-26 16:25:20 -07002157out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002158 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002159 return retval;
2160}
2161
2162/*
2163 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002164 * function to attach either it or all tasks in its threadgroup. Will lock
2165 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002166 */
2167static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002168{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002169 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002170 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002171 int ret;
2172
Ben Blum74a11662011-05-26 16:25:20 -07002173 if (!cgroup_lock_live_group(cgrp))
2174 return -ENODEV;
2175
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002176retry_find_task:
2177 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002178 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002179 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002180 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002181 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002182 ret= -ESRCH;
2183 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002184 }
Ben Blum74a11662011-05-26 16:25:20 -07002185 /*
2186 * even if we're attaching all tasks in the thread group, we
2187 * only need to check permissions on one of them.
2188 */
David Howellsc69e8d92008-11-14 10:39:19 +11002189 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002190 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2191 !uid_eq(cred->euid, tcred->uid) &&
2192 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002193 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002194 ret = -EACCES;
2195 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002196 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002197 } else
2198 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002199
2200 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002201 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002202
2203 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002204 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002205 * trapped in a cpuset, or RT worker may be born in a cgroup
2206 * with no rt_runtime allocated. Just say no.
2207 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002208 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002209 ret = -EINVAL;
2210 rcu_read_unlock();
2211 goto out_unlock_cgroup;
2212 }
2213
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002214 get_task_struct(tsk);
2215 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002216
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002217 threadgroup_lock(tsk);
2218 if (threadgroup) {
2219 if (!thread_group_leader(tsk)) {
2220 /*
2221 * a race with de_thread from another thread's exec()
2222 * may strip us of our leadership, if this happens,
2223 * there is no choice but to throw this task away and
2224 * try again; this is
2225 * "double-double-toil-and-trouble-check locking".
2226 */
2227 threadgroup_unlock(tsk);
2228 put_task_struct(tsk);
2229 goto retry_find_task;
2230 }
Li Zefan081aa452013-03-13 09:17:09 +08002231 }
2232
2233 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2234
Tejun Heocd3d0952011-12-12 18:12:21 -08002235 threadgroup_unlock(tsk);
2236
Paul Menagebbcb81d2007-10-18 23:39:32 -07002237 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002238out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002239 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002240 return ret;
2241}
2242
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002243/**
2244 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2245 * @from: attach to all cgroups of a given task
2246 * @tsk: the task to be attached
2247 */
2248int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2249{
2250 struct cgroupfs_root *root;
2251 int retval = 0;
2252
Tejun Heo47cfcd02013-04-07 09:29:51 -07002253 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002254 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002255 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002256
Li Zefan6f4b7e62013-07-31 16:18:36 +08002257 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002258 if (retval)
2259 break;
2260 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002261 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002262
2263 return retval;
2264}
2265EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2266
Tejun Heo182446d2013-08-08 20:11:24 -04002267static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2268 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002269{
Tejun Heo182446d2013-08-08 20:11:24 -04002270 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002271}
2272
Tejun Heo182446d2013-08-08 20:11:24 -04002273static int cgroup_procs_write(struct cgroup_subsys_state *css,
2274 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002275{
Tejun Heo182446d2013-08-08 20:11:24 -04002276 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002277}
2278
Tejun Heo182446d2013-08-08 20:11:24 -04002279static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2280 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002281{
Tejun Heo182446d2013-08-08 20:11:24 -04002282 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002283 if (strlen(buffer) >= PATH_MAX)
2284 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002285 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002286 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002287 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002288 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002289 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002290 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002291 return 0;
2292}
2293
Tejun Heo182446d2013-08-08 20:11:24 -04002294static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2295 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002296{
Tejun Heo182446d2013-08-08 20:11:24 -04002297 struct cgroup *cgrp = css->cgroup;
2298
Paul Menagee788e062008-07-25 01:46:59 -07002299 if (!cgroup_lock_live_group(cgrp))
2300 return -ENODEV;
2301 seq_puts(seq, cgrp->root->release_agent_path);
2302 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002303 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002304 return 0;
2305}
2306
Tejun Heo182446d2013-08-08 20:11:24 -04002307static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2308 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002309{
Tejun Heo182446d2013-08-08 20:11:24 -04002310 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002311 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002312}
2313
Paul Menage84eea842008-07-25 01:47:00 -07002314/* A buffer size big enough for numbers or short strings */
2315#define CGROUP_LOCAL_BUFFER_SIZE 64
2316
Tejun Heo182446d2013-08-08 20:11:24 -04002317static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2318 struct cftype *cft, struct file *file,
2319 const char __user *userbuf, size_t nbytes,
2320 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002321{
Paul Menage84eea842008-07-25 01:47:00 -07002322 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002323 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002324 char *end;
2325
2326 if (!nbytes)
2327 return -EINVAL;
2328 if (nbytes >= sizeof(buffer))
2329 return -E2BIG;
2330 if (copy_from_user(buffer, userbuf, nbytes))
2331 return -EFAULT;
2332
2333 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002334 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002335 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002336 if (*end)
2337 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002338 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002339 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002340 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002341 if (*end)
2342 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002343 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002344 }
Paul Menage355e0c42007-10-18 23:39:33 -07002345 if (!retval)
2346 retval = nbytes;
2347 return retval;
2348}
2349
Tejun Heo182446d2013-08-08 20:11:24 -04002350static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2351 struct cftype *cft, struct file *file,
2352 const char __user *userbuf, size_t nbytes,
2353 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002354{
Paul Menage84eea842008-07-25 01:47:00 -07002355 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002356 int retval = 0;
2357 size_t max_bytes = cft->max_write_len;
2358 char *buffer = local_buffer;
2359
2360 if (!max_bytes)
2361 max_bytes = sizeof(local_buffer) - 1;
2362 if (nbytes >= max_bytes)
2363 return -E2BIG;
2364 /* Allocate a dynamic buffer if we need one */
2365 if (nbytes >= sizeof(local_buffer)) {
2366 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2367 if (buffer == NULL)
2368 return -ENOMEM;
2369 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002370 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2371 retval = -EFAULT;
2372 goto out;
2373 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002374
2375 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002376 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002377 if (!retval)
2378 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002379out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002380 if (buffer != local_buffer)
2381 kfree(buffer);
2382 return retval;
2383}
2384
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002386 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002387{
Tejun Heo182446d2013-08-08 20:11:24 -04002388 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002389 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002390 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002391
Paul Menage355e0c42007-10-18 23:39:33 -07002392 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002393 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002394 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002395 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002396 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002397 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002398 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002399 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002400 return ret ? ret : nbytes;
2401 }
Paul Menage355e0c42007-10-18 23:39:33 -07002402 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002403}
2404
Tejun Heo182446d2013-08-08 20:11:24 -04002405static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2406 struct cftype *cft, struct file *file,
2407 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002408{
Paul Menage84eea842008-07-25 01:47:00 -07002409 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002410 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002411 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2412
2413 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2414}
2415
Tejun Heo182446d2013-08-08 20:11:24 -04002416static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2417 struct cftype *cft, struct file *file,
2418 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002419{
Paul Menage84eea842008-07-25 01:47:00 -07002420 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002421 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002422 int len = sprintf(tmp, "%lld\n", (long long) val);
2423
2424 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2425}
2426
Paul Menageddbcc7e2007-10-18 23:39:30 -07002427static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002428 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002429{
Tejun Heo182446d2013-08-08 20:11:24 -04002430 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002431 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002432 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433
Paul Menageddbcc7e2007-10-18 23:39:30 -07002434 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002435 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002436 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002437 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002438 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002439 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002440 return -EINVAL;
2441}
2442
Paul Menage91796562008-04-29 01:00:01 -07002443/*
2444 * seqfile ops/methods for returning structured data. Currently just
2445 * supports string->u64 maps, but can be extended in future.
2446 */
2447
Paul Menage91796562008-04-29 01:00:01 -07002448static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2449{
2450 struct seq_file *sf = cb->state;
2451 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2452}
2453
2454static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2455{
Li Zefane0798ce2013-07-31 17:36:25 +08002456 struct cfent *cfe = m->private;
2457 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002458 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002459
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002460 if (cft->read_map) {
2461 struct cgroup_map_cb cb = {
2462 .fill = cgroup_map_add,
2463 .state = m,
2464 };
Tejun Heo182446d2013-08-08 20:11:24 -04002465 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002466 }
Tejun Heo182446d2013-08-08 20:11:24 -04002467 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002468}
2469
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002470static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002471 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002472 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002473 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002474 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002475};
2476
Paul Menageddbcc7e2007-10-18 23:39:30 -07002477static int cgroup_file_open(struct inode *inode, struct file *file)
2478{
Tejun Heof7d58812013-08-08 20:11:23 -04002479 struct cfent *cfe = __d_cfe(file->f_dentry);
2480 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002481 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2482 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002484
2485 err = generic_file_open(inode, file);
2486 if (err)
2487 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002488
2489 /*
2490 * If the file belongs to a subsystem, pin the css. Will be
2491 * unpinned either on open failure or release. This ensures that
2492 * @css stays alive for all file operations.
2493 */
Tejun Heo105347b2013-08-13 11:01:55 -04002494 rcu_read_lock();
2495 if (cft->ss) {
2496 css = cgroup_css(cgrp, cft->ss->subsys_id);
2497 if (!css_tryget(css))
2498 css = NULL;
2499 } else {
2500 css = &cgrp->dummy_css;
2501 }
2502 rcu_read_unlock();
2503
2504 /* css should match @cfe->css, see cgroup_add_file() for details */
2505 if (!css || WARN_ON_ONCE(css != cfe->css))
Tejun Heof7d58812013-08-08 20:11:23 -04002506 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002507
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002508 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002509 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002510 err = single_open(file, cgroup_seqfile_show, cfe);
2511 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002512 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002513 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002514
Tejun Heo67f4c362013-08-08 20:11:24 -04002515 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002516 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002517 return err;
2518}
2519
2520static int cgroup_file_release(struct inode *inode, struct file *file)
2521{
Tejun Heof7d58812013-08-08 20:11:23 -04002522 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002523 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002524 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002525 int ret = 0;
2526
Paul Menageddbcc7e2007-10-18 23:39:30 -07002527 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002528 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002529 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002530 css_put(css);
2531 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002532}
2533
2534/*
2535 * cgroup_rename - Only allow simple rename of directories in place.
2536 */
2537static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2538 struct inode *new_dir, struct dentry *new_dentry)
2539{
Li Zefan65dff752013-03-01 15:01:56 +08002540 int ret;
2541 struct cgroup_name *name, *old_name;
2542 struct cgroup *cgrp;
2543
2544 /*
2545 * It's convinient to use parent dir's i_mutex to protected
2546 * cgrp->name.
2547 */
2548 lockdep_assert_held(&old_dir->i_mutex);
2549
Paul Menageddbcc7e2007-10-18 23:39:30 -07002550 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2551 return -ENOTDIR;
2552 if (new_dentry->d_inode)
2553 return -EEXIST;
2554 if (old_dir != new_dir)
2555 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002556
2557 cgrp = __d_cgrp(old_dentry);
2558
Tejun Heo6db8e852013-06-14 11:18:22 -07002559 /*
2560 * This isn't a proper migration and its usefulness is very
2561 * limited. Disallow if sane_behavior.
2562 */
2563 if (cgroup_sane_behavior(cgrp))
2564 return -EPERM;
2565
Li Zefan65dff752013-03-01 15:01:56 +08002566 name = cgroup_alloc_name(new_dentry);
2567 if (!name)
2568 return -ENOMEM;
2569
2570 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2571 if (ret) {
2572 kfree(name);
2573 return ret;
2574 }
2575
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002576 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002577 rcu_assign_pointer(cgrp->name, name);
2578
2579 kfree_rcu(old_name, rcu_head);
2580 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002581}
2582
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002583static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2584{
2585 if (S_ISDIR(dentry->d_inode->i_mode))
2586 return &__d_cgrp(dentry)->xattrs;
2587 else
Li Zefan712317a2013-04-18 23:09:52 -07002588 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002589}
2590
2591static inline int xattr_enabled(struct dentry *dentry)
2592{
2593 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002594 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002595}
2596
2597static bool is_valid_xattr(const char *name)
2598{
2599 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2600 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2601 return true;
2602 return false;
2603}
2604
2605static int cgroup_setxattr(struct dentry *dentry, const char *name,
2606 const void *val, size_t size, int flags)
2607{
2608 if (!xattr_enabled(dentry))
2609 return -EOPNOTSUPP;
2610 if (!is_valid_xattr(name))
2611 return -EINVAL;
2612 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2613}
2614
2615static int cgroup_removexattr(struct dentry *dentry, const char *name)
2616{
2617 if (!xattr_enabled(dentry))
2618 return -EOPNOTSUPP;
2619 if (!is_valid_xattr(name))
2620 return -EINVAL;
2621 return simple_xattr_remove(__d_xattrs(dentry), name);
2622}
2623
2624static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2625 void *buf, size_t size)
2626{
2627 if (!xattr_enabled(dentry))
2628 return -EOPNOTSUPP;
2629 if (!is_valid_xattr(name))
2630 return -EINVAL;
2631 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2632}
2633
2634static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2635{
2636 if (!xattr_enabled(dentry))
2637 return -EOPNOTSUPP;
2638 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2639}
2640
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002641static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002642 .read = cgroup_file_read,
2643 .write = cgroup_file_write,
2644 .llseek = generic_file_llseek,
2645 .open = cgroup_file_open,
2646 .release = cgroup_file_release,
2647};
2648
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002649static const struct inode_operations cgroup_file_inode_operations = {
2650 .setxattr = cgroup_setxattr,
2651 .getxattr = cgroup_getxattr,
2652 .listxattr = cgroup_listxattr,
2653 .removexattr = cgroup_removexattr,
2654};
2655
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002656static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002657 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002658 .mkdir = cgroup_mkdir,
2659 .rmdir = cgroup_rmdir,
2660 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002661 .setxattr = cgroup_setxattr,
2662 .getxattr = cgroup_getxattr,
2663 .listxattr = cgroup_listxattr,
2664 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002665};
2666
Al Viro00cd8dd2012-06-10 17:13:09 -04002667static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002668{
2669 if (dentry->d_name.len > NAME_MAX)
2670 return ERR_PTR(-ENAMETOOLONG);
2671 d_add(dentry, NULL);
2672 return NULL;
2673}
2674
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002675/*
2676 * Check if a file is a control file
2677 */
2678static inline struct cftype *__file_cft(struct file *file)
2679{
Al Viro496ad9a2013-01-23 17:07:38 -05002680 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002681 return ERR_PTR(-EINVAL);
2682 return __d_cft(file->f_dentry);
2683}
2684
Al Viroa5e7ed32011-07-26 01:55:55 -04002685static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002686 struct super_block *sb)
2687{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688 struct inode *inode;
2689
2690 if (!dentry)
2691 return -ENOENT;
2692 if (dentry->d_inode)
2693 return -EEXIST;
2694
2695 inode = cgroup_new_inode(mode, sb);
2696 if (!inode)
2697 return -ENOMEM;
2698
2699 if (S_ISDIR(mode)) {
2700 inode->i_op = &cgroup_dir_inode_operations;
2701 inode->i_fop = &simple_dir_operations;
2702
2703 /* start off with i_nlink == 2 (for "." entry) */
2704 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002705 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706
Tejun Heob8a2df62012-11-19 08:13:37 -08002707 /*
2708 * Control reaches here with cgroup_mutex held.
2709 * @inode->i_mutex should nest outside cgroup_mutex but we
2710 * want to populate it immediately without releasing
2711 * cgroup_mutex. As @inode isn't visible to anyone else
2712 * yet, trylock will always succeed without affecting
2713 * lockdep checks.
2714 */
2715 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716 } else if (S_ISREG(mode)) {
2717 inode->i_size = 0;
2718 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002719 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002720 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 d_instantiate(dentry, inode);
2722 dget(dentry); /* Extra count - pin the dentry in core */
2723 return 0;
2724}
2725
Li Zefan099fca32009-04-02 16:57:29 -07002726/**
2727 * cgroup_file_mode - deduce file mode of a control file
2728 * @cft: the control file in question
2729 *
2730 * returns cft->mode if ->mode is not 0
2731 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2732 * returns S_IRUGO if it has only a read handler
2733 * returns S_IWUSR if it has only a write hander
2734 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002735static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002736{
Al Viroa5e7ed32011-07-26 01:55:55 -04002737 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002738
2739 if (cft->mode)
2740 return cft->mode;
2741
2742 if (cft->read || cft->read_u64 || cft->read_s64 ||
2743 cft->read_map || cft->read_seq_string)
2744 mode |= S_IRUGO;
2745
2746 if (cft->write || cft->write_u64 || cft->write_s64 ||
2747 cft->write_string || cft->trigger)
2748 mode |= S_IWUSR;
2749
2750 return mode;
2751}
2752
Tejun Heo2bb566c2013-08-08 20:11:23 -04002753static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754{
Paul Menagebd89aab2007-10-18 23:40:44 -07002755 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002756 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002757 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002758 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002759 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002760 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002761 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002762
Tejun Heo2bb566c2013-08-08 20:11:23 -04002763 if (cft->ss && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2764 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002765 strcat(name, ".");
2766 }
2767 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002768
Paul Menageddbcc7e2007-10-18 23:39:30 -07002769 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002770
2771 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2772 if (!cfe)
2773 return -ENOMEM;
2774
Paul Menageddbcc7e2007-10-18 23:39:30 -07002775 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002776 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002777 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002778 goto out;
2779 }
2780
Li Zefand6cbf352013-05-14 19:44:20 +08002781 cfe->type = (void *)cft;
2782 cfe->dentry = dentry;
2783 dentry->d_fsdata = cfe;
2784 simple_xattrs_init(&cfe->xattrs);
2785
Tejun Heo105347b2013-08-13 11:01:55 -04002786 /*
2787 * cfe->css is used by read/write/close to determine the associated
2788 * css. file->private_data would be a better place but that's
2789 * already used by seqfile. Note that open will use the usual
2790 * cgroup_css() and css_tryget() to acquire the css and this
2791 * caching doesn't affect css lifetime management.
2792 */
2793 if (cft->ss)
2794 cfe->css = cgroup_css(cgrp, cft->ss->subsys_id);
2795 else
2796 cfe->css = &cgrp->dummy_css;
2797
Tejun Heo05ef1d72012-04-01 12:09:56 -07002798 mode = cgroup_file_mode(cft);
2799 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2800 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002801 list_add_tail(&cfe->node, &parent->files);
2802 cfe = NULL;
2803 }
2804 dput(dentry);
2805out:
2806 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002807 return error;
2808}
2809
Tejun Heob1f28d32013-06-28 16:24:10 -07002810/**
2811 * cgroup_addrm_files - add or remove files to a cgroup directory
2812 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002813 * @cfts: array of cftypes to be added
2814 * @is_add: whether to add or remove
2815 *
2816 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002817 * For removals, this function never fails. If addition fails, this
2818 * function doesn't remove files already added. The caller is responsible
2819 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002820 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002821static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2822 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002823{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002824 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002825 int ret;
2826
2827 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2828 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002829
2830 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002831 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002832 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2833 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002834 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2835 continue;
2836 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2837 continue;
2838
Li Zefan2739d3c2013-01-21 18:18:33 +08002839 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002840 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002841 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002842 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002843 cft->name, ret);
2844 return ret;
2845 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002846 } else {
2847 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002848 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002849 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002850 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002851}
2852
Tejun Heo8e3f6542012-04-01 12:09:55 -07002853static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002854 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002855{
2856 /*
2857 * Thanks to the entanglement with vfs inode locking, we can't walk
2858 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002859 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2860 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002861 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002862 mutex_lock(&cgroup_mutex);
2863}
2864
Tejun Heo2bb566c2013-08-08 20:11:23 -04002865static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002866 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002867{
2868 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002869 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002870 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002871 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002872 struct dentry *prev = NULL;
2873 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002874 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002875 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002876 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002877
2878 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002879 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002880 !atomic_inc_not_zero(&sb->s_active)) {
2881 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002882 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002883 }
2884
Li Zefane8c82d22013-06-18 18:48:37 +08002885 /*
2886 * All cgroups which are created after we drop cgroup_mutex will
2887 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002888 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002889 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002890 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002891
Tejun Heo8e3f6542012-04-01 12:09:55 -07002892 mutex_unlock(&cgroup_mutex);
2893
Li Zefane8c82d22013-06-18 18:48:37 +08002894 /* add/rm files for all cgroups created before */
2895 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002896 css_for_each_descendant_pre(css, cgroup_css(root, ss->subsys_id)) {
2897 struct cgroup *cgrp = css->cgroup;
2898
Li Zefane8c82d22013-06-18 18:48:37 +08002899 if (cgroup_is_dead(cgrp))
2900 continue;
2901
2902 inode = cgrp->dentry->d_inode;
2903 dget(cgrp->dentry);
2904 rcu_read_unlock();
2905
2906 dput(prev);
2907 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002908
2909 mutex_lock(&inode->i_mutex);
2910 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002911 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002912 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002913 mutex_unlock(&cgroup_mutex);
2914 mutex_unlock(&inode->i_mutex);
2915
Li Zefane8c82d22013-06-18 18:48:37 +08002916 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002917 if (ret)
2918 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002919 }
Li Zefane8c82d22013-06-18 18:48:37 +08002920 rcu_read_unlock();
2921 dput(prev);
2922 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002923 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002924}
2925
2926/**
2927 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2928 * @ss: target cgroup subsystem
2929 * @cfts: zero-length name terminated array of cftypes
2930 *
2931 * Register @cfts to @ss. Files described by @cfts are created for all
2932 * existing cgroups to which @ss is attached and all future cgroups will
2933 * have them too. This function can be called anytime whether @ss is
2934 * attached or not.
2935 *
2936 * Returns 0 on successful registration, -errno on failure. Note that this
2937 * function currently returns 0 as long as @cfts registration is successful
2938 * even if some file creation attempts on existing cgroups fail.
2939 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002940int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002941{
2942 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002943 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002944 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002945
2946 set = kzalloc(sizeof(*set), GFP_KERNEL);
2947 if (!set)
2948 return -ENOMEM;
2949
Tejun Heo2bb566c2013-08-08 20:11:23 -04002950 for (cft = cfts; cft->name[0] != '\0'; cft++)
2951 cft->ss = ss;
2952
Tejun Heo8e3f6542012-04-01 12:09:55 -07002953 cgroup_cfts_prepare();
2954 set->cfts = cfts;
2955 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002956 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002957 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002958 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002959 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002960}
2961EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2962
Li Zefana043e3b2008-02-23 15:24:09 -08002963/**
Tejun Heo79578622012-04-01 12:09:56 -07002964 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002965 * @cfts: zero-length name terminated array of cftypes
2966 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002967 * Unregister @cfts. Files described by @cfts are removed from all
2968 * existing cgroups and all future cgroups won't have them either. This
2969 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002970 *
2971 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002972 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002973 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002974int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002975{
2976 struct cftype_set *set;
2977
Tejun Heo2bb566c2013-08-08 20:11:23 -04002978 if (!cfts || !cfts[0].ss)
2979 return -ENOENT;
2980
Tejun Heo79578622012-04-01 12:09:56 -07002981 cgroup_cfts_prepare();
2982
Tejun Heo2bb566c2013-08-08 20:11:23 -04002983 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002984 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002985 list_del(&set->node);
2986 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002987 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002988 return 0;
2989 }
2990 }
2991
Tejun Heo2bb566c2013-08-08 20:11:23 -04002992 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002993 return -ENOENT;
2994}
2995
2996/**
Li Zefana043e3b2008-02-23 15:24:09 -08002997 * cgroup_task_count - count the number of tasks in a cgroup.
2998 * @cgrp: the cgroup in question
2999 *
3000 * Return the number of tasks in the cgroup.
3001 */
Paul Menagebd89aab2007-10-18 23:40:44 -07003002int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003003{
3004 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003005 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003006
Paul Menage817929e2007-10-18 23:39:36 -07003007 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003008 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3009 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07003010 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003011 return count;
3012}
3013
3014/*
Tejun Heo0942eee2013-08-08 20:11:26 -04003015 * To reduce the fork() overhead for systems that are not actually using
3016 * their cgroups capability, we don't maintain the lists running through
3017 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04003018 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003019 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003020static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003021{
3022 struct task_struct *p, *g;
3023 write_lock(&css_set_lock);
3024 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003025 /*
3026 * We need tasklist_lock because RCU is not safe against
3027 * while_each_thread(). Besides, a forking task that has passed
3028 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3029 * is not guaranteed to have its child immediately visible in the
3030 * tasklist if we walk through it with RCU.
3031 */
3032 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003033 do_each_thread(g, p) {
3034 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003035 /*
3036 * We should check if the process is exiting, otherwise
3037 * it will race with cgroup_exit() in that the list
3038 * entry won't be deleted though the process has exited.
3039 */
3040 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003041 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003042 task_unlock(p);
3043 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003044 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003045 write_unlock(&css_set_lock);
3046}
3047
Tejun Heo574bd9f2012-11-09 09:12:29 -08003048/**
Tejun Heo492eb212013-08-08 20:11:25 -04003049 * css_next_child - find the next child of a given css
3050 * @pos_css: the current position (%NULL to initiate traversal)
3051 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003052 *
Tejun Heo492eb212013-08-08 20:11:25 -04003053 * This function returns the next child of @parent_css and should be called
3054 * under RCU read lock. The only requirement is that @parent_css and
3055 * @pos_css are accessible. The next sibling is guaranteed to be returned
3056 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003057 */
Tejun Heo492eb212013-08-08 20:11:25 -04003058struct cgroup_subsys_state *
3059css_next_child(struct cgroup_subsys_state *pos_css,
3060 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003061{
Tejun Heo492eb212013-08-08 20:11:25 -04003062 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3063 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003064 struct cgroup *next;
3065
3066 WARN_ON_ONCE(!rcu_read_lock_held());
3067
3068 /*
3069 * @pos could already have been removed. Once a cgroup is removed,
3070 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003071 * changes. As CGRP_DEAD assertion is serialized and happens
3072 * before the cgroup is taken off the ->sibling list, if we see it
3073 * unasserted, it's guaranteed that the next sibling hasn't
3074 * finished its grace period even if it's already removed, and thus
3075 * safe to dereference from this RCU critical section. If
3076 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3077 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003078 *
3079 * If @pos is dead, its next pointer can't be dereferenced;
3080 * however, as each cgroup is given a monotonically increasing
3081 * unique serial number and always appended to the sibling list,
3082 * the next one can be found by walking the parent's children until
3083 * we see a cgroup with higher serial number than @pos's. While
3084 * this path can be slower, it's taken only when either the current
3085 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003086 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003087 if (!pos) {
3088 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3089 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003090 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003091 } else {
3092 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3093 if (next->serial_nr > pos->serial_nr)
3094 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003095 }
3096
Tejun Heo492eb212013-08-08 20:11:25 -04003097 if (&next->sibling == &cgrp->children)
3098 return NULL;
3099
3100 if (parent_css->ss)
3101 return cgroup_css(next, parent_css->ss->subsys_id);
3102 else
3103 return &next->dummy_css;
Tejun Heo53fa5262013-05-24 10:55:38 +09003104}
Tejun Heo492eb212013-08-08 20:11:25 -04003105EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003106
3107/**
Tejun Heo492eb212013-08-08 20:11:25 -04003108 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003109 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003110 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003111 *
Tejun Heo492eb212013-08-08 20:11:25 -04003112 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003113 * to visit for pre-order traversal of @root's descendants. @root is
3114 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003115 *
3116 * While this function requires RCU read locking, it doesn't require the
3117 * whole traversal to be contained in a single RCU critical section. This
3118 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003119 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003120 */
Tejun Heo492eb212013-08-08 20:11:25 -04003121struct cgroup_subsys_state *
3122css_next_descendant_pre(struct cgroup_subsys_state *pos,
3123 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003124{
Tejun Heo492eb212013-08-08 20:11:25 -04003125 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003126
3127 WARN_ON_ONCE(!rcu_read_lock_held());
3128
Tejun Heobd8815a2013-08-08 20:11:27 -04003129 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003130 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003131 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003132
3133 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003134 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003135 if (next)
3136 return next;
3137
3138 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003139 while (pos != root) {
3140 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003141 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003142 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003143 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003144 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003145
3146 return NULL;
3147}
Tejun Heo492eb212013-08-08 20:11:25 -04003148EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003149
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003150/**
Tejun Heo492eb212013-08-08 20:11:25 -04003151 * css_rightmost_descendant - return the rightmost descendant of a css
3152 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003153 *
Tejun Heo492eb212013-08-08 20:11:25 -04003154 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3155 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003156 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003157 *
3158 * While this function requires RCU read locking, it doesn't require the
3159 * whole traversal to be contained in a single RCU critical section. This
3160 * function will return the correct rightmost descendant as long as @pos is
3161 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003162 */
Tejun Heo492eb212013-08-08 20:11:25 -04003163struct cgroup_subsys_state *
3164css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003165{
Tejun Heo492eb212013-08-08 20:11:25 -04003166 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003167
3168 WARN_ON_ONCE(!rcu_read_lock_held());
3169
3170 do {
3171 last = pos;
3172 /* ->prev isn't RCU safe, walk ->next till the end */
3173 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003174 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003175 pos = tmp;
3176 } while (pos);
3177
3178 return last;
3179}
Tejun Heo492eb212013-08-08 20:11:25 -04003180EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003181
Tejun Heo492eb212013-08-08 20:11:25 -04003182static struct cgroup_subsys_state *
3183css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003184{
Tejun Heo492eb212013-08-08 20:11:25 -04003185 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003186
3187 do {
3188 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003189 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003190 } while (pos);
3191
3192 return last;
3193}
3194
3195/**
Tejun Heo492eb212013-08-08 20:11:25 -04003196 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003197 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003198 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003199 *
Tejun Heo492eb212013-08-08 20:11:25 -04003200 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003201 * to visit for post-order traversal of @root's descendants. @root is
3202 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003203 *
3204 * While this function requires RCU read locking, it doesn't require the
3205 * whole traversal to be contained in a single RCU critical section. This
3206 * function will return the correct next descendant as long as both @pos
3207 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003208 */
Tejun Heo492eb212013-08-08 20:11:25 -04003209struct cgroup_subsys_state *
3210css_next_descendant_post(struct cgroup_subsys_state *pos,
3211 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003212{
Tejun Heo492eb212013-08-08 20:11:25 -04003213 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003214
3215 WARN_ON_ONCE(!rcu_read_lock_held());
3216
3217 /* if first iteration, visit the leftmost descendant */
3218 if (!pos) {
Tejun Heo492eb212013-08-08 20:11:25 -04003219 next = css_leftmost_descendant(root);
3220 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003221 }
3222
Tejun Heobd8815a2013-08-08 20:11:27 -04003223 /* if we visited @root, we're done */
3224 if (pos == root)
3225 return NULL;
3226
Tejun Heo574bd9f2012-11-09 09:12:29 -08003227 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003228 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003229 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003230 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003231
3232 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003233 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003234}
Tejun Heo492eb212013-08-08 20:11:25 -04003235EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003236
Tejun Heo0942eee2013-08-08 20:11:26 -04003237/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003238 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003239 * @it: the iterator to advance
3240 *
3241 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003242 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003243static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003244{
3245 struct list_head *l = it->cset_link;
3246 struct cgrp_cset_link *link;
3247 struct css_set *cset;
3248
3249 /* Advance to the next non-empty css_set */
3250 do {
3251 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003252 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003253 it->cset_link = NULL;
3254 return;
3255 }
3256 link = list_entry(l, struct cgrp_cset_link, cset_link);
3257 cset = link->cset;
3258 } while (list_empty(&cset->tasks));
3259 it->cset_link = l;
3260 it->task = cset->tasks.next;
3261}
3262
Tejun Heo0942eee2013-08-08 20:11:26 -04003263/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003264 * css_task_iter_start - initiate task iteration
3265 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003266 * @it: the task iterator to use
3267 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003268 * Initiate iteration through the tasks of @css. The caller can call
3269 * css_task_iter_next() to walk through the tasks until the function
3270 * returns NULL. On completion of iteration, css_task_iter_end() must be
3271 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003272 *
3273 * Note that this function acquires a lock which is released when the
3274 * iteration finishes. The caller can't sleep while iteration is in
3275 * progress.
3276 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003277void css_task_iter_start(struct cgroup_subsys_state *css,
3278 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003279 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003280{
3281 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003282 * The first time anyone tries to iterate across a css, we need to
3283 * enable the list linking each css_set to its tasks, and fix up
3284 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003285 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003286 if (!use_task_css_set_links)
3287 cgroup_enable_task_cg_lists();
3288
Paul Menage817929e2007-10-18 23:39:36 -07003289 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003290
Tejun Heo72ec7022013-08-08 20:11:26 -04003291 it->origin_css = css;
3292 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003293
Tejun Heo72ec7022013-08-08 20:11:26 -04003294 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003295}
3296
Tejun Heo0942eee2013-08-08 20:11:26 -04003297/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003298 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003299 * @it: the task iterator being iterated
3300 *
3301 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003302 * initialized via css_task_iter_start(). Returns NULL when the iteration
3303 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003304 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003305struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003306{
3307 struct task_struct *res;
3308 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003309 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003310
3311 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003312 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003313 return NULL;
3314 res = list_entry(l, struct task_struct, cg_list);
3315 /* Advance iterator to find next entry */
3316 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003317 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3318 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003319 /*
3320 * We reached the end of this task list - move on to the
3321 * next cgrp_cset_link.
3322 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003323 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003324 } else {
3325 it->task = l;
3326 }
3327 return res;
3328}
3329
Tejun Heo0942eee2013-08-08 20:11:26 -04003330/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003331 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003332 * @it: the task iterator to finish
3333 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003334 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003335 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003336void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003337 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003338{
3339 read_unlock(&css_set_lock);
3340}
3341
Cliff Wickman31a7df02008-02-07 00:14:42 -08003342static inline int started_after_time(struct task_struct *t1,
3343 struct timespec *time,
3344 struct task_struct *t2)
3345{
3346 int start_diff = timespec_compare(&t1->start_time, time);
3347 if (start_diff > 0) {
3348 return 1;
3349 } else if (start_diff < 0) {
3350 return 0;
3351 } else {
3352 /*
3353 * Arbitrarily, if two processes started at the same
3354 * time, we'll say that the lower pointer value
3355 * started first. Note that t2 may have exited by now
3356 * so this may not be a valid pointer any longer, but
3357 * that's fine - it still serves to distinguish
3358 * between two tasks started (effectively) simultaneously.
3359 */
3360 return t1 > t2;
3361 }
3362}
3363
3364/*
3365 * This function is a callback from heap_insert() and is used to order
3366 * the heap.
3367 * In this case we order the heap in descending task start time.
3368 */
3369static inline int started_after(void *p1, void *p2)
3370{
3371 struct task_struct *t1 = p1;
3372 struct task_struct *t2 = p2;
3373 return started_after_time(t1, &t2->start_time, t2);
3374}
3375
3376/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003377 * css_scan_tasks - iterate though all the tasks in a css
3378 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003379 * @test: optional test callback
3380 * @process: process callback
3381 * @data: data passed to @test and @process
3382 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003383 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003384 * Iterate through all the tasks in @css, calling @test for each, and if it
3385 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003386 *
Tejun Heoe5358372013-08-08 20:11:26 -04003387 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003388 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003389 * lock css_set_lock for the call to @process.
3390 *
3391 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003392 * of @css for the duration of this call. This function may or may not
3393 * call @process for tasks that exit or move to a different css during the
3394 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003395 *
3396 * Note that @test may be called with locks held, and may in some
3397 * situations be called multiple times for the same task, so it should be
3398 * cheap.
3399 *
3400 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3401 * heap operations (and its "gt" member will be overwritten), else a
3402 * temporary heap will be used (allocation of which may cause this function
3403 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003404 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003405int css_scan_tasks(struct cgroup_subsys_state *css,
3406 bool (*test)(struct task_struct *, void *),
3407 void (*process)(struct task_struct *, void *),
3408 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003409{
3410 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003411 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003412 struct task_struct *p, *dropped;
3413 /* Never dereference latest_task, since it's not refcounted */
3414 struct task_struct *latest_task = NULL;
3415 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003416 struct timespec latest_time = { 0, 0 };
3417
Tejun Heoe5358372013-08-08 20:11:26 -04003418 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003419 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003420 heap->gt = &started_after;
3421 } else {
3422 /* We need to allocate our own heap memory */
3423 heap = &tmp_heap;
3424 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3425 if (retval)
3426 /* cannot allocate the heap */
3427 return retval;
3428 }
3429
3430 again:
3431 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003432 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003433 * which are of interest, and invoking @process callback on the
3434 * ones which need an update. Since we don't want to hold any
3435 * locks during the task updates, gather tasks to be processed in a
3436 * heap structure. The heap is sorted by descending task start
3437 * time. If the statically-sized heap fills up, we overflow tasks
3438 * that started later, and in future iterations only consider tasks
3439 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003440 * guarantees forward progress and that we don't miss any tasks.
3441 */
3442 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003443 css_task_iter_start(css, &it);
3444 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003445 /*
3446 * Only affect tasks that qualify per the caller's callback,
3447 * if he provided one
3448 */
Tejun Heoe5358372013-08-08 20:11:26 -04003449 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003450 continue;
3451 /*
3452 * Only process tasks that started after the last task
3453 * we processed
3454 */
3455 if (!started_after_time(p, &latest_time, latest_task))
3456 continue;
3457 dropped = heap_insert(heap, p);
3458 if (dropped == NULL) {
3459 /*
3460 * The new task was inserted; the heap wasn't
3461 * previously full
3462 */
3463 get_task_struct(p);
3464 } else if (dropped != p) {
3465 /*
3466 * The new task was inserted, and pushed out a
3467 * different task
3468 */
3469 get_task_struct(p);
3470 put_task_struct(dropped);
3471 }
3472 /*
3473 * Else the new task was newer than anything already in
3474 * the heap and wasn't inserted
3475 */
3476 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003477 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003478
3479 if (heap->size) {
3480 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003481 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003482 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003483 latest_time = q->start_time;
3484 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003485 }
3486 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003487 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003488 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003489 }
3490 /*
3491 * If we had to process any tasks at all, scan again
3492 * in case some of them were in the middle of forking
3493 * children that didn't get processed.
3494 * Not the most efficient way to do it, but it avoids
3495 * having to take callback_mutex in the fork path
3496 */
3497 goto again;
3498 }
3499 if (heap == &tmp_heap)
3500 heap_free(&tmp_heap);
3501 return 0;
3502}
3503
Tejun Heoe5358372013-08-08 20:11:26 -04003504static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003505{
Tejun Heoe5358372013-08-08 20:11:26 -04003506 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003507
Tejun Heo47cfcd02013-04-07 09:29:51 -07003508 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003509 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003510 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003511}
3512
3513/**
3514 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3515 * @to: cgroup to which the tasks will be moved
3516 * @from: cgroup in which the tasks currently reside
3517 */
3518int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3519{
Tejun Heo72ec7022013-08-08 20:11:26 -04003520 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3521 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003522}
3523
Paul Menage817929e2007-10-18 23:39:36 -07003524/*
Ben Blum102a7752009-09-23 15:56:26 -07003525 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003526 *
3527 * Reading this file can return large amounts of data if a cgroup has
3528 * *lots* of attached tasks. So it may need several calls to read(),
3529 * but we cannot guarantee that the information we produce is correct
3530 * unless we produce it entirely atomically.
3531 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003532 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003533
Li Zefan24528252012-01-20 11:58:43 +08003534/* which pidlist file are we talking about? */
3535enum cgroup_filetype {
3536 CGROUP_FILE_PROCS,
3537 CGROUP_FILE_TASKS,
3538};
3539
3540/*
3541 * A pidlist is a list of pids that virtually represents the contents of one
3542 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3543 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3544 * to the cgroup.
3545 */
3546struct cgroup_pidlist {
3547 /*
3548 * used to find which pidlist is wanted. doesn't change as long as
3549 * this particular list stays in the list.
3550 */
3551 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3552 /* array of xids */
3553 pid_t *list;
3554 /* how many elements the above list has */
3555 int length;
3556 /* how many files are using the current array */
3557 int use_count;
3558 /* each of these stored in a list by its cgroup */
3559 struct list_head links;
3560 /* pointer to the cgroup we belong to, for list removal purposes */
3561 struct cgroup *owner;
3562 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003563 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003564};
3565
Paul Menagebbcb81d2007-10-18 23:39:32 -07003566/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003567 * The following two functions "fix" the issue where there are more pids
3568 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3569 * TODO: replace with a kernel-wide solution to this problem
3570 */
3571#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3572static void *pidlist_allocate(int count)
3573{
3574 if (PIDLIST_TOO_LARGE(count))
3575 return vmalloc(count * sizeof(pid_t));
3576 else
3577 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3578}
3579static void pidlist_free(void *p)
3580{
3581 if (is_vmalloc_addr(p))
3582 vfree(p);
3583 else
3584 kfree(p);
3585}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003586
3587/*
Ben Blum102a7752009-09-23 15:56:26 -07003588 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003589 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003590 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003591static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003592{
Ben Blum102a7752009-09-23 15:56:26 -07003593 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003594
3595 /*
3596 * we presume the 0th element is unique, so i starts at 1. trivial
3597 * edge cases first; no work needs to be done for either
3598 */
3599 if (length == 0 || length == 1)
3600 return length;
3601 /* src and dest walk down the list; dest counts unique elements */
3602 for (src = 1; src < length; src++) {
3603 /* find next unique element */
3604 while (list[src] == list[src-1]) {
3605 src++;
3606 if (src == length)
3607 goto after;
3608 }
3609 /* dest always points to where the next unique element goes */
3610 list[dest] = list[src];
3611 dest++;
3612 }
3613after:
Ben Blum102a7752009-09-23 15:56:26 -07003614 return dest;
3615}
3616
3617static int cmppid(const void *a, const void *b)
3618{
3619 return *(pid_t *)a - *(pid_t *)b;
3620}
3621
3622/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003623 * find the appropriate pidlist for our purpose (given procs vs tasks)
3624 * returns with the lock on that pidlist already held, and takes care
3625 * of the use count, or returns NULL with no locks held if we're out of
3626 * memory.
3627 */
3628static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3629 enum cgroup_filetype type)
3630{
3631 struct cgroup_pidlist *l;
3632 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003633 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003634
Ben Blum72a8cb32009-09-23 15:56:27 -07003635 /*
Li Zefanb3958902013-08-01 09:52:15 +08003636 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003637 * the last ref-holder is trying to remove l from the list at the same
3638 * time. Holding the pidlist_mutex precludes somebody taking whichever
3639 * list we find out from under us - compare release_pid_array().
3640 */
3641 mutex_lock(&cgrp->pidlist_mutex);
3642 list_for_each_entry(l, &cgrp->pidlists, links) {
3643 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003644 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003645 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003646 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003647 return l;
3648 }
3649 }
3650 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003651 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003652 if (!l) {
3653 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003654 return l;
3655 }
Li Zefanb3958902013-08-01 09:52:15 +08003656 init_rwsem(&l->rwsem);
3657 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003658 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003659 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003660 l->owner = cgrp;
3661 list_add(&l->links, &cgrp->pidlists);
3662 mutex_unlock(&cgrp->pidlist_mutex);
3663 return l;
3664}
3665
3666/*
Ben Blum102a7752009-09-23 15:56:26 -07003667 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3668 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003669static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3670 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003671{
3672 pid_t *array;
3673 int length;
3674 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003675 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003676 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003677 struct cgroup_pidlist *l;
3678
3679 /*
3680 * If cgroup gets more users after we read count, we won't have
3681 * enough space - tough. This race is indistinguishable to the
3682 * caller from the case that the additional cgroup users didn't
3683 * show up until sometime later on.
3684 */
3685 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003686 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003687 if (!array)
3688 return -ENOMEM;
3689 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003690 css_task_iter_start(&cgrp->dummy_css, &it);
3691 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003692 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003693 break;
Ben Blum102a7752009-09-23 15:56:26 -07003694 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 if (type == CGROUP_FILE_PROCS)
3696 pid = task_tgid_vnr(tsk);
3697 else
3698 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003699 if (pid > 0) /* make sure to only use valid results */
3700 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003701 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003702 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003703 length = n;
3704 /* now sort & (if procs) strip out duplicates */
3705 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003706 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003707 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003708 l = cgroup_pidlist_find(cgrp, type);
3709 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003710 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003711 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003712 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003713 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003714 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003715 l->list = array;
3716 l->length = length;
3717 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003718 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003719 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003720 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003721}
3722
Balbir Singh846c7bb2007-10-18 23:39:44 -07003723/**
Li Zefana043e3b2008-02-23 15:24:09 -08003724 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003725 * @stats: cgroupstats to fill information into
3726 * @dentry: A dentry entry belonging to the cgroup for which stats have
3727 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003728 *
3729 * Build and fill cgroupstats so that taskstats can export it to user
3730 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003731 */
3732int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3733{
3734 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003735 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003736 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003737 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003738
Balbir Singh846c7bb2007-10-18 23:39:44 -07003739 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003740 * Validate dentry by checking the superblock operations,
3741 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003742 */
Li Zefan33d283b2008-11-19 15:36:48 -08003743 if (dentry->d_sb->s_op != &cgroup_ops ||
3744 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003745 goto err;
3746
3747 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003748 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003749
Tejun Heo72ec7022013-08-08 20:11:26 -04003750 css_task_iter_start(&cgrp->dummy_css, &it);
3751 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003752 switch (tsk->state) {
3753 case TASK_RUNNING:
3754 stats->nr_running++;
3755 break;
3756 case TASK_INTERRUPTIBLE:
3757 stats->nr_sleeping++;
3758 break;
3759 case TASK_UNINTERRUPTIBLE:
3760 stats->nr_uninterruptible++;
3761 break;
3762 case TASK_STOPPED:
3763 stats->nr_stopped++;
3764 break;
3765 default:
3766 if (delayacct_is_task_waiting_on_io(tsk))
3767 stats->nr_io_wait++;
3768 break;
3769 }
3770 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003771 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003772
Balbir Singh846c7bb2007-10-18 23:39:44 -07003773err:
3774 return ret;
3775}
3776
Paul Menage8f3ff202009-09-23 15:56:25 -07003777
Paul Menagecc31edc2008-10-18 20:28:04 -07003778/*
Ben Blum102a7752009-09-23 15:56:26 -07003779 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003780 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003781 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003782 */
3783
Ben Blum102a7752009-09-23 15:56:26 -07003784static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003785{
3786 /*
3787 * Initially we receive a position value that corresponds to
3788 * one more than the last pid shown (or 0 on the first call or
3789 * after a seek to the start). Use a binary-search to find the
3790 * next pid to display, if any
3791 */
Ben Blum102a7752009-09-23 15:56:26 -07003792 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003793 int index = 0, pid = *pos;
3794 int *iter;
3795
Li Zefanb3958902013-08-01 09:52:15 +08003796 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003797 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003798 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003799
Paul Menagecc31edc2008-10-18 20:28:04 -07003800 while (index < end) {
3801 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003802 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003803 index = mid;
3804 break;
Ben Blum102a7752009-09-23 15:56:26 -07003805 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003806 index = mid + 1;
3807 else
3808 end = mid;
3809 }
3810 }
3811 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003812 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003813 return NULL;
3814 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003815 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003816 *pos = *iter;
3817 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003818}
3819
Ben Blum102a7752009-09-23 15:56:26 -07003820static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003821{
Ben Blum102a7752009-09-23 15:56:26 -07003822 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003823 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003824}
3825
Ben Blum102a7752009-09-23 15:56:26 -07003826static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003827{
Ben Blum102a7752009-09-23 15:56:26 -07003828 struct cgroup_pidlist *l = s->private;
3829 pid_t *p = v;
3830 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003831 /*
3832 * Advance to the next pid in the array. If this goes off the
3833 * end, we're done
3834 */
3835 p++;
3836 if (p >= end) {
3837 return NULL;
3838 } else {
3839 *pos = *p;
3840 return p;
3841 }
3842}
3843
Ben Blum102a7752009-09-23 15:56:26 -07003844static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003845{
3846 return seq_printf(s, "%d\n", *(int *)v);
3847}
3848
Ben Blum102a7752009-09-23 15:56:26 -07003849/*
3850 * seq_operations functions for iterating on pidlists through seq_file -
3851 * independent of whether it's tasks or procs
3852 */
3853static const struct seq_operations cgroup_pidlist_seq_operations = {
3854 .start = cgroup_pidlist_start,
3855 .stop = cgroup_pidlist_stop,
3856 .next = cgroup_pidlist_next,
3857 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003858};
3859
Ben Blum102a7752009-09-23 15:56:26 -07003860static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003861{
Ben Blum72a8cb32009-09-23 15:56:27 -07003862 /*
3863 * the case where we're the last user of this particular pidlist will
3864 * have us remove it from the cgroup's list, which entails taking the
3865 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3866 * pidlist_mutex, we have to take pidlist_mutex first.
3867 */
3868 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003869 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003870 BUG_ON(!l->use_count);
3871 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003872 /* we're the last user if refcount is 0; remove and free */
3873 list_del(&l->links);
3874 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003875 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003876 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003877 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003878 kfree(l);
3879 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003880 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003881 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003882 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003883}
3884
Ben Blum102a7752009-09-23 15:56:26 -07003885static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003886{
Ben Blum102a7752009-09-23 15:56:26 -07003887 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003888 if (!(file->f_mode & FMODE_READ))
3889 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003890 /*
3891 * the seq_file will only be initialized if the file was opened for
3892 * reading; hence we check if it's not null only in that case.
3893 */
3894 l = ((struct seq_file *)file->private_data)->private;
3895 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003896 return seq_release(inode, file);
3897}
3898
Ben Blum102a7752009-09-23 15:56:26 -07003899static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003900 .read = seq_read,
3901 .llseek = seq_lseek,
3902 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003903 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003904};
3905
3906/*
Ben Blum102a7752009-09-23 15:56:26 -07003907 * The following functions handle opens on a file that displays a pidlist
3908 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3909 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003910 */
Ben Blum102a7752009-09-23 15:56:26 -07003911/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003912static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003913{
3914 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003915 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003916 int retval;
3917
3918 /* Nothing to do for write-only files */
3919 if (!(file->f_mode & FMODE_READ))
3920 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003921
Ben Blum102a7752009-09-23 15:56:26 -07003922 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003923 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003924 if (retval)
3925 return retval;
3926 /* configure file information */
3927 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003928
Ben Blum102a7752009-09-23 15:56:26 -07003929 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003930 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003931 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003932 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003933 }
Ben Blum102a7752009-09-23 15:56:26 -07003934 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003935 return 0;
3936}
Ben Blum102a7752009-09-23 15:56:26 -07003937static int cgroup_tasks_open(struct inode *unused, struct file *file)
3938{
Ben Blum72a8cb32009-09-23 15:56:27 -07003939 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003940}
3941static int cgroup_procs_open(struct inode *unused, struct file *file)
3942{
Ben Blum72a8cb32009-09-23 15:56:27 -07003943 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003944}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003945
Tejun Heo182446d2013-08-08 20:11:24 -04003946static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3947 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003948{
Tejun Heo182446d2013-08-08 20:11:24 -04003949 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003950}
3951
Tejun Heo182446d2013-08-08 20:11:24 -04003952static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3953 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003954{
Tejun Heo182446d2013-08-08 20:11:24 -04003955 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003956 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003957 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003958 else
Tejun Heo182446d2013-08-08 20:11:24 -04003959 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003960 return 0;
3961}
3962
Paul Menagebbcb81d2007-10-18 23:39:32 -07003963/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003964 * When dput() is called asynchronously, if umount has been done and
3965 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3966 * there's a small window that vfs will see the root dentry with non-zero
3967 * refcnt and trigger BUG().
3968 *
3969 * That's why we hold a reference before dput() and drop it right after.
3970 */
3971static void cgroup_dput(struct cgroup *cgrp)
3972{
3973 struct super_block *sb = cgrp->root->sb;
3974
3975 atomic_inc(&sb->s_active);
3976 dput(cgrp->dentry);
3977 deactivate_super(sb);
3978}
3979
3980/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003981 * Unregister event and free resources.
3982 *
3983 * Gets called from workqueue.
3984 */
3985static void cgroup_event_remove(struct work_struct *work)
3986{
3987 struct cgroup_event *event = container_of(work, struct cgroup_event,
3988 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003989 struct cgroup_subsys_state *css = event->css;
3990 struct cgroup *cgrp = css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003991
Li Zefan810cbee2013-02-18 18:56:14 +08003992 remove_wait_queue(event->wqh, &event->wait);
3993
Tejun Heo81eeaf02013-08-08 20:11:26 -04003994 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003995
Li Zefan810cbee2013-02-18 18:56:14 +08003996 /* Notify userspace the event is going away. */
3997 eventfd_signal(event->eventfd, 1);
3998
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003999 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004000 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08004001 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004002}
4003
4004/*
4005 * Gets called on POLLHUP on eventfd when user closes it.
4006 *
4007 * Called with wqh->lock held and interrupts disabled.
4008 */
4009static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
4010 int sync, void *key)
4011{
4012 struct cgroup_event *event = container_of(wait,
4013 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04004014 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004015 unsigned long flags = (unsigned long)key;
4016
4017 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004018 /*
Li Zefan810cbee2013-02-18 18:56:14 +08004019 * If the event has been detached at cgroup removal, we
4020 * can simply return knowing the other side will cleanup
4021 * for us.
4022 *
4023 * We can't race against event freeing since the other
4024 * side will require wqh->lock via remove_wait_queue(),
4025 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004026 */
Li Zefan810cbee2013-02-18 18:56:14 +08004027 spin_lock(&cgrp->event_list_lock);
4028 if (!list_empty(&event->list)) {
4029 list_del_init(&event->list);
4030 /*
4031 * We are in atomic context, but cgroup_event_remove()
4032 * may sleep, so we have to call it in workqueue.
4033 */
4034 schedule_work(&event->remove);
4035 }
4036 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004037 }
4038
4039 return 0;
4040}
4041
4042static void cgroup_event_ptable_queue_proc(struct file *file,
4043 wait_queue_head_t *wqh, poll_table *pt)
4044{
4045 struct cgroup_event *event = container_of(pt,
4046 struct cgroup_event, pt);
4047
4048 event->wqh = wqh;
4049 add_wait_queue(wqh, &event->wait);
4050}
4051
4052/*
4053 * Parse input and register new cgroup event handler.
4054 *
4055 * Input must be in format '<event_fd> <control_fd> <args>'.
4056 * Interpretation of args is defined by control file implementation.
4057 */
Tejun Heo182446d2013-08-08 20:11:24 -04004058static int cgroup_write_event_control(struct cgroup_subsys_state *css,
4059 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004060{
Tejun Heo182446d2013-08-08 20:11:24 -04004061 struct cgroup *cgrp = css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004062 struct cgroup_event *event;
Li Zefanf1690072013-02-18 14:13:35 +08004063 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004064 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08004065 struct file *efile;
4066 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004067 char *endp;
4068 int ret;
4069
4070 efd = simple_strtoul(buffer, &endp, 10);
4071 if (*endp != ' ')
4072 return -EINVAL;
4073 buffer = endp + 1;
4074
4075 cfd = simple_strtoul(buffer, &endp, 10);
4076 if ((*endp != ' ') && (*endp != '\0'))
4077 return -EINVAL;
4078 buffer = endp + 1;
4079
4080 event = kzalloc(sizeof(*event), GFP_KERNEL);
4081 if (!event)
4082 return -ENOMEM;
Tejun Heo81eeaf02013-08-08 20:11:26 -04004083 event->css = css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004084 INIT_LIST_HEAD(&event->list);
4085 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4086 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4087 INIT_WORK(&event->remove, cgroup_event_remove);
4088
4089 efile = eventfd_fget(efd);
4090 if (IS_ERR(efile)) {
4091 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004092 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004093 }
4094
4095 event->eventfd = eventfd_ctx_fileget(efile);
4096 if (IS_ERR(event->eventfd)) {
4097 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004098 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004099 }
4100
4101 cfile = fget(cfd);
4102 if (!cfile) {
4103 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004104 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004105 }
4106
4107 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004108 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004109 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004110 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004111 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004112
4113 event->cft = __file_cft(cfile);
4114 if (IS_ERR(event->cft)) {
4115 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004116 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004117 }
4118
Li Zefanf1690072013-02-18 14:13:35 +08004119 /*
4120 * The file to be monitored must be in the same cgroup as
4121 * cgroup.event_control is.
4122 */
4123 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4124 if (cgrp_cfile != cgrp) {
4125 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004126 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004127 }
4128
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004129 if (!event->cft->register_event || !event->cft->unregister_event) {
4130 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004131 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004132 }
4133
Tejun Heo81eeaf02013-08-08 20:11:26 -04004134 ret = event->cft->register_event(css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004135 event->eventfd, buffer);
4136 if (ret)
Li Zefan876ede82013-08-01 09:51:47 +08004137 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004138
Li Zefan7ef70e42013-04-26 11:58:03 -07004139 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004140
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004141 /*
4142 * Events should be removed after rmdir of cgroup directory, but before
4143 * destroying subsystem state objects. Let's take reference to cgroup
4144 * directory dentry to do that.
4145 */
4146 dget(cgrp->dentry);
4147
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004148 spin_lock(&cgrp->event_list_lock);
4149 list_add(&event->list, &cgrp->event_list);
4150 spin_unlock(&cgrp->event_list_lock);
4151
4152 fput(cfile);
4153 fput(efile);
4154
4155 return 0;
4156
Li Zefan876ede82013-08-01 09:51:47 +08004157out_put_cfile:
4158 fput(cfile);
4159out_put_eventfd:
4160 eventfd_ctx_put(event->eventfd);
4161out_put_efile:
4162 fput(efile);
4163out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004164 kfree(event);
4165
4166 return ret;
4167}
4168
Tejun Heo182446d2013-08-08 20:11:24 -04004169static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4170 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004171{
Tejun Heo182446d2013-08-08 20:11:24 -04004172 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004173}
4174
Tejun Heo182446d2013-08-08 20:11:24 -04004175static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4176 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004177{
4178 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004179 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004180 else
Tejun Heo182446d2013-08-08 20:11:24 -04004181 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004182 return 0;
4183}
4184
Tejun Heod5c56ce2013-06-03 19:14:34 -07004185static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004186 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004187 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004188 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004189 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004190 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004191 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004192 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004193 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004194 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004195 .write_string = cgroup_write_event_control,
4196 .mode = S_IWUGO,
4197 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004198 {
4199 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004200 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004201 .read_u64 = cgroup_clone_children_read,
4202 .write_u64 = cgroup_clone_children_write,
4203 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004204 {
Tejun Heo873fe092013-04-14 20:15:26 -07004205 .name = "cgroup.sane_behavior",
4206 .flags = CFTYPE_ONLY_ON_ROOT,
4207 .read_seq_string = cgroup_sane_behavior_show,
4208 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004209
4210 /*
4211 * Historical crazy stuff. These don't have "cgroup." prefix and
4212 * don't exist if sane_behavior. If you're depending on these, be
4213 * prepared to be burned.
4214 */
4215 {
4216 .name = "tasks",
4217 .flags = CFTYPE_INSANE, /* use "procs" instead */
4218 .open = cgroup_tasks_open,
4219 .write_u64 = cgroup_tasks_write,
4220 .release = cgroup_pidlist_release,
4221 .mode = S_IRUGO | S_IWUSR,
4222 },
4223 {
4224 .name = "notify_on_release",
4225 .flags = CFTYPE_INSANE,
4226 .read_u64 = cgroup_read_notify_on_release,
4227 .write_u64 = cgroup_write_notify_on_release,
4228 },
Tejun Heo873fe092013-04-14 20:15:26 -07004229 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004230 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004231 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004232 .read_seq_string = cgroup_release_agent_show,
4233 .write_string = cgroup_release_agent_write,
4234 .max_write_len = PATH_MAX,
4235 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004236 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004237};
4238
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004239/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004240 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004241 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004242 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004243 *
4244 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004245 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004246static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004248 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004249 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250
Tejun Heo8e3f6542012-04-01 12:09:55 -07004251 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004252 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004253 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004254
4255 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004256 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004257
Tejun Heobee55092013-06-28 16:24:11 -07004258 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004259 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004260 if (ret < 0)
4261 goto err;
4262 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004264
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004265 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004266 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04004267 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004268 struct css_id *id = rcu_dereference_protected(css->id, true);
4269
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004270 /*
4271 * Update id->css pointer and make this css visible from
4272 * CSS ID functions. This pointer will be dereferened
4273 * from RCU-read-side without locks.
4274 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004275 if (id)
4276 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004277 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004278
4279 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004280err:
4281 cgroup_clear_dir(cgrp, subsys_mask);
4282 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283}
4284
Tejun Heo35ef10d2013-08-13 11:01:54 -04004285static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004286{
4287 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004288 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004289
Tejun Heo0ae78e02013-08-13 11:01:54 -04004290 if (css->parent)
4291 css_put(css->parent);
4292
Li Zefan1c8158e2013-06-18 18:41:10 +08004293 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004294}
4295
Tejun Heod3daf282013-06-13 19:39:16 -07004296static void css_release(struct percpu_ref *ref)
4297{
4298 struct cgroup_subsys_state *css =
4299 container_of(ref, struct cgroup_subsys_state, refcnt);
4300
Tejun Heo35ef10d2013-08-13 11:01:54 -04004301 /*
4302 * css holds an extra ref to @cgrp->dentry which is put on the last
4303 * css_put(). dput() requires process context, which css_put() may
4304 * be called without. @css->destroy_work will be used to invoke
4305 * dput() asynchronously from css_put().
4306 */
4307 INIT_WORK(&css->destroy_work, css_free_work_fn);
4308 schedule_work(&css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004309}
4310
Paul Menageddbcc7e2007-10-18 23:39:30 -07004311static void init_cgroup_css(struct cgroup_subsys_state *css,
4312 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004313 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004314{
Paul Menagebd89aab2007-10-18 23:40:44 -07004315 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004316 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004317 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004318 css->id = NULL;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004319
4320 if (cgrp->parent)
4321 css->parent = cgroup_css(cgrp->parent, ss->subsys_id);
4322 else
Tejun Heo38b53ab2012-11-19 08:13:36 -08004323 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004324
Tejun Heo40e93b32013-08-13 11:01:53 -04004325 BUG_ON(cgroup_css(cgrp, ss->subsys_id));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004326 rcu_assign_pointer(cgrp->subsys[ss->subsys_id], css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004327}
4328
Li Zefan2a4ac632013-07-31 16:16:40 +08004329/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heob1929db2012-11-19 08:13:38 -08004330static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004331{
Tejun Heo40e93b32013-08-13 11:01:53 -04004332 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heob1929db2012-11-19 08:13:38 -08004333 int ret = 0;
4334
Tejun Heoa31f2d32012-11-19 08:13:37 -08004335 lockdep_assert_held(&cgroup_mutex);
4336
Tejun Heo92fb9742012-11-19 08:13:38 -08004337 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004338 ret = ss->css_online(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004339 if (!ret)
Tejun Heoeb954192013-08-08 20:11:23 -04004340 css->flags |= CSS_ONLINE;
Tejun Heob1929db2012-11-19 08:13:38 -08004341 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004342}
4343
Li Zefan2a4ac632013-07-31 16:16:40 +08004344/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heoa31f2d32012-11-19 08:13:37 -08004345static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004346{
Tejun Heo40e93b32013-08-13 11:01:53 -04004347 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004348
4349 lockdep_assert_held(&cgroup_mutex);
4350
4351 if (!(css->flags & CSS_ONLINE))
4352 return;
4353
Li Zefand7eeac12013-03-12 15:35:59 -07004354 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004355 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004356
Tejun Heoeb954192013-08-08 20:11:23 -04004357 css->flags &= ~CSS_ONLINE;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004358}
4359
Paul Menageddbcc7e2007-10-18 23:39:30 -07004360/*
Li Zefana043e3b2008-02-23 15:24:09 -08004361 * cgroup_create - create a cgroup
4362 * @parent: cgroup that will be parent of the new cgroup
4363 * @dentry: dentry of the new cgroup
4364 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004365 *
Li Zefana043e3b2008-02-23 15:24:09 -08004366 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004367 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004369 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370{
Paul Menagebd89aab2007-10-18 23:40:44 -07004371 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004372 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373 struct cgroupfs_root *root = parent->root;
4374 int err = 0;
4375 struct cgroup_subsys *ss;
4376 struct super_block *sb = root->sb;
4377
Tejun Heo0a950f62012-11-19 09:02:12 -08004378 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004379 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4380 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004381 return -ENOMEM;
4382
Li Zefan65dff752013-03-01 15:01:56 +08004383 name = cgroup_alloc_name(dentry);
4384 if (!name)
4385 goto err_free_cgrp;
4386 rcu_assign_pointer(cgrp->name, name);
4387
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004388 /*
4389 * Temporarily set the pointer to NULL, so idr_find() won't return
4390 * a half-baked cgroup.
4391 */
4392 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004393 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004394 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004395
Tejun Heo976c06b2012-11-05 09:16:59 -08004396 /*
4397 * Only live parents can have children. Note that the liveliness
4398 * check isn't strictly necessary because cgroup_mkdir() and
4399 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4400 * anyway so that locking is contained inside cgroup proper and we
4401 * don't get nasty surprises if we ever grow another caller.
4402 */
4403 if (!cgroup_lock_live_group(parent)) {
4404 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004405 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004406 }
4407
Paul Menageddbcc7e2007-10-18 23:39:30 -07004408 /* Grab a reference on the superblock so the hierarchy doesn't
4409 * get deleted on unmount if there are child cgroups. This
4410 * can be done outside cgroup_mutex, since the sb can't
4411 * disappear while someone has an open control file on the
4412 * fs */
4413 atomic_inc(&sb->s_active);
4414
Paul Menagecc31edc2008-10-18 20:28:04 -07004415 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004416
Li Zefanfe1c06c2013-01-24 14:30:22 +08004417 dentry->d_fsdata = cgrp;
4418 cgrp->dentry = dentry;
4419
Paul Menagebd89aab2007-10-18 23:40:44 -07004420 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004421 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004422 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004423
Li Zefanb6abdb02008-03-04 14:28:19 -08004424 if (notify_on_release(parent))
4425 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4426
Tejun Heo2260e7f2012-11-19 08:13:38 -08004427 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4428 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004429
Tejun Heo5549c492013-06-24 15:21:48 -07004430 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004431 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004432
Tejun Heo40e93b32013-08-13 11:01:53 -04004433 css = ss->css_alloc(cgroup_css(parent, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004434 if (IS_ERR(css)) {
4435 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004436 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004437 }
Tejun Heod3daf282013-06-13 19:39:16 -07004438
4439 err = percpu_ref_init(&css->refcnt, css_release);
Li Zefanda0a12c2013-07-31 16:16:28 +08004440 if (err) {
Tejun Heoeb954192013-08-08 20:11:23 -04004441 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004442 goto err_free_all;
Li Zefanda0a12c2013-07-31 16:16:28 +08004443 }
Tejun Heod3daf282013-06-13 19:39:16 -07004444
Paul Menagebd89aab2007-10-18 23:40:44 -07004445 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004446
Li Zefan4528fd02010-02-02 13:44:10 -08004447 if (ss->use_id) {
4448 err = alloc_css_id(ss, parent, cgrp);
4449 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004450 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004451 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452 }
4453
Tejun Heo4e139af2012-11-19 08:13:36 -08004454 /*
4455 * Create directory. cgroup_create_file() returns with the new
4456 * directory locked on success so that it can be populated without
4457 * dropping cgroup_mutex.
4458 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004459 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004460 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004461 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004462 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004463
Tejun Heo00356bd2013-06-18 11:14:22 -07004464 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004465
Tejun Heo4e139af2012-11-19 08:13:36 -08004466 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004467 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4468 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004469
Tejun Heo0ae78e02013-08-13 11:01:54 -04004470 /* each css holds a ref to the cgroup's dentry and the parent css */
4471 for_each_root_subsys(root, ss) {
4472 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
4473
Tejun Heoed9577932012-11-05 09:16:58 -08004474 dget(dentry);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004475 percpu_ref_get(&css->parent->refcnt);
4476 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004477
Li Zefan415cf072013-04-08 14:35:02 +08004478 /* hold a ref to the parent's dentry */
4479 dget(parent->dentry);
4480
Tejun Heob1929db2012-11-19 08:13:38 -08004481 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004482 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004483 err = online_css(ss, cgrp);
4484 if (err)
4485 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004486
4487 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4488 parent->parent) {
4489 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",
4490 current->comm, current->pid, ss->name);
4491 if (!strcmp(ss->name, "memory"))
4492 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4493 ss->warned_broken_hierarchy = true;
4494 }
Tejun Heoa8638032012-11-09 09:12:29 -08004495 }
4496
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004497 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4498
Tejun Heo2bb566c2013-08-08 20:11:23 -04004499 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004500 if (err)
4501 goto err_destroy;
4502
4503 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004504 if (err)
4505 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004506
4507 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004508 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004509
4510 return 0;
4511
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004512err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004513 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04004514 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heod3daf282013-06-13 19:39:16 -07004515
4516 if (css) {
4517 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004518 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004519 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004520 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004521 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004522 /* Release the reference count that we took on the superblock */
4523 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004524err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004525 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004526err_free_name:
4527 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004528err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004529 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004530 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004531
4532err_destroy:
4533 cgroup_destroy_locked(cgrp);
4534 mutex_unlock(&cgroup_mutex);
4535 mutex_unlock(&dentry->d_inode->i_mutex);
4536 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004537}
4538
Al Viro18bb1db2011-07-26 01:41:39 -04004539static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004540{
4541 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4542
4543 /* the vfs holds inode->i_mutex already */
4544 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4545}
4546
Tejun Heod3daf282013-06-13 19:39:16 -07004547static void cgroup_css_killed(struct cgroup *cgrp)
4548{
4549 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4550 return;
4551
4552 /* percpu ref's of all css's are killed, kick off the next step */
4553 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4554 schedule_work(&cgrp->destroy_work);
4555}
4556
4557static void css_ref_killed_fn(struct percpu_ref *ref)
4558{
4559 struct cgroup_subsys_state *css =
4560 container_of(ref, struct cgroup_subsys_state, refcnt);
4561
4562 cgroup_css_killed(css->cgroup);
4563}
4564
4565/**
4566 * cgroup_destroy_locked - the first stage of cgroup destruction
4567 * @cgrp: cgroup to be destroyed
4568 *
4569 * css's make use of percpu refcnts whose killing latency shouldn't be
4570 * exposed to userland and are RCU protected. Also, cgroup core needs to
4571 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4572 * invoked. To satisfy all the requirements, destruction is implemented in
4573 * the following two steps.
4574 *
4575 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4576 * userland visible parts and start killing the percpu refcnts of
4577 * css's. Set up so that the next stage will be kicked off once all
4578 * the percpu refcnts are confirmed to be killed.
4579 *
4580 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4581 * rest of destruction. Once all cgroup references are gone, the
4582 * cgroup is RCU-freed.
4583 *
4584 * This function implements s1. After this step, @cgrp is gone as far as
4585 * the userland is concerned and a new cgroup with the same name may be
4586 * created. As cgroup doesn't care about the names internally, this
4587 * doesn't cause any problem.
4588 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004589static int cgroup_destroy_locked(struct cgroup *cgrp)
4590 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004591{
Tejun Heo42809dd2012-11-19 08:13:37 -08004592 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004593 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004594 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004595 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004596
Tejun Heo42809dd2012-11-19 08:13:37 -08004597 lockdep_assert_held(&d->d_inode->i_mutex);
4598 lockdep_assert_held(&cgroup_mutex);
4599
Tejun Heoddd69142013-06-12 21:04:54 -07004600 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004601 * css_set_lock synchronizes access to ->cset_links and prevents
4602 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004603 */
4604 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004605 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004606 read_unlock(&css_set_lock);
4607 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004608 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004609
Tejun Heo1a90dd52012-11-05 09:16:59 -08004610 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004611 * Block new css_tryget() by killing css refcnts. cgroup core
4612 * guarantees that, by the time ->css_offline() is invoked, no new
4613 * css reference will be given out via css_tryget(). We can't
4614 * simply call percpu_ref_kill() and proceed to offlining css's
4615 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4616 * as killed on all CPUs on return.
4617 *
4618 * Use percpu_ref_kill_and_confirm() to get notifications as each
4619 * css is confirmed to be seen as killed on all CPUs. The
4620 * notification callback keeps track of the number of css's to be
4621 * killed and schedules cgroup_offline_fn() to perform the rest of
4622 * destruction once the percpu refs of all css's are confirmed to
4623 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004624 */
Tejun Heod3daf282013-06-13 19:39:16 -07004625 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004626 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04004627 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoed9577932012-11-05 09:16:58 -08004628
Tejun Heod3daf282013-06-13 19:39:16 -07004629 /*
4630 * Killing would put the base ref, but we need to keep it
4631 * alive until after ->css_offline.
4632 */
4633 percpu_ref_get(&css->refcnt);
4634
4635 atomic_inc(&cgrp->css_kill_cnt);
4636 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004637 }
Tejun Heod3daf282013-06-13 19:39:16 -07004638 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004639
4640 /*
4641 * Mark @cgrp dead. This prevents further task migration and child
4642 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004643 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004644 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004645 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004646 */
Tejun Heo54766d42013-06-12 21:04:53 -07004647 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004648
Tejun Heo455050d2013-06-13 19:27:41 -07004649 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4650 raw_spin_lock(&release_list_lock);
4651 if (!list_empty(&cgrp->release_list))
4652 list_del_init(&cgrp->release_list);
4653 raw_spin_unlock(&release_list_lock);
4654
4655 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004656 * Clear and remove @cgrp directory. The removal puts the base ref
4657 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004658 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004659 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004660 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004661 dget(d);
4662 cgroup_d_remove_dir(d);
4663
4664 /*
4665 * Unregister events and notify userspace.
4666 * Notify userspace about cgroup removing only after rmdir of cgroup
4667 * directory to avoid race between userspace and kernelspace.
4668 */
4669 spin_lock(&cgrp->event_list_lock);
4670 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4671 list_del_init(&event->list);
4672 schedule_work(&event->remove);
4673 }
4674 spin_unlock(&cgrp->event_list_lock);
4675
Tejun Heoea15f8c2013-06-13 19:27:42 -07004676 return 0;
4677};
4678
Tejun Heod3daf282013-06-13 19:39:16 -07004679/**
4680 * cgroup_offline_fn - the second step of cgroup destruction
4681 * @work: cgroup->destroy_free_work
4682 *
4683 * This function is invoked from a work item for a cgroup which is being
4684 * destroyed after the percpu refcnts of all css's are guaranteed to be
4685 * seen as killed on all CPUs, and performs the rest of destruction. This
4686 * is the second step of destruction described in the comment above
4687 * cgroup_destroy_locked().
4688 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004689static void cgroup_offline_fn(struct work_struct *work)
4690{
4691 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4692 struct cgroup *parent = cgrp->parent;
4693 struct dentry *d = cgrp->dentry;
4694 struct cgroup_subsys *ss;
4695
4696 mutex_lock(&cgroup_mutex);
4697
Tejun Heod3daf282013-06-13 19:39:16 -07004698 /*
4699 * css_tryget() is guaranteed to fail now. Tell subsystems to
4700 * initate destruction.
4701 */
Tejun Heo5549c492013-06-24 15:21:48 -07004702 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004703 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004704
4705 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004706 * Put the css refs from cgroup_destroy_locked(). Each css holds
4707 * an extra reference to the cgroup's dentry and cgroup removal
4708 * proceeds regardless of css refs. On the last put of each css,
4709 * whenever that may be, the extra dentry ref is put so that dentry
4710 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004711 */
Tejun Heo5549c492013-06-24 15:21:48 -07004712 for_each_root_subsys(cgrp->root, ss)
Tejun Heo40e93b32013-08-13 11:01:53 -04004713 css_put(cgroup_css(cgrp, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004714
Paul Menage999cd8a2009-01-07 18:08:36 -08004715 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004716 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004717
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004718 /*
4719 * We should remove the cgroup object from idr before its grace
4720 * period starts, so we won't be looking up a cgroup while the
4721 * cgroup is being freed.
4722 */
4723 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4724 cgrp->id = -1;
4725
Paul Menageddbcc7e2007-10-18 23:39:30 -07004726 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004727
Paul Menagebd89aab2007-10-18 23:40:44 -07004728 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004729 check_for_release(parent);
4730
Tejun Heoea15f8c2013-06-13 19:27:42 -07004731 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004732}
4733
Tejun Heo42809dd2012-11-19 08:13:37 -08004734static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4735{
4736 int ret;
4737
4738 mutex_lock(&cgroup_mutex);
4739 ret = cgroup_destroy_locked(dentry->d_fsdata);
4740 mutex_unlock(&cgroup_mutex);
4741
4742 return ret;
4743}
4744
Tejun Heo8e3f6542012-04-01 12:09:55 -07004745static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4746{
4747 INIT_LIST_HEAD(&ss->cftsets);
4748
4749 /*
4750 * base_cftset is embedded in subsys itself, no need to worry about
4751 * deregistration.
4752 */
4753 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004754 struct cftype *cft;
4755
4756 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4757 cft->ss = ss;
4758
Tejun Heo8e3f6542012-04-01 12:09:55 -07004759 ss->base_cftset.cfts = ss->base_cftypes;
4760 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4761 }
4762}
4763
Li Zefan06a11922008-04-29 01:00:07 -07004764static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004765{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004766 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004767
4768 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004769
Tejun Heo648bb562012-11-19 08:13:36 -08004770 mutex_lock(&cgroup_mutex);
4771
Tejun Heo8e3f6542012-04-01 12:09:55 -07004772 /* init base cftset */
4773 cgroup_init_cftsets(ss);
4774
Paul Menageddbcc7e2007-10-18 23:39:30 -07004775 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004776 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4777 ss->root = &cgroup_dummy_root;
Tejun Heo40e93b32013-08-13 11:01:53 -04004778 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004779 /* We don't handle early failures gracefully */
4780 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004781 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004782
Li Zefane8d55fd2008-04-29 01:00:13 -07004783 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004784 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004785 * newly registered, all tasks and hence the
4786 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004787 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004788
4789 need_forkexit_callback |= ss->fork || ss->exit;
4790
Li Zefane8d55fd2008-04-29 01:00:13 -07004791 /* At system boot, before all subsystems have been
4792 * registered, no tasks have been forked, so we don't
4793 * need to invoke fork callbacks here. */
4794 BUG_ON(!list_empty(&init_task.tasks));
4795
Tejun Heo9871bf92013-06-24 15:21:47 -07004796 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004797
Tejun Heo648bb562012-11-19 08:13:36 -08004798 mutex_unlock(&cgroup_mutex);
4799
Ben Blume6a11052010-03-10 15:22:09 -08004800 /* this function shouldn't be used with modular subsystems, since they
4801 * need to register a subsys_id, among other things */
4802 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004803}
4804
4805/**
Ben Blume6a11052010-03-10 15:22:09 -08004806 * cgroup_load_subsys: load and register a modular subsystem at runtime
4807 * @ss: the subsystem to load
4808 *
4809 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004810 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004811 * up for use. If the subsystem is built-in anyway, work is delegated to the
4812 * simpler cgroup_init_subsys.
4813 */
4814int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4815{
Ben Blume6a11052010-03-10 15:22:09 -08004816 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004817 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004818 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004819 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004820 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004821
4822 /* check name and function validity */
4823 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004824 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004825 return -EINVAL;
4826
4827 /*
4828 * we don't support callbacks in modular subsystems. this check is
4829 * before the ss->module check for consistency; a subsystem that could
4830 * be a module should still have no callbacks even if the user isn't
4831 * compiling it as one.
4832 */
4833 if (ss->fork || ss->exit)
4834 return -EINVAL;
4835
4836 /*
4837 * an optionally modular subsystem is built-in: we want to do nothing,
4838 * since cgroup_init_subsys will have already taken care of it.
4839 */
4840 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004841 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004842 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004843 return 0;
4844 }
4845
Tejun Heo8e3f6542012-04-01 12:09:55 -07004846 /* init base cftset */
4847 cgroup_init_cftsets(ss);
4848
Ben Blume6a11052010-03-10 15:22:09 -08004849 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004850 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004851
4852 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004853 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004854 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004855 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004856 */
Tejun Heo40e93b32013-08-13 11:01:53 -04004857 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Ben Blume6a11052010-03-10 15:22:09 -08004858 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004859 /* failure case - need to deassign the cgroup_subsys[] slot. */
4860 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004861 mutex_unlock(&cgroup_mutex);
4862 return PTR_ERR(css);
4863 }
4864
Tejun Heo9871bf92013-06-24 15:21:47 -07004865 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4866 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004867
4868 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004869 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004870 /* init_idr must be after init_cgroup_css because it sets css->id. */
4871 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004872 ret = cgroup_init_idr(ss, css);
4873 if (ret)
4874 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004875 }
4876
4877 /*
4878 * Now we need to entangle the css into the existing css_sets. unlike
4879 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4880 * will need a new pointer to it; done by iterating the css_set_table.
4881 * furthermore, modifying the existing css_sets will corrupt the hash
4882 * table state, so each changed css_set will need its hash recomputed.
4883 * this is all done under the css_set_lock.
4884 */
4885 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004886 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004887 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004888 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004889 continue;
4890 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004891 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004892 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004893 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004894 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004895 key = css_set_hash(cset->subsys);
4896 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004897 }
4898 write_unlock(&css_set_lock);
4899
Tejun Heo9871bf92013-06-24 15:21:47 -07004900 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004901 if (ret)
4902 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004903
Ben Blume6a11052010-03-10 15:22:09 -08004904 /* success! */
4905 mutex_unlock(&cgroup_mutex);
4906 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004907
4908err_unload:
4909 mutex_unlock(&cgroup_mutex);
4910 /* @ss can't be mounted here as try_module_get() would fail */
4911 cgroup_unload_subsys(ss);
4912 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004913}
4914EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4915
4916/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004917 * cgroup_unload_subsys: unload a modular subsystem
4918 * @ss: the subsystem to unload
4919 *
4920 * This function should be called in a modular subsystem's exitcall. When this
4921 * function is invoked, the refcount on the subsystem's module will be 0, so
4922 * the subsystem will not be attached to any hierarchy.
4923 */
4924void cgroup_unload_subsys(struct cgroup_subsys *ss)
4925{
Tejun Heo69d02062013-06-12 21:04:50 -07004926 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004927
4928 BUG_ON(ss->module == NULL);
4929
4930 /*
4931 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004932 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004933 * doesn't start being used while we're killing it off.
4934 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004935 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004936
4937 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004938
Tejun Heo9871bf92013-06-24 15:21:47 -07004939 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004940
Tejun Heoc897ff62013-02-27 17:03:49 -08004941 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004942 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004943
Ben Blumcf5d5942010-03-10 15:22:09 -08004944 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004945 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004946
Tejun Heo9871bf92013-06-24 15:21:47 -07004947 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004948 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004949
4950 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004951 * disentangle the css from all css_sets attached to the dummy
4952 * top. as in loading, we need to pay our respects to the hashtable
4953 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004954 */
4955 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004956 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004957 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004958 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004959
Tejun Heo5abb8852013-06-12 21:04:49 -07004960 hash_del(&cset->hlist);
4961 cset->subsys[ss->subsys_id] = NULL;
4962 key = css_set_hash(cset->subsys);
4963 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004964 }
4965 write_unlock(&css_set_lock);
4966
4967 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004968 * remove subsystem's css from the cgroup_dummy_top and free it -
4969 * need to free before marking as null because ss->css_free needs
4970 * the cgrp->subsys pointer to find their state. note that this
4971 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004972 */
Tejun Heo40e93b32013-08-13 11:01:53 -04004973 ss->css_free(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004974 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004975
4976 mutex_unlock(&cgroup_mutex);
4977}
4978EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4979
4980/**
Li Zefana043e3b2008-02-23 15:24:09 -08004981 * cgroup_init_early - cgroup initialization at system boot
4982 *
4983 * Initialize cgroups at system boot, and initialize any
4984 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004985 */
4986int __init cgroup_init_early(void)
4987{
Tejun Heo30159ec2013-06-25 11:53:37 -07004988 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004989 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004990
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004991 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004992 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004993 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004994 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004995 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004996 init_cgroup_root(&cgroup_dummy_root);
4997 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004998 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004999
Tejun Heo69d02062013-06-12 21:04:50 -07005000 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07005001 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
5002 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07005003 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005004
Tejun Heo30159ec2013-06-25 11:53:37 -07005005 /* at bootup time, we don't worry about modular subsystems */
5006 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005007 BUG_ON(!ss->name);
5008 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08005009 BUG_ON(!ss->css_alloc);
5010 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005011 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08005012 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07005013 ss->name, ss->subsys_id);
5014 BUG();
5015 }
5016
5017 if (ss->early_init)
5018 cgroup_init_subsys(ss);
5019 }
5020 return 0;
5021}
5022
5023/**
Li Zefana043e3b2008-02-23 15:24:09 -08005024 * cgroup_init - cgroup initialization
5025 *
5026 * Register cgroup filesystem and /proc file, and initialize
5027 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005028 */
5029int __init cgroup_init(void)
5030{
Tejun Heo30159ec2013-06-25 11:53:37 -07005031 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005032 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005033 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005034
5035 err = bdi_init(&cgroup_backing_dev_info);
5036 if (err)
5037 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005038
Tejun Heo30159ec2013-06-25 11:53:37 -07005039 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005040 if (!ss->early_init)
5041 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005042 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08005043 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005044 }
5045
Tejun Heofa3ca072013-04-14 11:36:56 -07005046 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005047 mutex_lock(&cgroup_mutex);
5048 mutex_lock(&cgroup_root_mutex);
5049
Tejun Heo82fe9b02013-06-25 11:53:37 -07005050 /* Add init_css_set to the hash table */
5051 key = css_set_hash(init_css_set.subsys);
5052 hash_add(css_set_table, &init_css_set.hlist, key);
5053
Tejun Heofc76df72013-06-25 11:53:37 -07005054 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005055
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005056 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5057 0, 1, GFP_KERNEL);
5058 BUG_ON(err < 0);
5059
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005060 mutex_unlock(&cgroup_root_mutex);
5061 mutex_unlock(&cgroup_mutex);
5062
Greg KH676db4a2010-08-05 13:53:35 -07005063 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5064 if (!cgroup_kobj) {
5065 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005066 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005067 }
5068
5069 err = register_filesystem(&cgroup_fs_type);
5070 if (err < 0) {
5071 kobject_put(cgroup_kobj);
5072 goto out;
5073 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005074
Li Zefan46ae2202008-04-29 01:00:08 -07005075 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005076
Paul Menageddbcc7e2007-10-18 23:39:30 -07005077out:
Paul Menagea4243162007-10-18 23:39:35 -07005078 if (err)
5079 bdi_destroy(&cgroup_backing_dev_info);
5080
Paul Menageddbcc7e2007-10-18 23:39:30 -07005081 return err;
5082}
Paul Menageb4f48b62007-10-18 23:39:33 -07005083
Paul Menagea4243162007-10-18 23:39:35 -07005084/*
5085 * proc_cgroup_show()
5086 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5087 * - Used for /proc/<pid>/cgroup.
5088 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5089 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005090 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005091 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5092 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5093 * cgroup to top_cgroup.
5094 */
5095
5096/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005097int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005098{
5099 struct pid *pid;
5100 struct task_struct *tsk;
5101 char *buf;
5102 int retval;
5103 struct cgroupfs_root *root;
5104
5105 retval = -ENOMEM;
5106 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5107 if (!buf)
5108 goto out;
5109
5110 retval = -ESRCH;
5111 pid = m->private;
5112 tsk = get_pid_task(pid, PIDTYPE_PID);
5113 if (!tsk)
5114 goto out_free;
5115
5116 retval = 0;
5117
5118 mutex_lock(&cgroup_mutex);
5119
Li Zefane5f6a862009-01-07 18:07:41 -08005120 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005121 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005122 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005123 int count = 0;
5124
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005125 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005126 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005127 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005128 if (strlen(root->name))
5129 seq_printf(m, "%sname=%s", count ? "," : "",
5130 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005131 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005132 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005133 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005134 if (retval < 0)
5135 goto out_unlock;
5136 seq_puts(m, buf);
5137 seq_putc(m, '\n');
5138 }
5139
5140out_unlock:
5141 mutex_unlock(&cgroup_mutex);
5142 put_task_struct(tsk);
5143out_free:
5144 kfree(buf);
5145out:
5146 return retval;
5147}
5148
Paul Menagea4243162007-10-18 23:39:35 -07005149/* Display information about each subsystem and each hierarchy */
5150static int proc_cgroupstats_show(struct seq_file *m, void *v)
5151{
Tejun Heo30159ec2013-06-25 11:53:37 -07005152 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005153 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005154
Paul Menage8bab8dd2008-04-04 14:29:57 -07005155 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005156 /*
5157 * ideally we don't want subsystems moving around while we do this.
5158 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5159 * subsys/hierarchy state.
5160 */
Paul Menagea4243162007-10-18 23:39:35 -07005161 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005162
5163 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005164 seq_printf(m, "%s\t%d\t%d\t%d\n",
5165 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005166 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005167
Paul Menagea4243162007-10-18 23:39:35 -07005168 mutex_unlock(&cgroup_mutex);
5169 return 0;
5170}
5171
5172static int cgroupstats_open(struct inode *inode, struct file *file)
5173{
Al Viro9dce07f2008-03-29 03:07:28 +00005174 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005175}
5176
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005177static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005178 .open = cgroupstats_open,
5179 .read = seq_read,
5180 .llseek = seq_lseek,
5181 .release = single_release,
5182};
5183
Paul Menageb4f48b62007-10-18 23:39:33 -07005184/**
5185 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005186 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005187 *
5188 * Description: A task inherits its parent's cgroup at fork().
5189 *
5190 * A pointer to the shared css_set was automatically copied in
5191 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005192 * it was not made under the protection of RCU or cgroup_mutex, so
5193 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5194 * have already changed current->cgroups, allowing the previously
5195 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005196 *
5197 * At the point that cgroup_fork() is called, 'current' is the parent
5198 * task, and the passed argument 'child' points to the child task.
5199 */
5200void cgroup_fork(struct task_struct *child)
5201{
Tejun Heo9bb71302012-10-18 17:52:07 -07005202 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005203 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005204 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005205 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005206 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005207}
5208
5209/**
Li Zefana043e3b2008-02-23 15:24:09 -08005210 * cgroup_post_fork - called on a new task after adding it to the task list
5211 * @child: the task in question
5212 *
Tejun Heo5edee612012-10-16 15:03:14 -07005213 * Adds the task to the list running through its css_set if necessary and
5214 * call the subsystem fork() callbacks. Has to be after the task is
5215 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005216 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005217 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005218 */
Paul Menage817929e2007-10-18 23:39:36 -07005219void cgroup_post_fork(struct task_struct *child)
5220{
Tejun Heo30159ec2013-06-25 11:53:37 -07005221 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005222 int i;
5223
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005224 /*
5225 * use_task_css_set_links is set to 1 before we walk the tasklist
5226 * under the tasklist_lock and we read it here after we added the child
5227 * to the tasklist under the tasklist_lock as well. If the child wasn't
5228 * yet in the tasklist when we walked through it from
5229 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5230 * should be visible now due to the paired locking and barriers implied
5231 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5232 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5233 * lock on fork.
5234 */
Paul Menage817929e2007-10-18 23:39:36 -07005235 if (use_task_css_set_links) {
5236 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005237 task_lock(child);
5238 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005239 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005240 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005241 write_unlock(&css_set_lock);
5242 }
Tejun Heo5edee612012-10-16 15:03:14 -07005243
5244 /*
5245 * Call ss->fork(). This must happen after @child is linked on
5246 * css_set; otherwise, @child might change state between ->fork()
5247 * and addition to css_set.
5248 */
5249 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005250 /*
5251 * fork/exit callbacks are supported only for builtin
5252 * subsystems, and the builtin section of the subsys
5253 * array is immutable, so we don't need to lock the
5254 * subsys array here. On the other hand, modular section
5255 * of the array can be freed at module unload, so we
5256 * can't touch that.
5257 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005258 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005259 if (ss->fork)
5260 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005261 }
Paul Menage817929e2007-10-18 23:39:36 -07005262}
Tejun Heo5edee612012-10-16 15:03:14 -07005263
Paul Menage817929e2007-10-18 23:39:36 -07005264/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005265 * cgroup_exit - detach cgroup from exiting task
5266 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005267 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005268 *
5269 * Description: Detach cgroup from @tsk and release it.
5270 *
5271 * Note that cgroups marked notify_on_release force every task in
5272 * them to take the global cgroup_mutex mutex when exiting.
5273 * This could impact scaling on very large systems. Be reluctant to
5274 * use notify_on_release cgroups where very high task exit scaling
5275 * is required on large systems.
5276 *
5277 * the_top_cgroup_hack:
5278 *
5279 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5280 *
5281 * We call cgroup_exit() while the task is still competent to
5282 * handle notify_on_release(), then leave the task attached to the
5283 * root cgroup in each hierarchy for the remainder of its exit.
5284 *
5285 * To do this properly, we would increment the reference count on
5286 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5287 * code we would add a second cgroup function call, to drop that
5288 * reference. This would just create an unnecessary hot spot on
5289 * the top_cgroup reference count, to no avail.
5290 *
5291 * Normally, holding a reference to a cgroup without bumping its
5292 * count is unsafe. The cgroup could go away, or someone could
5293 * attach us to a different cgroup, decrementing the count on
5294 * the first cgroup that we never incremented. But in this case,
5295 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005296 * which wards off any cgroup_attach_task() attempts, or task is a failed
5297 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005298 */
5299void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5300{
Tejun Heo30159ec2013-06-25 11:53:37 -07005301 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005302 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005303 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005304
5305 /*
5306 * Unlink from the css_set task list if necessary.
5307 * Optimistically check cg_list before taking
5308 * css_set_lock
5309 */
5310 if (!list_empty(&tsk->cg_list)) {
5311 write_lock(&css_set_lock);
5312 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005313 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005314 write_unlock(&css_set_lock);
5315 }
5316
Paul Menageb4f48b62007-10-18 23:39:33 -07005317 /* Reassign the task to the init_css_set. */
5318 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005319 cset = task_css_set(tsk);
5320 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005321
5322 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005323 /*
5324 * fork/exit callbacks are supported only for builtin
5325 * subsystems, see cgroup_post_fork() for details.
5326 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005327 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005328 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005329 struct cgroup_subsys_state *old_css = cset->subsys[i];
5330 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005331
Tejun Heoeb954192013-08-08 20:11:23 -04005332 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005333 }
5334 }
5335 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005336 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005337
Tejun Heo5abb8852013-06-12 21:04:49 -07005338 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005339}
Paul Menage697f4162007-10-18 23:39:34 -07005340
Paul Menagebd89aab2007-10-18 23:40:44 -07005341static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005342{
Li Zefanf50daa72013-03-01 15:06:07 +08005343 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005344 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005345 /*
5346 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005347 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005348 * it now
5349 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005350 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005351
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005352 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005353 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005354 list_empty(&cgrp->release_list)) {
5355 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005356 need_schedule_work = 1;
5357 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005358 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005359 if (need_schedule_work)
5360 schedule_work(&release_agent_work);
5361 }
5362}
5363
Paul Menage81a6a5c2007-10-18 23:39:38 -07005364/*
5365 * Notify userspace when a cgroup is released, by running the
5366 * configured release agent with the name of the cgroup (path
5367 * relative to the root of cgroup file system) as the argument.
5368 *
5369 * Most likely, this user command will try to rmdir this cgroup.
5370 *
5371 * This races with the possibility that some other task will be
5372 * attached to this cgroup before it is removed, or that some other
5373 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5374 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5375 * unused, and this cgroup will be reprieved from its death sentence,
5376 * to continue to serve a useful existence. Next time it's released,
5377 * we will get notified again, if it still has 'notify_on_release' set.
5378 *
5379 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5380 * means only wait until the task is successfully execve()'d. The
5381 * separate release agent task is forked by call_usermodehelper(),
5382 * then control in this thread returns here, without waiting for the
5383 * release agent task. We don't bother to wait because the caller of
5384 * this routine has no use for the exit status of the release agent
5385 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005386 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005387static void cgroup_release_agent(struct work_struct *work)
5388{
5389 BUG_ON(work != &release_agent_work);
5390 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005391 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005392 while (!list_empty(&release_list)) {
5393 char *argv[3], *envp[3];
5394 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005395 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005396 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005397 struct cgroup,
5398 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005399 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005400 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005401 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005402 if (!pathbuf)
5403 goto continue_free;
5404 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5405 goto continue_free;
5406 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5407 if (!agentbuf)
5408 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005409
5410 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005411 argv[i++] = agentbuf;
5412 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005413 argv[i] = NULL;
5414
5415 i = 0;
5416 /* minimal command environment */
5417 envp[i++] = "HOME=/";
5418 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5419 envp[i] = NULL;
5420
5421 /* Drop the lock while we invoke the usermode helper,
5422 * since the exec could involve hitting disk and hence
5423 * be a slow process */
5424 mutex_unlock(&cgroup_mutex);
5425 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005426 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005427 continue_free:
5428 kfree(pathbuf);
5429 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005430 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005431 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005432 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005433 mutex_unlock(&cgroup_mutex);
5434}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005435
5436static int __init cgroup_disable(char *str)
5437{
Tejun Heo30159ec2013-06-25 11:53:37 -07005438 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005439 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005440 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005441
5442 while ((token = strsep(&str, ",")) != NULL) {
5443 if (!*token)
5444 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005445
Tejun Heo30159ec2013-06-25 11:53:37 -07005446 /*
5447 * cgroup_disable, being at boot time, can't know about
5448 * module subsystems, so we don't worry about them.
5449 */
5450 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005451 if (!strcmp(token, ss->name)) {
5452 ss->disabled = 1;
5453 printk(KERN_INFO "Disabling %s control group"
5454 " subsystem\n", ss->name);
5455 break;
5456 }
5457 }
5458 }
5459 return 1;
5460}
5461__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005462
5463/*
5464 * Functons for CSS ID.
5465 */
5466
Tejun Heo54766d42013-06-12 21:04:53 -07005467/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005468unsigned short css_id(struct cgroup_subsys_state *css)
5469{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005470 struct css_id *cssid;
5471
5472 /*
5473 * This css_id() can return correct value when somone has refcnt
5474 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5475 * it's unchanged until freed.
5476 */
Tejun Heod3daf282013-06-13 19:39:16 -07005477 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005478
5479 if (cssid)
5480 return cssid->id;
5481 return 0;
5482}
Ben Blum67523c42010-03-10 15:22:11 -08005483EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005484
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005485/**
5486 * css_is_ancestor - test "root" css is an ancestor of "child"
5487 * @child: the css to be tested.
5488 * @root: the css supporsed to be an ancestor of the child.
5489 *
5490 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005491 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005492 * But, considering usual usage, the csses should be valid objects after test.
5493 * Assuming that the caller will do some action to the child if this returns
5494 * returns true, the caller must take "child";s reference count.
5495 * If "child" is valid object and this returns true, "root" is valid, too.
5496 */
5497
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005498bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005499 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005500{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005501 struct css_id *child_id;
5502 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005503
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005504 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005505 if (!child_id)
5506 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005507 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005508 if (!root_id)
5509 return false;
5510 if (child_id->depth < root_id->depth)
5511 return false;
5512 if (child_id->stack[root_id->depth] != root_id->id)
5513 return false;
5514 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005515}
5516
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005517void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5518{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005519 struct css_id *id = rcu_dereference_protected(css->id, true);
5520
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005521 /* When this is called before css_id initialization, id can be NULL */
5522 if (!id)
5523 return;
5524
5525 BUG_ON(!ss->use_id);
5526
5527 rcu_assign_pointer(id->css, NULL);
5528 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005529 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005530 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005531 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005532 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005533}
Ben Blum67523c42010-03-10 15:22:11 -08005534EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005535
5536/*
5537 * This is called by init or create(). Then, calls to this function are
5538 * always serialized (By cgroup_mutex() at create()).
5539 */
5540
5541static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5542{
5543 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005544 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005545
5546 BUG_ON(!ss->use_id);
5547
5548 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5549 newid = kzalloc(size, GFP_KERNEL);
5550 if (!newid)
5551 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005552
5553 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005554 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005555 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005556 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005557 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005558 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005559
5560 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005561 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005562 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005563
Tejun Heod228d9e2013-02-27 17:04:54 -08005564 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005565 newid->depth = depth;
5566 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005567err_out:
5568 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005569 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005570
5571}
5572
Ben Blume6a11052010-03-10 15:22:09 -08005573static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5574 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005575{
5576 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005577
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005578 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005579 idr_init(&ss->idr);
5580
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005581 newid = get_new_cssid(ss, 0);
5582 if (IS_ERR(newid))
5583 return PTR_ERR(newid);
5584
5585 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005586 RCU_INIT_POINTER(newid->css, rootcss);
5587 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005588 return 0;
5589}
5590
5591static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5592 struct cgroup *child)
5593{
5594 int subsys_id, i, depth = 0;
5595 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005596 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005597
5598 subsys_id = ss->subsys_id;
Tejun Heo40e93b32013-08-13 11:01:53 -04005599 parent_css = cgroup_css(parent, subsys_id);
5600 child_css = cgroup_css(child, subsys_id);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005601 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005602 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005603
5604 child_id = get_new_cssid(ss, depth);
5605 if (IS_ERR(child_id))
5606 return PTR_ERR(child_id);
5607
5608 for (i = 0; i < depth; i++)
5609 child_id->stack[i] = parent_id->stack[i];
5610 child_id->stack[depth] = child_id->id;
5611 /*
5612 * child_id->css pointer will be set after this cgroup is available
5613 * see cgroup_populate_dir()
5614 */
5615 rcu_assign_pointer(child_css->id, child_id);
5616
5617 return 0;
5618}
5619
5620/**
5621 * css_lookup - lookup css by id
5622 * @ss: cgroup subsys to be looked into.
5623 * @id: the id
5624 *
5625 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5626 * NULL if not. Should be called under rcu_read_lock()
5627 */
5628struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5629{
5630 struct css_id *cssid = NULL;
5631
5632 BUG_ON(!ss->use_id);
5633 cssid = idr_find(&ss->idr, id);
5634
5635 if (unlikely(!cssid))
5636 return NULL;
5637
5638 return rcu_dereference(cssid->css);
5639}
Ben Blum67523c42010-03-10 15:22:11 -08005640EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005641
Tejun Heob77d7b62013-08-13 11:01:54 -04005642/**
5643 * cgroup_css_from_dir - get corresponding css from file open on cgroup dir
5644 * @f: directory file of interest
5645 * @id: subsystem id of interest
5646 *
5647 * Must be called under RCU read lock. The caller is responsible for
5648 * pinning the returned css if it needs to be accessed outside the RCU
5649 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005650 */
5651struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5652{
5653 struct cgroup *cgrp;
5654 struct inode *inode;
5655 struct cgroup_subsys_state *css;
5656
Tejun Heob77d7b62013-08-13 11:01:54 -04005657 WARN_ON_ONCE(!rcu_read_lock_held());
5658
Al Viro496ad9a2013-01-23 17:07:38 -05005659 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005660 /* check in cgroup filesystem dir */
5661 if (inode->i_op != &cgroup_dir_inode_operations)
5662 return ERR_PTR(-EBADF);
5663
5664 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5665 return ERR_PTR(-EINVAL);
5666
5667 /* get cgroup */
5668 cgrp = __d_cgrp(f->f_dentry);
Tejun Heo40e93b32013-08-13 11:01:53 -04005669 css = cgroup_css(cgrp, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005670 return css ? css : ERR_PTR(-ENOENT);
5671}
5672
Paul Menagefe693432009-09-23 15:56:20 -07005673#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005674static struct cgroup_subsys_state *
5675debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005676{
5677 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5678
5679 if (!css)
5680 return ERR_PTR(-ENOMEM);
5681
5682 return css;
5683}
5684
Tejun Heoeb954192013-08-08 20:11:23 -04005685static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005686{
Tejun Heoeb954192013-08-08 20:11:23 -04005687 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005688}
5689
Tejun Heo182446d2013-08-08 20:11:24 -04005690static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5691 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005692{
Tejun Heo182446d2013-08-08 20:11:24 -04005693 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005694}
5695
Tejun Heo182446d2013-08-08 20:11:24 -04005696static u64 current_css_set_read(struct cgroup_subsys_state *css,
5697 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005698{
5699 return (u64)(unsigned long)current->cgroups;
5700}
5701
Tejun Heo182446d2013-08-08 20:11:24 -04005702static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005703 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005704{
5705 u64 count;
5706
5707 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005708 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005709 rcu_read_unlock();
5710 return count;
5711}
5712
Tejun Heo182446d2013-08-08 20:11:24 -04005713static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005714 struct cftype *cft,
5715 struct seq_file *seq)
5716{
Tejun Heo69d02062013-06-12 21:04:50 -07005717 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005718 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005719
5720 read_lock(&css_set_lock);
5721 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005722 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005723 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005724 struct cgroup *c = link->cgrp;
5725 const char *name;
5726
5727 if (c->dentry)
5728 name = c->dentry->d_name.name;
5729 else
5730 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005731 seq_printf(seq, "Root %d group %s\n",
5732 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005733 }
5734 rcu_read_unlock();
5735 read_unlock(&css_set_lock);
5736 return 0;
5737}
5738
5739#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005740static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5741 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005742{
Tejun Heo69d02062013-06-12 21:04:50 -07005743 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005744
5745 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005746 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005747 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005748 struct task_struct *task;
5749 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005750 seq_printf(seq, "css_set %p\n", cset);
5751 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005752 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5753 seq_puts(seq, " ...\n");
5754 break;
5755 } else {
5756 seq_printf(seq, " task %d\n",
5757 task_pid_vnr(task));
5758 }
5759 }
5760 }
5761 read_unlock(&css_set_lock);
5762 return 0;
5763}
5764
Tejun Heo182446d2013-08-08 20:11:24 -04005765static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005766{
Tejun Heo182446d2013-08-08 20:11:24 -04005767 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005768}
5769
5770static struct cftype debug_files[] = {
5771 {
Paul Menagefe693432009-09-23 15:56:20 -07005772 .name = "taskcount",
5773 .read_u64 = debug_taskcount_read,
5774 },
5775
5776 {
5777 .name = "current_css_set",
5778 .read_u64 = current_css_set_read,
5779 },
5780
5781 {
5782 .name = "current_css_set_refcount",
5783 .read_u64 = current_css_set_refcount_read,
5784 },
5785
5786 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005787 .name = "current_css_set_cg_links",
5788 .read_seq_string = current_css_set_cg_links_read,
5789 },
5790
5791 {
5792 .name = "cgroup_css_links",
5793 .read_seq_string = cgroup_css_links_read,
5794 },
5795
5796 {
Paul Menagefe693432009-09-23 15:56:20 -07005797 .name = "releasable",
5798 .read_u64 = releasable_read,
5799 },
Paul Menagefe693432009-09-23 15:56:20 -07005800
Tejun Heo4baf6e32012-04-01 12:09:55 -07005801 { } /* terminate */
5802};
Paul Menagefe693432009-09-23 15:56:20 -07005803
5804struct cgroup_subsys debug_subsys = {
5805 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005806 .css_alloc = debug_css_alloc,
5807 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005808 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005809 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005810};
5811#endif /* CONFIG_CGROUP_DEBUG */