blob: a93f6f1ebc69bfda2c24a728cbb8a6e85dd7cafd [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/spinlock.h>
Tejun Heo96d365e2014-02-13 06:58:40 -050045#include <linux/rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070047#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070048#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070049#include <linux/delayacct.h>
50#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080051#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070052#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070053#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070054#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020055#include <linux/kthread.h>
Tejun Heo776f02f2014-02-12 09:29:50 -050056#include <linux/delay.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070057
Arun Sharma600634972011-07-26 16:09:06 -070058#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070059
Tejun Heoe25e2cb2011-12-12 18:12:21 -080060/*
Tejun Heob1a21362013-11-29 10:42:58 -050061 * pidlists linger the following amount before being destroyed. The goal
62 * is avoiding frequent destruction in the middle of consecutive read calls
63 * Expiring in the middle is a performance problem not a correctness one.
64 * 1 sec should be enough.
65 */
66#define CGROUP_PIDLIST_DESTROY_DELAY HZ
67
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050068#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
69 MAX_CFTYPE_NAME + 2)
70
Tejun Heob1a21362013-11-29 10:42:58 -050071/*
Tejun Heoace2bee2014-02-11 11:52:47 -050072 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
73 * creation/removal and hierarchy changing operations including cgroup
74 * creation, removal, css association and controller rebinding. This outer
75 * lock is needed mainly to resolve the circular dependency between kernfs
76 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
77 */
78static DEFINE_MUTEX(cgroup_tree_mutex);
79
80/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080081 * cgroup_mutex is the master lock. Any modification to cgroup or its
82 * hierarchy must be performed while holding it.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080083 */
Tejun Heo22194492013-04-07 09:29:51 -070084#ifdef CONFIG_PROVE_RCU
85DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040086EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070087#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070088static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070089#endif
90
Tejun Heo69e943b2014-02-08 10:36:58 -050091/*
92 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
93 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
94 */
95static DEFINE_SPINLOCK(release_agent_path_lock);
96
Tejun Heoace2bee2014-02-11 11:52:47 -050097#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -050098 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -050099 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500100 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500101 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo87fb54f2013-12-06 15:11:55 -0500102
Ben Blumaae8aab2010-03-10 15:22:07 -0800103/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500104 * cgroup destruction makes heavy use of work items and there can be a lot
105 * of concurrent destructions. Use a separate workqueue so that cgroup
106 * destruction work items don't end up filling up max_active of system_wq
107 * which may lead to deadlock.
108 */
109static struct workqueue_struct *cgroup_destroy_wq;
110
111/*
Tejun Heob1a21362013-11-29 10:42:58 -0500112 * pidlist destructions need to be flushed on cgroup destruction. Use a
113 * separate workqueue as flush domain.
114 */
115static struct workqueue_struct *cgroup_pidlist_destroy_wq;
116
Tejun Heo3ed80a62014-02-08 10:36:58 -0500117/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500118#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500119static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120#include <linux/cgroup_subsys.h>
121};
Tejun Heo073219e2014-02-08 10:36:58 -0500122#undef SUBSYS
123
124/* array of cgroup subsystem names */
125#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
126static const char *cgroup_subsys_name[] = {
127#include <linux/cgroup_subsys.h>
128};
129#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700130
Paul Menageddbcc7e2007-10-18 23:39:30 -0700131/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700132 * The dummy hierarchy, reserved for the subsystems that are otherwise
133 * unattached - it never has more than a single cgroup, and all tasks are
134 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700135 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700136static struct cgroupfs_root cgroup_dummy_root;
137
138/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
139static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700140
141/* The list of hierarchy roots */
142
Tejun Heo9871bf92013-06-24 15:21:47 -0700143static LIST_HEAD(cgroup_roots);
144static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700145
Tejun Heo3417ae12014-02-08 10:37:01 -0500146/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700147static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700148
Li Zefan794611a2013-06-18 18:53:53 +0800149/*
150 * Assign a monotonically increasing serial number to cgroups. It
151 * guarantees cgroups with bigger numbers are newer than those with smaller
152 * numbers. Also, as cgroups are always appended to the parent's
153 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700154 * the ascending serial number order on the list. Protected by
155 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800156 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700157static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800158
Paul Menageddbcc7e2007-10-18 23:39:30 -0700159/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800160 * check for fork/exit handlers to call. This avoids us having to do
161 * extra work in the fork/exit path if none of the subsystems need to
162 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700163 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700164static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165
Tejun Heo628f7cd2013-06-28 16:24:11 -0700166static struct cftype cgroup_base_files[];
167
Tejun Heo59f52962014-02-11 11:52:49 -0500168static void cgroup_put(struct cgroup *cgrp);
Tejun Heof2e85d52014-02-11 11:52:49 -0500169static int rebind_subsystems(struct cgroupfs_root *root,
170 unsigned long added_mask, unsigned removed_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400171static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800172static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400173static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
174 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500175static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800176
Tejun Heo95109b62013-08-08 20:11:27 -0400177/**
178 * cgroup_css - obtain a cgroup's css for the specified subsystem
179 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400180 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400181 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400182 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
183 * function must be called either under cgroup_mutex or rcu_read_lock() and
184 * the caller is responsible for pinning the returned css if it wants to
185 * keep accessing it outside the said locks. This function may return
186 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400187 */
188static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400189 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400190{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400191 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500192 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500193 lockdep_is_held(&cgroup_tree_mutex) ||
194 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400195 else
196 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400197}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700198
Paul Menageddbcc7e2007-10-18 23:39:30 -0700199/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700200static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700201{
Tejun Heo54766d42013-06-12 21:04:53 -0700202 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700203}
204
Tejun Heo59f52962014-02-11 11:52:49 -0500205struct cgroup_subsys_state *seq_css(struct seq_file *seq)
206{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500207 struct kernfs_open_file *of = seq->private;
208 struct cgroup *cgrp = of->kn->parent->priv;
209 struct cftype *cft = seq_cft(seq);
210
211 /*
212 * This is open and unprotected implementation of cgroup_css().
213 * seq_css() is only called from a kernfs file operation which has
214 * an active reference on the file. Because all the subsystem
215 * files are drained before a css is disassociated with a cgroup,
216 * the matching css from the cgroup's subsys table is guaranteed to
217 * be and stay valid until the enclosing operation is complete.
218 */
219 if (cft->ss)
220 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
221 else
222 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500223}
224EXPORT_SYMBOL_GPL(seq_css);
225
Li Zefan78574cf2013-04-08 19:00:38 -0700226/**
227 * cgroup_is_descendant - test ancestry
228 * @cgrp: the cgroup to be tested
229 * @ancestor: possible ancestor of @cgrp
230 *
231 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
232 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
233 * and @ancestor are accessible.
234 */
235bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
236{
237 while (cgrp) {
238 if (cgrp == ancestor)
239 return true;
240 cgrp = cgrp->parent;
241 }
242 return false;
243}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700244
Adrian Bunke9685a02008-02-07 00:13:46 -0800245static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700246{
247 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700248 (1 << CGRP_RELEASABLE) |
249 (1 << CGRP_NOTIFY_ON_RELEASE);
250 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700251}
252
Adrian Bunke9685a02008-02-07 00:13:46 -0800253static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700254{
Paul Menagebd89aab2007-10-18 23:40:44 -0700255 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700256}
257
Tejun Heo30159ec2013-06-25 11:53:37 -0700258/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500259 * for_each_css - iterate all css's of a cgroup
260 * @css: the iteration cursor
261 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
262 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700263 *
264 * Should be called under cgroup_mutex.
265 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500266#define for_each_css(css, ssid, cgrp) \
267 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
268 if (!((css) = rcu_dereference_check( \
269 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500270 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500271 lockdep_is_held(&cgroup_mutex)))) { } \
272 else
273
274/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500275 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700276 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500277 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700278 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500279#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500280 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
281 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700282
Tejun Heo5549c492013-06-24 15:21:48 -0700283/* iterate across the active hierarchies */
284#define for_each_active_root(root) \
285 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700286
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700287/**
288 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
289 * @cgrp: the cgroup to be checked for liveness
290 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700291 * On success, returns true; the mutex should be later unlocked. On
292 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700293 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700294static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700295{
296 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700297 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700298 mutex_unlock(&cgroup_mutex);
299 return false;
300 }
301 return true;
302}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700303
Paul Menage81a6a5c2007-10-18 23:39:38 -0700304/* the list of cgroups eligible for automatic release. Protected by
305 * release_list_lock */
306static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200307static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700308static void cgroup_release_agent(struct work_struct *work);
309static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700310static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311
Tejun Heo69d02062013-06-12 21:04:50 -0700312/*
313 * A cgroup can be associated with multiple css_sets as different tasks may
314 * belong to different cgroups on different hierarchies. In the other
315 * direction, a css_set is naturally associated with multiple cgroups.
316 * This M:N relationship is represented by the following link structure
317 * which exists for each association and allows traversing the associations
318 * from both sides.
319 */
320struct cgrp_cset_link {
321 /* the cgroup and css_set this link associates */
322 struct cgroup *cgrp;
323 struct css_set *cset;
324
325 /* list of cgrp_cset_links anchored at cgrp->cset_links */
326 struct list_head cset_link;
327
328 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
329 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700330};
331
332/* The default css_set - used by init and its children prior to any
333 * hierarchies being mounted. It contains a pointer to the root state
334 * for each subsystem. Also used to anchor the list of css_sets. Not
335 * reference-counted, to improve performance when child cgroups
336 * haven't been created.
337 */
338
339static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700340static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700341
Tejun Heo0942eee2013-08-08 20:11:26 -0400342/*
Tejun Heo96d365e2014-02-13 06:58:40 -0500343 * css_set_rwsem protects the list of css_set objects, and the chain of
344 * tasks off each css_set.
Tejun Heo0942eee2013-08-08 20:11:26 -0400345 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500346static DECLARE_RWSEM(css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700347static int css_set_count;
348
Paul Menage7717f7b2009-09-23 15:56:22 -0700349/*
350 * hash table for cgroup groups. This improves the performance to find
351 * an existing css_set. This hash doesn't (currently) take into
352 * account cgroups in empty hierarchies.
353 */
Li Zefan472b1052008-04-29 01:00:11 -0700354#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800355static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700356
Li Zefan0ac801f2013-01-10 11:49:27 +0800357static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700358{
Li Zefan0ac801f2013-01-10 11:49:27 +0800359 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700360 struct cgroup_subsys *ss;
361 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700362
Tejun Heo30159ec2013-06-25 11:53:37 -0700363 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800364 key += (unsigned long)css[i];
365 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700366
Li Zefan0ac801f2013-01-10 11:49:27 +0800367 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700368}
369
Tejun Heo89c55092014-02-13 06:58:40 -0500370static void put_css_set_locked(struct css_set *cset, bool taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700371{
Tejun Heo69d02062013-06-12 21:04:50 -0700372 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700373
Tejun Heo89c55092014-02-13 06:58:40 -0500374 lockdep_assert_held(&css_set_rwsem);
375
376 if (!atomic_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700377 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700378
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700379 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700380 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700381 css_set_count--;
382
Tejun Heo69d02062013-06-12 21:04:50 -0700383 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700384 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700385
Tejun Heo69d02062013-06-12 21:04:50 -0700386 list_del(&link->cset_link);
387 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800388
Tejun Heo96d365e2014-02-13 06:58:40 -0500389 /* @cgrp can't go away while we're holding css_set_rwsem */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700390 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700391 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700392 set_bit(CGRP_RELEASABLE, &cgrp->flags);
393 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700394 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700395
396 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700397 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700398
Tejun Heo5abb8852013-06-12 21:04:49 -0700399 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700400}
401
Tejun Heo89c55092014-02-13 06:58:40 -0500402static void put_css_set(struct css_set *cset, bool taskexit)
403{
404 /*
405 * Ensure that the refcount doesn't hit zero while any readers
406 * can see it. Similar to atomic_dec_and_lock(), but for an
407 * rwlock
408 */
409 if (atomic_add_unless(&cset->refcount, -1, 1))
410 return;
411
412 down_write(&css_set_rwsem);
413 put_css_set_locked(cset, taskexit);
414 up_write(&css_set_rwsem);
415}
416
Paul Menage817929e2007-10-18 23:39:36 -0700417/*
418 * refcounted get/put for css_set objects
419 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700420static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700421{
Tejun Heo5abb8852013-06-12 21:04:49 -0700422 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700423}
424
Tejun Heob326f9d2013-06-24 15:21:48 -0700425/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700426 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700427 * @cset: candidate css_set being tested
428 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700429 * @new_cgrp: cgroup that's being entered by the task
430 * @template: desired set of css pointers in css_set (pre-calculated)
431 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800432 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700433 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
434 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700435static bool compare_css_sets(struct css_set *cset,
436 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700437 struct cgroup *new_cgrp,
438 struct cgroup_subsys_state *template[])
439{
440 struct list_head *l1, *l2;
441
Tejun Heo5abb8852013-06-12 21:04:49 -0700442 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700443 /* Not all subsystems matched */
444 return false;
445 }
446
447 /*
448 * Compare cgroup pointers in order to distinguish between
449 * different cgroups in heirarchies with no subsystems. We
450 * could get by with just this check alone (and skip the
451 * memcmp above) but on most setups the memcmp check will
452 * avoid the need for this more expensive check on almost all
453 * candidates.
454 */
455
Tejun Heo69d02062013-06-12 21:04:50 -0700456 l1 = &cset->cgrp_links;
457 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700458 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700459 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700460 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700461
462 l1 = l1->next;
463 l2 = l2->next;
464 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700465 if (l1 == &cset->cgrp_links) {
466 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700467 break;
468 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700469 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700470 }
471 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700472 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
473 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
474 cgrp1 = link1->cgrp;
475 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700476 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700477 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700478
479 /*
480 * If this hierarchy is the hierarchy of the cgroup
481 * that's changing, then we need to check that this
482 * css_set points to the new cgroup; if it's any other
483 * hierarchy, then this css_set should point to the
484 * same cgroup as the old css_set.
485 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700486 if (cgrp1->root == new_cgrp->root) {
487 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700488 return false;
489 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700490 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700491 return false;
492 }
493 }
494 return true;
495}
496
Tejun Heob326f9d2013-06-24 15:21:48 -0700497/**
498 * find_existing_css_set - init css array and find the matching css_set
499 * @old_cset: the css_set that we're using before the cgroup transition
500 * @cgrp: the cgroup that we're moving into
501 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700502 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700503static struct css_set *find_existing_css_set(struct css_set *old_cset,
504 struct cgroup *cgrp,
505 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700506{
Paul Menagebd89aab2007-10-18 23:40:44 -0700507 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700508 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700509 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800510 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700511 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700512
Ben Blumaae8aab2010-03-10 15:22:07 -0800513 /*
514 * Build the set of subsystem state objects that we want to see in the
515 * new css_set. while subsystems can change globally, the entries here
516 * won't change, so no need for locking.
517 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700518 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400519 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700520 /* Subsystem is in this hierarchy. So we want
521 * the subsystem state from the new
522 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400523 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700524 } else {
525 /* Subsystem is not in this hierarchy, so we
526 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700528 }
529 }
530
Li Zefan0ac801f2013-01-10 11:49:27 +0800531 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700532 hash_for_each_possible(css_set_table, cset, hlist, key) {
533 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700534 continue;
535
536 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700537 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700538 }
Paul Menage817929e2007-10-18 23:39:36 -0700539
540 /* No existing cgroup group matched */
541 return NULL;
542}
543
Tejun Heo69d02062013-06-12 21:04:50 -0700544static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700545{
Tejun Heo69d02062013-06-12 21:04:50 -0700546 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700547
Tejun Heo69d02062013-06-12 21:04:50 -0700548 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
549 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700550 kfree(link);
551 }
552}
553
Tejun Heo69d02062013-06-12 21:04:50 -0700554/**
555 * allocate_cgrp_cset_links - allocate cgrp_cset_links
556 * @count: the number of links to allocate
557 * @tmp_links: list_head the allocated links are put on
558 *
559 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
560 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700561 */
Tejun Heo69d02062013-06-12 21:04:50 -0700562static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700563{
Tejun Heo69d02062013-06-12 21:04:50 -0700564 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700565 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700566
567 INIT_LIST_HEAD(tmp_links);
568
Li Zefan36553432008-07-29 22:33:19 -0700569 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700570 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700571 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700572 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700573 return -ENOMEM;
574 }
Tejun Heo69d02062013-06-12 21:04:50 -0700575 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700576 }
577 return 0;
578}
579
Li Zefanc12f65d2009-01-07 18:07:42 -0800580/**
581 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700582 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700583 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800584 * @cgrp: the destination cgroup
585 */
Tejun Heo69d02062013-06-12 21:04:50 -0700586static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
587 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800588{
Tejun Heo69d02062013-06-12 21:04:50 -0700589 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800590
Tejun Heo69d02062013-06-12 21:04:50 -0700591 BUG_ON(list_empty(tmp_links));
592 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
593 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700594 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700595 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700596 /*
597 * Always add links to the tail of the list so that the list
598 * is sorted by order of hierarchy creation
599 */
Tejun Heo69d02062013-06-12 21:04:50 -0700600 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800601}
602
Tejun Heob326f9d2013-06-24 15:21:48 -0700603/**
604 * find_css_set - return a new css_set with one cgroup updated
605 * @old_cset: the baseline css_set
606 * @cgrp: the cgroup to be updated
607 *
608 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
609 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700610 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700611static struct css_set *find_css_set(struct css_set *old_cset,
612 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700613{
Tejun Heob326f9d2013-06-24 15:21:48 -0700614 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700615 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700616 struct list_head tmp_links;
617 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800618 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700619
Tejun Heob326f9d2013-06-24 15:21:48 -0700620 lockdep_assert_held(&cgroup_mutex);
621
Paul Menage817929e2007-10-18 23:39:36 -0700622 /* First see if we already have a cgroup group that matches
623 * the desired set */
Tejun Heo96d365e2014-02-13 06:58:40 -0500624 down_read(&css_set_rwsem);
Tejun Heo5abb8852013-06-12 21:04:49 -0700625 cset = find_existing_css_set(old_cset, cgrp, template);
626 if (cset)
627 get_css_set(cset);
Tejun Heo96d365e2014-02-13 06:58:40 -0500628 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700629
Tejun Heo5abb8852013-06-12 21:04:49 -0700630 if (cset)
631 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700632
Tejun Heof4f4be22013-06-12 21:04:51 -0700633 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700634 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700635 return NULL;
636
Tejun Heo69d02062013-06-12 21:04:50 -0700637 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700638 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700639 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700640 return NULL;
641 }
642
Tejun Heo5abb8852013-06-12 21:04:49 -0700643 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700644 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700645 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -0500646 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -0500647 INIT_LIST_HEAD(&cset->mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -0500648 INIT_LIST_HEAD(&cset->mg_node);
Tejun Heo5abb8852013-06-12 21:04:49 -0700649 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700650
651 /* Copy the set of subsystem state objects generated in
652 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700653 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700654
Tejun Heo96d365e2014-02-13 06:58:40 -0500655 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700656 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700657 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700658 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700659
Paul Menage7717f7b2009-09-23 15:56:22 -0700660 if (c->root == cgrp->root)
661 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700662 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700663 }
Paul Menage817929e2007-10-18 23:39:36 -0700664
Tejun Heo69d02062013-06-12 21:04:50 -0700665 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700666
Paul Menage817929e2007-10-18 23:39:36 -0700667 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700668
669 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700670 key = css_set_hash(cset->subsys);
671 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700672
Tejun Heo96d365e2014-02-13 06:58:40 -0500673 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -0700674
Tejun Heo5abb8852013-06-12 21:04:49 -0700675 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700676}
677
Tejun Heo2bd59d42014-02-11 11:52:49 -0500678static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
679{
680 struct cgroup *top_cgrp = kf_root->kn->priv;
681
682 return top_cgrp->root;
683}
684
Tejun Heof2e85d52014-02-11 11:52:49 -0500685static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
686{
687 int id;
688
689 lockdep_assert_held(&cgroup_mutex);
690
691 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
692 GFP_KERNEL);
693 if (id < 0)
694 return id;
695
696 root->hierarchy_id = id;
697 return 0;
698}
699
700static void cgroup_exit_root_id(struct cgroupfs_root *root)
701{
702 lockdep_assert_held(&cgroup_mutex);
703
704 if (root->hierarchy_id) {
705 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
706 root->hierarchy_id = 0;
707 }
708}
709
710static void cgroup_free_root(struct cgroupfs_root *root)
711{
712 if (root) {
713 /* hierarhcy ID shoulid already have been released */
714 WARN_ON_ONCE(root->hierarchy_id);
715
716 idr_destroy(&root->cgroup_idr);
717 kfree(root);
718 }
719}
720
Tejun Heo776f02f2014-02-12 09:29:50 -0500721static void cgroup_destroy_root(struct cgroupfs_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -0500722{
Tejun Heof2e85d52014-02-11 11:52:49 -0500723 struct cgroup *cgrp = &root->top_cgroup;
724 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -0500725
Tejun Heo2bd59d42014-02-11 11:52:49 -0500726 mutex_lock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500727 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500728
Tejun Heo776f02f2014-02-12 09:29:50 -0500729 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heof2e85d52014-02-11 11:52:49 -0500730 BUG_ON(!list_empty(&cgrp->children));
731
Tejun Heof2e85d52014-02-11 11:52:49 -0500732 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo35585572014-02-13 06:58:38 -0500733 WARN_ON(rebind_subsystems(root, 0, root->subsys_mask));
Tejun Heof2e85d52014-02-11 11:52:49 -0500734
735 /*
736 * Release all the links from cset_links to this hierarchy's
737 * root cgroup
738 */
Tejun Heo96d365e2014-02-13 06:58:40 -0500739 down_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500740
741 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
742 list_del(&link->cset_link);
743 list_del(&link->cgrp_link);
744 kfree(link);
745 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500746 up_write(&css_set_rwsem);
Tejun Heof2e85d52014-02-11 11:52:49 -0500747
748 if (!list_empty(&root->root_list)) {
749 list_del(&root->root_list);
750 cgroup_root_count--;
751 }
752
753 cgroup_exit_root_id(root);
754
755 mutex_unlock(&cgroup_mutex);
756 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500757
Tejun Heo2bd59d42014-02-11 11:52:49 -0500758 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500759 cgroup_free_root(root);
760}
761
Tejun Heoceb6a082014-02-25 10:04:02 -0500762/* look up cgroup associated with given css_set on the specified hierarchy */
763static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700764 struct cgroupfs_root *root)
765{
Paul Menage7717f7b2009-09-23 15:56:22 -0700766 struct cgroup *res = NULL;
767
Tejun Heo96d365e2014-02-13 06:58:40 -0500768 lockdep_assert_held(&cgroup_mutex);
769 lockdep_assert_held(&css_set_rwsem);
770
Tejun Heo5abb8852013-06-12 21:04:49 -0700771 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700772 res = &root->top_cgroup;
773 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700774 struct cgrp_cset_link *link;
775
776 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700777 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700778
Paul Menage7717f7b2009-09-23 15:56:22 -0700779 if (c->root == root) {
780 res = c;
781 break;
782 }
783 }
784 }
Tejun Heo96d365e2014-02-13 06:58:40 -0500785
Paul Menage7717f7b2009-09-23 15:56:22 -0700786 BUG_ON(!res);
787 return res;
788}
789
790/*
Tejun Heoceb6a082014-02-25 10:04:02 -0500791 * Return the cgroup for "task" from the given hierarchy. Must be
792 * called with cgroup_mutex and css_set_rwsem held.
793 */
794static struct cgroup *task_cgroup_from_root(struct task_struct *task,
795 struct cgroupfs_root *root)
796{
797 /*
798 * No need to lock the task - since we hold cgroup_mutex the
799 * task can't change groups, so the only thing that can happen
800 * is that it exits and its css is set back to init_css_set.
801 */
802 return cset_cgroup_from_root(task_css_set(task), root);
803}
804
805/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806 * There is one global cgroup mutex. We also require taking
807 * task_lock() when dereferencing a task's cgroup subsys pointers.
808 * See "The task_lock() exception", at the end of this comment.
809 *
810 * A task must hold cgroup_mutex to modify cgroups.
811 *
812 * Any task can increment and decrement the count field without lock.
813 * So in general, code holding cgroup_mutex can't rely on the count
814 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800815 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816 * means that no tasks are currently attached, therefore there is no
817 * way a task attached to that cgroup can fork (the other way to
818 * increment the count). So code holding cgroup_mutex can safely
819 * assume that if the count is zero, it will stay zero. Similarly, if
820 * a task holds cgroup_mutex on a cgroup with zero count, it
821 * knows that the cgroup won't be removed, as cgroup_rmdir()
822 * needs that mutex.
823 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
825 * (usually) take cgroup_mutex. These are the two most performance
826 * critical pieces of code here. The exception occurs on cgroup_exit(),
827 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
828 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800829 * to the release agent with the name of the cgroup (path relative to
830 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700831 *
832 * A cgroup can only be deleted if both its 'count' of using tasks
833 * is zero, and its list of 'children' cgroups is empty. Since all
834 * tasks in the system use _some_ cgroup, and since there is always at
835 * least one task in the system (init, pid == 1), therefore, top_cgroup
836 * always has either children cgroups and/or using tasks. So we don't
837 * need a special hack to ensure that top_cgroup cannot be deleted.
838 *
839 * The task_lock() exception
840 *
841 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800842 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800843 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700844 * several performance critical places that need to reference
845 * task->cgroup without the expense of grabbing a system global
846 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800847 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
849 * the task_struct routinely used for such matters.
850 *
851 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800852 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853 */
854
Tejun Heo628f7cd2013-06-28 16:24:11 -0700855static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500856static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700857static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700858
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500859static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
860 char *buf)
861{
862 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
863 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
864 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
865 cft->ss->name, cft->name);
866 else
867 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
868 return buf;
869}
870
Tejun Heof2e85d52014-02-11 11:52:49 -0500871/**
872 * cgroup_file_mode - deduce file mode of a control file
873 * @cft: the control file in question
874 *
875 * returns cft->mode if ->mode is not 0
876 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
877 * returns S_IRUGO if it has only a read handler
878 * returns S_IWUSR if it has only a write hander
879 */
880static umode_t cgroup_file_mode(const struct cftype *cft)
881{
882 umode_t mode = 0;
883
884 if (cft->mode)
885 return cft->mode;
886
887 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
888 mode |= S_IRUGO;
889
890 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
891 cft->trigger)
892 mode |= S_IWUSR;
893
894 return mode;
895}
896
Li Zefanbe445622013-01-24 14:31:42 +0800897static void cgroup_free_fn(struct work_struct *work)
898{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700899 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800900
Tejun Heo3c9c8252014-02-12 09:29:50 -0500901 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heob1a21362013-11-29 10:42:58 -0500902 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800903
Tejun Heo776f02f2014-02-12 09:29:50 -0500904 if (cgrp->parent) {
905 /*
906 * We get a ref to the parent, and put the ref when this
907 * cgroup is being freed, so it's guaranteed that the
908 * parent won't be destroyed before its children.
909 */
910 cgroup_put(cgrp->parent);
911 kernfs_put(cgrp->kn);
912 kfree(cgrp);
913 } else {
914 /*
915 * This is top cgroup's refcnt reaching zero, which
916 * indicates that the root should be released.
917 */
918 cgroup_destroy_root(cgrp->root);
919 }
Li Zefanbe445622013-01-24 14:31:42 +0800920}
921
922static void cgroup_free_rcu(struct rcu_head *head)
923{
924 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
925
Tejun Heoea15f8c2013-06-13 19:27:42 -0700926 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500927 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800928}
929
Tejun Heo59f52962014-02-11 11:52:49 -0500930static void cgroup_get(struct cgroup *cgrp)
931{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500932 WARN_ON_ONCE(cgroup_is_dead(cgrp));
933 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
934 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700935}
936
Tejun Heo59f52962014-02-11 11:52:49 -0500937static void cgroup_put(struct cgroup *cgrp)
938{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500939 if (!atomic_dec_and_test(&cgrp->refcnt))
940 return;
Tejun Heo776f02f2014-02-12 09:29:50 -0500941 if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
Tejun Heo2bd59d42014-02-11 11:52:49 -0500942 return;
Tejun Heo59f52962014-02-11 11:52:49 -0500943
Tejun Heo2bd59d42014-02-11 11:52:49 -0500944 /*
945 * XXX: cgrp->id is only used to look up css's. As cgroup and
946 * css's lifetimes will be decoupled, it should be made
947 * per-subsystem and moved to css->id so that lookups are
948 * successful until the target css is released.
949 */
950 mutex_lock(&cgroup_mutex);
951 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
952 mutex_unlock(&cgroup_mutex);
953 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700954
Tejun Heo2bd59d42014-02-11 11:52:49 -0500955 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700956}
957
Li Zefan2739d3c2013-01-21 18:18:33 +0800958static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500960 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961
Tejun Heoace2bee2014-02-11 11:52:47 -0500962 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500963 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -0700964}
965
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400966/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700967 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700968 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400969 * @subsys_mask: mask of the subsystem ids whose files should be removed
970 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700971static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700972{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400973 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700974 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700975
Tejun Heob420ba72013-07-12 12:34:02 -0700976 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -0500977 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -0700978
979 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400980 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -0500981 list_for_each_entry(cfts, &ss->cfts, node)
982 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400983 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984}
985
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700987 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988{
Paul Menagebd89aab2007-10-18 23:40:44 -0700989 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700990 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -0700991 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992
Tejun Heoace2bee2014-02-11 11:52:47 -0500993 lockdep_assert_held(&cgroup_tree_mutex);
994 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -0800995
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -0500997 for_each_subsys(ss, i)
998 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
999 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000
Tejun Heo31261212013-06-28 17:07:30 -07001001 ret = cgroup_populate_dir(cgrp, added_mask);
1002 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001003 return ret;
Tejun Heo31261212013-06-28 17:07:30 -07001004
1005 /*
1006 * Nothing can fail from this point on. Remove files for the
1007 * removed subsystems and rebind each subsystem.
1008 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001009 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001010 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -05001011 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001012
Tejun Heo30159ec2013-06-25 11:53:37 -07001013 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001015
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001016 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001018 BUG_ON(cgroup_css(cgrp, ss));
1019 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1020 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001021
Tejun Heo73e80ed2013-08-13 11:01:55 -04001022 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001023 cgroup_css(cgroup_dummy_top, ss));
1024 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001025
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001026 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001027 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001028 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001029
Ben Blumcf5d5942010-03-10 15:22:09 -08001030 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001031 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001032 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001034 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1035 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001036
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001038 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001039
Tejun Heoca8bdca2013-08-26 18:40:56 -04001040 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001041 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1042
Tejun Heo9871bf92013-06-24 15:21:47 -07001043 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001044 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 }
1046 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047
Tejun Heo2bd59d42014-02-11 11:52:49 -05001048 kernfs_activate(cgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 return 0;
1050}
1051
Tejun Heo2bd59d42014-02-11 11:52:49 -05001052static int cgroup_show_options(struct seq_file *seq,
1053 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001055 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001057 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058
Tejun Heob85d2042013-12-06 15:11:57 -05001059 for_each_subsys(ss, ssid)
1060 if (root->subsys_mask & (1 << ssid))
1061 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001062 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1063 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001064 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001066 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001067 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001068
1069 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001070 if (strlen(root->release_agent_path))
1071 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001072 spin_unlock(&release_agent_path_lock);
1073
Tejun Heo2260e7f2012-11-19 08:13:38 -08001074 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001075 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001076 if (strlen(root->name))
1077 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 return 0;
1079}
1080
1081struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001082 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001084 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001085 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001086 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001087 /* User explicitly requested empty subsystem */
1088 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089};
1090
Ben Blumaae8aab2010-03-10 15:22:07 -08001091/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001092 * Convert a hierarchy specifier into a bitmask of subsystems and
1093 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1094 * array. This function takes refcounts on subsystems to be used, unless it
1095 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001096 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001097static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001099 char *token, *o = data;
1100 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001101 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001102 struct cgroup_subsys *ss;
1103 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001104
Ben Blumaae8aab2010-03-10 15:22:07 -08001105 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1106
Li Zefanf9ab5b52009-06-17 16:26:33 -07001107#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001108 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001109#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110
Paul Menagec6d57f32009-09-23 15:56:19 -07001111 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112
1113 while ((token = strsep(&o, ",")) != NULL) {
1114 if (!*token)
1115 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001116 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001117 /* Explicitly have no subsystems */
1118 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001119 continue;
1120 }
1121 if (!strcmp(token, "all")) {
1122 /* Mutually exclusive option 'all' + subsystem name */
1123 if (one_ss)
1124 return -EINVAL;
1125 all_ss = true;
1126 continue;
1127 }
Tejun Heo873fe092013-04-14 20:15:26 -07001128 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1129 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1130 continue;
1131 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001132 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001133 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001134 continue;
1135 }
1136 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001137 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001138 continue;
1139 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001140 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001141 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001142 continue;
1143 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001144 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001145 /* Specifying two release agents is forbidden */
1146 if (opts->release_agent)
1147 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001148 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001149 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001150 if (!opts->release_agent)
1151 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001152 continue;
1153 }
1154 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001155 const char *name = token + 5;
1156 /* Can't specify an empty name */
1157 if (!strlen(name))
1158 return -EINVAL;
1159 /* Must match [\w.-]+ */
1160 for (i = 0; i < strlen(name); i++) {
1161 char c = name[i];
1162 if (isalnum(c))
1163 continue;
1164 if ((c == '.') || (c == '-') || (c == '_'))
1165 continue;
1166 return -EINVAL;
1167 }
1168 /* Specifying two names is forbidden */
1169 if (opts->name)
1170 return -EINVAL;
1171 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001172 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001173 GFP_KERNEL);
1174 if (!opts->name)
1175 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001176
1177 continue;
1178 }
1179
Tejun Heo30159ec2013-06-25 11:53:37 -07001180 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001181 if (strcmp(token, ss->name))
1182 continue;
1183 if (ss->disabled)
1184 continue;
1185
1186 /* Mutually exclusive option 'all' + subsystem name */
1187 if (all_ss)
1188 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001189 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001190 one_ss = true;
1191
1192 break;
1193 }
1194 if (i == CGROUP_SUBSYS_COUNT)
1195 return -ENOENT;
1196 }
1197
1198 /*
1199 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001200 * otherwise if 'none', 'name=' and a subsystem name options
1201 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001202 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001203 if (all_ss || (!one_ss && !opts->none && !opts->name))
1204 for_each_subsys(ss, i)
1205 if (!ss->disabled)
1206 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001207
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001208 /* Consistency checks */
1209
Tejun Heo873fe092013-04-14 20:15:26 -07001210 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1211 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1212
Tejun Heod3ba07c2014-02-13 06:58:38 -05001213 if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
1214 opts->cpuset_clone_children || opts->release_agent ||
1215 opts->name) {
1216 pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001217 return -EINVAL;
1218 }
Tejun Heo873fe092013-04-14 20:15:26 -07001219 }
1220
Li Zefanf9ab5b52009-06-17 16:26:33 -07001221 /*
1222 * Option noprefix was introduced just for backward compatibility
1223 * with the old cpuset, so we allow noprefix only if mounting just
1224 * the cpuset subsystem.
1225 */
Tejun Heo93438622013-04-14 20:15:25 -07001226 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001227 return -EINVAL;
1228
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001229
1230 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001231 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001232 return -EINVAL;
1233
1234 /*
1235 * We either have to specify by name or by subsystems. (So all
1236 * empty hierarchies must have a name).
1237 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001238 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239 return -EINVAL;
1240
1241 return 0;
1242}
1243
Tejun Heo2bd59d42014-02-11 11:52:49 -05001244static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001245{
1246 int ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001247 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001248 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001249 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001250
Tejun Heo873fe092013-04-14 20:15:26 -07001251 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1252 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1253 return -EINVAL;
1254 }
1255
Tejun Heoace2bee2014-02-11 11:52:47 -05001256 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001257 mutex_lock(&cgroup_mutex);
1258
1259 /* See what subsystems are wanted */
1260 ret = parse_cgroupfs_options(data, &opts);
1261 if (ret)
1262 goto out_unlock;
1263
Tejun Heoa8a648c2013-06-24 15:21:47 -07001264 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001265 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1266 task_tgid_nr(current), current->comm);
1267
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001268 added_mask = opts.subsys_mask & ~root->subsys_mask;
1269 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001270
Ben Blumcf5d5942010-03-10 15:22:09 -08001271 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001272 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001273 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001274 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1275 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1276 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001277 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001278 goto out_unlock;
1279 }
1280
Tejun Heof172e672013-06-28 17:07:30 -07001281 /* remounting is not allowed for populated hierarchies */
Tejun Heo3c9c8252014-02-12 09:29:50 -05001282 if (!list_empty(&root->top_cgroup.children)) {
Tejun Heof172e672013-06-28 17:07:30 -07001283 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001284 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001285 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001288 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001289 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001290
Tejun Heo69e943b2014-02-08 10:36:58 -05001291 if (opts.release_agent) {
1292 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001293 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001294 spin_unlock(&release_agent_path_lock);
1295 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001297 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001298 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001300 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301 return ret;
1302}
1303
Tejun Heoafeb0f92014-02-13 06:58:39 -05001304/*
1305 * To reduce the fork() overhead for systems that are not actually using
1306 * their cgroups capability, we don't maintain the lists running through
1307 * each css_set to its tasks until we see the list actually used - in other
1308 * words after the first mount.
1309 */
1310static bool use_task_css_set_links __read_mostly;
1311
1312static void cgroup_enable_task_cg_lists(void)
1313{
1314 struct task_struct *p, *g;
1315
Tejun Heo96d365e2014-02-13 06:58:40 -05001316 down_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001317
1318 if (use_task_css_set_links)
1319 goto out_unlock;
1320
1321 use_task_css_set_links = true;
1322
1323 /*
1324 * We need tasklist_lock because RCU is not safe against
1325 * while_each_thread(). Besides, a forking task that has passed
1326 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1327 * is not guaranteed to have its child immediately visible in the
1328 * tasklist if we walk through it with RCU.
1329 */
1330 read_lock(&tasklist_lock);
1331 do_each_thread(g, p) {
1332 task_lock(p);
1333
1334 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1335 task_css_set(p) != &init_css_set);
1336
1337 /*
1338 * We should check if the process is exiting, otherwise
1339 * it will race with cgroup_exit() in that the list
1340 * entry won't be deleted though the process has exited.
Tejun Heof153ad12014-02-25 09:56:49 -05001341 * Do it while holding siglock so that we don't end up
1342 * racing against cgroup_exit().
Tejun Heoafeb0f92014-02-13 06:58:39 -05001343 */
Tejun Heof153ad12014-02-25 09:56:49 -05001344 spin_lock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001345 if (!(p->flags & PF_EXITING))
1346 list_add(&p->cg_list, &task_css_set(p)->tasks);
Tejun Heof153ad12014-02-25 09:56:49 -05001347 spin_unlock_irq(&p->sighand->siglock);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001348
1349 task_unlock(p);
1350 } while_each_thread(g, p);
1351 read_unlock(&tasklist_lock);
1352out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05001353 up_write(&css_set_rwsem);
Tejun Heoafeb0f92014-02-13 06:58:39 -05001354}
1355
Paul Menagecc31edc2008-10-18 20:28:04 -07001356static void init_cgroup_housekeeping(struct cgroup *cgrp)
1357{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001358 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001359 INIT_LIST_HEAD(&cgrp->sibling);
1360 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001361 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001362 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001363 INIT_LIST_HEAD(&cgrp->pidlists);
1364 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001365 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001366}
Paul Menagec6d57f32009-09-23 15:56:19 -07001367
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368static void init_cgroup_root(struct cgroupfs_root *root)
1369{
Paul Menagebd89aab2007-10-18 23:40:44 -07001370 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001371
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001373 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001374 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001375 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001376 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377}
1378
Paul Menagec6d57f32009-09-23 15:56:19 -07001379static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1380{
1381 struct cgroupfs_root *root;
1382
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001383 if (!opts->subsys_mask && !opts->none)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001384 return ERR_PTR(-EINVAL);
Paul Menagec6d57f32009-09-23 15:56:19 -07001385
1386 root = kzalloc(sizeof(*root), GFP_KERNEL);
1387 if (!root)
1388 return ERR_PTR(-ENOMEM);
1389
1390 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001391
Paul Menagec6d57f32009-09-23 15:56:19 -07001392 root->flags = opts->flags;
1393 if (opts->release_agent)
1394 strcpy(root->release_agent_path, opts->release_agent);
1395 if (opts->name)
1396 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001397 if (opts->cpuset_clone_children)
1398 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001399 return root;
1400}
1401
Tejun Heo35585572014-02-13 06:58:38 -05001402static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask)
Tejun Heod427dfe2014-02-11 11:52:48 -05001403{
1404 LIST_HEAD(tmp_links);
Tejun Heod427dfe2014-02-11 11:52:48 -05001405 struct cgroup *root_cgrp = &root->top_cgroup;
Tejun Heod427dfe2014-02-11 11:52:48 -05001406 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001407 int i, ret;
1408
1409 lockdep_assert_held(&cgroup_tree_mutex);
1410 lockdep_assert_held(&cgroup_mutex);
Tejun Heod427dfe2014-02-11 11:52:48 -05001411
1412 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1413 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001414 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001415 root_cgrp->id = ret;
1416
Tejun Heod427dfe2014-02-11 11:52:48 -05001417 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05001418 * We're accessing css_set_count without locking css_set_rwsem here,
Tejun Heod427dfe2014-02-11 11:52:48 -05001419 * but that's OK - it can only be increased by someone holding
1420 * cgroup_lock, and that's us. The worst that can happen is that we
1421 * have some link structures left over
1422 */
1423 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1424 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001425 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001426
1427 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1428 ret = cgroup_init_root_id(root, 2, 0);
1429 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001430 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001431
Tejun Heo2bd59d42014-02-11 11:52:49 -05001432 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1433 KERNFS_ROOT_CREATE_DEACTIVATED,
1434 root_cgrp);
1435 if (IS_ERR(root->kf_root)) {
1436 ret = PTR_ERR(root->kf_root);
1437 goto exit_root_id;
1438 }
1439 root_cgrp->kn = root->kf_root->kn;
Tejun Heod427dfe2014-02-11 11:52:48 -05001440
1441 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1442 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001443 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001444
Tejun Heo35585572014-02-13 06:58:38 -05001445 ret = rebind_subsystems(root, ss_mask, 0);
Tejun Heod427dfe2014-02-11 11:52:48 -05001446 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001447 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001448
1449 /*
1450 * There must be no failure case after here, since rebinding takes
1451 * care of subsystems' refcounts, which are explicitly dropped in
1452 * the failure exit path.
1453 */
1454 list_add(&root->root_list, &cgroup_roots);
1455 cgroup_root_count++;
1456
1457 /*
1458 * Link the top cgroup in this hierarchy into all the css_set
1459 * objects.
1460 */
Tejun Heo96d365e2014-02-13 06:58:40 -05001461 down_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001462 hash_for_each(css_set_table, i, cset, hlist)
1463 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo96d365e2014-02-13 06:58:40 -05001464 up_write(&css_set_rwsem);
Tejun Heod427dfe2014-02-11 11:52:48 -05001465
1466 BUG_ON(!list_empty(&root_cgrp->children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05001467 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05001468
Tejun Heo2bd59d42014-02-11 11:52:49 -05001469 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001470 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001471 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001472
Tejun Heo2bd59d42014-02-11 11:52:49 -05001473destroy_root:
1474 kernfs_destroy_root(root->kf_root);
1475 root->kf_root = NULL;
1476exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001477 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001478out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001479 free_cgrp_cset_links(&tmp_links);
1480 return ret;
1481}
1482
Al Virof7e83572010-07-26 13:23:11 +04001483static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001484 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001485 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001487 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001488 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001489 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001490 int ret;
Tejun Heo56fde9e2014-02-13 06:58:38 -05001491
1492 /*
1493 * The first time anyone tries to mount a cgroup, enable the list
1494 * linking each css_set to its tasks and fix up all existing tasks.
1495 */
1496 if (!use_task_css_set_links)
1497 cgroup_enable_task_cg_lists();
Tejun Heo776f02f2014-02-12 09:29:50 -05001498retry:
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001499 mutex_lock(&cgroup_tree_mutex);
1500 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001501
1502 /* First find the desired set of subsystems */
1503 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001504 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001505 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001506
Tejun Heo2bd59d42014-02-11 11:52:49 -05001507 /* look for a matching existing root */
1508 for_each_active_root(root) {
1509 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510
Paul Menagec6d57f32009-09-23 15:56:19 -07001511 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001512 * If we asked for a name then it must match. Also, if
1513 * name matches but sybsys_mask doesn't, we should fail.
1514 * Remember whether name matched.
Paul Menagec6d57f32009-09-23 15:56:19 -07001515 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001516 if (opts.name) {
1517 if (strcmp(opts.name, root->name))
1518 continue;
1519 name_match = true;
1520 }
1521
1522 /*
1523 * If we asked for subsystems (or explicitly for no
1524 * subsystems) then they must match.
1525 */
1526 if ((opts.subsys_mask || opts.none) &&
1527 (opts.subsys_mask != root->subsys_mask)) {
1528 if (!name_match)
1529 continue;
1530 ret = -EBUSY;
1531 goto out_unlock;
1532 }
Tejun Heo873fe092013-04-14 20:15:26 -07001533
Tejun Heoc7ba8282013-06-29 14:06:10 -07001534 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001535 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1536 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1537 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001538 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001539 } else {
1540 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1541 }
Tejun Heo873fe092013-04-14 20:15:26 -07001542 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001543
Tejun Heo776f02f2014-02-12 09:29:50 -05001544 /*
1545 * A root's lifetime is governed by its top cgroup. Zero
1546 * ref indicate that the root is being destroyed. Wait for
1547 * destruction to complete so that the subsystems are free.
1548 * We can use wait_queue for the wait but this path is
1549 * super cold. Let's just sleep for a bit and retry.
1550 */
1551 if (!atomic_inc_not_zero(&root->top_cgroup.refcnt)) {
1552 mutex_unlock(&cgroup_mutex);
1553 mutex_unlock(&cgroup_tree_mutex);
Li Zefan6534fd62014-02-14 16:55:04 +08001554 kfree(opts.release_agent);
1555 kfree(opts.name);
Tejun Heo776f02f2014-02-12 09:29:50 -05001556 msleep(10);
1557 goto retry;
1558 }
1559
1560 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001561 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 }
1563
Tejun Heo2bd59d42014-02-11 11:52:49 -05001564 /* no such thing, create a new one */
1565 root = cgroup_root_from_opts(&opts);
1566 if (IS_ERR(root)) {
1567 ret = PTR_ERR(root);
1568 goto out_unlock;
1569 }
1570
Tejun Heo35585572014-02-13 06:58:38 -05001571 ret = cgroup_setup_root(root, opts.subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001572 if (ret)
1573 cgroup_free_root(root);
1574
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001575out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001576 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001577 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001578
Paul Menagec6d57f32009-09-23 15:56:19 -07001579 kfree(opts.release_agent);
1580 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001581
Tejun Heo2bd59d42014-02-11 11:52:49 -05001582 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001583 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001584
1585 dentry = kernfs_mount(fs_type, flags, root->kf_root);
1586 if (IS_ERR(dentry))
Tejun Heo776f02f2014-02-12 09:29:50 -05001587 cgroup_put(&root->top_cgroup);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001588 return dentry;
1589}
1590
1591static void cgroup_kill_sb(struct super_block *sb)
1592{
1593 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1594 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
1595
Tejun Heo776f02f2014-02-12 09:29:50 -05001596 cgroup_put(&root->top_cgroup);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001597 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598}
1599
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600static struct file_system_type cgroup_fs_type = {
1601 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001602 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603 .kill_sb = cgroup_kill_sb,
1604};
1605
Greg KH676db4a2010-08-05 13:53:35 -07001606static struct kobject *cgroup_kobj;
1607
Li Zefana043e3b2008-02-23 15:24:09 -08001608/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001609 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001610 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001611 * @buf: the buffer to write the path into
1612 * @buflen: the length of the buffer
1613 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001614 * Determine @task's cgroup on the first (the one with the lowest non-zero
1615 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1616 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1617 * cgroup controller callbacks.
1618 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001619 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001620 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001621char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001622{
1623 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001624 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001625 int hierarchy_id = 1;
1626 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001627
1628 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05001629 down_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001630
Tejun Heo913ffdb2013-07-11 16:34:48 -07001631 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1632
Tejun Heo857a2be2013-04-14 20:50:08 -07001633 if (root) {
1634 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001635 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001636 } else {
1637 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001638 if (strlcpy(buf, "/", buflen) < buflen)
1639 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001640 }
1641
Tejun Heo96d365e2014-02-13 06:58:40 -05001642 up_read(&css_set_rwsem);
Tejun Heo857a2be2013-04-14 20:50:08 -07001643 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001644 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001645}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001646EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001647
Tejun Heob3dc0942014-02-25 10:04:01 -05001648/* used to track tasks and other necessary states during migration */
Tejun Heo2f7ee562011-12-12 18:12:21 -08001649struct cgroup_taskset {
Tejun Heob3dc0942014-02-25 10:04:01 -05001650 /* the src and dst cset list running through cset->mg_node */
1651 struct list_head src_csets;
1652 struct list_head dst_csets;
1653
1654 /*
1655 * Fields for cgroup_taskset_*() iteration.
1656 *
1657 * Before migration is committed, the target migration tasks are on
1658 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
1659 * the csets on ->dst_csets. ->csets point to either ->src_csets
1660 * or ->dst_csets depending on whether migration is committed.
1661 *
1662 * ->cur_csets and ->cur_task point to the current task position
1663 * during iteration.
1664 */
1665 struct list_head *csets;
1666 struct css_set *cur_cset;
1667 struct task_struct *cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001668};
1669
1670/**
1671 * cgroup_taskset_first - reset taskset and return the first task
1672 * @tset: taskset of interest
1673 *
1674 * @tset iteration is initialized and the first task is returned.
1675 */
1676struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1677{
Tejun Heob3dc0942014-02-25 10:04:01 -05001678 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1679 tset->cur_task = NULL;
1680
1681 return cgroup_taskset_next(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001682}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001683
1684/**
1685 * cgroup_taskset_next - iterate to the next task in taskset
1686 * @tset: taskset of interest
1687 *
1688 * Return the next task in @tset. Iteration must have been initialized
1689 * with cgroup_taskset_first().
1690 */
1691struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1692{
Tejun Heob3dc0942014-02-25 10:04:01 -05001693 struct css_set *cset = tset->cur_cset;
1694 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001695
Tejun Heob3dc0942014-02-25 10:04:01 -05001696 while (&cset->mg_node != tset->csets) {
1697 if (!task)
1698 task = list_first_entry(&cset->mg_tasks,
1699 struct task_struct, cg_list);
1700 else
1701 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001702
Tejun Heob3dc0942014-02-25 10:04:01 -05001703 if (&task->cg_list != &cset->mg_tasks) {
1704 tset->cur_cset = cset;
1705 tset->cur_task = task;
1706 return task;
1707 }
1708
1709 cset = list_next_entry(cset, mg_node);
1710 task = NULL;
1711 }
1712
1713 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001714}
Tejun Heo2f7ee562011-12-12 18:12:21 -08001715
1716/**
Ben Blum74a11662011-05-26 16:25:20 -07001717 * cgroup_task_migrate - move a task from one cgroup to another.
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001718 * @old_cgrp; the cgroup @tsk is being migrated from
1719 * @tsk: the task being migrated
1720 * @new_cset: the new css_set @tsk is being attached to
Ben Blum74a11662011-05-26 16:25:20 -07001721 *
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001722 * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
Ben Blum74a11662011-05-26 16:25:20 -07001723 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001724static void cgroup_task_migrate(struct cgroup *old_cgrp,
1725 struct task_struct *tsk,
1726 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001727{
Tejun Heo5abb8852013-06-12 21:04:49 -07001728 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001729
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001730 lockdep_assert_held(&cgroup_mutex);
1731 lockdep_assert_held(&css_set_rwsem);
1732
Ben Blum74a11662011-05-26 16:25:20 -07001733 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001734 * We are synchronized through threadgroup_lock() against PF_EXITING
1735 * setting such that we can't race against cgroup_exit() changing the
1736 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001737 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001738 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001739 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001740
Tejun Heob3dc0942014-02-25 10:04:01 -05001741 get_css_set(new_cset);
1742
Ben Blum74a11662011-05-26 16:25:20 -07001743 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001744 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001745 task_unlock(tsk);
1746
Tejun Heob3dc0942014-02-25 10:04:01 -05001747 list_move(&tsk->cg_list, &new_cset->mg_tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001748
1749 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001750 * We just gained a reference on old_cset by taking it from the
1751 * task. As trading it for new_cset is protected by cgroup_mutex,
1752 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001753 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001754 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001755 put_css_set_locked(old_cset, false);
Ben Blum74a11662011-05-26 16:25:20 -07001756}
1757
Li Zefana043e3b2008-02-23 15:24:09 -08001758/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05001759 * cgroup_migrate_finish - cleanup after attach
1760 * @preloaded_csets: list of preloaded css_sets
Ben Blum74a11662011-05-26 16:25:20 -07001761 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05001762 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
1763 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07001764 */
Tejun Heo1958d2d2014-02-25 10:04:03 -05001765static void cgroup_migrate_finish(struct list_head *preloaded_csets)
1766{
1767 struct css_set *cset, *tmp_cset;
1768
1769 lockdep_assert_held(&cgroup_mutex);
1770
1771 down_write(&css_set_rwsem);
1772 list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
1773 cset->mg_src_cgrp = NULL;
1774 cset->mg_dst_cset = NULL;
1775 list_del_init(&cset->mg_preload_node);
1776 put_css_set_locked(cset, false);
1777 }
1778 up_write(&css_set_rwsem);
1779}
1780
1781/**
1782 * cgroup_migrate_add_src - add a migration source css_set
1783 * @src_cset: the source css_set to add
1784 * @dst_cgrp: the destination cgroup
1785 * @preloaded_csets: list of preloaded css_sets
1786 *
1787 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
1788 * @src_cset and add it to @preloaded_csets, which should later be cleaned
1789 * up by cgroup_migrate_finish().
1790 *
1791 * This function may be called without holding threadgroup_lock even if the
1792 * target is a process. Threads may be created and destroyed but as long
1793 * as cgroup_mutex is not dropped, no new css_set can be put into play and
1794 * the preloaded css_sets are guaranteed to cover all migrations.
1795 */
1796static void cgroup_migrate_add_src(struct css_set *src_cset,
1797 struct cgroup *dst_cgrp,
1798 struct list_head *preloaded_csets)
1799{
1800 struct cgroup *src_cgrp;
1801
1802 lockdep_assert_held(&cgroup_mutex);
1803 lockdep_assert_held(&css_set_rwsem);
1804
1805 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
1806
1807 /* nothing to do if this cset already belongs to the cgroup */
1808 if (src_cgrp == dst_cgrp)
1809 return;
1810
1811 if (!list_empty(&src_cset->mg_preload_node))
1812 return;
1813
1814 WARN_ON(src_cset->mg_src_cgrp);
1815 WARN_ON(!list_empty(&src_cset->mg_tasks));
1816 WARN_ON(!list_empty(&src_cset->mg_node));
1817
1818 src_cset->mg_src_cgrp = src_cgrp;
1819 get_css_set(src_cset);
1820 list_add(&src_cset->mg_preload_node, preloaded_csets);
1821}
1822
1823/**
1824 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
1825 * @dst_cgrp: the destination cgroup
1826 * @preloaded_csets: list of preloaded source css_sets
1827 *
1828 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
1829 * have been preloaded to @preloaded_csets. This function looks up and
1830 * pins all destination css_sets, links each to its source, and put them on
1831 * @preloaded_csets.
1832 *
1833 * This function must be called after cgroup_migrate_add_src() has been
1834 * called on each migration source css_set. After migration is performed
1835 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
1836 * @preloaded_csets.
1837 */
1838static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
1839 struct list_head *preloaded_csets)
1840{
1841 LIST_HEAD(csets);
1842 struct css_set *src_cset;
1843
1844 lockdep_assert_held(&cgroup_mutex);
1845
1846 /* look up the dst cset for each src cset and link it to src */
1847 list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) {
1848 struct css_set *dst_cset;
1849
1850 dst_cset = find_css_set(src_cset, dst_cgrp);
1851 if (!dst_cset)
1852 goto err;
1853
1854 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
1855 src_cset->mg_dst_cset = dst_cset;
1856
1857 if (list_empty(&dst_cset->mg_preload_node))
1858 list_add(&dst_cset->mg_preload_node, &csets);
1859 else
1860 put_css_set(dst_cset, false);
1861 }
1862
1863 list_splice(&csets, preloaded_csets);
1864 return 0;
1865err:
1866 cgroup_migrate_finish(&csets);
1867 return -ENOMEM;
1868}
1869
1870/**
1871 * cgroup_migrate - migrate a process or task to a cgroup
1872 * @cgrp: the destination cgroup
1873 * @leader: the leader of the process or the task to migrate
1874 * @threadgroup: whether @leader points to the whole process or a single task
1875 *
1876 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
1877 * process, the caller must be holding threadgroup_lock of @leader. The
1878 * caller is also responsible for invoking cgroup_migrate_add_src() and
1879 * cgroup_migrate_prepare_dst() on the targets before invoking this
1880 * function and following up with cgroup_migrate_finish().
1881 *
1882 * As long as a controller's ->can_attach() doesn't fail, this function is
1883 * guaranteed to succeed. This means that, excluding ->can_attach()
1884 * failure, when migrating multiple targets, the success or failure can be
1885 * decided for all targets by invoking group_migrate_prepare_dst() before
1886 * actually starting migrating.
1887 */
1888static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
1889 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001890{
Tejun Heob3dc0942014-02-25 10:04:01 -05001891 struct cgroup_taskset tset = {
1892 .src_csets = LIST_HEAD_INIT(tset.src_csets),
1893 .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
1894 .csets = &tset.src_csets,
1895 };
Tejun Heo1c6727a2013-12-06 15:11:56 -05001896 struct cgroup_subsys_state *css, *failed_css = NULL;
Tejun Heob3dc0942014-02-25 10:04:01 -05001897 struct css_set *cset, *tmp_cset;
1898 struct task_struct *task, *tmp_task;
1899 int i, ret;
Ben Blum74a11662011-05-26 16:25:20 -07001900
1901 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001902 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1903 * already PF_EXITING could be freed from underneath us unless we
1904 * take an rcu_read_lock.
1905 */
Tejun Heob3dc0942014-02-25 10:04:01 -05001906 down_write(&css_set_rwsem);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001907 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05001908 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07001909 do {
Tejun Heo9db8de32014-02-13 06:58:43 -05001910 /* @task either already exited or can't exit until the end */
1911 if (task->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001912 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001913
Tejun Heob3dc0942014-02-25 10:04:01 -05001914 cset = task_css_set(task);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001915 if (!cset->mg_src_cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001916 goto next;
Tejun Heob3dc0942014-02-25 10:04:01 -05001917
Tejun Heob3dc0942014-02-25 10:04:01 -05001918 list_move(&task->cg_list, &cset->mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05001919 list_move(&cset->mg_node, &tset.src_csets);
1920 list_move(&cset->mg_dst_cset->mg_node, &tset.dst_csets);
Anjana V Kumarea847532013-10-12 10:59:17 +08001921 next:
Li Zefan081aa452013-03-13 09:17:09 +08001922 if (!threadgroup)
1923 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05001924 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001925 rcu_read_unlock();
Tejun Heob3dc0942014-02-25 10:04:01 -05001926 up_write(&css_set_rwsem);
Ben Blum74a11662011-05-26 16:25:20 -07001927
Tejun Heo134d3372011-12-12 18:12:21 -08001928 /* methods shouldn't be called if no task is actually migrating */
Tejun Heob3dc0942014-02-25 10:04:01 -05001929 if (list_empty(&tset.src_csets))
1930 return 0;
Tejun Heo134d3372011-12-12 18:12:21 -08001931
Tejun Heo1958d2d2014-02-25 10:04:03 -05001932 /* check that we can legitimately attach to the cgroup */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001933 for_each_css(css, i, cgrp) {
1934 if (css->ss->can_attach) {
Tejun Heo9db8de32014-02-13 06:58:43 -05001935 ret = css->ss->can_attach(css, &tset);
1936 if (ret) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001937 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001938 goto out_cancel_attach;
1939 }
1940 }
Ben Blum74a11662011-05-26 16:25:20 -07001941 }
1942
1943 /*
Tejun Heo1958d2d2014-02-25 10:04:03 -05001944 * Now that we're guaranteed success, proceed to move all tasks to
1945 * the new cgroup. There are no failure cases after here, so this
1946 * is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001947 */
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001948 down_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05001949 list_for_each_entry(cset, &tset.src_csets, mg_node) {
1950 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
1951 cgroup_task_migrate(cset->mg_src_cgrp, task,
1952 cset->mg_dst_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001953 }
Tejun Heocb0f1fe2014-02-13 06:58:41 -05001954 up_write(&css_set_rwsem);
Tejun Heob3dc0942014-02-25 10:04:01 -05001955
Tejun Heo1958d2d2014-02-25 10:04:03 -05001956 /*
1957 * Migration is committed, all target tasks are now on dst_csets.
1958 * Nothing is sensitive to fork() after this point. Notify
1959 * controllers that migration is complete.
1960 */
Tejun Heob3dc0942014-02-25 10:04:01 -05001961 tset.csets = &tset.dst_csets;
1962
Tejun Heo1c6727a2013-12-06 15:11:56 -05001963 for_each_css(css, i, cgrp)
1964 if (css->ss->attach)
1965 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001966
Tejun Heo9db8de32014-02-13 06:58:43 -05001967 ret = 0;
Tejun Heob3dc0942014-02-25 10:04:01 -05001968 goto out_release_tset;
1969
Ben Blum74a11662011-05-26 16:25:20 -07001970out_cancel_attach:
Tejun Heob3dc0942014-02-25 10:04:01 -05001971 for_each_css(css, i, cgrp) {
1972 if (css == failed_css)
1973 break;
1974 if (css->ss->cancel_attach)
1975 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001976 }
Tejun Heob3dc0942014-02-25 10:04:01 -05001977out_release_tset:
1978 down_write(&css_set_rwsem);
1979 list_splice_init(&tset.dst_csets, &tset.src_csets);
1980 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
1981 list_splice_init(&cset->mg_tasks, &cset->tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05001982 list_del_init(&cset->mg_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05001983 }
1984 up_write(&css_set_rwsem);
Tejun Heo9db8de32014-02-13 06:58:43 -05001985 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07001986}
1987
Tejun Heo1958d2d2014-02-25 10:04:03 -05001988/**
1989 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1990 * @dst_cgrp: the cgroup to attach to
1991 * @leader: the task or the leader of the threadgroup to be attached
1992 * @threadgroup: attach the whole threadgroup?
1993 *
1994 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1995 * task_lock of @tsk or each thread in the threadgroup individually in turn.
1996 */
1997static int cgroup_attach_task(struct cgroup *dst_cgrp,
1998 struct task_struct *leader, bool threadgroup)
1999{
2000 LIST_HEAD(preloaded_csets);
2001 struct task_struct *task;
2002 int ret;
2003
2004 /* look up all src csets */
2005 down_read(&css_set_rwsem);
2006 rcu_read_lock();
2007 task = leader;
2008 do {
2009 cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
2010 &preloaded_csets);
2011 if (!threadgroup)
2012 break;
2013 } while_each_thread(leader, task);
2014 rcu_read_unlock();
2015 up_read(&css_set_rwsem);
2016
2017 /* prepare dst csets and commit */
2018 ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2019 if (!ret)
2020 ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
2021
2022 cgroup_migrate_finish(&preloaded_csets);
2023 return ret;
2024}
2025
Ben Blum74a11662011-05-26 16:25:20 -07002026/*
2027 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002028 * function to attach either it or all tasks in its threadgroup. Will lock
2029 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002030 */
2031static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002032{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002033 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002034 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002035 int ret;
2036
Ben Blum74a11662011-05-26 16:25:20 -07002037 if (!cgroup_lock_live_group(cgrp))
2038 return -ENODEV;
2039
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002040retry_find_task:
2041 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002042 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002043 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002044 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002045 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002046 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002047 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002048 }
Ben Blum74a11662011-05-26 16:25:20 -07002049 /*
2050 * even if we're attaching all tasks in the thread group, we
2051 * only need to check permissions on one of them.
2052 */
David Howellsc69e8d92008-11-14 10:39:19 +11002053 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002054 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2055 !uid_eq(cred->euid, tcred->uid) &&
2056 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002057 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002058 ret = -EACCES;
2059 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002060 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002061 } else
2062 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002063
2064 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002065 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002066
2067 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002068 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002069 * trapped in a cpuset, or RT worker may be born in a cgroup
2070 * with no rt_runtime allocated. Just say no.
2071 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002072 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002073 ret = -EINVAL;
2074 rcu_read_unlock();
2075 goto out_unlock_cgroup;
2076 }
2077
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002078 get_task_struct(tsk);
2079 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002080
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002081 threadgroup_lock(tsk);
2082 if (threadgroup) {
2083 if (!thread_group_leader(tsk)) {
2084 /*
2085 * a race with de_thread from another thread's exec()
2086 * may strip us of our leadership, if this happens,
2087 * there is no choice but to throw this task away and
2088 * try again; this is
2089 * "double-double-toil-and-trouble-check locking".
2090 */
2091 threadgroup_unlock(tsk);
2092 put_task_struct(tsk);
2093 goto retry_find_task;
2094 }
Li Zefan081aa452013-03-13 09:17:09 +08002095 }
2096
2097 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2098
Tejun Heocd3d0952011-12-12 18:12:21 -08002099 threadgroup_unlock(tsk);
2100
Paul Menagebbcb81d2007-10-18 23:39:32 -07002101 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002102out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002103 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002104 return ret;
2105}
2106
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002107/**
2108 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2109 * @from: attach to all cgroups of a given task
2110 * @tsk: the task to be attached
2111 */
2112int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2113{
2114 struct cgroupfs_root *root;
2115 int retval = 0;
2116
Tejun Heo47cfcd02013-04-07 09:29:51 -07002117 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002118 for_each_active_root(root) {
Tejun Heo96d365e2014-02-13 06:58:40 -05002119 struct cgroup *from_cgrp;
2120
2121 down_read(&css_set_rwsem);
2122 from_cgrp = task_cgroup_from_root(from, root);
2123 up_read(&css_set_rwsem);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002124
Li Zefan6f4b7e62013-07-31 16:18:36 +08002125 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002126 if (retval)
2127 break;
2128 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002129 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002130
2131 return retval;
2132}
2133EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2134
Tejun Heo182446d2013-08-08 20:11:24 -04002135static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2136 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002137{
Tejun Heo182446d2013-08-08 20:11:24 -04002138 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002139}
2140
Tejun Heo182446d2013-08-08 20:11:24 -04002141static int cgroup_procs_write(struct cgroup_subsys_state *css,
2142 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002143{
Tejun Heo182446d2013-08-08 20:11:24 -04002144 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002145}
2146
Tejun Heo182446d2013-08-08 20:11:24 -04002147static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2148 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002149{
Tejun Heo5f469902014-02-11 11:52:48 -05002150 struct cgroupfs_root *root = css->cgroup->root;
2151
2152 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002153 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002154 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002155 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002156 strlcpy(root->release_agent_path, buffer,
2157 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002158 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002159 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002160 return 0;
2161}
2162
Tejun Heo2da8ca82013-12-05 12:28:04 -05002163static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002164{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002165 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002166
Paul Menagee788e062008-07-25 01:46:59 -07002167 if (!cgroup_lock_live_group(cgrp))
2168 return -ENODEV;
2169 seq_puts(seq, cgrp->root->release_agent_path);
2170 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002171 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002172 return 0;
2173}
2174
Tejun Heo2da8ca82013-12-05 12:28:04 -05002175static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002176{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002177 struct cgroup *cgrp = seq_css(seq)->cgroup;
2178
2179 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002180 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002181}
2182
Tejun Heo2bd59d42014-02-11 11:52:49 -05002183static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2184 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002185{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002186 struct cgroup *cgrp = of->kn->parent->priv;
2187 struct cftype *cft = of->kn->priv;
2188 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002189 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002190
Tejun Heo2bd59d42014-02-11 11:52:49 -05002191 /*
2192 * kernfs guarantees that a file isn't deleted with operations in
2193 * flight, which means that the matching css is and stays alive and
2194 * doesn't need to be pinned. The RCU locking is not necessary
2195 * either. It's just for the convenience of using cgroup_css().
2196 */
2197 rcu_read_lock();
2198 css = cgroup_css(cgrp, cft->ss);
2199 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002200
Tejun Heoa742c592013-12-05 12:28:03 -05002201 if (cft->write_string) {
2202 ret = cft->write_string(css, cft, strstrip(buf));
2203 } else if (cft->write_u64) {
2204 unsigned long long v;
2205 ret = kstrtoull(buf, 0, &v);
2206 if (!ret)
2207 ret = cft->write_u64(css, cft, v);
2208 } else if (cft->write_s64) {
2209 long long v;
2210 ret = kstrtoll(buf, 0, &v);
2211 if (!ret)
2212 ret = cft->write_s64(css, cft, v);
2213 } else if (cft->trigger) {
2214 ret = cft->trigger(css, (unsigned int)cft->private);
2215 } else {
2216 ret = -EINVAL;
2217 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002218
Tejun Heoa742c592013-12-05 12:28:03 -05002219 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002220}
2221
Tejun Heo6612f052013-12-05 12:28:04 -05002222static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002223{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002224 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002225}
2226
2227static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2228{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002229 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002230}
2231
2232static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2233{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002234 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002235}
2236
2237static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2238{
Tejun Heo7da11272013-12-05 12:28:04 -05002239 struct cftype *cft = seq_cft(m);
2240 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002241
Tejun Heo2da8ca82013-12-05 12:28:04 -05002242 if (cft->seq_show)
2243 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002244
Tejun Heo896f5192013-12-05 12:28:04 -05002245 if (cft->read_u64)
2246 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2247 else if (cft->read_s64)
2248 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2249 else
2250 return -EINVAL;
2251 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002252}
2253
Tejun Heo2bd59d42014-02-11 11:52:49 -05002254static struct kernfs_ops cgroup_kf_single_ops = {
2255 .atomic_write_len = PAGE_SIZE,
2256 .write = cgroup_file_write,
2257 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002258};
2259
Tejun Heo2bd59d42014-02-11 11:52:49 -05002260static struct kernfs_ops cgroup_kf_ops = {
2261 .atomic_write_len = PAGE_SIZE,
2262 .write = cgroup_file_write,
2263 .seq_start = cgroup_seqfile_start,
2264 .seq_next = cgroup_seqfile_next,
2265 .seq_stop = cgroup_seqfile_stop,
2266 .seq_show = cgroup_seqfile_show,
2267};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002268
2269/*
2270 * cgroup_rename - Only allow simple rename of directories in place.
2271 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002272static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2273 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002274{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002275 struct cgroup *cgrp = kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002276 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002277
Tejun Heo2bd59d42014-02-11 11:52:49 -05002278 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002279 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002280 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002281 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002282
Tejun Heo6db8e852013-06-14 11:18:22 -07002283 /*
2284 * This isn't a proper migration and its usefulness is very
2285 * limited. Disallow if sane_behavior.
2286 */
2287 if (cgroup_sane_behavior(cgrp))
2288 return -EPERM;
2289
Tejun Heo2bd59d42014-02-11 11:52:49 -05002290 mutex_lock(&cgroup_tree_mutex);
2291 mutex_lock(&cgroup_mutex);
2292
2293 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002294
Tejun Heo2bd59d42014-02-11 11:52:49 -05002295 mutex_unlock(&cgroup_mutex);
2296 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002297 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002298}
2299
Tejun Heo2bb566c2013-08-08 20:11:23 -04002300static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002301{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002302 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002303 struct kernfs_node *kn;
2304 struct lock_class_key *key = NULL;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002305
Tejun Heo2bd59d42014-02-11 11:52:49 -05002306#ifdef CONFIG_DEBUG_LOCK_ALLOC
2307 key = &cft->lockdep_key;
2308#endif
2309 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2310 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2311 NULL, false, key);
Fengguang Wu430af8ad2014-02-13 16:42:43 -05002312 return PTR_ERR_OR_ZERO(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002313}
2314
Tejun Heob1f28d32013-06-28 16:24:10 -07002315/**
2316 * cgroup_addrm_files - add or remove files to a cgroup directory
2317 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002318 * @cfts: array of cftypes to be added
2319 * @is_add: whether to add or remove
2320 *
2321 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002322 * For removals, this function never fails. If addition fails, this
2323 * function doesn't remove files already added. The caller is responsible
2324 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002325 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002326static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2327 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002328{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002329 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002330 int ret;
2331
Tejun Heoace2bee2014-02-11 11:52:47 -05002332 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002333
2334 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002335 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002336 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2337 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002338 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2339 continue;
2340 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2341 continue;
2342
Li Zefan2739d3c2013-01-21 18:18:33 +08002343 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002344 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002345 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002346 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002347 cft->name, ret);
2348 return ret;
2349 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002350 } else {
2351 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002352 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002353 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002354 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002355}
2356
Tejun Heo21a2d3432014-02-12 09:29:49 -05002357static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002358{
2359 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002360 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002361 struct cgroup *root = &ss->root->top_cgroup;
Tejun Heo492eb212013-08-08 20:11:25 -04002362 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002363 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002364
Tejun Heo21a2d3432014-02-12 09:29:49 -05002365 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo4ac06012014-02-11 11:52:47 -05002366
Tejun Heo21a2d3432014-02-12 09:29:49 -05002367 /* don't bother if @ss isn't attached */
2368 if (ss->root == &cgroup_dummy_root)
Tejun Heo9ccece82013-06-28 16:24:11 -07002369 return 0;
Li Zefane8c82d22013-06-18 18:48:37 +08002370
Li Zefane8c82d22013-06-18 18:48:37 +08002371 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002372 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002373 struct cgroup *cgrp = css->cgroup;
2374
Li Zefane8c82d22013-06-18 18:48:37 +08002375 if (cgroup_is_dead(cgrp))
2376 continue;
2377
Tejun Heo21a2d3432014-02-12 09:29:49 -05002378 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002379 if (ret)
2380 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002381 }
Tejun Heo21a2d3432014-02-12 09:29:49 -05002382
2383 if (is_add && !ret)
2384 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002385 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002386}
2387
Tejun Heo2da440a2014-02-11 11:52:48 -05002388static void cgroup_exit_cftypes(struct cftype *cfts)
2389{
2390 struct cftype *cft;
2391
Tejun Heo2bd59d42014-02-11 11:52:49 -05002392 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2393 /* free copy for custom atomic_write_len, see init_cftypes() */
2394 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2395 kfree(cft->kf_ops);
2396 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002397 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002398 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002399}
2400
Tejun Heo2bd59d42014-02-11 11:52:49 -05002401static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002402{
2403 struct cftype *cft;
2404
Tejun Heo2bd59d42014-02-11 11:52:49 -05002405 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2406 struct kernfs_ops *kf_ops;
2407
Tejun Heo0adb0702014-02-12 09:29:48 -05002408 WARN_ON(cft->ss || cft->kf_ops);
2409
Tejun Heo2bd59d42014-02-11 11:52:49 -05002410 if (cft->seq_start)
2411 kf_ops = &cgroup_kf_ops;
2412 else
2413 kf_ops = &cgroup_kf_single_ops;
2414
2415 /*
2416 * Ugh... if @cft wants a custom max_write_len, we need to
2417 * make a copy of kf_ops to set its atomic_write_len.
2418 */
2419 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2420 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2421 if (!kf_ops) {
2422 cgroup_exit_cftypes(cfts);
2423 return -ENOMEM;
2424 }
2425 kf_ops->atomic_write_len = cft->max_write_len;
2426 }
2427
2428 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002429 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002430 }
2431
2432 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002433}
2434
Tejun Heo21a2d3432014-02-12 09:29:49 -05002435static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2436{
2437 lockdep_assert_held(&cgroup_tree_mutex);
2438
2439 if (!cfts || !cfts[0].ss)
2440 return -ENOENT;
2441
2442 list_del(&cfts->node);
2443 cgroup_apply_cftypes(cfts, false);
2444 cgroup_exit_cftypes(cfts);
2445 return 0;
2446}
2447
Tejun Heo8e3f6542012-04-01 12:09:55 -07002448/**
Tejun Heo80b13582014-02-12 09:29:48 -05002449 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2450 * @cfts: zero-length name terminated array of cftypes
2451 *
2452 * Unregister @cfts. Files described by @cfts are removed from all
2453 * existing cgroups and all future cgroups won't have them either. This
2454 * function can be called anytime whether @cfts' subsys is attached or not.
2455 *
2456 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2457 * registered.
2458 */
2459int cgroup_rm_cftypes(struct cftype *cfts)
2460{
Tejun Heo21a2d3432014-02-12 09:29:49 -05002461 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002462
Tejun Heo21a2d3432014-02-12 09:29:49 -05002463 mutex_lock(&cgroup_tree_mutex);
2464 ret = cgroup_rm_cftypes_locked(cfts);
2465 mutex_unlock(&cgroup_tree_mutex);
2466 return ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002467}
2468
2469/**
Tejun Heo8e3f6542012-04-01 12:09:55 -07002470 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2471 * @ss: target cgroup subsystem
2472 * @cfts: zero-length name terminated array of cftypes
2473 *
2474 * Register @cfts to @ss. Files described by @cfts are created for all
2475 * existing cgroups to which @ss is attached and all future cgroups will
2476 * have them too. This function can be called anytime whether @ss is
2477 * attached or not.
2478 *
2479 * Returns 0 on successful registration, -errno on failure. Note that this
2480 * function currently returns 0 as long as @cfts registration is successful
2481 * even if some file creation attempts on existing cgroups fail.
2482 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002483int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002484{
Tejun Heo9ccece82013-06-28 16:24:11 -07002485 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002486
Li Zefandc5736e2014-02-17 10:41:50 +08002487 if (!cfts || cfts[0].name[0] == '\0')
2488 return 0;
2489
Tejun Heo2bd59d42014-02-11 11:52:49 -05002490 ret = cgroup_init_cftypes(ss, cfts);
2491 if (ret)
2492 return ret;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002493
Tejun Heo21a2d3432014-02-12 09:29:49 -05002494 mutex_lock(&cgroup_tree_mutex);
2495
Tejun Heo0adb0702014-02-12 09:29:48 -05002496 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d3432014-02-12 09:29:49 -05002497 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002498 if (ret)
Tejun Heo21a2d3432014-02-12 09:29:49 -05002499 cgroup_rm_cftypes_locked(cfts);
2500
2501 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002502 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002503}
Tejun Heo8e3f6542012-04-01 12:09:55 -07002504
Li Zefana043e3b2008-02-23 15:24:09 -08002505/**
2506 * cgroup_task_count - count the number of tasks in a cgroup.
2507 * @cgrp: the cgroup in question
2508 *
2509 * Return the number of tasks in the cgroup.
2510 */
Tejun Heo07bc3562014-02-13 06:58:39 -05002511static int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002512{
2513 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002514 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002515
Tejun Heo96d365e2014-02-13 06:58:40 -05002516 down_read(&css_set_rwsem);
Tejun Heo69d02062013-06-12 21:04:50 -07002517 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2518 count += atomic_read(&link->cset->refcount);
Tejun Heo96d365e2014-02-13 06:58:40 -05002519 up_read(&css_set_rwsem);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002520 return count;
2521}
2522
Tejun Heo574bd9f2012-11-09 09:12:29 -08002523/**
Tejun Heo492eb212013-08-08 20:11:25 -04002524 * css_next_child - find the next child of a given css
2525 * @pos_css: the current position (%NULL to initiate traversal)
2526 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002527 *
Tejun Heo492eb212013-08-08 20:11:25 -04002528 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002529 * under either cgroup_mutex or RCU read lock. The only requirement is
2530 * that @parent_css and @pos_css are accessible. The next sibling is
2531 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002532 */
Tejun Heo492eb212013-08-08 20:11:25 -04002533struct cgroup_subsys_state *
2534css_next_child(struct cgroup_subsys_state *pos_css,
2535 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002536{
Tejun Heo492eb212013-08-08 20:11:25 -04002537 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2538 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002539 struct cgroup *next;
2540
Tejun Heoace2bee2014-02-11 11:52:47 -05002541 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002542
2543 /*
2544 * @pos could already have been removed. Once a cgroup is removed,
2545 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002546 * changes. As CGRP_DEAD assertion is serialized and happens
2547 * before the cgroup is taken off the ->sibling list, if we see it
2548 * unasserted, it's guaranteed that the next sibling hasn't
2549 * finished its grace period even if it's already removed, and thus
2550 * safe to dereference from this RCU critical section. If
2551 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2552 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002553 *
2554 * If @pos is dead, its next pointer can't be dereferenced;
2555 * however, as each cgroup is given a monotonically increasing
2556 * unique serial number and always appended to the sibling list,
2557 * the next one can be found by walking the parent's children until
2558 * we see a cgroup with higher serial number than @pos's. While
2559 * this path can be slower, it's taken only when either the current
2560 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002561 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002562 if (!pos) {
2563 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2564 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002565 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002566 } else {
2567 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2568 if (next->serial_nr > pos->serial_nr)
2569 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002570 }
2571
Tejun Heo492eb212013-08-08 20:11:25 -04002572 if (&next->sibling == &cgrp->children)
2573 return NULL;
2574
Tejun Heoca8bdca2013-08-26 18:40:56 -04002575 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002576}
Tejun Heo53fa5262013-05-24 10:55:38 +09002577
2578/**
Tejun Heo492eb212013-08-08 20:11:25 -04002579 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002580 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002581 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002582 *
Tejun Heo492eb212013-08-08 20:11:25 -04002583 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002584 * to visit for pre-order traversal of @root's descendants. @root is
2585 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002586 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002587 * While this function requires cgroup_mutex or RCU read locking, it
2588 * doesn't require the whole traversal to be contained in a single critical
2589 * section. This function will return the correct next descendant as long
2590 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002591 */
Tejun Heo492eb212013-08-08 20:11:25 -04002592struct cgroup_subsys_state *
2593css_next_descendant_pre(struct cgroup_subsys_state *pos,
2594 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002595{
Tejun Heo492eb212013-08-08 20:11:25 -04002596 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002597
Tejun Heoace2bee2014-02-11 11:52:47 -05002598 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002599
Tejun Heobd8815a2013-08-08 20:11:27 -04002600 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002601 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002602 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002603
2604 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002605 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002606 if (next)
2607 return next;
2608
2609 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002610 while (pos != root) {
2611 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002612 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002613 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002614 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002615 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002616
2617 return NULL;
2618}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002619
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002620/**
Tejun Heo492eb212013-08-08 20:11:25 -04002621 * css_rightmost_descendant - return the rightmost descendant of a css
2622 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002623 *
Tejun Heo492eb212013-08-08 20:11:25 -04002624 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2625 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002626 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002627 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002628 * While this function requires cgroup_mutex or RCU read locking, it
2629 * doesn't require the whole traversal to be contained in a single critical
2630 * section. This function will return the correct rightmost descendant as
2631 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002632 */
Tejun Heo492eb212013-08-08 20:11:25 -04002633struct cgroup_subsys_state *
2634css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002635{
Tejun Heo492eb212013-08-08 20:11:25 -04002636 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002637
Tejun Heoace2bee2014-02-11 11:52:47 -05002638 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002639
2640 do {
2641 last = pos;
2642 /* ->prev isn't RCU safe, walk ->next till the end */
2643 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002644 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002645 pos = tmp;
2646 } while (pos);
2647
2648 return last;
2649}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002650
Tejun Heo492eb212013-08-08 20:11:25 -04002651static struct cgroup_subsys_state *
2652css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002653{
Tejun Heo492eb212013-08-08 20:11:25 -04002654 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002655
2656 do {
2657 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002658 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002659 } while (pos);
2660
2661 return last;
2662}
2663
2664/**
Tejun Heo492eb212013-08-08 20:11:25 -04002665 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002666 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002667 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002668 *
Tejun Heo492eb212013-08-08 20:11:25 -04002669 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002670 * to visit for post-order traversal of @root's descendants. @root is
2671 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002672 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002673 * While this function requires cgroup_mutex or RCU read locking, it
2674 * doesn't require the whole traversal to be contained in a single critical
2675 * section. This function will return the correct next descendant as long
2676 * as both @pos and @cgroup are accessible and @pos is a descendant of
2677 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002678 */
Tejun Heo492eb212013-08-08 20:11:25 -04002679struct cgroup_subsys_state *
2680css_next_descendant_post(struct cgroup_subsys_state *pos,
2681 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002682{
Tejun Heo492eb212013-08-08 20:11:25 -04002683 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002684
Tejun Heoace2bee2014-02-11 11:52:47 -05002685 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002686
Tejun Heo58b79a92013-09-06 15:31:08 -04002687 /* if first iteration, visit leftmost descendant which may be @root */
2688 if (!pos)
2689 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002690
Tejun Heobd8815a2013-08-08 20:11:27 -04002691 /* if we visited @root, we're done */
2692 if (pos == root)
2693 return NULL;
2694
Tejun Heo574bd9f2012-11-09 09:12:29 -08002695 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002696 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002697 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002698 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002699
2700 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002701 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002702}
Tejun Heo574bd9f2012-11-09 09:12:29 -08002703
Tejun Heo0942eee2013-08-08 20:11:26 -04002704/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002705 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002706 * @it: the iterator to advance
2707 *
2708 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002709 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002710static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002711{
2712 struct list_head *l = it->cset_link;
2713 struct cgrp_cset_link *link;
2714 struct css_set *cset;
2715
2716 /* Advance to the next non-empty css_set */
2717 do {
2718 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002719 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002720 it->cset_link = NULL;
2721 return;
2722 }
2723 link = list_entry(l, struct cgrp_cset_link, cset_link);
2724 cset = link->cset;
Tejun Heoc7561122014-02-25 10:04:01 -05002725 } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
2726
Tejun Heod5158762013-08-08 20:11:26 -04002727 it->cset_link = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002728
2729 if (!list_empty(&cset->tasks))
2730 it->task = cset->tasks.next;
2731 else
2732 it->task = cset->mg_tasks.next;
Tejun Heod5158762013-08-08 20:11:26 -04002733}
2734
Tejun Heo0942eee2013-08-08 20:11:26 -04002735/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002736 * css_task_iter_start - initiate task iteration
2737 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002738 * @it: the task iterator to use
2739 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002740 * Initiate iteration through the tasks of @css. The caller can call
2741 * css_task_iter_next() to walk through the tasks until the function
2742 * returns NULL. On completion of iteration, css_task_iter_end() must be
2743 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002744 *
2745 * Note that this function acquires a lock which is released when the
2746 * iteration finishes. The caller can't sleep while iteration is in
2747 * progress.
2748 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002749void css_task_iter_start(struct cgroup_subsys_state *css,
2750 struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002751 __acquires(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002752{
Tejun Heo56fde9e2014-02-13 06:58:38 -05002753 /* no one should try to iterate before mounting cgroups */
2754 WARN_ON_ONCE(!use_task_css_set_links);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002755
Tejun Heo96d365e2014-02-13 06:58:40 -05002756 down_read(&css_set_rwsem);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002757
Tejun Heo72ec7022013-08-08 20:11:26 -04002758 it->origin_css = css;
2759 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002760
Tejun Heo72ec7022013-08-08 20:11:26 -04002761 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002762}
2763
Tejun Heo0942eee2013-08-08 20:11:26 -04002764/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002765 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002766 * @it: the task iterator being iterated
2767 *
2768 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002769 * initialized via css_task_iter_start(). Returns NULL when the iteration
2770 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002771 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002772struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002773{
2774 struct task_struct *res;
2775 struct list_head *l = it->task;
Tejun Heoc7561122014-02-25 10:04:01 -05002776 struct cgrp_cset_link *link = list_entry(it->cset_link,
2777 struct cgrp_cset_link, cset_link);
Paul Menage817929e2007-10-18 23:39:36 -07002778
2779 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002780 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002781 return NULL;
2782 res = list_entry(l, struct task_struct, cg_list);
Tejun Heoc7561122014-02-25 10:04:01 -05002783
2784 /*
2785 * Advance iterator to find next entry. cset->tasks is consumed
2786 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
2787 * next cset.
2788 */
Paul Menage817929e2007-10-18 23:39:36 -07002789 l = l->next;
Tejun Heoc7561122014-02-25 10:04:01 -05002790
2791 if (l == &link->cset->tasks)
2792 l = link->cset->mg_tasks.next;
2793
2794 if (l == &link->cset->mg_tasks)
Tejun Heo72ec7022013-08-08 20:11:26 -04002795 css_advance_task_iter(it);
Tejun Heoc7561122014-02-25 10:04:01 -05002796 else
Paul Menage817929e2007-10-18 23:39:36 -07002797 it->task = l;
Tejun Heoc7561122014-02-25 10:04:01 -05002798
Paul Menage817929e2007-10-18 23:39:36 -07002799 return res;
2800}
2801
Tejun Heo0942eee2013-08-08 20:11:26 -04002802/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002803 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002804 * @it: the task iterator to finish
2805 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002806 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002807 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002808void css_task_iter_end(struct css_task_iter *it)
Tejun Heo96d365e2014-02-13 06:58:40 -05002809 __releases(css_set_rwsem)
Paul Menage817929e2007-10-18 23:39:36 -07002810{
Tejun Heo96d365e2014-02-13 06:58:40 -05002811 up_read(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07002812}
2813
Tejun Heo8cc99342013-04-07 09:29:50 -07002814/**
2815 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2816 * @to: cgroup to which the tasks will be moved
2817 * @from: cgroup in which the tasks currently reside
2818 */
2819int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2820{
Tejun Heoe406d1c2014-02-13 06:58:39 -05002821 struct css_task_iter it;
2822 struct task_struct *task;
2823 int ret = 0;
2824
2825 do {
2826 css_task_iter_start(&from->dummy_css, &it);
2827 task = css_task_iter_next(&it);
2828 if (task)
2829 get_task_struct(task);
2830 css_task_iter_end(&it);
2831
2832 if (task) {
2833 mutex_lock(&cgroup_mutex);
2834 ret = cgroup_attach_task(to, task, false);
2835 mutex_unlock(&cgroup_mutex);
2836 put_task_struct(task);
2837 }
2838 } while (task && !ret);
2839
2840 return ret;
Tejun Heo8cc99342013-04-07 09:29:50 -07002841}
2842
Paul Menage817929e2007-10-18 23:39:36 -07002843/*
Ben Blum102a7752009-09-23 15:56:26 -07002844 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002845 *
2846 * Reading this file can return large amounts of data if a cgroup has
2847 * *lots* of attached tasks. So it may need several calls to read(),
2848 * but we cannot guarantee that the information we produce is correct
2849 * unless we produce it entirely atomically.
2850 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002851 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002852
Li Zefan24528252012-01-20 11:58:43 +08002853/* which pidlist file are we talking about? */
2854enum cgroup_filetype {
2855 CGROUP_FILE_PROCS,
2856 CGROUP_FILE_TASKS,
2857};
2858
2859/*
2860 * A pidlist is a list of pids that virtually represents the contents of one
2861 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2862 * a pair (one each for procs, tasks) for each pid namespace that's relevant
2863 * to the cgroup.
2864 */
2865struct cgroup_pidlist {
2866 /*
2867 * used to find which pidlist is wanted. doesn't change as long as
2868 * this particular list stays in the list.
2869 */
2870 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
2871 /* array of xids */
2872 pid_t *list;
2873 /* how many elements the above list has */
2874 int length;
Li Zefan24528252012-01-20 11:58:43 +08002875 /* each of these stored in a list by its cgroup */
2876 struct list_head links;
2877 /* pointer to the cgroup we belong to, for list removal purposes */
2878 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05002879 /* for delayed destruction */
2880 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08002881};
2882
Paul Menagebbcb81d2007-10-18 23:39:32 -07002883/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002884 * The following two functions "fix" the issue where there are more pids
2885 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2886 * TODO: replace with a kernel-wide solution to this problem
2887 */
2888#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2889static void *pidlist_allocate(int count)
2890{
2891 if (PIDLIST_TOO_LARGE(count))
2892 return vmalloc(count * sizeof(pid_t));
2893 else
2894 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2895}
Tejun Heob1a21362013-11-29 10:42:58 -05002896
Ben Blumd1d9fd32009-09-23 15:56:28 -07002897static void pidlist_free(void *p)
2898{
2899 if (is_vmalloc_addr(p))
2900 vfree(p);
2901 else
2902 kfree(p);
2903}
Ben Blumd1d9fd32009-09-23 15:56:28 -07002904
2905/*
Tejun Heob1a21362013-11-29 10:42:58 -05002906 * Used to destroy all pidlists lingering waiting for destroy timer. None
2907 * should be left afterwards.
2908 */
2909static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
2910{
2911 struct cgroup_pidlist *l, *tmp_l;
2912
2913 mutex_lock(&cgrp->pidlist_mutex);
2914 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
2915 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
2916 mutex_unlock(&cgrp->pidlist_mutex);
2917
2918 flush_workqueue(cgroup_pidlist_destroy_wq);
2919 BUG_ON(!list_empty(&cgrp->pidlists));
2920}
2921
2922static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
2923{
2924 struct delayed_work *dwork = to_delayed_work(work);
2925 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
2926 destroy_dwork);
2927 struct cgroup_pidlist *tofree = NULL;
2928
2929 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05002930
2931 /*
Tejun Heo04502362013-11-29 10:42:59 -05002932 * Destroy iff we didn't get queued again. The state won't change
2933 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05002934 */
Tejun Heo04502362013-11-29 10:42:59 -05002935 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05002936 list_del(&l->links);
2937 pidlist_free(l->list);
2938 put_pid_ns(l->key.ns);
2939 tofree = l;
2940 }
2941
Tejun Heob1a21362013-11-29 10:42:58 -05002942 mutex_unlock(&l->owner->pidlist_mutex);
2943 kfree(tofree);
2944}
2945
2946/*
Ben Blum102a7752009-09-23 15:56:26 -07002947 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07002948 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002949 */
Li Zefan6ee211a2013-03-12 15:36:00 -07002950static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002951{
Ben Blum102a7752009-09-23 15:56:26 -07002952 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07002953
2954 /*
2955 * we presume the 0th element is unique, so i starts at 1. trivial
2956 * edge cases first; no work needs to be done for either
2957 */
2958 if (length == 0 || length == 1)
2959 return length;
2960 /* src and dest walk down the list; dest counts unique elements */
2961 for (src = 1; src < length; src++) {
2962 /* find next unique element */
2963 while (list[src] == list[src-1]) {
2964 src++;
2965 if (src == length)
2966 goto after;
2967 }
2968 /* dest always points to where the next unique element goes */
2969 list[dest] = list[src];
2970 dest++;
2971 }
2972after:
Ben Blum102a7752009-09-23 15:56:26 -07002973 return dest;
2974}
2975
Tejun Heoafb2bc12013-11-29 10:42:59 -05002976/*
2977 * The two pid files - task and cgroup.procs - guaranteed that the result
2978 * is sorted, which forced this whole pidlist fiasco. As pid order is
2979 * different per namespace, each namespace needs differently sorted list,
2980 * making it impossible to use, for example, single rbtree of member tasks
2981 * sorted by task pointer. As pidlists can be fairly large, allocating one
2982 * per open file is dangerous, so cgroup had to implement shared pool of
2983 * pidlists keyed by cgroup and namespace.
2984 *
2985 * All this extra complexity was caused by the original implementation
2986 * committing to an entirely unnecessary property. In the long term, we
2987 * want to do away with it. Explicitly scramble sort order if
2988 * sane_behavior so that no such expectation exists in the new interface.
2989 *
2990 * Scrambling is done by swapping every two consecutive bits, which is
2991 * non-identity one-to-one mapping which disturbs sort order sufficiently.
2992 */
2993static pid_t pid_fry(pid_t pid)
2994{
2995 unsigned a = pid & 0x55555555;
2996 unsigned b = pid & 0xAAAAAAAA;
2997
2998 return (a << 1) | (b >> 1);
2999}
3000
3001static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3002{
3003 if (cgroup_sane_behavior(cgrp))
3004 return pid_fry(pid);
3005 else
3006 return pid;
3007}
3008
Ben Blum102a7752009-09-23 15:56:26 -07003009static int cmppid(const void *a, const void *b)
3010{
3011 return *(pid_t *)a - *(pid_t *)b;
3012}
3013
Tejun Heoafb2bc12013-11-29 10:42:59 -05003014static int fried_cmppid(const void *a, const void *b)
3015{
3016 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3017}
3018
Ben Blum72a8cb32009-09-23 15:56:27 -07003019static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3020 enum cgroup_filetype type)
3021{
3022 struct cgroup_pidlist *l;
3023 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003024 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003025
Tejun Heoe6b81712013-11-29 10:42:59 -05003026 lockdep_assert_held(&cgrp->pidlist_mutex);
3027
3028 list_for_each_entry(l, &cgrp->pidlists, links)
3029 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003030 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003031 return NULL;
3032}
3033
Ben Blum72a8cb32009-09-23 15:56:27 -07003034/*
3035 * find the appropriate pidlist for our purpose (given procs vs tasks)
3036 * returns with the lock on that pidlist already held, and takes care
3037 * of the use count, or returns NULL with no locks held if we're out of
3038 * memory.
3039 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003040static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3041 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003042{
3043 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003044
Tejun Heoe6b81712013-11-29 10:42:59 -05003045 lockdep_assert_held(&cgrp->pidlist_mutex);
3046
3047 l = cgroup_pidlist_find(cgrp, type);
3048 if (l)
3049 return l;
3050
Ben Blum72a8cb32009-09-23 15:56:27 -07003051 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003052 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003053 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003054 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003055
Tejun Heob1a21362013-11-29 10:42:58 -05003056 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003057 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003058 /* don't need task_nsproxy() if we're looking at ourself */
3059 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003060 l->owner = cgrp;
3061 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003062 return l;
3063}
3064
3065/*
Ben Blum102a7752009-09-23 15:56:26 -07003066 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3067 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003068static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3069 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003070{
3071 pid_t *array;
3072 int length;
3073 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003074 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003075 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003076 struct cgroup_pidlist *l;
3077
Tejun Heo4bac00d2013-11-29 10:42:59 -05003078 lockdep_assert_held(&cgrp->pidlist_mutex);
3079
Ben Blum102a7752009-09-23 15:56:26 -07003080 /*
3081 * If cgroup gets more users after we read count, we won't have
3082 * enough space - tough. This race is indistinguishable to the
3083 * caller from the case that the additional cgroup users didn't
3084 * show up until sometime later on.
3085 */
3086 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003087 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003088 if (!array)
3089 return -ENOMEM;
3090 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003091 css_task_iter_start(&cgrp->dummy_css, &it);
3092 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003093 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003094 break;
Ben Blum102a7752009-09-23 15:56:26 -07003095 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003096 if (type == CGROUP_FILE_PROCS)
3097 pid = task_tgid_vnr(tsk);
3098 else
3099 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003100 if (pid > 0) /* make sure to only use valid results */
3101 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003102 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003103 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003104 length = n;
3105 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003106 if (cgroup_sane_behavior(cgrp))
3107 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3108 else
3109 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003110 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003111 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003112
Tejun Heoe6b81712013-11-29 10:42:59 -05003113 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003114 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003115 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003116 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003117 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003118 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003119
3120 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003121 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003122 l->list = array;
3123 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003124 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003125 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003126}
3127
Balbir Singh846c7bb2007-10-18 23:39:44 -07003128/**
Li Zefana043e3b2008-02-23 15:24:09 -08003129 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003130 * @stats: cgroupstats to fill information into
3131 * @dentry: A dentry entry belonging to the cgroup for which stats have
3132 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003133 *
3134 * Build and fill cgroupstats so that taskstats can export it to user
3135 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003136 */
3137int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3138{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003139 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003140 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003141 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003142 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003143
Tejun Heo2bd59d42014-02-11 11:52:49 -05003144 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3145 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3146 kernfs_type(kn) != KERNFS_DIR)
3147 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003148
Li Zefanbad34662014-02-14 16:54:28 +08003149 mutex_lock(&cgroup_mutex);
3150
Tejun Heo2bd59d42014-02-11 11:52:49 -05003151 /*
3152 * We aren't being called from kernfs and there's no guarantee on
3153 * @kn->priv's validity. For this and css_tryget_from_dir(),
3154 * @kn->priv is RCU safe. Let's do the RCU dancing.
3155 */
3156 rcu_read_lock();
3157 cgrp = rcu_dereference(kn->priv);
Li Zefanbad34662014-02-14 16:54:28 +08003158 if (!cgrp || cgroup_is_dead(cgrp)) {
Tejun Heo2bd59d42014-02-11 11:52:49 -05003159 rcu_read_unlock();
Li Zefanbad34662014-02-14 16:54:28 +08003160 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003161 return -ENOENT;
3162 }
Li Zefanbad34662014-02-14 16:54:28 +08003163 rcu_read_unlock();
Balbir Singh846c7bb2007-10-18 23:39:44 -07003164
Tejun Heo72ec7022013-08-08 20:11:26 -04003165 css_task_iter_start(&cgrp->dummy_css, &it);
3166 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003167 switch (tsk->state) {
3168 case TASK_RUNNING:
3169 stats->nr_running++;
3170 break;
3171 case TASK_INTERRUPTIBLE:
3172 stats->nr_sleeping++;
3173 break;
3174 case TASK_UNINTERRUPTIBLE:
3175 stats->nr_uninterruptible++;
3176 break;
3177 case TASK_STOPPED:
3178 stats->nr_stopped++;
3179 break;
3180 default:
3181 if (delayacct_is_task_waiting_on_io(tsk))
3182 stats->nr_io_wait++;
3183 break;
3184 }
3185 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003186 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003187
Li Zefanbad34662014-02-14 16:54:28 +08003188 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003189 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003190}
3191
Paul Menage8f3ff202009-09-23 15:56:25 -07003192
Paul Menagecc31edc2008-10-18 20:28:04 -07003193/*
Ben Blum102a7752009-09-23 15:56:26 -07003194 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003195 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003196 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003197 */
3198
Ben Blum102a7752009-09-23 15:56:26 -07003199static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003200{
3201 /*
3202 * Initially we receive a position value that corresponds to
3203 * one more than the last pid shown (or 0 on the first call or
3204 * after a seek to the start). Use a binary-search to find the
3205 * next pid to display, if any
3206 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003207 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003208 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003209 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003210 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003211 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003212 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003213
Tejun Heo4bac00d2013-11-29 10:42:59 -05003214 mutex_lock(&cgrp->pidlist_mutex);
3215
3216 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003217 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003218 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003219 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003220 * could already have been destroyed.
3221 */
Tejun Heo5d224442013-12-05 12:28:04 -05003222 if (of->priv)
3223 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003224
3225 /*
3226 * Either this is the first start() after open or the matching
3227 * pidlist has been destroyed inbetween. Create a new one.
3228 */
Tejun Heo5d224442013-12-05 12:28:04 -05003229 if (!of->priv) {
3230 ret = pidlist_array_load(cgrp, type,
3231 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003232 if (ret)
3233 return ERR_PTR(ret);
3234 }
Tejun Heo5d224442013-12-05 12:28:04 -05003235 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003236
Paul Menagecc31edc2008-10-18 20:28:04 -07003237 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003238 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003239
Paul Menagecc31edc2008-10-18 20:28:04 -07003240 while (index < end) {
3241 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003242 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003243 index = mid;
3244 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003245 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003246 index = mid + 1;
3247 else
3248 end = mid;
3249 }
3250 }
3251 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003252 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003253 return NULL;
3254 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003255 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003256 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003257 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003258}
3259
Ben Blum102a7752009-09-23 15:56:26 -07003260static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003261{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003262 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003263 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003264
Tejun Heo5d224442013-12-05 12:28:04 -05003265 if (l)
3266 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003267 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003268 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003269}
3270
Ben Blum102a7752009-09-23 15:56:26 -07003271static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003272{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003273 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003274 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003275 pid_t *p = v;
3276 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003277 /*
3278 * Advance to the next pid in the array. If this goes off the
3279 * end, we're done
3280 */
3281 p++;
3282 if (p >= end) {
3283 return NULL;
3284 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003285 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003286 return p;
3287 }
3288}
3289
Ben Blum102a7752009-09-23 15:56:26 -07003290static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003291{
3292 return seq_printf(s, "%d\n", *(int *)v);
3293}
3294
Ben Blum102a7752009-09-23 15:56:26 -07003295/*
3296 * seq_operations functions for iterating on pidlists through seq_file -
3297 * independent of whether it's tasks or procs
3298 */
3299static const struct seq_operations cgroup_pidlist_seq_operations = {
3300 .start = cgroup_pidlist_start,
3301 .stop = cgroup_pidlist_stop,
3302 .next = cgroup_pidlist_next,
3303 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003304};
3305
Tejun Heo182446d2013-08-08 20:11:24 -04003306static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3307 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003308{
Tejun Heo182446d2013-08-08 20:11:24 -04003309 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003310}
3311
Tejun Heo182446d2013-08-08 20:11:24 -04003312static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3313 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003314{
Tejun Heo182446d2013-08-08 20:11:24 -04003315 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003316 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003317 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003318 else
Tejun Heo182446d2013-08-08 20:11:24 -04003319 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003320 return 0;
3321}
3322
Tejun Heo182446d2013-08-08 20:11:24 -04003323static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3324 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003325{
Tejun Heo182446d2013-08-08 20:11:24 -04003326 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003327}
3328
Tejun Heo182446d2013-08-08 20:11:24 -04003329static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3330 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003331{
3332 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003333 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003334 else
Tejun Heo182446d2013-08-08 20:11:24 -04003335 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003336 return 0;
3337}
3338
Tejun Heod5c56ce2013-06-03 19:14:34 -07003339static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003340 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003341 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003342 .seq_start = cgroup_pidlist_start,
3343 .seq_next = cgroup_pidlist_next,
3344 .seq_stop = cgroup_pidlist_stop,
3345 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003346 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003347 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003348 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003349 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003350 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003351 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003352 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003353 .read_u64 = cgroup_clone_children_read,
3354 .write_u64 = cgroup_clone_children_write,
3355 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003356 {
Tejun Heo873fe092013-04-14 20:15:26 -07003357 .name = "cgroup.sane_behavior",
3358 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003359 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003360 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003361
3362 /*
3363 * Historical crazy stuff. These don't have "cgroup." prefix and
3364 * don't exist if sane_behavior. If you're depending on these, be
3365 * prepared to be burned.
3366 */
3367 {
3368 .name = "tasks",
3369 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003370 .seq_start = cgroup_pidlist_start,
3371 .seq_next = cgroup_pidlist_next,
3372 .seq_stop = cgroup_pidlist_stop,
3373 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003374 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003375 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003376 .mode = S_IRUGO | S_IWUSR,
3377 },
3378 {
3379 .name = "notify_on_release",
3380 .flags = CFTYPE_INSANE,
3381 .read_u64 = cgroup_read_notify_on_release,
3382 .write_u64 = cgroup_write_notify_on_release,
3383 },
Tejun Heo873fe092013-04-14 20:15:26 -07003384 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003385 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003386 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003387 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003388 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003389 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003390 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003391 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003392};
3393
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003394/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003395 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003396 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003397 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003398 *
3399 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003400 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003401static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003402{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003403 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003404 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003405
Tejun Heo8e3f6542012-04-01 12:09:55 -07003406 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003407 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003408 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003409
3410 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003411 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003412
Tejun Heo0adb0702014-02-12 09:29:48 -05003413 list_for_each_entry(cfts, &ss->cfts, node) {
3414 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003415 if (ret < 0)
3416 goto err;
3417 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003418 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003419 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003420err:
3421 cgroup_clear_dir(cgrp, subsys_mask);
3422 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003423}
3424
Tejun Heo0c21ead2013-08-13 20:22:51 -04003425/*
3426 * css destruction is four-stage process.
3427 *
3428 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3429 * Implemented in kill_css().
3430 *
3431 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3432 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3433 * by invoking offline_css(). After offlining, the base ref is put.
3434 * Implemented in css_killed_work_fn().
3435 *
3436 * 3. When the percpu_ref reaches zero, the only possible remaining
3437 * accessors are inside RCU read sections. css_release() schedules the
3438 * RCU callback.
3439 *
3440 * 4. After the grace period, the css can be freed. Implemented in
3441 * css_free_work_fn().
3442 *
3443 * It is actually hairier because both step 2 and 4 require process context
3444 * and thus involve punting to css->destroy_work adding two additional
3445 * steps to the already complex sequence.
3446 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003447static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003448{
3449 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003450 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003451 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003452
Tejun Heo0ae78e02013-08-13 11:01:54 -04003453 if (css->parent)
3454 css_put(css->parent);
3455
Tejun Heo0c21ead2013-08-13 20:22:51 -04003456 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003457 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003458}
3459
3460static void css_free_rcu_fn(struct rcu_head *rcu_head)
3461{
3462 struct cgroup_subsys_state *css =
3463 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3464
Tejun Heo0c21ead2013-08-13 20:22:51 -04003465 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003466 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003467}
3468
Tejun Heod3daf282013-06-13 19:39:16 -07003469static void css_release(struct percpu_ref *ref)
3470{
3471 struct cgroup_subsys_state *css =
3472 container_of(ref, struct cgroup_subsys_state, refcnt);
3473
Tejun Heoaec25022014-02-08 10:36:58 -05003474 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003475 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003476}
3477
Tejun Heo623f9262013-08-13 11:01:55 -04003478static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3479 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003480{
Paul Menagebd89aab2007-10-18 23:40:44 -07003481 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003482 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003483 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003484
Tejun Heo0ae78e02013-08-13 11:01:54 -04003485 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003486 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003487 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003488 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003489
Tejun Heoca8bdca2013-08-26 18:40:56 -04003490 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003491}
3492
Li Zefan2a4ac632013-07-31 16:16:40 +08003493/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003494static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003495{
Tejun Heo623f9262013-08-13 11:01:55 -04003496 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003497 int ret = 0;
3498
Tejun Heoace2bee2014-02-11 11:52:47 -05003499 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003500 lockdep_assert_held(&cgroup_mutex);
3501
Tejun Heo92fb9742012-11-19 08:13:38 -08003502 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003503 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003504 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003505 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003506 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003507 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003508 }
Tejun Heob1929db2012-11-19 08:13:38 -08003509 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003510}
3511
Li Zefan2a4ac632013-07-31 16:16:40 +08003512/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003513static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003514{
Tejun Heo623f9262013-08-13 11:01:55 -04003515 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003516
Tejun Heoace2bee2014-02-11 11:52:47 -05003517 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003518 lockdep_assert_held(&cgroup_mutex);
3519
3520 if (!(css->flags & CSS_ONLINE))
3521 return;
3522
Li Zefand7eeac12013-03-12 15:35:59 -07003523 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003524 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003525
Tejun Heoeb954192013-08-08 20:11:23 -04003526 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003527 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003528 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003529}
3530
Tejun Heoc81c925a2013-12-06 15:11:56 -05003531/**
3532 * create_css - create a cgroup_subsys_state
3533 * @cgrp: the cgroup new css will be associated with
3534 * @ss: the subsys of new css
3535 *
3536 * Create a new css associated with @cgrp - @ss pair. On success, the new
3537 * css is online and installed in @cgrp with all interface files created.
3538 * Returns 0 on success, -errno on failure.
3539 */
3540static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3541{
3542 struct cgroup *parent = cgrp->parent;
3543 struct cgroup_subsys_state *css;
3544 int err;
3545
Tejun Heoc81c925a2013-12-06 15:11:56 -05003546 lockdep_assert_held(&cgroup_mutex);
3547
3548 css = ss->css_alloc(cgroup_css(parent, ss));
3549 if (IS_ERR(css))
3550 return PTR_ERR(css);
3551
3552 err = percpu_ref_init(&css->refcnt, css_release);
3553 if (err)
3554 goto err_free;
3555
3556 init_css(css, ss, cgrp);
3557
Tejun Heoaec25022014-02-08 10:36:58 -05003558 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003559 if (err)
3560 goto err_free;
3561
3562 err = online_css(css);
3563 if (err)
3564 goto err_free;
3565
Tejun Heo59f52962014-02-11 11:52:49 -05003566 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003567 css_get(css->parent);
3568
3569 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3570 parent->parent) {
3571 pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
3572 current->comm, current->pid, ss->name);
3573 if (!strcmp(ss->name, "memory"))
3574 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3575 ss->warned_broken_hierarchy = true;
3576 }
3577
3578 return 0;
3579
3580err_free:
3581 percpu_ref_cancel_init(&css->refcnt);
3582 ss->css_free(css);
3583 return err;
3584}
3585
Tejun Heo2bd59d42014-02-11 11:52:49 -05003586/**
Li Zefana043e3b2008-02-23 15:24:09 -08003587 * cgroup_create - create a cgroup
3588 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05003589 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003590 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003591 */
Tejun Heoe61734c2014-02-12 09:29:50 -05003592static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003593 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003594{
Paul Menagebd89aab2007-10-18 23:40:44 -07003595 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003596 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003597 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003598 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003599 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003600
Tejun Heo0a950f62012-11-19 09:02:12 -08003601 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003602 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3603 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003604 return -ENOMEM;
3605
Tejun Heoace2bee2014-02-11 11:52:47 -05003606 mutex_lock(&cgroup_tree_mutex);
3607
Li Zefan4e96ee82013-07-31 09:50:50 +08003608 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003609 * Only live parents can have children. Note that the liveliness
3610 * check isn't strictly necessary because cgroup_mkdir() and
3611 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3612 * anyway so that locking is contained inside cgroup proper and we
3613 * don't get nasty surprises if we ever grow another caller.
3614 */
3615 if (!cgroup_lock_live_group(parent)) {
3616 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003617 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003618 }
3619
3620 /*
3621 * Temporarily set the pointer to NULL, so idr_find() won't return
3622 * a half-baked cgroup.
3623 */
3624 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3625 if (cgrp->id < 0) {
3626 err = -ENOMEM;
3627 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003628 }
3629
Paul Menagecc31edc2008-10-18 20:28:04 -07003630 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003631
Paul Menagebd89aab2007-10-18 23:40:44 -07003632 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003633 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003634 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003635
Li Zefanb6abdb02008-03-04 14:28:19 -08003636 if (notify_on_release(parent))
3637 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3638
Tejun Heo2260e7f2012-11-19 08:13:38 -08003639 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3640 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003641
Tejun Heo2bd59d42014-02-11 11:52:49 -05003642 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05003643 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003644 if (IS_ERR(kn)) {
3645 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003646 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003647 }
3648 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003649
Tejun Heo6f305582014-02-12 09:29:50 -05003650 /*
3651 * This extra ref will be put in cgroup_free_fn() and guarantees
3652 * that @cgrp->kn is always accessible.
3653 */
3654 kernfs_get(kn);
3655
Tejun Heo00356bd2013-06-18 11:14:22 -07003656 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003657
Tejun Heo4e139af2012-11-19 08:13:36 -08003658 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003659 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05003660 atomic_inc(&root->nr_cgrps);
Tejun Heo59f52962014-02-11 11:52:49 -05003661 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003662
Tejun Heo0d802552013-12-06 15:11:56 -05003663 /*
3664 * @cgrp is now fully operational. If something fails after this
3665 * point, it'll be released via the normal destruction path.
3666 */
Li Zefan4e96ee82013-07-31 09:50:50 +08003667 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3668
Tejun Heo2bb566c2013-08-08 20:11:23 -04003669 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003670 if (err)
3671 goto err_destroy;
3672
Tejun Heo9d403e92013-12-06 15:11:56 -05003673 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003674 for_each_subsys(ss, ssid) {
3675 if (root->subsys_mask & (1 << ssid)) {
3676 err = create_css(cgrp, ss);
3677 if (err)
3678 goto err_destroy;
3679 }
Tejun Heoa8638032012-11-09 09:12:29 -08003680 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003681
Tejun Heo2bd59d42014-02-11 11:52:49 -05003682 kernfs_activate(kn);
3683
Paul Menageddbcc7e2007-10-18 23:39:30 -07003684 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003685 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003686
3687 return 0;
3688
Tejun Heo0a950f62012-11-19 09:02:12 -08003689err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08003690 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003691err_unlock:
3692 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003693err_unlock_tree:
3694 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003695 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003696 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08003697
3698err_destroy:
3699 cgroup_destroy_locked(cgrp);
3700 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003701 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08003702 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003703}
3704
Tejun Heo2bd59d42014-02-11 11:52:49 -05003705static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3706 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003707{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003708 struct cgroup *parent = parent_kn->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003709
Tejun Heo2bd59d42014-02-11 11:52:49 -05003710 return cgroup_create(parent, name, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003711}
3712
Tejun Heo223dbc32013-08-13 20:22:50 -04003713/*
3714 * This is called when the refcnt of a css is confirmed to be killed.
3715 * css_tryget() is now guaranteed to fail.
3716 */
3717static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003718{
Tejun Heo223dbc32013-08-13 20:22:50 -04003719 struct cgroup_subsys_state *css =
3720 container_of(work, struct cgroup_subsys_state, destroy_work);
3721 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003722
Tejun Heoace2bee2014-02-11 11:52:47 -05003723 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003724 mutex_lock(&cgroup_mutex);
3725
3726 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003727 * css_tryget() is guaranteed to fail now. Tell subsystems to
3728 * initate destruction.
3729 */
3730 offline_css(css);
3731
3732 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003733 * If @cgrp is marked dead, it's waiting for refs of all css's to
3734 * be disabled before proceeding to the second phase of cgroup
3735 * destruction. If we are the last one, kick it off.
3736 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003737 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003738 cgroup_destroy_css_killed(cgrp);
3739
3740 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003741 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003742
3743 /*
3744 * Put the css refs from kill_css(). Each css holds an extra
3745 * reference to the cgroup's dentry and cgroup removal proceeds
3746 * regardless of css refs. On the last put of each css, whenever
3747 * that may be, the extra dentry ref is put so that dentry
3748 * destruction happens only after all css's are released.
3749 */
3750 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003751}
3752
Tejun Heo223dbc32013-08-13 20:22:50 -04003753/* css kill confirmation processing requires process context, bounce */
3754static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003755{
3756 struct cgroup_subsys_state *css =
3757 container_of(ref, struct cgroup_subsys_state, refcnt);
3758
Tejun Heo223dbc32013-08-13 20:22:50 -04003759 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003760 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003761}
3762
3763/**
Tejun Heoedae0c32013-08-13 20:22:51 -04003764 * kill_css - destroy a css
3765 * @css: css to destroy
3766 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003767 * This function initiates destruction of @css by removing cgroup interface
3768 * files and putting its base reference. ->css_offline() will be invoked
3769 * asynchronously once css_tryget() is guaranteed to fail and when the
3770 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04003771 */
3772static void kill_css(struct cgroup_subsys_state *css)
3773{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003774 /*
3775 * This must happen before css is disassociated with its cgroup.
3776 * See seq_css() for details.
3777 */
Tejun Heoaec25022014-02-08 10:36:58 -05003778 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003779
Tejun Heoedae0c32013-08-13 20:22:51 -04003780 /*
3781 * Killing would put the base ref, but we need to keep it alive
3782 * until after ->css_offline().
3783 */
3784 css_get(css);
3785
3786 /*
3787 * cgroup core guarantees that, by the time ->css_offline() is
3788 * invoked, no new css reference will be given out via
3789 * css_tryget(). We can't simply call percpu_ref_kill() and
3790 * proceed to offlining css's because percpu_ref_kill() doesn't
3791 * guarantee that the ref is seen as killed on all CPUs on return.
3792 *
3793 * Use percpu_ref_kill_and_confirm() to get notifications as each
3794 * css is confirmed to be seen as killed on all CPUs.
3795 */
3796 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003797}
3798
3799/**
3800 * cgroup_destroy_locked - the first stage of cgroup destruction
3801 * @cgrp: cgroup to be destroyed
3802 *
3803 * css's make use of percpu refcnts whose killing latency shouldn't be
3804 * exposed to userland and are RCU protected. Also, cgroup core needs to
3805 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3806 * invoked. To satisfy all the requirements, destruction is implemented in
3807 * the following two steps.
3808 *
3809 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3810 * userland visible parts and start killing the percpu refcnts of
3811 * css's. Set up so that the next stage will be kicked off once all
3812 * the percpu refcnts are confirmed to be killed.
3813 *
3814 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3815 * rest of destruction. Once all cgroup references are gone, the
3816 * cgroup is RCU-freed.
3817 *
3818 * This function implements s1. After this step, @cgrp is gone as far as
3819 * the userland is concerned and a new cgroup with the same name may be
3820 * created. As cgroup doesn't care about the names internally, this
3821 * doesn't cause any problem.
3822 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003823static int cgroup_destroy_locked(struct cgroup *cgrp)
3824 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003825{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003826 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003827 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07003828 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05003829 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003830
Tejun Heoace2bee2014-02-11 11:52:47 -05003831 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003832 lockdep_assert_held(&cgroup_mutex);
3833
Tejun Heoddd69142013-06-12 21:04:54 -07003834 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05003835 * css_set_rwsem synchronizes access to ->cset_links and prevents
Tejun Heo89c55092014-02-13 06:58:40 -05003836 * @cgrp from being removed while put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07003837 */
Tejun Heo96d365e2014-02-13 06:58:40 -05003838 down_read(&css_set_rwsem);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003839 empty = list_empty(&cgrp->cset_links);
Tejun Heo96d365e2014-02-13 06:58:40 -05003840 up_read(&css_set_rwsem);
Tejun Heoddd69142013-06-12 21:04:54 -07003841 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003842 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08003843
Tejun Heo1a90dd52012-11-05 09:16:59 -08003844 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003845 * Make sure there's no live children. We can't test ->children
3846 * emptiness as dead children linger on it while being destroyed;
3847 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
3848 */
3849 empty = true;
3850 rcu_read_lock();
3851 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3852 empty = cgroup_is_dead(child);
3853 if (!empty)
3854 break;
3855 }
3856 rcu_read_unlock();
3857 if (!empty)
3858 return -EBUSY;
3859
3860 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04003861 * Initiate massacre of all css's. cgroup_destroy_css_killed()
3862 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05003863 * percpu refs of all css's are confirmed to be killed. This
3864 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08003865 */
Tejun Heo4ac06012014-02-11 11:52:47 -05003866 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05003867 for_each_css(css, ssid, cgrp)
3868 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05003869 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003870
3871 /*
3872 * Mark @cgrp dead. This prevents further task migration and child
3873 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04003874 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07003875 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04003876 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07003877 */
Tejun Heo54766d42013-06-12 21:04:53 -07003878 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08003879
Tejun Heo455050d2013-06-13 19:27:41 -07003880 /* CGRP_DEAD is set, remove from ->release_list for the last time */
3881 raw_spin_lock(&release_list_lock);
3882 if (!list_empty(&cgrp->release_list))
3883 list_del_init(&cgrp->release_list);
3884 raw_spin_unlock(&release_list_lock);
3885
3886 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003887 * If @cgrp has css's attached, the second stage of cgroup
3888 * destruction is kicked off from css_killed_work_fn() after the
3889 * refs of all attached css's are killed. If @cgrp doesn't have
3890 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07003891 */
Tejun Heof20104d2013-08-13 20:22:50 -04003892 if (!cgrp->nr_css)
3893 cgroup_destroy_css_killed(cgrp);
3894
Tejun Heo2bd59d42014-02-11 11:52:49 -05003895 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05003896 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003897
3898 /*
3899 * There are two control paths which try to determine cgroup from
3900 * dentry without going through kernfs - cgroupstats_build() and
3901 * css_tryget_from_dir(). Those are supported by RCU protecting
3902 * clearing of cgrp->kn->priv backpointer, which should happen
3903 * after all files under it have been removed.
3904 */
Tejun Heo6f305582014-02-12 09:29:50 -05003905 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003906 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003907
Tejun Heo4ac06012014-02-11 11:52:47 -05003908 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003909
Tejun Heoea15f8c2013-06-13 19:27:42 -07003910 return 0;
3911};
3912
Tejun Heod3daf282013-06-13 19:39:16 -07003913/**
Tejun Heof20104d2013-08-13 20:22:50 -04003914 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07003915 * @work: cgroup->destroy_free_work
3916 *
3917 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04003918 * destroyed after all css's are offlined and performs the rest of
3919 * destruction. This is the second step of destruction described in the
3920 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07003921 */
Tejun Heof20104d2013-08-13 20:22:50 -04003922static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07003923{
Tejun Heoea15f8c2013-06-13 19:27:42 -07003924 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07003925
Tejun Heoace2bee2014-02-11 11:52:47 -05003926 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003927 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003928
Paul Menage999cd8a2009-01-07 18:08:36 -08003929 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08003930 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07003931
Tejun Heo59f52962014-02-11 11:52:49 -05003932 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003933
Paul Menagebd89aab2007-10-18 23:40:44 -07003934 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003935 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003936}
3937
Tejun Heo2bd59d42014-02-11 11:52:49 -05003938static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08003939{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003940 struct cgroup *cgrp = kn->priv;
3941 int ret = 0;
3942
3943 /*
3944 * This is self-destruction but @kn can't be removed while this
3945 * callback is in progress. Let's break active protection. Once
3946 * the protection is broken, @cgrp can be destroyed at any point.
3947 * Pin it so that it stays accessible.
3948 */
3949 cgroup_get(cgrp);
3950 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08003951
Tejun Heoace2bee2014-02-11 11:52:47 -05003952 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003953 mutex_lock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003954
3955 /*
3956 * @cgrp might already have been destroyed while we're trying to
3957 * grab the mutexes.
3958 */
3959 if (!cgroup_is_dead(cgrp))
3960 ret = cgroup_destroy_locked(cgrp);
3961
Tejun Heo42809dd2012-11-19 08:13:37 -08003962 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003963 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003964
Tejun Heo2bd59d42014-02-11 11:52:49 -05003965 kernfs_unbreak_active_protection(kn);
3966 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08003967 return ret;
3968}
3969
Tejun Heo2bd59d42014-02-11 11:52:49 -05003970static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
3971 .remount_fs = cgroup_remount,
3972 .show_options = cgroup_show_options,
3973 .mkdir = cgroup_mkdir,
3974 .rmdir = cgroup_rmdir,
3975 .rename = cgroup_rename,
3976};
3977
Li Zefan06a11922008-04-29 01:00:07 -07003978static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003979{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003980 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08003981
3982 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003983
Tejun Heoace2bee2014-02-11 11:52:47 -05003984 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08003985 mutex_lock(&cgroup_mutex);
3986
Tejun Heo0adb0702014-02-12 09:29:48 -05003987 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07003988
Paul Menageddbcc7e2007-10-18 23:39:30 -07003989 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07003990 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04003991 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003992 /* We don't handle early failures gracefully */
3993 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04003994 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003995
Li Zefane8d55fd2008-04-29 01:00:13 -07003996 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07003997 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07003998 * newly registered, all tasks and hence the
3999 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004000 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004001
4002 need_forkexit_callback |= ss->fork || ss->exit;
4003
Li Zefane8d55fd2008-04-29 01:00:13 -07004004 /* At system boot, before all subsystems have been
4005 * registered, no tasks have been forked, so we don't
4006 * need to invoke fork callbacks here. */
4007 BUG_ON(!list_empty(&init_task.tasks));
4008
Tejun Heoae7f1642013-08-13 20:22:50 -04004009 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004010
Tejun Heo648bb562012-11-19 08:13:36 -08004011 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004012 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004013}
4014
4015/**
Li Zefana043e3b2008-02-23 15:24:09 -08004016 * cgroup_init_early - cgroup initialization at system boot
4017 *
4018 * Initialize cgroups at system boot, and initialize any
4019 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004020 */
4021int __init cgroup_init_early(void)
4022{
Tejun Heo30159ec2013-06-25 11:53:37 -07004023 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004024 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004025
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004026 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004027 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004028 INIT_LIST_HEAD(&init_css_set.tasks);
Tejun Heob3dc0942014-02-25 10:04:01 -05004029 INIT_LIST_HEAD(&init_css_set.mg_tasks);
Tejun Heo1958d2d2014-02-25 10:04:03 -05004030 INIT_LIST_HEAD(&init_css_set.mg_preload_node);
Tejun Heob3dc0942014-02-25 10:04:01 -05004031 INIT_LIST_HEAD(&init_css_set.mg_node);
Li Zefan472b1052008-04-29 01:00:11 -07004032 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004033 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004034 init_cgroup_root(&cgroup_dummy_root);
4035 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004036 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004037
Tejun Heo69d02062013-06-12 21:04:50 -07004038 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004039 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4040 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004041 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042
Tejun Heo3ed80a62014-02-08 10:36:58 -05004043 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004044 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004045 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4046 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004047 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004048 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4049 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4050
Tejun Heoaec25022014-02-08 10:36:58 -05004051 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004052 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004053
4054 if (ss->early_init)
4055 cgroup_init_subsys(ss);
4056 }
4057 return 0;
4058}
4059
4060/**
Li Zefana043e3b2008-02-23 15:24:09 -08004061 * cgroup_init - cgroup initialization
4062 *
4063 * Register cgroup filesystem and /proc file, and initialize
4064 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004065 */
4066int __init cgroup_init(void)
4067{
Tejun Heo30159ec2013-06-25 11:53:37 -07004068 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004069 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004070 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004071
Tejun Heo2bd59d42014-02-11 11:52:49 -05004072 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Tejun Heo2da440a2014-02-11 11:52:48 -05004073
Tejun Heo3ed80a62014-02-08 10:36:58 -05004074 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004075 if (!ss->early_init)
4076 cgroup_init_subsys(ss);
Tejun Heode00ffa2014-02-11 11:52:48 -05004077
4078 /*
4079 * cftype registration needs kmalloc and can't be done
4080 * during early_init. Register base cftypes separately.
4081 */
4082 if (ss->base_cftypes)
4083 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004084 }
4085
Tejun Heofa3ca072013-04-14 11:36:56 -07004086 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004087 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004088
Tejun Heo82fe9b02013-06-25 11:53:37 -07004089 /* Add init_css_set to the hash table */
4090 key = css_set_hash(init_css_set.subsys);
4091 hash_add(css_set_table, &init_css_set.hlist, key);
4092
Tejun Heofc76df72013-06-25 11:53:37 -07004093 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004094
Li Zefan4e96ee82013-07-31 09:50:50 +08004095 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4096 0, 1, GFP_KERNEL);
4097 BUG_ON(err < 0);
4098
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004099 mutex_unlock(&cgroup_mutex);
4100
Greg KH676db4a2010-08-05 13:53:35 -07004101 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004102 if (!cgroup_kobj)
4103 return -ENOMEM;
Greg KH676db4a2010-08-05 13:53:35 -07004104
4105 err = register_filesystem(&cgroup_fs_type);
4106 if (err < 0) {
4107 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004108 return err;
Greg KH676db4a2010-08-05 13:53:35 -07004109 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004110
Li Zefan46ae2202008-04-29 01:00:08 -07004111 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004112 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004113}
Paul Menageb4f48b62007-10-18 23:39:33 -07004114
Tejun Heoe5fca242013-11-22 17:14:39 -05004115static int __init cgroup_wq_init(void)
4116{
4117 /*
4118 * There isn't much point in executing destruction path in
4119 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004120 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004121 *
4122 * We would prefer to do this in cgroup_init() above, but that
4123 * is called before init_workqueues(): so leave this until after.
4124 */
Tejun Heo1a115332014-02-12 19:06:19 -05004125 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004126 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004127
4128 /*
4129 * Used to destroy pidlists and separate to serve as flush domain.
4130 * Cap @max_active to 1 too.
4131 */
4132 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4133 0, 1);
4134 BUG_ON(!cgroup_pidlist_destroy_wq);
4135
Tejun Heoe5fca242013-11-22 17:14:39 -05004136 return 0;
4137}
4138core_initcall(cgroup_wq_init);
4139
Paul Menagea4243162007-10-18 23:39:35 -07004140/*
4141 * proc_cgroup_show()
4142 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4143 * - Used for /proc/<pid>/cgroup.
4144 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4145 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004146 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004147 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4148 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4149 * cgroup to top_cgroup.
4150 */
4151
4152/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004153int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004154{
4155 struct pid *pid;
4156 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004157 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004158 int retval;
4159 struct cgroupfs_root *root;
4160
4161 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004162 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004163 if (!buf)
4164 goto out;
4165
4166 retval = -ESRCH;
4167 pid = m->private;
4168 tsk = get_pid_task(pid, PIDTYPE_PID);
4169 if (!tsk)
4170 goto out_free;
4171
4172 retval = 0;
4173
4174 mutex_lock(&cgroup_mutex);
Tejun Heo96d365e2014-02-13 06:58:40 -05004175 down_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004176
Li Zefane5f6a862009-01-07 18:07:41 -08004177 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004178 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004179 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004180 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004181
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004182 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004183 for_each_subsys(ss, ssid)
4184 if (root->subsys_mask & (1 << ssid))
4185 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004186 if (strlen(root->name))
4187 seq_printf(m, "%sname=%s", count ? "," : "",
4188 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004189 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004190 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004191 path = cgroup_path(cgrp, buf, PATH_MAX);
4192 if (!path) {
4193 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004194 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004195 }
4196 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004197 seq_putc(m, '\n');
4198 }
4199
4200out_unlock:
Tejun Heo96d365e2014-02-13 06:58:40 -05004201 up_read(&css_set_rwsem);
Paul Menagea4243162007-10-18 23:39:35 -07004202 mutex_unlock(&cgroup_mutex);
4203 put_task_struct(tsk);
4204out_free:
4205 kfree(buf);
4206out:
4207 return retval;
4208}
4209
Paul Menagea4243162007-10-18 23:39:35 -07004210/* Display information about each subsystem and each hierarchy */
4211static int proc_cgroupstats_show(struct seq_file *m, void *v)
4212{
Tejun Heo30159ec2013-06-25 11:53:37 -07004213 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004214 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004215
Paul Menage8bab8dd2008-04-04 14:29:57 -07004216 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004217 /*
4218 * ideally we don't want subsystems moving around while we do this.
4219 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4220 * subsys/hierarchy state.
4221 */
Paul Menagea4243162007-10-18 23:39:35 -07004222 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004223
4224 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004225 seq_printf(m, "%s\t%d\t%d\t%d\n",
4226 ss->name, ss->root->hierarchy_id,
Tejun Heo3c9c8252014-02-12 09:29:50 -05004227 atomic_read(&ss->root->nr_cgrps), !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004228
Paul Menagea4243162007-10-18 23:39:35 -07004229 mutex_unlock(&cgroup_mutex);
4230 return 0;
4231}
4232
4233static int cgroupstats_open(struct inode *inode, struct file *file)
4234{
Al Viro9dce07f2008-03-29 03:07:28 +00004235 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004236}
4237
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004238static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004239 .open = cgroupstats_open,
4240 .read = seq_read,
4241 .llseek = seq_lseek,
4242 .release = single_release,
4243};
4244
Paul Menageb4f48b62007-10-18 23:39:33 -07004245/**
4246 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004247 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004248 *
4249 * Description: A task inherits its parent's cgroup at fork().
4250 *
4251 * A pointer to the shared css_set was automatically copied in
4252 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004253 * it was not made under the protection of RCU or cgroup_mutex, so
4254 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4255 * have already changed current->cgroups, allowing the previously
4256 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004257 *
4258 * At the point that cgroup_fork() is called, 'current' is the parent
4259 * task, and the passed argument 'child' points to the child task.
4260 */
4261void cgroup_fork(struct task_struct *child)
4262{
Tejun Heo9bb71302012-10-18 17:52:07 -07004263 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004264 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004265 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004266 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004267 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004268}
4269
4270/**
Li Zefana043e3b2008-02-23 15:24:09 -08004271 * cgroup_post_fork - called on a new task after adding it to the task list
4272 * @child: the task in question
4273 *
Tejun Heo5edee612012-10-16 15:03:14 -07004274 * Adds the task to the list running through its css_set if necessary and
4275 * call the subsystem fork() callbacks. Has to be after the task is
4276 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004277 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004278 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004279 */
Paul Menage817929e2007-10-18 23:39:36 -07004280void cgroup_post_fork(struct task_struct *child)
4281{
Tejun Heo30159ec2013-06-25 11:53:37 -07004282 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004283 int i;
4284
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004285 /*
4286 * use_task_css_set_links is set to 1 before we walk the tasklist
4287 * under the tasklist_lock and we read it here after we added the child
4288 * to the tasklist under the tasklist_lock as well. If the child wasn't
4289 * yet in the tasklist when we walked through it from
4290 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4291 * should be visible now due to the paired locking and barriers implied
4292 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4293 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4294 * lock on fork.
4295 */
Paul Menage817929e2007-10-18 23:39:36 -07004296 if (use_task_css_set_links) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004297 down_write(&css_set_rwsem);
Tejun Heod8783832012-10-18 17:40:30 -07004298 task_lock(child);
4299 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004300 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004301 task_unlock(child);
Tejun Heo96d365e2014-02-13 06:58:40 -05004302 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004303 }
Tejun Heo5edee612012-10-16 15:03:14 -07004304
4305 /*
4306 * Call ss->fork(). This must happen after @child is linked on
4307 * css_set; otherwise, @child might change state between ->fork()
4308 * and addition to css_set.
4309 */
4310 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004311 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004312 if (ss->fork)
4313 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004314 }
Paul Menage817929e2007-10-18 23:39:36 -07004315}
Tejun Heo5edee612012-10-16 15:03:14 -07004316
Paul Menage817929e2007-10-18 23:39:36 -07004317/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004318 * cgroup_exit - detach cgroup from exiting task
4319 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004320 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004321 *
4322 * Description: Detach cgroup from @tsk and release it.
4323 *
4324 * Note that cgroups marked notify_on_release force every task in
4325 * them to take the global cgroup_mutex mutex when exiting.
4326 * This could impact scaling on very large systems. Be reluctant to
4327 * use notify_on_release cgroups where very high task exit scaling
4328 * is required on large systems.
4329 *
4330 * the_top_cgroup_hack:
4331 *
4332 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4333 *
4334 * We call cgroup_exit() while the task is still competent to
4335 * handle notify_on_release(), then leave the task attached to the
4336 * root cgroup in each hierarchy for the remainder of its exit.
4337 *
4338 * To do this properly, we would increment the reference count on
4339 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4340 * code we would add a second cgroup function call, to drop that
4341 * reference. This would just create an unnecessary hot spot on
4342 * the top_cgroup reference count, to no avail.
4343 *
4344 * Normally, holding a reference to a cgroup without bumping its
4345 * count is unsafe. The cgroup could go away, or someone could
4346 * attach us to a different cgroup, decrementing the count on
4347 * the first cgroup that we never incremented. But in this case,
4348 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004349 * which wards off any cgroup_attach_task() attempts, or task is a failed
4350 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004351 */
4352void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4353{
Tejun Heo30159ec2013-06-25 11:53:37 -07004354 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004355 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004356 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004357
4358 /*
Tejun Heo96d365e2014-02-13 06:58:40 -05004359 * Unlink from the css_set task list if necessary. Optimistically
4360 * check cg_list before taking css_set_rwsem.
Paul Menage817929e2007-10-18 23:39:36 -07004361 */
4362 if (!list_empty(&tsk->cg_list)) {
Tejun Heo96d365e2014-02-13 06:58:40 -05004363 down_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004364 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004365 list_del_init(&tsk->cg_list);
Tejun Heo96d365e2014-02-13 06:58:40 -05004366 up_write(&css_set_rwsem);
Paul Menage817929e2007-10-18 23:39:36 -07004367 }
4368
Paul Menageb4f48b62007-10-18 23:39:33 -07004369 /* Reassign the task to the init_css_set. */
4370 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004371 cset = task_css_set(tsk);
4372 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004373
4374 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004375 /* see cgroup_post_fork() for details */
4376 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004377 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004378 struct cgroup_subsys_state *old_css = cset->subsys[i];
4379 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004380
Tejun Heoeb954192013-08-08 20:11:23 -04004381 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004382 }
4383 }
4384 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004385 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004386
Tejun Heo89c55092014-02-13 06:58:40 -05004387 put_css_set(cset, true);
Paul Menageb4f48b62007-10-18 23:39:33 -07004388}
Paul Menage697f4162007-10-18 23:39:34 -07004389
Paul Menagebd89aab2007-10-18 23:40:44 -07004390static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004391{
Li Zefanf50daa72013-03-01 15:06:07 +08004392 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004393 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004394 /*
4395 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004396 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004397 * it now
4398 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004399 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004400
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004401 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004402 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004403 list_empty(&cgrp->release_list)) {
4404 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004405 need_schedule_work = 1;
4406 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004407 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004408 if (need_schedule_work)
4409 schedule_work(&release_agent_work);
4410 }
4411}
4412
Paul Menage81a6a5c2007-10-18 23:39:38 -07004413/*
4414 * Notify userspace when a cgroup is released, by running the
4415 * configured release agent with the name of the cgroup (path
4416 * relative to the root of cgroup file system) as the argument.
4417 *
4418 * Most likely, this user command will try to rmdir this cgroup.
4419 *
4420 * This races with the possibility that some other task will be
4421 * attached to this cgroup before it is removed, or that some other
4422 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4423 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4424 * unused, and this cgroup will be reprieved from its death sentence,
4425 * to continue to serve a useful existence. Next time it's released,
4426 * we will get notified again, if it still has 'notify_on_release' set.
4427 *
4428 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4429 * means only wait until the task is successfully execve()'d. The
4430 * separate release agent task is forked by call_usermodehelper(),
4431 * then control in this thread returns here, without waiting for the
4432 * release agent task. We don't bother to wait because the caller of
4433 * this routine has no use for the exit status of the release agent
4434 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004435 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004436static void cgroup_release_agent(struct work_struct *work)
4437{
4438 BUG_ON(work != &release_agent_work);
4439 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004440 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004441 while (!list_empty(&release_list)) {
4442 char *argv[3], *envp[3];
4443 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05004444 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07004445 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004446 struct cgroup,
4447 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004448 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004449 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004450 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004451 if (!pathbuf)
4452 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05004453 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4454 if (!path)
Paul Menagee788e062008-07-25 01:46:59 -07004455 goto continue_free;
4456 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4457 if (!agentbuf)
4458 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004459
4460 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004461 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05004462 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004463 argv[i] = NULL;
4464
4465 i = 0;
4466 /* minimal command environment */
4467 envp[i++] = "HOME=/";
4468 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4469 envp[i] = NULL;
4470
4471 /* Drop the lock while we invoke the usermode helper,
4472 * since the exec could involve hitting disk and hence
4473 * be a slow process */
4474 mutex_unlock(&cgroup_mutex);
4475 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004476 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004477 continue_free:
4478 kfree(pathbuf);
4479 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004480 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004481 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004482 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004483 mutex_unlock(&cgroup_mutex);
4484}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004485
4486static int __init cgroup_disable(char *str)
4487{
Tejun Heo30159ec2013-06-25 11:53:37 -07004488 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004489 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004490 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004491
4492 while ((token = strsep(&str, ",")) != NULL) {
4493 if (!*token)
4494 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004495
Tejun Heo3ed80a62014-02-08 10:36:58 -05004496 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004497 if (!strcmp(token, ss->name)) {
4498 ss->disabled = 1;
4499 printk(KERN_INFO "Disabling %s control group"
4500 " subsystem\n", ss->name);
4501 break;
4502 }
4503 }
4504 }
4505 return 1;
4506}
4507__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004508
Tejun Heob77d7b62013-08-13 11:01:54 -04004509/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004510 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004511 * @dentry: directory dentry of interest
4512 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004513 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004514 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4515 * to get the corresponding css and return it. If such css doesn't exist
4516 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004517 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004518struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4519 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004520{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004521 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4522 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004523 struct cgroup *cgrp;
Tejun Heob77d7b62013-08-13 11:01:54 -04004524
Tejun Heo35cf0832013-08-26 18:40:56 -04004525 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004526 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4527 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004528 return ERR_PTR(-EBADF);
4529
Tejun Heo5a17f542014-02-11 11:52:47 -05004530 rcu_read_lock();
4531
Tejun Heo2bd59d42014-02-11 11:52:49 -05004532 /*
4533 * This path doesn't originate from kernfs and @kn could already
4534 * have been or be removed at any point. @kn->priv is RCU
4535 * protected for this access. See destroy_locked() for details.
4536 */
4537 cgrp = rcu_dereference(kn->priv);
4538 if (cgrp)
4539 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004540
4541 if (!css || !css_tryget(css))
4542 css = ERR_PTR(-ENOENT);
4543
4544 rcu_read_unlock();
4545 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004546}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004547
Li Zefan1cb650b2013-08-19 10:05:24 +08004548/**
4549 * css_from_id - lookup css by id
4550 * @id: the cgroup id
4551 * @ss: cgroup subsys to be looked into
4552 *
4553 * Returns the css if there's valid one with @id, otherwise returns NULL.
4554 * Should be called under rcu_read_lock().
4555 */
4556struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4557{
4558 struct cgroup *cgrp;
4559
Tejun Heoace2bee2014-02-11 11:52:47 -05004560 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004561
4562 cgrp = idr_find(&ss->root->cgroup_idr, id);
4563 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004564 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004565 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004566}
4567
Paul Menagefe693432009-09-23 15:56:20 -07004568#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004569static struct cgroup_subsys_state *
4570debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004571{
4572 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4573
4574 if (!css)
4575 return ERR_PTR(-ENOMEM);
4576
4577 return css;
4578}
4579
Tejun Heoeb954192013-08-08 20:11:23 -04004580static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004581{
Tejun Heoeb954192013-08-08 20:11:23 -04004582 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004583}
4584
Tejun Heo182446d2013-08-08 20:11:24 -04004585static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4586 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004587{
Tejun Heo182446d2013-08-08 20:11:24 -04004588 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004589}
4590
Tejun Heo182446d2013-08-08 20:11:24 -04004591static u64 current_css_set_read(struct cgroup_subsys_state *css,
4592 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004593{
4594 return (u64)(unsigned long)current->cgroups;
4595}
4596
Tejun Heo182446d2013-08-08 20:11:24 -04004597static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004598 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004599{
4600 u64 count;
4601
4602 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004603 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004604 rcu_read_unlock();
4605 return count;
4606}
4607
Tejun Heo2da8ca82013-12-05 12:28:04 -05004608static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004609{
Tejun Heo69d02062013-06-12 21:04:50 -07004610 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004611 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05004612 char *name_buf;
4613
4614 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4615 if (!name_buf)
4616 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07004617
Tejun Heo96d365e2014-02-13 06:58:40 -05004618 down_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004619 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004620 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004621 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004622 struct cgroup *c = link->cgrp;
Tejun Heo59f52962014-02-11 11:52:49 -05004623 const char *name = "?";
Paul Menage7717f7b2009-09-23 15:56:22 -07004624
Tejun Heoe61734c2014-02-12 09:29:50 -05004625 if (c != cgroup_dummy_top) {
4626 cgroup_name(c, name_buf, NAME_MAX + 1);
4627 name = name_buf;
4628 }
Tejun Heo59f52962014-02-11 11:52:49 -05004629
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004630 seq_printf(seq, "Root %d group %s\n",
4631 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004632 }
4633 rcu_read_unlock();
Tejun Heo96d365e2014-02-13 06:58:40 -05004634 up_read(&css_set_rwsem);
Tejun Heoe61734c2014-02-12 09:29:50 -05004635 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004636 return 0;
4637}
4638
4639#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004640static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004641{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004642 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004643 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004644
Tejun Heo96d365e2014-02-13 06:58:40 -05004645 down_read(&css_set_rwsem);
Tejun Heo182446d2013-08-08 20:11:24 -04004646 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004647 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004648 struct task_struct *task;
4649 int count = 0;
Tejun Heoc7561122014-02-25 10:04:01 -05004650
Tejun Heo5abb8852013-06-12 21:04:49 -07004651 seq_printf(seq, "css_set %p\n", cset);
Tejun Heoc7561122014-02-25 10:04:01 -05004652
Tejun Heo5abb8852013-06-12 21:04:49 -07004653 list_for_each_entry(task, &cset->tasks, cg_list) {
Tejun Heoc7561122014-02-25 10:04:01 -05004654 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4655 goto overflow;
4656 seq_printf(seq, " task %d\n", task_pid_vnr(task));
Paul Menage7717f7b2009-09-23 15:56:22 -07004657 }
Tejun Heoc7561122014-02-25 10:04:01 -05004658
4659 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
4660 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
4661 goto overflow;
4662 seq_printf(seq, " task %d\n", task_pid_vnr(task));
4663 }
4664 continue;
4665 overflow:
4666 seq_puts(seq, " ...\n");
Paul Menage7717f7b2009-09-23 15:56:22 -07004667 }
Tejun Heo96d365e2014-02-13 06:58:40 -05004668 up_read(&css_set_rwsem);
Paul Menage7717f7b2009-09-23 15:56:22 -07004669 return 0;
4670}
4671
Tejun Heo182446d2013-08-08 20:11:24 -04004672static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004673{
Tejun Heo182446d2013-08-08 20:11:24 -04004674 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004675}
4676
4677static struct cftype debug_files[] = {
4678 {
Paul Menagefe693432009-09-23 15:56:20 -07004679 .name = "taskcount",
4680 .read_u64 = debug_taskcount_read,
4681 },
4682
4683 {
4684 .name = "current_css_set",
4685 .read_u64 = current_css_set_read,
4686 },
4687
4688 {
4689 .name = "current_css_set_refcount",
4690 .read_u64 = current_css_set_refcount_read,
4691 },
4692
4693 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004694 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004695 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004696 },
4697
4698 {
4699 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004700 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004701 },
4702
4703 {
Paul Menagefe693432009-09-23 15:56:20 -07004704 .name = "releasable",
4705 .read_u64 = releasable_read,
4706 },
Paul Menagefe693432009-09-23 15:56:20 -07004707
Tejun Heo4baf6e32012-04-01 12:09:55 -07004708 { } /* terminate */
4709};
Paul Menagefe693432009-09-23 15:56:20 -07004710
Tejun Heo073219e2014-02-08 10:36:58 -05004711struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004712 .css_alloc = debug_css_alloc,
4713 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004714 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004715};
4716#endif /* CONFIG_CGROUP_DEBUG */