blob: 174f4c3d72efd556dce8d6cbb0f21347a5f588ee [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040084EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070085#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -070099static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroupfs_root cgroup_dummy_root;
109
110/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Tejun Heo105347b2013-08-13 11:01:55 -0400120 struct cgroup_subsys_state *css;
Li Zefan712317a2013-04-18 23:09:52 -0700121
122 /* file xattrs */
123 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700124};
125
126/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700127 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
128 * cgroup_subsys->use_id != 0.
129 */
130#define CSS_ID_MAX (65535)
131struct css_id {
132 /*
133 * The css to which this ID points. This pointer is set to valid value
134 * after cgroup is populated. If cgroup is removed, this will be NULL.
135 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800136 * is called after synchronize_rcu(). But for safe use, css_tryget()
137 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700138 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100139 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700140 /*
141 * ID of this css.
142 */
143 unsigned short id;
144 /*
145 * Depth in hierarchy which this ID belongs to.
146 */
147 unsigned short depth;
148 /*
149 * ID is freed by RCU. (and lookup routine is RCU safe.)
150 */
151 struct rcu_head rcu_head;
152 /*
153 * Hierarchy of CSS ID belongs to.
154 */
155 unsigned short stack[0]; /* Array of Length (depth+1) */
156};
157
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800158/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300159 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800160 */
161struct cgroup_event {
162 /*
Tejun Heo81eeaf02013-08-08 20:11:26 -0400163 * css which the event belongs to.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800164 */
Tejun Heo81eeaf02013-08-08 20:11:26 -0400165 struct cgroup_subsys_state *css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800166 /*
167 * Control file which the event associated.
168 */
169 struct cftype *cft;
170 /*
171 * eventfd to signal userspace about the event.
172 */
173 struct eventfd_ctx *eventfd;
174 /*
175 * Each of these stored in a list by the cgroup.
176 */
177 struct list_head list;
178 /*
179 * All fields below needed to unregister event when
180 * userspace closes eventfd.
181 */
182 poll_table pt;
183 wait_queue_head_t *wqh;
184 wait_queue_t wait;
185 struct work_struct remove;
186};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700187
Paul Menageddbcc7e2007-10-18 23:39:30 -0700188/* The list of hierarchy roots */
189
Tejun Heo9871bf92013-06-24 15:21:47 -0700190static LIST_HEAD(cgroup_roots);
191static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700192
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700193/*
194 * Hierarchy ID allocation and mapping. It follows the same exclusion
195 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
196 * writes, either for reads.
197 */
Tejun Heo1a574232013-04-14 11:36:58 -0700198static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700199
Li Zefan65dff752013-03-01 15:01:56 +0800200static struct cgroup_name root_cgroup_name = { .name = "/" };
201
Li Zefan794611a2013-06-18 18:53:53 +0800202/*
203 * Assign a monotonically increasing serial number to cgroups. It
204 * guarantees cgroups with bigger numbers are newer than those with smaller
205 * numbers. Also, as cgroups are always appended to the parent's
206 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700207 * the ascending serial number order on the list. Protected by
208 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800209 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700210static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800211
Paul Menageddbcc7e2007-10-18 23:39:30 -0700212/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800213 * check for fork/exit handlers to call. This avoids us having to do
214 * extra work in the fork/exit path if none of the subsystems need to
215 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700216 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700217static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700218
Tejun Heo628f7cd2013-06-28 16:24:11 -0700219static struct cftype cgroup_base_files[];
220
Tejun Heof20104d2013-08-13 20:22:50 -0400221static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800222static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400223static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
224 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800225
Tejun Heo95109b62013-08-08 20:11:27 -0400226/**
227 * cgroup_css - obtain a cgroup's css for the specified subsystem
228 * @cgrp: the cgroup of interest
229 * @subsys_id: the subsystem of interest
230 *
231 * Return @cgrp's css (cgroup_subsys_state) associated with @subsys_id.
Tejun Heo73e80ed2013-08-13 11:01:55 -0400232 * This function must be called either under cgroup_mutex or
233 * rcu_read_lock() and the caller is responsible for pinning the returned
234 * css if it wants to keep accessing it outside the said locks. This
235 * function may return %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400236 */
237static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
238 int subsys_id)
239{
Tejun Heo73e80ed2013-08-13 11:01:55 -0400240 return rcu_dereference_check(cgrp->subsys[subsys_id],
241 lockdep_is_held(&cgroup_mutex));
Tejun Heo95109b62013-08-08 20:11:27 -0400242}
243
Paul Menageddbcc7e2007-10-18 23:39:30 -0700244/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700245static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700246{
Tejun Heo54766d42013-06-12 21:04:53 -0700247 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700248}
249
Li Zefan78574cf2013-04-08 19:00:38 -0700250/**
251 * cgroup_is_descendant - test ancestry
252 * @cgrp: the cgroup to be tested
253 * @ancestor: possible ancestor of @cgrp
254 *
255 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
256 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
257 * and @ancestor are accessible.
258 */
259bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
260{
261 while (cgrp) {
262 if (cgrp == ancestor)
263 return true;
264 cgrp = cgrp->parent;
265 }
266 return false;
267}
268EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700269
Adrian Bunke9685a02008-02-07 00:13:46 -0800270static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700271{
272 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700273 (1 << CGRP_RELEASABLE) |
274 (1 << CGRP_NOTIFY_ON_RELEASE);
275 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700276}
277
Adrian Bunke9685a02008-02-07 00:13:46 -0800278static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700279{
Paul Menagebd89aab2007-10-18 23:40:44 -0700280 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700281}
282
Tejun Heo30159ec2013-06-25 11:53:37 -0700283/**
284 * for_each_subsys - iterate all loaded cgroup subsystems
285 * @ss: the iteration cursor
286 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
287 *
288 * Should be called under cgroup_mutex.
289 */
290#define for_each_subsys(ss, i) \
291 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
292 if (({ lockdep_assert_held(&cgroup_mutex); \
293 !((ss) = cgroup_subsys[i]); })) { } \
294 else
295
296/**
297 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
298 * @ss: the iteration cursor
299 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
300 *
301 * Bulit-in subsystems are always present and iteration itself doesn't
302 * require any synchronization.
303 */
304#define for_each_builtin_subsys(ss, i) \
305 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
306 (((ss) = cgroup_subsys[i]) || true); (i)++)
307
Tejun Heo5549c492013-06-24 15:21:48 -0700308/* iterate each subsystem attached to a hierarchy */
309#define for_each_root_subsys(root, ss) \
310 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700311
Tejun Heo5549c492013-06-24 15:21:48 -0700312/* iterate across the active hierarchies */
313#define for_each_active_root(root) \
314 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700315
Tejun Heof6ea9372012-04-01 12:09:55 -0700316static inline struct cgroup *__d_cgrp(struct dentry *dentry)
317{
318 return dentry->d_fsdata;
319}
320
Tejun Heo05ef1d72012-04-01 12:09:56 -0700321static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700322{
323 return dentry->d_fsdata;
324}
325
Tejun Heo05ef1d72012-04-01 12:09:56 -0700326static inline struct cftype *__d_cft(struct dentry *dentry)
327{
328 return __d_cfe(dentry)->type;
329}
330
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700331/**
332 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
333 * @cgrp: the cgroup to be checked for liveness
334 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700335 * On success, returns true; the mutex should be later unlocked. On
336 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700337 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700338static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700339{
340 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700341 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700342 mutex_unlock(&cgroup_mutex);
343 return false;
344 }
345 return true;
346}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700347
Paul Menage81a6a5c2007-10-18 23:39:38 -0700348/* the list of cgroups eligible for automatic release. Protected by
349 * release_list_lock */
350static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200351static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700352static void cgroup_release_agent(struct work_struct *work);
353static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700354static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700355
Tejun Heo69d02062013-06-12 21:04:50 -0700356/*
357 * A cgroup can be associated with multiple css_sets as different tasks may
358 * belong to different cgroups on different hierarchies. In the other
359 * direction, a css_set is naturally associated with multiple cgroups.
360 * This M:N relationship is represented by the following link structure
361 * which exists for each association and allows traversing the associations
362 * from both sides.
363 */
364struct cgrp_cset_link {
365 /* the cgroup and css_set this link associates */
366 struct cgroup *cgrp;
367 struct css_set *cset;
368
369 /* list of cgrp_cset_links anchored at cgrp->cset_links */
370 struct list_head cset_link;
371
372 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
373 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700374};
375
376/* The default css_set - used by init and its children prior to any
377 * hierarchies being mounted. It contains a pointer to the root state
378 * for each subsystem. Also used to anchor the list of css_sets. Not
379 * reference-counted, to improve performance when child cgroups
380 * haven't been created.
381 */
382
383static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700384static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700385
Ben Blume6a11052010-03-10 15:22:09 -0800386static int cgroup_init_idr(struct cgroup_subsys *ss,
387 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700388
Tejun Heo0942eee2013-08-08 20:11:26 -0400389/*
390 * css_set_lock protects the list of css_set objects, and the chain of
391 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400392 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400393 */
Paul Menage817929e2007-10-18 23:39:36 -0700394static DEFINE_RWLOCK(css_set_lock);
395static int css_set_count;
396
Paul Menage7717f7b2009-09-23 15:56:22 -0700397/*
398 * hash table for cgroup groups. This improves the performance to find
399 * an existing css_set. This hash doesn't (currently) take into
400 * account cgroups in empty hierarchies.
401 */
Li Zefan472b1052008-04-29 01:00:11 -0700402#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800403static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700404
Li Zefan0ac801f2013-01-10 11:49:27 +0800405static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700406{
Li Zefan0ac801f2013-01-10 11:49:27 +0800407 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700408 struct cgroup_subsys *ss;
409 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700410
Tejun Heo30159ec2013-06-25 11:53:37 -0700411 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800412 key += (unsigned long)css[i];
413 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700414
Li Zefan0ac801f2013-01-10 11:49:27 +0800415 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700416}
417
Tejun Heo0942eee2013-08-08 20:11:26 -0400418/*
419 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400420 * until after the first call to css_task_iter_start(). This reduces the
421 * fork()/exit() overhead for people who have cgroups compiled into their
422 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400423 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700424static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700425
Tejun Heo5abb8852013-06-12 21:04:49 -0700426static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700427{
Tejun Heo69d02062013-06-12 21:04:50 -0700428 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700429
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700430 /*
431 * Ensure that the refcount doesn't hit zero while any readers
432 * can see it. Similar to atomic_dec_and_lock(), but for an
433 * rwlock
434 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700435 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700436 return;
437 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700438 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700439 write_unlock(&css_set_lock);
440 return;
441 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700442
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700443 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700444 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700445 css_set_count--;
446
Tejun Heo69d02062013-06-12 21:04:50 -0700447 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700448 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700449
Tejun Heo69d02062013-06-12 21:04:50 -0700450 list_del(&link->cset_link);
451 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800452
Tejun Heoddd69142013-06-12 21:04:54 -0700453 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700454 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700455 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700456 set_bit(CGRP_RELEASABLE, &cgrp->flags);
457 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700459
460 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700461 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700462
463 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700464 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700465}
466
467/*
468 * refcounted get/put for css_set objects
469 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700470static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700471{
Tejun Heo5abb8852013-06-12 21:04:49 -0700472 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700473}
474
Tejun Heo5abb8852013-06-12 21:04:49 -0700475static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700476{
Tejun Heo5abb8852013-06-12 21:04:49 -0700477 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700478}
479
Tejun Heo5abb8852013-06-12 21:04:49 -0700480static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700481{
Tejun Heo5abb8852013-06-12 21:04:49 -0700482 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700483}
484
Tejun Heob326f9d2013-06-24 15:21:48 -0700485/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700486 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700487 * @cset: candidate css_set being tested
488 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700489 * @new_cgrp: cgroup that's being entered by the task
490 * @template: desired set of css pointers in css_set (pre-calculated)
491 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800492 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700493 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
494 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700495static bool compare_css_sets(struct css_set *cset,
496 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700497 struct cgroup *new_cgrp,
498 struct cgroup_subsys_state *template[])
499{
500 struct list_head *l1, *l2;
501
Tejun Heo5abb8852013-06-12 21:04:49 -0700502 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700503 /* Not all subsystems matched */
504 return false;
505 }
506
507 /*
508 * Compare cgroup pointers in order to distinguish between
509 * different cgroups in heirarchies with no subsystems. We
510 * could get by with just this check alone (and skip the
511 * memcmp above) but on most setups the memcmp check will
512 * avoid the need for this more expensive check on almost all
513 * candidates.
514 */
515
Tejun Heo69d02062013-06-12 21:04:50 -0700516 l1 = &cset->cgrp_links;
517 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700518 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700519 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700520 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700521
522 l1 = l1->next;
523 l2 = l2->next;
524 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700525 if (l1 == &cset->cgrp_links) {
526 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700527 break;
528 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700529 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700530 }
531 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700532 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
533 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
534 cgrp1 = link1->cgrp;
535 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700536 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700537 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700538
539 /*
540 * If this hierarchy is the hierarchy of the cgroup
541 * that's changing, then we need to check that this
542 * css_set points to the new cgroup; if it's any other
543 * hierarchy, then this css_set should point to the
544 * same cgroup as the old css_set.
545 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700546 if (cgrp1->root == new_cgrp->root) {
547 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700548 return false;
549 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700551 return false;
552 }
553 }
554 return true;
555}
556
Tejun Heob326f9d2013-06-24 15:21:48 -0700557/**
558 * find_existing_css_set - init css array and find the matching css_set
559 * @old_cset: the css_set that we're using before the cgroup transition
560 * @cgrp: the cgroup that we're moving into
561 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700562 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700563static struct css_set *find_existing_css_set(struct css_set *old_cset,
564 struct cgroup *cgrp,
565 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700566{
Paul Menagebd89aab2007-10-18 23:40:44 -0700567 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700568 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700569 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800570 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700571 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700572
Ben Blumaae8aab2010-03-10 15:22:07 -0800573 /*
574 * Build the set of subsystem state objects that we want to see in the
575 * new css_set. while subsystems can change globally, the entries here
576 * won't change, so no need for locking.
577 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700578 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400579 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700580 /* Subsystem is in this hierarchy. So we want
581 * the subsystem state from the new
582 * cgroup */
Tejun Heo40e93b32013-08-13 11:01:53 -0400583 template[i] = cgroup_css(cgrp, i);
Paul Menage817929e2007-10-18 23:39:36 -0700584 } else {
585 /* Subsystem is not in this hierarchy, so we
586 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700587 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700588 }
589 }
590
Li Zefan0ac801f2013-01-10 11:49:27 +0800591 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700592 hash_for_each_possible(css_set_table, cset, hlist, key) {
593 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700594 continue;
595
596 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700597 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700598 }
Paul Menage817929e2007-10-18 23:39:36 -0700599
600 /* No existing cgroup group matched */
601 return NULL;
602}
603
Tejun Heo69d02062013-06-12 21:04:50 -0700604static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700605{
Tejun Heo69d02062013-06-12 21:04:50 -0700606 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700607
Tejun Heo69d02062013-06-12 21:04:50 -0700608 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
609 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700610 kfree(link);
611 }
612}
613
Tejun Heo69d02062013-06-12 21:04:50 -0700614/**
615 * allocate_cgrp_cset_links - allocate cgrp_cset_links
616 * @count: the number of links to allocate
617 * @tmp_links: list_head the allocated links are put on
618 *
619 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
620 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700621 */
Tejun Heo69d02062013-06-12 21:04:50 -0700622static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700623{
Tejun Heo69d02062013-06-12 21:04:50 -0700624 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700625 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700626
627 INIT_LIST_HEAD(tmp_links);
628
Li Zefan36553432008-07-29 22:33:19 -0700629 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700630 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700631 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700632 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700633 return -ENOMEM;
634 }
Tejun Heo69d02062013-06-12 21:04:50 -0700635 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700636 }
637 return 0;
638}
639
Li Zefanc12f65d2009-01-07 18:07:42 -0800640/**
641 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700642 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700643 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800644 * @cgrp: the destination cgroup
645 */
Tejun Heo69d02062013-06-12 21:04:50 -0700646static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
647 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800648{
Tejun Heo69d02062013-06-12 21:04:50 -0700649 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800650
Tejun Heo69d02062013-06-12 21:04:50 -0700651 BUG_ON(list_empty(tmp_links));
652 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
653 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700654 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700655 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700656 /*
657 * Always add links to the tail of the list so that the list
658 * is sorted by order of hierarchy creation
659 */
Tejun Heo69d02062013-06-12 21:04:50 -0700660 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800661}
662
Tejun Heob326f9d2013-06-24 15:21:48 -0700663/**
664 * find_css_set - return a new css_set with one cgroup updated
665 * @old_cset: the baseline css_set
666 * @cgrp: the cgroup to be updated
667 *
668 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
669 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700670 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700671static struct css_set *find_css_set(struct css_set *old_cset,
672 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700673{
Tejun Heob326f9d2013-06-24 15:21:48 -0700674 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700675 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700676 struct list_head tmp_links;
677 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800678 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700679
Tejun Heob326f9d2013-06-24 15:21:48 -0700680 lockdep_assert_held(&cgroup_mutex);
681
Paul Menage817929e2007-10-18 23:39:36 -0700682 /* First see if we already have a cgroup group that matches
683 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700684 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700685 cset = find_existing_css_set(old_cset, cgrp, template);
686 if (cset)
687 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700688 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700689
Tejun Heo5abb8852013-06-12 21:04:49 -0700690 if (cset)
691 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700692
Tejun Heof4f4be22013-06-12 21:04:51 -0700693 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700694 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700695 return NULL;
696
Tejun Heo69d02062013-06-12 21:04:50 -0700697 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700698 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700699 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700700 return NULL;
701 }
702
Tejun Heo5abb8852013-06-12 21:04:49 -0700703 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700704 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700705 INIT_LIST_HEAD(&cset->tasks);
706 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700707
708 /* Copy the set of subsystem state objects generated in
709 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700710 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700711
712 write_lock(&css_set_lock);
713 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700714 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700715 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700716
Paul Menage7717f7b2009-09-23 15:56:22 -0700717 if (c->root == cgrp->root)
718 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700719 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700720 }
Paul Menage817929e2007-10-18 23:39:36 -0700721
Tejun Heo69d02062013-06-12 21:04:50 -0700722 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700723
Paul Menage817929e2007-10-18 23:39:36 -0700724 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700725
726 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700727 key = css_set_hash(cset->subsys);
728 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700729
Paul Menage817929e2007-10-18 23:39:36 -0700730 write_unlock(&css_set_lock);
731
Tejun Heo5abb8852013-06-12 21:04:49 -0700732 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700733}
734
Paul Menageddbcc7e2007-10-18 23:39:30 -0700735/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700736 * Return the cgroup for "task" from the given hierarchy. Must be
737 * called with cgroup_mutex held.
738 */
739static struct cgroup *task_cgroup_from_root(struct task_struct *task,
740 struct cgroupfs_root *root)
741{
Tejun Heo5abb8852013-06-12 21:04:49 -0700742 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700743 struct cgroup *res = NULL;
744
745 BUG_ON(!mutex_is_locked(&cgroup_mutex));
746 read_lock(&css_set_lock);
747 /*
748 * No need to lock the task - since we hold cgroup_mutex the
749 * task can't change groups, so the only thing that can happen
750 * is that it exits and its css is set back to init_css_set.
751 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700752 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700753 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700754 res = &root->top_cgroup;
755 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700756 struct cgrp_cset_link *link;
757
758 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700759 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700760
Paul Menage7717f7b2009-09-23 15:56:22 -0700761 if (c->root == root) {
762 res = c;
763 break;
764 }
765 }
766 }
767 read_unlock(&css_set_lock);
768 BUG_ON(!res);
769 return res;
770}
771
772/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 * There is one global cgroup mutex. We also require taking
774 * task_lock() when dereferencing a task's cgroup subsys pointers.
775 * See "The task_lock() exception", at the end of this comment.
776 *
777 * A task must hold cgroup_mutex to modify cgroups.
778 *
779 * Any task can increment and decrement the count field without lock.
780 * So in general, code holding cgroup_mutex can't rely on the count
781 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800782 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783 * means that no tasks are currently attached, therefore there is no
784 * way a task attached to that cgroup can fork (the other way to
785 * increment the count). So code holding cgroup_mutex can safely
786 * assume that if the count is zero, it will stay zero. Similarly, if
787 * a task holds cgroup_mutex on a cgroup with zero count, it
788 * knows that the cgroup won't be removed, as cgroup_rmdir()
789 * needs that mutex.
790 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
792 * (usually) take cgroup_mutex. These are the two most performance
793 * critical pieces of code here. The exception occurs on cgroup_exit(),
794 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
795 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800796 * to the release agent with the name of the cgroup (path relative to
797 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798 *
799 * A cgroup can only be deleted if both its 'count' of using tasks
800 * is zero, and its list of 'children' cgroups is empty. Since all
801 * tasks in the system use _some_ cgroup, and since there is always at
802 * least one task in the system (init, pid == 1), therefore, top_cgroup
803 * always has either children cgroups and/or using tasks. So we don't
804 * need a special hack to ensure that top_cgroup cannot be deleted.
805 *
806 * The task_lock() exception
807 *
808 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800809 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800810 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700811 * several performance critical places that need to reference
812 * task->cgroup without the expense of grabbing a system global
813 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800814 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
816 * the task_struct routinely used for such matters.
817 *
818 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800819 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820 */
821
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822/*
823 * A couple of forward declarations required, due to cyclic reference loop:
824 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
825 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
826 * -> cgroup_mkdir.
827 */
828
Al Viro18bb1db2011-07-26 01:41:39 -0400829static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400830static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700831static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700832static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700833static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700834static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700835
836static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200837 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700838 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700839};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700840
Tejun Heo623f9262013-08-13 11:01:55 -0400841static int alloc_css_id(struct cgroup_subsys_state *child_css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700842
Al Viroa5e7ed32011-07-26 01:55:55 -0400843static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700844{
845 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846
847 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400848 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100850 inode->i_uid = current_fsuid();
851 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700852 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
853 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
854 }
855 return inode;
856}
857
Li Zefan65dff752013-03-01 15:01:56 +0800858static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
859{
860 struct cgroup_name *name;
861
862 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
863 if (!name)
864 return NULL;
865 strcpy(name->name, dentry->d_name.name);
866 return name;
867}
868
Li Zefanbe445622013-01-24 14:31:42 +0800869static void cgroup_free_fn(struct work_struct *work)
870{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700871 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800872 struct cgroup_subsys *ss;
873
874 mutex_lock(&cgroup_mutex);
875 /*
876 * Release the subsystem state objects.
877 */
Tejun Heoeb954192013-08-08 20:11:23 -0400878 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -0400879 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -0400880
881 ss->css_free(css);
882 }
Li Zefanbe445622013-01-24 14:31:42 +0800883
884 cgrp->root->number_of_cgroups--;
885 mutex_unlock(&cgroup_mutex);
886
887 /*
Li Zefan415cf072013-04-08 14:35:02 +0800888 * We get a ref to the parent's dentry, and put the ref when
889 * this cgroup is being freed, so it's guaranteed that the
890 * parent won't be destroyed before its children.
891 */
892 dput(cgrp->parent->dentry);
893
894 /*
Li Zefanbe445622013-01-24 14:31:42 +0800895 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700896 * created the cgroup. This will free cgrp->root, if we are
897 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800898 */
899 deactivate_super(cgrp->root->sb);
900
901 /*
902 * if we're getting rid of the cgroup, refcount should ensure
903 * that there are no pidlists left.
904 */
905 BUG_ON(!list_empty(&cgrp->pidlists));
906
907 simple_xattrs_free(&cgrp->xattrs);
908
Li Zefan65dff752013-03-01 15:01:56 +0800909 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800910 kfree(cgrp);
911}
912
913static void cgroup_free_rcu(struct rcu_head *head)
914{
915 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
916
Tejun Heoea15f8c2013-06-13 19:27:42 -0700917 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
918 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800919}
920
Paul Menageddbcc7e2007-10-18 23:39:30 -0700921static void cgroup_diput(struct dentry *dentry, struct inode *inode)
922{
923 /* is dentry a directory ? if so, kfree() associated cgroup */
924 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700925 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800926
Tejun Heo54766d42013-06-12 21:04:53 -0700927 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800928 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700929 } else {
930 struct cfent *cfe = __d_cfe(dentry);
931 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
932
933 WARN_ONCE(!list_empty(&cfe->node) &&
934 cgrp != &cgrp->root->top_cgroup,
935 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700936 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700938 }
939 iput(inode);
940}
941
Al Viroc72a04e2011-01-14 05:31:45 +0000942static int cgroup_delete(const struct dentry *d)
943{
944 return 1;
945}
946
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947static void remove_dir(struct dentry *d)
948{
949 struct dentry *parent = dget(d->d_parent);
950
951 d_delete(d);
952 simple_rmdir(parent->d_inode, d);
953 dput(parent);
954}
955
Li Zefan2739d3c2013-01-21 18:18:33 +0800956static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700958 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959
Tejun Heo05ef1d72012-04-01 12:09:56 -0700960 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
961 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100962
Li Zefan2739d3c2013-01-21 18:18:33 +0800963 /*
964 * If we're doing cleanup due to failure of cgroup_create(),
965 * the corresponding @cfe may not exist.
966 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700967 list_for_each_entry(cfe, &cgrp->files, node) {
968 struct dentry *d = cfe->dentry;
969
970 if (cft && cfe->type != cft)
971 continue;
972
973 dget(d);
974 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700975 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700976 list_del_init(&cfe->node);
977 dput(d);
978
Li Zefan2739d3c2013-01-21 18:18:33 +0800979 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700981}
982
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400983/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700984 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700985 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400986 * @subsys_mask: mask of the subsystem ids whose files should be removed
987 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700988static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700989{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400990 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700991 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700992
Tejun Heob420ba72013-07-12 12:34:02 -0700993 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400994 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700995
996 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400997 continue;
998 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400999 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001000 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001}
1002
1003/*
1004 * NOTE : the dentry must have been dget()'ed
1005 */
1006static void cgroup_d_remove_dir(struct dentry *dentry)
1007{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001008 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001010 parent = dentry->d_parent;
1011 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001012 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001014 spin_unlock(&dentry->d_lock);
1015 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 remove_dir(dentry);
1017}
1018
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001019/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001020 * Call with cgroup_mutex held. Drops reference counts on modules, including
1021 * any duplicate ones that parse_cgroupfs_options took. If this function
1022 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001023 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001025 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026{
Paul Menagebd89aab2007-10-18 23:40:44 -07001027 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001028 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001029 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001030 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031
Ben Blumaae8aab2010-03-10 15:22:07 -08001032 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001033 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001034
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001036 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001037 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001039
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001040 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001041 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001042 ret = -EBUSY;
1043 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001045
1046 /* pin the module */
1047 if (!try_module_get(ss->module)) {
1048 ret = -ENOENT;
1049 goto out_put;
1050 }
1051 pinned |= 1 << i;
1052 }
1053
1054 /* subsys could be missing if unloaded between parsing and here */
1055 if (added_mask != pinned) {
1056 ret = -ENOENT;
1057 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 }
1059
Tejun Heo31261212013-06-28 17:07:30 -07001060 ret = cgroup_populate_dir(cgrp, added_mask);
1061 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001062 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001063
1064 /*
1065 * Nothing can fail from this point on. Remove files for the
1066 * removed subsystems and rebind each subsystem.
1067 */
1068 cgroup_clear_dir(cgrp, removed_mask);
1069
Tejun Heo30159ec2013-06-25 11:53:37 -07001070 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001072
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001073 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 /* We're binding this subsystem to this hierarchy */
Tejun Heo40e93b32013-08-13 11:01:53 -04001075 BUG_ON(cgroup_css(cgrp, i));
1076 BUG_ON(!cgroup_css(cgroup_dummy_top, i));
1077 BUG_ON(cgroup_css(cgroup_dummy_top, i)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001078
Tejun Heo73e80ed2013-08-13 11:01:55 -04001079 rcu_assign_pointer(cgrp->subsys[i],
1080 cgroup_css(cgroup_dummy_top, i));
Tejun Heo40e93b32013-08-13 11:01:53 -04001081 cgroup_css(cgrp, i)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001082
Li Zefan33a68ac2009-01-07 18:07:42 -08001083 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001084 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 if (ss->bind)
Tejun Heo40e93b32013-08-13 11:01:53 -04001086 ss->bind(cgroup_css(cgrp, i));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001087
Ben Blumcf5d5942010-03-10 15:22:09 -08001088 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001089 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001090 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091 /* We're removing this subsystem */
Tejun Heo40e93b32013-08-13 11:01:53 -04001092 BUG_ON(cgroup_css(cgrp, i) != cgroup_css(cgroup_dummy_top, i));
1093 BUG_ON(cgroup_css(cgrp, i)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001094
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 if (ss->bind)
Tejun Heo40e93b32013-08-13 11:01:53 -04001096 ss->bind(cgroup_css(cgroup_dummy_top, i));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001097
Tejun Heo40e93b32013-08-13 11:01:53 -04001098 cgroup_css(cgroup_dummy_top, i)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001099 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1100
Tejun Heo9871bf92013-06-24 15:21:47 -07001101 cgroup_subsys[i]->root = &cgroup_dummy_root;
1102 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001103
Ben Blumcf5d5942010-03-10 15:22:09 -08001104 /* subsystem is now free - drop reference on module */
1105 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001106 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 }
1108 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109
Tejun Heo1672d042013-06-25 18:04:54 -07001110 /*
1111 * Mark @root has finished binding subsystems. @root->subsys_mask
1112 * now matches the bound subsystems.
1113 */
1114 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1115
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001117
1118out_put:
1119 for_each_subsys(ss, i)
1120 if (pinned & (1 << i))
1121 module_put(ss->module);
1122 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123}
1124
Al Viro34c80b12011-12-08 21:32:45 -05001125static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126{
Al Viro34c80b12011-12-08 21:32:45 -05001127 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128 struct cgroup_subsys *ss;
1129
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001130 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001131 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001132 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001133 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1134 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001135 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001137 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001138 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001139 if (strlen(root->release_agent_path))
1140 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001141 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001142 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001143 if (strlen(root->name))
1144 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001145 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146 return 0;
1147}
1148
1149struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001150 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001151 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001152 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001153 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001154 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001155 /* User explicitly requested empty subsystem */
1156 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001157
1158 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001159
Paul Menageddbcc7e2007-10-18 23:39:30 -07001160};
1161
Ben Blumaae8aab2010-03-10 15:22:07 -08001162/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001163 * Convert a hierarchy specifier into a bitmask of subsystems and
1164 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1165 * array. This function takes refcounts on subsystems to be used, unless it
1166 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001167 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001168static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001169{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001170 char *token, *o = data;
1171 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001172 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001173 struct cgroup_subsys *ss;
1174 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001175
Ben Blumaae8aab2010-03-10 15:22:07 -08001176 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1177
Li Zefanf9ab5b52009-06-17 16:26:33 -07001178#ifdef CONFIG_CPUSETS
1179 mask = ~(1UL << cpuset_subsys_id);
1180#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001181
Paul Menagec6d57f32009-09-23 15:56:19 -07001182 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001183
1184 while ((token = strsep(&o, ",")) != NULL) {
1185 if (!*token)
1186 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001187 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001188 /* Explicitly have no subsystems */
1189 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001190 continue;
1191 }
1192 if (!strcmp(token, "all")) {
1193 /* Mutually exclusive option 'all' + subsystem name */
1194 if (one_ss)
1195 return -EINVAL;
1196 all_ss = true;
1197 continue;
1198 }
Tejun Heo873fe092013-04-14 20:15:26 -07001199 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1200 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1201 continue;
1202 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001203 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001204 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001205 continue;
1206 }
1207 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001208 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001209 continue;
1210 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001211 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001212 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001213 continue;
1214 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001215 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001216 /* Specifying two release agents is forbidden */
1217 if (opts->release_agent)
1218 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001219 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001220 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001221 if (!opts->release_agent)
1222 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001223 continue;
1224 }
1225 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001226 const char *name = token + 5;
1227 /* Can't specify an empty name */
1228 if (!strlen(name))
1229 return -EINVAL;
1230 /* Must match [\w.-]+ */
1231 for (i = 0; i < strlen(name); i++) {
1232 char c = name[i];
1233 if (isalnum(c))
1234 continue;
1235 if ((c == '.') || (c == '-') || (c == '_'))
1236 continue;
1237 return -EINVAL;
1238 }
1239 /* Specifying two names is forbidden */
1240 if (opts->name)
1241 return -EINVAL;
1242 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001243 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001244 GFP_KERNEL);
1245 if (!opts->name)
1246 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001247
1248 continue;
1249 }
1250
Tejun Heo30159ec2013-06-25 11:53:37 -07001251 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001252 if (strcmp(token, ss->name))
1253 continue;
1254 if (ss->disabled)
1255 continue;
1256
1257 /* Mutually exclusive option 'all' + subsystem name */
1258 if (all_ss)
1259 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001260 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001261 one_ss = true;
1262
1263 break;
1264 }
1265 if (i == CGROUP_SUBSYS_COUNT)
1266 return -ENOENT;
1267 }
1268
1269 /*
1270 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001271 * otherwise if 'none', 'name=' and a subsystem name options
1272 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001273 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001274 if (all_ss || (!one_ss && !opts->none && !opts->name))
1275 for_each_subsys(ss, i)
1276 if (!ss->disabled)
1277 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001278
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001279 /* Consistency checks */
1280
Tejun Heo873fe092013-04-14 20:15:26 -07001281 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1282 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1283
1284 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1285 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1286 return -EINVAL;
1287 }
1288
1289 if (opts->cpuset_clone_children) {
1290 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1291 return -EINVAL;
1292 }
1293 }
1294
Li Zefanf9ab5b52009-06-17 16:26:33 -07001295 /*
1296 * Option noprefix was introduced just for backward compatibility
1297 * with the old cpuset, so we allow noprefix only if mounting just
1298 * the cpuset subsystem.
1299 */
Tejun Heo93438622013-04-14 20:15:25 -07001300 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001301 return -EINVAL;
1302
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001303
1304 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001305 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001306 return -EINVAL;
1307
1308 /*
1309 * We either have to specify by name or by subsystems. (So all
1310 * empty hierarchies must have a name).
1311 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001312 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 return -EINVAL;
1314
1315 return 0;
1316}
1317
1318static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1319{
1320 int ret = 0;
1321 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001322 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001324 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325
Tejun Heo873fe092013-04-14 20:15:26 -07001326 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1327 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1328 return -EINVAL;
1329 }
1330
Paul Menagebd89aab2007-10-18 23:40:44 -07001331 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001332 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001333 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001334
1335 /* See what subsystems are wanted */
1336 ret = parse_cgroupfs_options(data, &opts);
1337 if (ret)
1338 goto out_unlock;
1339
Tejun Heoa8a648c2013-06-24 15:21:47 -07001340 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001341 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1342 task_tgid_nr(current), current->comm);
1343
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001344 added_mask = opts.subsys_mask & ~root->subsys_mask;
1345 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001346
Ben Blumcf5d5942010-03-10 15:22:09 -08001347 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001348 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001349 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001350 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1351 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1352 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001353 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001354 goto out_unlock;
1355 }
1356
Tejun Heof172e672013-06-28 17:07:30 -07001357 /* remounting is not allowed for populated hierarchies */
1358 if (root->number_of_cgroups > 1) {
1359 ret = -EBUSY;
1360 goto out_unlock;
1361 }
1362
Tejun Heoa8a648c2013-06-24 15:21:47 -07001363 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001364 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001365 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366
Paul Menage81a6a5c2007-10-18 23:39:38 -07001367 if (opts.release_agent)
1368 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001370 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001371 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001372 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001373 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001374 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001375 return ret;
1376}
1377
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001378static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001379 .statfs = simple_statfs,
1380 .drop_inode = generic_delete_inode,
1381 .show_options = cgroup_show_options,
1382 .remount_fs = cgroup_remount,
1383};
1384
Paul Menagecc31edc2008-10-18 20:28:04 -07001385static void init_cgroup_housekeeping(struct cgroup *cgrp)
1386{
1387 INIT_LIST_HEAD(&cgrp->sibling);
1388 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001389 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001390 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001391 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001392 INIT_LIST_HEAD(&cgrp->pidlists);
1393 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001394 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001395 INIT_LIST_HEAD(&cgrp->event_list);
1396 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001397 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001398}
Paul Menagec6d57f32009-09-23 15:56:19 -07001399
Paul Menageddbcc7e2007-10-18 23:39:30 -07001400static void init_cgroup_root(struct cgroupfs_root *root)
1401{
Paul Menagebd89aab2007-10-18 23:40:44 -07001402 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001403
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404 INIT_LIST_HEAD(&root->subsys_list);
1405 INIT_LIST_HEAD(&root->root_list);
1406 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001407 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001408 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001409 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001410 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001411}
1412
Tejun Heofc76df72013-06-25 11:53:37 -07001413static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001414{
Tejun Heo1a574232013-04-14 11:36:58 -07001415 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001416
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001417 lockdep_assert_held(&cgroup_mutex);
1418 lockdep_assert_held(&cgroup_root_mutex);
1419
Tejun Heofc76df72013-06-25 11:53:37 -07001420 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1421 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001422 if (id < 0)
1423 return id;
1424
1425 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001426 return 0;
1427}
1428
1429static void cgroup_exit_root_id(struct cgroupfs_root *root)
1430{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001431 lockdep_assert_held(&cgroup_mutex);
1432 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001433
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001434 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001435 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001436 root->hierarchy_id = 0;
1437 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001438}
1439
Paul Menageddbcc7e2007-10-18 23:39:30 -07001440static int cgroup_test_super(struct super_block *sb, void *data)
1441{
Paul Menagec6d57f32009-09-23 15:56:19 -07001442 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001443 struct cgroupfs_root *root = sb->s_fs_info;
1444
Paul Menagec6d57f32009-09-23 15:56:19 -07001445 /* If we asked for a name then it must match */
1446 if (opts->name && strcmp(opts->name, root->name))
1447 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001448
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001449 /*
1450 * If we asked for subsystems (or explicitly for no
1451 * subsystems) then they must match
1452 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001453 if ((opts->subsys_mask || opts->none)
1454 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455 return 0;
1456
1457 return 1;
1458}
1459
Paul Menagec6d57f32009-09-23 15:56:19 -07001460static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1461{
1462 struct cgroupfs_root *root;
1463
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001464 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001465 return NULL;
1466
1467 root = kzalloc(sizeof(*root), GFP_KERNEL);
1468 if (!root)
1469 return ERR_PTR(-ENOMEM);
1470
1471 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001472
Tejun Heo1672d042013-06-25 18:04:54 -07001473 /*
1474 * We need to set @root->subsys_mask now so that @root can be
1475 * matched by cgroup_test_super() before it finishes
1476 * initialization; otherwise, competing mounts with the same
1477 * options may try to bind the same subsystems instead of waiting
1478 * for the first one leading to unexpected mount errors.
1479 * SUBSYS_BOUND will be set once actual binding is complete.
1480 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001481 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 root->flags = opts->flags;
1483 if (opts->release_agent)
1484 strcpy(root->release_agent_path, opts->release_agent);
1485 if (opts->name)
1486 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001487 if (opts->cpuset_clone_children)
1488 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001489 return root;
1490}
1491
Tejun Heofa3ca072013-04-14 11:36:56 -07001492static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001493{
Tejun Heofa3ca072013-04-14 11:36:56 -07001494 if (root) {
1495 /* hierarhcy ID shoulid already have been released */
1496 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001497
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001498 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001499 kfree(root);
1500 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001501}
1502
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503static int cgroup_set_super(struct super_block *sb, void *data)
1504{
1505 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001506 struct cgroup_sb_opts *opts = data;
1507
1508 /* If we don't have a new root, we can't set up a new sb */
1509 if (!opts->new_root)
1510 return -EINVAL;
1511
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001512 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513
1514 ret = set_anon_super(sb, NULL);
1515 if (ret)
1516 return ret;
1517
Paul Menagec6d57f32009-09-23 15:56:19 -07001518 sb->s_fs_info = opts->new_root;
1519 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520
1521 sb->s_blocksize = PAGE_CACHE_SIZE;
1522 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1523 sb->s_magic = CGROUP_SUPER_MAGIC;
1524 sb->s_op = &cgroup_ops;
1525
1526 return 0;
1527}
1528
1529static int cgroup_get_rootdir(struct super_block *sb)
1530{
Al Viro0df6a632010-12-21 13:29:29 -05001531 static const struct dentry_operations cgroup_dops = {
1532 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001533 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001534 };
1535
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536 struct inode *inode =
1537 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538
1539 if (!inode)
1540 return -ENOMEM;
1541
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542 inode->i_fop = &simple_dir_operations;
1543 inode->i_op = &cgroup_dir_inode_operations;
1544 /* directories start off with i_nlink == 2 (for "." entry) */
1545 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001546 sb->s_root = d_make_root(inode);
1547 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001549 /* for everything else we want ->d_op set */
1550 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 return 0;
1552}
1553
Al Virof7e83572010-07-26 13:23:11 +04001554static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001556 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557{
1558 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001559 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 int ret = 0;
1561 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001562 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001563 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001564 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001565 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566
1567 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001568 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001570 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001571 if (ret)
1572 goto out_err;
1573
1574 /*
1575 * Allocate a new cgroup root. We may not need it if we're
1576 * reusing an existing hierarchy.
1577 */
1578 new_root = cgroup_root_from_opts(&opts);
1579 if (IS_ERR(new_root)) {
1580 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001581 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001582 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584
Paul Menagec6d57f32009-09-23 15:56:19 -07001585 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001586 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001588 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001589 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001590 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591 }
1592
Paul Menagec6d57f32009-09-23 15:56:19 -07001593 root = sb->s_fs_info;
1594 BUG_ON(!root);
1595 if (root == opts.new_root) {
1596 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001597 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001598 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001599 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001600 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001601
1602 BUG_ON(sb->s_root != NULL);
1603
1604 ret = cgroup_get_rootdir(sb);
1605 if (ret)
1606 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001607 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608
Paul Menage817929e2007-10-18 23:39:36 -07001609 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001611 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001613 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1614 0, 1, GFP_KERNEL);
1615 if (root_cgrp->id < 0)
1616 goto unlock_drop;
1617
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001618 /* Check for name clashes with existing mounts */
1619 ret = -EBUSY;
1620 if (strlen(root->name))
1621 for_each_active_root(existing_root)
1622 if (!strcmp(existing_root->name, root->name))
1623 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001624
Paul Menage817929e2007-10-18 23:39:36 -07001625 /*
1626 * We're accessing css_set_count without locking
1627 * css_set_lock here, but that's OK - it can only be
1628 * increased by someone holding cgroup_lock, and
1629 * that's us. The worst that can happen is that we
1630 * have some link structures left over
1631 */
Tejun Heo69d02062013-06-12 21:04:50 -07001632 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001633 if (ret)
1634 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001635
Tejun Heofc76df72013-06-25 11:53:37 -07001636 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1637 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001638 if (ret)
1639 goto unlock_drop;
1640
Tejun Heo31261212013-06-28 17:07:30 -07001641 sb->s_root->d_fsdata = root_cgrp;
1642 root_cgrp->dentry = sb->s_root;
1643
1644 /*
1645 * We're inside get_sb() and will call lookup_one_len() to
1646 * create the root files, which doesn't work if SELinux is
1647 * in use. The following cred dancing somehow works around
1648 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1649 * populating new cgroupfs mount") for more details.
1650 */
1651 cred = override_creds(&init_cred);
1652
Tejun Heo2bb566c2013-08-08 20:11:23 -04001653 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001654 if (ret)
1655 goto rm_base_files;
1656
Tejun Heoa8a648c2013-06-24 15:21:47 -07001657 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001658 if (ret)
1659 goto rm_base_files;
1660
1661 revert_creds(cred);
1662
Ben Blumcf5d5942010-03-10 15:22:09 -08001663 /*
1664 * There must be no failure case after here, since rebinding
1665 * takes care of subsystems' refcounts, which are explicitly
1666 * dropped in the failure exit path.
1667 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668
Tejun Heo9871bf92013-06-24 15:21:47 -07001669 list_add(&root->root_list, &cgroup_roots);
1670 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671
Paul Menage817929e2007-10-18 23:39:36 -07001672 /* Link the top cgroup in this hierarchy into all
1673 * the css_set objects */
1674 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001675 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001676 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001677 write_unlock(&css_set_lock);
1678
Tejun Heo69d02062013-06-12 21:04:50 -07001679 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001680
Li Zefanc12f65d2009-01-07 18:07:42 -08001681 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682 BUG_ON(root->number_of_cgroups != 1);
1683
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001684 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001685 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001686 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001687 } else {
1688 /*
1689 * We re-used an existing hierarchy - the new root (if
1690 * any) is not needed
1691 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001692 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001693
Tejun Heoc7ba8282013-06-29 14:06:10 -07001694 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001695 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1696 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1697 ret = -EINVAL;
1698 goto drop_new_super;
1699 } else {
1700 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1701 }
Tejun Heo873fe092013-04-14 20:15:26 -07001702 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703 }
1704
Paul Menagec6d57f32009-09-23 15:56:19 -07001705 kfree(opts.release_agent);
1706 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001707 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708
Tejun Heo31261212013-06-28 17:07:30 -07001709 rm_base_files:
1710 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001711 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001712 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001713 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001714 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001715 mutex_unlock(&cgroup_root_mutex);
1716 mutex_unlock(&cgroup_mutex);
1717 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001719 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001720 out_err:
1721 kfree(opts.release_agent);
1722 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001723 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724}
1725
1726static void cgroup_kill_sb(struct super_block *sb) {
1727 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001728 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001729 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730 int ret;
1731
1732 BUG_ON(!root);
1733
1734 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001735 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736
Tejun Heo31261212013-06-28 17:07:30 -07001737 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001739 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740
1741 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001742 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1743 ret = rebind_subsystems(root, 0, root->subsys_mask);
1744 /* Shouldn't be able to fail ... */
1745 BUG_ON(ret);
1746 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747
Paul Menage817929e2007-10-18 23:39:36 -07001748 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001749 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001750 * root cgroup
1751 */
1752 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001753
Tejun Heo69d02062013-06-12 21:04:50 -07001754 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1755 list_del(&link->cset_link);
1756 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001757 kfree(link);
1758 }
1759 write_unlock(&css_set_lock);
1760
Paul Menage839ec542009-01-29 14:25:22 -08001761 if (!list_empty(&root->root_list)) {
1762 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001763 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001764 }
Li Zefane5f6a862009-01-07 18:07:41 -08001765
Tejun Heofa3ca072013-04-14 11:36:56 -07001766 cgroup_exit_root_id(root);
1767
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001768 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001770 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001772 simple_xattrs_free(&cgrp->xattrs);
1773
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001775 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776}
1777
1778static struct file_system_type cgroup_fs_type = {
1779 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001780 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 .kill_sb = cgroup_kill_sb,
1782};
1783
Greg KH676db4a2010-08-05 13:53:35 -07001784static struct kobject *cgroup_kobj;
1785
Li Zefana043e3b2008-02-23 15:24:09 -08001786/**
1787 * cgroup_path - generate the path of a cgroup
1788 * @cgrp: the cgroup in question
1789 * @buf: the buffer to write the path into
1790 * @buflen: the length of the buffer
1791 *
Li Zefan65dff752013-03-01 15:01:56 +08001792 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1793 *
1794 * We can't generate cgroup path using dentry->d_name, as accessing
1795 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1796 * inode's i_mutex, while on the other hand cgroup_path() can be called
1797 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001799int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800{
Li Zefan65dff752013-03-01 15:01:56 +08001801 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001802 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001803
Tejun Heoda1f2962013-04-14 10:32:19 -07001804 if (!cgrp->parent) {
1805 if (strlcpy(buf, "/", buflen) >= buflen)
1806 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001807 return 0;
1808 }
1809
Tao Ma316eb662012-11-08 21:36:38 +08001810 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001811 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001812
Li Zefan65dff752013-03-01 15:01:56 +08001813 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001814 do {
Li Zefan65dff752013-03-01 15:01:56 +08001815 const char *name = cgroup_name(cgrp);
1816 int len;
1817
1818 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001819 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001820 goto out;
1821 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001822
Paul Menageddbcc7e2007-10-18 23:39:30 -07001823 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001824 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001825 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001826
1827 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001828 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001829 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001830 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001831out:
1832 rcu_read_unlock();
1833 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001834}
Ben Blum67523c42010-03-10 15:22:11 -08001835EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001836
Tejun Heo857a2be2013-04-14 20:50:08 -07001837/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001838 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001839 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001840 * @buf: the buffer to write the path into
1841 * @buflen: the length of the buffer
1842 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001843 * Determine @task's cgroup on the first (the one with the lowest non-zero
1844 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1845 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1846 * cgroup controller callbacks.
1847 *
1848 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001849 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001850int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001851{
1852 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001853 struct cgroup *cgrp;
1854 int hierarchy_id = 1, ret = 0;
1855
1856 if (buflen < 2)
1857 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001858
1859 mutex_lock(&cgroup_mutex);
1860
Tejun Heo913ffdb2013-07-11 16:34:48 -07001861 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1862
Tejun Heo857a2be2013-04-14 20:50:08 -07001863 if (root) {
1864 cgrp = task_cgroup_from_root(task, root);
1865 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001866 } else {
1867 /* if no hierarchy exists, everyone is in "/" */
1868 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001869 }
1870
1871 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001872 return ret;
1873}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001874EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001875
Ben Blum74a11662011-05-26 16:25:20 -07001876/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001877 * Control Group taskset
1878 */
Tejun Heo134d3372011-12-12 18:12:21 -08001879struct task_and_cgroup {
1880 struct task_struct *task;
1881 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001882 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001883};
1884
Tejun Heo2f7ee562011-12-12 18:12:21 -08001885struct cgroup_taskset {
1886 struct task_and_cgroup single;
1887 struct flex_array *tc_array;
1888 int tc_array_len;
1889 int idx;
1890 struct cgroup *cur_cgrp;
1891};
1892
1893/**
1894 * cgroup_taskset_first - reset taskset and return the first task
1895 * @tset: taskset of interest
1896 *
1897 * @tset iteration is initialized and the first task is returned.
1898 */
1899struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1900{
1901 if (tset->tc_array) {
1902 tset->idx = 0;
1903 return cgroup_taskset_next(tset);
1904 } else {
1905 tset->cur_cgrp = tset->single.cgrp;
1906 return tset->single.task;
1907 }
1908}
1909EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1910
1911/**
1912 * cgroup_taskset_next - iterate to the next task in taskset
1913 * @tset: taskset of interest
1914 *
1915 * Return the next task in @tset. Iteration must have been initialized
1916 * with cgroup_taskset_first().
1917 */
1918struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1919{
1920 struct task_and_cgroup *tc;
1921
1922 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1923 return NULL;
1924
1925 tc = flex_array_get(tset->tc_array, tset->idx++);
1926 tset->cur_cgrp = tc->cgrp;
1927 return tc->task;
1928}
1929EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1930
1931/**
Tejun Heod99c8722013-08-08 20:11:27 -04001932 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001933 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001934 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001935 *
Tejun Heod99c8722013-08-08 20:11:27 -04001936 * Return the css for the current (last returned) task of @tset for
1937 * subsystem specified by @subsys_id. This function must be preceded by
1938 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001939 */
Tejun Heod99c8722013-08-08 20:11:27 -04001940struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1941 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001942{
Tejun Heod99c8722013-08-08 20:11:27 -04001943 return cgroup_css(tset->cur_cgrp, subsys_id);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001944}
Tejun Heod99c8722013-08-08 20:11:27 -04001945EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001946
1947/**
1948 * cgroup_taskset_size - return the number of tasks in taskset
1949 * @tset: taskset of interest
1950 */
1951int cgroup_taskset_size(struct cgroup_taskset *tset)
1952{
1953 return tset->tc_array ? tset->tc_array_len : 1;
1954}
1955EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1956
1957
Ben Blum74a11662011-05-26 16:25:20 -07001958/*
1959 * cgroup_task_migrate - move a task from one cgroup to another.
1960 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001961 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001962 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001963static void cgroup_task_migrate(struct cgroup *old_cgrp,
1964 struct task_struct *tsk,
1965 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001966{
Tejun Heo5abb8852013-06-12 21:04:49 -07001967 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001968
1969 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001970 * We are synchronized through threadgroup_lock() against PF_EXITING
1971 * setting such that we can't race against cgroup_exit() changing the
1972 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001973 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001974 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001975 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001976
Ben Blum74a11662011-05-26 16:25:20 -07001977 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001978 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001979 task_unlock(tsk);
1980
1981 /* Update the css_set linked lists if we're using them */
1982 write_lock(&css_set_lock);
1983 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001984 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001985 write_unlock(&css_set_lock);
1986
1987 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001988 * We just gained a reference on old_cset by taking it from the
1989 * task. As trading it for new_cset is protected by cgroup_mutex,
1990 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001991 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001992 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1993 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001994}
1995
Li Zefana043e3b2008-02-23 15:24:09 -08001996/**
Li Zefan081aa452013-03-13 09:17:09 +08001997 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001998 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001999 * @tsk: the task or the leader of the threadgroup to be attached
2000 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07002001 *
Tejun Heo257058a2011-12-12 18:12:21 -08002002 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08002003 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002004 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07002005static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2006 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002007{
2008 int retval, i, group_size;
2009 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002010 struct cgroupfs_root *root = cgrp->root;
2011 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002012 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002013 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002014 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002015 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002016
2017 /*
2018 * step 0: in order to do expensive, possibly blocking operations for
2019 * every thread, we cannot iterate the thread group list, since it needs
2020 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002021 * group - group_rwsem prevents new threads from appearing, and if
2022 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002023 */
Li Zefan081aa452013-03-13 09:17:09 +08002024 if (threadgroup)
2025 group_size = get_nr_threads(tsk);
2026 else
2027 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002028 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002029 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002030 if (!group)
2031 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002032 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002033 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002034 if (retval)
2035 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002036
Ben Blum74a11662011-05-26 16:25:20 -07002037 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002038 /*
2039 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2040 * already PF_EXITING could be freed from underneath us unless we
2041 * take an rcu_read_lock.
2042 */
2043 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002044 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002045 struct task_and_cgroup ent;
2046
Tejun Heocd3d0952011-12-12 18:12:21 -08002047 /* @tsk either already exited or can't exit until the end */
2048 if (tsk->flags & PF_EXITING)
2049 continue;
2050
Ben Blum74a11662011-05-26 16:25:20 -07002051 /* as per above, nr_threads may decrease, but not increase. */
2052 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002053 ent.task = tsk;
2054 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002055 /* nothing to do if this task is already in the cgroup */
2056 if (ent.cgrp == cgrp)
2057 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002058 /*
2059 * saying GFP_ATOMIC has no effect here because we did prealloc
2060 * earlier, but it's good form to communicate our expectations.
2061 */
Tejun Heo134d3372011-12-12 18:12:21 -08002062 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002063 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002064 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002065
2066 if (!threadgroup)
2067 break;
Ben Blum74a11662011-05-26 16:25:20 -07002068 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002069 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002070 /* remember the number of threads in the array for later. */
2071 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002072 tset.tc_array = group;
2073 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002074
Tejun Heo134d3372011-12-12 18:12:21 -08002075 /* methods shouldn't be called if no task is actually migrating */
2076 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002077 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002078 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002079
Ben Blum74a11662011-05-26 16:25:20 -07002080 /*
2081 * step 1: check that we can legitimately attach to the cgroup.
2082 */
Tejun Heo5549c492013-06-24 15:21:48 -07002083 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04002084 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -04002085
Ben Blum74a11662011-05-26 16:25:20 -07002086 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002087 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002088 if (retval) {
2089 failed_ss = ss;
2090 goto out_cancel_attach;
2091 }
2092 }
Ben Blum74a11662011-05-26 16:25:20 -07002093 }
2094
2095 /*
2096 * step 2: make sure css_sets exist for all threads to be migrated.
2097 * we use find_css_set, which allocates a new one if necessary.
2098 */
Ben Blum74a11662011-05-26 16:25:20 -07002099 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002100 struct css_set *old_cset;
2101
Tejun Heo134d3372011-12-12 18:12:21 -08002102 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002103 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002104 tc->cset = find_css_set(old_cset, cgrp);
2105 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002106 retval = -ENOMEM;
2107 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002108 }
2109 }
2110
2111 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002112 * step 3: now that we're guaranteed success wrt the css_sets,
2113 * proceed to move all tasks to the new cgroup. There are no
2114 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002115 */
Ben Blum74a11662011-05-26 16:25:20 -07002116 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002117 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002118 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002119 }
2120 /* nothing is sensitive to fork() after this point. */
2121
2122 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002123 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002124 */
Tejun Heo5549c492013-06-24 15:21:48 -07002125 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04002126 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -04002127
Ben Blum74a11662011-05-26 16:25:20 -07002128 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002129 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002130 }
2131
2132 /*
2133 * step 5: success! and cleanup
2134 */
Ben Blum74a11662011-05-26 16:25:20 -07002135 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002136out_put_css_set_refs:
2137 if (retval) {
2138 for (i = 0; i < group_size; i++) {
2139 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002140 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002141 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002142 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002143 }
Ben Blum74a11662011-05-26 16:25:20 -07002144 }
2145out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002146 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002147 for_each_root_subsys(root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04002148 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoeb954192013-08-08 20:11:23 -04002149
Tejun Heo494c1672011-12-12 18:12:22 -08002150 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002151 break;
Ben Blum74a11662011-05-26 16:25:20 -07002152 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002153 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002154 }
2155 }
Ben Blum74a11662011-05-26 16:25:20 -07002156out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002157 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002158 return retval;
2159}
2160
2161/*
2162 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002163 * function to attach either it or all tasks in its threadgroup. Will lock
2164 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002165 */
2166static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002167{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002168 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002169 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002170 int ret;
2171
Ben Blum74a11662011-05-26 16:25:20 -07002172 if (!cgroup_lock_live_group(cgrp))
2173 return -ENODEV;
2174
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002175retry_find_task:
2176 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002177 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002178 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002179 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002180 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002181 ret= -ESRCH;
2182 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002183 }
Ben Blum74a11662011-05-26 16:25:20 -07002184 /*
2185 * even if we're attaching all tasks in the thread group, we
2186 * only need to check permissions on one of them.
2187 */
David Howellsc69e8d92008-11-14 10:39:19 +11002188 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002189 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2190 !uid_eq(cred->euid, tcred->uid) &&
2191 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002192 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002193 ret = -EACCES;
2194 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002195 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002196 } else
2197 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002198
2199 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002200 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002201
2202 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002203 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002204 * trapped in a cpuset, or RT worker may be born in a cgroup
2205 * with no rt_runtime allocated. Just say no.
2206 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002207 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002208 ret = -EINVAL;
2209 rcu_read_unlock();
2210 goto out_unlock_cgroup;
2211 }
2212
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002213 get_task_struct(tsk);
2214 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002215
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002216 threadgroup_lock(tsk);
2217 if (threadgroup) {
2218 if (!thread_group_leader(tsk)) {
2219 /*
2220 * a race with de_thread from another thread's exec()
2221 * may strip us of our leadership, if this happens,
2222 * there is no choice but to throw this task away and
2223 * try again; this is
2224 * "double-double-toil-and-trouble-check locking".
2225 */
2226 threadgroup_unlock(tsk);
2227 put_task_struct(tsk);
2228 goto retry_find_task;
2229 }
Li Zefan081aa452013-03-13 09:17:09 +08002230 }
2231
2232 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2233
Tejun Heocd3d0952011-12-12 18:12:21 -08002234 threadgroup_unlock(tsk);
2235
Paul Menagebbcb81d2007-10-18 23:39:32 -07002236 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002237out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002238 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002239 return ret;
2240}
2241
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002242/**
2243 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2244 * @from: attach to all cgroups of a given task
2245 * @tsk: the task to be attached
2246 */
2247int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2248{
2249 struct cgroupfs_root *root;
2250 int retval = 0;
2251
Tejun Heo47cfcd02013-04-07 09:29:51 -07002252 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002253 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002254 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002255
Li Zefan6f4b7e62013-07-31 16:18:36 +08002256 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002257 if (retval)
2258 break;
2259 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002260 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002261
2262 return retval;
2263}
2264EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2265
Tejun Heo182446d2013-08-08 20:11:24 -04002266static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2267 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002268{
Tejun Heo182446d2013-08-08 20:11:24 -04002269 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002270}
2271
Tejun Heo182446d2013-08-08 20:11:24 -04002272static int cgroup_procs_write(struct cgroup_subsys_state *css,
2273 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002274{
Tejun Heo182446d2013-08-08 20:11:24 -04002275 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002276}
2277
Tejun Heo182446d2013-08-08 20:11:24 -04002278static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2279 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002280{
Tejun Heo182446d2013-08-08 20:11:24 -04002281 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002282 if (strlen(buffer) >= PATH_MAX)
2283 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002284 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002285 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002286 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002287 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002288 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002289 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002290 return 0;
2291}
2292
Tejun Heo182446d2013-08-08 20:11:24 -04002293static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2294 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002295{
Tejun Heo182446d2013-08-08 20:11:24 -04002296 struct cgroup *cgrp = css->cgroup;
2297
Paul Menagee788e062008-07-25 01:46:59 -07002298 if (!cgroup_lock_live_group(cgrp))
2299 return -ENODEV;
2300 seq_puts(seq, cgrp->root->release_agent_path);
2301 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002302 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002303 return 0;
2304}
2305
Tejun Heo182446d2013-08-08 20:11:24 -04002306static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2307 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002308{
Tejun Heo182446d2013-08-08 20:11:24 -04002309 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002310 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002311}
2312
Paul Menage84eea842008-07-25 01:47:00 -07002313/* A buffer size big enough for numbers or short strings */
2314#define CGROUP_LOCAL_BUFFER_SIZE 64
2315
Tejun Heo182446d2013-08-08 20:11:24 -04002316static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2317 struct cftype *cft, struct file *file,
2318 const char __user *userbuf, size_t nbytes,
2319 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002320{
Paul Menage84eea842008-07-25 01:47:00 -07002321 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002322 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002323 char *end;
2324
2325 if (!nbytes)
2326 return -EINVAL;
2327 if (nbytes >= sizeof(buffer))
2328 return -E2BIG;
2329 if (copy_from_user(buffer, userbuf, nbytes))
2330 return -EFAULT;
2331
2332 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002333 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002334 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002335 if (*end)
2336 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002337 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002338 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002339 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002340 if (*end)
2341 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002342 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002343 }
Paul Menage355e0c42007-10-18 23:39:33 -07002344 if (!retval)
2345 retval = nbytes;
2346 return retval;
2347}
2348
Tejun Heo182446d2013-08-08 20:11:24 -04002349static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2350 struct cftype *cft, struct file *file,
2351 const char __user *userbuf, size_t nbytes,
2352 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002353{
Paul Menage84eea842008-07-25 01:47:00 -07002354 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002355 int retval = 0;
2356 size_t max_bytes = cft->max_write_len;
2357 char *buffer = local_buffer;
2358
2359 if (!max_bytes)
2360 max_bytes = sizeof(local_buffer) - 1;
2361 if (nbytes >= max_bytes)
2362 return -E2BIG;
2363 /* Allocate a dynamic buffer if we need one */
2364 if (nbytes >= sizeof(local_buffer)) {
2365 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2366 if (buffer == NULL)
2367 return -ENOMEM;
2368 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002369 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2370 retval = -EFAULT;
2371 goto out;
2372 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002373
2374 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002375 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002376 if (!retval)
2377 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002378out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002379 if (buffer != local_buffer)
2380 kfree(buffer);
2381 return retval;
2382}
2383
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002385 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002386{
Tejun Heo182446d2013-08-08 20:11:24 -04002387 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002388 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002389 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390
Paul Menage355e0c42007-10-18 23:39:33 -07002391 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002392 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002393 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002394 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002395 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002396 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002397 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002398 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002399 return ret ? ret : nbytes;
2400 }
Paul Menage355e0c42007-10-18 23:39:33 -07002401 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002402}
2403
Tejun Heo182446d2013-08-08 20:11:24 -04002404static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2405 struct cftype *cft, struct file *file,
2406 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002407{
Paul Menage84eea842008-07-25 01:47:00 -07002408 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002409 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002410 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2411
2412 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2413}
2414
Tejun Heo182446d2013-08-08 20:11:24 -04002415static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2416 struct cftype *cft, struct file *file,
2417 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002418{
Paul Menage84eea842008-07-25 01:47:00 -07002419 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002420 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002421 int len = sprintf(tmp, "%lld\n", (long long) val);
2422
2423 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2424}
2425
Paul Menageddbcc7e2007-10-18 23:39:30 -07002426static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002427 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428{
Tejun Heo182446d2013-08-08 20:11:24 -04002429 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002431 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002434 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002435 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002436 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002437 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002438 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002439 return -EINVAL;
2440}
2441
Paul Menage91796562008-04-29 01:00:01 -07002442/*
2443 * seqfile ops/methods for returning structured data. Currently just
2444 * supports string->u64 maps, but can be extended in future.
2445 */
2446
Paul Menage91796562008-04-29 01:00:01 -07002447static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2448{
2449 struct seq_file *sf = cb->state;
2450 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2451}
2452
2453static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2454{
Li Zefane0798ce2013-07-31 17:36:25 +08002455 struct cfent *cfe = m->private;
2456 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002457 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002458
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002459 if (cft->read_map) {
2460 struct cgroup_map_cb cb = {
2461 .fill = cgroup_map_add,
2462 .state = m,
2463 };
Tejun Heo182446d2013-08-08 20:11:24 -04002464 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002465 }
Tejun Heo182446d2013-08-08 20:11:24 -04002466 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002467}
2468
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002469static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002470 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002471 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002472 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002473 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002474};
2475
Paul Menageddbcc7e2007-10-18 23:39:30 -07002476static int cgroup_file_open(struct inode *inode, struct file *file)
2477{
Tejun Heof7d58812013-08-08 20:11:23 -04002478 struct cfent *cfe = __d_cfe(file->f_dentry);
2479 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002480 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2481 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002482 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483
2484 err = generic_file_open(inode, file);
2485 if (err)
2486 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002487
2488 /*
2489 * If the file belongs to a subsystem, pin the css. Will be
2490 * unpinned either on open failure or release. This ensures that
2491 * @css stays alive for all file operations.
2492 */
Tejun Heo105347b2013-08-13 11:01:55 -04002493 rcu_read_lock();
2494 if (cft->ss) {
2495 css = cgroup_css(cgrp, cft->ss->subsys_id);
2496 if (!css_tryget(css))
2497 css = NULL;
2498 } else {
2499 css = &cgrp->dummy_css;
2500 }
2501 rcu_read_unlock();
2502
2503 /* css should match @cfe->css, see cgroup_add_file() for details */
2504 if (!css || WARN_ON_ONCE(css != cfe->css))
Tejun Heof7d58812013-08-08 20:11:23 -04002505 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002506
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002507 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002508 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002509 err = single_open(file, cgroup_seqfile_show, cfe);
2510 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002511 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002512 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002513
Tejun Heo67f4c362013-08-08 20:11:24 -04002514 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002515 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002516 return err;
2517}
2518
2519static int cgroup_file_release(struct inode *inode, struct file *file)
2520{
Tejun Heof7d58812013-08-08 20:11:23 -04002521 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002522 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002523 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002524 int ret = 0;
2525
Paul Menageddbcc7e2007-10-18 23:39:30 -07002526 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002527 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002528 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002529 css_put(css);
2530 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002531}
2532
2533/*
2534 * cgroup_rename - Only allow simple rename of directories in place.
2535 */
2536static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2537 struct inode *new_dir, struct dentry *new_dentry)
2538{
Li Zefan65dff752013-03-01 15:01:56 +08002539 int ret;
2540 struct cgroup_name *name, *old_name;
2541 struct cgroup *cgrp;
2542
2543 /*
2544 * It's convinient to use parent dir's i_mutex to protected
2545 * cgrp->name.
2546 */
2547 lockdep_assert_held(&old_dir->i_mutex);
2548
Paul Menageddbcc7e2007-10-18 23:39:30 -07002549 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2550 return -ENOTDIR;
2551 if (new_dentry->d_inode)
2552 return -EEXIST;
2553 if (old_dir != new_dir)
2554 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002555
2556 cgrp = __d_cgrp(old_dentry);
2557
Tejun Heo6db8e852013-06-14 11:18:22 -07002558 /*
2559 * This isn't a proper migration and its usefulness is very
2560 * limited. Disallow if sane_behavior.
2561 */
2562 if (cgroup_sane_behavior(cgrp))
2563 return -EPERM;
2564
Li Zefan65dff752013-03-01 15:01:56 +08002565 name = cgroup_alloc_name(new_dentry);
2566 if (!name)
2567 return -ENOMEM;
2568
2569 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2570 if (ret) {
2571 kfree(name);
2572 return ret;
2573 }
2574
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002575 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002576 rcu_assign_pointer(cgrp->name, name);
2577
2578 kfree_rcu(old_name, rcu_head);
2579 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002580}
2581
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002582static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2583{
2584 if (S_ISDIR(dentry->d_inode->i_mode))
2585 return &__d_cgrp(dentry)->xattrs;
2586 else
Li Zefan712317a2013-04-18 23:09:52 -07002587 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002588}
2589
2590static inline int xattr_enabled(struct dentry *dentry)
2591{
2592 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002593 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002594}
2595
2596static bool is_valid_xattr(const char *name)
2597{
2598 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2599 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2600 return true;
2601 return false;
2602}
2603
2604static int cgroup_setxattr(struct dentry *dentry, const char *name,
2605 const void *val, size_t size, int flags)
2606{
2607 if (!xattr_enabled(dentry))
2608 return -EOPNOTSUPP;
2609 if (!is_valid_xattr(name))
2610 return -EINVAL;
2611 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2612}
2613
2614static int cgroup_removexattr(struct dentry *dentry, const char *name)
2615{
2616 if (!xattr_enabled(dentry))
2617 return -EOPNOTSUPP;
2618 if (!is_valid_xattr(name))
2619 return -EINVAL;
2620 return simple_xattr_remove(__d_xattrs(dentry), name);
2621}
2622
2623static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2624 void *buf, size_t size)
2625{
2626 if (!xattr_enabled(dentry))
2627 return -EOPNOTSUPP;
2628 if (!is_valid_xattr(name))
2629 return -EINVAL;
2630 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2631}
2632
2633static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2634{
2635 if (!xattr_enabled(dentry))
2636 return -EOPNOTSUPP;
2637 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2638}
2639
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002640static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002641 .read = cgroup_file_read,
2642 .write = cgroup_file_write,
2643 .llseek = generic_file_llseek,
2644 .open = cgroup_file_open,
2645 .release = cgroup_file_release,
2646};
2647
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002648static const struct inode_operations cgroup_file_inode_operations = {
2649 .setxattr = cgroup_setxattr,
2650 .getxattr = cgroup_getxattr,
2651 .listxattr = cgroup_listxattr,
2652 .removexattr = cgroup_removexattr,
2653};
2654
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002655static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002656 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002657 .mkdir = cgroup_mkdir,
2658 .rmdir = cgroup_rmdir,
2659 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002660 .setxattr = cgroup_setxattr,
2661 .getxattr = cgroup_getxattr,
2662 .listxattr = cgroup_listxattr,
2663 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002664};
2665
Al Viro00cd8dd2012-06-10 17:13:09 -04002666static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002667{
2668 if (dentry->d_name.len > NAME_MAX)
2669 return ERR_PTR(-ENAMETOOLONG);
2670 d_add(dentry, NULL);
2671 return NULL;
2672}
2673
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002674/*
2675 * Check if a file is a control file
2676 */
2677static inline struct cftype *__file_cft(struct file *file)
2678{
Al Viro496ad9a2013-01-23 17:07:38 -05002679 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002680 return ERR_PTR(-EINVAL);
2681 return __d_cft(file->f_dentry);
2682}
2683
Al Viroa5e7ed32011-07-26 01:55:55 -04002684static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002685 struct super_block *sb)
2686{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002687 struct inode *inode;
2688
2689 if (!dentry)
2690 return -ENOENT;
2691 if (dentry->d_inode)
2692 return -EEXIST;
2693
2694 inode = cgroup_new_inode(mode, sb);
2695 if (!inode)
2696 return -ENOMEM;
2697
2698 if (S_ISDIR(mode)) {
2699 inode->i_op = &cgroup_dir_inode_operations;
2700 inode->i_fop = &simple_dir_operations;
2701
2702 /* start off with i_nlink == 2 (for "." entry) */
2703 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002704 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002705
Tejun Heob8a2df62012-11-19 08:13:37 -08002706 /*
2707 * Control reaches here with cgroup_mutex held.
2708 * @inode->i_mutex should nest outside cgroup_mutex but we
2709 * want to populate it immediately without releasing
2710 * cgroup_mutex. As @inode isn't visible to anyone else
2711 * yet, trylock will always succeed without affecting
2712 * lockdep checks.
2713 */
2714 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715 } else if (S_ISREG(mode)) {
2716 inode->i_size = 0;
2717 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002718 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002720 d_instantiate(dentry, inode);
2721 dget(dentry); /* Extra count - pin the dentry in core */
2722 return 0;
2723}
2724
Li Zefan099fca32009-04-02 16:57:29 -07002725/**
2726 * cgroup_file_mode - deduce file mode of a control file
2727 * @cft: the control file in question
2728 *
2729 * returns cft->mode if ->mode is not 0
2730 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2731 * returns S_IRUGO if it has only a read handler
2732 * returns S_IWUSR if it has only a write hander
2733 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002734static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002735{
Al Viroa5e7ed32011-07-26 01:55:55 -04002736 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002737
2738 if (cft->mode)
2739 return cft->mode;
2740
2741 if (cft->read || cft->read_u64 || cft->read_s64 ||
2742 cft->read_map || cft->read_seq_string)
2743 mode |= S_IRUGO;
2744
2745 if (cft->write || cft->write_u64 || cft->write_s64 ||
2746 cft->write_string || cft->trigger)
2747 mode |= S_IWUSR;
2748
2749 return mode;
2750}
2751
Tejun Heo2bb566c2013-08-08 20:11:23 -04002752static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002753{
Paul Menagebd89aab2007-10-18 23:40:44 -07002754 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002755 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002757 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002758 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002759 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002760 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002761
Tejun Heo2bb566c2013-08-08 20:11:23 -04002762 if (cft->ss && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2763 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002764 strcat(name, ".");
2765 }
2766 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002767
Paul Menageddbcc7e2007-10-18 23:39:30 -07002768 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002769
2770 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2771 if (!cfe)
2772 return -ENOMEM;
2773
Paul Menageddbcc7e2007-10-18 23:39:30 -07002774 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002775 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002776 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002777 goto out;
2778 }
2779
Li Zefand6cbf352013-05-14 19:44:20 +08002780 cfe->type = (void *)cft;
2781 cfe->dentry = dentry;
2782 dentry->d_fsdata = cfe;
2783 simple_xattrs_init(&cfe->xattrs);
2784
Tejun Heo105347b2013-08-13 11:01:55 -04002785 /*
2786 * cfe->css is used by read/write/close to determine the associated
2787 * css. file->private_data would be a better place but that's
2788 * already used by seqfile. Note that open will use the usual
2789 * cgroup_css() and css_tryget() to acquire the css and this
2790 * caching doesn't affect css lifetime management.
2791 */
2792 if (cft->ss)
2793 cfe->css = cgroup_css(cgrp, cft->ss->subsys_id);
2794 else
2795 cfe->css = &cgrp->dummy_css;
2796
Tejun Heo05ef1d72012-04-01 12:09:56 -07002797 mode = cgroup_file_mode(cft);
2798 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2799 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002800 list_add_tail(&cfe->node, &parent->files);
2801 cfe = NULL;
2802 }
2803 dput(dentry);
2804out:
2805 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002806 return error;
2807}
2808
Tejun Heob1f28d32013-06-28 16:24:10 -07002809/**
2810 * cgroup_addrm_files - add or remove files to a cgroup directory
2811 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002812 * @cfts: array of cftypes to be added
2813 * @is_add: whether to add or remove
2814 *
2815 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002816 * For removals, this function never fails. If addition fails, this
2817 * function doesn't remove files already added. The caller is responsible
2818 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002819 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002820static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2821 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002822{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002823 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002824 int ret;
2825
2826 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2827 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002828
2829 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002830 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002831 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2832 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002833 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2834 continue;
2835 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2836 continue;
2837
Li Zefan2739d3c2013-01-21 18:18:33 +08002838 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002839 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002840 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002841 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002842 cft->name, ret);
2843 return ret;
2844 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002845 } else {
2846 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002847 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002848 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002849 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002850}
2851
Tejun Heo8e3f6542012-04-01 12:09:55 -07002852static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002853 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854{
2855 /*
2856 * Thanks to the entanglement with vfs inode locking, we can't walk
2857 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002858 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2859 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002860 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002861 mutex_lock(&cgroup_mutex);
2862}
2863
Tejun Heo2bb566c2013-08-08 20:11:23 -04002864static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002865 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002866{
2867 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002868 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002869 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002870 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002871 struct dentry *prev = NULL;
2872 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002873 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002874 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002875 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002876
2877 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002878 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002879 !atomic_inc_not_zero(&sb->s_active)) {
2880 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002881 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002882 }
2883
Li Zefane8c82d22013-06-18 18:48:37 +08002884 /*
2885 * All cgroups which are created after we drop cgroup_mutex will
2886 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002887 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002888 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002889 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002890
Tejun Heo8e3f6542012-04-01 12:09:55 -07002891 mutex_unlock(&cgroup_mutex);
2892
Li Zefane8c82d22013-06-18 18:48:37 +08002893 /* add/rm files for all cgroups created before */
2894 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002895 css_for_each_descendant_pre(css, cgroup_css(root, ss->subsys_id)) {
2896 struct cgroup *cgrp = css->cgroup;
2897
Li Zefane8c82d22013-06-18 18:48:37 +08002898 if (cgroup_is_dead(cgrp))
2899 continue;
2900
2901 inode = cgrp->dentry->d_inode;
2902 dget(cgrp->dentry);
2903 rcu_read_unlock();
2904
2905 dput(prev);
2906 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002907
2908 mutex_lock(&inode->i_mutex);
2909 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002910 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002911 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002912 mutex_unlock(&cgroup_mutex);
2913 mutex_unlock(&inode->i_mutex);
2914
Li Zefane8c82d22013-06-18 18:48:37 +08002915 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002916 if (ret)
2917 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002918 }
Li Zefane8c82d22013-06-18 18:48:37 +08002919 rcu_read_unlock();
2920 dput(prev);
2921 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002922 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002923}
2924
2925/**
2926 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2927 * @ss: target cgroup subsystem
2928 * @cfts: zero-length name terminated array of cftypes
2929 *
2930 * Register @cfts to @ss. Files described by @cfts are created for all
2931 * existing cgroups to which @ss is attached and all future cgroups will
2932 * have them too. This function can be called anytime whether @ss is
2933 * attached or not.
2934 *
2935 * Returns 0 on successful registration, -errno on failure. Note that this
2936 * function currently returns 0 as long as @cfts registration is successful
2937 * even if some file creation attempts on existing cgroups fail.
2938 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002939int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002940{
2941 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002942 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002943 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002944
2945 set = kzalloc(sizeof(*set), GFP_KERNEL);
2946 if (!set)
2947 return -ENOMEM;
2948
Tejun Heo2bb566c2013-08-08 20:11:23 -04002949 for (cft = cfts; cft->name[0] != '\0'; cft++)
2950 cft->ss = ss;
2951
Tejun Heo8e3f6542012-04-01 12:09:55 -07002952 cgroup_cfts_prepare();
2953 set->cfts = cfts;
2954 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002955 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002956 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002957 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002958 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002959}
2960EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2961
Li Zefana043e3b2008-02-23 15:24:09 -08002962/**
Tejun Heo79578622012-04-01 12:09:56 -07002963 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002964 * @cfts: zero-length name terminated array of cftypes
2965 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002966 * Unregister @cfts. Files described by @cfts are removed from all
2967 * existing cgroups and all future cgroups won't have them either. This
2968 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002969 *
2970 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002971 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002972 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002973int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002974{
2975 struct cftype_set *set;
2976
Tejun Heo2bb566c2013-08-08 20:11:23 -04002977 if (!cfts || !cfts[0].ss)
2978 return -ENOENT;
2979
Tejun Heo79578622012-04-01 12:09:56 -07002980 cgroup_cfts_prepare();
2981
Tejun Heo2bb566c2013-08-08 20:11:23 -04002982 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002983 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002984 list_del(&set->node);
2985 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002986 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002987 return 0;
2988 }
2989 }
2990
Tejun Heo2bb566c2013-08-08 20:11:23 -04002991 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002992 return -ENOENT;
2993}
2994
2995/**
Li Zefana043e3b2008-02-23 15:24:09 -08002996 * cgroup_task_count - count the number of tasks in a cgroup.
2997 * @cgrp: the cgroup in question
2998 *
2999 * Return the number of tasks in the cgroup.
3000 */
Paul Menagebd89aab2007-10-18 23:40:44 -07003001int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003002{
3003 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003004 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003005
Paul Menage817929e2007-10-18 23:39:36 -07003006 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003007 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3008 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07003009 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003010 return count;
3011}
3012
3013/*
Tejun Heo0942eee2013-08-08 20:11:26 -04003014 * To reduce the fork() overhead for systems that are not actually using
3015 * their cgroups capability, we don't maintain the lists running through
3016 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04003017 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003018 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003019static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003020{
3021 struct task_struct *p, *g;
3022 write_lock(&css_set_lock);
3023 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003024 /*
3025 * We need tasklist_lock because RCU is not safe against
3026 * while_each_thread(). Besides, a forking task that has passed
3027 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3028 * is not guaranteed to have its child immediately visible in the
3029 * tasklist if we walk through it with RCU.
3030 */
3031 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003032 do_each_thread(g, p) {
3033 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003034 /*
3035 * We should check if the process is exiting, otherwise
3036 * it will race with cgroup_exit() in that the list
3037 * entry won't be deleted though the process has exited.
3038 */
3039 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003040 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003041 task_unlock(p);
3042 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003043 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003044 write_unlock(&css_set_lock);
3045}
3046
Tejun Heo574bd9f2012-11-09 09:12:29 -08003047/**
Tejun Heo492eb212013-08-08 20:11:25 -04003048 * css_next_child - find the next child of a given css
3049 * @pos_css: the current position (%NULL to initiate traversal)
3050 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003051 *
Tejun Heo492eb212013-08-08 20:11:25 -04003052 * This function returns the next child of @parent_css and should be called
3053 * under RCU read lock. The only requirement is that @parent_css and
3054 * @pos_css are accessible. The next sibling is guaranteed to be returned
3055 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003056 */
Tejun Heo492eb212013-08-08 20:11:25 -04003057struct cgroup_subsys_state *
3058css_next_child(struct cgroup_subsys_state *pos_css,
3059 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003060{
Tejun Heo492eb212013-08-08 20:11:25 -04003061 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3062 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003063 struct cgroup *next;
3064
3065 WARN_ON_ONCE(!rcu_read_lock_held());
3066
3067 /*
3068 * @pos could already have been removed. Once a cgroup is removed,
3069 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003070 * changes. As CGRP_DEAD assertion is serialized and happens
3071 * before the cgroup is taken off the ->sibling list, if we see it
3072 * unasserted, it's guaranteed that the next sibling hasn't
3073 * finished its grace period even if it's already removed, and thus
3074 * safe to dereference from this RCU critical section. If
3075 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3076 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003077 *
3078 * If @pos is dead, its next pointer can't be dereferenced;
3079 * however, as each cgroup is given a monotonically increasing
3080 * unique serial number and always appended to the sibling list,
3081 * the next one can be found by walking the parent's children until
3082 * we see a cgroup with higher serial number than @pos's. While
3083 * this path can be slower, it's taken only when either the current
3084 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003085 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003086 if (!pos) {
3087 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3088 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003089 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003090 } else {
3091 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3092 if (next->serial_nr > pos->serial_nr)
3093 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003094 }
3095
Tejun Heo492eb212013-08-08 20:11:25 -04003096 if (&next->sibling == &cgrp->children)
3097 return NULL;
3098
3099 if (parent_css->ss)
3100 return cgroup_css(next, parent_css->ss->subsys_id);
3101 else
3102 return &next->dummy_css;
Tejun Heo53fa5262013-05-24 10:55:38 +09003103}
Tejun Heo492eb212013-08-08 20:11:25 -04003104EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003105
3106/**
Tejun Heo492eb212013-08-08 20:11:25 -04003107 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003108 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003109 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003110 *
Tejun Heo492eb212013-08-08 20:11:25 -04003111 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003112 * to visit for pre-order traversal of @root's descendants. @root is
3113 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003114 *
3115 * While this function requires RCU read locking, it doesn't require the
3116 * whole traversal to be contained in a single RCU critical section. This
3117 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003118 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003119 */
Tejun Heo492eb212013-08-08 20:11:25 -04003120struct cgroup_subsys_state *
3121css_next_descendant_pre(struct cgroup_subsys_state *pos,
3122 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003123{
Tejun Heo492eb212013-08-08 20:11:25 -04003124 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003125
3126 WARN_ON_ONCE(!rcu_read_lock_held());
3127
Tejun Heobd8815a2013-08-08 20:11:27 -04003128 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003129 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003130 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003131
3132 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003133 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003134 if (next)
3135 return next;
3136
3137 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003138 while (pos != root) {
3139 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003140 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003141 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003142 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003143 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003144
3145 return NULL;
3146}
Tejun Heo492eb212013-08-08 20:11:25 -04003147EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003148
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003149/**
Tejun Heo492eb212013-08-08 20:11:25 -04003150 * css_rightmost_descendant - return the rightmost descendant of a css
3151 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003152 *
Tejun Heo492eb212013-08-08 20:11:25 -04003153 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3154 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003155 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003156 *
3157 * While this function requires RCU read locking, it doesn't require the
3158 * whole traversal to be contained in a single RCU critical section. This
3159 * function will return the correct rightmost descendant as long as @pos is
3160 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003161 */
Tejun Heo492eb212013-08-08 20:11:25 -04003162struct cgroup_subsys_state *
3163css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003164{
Tejun Heo492eb212013-08-08 20:11:25 -04003165 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003166
3167 WARN_ON_ONCE(!rcu_read_lock_held());
3168
3169 do {
3170 last = pos;
3171 /* ->prev isn't RCU safe, walk ->next till the end */
3172 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003173 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003174 pos = tmp;
3175 } while (pos);
3176
3177 return last;
3178}
Tejun Heo492eb212013-08-08 20:11:25 -04003179EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003180
Tejun Heo492eb212013-08-08 20:11:25 -04003181static struct cgroup_subsys_state *
3182css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003183{
Tejun Heo492eb212013-08-08 20:11:25 -04003184 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003185
3186 do {
3187 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003188 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003189 } while (pos);
3190
3191 return last;
3192}
3193
3194/**
Tejun Heo492eb212013-08-08 20:11:25 -04003195 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003196 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003197 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003198 *
Tejun Heo492eb212013-08-08 20:11:25 -04003199 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003200 * to visit for post-order traversal of @root's descendants. @root is
3201 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003202 *
3203 * While this function requires RCU read locking, it doesn't require the
3204 * whole traversal to be contained in a single RCU critical section. This
3205 * function will return the correct next descendant as long as both @pos
3206 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003207 */
Tejun Heo492eb212013-08-08 20:11:25 -04003208struct cgroup_subsys_state *
3209css_next_descendant_post(struct cgroup_subsys_state *pos,
3210 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003211{
Tejun Heo492eb212013-08-08 20:11:25 -04003212 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003213
3214 WARN_ON_ONCE(!rcu_read_lock_held());
3215
3216 /* if first iteration, visit the leftmost descendant */
3217 if (!pos) {
Tejun Heo492eb212013-08-08 20:11:25 -04003218 next = css_leftmost_descendant(root);
3219 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003220 }
3221
Tejun Heobd8815a2013-08-08 20:11:27 -04003222 /* if we visited @root, we're done */
3223 if (pos == root)
3224 return NULL;
3225
Tejun Heo574bd9f2012-11-09 09:12:29 -08003226 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003227 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003228 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003229 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003230
3231 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003232 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003233}
Tejun Heo492eb212013-08-08 20:11:25 -04003234EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003235
Tejun Heo0942eee2013-08-08 20:11:26 -04003236/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003237 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003238 * @it: the iterator to advance
3239 *
3240 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003241 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003242static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003243{
3244 struct list_head *l = it->cset_link;
3245 struct cgrp_cset_link *link;
3246 struct css_set *cset;
3247
3248 /* Advance to the next non-empty css_set */
3249 do {
3250 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003251 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003252 it->cset_link = NULL;
3253 return;
3254 }
3255 link = list_entry(l, struct cgrp_cset_link, cset_link);
3256 cset = link->cset;
3257 } while (list_empty(&cset->tasks));
3258 it->cset_link = l;
3259 it->task = cset->tasks.next;
3260}
3261
Tejun Heo0942eee2013-08-08 20:11:26 -04003262/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003263 * css_task_iter_start - initiate task iteration
3264 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003265 * @it: the task iterator to use
3266 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003267 * Initiate iteration through the tasks of @css. The caller can call
3268 * css_task_iter_next() to walk through the tasks until the function
3269 * returns NULL. On completion of iteration, css_task_iter_end() must be
3270 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003271 *
3272 * Note that this function acquires a lock which is released when the
3273 * iteration finishes. The caller can't sleep while iteration is in
3274 * progress.
3275 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003276void css_task_iter_start(struct cgroup_subsys_state *css,
3277 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003278 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003279{
3280 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003281 * The first time anyone tries to iterate across a css, we need to
3282 * enable the list linking each css_set to its tasks, and fix up
3283 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003284 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003285 if (!use_task_css_set_links)
3286 cgroup_enable_task_cg_lists();
3287
Paul Menage817929e2007-10-18 23:39:36 -07003288 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003289
Tejun Heo72ec7022013-08-08 20:11:26 -04003290 it->origin_css = css;
3291 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003292
Tejun Heo72ec7022013-08-08 20:11:26 -04003293 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003294}
3295
Tejun Heo0942eee2013-08-08 20:11:26 -04003296/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003297 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003298 * @it: the task iterator being iterated
3299 *
3300 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003301 * initialized via css_task_iter_start(). Returns NULL when the iteration
3302 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003303 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003304struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003305{
3306 struct task_struct *res;
3307 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003308 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003309
3310 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003311 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003312 return NULL;
3313 res = list_entry(l, struct task_struct, cg_list);
3314 /* Advance iterator to find next entry */
3315 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003316 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3317 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003318 /*
3319 * We reached the end of this task list - move on to the
3320 * next cgrp_cset_link.
3321 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003322 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003323 } else {
3324 it->task = l;
3325 }
3326 return res;
3327}
3328
Tejun Heo0942eee2013-08-08 20:11:26 -04003329/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003330 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003331 * @it: the task iterator to finish
3332 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003333 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003334 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003335void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003336 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003337{
3338 read_unlock(&css_set_lock);
3339}
3340
Cliff Wickman31a7df02008-02-07 00:14:42 -08003341static inline int started_after_time(struct task_struct *t1,
3342 struct timespec *time,
3343 struct task_struct *t2)
3344{
3345 int start_diff = timespec_compare(&t1->start_time, time);
3346 if (start_diff > 0) {
3347 return 1;
3348 } else if (start_diff < 0) {
3349 return 0;
3350 } else {
3351 /*
3352 * Arbitrarily, if two processes started at the same
3353 * time, we'll say that the lower pointer value
3354 * started first. Note that t2 may have exited by now
3355 * so this may not be a valid pointer any longer, but
3356 * that's fine - it still serves to distinguish
3357 * between two tasks started (effectively) simultaneously.
3358 */
3359 return t1 > t2;
3360 }
3361}
3362
3363/*
3364 * This function is a callback from heap_insert() and is used to order
3365 * the heap.
3366 * In this case we order the heap in descending task start time.
3367 */
3368static inline int started_after(void *p1, void *p2)
3369{
3370 struct task_struct *t1 = p1;
3371 struct task_struct *t2 = p2;
3372 return started_after_time(t1, &t2->start_time, t2);
3373}
3374
3375/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003376 * css_scan_tasks - iterate though all the tasks in a css
3377 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003378 * @test: optional test callback
3379 * @process: process callback
3380 * @data: data passed to @test and @process
3381 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003382 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003383 * Iterate through all the tasks in @css, calling @test for each, and if it
3384 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003385 *
Tejun Heoe5358372013-08-08 20:11:26 -04003386 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003387 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003388 * lock css_set_lock for the call to @process.
3389 *
3390 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003391 * of @css for the duration of this call. This function may or may not
3392 * call @process for tasks that exit or move to a different css during the
3393 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003394 *
3395 * Note that @test may be called with locks held, and may in some
3396 * situations be called multiple times for the same task, so it should be
3397 * cheap.
3398 *
3399 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3400 * heap operations (and its "gt" member will be overwritten), else a
3401 * temporary heap will be used (allocation of which may cause this function
3402 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003403 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003404int css_scan_tasks(struct cgroup_subsys_state *css,
3405 bool (*test)(struct task_struct *, void *),
3406 void (*process)(struct task_struct *, void *),
3407 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003408{
3409 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003410 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003411 struct task_struct *p, *dropped;
3412 /* Never dereference latest_task, since it's not refcounted */
3413 struct task_struct *latest_task = NULL;
3414 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003415 struct timespec latest_time = { 0, 0 };
3416
Tejun Heoe5358372013-08-08 20:11:26 -04003417 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003418 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003419 heap->gt = &started_after;
3420 } else {
3421 /* We need to allocate our own heap memory */
3422 heap = &tmp_heap;
3423 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3424 if (retval)
3425 /* cannot allocate the heap */
3426 return retval;
3427 }
3428
3429 again:
3430 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003431 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003432 * which are of interest, and invoking @process callback on the
3433 * ones which need an update. Since we don't want to hold any
3434 * locks during the task updates, gather tasks to be processed in a
3435 * heap structure. The heap is sorted by descending task start
3436 * time. If the statically-sized heap fills up, we overflow tasks
3437 * that started later, and in future iterations only consider tasks
3438 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003439 * guarantees forward progress and that we don't miss any tasks.
3440 */
3441 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003442 css_task_iter_start(css, &it);
3443 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003444 /*
3445 * Only affect tasks that qualify per the caller's callback,
3446 * if he provided one
3447 */
Tejun Heoe5358372013-08-08 20:11:26 -04003448 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003449 continue;
3450 /*
3451 * Only process tasks that started after the last task
3452 * we processed
3453 */
3454 if (!started_after_time(p, &latest_time, latest_task))
3455 continue;
3456 dropped = heap_insert(heap, p);
3457 if (dropped == NULL) {
3458 /*
3459 * The new task was inserted; the heap wasn't
3460 * previously full
3461 */
3462 get_task_struct(p);
3463 } else if (dropped != p) {
3464 /*
3465 * The new task was inserted, and pushed out a
3466 * different task
3467 */
3468 get_task_struct(p);
3469 put_task_struct(dropped);
3470 }
3471 /*
3472 * Else the new task was newer than anything already in
3473 * the heap and wasn't inserted
3474 */
3475 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003476 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003477
3478 if (heap->size) {
3479 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003480 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003481 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003482 latest_time = q->start_time;
3483 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003484 }
3485 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003486 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003487 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003488 }
3489 /*
3490 * If we had to process any tasks at all, scan again
3491 * in case some of them were in the middle of forking
3492 * children that didn't get processed.
3493 * Not the most efficient way to do it, but it avoids
3494 * having to take callback_mutex in the fork path
3495 */
3496 goto again;
3497 }
3498 if (heap == &tmp_heap)
3499 heap_free(&tmp_heap);
3500 return 0;
3501}
3502
Tejun Heoe5358372013-08-08 20:11:26 -04003503static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003504{
Tejun Heoe5358372013-08-08 20:11:26 -04003505 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003506
Tejun Heo47cfcd02013-04-07 09:29:51 -07003507 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003508 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003509 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003510}
3511
3512/**
3513 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3514 * @to: cgroup to which the tasks will be moved
3515 * @from: cgroup in which the tasks currently reside
3516 */
3517int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3518{
Tejun Heo72ec7022013-08-08 20:11:26 -04003519 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3520 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003521}
3522
Paul Menage817929e2007-10-18 23:39:36 -07003523/*
Ben Blum102a7752009-09-23 15:56:26 -07003524 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003525 *
3526 * Reading this file can return large amounts of data if a cgroup has
3527 * *lots* of attached tasks. So it may need several calls to read(),
3528 * but we cannot guarantee that the information we produce is correct
3529 * unless we produce it entirely atomically.
3530 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003531 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003532
Li Zefan24528252012-01-20 11:58:43 +08003533/* which pidlist file are we talking about? */
3534enum cgroup_filetype {
3535 CGROUP_FILE_PROCS,
3536 CGROUP_FILE_TASKS,
3537};
3538
3539/*
3540 * A pidlist is a list of pids that virtually represents the contents of one
3541 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3542 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3543 * to the cgroup.
3544 */
3545struct cgroup_pidlist {
3546 /*
3547 * used to find which pidlist is wanted. doesn't change as long as
3548 * this particular list stays in the list.
3549 */
3550 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3551 /* array of xids */
3552 pid_t *list;
3553 /* how many elements the above list has */
3554 int length;
3555 /* how many files are using the current array */
3556 int use_count;
3557 /* each of these stored in a list by its cgroup */
3558 struct list_head links;
3559 /* pointer to the cgroup we belong to, for list removal purposes */
3560 struct cgroup *owner;
3561 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003562 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003563};
3564
Paul Menagebbcb81d2007-10-18 23:39:32 -07003565/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003566 * The following two functions "fix" the issue where there are more pids
3567 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3568 * TODO: replace with a kernel-wide solution to this problem
3569 */
3570#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3571static void *pidlist_allocate(int count)
3572{
3573 if (PIDLIST_TOO_LARGE(count))
3574 return vmalloc(count * sizeof(pid_t));
3575 else
3576 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3577}
3578static void pidlist_free(void *p)
3579{
3580 if (is_vmalloc_addr(p))
3581 vfree(p);
3582 else
3583 kfree(p);
3584}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003585
3586/*
Ben Blum102a7752009-09-23 15:56:26 -07003587 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003588 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003589 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003590static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003591{
Ben Blum102a7752009-09-23 15:56:26 -07003592 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003593
3594 /*
3595 * we presume the 0th element is unique, so i starts at 1. trivial
3596 * edge cases first; no work needs to be done for either
3597 */
3598 if (length == 0 || length == 1)
3599 return length;
3600 /* src and dest walk down the list; dest counts unique elements */
3601 for (src = 1; src < length; src++) {
3602 /* find next unique element */
3603 while (list[src] == list[src-1]) {
3604 src++;
3605 if (src == length)
3606 goto after;
3607 }
3608 /* dest always points to where the next unique element goes */
3609 list[dest] = list[src];
3610 dest++;
3611 }
3612after:
Ben Blum102a7752009-09-23 15:56:26 -07003613 return dest;
3614}
3615
3616static int cmppid(const void *a, const void *b)
3617{
3618 return *(pid_t *)a - *(pid_t *)b;
3619}
3620
3621/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003622 * find the appropriate pidlist for our purpose (given procs vs tasks)
3623 * returns with the lock on that pidlist already held, and takes care
3624 * of the use count, or returns NULL with no locks held if we're out of
3625 * memory.
3626 */
3627static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3628 enum cgroup_filetype type)
3629{
3630 struct cgroup_pidlist *l;
3631 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003632 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003633
Ben Blum72a8cb32009-09-23 15:56:27 -07003634 /*
Li Zefanb3958902013-08-01 09:52:15 +08003635 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 * the last ref-holder is trying to remove l from the list at the same
3637 * time. Holding the pidlist_mutex precludes somebody taking whichever
3638 * list we find out from under us - compare release_pid_array().
3639 */
3640 mutex_lock(&cgrp->pidlist_mutex);
3641 list_for_each_entry(l, &cgrp->pidlists, links) {
3642 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003644 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003645 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003646 return l;
3647 }
3648 }
3649 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003650 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003651 if (!l) {
3652 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003653 return l;
3654 }
Li Zefanb3958902013-08-01 09:52:15 +08003655 init_rwsem(&l->rwsem);
3656 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003657 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003658 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003659 l->owner = cgrp;
3660 list_add(&l->links, &cgrp->pidlists);
3661 mutex_unlock(&cgrp->pidlist_mutex);
3662 return l;
3663}
3664
3665/*
Ben Blum102a7752009-09-23 15:56:26 -07003666 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3667 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003668static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3669 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003670{
3671 pid_t *array;
3672 int length;
3673 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003674 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003675 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003676 struct cgroup_pidlist *l;
3677
3678 /*
3679 * If cgroup gets more users after we read count, we won't have
3680 * enough space - tough. This race is indistinguishable to the
3681 * caller from the case that the additional cgroup users didn't
3682 * show up until sometime later on.
3683 */
3684 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003685 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003686 if (!array)
3687 return -ENOMEM;
3688 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003689 css_task_iter_start(&cgrp->dummy_css, &it);
3690 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003691 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003692 break;
Ben Blum102a7752009-09-23 15:56:26 -07003693 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003694 if (type == CGROUP_FILE_PROCS)
3695 pid = task_tgid_vnr(tsk);
3696 else
3697 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003698 if (pid > 0) /* make sure to only use valid results */
3699 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003700 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003701 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003702 length = n;
3703 /* now sort & (if procs) strip out duplicates */
3704 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003705 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003706 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003707 l = cgroup_pidlist_find(cgrp, type);
3708 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003709 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003710 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003711 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003712 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003713 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003714 l->list = array;
3715 l->length = length;
3716 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003717 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003718 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003719 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003720}
3721
Balbir Singh846c7bb2007-10-18 23:39:44 -07003722/**
Li Zefana043e3b2008-02-23 15:24:09 -08003723 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003724 * @stats: cgroupstats to fill information into
3725 * @dentry: A dentry entry belonging to the cgroup for which stats have
3726 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003727 *
3728 * Build and fill cgroupstats so that taskstats can export it to user
3729 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003730 */
3731int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3732{
3733 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003734 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003735 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003736 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003737
Balbir Singh846c7bb2007-10-18 23:39:44 -07003738 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003739 * Validate dentry by checking the superblock operations,
3740 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003741 */
Li Zefan33d283b2008-11-19 15:36:48 -08003742 if (dentry->d_sb->s_op != &cgroup_ops ||
3743 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003744 goto err;
3745
3746 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003747 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003748
Tejun Heo72ec7022013-08-08 20:11:26 -04003749 css_task_iter_start(&cgrp->dummy_css, &it);
3750 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003751 switch (tsk->state) {
3752 case TASK_RUNNING:
3753 stats->nr_running++;
3754 break;
3755 case TASK_INTERRUPTIBLE:
3756 stats->nr_sleeping++;
3757 break;
3758 case TASK_UNINTERRUPTIBLE:
3759 stats->nr_uninterruptible++;
3760 break;
3761 case TASK_STOPPED:
3762 stats->nr_stopped++;
3763 break;
3764 default:
3765 if (delayacct_is_task_waiting_on_io(tsk))
3766 stats->nr_io_wait++;
3767 break;
3768 }
3769 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003770 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003771
Balbir Singh846c7bb2007-10-18 23:39:44 -07003772err:
3773 return ret;
3774}
3775
Paul Menage8f3ff202009-09-23 15:56:25 -07003776
Paul Menagecc31edc2008-10-18 20:28:04 -07003777/*
Ben Blum102a7752009-09-23 15:56:26 -07003778 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003779 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003780 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003781 */
3782
Ben Blum102a7752009-09-23 15:56:26 -07003783static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003784{
3785 /*
3786 * Initially we receive a position value that corresponds to
3787 * one more than the last pid shown (or 0 on the first call or
3788 * after a seek to the start). Use a binary-search to find the
3789 * next pid to display, if any
3790 */
Ben Blum102a7752009-09-23 15:56:26 -07003791 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003792 int index = 0, pid = *pos;
3793 int *iter;
3794
Li Zefanb3958902013-08-01 09:52:15 +08003795 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003796 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003797 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003798
Paul Menagecc31edc2008-10-18 20:28:04 -07003799 while (index < end) {
3800 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003801 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003802 index = mid;
3803 break;
Ben Blum102a7752009-09-23 15:56:26 -07003804 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003805 index = mid + 1;
3806 else
3807 end = mid;
3808 }
3809 }
3810 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003811 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003812 return NULL;
3813 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003814 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003815 *pos = *iter;
3816 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003817}
3818
Ben Blum102a7752009-09-23 15:56:26 -07003819static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003820{
Ben Blum102a7752009-09-23 15:56:26 -07003821 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003822 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003823}
3824
Ben Blum102a7752009-09-23 15:56:26 -07003825static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003826{
Ben Blum102a7752009-09-23 15:56:26 -07003827 struct cgroup_pidlist *l = s->private;
3828 pid_t *p = v;
3829 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003830 /*
3831 * Advance to the next pid in the array. If this goes off the
3832 * end, we're done
3833 */
3834 p++;
3835 if (p >= end) {
3836 return NULL;
3837 } else {
3838 *pos = *p;
3839 return p;
3840 }
3841}
3842
Ben Blum102a7752009-09-23 15:56:26 -07003843static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003844{
3845 return seq_printf(s, "%d\n", *(int *)v);
3846}
3847
Ben Blum102a7752009-09-23 15:56:26 -07003848/*
3849 * seq_operations functions for iterating on pidlists through seq_file -
3850 * independent of whether it's tasks or procs
3851 */
3852static const struct seq_operations cgroup_pidlist_seq_operations = {
3853 .start = cgroup_pidlist_start,
3854 .stop = cgroup_pidlist_stop,
3855 .next = cgroup_pidlist_next,
3856 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003857};
3858
Ben Blum102a7752009-09-23 15:56:26 -07003859static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003860{
Ben Blum72a8cb32009-09-23 15:56:27 -07003861 /*
3862 * the case where we're the last user of this particular pidlist will
3863 * have us remove it from the cgroup's list, which entails taking the
3864 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3865 * pidlist_mutex, we have to take pidlist_mutex first.
3866 */
3867 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003868 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003869 BUG_ON(!l->use_count);
3870 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003871 /* we're the last user if refcount is 0; remove and free */
3872 list_del(&l->links);
3873 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003874 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003875 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003876 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003877 kfree(l);
3878 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003879 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003880 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003881 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003882}
3883
Ben Blum102a7752009-09-23 15:56:26 -07003884static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003885{
Ben Blum102a7752009-09-23 15:56:26 -07003886 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003887 if (!(file->f_mode & FMODE_READ))
3888 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003889 /*
3890 * the seq_file will only be initialized if the file was opened for
3891 * reading; hence we check if it's not null only in that case.
3892 */
3893 l = ((struct seq_file *)file->private_data)->private;
3894 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003895 return seq_release(inode, file);
3896}
3897
Ben Blum102a7752009-09-23 15:56:26 -07003898static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003899 .read = seq_read,
3900 .llseek = seq_lseek,
3901 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003902 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003903};
3904
3905/*
Ben Blum102a7752009-09-23 15:56:26 -07003906 * The following functions handle opens on a file that displays a pidlist
3907 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3908 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003909 */
Ben Blum102a7752009-09-23 15:56:26 -07003910/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003911static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003912{
3913 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003914 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003915 int retval;
3916
3917 /* Nothing to do for write-only files */
3918 if (!(file->f_mode & FMODE_READ))
3919 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003920
Ben Blum102a7752009-09-23 15:56:26 -07003921 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003922 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003923 if (retval)
3924 return retval;
3925 /* configure file information */
3926 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003927
Ben Blum102a7752009-09-23 15:56:26 -07003928 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003929 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003930 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003931 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003932 }
Ben Blum102a7752009-09-23 15:56:26 -07003933 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003934 return 0;
3935}
Ben Blum102a7752009-09-23 15:56:26 -07003936static int cgroup_tasks_open(struct inode *unused, struct file *file)
3937{
Ben Blum72a8cb32009-09-23 15:56:27 -07003938 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003939}
3940static int cgroup_procs_open(struct inode *unused, struct file *file)
3941{
Ben Blum72a8cb32009-09-23 15:56:27 -07003942 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003943}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003944
Tejun Heo182446d2013-08-08 20:11:24 -04003945static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3946 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003947{
Tejun Heo182446d2013-08-08 20:11:24 -04003948 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003949}
3950
Tejun Heo182446d2013-08-08 20:11:24 -04003951static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3952 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003953{
Tejun Heo182446d2013-08-08 20:11:24 -04003954 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003955 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003956 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003957 else
Tejun Heo182446d2013-08-08 20:11:24 -04003958 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003959 return 0;
3960}
3961
Paul Menagebbcb81d2007-10-18 23:39:32 -07003962/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003963 * When dput() is called asynchronously, if umount has been done and
3964 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3965 * there's a small window that vfs will see the root dentry with non-zero
3966 * refcnt and trigger BUG().
3967 *
3968 * That's why we hold a reference before dput() and drop it right after.
3969 */
3970static void cgroup_dput(struct cgroup *cgrp)
3971{
3972 struct super_block *sb = cgrp->root->sb;
3973
3974 atomic_inc(&sb->s_active);
3975 dput(cgrp->dentry);
3976 deactivate_super(sb);
3977}
3978
3979/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003980 * Unregister event and free resources.
3981 *
3982 * Gets called from workqueue.
3983 */
3984static void cgroup_event_remove(struct work_struct *work)
3985{
3986 struct cgroup_event *event = container_of(work, struct cgroup_event,
3987 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003988 struct cgroup_subsys_state *css = event->css;
3989 struct cgroup *cgrp = css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003990
Li Zefan810cbee2013-02-18 18:56:14 +08003991 remove_wait_queue(event->wqh, &event->wait);
3992
Tejun Heo81eeaf02013-08-08 20:11:26 -04003993 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003994
Li Zefan810cbee2013-02-18 18:56:14 +08003995 /* Notify userspace the event is going away. */
3996 eventfd_signal(event->eventfd, 1);
3997
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003998 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003999 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08004000 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004001}
4002
4003/*
4004 * Gets called on POLLHUP on eventfd when user closes it.
4005 *
4006 * Called with wqh->lock held and interrupts disabled.
4007 */
4008static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
4009 int sync, void *key)
4010{
4011 struct cgroup_event *event = container_of(wait,
4012 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04004013 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004014 unsigned long flags = (unsigned long)key;
4015
4016 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004017 /*
Li Zefan810cbee2013-02-18 18:56:14 +08004018 * If the event has been detached at cgroup removal, we
4019 * can simply return knowing the other side will cleanup
4020 * for us.
4021 *
4022 * We can't race against event freeing since the other
4023 * side will require wqh->lock via remove_wait_queue(),
4024 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004025 */
Li Zefan810cbee2013-02-18 18:56:14 +08004026 spin_lock(&cgrp->event_list_lock);
4027 if (!list_empty(&event->list)) {
4028 list_del_init(&event->list);
4029 /*
4030 * We are in atomic context, but cgroup_event_remove()
4031 * may sleep, so we have to call it in workqueue.
4032 */
4033 schedule_work(&event->remove);
4034 }
4035 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004036 }
4037
4038 return 0;
4039}
4040
4041static void cgroup_event_ptable_queue_proc(struct file *file,
4042 wait_queue_head_t *wqh, poll_table *pt)
4043{
4044 struct cgroup_event *event = container_of(pt,
4045 struct cgroup_event, pt);
4046
4047 event->wqh = wqh;
4048 add_wait_queue(wqh, &event->wait);
4049}
4050
4051/*
4052 * Parse input and register new cgroup event handler.
4053 *
4054 * Input must be in format '<event_fd> <control_fd> <args>'.
4055 * Interpretation of args is defined by control file implementation.
4056 */
Tejun Heo182446d2013-08-08 20:11:24 -04004057static int cgroup_write_event_control(struct cgroup_subsys_state *css,
4058 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004059{
Tejun Heo182446d2013-08-08 20:11:24 -04004060 struct cgroup *cgrp = css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004061 struct cgroup_event *event;
Li Zefanf1690072013-02-18 14:13:35 +08004062 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004063 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08004064 struct file *efile;
4065 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004066 char *endp;
4067 int ret;
4068
4069 efd = simple_strtoul(buffer, &endp, 10);
4070 if (*endp != ' ')
4071 return -EINVAL;
4072 buffer = endp + 1;
4073
4074 cfd = simple_strtoul(buffer, &endp, 10);
4075 if ((*endp != ' ') && (*endp != '\0'))
4076 return -EINVAL;
4077 buffer = endp + 1;
4078
4079 event = kzalloc(sizeof(*event), GFP_KERNEL);
4080 if (!event)
4081 return -ENOMEM;
Tejun Heo81eeaf02013-08-08 20:11:26 -04004082 event->css = css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004083 INIT_LIST_HEAD(&event->list);
4084 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4085 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4086 INIT_WORK(&event->remove, cgroup_event_remove);
4087
4088 efile = eventfd_fget(efd);
4089 if (IS_ERR(efile)) {
4090 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004091 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004092 }
4093
4094 event->eventfd = eventfd_ctx_fileget(efile);
4095 if (IS_ERR(event->eventfd)) {
4096 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004097 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004098 }
4099
4100 cfile = fget(cfd);
4101 if (!cfile) {
4102 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004103 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004104 }
4105
4106 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004107 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004108 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004109 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004110 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004111
4112 event->cft = __file_cft(cfile);
4113 if (IS_ERR(event->cft)) {
4114 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004115 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004116 }
4117
Li Zefanf1690072013-02-18 14:13:35 +08004118 /*
4119 * The file to be monitored must be in the same cgroup as
4120 * cgroup.event_control is.
4121 */
4122 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4123 if (cgrp_cfile != cgrp) {
4124 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004125 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004126 }
4127
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004128 if (!event->cft->register_event || !event->cft->unregister_event) {
4129 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004130 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004131 }
4132
Tejun Heo81eeaf02013-08-08 20:11:26 -04004133 ret = event->cft->register_event(css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004134 event->eventfd, buffer);
4135 if (ret)
Li Zefan876ede82013-08-01 09:51:47 +08004136 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004137
Li Zefan7ef70e42013-04-26 11:58:03 -07004138 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004139
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004140 /*
4141 * Events should be removed after rmdir of cgroup directory, but before
4142 * destroying subsystem state objects. Let's take reference to cgroup
4143 * directory dentry to do that.
4144 */
4145 dget(cgrp->dentry);
4146
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004147 spin_lock(&cgrp->event_list_lock);
4148 list_add(&event->list, &cgrp->event_list);
4149 spin_unlock(&cgrp->event_list_lock);
4150
4151 fput(cfile);
4152 fput(efile);
4153
4154 return 0;
4155
Li Zefan876ede82013-08-01 09:51:47 +08004156out_put_cfile:
4157 fput(cfile);
4158out_put_eventfd:
4159 eventfd_ctx_put(event->eventfd);
4160out_put_efile:
4161 fput(efile);
4162out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004163 kfree(event);
4164
4165 return ret;
4166}
4167
Tejun Heo182446d2013-08-08 20:11:24 -04004168static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4169 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004170{
Tejun Heo182446d2013-08-08 20:11:24 -04004171 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004172}
4173
Tejun Heo182446d2013-08-08 20:11:24 -04004174static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4175 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004176{
4177 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004178 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004179 else
Tejun Heo182446d2013-08-08 20:11:24 -04004180 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004181 return 0;
4182}
4183
Tejun Heod5c56ce2013-06-03 19:14:34 -07004184static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004185 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004186 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004187 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004188 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004189 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004190 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004191 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004192 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004193 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004194 .write_string = cgroup_write_event_control,
4195 .mode = S_IWUGO,
4196 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004197 {
4198 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004199 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004200 .read_u64 = cgroup_clone_children_read,
4201 .write_u64 = cgroup_clone_children_write,
4202 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004203 {
Tejun Heo873fe092013-04-14 20:15:26 -07004204 .name = "cgroup.sane_behavior",
4205 .flags = CFTYPE_ONLY_ON_ROOT,
4206 .read_seq_string = cgroup_sane_behavior_show,
4207 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004208
4209 /*
4210 * Historical crazy stuff. These don't have "cgroup." prefix and
4211 * don't exist if sane_behavior. If you're depending on these, be
4212 * prepared to be burned.
4213 */
4214 {
4215 .name = "tasks",
4216 .flags = CFTYPE_INSANE, /* use "procs" instead */
4217 .open = cgroup_tasks_open,
4218 .write_u64 = cgroup_tasks_write,
4219 .release = cgroup_pidlist_release,
4220 .mode = S_IRUGO | S_IWUSR,
4221 },
4222 {
4223 .name = "notify_on_release",
4224 .flags = CFTYPE_INSANE,
4225 .read_u64 = cgroup_read_notify_on_release,
4226 .write_u64 = cgroup_write_notify_on_release,
4227 },
Tejun Heo873fe092013-04-14 20:15:26 -07004228 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004229 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004230 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004231 .read_seq_string = cgroup_release_agent_show,
4232 .write_string = cgroup_release_agent_write,
4233 .max_write_len = PATH_MAX,
4234 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004235 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004236};
4237
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004238/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004239 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004240 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004241 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004242 *
4243 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004244 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004245static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004248 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249
Tejun Heo8e3f6542012-04-01 12:09:55 -07004250 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004251 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004252 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004253
4254 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004255 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004256
Tejun Heobee55092013-06-28 16:24:11 -07004257 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004258 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004259 if (ret < 0)
4260 goto err;
4261 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004262 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004263
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004264 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004265 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04004266 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004267 struct css_id *id = rcu_dereference_protected(css->id, true);
4268
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004269 /*
4270 * Update id->css pointer and make this css visible from
4271 * CSS ID functions. This pointer will be dereferened
4272 * from RCU-read-side without locks.
4273 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004274 if (id)
4275 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004276 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004277
4278 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004279err:
4280 cgroup_clear_dir(cgrp, subsys_mask);
4281 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004282}
4283
Tejun Heo35ef10d2013-08-13 11:01:54 -04004284static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004285{
4286 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004287 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004288
Tejun Heo0ae78e02013-08-13 11:01:54 -04004289 if (css->parent)
4290 css_put(css->parent);
4291
Li Zefan1c8158e2013-06-18 18:41:10 +08004292 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004293}
4294
Tejun Heod3daf282013-06-13 19:39:16 -07004295static void css_release(struct percpu_ref *ref)
4296{
4297 struct cgroup_subsys_state *css =
4298 container_of(ref, struct cgroup_subsys_state, refcnt);
4299
Tejun Heo35ef10d2013-08-13 11:01:54 -04004300 /*
4301 * css holds an extra ref to @cgrp->dentry which is put on the last
4302 * css_put(). dput() requires process context, which css_put() may
4303 * be called without. @css->destroy_work will be used to invoke
4304 * dput() asynchronously from css_put().
4305 */
4306 INIT_WORK(&css->destroy_work, css_free_work_fn);
4307 schedule_work(&css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004308}
4309
Tejun Heo623f9262013-08-13 11:01:55 -04004310static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4311 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004312{
Paul Menagebd89aab2007-10-18 23:40:44 -07004313 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004314 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004315 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004316 css->id = NULL;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004317
4318 if (cgrp->parent)
4319 css->parent = cgroup_css(cgrp->parent, ss->subsys_id);
4320 else
Tejun Heo38b53ab2012-11-19 08:13:36 -08004321 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004322
Tejun Heo40e93b32013-08-13 11:01:53 -04004323 BUG_ON(cgroup_css(cgrp, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324}
4325
Li Zefan2a4ac632013-07-31 16:16:40 +08004326/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004327static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004328{
Tejun Heo623f9262013-08-13 11:01:55 -04004329 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004330 int ret = 0;
4331
Tejun Heoa31f2d32012-11-19 08:13:37 -08004332 lockdep_assert_held(&cgroup_mutex);
4333
Tejun Heo92fb9742012-11-19 08:13:38 -08004334 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004335 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004336 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004337 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004338 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004339 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4340 }
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 Heo623f9262013-08-13 11:01:55 -04004345static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004346{
Tejun Heo623f9262013-08-13 11:01:55 -04004347 struct cgroup_subsys *ss = css->ss;
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{
Tejun Heoae7f1642013-08-13 20:22:50 -04004371 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004372 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004373 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004374 struct cgroupfs_root *root = parent->root;
4375 int err = 0;
4376 struct cgroup_subsys *ss;
4377 struct super_block *sb = root->sb;
4378
Tejun Heo0a950f62012-11-19 09:02:12 -08004379 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004380 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4381 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382 return -ENOMEM;
4383
Li Zefan65dff752013-03-01 15:01:56 +08004384 name = cgroup_alloc_name(dentry);
4385 if (!name)
4386 goto err_free_cgrp;
4387 rcu_assign_pointer(cgrp->name, name);
4388
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004389 /*
4390 * Temporarily set the pointer to NULL, so idr_find() won't return
4391 * a half-baked cgroup.
4392 */
4393 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004394 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004395 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004396
Tejun Heo976c06b2012-11-05 09:16:59 -08004397 /*
4398 * Only live parents can have children. Note that the liveliness
4399 * check isn't strictly necessary because cgroup_mkdir() and
4400 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4401 * anyway so that locking is contained inside cgroup proper and we
4402 * don't get nasty surprises if we ever grow another caller.
4403 */
4404 if (!cgroup_lock_live_group(parent)) {
4405 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004406 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004407 }
4408
Paul Menageddbcc7e2007-10-18 23:39:30 -07004409 /* Grab a reference on the superblock so the hierarchy doesn't
4410 * get deleted on unmount if there are child cgroups. This
4411 * can be done outside cgroup_mutex, since the sb can't
4412 * disappear while someone has an open control file on the
4413 * fs */
4414 atomic_inc(&sb->s_active);
4415
Paul Menagecc31edc2008-10-18 20:28:04 -07004416 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004417
Li Zefanfe1c06c2013-01-24 14:30:22 +08004418 dentry->d_fsdata = cgrp;
4419 cgrp->dentry = dentry;
4420
Paul Menagebd89aab2007-10-18 23:40:44 -07004421 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004422 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004423 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004424
Li Zefanb6abdb02008-03-04 14:28:19 -08004425 if (notify_on_release(parent))
4426 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4427
Tejun Heo2260e7f2012-11-19 08:13:38 -08004428 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4429 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004430
Tejun Heo5549c492013-06-24 15:21:48 -07004431 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004432 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004433
Tejun Heo40e93b32013-08-13 11:01:53 -04004434 css = ss->css_alloc(cgroup_css(parent, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004435 if (IS_ERR(css)) {
4436 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004437 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004438 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004439 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004440
4441 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004442 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004443 goto err_free_all;
4444
Tejun Heo623f9262013-08-13 11:01:55 -04004445 init_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004446
Li Zefan4528fd02010-02-02 13:44:10 -08004447 if (ss->use_id) {
Tejun Heo623f9262013-08-13 11:01:55 -04004448 err = alloc_css_id(css);
Li Zefan4528fd02010-02-02 13:44:10 -08004449 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) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004472 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo0ae78e02013-08-13 11:01:54 -04004473
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 Heoae7f1642013-08-13 20:22:50 -04004483 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004484
4485 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004486 if (err)
4487 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004488
4489 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4490 parent->parent) {
4491 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",
4492 current->comm, current->pid, ss->name);
4493 if (!strcmp(ss->name, "memory"))
4494 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4495 ss->warned_broken_hierarchy = true;
4496 }
Tejun Heoa8638032012-11-09 09:12:29 -08004497 }
4498
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004499 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4500
Tejun Heo2bb566c2013-08-08 20:11:23 -04004501 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004502 if (err)
4503 goto err_destroy;
4504
4505 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004506 if (err)
4507 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004508
4509 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004510 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004511
4512 return 0;
4513
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004514err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004515 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004516 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004517
4518 if (css) {
4519 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004520 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004521 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004522 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004523 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004524 /* Release the reference count that we took on the superblock */
4525 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004526err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004527 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004528err_free_name:
4529 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004530err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004531 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004532 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004533
4534err_destroy:
4535 cgroup_destroy_locked(cgrp);
4536 mutex_unlock(&cgroup_mutex);
4537 mutex_unlock(&dentry->d_inode->i_mutex);
4538 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004539}
4540
Al Viro18bb1db2011-07-26 01:41:39 -04004541static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004542{
4543 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4544
4545 /* the vfs holds inode->i_mutex already */
4546 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4547}
4548
Tejun Heo223dbc32013-08-13 20:22:50 -04004549/*
4550 * This is called when the refcnt of a css is confirmed to be killed.
4551 * css_tryget() is now guaranteed to fail.
4552 */
4553static void css_killed_work_fn(struct work_struct *work)
4554{
4555 struct cgroup_subsys_state *css =
4556 container_of(work, struct cgroup_subsys_state, destroy_work);
4557 struct cgroup *cgrp = css->cgroup;
4558
Tejun Heof20104d2013-08-13 20:22:50 -04004559 mutex_lock(&cgroup_mutex);
4560
4561 /*
4562 * If @cgrp is marked dead, it's waiting for refs of all css's to
4563 * be disabled before proceeding to the second phase of cgroup
4564 * destruction. If we are the last one, kick it off.
4565 */
4566 if (!--cgrp->nr_css && cgroup_is_dead(cgrp))
4567 cgroup_destroy_css_killed(cgrp);
4568
4569 mutex_unlock(&cgroup_mutex);
Tejun Heo223dbc32013-08-13 20:22:50 -04004570}
4571
4572/* css kill confirmation processing requires process context, bounce */
4573static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004574{
4575 struct cgroup_subsys_state *css =
4576 container_of(ref, struct cgroup_subsys_state, refcnt);
4577
Tejun Heo223dbc32013-08-13 20:22:50 -04004578 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4579 schedule_work(&css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004580}
4581
4582/**
4583 * cgroup_destroy_locked - the first stage of cgroup destruction
4584 * @cgrp: cgroup to be destroyed
4585 *
4586 * css's make use of percpu refcnts whose killing latency shouldn't be
4587 * exposed to userland and are RCU protected. Also, cgroup core needs to
4588 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4589 * invoked. To satisfy all the requirements, destruction is implemented in
4590 * the following two steps.
4591 *
4592 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4593 * userland visible parts and start killing the percpu refcnts of
4594 * css's. Set up so that the next stage will be kicked off once all
4595 * the percpu refcnts are confirmed to be killed.
4596 *
4597 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4598 * rest of destruction. Once all cgroup references are gone, the
4599 * cgroup is RCU-freed.
4600 *
4601 * This function implements s1. After this step, @cgrp is gone as far as
4602 * the userland is concerned and a new cgroup with the same name may be
4603 * created. As cgroup doesn't care about the names internally, this
4604 * doesn't cause any problem.
4605 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004606static int cgroup_destroy_locked(struct cgroup *cgrp)
4607 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004608{
Tejun Heo42809dd2012-11-19 08:13:37 -08004609 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004610 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004611 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004612 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613
Tejun Heo42809dd2012-11-19 08:13:37 -08004614 lockdep_assert_held(&d->d_inode->i_mutex);
4615 lockdep_assert_held(&cgroup_mutex);
4616
Tejun Heoddd69142013-06-12 21:04:54 -07004617 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004618 * css_set_lock synchronizes access to ->cset_links and prevents
4619 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004620 */
4621 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004622 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004623 read_unlock(&css_set_lock);
4624 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004626
Tejun Heo1a90dd52012-11-05 09:16:59 -08004627 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004628 * Block new css_tryget() by killing css refcnts. cgroup core
4629 * guarantees that, by the time ->css_offline() is invoked, no new
4630 * css reference will be given out via css_tryget(). We can't
4631 * simply call percpu_ref_kill() and proceed to offlining css's
4632 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4633 * as killed on all CPUs on return.
4634 *
4635 * Use percpu_ref_kill_and_confirm() to get notifications as each
4636 * css is confirmed to be seen as killed on all CPUs. The
4637 * notification callback keeps track of the number of css's to be
Tejun Heof20104d2013-08-13 20:22:50 -04004638 * killed and invokes cgroup_destroy_css_killed() to perform the
4639 * rest of destruction once the percpu refs of all css's are
4640 * confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004641 */
Tejun Heo5549c492013-06-24 15:21:48 -07004642 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo40e93b32013-08-13 11:01:53 -04004643 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss->subsys_id);
Tejun Heoed9577932012-11-05 09:16:58 -08004644
Tejun Heod3daf282013-06-13 19:39:16 -07004645 /*
4646 * Killing would put the base ref, but we need to keep it
4647 * alive until after ->css_offline.
4648 */
4649 percpu_ref_get(&css->refcnt);
4650
Tejun Heo223dbc32013-08-13 20:22:50 -04004651 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004652 }
Tejun Heo455050d2013-06-13 19:27:41 -07004653
4654 /*
4655 * Mark @cgrp dead. This prevents further task migration and child
4656 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004657 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004658 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004659 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004660 */
Tejun Heo54766d42013-06-12 21:04:53 -07004661 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004662
Tejun Heo455050d2013-06-13 19:27:41 -07004663 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4664 raw_spin_lock(&release_list_lock);
4665 if (!list_empty(&cgrp->release_list))
4666 list_del_init(&cgrp->release_list);
4667 raw_spin_unlock(&release_list_lock);
4668
4669 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004670 * If @cgrp has css's attached, the second stage of cgroup
4671 * destruction is kicked off from css_killed_work_fn() after the
4672 * refs of all attached css's are killed. If @cgrp doesn't have
4673 * any css, we kick it off here.
4674 */
4675 if (!cgrp->nr_css)
4676 cgroup_destroy_css_killed(cgrp);
4677
4678 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004679 * Clear and remove @cgrp directory. The removal puts the base ref
4680 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004681 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004682 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004683 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004684 dget(d);
4685 cgroup_d_remove_dir(d);
4686
4687 /*
4688 * Unregister events and notify userspace.
4689 * Notify userspace about cgroup removing only after rmdir of cgroup
4690 * directory to avoid race between userspace and kernelspace.
4691 */
4692 spin_lock(&cgrp->event_list_lock);
4693 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4694 list_del_init(&event->list);
4695 schedule_work(&event->remove);
4696 }
4697 spin_unlock(&cgrp->event_list_lock);
4698
Tejun Heoea15f8c2013-06-13 19:27:42 -07004699 return 0;
4700};
4701
Tejun Heod3daf282013-06-13 19:39:16 -07004702/**
Tejun Heof20104d2013-08-13 20:22:50 -04004703 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004704 * @work: cgroup->destroy_free_work
4705 *
4706 * This function is invoked from a work item for a cgroup which is being
4707 * destroyed after the percpu refcnts of all css's are guaranteed to be
4708 * seen as killed on all CPUs, and performs the rest of destruction. This
4709 * is the second step of destruction described in the comment above
4710 * cgroup_destroy_locked().
4711 */
Tejun Heof20104d2013-08-13 20:22:50 -04004712static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004713{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004714 struct cgroup *parent = cgrp->parent;
4715 struct dentry *d = cgrp->dentry;
4716 struct cgroup_subsys *ss;
4717
Tejun Heof20104d2013-08-13 20:22:50 -04004718 lockdep_assert_held(&cgroup_mutex);
Tejun Heoea15f8c2013-06-13 19:27:42 -07004719
Tejun Heod3daf282013-06-13 19:39:16 -07004720 /*
4721 * css_tryget() is guaranteed to fail now. Tell subsystems to
4722 * initate destruction.
4723 */
Tejun Heo5549c492013-06-24 15:21:48 -07004724 for_each_root_subsys(cgrp->root, ss)
Tejun Heo623f9262013-08-13 11:01:55 -04004725 offline_css(cgroup_css(cgrp, ss->subsys_id));
Tejun Heoed9577932012-11-05 09:16:58 -08004726
4727 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004728 * Put the css refs from cgroup_destroy_locked(). Each css holds
4729 * an extra reference to the cgroup's dentry and cgroup removal
4730 * proceeds regardless of css refs. On the last put of each css,
4731 * whenever that may be, the extra dentry ref is put so that dentry
4732 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004733 */
Tejun Heo5549c492013-06-24 15:21:48 -07004734 for_each_root_subsys(cgrp->root, ss)
Tejun Heo40e93b32013-08-13 11:01:53 -04004735 css_put(cgroup_css(cgrp, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004736
Paul Menage999cd8a2009-01-07 18:08:36 -08004737 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004738 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004739
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004740 /*
4741 * We should remove the cgroup object from idr before its grace
4742 * period starts, so we won't be looking up a cgroup while the
4743 * cgroup is being freed.
4744 */
4745 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4746 cgrp->id = -1;
4747
Paul Menageddbcc7e2007-10-18 23:39:30 -07004748 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004749
Paul Menagebd89aab2007-10-18 23:40:44 -07004750 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004751 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004752}
4753
Tejun Heo42809dd2012-11-19 08:13:37 -08004754static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4755{
4756 int ret;
4757
4758 mutex_lock(&cgroup_mutex);
4759 ret = cgroup_destroy_locked(dentry->d_fsdata);
4760 mutex_unlock(&cgroup_mutex);
4761
4762 return ret;
4763}
4764
Tejun Heo8e3f6542012-04-01 12:09:55 -07004765static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4766{
4767 INIT_LIST_HEAD(&ss->cftsets);
4768
4769 /*
4770 * base_cftset is embedded in subsys itself, no need to worry about
4771 * deregistration.
4772 */
4773 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004774 struct cftype *cft;
4775
4776 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4777 cft->ss = ss;
4778
Tejun Heo8e3f6542012-04-01 12:09:55 -07004779 ss->base_cftset.cfts = ss->base_cftypes;
4780 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4781 }
4782}
4783
Li Zefan06a11922008-04-29 01:00:07 -07004784static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004785{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004786 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004787
4788 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004789
Tejun Heo648bb562012-11-19 08:13:36 -08004790 mutex_lock(&cgroup_mutex);
4791
Tejun Heo8e3f6542012-04-01 12:09:55 -07004792 /* init base cftset */
4793 cgroup_init_cftsets(ss);
4794
Paul Menageddbcc7e2007-10-18 23:39:30 -07004795 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004796 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4797 ss->root = &cgroup_dummy_root;
Tejun Heo40e93b32013-08-13 11:01:53 -04004798 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004799 /* We don't handle early failures gracefully */
4800 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004801 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004802
Li Zefane8d55fd2008-04-29 01:00:13 -07004803 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004804 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004805 * newly registered, all tasks and hence the
4806 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004807 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004808
4809 need_forkexit_callback |= ss->fork || ss->exit;
4810
Li Zefane8d55fd2008-04-29 01:00:13 -07004811 /* At system boot, before all subsystems have been
4812 * registered, no tasks have been forked, so we don't
4813 * need to invoke fork callbacks here. */
4814 BUG_ON(!list_empty(&init_task.tasks));
4815
Tejun Heoae7f1642013-08-13 20:22:50 -04004816 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004817
Tejun Heo648bb562012-11-19 08:13:36 -08004818 mutex_unlock(&cgroup_mutex);
4819
Ben Blume6a11052010-03-10 15:22:09 -08004820 /* this function shouldn't be used with modular subsystems, since they
4821 * need to register a subsys_id, among other things */
4822 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004823}
4824
4825/**
Ben Blume6a11052010-03-10 15:22:09 -08004826 * cgroup_load_subsys: load and register a modular subsystem at runtime
4827 * @ss: the subsystem to load
4828 *
4829 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004830 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004831 * up for use. If the subsystem is built-in anyway, work is delegated to the
4832 * simpler cgroup_init_subsys.
4833 */
4834int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4835{
Ben Blume6a11052010-03-10 15:22:09 -08004836 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004837 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004838 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004839 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004840 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004841
4842 /* check name and function validity */
4843 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004844 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004845 return -EINVAL;
4846
4847 /*
4848 * we don't support callbacks in modular subsystems. this check is
4849 * before the ss->module check for consistency; a subsystem that could
4850 * be a module should still have no callbacks even if the user isn't
4851 * compiling it as one.
4852 */
4853 if (ss->fork || ss->exit)
4854 return -EINVAL;
4855
4856 /*
4857 * an optionally modular subsystem is built-in: we want to do nothing,
4858 * since cgroup_init_subsys will have already taken care of it.
4859 */
4860 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004861 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004862 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004863 return 0;
4864 }
4865
Tejun Heo8e3f6542012-04-01 12:09:55 -07004866 /* init base cftset */
4867 cgroup_init_cftsets(ss);
4868
Ben Blume6a11052010-03-10 15:22:09 -08004869 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004870 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004871
4872 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004873 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004874 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004875 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004876 */
Tejun Heo40e93b32013-08-13 11:01:53 -04004877 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Ben Blume6a11052010-03-10 15:22:09 -08004878 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004879 /* failure case - need to deassign the cgroup_subsys[] slot. */
4880 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004881 mutex_unlock(&cgroup_mutex);
4882 return PTR_ERR(css);
4883 }
4884
Tejun Heo9871bf92013-06-24 15:21:47 -07004885 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4886 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004887
4888 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004889 init_css(css, ss, cgroup_dummy_top);
4890 /* init_idr must be after init_css() because it sets css->id. */
Ben Blume6a11052010-03-10 15:22:09 -08004891 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004892 ret = cgroup_init_idr(ss, css);
4893 if (ret)
4894 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004895 }
4896
4897 /*
4898 * Now we need to entangle the css into the existing css_sets. unlike
4899 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4900 * will need a new pointer to it; done by iterating the css_set_table.
4901 * furthermore, modifying the existing css_sets will corrupt the hash
4902 * table state, so each changed css_set will need its hash recomputed.
4903 * this is all done under the css_set_lock.
4904 */
4905 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004906 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004907 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004908 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004909 continue;
4910 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004911 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004912 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004913 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004914 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004915 key = css_set_hash(cset->subsys);
4916 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004917 }
4918 write_unlock(&css_set_lock);
4919
Tejun Heoae7f1642013-08-13 20:22:50 -04004920 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004921 if (ret)
4922 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004923
Ben Blume6a11052010-03-10 15:22:09 -08004924 /* success! */
4925 mutex_unlock(&cgroup_mutex);
4926 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004927
4928err_unload:
4929 mutex_unlock(&cgroup_mutex);
4930 /* @ss can't be mounted here as try_module_get() would fail */
4931 cgroup_unload_subsys(ss);
4932 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004933}
4934EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4935
4936/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004937 * cgroup_unload_subsys: unload a modular subsystem
4938 * @ss: the subsystem to unload
4939 *
4940 * This function should be called in a modular subsystem's exitcall. When this
4941 * function is invoked, the refcount on the subsystem's module will be 0, so
4942 * the subsystem will not be attached to any hierarchy.
4943 */
4944void cgroup_unload_subsys(struct cgroup_subsys *ss)
4945{
Tejun Heo69d02062013-06-12 21:04:50 -07004946 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004947
4948 BUG_ON(ss->module == NULL);
4949
4950 /*
4951 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004952 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004953 * doesn't start being used while we're killing it off.
4954 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004955 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004956
4957 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004958
Tejun Heo623f9262013-08-13 11:01:55 -04004959 offline_css(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Tejun Heo02ae7482012-11-19 08:13:37 -08004960
Tejun Heoc897ff62013-02-27 17:03:49 -08004961 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004962 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004963
Ben Blumcf5d5942010-03-10 15:22:09 -08004964 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004965 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004966
Tejun Heo9871bf92013-06-24 15:21:47 -07004967 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004968 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004969
4970 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004971 * disentangle the css from all css_sets attached to the dummy
4972 * top. as in loading, we need to pay our respects to the hashtable
4973 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004974 */
4975 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004976 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004977 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004978 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004979
Tejun Heo5abb8852013-06-12 21:04:49 -07004980 hash_del(&cset->hlist);
4981 cset->subsys[ss->subsys_id] = NULL;
4982 key = css_set_hash(cset->subsys);
4983 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004984 }
4985 write_unlock(&css_set_lock);
4986
4987 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004988 * remove subsystem's css from the cgroup_dummy_top and free it -
4989 * need to free before marking as null because ss->css_free needs
4990 * the cgrp->subsys pointer to find their state. note that this
4991 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004992 */
Tejun Heo40e93b32013-08-13 11:01:53 -04004993 ss->css_free(cgroup_css(cgroup_dummy_top, ss->subsys_id));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004994 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004995
4996 mutex_unlock(&cgroup_mutex);
4997}
4998EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4999
5000/**
Li Zefana043e3b2008-02-23 15:24:09 -08005001 * cgroup_init_early - cgroup initialization at system boot
5002 *
5003 * Initialize cgroups at system boot, and initialize any
5004 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005005 */
5006int __init cgroup_init_early(void)
5007{
Tejun Heo30159ec2013-06-25 11:53:37 -07005008 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005009 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005010
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07005011 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07005012 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07005013 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07005014 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07005015 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07005016 init_cgroup_root(&cgroup_dummy_root);
5017 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005018 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005019
Tejun Heo69d02062013-06-12 21:04:50 -07005020 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07005021 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
5022 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07005023 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005024
Tejun Heo30159ec2013-06-25 11:53:37 -07005025 /* at bootup time, we don't worry about modular subsystems */
5026 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005027 BUG_ON(!ss->name);
5028 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08005029 BUG_ON(!ss->css_alloc);
5030 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005031 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08005032 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07005033 ss->name, ss->subsys_id);
5034 BUG();
5035 }
5036
5037 if (ss->early_init)
5038 cgroup_init_subsys(ss);
5039 }
5040 return 0;
5041}
5042
5043/**
Li Zefana043e3b2008-02-23 15:24:09 -08005044 * cgroup_init - cgroup initialization
5045 *
5046 * Register cgroup filesystem and /proc file, and initialize
5047 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005048 */
5049int __init cgroup_init(void)
5050{
Tejun Heo30159ec2013-06-25 11:53:37 -07005051 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005052 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005053 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005054
5055 err = bdi_init(&cgroup_backing_dev_info);
5056 if (err)
5057 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005058
Tejun Heo30159ec2013-06-25 11:53:37 -07005059 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005060 if (!ss->early_init)
5061 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005062 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08005063 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005064 }
5065
Tejun Heofa3ca072013-04-14 11:36:56 -07005066 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005067 mutex_lock(&cgroup_mutex);
5068 mutex_lock(&cgroup_root_mutex);
5069
Tejun Heo82fe9b02013-06-25 11:53:37 -07005070 /* Add init_css_set to the hash table */
5071 key = css_set_hash(init_css_set.subsys);
5072 hash_add(css_set_table, &init_css_set.hlist, key);
5073
Tejun Heofc76df72013-06-25 11:53:37 -07005074 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005075
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005076 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5077 0, 1, GFP_KERNEL);
5078 BUG_ON(err < 0);
5079
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005080 mutex_unlock(&cgroup_root_mutex);
5081 mutex_unlock(&cgroup_mutex);
5082
Greg KH676db4a2010-08-05 13:53:35 -07005083 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5084 if (!cgroup_kobj) {
5085 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005086 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005087 }
5088
5089 err = register_filesystem(&cgroup_fs_type);
5090 if (err < 0) {
5091 kobject_put(cgroup_kobj);
5092 goto out;
5093 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005094
Li Zefan46ae2202008-04-29 01:00:08 -07005095 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005096
Paul Menageddbcc7e2007-10-18 23:39:30 -07005097out:
Paul Menagea4243162007-10-18 23:39:35 -07005098 if (err)
5099 bdi_destroy(&cgroup_backing_dev_info);
5100
Paul Menageddbcc7e2007-10-18 23:39:30 -07005101 return err;
5102}
Paul Menageb4f48b62007-10-18 23:39:33 -07005103
Paul Menagea4243162007-10-18 23:39:35 -07005104/*
5105 * proc_cgroup_show()
5106 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5107 * - Used for /proc/<pid>/cgroup.
5108 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5109 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005110 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005111 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5112 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5113 * cgroup to top_cgroup.
5114 */
5115
5116/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005117int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005118{
5119 struct pid *pid;
5120 struct task_struct *tsk;
5121 char *buf;
5122 int retval;
5123 struct cgroupfs_root *root;
5124
5125 retval = -ENOMEM;
5126 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5127 if (!buf)
5128 goto out;
5129
5130 retval = -ESRCH;
5131 pid = m->private;
5132 tsk = get_pid_task(pid, PIDTYPE_PID);
5133 if (!tsk)
5134 goto out_free;
5135
5136 retval = 0;
5137
5138 mutex_lock(&cgroup_mutex);
5139
Li Zefane5f6a862009-01-07 18:07:41 -08005140 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005141 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005142 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005143 int count = 0;
5144
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005145 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005146 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005147 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005148 if (strlen(root->name))
5149 seq_printf(m, "%sname=%s", count ? "," : "",
5150 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005151 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005152 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005153 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005154 if (retval < 0)
5155 goto out_unlock;
5156 seq_puts(m, buf);
5157 seq_putc(m, '\n');
5158 }
5159
5160out_unlock:
5161 mutex_unlock(&cgroup_mutex);
5162 put_task_struct(tsk);
5163out_free:
5164 kfree(buf);
5165out:
5166 return retval;
5167}
5168
Paul Menagea4243162007-10-18 23:39:35 -07005169/* Display information about each subsystem and each hierarchy */
5170static int proc_cgroupstats_show(struct seq_file *m, void *v)
5171{
Tejun Heo30159ec2013-06-25 11:53:37 -07005172 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005173 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005174
Paul Menage8bab8dd2008-04-04 14:29:57 -07005175 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005176 /*
5177 * ideally we don't want subsystems moving around while we do this.
5178 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5179 * subsys/hierarchy state.
5180 */
Paul Menagea4243162007-10-18 23:39:35 -07005181 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005182
5183 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005184 seq_printf(m, "%s\t%d\t%d\t%d\n",
5185 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005186 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005187
Paul Menagea4243162007-10-18 23:39:35 -07005188 mutex_unlock(&cgroup_mutex);
5189 return 0;
5190}
5191
5192static int cgroupstats_open(struct inode *inode, struct file *file)
5193{
Al Viro9dce07f2008-03-29 03:07:28 +00005194 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005195}
5196
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005197static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005198 .open = cgroupstats_open,
5199 .read = seq_read,
5200 .llseek = seq_lseek,
5201 .release = single_release,
5202};
5203
Paul Menageb4f48b62007-10-18 23:39:33 -07005204/**
5205 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005206 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005207 *
5208 * Description: A task inherits its parent's cgroup at fork().
5209 *
5210 * A pointer to the shared css_set was automatically copied in
5211 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005212 * it was not made under the protection of RCU or cgroup_mutex, so
5213 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5214 * have already changed current->cgroups, allowing the previously
5215 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005216 *
5217 * At the point that cgroup_fork() is called, 'current' is the parent
5218 * task, and the passed argument 'child' points to the child task.
5219 */
5220void cgroup_fork(struct task_struct *child)
5221{
Tejun Heo9bb71302012-10-18 17:52:07 -07005222 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005223 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005224 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005225 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005226 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005227}
5228
5229/**
Li Zefana043e3b2008-02-23 15:24:09 -08005230 * cgroup_post_fork - called on a new task after adding it to the task list
5231 * @child: the task in question
5232 *
Tejun Heo5edee612012-10-16 15:03:14 -07005233 * Adds the task to the list running through its css_set if necessary and
5234 * call the subsystem fork() callbacks. Has to be after the task is
5235 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005236 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005237 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005238 */
Paul Menage817929e2007-10-18 23:39:36 -07005239void cgroup_post_fork(struct task_struct *child)
5240{
Tejun Heo30159ec2013-06-25 11:53:37 -07005241 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005242 int i;
5243
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005244 /*
5245 * use_task_css_set_links is set to 1 before we walk the tasklist
5246 * under the tasklist_lock and we read it here after we added the child
5247 * to the tasklist under the tasklist_lock as well. If the child wasn't
5248 * yet in the tasklist when we walked through it from
5249 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5250 * should be visible now due to the paired locking and barriers implied
5251 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5252 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5253 * lock on fork.
5254 */
Paul Menage817929e2007-10-18 23:39:36 -07005255 if (use_task_css_set_links) {
5256 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005257 task_lock(child);
5258 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005259 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005260 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005261 write_unlock(&css_set_lock);
5262 }
Tejun Heo5edee612012-10-16 15:03:14 -07005263
5264 /*
5265 * Call ss->fork(). This must happen after @child is linked on
5266 * css_set; otherwise, @child might change state between ->fork()
5267 * and addition to css_set.
5268 */
5269 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005270 /*
5271 * fork/exit callbacks are supported only for builtin
5272 * subsystems, and the builtin section of the subsys
5273 * array is immutable, so we don't need to lock the
5274 * subsys array here. On the other hand, modular section
5275 * of the array can be freed at module unload, so we
5276 * can't touch that.
5277 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005278 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005279 if (ss->fork)
5280 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005281 }
Paul Menage817929e2007-10-18 23:39:36 -07005282}
Tejun Heo5edee612012-10-16 15:03:14 -07005283
Paul Menage817929e2007-10-18 23:39:36 -07005284/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005285 * cgroup_exit - detach cgroup from exiting task
5286 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005287 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005288 *
5289 * Description: Detach cgroup from @tsk and release it.
5290 *
5291 * Note that cgroups marked notify_on_release force every task in
5292 * them to take the global cgroup_mutex mutex when exiting.
5293 * This could impact scaling on very large systems. Be reluctant to
5294 * use notify_on_release cgroups where very high task exit scaling
5295 * is required on large systems.
5296 *
5297 * the_top_cgroup_hack:
5298 *
5299 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5300 *
5301 * We call cgroup_exit() while the task is still competent to
5302 * handle notify_on_release(), then leave the task attached to the
5303 * root cgroup in each hierarchy for the remainder of its exit.
5304 *
5305 * To do this properly, we would increment the reference count on
5306 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5307 * code we would add a second cgroup function call, to drop that
5308 * reference. This would just create an unnecessary hot spot on
5309 * the top_cgroup reference count, to no avail.
5310 *
5311 * Normally, holding a reference to a cgroup without bumping its
5312 * count is unsafe. The cgroup could go away, or someone could
5313 * attach us to a different cgroup, decrementing the count on
5314 * the first cgroup that we never incremented. But in this case,
5315 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005316 * which wards off any cgroup_attach_task() attempts, or task is a failed
5317 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005318 */
5319void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5320{
Tejun Heo30159ec2013-06-25 11:53:37 -07005321 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005322 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005323 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005324
5325 /*
5326 * Unlink from the css_set task list if necessary.
5327 * Optimistically check cg_list before taking
5328 * css_set_lock
5329 */
5330 if (!list_empty(&tsk->cg_list)) {
5331 write_lock(&css_set_lock);
5332 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005333 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005334 write_unlock(&css_set_lock);
5335 }
5336
Paul Menageb4f48b62007-10-18 23:39:33 -07005337 /* Reassign the task to the init_css_set. */
5338 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005339 cset = task_css_set(tsk);
5340 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005341
5342 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005343 /*
5344 * fork/exit callbacks are supported only for builtin
5345 * subsystems, see cgroup_post_fork() for details.
5346 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005347 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005348 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005349 struct cgroup_subsys_state *old_css = cset->subsys[i];
5350 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005351
Tejun Heoeb954192013-08-08 20:11:23 -04005352 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005353 }
5354 }
5355 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005356 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005357
Tejun Heo5abb8852013-06-12 21:04:49 -07005358 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005359}
Paul Menage697f4162007-10-18 23:39:34 -07005360
Paul Menagebd89aab2007-10-18 23:40:44 -07005361static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005362{
Li Zefanf50daa72013-03-01 15:06:07 +08005363 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005364 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005365 /*
5366 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005367 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005368 * it now
5369 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005370 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005371
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005372 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005373 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005374 list_empty(&cgrp->release_list)) {
5375 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005376 need_schedule_work = 1;
5377 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005378 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005379 if (need_schedule_work)
5380 schedule_work(&release_agent_work);
5381 }
5382}
5383
Paul Menage81a6a5c2007-10-18 23:39:38 -07005384/*
5385 * Notify userspace when a cgroup is released, by running the
5386 * configured release agent with the name of the cgroup (path
5387 * relative to the root of cgroup file system) as the argument.
5388 *
5389 * Most likely, this user command will try to rmdir this cgroup.
5390 *
5391 * This races with the possibility that some other task will be
5392 * attached to this cgroup before it is removed, or that some other
5393 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5394 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5395 * unused, and this cgroup will be reprieved from its death sentence,
5396 * to continue to serve a useful existence. Next time it's released,
5397 * we will get notified again, if it still has 'notify_on_release' set.
5398 *
5399 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5400 * means only wait until the task is successfully execve()'d. The
5401 * separate release agent task is forked by call_usermodehelper(),
5402 * then control in this thread returns here, without waiting for the
5403 * release agent task. We don't bother to wait because the caller of
5404 * this routine has no use for the exit status of the release agent
5405 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005406 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005407static void cgroup_release_agent(struct work_struct *work)
5408{
5409 BUG_ON(work != &release_agent_work);
5410 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005411 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005412 while (!list_empty(&release_list)) {
5413 char *argv[3], *envp[3];
5414 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005415 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005416 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005417 struct cgroup,
5418 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005419 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005420 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005421 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005422 if (!pathbuf)
5423 goto continue_free;
5424 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5425 goto continue_free;
5426 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5427 if (!agentbuf)
5428 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005429
5430 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005431 argv[i++] = agentbuf;
5432 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005433 argv[i] = NULL;
5434
5435 i = 0;
5436 /* minimal command environment */
5437 envp[i++] = "HOME=/";
5438 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5439 envp[i] = NULL;
5440
5441 /* Drop the lock while we invoke the usermode helper,
5442 * since the exec could involve hitting disk and hence
5443 * be a slow process */
5444 mutex_unlock(&cgroup_mutex);
5445 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005446 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005447 continue_free:
5448 kfree(pathbuf);
5449 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005450 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005451 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005452 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005453 mutex_unlock(&cgroup_mutex);
5454}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005455
5456static int __init cgroup_disable(char *str)
5457{
Tejun Heo30159ec2013-06-25 11:53:37 -07005458 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005459 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005460 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005461
5462 while ((token = strsep(&str, ",")) != NULL) {
5463 if (!*token)
5464 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005465
Tejun Heo30159ec2013-06-25 11:53:37 -07005466 /*
5467 * cgroup_disable, being at boot time, can't know about
5468 * module subsystems, so we don't worry about them.
5469 */
5470 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005471 if (!strcmp(token, ss->name)) {
5472 ss->disabled = 1;
5473 printk(KERN_INFO "Disabling %s control group"
5474 " subsystem\n", ss->name);
5475 break;
5476 }
5477 }
5478 }
5479 return 1;
5480}
5481__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005482
5483/*
5484 * Functons for CSS ID.
5485 */
5486
Tejun Heo54766d42013-06-12 21:04:53 -07005487/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005488unsigned short css_id(struct cgroup_subsys_state *css)
5489{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005490 struct css_id *cssid;
5491
5492 /*
5493 * This css_id() can return correct value when somone has refcnt
5494 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5495 * it's unchanged until freed.
5496 */
Tejun Heod3daf282013-06-13 19:39:16 -07005497 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005498
5499 if (cssid)
5500 return cssid->id;
5501 return 0;
5502}
Ben Blum67523c42010-03-10 15:22:11 -08005503EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005504
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005505/**
5506 * css_is_ancestor - test "root" css is an ancestor of "child"
5507 * @child: the css to be tested.
5508 * @root: the css supporsed to be an ancestor of the child.
5509 *
5510 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005511 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005512 * But, considering usual usage, the csses should be valid objects after test.
5513 * Assuming that the caller will do some action to the child if this returns
5514 * returns true, the caller must take "child";s reference count.
5515 * If "child" is valid object and this returns true, "root" is valid, too.
5516 */
5517
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005518bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005519 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005520{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005521 struct css_id *child_id;
5522 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005523
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005524 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005525 if (!child_id)
5526 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005527 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005528 if (!root_id)
5529 return false;
5530 if (child_id->depth < root_id->depth)
5531 return false;
5532 if (child_id->stack[root_id->depth] != root_id->id)
5533 return false;
5534 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005535}
5536
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005537void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5538{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005539 struct css_id *id = rcu_dereference_protected(css->id, true);
5540
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005541 /* When this is called before css_id initialization, id can be NULL */
5542 if (!id)
5543 return;
5544
5545 BUG_ON(!ss->use_id);
5546
5547 rcu_assign_pointer(id->css, NULL);
5548 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005549 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005550 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005551 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005552 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005553}
Ben Blum67523c42010-03-10 15:22:11 -08005554EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005555
5556/*
5557 * This is called by init or create(). Then, calls to this function are
5558 * always serialized (By cgroup_mutex() at create()).
5559 */
5560
5561static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5562{
5563 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005564 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005565
5566 BUG_ON(!ss->use_id);
5567
5568 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5569 newid = kzalloc(size, GFP_KERNEL);
5570 if (!newid)
5571 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005572
5573 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005574 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005575 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005576 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005577 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005578 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005579
5580 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005581 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005582 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005583
Tejun Heod228d9e2013-02-27 17:04:54 -08005584 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005585 newid->depth = depth;
5586 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005587err_out:
5588 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005589 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005590
5591}
5592
Ben Blume6a11052010-03-10 15:22:09 -08005593static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5594 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005595{
5596 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005597
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005598 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005599 idr_init(&ss->idr);
5600
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005601 newid = get_new_cssid(ss, 0);
5602 if (IS_ERR(newid))
5603 return PTR_ERR(newid);
5604
5605 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005606 RCU_INIT_POINTER(newid->css, rootcss);
5607 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005608 return 0;
5609}
5610
Tejun Heo623f9262013-08-13 11:01:55 -04005611static int alloc_css_id(struct cgroup_subsys_state *child_css)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005612{
Tejun Heo623f9262013-08-13 11:01:55 -04005613 struct cgroup_subsys_state *parent_css = css_parent(child_css);
Li Zefanfae9c792010-04-22 17:30:00 +08005614 struct css_id *child_id, *parent_id;
Tejun Heo623f9262013-08-13 11:01:55 -04005615 int i, depth;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005616
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005617 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005618 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005619
Tejun Heo623f9262013-08-13 11:01:55 -04005620 child_id = get_new_cssid(child_css->ss, depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005621 if (IS_ERR(child_id))
5622 return PTR_ERR(child_id);
5623
5624 for (i = 0; i < depth; i++)
5625 child_id->stack[i] = parent_id->stack[i];
5626 child_id->stack[depth] = child_id->id;
5627 /*
5628 * child_id->css pointer will be set after this cgroup is available
5629 * see cgroup_populate_dir()
5630 */
5631 rcu_assign_pointer(child_css->id, child_id);
5632
5633 return 0;
5634}
5635
5636/**
5637 * css_lookup - lookup css by id
5638 * @ss: cgroup subsys to be looked into.
5639 * @id: the id
5640 *
5641 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5642 * NULL if not. Should be called under rcu_read_lock()
5643 */
5644struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5645{
5646 struct css_id *cssid = NULL;
5647
5648 BUG_ON(!ss->use_id);
5649 cssid = idr_find(&ss->idr, id);
5650
5651 if (unlikely(!cssid))
5652 return NULL;
5653
5654 return rcu_dereference(cssid->css);
5655}
Ben Blum67523c42010-03-10 15:22:11 -08005656EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005657
Tejun Heob77d7b62013-08-13 11:01:54 -04005658/**
5659 * cgroup_css_from_dir - get corresponding css from file open on cgroup dir
5660 * @f: directory file of interest
5661 * @id: subsystem id of interest
5662 *
5663 * Must be called under RCU read lock. The caller is responsible for
5664 * pinning the returned css if it needs to be accessed outside the RCU
5665 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005666 */
5667struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5668{
5669 struct cgroup *cgrp;
5670 struct inode *inode;
5671 struct cgroup_subsys_state *css;
5672
Tejun Heob77d7b62013-08-13 11:01:54 -04005673 WARN_ON_ONCE(!rcu_read_lock_held());
5674
Al Viro496ad9a2013-01-23 17:07:38 -05005675 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005676 /* check in cgroup filesystem dir */
5677 if (inode->i_op != &cgroup_dir_inode_operations)
5678 return ERR_PTR(-EBADF);
5679
5680 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5681 return ERR_PTR(-EINVAL);
5682
5683 /* get cgroup */
5684 cgrp = __d_cgrp(f->f_dentry);
Tejun Heo40e93b32013-08-13 11:01:53 -04005685 css = cgroup_css(cgrp, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005686 return css ? css : ERR_PTR(-ENOENT);
5687}
5688
Paul Menagefe693432009-09-23 15:56:20 -07005689#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005690static struct cgroup_subsys_state *
5691debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005692{
5693 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5694
5695 if (!css)
5696 return ERR_PTR(-ENOMEM);
5697
5698 return css;
5699}
5700
Tejun Heoeb954192013-08-08 20:11:23 -04005701static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005702{
Tejun Heoeb954192013-08-08 20:11:23 -04005703 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005704}
5705
Tejun Heo182446d2013-08-08 20:11:24 -04005706static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5707 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005708{
Tejun Heo182446d2013-08-08 20:11:24 -04005709 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005710}
5711
Tejun Heo182446d2013-08-08 20:11:24 -04005712static u64 current_css_set_read(struct cgroup_subsys_state *css,
5713 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005714{
5715 return (u64)(unsigned long)current->cgroups;
5716}
5717
Tejun Heo182446d2013-08-08 20:11:24 -04005718static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005719 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005720{
5721 u64 count;
5722
5723 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005724 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005725 rcu_read_unlock();
5726 return count;
5727}
5728
Tejun Heo182446d2013-08-08 20:11:24 -04005729static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005730 struct cftype *cft,
5731 struct seq_file *seq)
5732{
Tejun Heo69d02062013-06-12 21:04:50 -07005733 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005734 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005735
5736 read_lock(&css_set_lock);
5737 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005738 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005739 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005740 struct cgroup *c = link->cgrp;
5741 const char *name;
5742
5743 if (c->dentry)
5744 name = c->dentry->d_name.name;
5745 else
5746 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005747 seq_printf(seq, "Root %d group %s\n",
5748 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005749 }
5750 rcu_read_unlock();
5751 read_unlock(&css_set_lock);
5752 return 0;
5753}
5754
5755#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005756static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5757 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005758{
Tejun Heo69d02062013-06-12 21:04:50 -07005759 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005760
5761 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005762 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005763 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005764 struct task_struct *task;
5765 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005766 seq_printf(seq, "css_set %p\n", cset);
5767 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005768 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5769 seq_puts(seq, " ...\n");
5770 break;
5771 } else {
5772 seq_printf(seq, " task %d\n",
5773 task_pid_vnr(task));
5774 }
5775 }
5776 }
5777 read_unlock(&css_set_lock);
5778 return 0;
5779}
5780
Tejun Heo182446d2013-08-08 20:11:24 -04005781static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005782{
Tejun Heo182446d2013-08-08 20:11:24 -04005783 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005784}
5785
5786static struct cftype debug_files[] = {
5787 {
Paul Menagefe693432009-09-23 15:56:20 -07005788 .name = "taskcount",
5789 .read_u64 = debug_taskcount_read,
5790 },
5791
5792 {
5793 .name = "current_css_set",
5794 .read_u64 = current_css_set_read,
5795 },
5796
5797 {
5798 .name = "current_css_set_refcount",
5799 .read_u64 = current_css_set_refcount_read,
5800 },
5801
5802 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005803 .name = "current_css_set_cg_links",
5804 .read_seq_string = current_css_set_cg_links_read,
5805 },
5806
5807 {
5808 .name = "cgroup_css_links",
5809 .read_seq_string = cgroup_css_links_read,
5810 },
5811
5812 {
Paul Menagefe693432009-09-23 15:56:20 -07005813 .name = "releasable",
5814 .read_u64 = releasable_read,
5815 },
Paul Menagefe693432009-09-23 15:56:20 -07005816
Tejun Heo4baf6e32012-04-01 12:09:55 -07005817 { } /* terminate */
5818};
Paul Menagefe693432009-09-23 15:56:20 -07005819
5820struct cgroup_subsys debug_subsys = {
5821 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005822 .css_alloc = debug_css_alloc,
5823 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005824 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005825 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005826};
5827#endif /* CONFIG_CGROUP_DEBUG */