blob: 9e9e8fd632d8a983ae4e452d3d1fcca4a25177ad [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/slab.h>
45#include <linux/magic.h>
46#include <linux/spinlock.h>
47#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070048#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070049#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070050#include <linux/delayacct.h>
51#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080052#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040053#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070054#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070055#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070056#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080057#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020058#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070059
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070061
Tejun Heoe25e2cb2011-12-12 18:12:21 -080062/*
Tejun Heob1a21362013-11-29 10:42:58 -050063 * pidlists linger the following amount before being destroyed. The goal
64 * is avoiding frequent destruction in the middle of consecutive read calls
65 * Expiring in the middle is a performance problem not a correctness one.
66 * 1 sec should be enough.
67 */
68#define CGROUP_PIDLIST_DESTROY_DELAY HZ
69
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050070#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
71 MAX_CFTYPE_NAME + 2)
72
Tejun Heob1a21362013-11-29 10:42:58 -050073/*
Tejun Heoace2bee2014-02-11 11:52:47 -050074 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
75 * creation/removal and hierarchy changing operations including cgroup
76 * creation, removal, css association and controller rebinding. This outer
77 * lock is needed mainly to resolve the circular dependency between kernfs
78 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
79 */
80static DEFINE_MUTEX(cgroup_tree_mutex);
81
82/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080083 * cgroup_mutex is the master lock. Any modification to cgroup or its
84 * hierarchy must be performed while holding it.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080085 */
Tejun Heo22194492013-04-07 09:29:51 -070086#ifdef CONFIG_PROVE_RCU
87DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040088EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070089#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070090static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070091#endif
92
Tejun Heo69e943b2014-02-08 10:36:58 -050093/*
94 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
95 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
96 */
97static DEFINE_SPINLOCK(release_agent_path_lock);
98
Tejun Heoace2bee2014-02-11 11:52:47 -050099#define cgroup_assert_mutexes_or_rcu_locked() \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500100 rcu_lockdep_assert(rcu_read_lock_held() || \
Tejun Heoace2bee2014-02-11 11:52:47 -0500101 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo87fb54f2013-12-06 15:11:55 -0500102 lockdep_is_held(&cgroup_mutex), \
Tejun Heoace2bee2014-02-11 11:52:47 -0500103 "cgroup_[tree_]mutex or RCU read lock required");
Tejun Heo87fb54f2013-12-06 15:11:55 -0500104
Ben Blumaae8aab2010-03-10 15:22:07 -0800105/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500106 * cgroup destruction makes heavy use of work items and there can be a lot
107 * of concurrent destructions. Use a separate workqueue so that cgroup
108 * destruction work items don't end up filling up max_active of system_wq
109 * which may lead to deadlock.
110 */
111static struct workqueue_struct *cgroup_destroy_wq;
112
113/*
Tejun Heob1a21362013-11-29 10:42:58 -0500114 * pidlist destructions need to be flushed on cgroup destruction. Use a
115 * separate workqueue as flush domain.
116 */
117static struct workqueue_struct *cgroup_pidlist_destroy_wq;
118
Tejun Heo3ed80a62014-02-08 10:36:58 -0500119/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500120#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo3ed80a62014-02-08 10:36:58 -0500121static struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122#include <linux/cgroup_subsys.h>
123};
Tejun Heo073219e2014-02-08 10:36:58 -0500124#undef SUBSYS
125
126/* array of cgroup subsystem names */
127#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
128static const char *cgroup_subsys_name[] = {
129#include <linux/cgroup_subsys.h>
130};
131#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700134 * The dummy hierarchy, reserved for the subsystems that are otherwise
135 * unattached - it never has more than a single cgroup, and all tasks are
136 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700138static struct cgroupfs_root cgroup_dummy_root;
139
140/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
141static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700142
143/* The list of hierarchy roots */
144
Tejun Heo9871bf92013-06-24 15:21:47 -0700145static LIST_HEAD(cgroup_roots);
146static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700147
Tejun Heo3417ae12014-02-08 10:37:01 -0500148/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700149static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700150
Li Zefan65dff752013-03-01 15:01:56 +0800151static struct cgroup_name root_cgroup_name = { .name = "/" };
152
Li Zefan794611a2013-06-18 18:53:53 +0800153/*
154 * Assign a monotonically increasing serial number to cgroups. It
155 * guarantees cgroups with bigger numbers are newer than those with smaller
156 * numbers. Also, as cgroups are always appended to the parent's
157 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700158 * the ascending serial number order on the list. Protected by
159 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800160 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700161static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800162
Paul Menageddbcc7e2007-10-18 23:39:30 -0700163/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800164 * check for fork/exit handlers to call. This avoids us having to do
165 * extra work in the fork/exit path if none of the subsystems need to
166 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700167 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700168static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169
Tejun Heo628f7cd2013-06-28 16:24:11 -0700170static struct cftype cgroup_base_files[];
171
Tejun Heo59f52962014-02-11 11:52:49 -0500172static void cgroup_put(struct cgroup *cgrp);
Tejun Heof20104d2013-08-13 20:22:50 -0400173static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800174static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400175static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
176 bool is_add);
Tejun Heoe605b362013-11-27 18:16:21 -0500177static int cgroup_file_release(struct inode *inode, struct file *file);
Tejun Heob1a21362013-11-29 10:42:58 -0500178static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800179
Tejun Heo95109b62013-08-08 20:11:27 -0400180/**
181 * cgroup_css - obtain a cgroup's css for the specified subsystem
182 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400183 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400184 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400185 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
186 * function must be called either under cgroup_mutex or rcu_read_lock() and
187 * the caller is responsible for pinning the returned css if it wants to
188 * keep accessing it outside the said locks. This function may return
189 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400190 */
191static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400192 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400193{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400194 if (ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500195 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500196 lockdep_is_held(&cgroup_tree_mutex) ||
197 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400198 else
199 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400200}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700201
Paul Menageddbcc7e2007-10-18 23:39:30 -0700202/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700203static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700204{
Tejun Heo54766d42013-06-12 21:04:53 -0700205 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700206}
207
Tejun Heo59f52962014-02-11 11:52:49 -0500208struct cgroup_subsys_state *seq_css(struct seq_file *seq)
209{
210 struct cgroup_open_file *of = seq->private;
211 return of->cfe->css;
212}
213EXPORT_SYMBOL_GPL(seq_css);
214
Li Zefan78574cf2013-04-08 19:00:38 -0700215/**
216 * cgroup_is_descendant - test ancestry
217 * @cgrp: the cgroup to be tested
218 * @ancestor: possible ancestor of @cgrp
219 *
220 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
221 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
222 * and @ancestor are accessible.
223 */
224bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
225{
226 while (cgrp) {
227 if (cgrp == ancestor)
228 return true;
229 cgrp = cgrp->parent;
230 }
231 return false;
232}
233EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700234
Adrian Bunke9685a02008-02-07 00:13:46 -0800235static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700236{
237 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700238 (1 << CGRP_RELEASABLE) |
239 (1 << CGRP_NOTIFY_ON_RELEASE);
240 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700241}
242
Adrian Bunke9685a02008-02-07 00:13:46 -0800243static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700244{
Paul Menagebd89aab2007-10-18 23:40:44 -0700245 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700246}
247
Tejun Heo30159ec2013-06-25 11:53:37 -0700248/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500249 * for_each_css - iterate all css's of a cgroup
250 * @css: the iteration cursor
251 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
252 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700253 *
254 * Should be called under cgroup_mutex.
255 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500256#define for_each_css(css, ssid, cgrp) \
257 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
258 if (!((css) = rcu_dereference_check( \
259 (cgrp)->subsys[(ssid)], \
Tejun Heoace2bee2014-02-11 11:52:47 -0500260 lockdep_is_held(&cgroup_tree_mutex) || \
Tejun Heo1c6727a2013-12-06 15:11:56 -0500261 lockdep_is_held(&cgroup_mutex)))) { } \
262 else
263
264/**
Tejun Heo3ed80a62014-02-08 10:36:58 -0500265 * for_each_subsys - iterate all enabled cgroup subsystems
Tejun Heo30159ec2013-06-25 11:53:37 -0700266 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500267 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700268 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500269#define for_each_subsys(ss, ssid) \
Tejun Heo3ed80a62014-02-08 10:36:58 -0500270 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
271 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
Tejun Heo30159ec2013-06-25 11:53:37 -0700272
Tejun Heo5549c492013-06-24 15:21:48 -0700273/* iterate across the active hierarchies */
274#define for_each_active_root(root) \
275 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700276
Tejun Heof6ea9372012-04-01 12:09:55 -0700277static inline struct cgroup *__d_cgrp(struct dentry *dentry)
278{
279 return dentry->d_fsdata;
280}
281
Tejun Heo05ef1d72012-04-01 12:09:56 -0700282static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700283{
284 return dentry->d_fsdata;
285}
286
Tejun Heo05ef1d72012-04-01 12:09:56 -0700287static inline struct cftype *__d_cft(struct dentry *dentry)
288{
289 return __d_cfe(dentry)->type;
290}
291
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700292/**
293 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
294 * @cgrp: the cgroup to be checked for liveness
295 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700296 * On success, returns true; the mutex should be later unlocked. On
297 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700298 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700299static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700300{
301 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700302 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700303 mutex_unlock(&cgroup_mutex);
304 return false;
305 }
306 return true;
307}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700308
Paul Menage81a6a5c2007-10-18 23:39:38 -0700309/* the list of cgroups eligible for automatic release. Protected by
310 * release_list_lock */
311static LIST_HEAD(release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +0200312static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700313static void cgroup_release_agent(struct work_struct *work);
314static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700315static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700316
Tejun Heo69d02062013-06-12 21:04:50 -0700317/*
318 * A cgroup can be associated with multiple css_sets as different tasks may
319 * belong to different cgroups on different hierarchies. In the other
320 * direction, a css_set is naturally associated with multiple cgroups.
321 * This M:N relationship is represented by the following link structure
322 * which exists for each association and allows traversing the associations
323 * from both sides.
324 */
325struct cgrp_cset_link {
326 /* the cgroup and css_set this link associates */
327 struct cgroup *cgrp;
328 struct css_set *cset;
329
330 /* list of cgrp_cset_links anchored at cgrp->cset_links */
331 struct list_head cset_link;
332
333 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
334 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700335};
336
337/* The default css_set - used by init and its children prior to any
338 * hierarchies being mounted. It contains a pointer to the root state
339 * for each subsystem. Also used to anchor the list of css_sets. Not
340 * reference-counted, to improve performance when child cgroups
341 * haven't been created.
342 */
343
344static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700345static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700346
Tejun Heo0942eee2013-08-08 20:11:26 -0400347/*
348 * css_set_lock protects the list of css_set objects, and the chain of
349 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400350 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400351 */
Paul Menage817929e2007-10-18 23:39:36 -0700352static DEFINE_RWLOCK(css_set_lock);
353static int css_set_count;
354
Paul Menage7717f7b2009-09-23 15:56:22 -0700355/*
356 * hash table for cgroup groups. This improves the performance to find
357 * an existing css_set. This hash doesn't (currently) take into
358 * account cgroups in empty hierarchies.
359 */
Li Zefan472b1052008-04-29 01:00:11 -0700360#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800361static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700362
Li Zefan0ac801f2013-01-10 11:49:27 +0800363static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700364{
Li Zefan0ac801f2013-01-10 11:49:27 +0800365 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700366 struct cgroup_subsys *ss;
367 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700368
Tejun Heo30159ec2013-06-25 11:53:37 -0700369 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800370 key += (unsigned long)css[i];
371 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700372
Li Zefan0ac801f2013-01-10 11:49:27 +0800373 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700374}
375
Tejun Heo0942eee2013-08-08 20:11:26 -0400376/*
377 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400378 * until after the first call to css_task_iter_start(). This reduces the
379 * fork()/exit() overhead for people who have cgroups compiled into their
380 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400381 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700382static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700383
Tejun Heo5abb8852013-06-12 21:04:49 -0700384static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700385{
Tejun Heo69d02062013-06-12 21:04:50 -0700386 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700387
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700388 /*
389 * Ensure that the refcount doesn't hit zero while any readers
390 * can see it. Similar to atomic_dec_and_lock(), but for an
391 * rwlock
392 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700393 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700394 return;
395 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700396 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700397 write_unlock(&css_set_lock);
398 return;
399 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700400
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700401 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700402 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700403 css_set_count--;
404
Tejun Heo69d02062013-06-12 21:04:50 -0700405 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700406 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700407
Tejun Heo69d02062013-06-12 21:04:50 -0700408 list_del(&link->cset_link);
409 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800410
Tejun Heoddd69142013-06-12 21:04:54 -0700411 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700412 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700413 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700414 set_bit(CGRP_RELEASABLE, &cgrp->flags);
415 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700416 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700417
418 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420
421 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700422 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700423}
424
425/*
426 * refcounted get/put for css_set objects
427 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700428static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700429{
Tejun Heo5abb8852013-06-12 21:04:49 -0700430 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700431}
432
Tejun Heo5abb8852013-06-12 21:04:49 -0700433static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700434{
Tejun Heo5abb8852013-06-12 21:04:49 -0700435 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700436}
437
Tejun Heo5abb8852013-06-12 21:04:49 -0700438static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700439{
Tejun Heo5abb8852013-06-12 21:04:49 -0700440 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700441}
442
Tejun Heob326f9d2013-06-24 15:21:48 -0700443/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700444 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700445 * @cset: candidate css_set being tested
446 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700447 * @new_cgrp: cgroup that's being entered by the task
448 * @template: desired set of css pointers in css_set (pre-calculated)
449 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800450 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700451 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
452 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700453static bool compare_css_sets(struct css_set *cset,
454 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700455 struct cgroup *new_cgrp,
456 struct cgroup_subsys_state *template[])
457{
458 struct list_head *l1, *l2;
459
Tejun Heo5abb8852013-06-12 21:04:49 -0700460 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700461 /* Not all subsystems matched */
462 return false;
463 }
464
465 /*
466 * Compare cgroup pointers in order to distinguish between
467 * different cgroups in heirarchies with no subsystems. We
468 * could get by with just this check alone (and skip the
469 * memcmp above) but on most setups the memcmp check will
470 * avoid the need for this more expensive check on almost all
471 * candidates.
472 */
473
Tejun Heo69d02062013-06-12 21:04:50 -0700474 l1 = &cset->cgrp_links;
475 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700476 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700477 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700478 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700479
480 l1 = l1->next;
481 l2 = l2->next;
482 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700483 if (l1 == &cset->cgrp_links) {
484 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700485 break;
486 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700487 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700488 }
489 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700490 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
491 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
492 cgrp1 = link1->cgrp;
493 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700494 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700495 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700496
497 /*
498 * If this hierarchy is the hierarchy of the cgroup
499 * that's changing, then we need to check that this
500 * css_set points to the new cgroup; if it's any other
501 * hierarchy, then this css_set should point to the
502 * same cgroup as the old css_set.
503 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700504 if (cgrp1->root == new_cgrp->root) {
505 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700506 return false;
507 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700508 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700509 return false;
510 }
511 }
512 return true;
513}
514
Tejun Heob326f9d2013-06-24 15:21:48 -0700515/**
516 * find_existing_css_set - init css array and find the matching css_set
517 * @old_cset: the css_set that we're using before the cgroup transition
518 * @cgrp: the cgroup that we're moving into
519 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700520 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700521static struct css_set *find_existing_css_set(struct css_set *old_cset,
522 struct cgroup *cgrp,
523 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700524{
Paul Menagebd89aab2007-10-18 23:40:44 -0700525 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700526 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800528 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700529 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700530
Ben Blumaae8aab2010-03-10 15:22:07 -0800531 /*
532 * Build the set of subsystem state objects that we want to see in the
533 * new css_set. while subsystems can change globally, the entries here
534 * won't change, so no need for locking.
535 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700536 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400537 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700538 /* Subsystem is in this hierarchy. So we want
539 * the subsystem state from the new
540 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400541 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700542 } else {
543 /* Subsystem is not in this hierarchy, so we
544 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700545 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700546 }
547 }
548
Li Zefan0ac801f2013-01-10 11:49:27 +0800549 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 hash_for_each_possible(css_set_table, cset, hlist, key) {
551 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700552 continue;
553
554 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700555 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700556 }
Paul Menage817929e2007-10-18 23:39:36 -0700557
558 /* No existing cgroup group matched */
559 return NULL;
560}
561
Tejun Heo69d02062013-06-12 21:04:50 -0700562static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700563{
Tejun Heo69d02062013-06-12 21:04:50 -0700564 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700565
Tejun Heo69d02062013-06-12 21:04:50 -0700566 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
567 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700568 kfree(link);
569 }
570}
571
Tejun Heo69d02062013-06-12 21:04:50 -0700572/**
573 * allocate_cgrp_cset_links - allocate cgrp_cset_links
574 * @count: the number of links to allocate
575 * @tmp_links: list_head the allocated links are put on
576 *
577 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
578 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700579 */
Tejun Heo69d02062013-06-12 21:04:50 -0700580static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700581{
Tejun Heo69d02062013-06-12 21:04:50 -0700582 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700583 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700584
585 INIT_LIST_HEAD(tmp_links);
586
Li Zefan36553432008-07-29 22:33:19 -0700587 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700588 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700589 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700590 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700591 return -ENOMEM;
592 }
Tejun Heo69d02062013-06-12 21:04:50 -0700593 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700594 }
595 return 0;
596}
597
Li Zefanc12f65d2009-01-07 18:07:42 -0800598/**
599 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700600 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700601 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800602 * @cgrp: the destination cgroup
603 */
Tejun Heo69d02062013-06-12 21:04:50 -0700604static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
605 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800606{
Tejun Heo69d02062013-06-12 21:04:50 -0700607 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800608
Tejun Heo69d02062013-06-12 21:04:50 -0700609 BUG_ON(list_empty(tmp_links));
610 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
611 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700612 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700613 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700614 /*
615 * Always add links to the tail of the list so that the list
616 * is sorted by order of hierarchy creation
617 */
Tejun Heo69d02062013-06-12 21:04:50 -0700618 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800619}
620
Tejun Heob326f9d2013-06-24 15:21:48 -0700621/**
622 * find_css_set - return a new css_set with one cgroup updated
623 * @old_cset: the baseline css_set
624 * @cgrp: the cgroup to be updated
625 *
626 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
627 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700628 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700629static struct css_set *find_css_set(struct css_set *old_cset,
630 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700631{
Tejun Heob326f9d2013-06-24 15:21:48 -0700632 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700633 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700634 struct list_head tmp_links;
635 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800636 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700637
Tejun Heob326f9d2013-06-24 15:21:48 -0700638 lockdep_assert_held(&cgroup_mutex);
639
Paul Menage817929e2007-10-18 23:39:36 -0700640 /* First see if we already have a cgroup group that matches
641 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700642 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700643 cset = find_existing_css_set(old_cset, cgrp, template);
644 if (cset)
645 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700646 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700647
Tejun Heo5abb8852013-06-12 21:04:49 -0700648 if (cset)
649 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700650
Tejun Heof4f4be22013-06-12 21:04:51 -0700651 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700652 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700653 return NULL;
654
Tejun Heo69d02062013-06-12 21:04:50 -0700655 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700656 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700657 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700658 return NULL;
659 }
660
Tejun Heo5abb8852013-06-12 21:04:49 -0700661 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700662 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700663 INIT_LIST_HEAD(&cset->tasks);
664 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700665
666 /* Copy the set of subsystem state objects generated in
667 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700668 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700669
670 write_lock(&css_set_lock);
671 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700672 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700673 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700674
Paul Menage7717f7b2009-09-23 15:56:22 -0700675 if (c->root == cgrp->root)
676 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700677 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700678 }
Paul Menage817929e2007-10-18 23:39:36 -0700679
Tejun Heo69d02062013-06-12 21:04:50 -0700680 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700681
Paul Menage817929e2007-10-18 23:39:36 -0700682 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700683
684 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700685 key = css_set_hash(cset->subsys);
686 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700687
Paul Menage817929e2007-10-18 23:39:36 -0700688 write_unlock(&css_set_lock);
689
Tejun Heo5abb8852013-06-12 21:04:49 -0700690 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700691}
692
Tejun Heo59f52962014-02-11 11:52:49 -0500693static void cgroup_get_root(struct cgroupfs_root *root)
694{
695 atomic_inc(&root->sb->s_active);
696}
697
698static void cgroup_put_root(struct cgroupfs_root *root)
699{
700 deactivate_super(root->sb);
701}
702
Paul Menageddbcc7e2007-10-18 23:39:30 -0700703/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700704 * Return the cgroup for "task" from the given hierarchy. Must be
705 * called with cgroup_mutex held.
706 */
707static struct cgroup *task_cgroup_from_root(struct task_struct *task,
708 struct cgroupfs_root *root)
709{
Tejun Heo5abb8852013-06-12 21:04:49 -0700710 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700711 struct cgroup *res = NULL;
712
713 BUG_ON(!mutex_is_locked(&cgroup_mutex));
714 read_lock(&css_set_lock);
715 /*
716 * No need to lock the task - since we hold cgroup_mutex the
717 * task can't change groups, so the only thing that can happen
718 * is that it exits and its css is set back to init_css_set.
719 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700720 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700721 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700722 res = &root->top_cgroup;
723 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700724 struct cgrp_cset_link *link;
725
726 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700727 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700728
Paul Menage7717f7b2009-09-23 15:56:22 -0700729 if (c->root == root) {
730 res = c;
731 break;
732 }
733 }
734 }
735 read_unlock(&css_set_lock);
736 BUG_ON(!res);
737 return res;
738}
739
740/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700741 * There is one global cgroup mutex. We also require taking
742 * task_lock() when dereferencing a task's cgroup subsys pointers.
743 * See "The task_lock() exception", at the end of this comment.
744 *
745 * A task must hold cgroup_mutex to modify cgroups.
746 *
747 * Any task can increment and decrement the count field without lock.
748 * So in general, code holding cgroup_mutex can't rely on the count
749 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800750 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 * means that no tasks are currently attached, therefore there is no
752 * way a task attached to that cgroup can fork (the other way to
753 * increment the count). So code holding cgroup_mutex can safely
754 * assume that if the count is zero, it will stay zero. Similarly, if
755 * a task holds cgroup_mutex on a cgroup with zero count, it
756 * knows that the cgroup won't be removed, as cgroup_rmdir()
757 * needs that mutex.
758 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700759 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
760 * (usually) take cgroup_mutex. These are the two most performance
761 * critical pieces of code here. The exception occurs on cgroup_exit(),
762 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
763 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800764 * to the release agent with the name of the cgroup (path relative to
765 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 *
767 * A cgroup can only be deleted if both its 'count' of using tasks
768 * is zero, and its list of 'children' cgroups is empty. Since all
769 * tasks in the system use _some_ cgroup, and since there is always at
770 * least one task in the system (init, pid == 1), therefore, top_cgroup
771 * always has either children cgroups and/or using tasks. So we don't
772 * need a special hack to ensure that top_cgroup cannot be deleted.
773 *
774 * The task_lock() exception
775 *
776 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800777 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800778 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700779 * several performance critical places that need to reference
780 * task->cgroup without the expense of grabbing a system global
781 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800782 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
784 * the task_struct routinely used for such matters.
785 *
786 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800787 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788 */
789
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790/*
791 * A couple of forward declarations required, due to cyclic reference loop:
792 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
793 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
794 * -> cgroup_mkdir.
795 */
796
Al Viro18bb1db2011-07-26 01:41:39 -0400797static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700799static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700800static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700801static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700802
803static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200804 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700805 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700806};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807
Al Viroa5e7ed32011-07-26 01:55:55 -0400808static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809{
810 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700811
812 if (inode) {
Tejun Heob1664922014-02-11 11:52:49 -0500813 do {
814 /* ino 0 is reserved for dummy_root */
815 inode->i_ino = get_next_ino();
816 } while (!inode->i_ino);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100818 inode->i_uid = current_fsuid();
819 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
821 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
822 }
823 return inode;
824}
825
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500826static struct cgroup_name *cgroup_alloc_name(const char *name_str)
Li Zefan65dff752013-03-01 15:01:56 +0800827{
828 struct cgroup_name *name;
829
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500830 name = kmalloc(sizeof(*name) + strlen(name_str) + 1, GFP_KERNEL);
Li Zefan65dff752013-03-01 15:01:56 +0800831 if (!name)
832 return NULL;
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500833 strcpy(name->name, name_str);
Li Zefan65dff752013-03-01 15:01:56 +0800834 return name;
835}
836
Tejun Heo8d7e6fb2014-02-11 11:52:48 -0500837static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
838 char *buf)
839{
840 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
841 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
842 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
843 cft->ss->name, cft->name);
844 else
845 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
846 return buf;
847}
848
Li Zefanbe445622013-01-24 14:31:42 +0800849static void cgroup_free_fn(struct work_struct *work)
850{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700851 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800852
853 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800854 cgrp->root->number_of_cgroups--;
855 mutex_unlock(&cgroup_mutex);
856
857 /*
Tejun Heo59f52962014-02-11 11:52:49 -0500858 * We get a ref to the parent, and put the ref when this cgroup is
859 * being freed, so it's guaranteed that the parent won't be
860 * destroyed before its children.
Li Zefan415cf072013-04-08 14:35:02 +0800861 */
Tejun Heo59f52962014-02-11 11:52:49 -0500862 cgroup_put(cgrp->parent);
Li Zefan415cf072013-04-08 14:35:02 +0800863
Tejun Heo59f52962014-02-11 11:52:49 -0500864 /* put the root reference that we took when we created the cgroup */
865 cgroup_put_root(cgrp->root);
Li Zefanbe445622013-01-24 14:31:42 +0800866
Tejun Heob1a21362013-11-29 10:42:58 -0500867 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800868
869 simple_xattrs_free(&cgrp->xattrs);
870
Li Zefan65dff752013-03-01 15:01:56 +0800871 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800872 kfree(cgrp);
873}
874
875static void cgroup_free_rcu(struct rcu_head *head)
876{
877 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
878
Tejun Heoea15f8c2013-06-13 19:27:42 -0700879 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500880 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800881}
882
Tejun Heo59f52962014-02-11 11:52:49 -0500883static void cgroup_get(struct cgroup *cgrp)
884{
885 dget(cgrp->dentry);
886}
887
Paul Menageddbcc7e2007-10-18 23:39:30 -0700888static void cgroup_diput(struct dentry *dentry, struct inode *inode)
889{
890 /* is dentry a directory ? if so, kfree() associated cgroup */
891 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700892 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800893
Tejun Heo54766d42013-06-12 21:04:53 -0700894 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanc1a71502013-12-17 11:13:39 +0800895
896 /*
897 * XXX: cgrp->id is only used to look up css's. As cgroup
898 * and css's lifetimes will be decoupled, it should be made
899 * per-subsystem and moved to css->id so that lookups are
900 * successful until the target css is released.
901 */
Li Zefan0ab02ca2014-02-11 16:05:46 +0800902 mutex_lock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800903 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +0800904 mutex_unlock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800905 cgrp->id = -1;
906
Li Zefanbe445622013-01-24 14:31:42 +0800907 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700908 } else {
909 struct cfent *cfe = __d_cfe(dentry);
910 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
911
912 WARN_ONCE(!list_empty(&cfe->node) &&
913 cgrp != &cgrp->root->top_cgroup,
914 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700915 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700916 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700917 }
918 iput(inode);
919}
920
Tejun Heo59f52962014-02-11 11:52:49 -0500921static void cgroup_put(struct cgroup *cgrp)
922{
923 dput(cgrp->dentry);
924}
925
Paul Menageddbcc7e2007-10-18 23:39:30 -0700926static void remove_dir(struct dentry *d)
927{
928 struct dentry *parent = dget(d->d_parent);
929
930 d_delete(d);
931 simple_rmdir(parent->d_inode, d);
932 dput(parent);
933}
934
Li Zefan2739d3c2013-01-21 18:18:33 +0800935static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700936{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700938
Tejun Heo05ef1d72012-04-01 12:09:56 -0700939 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -0500940 lockdep_assert_held(&cgroup_tree_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100941
Li Zefan2739d3c2013-01-21 18:18:33 +0800942 /*
943 * If we're doing cleanup due to failure of cgroup_create(),
944 * the corresponding @cfe may not exist.
945 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700946 list_for_each_entry(cfe, &cgrp->files, node) {
947 struct dentry *d = cfe->dentry;
948
949 if (cft && cfe->type != cft)
950 continue;
951
952 dget(d);
953 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700954 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700955 list_del_init(&cfe->node);
956 dput(d);
957
Li Zefan2739d3c2013-01-21 18:18:33 +0800958 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700959 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700960}
961
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400962/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700963 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700964 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400965 * @subsys_mask: mask of the subsystem ids whose files should be removed
966 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700967static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700968{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400969 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700970 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700971
Tejun Heob420ba72013-07-12 12:34:02 -0700972 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400973 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700974
975 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976 continue;
977 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400978 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400979 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980}
981
982/*
983 * NOTE : the dentry must have been dget()'ed
984 */
985static void cgroup_d_remove_dir(struct dentry *dentry)
986{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100987 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100989 parent = dentry->d_parent;
990 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800991 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100993 spin_unlock(&dentry->d_lock);
994 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995 remove_dir(dentry);
996}
997
998static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700999 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000{
Paul Menagebd89aab2007-10-18 23:40:44 -07001001 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001002 struct cgroup_subsys *ss;
Tejun Heo31261212013-06-28 17:07:30 -07001003 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004
Tejun Heoace2bee2014-02-11 11:52:47 -05001005 lockdep_assert_held(&cgroup_tree_mutex);
1006 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001007
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008 /* Check that any added subsystems are currently free */
Tejun Heo3ed80a62014-02-08 10:36:58 -05001009 for_each_subsys(ss, i)
1010 if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
1011 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012
Tejun Heo31261212013-06-28 17:07:30 -07001013 ret = cgroup_populate_dir(cgrp, added_mask);
1014 if (ret)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001015 return ret;
Tejun Heo31261212013-06-28 17:07:30 -07001016
1017 /*
1018 * Nothing can fail from this point on. Remove files for the
1019 * removed subsystems and rebind each subsystem.
1020 */
Tejun Heo4ac06012014-02-11 11:52:47 -05001021 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001022 cgroup_clear_dir(cgrp, removed_mask);
Tejun Heo4ac06012014-02-11 11:52:47 -05001023 mutex_lock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001024
Tejun Heo30159ec2013-06-25 11:53:37 -07001025 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001027
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001028 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001030 BUG_ON(cgroup_css(cgrp, ss));
1031 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1032 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001033
Tejun Heo73e80ed2013-08-13 11:01:55 -04001034 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001035 cgroup_css(cgroup_dummy_top, ss));
1036 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001037
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001038 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001040 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001041
Ben Blumcf5d5942010-03-10 15:22:09 -08001042 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001043 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001044 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001046 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1047 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001048
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001050 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001051
Tejun Heoca8bdca2013-08-26 18:40:56 -04001052 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001053 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1054
Tejun Heo9871bf92013-06-24 15:21:47 -07001055 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001056 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057 }
1058 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059
Tejun Heo1672d042013-06-25 18:04:54 -07001060 /*
1061 * Mark @root has finished binding subsystems. @root->subsys_mask
1062 * now matches the bound subsystems.
1063 */
1064 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1065
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 return 0;
1067}
1068
Al Viro34c80b12011-12-08 21:32:45 -05001069static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070{
Al Viro34c80b12011-12-08 21:32:45 -05001071 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001073 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074
Tejun Heob85d2042013-12-06 15:11:57 -05001075 for_each_subsys(ss, ssid)
1076 if (root->subsys_mask & (1 << ssid))
1077 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001078 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1079 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001080 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001082 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001083 seq_puts(seq, ",xattr");
Tejun Heo69e943b2014-02-08 10:36:58 -05001084
1085 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001086 if (strlen(root->release_agent_path))
1087 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo69e943b2014-02-08 10:36:58 -05001088 spin_unlock(&release_agent_path_lock);
1089
Tejun Heo2260e7f2012-11-19 08:13:38 -08001090 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001091 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001092 if (strlen(root->name))
1093 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 return 0;
1095}
1096
1097struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001098 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001100 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001101 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001102 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001103 /* User explicitly requested empty subsystem */
1104 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001105
1106 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001107
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108};
1109
Ben Blumaae8aab2010-03-10 15:22:07 -08001110/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001111 * Convert a hierarchy specifier into a bitmask of subsystems and
1112 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1113 * array. This function takes refcounts on subsystems to be used, unless it
1114 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001115 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001116static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001117{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001118 char *token, *o = data;
1119 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001120 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001121 struct cgroup_subsys *ss;
1122 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001123
Ben Blumaae8aab2010-03-10 15:22:07 -08001124 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1125
Li Zefanf9ab5b52009-06-17 16:26:33 -07001126#ifdef CONFIG_CPUSETS
Tejun Heo073219e2014-02-08 10:36:58 -05001127 mask = ~(1UL << cpuset_cgrp_id);
Li Zefanf9ab5b52009-06-17 16:26:33 -07001128#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129
Paul Menagec6d57f32009-09-23 15:56:19 -07001130 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131
1132 while ((token = strsep(&o, ",")) != NULL) {
1133 if (!*token)
1134 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001135 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001136 /* Explicitly have no subsystems */
1137 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001138 continue;
1139 }
1140 if (!strcmp(token, "all")) {
1141 /* Mutually exclusive option 'all' + subsystem name */
1142 if (one_ss)
1143 return -EINVAL;
1144 all_ss = true;
1145 continue;
1146 }
Tejun Heo873fe092013-04-14 20:15:26 -07001147 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1148 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1149 continue;
1150 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001151 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001152 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001153 continue;
1154 }
1155 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001156 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001157 continue;
1158 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001159 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001160 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001161 continue;
1162 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001163 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001164 /* Specifying two release agents is forbidden */
1165 if (opts->release_agent)
1166 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001167 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001168 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001169 if (!opts->release_agent)
1170 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001171 continue;
1172 }
1173 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001174 const char *name = token + 5;
1175 /* Can't specify an empty name */
1176 if (!strlen(name))
1177 return -EINVAL;
1178 /* Must match [\w.-]+ */
1179 for (i = 0; i < strlen(name); i++) {
1180 char c = name[i];
1181 if (isalnum(c))
1182 continue;
1183 if ((c == '.') || (c == '-') || (c == '_'))
1184 continue;
1185 return -EINVAL;
1186 }
1187 /* Specifying two names is forbidden */
1188 if (opts->name)
1189 return -EINVAL;
1190 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001191 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001192 GFP_KERNEL);
1193 if (!opts->name)
1194 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001195
1196 continue;
1197 }
1198
Tejun Heo30159ec2013-06-25 11:53:37 -07001199 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001200 if (strcmp(token, ss->name))
1201 continue;
1202 if (ss->disabled)
1203 continue;
1204
1205 /* Mutually exclusive option 'all' + subsystem name */
1206 if (all_ss)
1207 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001208 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001209 one_ss = true;
1210
1211 break;
1212 }
1213 if (i == CGROUP_SUBSYS_COUNT)
1214 return -ENOENT;
1215 }
1216
1217 /*
1218 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001219 * otherwise if 'none', 'name=' and a subsystem name options
1220 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001221 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001222 if (all_ss || (!one_ss && !opts->none && !opts->name))
1223 for_each_subsys(ss, i)
1224 if (!ss->disabled)
1225 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001226
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001227 /* Consistency checks */
1228
Tejun Heo873fe092013-04-14 20:15:26 -07001229 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1230 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1231
1232 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1233 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1234 return -EINVAL;
1235 }
1236
1237 if (opts->cpuset_clone_children) {
1238 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1239 return -EINVAL;
1240 }
1241 }
1242
Li Zefanf9ab5b52009-06-17 16:26:33 -07001243 /*
1244 * Option noprefix was introduced just for backward compatibility
1245 * with the old cpuset, so we allow noprefix only if mounting just
1246 * the cpuset subsystem.
1247 */
Tejun Heo93438622013-04-14 20:15:25 -07001248 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001249 return -EINVAL;
1250
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001251
1252 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001253 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001254 return -EINVAL;
1255
1256 /*
1257 * We either have to specify by name or by subsystems. (So all
1258 * empty hierarchies must have a name).
1259 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001260 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261 return -EINVAL;
1262
1263 return 0;
1264}
1265
1266static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1267{
1268 int ret = 0;
1269 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001270 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001271 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001272 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001273
Tejun Heo873fe092013-04-14 20:15:26 -07001274 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1275 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1276 return -EINVAL;
1277 }
1278
Paul Menagebd89aab2007-10-18 23:40:44 -07001279 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001280 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001281 mutex_lock(&cgroup_mutex);
1282
1283 /* See what subsystems are wanted */
1284 ret = parse_cgroupfs_options(data, &opts);
1285 if (ret)
1286 goto out_unlock;
1287
Tejun Heoa8a648c2013-06-24 15:21:47 -07001288 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001289 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1290 task_tgid_nr(current), current->comm);
1291
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001292 added_mask = opts.subsys_mask & ~root->subsys_mask;
1293 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001294
Ben Blumcf5d5942010-03-10 15:22:09 -08001295 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001296 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001297 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001298 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1299 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1300 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001302 goto out_unlock;
1303 }
1304
Tejun Heof172e672013-06-28 17:07:30 -07001305 /* remounting is not allowed for populated hierarchies */
1306 if (root->number_of_cgroups > 1) {
1307 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001308 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001310
Paul Menageddbcc7e2007-10-18 23:39:30 -07001311 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001312 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314
Tejun Heo69e943b2014-02-08 10:36:58 -05001315 if (opts.release_agent) {
1316 spin_lock(&release_agent_path_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001317 strcpy(root->release_agent_path, opts.release_agent);
Tejun Heo69e943b2014-02-08 10:36:58 -05001318 spin_unlock(&release_agent_path_lock);
1319 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001320 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001321 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001322 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001324 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001325 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 return ret;
1327}
1328
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001329static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330 .statfs = simple_statfs,
1331 .drop_inode = generic_delete_inode,
1332 .show_options = cgroup_show_options,
1333 .remount_fs = cgroup_remount,
1334};
1335
Paul Menagecc31edc2008-10-18 20:28:04 -07001336static void init_cgroup_housekeeping(struct cgroup *cgrp)
1337{
1338 INIT_LIST_HEAD(&cgrp->sibling);
1339 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001340 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001341 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001342 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001343 INIT_LIST_HEAD(&cgrp->pidlists);
1344 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001345 cgrp->dummy_css.cgroup = cgrp;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001346 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001347}
Paul Menagec6d57f32009-09-23 15:56:19 -07001348
Paul Menageddbcc7e2007-10-18 23:39:30 -07001349static void init_cgroup_root(struct cgroupfs_root *root)
1350{
Paul Menagebd89aab2007-10-18 23:40:44 -07001351 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001352
Paul Menageddbcc7e2007-10-18 23:39:30 -07001353 INIT_LIST_HEAD(&root->root_list);
1354 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001355 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001356 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001357 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001358 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359}
1360
Tejun Heofc76df72013-06-25 11:53:37 -07001361static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001362{
Tejun Heo1a574232013-04-14 11:36:58 -07001363 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001364
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001365 lockdep_assert_held(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001366
Tejun Heofc76df72013-06-25 11:53:37 -07001367 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1368 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001369 if (id < 0)
1370 return id;
1371
1372 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001373 return 0;
1374}
1375
1376static void cgroup_exit_root_id(struct cgroupfs_root *root)
1377{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001378 lockdep_assert_held(&cgroup_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001379
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001380 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001381 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001382 root->hierarchy_id = 0;
1383 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001384}
1385
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386static int cgroup_test_super(struct super_block *sb, void *data)
1387{
Paul Menagec6d57f32009-09-23 15:56:19 -07001388 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389 struct cgroupfs_root *root = sb->s_fs_info;
1390
Paul Menagec6d57f32009-09-23 15:56:19 -07001391 /* If we asked for a name then it must match */
1392 if (opts->name && strcmp(opts->name, root->name))
1393 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001395 /*
1396 * If we asked for subsystems (or explicitly for no
1397 * subsystems) then they must match
1398 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001399 if ((opts->subsys_mask || opts->none)
1400 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 return 0;
1402
1403 return 1;
1404}
1405
Paul Menagec6d57f32009-09-23 15:56:19 -07001406static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1407{
1408 struct cgroupfs_root *root;
1409
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001410 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001411 return NULL;
1412
1413 root = kzalloc(sizeof(*root), GFP_KERNEL);
1414 if (!root)
1415 return ERR_PTR(-ENOMEM);
1416
1417 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001418
Tejun Heo1672d042013-06-25 18:04:54 -07001419 /*
1420 * We need to set @root->subsys_mask now so that @root can be
1421 * matched by cgroup_test_super() before it finishes
1422 * initialization; otherwise, competing mounts with the same
1423 * options may try to bind the same subsystems instead of waiting
1424 * for the first one leading to unexpected mount errors.
1425 * SUBSYS_BOUND will be set once actual binding is complete.
1426 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001427 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001428 root->flags = opts->flags;
1429 if (opts->release_agent)
1430 strcpy(root->release_agent_path, opts->release_agent);
1431 if (opts->name)
1432 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001433 if (opts->cpuset_clone_children)
1434 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001435 return root;
1436}
1437
Tejun Heofa3ca072013-04-14 11:36:56 -07001438static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001439{
Tejun Heofa3ca072013-04-14 11:36:56 -07001440 if (root) {
1441 /* hierarhcy ID shoulid already have been released */
1442 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001443
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001444 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001445 kfree(root);
1446 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001447}
1448
Paul Menageddbcc7e2007-10-18 23:39:30 -07001449static int cgroup_set_super(struct super_block *sb, void *data)
1450{
1451 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001452 struct cgroup_sb_opts *opts = data;
1453
1454 /* If we don't have a new root, we can't set up a new sb */
1455 if (!opts->new_root)
1456 return -EINVAL;
1457
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001458 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459
1460 ret = set_anon_super(sb, NULL);
1461 if (ret)
1462 return ret;
1463
Paul Menagec6d57f32009-09-23 15:56:19 -07001464 sb->s_fs_info = opts->new_root;
1465 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466
1467 sb->s_blocksize = PAGE_CACHE_SIZE;
1468 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1469 sb->s_magic = CGROUP_SUPER_MAGIC;
1470 sb->s_op = &cgroup_ops;
1471
1472 return 0;
1473}
1474
1475static int cgroup_get_rootdir(struct super_block *sb)
1476{
Al Viro0df6a632010-12-21 13:29:29 -05001477 static const struct dentry_operations cgroup_dops = {
1478 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001479 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001480 };
1481
Paul Menageddbcc7e2007-10-18 23:39:30 -07001482 struct inode *inode =
1483 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001484
1485 if (!inode)
1486 return -ENOMEM;
1487
Paul Menageddbcc7e2007-10-18 23:39:30 -07001488 inode->i_fop = &simple_dir_operations;
1489 inode->i_op = &cgroup_dir_inode_operations;
1490 /* directories start off with i_nlink == 2 (for "." entry) */
1491 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001492 sb->s_root = d_make_root(inode);
1493 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001495 /* for everything else we want ->d_op set */
1496 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001497 return 0;
1498}
1499
Tejun Heod427dfe2014-02-11 11:52:48 -05001500static int cgroup_setup_root(struct cgroupfs_root *root)
1501{
1502 LIST_HEAD(tmp_links);
1503 struct super_block *sb = root->sb;
1504 struct cgroup *root_cgrp = &root->top_cgroup;
1505 struct cgroupfs_root *existing_root;
1506 struct css_set *cset;
1507 struct inode *inode;
1508 const struct cred *cred;
1509 int i, ret;
1510
1511 lockdep_assert_held(&cgroup_tree_mutex);
1512 lockdep_assert_held(&cgroup_mutex);
1513 BUG_ON(sb->s_root != NULL);
1514
1515 mutex_unlock(&cgroup_mutex);
1516 mutex_unlock(&cgroup_tree_mutex);
1517
1518 ret = cgroup_get_rootdir(sb);
1519 if (ret) {
1520 mutex_lock(&cgroup_tree_mutex);
1521 mutex_lock(&cgroup_mutex);
1522 return ret;
1523 }
1524 inode = sb->s_root->d_inode;
1525
1526 mutex_lock(&inode->i_mutex);
1527 mutex_lock(&cgroup_tree_mutex);
1528 mutex_lock(&cgroup_mutex);
1529
1530 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1531 if (ret < 0)
1532 goto out_unlock;
1533 root_cgrp->id = ret;
1534
1535 /* check for name clashes with existing mounts */
1536 ret = -EBUSY;
1537 if (strlen(root->name))
1538 for_each_active_root(existing_root)
1539 if (!strcmp(existing_root->name, root->name))
1540 goto out_unlock;
1541
1542 /*
1543 * We're accessing css_set_count without locking css_set_lock here,
1544 * but that's OK - it can only be increased by someone holding
1545 * cgroup_lock, and that's us. The worst that can happen is that we
1546 * have some link structures left over
1547 */
1548 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1549 if (ret)
1550 goto out_unlock;
1551
1552 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1553 ret = cgroup_init_root_id(root, 2, 0);
1554 if (ret)
1555 goto out_unlock;
1556
1557 sb->s_root->d_fsdata = root_cgrp;
1558 root_cgrp->dentry = sb->s_root;
1559
1560 /*
1561 * We're inside get_sb() and will call lookup_one_len() to create
1562 * the root files, which doesn't work if SELinux is in use. The
1563 * following cred dancing somehow works around it. See 2ce9738ba
1564 * ("cgroupfs: use init_cred when populating new cgroupfs mount")
1565 * for more details.
1566 */
1567 cred = override_creds(&init_cred);
1568
1569 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1570 if (ret)
1571 goto rm_base_files;
1572
1573 ret = rebind_subsystems(root, root->subsys_mask, 0);
1574 if (ret)
1575 goto rm_base_files;
1576
1577 revert_creds(cred);
1578
1579 /*
1580 * There must be no failure case after here, since rebinding takes
1581 * care of subsystems' refcounts, which are explicitly dropped in
1582 * the failure exit path.
1583 */
1584 list_add(&root->root_list, &cgroup_roots);
1585 cgroup_root_count++;
1586
1587 /*
1588 * Link the top cgroup in this hierarchy into all the css_set
1589 * objects.
1590 */
1591 write_lock(&css_set_lock);
1592 hash_for_each(css_set_table, i, cset, hlist)
1593 link_css_set(&tmp_links, cset, root_cgrp);
1594 write_unlock(&css_set_lock);
1595
1596 BUG_ON(!list_empty(&root_cgrp->children));
1597 BUG_ON(root->number_of_cgroups != 1);
1598
1599 ret = 0;
1600 goto out_unlock;
1601
1602rm_base_files:
1603 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
1604 revert_creds(cred);
1605 cgroup_exit_root_id(root);
1606out_unlock:
1607 mutex_unlock(&inode->i_mutex);
1608 free_cgrp_cset_links(&tmp_links);
1609 return ret;
1610}
1611
Al Virof7e83572010-07-26 13:23:11 +04001612static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001614 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001615{
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001616 struct super_block *sb = NULL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001617 struct cgroupfs_root *root = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001618 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001619 struct cgroupfs_root *new_root;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001620 int ret;
1621
1622 mutex_lock(&cgroup_tree_mutex);
1623 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624
1625 /* First find the desired set of subsystems */
1626 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001627 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001628 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001629
1630 /*
1631 * Allocate a new cgroup root. We may not need it if we're
1632 * reusing an existing hierarchy.
1633 */
1634 new_root = cgroup_root_from_opts(&opts);
1635 if (IS_ERR(new_root)) {
1636 ret = PTR_ERR(new_root);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001637 goto out_unlock;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001638 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001639 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640
Paul Menagec6d57f32009-09-23 15:56:19 -07001641 /* Locate an existing or new sb for this hierarchy */
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001642 mutex_unlock(&cgroup_mutex);
1643 mutex_unlock(&cgroup_tree_mutex);
David Howells9249e172012-06-25 12:55:37 +01001644 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001645 mutex_lock(&cgroup_tree_mutex);
1646 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001648 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001649 cgroup_free_root(opts.new_root);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001650 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001651 }
1652
Paul Menagec6d57f32009-09-23 15:56:19 -07001653 root = sb->s_fs_info;
1654 BUG_ON(!root);
1655 if (root == opts.new_root) {
Tejun Heod427dfe2014-02-11 11:52:48 -05001656 ret = cgroup_setup_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657 if (ret)
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001658 goto out_unlock;
Paul Menagec6d57f32009-09-23 15:56:19 -07001659 } else {
1660 /*
1661 * We re-used an existing hierarchy - the new root (if
1662 * any) is not needed
1663 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001664 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001665
Tejun Heoc7ba8282013-06-29 14:06:10 -07001666 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001667 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1668 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1669 ret = -EINVAL;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001670 goto out_unlock;
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001671 } else {
1672 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1673 }
Tejun Heo873fe092013-04-14 20:15:26 -07001674 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675 }
1676
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001677 ret = 0;
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001678out_unlock:
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001679 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001680 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001681
1682 if (ret && !IS_ERR_OR_NULL(sb))
1683 deactivate_locked_super(sb);
1684
Paul Menagec6d57f32009-09-23 15:56:19 -07001685 kfree(opts.release_agent);
1686 kfree(opts.name);
Tejun Heo8e30e2b2014-02-11 11:52:48 -05001687
1688 if (!ret)
1689 return dget(sb->s_root);
1690 else
1691 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692}
1693
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001694static void cgroup_kill_sb(struct super_block *sb)
1695{
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001697 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001698 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699 int ret;
1700
1701 BUG_ON(!root);
1702
1703 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001704 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705
Tejun Heo31261212013-06-28 17:07:30 -07001706 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001707 mutex_lock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708 mutex_lock(&cgroup_mutex);
1709
1710 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001711 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1712 ret = rebind_subsystems(root, 0, root->subsys_mask);
1713 /* Shouldn't be able to fail ... */
1714 BUG_ON(ret);
1715 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716
Paul Menage817929e2007-10-18 23:39:36 -07001717 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001718 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001719 * root cgroup
1720 */
1721 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001722
Tejun Heo69d02062013-06-12 21:04:50 -07001723 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1724 list_del(&link->cset_link);
1725 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001726 kfree(link);
1727 }
1728 write_unlock(&css_set_lock);
1729
Paul Menage839ec542009-01-29 14:25:22 -08001730 if (!list_empty(&root->root_list)) {
1731 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001732 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001733 }
Li Zefane5f6a862009-01-07 18:07:41 -08001734
Tejun Heofa3ca072013-04-14 11:36:56 -07001735 cgroup_exit_root_id(root);
1736
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05001738 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001739 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001741 simple_xattrs_free(&cgrp->xattrs);
1742
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001744 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001745}
1746
1747static struct file_system_type cgroup_fs_type = {
1748 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001749 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750 .kill_sb = cgroup_kill_sb,
1751};
1752
Greg KH676db4a2010-08-05 13:53:35 -07001753static struct kobject *cgroup_kobj;
1754
Li Zefana043e3b2008-02-23 15:24:09 -08001755/**
1756 * cgroup_path - generate the path of a cgroup
1757 * @cgrp: the cgroup in question
1758 * @buf: the buffer to write the path into
1759 * @buflen: the length of the buffer
1760 *
Li Zefan65dff752013-03-01 15:01:56 +08001761 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1762 *
1763 * We can't generate cgroup path using dentry->d_name, as accessing
1764 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1765 * inode's i_mutex, while on the other hand cgroup_path() can be called
1766 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001768int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769{
Li Zefan65dff752013-03-01 15:01:56 +08001770 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001772
Tejun Heoda1f2962013-04-14 10:32:19 -07001773 if (!cgrp->parent) {
1774 if (strlcpy(buf, "/", buflen) >= buflen)
1775 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 return 0;
1777 }
1778
Tao Ma316eb662012-11-08 21:36:38 +08001779 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001780 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001781
Li Zefan65dff752013-03-01 15:01:56 +08001782 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001783 do {
Li Zefan65dff752013-03-01 15:01:56 +08001784 const char *name = cgroup_name(cgrp);
1785 int len;
1786
1787 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001789 goto out;
1790 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001791
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001793 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001794 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001795
1796 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001797 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001798 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001800out:
1801 rcu_read_unlock();
1802 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803}
Ben Blum67523c42010-03-10 15:22:11 -08001804EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805
Tejun Heo857a2be2013-04-14 20:50:08 -07001806/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001807 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001808 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001809 * @buf: the buffer to write the path into
1810 * @buflen: the length of the buffer
1811 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001812 * Determine @task's cgroup on the first (the one with the lowest non-zero
1813 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1814 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1815 * cgroup controller callbacks.
1816 *
1817 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001818 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001819int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001820{
1821 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001822 struct cgroup *cgrp;
1823 int hierarchy_id = 1, ret = 0;
1824
1825 if (buflen < 2)
1826 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001827
1828 mutex_lock(&cgroup_mutex);
1829
Tejun Heo913ffdb2013-07-11 16:34:48 -07001830 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1831
Tejun Heo857a2be2013-04-14 20:50:08 -07001832 if (root) {
1833 cgrp = task_cgroup_from_root(task, root);
1834 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001835 } else {
1836 /* if no hierarchy exists, everyone is in "/" */
1837 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001838 }
1839
1840 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001841 return ret;
1842}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001843EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001844
Ben Blum74a11662011-05-26 16:25:20 -07001845/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001846 * Control Group taskset
1847 */
Tejun Heo134d3372011-12-12 18:12:21 -08001848struct task_and_cgroup {
1849 struct task_struct *task;
1850 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001851 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001852};
1853
Tejun Heo2f7ee562011-12-12 18:12:21 -08001854struct cgroup_taskset {
1855 struct task_and_cgroup single;
1856 struct flex_array *tc_array;
1857 int tc_array_len;
1858 int idx;
1859 struct cgroup *cur_cgrp;
1860};
1861
1862/**
1863 * cgroup_taskset_first - reset taskset and return the first task
1864 * @tset: taskset of interest
1865 *
1866 * @tset iteration is initialized and the first task is returned.
1867 */
1868struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1869{
1870 if (tset->tc_array) {
1871 tset->idx = 0;
1872 return cgroup_taskset_next(tset);
1873 } else {
1874 tset->cur_cgrp = tset->single.cgrp;
1875 return tset->single.task;
1876 }
1877}
1878EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1879
1880/**
1881 * cgroup_taskset_next - iterate to the next task in taskset
1882 * @tset: taskset of interest
1883 *
1884 * Return the next task in @tset. Iteration must have been initialized
1885 * with cgroup_taskset_first().
1886 */
1887struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1888{
1889 struct task_and_cgroup *tc;
1890
1891 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1892 return NULL;
1893
1894 tc = flex_array_get(tset->tc_array, tset->idx++);
1895 tset->cur_cgrp = tc->cgrp;
1896 return tc->task;
1897}
1898EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1899
1900/**
Tejun Heod99c8722013-08-08 20:11:27 -04001901 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001902 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001903 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001904 *
Tejun Heod99c8722013-08-08 20:11:27 -04001905 * Return the css for the current (last returned) task of @tset for
1906 * subsystem specified by @subsys_id. This function must be preceded by
1907 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001908 */
Tejun Heod99c8722013-08-08 20:11:27 -04001909struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1910 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001911{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001912 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001913}
Tejun Heod99c8722013-08-08 20:11:27 -04001914EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001915
1916/**
1917 * cgroup_taskset_size - return the number of tasks in taskset
1918 * @tset: taskset of interest
1919 */
1920int cgroup_taskset_size(struct cgroup_taskset *tset)
1921{
1922 return tset->tc_array ? tset->tc_array_len : 1;
1923}
1924EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1925
1926
Ben Blum74a11662011-05-26 16:25:20 -07001927/*
1928 * cgroup_task_migrate - move a task from one cgroup to another.
1929 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001930 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001931 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001932static void cgroup_task_migrate(struct cgroup *old_cgrp,
1933 struct task_struct *tsk,
1934 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001935{
Tejun Heo5abb8852013-06-12 21:04:49 -07001936 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001937
1938 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001939 * We are synchronized through threadgroup_lock() against PF_EXITING
1940 * setting such that we can't race against cgroup_exit() changing the
1941 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001942 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001943 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001944 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001945
Ben Blum74a11662011-05-26 16:25:20 -07001946 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001947 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001948 task_unlock(tsk);
1949
1950 /* Update the css_set linked lists if we're using them */
1951 write_lock(&css_set_lock);
1952 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001953 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001954 write_unlock(&css_set_lock);
1955
1956 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001957 * We just gained a reference on old_cset by taking it from the
1958 * task. As trading it for new_cset is protected by cgroup_mutex,
1959 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001960 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001961 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1962 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001963}
1964
Li Zefana043e3b2008-02-23 15:24:09 -08001965/**
Li Zefan081aa452013-03-13 09:17:09 +08001966 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001967 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001968 * @tsk: the task or the leader of the threadgroup to be attached
1969 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001970 *
Tejun Heo257058a2011-12-12 18:12:21 -08001971 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001972 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001973 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001974static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1975 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001976{
1977 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001978 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001979 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001980 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001981 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001982 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001983 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001984 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001985
1986 /*
1987 * step 0: in order to do expensive, possibly blocking operations for
1988 * every thread, we cannot iterate the thread group list, since it needs
1989 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001990 * group - group_rwsem prevents new threads from appearing, and if
1991 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001992 */
Li Zefan081aa452013-03-13 09:17:09 +08001993 if (threadgroup)
1994 group_size = get_nr_threads(tsk);
1995 else
1996 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001997 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001998 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001999 if (!group)
2000 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002001 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002002 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002003 if (retval)
2004 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002005
Ben Blum74a11662011-05-26 16:25:20 -07002006 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002007 /*
2008 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2009 * already PF_EXITING could be freed from underneath us unless we
2010 * take an rcu_read_lock.
2011 */
2012 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002013 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002014 struct task_and_cgroup ent;
2015
Tejun Heocd3d0952011-12-12 18:12:21 -08002016 /* @tsk either already exited or can't exit until the end */
2017 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002018 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002019
Ben Blum74a11662011-05-26 16:25:20 -07002020 /* as per above, nr_threads may decrease, but not increase. */
2021 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002022 ent.task = tsk;
2023 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002024 /* nothing to do if this task is already in the cgroup */
2025 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08002026 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002027 /*
2028 * saying GFP_ATOMIC has no effect here because we did prealloc
2029 * earlier, but it's good form to communicate our expectations.
2030 */
Tejun Heo134d3372011-12-12 18:12:21 -08002031 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002032 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002033 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002034 next:
Li Zefan081aa452013-03-13 09:17:09 +08002035 if (!threadgroup)
2036 break;
Ben Blum74a11662011-05-26 16:25:20 -07002037 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002038 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002039 /* remember the number of threads in the array for later. */
2040 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002041 tset.tc_array = group;
2042 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002043
Tejun Heo134d3372011-12-12 18:12:21 -08002044 /* methods shouldn't be called if no task is actually migrating */
2045 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002046 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002047 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002048
Ben Blum74a11662011-05-26 16:25:20 -07002049 /*
2050 * step 1: check that we can legitimately attach to the cgroup.
2051 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002052 for_each_css(css, i, cgrp) {
2053 if (css->ss->can_attach) {
2054 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002055 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002056 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002057 goto out_cancel_attach;
2058 }
2059 }
Ben Blum74a11662011-05-26 16:25:20 -07002060 }
2061
2062 /*
2063 * step 2: make sure css_sets exist for all threads to be migrated.
2064 * we use find_css_set, which allocates a new one if necessary.
2065 */
Ben Blum74a11662011-05-26 16:25:20 -07002066 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002067 struct css_set *old_cset;
2068
Tejun Heo134d3372011-12-12 18:12:21 -08002069 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002070 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002071 tc->cset = find_css_set(old_cset, cgrp);
2072 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002073 retval = -ENOMEM;
2074 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002075 }
2076 }
2077
2078 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002079 * step 3: now that we're guaranteed success wrt the css_sets,
2080 * proceed to move all tasks to the new cgroup. There are no
2081 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002082 */
Ben Blum74a11662011-05-26 16:25:20 -07002083 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002084 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002085 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002086 }
2087 /* nothing is sensitive to fork() after this point. */
2088
2089 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002090 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002091 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002092 for_each_css(css, i, cgrp)
2093 if (css->ss->attach)
2094 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002095
2096 /*
2097 * step 5: success! and cleanup
2098 */
Ben Blum74a11662011-05-26 16:25:20 -07002099 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002100out_put_css_set_refs:
2101 if (retval) {
2102 for (i = 0; i < group_size; i++) {
2103 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002104 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002105 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002106 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002107 }
Ben Blum74a11662011-05-26 16:25:20 -07002108 }
2109out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002110 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002111 for_each_css(css, i, cgrp) {
2112 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07002113 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05002114 if (css->ss->cancel_attach)
2115 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002116 }
2117 }
Ben Blum74a11662011-05-26 16:25:20 -07002118out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002119 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002120 return retval;
2121}
2122
2123/*
2124 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002125 * function to attach either it or all tasks in its threadgroup. Will lock
2126 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002127 */
2128static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002130 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002131 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002132 int ret;
2133
Ben Blum74a11662011-05-26 16:25:20 -07002134 if (!cgroup_lock_live_group(cgrp))
2135 return -ENODEV;
2136
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002137retry_find_task:
2138 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002140 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002141 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002143 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002144 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002145 }
Ben Blum74a11662011-05-26 16:25:20 -07002146 /*
2147 * even if we're attaching all tasks in the thread group, we
2148 * only need to check permissions on one of them.
2149 */
David Howellsc69e8d92008-11-14 10:39:19 +11002150 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002151 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2152 !uid_eq(cred->euid, tcred->uid) &&
2153 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002154 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155 ret = -EACCES;
2156 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002157 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002158 } else
2159 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002160
2161 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002162 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002163
2164 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002165 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002166 * trapped in a cpuset, or RT worker may be born in a cgroup
2167 * with no rt_runtime allocated. Just say no.
2168 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002169 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002170 ret = -EINVAL;
2171 rcu_read_unlock();
2172 goto out_unlock_cgroup;
2173 }
2174
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002175 get_task_struct(tsk);
2176 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002177
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002178 threadgroup_lock(tsk);
2179 if (threadgroup) {
2180 if (!thread_group_leader(tsk)) {
2181 /*
2182 * a race with de_thread from another thread's exec()
2183 * may strip us of our leadership, if this happens,
2184 * there is no choice but to throw this task away and
2185 * try again; this is
2186 * "double-double-toil-and-trouble-check locking".
2187 */
2188 threadgroup_unlock(tsk);
2189 put_task_struct(tsk);
2190 goto retry_find_task;
2191 }
Li Zefan081aa452013-03-13 09:17:09 +08002192 }
2193
2194 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2195
Tejun Heocd3d0952011-12-12 18:12:21 -08002196 threadgroup_unlock(tsk);
2197
Paul Menagebbcb81d2007-10-18 23:39:32 -07002198 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002199out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002200 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002201 return ret;
2202}
2203
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002204/**
2205 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2206 * @from: attach to all cgroups of a given task
2207 * @tsk: the task to be attached
2208 */
2209int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2210{
2211 struct cgroupfs_root *root;
2212 int retval = 0;
2213
Tejun Heo47cfcd02013-04-07 09:29:51 -07002214 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002215 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002216 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002217
Li Zefan6f4b7e62013-07-31 16:18:36 +08002218 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002219 if (retval)
2220 break;
2221 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002222 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002223
2224 return retval;
2225}
2226EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2227
Tejun Heo182446d2013-08-08 20:11:24 -04002228static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2229 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002230{
Tejun Heo182446d2013-08-08 20:11:24 -04002231 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002232}
2233
Tejun Heo182446d2013-08-08 20:11:24 -04002234static int cgroup_procs_write(struct cgroup_subsys_state *css,
2235 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002236{
Tejun Heo182446d2013-08-08 20:11:24 -04002237 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002238}
2239
Tejun Heo182446d2013-08-08 20:11:24 -04002240static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2241 struct cftype *cft, const char *buffer)
Paul Menagee788e0662008-07-25 01:46:59 -07002242{
Tejun Heo5f469902014-02-11 11:52:48 -05002243 struct cgroupfs_root *root = css->cgroup->root;
2244
2245 BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
Tejun Heo182446d2013-08-08 20:11:24 -04002246 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e0662008-07-25 01:46:59 -07002247 return -ENODEV;
Tejun Heo69e943b2014-02-08 10:36:58 -05002248 spin_lock(&release_agent_path_lock);
Tejun Heo5f469902014-02-11 11:52:48 -05002249 strlcpy(root->release_agent_path, buffer,
2250 sizeof(root->release_agent_path));
Tejun Heo69e943b2014-02-08 10:36:58 -05002251 spin_unlock(&release_agent_path_lock);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002252 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002253 return 0;
2254}
2255
Tejun Heo2da8ca82013-12-05 12:28:04 -05002256static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e0662008-07-25 01:46:59 -07002257{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002258 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002259
Paul Menagee788e0662008-07-25 01:46:59 -07002260 if (!cgroup_lock_live_group(cgrp))
2261 return -ENODEV;
2262 seq_puts(seq, cgrp->root->release_agent_path);
2263 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002264 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002265 return 0;
2266}
2267
Tejun Heo2da8ca82013-12-05 12:28:04 -05002268static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002269{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002270 struct cgroup *cgrp = seq_css(seq)->cgroup;
2271
2272 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002273 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002274}
2275
Tejun Heoa742c592013-12-05 12:28:03 -05002276static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
Tejun Heo182446d2013-08-08 20:11:24 -04002277 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002278{
Tejun Heo182446d2013-08-08 20:11:24 -04002279 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002280 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002281 struct cgroup_subsys_state *css = cfe->css;
Tejun Heo5f469902014-02-11 11:52:48 -05002282 size_t max_bytes = max(cft->max_write_len, PAGE_SIZE);
Tejun Heoa742c592013-12-05 12:28:03 -05002283 char *buf;
2284 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002285
Tejun Heo5f469902014-02-11 11:52:48 -05002286 if (nbytes > max_bytes)
Tejun Heoa742c592013-12-05 12:28:03 -05002287 return -E2BIG;
2288
2289 buf = kmalloc(nbytes + 1, GFP_KERNEL);
2290 if (!buf)
2291 return -ENOMEM;
2292
2293 if (copy_from_user(buf, userbuf, nbytes)) {
2294 ret = -EFAULT;
2295 goto out_free;
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002296 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002297
Tejun Heoa742c592013-12-05 12:28:03 -05002298 buf[nbytes] = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07002299
Tejun Heoa742c592013-12-05 12:28:03 -05002300 if (cft->write_string) {
2301 ret = cft->write_string(css, cft, strstrip(buf));
2302 } else if (cft->write_u64) {
2303 unsigned long long v;
2304 ret = kstrtoull(buf, 0, &v);
2305 if (!ret)
2306 ret = cft->write_u64(css, cft, v);
2307 } else if (cft->write_s64) {
2308 long long v;
2309 ret = kstrtoll(buf, 0, &v);
2310 if (!ret)
2311 ret = cft->write_s64(css, cft, v);
2312 } else if (cft->trigger) {
2313 ret = cft->trigger(css, (unsigned int)cft->private);
2314 } else {
2315 ret = -EINVAL;
2316 }
2317out_free:
2318 kfree(buf);
2319 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002320}
2321
Paul Menage91796562008-04-29 01:00:01 -07002322/*
2323 * seqfile ops/methods for returning structured data. Currently just
2324 * supports string->u64 maps, but can be extended in future.
2325 */
2326
Tejun Heo6612f052013-12-05 12:28:04 -05002327static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002328{
Tejun Heo6612f052013-12-05 12:28:04 -05002329 struct cftype *cft = seq_cft(seq);
2330
2331 if (cft->seq_start) {
2332 return cft->seq_start(seq, ppos);
2333 } else {
2334 /*
2335 * The same behavior and code as single_open(). Returns
2336 * !NULL if pos is at the beginning; otherwise, NULL.
2337 */
2338 return NULL + !*ppos;
2339 }
2340}
2341
2342static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2343{
2344 struct cftype *cft = seq_cft(seq);
2345
2346 if (cft->seq_next) {
2347 return cft->seq_next(seq, v, ppos);
2348 } else {
2349 /*
2350 * The same behavior and code as single_open(), always
2351 * terminate after the initial read.
2352 */
2353 ++*ppos;
2354 return NULL;
2355 }
2356}
2357
2358static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2359{
2360 struct cftype *cft = seq_cft(seq);
2361
2362 if (cft->seq_stop)
2363 cft->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002364}
2365
2366static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2367{
Tejun Heo7da11272013-12-05 12:28:04 -05002368 struct cftype *cft = seq_cft(m);
2369 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002370
Tejun Heo2da8ca82013-12-05 12:28:04 -05002371 if (cft->seq_show)
2372 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002373
Tejun Heo896f5192013-12-05 12:28:04 -05002374 if (cft->read_u64)
2375 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2376 else if (cft->read_s64)
2377 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2378 else
2379 return -EINVAL;
2380 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002381}
2382
Tejun Heo6612f052013-12-05 12:28:04 -05002383static struct seq_operations cgroup_seq_operations = {
2384 .start = cgroup_seqfile_start,
2385 .next = cgroup_seqfile_next,
2386 .stop = cgroup_seqfile_stop,
2387 .show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002388};
2389
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390static int cgroup_file_open(struct inode *inode, struct file *file)
2391{
Tejun Heof7d58812013-08-08 20:11:23 -04002392 struct cfent *cfe = __d_cfe(file->f_dentry);
2393 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002394 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2395 struct cgroup_subsys_state *css;
Tejun Heo6612f052013-12-05 12:28:04 -05002396 struct cgroup_open_file *of;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002398
2399 err = generic_file_open(inode, file);
2400 if (err)
2401 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002402
2403 /*
2404 * If the file belongs to a subsystem, pin the css. Will be
2405 * unpinned either on open failure or release. This ensures that
2406 * @css stays alive for all file operations.
2407 */
Tejun Heo105347b2013-08-13 11:01:55 -04002408 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002409 css = cgroup_css(cgrp, cft->ss);
2410 if (cft->ss && !css_tryget(css))
2411 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002412 rcu_read_unlock();
2413
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002414 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002415 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002416
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002417 /*
2418 * @cfe->css is used by read/write/close to determine the
2419 * associated css. @file->private_data would be a better place but
2420 * that's already used by seqfile. Multiple accessors may use it
2421 * simultaneously which is okay as the association never changes.
2422 */
2423 WARN_ON_ONCE(cfe->css && cfe->css != css);
2424 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425
Tejun Heo6612f052013-12-05 12:28:04 -05002426 of = __seq_open_private(file, &cgroup_seq_operations,
2427 sizeof(struct cgroup_open_file));
2428 if (of) {
2429 of->cfe = cfe;
2430 return 0;
Li Zefane0798ce2013-07-31 17:36:25 +08002431 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432
Tejun Heo6612f052013-12-05 12:28:04 -05002433 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002434 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002435 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002436}
2437
2438static int cgroup_file_release(struct inode *inode, struct file *file)
2439{
Tejun Heof7d58812013-08-08 20:11:23 -04002440 struct cfent *cfe = __d_cfe(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002441 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002442
Tejun Heo67f4c362013-08-08 20:11:24 -04002443 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002444 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002445 return seq_release_private(inode, file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002446}
2447
2448/*
2449 * cgroup_rename - Only allow simple rename of directories in place.
2450 */
2451static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2452 struct inode *new_dir, struct dentry *new_dentry)
2453{
Li Zefan65dff752013-03-01 15:01:56 +08002454 int ret;
2455 struct cgroup_name *name, *old_name;
2456 struct cgroup *cgrp;
2457
2458 /*
2459 * It's convinient to use parent dir's i_mutex to protected
2460 * cgrp->name.
2461 */
2462 lockdep_assert_held(&old_dir->i_mutex);
2463
Paul Menageddbcc7e2007-10-18 23:39:30 -07002464 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2465 return -ENOTDIR;
2466 if (new_dentry->d_inode)
2467 return -EEXIST;
2468 if (old_dir != new_dir)
2469 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002470
2471 cgrp = __d_cgrp(old_dentry);
2472
Tejun Heo6db8e852013-06-14 11:18:22 -07002473 /*
2474 * This isn't a proper migration and its usefulness is very
2475 * limited. Disallow if sane_behavior.
2476 */
2477 if (cgroup_sane_behavior(cgrp))
2478 return -EPERM;
2479
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002480 name = cgroup_alloc_name(new_dentry->d_name.name);
Li Zefan65dff752013-03-01 15:01:56 +08002481 if (!name)
2482 return -ENOMEM;
2483
2484 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2485 if (ret) {
2486 kfree(name);
2487 return ret;
2488 }
2489
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002490 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002491 rcu_assign_pointer(cgrp->name, name);
2492
2493 kfree_rcu(old_name, rcu_head);
2494 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002495}
2496
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002497static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2498{
2499 if (S_ISDIR(dentry->d_inode->i_mode))
2500 return &__d_cgrp(dentry)->xattrs;
2501 else
Li Zefan712317a2013-04-18 23:09:52 -07002502 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002503}
2504
2505static inline int xattr_enabled(struct dentry *dentry)
2506{
2507 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002508 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002509}
2510
2511static bool is_valid_xattr(const char *name)
2512{
2513 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2514 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2515 return true;
2516 return false;
2517}
2518
2519static int cgroup_setxattr(struct dentry *dentry, const char *name,
2520 const void *val, size_t size, int flags)
2521{
2522 if (!xattr_enabled(dentry))
2523 return -EOPNOTSUPP;
2524 if (!is_valid_xattr(name))
2525 return -EINVAL;
2526 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2527}
2528
2529static int cgroup_removexattr(struct dentry *dentry, const char *name)
2530{
2531 if (!xattr_enabled(dentry))
2532 return -EOPNOTSUPP;
2533 if (!is_valid_xattr(name))
2534 return -EINVAL;
2535 return simple_xattr_remove(__d_xattrs(dentry), name);
2536}
2537
2538static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2539 void *buf, size_t size)
2540{
2541 if (!xattr_enabled(dentry))
2542 return -EOPNOTSUPP;
2543 if (!is_valid_xattr(name))
2544 return -EINVAL;
2545 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2546}
2547
2548static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2549{
2550 if (!xattr_enabled(dentry))
2551 return -EOPNOTSUPP;
2552 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2553}
2554
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002555static const struct file_operations cgroup_file_operations = {
Tejun Heo896f5192013-12-05 12:28:04 -05002556 .read = seq_read,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002557 .write = cgroup_file_write,
2558 .llseek = generic_file_llseek,
2559 .open = cgroup_file_open,
2560 .release = cgroup_file_release,
2561};
2562
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002563static const struct inode_operations cgroup_file_inode_operations = {
2564 .setxattr = cgroup_setxattr,
2565 .getxattr = cgroup_getxattr,
2566 .listxattr = cgroup_listxattr,
2567 .removexattr = cgroup_removexattr,
2568};
2569
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002570static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002571 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002572 .mkdir = cgroup_mkdir,
2573 .rmdir = cgroup_rmdir,
2574 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002575 .setxattr = cgroup_setxattr,
2576 .getxattr = cgroup_getxattr,
2577 .listxattr = cgroup_listxattr,
2578 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002579};
2580
Al Viroa5e7ed32011-07-26 01:55:55 -04002581static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002582 struct super_block *sb)
2583{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002584 struct inode *inode;
2585
2586 if (!dentry)
2587 return -ENOENT;
2588 if (dentry->d_inode)
2589 return -EEXIST;
2590
2591 inode = cgroup_new_inode(mode, sb);
2592 if (!inode)
2593 return -ENOMEM;
2594
2595 if (S_ISDIR(mode)) {
2596 inode->i_op = &cgroup_dir_inode_operations;
2597 inode->i_fop = &simple_dir_operations;
2598
2599 /* start off with i_nlink == 2 (for "." entry) */
2600 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002601 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002602
Tejun Heob8a2df6a2012-11-19 08:13:37 -08002603 /*
2604 * Control reaches here with cgroup_mutex held.
2605 * @inode->i_mutex should nest outside cgroup_mutex but we
2606 * want to populate it immediately without releasing
2607 * cgroup_mutex. As @inode isn't visible to anyone else
2608 * yet, trylock will always succeed without affecting
2609 * lockdep checks.
2610 */
2611 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002612 } else if (S_ISREG(mode)) {
2613 inode->i_size = 0;
2614 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002615 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002616 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002617 d_instantiate(dentry, inode);
2618 dget(dentry); /* Extra count - pin the dentry in core */
2619 return 0;
2620}
2621
Li Zefan099fca32009-04-02 16:57:29 -07002622/**
2623 * cgroup_file_mode - deduce file mode of a control file
2624 * @cft: the control file in question
2625 *
2626 * returns cft->mode if ->mode is not 0
2627 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2628 * returns S_IRUGO if it has only a read handler
2629 * returns S_IWUSR if it has only a write hander
2630 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002631static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002632{
Al Viroa5e7ed32011-07-26 01:55:55 -04002633 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002634
2635 if (cft->mode)
2636 return cft->mode;
2637
Tejun Heo2da8ca82013-12-05 12:28:04 -05002638 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
Li Zefan099fca32009-04-02 16:57:29 -07002639 mode |= S_IRUGO;
2640
Tejun Heo6e0755b2013-12-05 12:28:03 -05002641 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
2642 cft->trigger)
Li Zefan099fca32009-04-02 16:57:29 -07002643 mode |= S_IWUSR;
2644
2645 return mode;
2646}
2647
Tejun Heo2bb566c2013-08-08 20:11:23 -04002648static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002649{
Paul Menagebd89aab2007-10-18 23:40:44 -07002650 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002651 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002652 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002653 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002654 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002655 umode_t mode;
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002656 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo05ef1d72012-04-01 12:09:56 -07002657
Paul Menageddbcc7e2007-10-18 23:39:30 -07002658 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002659
2660 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2661 if (!cfe)
2662 return -ENOMEM;
2663
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05002664 cgroup_file_name(cgrp, cft, name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002665 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002666 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002667 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002668 goto out;
2669 }
2670
Li Zefand6cbf352013-05-14 19:44:20 +08002671 cfe->type = (void *)cft;
2672 cfe->dentry = dentry;
2673 dentry->d_fsdata = cfe;
2674 simple_xattrs_init(&cfe->xattrs);
2675
Tejun Heo05ef1d72012-04-01 12:09:56 -07002676 mode = cgroup_file_mode(cft);
2677 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2678 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002679 list_add_tail(&cfe->node, &parent->files);
2680 cfe = NULL;
2681 }
2682 dput(dentry);
2683out:
2684 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002685 return error;
2686}
2687
Tejun Heob1f28d32013-06-28 16:24:10 -07002688/**
2689 * cgroup_addrm_files - add or remove files to a cgroup directory
2690 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002691 * @cfts: array of cftypes to be added
2692 * @is_add: whether to add or remove
2693 *
2694 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002695 * For removals, this function never fails. If addition fails, this
2696 * function doesn't remove files already added. The caller is responsible
2697 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002698 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002699static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2700 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002701{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002702 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002703 int ret;
2704
2705 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05002706 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002707
2708 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002709 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002710 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2711 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002712 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2713 continue;
2714 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2715 continue;
2716
Li Zefan2739d3c2013-01-21 18:18:33 +08002717 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002718 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002719 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002720 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002721 cft->name, ret);
2722 return ret;
2723 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002724 } else {
2725 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002726 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002728 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002729}
2730
Tejun Heo8e3f6542012-04-01 12:09:55 -07002731static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002732 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002733{
2734 /*
2735 * Thanks to the entanglement with vfs inode locking, we can't walk
2736 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002737 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2738 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002739 */
Tejun Heoace2bee2014-02-11 11:52:47 -05002740 mutex_lock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002741 mutex_lock(&cgroup_mutex);
2742}
2743
Tejun Heo2bb566c2013-08-08 20:11:23 -04002744static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002745 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002746{
2747 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002748 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002749 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002750 struct super_block *sb = ss->root->sb;
Tejun Heo59f52962014-02-11 11:52:49 -05002751 struct cgroup *prev = NULL;
Li Zefane8c82d22013-06-18 18:48:37 +08002752 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002753 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002754 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002755 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002756
Tejun Heo4ac06012014-02-11 11:52:47 -05002757 mutex_unlock(&cgroup_mutex);
2758
Tejun Heo8e3f6542012-04-01 12:09:55 -07002759 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002760 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002761 !atomic_inc_not_zero(&sb->s_active)) {
Tejun Heoace2bee2014-02-11 11:52:47 -05002762 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002763 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002764 }
2765
Li Zefane8c82d22013-06-18 18:48:37 +08002766 /*
2767 * All cgroups which are created after we drop cgroup_mutex will
2768 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002769 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002770 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002771 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002772
Li Zefane8c82d22013-06-18 18:48:37 +08002773 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002774 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002775 struct cgroup *cgrp = css->cgroup;
2776
Li Zefane8c82d22013-06-18 18:48:37 +08002777 if (cgroup_is_dead(cgrp))
2778 continue;
2779
2780 inode = cgrp->dentry->d_inode;
Tejun Heo59f52962014-02-11 11:52:49 -05002781 cgroup_get(cgrp);
2782 if (prev)
2783 cgroup_put(prev);
2784 prev = cgrp;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002785
Tejun Heoace2bee2014-02-11 11:52:47 -05002786 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002787 mutex_lock(&inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05002788 mutex_lock(&cgroup_tree_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002789 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002790 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002791 mutex_unlock(&inode->i_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002792 if (ret)
2793 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002794 }
Tejun Heoace2bee2014-02-11 11:52:47 -05002795 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo59f52962014-02-11 11:52:49 -05002796 cgroup_put(prev);
2797 cgroup_put_root(ss->root);
Tejun Heo9ccece82013-06-28 16:24:11 -07002798 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002799}
2800
Tejun Heo2da440a2014-02-11 11:52:48 -05002801static void cgroup_exit_cftypes(struct cftype *cfts)
2802{
2803 struct cftype *cft;
2804
2805 for (cft = cfts; cft->name[0] != '\0'; cft++)
2806 cft->ss = NULL;
2807}
2808
2809static void cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2810{
2811 struct cftype *cft;
2812
2813 for (cft = cfts; cft->name[0] != '\0'; cft++)
2814 cft->ss = ss;
2815}
2816
Tejun Heo8e3f6542012-04-01 12:09:55 -07002817/**
2818 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2819 * @ss: target cgroup subsystem
2820 * @cfts: zero-length name terminated array of cftypes
2821 *
2822 * Register @cfts to @ss. Files described by @cfts are created for all
2823 * existing cgroups to which @ss is attached and all future cgroups will
2824 * have them too. This function can be called anytime whether @ss is
2825 * attached or not.
2826 *
2827 * Returns 0 on successful registration, -errno on failure. Note that this
2828 * function currently returns 0 as long as @cfts registration is successful
2829 * even if some file creation attempts on existing cgroups fail.
2830 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002831int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002832{
2833 struct cftype_set *set;
Tejun Heo9ccece82013-06-28 16:24:11 -07002834 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002835
2836 set = kzalloc(sizeof(*set), GFP_KERNEL);
2837 if (!set)
2838 return -ENOMEM;
2839
Tejun Heo2da440a2014-02-11 11:52:48 -05002840 cgroup_init_cftypes(ss, cfts);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002841
Tejun Heo8e3f6542012-04-01 12:09:55 -07002842 cgroup_cfts_prepare();
2843 set->cfts = cfts;
2844 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002845 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002846 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002847 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002848 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002849}
2850EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2851
Li Zefana043e3b2008-02-23 15:24:09 -08002852/**
Tejun Heo79578622012-04-01 12:09:56 -07002853 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002854 * @cfts: zero-length name terminated array of cftypes
2855 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002856 * Unregister @cfts. Files described by @cfts are removed from all
2857 * existing cgroups and all future cgroups won't have them either. This
2858 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002859 *
2860 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002861 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002862 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002863int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002864{
Tejun Heo2da440a2014-02-11 11:52:48 -05002865 struct cftype *found = NULL;
Tejun Heo79578622012-04-01 12:09:56 -07002866 struct cftype_set *set;
2867
Tejun Heo2bb566c2013-08-08 20:11:23 -04002868 if (!cfts || !cfts[0].ss)
2869 return -ENOENT;
2870
Tejun Heo79578622012-04-01 12:09:56 -07002871 cgroup_cfts_prepare();
2872
Tejun Heo2bb566c2013-08-08 20:11:23 -04002873 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002874 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002875 list_del(&set->node);
2876 kfree(set);
Tejun Heo2da440a2014-02-11 11:52:48 -05002877 found = cfts;
2878 break;
Tejun Heo79578622012-04-01 12:09:56 -07002879 }
2880 }
2881
Tejun Heo2da440a2014-02-11 11:52:48 -05002882 cgroup_cfts_commit(found, false);
2883 cgroup_exit_cftypes(cfts);
2884 return found ? 0 : -ENOENT;
Tejun Heo79578622012-04-01 12:09:56 -07002885}
2886
2887/**
Li Zefana043e3b2008-02-23 15:24:09 -08002888 * cgroup_task_count - count the number of tasks in a cgroup.
2889 * @cgrp: the cgroup in question
2890 *
2891 * Return the number of tasks in the cgroup.
2892 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002893int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002894{
2895 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002896 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002897
Paul Menage817929e2007-10-18 23:39:36 -07002898 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002899 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2900 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002901 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002902 return count;
2903}
2904
2905/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002906 * To reduce the fork() overhead for systems that are not actually using
2907 * their cgroups capability, we don't maintain the lists running through
2908 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002909 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002910 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002911static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002912{
2913 struct task_struct *p, *g;
2914 write_lock(&css_set_lock);
2915 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002916 /*
2917 * We need tasklist_lock because RCU is not safe against
2918 * while_each_thread(). Besides, a forking task that has passed
2919 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2920 * is not guaranteed to have its child immediately visible in the
2921 * tasklist if we walk through it with RCU.
2922 */
2923 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002924 do_each_thread(g, p) {
2925 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002926 /*
2927 * We should check if the process is exiting, otherwise
2928 * it will race with cgroup_exit() in that the list
2929 * entry won't be deleted though the process has exited.
2930 */
2931 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002932 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002933 task_unlock(p);
2934 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002935 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002936 write_unlock(&css_set_lock);
2937}
2938
Tejun Heo574bd9f2012-11-09 09:12:29 -08002939/**
Tejun Heo492eb212013-08-08 20:11:25 -04002940 * css_next_child - find the next child of a given css
2941 * @pos_css: the current position (%NULL to initiate traversal)
2942 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002943 *
Tejun Heo492eb212013-08-08 20:11:25 -04002944 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002945 * under either cgroup_mutex or RCU read lock. The only requirement is
2946 * that @parent_css and @pos_css are accessible. The next sibling is
2947 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002948 */
Tejun Heo492eb212013-08-08 20:11:25 -04002949struct cgroup_subsys_state *
2950css_next_child(struct cgroup_subsys_state *pos_css,
2951 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002952{
Tejun Heo492eb212013-08-08 20:11:25 -04002953 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2954 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002955 struct cgroup *next;
2956
Tejun Heoace2bee2014-02-11 11:52:47 -05002957 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002958
2959 /*
2960 * @pos could already have been removed. Once a cgroup is removed,
2961 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002962 * changes. As CGRP_DEAD assertion is serialized and happens
2963 * before the cgroup is taken off the ->sibling list, if we see it
2964 * unasserted, it's guaranteed that the next sibling hasn't
2965 * finished its grace period even if it's already removed, and thus
2966 * safe to dereference from this RCU critical section. If
2967 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2968 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002969 *
2970 * If @pos is dead, its next pointer can't be dereferenced;
2971 * however, as each cgroup is given a monotonically increasing
2972 * unique serial number and always appended to the sibling list,
2973 * the next one can be found by walking the parent's children until
2974 * we see a cgroup with higher serial number than @pos's. While
2975 * this path can be slower, it's taken only when either the current
2976 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002977 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002978 if (!pos) {
2979 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2980 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002981 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002982 } else {
2983 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2984 if (next->serial_nr > pos->serial_nr)
2985 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002986 }
2987
Tejun Heo492eb212013-08-08 20:11:25 -04002988 if (&next->sibling == &cgrp->children)
2989 return NULL;
2990
Tejun Heoca8bdca2013-08-26 18:40:56 -04002991 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002992}
Tejun Heo492eb212013-08-08 20:11:25 -04002993EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002994
2995/**
Tejun Heo492eb212013-08-08 20:11:25 -04002996 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002997 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002998 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002999 *
Tejun Heo492eb212013-08-08 20:11:25 -04003000 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003001 * to visit for pre-order traversal of @root's descendants. @root is
3002 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003003 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003004 * While this function requires cgroup_mutex or RCU read locking, it
3005 * doesn't require the whole traversal to be contained in a single critical
3006 * section. This function will return the correct next descendant as long
3007 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003008 */
Tejun Heo492eb212013-08-08 20:11:25 -04003009struct cgroup_subsys_state *
3010css_next_descendant_pre(struct cgroup_subsys_state *pos,
3011 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003012{
Tejun Heo492eb212013-08-08 20:11:25 -04003013 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003014
Tejun Heoace2bee2014-02-11 11:52:47 -05003015 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003016
Tejun Heobd8815a2013-08-08 20:11:27 -04003017 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003018 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003019 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003020
3021 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003022 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003023 if (next)
3024 return next;
3025
3026 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003027 while (pos != root) {
3028 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003029 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003030 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003031 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003032 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003033
3034 return NULL;
3035}
Tejun Heo492eb212013-08-08 20:11:25 -04003036EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003037
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003038/**
Tejun Heo492eb212013-08-08 20:11:25 -04003039 * css_rightmost_descendant - return the rightmost descendant of a css
3040 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003041 *
Tejun Heo492eb212013-08-08 20:11:25 -04003042 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3043 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003044 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003045 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003046 * While this function requires cgroup_mutex or RCU read locking, it
3047 * doesn't require the whole traversal to be contained in a single critical
3048 * section. This function will return the correct rightmost descendant as
3049 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003050 */
Tejun Heo492eb212013-08-08 20:11:25 -04003051struct cgroup_subsys_state *
3052css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003053{
Tejun Heo492eb212013-08-08 20:11:25 -04003054 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003055
Tejun Heoace2bee2014-02-11 11:52:47 -05003056 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003057
3058 do {
3059 last = pos;
3060 /* ->prev isn't RCU safe, walk ->next till the end */
3061 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003062 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003063 pos = tmp;
3064 } while (pos);
3065
3066 return last;
3067}
Tejun Heo492eb212013-08-08 20:11:25 -04003068EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003069
Tejun Heo492eb212013-08-08 20:11:25 -04003070static struct cgroup_subsys_state *
3071css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003072{
Tejun Heo492eb212013-08-08 20:11:25 -04003073 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003074
3075 do {
3076 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003077 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003078 } while (pos);
3079
3080 return last;
3081}
3082
3083/**
Tejun Heo492eb212013-08-08 20:11:25 -04003084 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003085 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003086 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003087 *
Tejun Heo492eb212013-08-08 20:11:25 -04003088 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003089 * to visit for post-order traversal of @root's descendants. @root is
3090 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003091 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003092 * While this function requires cgroup_mutex or RCU read locking, it
3093 * doesn't require the whole traversal to be contained in a single critical
3094 * section. This function will return the correct next descendant as long
3095 * as both @pos and @cgroup are accessible and @pos is a descendant of
3096 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097 */
Tejun Heo492eb212013-08-08 20:11:25 -04003098struct cgroup_subsys_state *
3099css_next_descendant_post(struct cgroup_subsys_state *pos,
3100 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003101{
Tejun Heo492eb212013-08-08 20:11:25 -04003102 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003103
Tejun Heoace2bee2014-02-11 11:52:47 -05003104 cgroup_assert_mutexes_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003105
Tejun Heo58b79a92013-09-06 15:31:08 -04003106 /* if first iteration, visit leftmost descendant which may be @root */
3107 if (!pos)
3108 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003109
Tejun Heobd8815a2013-08-08 20:11:27 -04003110 /* if we visited @root, we're done */
3111 if (pos == root)
3112 return NULL;
3113
Tejun Heo574bd9f2012-11-09 09:12:29 -08003114 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003115 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003116 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003117 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003118
3119 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003120 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003121}
Tejun Heo492eb212013-08-08 20:11:25 -04003122EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003123
Tejun Heo0942eee2013-08-08 20:11:26 -04003124/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003125 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003126 * @it: the iterator to advance
3127 *
3128 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003129 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003130static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003131{
3132 struct list_head *l = it->cset_link;
3133 struct cgrp_cset_link *link;
3134 struct css_set *cset;
3135
3136 /* Advance to the next non-empty css_set */
3137 do {
3138 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003139 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003140 it->cset_link = NULL;
3141 return;
3142 }
3143 link = list_entry(l, struct cgrp_cset_link, cset_link);
3144 cset = link->cset;
3145 } while (list_empty(&cset->tasks));
3146 it->cset_link = l;
3147 it->task = cset->tasks.next;
3148}
3149
Tejun Heo0942eee2013-08-08 20:11:26 -04003150/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003151 * css_task_iter_start - initiate task iteration
3152 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003153 * @it: the task iterator to use
3154 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003155 * Initiate iteration through the tasks of @css. The caller can call
3156 * css_task_iter_next() to walk through the tasks until the function
3157 * returns NULL. On completion of iteration, css_task_iter_end() must be
3158 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003159 *
3160 * Note that this function acquires a lock which is released when the
3161 * iteration finishes. The caller can't sleep while iteration is in
3162 * progress.
3163 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003164void css_task_iter_start(struct cgroup_subsys_state *css,
3165 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003166 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003167{
3168 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003169 * The first time anyone tries to iterate across a css, we need to
3170 * enable the list linking each css_set to its tasks, and fix up
3171 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003172 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003173 if (!use_task_css_set_links)
3174 cgroup_enable_task_cg_lists();
3175
Paul Menage817929e2007-10-18 23:39:36 -07003176 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003177
Tejun Heo72ec7022013-08-08 20:11:26 -04003178 it->origin_css = css;
3179 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003180
Tejun Heo72ec7022013-08-08 20:11:26 -04003181 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003182}
3183
Tejun Heo0942eee2013-08-08 20:11:26 -04003184/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003185 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003186 * @it: the task iterator being iterated
3187 *
3188 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003189 * initialized via css_task_iter_start(). Returns NULL when the iteration
3190 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003191 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003192struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003193{
3194 struct task_struct *res;
3195 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003196 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003197
3198 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003199 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003200 return NULL;
3201 res = list_entry(l, struct task_struct, cg_list);
3202 /* Advance iterator to find next entry */
3203 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003204 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3205 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003206 /*
3207 * We reached the end of this task list - move on to the
3208 * next cgrp_cset_link.
3209 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003210 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003211 } else {
3212 it->task = l;
3213 }
3214 return res;
3215}
3216
Tejun Heo0942eee2013-08-08 20:11:26 -04003217/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003218 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003219 * @it: the task iterator to finish
3220 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003221 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003222 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003223void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003224 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003225{
3226 read_unlock(&css_set_lock);
3227}
3228
Cliff Wickman31a7df02008-02-07 00:14:42 -08003229static inline int started_after_time(struct task_struct *t1,
3230 struct timespec *time,
3231 struct task_struct *t2)
3232{
3233 int start_diff = timespec_compare(&t1->start_time, time);
3234 if (start_diff > 0) {
3235 return 1;
3236 } else if (start_diff < 0) {
3237 return 0;
3238 } else {
3239 /*
3240 * Arbitrarily, if two processes started at the same
3241 * time, we'll say that the lower pointer value
3242 * started first. Note that t2 may have exited by now
3243 * so this may not be a valid pointer any longer, but
3244 * that's fine - it still serves to distinguish
3245 * between two tasks started (effectively) simultaneously.
3246 */
3247 return t1 > t2;
3248 }
3249}
3250
3251/*
3252 * This function is a callback from heap_insert() and is used to order
3253 * the heap.
3254 * In this case we order the heap in descending task start time.
3255 */
3256static inline int started_after(void *p1, void *p2)
3257{
3258 struct task_struct *t1 = p1;
3259 struct task_struct *t2 = p2;
3260 return started_after_time(t1, &t2->start_time, t2);
3261}
3262
3263/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003264 * css_scan_tasks - iterate though all the tasks in a css
3265 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003266 * @test: optional test callback
3267 * @process: process callback
3268 * @data: data passed to @test and @process
3269 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003270 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003271 * Iterate through all the tasks in @css, calling @test for each, and if it
3272 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003273 *
Tejun Heoe5358372013-08-08 20:11:26 -04003274 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003275 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003276 * lock css_set_lock for the call to @process.
3277 *
3278 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003279 * of @css for the duration of this call. This function may or may not
3280 * call @process for tasks that exit or move to a different css during the
3281 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003282 *
3283 * Note that @test may be called with locks held, and may in some
3284 * situations be called multiple times for the same task, so it should be
3285 * cheap.
3286 *
3287 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3288 * heap operations (and its "gt" member will be overwritten), else a
3289 * temporary heap will be used (allocation of which may cause this function
3290 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003291 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003292int css_scan_tasks(struct cgroup_subsys_state *css,
3293 bool (*test)(struct task_struct *, void *),
3294 void (*process)(struct task_struct *, void *),
3295 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003296{
3297 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003298 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003299 struct task_struct *p, *dropped;
3300 /* Never dereference latest_task, since it's not refcounted */
3301 struct task_struct *latest_task = NULL;
3302 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003303 struct timespec latest_time = { 0, 0 };
3304
Tejun Heoe5358372013-08-08 20:11:26 -04003305 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003306 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003307 heap->gt = &started_after;
3308 } else {
3309 /* We need to allocate our own heap memory */
3310 heap = &tmp_heap;
3311 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3312 if (retval)
3313 /* cannot allocate the heap */
3314 return retval;
3315 }
3316
3317 again:
3318 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003319 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003320 * which are of interest, and invoking @process callback on the
3321 * ones which need an update. Since we don't want to hold any
3322 * locks during the task updates, gather tasks to be processed in a
3323 * heap structure. The heap is sorted by descending task start
3324 * time. If the statically-sized heap fills up, we overflow tasks
3325 * that started later, and in future iterations only consider tasks
3326 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003327 * guarantees forward progress and that we don't miss any tasks.
3328 */
3329 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003330 css_task_iter_start(css, &it);
3331 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003332 /*
3333 * Only affect tasks that qualify per the caller's callback,
3334 * if he provided one
3335 */
Tejun Heoe5358372013-08-08 20:11:26 -04003336 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003337 continue;
3338 /*
3339 * Only process tasks that started after the last task
3340 * we processed
3341 */
3342 if (!started_after_time(p, &latest_time, latest_task))
3343 continue;
3344 dropped = heap_insert(heap, p);
3345 if (dropped == NULL) {
3346 /*
3347 * The new task was inserted; the heap wasn't
3348 * previously full
3349 */
3350 get_task_struct(p);
3351 } else if (dropped != p) {
3352 /*
3353 * The new task was inserted, and pushed out a
3354 * different task
3355 */
3356 get_task_struct(p);
3357 put_task_struct(dropped);
3358 }
3359 /*
3360 * Else the new task was newer than anything already in
3361 * the heap and wasn't inserted
3362 */
3363 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003364 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003365
3366 if (heap->size) {
3367 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003368 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003369 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003370 latest_time = q->start_time;
3371 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003372 }
3373 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003374 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003375 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003376 }
3377 /*
3378 * If we had to process any tasks at all, scan again
3379 * in case some of them were in the middle of forking
3380 * children that didn't get processed.
3381 * Not the most efficient way to do it, but it avoids
3382 * having to take callback_mutex in the fork path
3383 */
3384 goto again;
3385 }
3386 if (heap == &tmp_heap)
3387 heap_free(&tmp_heap);
3388 return 0;
3389}
3390
Tejun Heoe5358372013-08-08 20:11:26 -04003391static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003392{
Tejun Heoe5358372013-08-08 20:11:26 -04003393 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003394
Tejun Heo47cfcd02013-04-07 09:29:51 -07003395 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003396 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003397 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003398}
3399
3400/**
3401 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3402 * @to: cgroup to which the tasks will be moved
3403 * @from: cgroup in which the tasks currently reside
3404 */
3405int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3406{
Tejun Heo72ec7022013-08-08 20:11:26 -04003407 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3408 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003409}
3410
Paul Menage817929e2007-10-18 23:39:36 -07003411/*
Ben Blum102a7752009-09-23 15:56:26 -07003412 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003413 *
3414 * Reading this file can return large amounts of data if a cgroup has
3415 * *lots* of attached tasks. So it may need several calls to read(),
3416 * but we cannot guarantee that the information we produce is correct
3417 * unless we produce it entirely atomically.
3418 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003419 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003420
Li Zefan24528252012-01-20 11:58:43 +08003421/* which pidlist file are we talking about? */
3422enum cgroup_filetype {
3423 CGROUP_FILE_PROCS,
3424 CGROUP_FILE_TASKS,
3425};
3426
3427/*
3428 * A pidlist is a list of pids that virtually represents the contents of one
3429 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3430 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3431 * to the cgroup.
3432 */
3433struct cgroup_pidlist {
3434 /*
3435 * used to find which pidlist is wanted. doesn't change as long as
3436 * this particular list stays in the list.
3437 */
3438 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3439 /* array of xids */
3440 pid_t *list;
3441 /* how many elements the above list has */
3442 int length;
Li Zefan24528252012-01-20 11:58:43 +08003443 /* each of these stored in a list by its cgroup */
3444 struct list_head links;
3445 /* pointer to the cgroup we belong to, for list removal purposes */
3446 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003447 /* for delayed destruction */
3448 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003449};
3450
Paul Menagebbcb81d2007-10-18 23:39:32 -07003451/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003452 * The following two functions "fix" the issue where there are more pids
3453 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3454 * TODO: replace with a kernel-wide solution to this problem
3455 */
3456#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3457static void *pidlist_allocate(int count)
3458{
3459 if (PIDLIST_TOO_LARGE(count))
3460 return vmalloc(count * sizeof(pid_t));
3461 else
3462 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3463}
Tejun Heob1a21362013-11-29 10:42:58 -05003464
Ben Blumd1d9fd32009-09-23 15:56:28 -07003465static void pidlist_free(void *p)
3466{
3467 if (is_vmalloc_addr(p))
3468 vfree(p);
3469 else
3470 kfree(p);
3471}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003472
3473/*
Tejun Heob1a21362013-11-29 10:42:58 -05003474 * Used to destroy all pidlists lingering waiting for destroy timer. None
3475 * should be left afterwards.
3476 */
3477static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3478{
3479 struct cgroup_pidlist *l, *tmp_l;
3480
3481 mutex_lock(&cgrp->pidlist_mutex);
3482 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3483 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3484 mutex_unlock(&cgrp->pidlist_mutex);
3485
3486 flush_workqueue(cgroup_pidlist_destroy_wq);
3487 BUG_ON(!list_empty(&cgrp->pidlists));
3488}
3489
3490static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3491{
3492 struct delayed_work *dwork = to_delayed_work(work);
3493 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3494 destroy_dwork);
3495 struct cgroup_pidlist *tofree = NULL;
3496
3497 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003498
3499 /*
Tejun Heo04502362013-11-29 10:42:59 -05003500 * Destroy iff we didn't get queued again. The state won't change
3501 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003502 */
Tejun Heo04502362013-11-29 10:42:59 -05003503 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003504 list_del(&l->links);
3505 pidlist_free(l->list);
3506 put_pid_ns(l->key.ns);
3507 tofree = l;
3508 }
3509
Tejun Heob1a21362013-11-29 10:42:58 -05003510 mutex_unlock(&l->owner->pidlist_mutex);
3511 kfree(tofree);
3512}
3513
3514/*
Ben Blum102a7752009-09-23 15:56:26 -07003515 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003516 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003517 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003518static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003519{
Ben Blum102a7752009-09-23 15:56:26 -07003520 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003521
3522 /*
3523 * we presume the 0th element is unique, so i starts at 1. trivial
3524 * edge cases first; no work needs to be done for either
3525 */
3526 if (length == 0 || length == 1)
3527 return length;
3528 /* src and dest walk down the list; dest counts unique elements */
3529 for (src = 1; src < length; src++) {
3530 /* find next unique element */
3531 while (list[src] == list[src-1]) {
3532 src++;
3533 if (src == length)
3534 goto after;
3535 }
3536 /* dest always points to where the next unique element goes */
3537 list[dest] = list[src];
3538 dest++;
3539 }
3540after:
Ben Blum102a7752009-09-23 15:56:26 -07003541 return dest;
3542}
3543
Tejun Heoafb2bc12013-11-29 10:42:59 -05003544/*
3545 * The two pid files - task and cgroup.procs - guaranteed that the result
3546 * is sorted, which forced this whole pidlist fiasco. As pid order is
3547 * different per namespace, each namespace needs differently sorted list,
3548 * making it impossible to use, for example, single rbtree of member tasks
3549 * sorted by task pointer. As pidlists can be fairly large, allocating one
3550 * per open file is dangerous, so cgroup had to implement shared pool of
3551 * pidlists keyed by cgroup and namespace.
3552 *
3553 * All this extra complexity was caused by the original implementation
3554 * committing to an entirely unnecessary property. In the long term, we
3555 * want to do away with it. Explicitly scramble sort order if
3556 * sane_behavior so that no such expectation exists in the new interface.
3557 *
3558 * Scrambling is done by swapping every two consecutive bits, which is
3559 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3560 */
3561static pid_t pid_fry(pid_t pid)
3562{
3563 unsigned a = pid & 0x55555555;
3564 unsigned b = pid & 0xAAAAAAAA;
3565
3566 return (a << 1) | (b >> 1);
3567}
3568
3569static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3570{
3571 if (cgroup_sane_behavior(cgrp))
3572 return pid_fry(pid);
3573 else
3574 return pid;
3575}
3576
Ben Blum102a7752009-09-23 15:56:26 -07003577static int cmppid(const void *a, const void *b)
3578{
3579 return *(pid_t *)a - *(pid_t *)b;
3580}
3581
Tejun Heoafb2bc12013-11-29 10:42:59 -05003582static int fried_cmppid(const void *a, const void *b)
3583{
3584 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3585}
3586
Ben Blum72a8cb32009-09-23 15:56:27 -07003587static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3588 enum cgroup_filetype type)
3589{
3590 struct cgroup_pidlist *l;
3591 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003592 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003593
Tejun Heoe6b81712013-11-29 10:42:59 -05003594 lockdep_assert_held(&cgrp->pidlist_mutex);
3595
3596 list_for_each_entry(l, &cgrp->pidlists, links)
3597 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003598 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003599 return NULL;
3600}
3601
Ben Blum72a8cb32009-09-23 15:56:27 -07003602/*
3603 * find the appropriate pidlist for our purpose (given procs vs tasks)
3604 * returns with the lock on that pidlist already held, and takes care
3605 * of the use count, or returns NULL with no locks held if we're out of
3606 * memory.
3607 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003608static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3609 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003610{
3611 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003612
Tejun Heoe6b81712013-11-29 10:42:59 -05003613 lockdep_assert_held(&cgrp->pidlist_mutex);
3614
3615 l = cgroup_pidlist_find(cgrp, type);
3616 if (l)
3617 return l;
3618
Ben Blum72a8cb32009-09-23 15:56:27 -07003619 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003620 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003621 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003622 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003623
Tejun Heob1a21362013-11-29 10:42:58 -05003624 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003625 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003626 /* don't need task_nsproxy() if we're looking at ourself */
3627 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003628 l->owner = cgrp;
3629 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003630 return l;
3631}
3632
3633/*
Ben Blum102a7752009-09-23 15:56:26 -07003634 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3635 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003636static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3637 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003638{
3639 pid_t *array;
3640 int length;
3641 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003642 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003643 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003644 struct cgroup_pidlist *l;
3645
Tejun Heo4bac00d2013-11-29 10:42:59 -05003646 lockdep_assert_held(&cgrp->pidlist_mutex);
3647
Ben Blum102a7752009-09-23 15:56:26 -07003648 /*
3649 * If cgroup gets more users after we read count, we won't have
3650 * enough space - tough. This race is indistinguishable to the
3651 * caller from the case that the additional cgroup users didn't
3652 * show up until sometime later on.
3653 */
3654 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003655 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003656 if (!array)
3657 return -ENOMEM;
3658 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003659 css_task_iter_start(&cgrp->dummy_css, &it);
3660 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003661 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003662 break;
Ben Blum102a7752009-09-23 15:56:26 -07003663 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003664 if (type == CGROUP_FILE_PROCS)
3665 pid = task_tgid_vnr(tsk);
3666 else
3667 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003668 if (pid > 0) /* make sure to only use valid results */
3669 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003670 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003671 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003672 length = n;
3673 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003674 if (cgroup_sane_behavior(cgrp))
3675 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3676 else
3677 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003678 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003679 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003680
Tejun Heoe6b81712013-11-29 10:42:59 -05003681 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003682 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003683 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003684 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003685 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003686 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003687
3688 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003689 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003690 l->list = array;
3691 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003692 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003693 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003694}
3695
Balbir Singh846c7bb2007-10-18 23:39:44 -07003696/**
Li Zefana043e3b2008-02-23 15:24:09 -08003697 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003698 * @stats: cgroupstats to fill information into
3699 * @dentry: A dentry entry belonging to the cgroup for which stats have
3700 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003701 *
3702 * Build and fill cgroupstats so that taskstats can export it to user
3703 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003704 */
3705int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3706{
3707 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003708 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003709 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003710 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003711
Balbir Singh846c7bb2007-10-18 23:39:44 -07003712 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003713 * Validate dentry by checking the superblock operations,
3714 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003715 */
Li Zefan33d283b2008-11-19 15:36:48 -08003716 if (dentry->d_sb->s_op != &cgroup_ops ||
3717 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003718 goto err;
3719
3720 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003721 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003722
Tejun Heo72ec7022013-08-08 20:11:26 -04003723 css_task_iter_start(&cgrp->dummy_css, &it);
3724 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003725 switch (tsk->state) {
3726 case TASK_RUNNING:
3727 stats->nr_running++;
3728 break;
3729 case TASK_INTERRUPTIBLE:
3730 stats->nr_sleeping++;
3731 break;
3732 case TASK_UNINTERRUPTIBLE:
3733 stats->nr_uninterruptible++;
3734 break;
3735 case TASK_STOPPED:
3736 stats->nr_stopped++;
3737 break;
3738 default:
3739 if (delayacct_is_task_waiting_on_io(tsk))
3740 stats->nr_io_wait++;
3741 break;
3742 }
3743 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003744 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003745
Balbir Singh846c7bb2007-10-18 23:39:44 -07003746err:
3747 return ret;
3748}
3749
Paul Menage8f3ff202009-09-23 15:56:25 -07003750
Paul Menagecc31edc2008-10-18 20:28:04 -07003751/*
Ben Blum102a7752009-09-23 15:56:26 -07003752 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003753 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003754 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003755 */
3756
Ben Blum102a7752009-09-23 15:56:26 -07003757static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003758{
3759 /*
3760 * Initially we receive a position value that corresponds to
3761 * one more than the last pid shown (or 0 on the first call or
3762 * after a seek to the start). Use a binary-search to find the
3763 * next pid to display, if any
3764 */
Tejun Heo5d224442013-12-05 12:28:04 -05003765 struct cgroup_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003766 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003767 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003768 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003769 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003770 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003771
Tejun Heo4bac00d2013-11-29 10:42:59 -05003772 mutex_lock(&cgrp->pidlist_mutex);
3773
3774 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003775 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003776 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003777 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003778 * could already have been destroyed.
3779 */
Tejun Heo5d224442013-12-05 12:28:04 -05003780 if (of->priv)
3781 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003782
3783 /*
3784 * Either this is the first start() after open or the matching
3785 * pidlist has been destroyed inbetween. Create a new one.
3786 */
Tejun Heo5d224442013-12-05 12:28:04 -05003787 if (!of->priv) {
3788 ret = pidlist_array_load(cgrp, type,
3789 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003790 if (ret)
3791 return ERR_PTR(ret);
3792 }
Tejun Heo5d224442013-12-05 12:28:04 -05003793 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003794
Paul Menagecc31edc2008-10-18 20:28:04 -07003795 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003796 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003797
Paul Menagecc31edc2008-10-18 20:28:04 -07003798 while (index < end) {
3799 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003800 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003801 index = mid;
3802 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003803 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003804 index = mid + 1;
3805 else
3806 end = mid;
3807 }
3808 }
3809 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003810 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003811 return NULL;
3812 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003813 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003814 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003815 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003816}
3817
Ben Blum102a7752009-09-23 15:56:26 -07003818static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003819{
Tejun Heo5d224442013-12-05 12:28:04 -05003820 struct cgroup_open_file *of = s->private;
3821 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003822
Tejun Heo5d224442013-12-05 12:28:04 -05003823 if (l)
3824 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003825 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003826 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003827}
3828
Ben Blum102a7752009-09-23 15:56:26 -07003829static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003830{
Tejun Heo5d224442013-12-05 12:28:04 -05003831 struct cgroup_open_file *of = s->private;
3832 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003833 pid_t *p = v;
3834 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003835 /*
3836 * Advance to the next pid in the array. If this goes off the
3837 * end, we're done
3838 */
3839 p++;
3840 if (p >= end) {
3841 return NULL;
3842 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003843 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003844 return p;
3845 }
3846}
3847
Ben Blum102a7752009-09-23 15:56:26 -07003848static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003849{
3850 return seq_printf(s, "%d\n", *(int *)v);
3851}
3852
Ben Blum102a7752009-09-23 15:56:26 -07003853/*
3854 * seq_operations functions for iterating on pidlists through seq_file -
3855 * independent of whether it's tasks or procs
3856 */
3857static const struct seq_operations cgroup_pidlist_seq_operations = {
3858 .start = cgroup_pidlist_start,
3859 .stop = cgroup_pidlist_stop,
3860 .next = cgroup_pidlist_next,
3861 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003862};
3863
Tejun Heo182446d2013-08-08 20:11:24 -04003864static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3865 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003866{
Tejun Heo182446d2013-08-08 20:11:24 -04003867 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003868}
3869
Tejun Heo182446d2013-08-08 20:11:24 -04003870static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3871 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003872{
Tejun Heo182446d2013-08-08 20:11:24 -04003873 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003874 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003875 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003876 else
Tejun Heo182446d2013-08-08 20:11:24 -04003877 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003878 return 0;
3879}
3880
Paul Menagebbcb81d2007-10-18 23:39:32 -07003881/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003882 * When dput() is called asynchronously, if umount has been done and
3883 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3884 * there's a small window that vfs will see the root dentry with non-zero
3885 * refcnt and trigger BUG().
3886 *
3887 * That's why we hold a reference before dput() and drop it right after.
3888 */
3889static void cgroup_dput(struct cgroup *cgrp)
3890{
Tejun Heo59f52962014-02-11 11:52:49 -05003891 cgroup_get_root(cgrp->root);
3892 cgroup_put(cgrp);
3893 cgroup_put_root(cgrp->root);
Li Zefan1c8158e2013-06-18 18:41:10 +08003894}
3895
Tejun Heo182446d2013-08-08 20:11:24 -04003896static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3897 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003898{
Tejun Heo182446d2013-08-08 20:11:24 -04003899 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003900}
3901
Tejun Heo182446d2013-08-08 20:11:24 -04003902static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3903 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003904{
3905 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003906 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003907 else
Tejun Heo182446d2013-08-08 20:11:24 -04003908 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003909 return 0;
3910}
3911
Tejun Heod5c56ce2013-06-03 19:14:34 -07003912static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003913 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003914 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003915 .seq_start = cgroup_pidlist_start,
3916 .seq_next = cgroup_pidlist_next,
3917 .seq_stop = cgroup_pidlist_stop,
3918 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003919 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003920 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003921 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003922 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003923 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003924 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003925 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003926 .read_u64 = cgroup_clone_children_read,
3927 .write_u64 = cgroup_clone_children_write,
3928 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003929 {
Tejun Heo873fe092013-04-14 20:15:26 -07003930 .name = "cgroup.sane_behavior",
3931 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003932 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003933 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003934
3935 /*
3936 * Historical crazy stuff. These don't have "cgroup." prefix and
3937 * don't exist if sane_behavior. If you're depending on these, be
3938 * prepared to be burned.
3939 */
3940 {
3941 .name = "tasks",
3942 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003943 .seq_start = cgroup_pidlist_start,
3944 .seq_next = cgroup_pidlist_next,
3945 .seq_stop = cgroup_pidlist_stop,
3946 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003947 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003948 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003949 .mode = S_IRUGO | S_IWUSR,
3950 },
3951 {
3952 .name = "notify_on_release",
3953 .flags = CFTYPE_INSANE,
3954 .read_u64 = cgroup_read_notify_on_release,
3955 .write_u64 = cgroup_write_notify_on_release,
3956 },
Tejun Heo873fe092013-04-14 20:15:26 -07003957 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003958 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003959 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003960 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003961 .write_string = cgroup_release_agent_write,
Tejun Heo5f469902014-02-11 11:52:48 -05003962 .max_write_len = PATH_MAX - 1,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003963 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003964 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003965};
3966
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003967/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003968 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003969 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003970 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003971 *
3972 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003973 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003974static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003975{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003976 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003977 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003978
Tejun Heo8e3f6542012-04-01 12:09:55 -07003979 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003980 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003981 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003982
3983 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003984 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003985
Tejun Heobee55092013-06-28 16:24:11 -07003986 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003987 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003988 if (ret < 0)
3989 goto err;
3990 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003991 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003992 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003993err:
3994 cgroup_clear_dir(cgrp, subsys_mask);
3995 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003996}
3997
Tejun Heo0c21ead2013-08-13 20:22:51 -04003998/*
3999 * css destruction is four-stage process.
4000 *
4001 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4002 * Implemented in kill_css().
4003 *
4004 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4005 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4006 * by invoking offline_css(). After offlining, the base ref is put.
4007 * Implemented in css_killed_work_fn().
4008 *
4009 * 3. When the percpu_ref reaches zero, the only possible remaining
4010 * accessors are inside RCU read sections. css_release() schedules the
4011 * RCU callback.
4012 *
4013 * 4. After the grace period, the css can be freed. Implemented in
4014 * css_free_work_fn().
4015 *
4016 * It is actually hairier because both step 2 and 4 require process context
4017 * and thus involve punting to css->destroy_work adding two additional
4018 * steps to the already complex sequence.
4019 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004020static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004021{
4022 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004023 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004024 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004025
Tejun Heo0ae78e02013-08-13 11:01:54 -04004026 if (css->parent)
4027 css_put(css->parent);
4028
Tejun Heo0c21ead2013-08-13 20:22:51 -04004029 css->ss->css_free(css);
4030 cgroup_dput(cgrp);
4031}
4032
4033static void css_free_rcu_fn(struct rcu_head *rcu_head)
4034{
4035 struct cgroup_subsys_state *css =
4036 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4037
4038 /*
4039 * css holds an extra ref to @cgrp->dentry which is put on the last
4040 * css_put(). dput() requires process context which we don't have.
4041 */
4042 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004043 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004044}
4045
Tejun Heod3daf282013-06-13 19:39:16 -07004046static void css_release(struct percpu_ref *ref)
4047{
4048 struct cgroup_subsys_state *css =
4049 container_of(ref, struct cgroup_subsys_state, refcnt);
4050
Tejun Heoaec25022014-02-08 10:36:58 -05004051 rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004052 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004053}
4054
Tejun Heo623f9262013-08-13 11:01:55 -04004055static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4056 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004057{
Paul Menagebd89aab2007-10-18 23:40:44 -07004058 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004059 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004060 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004061
Tejun Heo0ae78e02013-08-13 11:01:54 -04004062 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004063 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004064 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004065 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004066
Tejun Heoca8bdca2013-08-26 18:40:56 -04004067 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004068}
4069
Li Zefan2a4ac632013-07-31 16:16:40 +08004070/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004071static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004072{
Tejun Heo623f9262013-08-13 11:01:55 -04004073 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004074 int ret = 0;
4075
Tejun Heoace2bee2014-02-11 11:52:47 -05004076 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004077 lockdep_assert_held(&cgroup_mutex);
4078
Tejun Heo92fb9742012-11-19 08:13:38 -08004079 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004080 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004081 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004082 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004083 css->cgroup->nr_css++;
Tejun Heoaec25022014-02-08 10:36:58 -05004084 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004085 }
Tejun Heob1929db2012-11-19 08:13:38 -08004086 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004087}
4088
Li Zefan2a4ac632013-07-31 16:16:40 +08004089/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004090static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004091{
Tejun Heo623f9262013-08-13 11:01:55 -04004092 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004093
Tejun Heoace2bee2014-02-11 11:52:47 -05004094 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004095 lockdep_assert_held(&cgroup_mutex);
4096
4097 if (!(css->flags & CSS_ONLINE))
4098 return;
4099
Li Zefand7eeac12013-03-12 15:35:59 -07004100 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004101 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004102
Tejun Heoeb954192013-08-08 20:11:23 -04004103 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004104 css->cgroup->nr_css--;
Tejun Heoaec25022014-02-08 10:36:58 -05004105 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004106}
4107
Tejun Heoc81c925a2013-12-06 15:11:56 -05004108/**
4109 * create_css - create a cgroup_subsys_state
4110 * @cgrp: the cgroup new css will be associated with
4111 * @ss: the subsys of new css
4112 *
4113 * Create a new css associated with @cgrp - @ss pair. On success, the new
4114 * css is online and installed in @cgrp with all interface files created.
4115 * Returns 0 on success, -errno on failure.
4116 */
4117static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4118{
4119 struct cgroup *parent = cgrp->parent;
4120 struct cgroup_subsys_state *css;
4121 int err;
4122
4123 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
4124 lockdep_assert_held(&cgroup_mutex);
4125
4126 css = ss->css_alloc(cgroup_css(parent, ss));
4127 if (IS_ERR(css))
4128 return PTR_ERR(css);
4129
4130 err = percpu_ref_init(&css->refcnt, css_release);
4131 if (err)
4132 goto err_free;
4133
4134 init_css(css, ss, cgrp);
4135
Tejun Heoaec25022014-02-08 10:36:58 -05004136 err = cgroup_populate_dir(cgrp, 1 << ss->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004137 if (err)
4138 goto err_free;
4139
4140 err = online_css(css);
4141 if (err)
4142 goto err_free;
4143
Tejun Heo59f52962014-02-11 11:52:49 -05004144 cgroup_get(cgrp);
Tejun Heoc81c925a2013-12-06 15:11:56 -05004145 css_get(css->parent);
4146
4147 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4148 parent->parent) {
4149 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",
4150 current->comm, current->pid, ss->name);
4151 if (!strcmp(ss->name, "memory"))
4152 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4153 ss->warned_broken_hierarchy = true;
4154 }
4155
4156 return 0;
4157
4158err_free:
4159 percpu_ref_cancel_init(&css->refcnt);
4160 ss->css_free(css);
4161 return err;
4162}
4163
Paul Menageddbcc7e2007-10-18 23:39:30 -07004164/*
Li Zefana043e3b2008-02-23 15:24:09 -08004165 * cgroup_create - create a cgroup
4166 * @parent: cgroup that will be parent of the new cgroup
4167 * @dentry: dentry of the new cgroup
4168 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169 *
Li Zefana043e3b2008-02-23 15:24:09 -08004170 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004171 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004172static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004173 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004174{
Paul Menagebd89aab2007-10-18 23:40:44 -07004175 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004176 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004177 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05004178 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004179 struct cgroup_subsys *ss;
4180 struct super_block *sb = root->sb;
4181
Tejun Heo0a950f62012-11-19 09:02:12 -08004182 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004183 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4184 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004185 return -ENOMEM;
4186
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05004187 name = cgroup_alloc_name(dentry->d_name.name);
Tejun Heob58c8992014-02-08 10:26:33 -05004188 if (!name) {
4189 err = -ENOMEM;
Li Zefan65dff752013-03-01 15:01:56 +08004190 goto err_free_cgrp;
Tejun Heob58c8992014-02-08 10:26:33 -05004191 }
Li Zefan65dff752013-03-01 15:01:56 +08004192 rcu_assign_pointer(cgrp->name, name);
4193
Tejun Heoace2bee2014-02-11 11:52:47 -05004194 mutex_lock(&cgroup_tree_mutex);
4195
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004196 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08004197 * Only live parents can have children. Note that the liveliness
4198 * check isn't strictly necessary because cgroup_mkdir() and
4199 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4200 * anyway so that locking is contained inside cgroup proper and we
4201 * don't get nasty surprises if we ever grow another caller.
4202 */
4203 if (!cgroup_lock_live_group(parent)) {
4204 err = -ENODEV;
Tejun Heoace2bee2014-02-11 11:52:47 -05004205 goto err_unlock_tree;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004206 }
4207
4208 /*
4209 * Temporarily set the pointer to NULL, so idr_find() won't return
4210 * a half-baked cgroup.
4211 */
4212 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
4213 if (cgrp->id < 0) {
4214 err = -ENOMEM;
4215 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08004216 }
4217
Paul Menageddbcc7e2007-10-18 23:39:30 -07004218 /* Grab a reference on the superblock so the hierarchy doesn't
4219 * get deleted on unmount if there are child cgroups. This
4220 * can be done outside cgroup_mutex, since the sb can't
4221 * disappear while someone has an open control file on the
4222 * fs */
Tejun Heo59f52962014-02-11 11:52:49 -05004223 cgroup_get_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004224
Paul Menagecc31edc2008-10-18 20:28:04 -07004225 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004226
Li Zefanfe1c06c2013-01-24 14:30:22 +08004227 dentry->d_fsdata = cgrp;
4228 cgrp->dentry = dentry;
4229
Paul Menagebd89aab2007-10-18 23:40:44 -07004230 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004231 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004232 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004233
Li Zefanb6abdb02008-03-04 14:28:19 -08004234 if (notify_on_release(parent))
4235 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4236
Tejun Heo2260e7f2012-11-19 08:13:38 -08004237 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4238 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004239
Tejun Heo4e139af2012-11-19 08:13:36 -08004240 /*
4241 * Create directory. cgroup_create_file() returns with the new
4242 * directory locked on success so that it can be populated without
4243 * dropping cgroup_mutex.
4244 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004245 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246 if (err < 0)
Li Zefan0ab02ca2014-02-11 16:05:46 +08004247 goto err_free_id;
Tejun Heo4e139af2012-11-19 08:13:36 -08004248 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249
Tejun Heo00356bd2013-06-18 11:14:22 -07004250 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004251
Tejun Heo4e139af2012-11-19 08:13:36 -08004252 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004253 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4254 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004255
Li Zefan415cf072013-04-08 14:35:02 +08004256 /* hold a ref to the parent's dentry */
Tejun Heo59f52962014-02-11 11:52:49 -05004257 cgroup_get(parent);
Li Zefan415cf072013-04-08 14:35:02 +08004258
Tejun Heo0d802552013-12-06 15:11:56 -05004259 /*
4260 * @cgrp is now fully operational. If something fails after this
4261 * point, it'll be released via the normal destruction path.
4262 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004263 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4264
Tejun Heo2bb566c2013-08-08 20:11:23 -04004265 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004266 if (err)
4267 goto err_destroy;
4268
Tejun Heo9d403e92013-12-06 15:11:56 -05004269 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004270 for_each_subsys(ss, ssid) {
4271 if (root->subsys_mask & (1 << ssid)) {
4272 err = create_css(cgrp, ss);
4273 if (err)
4274 goto err_destroy;
4275 }
Tejun Heoa8638032012-11-09 09:12:29 -08004276 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004277
4278 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004279 mutex_unlock(&cgroup_tree_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004280 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281
4282 return 0;
4283
Tejun Heo0a950f62012-11-19 09:02:12 -08004284err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004285 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004286 /* Release the reference count that we took on the superblock */
Tejun Heo59f52962014-02-11 11:52:49 -05004287 cgroup_put_root(root);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004288err_unlock:
4289 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004290err_unlock_tree:
4291 mutex_unlock(&cgroup_tree_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08004292 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004293err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004294 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004295 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004296
4297err_destroy:
4298 cgroup_destroy_locked(cgrp);
4299 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004300 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004301 mutex_unlock(&dentry->d_inode->i_mutex);
4302 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004303}
4304
Al Viro18bb1db2011-07-26 01:41:39 -04004305static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004306{
4307 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4308
4309 /* the vfs holds inode->i_mutex already */
4310 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4311}
4312
Tejun Heo223dbc32013-08-13 20:22:50 -04004313/*
4314 * This is called when the refcnt of a css is confirmed to be killed.
4315 * css_tryget() is now guaranteed to fail.
4316 */
4317static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004318{
Tejun Heo223dbc32013-08-13 20:22:50 -04004319 struct cgroup_subsys_state *css =
4320 container_of(work, struct cgroup_subsys_state, destroy_work);
4321 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004322
Tejun Heoace2bee2014-02-11 11:52:47 -05004323 mutex_lock(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004324 mutex_lock(&cgroup_mutex);
4325
4326 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004327 * css_tryget() is guaranteed to fail now. Tell subsystems to
4328 * initate destruction.
4329 */
4330 offline_css(css);
4331
4332 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004333 * If @cgrp is marked dead, it's waiting for refs of all css's to
4334 * be disabled before proceeding to the second phase of cgroup
4335 * destruction. If we are the last one, kick it off.
4336 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004337 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004338 cgroup_destroy_css_killed(cgrp);
4339
4340 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004341 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004342
4343 /*
4344 * Put the css refs from kill_css(). Each css holds an extra
4345 * reference to the cgroup's dentry and cgroup removal proceeds
4346 * regardless of css refs. On the last put of each css, whenever
4347 * that may be, the extra dentry ref is put so that dentry
4348 * destruction happens only after all css's are released.
4349 */
4350 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004351}
4352
Tejun Heo223dbc32013-08-13 20:22:50 -04004353/* css kill confirmation processing requires process context, bounce */
4354static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004355{
4356 struct cgroup_subsys_state *css =
4357 container_of(ref, struct cgroup_subsys_state, refcnt);
4358
Tejun Heo223dbc32013-08-13 20:22:50 -04004359 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004360 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004361}
4362
4363/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004364 * kill_css - destroy a css
4365 * @css: css to destroy
4366 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004367 * This function initiates destruction of @css by removing cgroup interface
4368 * files and putting its base reference. ->css_offline() will be invoked
4369 * asynchronously once css_tryget() is guaranteed to fail and when the
4370 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004371 */
4372static void kill_css(struct cgroup_subsys_state *css)
4373{
Tejun Heoaec25022014-02-08 10:36:58 -05004374 cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004375
Tejun Heoedae0c32013-08-13 20:22:51 -04004376 /*
4377 * Killing would put the base ref, but we need to keep it alive
4378 * until after ->css_offline().
4379 */
4380 css_get(css);
4381
4382 /*
4383 * cgroup core guarantees that, by the time ->css_offline() is
4384 * invoked, no new css reference will be given out via
4385 * css_tryget(). We can't simply call percpu_ref_kill() and
4386 * proceed to offlining css's because percpu_ref_kill() doesn't
4387 * guarantee that the ref is seen as killed on all CPUs on return.
4388 *
4389 * Use percpu_ref_kill_and_confirm() to get notifications as each
4390 * css is confirmed to be seen as killed on all CPUs.
4391 */
4392 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004393}
4394
4395/**
4396 * cgroup_destroy_locked - the first stage of cgroup destruction
4397 * @cgrp: cgroup to be destroyed
4398 *
4399 * css's make use of percpu refcnts whose killing latency shouldn't be
4400 * exposed to userland and are RCU protected. Also, cgroup core needs to
4401 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4402 * invoked. To satisfy all the requirements, destruction is implemented in
4403 * the following two steps.
4404 *
4405 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4406 * userland visible parts and start killing the percpu refcnts of
4407 * css's. Set up so that the next stage will be kicked off once all
4408 * the percpu refcnts are confirmed to be killed.
4409 *
4410 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4411 * rest of destruction. Once all cgroup references are gone, the
4412 * cgroup is RCU-freed.
4413 *
4414 * This function implements s1. After this step, @cgrp is gone as far as
4415 * the userland is concerned and a new cgroup with the same name may be
4416 * created. As cgroup doesn't care about the names internally, this
4417 * doesn't cause any problem.
4418 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004419static int cgroup_destroy_locked(struct cgroup *cgrp)
4420 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004421{
Tejun Heo42809dd2012-11-19 08:13:37 -08004422 struct dentry *d = cgrp->dentry;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004423 struct cgroup_subsys_state *css;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004424 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004425 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004426 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004427
Tejun Heo42809dd2012-11-19 08:13:37 -08004428 lockdep_assert_held(&d->d_inode->i_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004429 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004430 lockdep_assert_held(&cgroup_mutex);
4431
Tejun Heoddd69142013-06-12 21:04:54 -07004432 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004433 * css_set_lock synchronizes access to ->cset_links and prevents
4434 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004435 */
4436 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004437 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004438 read_unlock(&css_set_lock);
4439 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004440 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004441
Tejun Heo1a90dd52012-11-05 09:16:59 -08004442 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004443 * Make sure there's no live children. We can't test ->children
4444 * emptiness as dead children linger on it while being destroyed;
4445 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4446 */
4447 empty = true;
4448 rcu_read_lock();
4449 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4450 empty = cgroup_is_dead(child);
4451 if (!empty)
4452 break;
4453 }
4454 rcu_read_unlock();
4455 if (!empty)
4456 return -EBUSY;
4457
4458 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004459 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4460 * will be invoked to perform the rest of destruction once the
Tejun Heo4ac06012014-02-11 11:52:47 -05004461 * percpu refs of all css's are confirmed to be killed. This
4462 * involves removing the subsystem's files, drop cgroup_mutex.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004463 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004464 mutex_unlock(&cgroup_mutex);
Tejun Heo1c6727a2013-12-06 15:11:56 -05004465 for_each_css(css, ssid, cgrp)
4466 kill_css(css);
Tejun Heo4ac06012014-02-11 11:52:47 -05004467 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004468
4469 /*
4470 * Mark @cgrp dead. This prevents further task migration and child
4471 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004472 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004473 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004474 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004475 */
Tejun Heo54766d42013-06-12 21:04:53 -07004476 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004477
Tejun Heo455050d2013-06-13 19:27:41 -07004478 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4479 raw_spin_lock(&release_list_lock);
4480 if (!list_empty(&cgrp->release_list))
4481 list_del_init(&cgrp->release_list);
4482 raw_spin_unlock(&release_list_lock);
4483
4484 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004485 * If @cgrp has css's attached, the second stage of cgroup
4486 * destruction is kicked off from css_killed_work_fn() after the
4487 * refs of all attached css's are killed. If @cgrp doesn't have
4488 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004489 */
Tejun Heof20104d2013-08-13 20:22:50 -04004490 if (!cgrp->nr_css)
4491 cgroup_destroy_css_killed(cgrp);
4492
4493 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004494 * Clear the base files and remove @cgrp directory. The removal
4495 * puts the base ref but we aren't quite done with @cgrp yet, so
4496 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004497 */
Tejun Heo4ac06012014-02-11 11:52:47 -05004498 mutex_unlock(&cgroup_mutex);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004499 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004500 dget(d);
4501 cgroup_d_remove_dir(d);
Tejun Heo4ac06012014-02-11 11:52:47 -05004502 mutex_lock(&cgroup_mutex);
Tejun Heo455050d2013-06-13 19:27:41 -07004503
Tejun Heoea15f8c2013-06-13 19:27:42 -07004504 return 0;
4505};
4506
Tejun Heod3daf282013-06-13 19:39:16 -07004507/**
Tejun Heof20104d2013-08-13 20:22:50 -04004508 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004509 * @work: cgroup->destroy_free_work
4510 *
4511 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004512 * destroyed after all css's are offlined and performs the rest of
4513 * destruction. This is the second step of destruction described in the
4514 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004515 */
Tejun Heof20104d2013-08-13 20:22:50 -04004516static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004517{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004518 struct cgroup *parent = cgrp->parent;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004519
Tejun Heoace2bee2014-02-11 11:52:47 -05004520 lockdep_assert_held(&cgroup_tree_mutex);
Tejun Heof20104d2013-08-13 20:22:50 -04004521 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004522
Paul Menage999cd8a2009-01-07 18:08:36 -08004523 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004524 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004525
Tejun Heo59f52962014-02-11 11:52:49 -05004526 cgroup_put(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004527
Paul Menagebd89aab2007-10-18 23:40:44 -07004528 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004529 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004530}
4531
Tejun Heo42809dd2012-11-19 08:13:37 -08004532static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4533{
4534 int ret;
4535
Tejun Heoace2bee2014-02-11 11:52:47 -05004536 mutex_lock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004537 mutex_lock(&cgroup_mutex);
4538 ret = cgroup_destroy_locked(dentry->d_fsdata);
4539 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004540 mutex_unlock(&cgroup_tree_mutex);
Tejun Heo42809dd2012-11-19 08:13:37 -08004541
4542 return ret;
4543}
4544
Li Zefan06a11922008-04-29 01:00:07 -07004545static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004546{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004547 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004548
4549 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004550
Tejun Heoace2bee2014-02-11 11:52:47 -05004551 mutex_lock(&cgroup_tree_mutex);
Tejun Heo648bb562012-11-19 08:13:36 -08004552 mutex_lock(&cgroup_mutex);
4553
Tejun Heode00ffa2014-02-11 11:52:48 -05004554 INIT_LIST_HEAD(&ss->cftsets);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004555
Paul Menageddbcc7e2007-10-18 23:39:30 -07004556 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004557 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004558 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004559 /* We don't handle early failures gracefully */
4560 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004561 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004562
Li Zefane8d55fd2008-04-29 01:00:13 -07004563 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004564 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004565 * newly registered, all tasks and hence the
4566 * init_css_set is in the subsystem's top cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05004567 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004568
4569 need_forkexit_callback |= ss->fork || ss->exit;
4570
Li Zefane8d55fd2008-04-29 01:00:13 -07004571 /* At system boot, before all subsystems have been
4572 * registered, no tasks have been forked, so we don't
4573 * need to invoke fork callbacks here. */
4574 BUG_ON(!list_empty(&init_task.tasks));
4575
Tejun Heoae7f1642013-08-13 20:22:50 -04004576 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004577
Tejun Heo648bb562012-11-19 08:13:36 -08004578 mutex_unlock(&cgroup_mutex);
Tejun Heoace2bee2014-02-11 11:52:47 -05004579 mutex_unlock(&cgroup_tree_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004580}
4581
4582/**
Li Zefana043e3b2008-02-23 15:24:09 -08004583 * cgroup_init_early - cgroup initialization at system boot
4584 *
4585 * Initialize cgroups at system boot, and initialize any
4586 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004587 */
4588int __init cgroup_init_early(void)
4589{
Tejun Heo30159ec2013-06-25 11:53:37 -07004590 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004591 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004592
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004593 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004594 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004595 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004596 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004597 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004598 init_cgroup_root(&cgroup_dummy_root);
4599 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004600 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004601
Tejun Heo69d02062013-06-12 21:04:50 -07004602 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004603 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4604 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004605 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004606
Tejun Heo3ed80a62014-02-08 10:36:58 -05004607 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05004608 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Tejun Heo073219e2014-02-08 10:36:58 -05004609 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4610 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05004611 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05004612 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4613 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4614
Tejun Heoaec25022014-02-08 10:36:58 -05004615 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05004616 ss->name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004617
4618 if (ss->early_init)
4619 cgroup_init_subsys(ss);
4620 }
4621 return 0;
4622}
4623
4624/**
Li Zefana043e3b2008-02-23 15:24:09 -08004625 * cgroup_init - cgroup initialization
4626 *
4627 * Register cgroup filesystem and /proc file, and initialize
4628 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004629 */
4630int __init cgroup_init(void)
4631{
Tejun Heo30159ec2013-06-25 11:53:37 -07004632 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004633 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004634 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004635
4636 err = bdi_init(&cgroup_backing_dev_info);
4637 if (err)
4638 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639
Tejun Heo2da440a2014-02-11 11:52:48 -05004640 cgroup_init_cftypes(NULL, cgroup_base_files);
4641
Tejun Heo3ed80a62014-02-08 10:36:58 -05004642 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004643 if (!ss->early_init)
4644 cgroup_init_subsys(ss);
Tejun Heode00ffa2014-02-11 11:52:48 -05004645
4646 /*
4647 * cftype registration needs kmalloc and can't be done
4648 * during early_init. Register base cftypes separately.
4649 */
4650 if (ss->base_cftypes)
4651 WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004652 }
4653
Tejun Heofa3ca072013-04-14 11:36:56 -07004654 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004655 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004656
Tejun Heo82fe9b02013-06-25 11:53:37 -07004657 /* Add init_css_set to the hash table */
4658 key = css_set_hash(init_css_set.subsys);
4659 hash_add(css_set_table, &init_css_set.hlist, key);
4660
Tejun Heofc76df72013-06-25 11:53:37 -07004661 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004662
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004663 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4664 0, 1, GFP_KERNEL);
4665 BUG_ON(err < 0);
4666
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004667 mutex_unlock(&cgroup_mutex);
4668
Greg KH676db4a2010-08-05 13:53:35 -07004669 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4670 if (!cgroup_kobj) {
4671 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004672 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004673 }
4674
4675 err = register_filesystem(&cgroup_fs_type);
4676 if (err < 0) {
4677 kobject_put(cgroup_kobj);
4678 goto out;
4679 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004680
Li Zefan46ae2202008-04-29 01:00:08 -07004681 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004682
Paul Menageddbcc7e2007-10-18 23:39:30 -07004683out:
Paul Menagea4243162007-10-18 23:39:35 -07004684 if (err)
4685 bdi_destroy(&cgroup_backing_dev_info);
4686
Paul Menageddbcc7e2007-10-18 23:39:30 -07004687 return err;
4688}
Paul Menageb4f48b62007-10-18 23:39:33 -07004689
Tejun Heoe5fca242013-11-22 17:14:39 -05004690static int __init cgroup_wq_init(void)
4691{
4692 /*
4693 * There isn't much point in executing destruction path in
4694 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004695 *
4696 * XXX: Must be ordered to make sure parent is offlined after
4697 * children. The ordering requirement is for memcg where a
4698 * parent's offline may wait for a child's leading to deadlock. In
4699 * the long term, this should be fixed from memcg side.
Tejun Heoe5fca242013-11-22 17:14:39 -05004700 *
4701 * We would prefer to do this in cgroup_init() above, but that
4702 * is called before init_workqueues(): so leave this until after.
4703 */
Hugh Dickinsab3f5fa2014-02-06 15:56:01 -08004704 cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
Tejun Heoe5fca242013-11-22 17:14:39 -05004705 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004706
4707 /*
4708 * Used to destroy pidlists and separate to serve as flush domain.
4709 * Cap @max_active to 1 too.
4710 */
4711 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4712 0, 1);
4713 BUG_ON(!cgroup_pidlist_destroy_wq);
4714
Tejun Heoe5fca242013-11-22 17:14:39 -05004715 return 0;
4716}
4717core_initcall(cgroup_wq_init);
4718
Paul Menagea4243162007-10-18 23:39:35 -07004719/*
4720 * proc_cgroup_show()
4721 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4722 * - Used for /proc/<pid>/cgroup.
4723 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4724 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004725 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004726 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4727 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4728 * cgroup to top_cgroup.
4729 */
4730
4731/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004732int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004733{
4734 struct pid *pid;
4735 struct task_struct *tsk;
4736 char *buf;
4737 int retval;
4738 struct cgroupfs_root *root;
4739
4740 retval = -ENOMEM;
4741 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4742 if (!buf)
4743 goto out;
4744
4745 retval = -ESRCH;
4746 pid = m->private;
4747 tsk = get_pid_task(pid, PIDTYPE_PID);
4748 if (!tsk)
4749 goto out_free;
4750
4751 retval = 0;
4752
4753 mutex_lock(&cgroup_mutex);
4754
Li Zefane5f6a862009-01-07 18:07:41 -08004755 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004756 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004757 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004758 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004759
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004760 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004761 for_each_subsys(ss, ssid)
4762 if (root->subsys_mask & (1 << ssid))
4763 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004764 if (strlen(root->name))
4765 seq_printf(m, "%sname=%s", count ? "," : "",
4766 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004767 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004768 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004769 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004770 if (retval < 0)
4771 goto out_unlock;
4772 seq_puts(m, buf);
4773 seq_putc(m, '\n');
4774 }
4775
4776out_unlock:
4777 mutex_unlock(&cgroup_mutex);
4778 put_task_struct(tsk);
4779out_free:
4780 kfree(buf);
4781out:
4782 return retval;
4783}
4784
Paul Menagea4243162007-10-18 23:39:35 -07004785/* Display information about each subsystem and each hierarchy */
4786static int proc_cgroupstats_show(struct seq_file *m, void *v)
4787{
Tejun Heo30159ec2013-06-25 11:53:37 -07004788 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004789 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004790
Paul Menage8bab8dd2008-04-04 14:29:57 -07004791 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004792 /*
4793 * ideally we don't want subsystems moving around while we do this.
4794 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4795 * subsys/hierarchy state.
4796 */
Paul Menagea4243162007-10-18 23:39:35 -07004797 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004798
4799 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004800 seq_printf(m, "%s\t%d\t%d\t%d\n",
4801 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004802 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004803
Paul Menagea4243162007-10-18 23:39:35 -07004804 mutex_unlock(&cgroup_mutex);
4805 return 0;
4806}
4807
4808static int cgroupstats_open(struct inode *inode, struct file *file)
4809{
Al Viro9dce07f2008-03-29 03:07:28 +00004810 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004811}
4812
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004813static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004814 .open = cgroupstats_open,
4815 .read = seq_read,
4816 .llseek = seq_lseek,
4817 .release = single_release,
4818};
4819
Paul Menageb4f48b62007-10-18 23:39:33 -07004820/**
4821 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004822 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004823 *
4824 * Description: A task inherits its parent's cgroup at fork().
4825 *
4826 * A pointer to the shared css_set was automatically copied in
4827 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004828 * it was not made under the protection of RCU or cgroup_mutex, so
4829 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4830 * have already changed current->cgroups, allowing the previously
4831 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004832 *
4833 * At the point that cgroup_fork() is called, 'current' is the parent
4834 * task, and the passed argument 'child' points to the child task.
4835 */
4836void cgroup_fork(struct task_struct *child)
4837{
Tejun Heo9bb71302012-10-18 17:52:07 -07004838 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004839 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004840 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004841 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004842 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004843}
4844
4845/**
Li Zefana043e3b2008-02-23 15:24:09 -08004846 * cgroup_post_fork - called on a new task after adding it to the task list
4847 * @child: the task in question
4848 *
Tejun Heo5edee612012-10-16 15:03:14 -07004849 * Adds the task to the list running through its css_set if necessary and
4850 * call the subsystem fork() callbacks. Has to be after the task is
4851 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004852 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004853 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004854 */
Paul Menage817929e2007-10-18 23:39:36 -07004855void cgroup_post_fork(struct task_struct *child)
4856{
Tejun Heo30159ec2013-06-25 11:53:37 -07004857 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004858 int i;
4859
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004860 /*
4861 * use_task_css_set_links is set to 1 before we walk the tasklist
4862 * under the tasklist_lock and we read it here after we added the child
4863 * to the tasklist under the tasklist_lock as well. If the child wasn't
4864 * yet in the tasklist when we walked through it from
4865 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4866 * should be visible now due to the paired locking and barriers implied
4867 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4868 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4869 * lock on fork.
4870 */
Paul Menage817929e2007-10-18 23:39:36 -07004871 if (use_task_css_set_links) {
4872 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004873 task_lock(child);
4874 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004875 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004876 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004877 write_unlock(&css_set_lock);
4878 }
Tejun Heo5edee612012-10-16 15:03:14 -07004879
4880 /*
4881 * Call ss->fork(). This must happen after @child is linked on
4882 * css_set; otherwise, @child might change state between ->fork()
4883 * and addition to css_set.
4884 */
4885 if (need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004886 for_each_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07004887 if (ss->fork)
4888 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07004889 }
Paul Menage817929e2007-10-18 23:39:36 -07004890}
Tejun Heo5edee612012-10-16 15:03:14 -07004891
Paul Menage817929e2007-10-18 23:39:36 -07004892/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004893 * cgroup_exit - detach cgroup from exiting task
4894 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004895 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004896 *
4897 * Description: Detach cgroup from @tsk and release it.
4898 *
4899 * Note that cgroups marked notify_on_release force every task in
4900 * them to take the global cgroup_mutex mutex when exiting.
4901 * This could impact scaling on very large systems. Be reluctant to
4902 * use notify_on_release cgroups where very high task exit scaling
4903 * is required on large systems.
4904 *
4905 * the_top_cgroup_hack:
4906 *
4907 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4908 *
4909 * We call cgroup_exit() while the task is still competent to
4910 * handle notify_on_release(), then leave the task attached to the
4911 * root cgroup in each hierarchy for the remainder of its exit.
4912 *
4913 * To do this properly, we would increment the reference count on
4914 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4915 * code we would add a second cgroup function call, to drop that
4916 * reference. This would just create an unnecessary hot spot on
4917 * the top_cgroup reference count, to no avail.
4918 *
4919 * Normally, holding a reference to a cgroup without bumping its
4920 * count is unsafe. The cgroup could go away, or someone could
4921 * attach us to a different cgroup, decrementing the count on
4922 * the first cgroup that we never incremented. But in this case,
4923 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004924 * which wards off any cgroup_attach_task() attempts, or task is a failed
4925 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004926 */
4927void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4928{
Tejun Heo30159ec2013-06-25 11:53:37 -07004929 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07004930 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004931 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004932
4933 /*
4934 * Unlink from the css_set task list if necessary.
4935 * Optimistically check cg_list before taking
4936 * css_set_lock
4937 */
4938 if (!list_empty(&tsk->cg_list)) {
4939 write_lock(&css_set_lock);
4940 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004941 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004942 write_unlock(&css_set_lock);
4943 }
4944
Paul Menageb4f48b62007-10-18 23:39:33 -07004945 /* Reassign the task to the init_css_set. */
4946 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004947 cset = task_css_set(tsk);
4948 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004949
4950 if (run_callbacks && need_forkexit_callback) {
Tejun Heo3ed80a62014-02-08 10:36:58 -05004951 /* see cgroup_post_fork() for details */
4952 for_each_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004953 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04004954 struct cgroup_subsys_state *old_css = cset->subsys[i];
4955 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07004956
Tejun Heoeb954192013-08-08 20:11:23 -04004957 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004958 }
4959 }
4960 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004961 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004962
Tejun Heo5abb8852013-06-12 21:04:49 -07004963 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07004964}
Paul Menage697f4162007-10-18 23:39:34 -07004965
Paul Menagebd89aab2007-10-18 23:40:44 -07004966static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004967{
Li Zefanf50daa72013-03-01 15:06:07 +08004968 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004969 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08004970 /*
4971 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004972 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004973 * it now
4974 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004975 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004976
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004977 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07004978 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07004979 list_empty(&cgrp->release_list)) {
4980 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004981 need_schedule_work = 1;
4982 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004983 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004984 if (need_schedule_work)
4985 schedule_work(&release_agent_work);
4986 }
4987}
4988
Paul Menage81a6a5c2007-10-18 23:39:38 -07004989/*
4990 * Notify userspace when a cgroup is released, by running the
4991 * configured release agent with the name of the cgroup (path
4992 * relative to the root of cgroup file system) as the argument.
4993 *
4994 * Most likely, this user command will try to rmdir this cgroup.
4995 *
4996 * This races with the possibility that some other task will be
4997 * attached to this cgroup before it is removed, or that some other
4998 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4999 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5000 * unused, and this cgroup will be reprieved from its death sentence,
5001 * to continue to serve a useful existence. Next time it's released,
5002 * we will get notified again, if it still has 'notify_on_release' set.
5003 *
5004 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5005 * means only wait until the task is successfully execve()'d. The
5006 * separate release agent task is forked by call_usermodehelper(),
5007 * then control in this thread returns here, without waiting for the
5008 * release agent task. We don't bother to wait because the caller of
5009 * this routine has no use for the exit status of the release agent
5010 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005011 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005012static void cgroup_release_agent(struct work_struct *work)
5013{
5014 BUG_ON(work != &release_agent_work);
5015 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005016 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005017 while (!list_empty(&release_list)) {
5018 char *argv[3], *envp[3];
5019 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07005020 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005021 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005022 struct cgroup,
5023 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005024 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005025 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005026 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07005027 if (!pathbuf)
5028 goto continue_free;
5029 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5030 goto continue_free;
5031 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5032 if (!agentbuf)
5033 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005034
5035 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07005036 argv[i++] = agentbuf;
5037 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005038 argv[i] = NULL;
5039
5040 i = 0;
5041 /* minimal command environment */
5042 envp[i++] = "HOME=/";
5043 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5044 envp[i] = NULL;
5045
5046 /* Drop the lock while we invoke the usermode helper,
5047 * since the exec could involve hitting disk and hence
5048 * be a slow process */
5049 mutex_unlock(&cgroup_mutex);
5050 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07005052 continue_free:
5053 kfree(pathbuf);
5054 kfree(agentbuf);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005055 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005056 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005057 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005058 mutex_unlock(&cgroup_mutex);
5059}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005060
5061static int __init cgroup_disable(char *str)
5062{
Tejun Heo30159ec2013-06-25 11:53:37 -07005063 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005064 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005065 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005066
5067 while ((token = strsep(&str, ",")) != NULL) {
5068 if (!*token)
5069 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005070
Tejun Heo3ed80a62014-02-08 10:36:58 -05005071 for_each_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005072 if (!strcmp(token, ss->name)) {
5073 ss->disabled = 1;
5074 printk(KERN_INFO "Disabling %s control group"
5075 " subsystem\n", ss->name);
5076 break;
5077 }
5078 }
5079 }
5080 return 1;
5081}
5082__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005083
Tejun Heob77d7b62013-08-13 11:01:54 -04005084/**
Tejun Heo5a17f542014-02-11 11:52:47 -05005085 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
Tejun Heo35cf0832013-08-26 18:40:56 -04005086 * @dentry: directory dentry of interest
5087 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005088 *
Tejun Heo5a17f542014-02-11 11:52:47 -05005089 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5090 * to get the corresponding css and return it. If such css doesn't exist
5091 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005092 */
Tejun Heo5a17f542014-02-11 11:52:47 -05005093struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
5094 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005095{
5096 struct cgroup *cgrp;
Tejun Heo5a17f542014-02-11 11:52:47 -05005097 struct cgroup_subsys_state *css;
Tejun Heob77d7b62013-08-13 11:01:54 -04005098
Tejun Heo35cf0832013-08-26 18:40:56 -04005099 /* is @dentry a cgroup dir? */
5100 if (!dentry->d_inode ||
5101 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005102 return ERR_PTR(-EBADF);
5103
Tejun Heo5a17f542014-02-11 11:52:47 -05005104 rcu_read_lock();
5105
Tejun Heo35cf0832013-08-26 18:40:56 -04005106 cgrp = __d_cgrp(dentry);
Tejun Heo5a17f542014-02-11 11:52:47 -05005107 css = cgroup_css(cgrp, ss);
5108
5109 if (!css || !css_tryget(css))
5110 css = ERR_PTR(-ENOENT);
5111
5112 rcu_read_unlock();
5113 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005114}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005115
Li Zefan1cb650b2013-08-19 10:05:24 +08005116/**
5117 * css_from_id - lookup css by id
5118 * @id: the cgroup id
5119 * @ss: cgroup subsys to be looked into
5120 *
5121 * Returns the css if there's valid one with @id, otherwise returns NULL.
5122 * Should be called under rcu_read_lock().
5123 */
5124struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5125{
5126 struct cgroup *cgrp;
5127
Tejun Heoace2bee2014-02-11 11:52:47 -05005128 cgroup_assert_mutexes_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005129
5130 cgrp = idr_find(&ss->root->cgroup_idr, id);
5131 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005132 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005133 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005134}
5135
Paul Menagefe693432009-09-23 15:56:20 -07005136#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005137static struct cgroup_subsys_state *
5138debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005139{
5140 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5141
5142 if (!css)
5143 return ERR_PTR(-ENOMEM);
5144
5145 return css;
5146}
5147
Tejun Heoeb954192013-08-08 20:11:23 -04005148static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005149{
Tejun Heoeb954192013-08-08 20:11:23 -04005150 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005151}
5152
Tejun Heo182446d2013-08-08 20:11:24 -04005153static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5154 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005155{
Tejun Heo182446d2013-08-08 20:11:24 -04005156 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005157}
5158
Tejun Heo182446d2013-08-08 20:11:24 -04005159static u64 current_css_set_read(struct cgroup_subsys_state *css,
5160 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005161{
5162 return (u64)(unsigned long)current->cgroups;
5163}
5164
Tejun Heo182446d2013-08-08 20:11:24 -04005165static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005166 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005167{
5168 u64 count;
5169
5170 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005171 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005172 rcu_read_unlock();
5173 return count;
5174}
5175
Tejun Heo2da8ca82013-12-05 12:28:04 -05005176static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005177{
Tejun Heo69d02062013-06-12 21:04:50 -07005178 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005179 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005180
5181 read_lock(&css_set_lock);
5182 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005183 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005184 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005185 struct cgroup *c = link->cgrp;
Tejun Heo59f52962014-02-11 11:52:49 -05005186 const char *name = "?";
Paul Menage7717f7b2009-09-23 15:56:22 -07005187
Tejun Heo59f52962014-02-11 11:52:49 -05005188 if (c != cgroup_dummy_top)
5189 name = cgroup_name(c);
5190
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005191 seq_printf(seq, "Root %d group %s\n",
5192 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005193 }
5194 rcu_read_unlock();
5195 read_unlock(&css_set_lock);
5196 return 0;
5197}
5198
5199#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005200static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005201{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005202 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005203 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005204
5205 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005206 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005207 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005208 struct task_struct *task;
5209 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005210 seq_printf(seq, "css_set %p\n", cset);
5211 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005212 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5213 seq_puts(seq, " ...\n");
5214 break;
5215 } else {
5216 seq_printf(seq, " task %d\n",
5217 task_pid_vnr(task));
5218 }
5219 }
5220 }
5221 read_unlock(&css_set_lock);
5222 return 0;
5223}
5224
Tejun Heo182446d2013-08-08 20:11:24 -04005225static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005226{
Tejun Heo182446d2013-08-08 20:11:24 -04005227 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005228}
5229
5230static struct cftype debug_files[] = {
5231 {
Paul Menagefe693432009-09-23 15:56:20 -07005232 .name = "taskcount",
5233 .read_u64 = debug_taskcount_read,
5234 },
5235
5236 {
5237 .name = "current_css_set",
5238 .read_u64 = current_css_set_read,
5239 },
5240
5241 {
5242 .name = "current_css_set_refcount",
5243 .read_u64 = current_css_set_refcount_read,
5244 },
5245
5246 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005247 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005248 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005249 },
5250
5251 {
5252 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005253 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005254 },
5255
5256 {
Paul Menagefe693432009-09-23 15:56:20 -07005257 .name = "releasable",
5258 .read_u64 = releasable_read,
5259 },
Paul Menagefe693432009-09-23 15:56:20 -07005260
Tejun Heo4baf6e32012-04-01 12:09:55 -07005261 { } /* terminate */
5262};
Paul Menagefe693432009-09-23 15:56:20 -07005263
Tejun Heo073219e2014-02-08 10:36:58 -05005264struct cgroup_subsys debug_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005265 .css_alloc = debug_css_alloc,
5266 .css_free = debug_css_free,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005267 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005268};
5269#endif /* CONFIG_CGROUP_DEBUG */