blob: 3251cc9070fa33198b445f01f5f8f45d85fa651a [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
Joe Perchesed3d2612014-04-25 18:28:03 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Paul Menageddbcc7e2007-10-18 23:39:30 -070031#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100032#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070033#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100035#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/kernel.h>
37#include <linux/list.h>
38#include <linux/mm.h>
39#include <linux/mutex.h>
40#include <linux/mount.h>
41#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070042#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/rcupdate.h>
44#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/spinlock.h>
Tejun Heo96d365e2014-02-13 06:58:40 -050047#include <linux/rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070048#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>
Balbir Singh846c7bb2007-10-18 23:39:44 -070051#include <linux/delayacct.h>
52#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080053#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070054#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070055#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070056#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020057#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050058#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070059
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070061
Tejun Heoe25e2cb2011-12-12 18:12:21 -080062/*
Tejun Heob1a21362013-11-29 10:42:58 -050063 * pidlists linger the following amount before being destroyed. The goal
64 * is avoiding frequent destruction in the middle of consecutive read calls
65 * Expiring in the middle is a performance problem not a correctness one.
66 * 1 sec should be enough.
67 */
68#define CGROUP_PIDLIST_DESTROY_DELAY HZ
69
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050070#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
71 MAX_CFTYPE_NAME + 2)
72
Tejun Heob1a21362013-11-29 10:42:58 -050073/*
Tejun Heoace2bee2014-02-11 11:52:47 -050074 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
75 * creation/removal and hierarchy changing operations including cgroup
76 * creation, removal, css association and controller rebinding. This outer
77 * lock is needed mainly to resolve the circular dependency between kernfs
78 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
79 */
80static DEFINE_MUTEX(cgroup_tree_mutex);
81
Tejun Heoe25e2cb2011-12-12 18:12:21 -080082/*
83 * cgroup_mutex is the master lock. Any modification to cgroup or its
84 * hierarchy must be performed while holding it.
85 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050086 * css_set_rwsem protects task->cgroups pointer, the list of css_set
87 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080088 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050089 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
90 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080091 */
Tejun Heo22194492013-04-07 09:29:51 -070092#ifdef CONFIG_PROVE_RCU
93DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050094DECLARE_RWSEM(css_set_rwsem);
95EXPORT_SYMBOL_GPL(cgroup_mutex);
96EXPORT_SYMBOL_GPL(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -070097#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070098static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo0e1d7682014-02-25 10:04:03 -050099static DECLARE_RWSEM(css_set_rwsem);
Tejun Heo22194492013-04-07 09:29:51 -0700100#endif
101
Tejun Heo69e943b2014-02-08 10:36:58 -0500102/*
Tejun Heo15a4c832014-05-04 15:09:14 -0400103 * Protects cgroup_idr and css_idr so that IDs can be released without
104 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -0400105 */
106static DEFINE_SPINLOCK(cgroup_idr_lock);
107
108/*
Tejun Heo69e943b2014-02-08 10:36:58 -0500109 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
110 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
111 */
112static DEFINE_SPINLOCK(release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700113
Tejun Heoace2bee2014-02-11 11:52:47 -0500114#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500115 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -0500116 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500117 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500118 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500119
Ben Blumaae8aab2010-03-10 15:22:07 -0800120/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500121 * cgroup destruction makes heavy use of work items and there can be a lot
122 * of concurrent destructions. Use a separate workqueue so that cgroup
123 * destruction work items don't end up filling up max_active of system_wq
124 * which may lead to deadlock.
125 */
126static struct workqueue_struct *cgroup_destroy_wq;
127
128/*
Tejun Heob1a21362013-11-29 10:42:58 -0500129 * pidlist destructions need to be flushed on cgroup destruction. Use a
130 * separate workqueue as flush domain.
131 */
132static struct workqueue_struct *cgroup_pidlist_destroy_wq;
133
Tejun Heo3ed80a62014-02-08 10:36:58 -0500134/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500135#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500136static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137#include <linux/cgroup_subsys.h>
138};
Tejun Heo073219e2014-02-08 10:36:58 -0500139#undef SUBSYS
140
141/* array of cgroup subsystem names */
142#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
143static const char *cgroup_subsys_name[] = {
144#include <linux/cgroup_subsys.h>
145};
146#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700147
Paul Menageddbcc7e2007-10-18 23:39:30 -0700148/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400149 * The default hierarchy, reserved for the subsystems that are otherwise
Tejun Heo9871bf92013-06-24 15:21:47 -0700150 * unattached - it never has more than a single cgroup, and all tasks are
151 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700152 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400153struct cgroup_root cgrp_dfl_root;
Tejun Heo9871bf92013-06-24 15:21:47 -0700154
Tejun Heoa2dd4242014-03-19 10:23:55 -0400155/*
156 * The default hierarchy always exists but is hidden until mounted for the
157 * first time. This is for backward compatibility.
158 */
159static bool cgrp_dfl_root_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700160
161/* The list of hierarchy roots */
162
Tejun Heo9871bf92013-06-24 15:21:47 -0700163static LIST_HEAD(cgroup_roots);
164static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165
Tejun Heo3417ae12014-02-08 10:37:01 -0500166/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700167static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700168
Li Zefan794611a2013-06-18 18:53:53 +0800169/*
170 * Assign a monotonically increasing serial number to cgroups. It
171 * guarantees cgroups with bigger numbers are newer than those with smaller
172 * numbers. Also, as cgroups are always appended to the parent's
173 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700174 * the ascending serial number order on the list. Protected by
175 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800176 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700177static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800178
Paul Menageddbcc7e2007-10-18 23:39:30 -0700179/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800180 * check for fork/exit handlers to call. This avoids us having to do
181 * extra work in the fork/exit path if none of the subsystems need to
182 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700183 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700184static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700185
Tejun Heo628f7cd2013-06-28 16:24:11 -0700186static struct cftype cgroup_base_files[];
187
Tejun Heo59f52962014-02-11 11:52:49 -0500188static void cgroup_put(struct cgroup *cgrp);
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400189static int rebind_subsystems(struct cgroup_root *dst_root,
Tejun Heo69dfa002014-05-04 15:09:13 -0400190 unsigned int ss_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400191static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800192static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heof8f22e52014-04-23 11:13:16 -0400193static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
194static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400195static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
196 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500197static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800198
Tejun Heo6fa49182014-05-04 15:09:13 -0400199/* IDR wrappers which synchronize using cgroup_idr_lock */
200static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
201 gfp_t gfp_mask)
202{
203 int ret;
204
205 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400206 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400207 ret = idr_alloc(idr, ptr, start, end, gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400208 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400209 idr_preload_end();
210 return ret;
211}
212
213static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
214{
215 void *ret;
216
Tejun Heo54504e92014-05-13 12:10:59 -0400217 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400218 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400219 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400220 return ret;
221}
222
223static void cgroup_idr_remove(struct idr *idr, int id)
224{
Tejun Heo54504e92014-05-13 12:10:59 -0400225 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400226 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400227 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400228}
229
Tejun Heo95109b62013-08-08 20:11:27 -0400230/**
231 * cgroup_css - obtain a cgroup's css for the specified subsystem
232 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400233 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400234 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400235 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
236 * function must be called either under cgroup_mutex or rcu_read_lock() and
237 * the caller is responsible for pinning the returned css if it wants to
238 * keep accessing it outside the said locks. This function may return
239 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400240 */
241static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400242 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400243{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400244 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500245 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500246 lockdep_is_held(&cgroup_tree_mutex) ||
247 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400248 else
249 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400250}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700251
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400252/**
253 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
254 * @cgrp: the cgroup of interest
255 * @ss: the subsystem of interest (%NULL returns the dummy_css)
256 *
257 * Similar to cgroup_css() but returns the effctive css, which is defined
258 * as the matching css of the nearest ancestor including self which has @ss
259 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
260 * function is guaranteed to return non-NULL css.
261 */
262static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
263 struct cgroup_subsys *ss)
264{
265 lockdep_assert_held(&cgroup_mutex);
266
267 if (!ss)
268 return &cgrp->dummy_css;
269
270 if (!(cgrp->root->subsys_mask & (1 << ss->id)))
271 return NULL;
272
273 while (cgrp->parent &&
274 !(cgrp->parent->child_subsys_mask & (1 << ss->id)))
275 cgrp = cgrp->parent;
276
277 return cgroup_css(cgrp, ss);
278}
279
Paul Menageddbcc7e2007-10-18 23:39:30 -0700280/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700281static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700282{
Tejun Heo54766d42013-06-12 21:04:53 -0700283 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700284}
285
Tejun Heo59f52962014-02-11 11:52:49 -0500286struct cgroup_subsys_state *seq_css(struct seq_file *seq)
287{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500288 struct kernfs_open_file *of = seq->private;
289 struct cgroup *cgrp = of->kn->parent->priv;
290 struct cftype *cft = seq_cft(seq);
291
292 /*
293 * This is open and unprotected implementation of cgroup_css().
294 * seq_css() is only called from a kernfs file operation which has
295 * an active reference on the file. Because all the subsystem
296 * files are drained before a css is disassociated with a cgroup,
297 * the matching css from the cgroup's subsys table is guaranteed to
298 * be and stay valid until the enclosing operation is complete.
299 */
300 if (cft->ss)
301 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
302 else
303 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500304}
305EXPORT_SYMBOL_GPL(seq_css);
306
Li Zefan78574cf2013-04-08 19:00:38 -0700307/**
308 * cgroup_is_descendant - test ancestry
309 * @cgrp: the cgroup to be tested
310 * @ancestor: possible ancestor of @cgrp
311 *
312 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
313 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
314 * and @ancestor are accessible.
315 */
316bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
317{
318 while (cgrp) {
319 if (cgrp == ancestor)
320 return true;
321 cgrp = cgrp->parent;
322 }
323 return false;
324}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700325
Adrian Bunke9685a02008-02-07 00:13:46 -0800326static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700327{
328 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700329 (1 << CGRP_RELEASABLE) |
330 (1 << CGRP_NOTIFY_ON_RELEASE);
331 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700332}
333
Adrian Bunke9685a02008-02-07 00:13:46 -0800334static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335{
Paul Menagebd89aab2007-10-18 23:40:44 -0700336 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700337}
338
Tejun Heo30159ec2013-06-25 11:53:37 -0700339/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500340 * for_each_css - iterate all css's of a cgroup
341 * @css: the iteration cursor
342 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
343 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700344 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400345 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700346 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500347#define for_each_css(css, ssid, cgrp) \
348 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
349 if (!((css) = rcu_dereference_check( \
350 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500351 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500352 lockdep_is_held(&cgroup_mutex)))) { } \
353 else
354
355/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400356 * for_each_e_css - iterate all effective css's of a cgroup
357 * @css: the iteration cursor
358 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
359 * @cgrp: the target cgroup to iterate css's of
360 *
361 * Should be called under cgroup_[tree_]mutex.
362 */
363#define for_each_e_css(css, ssid, cgrp) \
364 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
365 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
366 ; \
367 else
368
369/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500370 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700371 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500372 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700373 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500374#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500375 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
376 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700377
Tejun Heo985ed672014-03-19 10:23:53 -0400378/* iterate across the hierarchies */
379#define for_each_root(root) \
Tejun Heo5549c492013-06-24 15:21:48 -0700380 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700381
Tejun Heof8f22e52014-04-23 11:13:16 -0400382/* iterate over child cgrps, lock should be held throughout iteration */
383#define cgroup_for_each_live_child(child, cgrp) \
384 list_for_each_entry((child), &(cgrp)->children, sibling) \
385 if (({ lockdep_assert_held(&cgroup_tree_mutex); \
386 cgroup_is_dead(child); })) \
387 ; \
388 else
389
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700390/**
391 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
392 * @cgrp: the cgroup to be checked for liveness
393 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700394 * On success, returns true; the mutex should be later unlocked. On
395 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700396 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700397static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700398{
399 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700400 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700401 mutex_unlock(&cgroup_mutex);
402 return false;
403 }
404 return true;
405}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700406
Paul Menage81a6a5c2007-10-18 23:39:38 -0700407/* the list of cgroups eligible for automatic release. Protected by
408 * release_list_lock */
409static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200410static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700411static void cgroup_release_agent(struct work_struct *work);
412static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700413static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700414
Tejun Heo69d02062013-06-12 21:04:50 -0700415/*
416 * A cgroup can be associated with multiple css_sets as different tasks may
417 * belong to different cgroups on different hierarchies. In the other
418 * direction, a css_set is naturally associated with multiple cgroups.
419 * This M:N relationship is represented by the following link structure
420 * which exists for each association and allows traversing the associations
421 * from both sides.
422 */
423struct cgrp_cset_link {
424 /* the cgroup and css_set this link associates */
425 struct cgroup *cgrp;
426 struct css_set *cset;
427
428 /* list of cgrp_cset_links anchored at cgrp->cset_links */
429 struct list_head cset_link;
430
431 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
432 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700433};
434
Tejun Heo172a2c062014-03-19 10:23:53 -0400435/*
436 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700437 * hierarchies being mounted. It contains a pointer to the root state
438 * for each subsystem. Also used to anchor the list of css_sets. Not
439 * reference-counted, to improve performance when child cgroups
440 * haven't been created.
441 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400442struct css_set init_css_set = {
Tejun Heo172a2c062014-03-19 10:23:53 -0400443 .refcount = ATOMIC_INIT(1),
444 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
445 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
446 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
447 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
448 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
449};
Paul Menage817929e2007-10-18 23:39:36 -0700450
Tejun Heo172a2c062014-03-19 10:23:53 -0400451static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700452
Tejun Heo842b5972014-04-25 18:28:02 -0400453/**
454 * cgroup_update_populated - updated populated count of a cgroup
455 * @cgrp: the target cgroup
456 * @populated: inc or dec populated count
457 *
458 * @cgrp is either getting the first task (css_set) or losing the last.
459 * Update @cgrp->populated_cnt accordingly. The count is propagated
460 * towards root so that a given cgroup's populated_cnt is zero iff the
461 * cgroup and all its descendants are empty.
462 *
463 * @cgrp's interface file "cgroup.populated" is zero if
464 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
465 * changes from or to zero, userland is notified that the content of the
466 * interface file has changed. This can be used to detect when @cgrp and
467 * its descendants become populated or empty.
468 */
469static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
470{
471 lockdep_assert_held(&css_set_rwsem);
472
473 do {
474 bool trigger;
475
476 if (populated)
477 trigger = !cgrp->populated_cnt++;
478 else
479 trigger = !--cgrp->populated_cnt;
480
481 if (!trigger)
482 break;
483
484 if (cgrp->populated_kn)
485 kernfs_notify(cgrp->populated_kn);
486 cgrp = cgrp->parent;
487 } while (cgrp);
488}
489
Paul Menage7717f7b2009-09-23 15:56:22 -0700490/*
491 * hash table for cgroup groups. This improves the performance to find
492 * an existing css_set. This hash doesn't (currently) take into
493 * account cgroups in empty hierarchies.
494 */
Li Zefan472b1052008-04-29 01:00:11 -0700495#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800496static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700497
Li Zefan0ac801f2013-01-10 11:49:27 +0800498static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700499{
Li Zefan0ac801f2013-01-10 11:49:27 +0800500 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700501 struct cgroup_subsys *ss;
502 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700503
Tejun Heo30159ec2013-06-25 11:53:37 -0700504 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800505 key += (unsigned long)css[i];
506 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700507
Li Zefan0ac801f2013-01-10 11:49:27 +0800508 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700509}
510
Tejun Heo89c55092014-02-13 06:58:40 -0500511static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700512{
Tejun Heo69d02062013-06-12 21:04:50 -0700513 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400514 struct cgroup_subsys *ss;
515 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700516
Tejun Heo89c55092014-02-13 06:58:40 -0500517 lockdep_assert_held(&css_set_rwsem);
518
519 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700520 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700521
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700522 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo2d8f2432014-04-23 11:13:15 -0400523 for_each_subsys(ss, ssid)
524 list_del(&cset->e_cset_node[ssid]);
Tejun Heo5abb8852013-06-12 21:04:49 -0700525 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700526 css_set_count--;
527
Tejun Heo69d02062013-06-12 21:04:50 -0700528 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700529 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700530
Tejun Heo69d02062013-06-12 21:04:50 -0700531 list_del(&link->cset_link);
532 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800533
Tejun Heo96d365e2014-02-13 06:58:40 -0500534 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo842b5972014-04-25 18:28:02 -0400535 if (list_empty(&cgrp->cset_links)) {
536 cgroup_update_populated(cgrp, false);
537 if (notify_on_release(cgrp)) {
538 if (taskexit)
539 set_bit(CGRP_RELEASABLE, &cgrp->flags);
540 check_for_release(cgrp);
541 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700542 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700543
544 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700545 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700546
Tejun Heo5abb8852013-06-12 21:04:49 -0700547 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700548}
549
Tejun Heo89c55092014-02-13 06:58:40 -0500550static void put_css_set(struct css_set *cset, bool taskexit)
551{
552 /*
553 * Ensure that the refcount doesn't hit zero while any readers
554 * can see it. Similar to atomic_dec_and_lock(), but for an
555 * rwlock
556 */
557 if (atomic_add_unless(&cset->refcount, -1, 1))
558 return;
559
560 down_write(&css_set_rwsem);
561 put_css_set_locked(cset, taskexit);
562 up_write(&css_set_rwsem);
563}
564
Paul Menage817929e2007-10-18 23:39:36 -0700565/*
566 * refcounted get/put for css_set objects
567 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700568static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700569{
Tejun Heo5abb8852013-06-12 21:04:49 -0700570 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700571}
572
Tejun Heob326f9d2013-06-24 15:21:48 -0700573/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700574 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700575 * @cset: candidate css_set being tested
576 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700577 * @new_cgrp: cgroup that's being entered by the task
578 * @template: desired set of css pointers in css_set (pre-calculated)
579 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800580 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700581 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
582 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700583static bool compare_css_sets(struct css_set *cset,
584 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700585 struct cgroup *new_cgrp,
586 struct cgroup_subsys_state *template[])
587{
588 struct list_head *l1, *l2;
589
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400590 /*
591 * On the default hierarchy, there can be csets which are
592 * associated with the same set of cgroups but different csses.
593 * Let's first ensure that csses match.
594 */
595 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -0700596 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -0700597
598 /*
599 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400600 * different cgroups in hierarchies. As different cgroups may
601 * share the same effective css, this comparison is always
602 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -0700603 */
Tejun Heo69d02062013-06-12 21:04:50 -0700604 l1 = &cset->cgrp_links;
605 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700606 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700607 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700608 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700609
610 l1 = l1->next;
611 l2 = l2->next;
612 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700613 if (l1 == &cset->cgrp_links) {
614 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700615 break;
616 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700617 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700618 }
619 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700620 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
621 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
622 cgrp1 = link1->cgrp;
623 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700624 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700625 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700626
627 /*
628 * If this hierarchy is the hierarchy of the cgroup
629 * that's changing, then we need to check that this
630 * css_set points to the new cgroup; if it's any other
631 * hierarchy, then this css_set should point to the
632 * same cgroup as the old css_set.
633 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700634 if (cgrp1->root == new_cgrp->root) {
635 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700636 return false;
637 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700638 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700639 return false;
640 }
641 }
642 return true;
643}
644
Tejun Heob326f9d2013-06-24 15:21:48 -0700645/**
646 * find_existing_css_set - init css array and find the matching css_set
647 * @old_cset: the css_set that we're using before the cgroup transition
648 * @cgrp: the cgroup that we're moving into
649 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700650 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700651static struct css_set *find_existing_css_set(struct css_set *old_cset,
652 struct cgroup *cgrp,
653 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700654{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400655 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700656 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700657 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800658 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700659 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700660
Ben Blumaae8aab2010-03-10 15:22:07 -0800661 /*
662 * Build the set of subsystem state objects that we want to see in the
663 * new css_set. while subsystems can change globally, the entries here
664 * won't change, so no need for locking.
665 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700666 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -0400667 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400668 /*
669 * @ss is in this hierarchy, so we want the
670 * effective css from @cgrp.
671 */
672 template[i] = cgroup_e_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700673 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400674 /*
675 * @ss is not in this hierarchy, so we don't want
676 * to change the css.
677 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700678 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700679 }
680 }
681
Li Zefan0ac801f2013-01-10 11:49:27 +0800682 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700683 hash_for_each_possible(css_set_table, cset, hlist, key) {
684 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700685 continue;
686
687 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700689 }
Paul Menage817929e2007-10-18 23:39:36 -0700690
691 /* No existing cgroup group matched */
692 return NULL;
693}
694
Tejun Heo69d02062013-06-12 21:04:50 -0700695static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700696{
Tejun Heo69d02062013-06-12 21:04:50 -0700697 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700698
Tejun Heo69d02062013-06-12 21:04:50 -0700699 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
700 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700701 kfree(link);
702 }
703}
704
Tejun Heo69d02062013-06-12 21:04:50 -0700705/**
706 * allocate_cgrp_cset_links - allocate cgrp_cset_links
707 * @count: the number of links to allocate
708 * @tmp_links: list_head the allocated links are put on
709 *
710 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
711 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700712 */
Tejun Heo69d02062013-06-12 21:04:50 -0700713static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700714{
Tejun Heo69d02062013-06-12 21:04:50 -0700715 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700716 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700717
718 INIT_LIST_HEAD(tmp_links);
719
Li Zefan36553432008-07-29 22:33:19 -0700720 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700721 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700722 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700723 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700724 return -ENOMEM;
725 }
Tejun Heo69d02062013-06-12 21:04:50 -0700726 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700727 }
728 return 0;
729}
730
Li Zefanc12f65d2009-01-07 18:07:42 -0800731/**
732 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700733 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700734 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800735 * @cgrp: the destination cgroup
736 */
Tejun Heo69d02062013-06-12 21:04:50 -0700737static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
738 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800739{
Tejun Heo69d02062013-06-12 21:04:50 -0700740 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800741
Tejun Heo69d02062013-06-12 21:04:50 -0700742 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -0400743
744 if (cgroup_on_dfl(cgrp))
745 cset->dfl_cgrp = cgrp;
746
Tejun Heo69d02062013-06-12 21:04:50 -0700747 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
748 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700749 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -0400750
751 if (list_empty(&cgrp->cset_links))
752 cgroup_update_populated(cgrp, true);
Tejun Heo69d02062013-06-12 21:04:50 -0700753 list_move(&link->cset_link, &cgrp->cset_links);
Tejun Heo842b5972014-04-25 18:28:02 -0400754
Paul Menage7717f7b2009-09-23 15:56:22 -0700755 /*
756 * Always add links to the tail of the list so that the list
757 * is sorted by order of hierarchy creation
758 */
Tejun Heo69d02062013-06-12 21:04:50 -0700759 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800760}
761
Tejun Heob326f9d2013-06-24 15:21:48 -0700762/**
763 * find_css_set - return a new css_set with one cgroup updated
764 * @old_cset: the baseline css_set
765 * @cgrp: the cgroup to be updated
766 *
767 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
768 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700769 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700770static struct css_set *find_css_set(struct css_set *old_cset,
771 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700772{
Tejun Heob326f9d2013-06-24 15:21:48 -0700773 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700774 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700775 struct list_head tmp_links;
776 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400777 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +0800778 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400779 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -0700780
Tejun Heob326f9d2013-06-24 15:21:48 -0700781 lockdep_assert_held(&cgroup_mutex);
782
Paul Menage817929e2007-10-18 23:39:36 -0700783 /* First see if we already have a cgroup group that matches
784 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500785 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700786 cset = find_existing_css_set(old_cset, cgrp, template);
787 if (cset)
788 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500789 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700790
Tejun Heo5abb8852013-06-12 21:04:49 -0700791 if (cset)
792 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700793
Tejun Heof4f4be22013-06-12 21:04:51 -0700794 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700795 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700796 return NULL;
797
Tejun Heo69d02062013-06-12 21:04:50 -0700798 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700799 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700800 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700801 return NULL;
802 }
803
Tejun Heo5abb8852013-06-12 21:04:49 -0700804 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700805 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700806 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500807 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500808 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500809 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700810 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700811
812 /* Copy the set of subsystem state objects generated in
813 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700814 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700815
Tejun Heo96d365e2014-02-13 06:58:40 -0500816 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700817 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700818 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700819 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700820
Paul Menage7717f7b2009-09-23 15:56:22 -0700821 if (c->root == cgrp->root)
822 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700823 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700824 }
Paul Menage817929e2007-10-18 23:39:36 -0700825
Tejun Heo69d02062013-06-12 21:04:50 -0700826 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700827
Paul Menage817929e2007-10-18 23:39:36 -0700828 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700829
Tejun Heo2d8f2432014-04-23 11:13:15 -0400830 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700831 key = css_set_hash(cset->subsys);
832 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700833
Tejun Heo2d8f2432014-04-23 11:13:15 -0400834 for_each_subsys(ss, ssid)
835 list_add_tail(&cset->e_cset_node[ssid],
836 &cset->subsys[ssid]->cgroup->e_csets[ssid]);
837
Tejun Heo96d365e2014-02-13 06:58:40 -0500838 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700839
Tejun Heo5abb8852013-06-12 21:04:49 -0700840 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700841}
842
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400843static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700844{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400845 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500846
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400847 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -0500848}
849
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400850static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500851{
852 int id;
853
854 lockdep_assert_held(&cgroup_mutex);
855
Tejun Heo985ed672014-03-19 10:23:53 -0400856 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -0500857 if (id < 0)
858 return id;
859
860 root->hierarchy_id = id;
861 return 0;
862}
863
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400864static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500865{
866 lockdep_assert_held(&cgroup_mutex);
867
868 if (root->hierarchy_id) {
869 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
870 root->hierarchy_id = 0;
871 }
872}
873
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400874static void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -0500875{
876 if (root) {
877 /* hierarhcy ID shoulid already have been released */
878 WARN_ON_ONCE(root->hierarchy_id);
879
880 idr_destroy(&root->cgroup_idr);
881 kfree(root);
882 }
883}
884
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400885static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500886{
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400887 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -0500888 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500889
Tejun Heo2bd59d42014-02-11 11:52:49 -0500890 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500891 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500892
Tejun Heo776f02f2014-02-12 09:29:50 -0500893 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500894 BUG_ON(!list_empty(&cgrp->children));
895
Tejun Heof2e85d52014-02-11 11:52:49 -0500896 /* Rebind all subsystems back to the default hierarchy */
Tejun Heof392e512014-04-23 11:13:14 -0400897 rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
Tejun Heof2e85d52014-02-11 11:52:49 -0500898
899 /*
900 * Release all the links from cset_links to this hierarchy's
901 * root cgroup
902 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500903 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500904
905 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
906 list_del(&link->cset_link);
907 list_del(&link->cgrp_link);
908 kfree(link);
909 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500910 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500911
912 if (!list_empty(&root->root_list)) {
913 list_del(&root->root_list);
914 cgroup_root_count--;
915 }
916
917 cgroup_exit_root_id(root);
918
919 mutex_unlock(&cgroup_mutex);
920 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500921
Tejun Heo2bd59d42014-02-11 11:52:49 -0500922 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500923 cgroup_free_root(root);
924}
925
Tejun Heoceb6a082014-02-25 10:04:02 -0500926/* look up cgroup associated with given css_set on the specified hierarchy */
927static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400928 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -0700929{
Paul Menage7717f7b2009-09-23 15:56:22 -0700930 struct cgroup *res = NULL;
931
Tejun Heo96d365e2014-02-13 06:58:40 -0500932 lockdep_assert_held(&cgroup_mutex);
933 lockdep_assert_held(&css_set_rwsem);
934
Tejun Heo5abb8852013-06-12 21:04:49 -0700935 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400936 res = &root->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700937 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700938 struct cgrp_cset_link *link;
939
940 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700941 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700942
Paul Menage7717f7b2009-09-23 15:56:22 -0700943 if (c->root == root) {
944 res = c;
945 break;
946 }
947 }
948 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500949
Paul Menage7717f7b2009-09-23 15:56:22 -0700950 BUG_ON(!res);
951 return res;
952}
953
954/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500955 * Return the cgroup for "task" from the given hierarchy. Must be
956 * called with cgroup_mutex and css_set_rwsem held.
957 */
958static struct cgroup *task_cgroup_from_root(struct task_struct *task,
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400959 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -0500960{
961 /*
962 * No need to lock the task - since we hold cgroup_mutex the
963 * task can't change groups, so the only thing that can happen
964 * is that it exits and its css is set back to init_css_set.
965 */
966 return cset_cgroup_from_root(task_css_set(task), root);
967}
968
969/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970 * A task must hold cgroup_mutex to modify cgroups.
971 *
972 * Any task can increment and decrement the count field without lock.
973 * So in general, code holding cgroup_mutex can't rely on the count
974 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800975 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976 * means that no tasks are currently attached, therefore there is no
977 * way a task attached to that cgroup can fork (the other way to
978 * increment the count). So code holding cgroup_mutex can safely
979 * assume that if the count is zero, it will stay zero. Similarly, if
980 * a task holds cgroup_mutex on a cgroup with zero count, it
981 * knows that the cgroup won't be removed, as cgroup_rmdir()
982 * needs that mutex.
983 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
985 * (usually) take cgroup_mutex. These are the two most performance
986 * critical pieces of code here. The exception occurs on cgroup_exit(),
987 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
988 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800989 * to the release agent with the name of the cgroup (path relative to
990 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 *
992 * A cgroup can only be deleted if both its 'count' of using tasks
993 * is zero, and its list of 'children' cgroups is empty. Since all
994 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400995 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400997 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998 *
999 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001000 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001 */
1002
Tejun Heo69dfa002014-05-04 15:09:13 -04001003static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001004static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001005static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -07001006
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001007static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1008 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001010 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1011 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1012 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
1013 cft->ss->name, cft->name);
1014 else
1015 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1016 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017}
1018
Tejun Heof2e85d52014-02-11 11:52:49 -05001019/**
1020 * cgroup_file_mode - deduce file mode of a control file
1021 * @cft: the control file in question
1022 *
1023 * returns cft->mode if ->mode is not 0
1024 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1025 * returns S_IRUGO if it has only a read handler
1026 * returns S_IWUSR if it has only a write hander
1027 */
1028static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001029{
Tejun Heof2e85d52014-02-11 11:52:49 -05001030 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001031
Tejun Heof2e85d52014-02-11 11:52:49 -05001032 if (cft->mode)
1033 return cft->mode;
1034
1035 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1036 mode |= S_IRUGO;
1037
1038 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
1039 cft->trigger)
1040 mode |= S_IWUSR;
1041
1042 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001043}
1044
Li Zefanbe445622013-01-24 14:31:42 +08001045static void cgroup_free_fn(struct work_struct *work)
1046{
Tejun Heoea15f8c2013-06-13 19:27:42 -07001047 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001048
Tejun Heo3c9c8252014-02-12 09:29:50 -05001049 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -05001050 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +08001051
Tejun Heo776f02f2014-02-12 09:29:50 -05001052 if (cgrp->parent) {
1053 /*
1054 * We get a ref to the parent, and put the ref when this
1055 * cgroup is being freed, so it's guaranteed that the
1056 * parent won't be destroyed before its children.
1057 */
1058 cgroup_put(cgrp->parent);
1059 kernfs_put(cgrp->kn);
1060 kfree(cgrp);
1061 } else {
1062 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001063 * This is root cgroup's refcnt reaching zero, which
Tejun Heo776f02f2014-02-12 09:29:50 -05001064 * indicates that the root should be released.
1065 */
1066 cgroup_destroy_root(cgrp->root);
1067 }
Li Zefanbe445622013-01-24 14:31:42 +08001068}
1069
1070static void cgroup_free_rcu(struct rcu_head *head)
1071{
1072 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
1073
Tejun Heoea15f8c2013-06-13 19:27:42 -07001074 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05001075 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +08001076}
1077
Tejun Heo59f52962014-02-11 11:52:49 -05001078static void cgroup_get(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001080 WARN_ON_ONCE(cgroup_is_dead(cgrp));
1081 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
1082 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083}
1084
Tejun Heo59f52962014-02-11 11:52:49 -05001085static void cgroup_put(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001087 if (!atomic_dec_and_test(&cgrp->refcnt))
1088 return;
Tejun Heo776f02f2014-02-12 09:29:50 -05001089 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -05001090 return;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091
Tejun Heo6fa49182014-05-04 15:09:13 -04001092 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001093 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094
Tejun Heo2bd59d42014-02-11 11:52:49 -05001095 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096}
1097
Li Zefan2739d3c2013-01-21 18:18:33 +08001098static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001100 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101
Tejun Heoace2bee2014-02-11 11:52:47 -05001102 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001103 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001104}
1105
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001106/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07001107 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -07001108 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001109 * @subsys_mask: mask of the subsystem ids whose files should be removed
1110 */
Tejun Heo69dfa002014-05-04 15:09:13 -04001111static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001112{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001113 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001114 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001115
Tejun Heob420ba72013-07-12 12:34:02 -07001116 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001117 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001118
Tejun Heo69dfa002014-05-04 15:09:13 -04001119 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001120 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001121 list_for_each_entry(cfts, &ss->cfts, node)
1122 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001123 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124}
1125
Tejun Heo69dfa002014-05-04 15:09:13 -04001126static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127{
Tejun Heo30159ec2013-06-25 11:53:37 -07001128 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001129 int ssid, i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001130
Tejun Heoace2bee2014-02-11 11:52:47 -05001131 lockdep_assert_held(&cgroup_tree_mutex);
1132 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001133
Tejun Heo5df36032014-03-19 10:23:54 -04001134 for_each_subsys(ss, ssid) {
1135 if (!(ss_mask & (1 << ssid)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001137
Tejun Heo7fd8c562014-04-23 11:13:16 -04001138 /* if @ss has non-root csses attached to it, can't move */
1139 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
Tejun Heo3ed80a62014-02-08 10:36:58 -05001140 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141
Tejun Heo5df36032014-03-19 10:23:54 -04001142 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001143 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001144 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001145 }
1146
Tejun Heoa2dd4242014-03-19 10:23:55 -04001147 ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
1148 if (ret) {
1149 if (dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001150 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001151
Tejun Heoa2dd4242014-03-19 10:23:55 -04001152 /*
1153 * Rebinding back to the default root is not allowed to
1154 * fail. Using both default and non-default roots should
1155 * be rare. Moving subsystems back and forth even more so.
1156 * Just warn about it and continue.
1157 */
1158 if (cgrp_dfl_root_visible) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001159 pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001160 ret, ss_mask);
Joe Perchesed3d2612014-04-25 18:28:03 -04001161 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
Tejun Heoa2dd4242014-03-19 10:23:55 -04001162 }
Tejun Heo5df36032014-03-19 10:23:54 -04001163 }
Tejun Heo31261212013-06-28 17:07:30 -07001164
1165 /*
1166 * Nothing can fail from this point on. Remove files for the
1167 * removed subsystems and rebind each subsystem.
1168 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001169 mutex_unlock(&cgroup_mutex);
Tejun Heo5df36032014-03-19 10:23:54 -04001170 for_each_subsys(ss, ssid)
Tejun Heoa2dd4242014-03-19 10:23:55 -04001171 if (ss_mask & (1 << ssid))
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001172 cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
Tejun Heo4ac06012014-02-11 11:52:47 -05001173 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001174
Tejun Heo5df36032014-03-19 10:23:54 -04001175 for_each_subsys(ss, ssid) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001176 struct cgroup_root *src_root;
Tejun Heo5df36032014-03-19 10:23:54 -04001177 struct cgroup_subsys_state *css;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001178 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001179
Tejun Heo5df36032014-03-19 10:23:54 -04001180 if (!(ss_mask & (1 << ssid)))
1181 continue;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001182
Tejun Heo5df36032014-03-19 10:23:54 -04001183 src_root = ss->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001184 css = cgroup_css(&src_root->cgrp, ss);
Tejun Heo73e80ed2013-08-13 11:01:55 -04001185
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001186 WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001187
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001188 RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
1189 rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001190 ss->root = dst_root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001191 css->cgroup = &dst_root->cgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001192
Tejun Heo2d8f2432014-04-23 11:13:15 -04001193 down_write(&css_set_rwsem);
1194 hash_for_each(css_set_table, i, cset, hlist)
1195 list_move_tail(&cset->e_cset_node[ss->id],
1196 &dst_root->cgrp.e_csets[ss->id]);
1197 up_write(&css_set_rwsem);
1198
Tejun Heof392e512014-04-23 11:13:14 -04001199 src_root->subsys_mask &= ~(1 << ssid);
1200 src_root->cgrp.child_subsys_mask &= ~(1 << ssid);
1201
Tejun Heobd53d612014-04-23 11:13:16 -04001202 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001203 dst_root->subsys_mask |= 1 << ssid;
Tejun Heobd53d612014-04-23 11:13:16 -04001204 if (dst_root != &cgrp_dfl_root)
1205 dst_root->cgrp.child_subsys_mask |= 1 << ssid;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001206
Tejun Heo5df36032014-03-19 10:23:54 -04001207 if (ss->bind)
1208 ss->bind(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001209 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001210
Tejun Heoa2dd4242014-03-19 10:23:55 -04001211 kernfs_activate(dst_root->cgrp.kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001212 return 0;
1213}
1214
Tejun Heo2bd59d42014-02-11 11:52:49 -05001215static int cgroup_show_options(struct seq_file *seq,
1216 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001217{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001218 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001219 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001220 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001221
Tejun Heob85d2042013-12-06 15:11:57 -05001222 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04001223 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05001224 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001225 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1226 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001227 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001228 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001229 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001230 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001231
1232 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001233 if (strlen(root->release_agent_path))
1234 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001235 spin_unlock(&release_agent_path_lock);
1236
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001237 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001238 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001239 if (strlen(root->name))
1240 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001241 return 0;
1242}
1243
1244struct cgroup_sb_opts {
Tejun Heo69dfa002014-05-04 15:09:13 -04001245 unsigned int subsys_mask;
1246 unsigned int flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001247 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001248 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001249 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001250 /* User explicitly requested empty subsystem */
1251 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252};
1253
Ben Blumcf5d5942010-03-10 15:22:09 -08001254static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001255{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001256 char *token, *o = data;
1257 bool all_ss = false, one_ss = false;
Tejun Heo69dfa002014-05-04 15:09:13 -04001258 unsigned int mask = -1U;
Tejun Heo30159ec2013-06-25 11:53:37 -07001259 struct cgroup_subsys *ss;
1260 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001261
1262#ifdef CONFIG_CPUSETS
Tejun Heo69dfa002014-05-04 15:09:13 -04001263 mask = ~(1U << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001264#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001265
Paul Menagec6d57f32009-09-23 15:56:19 -07001266 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001267
1268 while ((token = strsep(&o, ",")) != NULL) {
1269 if (!*token)
1270 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001271 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001272 /* Explicitly have no subsystems */
1273 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001274 continue;
1275 }
1276 if (!strcmp(token, "all")) {
1277 /* Mutually exclusive option 'all' + subsystem name */
1278 if (one_ss)
1279 return -EINVAL;
1280 all_ss = true;
1281 continue;
1282 }
Tejun Heo873fe092013-04-14 20:15:26 -07001283 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1284 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1285 continue;
1286 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001287 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001288 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001289 continue;
1290 }
1291 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001292 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001293 continue;
1294 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001295 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001296 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001297 continue;
1298 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001299 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001300 /* Specifying two release agents is forbidden */
1301 if (opts->release_agent)
1302 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001303 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001304 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001305 if (!opts->release_agent)
1306 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001307 continue;
1308 }
1309 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001310 const char *name = token + 5;
1311 /* Can't specify an empty name */
1312 if (!strlen(name))
1313 return -EINVAL;
1314 /* Must match [\w.-]+ */
1315 for (i = 0; i < strlen(name); i++) {
1316 char c = name[i];
1317 if (isalnum(c))
1318 continue;
1319 if ((c == '.') || (c == '-') || (c == '_'))
1320 continue;
1321 return -EINVAL;
1322 }
1323 /* Specifying two names is forbidden */
1324 if (opts->name)
1325 return -EINVAL;
1326 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001327 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001328 GFP_KERNEL);
1329 if (!opts->name)
1330 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001331
1332 continue;
1333 }
1334
Tejun Heo30159ec2013-06-25 11:53:37 -07001335 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001336 if (strcmp(token, ss->name))
1337 continue;
1338 if (ss->disabled)
1339 continue;
1340
1341 /* Mutually exclusive option 'all' + subsystem name */
1342 if (all_ss)
1343 return -EINVAL;
Tejun Heo69dfa002014-05-04 15:09:13 -04001344 opts->subsys_mask |= (1 << i);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001345 one_ss = true;
1346
1347 break;
1348 }
1349 if (i == CGROUP_SUBSYS_COUNT)
1350 return -ENOENT;
1351 }
1352
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001353 /* Consistency checks */
1354
Tejun Heo873fe092013-04-14 20:15:26 -07001355 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001356 pr_warn("sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001357
Tejun Heod3ba07c2014-02-13 06:58:38 -05001358 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1359 opts->cpuset_clone_children || opts->release_agent ||
1360 opts->name) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001361 pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001362 return -EINVAL;
1363 }
Tejun Heoa2dd4242014-03-19 10:23:55 -04001364 } else {
1365 /*
1366 * If the 'all' option was specified select all the
1367 * subsystems, otherwise if 'none', 'name=' and a subsystem
1368 * name options were not specified, let's default to 'all'
1369 */
1370 if (all_ss || (!one_ss && !opts->none && !opts->name))
1371 for_each_subsys(ss, i)
1372 if (!ss->disabled)
Tejun Heo69dfa002014-05-04 15:09:13 -04001373 opts->subsys_mask |= (1 << i);
Tejun Heo873fe092013-04-14 20:15:26 -07001374
Tejun Heoa2dd4242014-03-19 10:23:55 -04001375 /*
1376 * We either have to specify by name or by subsystems. (So
1377 * all empty hierarchies must have a name).
1378 */
1379 if (!opts->subsys_mask && !opts->name)
Tejun Heo873fe092013-04-14 20:15:26 -07001380 return -EINVAL;
Tejun Heo873fe092013-04-14 20:15:26 -07001381 }
1382
Li Zefanf9ab5b52009-06-17 16:26:33 -07001383 /*
1384 * Option noprefix was introduced just for backward compatibility
1385 * with the old cpuset, so we allow noprefix only if mounting just
1386 * the cpuset subsystem.
1387 */
Tejun Heo93438622013-04-14 20:15:25 -07001388 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001389 return -EINVAL;
1390
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001391
1392 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001393 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001394 return -EINVAL;
1395
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396 return 0;
1397}
1398
Tejun Heo2bd59d42014-02-11 11:52:49 -05001399static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001400{
1401 int ret = 0;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001402 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403 struct cgroup_sb_opts opts;
Tejun Heo69dfa002014-05-04 15:09:13 -04001404 unsigned int added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001405
Tejun Heo873fe092013-04-14 20:15:26 -07001406 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001407 pr_err("sane_behavior: remount is not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001408 return -EINVAL;
1409 }
1410
Tejun Heoace2bee2014-02-11 11:52:47 -05001411 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412 mutex_lock(&cgroup_mutex);
1413
1414 /* See what subsystems are wanted */
1415 ret = parse_cgroupfs_options(data, &opts);
1416 if (ret)
1417 goto out_unlock;
1418
Tejun Heof392e512014-04-23 11:13:14 -04001419 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Joe Perchesed3d2612014-04-25 18:28:03 -04001420 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04001421 task_tgid_nr(current), current->comm);
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001422
Tejun Heof392e512014-04-23 11:13:14 -04001423 added_mask = opts.subsys_mask & ~root->subsys_mask;
1424 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001425
Ben Blumcf5d5942010-03-10 15:22:09 -08001426 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001427 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001428 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo69dfa002014-05-04 15:09:13 -04001429 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001430 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1431 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001433 goto out_unlock;
1434 }
1435
Tejun Heof172e672013-06-28 17:07:30 -07001436 /* remounting is not allowed for populated hierarchies */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001437 if (!list_empty(&root->cgrp.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001438 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001439 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001440 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001441
Tejun Heo5df36032014-03-19 10:23:54 -04001442 ret = rebind_subsystems(root, added_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001443 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001446 rebind_subsystems(&cgrp_dfl_root, removed_mask);
Tejun Heo5df36032014-03-19 10:23:54 -04001447
Tejun Heo69e943b2014-02-08 10:36:58 -05001448 if (opts.release_agent) {
1449 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001450 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001451 spin_unlock(&release_agent_path_lock);
1452 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001454 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001455 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001456 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001457 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458 return ret;
1459}
1460
Tejun Heoafeb0f92014-02-13 06:58:39 -05001461/*
1462 * To reduce the fork() overhead for systems that are not actually using
1463 * their cgroups capability, we don't maintain the lists running through
1464 * each css_set to its tasks until we see the list actually used - in other
1465 * words after the first mount.
1466 */
1467static bool use_task_css_set_links __read_mostly;
1468
1469static void cgroup_enable_task_cg_lists(void)
1470{
1471 struct task_struct *p, *g;
1472
Tejun Heo96d365e2014-02-13 06:58:40 -05001473 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001474
1475 if (use_task_css_set_links)
1476 goto out_unlock;
1477
1478 use_task_css_set_links = true;
1479
1480 /*
1481 * We need tasklist_lock because RCU is not safe against
1482 * while_each_thread(). Besides, a forking task that has passed
1483 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1484 * is not guaranteed to have its child immediately visible in the
1485 * tasklist if we walk through it with RCU.
1486 */
1487 read_lock(&tasklist_lock);
1488 do_each_thread(g, p) {
Tejun Heoafeb0f92014-02-13 06:58:39 -05001489 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1490 task_css_set(p) != &init_css_set);
1491
1492 /*
1493 * We should check if the process is exiting, otherwise
1494 * it will race with cgroup_exit() in that the list
1495 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001496 * Do it while holding siglock so that we don't end up
1497 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001498 */
Tejun Heof153ad12014-02-25 09:56:49 -05001499 spin_lock_irq(&p->sighand->siglock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05001500 if (!(p->flags & PF_EXITING)) {
1501 struct css_set *cset = task_css_set(p);
1502
1503 list_add(&p->cg_list, &cset->tasks);
1504 get_css_set(cset);
1505 }
Tejun Heof153ad12014-02-25 09:56:49 -05001506 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001507 } while_each_thread(g, p);
1508 read_unlock(&tasklist_lock);
1509out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001510 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001511}
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512
Paul Menagecc31edc2008-10-18 20:28:04 -07001513static void init_cgroup_housekeeping(struct cgroup *cgrp)
1514{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001515 struct cgroup_subsys *ss;
1516 int ssid;
1517
Tejun Heo2bd59d42014-02-11 11:52:49 -05001518 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001519 INIT_LIST_HEAD(&cgrp->sibling);
1520 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001521 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001522 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001523 INIT_LIST_HEAD(&cgrp->pidlists);
1524 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001525 cgrp->dummy_css.cgroup = cgrp;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001526
1527 for_each_subsys(ss, ssid)
1528 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001529
1530 init_waitqueue_head(&cgrp->offline_waitq);
Paul Menagecc31edc2008-10-18 20:28:04 -07001531}
Paul Menagec6d57f32009-09-23 15:56:19 -07001532
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001533static void init_cgroup_root(struct cgroup_root *root,
Tejun Heo172a2c062014-03-19 10:23:53 -04001534 struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001536 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001537
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001539 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001540 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001541 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001542 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543
Paul Menagec6d57f32009-09-23 15:56:19 -07001544 root->flags = opts->flags;
1545 if (opts->release_agent)
1546 strcpy(root->release_agent_path, opts->release_agent);
1547 if (opts->name)
1548 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001549 if (opts->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001550 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001551}
1552
Tejun Heo69dfa002014-05-04 15:09:13 -04001553static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001554{
Tejun Heod427dfe2014-02-11 11:52:48 -05001555 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001556 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heod427dfe2014-02-11 11:52:48 -05001557 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001558 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001559
Tejun Heod427dfe2014-02-11 11:52:48 -05001560 lockdep_assert_held(&cgroup_tree_mutex);
1561 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001562
Tejun Heo6fa49182014-05-04 15:09:13 -04001563 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT);
Tejun Heod427dfe2014-02-11 11:52:48 -05001564 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001565 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001566 root_cgrp->id = ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001567
Tejun Heod427dfe2014-02-11 11:52:48 -05001568 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001569 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001570 * but that's OK - it can only be increased by someone holding
1571 * cgroup_lock, and that's us. The worst that can happen is that we
1572 * have some link structures left over
1573 */
1574 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001576 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577
Tejun Heo985ed672014-03-19 10:23:53 -04001578 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05001579 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001580 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581
Tejun Heo2bd59d42014-02-11 11:52:49 -05001582 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1583 KERNFS_ROOT_CREATE_DEACTIVATED,
1584 root_cgrp);
1585 if (IS_ERR(root->kf_root)) {
1586 ret = PTR_ERR(root->kf_root);
1587 goto exit_root_id;
1588 }
1589 root_cgrp->kn = root->kf_root->kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590
Tejun Heod427dfe2014-02-11 11:52:48 -05001591 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1592 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001593 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594
Tejun Heo5df36032014-03-19 10:23:54 -04001595 ret = rebind_subsystems(root, ss_mask);
Tejun Heod427dfe2014-02-11 11:52:48 -05001596 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001597 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05001598
Tejun Heod427dfe2014-02-11 11:52:48 -05001599 /*
1600 * There must be no failure case after here, since rebinding takes
1601 * care of subsystems' refcounts, which are explicitly dropped in
1602 * the failure exit path.
1603 */
1604 list_add(&root->root_list, &cgroup_roots);
1605 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001606
Tejun Heod427dfe2014-02-11 11:52:48 -05001607 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001608 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05001609 * objects.
1610 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001611 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001612 hash_for_each(css_set_table, i, cset, hlist)
1613 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001614 up_write(&css_set_rwsem);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001615
Tejun Heod427dfe2014-02-11 11:52:48 -05001616 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001617 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001618
Tejun Heo2bd59d42014-02-11 11:52:49 -05001619 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001620 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001621 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001622
Tejun Heo2bd59d42014-02-11 11:52:49 -05001623destroy_root:
1624 kernfs_destroy_root(root->kf_root);
1625 root->kf_root = NULL;
1626exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001627 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001628out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001629 free_cgrp_cset_links(&tmp_links);
1630 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631}
1632
Al Virof7e83572010-07-26 13:23:11 +04001633static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001635 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001637 struct cgroup_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001639 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001640 int ret;
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001641 bool new_sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001642
1643 /*
Tejun Heo56fde9e2014-02-13 06:58:38 -05001644 * The first time anyone tries to mount a cgroup, enable the list
1645 * linking each css_set to its tasks and fix up all existing tasks.
Paul Menagec6d57f32009-09-23 15:56:19 -07001646 */
Tejun Heo56fde9e2014-02-13 06:58:38 -05001647 if (!use_task_css_set_links)
1648 cgroup_enable_task_cg_lists();
Li Zefane37a06f2014-04-17 13:53:08 +08001649
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001650 mutex_lock(&cgroup_tree_mutex);
1651 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652
Paul Menageddbcc7e2007-10-18 23:39:30 -07001653 /* First find the desired set of subsystems */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654 ret = parse_cgroupfs_options(data, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001656 goto out_unlock;
Li Zefane37a06f2014-04-17 13:53:08 +08001657retry:
Tejun Heo2bd59d42014-02-11 11:52:49 -05001658 /* look for a matching existing root */
Tejun Heoa2dd4242014-03-19 10:23:55 -04001659 if (!opts.subsys_mask && !opts.none && !opts.name) {
1660 cgrp_dfl_root_visible = true;
1661 root = &cgrp_dfl_root;
1662 cgroup_get(&root->cgrp);
1663 ret = 0;
1664 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665 }
1666
Tejun Heo985ed672014-03-19 10:23:53 -04001667 for_each_root(root) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001668 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001670 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04001671 continue;
Paul Menagec6d57f32009-09-23 15:56:19 -07001672
Paul Menage817929e2007-10-18 23:39:36 -07001673 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001674 * If we asked for a name then it must match. Also, if
1675 * name matches but sybsys_mask doesn't, we should fail.
1676 * Remember whether name matched.
Paul Menage817929e2007-10-18 23:39:36 -07001677 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001678 if (opts.name) {
1679 if (strcmp(opts.name, root->name))
1680 continue;
1681 name_match = true;
1682 }
Tejun Heo31261212013-06-28 17:07:30 -07001683
1684 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001685 * If we asked for subsystems (or explicitly for no
1686 * subsystems) then they must match.
Tejun Heo31261212013-06-28 17:07:30 -07001687 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001688 if ((opts.subsys_mask || opts.none) &&
Tejun Heof392e512014-04-23 11:13:14 -04001689 (opts.subsys_mask != root->subsys_mask)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05001690 if (!name_match)
1691 continue;
1692 ret = -EBUSY;
1693 goto out_unlock;
1694 }
Tejun Heo873fe092013-04-14 20:15:26 -07001695
Tejun Heoc7ba8282013-06-29 14:06:10 -07001696 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001697 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
Joe Perchesed3d2612014-04-25 18:28:03 -04001698 pr_err("sane_behavior: new mount options should match the existing superblock\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001699 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001700 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001701 } else {
Joe Perchesed3d2612014-04-25 18:28:03 -04001702 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001703 }
Tejun Heo873fe092013-04-14 20:15:26 -07001704 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001705
Tejun Heo776f02f2014-02-12 09:29:50 -05001706 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001707 * A root's lifetime is governed by its root cgroup. Zero
Tejun Heo776f02f2014-02-12 09:29:50 -05001708 * ref indicate that the root is being destroyed. Wait for
1709 * destruction to complete so that the subsystems are free.
1710 * We can use wait_queue for the wait but this path is
1711 * super cold. Let's just sleep for a bit and retry.
1712 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001713 if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
Tejun Heo776f02f2014-02-12 09:29:50 -05001714 mutex_unlock(&cgroup_mutex);
1715 mutex_unlock(&cgroup_tree_mutex);
1716 msleep(10);
Li Zefane37a06f2014-04-17 13:53:08 +08001717 mutex_lock(&cgroup_tree_mutex);
1718 mutex_lock(&cgroup_mutex);
Tejun Heo776f02f2014-02-12 09:29:50 -05001719 goto retry;
1720 }
1721
1722 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001723 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724 }
1725
Tejun Heo172a2c062014-03-19 10:23:53 -04001726 /*
1727 * No such thing, create a new one. name= matching without subsys
1728 * specification is allowed for already existing hierarchies but we
1729 * can't create new one without subsys specification.
1730 */
1731 if (!opts.subsys_mask && !opts.none) {
1732 ret = -EINVAL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001733 goto out_unlock;
1734 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735
Tejun Heo172a2c062014-03-19 10:23:53 -04001736 root = kzalloc(sizeof(*root), GFP_KERNEL);
1737 if (!root) {
1738 ret = -ENOMEM;
1739 goto out_unlock;
1740 }
1741
1742 init_cgroup_root(root, &opts);
1743
Tejun Heo35585572014-02-13 06:58:38 -05001744 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001745 if (ret)
1746 cgroup_free_root(root);
1747
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001748out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001749 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001750 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001751
Paul Menagec6d57f32009-09-23 15:56:19 -07001752 kfree(opts.release_agent);
1753 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001754
Tejun Heo2bd59d42014-02-11 11:52:49 -05001755 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001756 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001757
Li Zefanc6b3d5b2014-04-04 17:14:41 +08001758 dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb);
1759 if (IS_ERR(dentry) || !new_sb)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001760 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001761 return dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762}
1763
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001764static void cgroup_kill_sb(struct super_block *sb)
1765{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001766 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001767 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001769 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001770 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771}
1772
1773static struct file_system_type cgroup_fs_type = {
1774 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001775 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 .kill_sb = cgroup_kill_sb,
1777};
1778
Greg KH676db4a2010-08-05 13:53:35 -07001779static struct kobject *cgroup_kobj;
1780
Li Zefana043e3b2008-02-23 15:24:09 -08001781/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001782 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001783 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001784 * @buf: the buffer to write the path into
1785 * @buflen: the length of the buffer
1786 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001787 * Determine @task's cgroup on the first (the one with the lowest non-zero
1788 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1789 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1790 * cgroup controller callbacks.
1791 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001792 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001793 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001794char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001795{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001796 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001797 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001798 int hierarchy_id = 1;
1799 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001800
1801 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001802 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001803
Tejun Heo913ffdb2013-07-11 16:34:48 -07001804 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1805
Tejun Heo857a2be2013-04-14 20:50:08 -07001806 if (root) {
1807 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001808 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001809 } else {
1810 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001811 if (strlcpy(buf, "/", buflen) < buflen)
1812 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001813 }
1814
Tejun Heo96d365e2014-02-13 06:58:40 -05001815 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001816 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001817 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001818}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001819EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001820
Tejun Heob3dc0942014-02-25 10:04:01 -05001821/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001822struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001823 /* the src and dst cset list running through cset->mg_node */
1824 struct list_head src_csets;
1825 struct list_head dst_csets;
1826
1827 /*
1828 * Fields for cgroup_taskset_*() iteration.
1829 *
1830 * Before migration is committed, the target migration tasks are on
1831 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1832 * the csets on ->dst_csets. ->csets point to either ->src_csets
1833 * or ->dst_csets depending on whether migration is committed.
1834 *
1835 * ->cur_csets and ->cur_task point to the current task position
1836 * during iteration.
1837 */
1838 struct list_head *csets;
1839 struct css_set *cur_cset;
1840 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001841};
1842
1843/**
1844 * cgroup_taskset_first - reset taskset and return the first task
1845 * @tset: taskset of interest
1846 *
1847 * @tset iteration is initialized and the first task is returned.
1848 */
1849struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1850{
Tejun Heob3dc0942014-02-25 10:04:01 -05001851 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1852 tset->cur_task = NULL;
1853
1854 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001855}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001856
1857/**
1858 * cgroup_taskset_next - iterate to the next task in taskset
1859 * @tset: taskset of interest
1860 *
1861 * Return the next task in @tset. Iteration must have been initialized
1862 * with cgroup_taskset_first().
1863 */
1864struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1865{
Tejun Heob3dc0942014-02-25 10:04:01 -05001866 struct css_set *cset = tset->cur_cset;
1867 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001868
Tejun Heob3dc0942014-02-25 10:04:01 -05001869 while (&cset->mg_node != tset->csets) {
1870 if (!task)
1871 task = list_first_entry(&cset->mg_tasks,
1872 struct task_struct, cg_list);
1873 else
1874 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001875
Tejun Heob3dc0942014-02-25 10:04:01 -05001876 if (&task->cg_list != &cset->mg_tasks) {
1877 tset->cur_cset = cset;
1878 tset->cur_task = task;
1879 return task;
1880 }
1881
1882 cset = list_next_entry(cset, mg_node);
1883 task = NULL;
1884 }
1885
1886 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001887}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001888
1889/**
Ben Blum74a11662011-05-26 16:25:20 -07001890 * cgroup_task_migrate - move a task from one cgroup to another.
Fabian Frederick60106942014-05-05 20:08:13 +02001891 * @old_cgrp: the cgroup @tsk is being migrated from
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001892 * @tsk: the task being migrated
1893 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001894 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001895 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001896 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001897static void cgroup_task_migrate(struct cgroup *old_cgrp,
1898 struct task_struct *tsk,
1899 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001900{
Tejun Heo5abb8852013-06-12 21:04:49 -07001901 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001902
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001903 lockdep_assert_held(&cgroup_mutex);
1904 lockdep_assert_held(&css_set_rwsem);
1905
Ben Blum74a11662011-05-26 16:25:20 -07001906 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001907 * We are synchronized through threadgroup_lock() against PF_EXITING
1908 * setting such that we can't race against cgroup_exit() changing the
1909 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001910 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001911 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001912 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001913
Tejun Heob3dc0942014-02-25 10:04:01 -05001914 get_css_set(new_cset);
Tejun Heo5abb8852013-06-12 21:04:49 -07001915 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001916
Tejun Heo1b9aba42014-03-19 17:43:21 -04001917 /*
1918 * Use move_tail so that cgroup_taskset_first() still returns the
1919 * leader after migration. This works because cgroup_migrate()
1920 * ensures that the dst_cset of the leader is the first on the
1921 * tset's dst_csets list.
1922 */
1923 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001924
1925 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001926 * We just gained a reference on old_cset by taking it from the
1927 * task. As trading it for new_cset is protected by cgroup_mutex,
1928 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001929 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001930 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001931 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001932}
1933
Li Zefana043e3b2008-02-23 15:24:09 -08001934/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001935 * cgroup_migrate_finish - cleanup after attach
1936 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001937 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001938 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1939 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001940 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001941static void cgroup_migrate_finish(struct list_head *preloaded_csets)
Ben Blum74a11662011-05-26 16:25:20 -07001942{
Tejun Heo1958d2d2014-02-25 10:04:03 -05001943 struct css_set *cset, *tmp_cset;
1944
1945 lockdep_assert_held(&cgroup_mutex);
1946
1947 down_write(&css_set_rwsem);
1948 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1949 cset->mg_src_cgrp = NULL;
1950 cset->mg_dst_cset = NULL;
1951 list_del_init(&cset->mg_preload_node);
1952 put_css_set_locked(cset, false);
1953 }
1954 up_write(&css_set_rwsem);
1955}
1956
1957/**
1958 * cgroup_migrate_add_src - add a migration source css_set
1959 * @src_cset: the source css_set to add
1960 * @dst_cgrp: the destination cgroup
1961 * @preloaded_csets: list of preloaded css_sets
1962 *
1963 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1964 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1965 * up by cgroup_migrate_finish().
1966 *
1967 * This function may be called without holding threadgroup_lock even if the
1968 * target is a process. Threads may be created and destroyed but as long
1969 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1970 * the preloaded css_sets are guaranteed to cover all migrations.
1971 */
1972static void cgroup_migrate_add_src(struct css_set *src_cset,
1973 struct cgroup *dst_cgrp,
1974 struct list_head *preloaded_csets)
1975{
1976 struct cgroup *src_cgrp;
1977
1978 lockdep_assert_held(&cgroup_mutex);
1979 lockdep_assert_held(&css_set_rwsem);
1980
1981 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1982
Tejun Heo1958d2d2014-02-25 10:04:03 -05001983 if (!list_empty(&src_cset->mg_preload_node))
1984 return;
1985
1986 WARN_ON(src_cset->mg_src_cgrp);
1987 WARN_ON(!list_empty(&src_cset->mg_tasks));
1988 WARN_ON(!list_empty(&src_cset->mg_node));
1989
1990 src_cset->mg_src_cgrp = src_cgrp;
1991 get_css_set(src_cset);
1992 list_add(&src_cset->mg_preload_node, preloaded_csets);
1993}
1994
1995/**
1996 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heof817de92014-04-23 11:13:16 -04001997 * @dst_cgrp: the destination cgroup (may be %NULL)
Tejun Heo1958d2d2014-02-25 10:04:03 -05001998 * @preloaded_csets: list of preloaded source css_sets
1999 *
2000 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2001 * have been preloaded to @preloaded_csets. This function looks up and
Tejun Heof817de92014-04-23 11:13:16 -04002002 * pins all destination css_sets, links each to its source, and append them
2003 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2004 * source css_set is assumed to be its cgroup on the default hierarchy.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002005 *
2006 * This function must be called after cgroup_migrate_add_src() has been
2007 * called on each migration source css_set. After migration is performed
2008 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2009 * @preloaded_csets.
2010 */
2011static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2012 struct list_head *preloaded_csets)
2013{
2014 LIST_HEAD(csets);
Tejun Heof817de92014-04-23 11:13:16 -04002015 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002016
2017 lockdep_assert_held(&cgroup_mutex);
2018
Tejun Heof8f22e52014-04-23 11:13:16 -04002019 /*
2020 * Except for the root, child_subsys_mask must be zero for a cgroup
2021 * with tasks so that child cgroups don't compete against tasks.
2022 */
2023 if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
2024 dst_cgrp->child_subsys_mask)
2025 return -EBUSY;
2026
Tejun Heo1958d2d2014-02-25 10:04:03 -05002027 /* look up the dst cset for each src cset and link it to src */
Tejun Heof817de92014-04-23 11:13:16 -04002028 list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002029 struct css_set *dst_cset;
2030
Tejun Heof817de92014-04-23 11:13:16 -04002031 dst_cset = find_css_set(src_cset,
2032 dst_cgrp ?: src_cset->dfl_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002033 if (!dst_cset)
2034 goto err;
2035
2036 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002037
2038 /*
2039 * If src cset equals dst, it's noop. Drop the src.
2040 * cgroup_migrate() will skip the cset too. Note that we
2041 * can't handle src == dst as some nodes are used by both.
2042 */
2043 if (src_cset == dst_cset) {
2044 src_cset->mg_src_cgrp = NULL;
2045 list_del_init(&src_cset->mg_preload_node);
2046 put_css_set(src_cset, false);
2047 put_css_set(dst_cset, false);
2048 continue;
2049 }
2050
Tejun Heo1958d2d2014-02-25 10:04:03 -05002051 src_cset->mg_dst_cset = dst_cset;
2052
2053 if (list_empty(&dst_cset->mg_preload_node))
2054 list_add(&dst_cset->mg_preload_node, &csets);
2055 else
2056 put_css_set(dst_cset, false);
2057 }
2058
Tejun Heof817de92014-04-23 11:13:16 -04002059 list_splice_tail(&csets, preloaded_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002060 return 0;
2061err:
2062 cgroup_migrate_finish(&csets);
2063 return -ENOMEM;
2064}
2065
2066/**
2067 * cgroup_migrate - migrate a process or task to a cgroup
2068 * @cgrp: the destination cgroup
2069 * @leader: the leader of the process or the task to migrate
2070 * @threadgroup: whether @leader points to the whole process or a single task
2071 *
2072 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2073 * process, the caller must be holding threadgroup_lock of @leader. The
2074 * caller is also responsible for invoking cgroup_migrate_add_src() and
2075 * cgroup_migrate_prepare_dst() on the targets before invoking this
2076 * function and following up with cgroup_migrate_finish().
2077 *
2078 * As long as a controller's ->can_attach() doesn't fail, this function is
2079 * guaranteed to succeed. This means that, excluding ->can_attach()
2080 * failure, when migrating multiple targets, the success or failure can be
2081 * decided for all targets by invoking group_migrate_prepare_dst() before
2082 * actually starting migrating.
2083 */
2084static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
2085 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002086{
Tejun Heob3dc0942014-02-25 10:04:01 -05002087 struct cgroup_taskset tset = {
2088 .src_csets = LIST_HEAD_INIT(tset.src_csets),
2089 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
2090 .csets = &tset.src_csets,
2091 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05002092 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05002093 struct css_set *cset, *tmp_cset;
2094 struct task_struct *task, *tmp_task;
2095 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07002096
2097 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002098 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2099 * already PF_EXITING could be freed from underneath us unless we
2100 * take an rcu_read_lock.
2101 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002102 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002103 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002104 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002105 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05002106 /* @task either already exited or can't exit until the end */
2107 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002108 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002109
Tejun Heoeaf797a2014-02-25 10:04:03 -05002110 /* leave @task alone if post_fork() hasn't linked it yet */
2111 if (list_empty(&task->cg_list))
Anjana V Kumarea847532013-10-12 10:59:17 +08002112 goto next;
Tejun Heoeaf797a2014-02-25 10:04:03 -05002113
Tejun Heob3dc0942014-02-25 10:04:01 -05002114 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002115 if (!cset->mg_src_cgrp)
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002116 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05002117
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002118 /*
Tejun Heo1b9aba42014-03-19 17:43:21 -04002119 * cgroup_taskset_first() must always return the leader.
2120 * Take care to avoid disturbing the ordering.
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002121 */
Tejun Heo1b9aba42014-03-19 17:43:21 -04002122 list_move_tail(&task->cg_list, &cset->mg_tasks);
2123 if (list_empty(&cset->mg_node))
2124 list_add_tail(&cset->mg_node, &tset.src_csets);
2125 if (list_empty(&cset->mg_dst_cset->mg_node))
2126 list_move_tail(&cset->mg_dst_cset->mg_node,
2127 &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08002128 next:
Li Zefan081aa452013-03-13 09:17:09 +08002129 if (!threadgroup)
2130 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002131 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002132 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05002133 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002134
Tejun Heo134d3372011-12-12 18:12:21 -08002135 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05002136 if (list_empty(&tset.src_csets))
2137 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08002138
Tejun Heo1958d2d2014-02-25 10:04:03 -05002139 /* check that we can legitimately attach to the cgroup */
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002140 for_each_e_css(css, i, cgrp) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002141 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05002142 ret = css->ss->can_attach(css, &tset);
2143 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002144 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002145 goto out_cancel_attach;
2146 }
2147 }
Ben Blum74a11662011-05-26 16:25:20 -07002148 }
2149
2150 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002151 * Now that we're guaranteed success, proceed to move all tasks to
2152 * the new cgroup. There are no failure cases after here, so this
2153 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002154 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002155 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05002156 list_for_each_entry(cset, &tset.src_csets, mg_node) {
2157 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
2158 cgroup_task_migrate(cset->mg_src_cgrp, task,
2159 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002160 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05002161 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07002162
2163 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05002164 * Migration is committed, all target tasks are now on dst_csets.
2165 * Nothing is sensitive to fork() after this point. Notify
2166 * controllers that migration is complete.
Ben Blum74a11662011-05-26 16:25:20 -07002167 */
Tejun Heob3dc0942014-02-25 10:04:01 -05002168 tset.csets = &tset.dst_csets;
Ben Blum74a11662011-05-26 16:25:20 -07002169
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002170 for_each_e_css(css, i, cgrp)
Tejun Heo1c6727a2013-12-06 15:11:56 -05002171 if (css->ss->attach)
2172 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002173
Tejun Heo9db8de32014-02-13 06:58:43 -05002174 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05002175 goto out_release_tset;
2176
Ben Blum74a11662011-05-26 16:25:20 -07002177out_cancel_attach:
Tejun Heoaec3dfc2014-04-23 11:13:14 -04002178 for_each_e_css(css, i, cgrp) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002179 if (css == failed_css)
2180 break;
2181 if (css->ss->cancel_attach)
2182 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002183 }
Tejun Heob3dc0942014-02-25 10:04:01 -05002184out_release_tset:
2185 down_write(&css_set_rwsem);
2186 list_splice_init(&tset.dst_csets, &tset.src_csets);
2187 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
Tejun Heo1b9aba42014-03-19 17:43:21 -04002188 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05002189 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05002190 }
2191 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05002192 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002193}
2194
Tejun Heo1958d2d2014-02-25 10:04:03 -05002195/**
2196 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2197 * @dst_cgrp: the cgroup to attach to
2198 * @leader: the task or the leader of the threadgroup to be attached
2199 * @threadgroup: attach the whole threadgroup?
2200 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05002201 * Call holding cgroup_mutex and threadgroup_lock of @leader.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002202 */
2203static int cgroup_attach_task(struct cgroup *dst_cgrp,
2204 struct task_struct *leader, bool threadgroup)
2205{
2206 LIST_HEAD(preloaded_csets);
2207 struct task_struct *task;
2208 int ret;
2209
2210 /* look up all src csets */
2211 down_read(&css_set_rwsem);
2212 rcu_read_lock();
2213 task = leader;
2214 do {
2215 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2216 &preloaded_csets);
2217 if (!threadgroup)
2218 break;
2219 } while_each_thread(leader, task);
2220 rcu_read_unlock();
2221 up_read(&css_set_rwsem);
2222
2223 /* prepare dst csets and commit */
2224 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2225 if (!ret)
2226 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2227
2228 cgroup_migrate_finish(&preloaded_csets);
2229 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002230}
2231
2232/*
2233 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002234 * function to attach either it or all tasks in its threadgroup. Will lock
Tejun Heo0e1d7682014-02-25 10:04:03 -05002235 * cgroup_mutex and threadgroup.
Ben Blum74a11662011-05-26 16:25:20 -07002236 */
2237static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002238{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002239 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002240 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002241 int ret;
2242
Ben Blum74a11662011-05-26 16:25:20 -07002243 if (!cgroup_lock_live_group(cgrp))
2244 return -ENODEV;
2245
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002246retry_find_task:
2247 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002248 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002249 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002250 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002251 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002252 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002253 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002254 }
Ben Blum74a11662011-05-26 16:25:20 -07002255 /*
2256 * even if we're attaching all tasks in the thread group, we
2257 * only need to check permissions on one of them.
2258 */
David Howellsc69e8d92008-11-14 10:39:19 +11002259 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002260 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2261 !uid_eq(cred->euid, tcred->uid) &&
2262 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002263 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002264 ret = -EACCES;
2265 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002266 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002267 } else
2268 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002269
2270 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002271 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002272
2273 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002274 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002275 * trapped in a cpuset, or RT worker may be born in a cgroup
2276 * with no rt_runtime allocated. Just say no.
2277 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002278 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002279 ret = -EINVAL;
2280 rcu_read_unlock();
2281 goto out_unlock_cgroup;
2282 }
2283
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002284 get_task_struct(tsk);
2285 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002286
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002287 threadgroup_lock(tsk);
2288 if (threadgroup) {
2289 if (!thread_group_leader(tsk)) {
2290 /*
2291 * a race with de_thread from another thread's exec()
2292 * may strip us of our leadership, if this happens,
2293 * there is no choice but to throw this task away and
2294 * try again; this is
2295 * "double-double-toil-and-trouble-check locking".
2296 */
2297 threadgroup_unlock(tsk);
2298 put_task_struct(tsk);
2299 goto retry_find_task;
2300 }
Li Zefan081aa452013-03-13 09:17:09 +08002301 }
2302
2303 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2304
Tejun Heocd3d0952011-12-12 18:12:21 -08002305 threadgroup_unlock(tsk);
2306
Paul Menagebbcb81d2007-10-18 23:39:32 -07002307 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002308out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002309 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002310 return ret;
2311}
2312
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002313/**
2314 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2315 * @from: attach to all cgroups of a given task
2316 * @tsk: the task to be attached
2317 */
2318int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2319{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002320 struct cgroup_root *root;
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002321 int retval = 0;
2322
Tejun Heo47cfcd02013-04-07 09:29:51 -07002323 mutex_lock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04002324 for_each_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002325 struct cgroup *from_cgrp;
2326
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002327 if (root == &cgrp_dfl_root)
Tejun Heo985ed672014-03-19 10:23:53 -04002328 continue;
2329
Tejun Heo96d365e2014-02-13 06:58:40 -05002330 down_read(&css_set_rwsem);
2331 from_cgrp = task_cgroup_from_root(from, root);
2332 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002333
Li Zefan6f4b7e62013-07-31 16:18:36 +08002334 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002335 if (retval)
2336 break;
2337 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002338 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002339
2340 return retval;
2341}
2342EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2343
Tejun Heo182446d2013-08-08 20:11:24 -04002344static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2345 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002346{
Tejun Heo182446d2013-08-08 20:11:24 -04002347 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002348}
2349
Tejun Heo182446d2013-08-08 20:11:24 -04002350static int cgroup_procs_write(struct cgroup_subsys_state *css,
2351 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002352{
Tejun Heo182446d2013-08-08 20:11:24 -04002353 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002354}
2355
Tejun Heo182446d2013-08-08 20:11:24 -04002356static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
Tejun Heo4d3bb512014-03-19 10:23:54 -04002357 struct cftype *cft, char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002358{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002359 struct cgroup_root *root = css->cgroup->root;
Tejun Heo5f469902014-02-11 11:52:48 -05002360
2361 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002362 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002363 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002364 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002365 strlcpy(root->release_agent_path, buffer,
2366 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002367 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002368 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002369 return 0;
2370}
2371
Tejun Heo2da8ca82013-12-05 12:28:04 -05002372static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002373{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002374 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002375
Paul Menagee788e062008-07-25 01:46:59 -07002376 if (!cgroup_lock_live_group(cgrp))
2377 return -ENODEV;
2378 seq_puts(seq, cgrp->root->release_agent_path);
2379 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002380 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002381 return 0;
2382}
2383
Tejun Heo2da8ca82013-12-05 12:28:04 -05002384static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002385{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002386 struct cgroup *cgrp = seq_css(seq)->cgroup;
2387
2388 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002389 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390}
2391
Tejun Heof8f22e52014-04-23 11:13:16 -04002392static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask)
2393{
2394 struct cgroup_subsys *ss;
2395 bool printed = false;
2396 int ssid;
2397
2398 for_each_subsys(ss, ssid) {
2399 if (ss_mask & (1 << ssid)) {
2400 if (printed)
2401 seq_putc(seq, ' ');
2402 seq_printf(seq, "%s", ss->name);
2403 printed = true;
2404 }
2405 }
2406 if (printed)
2407 seq_putc(seq, '\n');
2408}
2409
2410/* show controllers which are currently attached to the default hierarchy */
2411static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
2412{
2413 struct cgroup *cgrp = seq_css(seq)->cgroup;
2414
2415 cgroup_print_ss_mask(seq, cgrp->root->subsys_mask);
2416 return 0;
2417}
2418
2419/* show controllers which are enabled from the parent */
2420static int cgroup_controllers_show(struct seq_file *seq, void *v)
2421{
2422 struct cgroup *cgrp = seq_css(seq)->cgroup;
2423
2424 cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
2425 return 0;
2426}
2427
2428/* show controllers which are enabled for a given cgroup's children */
2429static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2430{
2431 struct cgroup *cgrp = seq_css(seq)->cgroup;
2432
2433 cgroup_print_ss_mask(seq, cgrp->child_subsys_mask);
2434 return 0;
2435}
2436
2437/**
2438 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2439 * @cgrp: root of the subtree to update csses for
2440 *
2441 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2442 * css associations need to be updated accordingly. This function looks up
2443 * all css_sets which are attached to the subtree, creates the matching
2444 * updated css_sets and migrates the tasks to the new ones.
2445 */
2446static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2447{
2448 LIST_HEAD(preloaded_csets);
2449 struct cgroup_subsys_state *css;
2450 struct css_set *src_cset;
2451 int ret;
2452
2453 lockdep_assert_held(&cgroup_tree_mutex);
2454 lockdep_assert_held(&cgroup_mutex);
2455
2456 /* look up all csses currently attached to @cgrp's subtree */
2457 down_read(&css_set_rwsem);
2458 css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
2459 struct cgrp_cset_link *link;
2460
2461 /* self is not affected by child_subsys_mask change */
2462 if (css->cgroup == cgrp)
2463 continue;
2464
2465 list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
2466 cgroup_migrate_add_src(link->cset, cgrp,
2467 &preloaded_csets);
2468 }
2469 up_read(&css_set_rwsem);
2470
2471 /* NULL dst indicates self on default hierarchy */
2472 ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2473 if (ret)
2474 goto out_finish;
2475
2476 list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
2477 struct task_struct *last_task = NULL, *task;
2478
2479 /* src_csets precede dst_csets, break on the first dst_cset */
2480 if (!src_cset->mg_src_cgrp)
2481 break;
2482
2483 /*
2484 * All tasks in src_cset need to be migrated to the
2485 * matching dst_cset. Empty it process by process. We
2486 * walk tasks but migrate processes. The leader might even
2487 * belong to a different cset but such src_cset would also
2488 * be among the target src_csets because the default
2489 * hierarchy enforces per-process membership.
2490 */
2491 while (true) {
2492 down_read(&css_set_rwsem);
2493 task = list_first_entry_or_null(&src_cset->tasks,
2494 struct task_struct, cg_list);
2495 if (task) {
2496 task = task->group_leader;
2497 WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp);
2498 get_task_struct(task);
2499 }
2500 up_read(&css_set_rwsem);
2501
2502 if (!task)
2503 break;
2504
2505 /* guard against possible infinite loop */
2506 if (WARN(last_task == task,
2507 "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n"))
2508 goto out_finish;
2509 last_task = task;
2510
2511 threadgroup_lock(task);
2512 /* raced against de_thread() from another thread? */
2513 if (!thread_group_leader(task)) {
2514 threadgroup_unlock(task);
2515 put_task_struct(task);
2516 continue;
2517 }
2518
2519 ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
2520
2521 threadgroup_unlock(task);
2522 put_task_struct(task);
2523
2524 if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
2525 goto out_finish;
2526 }
2527 }
2528
2529out_finish:
2530 cgroup_migrate_finish(&preloaded_csets);
2531 return ret;
2532}
2533
2534/* change the enabled child controllers for a cgroup in the default hierarchy */
2535static int cgroup_subtree_control_write(struct cgroup_subsys_state *dummy_css,
2536 struct cftype *cft, char *buffer)
2537{
Tejun Heo7d331fa2014-05-13 12:11:00 -04002538 unsigned int enable = 0, disable = 0;
Tejun Heof8f22e52014-04-23 11:13:16 -04002539 struct cgroup *cgrp = dummy_css->cgroup, *child;
2540 struct cgroup_subsys *ss;
2541 char *tok, *p;
2542 int ssid, ret;
2543
2544 /*
Tejun Heod37167a2014-05-13 12:10:59 -04002545 * Parse input - space separated list of subsystem names prefixed
2546 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04002547 */
2548 p = buffer;
Tejun Heod37167a2014-05-13 12:10:59 -04002549 while ((tok = strsep(&p, " "))) {
2550 if (tok[0] == '\0')
2551 continue;
Tejun Heof8f22e52014-04-23 11:13:16 -04002552 for_each_subsys(ss, ssid) {
2553 if (ss->disabled || strcmp(tok + 1, ss->name))
2554 continue;
2555
2556 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002557 enable |= 1 << ssid;
2558 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002559 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04002560 disable |= 1 << ssid;
2561 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04002562 } else {
2563 return -EINVAL;
2564 }
2565 break;
2566 }
2567 if (ssid == CGROUP_SUBSYS_COUNT)
2568 return -EINVAL;
2569 }
2570
2571 /*
2572 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2573 * active_ref. cgroup_lock_live_group() already provides enough
2574 * protection. Ensure @cgrp stays accessible and break the
2575 * active_ref protection.
2576 */
2577 cgroup_get(cgrp);
2578 kernfs_break_active_protection(cgrp->control_kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002579
2580 mutex_lock(&cgroup_tree_mutex);
2581
2582 for_each_subsys(ss, ssid) {
2583 if (enable & (1 << ssid)) {
2584 if (cgrp->child_subsys_mask & (1 << ssid)) {
2585 enable &= ~(1 << ssid);
2586 continue;
2587 }
2588
2589 /*
2590 * Because css offlining is asynchronous, userland
2591 * might try to re-enable the same controller while
2592 * the previous instance is still around. In such
2593 * cases, wait till it's gone using offline_waitq.
2594 */
2595 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo0cee8b72014-05-13 12:10:59 -04002596 DEFINE_WAIT(wait);
Tejun Heof8f22e52014-04-23 11:13:16 -04002597
2598 if (!cgroup_css(child, ss))
2599 continue;
2600
Tejun Heo0cee8b72014-05-13 12:10:59 -04002601 cgroup_get(child);
Tejun Heof8f22e52014-04-23 11:13:16 -04002602 prepare_to_wait(&child->offline_waitq, &wait,
2603 TASK_UNINTERRUPTIBLE);
2604 mutex_unlock(&cgroup_tree_mutex);
2605 schedule();
2606 finish_wait(&child->offline_waitq, &wait);
Tejun Heo0cee8b72014-05-13 12:10:59 -04002607 cgroup_put(child);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002608
2609 ret = restart_syscall();
2610 goto out_unbreak;
Tejun Heof8f22e52014-04-23 11:13:16 -04002611 }
2612
2613 /* unavailable or not enabled on the parent? */
2614 if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
2615 (cgrp->parent &&
2616 !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
2617 ret = -ENOENT;
2618 goto out_unlock_tree;
2619 }
2620 } else if (disable & (1 << ssid)) {
2621 if (!(cgrp->child_subsys_mask & (1 << ssid))) {
2622 disable &= ~(1 << ssid);
2623 continue;
2624 }
2625
2626 /* a child has it enabled? */
2627 cgroup_for_each_live_child(child, cgrp) {
2628 if (child->child_subsys_mask & (1 << ssid)) {
2629 ret = -EBUSY;
2630 goto out_unlock_tree;
2631 }
2632 }
2633 }
2634 }
2635
2636 if (!enable && !disable) {
2637 ret = 0;
2638 goto out_unlock_tree;
2639 }
2640
2641 if (!cgroup_lock_live_group(cgrp)) {
2642 ret = -ENODEV;
2643 goto out_unlock_tree;
2644 }
2645
2646 /*
2647 * Except for the root, child_subsys_mask must be zero for a cgroup
2648 * with tasks so that child cgroups don't compete against tasks.
2649 */
2650 if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
2651 ret = -EBUSY;
2652 goto out_unlock;
2653 }
2654
2655 /*
2656 * Create csses for enables and update child_subsys_mask. This
2657 * changes cgroup_e_css() results which in turn makes the
2658 * subsequent cgroup_update_dfl_csses() associate all tasks in the
2659 * subtree to the updated csses.
2660 */
2661 for_each_subsys(ss, ssid) {
2662 if (!(enable & (1 << ssid)))
2663 continue;
2664
2665 cgroup_for_each_live_child(child, cgrp) {
2666 ret = create_css(child, ss);
2667 if (ret)
2668 goto err_undo_css;
2669 }
2670 }
2671
2672 cgrp->child_subsys_mask |= enable;
2673 cgrp->child_subsys_mask &= ~disable;
2674
2675 ret = cgroup_update_dfl_csses(cgrp);
2676 if (ret)
2677 goto err_undo_css;
2678
2679 /* all tasks are now migrated away from the old csses, kill them */
2680 for_each_subsys(ss, ssid) {
2681 if (!(disable & (1 << ssid)))
2682 continue;
2683
2684 cgroup_for_each_live_child(child, cgrp)
2685 kill_css(cgroup_css(child, ss));
2686 }
2687
2688 kernfs_activate(cgrp->kn);
2689 ret = 0;
2690out_unlock:
2691 mutex_unlock(&cgroup_mutex);
2692out_unlock_tree:
2693 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo7d331fa2014-05-13 12:11:00 -04002694out_unbreak:
Tejun Heof8f22e52014-04-23 11:13:16 -04002695 kernfs_unbreak_active_protection(cgrp->control_kn);
2696 cgroup_put(cgrp);
2697 return ret;
2698
2699err_undo_css:
2700 cgrp->child_subsys_mask &= ~enable;
2701 cgrp->child_subsys_mask |= disable;
2702
2703 for_each_subsys(ss, ssid) {
2704 if (!(enable & (1 << ssid)))
2705 continue;
2706
2707 cgroup_for_each_live_child(child, cgrp) {
2708 struct cgroup_subsys_state *css = cgroup_css(child, ss);
2709 if (css)
2710 kill_css(css);
2711 }
2712 }
2713 goto out_unlock;
2714}
2715
Tejun Heo842b5972014-04-25 18:28:02 -04002716static int cgroup_populated_show(struct seq_file *seq, void *v)
2717{
2718 seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt);
2719 return 0;
2720}
2721
Tejun Heo2bd59d42014-02-11 11:52:49 -05002722static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2723 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002725 struct cgroup *cgrp = of->kn->parent->priv;
2726 struct cftype *cft = of->kn->priv;
2727 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002728 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002729
Tejun Heo2bd59d42014-02-11 11:52:49 -05002730 /*
2731 * kernfs guarantees that a file isn't deleted with operations in
2732 * flight, which means that the matching css is and stays alive and
2733 * doesn't need to be pinned. The RCU locking is not necessary
2734 * either. It's just for the convenience of using cgroup_css().
2735 */
2736 rcu_read_lock();
2737 css = cgroup_css(cgrp, cft->ss);
2738 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739
Tejun Heoa742c592013-12-05 12:28:03 -05002740 if (cft->write_string) {
2741 ret = cft->write_string(css, cft, strstrip(buf));
2742 } else if (cft->write_u64) {
2743 unsigned long long v;
2744 ret = kstrtoull(buf, 0, &v);
2745 if (!ret)
2746 ret = cft->write_u64(css, cft, v);
2747 } else if (cft->write_s64) {
2748 long long v;
2749 ret = kstrtoll(buf, 0, &v);
2750 if (!ret)
2751 ret = cft->write_s64(css, cft, v);
2752 } else if (cft->trigger) {
2753 ret = cft->trigger(css, (unsigned int)cft->private);
2754 } else {
2755 ret = -EINVAL;
2756 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002757
Tejun Heoa742c592013-12-05 12:28:03 -05002758 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002759}
2760
Tejun Heo6612f052013-12-05 12:28:04 -05002761static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002762{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002763 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002764}
2765
2766static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2767{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002768 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002769}
2770
2771static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2772{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002773 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002774}
2775
2776static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2777{
Tejun Heo7da11272013-12-05 12:28:04 -05002778 struct cftype *cft = seq_cft(m);
2779 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002780
Tejun Heo2da8ca82013-12-05 12:28:04 -05002781 if (cft->seq_show)
2782 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002783
Tejun Heo896f5192013-12-05 12:28:04 -05002784 if (cft->read_u64)
2785 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2786 else if (cft->read_s64)
2787 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2788 else
2789 return -EINVAL;
2790 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002791}
2792
Tejun Heo2bd59d42014-02-11 11:52:49 -05002793static struct kernfs_ops cgroup_kf_single_ops = {
2794 .atomic_write_len = PAGE_SIZE,
2795 .write = cgroup_file_write,
2796 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002797};
2798
Tejun Heo2bd59d42014-02-11 11:52:49 -05002799static struct kernfs_ops cgroup_kf_ops = {
2800 .atomic_write_len = PAGE_SIZE,
2801 .write = cgroup_file_write,
2802 .seq_start = cgroup_seqfile_start,
2803 .seq_next = cgroup_seqfile_next,
2804 .seq_stop = cgroup_seqfile_stop,
2805 .seq_show = cgroup_seqfile_show,
2806};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002807
2808/*
2809 * cgroup_rename - Only allow simple rename of directories in place.
2810 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002811static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2812 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002813{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002814 struct cgroup *cgrp = kn->priv;
Li Zefan65dff752013-03-01 15:01:56 +08002815 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002816
Tejun Heo2bd59d42014-02-11 11:52:49 -05002817 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002818 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002819 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002820 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002821
Tejun Heo6db8e852013-06-14 11:18:22 -07002822 /*
2823 * This isn't a proper migration and its usefulness is very
2824 * limited. Disallow if sane_behavior.
2825 */
2826 if (cgroup_sane_behavior(cgrp))
2827 return -EPERM;
2828
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002829 /*
2830 * We're gonna grab cgroup_tree_mutex which nests outside kernfs
2831 * active_ref. kernfs_rename() doesn't require active_ref
2832 * protection. Break them before grabbing cgroup_tree_mutex.
2833 */
2834 kernfs_break_active_protection(new_parent);
2835 kernfs_break_active_protection(kn);
Li Zefan65dff752013-03-01 15:01:56 +08002836
Tejun Heo2bd59d42014-02-11 11:52:49 -05002837 mutex_lock(&cgroup_tree_mutex);
2838 mutex_lock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08002839
Tejun Heo2bd59d42014-02-11 11:52:49 -05002840 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002841
Tejun Heo2bd59d42014-02-11 11:52:49 -05002842 mutex_unlock(&cgroup_mutex);
2843 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002844
Tejun Heoe1b2dc12014-03-20 11:10:15 -04002845 kernfs_unbreak_active_protection(kn);
2846 kernfs_unbreak_active_protection(new_parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002847 return ret;
Li Zefan099fca32009-04-02 16:57:29 -07002848}
2849
Tejun Heo49957f82014-04-07 16:44:47 -04002850/* set uid and gid of cgroup dirs and files to that of the creator */
2851static int cgroup_kn_set_ugid(struct kernfs_node *kn)
2852{
2853 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
2854 .ia_uid = current_fsuid(),
2855 .ia_gid = current_fsgid(), };
2856
2857 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
2858 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
2859 return 0;
2860
2861 return kernfs_setattr(kn, &iattr);
2862}
2863
Tejun Heo2bb566c2013-08-08 20:11:23 -04002864static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002865{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002866 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002867 struct kernfs_node *kn;
2868 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04002869 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002870
Tejun Heo2bd59d42014-02-11 11:52:49 -05002871#ifdef CONFIG_DEBUG_LOCK_ALLOC
2872 key = &cft->lockdep_key;
2873#endif
2874 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2875 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2876 NULL, false, key);
Tejun Heo49957f82014-04-07 16:44:47 -04002877 if (IS_ERR(kn))
2878 return PTR_ERR(kn);
2879
2880 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002881 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04002882 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04002883 return ret;
2884 }
2885
2886 if (cft->seq_show == cgroup_subtree_control_show)
2887 cgrp->control_kn = kn;
Tejun Heo842b5972014-04-25 18:28:02 -04002888 else if (cft->seq_show == cgroup_populated_show)
2889 cgrp->populated_kn = kn;
Tejun Heof8f22e52014-04-23 11:13:16 -04002890 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002891}
2892
Tejun Heob1f28d32013-06-28 16:24:10 -07002893/**
2894 * cgroup_addrm_files - add or remove files to a cgroup directory
2895 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002896 * @cfts: array of cftypes to be added
2897 * @is_add: whether to add or remove
2898 *
2899 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002900 * For removals, this function never fails. If addition fails, this
2901 * function doesn't remove files already added. The caller is responsible
2902 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002903 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002904static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2905 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002906{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002907 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002908 int ret;
2909
Tejun Heoace2bee2014-02-11 11:52:47 -05002910 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002911
2912 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002913 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04002914 if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
2915 continue;
Tejun Heo873fe092013-04-14 20:15:26 -07002916 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2917 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002918 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2919 continue;
2920 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2921 continue;
2922
Li Zefan2739d3c2013-01-21 18:18:33 +08002923 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002924 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002925 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04002926 pr_warn("%s: failed to add %s, err=%d\n",
2927 __func__, cft->name, ret);
Tejun Heob1f28d32013-06-28 16:24:10 -07002928 return ret;
2929 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002930 } else {
2931 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002932 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002933 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002934 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002935}
2936
Tejun Heo21a2d342014-02-12 09:29:49 -05002937static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002938{
2939 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002940 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002941 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04002942 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002943 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002944
Tejun Heo21a2d342014-02-12 09:29:49 -05002945 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002946
Li Zefane8c82d22013-06-18 18:48:37 +08002947 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002948 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002949 struct cgroup *cgrp = css->cgroup;
2950
Li Zefane8c82d22013-06-18 18:48:37 +08002951 if (cgroup_is_dead(cgrp))
2952 continue;
2953
Tejun Heo21a2d342014-02-12 09:29:49 -05002954 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002955 if (ret)
2956 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002957 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002958
2959 if (is_add && !ret)
2960 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002961 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002962}
2963
Tejun Heo2da440a2014-02-11 11:52:48 -05002964static void cgroup_exit_cftypes(struct cftype *cfts)
2965{
2966 struct cftype *cft;
2967
Tejun Heo2bd59d42014-02-11 11:52:49 -05002968 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2969 /* free copy for custom atomic_write_len, see init_cftypes() */
2970 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2971 kfree(cft->kf_ops);
2972 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002973 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002974 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002975}
2976
Tejun Heo2bd59d42014-02-11 11:52:49 -05002977static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002978{
2979 struct cftype *cft;
2980
Tejun Heo2bd59d42014-02-11 11:52:49 -05002981 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2982 struct kernfs_ops *kf_ops;
2983
Tejun Heo0adb0702014-02-12 09:29:48 -05002984 WARN_ON(cft->ss || cft->kf_ops);
2985
Tejun Heo2bd59d42014-02-11 11:52:49 -05002986 if (cft->seq_start)
2987 kf_ops = &cgroup_kf_ops;
2988 else
2989 kf_ops = &cgroup_kf_single_ops;
2990
2991 /*
2992 * Ugh... if @cft wants a custom max_write_len, we need to
2993 * make a copy of kf_ops to set its atomic_write_len.
2994 */
2995 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2996 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2997 if (!kf_ops) {
2998 cgroup_exit_cftypes(cfts);
2999 return -ENOMEM;
3000 }
3001 kf_ops->atomic_write_len = cft->max_write_len;
3002 }
3003
3004 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05003005 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003006 }
3007
3008 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05003009}
3010
Tejun Heo21a2d342014-02-12 09:29:49 -05003011static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3012{
3013 lockdep_assert_held(&cgroup_tree_mutex);
3014
3015 if (!cfts || !cfts[0].ss)
3016 return -ENOENT;
3017
3018 list_del(&cfts->node);
3019 cgroup_apply_cftypes(cfts, false);
3020 cgroup_exit_cftypes(cfts);
3021 return 0;
3022}
3023
Tejun Heo8e3f6542012-04-01 12:09:55 -07003024/**
Tejun Heo80b13582014-02-12 09:29:48 -05003025 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3026 * @cfts: zero-length name terminated array of cftypes
3027 *
3028 * Unregister @cfts. Files described by @cfts are removed from all
3029 * existing cgroups and all future cgroups won't have them either. This
3030 * function can be called anytime whether @cfts' subsys is attached or not.
3031 *
3032 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3033 * registered.
3034 */
3035int cgroup_rm_cftypes(struct cftype *cfts)
3036{
Tejun Heo21a2d342014-02-12 09:29:49 -05003037 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05003038
Tejun Heo21a2d342014-02-12 09:29:49 -05003039 mutex_lock(&cgroup_tree_mutex);
3040 ret = cgroup_rm_cftypes_locked(cfts);
3041 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003042 return ret;
3043}
3044
3045/**
3046 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3047 * @ss: target cgroup subsystem
3048 * @cfts: zero-length name terminated array of cftypes
3049 *
3050 * Register @cfts to @ss. Files described by @cfts are created for all
3051 * existing cgroups to which @ss is attached and all future cgroups will
3052 * have them too. This function can be called anytime whether @ss is
3053 * attached or not.
3054 *
3055 * Returns 0 on successful registration, -errno on failure. Note that this
3056 * function currently returns 0 as long as @cfts registration is successful
3057 * even if some file creation attempts on existing cgroups fail.
3058 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04003059int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07003060{
Tejun Heo9ccece82013-06-28 16:24:11 -07003061 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003062
Li Zefandc5736e2014-02-17 10:41:50 +08003063 if (!cfts || cfts[0].name[0] == '\0')
3064 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003065
Tejun Heo2bd59d42014-02-11 11:52:49 -05003066 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07003067 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05003068 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003069
Tejun Heo21a2d342014-02-12 09:29:49 -05003070 mutex_lock(&cgroup_tree_mutex);
3071
Tejun Heo0adb0702014-02-12 09:29:48 -05003072 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05003073 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07003074 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05003075 cgroup_rm_cftypes_locked(cfts);
3076
3077 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07003078 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003079}
Tejun Heo79578622012-04-01 12:09:56 -07003080
3081/**
Li Zefana043e3b2008-02-23 15:24:09 -08003082 * cgroup_task_count - count the number of tasks in a cgroup.
3083 * @cgrp: the cgroup in question
3084 *
3085 * Return the number of tasks in the cgroup.
3086 */
Tejun Heo07bc3562014-02-13 06:58:39 -05003087static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003088{
3089 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07003090 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003091
Tejun Heo96d365e2014-02-13 06:58:40 -05003092 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07003093 list_for_each_entry(link, &cgrp->cset_links, cset_link)
3094 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05003095 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003096 return count;
3097}
3098
Tejun Heo574bd9f2012-11-09 09:12:29 -08003099/**
Tejun Heo492eb212013-08-08 20:11:25 -04003100 * css_next_child - find the next child of a given css
3101 * @pos_css: the current position (%NULL to initiate traversal)
3102 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003103 *
Tejun Heo492eb212013-08-08 20:11:25 -04003104 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05003105 * under either cgroup_mutex or RCU read lock. The only requirement is
3106 * that @parent_css and @pos_css are accessible. The next sibling is
3107 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003108 */
Tejun Heo492eb212013-08-08 20:11:25 -04003109struct cgroup_subsys_state *
3110css_next_child(struct cgroup_subsys_state *pos_css,
3111 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003112{
Tejun Heo492eb212013-08-08 20:11:25 -04003113 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3114 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003115 struct cgroup *next;
3116
Tejun Heoace2bee2014-02-11 11:52:47 -05003117 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09003118
3119 /*
3120 * @pos could already have been removed. Once a cgroup is removed,
3121 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003122 * changes. As CGRP_DEAD assertion is serialized and happens
3123 * before the cgroup is taken off the ->sibling list, if we see it
3124 * unasserted, it's guaranteed that the next sibling hasn't
3125 * finished its grace period even if it's already removed, and thus
3126 * safe to dereference from this RCU critical section. If
3127 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3128 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003129 *
3130 * If @pos is dead, its next pointer can't be dereferenced;
3131 * however, as each cgroup is given a monotonically increasing
3132 * unique serial number and always appended to the sibling list,
3133 * the next one can be found by walking the parent's children until
3134 * we see a cgroup with higher serial number than @pos's. While
3135 * this path can be slower, it's taken only when either the current
3136 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003137 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003138 if (!pos) {
3139 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3140 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003141 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003142 } else {
3143 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3144 if (next->serial_nr > pos->serial_nr)
3145 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003146 }
3147
Tejun Heo3b281af2014-04-23 11:13:15 -04003148 /*
3149 * @next, if not pointing to the head, can be dereferenced and is
3150 * the next sibling; however, it might have @ss disabled. If so,
3151 * fast-forward to the next enabled one.
3152 */
3153 while (&next->sibling != &cgrp->children) {
3154 struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
Tejun Heo492eb212013-08-08 20:11:25 -04003155
Tejun Heo3b281af2014-04-23 11:13:15 -04003156 if (next_css)
3157 return next_css;
3158 next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
3159 }
3160 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09003161}
Tejun Heo53fa5262013-05-24 10:55:38 +09003162
3163/**
Tejun Heo492eb212013-08-08 20:11:25 -04003164 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003165 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003166 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003167 *
Tejun Heo492eb212013-08-08 20:11:25 -04003168 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003169 * to visit for pre-order traversal of @root's descendants. @root is
3170 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003171 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003172 * While this function requires cgroup_mutex or RCU read locking, it
3173 * doesn't require the whole traversal to be contained in a single critical
3174 * section. This function will return the correct next descendant as long
3175 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003176 */
Tejun Heo492eb212013-08-08 20:11:25 -04003177struct cgroup_subsys_state *
3178css_next_descendant_pre(struct cgroup_subsys_state *pos,
3179 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003180{
Tejun Heo492eb212013-08-08 20:11:25 -04003181 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003182
Tejun Heoace2bee2014-02-11 11:52:47 -05003183 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003184
Tejun Heobd8815a2013-08-08 20:11:27 -04003185 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003186 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003187 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003188
3189 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003190 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003191 if (next)
3192 return next;
3193
3194 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003195 while (pos != root) {
3196 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003197 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003198 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003199 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003200 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003201
3202 return NULL;
3203}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003204
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003205/**
Tejun Heo492eb212013-08-08 20:11:25 -04003206 * css_rightmost_descendant - return the rightmost descendant of a css
3207 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003208 *
Tejun Heo492eb212013-08-08 20:11:25 -04003209 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3210 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003211 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003212 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003213 * While this function requires cgroup_mutex or RCU read locking, it
3214 * doesn't require the whole traversal to be contained in a single critical
3215 * section. This function will return the correct rightmost descendant as
3216 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003217 */
Tejun Heo492eb212013-08-08 20:11:25 -04003218struct cgroup_subsys_state *
3219css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003220{
Tejun Heo492eb212013-08-08 20:11:25 -04003221 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003222
Tejun Heoace2bee2014-02-11 11:52:47 -05003223 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003224
3225 do {
3226 last = pos;
3227 /* ->prev isn't RCU safe, walk ->next till the end */
3228 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003229 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003230 pos = tmp;
3231 } while (pos);
3232
3233 return last;
3234}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003235
Tejun Heo492eb212013-08-08 20:11:25 -04003236static struct cgroup_subsys_state *
3237css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003238{
Tejun Heo492eb212013-08-08 20:11:25 -04003239 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003240
3241 do {
3242 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003243 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003244 } while (pos);
3245
3246 return last;
3247}
3248
3249/**
Tejun Heo492eb212013-08-08 20:11:25 -04003250 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003251 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003252 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003253 *
Tejun Heo492eb212013-08-08 20:11:25 -04003254 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003255 * to visit for post-order traversal of @root's descendants. @root is
3256 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003257 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003258 * While this function requires cgroup_mutex or RCU read locking, it
3259 * doesn't require the whole traversal to be contained in a single critical
3260 * section. This function will return the correct next descendant as long
3261 * as both @pos and @cgroup are accessible and @pos is a descendant of
3262 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003263 */
Tejun Heo492eb212013-08-08 20:11:25 -04003264struct cgroup_subsys_state *
3265css_next_descendant_post(struct cgroup_subsys_state *pos,
3266 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003267{
Tejun Heo492eb212013-08-08 20:11:25 -04003268 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003269
Tejun Heoace2bee2014-02-11 11:52:47 -05003270 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003271
Tejun Heo58b79a92013-09-06 15:31:08 -04003272 /* if first iteration, visit leftmost descendant which may be @root */
3273 if (!pos)
3274 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003275
Tejun Heobd8815a2013-08-08 20:11:27 -04003276 /* if we visited @root, we're done */
3277 if (pos == root)
3278 return NULL;
3279
Tejun Heo574bd9f2012-11-09 09:12:29 -08003280 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003281 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003282 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003283 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003284
3285 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003286 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003287}
Tejun Heo574bd9f2012-11-09 09:12:29 -08003288
Tejun Heo0942eee2013-08-08 20:11:26 -04003289/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003290 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003291 * @it: the iterator to advance
3292 *
3293 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003294 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003295static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003296{
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003297 struct list_head *l = it->cset_pos;
Tejun Heod5158762013-08-08 20:11:26 -04003298 struct cgrp_cset_link *link;
3299 struct css_set *cset;
3300
3301 /* Advance to the next non-empty css_set */
3302 do {
3303 l = l->next;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003304 if (l == it->cset_head) {
3305 it->cset_pos = NULL;
Tejun Heod5158762013-08-08 20:11:26 -04003306 return;
3307 }
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003308
3309 if (it->ss) {
3310 cset = container_of(l, struct css_set,
3311 e_cset_node[it->ss->id]);
3312 } else {
3313 link = list_entry(l, struct cgrp_cset_link, cset_link);
3314 cset = link->cset;
3315 }
Tejun Heoc7561122014-02-25 10:04:01 -05003316 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
3317
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003318 it->cset_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003319
3320 if (!list_empty(&cset->tasks))
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003321 it->task_pos = cset->tasks.next;
Tejun Heoc7561122014-02-25 10:04:01 -05003322 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003323 it->task_pos = cset->mg_tasks.next;
3324
3325 it->tasks_head = &cset->tasks;
3326 it->mg_tasks_head = &cset->mg_tasks;
Tejun Heod5158762013-08-08 20:11:26 -04003327}
3328
Tejun Heo0942eee2013-08-08 20:11:26 -04003329/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003330 * css_task_iter_start - initiate task iteration
3331 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003332 * @it: the task iterator to use
3333 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003334 * Initiate iteration through the tasks of @css. The caller can call
3335 * css_task_iter_next() to walk through the tasks until the function
3336 * returns NULL. On completion of iteration, css_task_iter_end() must be
3337 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003338 *
3339 * Note that this function acquires a lock which is released when the
3340 * iteration finishes. The caller can't sleep while iteration is in
3341 * progress.
3342 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003343void css_task_iter_start(struct cgroup_subsys_state *css,
3344 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003345 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003346{
Tejun Heo56fde9e2014-02-13 06:58:38 -05003347 /* no one should try to iterate before mounting cgroups */
3348 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003349
Tejun Heo96d365e2014-02-13 06:58:40 -05003350 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003351
Tejun Heo3ebb2b62014-04-23 11:13:15 -04003352 it->ss = css->ss;
3353
3354 if (it->ss)
3355 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3356 else
3357 it->cset_pos = &css->cgroup->cset_links;
3358
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003359 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003360
Tejun Heo72ec7022013-08-08 20:11:26 -04003361 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003362}
3363
Tejun Heo0942eee2013-08-08 20:11:26 -04003364/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003365 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003366 * @it: the task iterator being iterated
3367 *
3368 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003369 * initialized via css_task_iter_start(). Returns NULL when the iteration
3370 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003371 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003372struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003373{
3374 struct task_struct *res;
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003375 struct list_head *l = it->task_pos;
Paul Menage817929e2007-10-18 23:39:36 -07003376
3377 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003378 if (!it->cset_pos)
Paul Menage817929e2007-10-18 23:39:36 -07003379 return NULL;
3380 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05003381
3382 /*
3383 * Advance iterator to find next entry. cset->tasks is consumed
3384 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3385 * next cset.
3386 */
Paul Menage817929e2007-10-18 23:39:36 -07003387 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003388
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003389 if (l == it->tasks_head)
3390 l = it->mg_tasks_head->next;
Tejun Heoc7561122014-02-25 10:04:01 -05003391
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003392 if (l == it->mg_tasks_head)
Tejun Heo72ec7022013-08-08 20:11:26 -04003393 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05003394 else
Tejun Heo0f0a2b42014-04-23 11:13:15 -04003395 it->task_pos = l;
Tejun Heoc7561122014-02-25 10:04:01 -05003396
Paul Menage817929e2007-10-18 23:39:36 -07003397 return res;
3398}
3399
Tejun Heo0942eee2013-08-08 20:11:26 -04003400/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003401 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003402 * @it: the task iterator to finish
3403 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003404 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003405 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003406void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05003407 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07003408{
Tejun Heo96d365e2014-02-13 06:58:40 -05003409 up_read(&css_set_rwsem);
Tejun Heo8cc99342013-04-07 09:29:50 -07003410}
3411
3412/**
3413 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3414 * @to: cgroup to which the tasks will be moved
3415 * @from: cgroup in which the tasks currently reside
Tejun Heoeaf797a2014-02-25 10:04:03 -05003416 *
3417 * Locking rules between cgroup_post_fork() and the migration path
3418 * guarantee that, if a task is forking while being migrated, the new child
3419 * is guaranteed to be either visible in the source cgroup after the
3420 * parent's migration is complete or put into the target cgroup. No task
3421 * can slip out of migration through forking.
Tejun Heo8cc99342013-04-07 09:29:50 -07003422 */
3423int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3424{
Tejun Heo952aaa12014-02-25 10:04:03 -05003425 LIST_HEAD(preloaded_csets);
3426 struct cgrp_cset_link *link;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003427 struct css_task_iter it;
3428 struct task_struct *task;
Tejun Heo952aaa12014-02-25 10:04:03 -05003429 int ret;
Tejun Heoe406d1c2014-02-13 06:58:39 -05003430
Tejun Heo952aaa12014-02-25 10:04:03 -05003431 mutex_lock(&cgroup_mutex);
3432
3433 /* all tasks in @from are being moved, all csets are source */
3434 down_read(&css_set_rwsem);
3435 list_for_each_entry(link, &from->cset_links, cset_link)
3436 cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
3437 up_read(&css_set_rwsem);
3438
3439 ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
3440 if (ret)
3441 goto out_err;
3442
3443 /*
3444 * Migrate tasks one-by-one until @form is empty. This fails iff
3445 * ->can_attach() fails.
3446 */
Tejun Heoe406d1c2014-02-13 06:58:39 -05003447 do {
3448 css_task_iter_start(&from->dummy_css, &it);
3449 task = css_task_iter_next(&it);
3450 if (task)
3451 get_task_struct(task);
3452 css_task_iter_end(&it);
3453
3454 if (task) {
Tejun Heo952aaa12014-02-25 10:04:03 -05003455 ret = cgroup_migrate(to, task, false);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003456 put_task_struct(task);
3457 }
3458 } while (task && !ret);
Tejun Heo952aaa12014-02-25 10:04:03 -05003459out_err:
3460 cgroup_migrate_finish(&preloaded_csets);
3461 mutex_unlock(&cgroup_mutex);
Tejun Heoe406d1c2014-02-13 06:58:39 -05003462 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07003463}
3464
Paul Menage817929e2007-10-18 23:39:36 -07003465/*
Ben Blum102a7752009-09-23 15:56:26 -07003466 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003467 *
3468 * Reading this file can return large amounts of data if a cgroup has
3469 * *lots* of attached tasks. So it may need several calls to read(),
3470 * but we cannot guarantee that the information we produce is correct
3471 * unless we produce it entirely atomically.
3472 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003473 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003474
Li Zefan24528252012-01-20 11:58:43 +08003475/* which pidlist file are we talking about? */
3476enum cgroup_filetype {
3477 CGROUP_FILE_PROCS,
3478 CGROUP_FILE_TASKS,
3479};
3480
3481/*
3482 * A pidlist is a list of pids that virtually represents the contents of one
3483 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3484 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3485 * to the cgroup.
3486 */
3487struct cgroup_pidlist {
3488 /*
3489 * used to find which pidlist is wanted. doesn't change as long as
3490 * this particular list stays in the list.
3491 */
3492 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3493 /* array of xids */
3494 pid_t *list;
3495 /* how many elements the above list has */
3496 int length;
Li Zefan24528252012-01-20 11:58:43 +08003497 /* each of these stored in a list by its cgroup */
3498 struct list_head links;
3499 /* pointer to the cgroup we belong to, for list removal purposes */
3500 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003501 /* for delayed destruction */
3502 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003503};
3504
Paul Menagebbcb81d2007-10-18 23:39:32 -07003505/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003506 * The following two functions "fix" the issue where there are more pids
3507 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3508 * TODO: replace with a kernel-wide solution to this problem
3509 */
3510#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3511static void *pidlist_allocate(int count)
3512{
3513 if (PIDLIST_TOO_LARGE(count))
3514 return vmalloc(count * sizeof(pid_t));
3515 else
3516 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3517}
Tejun Heob1a21362013-11-29 10:42:58 -05003518
Ben Blumd1d9fd32009-09-23 15:56:28 -07003519static void pidlist_free(void *p)
3520{
3521 if (is_vmalloc_addr(p))
3522 vfree(p);
3523 else
3524 kfree(p);
3525}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003526
3527/*
Tejun Heob1a21362013-11-29 10:42:58 -05003528 * Used to destroy all pidlists lingering waiting for destroy timer. None
3529 * should be left afterwards.
3530 */
3531static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3532{
3533 struct cgroup_pidlist *l, *tmp_l;
3534
3535 mutex_lock(&cgrp->pidlist_mutex);
3536 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3537 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3538 mutex_unlock(&cgrp->pidlist_mutex);
3539
3540 flush_workqueue(cgroup_pidlist_destroy_wq);
3541 BUG_ON(!list_empty(&cgrp->pidlists));
3542}
3543
3544static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3545{
3546 struct delayed_work *dwork = to_delayed_work(work);
3547 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3548 destroy_dwork);
3549 struct cgroup_pidlist *tofree = NULL;
3550
3551 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003552
3553 /*
Tejun Heo04502362013-11-29 10:42:59 -05003554 * Destroy iff we didn't get queued again. The state won't change
3555 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003556 */
Tejun Heo04502362013-11-29 10:42:59 -05003557 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003558 list_del(&l->links);
3559 pidlist_free(l->list);
3560 put_pid_ns(l->key.ns);
3561 tofree = l;
3562 }
3563
Tejun Heob1a21362013-11-29 10:42:58 -05003564 mutex_unlock(&l->owner->pidlist_mutex);
3565 kfree(tofree);
3566}
3567
3568/*
Ben Blum102a7752009-09-23 15:56:26 -07003569 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003570 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003571 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003572static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003573{
Ben Blum102a7752009-09-23 15:56:26 -07003574 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003575
3576 /*
3577 * we presume the 0th element is unique, so i starts at 1. trivial
3578 * edge cases first; no work needs to be done for either
3579 */
3580 if (length == 0 || length == 1)
3581 return length;
3582 /* src and dest walk down the list; dest counts unique elements */
3583 for (src = 1; src < length; src++) {
3584 /* find next unique element */
3585 while (list[src] == list[src-1]) {
3586 src++;
3587 if (src == length)
3588 goto after;
3589 }
3590 /* dest always points to where the next unique element goes */
3591 list[dest] = list[src];
3592 dest++;
3593 }
3594after:
Ben Blum102a7752009-09-23 15:56:26 -07003595 return dest;
3596}
3597
Tejun Heoafb2bc12013-11-29 10:42:59 -05003598/*
3599 * The two pid files - task and cgroup.procs - guaranteed that the result
3600 * is sorted, which forced this whole pidlist fiasco. As pid order is
3601 * different per namespace, each namespace needs differently sorted list,
3602 * making it impossible to use, for example, single rbtree of member tasks
3603 * sorted by task pointer. As pidlists can be fairly large, allocating one
3604 * per open file is dangerous, so cgroup had to implement shared pool of
3605 * pidlists keyed by cgroup and namespace.
3606 *
3607 * All this extra complexity was caused by the original implementation
3608 * committing to an entirely unnecessary property. In the long term, we
3609 * want to do away with it. Explicitly scramble sort order if
3610 * sane_behavior so that no such expectation exists in the new interface.
3611 *
3612 * Scrambling is done by swapping every two consecutive bits, which is
3613 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3614 */
3615static pid_t pid_fry(pid_t pid)
3616{
3617 unsigned a = pid & 0x55555555;
3618 unsigned b = pid & 0xAAAAAAAA;
3619
3620 return (a << 1) | (b >> 1);
3621}
3622
3623static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3624{
3625 if (cgroup_sane_behavior(cgrp))
3626 return pid_fry(pid);
3627 else
3628 return pid;
3629}
3630
Ben Blum102a7752009-09-23 15:56:26 -07003631static int cmppid(const void *a, const void *b)
3632{
3633 return *(pid_t *)a - *(pid_t *)b;
3634}
3635
Tejun Heoafb2bc12013-11-29 10:42:59 -05003636static int fried_cmppid(const void *a, const void *b)
3637{
3638 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3639}
3640
Ben Blum72a8cb32009-09-23 15:56:27 -07003641static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3642 enum cgroup_filetype type)
3643{
3644 struct cgroup_pidlist *l;
3645 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003646 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003647
Tejun Heoe6b81712013-11-29 10:42:59 -05003648 lockdep_assert_held(&cgrp->pidlist_mutex);
3649
3650 list_for_each_entry(l, &cgrp->pidlists, links)
3651 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003652 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003653 return NULL;
3654}
3655
Ben Blum72a8cb32009-09-23 15:56:27 -07003656/*
3657 * find the appropriate pidlist for our purpose (given procs vs tasks)
3658 * returns with the lock on that pidlist already held, and takes care
3659 * of the use count, or returns NULL with no locks held if we're out of
3660 * memory.
3661 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003662static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3663 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003664{
3665 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003666
Tejun Heoe6b81712013-11-29 10:42:59 -05003667 lockdep_assert_held(&cgrp->pidlist_mutex);
3668
3669 l = cgroup_pidlist_find(cgrp, type);
3670 if (l)
3671 return l;
3672
Ben Blum72a8cb32009-09-23 15:56:27 -07003673 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003674 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003675 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003676 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003677
Tejun Heob1a21362013-11-29 10:42:58 -05003678 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003679 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003680 /* don't need task_nsproxy() if we're looking at ourself */
3681 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003682 l->owner = cgrp;
3683 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003684 return l;
3685}
3686
3687/*
Ben Blum102a7752009-09-23 15:56:26 -07003688 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3689 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003690static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3691 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003692{
3693 pid_t *array;
3694 int length;
3695 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003696 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003697 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003698 struct cgroup_pidlist *l;
3699
Tejun Heo4bac00d2013-11-29 10:42:59 -05003700 lockdep_assert_held(&cgrp->pidlist_mutex);
3701
Ben Blum102a7752009-09-23 15:56:26 -07003702 /*
3703 * If cgroup gets more users after we read count, we won't have
3704 * enough space - tough. This race is indistinguishable to the
3705 * caller from the case that the additional cgroup users didn't
3706 * show up until sometime later on.
3707 */
3708 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003709 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003710 if (!array)
3711 return -ENOMEM;
3712 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003713 css_task_iter_start(&cgrp->dummy_css, &it);
3714 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003715 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003716 break;
Ben Blum102a7752009-09-23 15:56:26 -07003717 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003718 if (type == CGROUP_FILE_PROCS)
3719 pid = task_tgid_vnr(tsk);
3720 else
3721 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003722 if (pid > 0) /* make sure to only use valid results */
3723 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003724 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003725 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003726 length = n;
3727 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003728 if (cgroup_sane_behavior(cgrp))
3729 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3730 else
3731 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003732 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003733 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003734
Tejun Heoe6b81712013-11-29 10:42:59 -05003735 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003736 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003737 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003738 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003739 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003740 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003741
3742 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003743 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003744 l->list = array;
3745 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003746 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003747 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003748}
3749
Balbir Singh846c7bb2007-10-18 23:39:44 -07003750/**
Li Zefana043e3b2008-02-23 15:24:09 -08003751 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003752 * @stats: cgroupstats to fill information into
3753 * @dentry: A dentry entry belonging to the cgroup for which stats have
3754 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003755 *
3756 * Build and fill cgroupstats so that taskstats can export it to user
3757 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003758 */
3759int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3760{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003761 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003762 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003763 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003764 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003765
Tejun Heo2bd59d42014-02-11 11:52:49 -05003766 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3767 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3768 kernfs_type(kn) != KERNFS_DIR)
3769 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003770
Li Zefanbad34662014-02-14 16:54:28 +08003771 mutex_lock(&cgroup_mutex);
3772
Tejun Heo2bd59d42014-02-11 11:52:49 -05003773 /*
3774 * We aren't being called from kernfs and there's no guarantee on
3775 * @kn->priv's validity. For this and css_tryget_from_dir(),
3776 * @kn->priv is RCU safe. Let's do the RCU dancing.
3777 */
3778 rcu_read_lock();
3779 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003780 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003781 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003782 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003783 return -ENOENT;
3784 }
Li Zefanbad34662014-02-14 16:54:28 +08003785 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003786
Tejun Heo72ec7022013-08-08 20:11:26 -04003787 css_task_iter_start(&cgrp->dummy_css, &it);
3788 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003789 switch (tsk->state) {
3790 case TASK_RUNNING:
3791 stats->nr_running++;
3792 break;
3793 case TASK_INTERRUPTIBLE:
3794 stats->nr_sleeping++;
3795 break;
3796 case TASK_UNINTERRUPTIBLE:
3797 stats->nr_uninterruptible++;
3798 break;
3799 case TASK_STOPPED:
3800 stats->nr_stopped++;
3801 break;
3802 default:
3803 if (delayacct_is_task_waiting_on_io(tsk))
3804 stats->nr_io_wait++;
3805 break;
3806 }
3807 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003808 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003809
Li Zefanbad34662014-02-14 16:54:28 +08003810 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003811 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003812}
3813
Paul Menage8f3ff202009-09-23 15:56:25 -07003814
Paul Menagecc31edc2008-10-18 20:28:04 -07003815/*
Ben Blum102a7752009-09-23 15:56:26 -07003816 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003817 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003818 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003819 */
3820
Ben Blum102a7752009-09-23 15:56:26 -07003821static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003822{
3823 /*
3824 * Initially we receive a position value that corresponds to
3825 * one more than the last pid shown (or 0 on the first call or
3826 * after a seek to the start). Use a binary-search to find the
3827 * next pid to display, if any
3828 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003829 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003830 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003831 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003832 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003833 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003834 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003835
Tejun Heo4bac00d2013-11-29 10:42:59 -05003836 mutex_lock(&cgrp->pidlist_mutex);
3837
3838 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003839 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003840 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003841 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003842 * could already have been destroyed.
3843 */
Tejun Heo5d224442013-12-05 12:28:04 -05003844 if (of->priv)
3845 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003846
3847 /*
3848 * Either this is the first start() after open or the matching
3849 * pidlist has been destroyed inbetween. Create a new one.
3850 */
Tejun Heo5d224442013-12-05 12:28:04 -05003851 if (!of->priv) {
3852 ret = pidlist_array_load(cgrp, type,
3853 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003854 if (ret)
3855 return ERR_PTR(ret);
3856 }
Tejun Heo5d224442013-12-05 12:28:04 -05003857 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003858
Paul Menagecc31edc2008-10-18 20:28:04 -07003859 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003860 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003861
Paul Menagecc31edc2008-10-18 20:28:04 -07003862 while (index < end) {
3863 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003864 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003865 index = mid;
3866 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003867 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003868 index = mid + 1;
3869 else
3870 end = mid;
3871 }
3872 }
3873 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003874 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003875 return NULL;
3876 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003877 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003878 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003879 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003880}
3881
Ben Blum102a7752009-09-23 15:56:26 -07003882static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003883{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003884 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003885 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003886
Tejun Heo5d224442013-12-05 12:28:04 -05003887 if (l)
3888 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003889 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003890 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003891}
3892
Ben Blum102a7752009-09-23 15:56:26 -07003893static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003894{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003895 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003896 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003897 pid_t *p = v;
3898 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003899 /*
3900 * Advance to the next pid in the array. If this goes off the
3901 * end, we're done
3902 */
3903 p++;
3904 if (p >= end) {
3905 return NULL;
3906 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003907 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003908 return p;
3909 }
3910}
3911
Ben Blum102a7752009-09-23 15:56:26 -07003912static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003913{
3914 return seq_printf(s, "%d\n", *(int *)v);
3915}
3916
Tejun Heo182446d2013-08-08 20:11:24 -04003917static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3918 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003919{
Tejun Heo182446d2013-08-08 20:11:24 -04003920 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003921}
3922
Tejun Heo182446d2013-08-08 20:11:24 -04003923static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3924 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003925{
Tejun Heo182446d2013-08-08 20:11:24 -04003926 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003927 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003928 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003929 else
Tejun Heo182446d2013-08-08 20:11:24 -04003930 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003931 return 0;
3932}
3933
Tejun Heo182446d2013-08-08 20:11:24 -04003934static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3935 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003936{
Tejun Heo182446d2013-08-08 20:11:24 -04003937 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003938}
3939
Tejun Heo182446d2013-08-08 20:11:24 -04003940static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3941 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003942{
3943 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003944 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003945 else
Tejun Heo182446d2013-08-08 20:11:24 -04003946 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003947 return 0;
3948}
3949
Tejun Heod5c56ce2013-06-03 19:14:34 -07003950static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003951 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003952 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003953 .seq_start = cgroup_pidlist_start,
3954 .seq_next = cgroup_pidlist_next,
3955 .seq_stop = cgroup_pidlist_stop,
3956 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003957 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003958 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003959 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003960 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003961 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003962 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003963 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003964 .read_u64 = cgroup_clone_children_read,
3965 .write_u64 = cgroup_clone_children_write,
3966 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003967 {
Tejun Heo873fe092013-04-14 20:15:26 -07003968 .name = "cgroup.sane_behavior",
3969 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003970 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003971 },
Tejun Heof8f22e52014-04-23 11:13:16 -04003972 {
3973 .name = "cgroup.controllers",
3974 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT,
3975 .seq_show = cgroup_root_controllers_show,
3976 },
3977 {
3978 .name = "cgroup.controllers",
3979 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3980 .seq_show = cgroup_controllers_show,
3981 },
3982 {
3983 .name = "cgroup.subtree_control",
3984 .flags = CFTYPE_ONLY_ON_DFL,
3985 .seq_show = cgroup_subtree_control_show,
3986 .write_string = cgroup_subtree_control_write,
3987 },
Tejun Heo842b5972014-04-25 18:28:02 -04003988 {
3989 .name = "cgroup.populated",
3990 .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT,
3991 .seq_show = cgroup_populated_show,
3992 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003993
3994 /*
3995 * Historical crazy stuff. These don't have "cgroup." prefix and
3996 * don't exist if sane_behavior. If you're depending on these, be
3997 * prepared to be burned.
3998 */
3999 {
4000 .name = "tasks",
4001 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05004002 .seq_start = cgroup_pidlist_start,
4003 .seq_next = cgroup_pidlist_next,
4004 .seq_stop = cgroup_pidlist_stop,
4005 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05004006 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004007 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07004008 .mode = S_IRUGO | S_IWUSR,
4009 },
4010 {
4011 .name = "notify_on_release",
4012 .flags = CFTYPE_INSANE,
4013 .read_u64 = cgroup_read_notify_on_release,
4014 .write_u64 = cgroup_write_notify_on_release,
4015 },
Tejun Heo873fe092013-04-14 20:15:26 -07004016 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004017 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004018 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05004019 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004020 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05004021 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004022 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004023 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004024};
4025
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004026/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004027 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004028 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004029 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004030 *
4031 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004032 */
Tejun Heo69dfa002014-05-04 15:09:13 -04004033static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004034{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004035 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004036 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004037
Tejun Heo8e3f6542012-04-01 12:09:55 -07004038 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004039 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05004040 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07004041
Tejun Heo69dfa002014-05-04 15:09:13 -04004042 if (!(subsys_mask & (1 << i)))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004043 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004044
Tejun Heo0adb0702014-02-12 09:29:48 -05004045 list_for_each_entry(cfts, &ss->cfts, node) {
4046 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004047 if (ret < 0)
4048 goto err;
4049 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004051 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004052err:
4053 cgroup_clear_dir(cgrp, subsys_mask);
4054 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004055}
4056
Tejun Heo0c21ead2013-08-13 20:22:51 -04004057/*
4058 * css destruction is four-stage process.
4059 *
4060 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4061 * Implemented in kill_css().
4062 *
4063 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4064 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4065 * by invoking offline_css(). After offlining, the base ref is put.
4066 * Implemented in css_killed_work_fn().
4067 *
4068 * 3. When the percpu_ref reaches zero, the only possible remaining
4069 * accessors are inside RCU read sections. css_release() schedules the
4070 * RCU callback.
4071 *
4072 * 4. After the grace period, the css can be freed. Implemented in
4073 * css_free_work_fn().
4074 *
4075 * It is actually hairier because both step 2 and 4 require process context
4076 * and thus involve punting to css->destroy_work adding two additional
4077 * steps to the already complex sequence.
4078 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004079static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004080{
4081 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004082 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004083 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004084
Tejun Heo0ae78e02013-08-13 11:01:54 -04004085 if (css->parent)
4086 css_put(css->parent);
4087
Tejun Heo0c21ead2013-08-13 20:22:51 -04004088 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004089 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004090}
4091
4092static void css_free_rcu_fn(struct rcu_head *rcu_head)
4093{
4094 struct cgroup_subsys_state *css =
4095 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4096
Tejun Heo0c21ead2013-08-13 20:22:51 -04004097 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004098 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004099}
4100
Tejun Heod3daf282013-06-13 19:39:16 -07004101static void css_release(struct percpu_ref *ref)
4102{
4103 struct cgroup_subsys_state *css =
4104 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo15a4c832014-05-04 15:09:14 -04004105 struct cgroup_subsys *ss = css->ss;
Tejun Heod3daf282013-06-13 19:39:16 -07004106
Tejun Heo15a4c832014-05-04 15:09:14 -04004107 cgroup_idr_remove(&ss->css_idr, css->id);
4108
Tejun Heo0c21ead2013-08-13 20:22:51 -04004109 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004110}
4111
Tejun Heoddfcada2014-05-04 15:09:14 -04004112static void init_and_link_css(struct cgroup_subsys_state *css,
4113 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004114{
Tejun Heoddfcada2014-05-04 15:09:14 -04004115 cgroup_get(cgrp);
4116
Paul Menagebd89aab2007-10-18 23:40:44 -07004117 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004118 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004119 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004120
Tejun Heoddfcada2014-05-04 15:09:14 -04004121 if (cgrp->parent) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004122 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04004123 css_get(css->parent);
4124 } else {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004125 css->flags |= CSS_ROOT;
Tejun Heoddfcada2014-05-04 15:09:14 -04004126 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04004127
Tejun Heoca8bdca2013-08-26 18:40:56 -04004128 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004129}
4130
Li Zefan2a4ac632013-07-31 16:16:40 +08004131/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004132static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004133{
Tejun Heo623f9262013-08-13 11:01:55 -04004134 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004135 int ret = 0;
4136
Tejun Heoace2bee2014-02-11 11:52:47 -05004137 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004138 lockdep_assert_held(&cgroup_mutex);
4139
Tejun Heo92fb9742012-11-19 08:13:38 -08004140 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004141 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004142 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004143 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004144 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004145 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004146 }
Tejun Heob1929db2012-11-19 08:13:38 -08004147 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004148}
4149
Li Zefan2a4ac632013-07-31 16:16:40 +08004150/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004151static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004152{
Tejun Heo623f9262013-08-13 11:01:55 -04004153 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004154
Tejun Heoace2bee2014-02-11 11:52:47 -05004155 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004156 lockdep_assert_held(&cgroup_mutex);
4157
4158 if (!(css->flags & CSS_ONLINE))
4159 return;
4160
Li Zefand7eeac12013-03-12 15:35:59 -07004161 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004162 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004163
Tejun Heoeb954192013-08-08 20:11:23 -04004164 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004165 css->cgroup->nr_css--;
Tejun Heoe3297802014-04-23 11:13:15 -04004166 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04004167
4168 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004169}
4170
Tejun Heoc81c925a2013-12-06 15:11:56 -05004171/**
4172 * create_css - create a cgroup_subsys_state
4173 * @cgrp: the cgroup new css will be associated with
4174 * @ss: the subsys of new css
4175 *
4176 * Create a new css associated with @cgrp - @ss pair. On success, the new
4177 * css is online and installed in @cgrp with all interface files created.
4178 * Returns 0 on success, -errno on failure.
4179 */
4180static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4181{
4182 struct cgroup *parent = cgrp->parent;
4183 struct cgroup_subsys_state *css;
4184 int err;
4185
Tejun Heoc81c925a2013-12-06 15:11:56 -05004186 lockdep_assert_held(&cgroup_mutex);
4187
4188 css = ss->css_alloc(cgroup_css(parent, ss));
4189 if (IS_ERR(css))
4190 return PTR_ERR(css);
4191
Tejun Heoddfcada2014-05-04 15:09:14 -04004192 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04004193
Tejun Heoc81c925a2013-12-06 15:11:56 -05004194 err = percpu_ref_init(&css->refcnt, css_release);
4195 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004196 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004197
Tejun Heo15a4c832014-05-04 15:09:14 -04004198 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT);
4199 if (err < 0)
4200 goto err_free_percpu_ref;
4201 css->id = err;
4202
Tejun Heoaec25022014-02-08 10:36:58 -05004203 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004204 if (err)
Tejun Heo15a4c832014-05-04 15:09:14 -04004205 goto err_free_id;
4206
4207 /* @css is ready to be brought online now, make it visible */
4208 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004209
4210 err = online_css(css);
4211 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08004212 goto err_clear_dir;
Tejun Heoc81c925a2013-12-06 15:11:56 -05004213
Tejun Heoc81c925a2013-12-06 15:11:56 -05004214 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4215 parent->parent) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004216 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
Jianyu Zhana2a1f9e2014-04-25 18:28:03 -04004217 current->comm, current->pid, ss->name);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004218 if (!strcmp(ss->name, "memory"))
Joe Perchesed3d2612014-04-25 18:28:03 -04004219 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
Tejun Heoc81c925a2013-12-06 15:11:56 -05004220 ss->warned_broken_hierarchy = true;
4221 }
4222
4223 return 0;
4224
Li Zefan3eb59ec2014-03-18 17:02:36 +08004225err_clear_dir:
Linus Torvalds32d01dc2014-04-03 13:05:42 -07004226 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo15a4c832014-05-04 15:09:14 -04004227err_free_id:
4228 cgroup_idr_remove(&ss->css_idr, css->id);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004229err_free_percpu_ref:
Tejun Heoc81c925a2013-12-06 15:11:56 -05004230 percpu_ref_cancel_init(&css->refcnt);
Li Zefan3eb59ec2014-03-18 17:02:36 +08004231err_free_css:
Tejun Heoa2bed822014-05-04 15:09:14 -04004232 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004233 return err;
4234}
4235
Tejun Heo2bd59d42014-02-11 11:52:49 -05004236/**
Li Zefana043e3b2008-02-23 15:24:09 -08004237 * cgroup_create - create a cgroup
4238 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05004239 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05004240 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241 */
Tejun Heoe61734c2014-02-12 09:29:50 -05004242static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05004243 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244{
Paul Menagebd89aab2007-10-18 23:40:44 -07004245 struct cgroup *cgrp;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004246 struct cgroup_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05004247 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004248 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004249 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250
Tejun Heo0a950f62012-11-19 09:02:12 -08004251 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004252 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4253 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004254 return -ENOMEM;
4255
Tejun Heoace2bee2014-02-11 11:52:47 -05004256 mutex_lock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08004257
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004258 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08004259 * Only live parents can have children. Note that the liveliness
4260 * check isn't strictly necessary because cgroup_mkdir() and
4261 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4262 * anyway so that locking is contained inside cgroup proper and we
4263 * don't get nasty surprises if we ever grow another caller.
4264 */
4265 if (!cgroup_lock_live_group(parent)) {
4266 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05004267 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004268 }
4269
4270 /*
4271 * Temporarily set the pointer to NULL, so idr_find() won't return
4272 * a half-baked cgroup.
4273 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004274 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004275 if (cgrp->id < 0) {
4276 err = -ENOMEM;
4277 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08004278 }
4279
Paul Menagecc31edc2008-10-18 20:28:04 -07004280 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281
Paul Menagebd89aab2007-10-18 23:40:44 -07004282 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004283 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004284 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004285
Li Zefanb6abdb02008-03-04 14:28:19 -08004286 if (notify_on_release(parent))
4287 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4288
Tejun Heo2260e7f2012-11-19 08:13:38 -08004289 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4290 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004291
Tejun Heo2bd59d42014-02-11 11:52:49 -05004292 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05004293 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004294 if (IS_ERR(kn)) {
4295 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004296 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004297 }
4298 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004299
Tejun Heo6f305582014-02-12 09:29:50 -05004300 /*
4301 * This extra ref will be put in cgroup_free_fn() and guarantees
4302 * that @cgrp->kn is always accessible.
4303 */
4304 kernfs_get(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305
Tejun Heo00356bd2013-06-18 11:14:22 -07004306 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004307
Tejun Heo4e139af2012-11-19 08:13:36 -08004308 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004309 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05004310 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05004311 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004312
Tejun Heo0d802552013-12-06 15:11:56 -05004313 /*
4314 * @cgrp is now fully operational. If something fails after this
4315 * point, it'll be released via the normal destruction path.
4316 */
Tejun Heo6fa49182014-05-04 15:09:13 -04004317 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004318
Tejun Heo49957f82014-04-07 16:44:47 -04004319 err = cgroup_kn_set_ugid(kn);
4320 if (err)
4321 goto err_destroy;
4322
Tejun Heo2bb566c2013-08-08 20:11:23 -04004323 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004324 if (err)
4325 goto err_destroy;
4326
Tejun Heo9d403e92013-12-06 15:11:56 -05004327 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004328 for_each_subsys(ss, ssid) {
Tejun Heof392e512014-04-23 11:13:14 -04004329 if (parent->child_subsys_mask & (1 << ssid)) {
Tejun Heob85d2042013-12-06 15:11:57 -05004330 err = create_css(cgrp, ss);
4331 if (err)
4332 goto err_destroy;
4333 }
Tejun Heoa8638032012-11-09 09:12:29 -08004334 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335
Tejun Heobd53d612014-04-23 11:13:16 -04004336 /*
4337 * On the default hierarchy, a child doesn't automatically inherit
4338 * child_subsys_mask from the parent. Each is configured manually.
4339 */
4340 if (!cgroup_on_dfl(cgrp))
4341 cgrp->child_subsys_mask = parent->child_subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -04004342
Tejun Heo2bd59d42014-02-11 11:52:49 -05004343 kernfs_activate(kn);
4344
Paul Menageddbcc7e2007-10-18 23:39:30 -07004345 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004346 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347
4348 return 0;
4349
Tejun Heo0a950f62012-11-19 09:02:12 -08004350err_free_id:
Tejun Heo6fa49182014-05-04 15:09:13 -04004351 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004352err_unlock:
4353 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004354err_unlock_tree:
4355 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004356 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004357 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004358
4359err_destroy:
4360 cgroup_destroy_locked(cgrp);
4361 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004362 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004363 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004364}
4365
Tejun Heo2bd59d42014-02-11 11:52:49 -05004366static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
4367 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004369 struct cgroup *parent = parent_kn->priv;
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004370 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371
Tejun Heoe1b2dc12014-03-20 11:10:15 -04004372 /*
4373 * cgroup_create() grabs cgroup_tree_mutex which nests outside
4374 * kernfs active_ref and cgroup_create() already synchronizes
4375 * properly against removal through cgroup_lock_live_group().
4376 * Break it before calling cgroup_create().
4377 */
4378 cgroup_get(parent);
4379 kernfs_break_active_protection(parent_kn);
4380
4381 ret = cgroup_create(parent, name, mode);
4382
4383 kernfs_unbreak_active_protection(parent_kn);
4384 cgroup_put(parent);
4385 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386}
4387
Tejun Heo223dbc32013-08-13 20:22:50 -04004388/*
4389 * This is called when the refcnt of a css is confirmed to be killed.
4390 * css_tryget() is now guaranteed to fail.
4391 */
4392static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004393{
Tejun Heo223dbc32013-08-13 20:22:50 -04004394 struct cgroup_subsys_state *css =
4395 container_of(work, struct cgroup_subsys_state, destroy_work);
4396 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004397
Tejun Heoace2bee2014-02-11 11:52:47 -05004398 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004399 mutex_lock(&cgroup_mutex);
4400
4401 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004402 * css_tryget() is guaranteed to fail now. Tell subsystems to
4403 * initate destruction.
4404 */
4405 offline_css(css);
4406
4407 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004408 * If @cgrp is marked dead, it's waiting for refs of all css's to
4409 * be disabled before proceeding to the second phase of cgroup
4410 * destruction. If we are the last one, kick it off.
4411 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004412 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004413 cgroup_destroy_css_killed(cgrp);
4414
4415 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004416 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004417
4418 /*
4419 * Put the css refs from kill_css(). Each css holds an extra
4420 * reference to the cgroup's dentry and cgroup removal proceeds
4421 * regardless of css refs. On the last put of each css, whenever
4422 * that may be, the extra dentry ref is put so that dentry
4423 * destruction happens only after all css's are released.
4424 */
4425 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004426}
4427
Tejun Heo223dbc32013-08-13 20:22:50 -04004428/* css kill confirmation processing requires process context, bounce */
4429static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004430{
4431 struct cgroup_subsys_state *css =
4432 container_of(ref, struct cgroup_subsys_state, refcnt);
4433
Tejun Heo223dbc32013-08-13 20:22:50 -04004434 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004435 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004436}
4437
Tejun Heof392e512014-04-23 11:13:14 -04004438/**
4439 * kill_css - destroy a css
4440 * @css: css to destroy
4441 *
4442 * This function initiates destruction of @css by removing cgroup interface
4443 * files and putting its base reference. ->css_offline() will be invoked
4444 * asynchronously once css_tryget() is guaranteed to fail and when the
4445 * reference count reaches zero, @css will be released.
4446 */
4447static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04004448{
Tejun Heo94419622014-03-19 10:23:54 -04004449 lockdep_assert_held(&cgroup_tree_mutex);
4450
Tejun Heo2bd59d42014-02-11 11:52:49 -05004451 /*
4452 * This must happen before css is disassociated with its cgroup.
4453 * See seq_css() for details.
4454 */
Tejun Heoaec25022014-02-08 10:36:58 -05004455 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004456
Tejun Heoedae0c32013-08-13 20:22:51 -04004457 /*
4458 * Killing would put the base ref, but we need to keep it alive
4459 * until after ->css_offline().
4460 */
4461 css_get(css);
4462
4463 /*
4464 * cgroup core guarantees that, by the time ->css_offline() is
4465 * invoked, no new css reference will be given out via
4466 * css_tryget(). We can't simply call percpu_ref_kill() and
4467 * proceed to offlining css's because percpu_ref_kill() doesn't
4468 * guarantee that the ref is seen as killed on all CPUs on return.
4469 *
4470 * Use percpu_ref_kill_and_confirm() to get notifications as each
4471 * css is confirmed to be seen as killed on all CPUs.
4472 */
4473 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004474}
4475
4476/**
4477 * cgroup_destroy_locked - the first stage of cgroup destruction
4478 * @cgrp: cgroup to be destroyed
4479 *
4480 * css's make use of percpu refcnts whose killing latency shouldn't be
4481 * exposed to userland and are RCU protected. Also, cgroup core needs to
4482 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4483 * invoked. To satisfy all the requirements, destruction is implemented in
4484 * the following two steps.
4485 *
4486 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4487 * userland visible parts and start killing the percpu refcnts of
4488 * css's. Set up so that the next stage will be kicked off once all
4489 * the percpu refcnts are confirmed to be killed.
4490 *
4491 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4492 * rest of destruction. Once all cgroup references are gone, the
4493 * cgroup is RCU-freed.
4494 *
4495 * This function implements s1. After this step, @cgrp is gone as far as
4496 * the userland is concerned and a new cgroup with the same name may be
4497 * created. As cgroup doesn't care about the names internally, this
4498 * doesn't cause any problem.
4499 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004500static int cgroup_destroy_locked(struct cgroup *cgrp)
4501 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004502{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004503 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004504 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07004505 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004506 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004507
Tejun Heoace2bee2014-02-11 11:52:47 -05004508 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004509 lockdep_assert_held(&cgroup_mutex);
4510
Tejun Heoddd69142013-06-12 21:04:54 -07004511 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004512 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05004513 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004514 */
Tejun Heo96d365e2014-02-13 06:58:40 -05004515 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004516 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05004517 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07004518 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004519 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004520
Tejun Heo1a90dd52012-11-05 09:16:59 -08004521 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004522 * Make sure there's no live children. We can't test ->children
4523 * emptiness as dead children linger on it while being destroyed;
4524 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4525 */
4526 empty = true;
4527 rcu_read_lock();
4528 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4529 empty = cgroup_is_dead(child);
4530 if (!empty)
4531 break;
4532 }
4533 rcu_read_unlock();
4534 if (!empty)
4535 return -EBUSY;
4536
4537 /*
Tejun Heo455050d2013-06-13 19:27:41 -07004538 * Mark @cgrp dead. This prevents further task migration and child
4539 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004540 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004541 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004542 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004543 */
Tejun Heo54766d42013-06-12 21:04:53 -07004544 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004545
Tejun Heo5d773812014-03-19 10:23:53 -04004546 /*
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004547 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4548 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004549 * percpu refs of all css's are confirmed to be killed. This
4550 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004551 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004552 mutex_unlock(&cgroup_mutex);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004553 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07004554 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004555 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004556
Tejun Heo455050d2013-06-13 19:27:41 -07004557 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4558 raw_spin_lock(&release_list_lock);
4559 if (!list_empty(&cgrp->release_list))
4560 list_del_init(&cgrp->release_list);
4561 raw_spin_unlock(&release_list_lock);
4562
4563 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004564 * If @cgrp has css's attached, the second stage of cgroup
4565 * destruction is kicked off from css_killed_work_fn() after the
4566 * refs of all attached css's are killed. If @cgrp doesn't have
4567 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004568 */
Tejun Heof20104d2013-08-13 20:22:50 -04004569 if (!cgrp->nr_css)
4570 cgroup_destroy_css_killed(cgrp);
4571
Tejun Heo2bd59d42014-02-11 11:52:49 -05004572 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05004573 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004574
Tejun Heof20104d2013-08-13 20:22:50 -04004575 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05004576 * There are two control paths which try to determine cgroup from
4577 * dentry without going through kernfs - cgroupstats_build() and
4578 * css_tryget_from_dir(). Those are supported by RCU protecting
4579 * clearing of cgrp->kn->priv backpointer, which should happen
4580 * after all files under it have been removed.
Tejun Heo455050d2013-06-13 19:27:41 -07004581 */
Tejun Heo6f305582014-02-12 09:29:50 -05004582 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004583 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004584
Tejun Heo4ac06012014-02-11 11:52:47 -05004585 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004586
Tejun Heoea15f8c2013-06-13 19:27:42 -07004587 return 0;
4588};
4589
Tejun Heod3daf282013-06-13 19:39:16 -07004590/**
Tejun Heof20104d2013-08-13 20:22:50 -04004591 * cgroup_destroy_css_killed - the second step of cgroup destruction
Fabian Frederick60106942014-05-05 20:08:13 +02004592 * @cgrp: the cgroup whose csses have just finished offlining
Tejun Heod3daf282013-06-13 19:39:16 -07004593 *
4594 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004595 * destroyed after all css's are offlined and performs the rest of
4596 * destruction. This is the second step of destruction described in the
4597 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004598 */
Tejun Heof20104d2013-08-13 20:22:50 -04004599static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004600{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004601 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004602
Tejun Heoace2bee2014-02-11 11:52:47 -05004603 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004604 lockdep_assert_held(&cgroup_mutex);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004605
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004606 /* delete this cgroup from parent->children */
4607 list_del_rcu(&cgrp->sibling);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004608
Tejun Heo59f52962014-02-11 11:52:49 -05004609 cgroup_put(cgrp);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004610
Paul Menageddbcc7e2007-10-18 23:39:30 -07004611 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004612 check_for_release(parent);
4613}
4614
Tejun Heo2bd59d42014-02-11 11:52:49 -05004615static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08004616{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004617 struct cgroup *cgrp = kn->priv;
4618 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08004619
Tejun Heo2bd59d42014-02-11 11:52:49 -05004620 /*
4621 * This is self-destruction but @kn can't be removed while this
4622 * callback is in progress. Let's break active protection. Once
4623 * the protection is broken, @cgrp can be destroyed at any point.
4624 * Pin it so that it stays accessible.
4625 */
4626 cgroup_get(cgrp);
4627 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004628
Tejun Heoace2bee2014-02-11 11:52:47 -05004629 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004630 mutex_lock(&cgroup_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004631
Tejun Heo2bd59d42014-02-11 11:52:49 -05004632 /*
4633 * @cgrp might already have been destroyed while we're trying to
4634 * grab the mutexes.
4635 */
4636 if (!cgroup_is_dead(cgrp))
4637 ret = cgroup_destroy_locked(cgrp);
4638
Tejun Heo42809dd2012-11-19 08:13:37 -08004639 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004640 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004641
Tejun Heo2bd59d42014-02-11 11:52:49 -05004642 kernfs_unbreak_active_protection(kn);
4643 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004644 return ret;
4645}
4646
Tejun Heo2bd59d42014-02-11 11:52:49 -05004647static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4648 .remount_fs = cgroup_remount,
4649 .show_options = cgroup_show_options,
4650 .mkdir = cgroup_mkdir,
4651 .rmdir = cgroup_rmdir,
4652 .rename = cgroup_rename,
4653};
Tejun Heo8e3f6542012-04-01 12:09:55 -07004654
Tejun Heo15a4c832014-05-04 15:09:14 -04004655static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004656{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004657 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004658
4659 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004660
Tejun Heoace2bee2014-02-11 11:52:47 -05004661 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004662 mutex_lock(&cgroup_mutex);
4663
Tejun Heo15a4c832014-05-04 15:09:14 -04004664 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05004665 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004666
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004667 /* Create the root cgroup state for this subsystem */
4668 ss->root = &cgrp_dfl_root;
4669 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004670 /* We don't handle early failures gracefully */
4671 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04004672 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo15a4c832014-05-04 15:09:14 -04004673 if (early) {
4674 /* idr_alloc() can't be called safely during early init */
4675 css->id = 1;
4676 } else {
4677 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4678 BUG_ON(css->id < 0);
4679 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004680
Li Zefane8d55fd2008-04-29 01:00:13 -07004681 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004682 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004683 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004684 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004685 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004686
4687 need_forkexit_callback |= ss->fork || ss->exit;
4688
Li Zefane8d55fd2008-04-29 01:00:13 -07004689 /* At system boot, before all subsystems have been
4690 * registered, no tasks have been forked, so we don't
4691 * need to invoke fork callbacks here. */
4692 BUG_ON(!list_empty(&init_task.tasks));
4693
Tejun Heoae7f1642013-08-13 20:22:50 -04004694 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004695
Tejun Heof392e512014-04-23 11:13:14 -04004696 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
Tejun Heo648bb562012-11-19 08:13:36 -08004697
Ben Blume6a11052010-03-10 15:22:09 -08004698 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004699 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004700}
4701
4702/**
Li Zefana043e3b2008-02-23 15:24:09 -08004703 * cgroup_init_early - cgroup initialization at system boot
4704 *
4705 * Initialize cgroups at system boot, and initialize any
4706 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004707 */
4708int __init cgroup_init_early(void)
4709{
Tejun Heoa2dd4242014-03-19 10:23:55 -04004710 static struct cgroup_sb_opts __initdata opts =
4711 { .flags = CGRP_ROOT_SANE_BEHAVIOR };
Tejun Heo30159ec2013-06-25 11:53:37 -07004712 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004713 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004714
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004715 init_cgroup_root(&cgrp_dfl_root, &opts);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004716 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004717
Tejun Heo3ed80a62014-02-08 10:36:58 -05004718 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004719 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004720 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4721 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004722 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004723 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4724 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004725
Tejun Heoaec25022014-02-08 10:36:58 -05004726 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004727 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004728
4729 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04004730 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004731 }
4732 return 0;
4733}
4734
4735/**
Li Zefana043e3b2008-02-23 15:24:09 -08004736 * cgroup_init - cgroup initialization
4737 *
4738 * Register cgroup filesystem and /proc file, and initialize
4739 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004740 */
4741int __init cgroup_init(void)
4742{
Tejun Heo30159ec2013-06-25 11:53:37 -07004743 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004744 unsigned long key;
Tejun Heo172a2c062014-03-19 10:23:53 -04004745 int ssid, err;
Paul Menagea4243162007-10-18 23:39:35 -07004746
Tejun Heo2bd59d42014-02-11 11:52:49 -05004747 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004748
Tejun Heo985ed672014-03-19 10:23:53 -04004749 mutex_lock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004750 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004751
Tejun Heo82fe9b02013-06-25 11:53:37 -07004752 /* Add init_css_set to the hash table */
4753 key = css_set_hash(init_css_set.subsys);
4754 hash_add(css_set_table, &init_css_set.hlist, key);
4755
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004756 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07004757
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004758 mutex_unlock(&cgroup_mutex);
Tejun Heo985ed672014-03-19 10:23:53 -04004759 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004760
Tejun Heo172a2c062014-03-19 10:23:53 -04004761 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04004762 if (ss->early_init) {
4763 struct cgroup_subsys_state *css =
4764 init_css_set.subsys[ss->id];
4765
4766 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4767 GFP_KERNEL);
4768 BUG_ON(css->id < 0);
4769 } else {
4770 cgroup_init_subsys(ss, false);
4771 }
Tejun Heo172a2c062014-03-19 10:23:53 -04004772
Tejun Heo2d8f2432014-04-23 11:13:15 -04004773 list_add_tail(&init_css_set.e_cset_node[ssid],
4774 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4775
Tejun Heo172a2c062014-03-19 10:23:53 -04004776 /*
4777 * cftype registration needs kmalloc and can't be done
4778 * during early_init. Register base cftypes separately.
4779 */
4780 if (ss->base_cftypes)
4781 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
4782 }
Greg KH676db4a2010-08-05 13:53:35 -07004783
Paul Menageddbcc7e2007-10-18 23:39:30 -07004784 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004785 if (!cgroup_kobj)
4786 return -ENOMEM;
Paul Menagea4243162007-10-18 23:39:35 -07004787
4788 err = register_filesystem(&cgroup_fs_type);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004789 if (err < 0) {
4790 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004791 return err;
Paul Menagea4243162007-10-18 23:39:35 -07004792 }
4793
4794 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004795 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004796}
Paul Menageb4f48b62007-10-18 23:39:33 -07004797
Tejun Heoe5fca242013-11-22 17:14:39 -05004798static int __init cgroup_wq_init(void)
4799{
4800 /*
4801 * There isn't much point in executing destruction path in
4802 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004803 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004804 *
4805 * We would prefer to do this in cgroup_init() above, but that
4806 * is called before init_workqueues(): so leave this until after.
4807 */
Tejun Heo1a115332014-02-12 19:06:19 -05004808 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004809 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004810
4811 /*
4812 * Used to destroy pidlists and separate to serve as flush domain.
4813 * Cap @max_active to 1 too.
4814 */
4815 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4816 0, 1);
4817 BUG_ON(!cgroup_pidlist_destroy_wq);
4818
Tejun Heoe5fca242013-11-22 17:14:39 -05004819 return 0;
4820}
4821core_initcall(cgroup_wq_init);
4822
Paul Menagea4243162007-10-18 23:39:35 -07004823/*
4824 * proc_cgroup_show()
4825 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4826 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07004827 */
4828
4829/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004830int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004831{
4832 struct pid *pid;
4833 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004834 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004835 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004836 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07004837
4838 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004839 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004840 if (!buf)
4841 goto out;
4842
4843 retval = -ESRCH;
4844 pid = m->private;
4845 tsk = get_pid_task(pid, PIDTYPE_PID);
4846 if (!tsk)
4847 goto out_free;
4848
4849 retval = 0;
4850
4851 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004852 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004853
Tejun Heo985ed672014-03-19 10:23:53 -04004854 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004855 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004856 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004857 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004858
Tejun Heoa2dd4242014-03-19 10:23:55 -04004859 if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04004860 continue;
4861
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004862 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004863 for_each_subsys(ss, ssid)
Tejun Heof392e512014-04-23 11:13:14 -04004864 if (root->subsys_mask & (1 << ssid))
Tejun Heob85d2042013-12-06 15:11:57 -05004865 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004866 if (strlen(root->name))
4867 seq_printf(m, "%sname=%s", count ? "," : "",
4868 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004869 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004870 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004871 path = cgroup_path(cgrp, buf, PATH_MAX);
4872 if (!path) {
4873 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004874 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004875 }
4876 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004877 seq_putc(m, '\n');
4878 }
4879
4880out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004881 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004882 mutex_unlock(&cgroup_mutex);
4883 put_task_struct(tsk);
4884out_free:
4885 kfree(buf);
4886out:
4887 return retval;
4888}
4889
Paul Menagea4243162007-10-18 23:39:35 -07004890/* Display information about each subsystem and each hierarchy */
4891static int proc_cgroupstats_show(struct seq_file *m, void *v)
4892{
Tejun Heo30159ec2013-06-25 11:53:37 -07004893 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004894 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004895
Paul Menage8bab8dd2008-04-04 14:29:57 -07004896 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004897 /*
4898 * ideally we don't want subsystems moving around while we do this.
4899 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4900 * subsys/hierarchy state.
4901 */
Paul Menagea4243162007-10-18 23:39:35 -07004902 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004903
4904 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004905 seq_printf(m, "%s\t%d\t%d\t%d\n",
4906 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004907 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004908
Paul Menagea4243162007-10-18 23:39:35 -07004909 mutex_unlock(&cgroup_mutex);
4910 return 0;
4911}
4912
4913static int cgroupstats_open(struct inode *inode, struct file *file)
4914{
Al Viro9dce07f2008-03-29 03:07:28 +00004915 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004916}
4917
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004918static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004919 .open = cgroupstats_open,
4920 .read = seq_read,
4921 .llseek = seq_lseek,
4922 .release = single_release,
4923};
4924
Paul Menageb4f48b62007-10-18 23:39:33 -07004925/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05004926 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08004927 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004928 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05004929 * A task is associated with the init_css_set until cgroup_post_fork()
4930 * attaches it to the parent's css_set. Empty cg_list indicates that
4931 * @child isn't holding reference to its css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07004932 */
4933void cgroup_fork(struct task_struct *child)
4934{
Tejun Heoeaf797a2014-02-25 10:04:03 -05004935 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004936 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004937}
4938
4939/**
Li Zefana043e3b2008-02-23 15:24:09 -08004940 * cgroup_post_fork - called on a new task after adding it to the task list
4941 * @child: the task in question
4942 *
Tejun Heo5edee612012-10-16 15:03:14 -07004943 * Adds the task to the list running through its css_set if necessary and
4944 * call the subsystem fork() callbacks. Has to be after the task is
4945 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004946 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004947 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004948 */
Paul Menage817929e2007-10-18 23:39:36 -07004949void cgroup_post_fork(struct task_struct *child)
4950{
Tejun Heo30159ec2013-06-25 11:53:37 -07004951 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004952 int i;
4953
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004954 /*
Tejun Heoeaf797a2014-02-25 10:04:03 -05004955 * This may race against cgroup_enable_task_cg_links(). As that
4956 * function sets use_task_css_set_links before grabbing
4957 * tasklist_lock and we just went through tasklist_lock to add
4958 * @child, it's guaranteed that either we see the set
4959 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4960 * @child during its iteration.
4961 *
4962 * If we won the race, @child is associated with %current's
4963 * css_set. Grabbing css_set_rwsem guarantees both that the
4964 * association is stable, and, on completion of the parent's
4965 * migration, @child is visible in the source of migration or
4966 * already in the destination cgroup. This guarantee is necessary
4967 * when implementing operations which need to migrate all tasks of
4968 * a cgroup to another.
4969 *
4970 * Note that if we lose to cgroup_enable_task_cg_links(), @child
4971 * will remain in init_css_set. This is safe because all tasks are
4972 * in the init_css_set before cg_links is enabled and there's no
4973 * operation which transfers all tasks out of init_css_set.
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004974 */
Paul Menage817929e2007-10-18 23:39:36 -07004975 if (use_task_css_set_links) {
Tejun Heoeaf797a2014-02-25 10:04:03 -05004976 struct css_set *cset;
4977
Tejun Heo96d365e2014-02-13 06:58:40 -05004978 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05004979 cset = task_css_set(current);
Tejun Heoeaf797a2014-02-25 10:04:03 -05004980 if (list_empty(&child->cg_list)) {
4981 rcu_assign_pointer(child->cgroups, cset);
4982 list_add(&child->cg_list, &cset->tasks);
4983 get_css_set(cset);
4984 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004985 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004986 }
Tejun Heo5edee612012-10-16 15:03:14 -07004987
4988 /*
4989 * Call ss->fork(). This must happen after @child is linked on
4990 * css_set; otherwise, @child might change state between ->fork()
4991 * and addition to css_set.
4992 */
4993 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004994 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004995 if (ss->fork)
4996 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004997 }
Paul Menage817929e2007-10-18 23:39:36 -07004998}
Tejun Heo5edee612012-10-16 15:03:14 -07004999
Paul Menage817929e2007-10-18 23:39:36 -07005000/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005001 * cgroup_exit - detach cgroup from exiting task
5002 * @tsk: pointer to task_struct of exiting process
5003 *
5004 * Description: Detach cgroup from @tsk and release it.
5005 *
5006 * Note that cgroups marked notify_on_release force every task in
5007 * them to take the global cgroup_mutex mutex when exiting.
5008 * This could impact scaling on very large systems. Be reluctant to
5009 * use notify_on_release cgroups where very high task exit scaling
5010 * is required on large systems.
5011 *
Tejun Heo0e1d7682014-02-25 10:04:03 -05005012 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5013 * call cgroup_exit() while the task is still competent to handle
5014 * notify_on_release(), then leave the task attached to the root cgroup in
5015 * each hierarchy for the remainder of its exit. No need to bother with
5016 * init_css_set refcnting. init_css_set never goes away and we can't race
Li Zefane8604cb2014-03-28 15:18:27 +08005017 * with migration path - PF_EXITING is visible to migration path.
Paul Menageb4f48b62007-10-18 23:39:33 -07005018 */
Li Zefan1ec41832014-03-28 15:22:19 +08005019void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07005020{
Tejun Heo30159ec2013-06-25 11:53:37 -07005021 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005022 struct css_set *cset;
Tejun Heoeaf797a2014-02-25 10:04:03 -05005023 bool put_cset = false;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005024 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005025
5026 /*
Tejun Heo0e1d7682014-02-25 10:04:03 -05005027 * Unlink from @tsk from its css_set. As migration path can't race
5028 * with us, we can check cg_list without grabbing css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07005029 */
5030 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05005031 down_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005032 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05005033 up_write(&css_set_rwsem);
Tejun Heo0e1d7682014-02-25 10:04:03 -05005034 put_cset = true;
Paul Menage817929e2007-10-18 23:39:36 -07005035 }
5036
Paul Menageb4f48b62007-10-18 23:39:33 -07005037 /* Reassign the task to the init_css_set. */
Tejun Heoa8ad8052013-06-21 15:52:04 -07005038 cset = task_css_set(tsk);
5039 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005040
Li Zefan1ec41832014-03-28 15:22:19 +08005041 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05005042 /* see cgroup_post_fork() for details */
5043 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005044 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005045 struct cgroup_subsys_state *old_css = cset->subsys[i];
5046 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005047
Tejun Heoeb954192013-08-08 20:11:23 -04005048 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005049 }
5050 }
5051 }
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005052
Tejun Heoeaf797a2014-02-25 10:04:03 -05005053 if (put_cset)
5054 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07005055}
Paul Menage697f4162007-10-18 23:39:34 -07005056
Paul Menagebd89aab2007-10-18 23:40:44 -07005057static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005058{
Li Zefanf50daa72013-03-01 15:06:07 +08005059 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005060 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005061 /*
5062 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005063 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005064 * it now
5065 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005066 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005067
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005068 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005069 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005070 list_empty(&cgrp->release_list)) {
5071 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005072 need_schedule_work = 1;
5073 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005074 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005075 if (need_schedule_work)
5076 schedule_work(&release_agent_work);
5077 }
5078}
5079
Paul Menage81a6a5c2007-10-18 23:39:38 -07005080/*
5081 * Notify userspace when a cgroup is released, by running the
5082 * configured release agent with the name of the cgroup (path
5083 * relative to the root of cgroup file system) as the argument.
5084 *
5085 * Most likely, this user command will try to rmdir this cgroup.
5086 *
5087 * This races with the possibility that some other task will be
5088 * attached to this cgroup before it is removed, or that some other
5089 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5090 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5091 * unused, and this cgroup will be reprieved from its death sentence,
5092 * to continue to serve a useful existence. Next time it's released,
5093 * we will get notified again, if it still has 'notify_on_release' set.
5094 *
5095 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5096 * means only wait until the task is successfully execve()'d. The
5097 * separate release agent task is forked by call_usermodehelper(),
5098 * then control in this thread returns here, without waiting for the
5099 * release agent task. We don't bother to wait because the caller of
5100 * this routine has no use for the exit status of the release agent
5101 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005102 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005103static void cgroup_release_agent(struct work_struct *work)
5104{
5105 BUG_ON(work != &release_agent_work);
5106 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005107 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005108 while (!list_empty(&release_list)) {
5109 char *argv[3], *envp[3];
5110 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05005111 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07005112 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005113 struct cgroup,
5114 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005115 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005116 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05005117 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005118 if (!pathbuf)
5119 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05005120 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
5121 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07005122 goto continue_free;
5123 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5124 if (!agentbuf)
5125 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005126
5127 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005128 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05005129 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005130 argv[i] = NULL;
5131
5132 i = 0;
5133 /* minimal command environment */
5134 envp[i++] = "HOME=/";
5135 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5136 envp[i] = NULL;
5137
5138 /* Drop the lock while we invoke the usermode helper,
5139 * since the exec could involve hitting disk and hence
5140 * be a slow process */
5141 mutex_unlock(&cgroup_mutex);
5142 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005143 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005144 continue_free:
5145 kfree(pathbuf);
5146 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005147 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005148 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005149 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005150 mutex_unlock(&cgroup_mutex);
5151}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005152
5153static int __init cgroup_disable(char *str)
5154{
Tejun Heo30159ec2013-06-25 11:53:37 -07005155 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005156 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005157 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005158
5159 while ((token = strsep(&str, ",")) != NULL) {
5160 if (!*token)
5161 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005162
Tejun Heo3ed80a62014-02-08 10:36:58 -05005163 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005164 if (!strcmp(token, ss->name)) {
5165 ss->disabled = 1;
5166 printk(KERN_INFO "Disabling %s control group"
5167 " subsystem\n", ss->name);
5168 break;
5169 }
5170 }
5171 }
5172 return 1;
5173}
5174__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005175
Tejun Heob77d7b62013-08-13 11:01:54 -04005176/**
Tejun Heo5a17f542014-02-11 11:52:47 -05005177 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04005178 * @dentry: directory dentry of interest
5179 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005180 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005181 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5182 * to get the corresponding css and return it. If such css doesn't exist
5183 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005184 */
Tejun Heo5a17f542014-02-11 11:52:47 -05005185struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
5186 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005187{
Tejun Heo2bd59d42014-02-11 11:52:49 -05005188 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5189 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005190 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005191
Tejun Heo35cf0832013-08-26 18:40:56 -04005192 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05005193 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
5194 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005195 return ERR_PTR(-EBADF);
5196
Tejun Heo5a17f542014-02-11 11:52:47 -05005197 rcu_read_lock();
5198
Tejun Heo2bd59d42014-02-11 11:52:49 -05005199 /*
5200 * This path doesn't originate from kernfs and @kn could already
5201 * have been or be removed at any point. @kn->priv is RCU
5202 * protected for this access. See destroy_locked() for details.
5203 */
5204 cgrp = rcu_dereference(kn->priv);
5205 if (cgrp)
5206 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05005207
5208 if (!css || !css_tryget(css))
5209 css = ERR_PTR(-ENOENT);
5210
5211 rcu_read_unlock();
5212 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005213}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005214
Li Zefan1cb650b2013-08-19 10:05:24 +08005215/**
5216 * css_from_id - lookup css by id
5217 * @id: the cgroup id
5218 * @ss: cgroup subsys to be looked into
5219 *
5220 * Returns the css if there's valid one with @id, otherwise returns NULL.
5221 * Should be called under rcu_read_lock().
5222 */
5223struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5224{
Tejun Heo6fa49182014-05-04 15:09:13 -04005225 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo15a4c832014-05-04 15:09:14 -04005226 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005227}
5228
Paul Menagefe693432009-09-23 15:56:20 -07005229#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005230static struct cgroup_subsys_state *
5231debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005232{
5233 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5234
5235 if (!css)
5236 return ERR_PTR(-ENOMEM);
5237
5238 return css;
5239}
5240
Tejun Heoeb954192013-08-08 20:11:23 -04005241static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005242{
Tejun Heoeb954192013-08-08 20:11:23 -04005243 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005244}
5245
Tejun Heo182446d2013-08-08 20:11:24 -04005246static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5247 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005248{
Tejun Heo182446d2013-08-08 20:11:24 -04005249 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005250}
5251
Tejun Heo182446d2013-08-08 20:11:24 -04005252static u64 current_css_set_read(struct cgroup_subsys_state *css,
5253 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005254{
5255 return (u64)(unsigned long)current->cgroups;
5256}
5257
Tejun Heo182446d2013-08-08 20:11:24 -04005258static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005259 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005260{
5261 u64 count;
5262
5263 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005264 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005265 rcu_read_unlock();
5266 return count;
5267}
5268
Tejun Heo2da8ca82013-12-05 12:28:04 -05005269static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005270{
Tejun Heo69d02062013-06-12 21:04:50 -07005271 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005272 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05005273 char *name_buf;
Paul Menage7717f7b2009-09-23 15:56:22 -07005274
Tejun Heoe61734c2014-02-12 09:29:50 -05005275 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
5276 if (!name_buf)
5277 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07005278
Tejun Heo96d365e2014-02-13 06:58:40 -05005279 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005280 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005281 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005282 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005283 struct cgroup *c = link->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07005284
Tejun Heoa2dd4242014-03-19 10:23:55 -04005285 cgroup_name(c, name_buf, NAME_MAX + 1);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005286 seq_printf(seq, "Root %d group %s\n",
Tejun Heoa2dd4242014-03-19 10:23:55 -04005287 c->root->hierarchy_id, name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005288 }
5289 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05005290 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05005291 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07005292 return 0;
5293}
5294
5295#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005296static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005297{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005298 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005299 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005300
Tejun Heo96d365e2014-02-13 06:58:40 -05005301 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04005302 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005303 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005304 struct task_struct *task;
5305 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05005306
Tejun Heo5abb8852013-06-12 21:04:49 -07005307 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05005308
Tejun Heo5abb8852013-06-12 21:04:49 -07005309 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05005310 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5311 goto overflow;
5312 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07005313 }
Tejun Heoc7561122014-02-25 10:04:01 -05005314
5315 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
5316 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
5317 goto overflow;
5318 seq_printf(seq, " task %d\n", task_pid_vnr(task));
5319 }
5320 continue;
5321 overflow:
5322 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07005323 }
Tejun Heo96d365e2014-02-13 06:58:40 -05005324 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07005325 return 0;
5326}
5327
Tejun Heo182446d2013-08-08 20:11:24 -04005328static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005329{
Tejun Heo182446d2013-08-08 20:11:24 -04005330 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005331}
5332
5333static struct cftype debug_files[] = {
5334 {
Paul Menagefe693432009-09-23 15:56:20 -07005335 .name = "taskcount",
5336 .read_u64 = debug_taskcount_read,
5337 },
5338
5339 {
5340 .name = "current_css_set",
5341 .read_u64 = current_css_set_read,
5342 },
5343
5344 {
5345 .name = "current_css_set_refcount",
5346 .read_u64 = current_css_set_refcount_read,
5347 },
5348
5349 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005350 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005351 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005352 },
5353
5354 {
5355 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005356 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005357 },
5358
5359 {
Paul Menagefe693432009-09-23 15:56:20 -07005360 .name = "releasable",
5361 .read_u64 = releasable_read,
5362 },
Paul Menagefe693432009-09-23 15:56:20 -07005363
Tejun Heo4baf6e32012-04-01 12:09:55 -07005364 { } /* terminate */
5365};
Paul Menagefe693432009-09-23 15:56:20 -07005366
Tejun Heo073219e2014-02-08 10:36:58 -05005367struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005368 .css_alloc = debug_css_alloc,
5369 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005370 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005371};
5372#endif /* CONFIG_CGROUP_DEBUG */