blob: 638df032fb9441d60f9faf3806d163c344e7efed [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>
45#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070046#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070047#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070048#include <linux/delayacct.h>
49#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080050#include <linux/hashtable.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070051#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070052#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070053#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080054#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020055#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070056
Arun Sharma600634972011-07-26 16:09:06 -070057#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070058
Tejun Heoe25e2cb2011-12-12 18:12:21 -080059/*
Tejun Heob1a21362013-11-29 10:42:58 -050060 * pidlists linger the following amount before being destroyed. The goal
61 * is avoiding frequent destruction in the middle of consecutive read calls
62 * Expiring in the middle is a performance problem not a correctness one.
63 * 1 sec should be enough.
64 */
65#define CGROUP_PIDLIST_DESTROY_DELAY HZ
66
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050067#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
68 MAX_CFTYPE_NAME + 2)
69
Tejun Heob1a21362013-11-29 10:42:58 -050070/*
Tejun Heoace2bee2014-02-11 11:52:47 -050071 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
72 * creation/removal and hierarchy changing operations including cgroup
73 * creation, removal, css association and controller rebinding. This outer
74 * lock is needed mainly to resolve the circular dependency between kernfs
75 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
76 */
77static DEFINE_MUTEX(cgroup_tree_mutex);
78
79/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080080 * cgroup_mutex is the master lock. Any modification to cgroup or its
81 * hierarchy must be performed while holding it.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080082 */
Tejun Heo22194492013-04-07 09:29:51 -070083#ifdef CONFIG_PROVE_RCU
84DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040085EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070086#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070087static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070088#endif
89
Tejun Heo69e943b2014-02-08 10:36:58 -050090/*
91 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
92 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
93 */
94static DEFINE_SPINLOCK(release_agent_path_lock);
95
Tejun Heoace2bee2014-02-11 11:52:47 -050096#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -050097 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -050098 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -050099 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500100 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo87fb54f2013-12-06 15:11:55 -0500101
Ben Blumaae8aab2010-03-10 15:22:07 -0800102/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500103 * cgroup destruction makes heavy use of work items and there can be a lot
104 * of concurrent destructions. Use a separate workqueue so that cgroup
105 * destruction work items don't end up filling up max_active of system_wq
106 * which may lead to deadlock.
107 */
108static struct workqueue_struct *cgroup_destroy_wq;
109
110/*
Tejun Heob1a21362013-11-29 10:42:58 -0500111 * pidlist destructions need to be flushed on cgroup destruction. Use a
112 * separate workqueue as flush domain.
113 */
114static struct workqueue_struct *cgroup_pidlist_destroy_wq;
115
Tejun Heo3ed80a62014-02-08 10:36:58 -0500116/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500117#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500118static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700119#include <linux/cgroup_subsys.h>
120};
Tejun Heo073219e2014-02-08 10:36:58 -0500121#undef SUBSYS
122
123/* array of cgroup subsystem names */
124#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
125static const char *cgroup_subsys_name[] = {
126#include <linux/cgroup_subsys.h>
127};
128#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700129
Paul Menageddbcc7e2007-10-18 23:39:30 -0700130/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700131 * The dummy hierarchy, reserved for the subsystems that are otherwise
132 * unattached - it never has more than a single cgroup, and all tasks are
133 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700134 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700135static struct cgroupfs_root cgroup_dummy_root;
136
137/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
138static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139
140/* The list of hierarchy roots */
141
Tejun Heo9871bf92013-06-24 15:21:47 -0700142static LIST_HEAD(cgroup_roots);
143static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144
Tejun Heo3417ae12014-02-08 10:37:01 -0500145/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700146static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700147
Li Zefan794611a2013-06-18 18:53:53 +0800148/*
149 * Assign a monotonically increasing serial number to cgroups. It
150 * guarantees cgroups with bigger numbers are newer than those with smaller
151 * numbers. Also, as cgroups are always appended to the parent's
152 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700153 * the ascending serial number order on the list. Protected by
154 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800155 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700156static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800157
Paul Menageddbcc7e2007-10-18 23:39:30 -0700158/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800159 * check for fork/exit handlers to call. This avoids us having to do
160 * extra work in the fork/exit path if none of the subsystems need to
161 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700162 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700163static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700164
Tejun Heo628f7cd2013-06-28 16:24:11 -0700165static struct cftype cgroup_base_files[];
166
Tejun Heo59f52962014-02-11 11:52:49 -0500167static void cgroup_put(struct cgroup *cgrp);
Tejun Heof2e85d52014-02-11 11:52:49 -0500168static int rebind_subsystems(struct cgroupfs_root *root,
169 unsigned long added_mask, unsigned removed_mask);
Tejun Heof20104d2013-08-13 20:22:50 -0400170static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800171static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400172static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
173 bool is_add);
Tejun Heob1a21362013-11-29 10:42:58 -0500174static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800175
Tejun Heo95109b62013-08-08 20:11:27 -0400176/**
177 * cgroup_css - obtain a cgroup's css for the specified subsystem
178 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400179 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400180 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400181 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
182 * function must be called either under cgroup_mutex or rcu_read_lock() and
183 * the caller is responsible for pinning the returned css if it wants to
184 * keep accessing it outside the said locks. This function may return
185 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400186 */
187static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400188 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400189{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400190 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500191 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500192 lockdep_is_held(&cgroup_tree_mutex) ||
193 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400194 else
195 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400196}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700197
Paul Menageddbcc7e2007-10-18 23:39:30 -0700198/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700199static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200{
Tejun Heo54766d42013-06-12 21:04:53 -0700201 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700202}
203
Tejun Heo59f52962014-02-11 11:52:49 -0500204struct cgroup_subsys_state *seq_css(struct seq_file *seq)
205{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500206 struct kernfs_open_file *of = seq->private;
207 struct cgroup *cgrp = of->kn->parent->priv;
208 struct cftype *cft = seq_cft(seq);
209
210 /*
211 * This is open and unprotected implementation of cgroup_css().
212 * seq_css() is only called from a kernfs file operation which has
213 * an active reference on the file. Because all the subsystem
214 * files are drained before a css is disassociated with a cgroup,
215 * the matching css from the cgroup's subsys table is guaranteed to
216 * be and stay valid until the enclosing operation is complete.
217 */
218 if (cft->ss)
219 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
220 else
221 return &cgrp->dummy_css;
Tejun Heo59f52962014-02-11 11:52:49 -0500222}
223EXPORT_SYMBOL_GPL(seq_css);
224
Li Zefan78574cf2013-04-08 19:00:38 -0700225/**
226 * cgroup_is_descendant - test ancestry
227 * @cgrp: the cgroup to be tested
228 * @ancestor: possible ancestor of @cgrp
229 *
230 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
231 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
232 * and @ancestor are accessible.
233 */
234bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
235{
236 while (cgrp) {
237 if (cgrp == ancestor)
238 return true;
239 cgrp = cgrp->parent;
240 }
241 return false;
242}
243EXPORT_SYMBOL_GPL(cgroup_is_descendant);
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 Gleixnercdcc136f2009-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/*
343 * css_set_lock protects the list of css_set objects, and the chain of
344 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400345 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400346 */
Paul Menage817929e2007-10-18 23:39:36 -0700347static DEFINE_RWLOCK(css_set_lock);
348static int css_set_count;
349
Paul Menage7717f7b2009-09-23 15:56:22 -0700350/*
351 * hash table for cgroup groups. This improves the performance to find
352 * an existing css_set. This hash doesn't (currently) take into
353 * account cgroups in empty hierarchies.
354 */
Li Zefan472b1052008-04-29 01:00:11 -0700355#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800356static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700357
Li Zefan0ac801f2013-01-10 11:49:27 +0800358static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700359{
Li Zefan0ac801f2013-01-10 11:49:27 +0800360 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700361 struct cgroup_subsys *ss;
362 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700363
Tejun Heo30159ec2013-06-25 11:53:37 -0700364 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800365 key += (unsigned long)css[i];
366 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700367
Li Zefan0ac801f2013-01-10 11:49:27 +0800368 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700369}
370
Tejun Heo0942eee2013-08-08 20:11:26 -0400371/*
372 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400373 * until after the first call to css_task_iter_start(). This reduces the
374 * fork()/exit() overhead for people who have cgroups compiled into their
375 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400376 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700377static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700378
Tejun Heo5abb8852013-06-12 21:04:49 -0700379static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700380{
Tejun Heo69d02062013-06-12 21:04:50 -0700381 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700382
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700383 /*
384 * Ensure that the refcount doesn't hit zero while any readers
385 * can see it. Similar to atomic_dec_and_lock(), but for an
386 * rwlock
387 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700388 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700389 return;
390 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700391 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700392 write_unlock(&css_set_lock);
393 return;
394 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700395
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700396 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700397 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700398 css_set_count--;
399
Tejun Heo69d02062013-06-12 21:04:50 -0700400 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700401 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700402
Tejun Heo69d02062013-06-12 21:04:50 -0700403 list_del(&link->cset_link);
404 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800405
Tejun Heoddd69142013-06-12 21:04:54 -0700406 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700407 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700408 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700409 set_bit(CGRP_RELEASABLE, &cgrp->flags);
410 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700411 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700412
413 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700414 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700415
416 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700417 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700418}
419
420/*
421 * refcounted get/put for css_set objects
422 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700423static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700424{
Tejun Heo5abb8852013-06-12 21:04:49 -0700425 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700426}
427
Tejun Heo5abb8852013-06-12 21:04:49 -0700428static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700429{
Tejun Heo5abb8852013-06-12 21:04:49 -0700430 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700431}
432
Tejun Heo5abb8852013-06-12 21:04:49 -0700433static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434{
Tejun Heo5abb8852013-06-12 21:04:49 -0700435 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700436}
437
Tejun Heob326f9d2013-06-24 15:21:48 -0700438/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700439 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700440 * @cset: candidate css_set being tested
441 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700442 * @new_cgrp: cgroup that's being entered by the task
443 * @template: desired set of css pointers in css_set (pre-calculated)
444 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800445 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700446 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
447 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700448static bool compare_css_sets(struct css_set *cset,
449 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700450 struct cgroup *new_cgrp,
451 struct cgroup_subsys_state *template[])
452{
453 struct list_head *l1, *l2;
454
Tejun Heo5abb8852013-06-12 21:04:49 -0700455 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700456 /* Not all subsystems matched */
457 return false;
458 }
459
460 /*
461 * Compare cgroup pointers in order to distinguish between
462 * different cgroups in heirarchies with no subsystems. We
463 * could get by with just this check alone (and skip the
464 * memcmp above) but on most setups the memcmp check will
465 * avoid the need for this more expensive check on almost all
466 * candidates.
467 */
468
Tejun Heo69d02062013-06-12 21:04:50 -0700469 l1 = &cset->cgrp_links;
470 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700471 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700472 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700473 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700474
475 l1 = l1->next;
476 l2 = l2->next;
477 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700478 if (l1 == &cset->cgrp_links) {
479 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700480 break;
481 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700482 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700483 }
484 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700485 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
486 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
487 cgrp1 = link1->cgrp;
488 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700489 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700490 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700491
492 /*
493 * If this hierarchy is the hierarchy of the cgroup
494 * that's changing, then we need to check that this
495 * css_set points to the new cgroup; if it's any other
496 * hierarchy, then this css_set should point to the
497 * same cgroup as the old css_set.
498 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700499 if (cgrp1->root == new_cgrp->root) {
500 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700501 return false;
502 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700503 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700504 return false;
505 }
506 }
507 return true;
508}
509
Tejun Heob326f9d2013-06-24 15:21:48 -0700510/**
511 * find_existing_css_set - init css array and find the matching css_set
512 * @old_cset: the css_set that we're using before the cgroup transition
513 * @cgrp: the cgroup that we're moving into
514 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700515 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700516static struct css_set *find_existing_css_set(struct css_set *old_cset,
517 struct cgroup *cgrp,
518 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700519{
Paul Menagebd89aab2007-10-18 23:40:44 -0700520 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700521 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700522 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800523 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700524 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700525
Ben Blumaae8aab2010-03-10 15:22:07 -0800526 /*
527 * Build the set of subsystem state objects that we want to see in the
528 * new css_set. while subsystems can change globally, the entries here
529 * won't change, so no need for locking.
530 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700531 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400532 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700533 /* Subsystem is in this hierarchy. So we want
534 * the subsystem state from the new
535 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400536 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700537 } else {
538 /* Subsystem is not in this hierarchy, so we
539 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700541 }
542 }
543
Li Zefan0ac801f2013-01-10 11:49:27 +0800544 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700545 hash_for_each_possible(css_set_table, cset, hlist, key) {
546 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700547 continue;
548
549 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700551 }
Paul Menage817929e2007-10-18 23:39:36 -0700552
553 /* No existing cgroup group matched */
554 return NULL;
555}
556
Tejun Heo69d02062013-06-12 21:04:50 -0700557static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700558{
Tejun Heo69d02062013-06-12 21:04:50 -0700559 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700560
Tejun Heo69d02062013-06-12 21:04:50 -0700561 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
562 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700563 kfree(link);
564 }
565}
566
Tejun Heo69d02062013-06-12 21:04:50 -0700567/**
568 * allocate_cgrp_cset_links - allocate cgrp_cset_links
569 * @count: the number of links to allocate
570 * @tmp_links: list_head the allocated links are put on
571 *
572 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
573 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700574 */
Tejun Heo69d02062013-06-12 21:04:50 -0700575static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700576{
Tejun Heo69d02062013-06-12 21:04:50 -0700577 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700578 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700579
580 INIT_LIST_HEAD(tmp_links);
581
Li Zefan36553432008-07-29 22:33:19 -0700582 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700583 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700584 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700585 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700586 return -ENOMEM;
587 }
Tejun Heo69d02062013-06-12 21:04:50 -0700588 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700589 }
590 return 0;
591}
592
Li Zefanc12f65d2009-01-07 18:07:42 -0800593/**
594 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700595 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700596 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800597 * @cgrp: the destination cgroup
598 */
Tejun Heo69d02062013-06-12 21:04:50 -0700599static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
600 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800601{
Tejun Heo69d02062013-06-12 21:04:50 -0700602 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800603
Tejun Heo69d02062013-06-12 21:04:50 -0700604 BUG_ON(list_empty(tmp_links));
605 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
606 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700607 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700608 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700609 /*
610 * Always add links to the tail of the list so that the list
611 * is sorted by order of hierarchy creation
612 */
Tejun Heo69d02062013-06-12 21:04:50 -0700613 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800614}
615
Tejun Heob326f9d2013-06-24 15:21:48 -0700616/**
617 * find_css_set - return a new css_set with one cgroup updated
618 * @old_cset: the baseline css_set
619 * @cgrp: the cgroup to be updated
620 *
621 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
622 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700623 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700624static struct css_set *find_css_set(struct css_set *old_cset,
625 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700626{
Tejun Heob326f9d2013-06-24 15:21:48 -0700627 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700628 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700629 struct list_head tmp_links;
630 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800631 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700632
Tejun Heob326f9d2013-06-24 15:21:48 -0700633 lockdep_assert_held(&cgroup_mutex);
634
Paul Menage817929e2007-10-18 23:39:36 -0700635 /* First see if we already have a cgroup group that matches
636 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700637 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700638 cset = find_existing_css_set(old_cset, cgrp, template);
639 if (cset)
640 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700641 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700642
Tejun Heo5abb8852013-06-12 21:04:49 -0700643 if (cset)
644 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700645
Tejun Heof4f4be22013-06-12 21:04:51 -0700646 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700647 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700648 return NULL;
649
Tejun Heo69d02062013-06-12 21:04:50 -0700650 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700651 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700652 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700653 return NULL;
654 }
655
Tejun Heo5abb8852013-06-12 21:04:49 -0700656 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700657 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700658 INIT_LIST_HEAD(&cset->tasks);
659 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700660
661 /* Copy the set of subsystem state objects generated in
662 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700663 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700664
665 write_lock(&css_set_lock);
666 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700667 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700668 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700669
Paul Menage7717f7b2009-09-23 15:56:22 -0700670 if (c->root == cgrp->root)
671 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700672 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700673 }
Paul Menage817929e2007-10-18 23:39:36 -0700674
Tejun Heo69d02062013-06-12 21:04:50 -0700675 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700676
Paul Menage817929e2007-10-18 23:39:36 -0700677 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700678
679 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700680 key = css_set_hash(cset->subsys);
681 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700682
Paul Menage817929e2007-10-18 23:39:36 -0700683 write_unlock(&css_set_lock);
684
Tejun Heo5abb8852013-06-12 21:04:49 -0700685 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700686}
687
Tejun Heo2bd59d42014-02-11 11:52:49 -0500688static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
689{
690 struct cgroup *top_cgrp = kf_root->kn->priv;
691
692 return top_cgrp->root;
693}
694
Tejun Heof2e85d52014-02-11 11:52:49 -0500695static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
696{
697 int id;
698
699 lockdep_assert_held(&cgroup_mutex);
700
701 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
702 GFP_KERNEL);
703 if (id < 0)
704 return id;
705
706 root->hierarchy_id = id;
707 return 0;
708}
709
710static void cgroup_exit_root_id(struct cgroupfs_root *root)
711{
712 lockdep_assert_held(&cgroup_mutex);
713
714 if (root->hierarchy_id) {
715 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
716 root->hierarchy_id = 0;
717 }
718}
719
720static void cgroup_free_root(struct cgroupfs_root *root)
721{
722 if (root) {
723 /* hierarhcy ID shoulid already have been released */
724 WARN_ON_ONCE(root->hierarchy_id);
725
726 idr_destroy(&root->cgroup_idr);
727 kfree(root);
728 }
729}
730
Tejun Heo59f52962014-02-11 11:52:49 -0500731static void cgroup_get_root(struct cgroupfs_root *root)
732{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500733 /*
734 * The caller must ensure that @root is alive, which can be
735 * achieved by holding a ref on one of the member cgroups or
736 * following a registered reference to @root while holding
737 * cgroup_tree_mutex.
738 */
739 WARN_ON_ONCE(atomic_read(&root->refcnt) <= 0);
740 atomic_inc(&root->refcnt);
Tejun Heo59f52962014-02-11 11:52:49 -0500741}
742
743static void cgroup_put_root(struct cgroupfs_root *root)
744{
Tejun Heof2e85d52014-02-11 11:52:49 -0500745 struct cgroup *cgrp = &root->top_cgroup;
746 struct cgrp_cset_link *link, *tmp_link;
747 int ret;
748
Tejun Heo2bd59d42014-02-11 11:52:49 -0500749 /*
750 * @root's refcnt reaching zero and its deregistration should be
751 * atomic w.r.t. cgroup_tree_mutex. This ensures that
752 * cgroup_get_root() is safe to invoke if @root is registered.
753 */
754 mutex_lock(&cgroup_tree_mutex);
755 if (!atomic_dec_and_test(&root->refcnt)) {
756 mutex_unlock(&cgroup_tree_mutex);
757 return;
758 }
759 mutex_lock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500760
761 BUG_ON(root->number_of_cgroups != 1);
762 BUG_ON(!list_empty(&cgrp->children));
763
Tejun Heof2e85d52014-02-11 11:52:49 -0500764 /* Rebind all subsystems back to the default hierarchy */
765 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
766 ret = rebind_subsystems(root, 0, root->subsys_mask);
767 /* Shouldn't be able to fail ... */
768 BUG_ON(ret);
769 }
770
771 /*
772 * Release all the links from cset_links to this hierarchy's
773 * root cgroup
774 */
775 write_lock(&css_set_lock);
776
777 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
778 list_del(&link->cset_link);
779 list_del(&link->cgrp_link);
780 kfree(link);
781 }
782 write_unlock(&css_set_lock);
783
784 if (!list_empty(&root->root_list)) {
785 list_del(&root->root_list);
786 cgroup_root_count--;
787 }
788
789 cgroup_exit_root_id(root);
790
791 mutex_unlock(&cgroup_mutex);
792 mutex_unlock(&cgroup_tree_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -0500793
Tejun Heo2bd59d42014-02-11 11:52:49 -0500794 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -0500795 cgroup_free_root(root);
796}
797
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700799 * Return the cgroup for "task" from the given hierarchy. Must be
800 * called with cgroup_mutex held.
801 */
802static struct cgroup *task_cgroup_from_root(struct task_struct *task,
803 struct cgroupfs_root *root)
804{
Tejun Heo5abb8852013-06-12 21:04:49 -0700805 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700806 struct cgroup *res = NULL;
807
808 BUG_ON(!mutex_is_locked(&cgroup_mutex));
809 read_lock(&css_set_lock);
810 /*
811 * No need to lock the task - since we hold cgroup_mutex the
812 * task can't change groups, so the only thing that can happen
813 * is that it exits and its css is set back to init_css_set.
814 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700815 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700816 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700817 res = &root->top_cgroup;
818 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700819 struct cgrp_cset_link *link;
820
821 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700822 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700823
Paul Menage7717f7b2009-09-23 15:56:22 -0700824 if (c->root == root) {
825 res = c;
826 break;
827 }
828 }
829 }
830 read_unlock(&css_set_lock);
831 BUG_ON(!res);
832 return res;
833}
834
835/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700836 * There is one global cgroup mutex. We also require taking
837 * task_lock() when dereferencing a task's cgroup subsys pointers.
838 * See "The task_lock() exception", at the end of this comment.
839 *
840 * A task must hold cgroup_mutex to modify cgroups.
841 *
842 * Any task can increment and decrement the count field without lock.
843 * So in general, code holding cgroup_mutex can't rely on the count
844 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800845 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846 * means that no tasks are currently attached, therefore there is no
847 * way a task attached to that cgroup can fork (the other way to
848 * increment the count). So code holding cgroup_mutex can safely
849 * assume that if the count is zero, it will stay zero. Similarly, if
850 * a task holds cgroup_mutex on a cgroup with zero count, it
851 * knows that the cgroup won't be removed, as cgroup_rmdir()
852 * needs that mutex.
853 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700854 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
855 * (usually) take cgroup_mutex. These are the two most performance
856 * critical pieces of code here. The exception occurs on cgroup_exit(),
857 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
858 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800859 * to the release agent with the name of the cgroup (path relative to
860 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700861 *
862 * A cgroup can only be deleted if both its 'count' of using tasks
863 * is zero, and its list of 'children' cgroups is empty. Since all
864 * tasks in the system use _some_ cgroup, and since there is always at
865 * least one task in the system (init, pid == 1), therefore, top_cgroup
866 * always has either children cgroups and/or using tasks. So we don't
867 * need a special hack to ensure that top_cgroup cannot be deleted.
868 *
869 * The task_lock() exception
870 *
871 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800872 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800873 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700874 * several performance critical places that need to reference
875 * task->cgroup without the expense of grabbing a system global
876 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800877 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700878 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
879 * the task_struct routinely used for such matters.
880 *
881 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800882 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700883 */
884
Tejun Heo628f7cd2013-06-28 16:24:11 -0700885static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500886static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700887static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700888
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500889static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
890 char *buf)
891{
892 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
893 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
894 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
895 cft->ss->name, cft->name);
896 else
897 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
898 return buf;
899}
900
Tejun Heof2e85d52014-02-11 11:52:49 -0500901/**
902 * cgroup_file_mode - deduce file mode of a control file
903 * @cft: the control file in question
904 *
905 * returns cft->mode if ->mode is not 0
906 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
907 * returns S_IRUGO if it has only a read handler
908 * returns S_IWUSR if it has only a write hander
909 */
910static umode_t cgroup_file_mode(const struct cftype *cft)
911{
912 umode_t mode = 0;
913
914 if (cft->mode)
915 return cft->mode;
916
917 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
918 mode |= S_IRUGO;
919
920 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
921 cft->trigger)
922 mode |= S_IWUSR;
923
924 return mode;
925}
926
Li Zefanbe445622013-01-24 14:31:42 +0800927static void cgroup_free_fn(struct work_struct *work)
928{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700929 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800930
931 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800932 cgrp->root->number_of_cgroups--;
933 mutex_unlock(&cgroup_mutex);
934
935 /*
Tejun Heo59f52962014-02-11 11:52:49 -0500936 * We get a ref to the parent, and put the ref when this cgroup is
937 * being freed, so it's guaranteed that the parent won't be
938 * destroyed before its children.
Li Zefan415cf072013-04-08 14:35:02 +0800939 */
Tejun Heo59f52962014-02-11 11:52:49 -0500940 cgroup_put(cgrp->parent);
Li Zefan415cf072013-04-08 14:35:02 +0800941
Tejun Heo59f52962014-02-11 11:52:49 -0500942 /* put the root reference that we took when we created the cgroup */
943 cgroup_put_root(cgrp->root);
Li Zefanbe445622013-01-24 14:31:42 +0800944
Tejun Heob1a21362013-11-29 10:42:58 -0500945 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800946
Tejun Heo6f305582014-02-12 09:29:50 -0500947 kernfs_put(cgrp->kn);
Li Zefanbe445622013-01-24 14:31:42 +0800948 kfree(cgrp);
949}
950
951static void cgroup_free_rcu(struct rcu_head *head)
952{
953 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
954
Tejun Heoea15f8c2013-06-13 19:27:42 -0700955 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500956 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800957}
958
Tejun Heo59f52962014-02-11 11:52:49 -0500959static void cgroup_get(struct cgroup *cgrp)
960{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500961 WARN_ON_ONCE(cgroup_is_dead(cgrp));
962 WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
963 atomic_inc(&cgrp->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964}
965
Tejun Heo59f52962014-02-11 11:52:49 -0500966static void cgroup_put(struct cgroup *cgrp)
967{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500968 if (!atomic_dec_and_test(&cgrp->refcnt))
969 return;
970 if (WARN_ON_ONCE(!cgroup_is_dead(cgrp)))
971 return;
Tejun Heo59f52962014-02-11 11:52:49 -0500972
Tejun Heo2bd59d42014-02-11 11:52:49 -0500973 /*
974 * XXX: cgrp->id is only used to look up css's. As cgroup and
975 * css's lifetimes will be decoupled, it should be made
976 * per-subsystem and moved to css->id so that lookups are
977 * successful until the target css is released.
978 */
979 mutex_lock(&cgroup_mutex);
980 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
981 mutex_unlock(&cgroup_mutex);
982 cgrp->id = -1;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983
Tejun Heo2bd59d42014-02-11 11:52:49 -0500984 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985}
986
Li Zefan2739d3c2013-01-21 18:18:33 +0800987static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500989 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990
Tejun Heoace2bee2014-02-11 11:52:47 -0500991 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500992 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -0700993}
994
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400995/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700996 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700997 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400998 * @subsys_mask: mask of the subsystem ids whose files should be removed
999 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001000static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001001{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001002 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07001003 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001004
Tejun Heob420ba72013-07-12 12:34:02 -07001005 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05001006 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07001007
1008 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001009 continue;
Tejun Heo0adb0702014-02-12 09:29:48 -05001010 list_for_each_entry(cfts, &ss->cfts, node)
1011 cgroup_addrm_files(cgrp, cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001012 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013}
1014
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001016 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017{
Paul Menagebd89aab2007-10-18 23:40:44 -07001018 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001019 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -07001020 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001021
Tejun Heoace2bee2014-02-11 11:52:47 -05001022 lockdep_assert_held(&cgroup_tree_mutex);
1023 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001024
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -05001026 for_each_subsys(ss, i)
1027 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
1028 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029
Tejun Heo31261212013-06-28 17:07:30 -07001030 ret = cgroup_populate_dir(cgrp, added_mask);
1031 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001032 return ret;
Tejun Heo31261212013-06-28 17:07:30 -07001033
1034 /*
1035 * Nothing can fail from this point on. Remove files for the
1036 * removed subsystems and rebind each subsystem.
1037 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001038 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001039 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -05001040 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001041
Tejun Heo30159ec2013-06-25 11:53:37 -07001042 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001044
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001045 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001047 BUG_ON(cgroup_css(cgrp, ss));
1048 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1049 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001050
Tejun Heo73e80ed2013-08-13 11:01:55 -04001051 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001052 cgroup_css(cgroup_dummy_top, ss));
1053 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001054
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001055 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001057 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001058
Ben Blumcf5d5942010-03-10 15:22:09 -08001059 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001060 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001061 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001063 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1064 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001065
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001067 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001068
Tejun Heoca8bdca2013-08-26 18:40:56 -04001069 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001070 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1071
Tejun Heo9871bf92013-06-24 15:21:47 -07001072 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001073 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 }
1075 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076
Tejun Heo1672d042013-06-25 18:04:54 -07001077 /*
1078 * Mark @root has finished binding subsystems. @root->subsys_mask
1079 * now matches the bound subsystems.
1080 */
1081 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001082 kernfs_activate(cgrp->kn);
Tejun Heo1672d042013-06-25 18:04:54 -07001083
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084 return 0;
1085}
1086
Tejun Heo2bd59d42014-02-11 11:52:49 -05001087static int cgroup_show_options(struct seq_file *seq,
1088 struct kernfs_root *kf_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001090 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001092 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093
Tejun Heob85d2042013-12-06 15:11:57 -05001094 for_each_subsys(ss, ssid)
1095 if (root->subsys_mask & (1 << ssid))
1096 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001097 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1098 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001099 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001101 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001102 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001103
1104 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001105 if (strlen(root->release_agent_path))
1106 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001107 spin_unlock(&release_agent_path_lock);
1108
Tejun Heo2260e7f2012-11-19 08:13:38 -08001109 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001110 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001111 if (strlen(root->name))
1112 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001113 return 0;
1114}
1115
1116struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001117 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001119 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001120 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001121 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001122 /* User explicitly requested empty subsystem */
1123 bool none;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124};
1125
Ben Blumaae8aab2010-03-10 15:22:07 -08001126/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001127 * Convert a hierarchy specifier into a bitmask of subsystems and
1128 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1129 * array. This function takes refcounts on subsystems to be used, unless it
1130 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001131 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001132static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001134 char *token, *o = data;
1135 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001137 struct cgroup_subsys *ss;
1138 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001139
Ben Blumaae8aab2010-03-10 15:22:07 -08001140 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1141
Li Zefanf9ab5b52009-06-17 16:26:33 -07001142#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001143 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001144#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001145
Paul Menagec6d57f32009-09-23 15:56:19 -07001146 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147
1148 while ((token = strsep(&o, ",")) != NULL) {
1149 if (!*token)
1150 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001151 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001152 /* Explicitly have no subsystems */
1153 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001154 continue;
1155 }
1156 if (!strcmp(token, "all")) {
1157 /* Mutually exclusive option 'all' + subsystem name */
1158 if (one_ss)
1159 return -EINVAL;
1160 all_ss = true;
1161 continue;
1162 }
Tejun Heo873fe092013-04-14 20:15:26 -07001163 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1164 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1165 continue;
1166 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001167 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001168 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001169 continue;
1170 }
1171 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001172 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001173 continue;
1174 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001175 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001176 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001177 continue;
1178 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001179 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001180 /* Specifying two release agents is forbidden */
1181 if (opts->release_agent)
1182 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001183 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001184 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001185 if (!opts->release_agent)
1186 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001187 continue;
1188 }
1189 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001190 const char *name = token + 5;
1191 /* Can't specify an empty name */
1192 if (!strlen(name))
1193 return -EINVAL;
1194 /* Must match [\w.-]+ */
1195 for (i = 0; i < strlen(name); i++) {
1196 char c = name[i];
1197 if (isalnum(c))
1198 continue;
1199 if ((c == '.') || (c == '-') || (c == '_'))
1200 continue;
1201 return -EINVAL;
1202 }
1203 /* Specifying two names is forbidden */
1204 if (opts->name)
1205 return -EINVAL;
1206 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001207 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001208 GFP_KERNEL);
1209 if (!opts->name)
1210 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001211
1212 continue;
1213 }
1214
Tejun Heo30159ec2013-06-25 11:53:37 -07001215 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001216 if (strcmp(token, ss->name))
1217 continue;
1218 if (ss->disabled)
1219 continue;
1220
1221 /* Mutually exclusive option 'all' + subsystem name */
1222 if (all_ss)
1223 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001224 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001225 one_ss = true;
1226
1227 break;
1228 }
1229 if (i == CGROUP_SUBSYS_COUNT)
1230 return -ENOENT;
1231 }
1232
1233 /*
1234 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001235 * otherwise if 'none', 'name=' and a subsystem name options
1236 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001237 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001238 if (all_ss || (!one_ss && !opts->none && !opts->name))
1239 for_each_subsys(ss, i)
1240 if (!ss->disabled)
1241 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001242
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001243 /* Consistency checks */
1244
Tejun Heo873fe092013-04-14 20:15:26 -07001245 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1246 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1247
1248 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1249 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1250 return -EINVAL;
1251 }
1252
1253 if (opts->cpuset_clone_children) {
1254 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1255 return -EINVAL;
1256 }
Tejun Heo86bf4b62014-02-12 09:29:48 -05001257
1258 if (opts->flags & CGRP_ROOT_XATTR)
1259 pr_warning("cgroup: sane_behavior: xattr is always available, flag unnecessary\n");
Tejun Heo873fe092013-04-14 20:15:26 -07001260 }
1261
Li Zefanf9ab5b52009-06-17 16:26:33 -07001262 /*
1263 * Option noprefix was introduced just for backward compatibility
1264 * with the old cpuset, so we allow noprefix only if mounting just
1265 * the cpuset subsystem.
1266 */
Tejun Heo93438622013-04-14 20:15:25 -07001267 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001268 return -EINVAL;
1269
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001270
1271 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001272 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001273 return -EINVAL;
1274
1275 /*
1276 * We either have to specify by name or by subsystems. (So all
1277 * empty hierarchies must have a name).
1278 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001279 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001280 return -EINVAL;
1281
1282 return 0;
1283}
1284
Tejun Heo2bd59d42014-02-11 11:52:49 -05001285static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286{
1287 int ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001288 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001289 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001290 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001291
Tejun Heo873fe092013-04-14 20:15:26 -07001292 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1293 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1294 return -EINVAL;
1295 }
1296
Tejun Heoace2bee2014-02-11 11:52:47 -05001297 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298 mutex_lock(&cgroup_mutex);
1299
1300 /* See what subsystems are wanted */
1301 ret = parse_cgroupfs_options(data, &opts);
1302 if (ret)
1303 goto out_unlock;
1304
Tejun Heoa8a648c2013-06-24 15:21:47 -07001305 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001306 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1307 task_tgid_nr(current), current->comm);
1308
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001309 added_mask = opts.subsys_mask & ~root->subsys_mask;
1310 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001311
Ben Blumcf5d5942010-03-10 15:22:09 -08001312 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001313 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001314 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001315 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1316 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1317 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001318 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001319 goto out_unlock;
1320 }
1321
Tejun Heof172e672013-06-28 17:07:30 -07001322 /* remounting is not allowed for populated hierarchies */
1323 if (root->number_of_cgroups > 1) {
1324 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001325 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001326 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001329 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331
Tejun Heo69e943b2014-02-08 10:36:58 -05001332 if (opts.release_agent) {
1333 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001334 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001335 spin_unlock(&release_agent_path_lock);
1336 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001337 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001338 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001339 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001341 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342 return ret;
1343}
1344
Paul Menagecc31edc2008-10-18 20:28:04 -07001345static void init_cgroup_housekeeping(struct cgroup *cgrp)
1346{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001347 atomic_set(&cgrp->refcnt, 1);
Paul Menagecc31edc2008-10-18 20:28:04 -07001348 INIT_LIST_HEAD(&cgrp->sibling);
1349 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo69d02062013-06-12 21:04:50 -07001350 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001351 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001352 INIT_LIST_HEAD(&cgrp->pidlists);
1353 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001354 cgrp->dummy_css.cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001355}
Paul Menagec6d57f32009-09-23 15:56:19 -07001356
Paul Menageddbcc7e2007-10-18 23:39:30 -07001357static void init_cgroup_root(struct cgroupfs_root *root)
1358{
Paul Menagebd89aab2007-10-18 23:40:44 -07001359 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001360
Tejun Heo2bd59d42014-02-11 11:52:49 -05001361 atomic_set(&root->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001362 INIT_LIST_HEAD(&root->root_list);
1363 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001364 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001365 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001366 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367}
1368
Paul Menagec6d57f32009-09-23 15:56:19 -07001369static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1370{
1371 struct cgroupfs_root *root;
1372
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001373 if (!opts->subsys_mask && !opts->none)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001374 return ERR_PTR(-EINVAL);
Paul Menagec6d57f32009-09-23 15:56:19 -07001375
1376 root = kzalloc(sizeof(*root), GFP_KERNEL);
1377 if (!root)
1378 return ERR_PTR(-ENOMEM);
1379
1380 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001381
Tejun Heo1672d042013-06-25 18:04:54 -07001382 /*
1383 * We need to set @root->subsys_mask now so that @root can be
1384 * matched by cgroup_test_super() before it finishes
1385 * initialization; otherwise, competing mounts with the same
1386 * options may try to bind the same subsystems instead of waiting
1387 * for the first one leading to unexpected mount errors.
1388 * SUBSYS_BOUND will be set once actual binding is complete.
1389 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001390 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001391 root->flags = opts->flags;
1392 if (opts->release_agent)
1393 strcpy(root->release_agent_path, opts->release_agent);
1394 if (opts->name)
1395 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001396 if (opts->cpuset_clone_children)
1397 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001398 return root;
1399}
1400
Tejun Heod427dfe2014-02-11 11:52:48 -05001401static int cgroup_setup_root(struct cgroupfs_root *root)
1402{
1403 LIST_HEAD(tmp_links);
Tejun Heod427dfe2014-02-11 11:52:48 -05001404 struct cgroup *root_cgrp = &root->top_cgroup;
Tejun Heod427dfe2014-02-11 11:52:48 -05001405 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001406 int i, ret;
1407
1408 lockdep_assert_held(&cgroup_tree_mutex);
1409 lockdep_assert_held(&cgroup_mutex);
Tejun Heod427dfe2014-02-11 11:52:48 -05001410
1411 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1412 if (ret < 0)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001413 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001414 root_cgrp->id = ret;
1415
Tejun Heod427dfe2014-02-11 11:52:48 -05001416 /*
1417 * We're accessing css_set_count without locking css_set_lock here,
1418 * but that's OK - it can only be increased by someone holding
1419 * cgroup_lock, and that's us. The worst that can happen is that we
1420 * have some link structures left over
1421 */
1422 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1423 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001424 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001425
1426 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1427 ret = cgroup_init_root_id(root, 2, 0);
1428 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001429 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001430
Tejun Heo2bd59d42014-02-11 11:52:49 -05001431 root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
1432 KERNFS_ROOT_CREATE_DEACTIVATED,
1433 root_cgrp);
1434 if (IS_ERR(root->kf_root)) {
1435 ret = PTR_ERR(root->kf_root);
1436 goto exit_root_id;
1437 }
1438 root_cgrp->kn = root->kf_root->kn;
Tejun Heod427dfe2014-02-11 11:52:48 -05001439
1440 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1441 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001442 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001443
1444 ret = rebind_subsystems(root, root->subsys_mask, 0);
1445 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05001446 goto destroy_root;
Tejun Heod427dfe2014-02-11 11:52:48 -05001447
1448 /*
1449 * There must be no failure case after here, since rebinding takes
1450 * care of subsystems' refcounts, which are explicitly dropped in
1451 * the failure exit path.
1452 */
1453 list_add(&root->root_list, &cgroup_roots);
1454 cgroup_root_count++;
1455
1456 /*
1457 * Link the top cgroup in this hierarchy into all the css_set
1458 * objects.
1459 */
1460 write_lock(&css_set_lock);
1461 hash_for_each(css_set_table, i, cset, hlist)
1462 link_css_set(&tmp_links, cset, root_cgrp);
1463 write_unlock(&css_set_lock);
1464
1465 BUG_ON(!list_empty(&root_cgrp->children));
1466 BUG_ON(root->number_of_cgroups != 1);
1467
Tejun Heo2bd59d42014-02-11 11:52:49 -05001468 kernfs_activate(root_cgrp->kn);
Tejun Heod427dfe2014-02-11 11:52:48 -05001469 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001470 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05001471
Tejun Heo2bd59d42014-02-11 11:52:49 -05001472destroy_root:
1473 kernfs_destroy_root(root->kf_root);
1474 root->kf_root = NULL;
1475exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05001476 cgroup_exit_root_id(root);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001477out:
Tejun Heod427dfe2014-02-11 11:52:48 -05001478 free_cgrp_cset_links(&tmp_links);
1479 return ret;
1480}
1481
Al Virof7e83572010-07-26 13:23:11 +04001482static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001484 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001485{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001486 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487 struct cgroup_sb_opts opts;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001488 struct dentry *dentry;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001489 int ret;
1490
1491 mutex_lock(&cgroup_tree_mutex);
1492 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001493
1494 /* First find the desired set of subsystems */
1495 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001496 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001497 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001498
Tejun Heo2bd59d42014-02-11 11:52:49 -05001499 /* look for a matching existing root */
1500 for_each_active_root(root) {
1501 bool name_match = false;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001502
Paul Menagec6d57f32009-09-23 15:56:19 -07001503 /*
Tejun Heo2bd59d42014-02-11 11:52:49 -05001504 * If we asked for a name then it must match. Also, if
1505 * name matches but sybsys_mask doesn't, we should fail.
1506 * Remember whether name matched.
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05001508 if (opts.name) {
1509 if (strcmp(opts.name, root->name))
1510 continue;
1511 name_match = true;
1512 }
1513
1514 /*
1515 * If we asked for subsystems (or explicitly for no
1516 * subsystems) then they must match.
1517 */
1518 if ((opts.subsys_mask || opts.none) &&
1519 (opts.subsys_mask != root->subsys_mask)) {
1520 if (!name_match)
1521 continue;
1522 ret = -EBUSY;
1523 goto out_unlock;
1524 }
Tejun Heo873fe092013-04-14 20:15:26 -07001525
Tejun Heoc7ba8282013-06-29 14:06:10 -07001526 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001527 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1528 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1529 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001530 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001531 } else {
1532 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1533 }
Tejun Heo873fe092013-04-14 20:15:26 -07001534 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05001535
1536 cgroup_get_root(root);
1537 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538 }
1539
Tejun Heo2bd59d42014-02-11 11:52:49 -05001540 /* no such thing, create a new one */
1541 root = cgroup_root_from_opts(&opts);
1542 if (IS_ERR(root)) {
1543 ret = PTR_ERR(root);
1544 goto out_unlock;
1545 }
1546
1547 ret = cgroup_setup_root(root);
1548 if (ret)
1549 cgroup_free_root(root);
1550
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001551out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001552 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001553 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001554
Paul Menagec6d57f32009-09-23 15:56:19 -07001555 kfree(opts.release_agent);
1556 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001557
Tejun Heo2bd59d42014-02-11 11:52:49 -05001558 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001559 return ERR_PTR(ret);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001560
1561 dentry = kernfs_mount(fs_type, flags, root->kf_root);
1562 if (IS_ERR(dentry))
1563 cgroup_put_root(root);
1564 return dentry;
1565}
1566
1567static void cgroup_kill_sb(struct super_block *sb)
1568{
1569 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1570 struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
1571
1572 cgroup_put_root(root);
1573 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574}
1575
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576static struct file_system_type cgroup_fs_type = {
1577 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001578 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579 .kill_sb = cgroup_kill_sb,
1580};
1581
Greg KH676db4a2010-08-05 13:53:35 -07001582static struct kobject *cgroup_kobj;
1583
Li Zefana043e3b2008-02-23 15:24:09 -08001584/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001585 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001586 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001587 * @buf: the buffer to write the path into
1588 * @buflen: the length of the buffer
1589 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001590 * Determine @task's cgroup on the first (the one with the lowest non-zero
1591 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1592 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1593 * cgroup controller callbacks.
1594 *
Tejun Heoe61734c2014-02-12 09:29:50 -05001595 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07001596 */
Tejun Heoe61734c2014-02-12 09:29:50 -05001597char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001598{
1599 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001600 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05001601 int hierarchy_id = 1;
1602 char *path = NULL;
Tejun Heo857a2be2013-04-14 20:50:08 -07001603
1604 mutex_lock(&cgroup_mutex);
1605
Tejun Heo913ffdb2013-07-11 16:34:48 -07001606 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1607
Tejun Heo857a2be2013-04-14 20:50:08 -07001608 if (root) {
1609 cgrp = task_cgroup_from_root(task, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05001610 path = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001611 } else {
1612 /* if no hierarchy exists, everyone is in "/" */
Tejun Heoe61734c2014-02-12 09:29:50 -05001613 if (strlcpy(buf, "/", buflen) < buflen)
1614 path = buf;
Tejun Heo857a2be2013-04-14 20:50:08 -07001615 }
1616
1617 mutex_unlock(&cgroup_mutex);
Tejun Heoe61734c2014-02-12 09:29:50 -05001618 return path;
Tejun Heo857a2be2013-04-14 20:50:08 -07001619}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001620EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001621
Ben Blum74a11662011-05-26 16:25:20 -07001622/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001623 * Control Group taskset
1624 */
Tejun Heo134d3372011-12-12 18:12:21 -08001625struct task_and_cgroup {
1626 struct task_struct *task;
1627 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001628 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001629};
1630
Tejun Heo2f7ee562011-12-12 18:12:21 -08001631struct cgroup_taskset {
1632 struct task_and_cgroup single;
1633 struct flex_array *tc_array;
1634 int tc_array_len;
1635 int idx;
1636 struct cgroup *cur_cgrp;
1637};
1638
1639/**
1640 * cgroup_taskset_first - reset taskset and return the first task
1641 * @tset: taskset of interest
1642 *
1643 * @tset iteration is initialized and the first task is returned.
1644 */
1645struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1646{
1647 if (tset->tc_array) {
1648 tset->idx = 0;
1649 return cgroup_taskset_next(tset);
1650 } else {
1651 tset->cur_cgrp = tset->single.cgrp;
1652 return tset->single.task;
1653 }
1654}
1655EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1656
1657/**
1658 * cgroup_taskset_next - iterate to the next task in taskset
1659 * @tset: taskset of interest
1660 *
1661 * Return the next task in @tset. Iteration must have been initialized
1662 * with cgroup_taskset_first().
1663 */
1664struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1665{
1666 struct task_and_cgroup *tc;
1667
1668 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1669 return NULL;
1670
1671 tc = flex_array_get(tset->tc_array, tset->idx++);
1672 tset->cur_cgrp = tc->cgrp;
1673 return tc->task;
1674}
1675EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1676
1677/**
Tejun Heod99c8722013-08-08 20:11:27 -04001678 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001679 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001680 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001681 *
Tejun Heod99c8722013-08-08 20:11:27 -04001682 * Return the css for the current (last returned) task of @tset for
1683 * subsystem specified by @subsys_id. This function must be preceded by
1684 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001685 */
Tejun Heod99c8722013-08-08 20:11:27 -04001686struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1687 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001688{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001689 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001690}
Tejun Heod99c8722013-08-08 20:11:27 -04001691EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001692
1693/**
1694 * cgroup_taskset_size - return the number of tasks in taskset
1695 * @tset: taskset of interest
1696 */
1697int cgroup_taskset_size(struct cgroup_taskset *tset)
1698{
1699 return tset->tc_array ? tset->tc_array_len : 1;
1700}
1701EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1702
1703
Ben Blum74a11662011-05-26 16:25:20 -07001704/*
1705 * cgroup_task_migrate - move a task from one cgroup to another.
1706 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001707 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001708 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001709static void cgroup_task_migrate(struct cgroup *old_cgrp,
1710 struct task_struct *tsk,
1711 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001712{
Tejun Heo5abb8852013-06-12 21:04:49 -07001713 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001714
1715 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001716 * We are synchronized through threadgroup_lock() against PF_EXITING
1717 * setting such that we can't race against cgroup_exit() changing the
1718 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001719 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001720 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001721 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001722
Ben Blum74a11662011-05-26 16:25:20 -07001723 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001724 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001725 task_unlock(tsk);
1726
1727 /* Update the css_set linked lists if we're using them */
1728 write_lock(&css_set_lock);
1729 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001730 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001731 write_unlock(&css_set_lock);
1732
1733 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001734 * We just gained a reference on old_cset by taking it from the
1735 * task. As trading it for new_cset is protected by cgroup_mutex,
1736 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001737 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001738 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1739 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001740}
1741
Li Zefana043e3b2008-02-23 15:24:09 -08001742/**
Li Zefan081aa452013-03-13 09:17:09 +08001743 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001744 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001745 * @tsk: the task or the leader of the threadgroup to be attached
1746 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001747 *
Tejun Heo257058a2011-12-12 18:12:21 -08001748 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001749 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001750 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001751static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1752 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001753{
1754 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001755 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001756 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001757 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001758 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001759 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001760 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001761 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001762
1763 /*
1764 * step 0: in order to do expensive, possibly blocking operations for
1765 * every thread, we cannot iterate the thread group list, since it needs
1766 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001767 * group - group_rwsem prevents new threads from appearing, and if
1768 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001769 */
Li Zefan081aa452013-03-13 09:17:09 +08001770 if (threadgroup)
1771 group_size = get_nr_threads(tsk);
1772 else
1773 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001774 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001775 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001776 if (!group)
1777 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001778 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001779 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001780 if (retval)
1781 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001782
Ben Blum74a11662011-05-26 16:25:20 -07001783 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001784 /*
1785 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1786 * already PF_EXITING could be freed from underneath us unless we
1787 * take an rcu_read_lock.
1788 */
1789 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001790 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001791 struct task_and_cgroup ent;
1792
Tejun Heocd3d0952011-12-12 18:12:21 -08001793 /* @tsk either already exited or can't exit until the end */
1794 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001795 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001796
Ben Blum74a11662011-05-26 16:25:20 -07001797 /* as per above, nr_threads may decrease, but not increase. */
1798 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001799 ent.task = tsk;
1800 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001801 /* nothing to do if this task is already in the cgroup */
1802 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001803 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001804 /*
1805 * saying GFP_ATOMIC has no effect here because we did prealloc
1806 * earlier, but it's good form to communicate our expectations.
1807 */
Tejun Heo134d3372011-12-12 18:12:21 -08001808 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001809 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001810 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001811 next:
Li Zefan081aa452013-03-13 09:17:09 +08001812 if (!threadgroup)
1813 break;
Ben Blum74a11662011-05-26 16:25:20 -07001814 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001815 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07001816 /* remember the number of threads in the array for later. */
1817 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001818 tset.tc_array = group;
1819 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001820
Tejun Heo134d3372011-12-12 18:12:21 -08001821 /* methods shouldn't be called if no task is actually migrating */
1822 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001823 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08001824 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08001825
Ben Blum74a11662011-05-26 16:25:20 -07001826 /*
1827 * step 1: check that we can legitimately attach to the cgroup.
1828 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001829 for_each_css(css, i, cgrp) {
1830 if (css->ss->can_attach) {
1831 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001832 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001833 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07001834 goto out_cancel_attach;
1835 }
1836 }
Ben Blum74a11662011-05-26 16:25:20 -07001837 }
1838
1839 /*
1840 * step 2: make sure css_sets exist for all threads to be migrated.
1841 * we use find_css_set, which allocates a new one if necessary.
1842 */
Ben Blum74a11662011-05-26 16:25:20 -07001843 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07001844 struct css_set *old_cset;
1845
Tejun Heo134d3372011-12-12 18:12:21 -08001846 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001847 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001848 tc->cset = find_css_set(old_cset, cgrp);
1849 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001850 retval = -ENOMEM;
1851 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07001852 }
1853 }
1854
1855 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001856 * step 3: now that we're guaranteed success wrt the css_sets,
1857 * proceed to move all tasks to the new cgroup. There are no
1858 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07001859 */
Ben Blum74a11662011-05-26 16:25:20 -07001860 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08001861 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001862 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07001863 }
1864 /* nothing is sensitive to fork() after this point. */
1865
1866 /*
Tejun Heo494c1672011-12-12 18:12:22 -08001867 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07001868 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05001869 for_each_css(css, i, cgrp)
1870 if (css->ss->attach)
1871 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001872
1873 /*
1874 * step 5: success! and cleanup
1875 */
Ben Blum74a11662011-05-26 16:25:20 -07001876 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001877out_put_css_set_refs:
1878 if (retval) {
1879 for (i = 0; i < group_size; i++) {
1880 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08001881 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001882 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001883 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001884 }
Ben Blum74a11662011-05-26 16:25:20 -07001885 }
1886out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07001887 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05001888 for_each_css(css, i, cgrp) {
1889 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07001890 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001891 if (css->ss->cancel_attach)
1892 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07001893 }
1894 }
Ben Blum74a11662011-05-26 16:25:20 -07001895out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07001896 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07001897 return retval;
1898}
1899
1900/*
1901 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08001902 * function to attach either it or all tasks in its threadgroup. Will lock
1903 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07001904 */
1905static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001906{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001907 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001908 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001909 int ret;
1910
Ben Blum74a11662011-05-26 16:25:20 -07001911 if (!cgroup_lock_live_group(cgrp))
1912 return -ENODEV;
1913
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001914retry_find_task:
1915 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001916 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001917 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07001918 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07001919 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001920 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001921 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001922 }
Ben Blum74a11662011-05-26 16:25:20 -07001923 /*
1924 * even if we're attaching all tasks in the thread group, we
1925 * only need to check permissions on one of them.
1926 */
David Howellsc69e8d92008-11-14 10:39:19 +11001927 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07001928 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
1929 !uid_eq(cred->euid, tcred->uid) &&
1930 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001931 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001932 ret = -EACCES;
1933 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001934 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001935 } else
1936 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08001937
1938 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001939 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001940
1941 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07001942 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001943 * trapped in a cpuset, or RT worker may be born in a cgroup
1944 * with no rt_runtime allocated. Just say no.
1945 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07001946 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02001947 ret = -EINVAL;
1948 rcu_read_unlock();
1949 goto out_unlock_cgroup;
1950 }
1951
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001952 get_task_struct(tsk);
1953 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08001954
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001955 threadgroup_lock(tsk);
1956 if (threadgroup) {
1957 if (!thread_group_leader(tsk)) {
1958 /*
1959 * a race with de_thread from another thread's exec()
1960 * may strip us of our leadership, if this happens,
1961 * there is no choice but to throw this task away and
1962 * try again; this is
1963 * "double-double-toil-and-trouble-check locking".
1964 */
1965 threadgroup_unlock(tsk);
1966 put_task_struct(tsk);
1967 goto retry_find_task;
1968 }
Li Zefan081aa452013-03-13 09:17:09 +08001969 }
1970
1971 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
1972
Tejun Heocd3d0952011-12-12 18:12:21 -08001973 threadgroup_unlock(tsk);
1974
Paul Menagebbcb81d2007-10-18 23:39:32 -07001975 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08001976out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07001977 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001978 return ret;
1979}
1980
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001981/**
1982 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1983 * @from: attach to all cgroups of a given task
1984 * @tsk: the task to be attached
1985 */
1986int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
1987{
1988 struct cgroupfs_root *root;
1989 int retval = 0;
1990
Tejun Heo47cfcd02013-04-07 09:29:51 -07001991 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001992 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08001993 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001994
Li Zefan6f4b7e62013-07-31 16:18:36 +08001995 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07001996 if (retval)
1997 break;
1998 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07001999 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002000
2001 return retval;
2002}
2003EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2004
Tejun Heo182446d2013-08-08 20:11:24 -04002005static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2006 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002007{
Tejun Heo182446d2013-08-08 20:11:24 -04002008 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002009}
2010
Tejun Heo182446d2013-08-08 20:11:24 -04002011static int cgroup_procs_write(struct cgroup_subsys_state *css,
2012 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002013{
Tejun Heo182446d2013-08-08 20:11:24 -04002014 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002015}
2016
Tejun Heo182446d2013-08-08 20:11:24 -04002017static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2018 struct cftype *cft, const char *buffer)
Paul Menagee788e0662008-07-25 01:46:59 -07002019{
Tejun Heo5f469902014-02-11 11:52:48 -05002020 struct cgroupfs_root *root = css->cgroup->root;
2021
2022 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002023 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e0662008-07-25 01:46:59 -07002024 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002025 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002026 strlcpy(root->release_agent_path, buffer,
2027 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002028 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002029 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002030 return 0;
2031}
2032
Tejun Heo2da8ca82013-12-05 12:28:04 -05002033static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e0662008-07-25 01:46:59 -07002034{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002035 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002036
Paul Menagee788e0662008-07-25 01:46:59 -07002037 if (!cgroup_lock_live_group(cgrp))
2038 return -ENODEV;
2039 seq_puts(seq, cgrp->root->release_agent_path);
2040 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002041 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002042 return 0;
2043}
2044
Tejun Heo2da8ca82013-12-05 12:28:04 -05002045static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002046{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002047 struct cgroup *cgrp = seq_css(seq)->cgroup;
2048
2049 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002050 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002051}
2052
Tejun Heo2bd59d42014-02-11 11:52:49 -05002053static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2054 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002055{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002056 struct cgroup *cgrp = of->kn->parent->priv;
2057 struct cftype *cft = of->kn->priv;
2058 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05002059 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002060
Tejun Heo2bd59d42014-02-11 11:52:49 -05002061 /*
2062 * kernfs guarantees that a file isn't deleted with operations in
2063 * flight, which means that the matching css is and stays alive and
2064 * doesn't need to be pinned. The RCU locking is not necessary
2065 * either. It's just for the convenience of using cgroup_css().
2066 */
2067 rcu_read_lock();
2068 css = cgroup_css(cgrp, cft->ss);
2069 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07002070
Tejun Heoa742c592013-12-05 12:28:03 -05002071 if (cft->write_string) {
2072 ret = cft->write_string(css, cft, strstrip(buf));
2073 } else if (cft->write_u64) {
2074 unsigned long long v;
2075 ret = kstrtoull(buf, 0, &v);
2076 if (!ret)
2077 ret = cft->write_u64(css, cft, v);
2078 } else if (cft->write_s64) {
2079 long long v;
2080 ret = kstrtoll(buf, 0, &v);
2081 if (!ret)
2082 ret = cft->write_s64(css, cft, v);
2083 } else if (cft->trigger) {
2084 ret = cft->trigger(css, (unsigned int)cft->private);
2085 } else {
2086 ret = -EINVAL;
2087 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05002088
Tejun Heoa742c592013-12-05 12:28:03 -05002089 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002090}
2091
Tejun Heo6612f052013-12-05 12:28:04 -05002092static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002093{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002094 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002095}
2096
2097static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2098{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002099 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05002100}
2101
2102static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2103{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002104 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002105}
2106
2107static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2108{
Tejun Heo7da11272013-12-05 12:28:04 -05002109 struct cftype *cft = seq_cft(m);
2110 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002111
Tejun Heo2da8ca82013-12-05 12:28:04 -05002112 if (cft->seq_show)
2113 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002114
Tejun Heo896f5192013-12-05 12:28:04 -05002115 if (cft->read_u64)
2116 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2117 else if (cft->read_s64)
2118 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2119 else
2120 return -EINVAL;
2121 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002122}
2123
Tejun Heo2bd59d42014-02-11 11:52:49 -05002124static struct kernfs_ops cgroup_kf_single_ops = {
2125 .atomic_write_len = PAGE_SIZE,
2126 .write = cgroup_file_write,
2127 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002128};
2129
Tejun Heo2bd59d42014-02-11 11:52:49 -05002130static struct kernfs_ops cgroup_kf_ops = {
2131 .atomic_write_len = PAGE_SIZE,
2132 .write = cgroup_file_write,
2133 .seq_start = cgroup_seqfile_start,
2134 .seq_next = cgroup_seqfile_next,
2135 .seq_stop = cgroup_seqfile_stop,
2136 .seq_show = cgroup_seqfile_show,
2137};
Paul Menageddbcc7e2007-10-18 23:39:30 -07002138
2139/*
2140 * cgroup_rename - Only allow simple rename of directories in place.
2141 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05002142static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
2143 const char *new_name_str)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002144{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002145 struct cgroup *cgrp = kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002146 int ret;
Li Zefan65dff752013-03-01 15:01:56 +08002147
Tejun Heo2bd59d42014-02-11 11:52:49 -05002148 if (kernfs_type(kn) != KERNFS_DIR)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002149 return -ENOTDIR;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002150 if (kn->parent != new_parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002151 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002152
Tejun Heo6db8e852013-06-14 11:18:22 -07002153 /*
2154 * This isn't a proper migration and its usefulness is very
2155 * limited. Disallow if sane_behavior.
2156 */
2157 if (cgroup_sane_behavior(cgrp))
2158 return -EPERM;
2159
Tejun Heo2bd59d42014-02-11 11:52:49 -05002160 mutex_lock(&cgroup_tree_mutex);
2161 mutex_lock(&cgroup_mutex);
2162
2163 ret = kernfs_rename(kn, new_parent, new_name_str);
Li Zefan65dff752013-03-01 15:01:56 +08002164
Tejun Heo2bd59d42014-02-11 11:52:49 -05002165 mutex_unlock(&cgroup_mutex);
2166 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002167 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002168}
2169
Tejun Heo2bb566c2013-08-08 20:11:23 -04002170static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002171{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002172 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05002173 struct kernfs_node *kn;
2174 struct lock_class_key *key = NULL;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002175
Tejun Heo2bd59d42014-02-11 11:52:49 -05002176#ifdef CONFIG_DEBUG_LOCK_ALLOC
2177 key = &cft->lockdep_key;
2178#endif
2179 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
2180 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
2181 NULL, false, key);
2182 if (IS_ERR(kn))
2183 return PTR_ERR(kn);
2184 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002185}
2186
Tejun Heob1f28d32013-06-28 16:24:10 -07002187/**
2188 * cgroup_addrm_files - add or remove files to a cgroup directory
2189 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002190 * @cfts: array of cftypes to be added
2191 * @is_add: whether to add or remove
2192 *
2193 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002194 * For removals, this function never fails. If addition fails, this
2195 * function doesn't remove files already added. The caller is responsible
2196 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002197 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002198static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2199 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002200{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002201 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002202 int ret;
2203
Tejun Heoace2bee2014-02-11 11:52:47 -05002204 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002205
2206 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002207 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002208 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2209 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002210 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2211 continue;
2212 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2213 continue;
2214
Li Zefan2739d3c2013-01-21 18:18:33 +08002215 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002216 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002217 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002218 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002219 cft->name, ret);
2220 return ret;
2221 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002222 } else {
2223 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002224 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002225 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002226 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002227}
2228
Tejun Heo21a2d342014-02-12 09:29:49 -05002229static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002230{
2231 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002232 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002233 struct cgroup *root = &ss->root->top_cgroup;
Tejun Heo492eb212013-08-08 20:11:25 -04002234 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07002235 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002236
Tejun Heo21a2d342014-02-12 09:29:49 -05002237 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo4ac06012014-02-11 11:52:47 -05002238
Tejun Heo21a2d342014-02-12 09:29:49 -05002239 /* don't bother if @ss isn't attached */
2240 if (ss->root == &cgroup_dummy_root)
Tejun Heo9ccece82013-06-28 16:24:11 -07002241 return 0;
Li Zefane8c82d22013-06-18 18:48:37 +08002242
Li Zefane8c82d22013-06-18 18:48:37 +08002243 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002244 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002245 struct cgroup *cgrp = css->cgroup;
2246
Li Zefane8c82d22013-06-18 18:48:37 +08002247 if (cgroup_is_dead(cgrp))
2248 continue;
2249
Tejun Heo21a2d342014-02-12 09:29:49 -05002250 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07002251 if (ret)
2252 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002253 }
Tejun Heo21a2d342014-02-12 09:29:49 -05002254
2255 if (is_add && !ret)
2256 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07002257 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002258}
2259
Tejun Heo2da440a2014-02-11 11:52:48 -05002260static void cgroup_exit_cftypes(struct cftype *cfts)
2261{
2262 struct cftype *cft;
2263
Tejun Heo2bd59d42014-02-11 11:52:49 -05002264 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2265 /* free copy for custom atomic_write_len, see init_cftypes() */
2266 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
2267 kfree(cft->kf_ops);
2268 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05002269 cft->ss = NULL;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002270 }
Tejun Heo2da440a2014-02-11 11:52:48 -05002271}
2272
Tejun Heo2bd59d42014-02-11 11:52:49 -05002273static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05002274{
2275 struct cftype *cft;
2276
Tejun Heo2bd59d42014-02-11 11:52:49 -05002277 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2278 struct kernfs_ops *kf_ops;
2279
Tejun Heo0adb0702014-02-12 09:29:48 -05002280 WARN_ON(cft->ss || cft->kf_ops);
2281
Tejun Heo2bd59d42014-02-11 11:52:49 -05002282 if (cft->seq_start)
2283 kf_ops = &cgroup_kf_ops;
2284 else
2285 kf_ops = &cgroup_kf_single_ops;
2286
2287 /*
2288 * Ugh... if @cft wants a custom max_write_len, we need to
2289 * make a copy of kf_ops to set its atomic_write_len.
2290 */
2291 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
2292 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
2293 if (!kf_ops) {
2294 cgroup_exit_cftypes(cfts);
2295 return -ENOMEM;
2296 }
2297 kf_ops->atomic_write_len = cft->max_write_len;
2298 }
2299
2300 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05002301 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002302 }
2303
2304 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05002305}
2306
Tejun Heo21a2d342014-02-12 09:29:49 -05002307static int cgroup_rm_cftypes_locked(struct cftype *cfts)
2308{
2309 lockdep_assert_held(&cgroup_tree_mutex);
2310
2311 if (!cfts || !cfts[0].ss)
2312 return -ENOENT;
2313
2314 list_del(&cfts->node);
2315 cgroup_apply_cftypes(cfts, false);
2316 cgroup_exit_cftypes(cfts);
2317 return 0;
2318}
2319
Tejun Heo8e3f6542012-04-01 12:09:55 -07002320/**
Tejun Heo80b13582014-02-12 09:29:48 -05002321 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2322 * @cfts: zero-length name terminated array of cftypes
2323 *
2324 * Unregister @cfts. Files described by @cfts are removed from all
2325 * existing cgroups and all future cgroups won't have them either. This
2326 * function can be called anytime whether @cfts' subsys is attached or not.
2327 *
2328 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2329 * registered.
2330 */
2331int cgroup_rm_cftypes(struct cftype *cfts)
2332{
Tejun Heo21a2d342014-02-12 09:29:49 -05002333 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002334
Tejun Heo21a2d342014-02-12 09:29:49 -05002335 mutex_lock(&cgroup_tree_mutex);
2336 ret = cgroup_rm_cftypes_locked(cfts);
2337 mutex_unlock(&cgroup_tree_mutex);
2338 return ret;
Tejun Heo80b13582014-02-12 09:29:48 -05002339}
2340
2341/**
Tejun Heo8e3f6542012-04-01 12:09:55 -07002342 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2343 * @ss: target cgroup subsystem
2344 * @cfts: zero-length name terminated array of cftypes
2345 *
2346 * Register @cfts to @ss. Files described by @cfts are created for all
2347 * existing cgroups to which @ss is attached and all future cgroups will
2348 * have them too. This function can be called anytime whether @ss is
2349 * attached or not.
2350 *
2351 * Returns 0 on successful registration, -errno on failure. Note that this
2352 * function currently returns 0 as long as @cfts registration is successful
2353 * even if some file creation attempts on existing cgroups fail.
2354 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002355int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002356{
Tejun Heo9ccece82013-06-28 16:24:11 -07002357 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002358
Tejun Heo2bd59d42014-02-11 11:52:49 -05002359 ret = cgroup_init_cftypes(ss, cfts);
2360 if (ret)
2361 return ret;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002362
Tejun Heo21a2d342014-02-12 09:29:49 -05002363 mutex_lock(&cgroup_tree_mutex);
2364
Tejun Heo0adb0702014-02-12 09:29:48 -05002365 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05002366 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002367 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05002368 cgroup_rm_cftypes_locked(cfts);
2369
2370 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002371 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002372}
2373EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2374
Li Zefana043e3b2008-02-23 15:24:09 -08002375/**
2376 * cgroup_task_count - count the number of tasks in a cgroup.
2377 * @cgrp: the cgroup in question
2378 *
2379 * Return the number of tasks in the cgroup.
2380 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002381int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002382{
2383 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002384 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002385
Paul Menage817929e2007-10-18 23:39:36 -07002386 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002387 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2388 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002389 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002390 return count;
2391}
2392
2393/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002394 * To reduce the fork() overhead for systems that are not actually using
2395 * their cgroups capability, we don't maintain the lists running through
2396 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002397 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002398 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002399static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002400{
2401 struct task_struct *p, *g;
2402 write_lock(&css_set_lock);
2403 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002404 /*
2405 * We need tasklist_lock because RCU is not safe against
2406 * while_each_thread(). Besides, a forking task that has passed
2407 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2408 * is not guaranteed to have its child immediately visible in the
2409 * tasklist if we walk through it with RCU.
2410 */
2411 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002412 do_each_thread(g, p) {
2413 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002414 /*
2415 * We should check if the process is exiting, otherwise
2416 * it will race with cgroup_exit() in that the list
2417 * entry won't be deleted though the process has exited.
2418 */
2419 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002420 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002421 task_unlock(p);
2422 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002423 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002424 write_unlock(&css_set_lock);
2425}
2426
Tejun Heo574bd9f2012-11-09 09:12:29 -08002427/**
Tejun Heo492eb212013-08-08 20:11:25 -04002428 * css_next_child - find the next child of a given css
2429 * @pos_css: the current position (%NULL to initiate traversal)
2430 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002431 *
Tejun Heo492eb212013-08-08 20:11:25 -04002432 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002433 * under either cgroup_mutex or RCU read lock. The only requirement is
2434 * that @parent_css and @pos_css are accessible. The next sibling is
2435 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002436 */
Tejun Heo492eb212013-08-08 20:11:25 -04002437struct cgroup_subsys_state *
2438css_next_child(struct cgroup_subsys_state *pos_css,
2439 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002440{
Tejun Heo492eb212013-08-08 20:11:25 -04002441 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2442 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002443 struct cgroup *next;
2444
Tejun Heoace2bee2014-02-11 11:52:47 -05002445 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002446
2447 /*
2448 * @pos could already have been removed. Once a cgroup is removed,
2449 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002450 * changes. As CGRP_DEAD assertion is serialized and happens
2451 * before the cgroup is taken off the ->sibling list, if we see it
2452 * unasserted, it's guaranteed that the next sibling hasn't
2453 * finished its grace period even if it's already removed, and thus
2454 * safe to dereference from this RCU critical section. If
2455 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2456 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002457 *
2458 * If @pos is dead, its next pointer can't be dereferenced;
2459 * however, as each cgroup is given a monotonically increasing
2460 * unique serial number and always appended to the sibling list,
2461 * the next one can be found by walking the parent's children until
2462 * we see a cgroup with higher serial number than @pos's. While
2463 * this path can be slower, it's taken only when either the current
2464 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002465 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002466 if (!pos) {
2467 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2468 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002469 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002470 } else {
2471 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2472 if (next->serial_nr > pos->serial_nr)
2473 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002474 }
2475
Tejun Heo492eb212013-08-08 20:11:25 -04002476 if (&next->sibling == &cgrp->children)
2477 return NULL;
2478
Tejun Heoca8bdca2013-08-26 18:40:56 -04002479 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002480}
Tejun Heo492eb212013-08-08 20:11:25 -04002481EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002482
2483/**
Tejun Heo492eb212013-08-08 20:11:25 -04002484 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002485 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002486 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002487 *
Tejun Heo492eb212013-08-08 20:11:25 -04002488 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002489 * to visit for pre-order traversal of @root's descendants. @root is
2490 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002491 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002492 * While this function requires cgroup_mutex or RCU read locking, it
2493 * doesn't require the whole traversal to be contained in a single critical
2494 * section. This function will return the correct next descendant as long
2495 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002496 */
Tejun Heo492eb212013-08-08 20:11:25 -04002497struct cgroup_subsys_state *
2498css_next_descendant_pre(struct cgroup_subsys_state *pos,
2499 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002500{
Tejun Heo492eb212013-08-08 20:11:25 -04002501 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002502
Tejun Heoace2bee2014-02-11 11:52:47 -05002503 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002504
Tejun Heobd8815a2013-08-08 20:11:27 -04002505 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002506 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002507 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002508
2509 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002510 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002511 if (next)
2512 return next;
2513
2514 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002515 while (pos != root) {
2516 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002517 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002518 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002519 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002520 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002521
2522 return NULL;
2523}
Tejun Heo492eb212013-08-08 20:11:25 -04002524EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002525
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002526/**
Tejun Heo492eb212013-08-08 20:11:25 -04002527 * css_rightmost_descendant - return the rightmost descendant of a css
2528 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002529 *
Tejun Heo492eb212013-08-08 20:11:25 -04002530 * Return the rightmost descendant of @pos. If there's no descendant, @pos
2531 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002532 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09002533 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002534 * While this function requires cgroup_mutex or RCU read locking, it
2535 * doesn't require the whole traversal to be contained in a single critical
2536 * section. This function will return the correct rightmost descendant as
2537 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002538 */
Tejun Heo492eb212013-08-08 20:11:25 -04002539struct cgroup_subsys_state *
2540css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002541{
Tejun Heo492eb212013-08-08 20:11:25 -04002542 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002543
Tejun Heoace2bee2014-02-11 11:52:47 -05002544 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002545
2546 do {
2547 last = pos;
2548 /* ->prev isn't RCU safe, walk ->next till the end */
2549 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04002550 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002551 pos = tmp;
2552 } while (pos);
2553
2554 return last;
2555}
Tejun Heo492eb212013-08-08 20:11:25 -04002556EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002557
Tejun Heo492eb212013-08-08 20:11:25 -04002558static struct cgroup_subsys_state *
2559css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002560{
Tejun Heo492eb212013-08-08 20:11:25 -04002561 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002562
2563 do {
2564 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04002565 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002566 } while (pos);
2567
2568 return last;
2569}
2570
2571/**
Tejun Heo492eb212013-08-08 20:11:25 -04002572 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002573 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002574 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002575 *
Tejun Heo492eb212013-08-08 20:11:25 -04002576 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002577 * to visit for post-order traversal of @root's descendants. @root is
2578 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002579 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002580 * While this function requires cgroup_mutex or RCU read locking, it
2581 * doesn't require the whole traversal to be contained in a single critical
2582 * section. This function will return the correct next descendant as long
2583 * as both @pos and @cgroup are accessible and @pos is a descendant of
2584 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002585 */
Tejun Heo492eb212013-08-08 20:11:25 -04002586struct cgroup_subsys_state *
2587css_next_descendant_post(struct cgroup_subsys_state *pos,
2588 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002589{
Tejun Heo492eb212013-08-08 20:11:25 -04002590 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002591
Tejun Heoace2bee2014-02-11 11:52:47 -05002592 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002593
Tejun Heo58b79a92013-09-06 15:31:08 -04002594 /* if first iteration, visit leftmost descendant which may be @root */
2595 if (!pos)
2596 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002597
Tejun Heobd8815a2013-08-08 20:11:27 -04002598 /* if we visited @root, we're done */
2599 if (pos == root)
2600 return NULL;
2601
Tejun Heo574bd9f2012-11-09 09:12:29 -08002602 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04002603 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002604 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04002605 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002606
2607 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04002608 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002609}
Tejun Heo492eb212013-08-08 20:11:25 -04002610EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002611
Tejun Heo0942eee2013-08-08 20:11:26 -04002612/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002613 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04002614 * @it: the iterator to advance
2615 *
2616 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04002617 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002618static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04002619{
2620 struct list_head *l = it->cset_link;
2621 struct cgrp_cset_link *link;
2622 struct css_set *cset;
2623
2624 /* Advance to the next non-empty css_set */
2625 do {
2626 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04002627 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04002628 it->cset_link = NULL;
2629 return;
2630 }
2631 link = list_entry(l, struct cgrp_cset_link, cset_link);
2632 cset = link->cset;
2633 } while (list_empty(&cset->tasks));
2634 it->cset_link = l;
2635 it->task = cset->tasks.next;
2636}
2637
Tejun Heo0942eee2013-08-08 20:11:26 -04002638/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002639 * css_task_iter_start - initiate task iteration
2640 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04002641 * @it: the task iterator to use
2642 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002643 * Initiate iteration through the tasks of @css. The caller can call
2644 * css_task_iter_next() to walk through the tasks until the function
2645 * returns NULL. On completion of iteration, css_task_iter_end() must be
2646 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04002647 *
2648 * Note that this function acquires a lock which is released when the
2649 * iteration finishes. The caller can't sleep while iteration is in
2650 * progress.
2651 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002652void css_task_iter_start(struct cgroup_subsys_state *css,
2653 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002654 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002655{
2656 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002657 * The first time anyone tries to iterate across a css, we need to
2658 * enable the list linking each css_set to its tasks, and fix up
2659 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07002660 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002661 if (!use_task_css_set_links)
2662 cgroup_enable_task_cg_lists();
2663
Paul Menage817929e2007-10-18 23:39:36 -07002664 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002665
Tejun Heo72ec7022013-08-08 20:11:26 -04002666 it->origin_css = css;
2667 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04002668
Tejun Heo72ec7022013-08-08 20:11:26 -04002669 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002670}
2671
Tejun Heo0942eee2013-08-08 20:11:26 -04002672/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002673 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04002674 * @it: the task iterator being iterated
2675 *
2676 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04002677 * initialized via css_task_iter_start(). Returns NULL when the iteration
2678 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04002679 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002680struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002681{
2682 struct task_struct *res;
2683 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07002684 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002685
2686 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07002687 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07002688 return NULL;
2689 res = list_entry(l, struct task_struct, cg_list);
2690 /* Advance iterator to find next entry */
2691 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002692 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
2693 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04002694 /*
2695 * We reached the end of this task list - move on to the
2696 * next cgrp_cset_link.
2697 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002698 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07002699 } else {
2700 it->task = l;
2701 }
2702 return res;
2703}
2704
Tejun Heo0942eee2013-08-08 20:11:26 -04002705/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002706 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04002707 * @it: the task iterator to finish
2708 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002709 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04002710 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002711void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02002712 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07002713{
2714 read_unlock(&css_set_lock);
2715}
2716
Cliff Wickman31a7df02008-02-07 00:14:42 -08002717static inline int started_after_time(struct task_struct *t1,
2718 struct timespec *time,
2719 struct task_struct *t2)
2720{
2721 int start_diff = timespec_compare(&t1->start_time, time);
2722 if (start_diff > 0) {
2723 return 1;
2724 } else if (start_diff < 0) {
2725 return 0;
2726 } else {
2727 /*
2728 * Arbitrarily, if two processes started at the same
2729 * time, we'll say that the lower pointer value
2730 * started first. Note that t2 may have exited by now
2731 * so this may not be a valid pointer any longer, but
2732 * that's fine - it still serves to distinguish
2733 * between two tasks started (effectively) simultaneously.
2734 */
2735 return t1 > t2;
2736 }
2737}
2738
2739/*
2740 * This function is a callback from heap_insert() and is used to order
2741 * the heap.
2742 * In this case we order the heap in descending task start time.
2743 */
2744static inline int started_after(void *p1, void *p2)
2745{
2746 struct task_struct *t1 = p1;
2747 struct task_struct *t2 = p2;
2748 return started_after_time(t1, &t2->start_time, t2);
2749}
2750
2751/**
Tejun Heo72ec7022013-08-08 20:11:26 -04002752 * css_scan_tasks - iterate though all the tasks in a css
2753 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04002754 * @test: optional test callback
2755 * @process: process callback
2756 * @data: data passed to @test and @process
2757 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08002758 *
Tejun Heo72ec7022013-08-08 20:11:26 -04002759 * Iterate through all the tasks in @css, calling @test for each, and if it
2760 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08002761 *
Tejun Heoe5358372013-08-08 20:11:26 -04002762 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04002763 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04002764 * lock css_set_lock for the call to @process.
2765 *
2766 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04002767 * of @css for the duration of this call. This function may or may not
2768 * call @process for tasks that exit or move to a different css during the
2769 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04002770 *
2771 * Note that @test may be called with locks held, and may in some
2772 * situations be called multiple times for the same task, so it should be
2773 * cheap.
2774 *
2775 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
2776 * heap operations (and its "gt" member will be overwritten), else a
2777 * temporary heap will be used (allocation of which may cause this function
2778 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08002779 */
Tejun Heo72ec7022013-08-08 20:11:26 -04002780int css_scan_tasks(struct cgroup_subsys_state *css,
2781 bool (*test)(struct task_struct *, void *),
2782 void (*process)(struct task_struct *, void *),
2783 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002784{
2785 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04002786 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002787 struct task_struct *p, *dropped;
2788 /* Never dereference latest_task, since it's not refcounted */
2789 struct task_struct *latest_task = NULL;
2790 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002791 struct timespec latest_time = { 0, 0 };
2792
Tejun Heoe5358372013-08-08 20:11:26 -04002793 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002794 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002795 heap->gt = &started_after;
2796 } else {
2797 /* We need to allocate our own heap memory */
2798 heap = &tmp_heap;
2799 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2800 if (retval)
2801 /* cannot allocate the heap */
2802 return retval;
2803 }
2804
2805 again:
2806 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04002807 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04002808 * which are of interest, and invoking @process callback on the
2809 * ones which need an update. Since we don't want to hold any
2810 * locks during the task updates, gather tasks to be processed in a
2811 * heap structure. The heap is sorted by descending task start
2812 * time. If the statically-sized heap fills up, we overflow tasks
2813 * that started later, and in future iterations only consider tasks
2814 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08002815 * guarantees forward progress and that we don't miss any tasks.
2816 */
2817 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04002818 css_task_iter_start(css, &it);
2819 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08002820 /*
2821 * Only affect tasks that qualify per the caller's callback,
2822 * if he provided one
2823 */
Tejun Heoe5358372013-08-08 20:11:26 -04002824 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002825 continue;
2826 /*
2827 * Only process tasks that started after the last task
2828 * we processed
2829 */
2830 if (!started_after_time(p, &latest_time, latest_task))
2831 continue;
2832 dropped = heap_insert(heap, p);
2833 if (dropped == NULL) {
2834 /*
2835 * The new task was inserted; the heap wasn't
2836 * previously full
2837 */
2838 get_task_struct(p);
2839 } else if (dropped != p) {
2840 /*
2841 * The new task was inserted, and pushed out a
2842 * different task
2843 */
2844 get_task_struct(p);
2845 put_task_struct(dropped);
2846 }
2847 /*
2848 * Else the new task was newer than anything already in
2849 * the heap and wasn't inserted
2850 */
2851 }
Tejun Heo72ec7022013-08-08 20:11:26 -04002852 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002853
2854 if (heap->size) {
2855 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002856 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002857 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002858 latest_time = q->start_time;
2859 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002860 }
2861 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04002862 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07002863 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002864 }
2865 /*
2866 * If we had to process any tasks at all, scan again
2867 * in case some of them were in the middle of forking
2868 * children that didn't get processed.
2869 * Not the most efficient way to do it, but it avoids
2870 * having to take callback_mutex in the fork path
2871 */
2872 goto again;
2873 }
2874 if (heap == &tmp_heap)
2875 heap_free(&tmp_heap);
2876 return 0;
2877}
2878
Tejun Heoe5358372013-08-08 20:11:26 -04002879static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07002880{
Tejun Heoe5358372013-08-08 20:11:26 -04002881 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07002882
Tejun Heo47cfcd02013-04-07 09:29:51 -07002883 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07002884 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002885 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07002886}
2887
2888/**
2889 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
2890 * @to: cgroup to which the tasks will be moved
2891 * @from: cgroup in which the tasks currently reside
2892 */
2893int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
2894{
Tejun Heo72ec7022013-08-08 20:11:26 -04002895 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
2896 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07002897}
2898
Paul Menage817929e2007-10-18 23:39:36 -07002899/*
Ben Blum102a7752009-09-23 15:56:26 -07002900 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002901 *
2902 * Reading this file can return large amounts of data if a cgroup has
2903 * *lots* of attached tasks. So it may need several calls to read(),
2904 * but we cannot guarantee that the information we produce is correct
2905 * unless we produce it entirely atomically.
2906 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002907 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002908
Li Zefan24528252012-01-20 11:58:43 +08002909/* which pidlist file are we talking about? */
2910enum cgroup_filetype {
2911 CGROUP_FILE_PROCS,
2912 CGROUP_FILE_TASKS,
2913};
2914
2915/*
2916 * A pidlist is a list of pids that virtually represents the contents of one
2917 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
2918 * a pair (one each for procs, tasks) for each pid namespace that's relevant
2919 * to the cgroup.
2920 */
2921struct cgroup_pidlist {
2922 /*
2923 * used to find which pidlist is wanted. doesn't change as long as
2924 * this particular list stays in the list.
2925 */
2926 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
2927 /* array of xids */
2928 pid_t *list;
2929 /* how many elements the above list has */
2930 int length;
Li Zefan24528252012-01-20 11:58:43 +08002931 /* each of these stored in a list by its cgroup */
2932 struct list_head links;
2933 /* pointer to the cgroup we belong to, for list removal purposes */
2934 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05002935 /* for delayed destruction */
2936 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08002937};
2938
Paul Menagebbcb81d2007-10-18 23:39:32 -07002939/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002940 * The following two functions "fix" the issue where there are more pids
2941 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2942 * TODO: replace with a kernel-wide solution to this problem
2943 */
2944#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2945static void *pidlist_allocate(int count)
2946{
2947 if (PIDLIST_TOO_LARGE(count))
2948 return vmalloc(count * sizeof(pid_t));
2949 else
2950 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2951}
Tejun Heob1a21362013-11-29 10:42:58 -05002952
Ben Blumd1d9fd32009-09-23 15:56:28 -07002953static void pidlist_free(void *p)
2954{
2955 if (is_vmalloc_addr(p))
2956 vfree(p);
2957 else
2958 kfree(p);
2959}
Ben Blumd1d9fd32009-09-23 15:56:28 -07002960
2961/*
Tejun Heob1a21362013-11-29 10:42:58 -05002962 * Used to destroy all pidlists lingering waiting for destroy timer. None
2963 * should be left afterwards.
2964 */
2965static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
2966{
2967 struct cgroup_pidlist *l, *tmp_l;
2968
2969 mutex_lock(&cgrp->pidlist_mutex);
2970 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
2971 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
2972 mutex_unlock(&cgrp->pidlist_mutex);
2973
2974 flush_workqueue(cgroup_pidlist_destroy_wq);
2975 BUG_ON(!list_empty(&cgrp->pidlists));
2976}
2977
2978static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
2979{
2980 struct delayed_work *dwork = to_delayed_work(work);
2981 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
2982 destroy_dwork);
2983 struct cgroup_pidlist *tofree = NULL;
2984
2985 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05002986
2987 /*
Tejun Heo04502362013-11-29 10:42:59 -05002988 * Destroy iff we didn't get queued again. The state won't change
2989 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05002990 */
Tejun Heo04502362013-11-29 10:42:59 -05002991 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05002992 list_del(&l->links);
2993 pidlist_free(l->list);
2994 put_pid_ns(l->key.ns);
2995 tofree = l;
2996 }
2997
Tejun Heob1a21362013-11-29 10:42:58 -05002998 mutex_unlock(&l->owner->pidlist_mutex);
2999 kfree(tofree);
3000}
3001
3002/*
Ben Blum102a7752009-09-23 15:56:26 -07003003 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003004 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003005 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003006static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003007{
Ben Blum102a7752009-09-23 15:56:26 -07003008 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003009
3010 /*
3011 * we presume the 0th element is unique, so i starts at 1. trivial
3012 * edge cases first; no work needs to be done for either
3013 */
3014 if (length == 0 || length == 1)
3015 return length;
3016 /* src and dest walk down the list; dest counts unique elements */
3017 for (src = 1; src < length; src++) {
3018 /* find next unique element */
3019 while (list[src] == list[src-1]) {
3020 src++;
3021 if (src == length)
3022 goto after;
3023 }
3024 /* dest always points to where the next unique element goes */
3025 list[dest] = list[src];
3026 dest++;
3027 }
3028after:
Ben Blum102a7752009-09-23 15:56:26 -07003029 return dest;
3030}
3031
Tejun Heoafb2bc12013-11-29 10:42:59 -05003032/*
3033 * The two pid files - task and cgroup.procs - guaranteed that the result
3034 * is sorted, which forced this whole pidlist fiasco. As pid order is
3035 * different per namespace, each namespace needs differently sorted list,
3036 * making it impossible to use, for example, single rbtree of member tasks
3037 * sorted by task pointer. As pidlists can be fairly large, allocating one
3038 * per open file is dangerous, so cgroup had to implement shared pool of
3039 * pidlists keyed by cgroup and namespace.
3040 *
3041 * All this extra complexity was caused by the original implementation
3042 * committing to an entirely unnecessary property. In the long term, we
3043 * want to do away with it. Explicitly scramble sort order if
3044 * sane_behavior so that no such expectation exists in the new interface.
3045 *
3046 * Scrambling is done by swapping every two consecutive bits, which is
3047 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3048 */
3049static pid_t pid_fry(pid_t pid)
3050{
3051 unsigned a = pid & 0x55555555;
3052 unsigned b = pid & 0xAAAAAAAA;
3053
3054 return (a << 1) | (b >> 1);
3055}
3056
3057static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3058{
3059 if (cgroup_sane_behavior(cgrp))
3060 return pid_fry(pid);
3061 else
3062 return pid;
3063}
3064
Ben Blum102a7752009-09-23 15:56:26 -07003065static int cmppid(const void *a, const void *b)
3066{
3067 return *(pid_t *)a - *(pid_t *)b;
3068}
3069
Tejun Heoafb2bc12013-11-29 10:42:59 -05003070static int fried_cmppid(const void *a, const void *b)
3071{
3072 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3073}
3074
Ben Blum72a8cb32009-09-23 15:56:27 -07003075static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3076 enum cgroup_filetype type)
3077{
3078 struct cgroup_pidlist *l;
3079 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003080 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003081
Tejun Heoe6b81712013-11-29 10:42:59 -05003082 lockdep_assert_held(&cgrp->pidlist_mutex);
3083
3084 list_for_each_entry(l, &cgrp->pidlists, links)
3085 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003086 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003087 return NULL;
3088}
3089
Ben Blum72a8cb32009-09-23 15:56:27 -07003090/*
3091 * find the appropriate pidlist for our purpose (given procs vs tasks)
3092 * returns with the lock on that pidlist already held, and takes care
3093 * of the use count, or returns NULL with no locks held if we're out of
3094 * memory.
3095 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003096static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3097 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003098{
3099 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003100
Tejun Heoe6b81712013-11-29 10:42:59 -05003101 lockdep_assert_held(&cgrp->pidlist_mutex);
3102
3103 l = cgroup_pidlist_find(cgrp, type);
3104 if (l)
3105 return l;
3106
Ben Blum72a8cb32009-09-23 15:56:27 -07003107 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003108 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003109 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003110 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003111
Tejun Heob1a21362013-11-29 10:42:58 -05003112 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003113 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003114 /* don't need task_nsproxy() if we're looking at ourself */
3115 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003116 l->owner = cgrp;
3117 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003118 return l;
3119}
3120
3121/*
Ben Blum102a7752009-09-23 15:56:26 -07003122 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3123 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003124static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3125 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003126{
3127 pid_t *array;
3128 int length;
3129 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003130 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003131 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003132 struct cgroup_pidlist *l;
3133
Tejun Heo4bac00d2013-11-29 10:42:59 -05003134 lockdep_assert_held(&cgrp->pidlist_mutex);
3135
Ben Blum102a7752009-09-23 15:56:26 -07003136 /*
3137 * If cgroup gets more users after we read count, we won't have
3138 * enough space - tough. This race is indistinguishable to the
3139 * caller from the case that the additional cgroup users didn't
3140 * show up until sometime later on.
3141 */
3142 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003143 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003144 if (!array)
3145 return -ENOMEM;
3146 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003147 css_task_iter_start(&cgrp->dummy_css, &it);
3148 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003149 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003150 break;
Ben Blum102a7752009-09-23 15:56:26 -07003151 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003152 if (type == CGROUP_FILE_PROCS)
3153 pid = task_tgid_vnr(tsk);
3154 else
3155 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003156 if (pid > 0) /* make sure to only use valid results */
3157 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003158 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003159 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003160 length = n;
3161 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003162 if (cgroup_sane_behavior(cgrp))
3163 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3164 else
3165 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003166 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003167 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003168
Tejun Heoe6b81712013-11-29 10:42:59 -05003169 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003170 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003171 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003172 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003173 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003174 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003175
3176 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003177 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003178 l->list = array;
3179 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003180 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003181 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003182}
3183
Balbir Singh846c7bb2007-10-18 23:39:44 -07003184/**
Li Zefana043e3b2008-02-23 15:24:09 -08003185 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003186 * @stats: cgroupstats to fill information into
3187 * @dentry: A dentry entry belonging to the cgroup for which stats have
3188 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003189 *
3190 * Build and fill cgroupstats so that taskstats can export it to user
3191 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003192 */
3193int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3194{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003195 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07003196 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003197 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003198 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003199
Tejun Heo2bd59d42014-02-11 11:52:49 -05003200 /* it should be kernfs_node belonging to cgroupfs and is a directory */
3201 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
3202 kernfs_type(kn) != KERNFS_DIR)
3203 return -EINVAL;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003204
Tejun Heo2bd59d42014-02-11 11:52:49 -05003205 /*
3206 * We aren't being called from kernfs and there's no guarantee on
3207 * @kn->priv's validity. For this and css_tryget_from_dir(),
3208 * @kn->priv is RCU safe. Let's do the RCU dancing.
3209 */
3210 rcu_read_lock();
3211 cgrp = rcu_dereference(kn->priv);
3212 if (!cgrp) {
3213 rcu_read_unlock();
3214 return -ENOENT;
3215 }
Balbir Singh846c7bb2007-10-18 23:39:44 -07003216
Tejun Heo72ec7022013-08-08 20:11:26 -04003217 css_task_iter_start(&cgrp->dummy_css, &it);
3218 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003219 switch (tsk->state) {
3220 case TASK_RUNNING:
3221 stats->nr_running++;
3222 break;
3223 case TASK_INTERRUPTIBLE:
3224 stats->nr_sleeping++;
3225 break;
3226 case TASK_UNINTERRUPTIBLE:
3227 stats->nr_uninterruptible++;
3228 break;
3229 case TASK_STOPPED:
3230 stats->nr_stopped++;
3231 break;
3232 default:
3233 if (delayacct_is_task_waiting_on_io(tsk))
3234 stats->nr_io_wait++;
3235 break;
3236 }
3237 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003238 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003239
Tejun Heo2bd59d42014-02-11 11:52:49 -05003240 rcu_read_unlock();
3241 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003242}
3243
Paul Menage8f3ff202009-09-23 15:56:25 -07003244
Paul Menagecc31edc2008-10-18 20:28:04 -07003245/*
Ben Blum102a7752009-09-23 15:56:26 -07003246 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003247 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003248 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003249 */
3250
Ben Blum102a7752009-09-23 15:56:26 -07003251static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003252{
3253 /*
3254 * Initially we receive a position value that corresponds to
3255 * one more than the last pid shown (or 0 on the first call or
3256 * after a seek to the start). Use a binary-search to find the
3257 * next pid to display, if any
3258 */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003259 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003260 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003261 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003262 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003263 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003264 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003265
Tejun Heo4bac00d2013-11-29 10:42:59 -05003266 mutex_lock(&cgrp->pidlist_mutex);
3267
3268 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003269 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003270 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003271 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003272 * could already have been destroyed.
3273 */
Tejun Heo5d224442013-12-05 12:28:04 -05003274 if (of->priv)
3275 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003276
3277 /*
3278 * Either this is the first start() after open or the matching
3279 * pidlist has been destroyed inbetween. Create a new one.
3280 */
Tejun Heo5d224442013-12-05 12:28:04 -05003281 if (!of->priv) {
3282 ret = pidlist_array_load(cgrp, type,
3283 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003284 if (ret)
3285 return ERR_PTR(ret);
3286 }
Tejun Heo5d224442013-12-05 12:28:04 -05003287 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003288
Paul Menagecc31edc2008-10-18 20:28:04 -07003289 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003290 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003291
Paul Menagecc31edc2008-10-18 20:28:04 -07003292 while (index < end) {
3293 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003294 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003295 index = mid;
3296 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003297 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003298 index = mid + 1;
3299 else
3300 end = mid;
3301 }
3302 }
3303 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003304 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003305 return NULL;
3306 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003307 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003308 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003309 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003310}
3311
Ben Blum102a7752009-09-23 15:56:26 -07003312static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003313{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003314 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003315 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003316
Tejun Heo5d224442013-12-05 12:28:04 -05003317 if (l)
3318 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003319 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003320 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003321}
3322
Ben Blum102a7752009-09-23 15:56:26 -07003323static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003324{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003325 struct kernfs_open_file *of = s->private;
Tejun Heo5d224442013-12-05 12:28:04 -05003326 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003327 pid_t *p = v;
3328 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003329 /*
3330 * Advance to the next pid in the array. If this goes off the
3331 * end, we're done
3332 */
3333 p++;
3334 if (p >= end) {
3335 return NULL;
3336 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003337 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003338 return p;
3339 }
3340}
3341
Ben Blum102a7752009-09-23 15:56:26 -07003342static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003343{
3344 return seq_printf(s, "%d\n", *(int *)v);
3345}
3346
Ben Blum102a7752009-09-23 15:56:26 -07003347/*
3348 * seq_operations functions for iterating on pidlists through seq_file -
3349 * independent of whether it's tasks or procs
3350 */
3351static const struct seq_operations cgroup_pidlist_seq_operations = {
3352 .start = cgroup_pidlist_start,
3353 .stop = cgroup_pidlist_stop,
3354 .next = cgroup_pidlist_next,
3355 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003356};
3357
Tejun Heo182446d2013-08-08 20:11:24 -04003358static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3359 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003360{
Tejun Heo182446d2013-08-08 20:11:24 -04003361 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003362}
3363
Tejun Heo182446d2013-08-08 20:11:24 -04003364static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3365 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003366{
Tejun Heo182446d2013-08-08 20:11:24 -04003367 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003368 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003369 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003370 else
Tejun Heo182446d2013-08-08 20:11:24 -04003371 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003372 return 0;
3373}
3374
Tejun Heo182446d2013-08-08 20:11:24 -04003375static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3376 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003377{
Tejun Heo182446d2013-08-08 20:11:24 -04003378 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003379}
3380
Tejun Heo182446d2013-08-08 20:11:24 -04003381static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3382 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003383{
3384 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003385 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003386 else
Tejun Heo182446d2013-08-08 20:11:24 -04003387 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003388 return 0;
3389}
3390
Tejun Heod5c56ce2013-06-03 19:14:34 -07003391static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003392 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003393 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003394 .seq_start = cgroup_pidlist_start,
3395 .seq_next = cgroup_pidlist_next,
3396 .seq_stop = cgroup_pidlist_stop,
3397 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003398 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003399 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003400 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003401 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003402 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003403 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003404 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003405 .read_u64 = cgroup_clone_children_read,
3406 .write_u64 = cgroup_clone_children_write,
3407 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003408 {
Tejun Heo873fe092013-04-14 20:15:26 -07003409 .name = "cgroup.sane_behavior",
3410 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003411 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003412 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003413
3414 /*
3415 * Historical crazy stuff. These don't have "cgroup." prefix and
3416 * don't exist if sane_behavior. If you're depending on these, be
3417 * prepared to be burned.
3418 */
3419 {
3420 .name = "tasks",
3421 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003422 .seq_start = cgroup_pidlist_start,
3423 .seq_next = cgroup_pidlist_next,
3424 .seq_stop = cgroup_pidlist_stop,
3425 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003426 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003427 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003428 .mode = S_IRUGO | S_IWUSR,
3429 },
3430 {
3431 .name = "notify_on_release",
3432 .flags = CFTYPE_INSANE,
3433 .read_u64 = cgroup_read_notify_on_release,
3434 .write_u64 = cgroup_write_notify_on_release,
3435 },
Tejun Heo873fe092013-04-14 20:15:26 -07003436 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003437 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003438 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003439 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003440 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003441 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003442 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003443 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003444};
3445
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003446/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003447 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003448 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003449 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003450 *
3451 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003452 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003453static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003454{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003455 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003456 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003457
Tejun Heo8e3f6542012-04-01 12:09:55 -07003458 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003459 for_each_subsys(ss, i) {
Tejun Heo0adb0702014-02-12 09:29:48 -05003460 struct cftype *cfts;
Tejun Heob420ba72013-07-12 12:34:02 -07003461
3462 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003463 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003464
Tejun Heo0adb0702014-02-12 09:29:48 -05003465 list_for_each_entry(cfts, &ss->cfts, node) {
3466 ret = cgroup_addrm_files(cgrp, cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003467 if (ret < 0)
3468 goto err;
3469 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003470 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003471 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003472err:
3473 cgroup_clear_dir(cgrp, subsys_mask);
3474 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003475}
3476
Tejun Heo0c21ead2013-08-13 20:22:51 -04003477/*
3478 * css destruction is four-stage process.
3479 *
3480 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3481 * Implemented in kill_css().
3482 *
3483 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3484 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3485 * by invoking offline_css(). After offlining, the base ref is put.
3486 * Implemented in css_killed_work_fn().
3487 *
3488 * 3. When the percpu_ref reaches zero, the only possible remaining
3489 * accessors are inside RCU read sections. css_release() schedules the
3490 * RCU callback.
3491 *
3492 * 4. After the grace period, the css can be freed. Implemented in
3493 * css_free_work_fn().
3494 *
3495 * It is actually hairier because both step 2 and 4 require process context
3496 * and thus involve punting to css->destroy_work adding two additional
3497 * steps to the already complex sequence.
3498 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003499static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003500{
3501 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003502 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003503 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003504
Tejun Heo0ae78e02013-08-13 11:01:54 -04003505 if (css->parent)
3506 css_put(css->parent);
3507
Tejun Heo0c21ead2013-08-13 20:22:51 -04003508 css->ss->css_free(css);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003509 cgroup_put(cgrp);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003510}
3511
3512static void css_free_rcu_fn(struct rcu_head *rcu_head)
3513{
3514 struct cgroup_subsys_state *css =
3515 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3516
Tejun Heo0c21ead2013-08-13 20:22:51 -04003517 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003518 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07003519}
3520
Tejun Heod3daf282013-06-13 19:39:16 -07003521static void css_release(struct percpu_ref *ref)
3522{
3523 struct cgroup_subsys_state *css =
3524 container_of(ref, struct cgroup_subsys_state, refcnt);
3525
Tejun Heoaec25022014-02-08 10:36:58 -05003526 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003527 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003528}
3529
Tejun Heo623f9262013-08-13 11:01:55 -04003530static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
3531 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003532{
Paul Menagebd89aab2007-10-18 23:40:44 -07003533 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04003534 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003535 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003536
Tejun Heo0ae78e02013-08-13 11:01:54 -04003537 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04003538 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04003539 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07003540 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003541
Tejun Heoca8bdca2013-08-26 18:40:56 -04003542 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003543}
3544
Li Zefan2a4ac632013-07-31 16:16:40 +08003545/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04003546static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003547{
Tejun Heo623f9262013-08-13 11:01:55 -04003548 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08003549 int ret = 0;
3550
Tejun Heoace2bee2014-02-11 11:52:47 -05003551 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003552 lockdep_assert_held(&cgroup_mutex);
3553
Tejun Heo92fb9742012-11-19 08:13:38 -08003554 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04003555 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003556 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04003557 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04003558 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05003559 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04003560 }
Tejun Heob1929db2012-11-19 08:13:38 -08003561 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003562}
3563
Li Zefan2a4ac632013-07-31 16:16:40 +08003564/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04003565static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08003566{
Tejun Heo623f9262013-08-13 11:01:55 -04003567 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08003568
Tejun Heoace2bee2014-02-11 11:52:47 -05003569 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003570 lockdep_assert_held(&cgroup_mutex);
3571
3572 if (!(css->flags & CSS_ONLINE))
3573 return;
3574
Li Zefand7eeac12013-03-12 15:35:59 -07003575 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04003576 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003577
Tejun Heoeb954192013-08-08 20:11:23 -04003578 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04003579 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05003580 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08003581}
3582
Tejun Heoc81c925a2013-12-06 15:11:56 -05003583/**
3584 * create_css - create a cgroup_subsys_state
3585 * @cgrp: the cgroup new css will be associated with
3586 * @ss: the subsys of new css
3587 *
3588 * Create a new css associated with @cgrp - @ss pair. On success, the new
3589 * css is online and installed in @cgrp with all interface files created.
3590 * Returns 0 on success, -errno on failure.
3591 */
3592static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
3593{
3594 struct cgroup *parent = cgrp->parent;
3595 struct cgroup_subsys_state *css;
3596 int err;
3597
Tejun Heoc81c925a2013-12-06 15:11:56 -05003598 lockdep_assert_held(&cgroup_mutex);
3599
3600 css = ss->css_alloc(cgroup_css(parent, ss));
3601 if (IS_ERR(css))
3602 return PTR_ERR(css);
3603
3604 err = percpu_ref_init(&css->refcnt, css_release);
3605 if (err)
3606 goto err_free;
3607
3608 init_css(css, ss, cgrp);
3609
Tejun Heoaec25022014-02-08 10:36:58 -05003610 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003611 if (err)
3612 goto err_free;
3613
3614 err = online_css(css);
3615 if (err)
3616 goto err_free;
3617
Tejun Heo59f52962014-02-11 11:52:49 -05003618 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05003619 css_get(css->parent);
3620
3621 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
3622 parent->parent) {
3623 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",
3624 current->comm, current->pid, ss->name);
3625 if (!strcmp(ss->name, "memory"))
3626 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
3627 ss->warned_broken_hierarchy = true;
3628 }
3629
3630 return 0;
3631
3632err_free:
3633 percpu_ref_cancel_init(&css->refcnt);
3634 ss->css_free(css);
3635 return err;
3636}
3637
Tejun Heo2bd59d42014-02-11 11:52:49 -05003638/**
Li Zefana043e3b2008-02-23 15:24:09 -08003639 * cgroup_create - create a cgroup
3640 * @parent: cgroup that will be parent of the new cgroup
Tejun Heoe61734c2014-02-12 09:29:50 -05003641 * @name: name of the new cgroup
Tejun Heo2bd59d42014-02-11 11:52:49 -05003642 * @mode: mode to set on new cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07003643 */
Tejun Heoe61734c2014-02-12 09:29:50 -05003644static long cgroup_create(struct cgroup *parent, const char *name,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003645 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003646{
Paul Menagebd89aab2007-10-18 23:40:44 -07003647 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003648 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05003649 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003650 struct cgroup_subsys *ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003651 struct kernfs_node *kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003652
Tejun Heo0a950f62012-11-19 09:02:12 -08003653 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07003654 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3655 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003656 return -ENOMEM;
3657
Tejun Heoace2bee2014-02-11 11:52:47 -05003658 mutex_lock(&cgroup_tree_mutex);
3659
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003660 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08003661 * Only live parents can have children. Note that the liveliness
3662 * check isn't strictly necessary because cgroup_mkdir() and
3663 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
3664 * anyway so that locking is contained inside cgroup proper and we
3665 * don't get nasty surprises if we ever grow another caller.
3666 */
3667 if (!cgroup_lock_live_group(parent)) {
3668 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05003669 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08003670 }
3671
3672 /*
3673 * Temporarily set the pointer to NULL, so idr_find() won't return
3674 * a half-baked cgroup.
3675 */
3676 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
3677 if (cgrp->id < 0) {
3678 err = -ENOMEM;
3679 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08003680 }
3681
Paul Menagecc31edc2008-10-18 20:28:04 -07003682 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003683
Paul Menagebd89aab2007-10-18 23:40:44 -07003684 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04003685 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07003686 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003687
Li Zefanb6abdb02008-03-04 14:28:19 -08003688 if (notify_on_release(parent))
3689 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3690
Tejun Heo2260e7f2012-11-19 08:13:38 -08003691 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
3692 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003693
Tejun Heo2bd59d42014-02-11 11:52:49 -05003694 /* create the directory */
Tejun Heoe61734c2014-02-12 09:29:50 -05003695 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003696 if (IS_ERR(kn)) {
3697 err = PTR_ERR(kn);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003698 goto err_free_id;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003699 }
3700 cgrp->kn = kn;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003701
Tejun Heo6f305582014-02-12 09:29:50 -05003702 /*
3703 * This extra ref will be put in cgroup_free_fn() and guarantees
3704 * that @cgrp->kn is always accessible.
3705 */
3706 kernfs_get(kn);
3707
Tejun Heo00356bd2013-06-18 11:14:22 -07003708 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09003709
Tejun Heo4e139af2012-11-19 08:13:36 -08003710 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08003711 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
3712 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08003713
Tejun Heo2bd59d42014-02-11 11:52:49 -05003714 /*
3715 * Grab a reference on the root and parent so that they don't get
3716 * deleted while there are child cgroups.
3717 */
3718 cgroup_get_root(root);
Tejun Heo59f52962014-02-11 11:52:49 -05003719 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08003720
Tejun Heo0d802552013-12-06 15:11:56 -05003721 /*
3722 * @cgrp is now fully operational. If something fails after this
3723 * point, it'll be released via the normal destruction path.
3724 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003725 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
3726
Tejun Heo2bb566c2013-08-08 20:11:23 -04003727 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07003728 if (err)
3729 goto err_destroy;
3730
Tejun Heo9d403e92013-12-06 15:11:56 -05003731 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05003732 for_each_subsys(ss, ssid) {
3733 if (root->subsys_mask & (1 << ssid)) {
3734 err = create_css(cgrp, ss);
3735 if (err)
3736 goto err_destroy;
3737 }
Tejun Heoa8638032012-11-09 09:12:29 -08003738 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003739
Tejun Heo2bd59d42014-02-11 11:52:49 -05003740 kernfs_activate(kn);
3741
Paul Menageddbcc7e2007-10-18 23:39:30 -07003742 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003743 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003744
3745 return 0;
3746
Tejun Heo0a950f62012-11-19 09:02:12 -08003747err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08003748 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08003749err_unlock:
3750 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003751err_unlock_tree:
3752 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003753 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003754 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003755
3756err_destroy:
3757 cgroup_destroy_locked(cgrp);
3758 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003759 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08003760 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003761}
3762
Tejun Heo2bd59d42014-02-11 11:52:49 -05003763static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3764 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003765{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003766 struct cgroup *parent = parent_kn->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003767
Tejun Heo2bd59d42014-02-11 11:52:49 -05003768 return cgroup_create(parent, name, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003769}
3770
Tejun Heo223dbc32013-08-13 20:22:50 -04003771/*
3772 * This is called when the refcnt of a css is confirmed to be killed.
3773 * css_tryget() is now guaranteed to fail.
3774 */
3775static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07003776{
Tejun Heo223dbc32013-08-13 20:22:50 -04003777 struct cgroup_subsys_state *css =
3778 container_of(work, struct cgroup_subsys_state, destroy_work);
3779 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07003780
Tejun Heoace2bee2014-02-11 11:52:47 -05003781 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003782 mutex_lock(&cgroup_mutex);
3783
3784 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04003785 * css_tryget() is guaranteed to fail now. Tell subsystems to
3786 * initate destruction.
3787 */
3788 offline_css(css);
3789
3790 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003791 * If @cgrp is marked dead, it's waiting for refs of all css's to
3792 * be disabled before proceeding to the second phase of cgroup
3793 * destruction. If we are the last one, kick it off.
3794 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04003795 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04003796 cgroup_destroy_css_killed(cgrp);
3797
3798 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05003799 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04003800
3801 /*
3802 * Put the css refs from kill_css(). Each css holds an extra
3803 * reference to the cgroup's dentry and cgroup removal proceeds
3804 * regardless of css refs. On the last put of each css, whenever
3805 * that may be, the extra dentry ref is put so that dentry
3806 * destruction happens only after all css's are released.
3807 */
3808 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07003809}
3810
Tejun Heo223dbc32013-08-13 20:22:50 -04003811/* css kill confirmation processing requires process context, bounce */
3812static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07003813{
3814 struct cgroup_subsys_state *css =
3815 container_of(ref, struct cgroup_subsys_state, refcnt);
3816
Tejun Heo223dbc32013-08-13 20:22:50 -04003817 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05003818 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07003819}
3820
3821/**
Tejun Heoedae0c32013-08-13 20:22:51 -04003822 * kill_css - destroy a css
3823 * @css: css to destroy
3824 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003825 * This function initiates destruction of @css by removing cgroup interface
3826 * files and putting its base reference. ->css_offline() will be invoked
3827 * asynchronously once css_tryget() is guaranteed to fail and when the
3828 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04003829 */
3830static void kill_css(struct cgroup_subsys_state *css)
3831{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003832 /*
3833 * This must happen before css is disassociated with its cgroup.
3834 * See seq_css() for details.
3835 */
Tejun Heoaec25022014-02-08 10:36:58 -05003836 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04003837
Tejun Heoedae0c32013-08-13 20:22:51 -04003838 /*
3839 * Killing would put the base ref, but we need to keep it alive
3840 * until after ->css_offline().
3841 */
3842 css_get(css);
3843
3844 /*
3845 * cgroup core guarantees that, by the time ->css_offline() is
3846 * invoked, no new css reference will be given out via
3847 * css_tryget(). We can't simply call percpu_ref_kill() and
3848 * proceed to offlining css's because percpu_ref_kill() doesn't
3849 * guarantee that the ref is seen as killed on all CPUs on return.
3850 *
3851 * Use percpu_ref_kill_and_confirm() to get notifications as each
3852 * css is confirmed to be seen as killed on all CPUs.
3853 */
3854 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07003855}
3856
3857/**
3858 * cgroup_destroy_locked - the first stage of cgroup destruction
3859 * @cgrp: cgroup to be destroyed
3860 *
3861 * css's make use of percpu refcnts whose killing latency shouldn't be
3862 * exposed to userland and are RCU protected. Also, cgroup core needs to
3863 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
3864 * invoked. To satisfy all the requirements, destruction is implemented in
3865 * the following two steps.
3866 *
3867 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
3868 * userland visible parts and start killing the percpu refcnts of
3869 * css's. Set up so that the next stage will be kicked off once all
3870 * the percpu refcnts are confirmed to be killed.
3871 *
3872 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
3873 * rest of destruction. Once all cgroup references are gone, the
3874 * cgroup is RCU-freed.
3875 *
3876 * This function implements s1. After this step, @cgrp is gone as far as
3877 * the userland is concerned and a new cgroup with the same name may be
3878 * created. As cgroup doesn't care about the names internally, this
3879 * doesn't cause any problem.
3880 */
Tejun Heo42809dd2012-11-19 08:13:37 -08003881static int cgroup_destroy_locked(struct cgroup *cgrp)
3882 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003883{
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003884 struct cgroup *child;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003885 struct cgroup_subsys_state *css;
Tejun Heoddd69142013-06-12 21:04:54 -07003886 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05003887 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003888
Tejun Heoace2bee2014-02-11 11:52:47 -05003889 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08003890 lockdep_assert_held(&cgroup_mutex);
3891
Tejun Heoddd69142013-06-12 21:04:54 -07003892 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07003893 * css_set_lock synchronizes access to ->cset_links and prevents
3894 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07003895 */
3896 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003897 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07003898 read_unlock(&css_set_lock);
3899 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003900 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08003901
Tejun Heo1a90dd52012-11-05 09:16:59 -08003902 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07003903 * Make sure there's no live children. We can't test ->children
3904 * emptiness as dead children linger on it while being destroyed;
3905 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
3906 */
3907 empty = true;
3908 rcu_read_lock();
3909 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
3910 empty = cgroup_is_dead(child);
3911 if (!empty)
3912 break;
3913 }
3914 rcu_read_unlock();
3915 if (!empty)
3916 return -EBUSY;
3917
3918 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04003919 * Initiate massacre of all css's. cgroup_destroy_css_killed()
3920 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05003921 * percpu refs of all css's are confirmed to be killed. This
3922 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08003923 */
Tejun Heo4ac06012014-02-11 11:52:47 -05003924 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05003925 for_each_css(css, ssid, cgrp)
3926 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05003927 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003928
3929 /*
3930 * Mark @cgrp dead. This prevents further task migration and child
3931 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04003932 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07003933 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04003934 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07003935 */
Tejun Heo54766d42013-06-12 21:04:53 -07003936 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08003937
Tejun Heo455050d2013-06-13 19:27:41 -07003938 /* CGRP_DEAD is set, remove from ->release_list for the last time */
3939 raw_spin_lock(&release_list_lock);
3940 if (!list_empty(&cgrp->release_list))
3941 list_del_init(&cgrp->release_list);
3942 raw_spin_unlock(&release_list_lock);
3943
3944 /*
Tejun Heof20104d2013-08-13 20:22:50 -04003945 * If @cgrp has css's attached, the second stage of cgroup
3946 * destruction is kicked off from css_killed_work_fn() after the
3947 * refs of all attached css's are killed. If @cgrp doesn't have
3948 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07003949 */
Tejun Heof20104d2013-08-13 20:22:50 -04003950 if (!cgrp->nr_css)
3951 cgroup_destroy_css_killed(cgrp);
3952
Tejun Heo2bd59d42014-02-11 11:52:49 -05003953 /* remove @cgrp directory along with the base files */
Tejun Heo4ac06012014-02-11 11:52:47 -05003954 mutex_unlock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003955
3956 /*
3957 * There are two control paths which try to determine cgroup from
3958 * dentry without going through kernfs - cgroupstats_build() and
3959 * css_tryget_from_dir(). Those are supported by RCU protecting
3960 * clearing of cgrp->kn->priv backpointer, which should happen
3961 * after all files under it have been removed.
3962 */
Tejun Heo6f305582014-02-12 09:29:50 -05003963 kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
Tejun Heo2bd59d42014-02-11 11:52:49 -05003964 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003965
Tejun Heo4ac06012014-02-11 11:52:47 -05003966 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07003967
Tejun Heoea15f8c2013-06-13 19:27:42 -07003968 return 0;
3969};
3970
Tejun Heod3daf282013-06-13 19:39:16 -07003971/**
Tejun Heof20104d2013-08-13 20:22:50 -04003972 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07003973 * @work: cgroup->destroy_free_work
3974 *
3975 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04003976 * destroyed after all css's are offlined and performs the rest of
3977 * destruction. This is the second step of destruction described in the
3978 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07003979 */
Tejun Heof20104d2013-08-13 20:22:50 -04003980static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07003981{
Tejun Heoea15f8c2013-06-13 19:27:42 -07003982 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07003983
Tejun Heoace2bee2014-02-11 11:52:47 -05003984 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04003985 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003986
Paul Menage999cd8a2009-01-07 18:08:36 -08003987 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08003988 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07003989
Tejun Heo59f52962014-02-11 11:52:49 -05003990 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003991
Paul Menagebd89aab2007-10-18 23:40:44 -07003992 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003993 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003994}
3995
Tejun Heo2bd59d42014-02-11 11:52:49 -05003996static int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08003997{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003998 struct cgroup *cgrp = kn->priv;
3999 int ret = 0;
4000
4001 /*
4002 * This is self-destruction but @kn can't be removed while this
4003 * callback is in progress. Let's break active protection. Once
4004 * the protection is broken, @cgrp can be destroyed at any point.
4005 * Pin it so that it stays accessible.
4006 */
4007 cgroup_get(cgrp);
4008 kernfs_break_active_protection(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08004009
Tejun Heoace2bee2014-02-11 11:52:47 -05004010 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004011 mutex_lock(&cgroup_mutex);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004012
4013 /*
4014 * @cgrp might already have been destroyed while we're trying to
4015 * grab the mutexes.
4016 */
4017 if (!cgroup_is_dead(cgrp))
4018 ret = cgroup_destroy_locked(cgrp);
4019
Tejun Heo42809dd2012-11-19 08:13:37 -08004020 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004021 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004022
Tejun Heo2bd59d42014-02-11 11:52:49 -05004023 kernfs_unbreak_active_protection(kn);
4024 cgroup_put(cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -08004025 return ret;
4026}
4027
Tejun Heo2bd59d42014-02-11 11:52:49 -05004028static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4029 .remount_fs = cgroup_remount,
4030 .show_options = cgroup_show_options,
4031 .mkdir = cgroup_mkdir,
4032 .rmdir = cgroup_rmdir,
4033 .rename = cgroup_rename,
4034};
4035
Li Zefan06a11922008-04-29 01:00:07 -07004036static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004037{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004038 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004039
4040 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041
Tejun Heoace2bee2014-02-11 11:52:47 -05004042 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004043 mutex_lock(&cgroup_mutex);
4044
Tejun Heo0adb0702014-02-12 09:29:48 -05004045 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004046
Paul Menageddbcc7e2007-10-18 23:39:30 -07004047 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004048 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004049 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050 /* We don't handle early failures gracefully */
4051 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004052 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004053
Li Zefane8d55fd2008-04-29 01:00:13 -07004054 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004055 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004056 * newly registered, all tasks and hence the
4057 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004058 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004059
4060 need_forkexit_callback |= ss->fork || ss->exit;
4061
Li Zefane8d55fd2008-04-29 01:00:13 -07004062 /* At system boot, before all subsystems have been
4063 * registered, no tasks have been forked, so we don't
4064 * need to invoke fork callbacks here. */
4065 BUG_ON(!list_empty(&init_task.tasks));
4066
Tejun Heoae7f1642013-08-13 20:22:50 -04004067 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004068
Tejun Heo648bb562012-11-19 08:13:36 -08004069 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004070 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004071}
4072
4073/**
Li Zefana043e3b2008-02-23 15:24:09 -08004074 * cgroup_init_early - cgroup initialization at system boot
4075 *
4076 * Initialize cgroups at system boot, and initialize any
4077 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004078 */
4079int __init cgroup_init_early(void)
4080{
Tejun Heo30159ec2013-06-25 11:53:37 -07004081 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004082 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004083
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004084 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004085 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004086 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004087 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004088 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004089 init_cgroup_root(&cgroup_dummy_root);
4090 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004091 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004092
Tejun Heo69d02062013-06-12 21:04:50 -07004093 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004094 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4095 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004096 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004097
Tejun Heo3ed80a62014-02-08 10:36:58 -05004098 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004099 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004100 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4101 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004102 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004103 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4104 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4105
Tejun Heoaec25022014-02-08 10:36:58 -05004106 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004107 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004108
4109 if (ss->early_init)
4110 cgroup_init_subsys(ss);
4111 }
4112 return 0;
4113}
4114
4115/**
Li Zefana043e3b2008-02-23 15:24:09 -08004116 * cgroup_init - cgroup initialization
4117 *
4118 * Register cgroup filesystem and /proc file, and initialize
4119 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004120 */
4121int __init cgroup_init(void)
4122{
Tejun Heo30159ec2013-06-25 11:53:37 -07004123 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004124 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004125 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004126
Tejun Heo2bd59d42014-02-11 11:52:49 -05004127 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
Tejun Heo2da440a2014-02-11 11:52:48 -05004128
Tejun Heo3ed80a62014-02-08 10:36:58 -05004129 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004130 if (!ss->early_init)
4131 cgroup_init_subsys(ss);
Tejun Heode00ffa2014-02-11 11:52:48 -05004132
4133 /*
4134 * cftype registration needs kmalloc and can't be done
4135 * during early_init. Register base cftypes separately.
4136 */
4137 if (ss->base_cftypes)
4138 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004139 }
4140
Tejun Heofa3ca072013-04-14 11:36:56 -07004141 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004142 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004143
Tejun Heo82fe9b02013-06-25 11:53:37 -07004144 /* Add init_css_set to the hash table */
4145 key = css_set_hash(init_css_set.subsys);
4146 hash_add(css_set_table, &init_css_set.hlist, key);
4147
Tejun Heofc76df72013-06-25 11:53:37 -07004148 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004149
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004150 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4151 0, 1, GFP_KERNEL);
4152 BUG_ON(err < 0);
4153
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004154 mutex_unlock(&cgroup_mutex);
4155
Greg KH676db4a2010-08-05 13:53:35 -07004156 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004157 if (!cgroup_kobj)
4158 return -ENOMEM;
Greg KH676db4a2010-08-05 13:53:35 -07004159
4160 err = register_filesystem(&cgroup_fs_type);
4161 if (err < 0) {
4162 kobject_put(cgroup_kobj);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004163 return err;
Greg KH676db4a2010-08-05 13:53:35 -07004164 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004165
Li Zefan46ae2202008-04-29 01:00:08 -07004166 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004167 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004168}
Paul Menageb4f48b62007-10-18 23:39:33 -07004169
Tejun Heoe5fca242013-11-22 17:14:39 -05004170static int __init cgroup_wq_init(void)
4171{
4172 /*
4173 * There isn't much point in executing destruction path in
4174 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004175 *
4176 * XXX: Must be ordered to make sure parent is offlined after
4177 * children. The ordering requirement is for memcg where a
4178 * parent's offline may wait for a child's leading to deadlock. In
4179 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004180 *
4181 * We would prefer to do this in cgroup_init() above, but that
4182 * is called before init_workqueues(): so leave this until after.
4183 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004184 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004185 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004186
4187 /*
4188 * Used to destroy pidlists and separate to serve as flush domain.
4189 * Cap @max_active to 1 too.
4190 */
4191 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4192 0, 1);
4193 BUG_ON(!cgroup_pidlist_destroy_wq);
4194
Tejun Heoe5fca242013-11-22 17:14:39 -05004195 return 0;
4196}
4197core_initcall(cgroup_wq_init);
4198
Paul Menagea4243162007-10-18 23:39:35 -07004199/*
4200 * proc_cgroup_show()
4201 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4202 * - Used for /proc/<pid>/cgroup.
4203 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4204 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004205 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004206 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4207 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4208 * cgroup to top_cgroup.
4209 */
4210
4211/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004212int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004213{
4214 struct pid *pid;
4215 struct task_struct *tsk;
Tejun Heoe61734c2014-02-12 09:29:50 -05004216 char *buf, *path;
Paul Menagea4243162007-10-18 23:39:35 -07004217 int retval;
4218 struct cgroupfs_root *root;
4219
4220 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05004221 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07004222 if (!buf)
4223 goto out;
4224
4225 retval = -ESRCH;
4226 pid = m->private;
4227 tsk = get_pid_task(pid, PIDTYPE_PID);
4228 if (!tsk)
4229 goto out_free;
4230
4231 retval = 0;
4232
4233 mutex_lock(&cgroup_mutex);
4234
Li Zefane5f6a862009-01-07 18:07:41 -08004235 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004236 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004237 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004238 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004239
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004240 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004241 for_each_subsys(ss, ssid)
4242 if (root->subsys_mask & (1 << ssid))
4243 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004244 if (strlen(root->name))
4245 seq_printf(m, "%sname=%s", count ? "," : "",
4246 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004247 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004248 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heoe61734c2014-02-12 09:29:50 -05004249 path = cgroup_path(cgrp, buf, PATH_MAX);
4250 if (!path) {
4251 retval = -ENAMETOOLONG;
Paul Menagea4243162007-10-18 23:39:35 -07004252 goto out_unlock;
Tejun Heoe61734c2014-02-12 09:29:50 -05004253 }
4254 seq_puts(m, path);
Paul Menagea4243162007-10-18 23:39:35 -07004255 seq_putc(m, '\n');
4256 }
4257
4258out_unlock:
4259 mutex_unlock(&cgroup_mutex);
4260 put_task_struct(tsk);
4261out_free:
4262 kfree(buf);
4263out:
4264 return retval;
4265}
4266
Paul Menagea4243162007-10-18 23:39:35 -07004267/* Display information about each subsystem and each hierarchy */
4268static int proc_cgroupstats_show(struct seq_file *m, void *v)
4269{
Tejun Heo30159ec2013-06-25 11:53:37 -07004270 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004271 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004272
Paul Menage8bab8dd2008-04-04 14:29:57 -07004273 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004274 /*
4275 * ideally we don't want subsystems moving around while we do this.
4276 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4277 * subsys/hierarchy state.
4278 */
Paul Menagea4243162007-10-18 23:39:35 -07004279 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004280
4281 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004282 seq_printf(m, "%s\t%d\t%d\t%d\n",
4283 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004284 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004285
Paul Menagea4243162007-10-18 23:39:35 -07004286 mutex_unlock(&cgroup_mutex);
4287 return 0;
4288}
4289
4290static int cgroupstats_open(struct inode *inode, struct file *file)
4291{
Al Viro9dce07f2008-03-29 03:07:28 +00004292 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004293}
4294
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004295static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004296 .open = cgroupstats_open,
4297 .read = seq_read,
4298 .llseek = seq_lseek,
4299 .release = single_release,
4300};
4301
Paul Menageb4f48b62007-10-18 23:39:33 -07004302/**
4303 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004304 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004305 *
4306 * Description: A task inherits its parent's cgroup at fork().
4307 *
4308 * A pointer to the shared css_set was automatically copied in
4309 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004310 * it was not made under the protection of RCU or cgroup_mutex, so
4311 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4312 * have already changed current->cgroups, allowing the previously
4313 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004314 *
4315 * At the point that cgroup_fork() is called, 'current' is the parent
4316 * task, and the passed argument 'child' points to the child task.
4317 */
4318void cgroup_fork(struct task_struct *child)
4319{
Tejun Heo9bb71302012-10-18 17:52:07 -07004320 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004321 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004322 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004323 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004324 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004325}
4326
4327/**
Li Zefana043e3b2008-02-23 15:24:09 -08004328 * cgroup_post_fork - called on a new task after adding it to the task list
4329 * @child: the task in question
4330 *
Tejun Heo5edee612012-10-16 15:03:14 -07004331 * Adds the task to the list running through its css_set if necessary and
4332 * call the subsystem fork() callbacks. Has to be after the task is
4333 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004334 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004335 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004336 */
Paul Menage817929e2007-10-18 23:39:36 -07004337void cgroup_post_fork(struct task_struct *child)
4338{
Tejun Heo30159ec2013-06-25 11:53:37 -07004339 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004340 int i;
4341
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004342 /*
4343 * use_task_css_set_links is set to 1 before we walk the tasklist
4344 * under the tasklist_lock and we read it here after we added the child
4345 * to the tasklist under the tasklist_lock as well. If the child wasn't
4346 * yet in the tasklist when we walked through it from
4347 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4348 * should be visible now due to the paired locking and barriers implied
4349 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4350 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4351 * lock on fork.
4352 */
Paul Menage817929e2007-10-18 23:39:36 -07004353 if (use_task_css_set_links) {
4354 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004355 task_lock(child);
4356 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004357 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004358 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004359 write_unlock(&css_set_lock);
4360 }
Tejun Heo5edee612012-10-16 15:03:14 -07004361
4362 /*
4363 * Call ss->fork(). This must happen after @child is linked on
4364 * css_set; otherwise, @child might change state between ->fork()
4365 * and addition to css_set.
4366 */
4367 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004368 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004369 if (ss->fork)
4370 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004371 }
Paul Menage817929e2007-10-18 23:39:36 -07004372}
Tejun Heo5edee612012-10-16 15:03:14 -07004373
Paul Menage817929e2007-10-18 23:39:36 -07004374/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004375 * cgroup_exit - detach cgroup from exiting task
4376 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004377 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004378 *
4379 * Description: Detach cgroup from @tsk and release it.
4380 *
4381 * Note that cgroups marked notify_on_release force every task in
4382 * them to take the global cgroup_mutex mutex when exiting.
4383 * This could impact scaling on very large systems. Be reluctant to
4384 * use notify_on_release cgroups where very high task exit scaling
4385 * is required on large systems.
4386 *
4387 * the_top_cgroup_hack:
4388 *
4389 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4390 *
4391 * We call cgroup_exit() while the task is still competent to
4392 * handle notify_on_release(), then leave the task attached to the
4393 * root cgroup in each hierarchy for the remainder of its exit.
4394 *
4395 * To do this properly, we would increment the reference count on
4396 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4397 * code we would add a second cgroup function call, to drop that
4398 * reference. This would just create an unnecessary hot spot on
4399 * the top_cgroup reference count, to no avail.
4400 *
4401 * Normally, holding a reference to a cgroup without bumping its
4402 * count is unsafe. The cgroup could go away, or someone could
4403 * attach us to a different cgroup, decrementing the count on
4404 * the first cgroup that we never incremented. But in this case,
4405 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004406 * which wards off any cgroup_attach_task() attempts, or task is a failed
4407 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004408 */
4409void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4410{
Tejun Heo30159ec2013-06-25 11:53:37 -07004411 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004412 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004413 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004414
4415 /*
4416 * Unlink from the css_set task list if necessary.
4417 * Optimistically check cg_list before taking
4418 * css_set_lock
4419 */
4420 if (!list_empty(&tsk->cg_list)) {
4421 write_lock(&css_set_lock);
4422 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004423 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004424 write_unlock(&css_set_lock);
4425 }
4426
Paul Menageb4f48b62007-10-18 23:39:33 -07004427 /* Reassign the task to the init_css_set. */
4428 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004429 cset = task_css_set(tsk);
4430 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004431
4432 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004433 /* see cgroup_post_fork() for details */
4434 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004435 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004436 struct cgroup_subsys_state *old_css = cset->subsys[i];
4437 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004438
Tejun Heoeb954192013-08-08 20:11:23 -04004439 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004440 }
4441 }
4442 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004443 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004444
Tejun Heo5abb8852013-06-12 21:04:49 -07004445 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004446}
Paul Menage697f4162007-10-18 23:39:34 -07004447
Paul Menagebd89aab2007-10-18 23:40:44 -07004448static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004449{
Li Zefanf50daa72013-03-01 15:06:07 +08004450 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004451 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004452 /*
4453 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004454 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004455 * it now
4456 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004457 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004458
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004459 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004460 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004461 list_empty(&cgrp->release_list)) {
4462 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004463 need_schedule_work = 1;
4464 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004465 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004466 if (need_schedule_work)
4467 schedule_work(&release_agent_work);
4468 }
4469}
4470
Paul Menage81a6a5c2007-10-18 23:39:38 -07004471/*
4472 * Notify userspace when a cgroup is released, by running the
4473 * configured release agent with the name of the cgroup (path
4474 * relative to the root of cgroup file system) as the argument.
4475 *
4476 * Most likely, this user command will try to rmdir this cgroup.
4477 *
4478 * This races with the possibility that some other task will be
4479 * attached to this cgroup before it is removed, or that some other
4480 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4481 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4482 * unused, and this cgroup will be reprieved from its death sentence,
4483 * to continue to serve a useful existence. Next time it's released,
4484 * we will get notified again, if it still has 'notify_on_release' set.
4485 *
4486 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4487 * means only wait until the task is successfully execve()'d. The
4488 * separate release agent task is forked by call_usermodehelper(),
4489 * then control in this thread returns here, without waiting for the
4490 * release agent task. We don't bother to wait because the caller of
4491 * this routine has no use for the exit status of the release agent
4492 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004493 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004494static void cgroup_release_agent(struct work_struct *work)
4495{
4496 BUG_ON(work != &release_agent_work);
4497 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004498 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004499 while (!list_empty(&release_list)) {
4500 char *argv[3], *envp[3];
4501 int i;
Tejun Heoe61734c2014-02-12 09:29:50 -05004502 char *pathbuf = NULL, *agentbuf = NULL, *path;
Paul Menagebd89aab2007-10-18 23:40:44 -07004503 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004504 struct cgroup,
4505 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004506 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004507 raw_spin_unlock(&release_list_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004508 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07004509 if (!pathbuf)
4510 goto continue_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05004511 path = cgroup_path(cgrp, pathbuf, PATH_MAX);
4512 if (!path)
Paul Menagee788e0662008-07-25 01:46:59 -07004513 goto continue_free;
4514 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4515 if (!agentbuf)
4516 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004517
4518 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07004519 argv[i++] = agentbuf;
Tejun Heoe61734c2014-02-12 09:29:50 -05004520 argv[i++] = path;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004521 argv[i] = NULL;
4522
4523 i = 0;
4524 /* minimal command environment */
4525 envp[i++] = "HOME=/";
4526 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4527 envp[i] = NULL;
4528
4529 /* Drop the lock while we invoke the usermode helper,
4530 * since the exec could involve hitting disk and hence
4531 * be a slow process */
4532 mutex_unlock(&cgroup_mutex);
4533 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004534 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07004535 continue_free:
4536 kfree(pathbuf);
4537 kfree(agentbuf);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004538 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004539 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004540 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004541 mutex_unlock(&cgroup_mutex);
4542}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004543
4544static int __init cgroup_disable(char *str)
4545{
Tejun Heo30159ec2013-06-25 11:53:37 -07004546 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004547 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07004548 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004549
4550 while ((token = strsep(&str, ",")) != NULL) {
4551 if (!*token)
4552 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07004553
Tejun Heo3ed80a62014-02-08 10:36:58 -05004554 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004555 if (!strcmp(token, ss->name)) {
4556 ss->disabled = 1;
4557 printk(KERN_INFO "Disabling %s control group"
4558 " subsystem\n", ss->name);
4559 break;
4560 }
4561 }
4562 }
4563 return 1;
4564}
4565__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004566
Tejun Heob77d7b62013-08-13 11:01:54 -04004567/**
Tejun Heo5a17f542014-02-11 11:52:47 -05004568 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04004569 * @dentry: directory dentry of interest
4570 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04004571 *
Tejun Heo5a17f542014-02-11 11:52:47 -05004572 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4573 * to get the corresponding css and return it. If such css doesn't exist
4574 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02004575 */
Tejun Heo5a17f542014-02-11 11:52:47 -05004576struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
4577 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004578{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004579 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4580 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004581 struct cgroup *cgrp;
Tejun Heob77d7b62013-08-13 11:01:54 -04004582
Tejun Heo35cf0832013-08-26 18:40:56 -04004583 /* is @dentry a cgroup dir? */
Tejun Heo2bd59d42014-02-11 11:52:49 -05004584 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
4585 kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02004586 return ERR_PTR(-EBADF);
4587
Tejun Heo5a17f542014-02-11 11:52:47 -05004588 rcu_read_lock();
4589
Tejun Heo2bd59d42014-02-11 11:52:49 -05004590 /*
4591 * This path doesn't originate from kernfs and @kn could already
4592 * have been or be removed at any point. @kn->priv is RCU
4593 * protected for this access. See destroy_locked() for details.
4594 */
4595 cgrp = rcu_dereference(kn->priv);
4596 if (cgrp)
4597 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05004598
4599 if (!css || !css_tryget(css))
4600 css = ERR_PTR(-ENOENT);
4601
4602 rcu_read_unlock();
4603 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004604}
Stephane Eraniane5d13672011-02-14 11:20:01 +02004605
Li Zefan1cb650b2013-08-19 10:05:24 +08004606/**
4607 * css_from_id - lookup css by id
4608 * @id: the cgroup id
4609 * @ss: cgroup subsys to be looked into
4610 *
4611 * Returns the css if there's valid one with @id, otherwise returns NULL.
4612 * Should be called under rcu_read_lock().
4613 */
4614struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4615{
4616 struct cgroup *cgrp;
4617
Tejun Heoace2bee2014-02-11 11:52:47 -05004618 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08004619
4620 cgrp = idr_find(&ss->root->cgroup_idr, id);
4621 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04004622 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08004623 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02004624}
4625
Paul Menagefe693432009-09-23 15:56:20 -07004626#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04004627static struct cgroup_subsys_state *
4628debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07004629{
4630 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4631
4632 if (!css)
4633 return ERR_PTR(-ENOMEM);
4634
4635 return css;
4636}
4637
Tejun Heoeb954192013-08-08 20:11:23 -04004638static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07004639{
Tejun Heoeb954192013-08-08 20:11:23 -04004640 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07004641}
4642
Tejun Heo182446d2013-08-08 20:11:24 -04004643static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
4644 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004645{
Tejun Heo182446d2013-08-08 20:11:24 -04004646 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07004647}
4648
Tejun Heo182446d2013-08-08 20:11:24 -04004649static u64 current_css_set_read(struct cgroup_subsys_state *css,
4650 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004651{
4652 return (u64)(unsigned long)current->cgroups;
4653}
4654
Tejun Heo182446d2013-08-08 20:11:24 -04004655static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08004656 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004657{
4658 u64 count;
4659
4660 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07004661 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07004662 rcu_read_unlock();
4663 return count;
4664}
4665
Tejun Heo2da8ca82013-12-05 12:28:04 -05004666static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004667{
Tejun Heo69d02062013-06-12 21:04:50 -07004668 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07004669 struct css_set *cset;
Tejun Heoe61734c2014-02-12 09:29:50 -05004670 char *name_buf;
4671
4672 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
4673 if (!name_buf)
4674 return -ENOMEM;
Paul Menage7717f7b2009-09-23 15:56:22 -07004675
4676 read_lock(&css_set_lock);
4677 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07004678 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07004679 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004680 struct cgroup *c = link->cgrp;
Tejun Heo59f52962014-02-11 11:52:49 -05004681 const char *name = "?";
Paul Menage7717f7b2009-09-23 15:56:22 -07004682
Tejun Heoe61734c2014-02-12 09:29:50 -05004683 if (c != cgroup_dummy_top) {
4684 cgroup_name(c, name_buf, NAME_MAX + 1);
4685 name = name_buf;
4686 }
Tejun Heo59f52962014-02-11 11:52:49 -05004687
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004688 seq_printf(seq, "Root %d group %s\n",
4689 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004690 }
4691 rcu_read_unlock();
4692 read_unlock(&css_set_lock);
Tejun Heoe61734c2014-02-12 09:29:50 -05004693 kfree(name_buf);
Paul Menage7717f7b2009-09-23 15:56:22 -07004694 return 0;
4695}
4696
4697#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05004698static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07004699{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004700 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07004701 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07004702
4703 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04004704 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004705 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07004706 struct task_struct *task;
4707 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07004708 seq_printf(seq, "css_set %p\n", cset);
4709 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07004710 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4711 seq_puts(seq, " ...\n");
4712 break;
4713 } else {
4714 seq_printf(seq, " task %d\n",
4715 task_pid_vnr(task));
4716 }
4717 }
4718 }
4719 read_unlock(&css_set_lock);
4720 return 0;
4721}
4722
Tejun Heo182446d2013-08-08 20:11:24 -04004723static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07004724{
Tejun Heo182446d2013-08-08 20:11:24 -04004725 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07004726}
4727
4728static struct cftype debug_files[] = {
4729 {
Paul Menagefe693432009-09-23 15:56:20 -07004730 .name = "taskcount",
4731 .read_u64 = debug_taskcount_read,
4732 },
4733
4734 {
4735 .name = "current_css_set",
4736 .read_u64 = current_css_set_read,
4737 },
4738
4739 {
4740 .name = "current_css_set_refcount",
4741 .read_u64 = current_css_set_refcount_read,
4742 },
4743
4744 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004745 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004746 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004747 },
4748
4749 {
4750 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004751 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07004752 },
4753
4754 {
Paul Menagefe693432009-09-23 15:56:20 -07004755 .name = "releasable",
4756 .read_u64 = releasable_read,
4757 },
Paul Menagefe693432009-09-23 15:56:20 -07004758
Tejun Heo4baf6e32012-04-01 12:09:55 -07004759 { } /* terminate */
4760};
Paul Menagefe693432009-09-23 15:56:20 -07004761
Tejun Heo073219e2014-02-08 10:36:58 -05004762struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08004763 .css_alloc = debug_css_alloc,
4764 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07004765 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07004766};
4767#endif /* CONFIG_CGROUP_DEBUG */