blob: 9d05c3ca2d5e3cb9cfaebe123a885e4ddd6fd522 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
Joe Perchesed3d2612014-04-25 18:28:03 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Tejun Heo0a268db2016-12-27 14:49:06 -050031#include "cgroup-internal.h"
32
Jakub Kicinskiaef2fed2021-12-15 18:55:37 -080033#include <linux/bpf-cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100034#include <linux/cred.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100036#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070037#include <linux/kernel.h>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080038#include <linux/magic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070039#include <linux/mutex.h>
40#include <linux/mount.h>
41#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070042#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/rcupdate.h>
44#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +010045#include <linux/sched/task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070046#include <linux/slab.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070047#include <linux/spinlock.h>
Tejun Heo1ed13282015-09-16 12:53:17 -040048#include <linux/percpu-rwsem.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070049#include <linux/string.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080050#include <linux/hashtable.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070051#include <linux/idr.h>
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020052#include <linux/kthread.h>
Arun Sharma600634972011-07-26 16:09:06 -070053#include <linux/atomic.h>
Tejun Heoe93ad192016-01-19 12:18:41 -050054#include <linux/cpuset.h>
Aditya Kalia79a9082016-01-29 02:54:06 -060055#include <linux/proc_ns.h>
56#include <linux/nsproxy.h>
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -070057#include <linux/file.h>
Al Viroe34a98d2019-01-17 00:22:58 -050058#include <linux/fs_parser.h>
Tejun Heod4ff7492018-04-26 14:29:04 -070059#include <linux/sched/cputime.h>
Johannes Weiner2ce71352018-10-26 15:06:31 -070060#include <linux/psi.h>
Tejun Heobd1060a2015-12-07 17:38:53 -050061#include <net/sock.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070062
Tejun Heoed1777d2016-08-10 11:23:44 -040063#define CREATE_TRACE_POINTS
64#include <trace/events/cgroup.h>
65
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050066#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
67 MAX_CFTYPE_NAME + 2)
Tejun Heob12e3582018-04-26 14:29:04 -070068/* let's not notify more than 100 times per second */
69#define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
Tejun Heo8d7e6fb2014-02-11 11:52:48 -050070
Tejun Heob1a21362013-11-29 10:42:58 -050071/*
Kees Cookd20d30e2021-08-27 17:02:55 -070072 * To avoid confusing the compiler (and generating warnings) with code
73 * that attempts to access what would be a 0-element array (i.e. sized
74 * to a potentially empty array when CGROUP_SUBSYS_COUNT == 0), this
75 * constant expression can be added.
76 */
77#define CGROUP_HAS_SUBSYS_CONFIG (CGROUP_SUBSYS_COUNT > 0)
78
79/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080080 * cgroup_mutex is the master lock. Any modification to cgroup or its
81 * hierarchy must be performed while holding it.
82 *
Tejun Heof0d9a5f2015-10-15 16:41:53 -040083 * css_set_lock protects task->cgroups pointer, the list of css_set
Tejun Heo0e1d7682014-02-25 10:04:03 -050084 * objects, and the chain of tasks off each css_set.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080085 *
Tejun Heo0e1d7682014-02-25 10:04:03 -050086 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
87 * cgroup.h can use them for lockdep annotations.
Tejun Heoe25e2cb2011-12-12 18:12:21 -080088 */
Tejun Heo22194492013-04-07 09:29:51 -070089DEFINE_MUTEX(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040090DEFINE_SPINLOCK(css_set_lock);
Tejun Heo0a268db2016-12-27 14:49:06 -050091
92#ifdef CONFIG_PROVE_RCU
Tejun Heo0e1d7682014-02-25 10:04:03 -050093EXPORT_SYMBOL_GPL(cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -040094EXPORT_SYMBOL_GPL(css_set_lock);
Tejun Heo22194492013-04-07 09:29:51 -070095#endif
96
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -040097DEFINE_SPINLOCK(trace_cgroup_path_lock);
98char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
Waiman Long5cf81142018-11-08 10:08:46 -050099bool cgroup_debug __read_mostly;
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -0400100
Tejun Heo69e943b2014-02-08 10:36:58 -0500101/*
Tejun Heo15a4c832014-05-04 15:09:14 -0400102 * Protects cgroup_idr and css_idr so that IDs can be released without
103 * grabbing cgroup_mutex.
Tejun Heo6fa49182014-05-04 15:09:13 -0400104 */
105static DEFINE_SPINLOCK(cgroup_idr_lock);
106
107/*
Tejun Heo34c06252015-11-05 00:12:24 -0500108 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
109 * against file removal/re-creation across css hiding.
110 */
111static DEFINE_SPINLOCK(cgroup_file_kn_lock);
112
Oleg Nesterov3f2947b2019-04-23 18:32:41 +0200113DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem);
Tejun Heo1ed13282015-09-16 12:53:17 -0400114
Tejun Heo8353da12014-05-13 12:19:23 -0400115#define cgroup_assert_mutex_or_rcu_locked() \
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700116 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
117 !lockdep_is_held(&cgroup_mutex), \
Tejun Heo8353da12014-05-13 12:19:23 -0400118 "cgroup_mutex or RCU read lock required");
Tejun Heo780cd8b2013-12-06 15:11:56 -0500119
Ben Blumaae8aab2010-03-10 15:22:07 -0800120/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500121 * cgroup destruction makes heavy use of work items and there can be a lot
122 * of concurrent destructions. Use a separate workqueue so that cgroup
123 * destruction work items don't end up filling up max_active of system_wq
124 * which may lead to deadlock.
125 */
126static struct workqueue_struct *cgroup_destroy_wq;
127
Tejun Heo3ed80a62014-02-08 10:36:58 -0500128/* generate an array of cgroup subsystem pointers */
Tejun Heo073219e2014-02-08 10:36:58 -0500129#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
Tejun Heo0a268db2016-12-27 14:49:06 -0500130struct cgroup_subsys *cgroup_subsys[] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700131#include <linux/cgroup_subsys.h>
132};
Tejun Heo073219e2014-02-08 10:36:58 -0500133#undef SUBSYS
134
135/* array of cgroup subsystem names */
136#define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
137static const char *cgroup_subsys_name[] = {
138#include <linux/cgroup_subsys.h>
139};
140#undef SUBSYS
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141
Tejun Heo49d1dc42015-09-18 11:56:28 -0400142/* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
143#define SUBSYS(_x) \
144 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
145 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
146 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
147 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
148#include <linux/cgroup_subsys.h>
149#undef SUBSYS
150
151#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
152static struct static_key_true *cgroup_subsys_enabled_key[] = {
153#include <linux/cgroup_subsys.h>
154};
155#undef SUBSYS
156
157#define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
158static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
159#include <linux/cgroup_subsys.h>
160};
161#undef SUBSYS
162
Tejun Heoc58632b2018-04-26 14:29:04 -0700163static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
Tejun Heo041cd642017-09-25 08:12:05 -0700164
Zefan Li6b6ebb32020-05-13 10:13:11 +0800165/* the default hierarchy */
Tejun Heoc58632b2018-04-26 14:29:04 -0700166struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };
Tejun Heod0ec4232015-08-05 16:03:19 -0400167EXPORT_SYMBOL_GPL(cgrp_dfl_root);
Tejun Heo9871bf92013-06-24 15:21:47 -0700168
Tejun Heoa2dd4242014-03-19 10:23:55 -0400169/*
170 * The default hierarchy always exists but is hidden until mounted for the
171 * first time. This is for backward compatibility.
172 */
Tejun Heoa7165262016-02-23 10:00:50 -0500173static bool cgrp_dfl_visible;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700174
Tejun Heo5533e012014-05-14 19:33:07 -0400175/* some controllers are not supported in the default hierarchy */
Tejun Heoa7165262016-02-23 10:00:50 -0500176static u16 cgrp_dfl_inhibit_ss_mask;
Tejun Heo5533e012014-05-14 19:33:07 -0400177
Tejun Heof6d635ad2016-03-08 11:51:26 -0500178/* some controllers are implicitly enabled on the default hierarchy */
Tejun Heob8074212017-01-20 12:06:08 -0500179static u16 cgrp_dfl_implicit_ss_mask;
Tejun Heof6d635ad2016-03-08 11:51:26 -0500180
Tejun Heo8cfd8142017-07-21 11:14:51 -0400181/* some controllers can be threaded on the default hierarchy */
182static u16 cgrp_dfl_threaded_ss_mask;
183
Paul Menageddbcc7e2007-10-18 23:39:30 -0700184/* The list of hierarchy roots */
Tejun Heo0a268db2016-12-27 14:49:06 -0500185LIST_HEAD(cgroup_roots);
Tejun Heo9871bf92013-06-24 15:21:47 -0700186static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187
Tejun Heo3417ae12014-02-08 10:37:01 -0500188/* hierarchy ID allocation and mapping, protected by cgroup_mutex */
Tejun Heo1a574232013-04-14 11:36:58 -0700189static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700190
Li Zefan794611a2013-06-18 18:53:53 +0800191/*
Tejun Heo0cb51d72014-05-16 13:22:49 -0400192 * Assign a monotonically increasing serial number to csses. It guarantees
193 * cgroups with bigger numbers are newer than those with smaller numbers.
194 * Also, as csses are always appended to the parent's ->children list, it
195 * guarantees that sibling csses are always sorted in the ascending serial
196 * number order on the list. Protected by cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800197 */
Tejun Heo0cb51d72014-05-16 13:22:49 -0400198static u64 css_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800199
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000200/*
Tejun Heob8074212017-01-20 12:06:08 -0500201 * These bitmasks identify subsystems with specific features to avoid
202 * having to do iterative checks repeatedly.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700203 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500204static u16 have_fork_callback __read_mostly;
205static u16 have_exit_callback __read_mostly;
Oleg Nesterov51bee5a2019-01-28 17:00:13 +0100206static u16 have_release_callback __read_mostly;
Tejun Heob8074212017-01-20 12:06:08 -0500207static u16 have_canfork_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700208
Aditya Kalia79a9082016-01-29 02:54:06 -0600209/* cgroup namespace for init task */
210struct cgroup_namespace init_cgroup_ns = {
Kirill Tkhaif3878822020-08-03 13:16:50 +0300211 .ns.count = REFCOUNT_INIT(2),
Aditya Kalia79a9082016-01-29 02:54:06 -0600212 .user_ns = &init_user_ns,
213 .ns.ops = &cgroupns_operations,
214 .ns.inum = PROC_CGROUP_INIT_INO,
215 .root_cset = &init_css_set,
216};
217
Tejun Heo67e9c742015-11-16 11:13:34 -0500218static struct file_system_type cgroup2_fs_type;
Tejun Heod62beb72016-12-27 14:49:08 -0500219static struct cftype cgroup_base_files[];
Tejun Heo628f7cd2013-06-28 16:24:11 -0700220
Suren Baghdasaryan3958e2d2021-05-24 12:53:39 -0700221/* cgroup optional features */
222enum cgroup_opt_features {
223#ifdef CONFIG_PSI
224 OPT_FEATURE_PRESSURE,
225#endif
226 OPT_FEATURE_COUNT
227};
228
229static const char *cgroup_opt_feature_names[OPT_FEATURE_COUNT] = {
230#ifdef CONFIG_PSI
231 "pressure",
232#endif
233};
234
235static u16 cgroup_feature_disable_mask __read_mostly;
236
Tejun Heo334c3672016-03-03 09:58:01 -0500237static int cgroup_apply_control(struct cgroup *cgrp);
238static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
Tejun Heob636fd32019-05-31 10:38:58 -0700239static void css_task_iter_skip(struct css_task_iter *it,
240 struct task_struct *task);
Tejun Heo42809dd2012-11-19 08:13:37 -0800241static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -0500242static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
243 struct cgroup_subsys *ss);
Tejun Heo9d755d32014-05-14 09:15:02 -0400244static void css_release(struct percpu_ref *ref);
Tejun Heof8f22e52014-04-23 11:13:16 -0400245static void kill_css(struct cgroup_subsys_state *css);
Tejun Heo4df8dc92015-09-18 17:54:23 -0400246static int cgroup_addrm_files(struct cgroup_subsys_state *css,
247 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -0400248 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800249
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400250/**
251 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
252 * @ssid: subsys ID of interest
253 *
254 * cgroup_subsys_enabled() can only be used with literal subsys names which
255 * is fine for individual subsystems but unsuitable for cgroup core. This
256 * is slower static_key_enabled() based test indexed by @ssid.
257 */
Tejun Heo0a268db2016-12-27 14:49:06 -0500258bool cgroup_ssid_enabled(int ssid)
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400259{
Kees Cookd20d30e2021-08-27 17:02:55 -0700260 if (!CGROUP_HAS_SUBSYS_CONFIG)
Arnd Bergmanncfe02a82016-03-15 00:21:06 +0100261 return false;
262
Tejun Heofc5ed1e2015-09-18 11:56:28 -0400263 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
264}
265
Tejun Heo9e10a132015-09-18 11:56:28 -0400266/**
267 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
268 * @cgrp: the cgroup of interest
269 *
270 * The default hierarchy is the v2 interface of cgroup and this function
271 * can be used to test whether a cgroup is on the default hierarchy for
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +0530272 * cases where a subsystem should behave differently depending on the
Tejun Heo9e10a132015-09-18 11:56:28 -0400273 * interface version.
274 *
Tejun Heo9e10a132015-09-18 11:56:28 -0400275 * List of changed behaviors:
276 *
277 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
278 * and "name" are disallowed.
279 *
280 * - When mounting an existing superblock, mount options should match.
281 *
282 * - Remount is disallowed.
283 *
284 * - rename(2) is disallowed.
285 *
286 * - "tasks" is removed. Everything should be at process granularity. Use
287 * "cgroup.procs" instead.
288 *
289 * - "cgroup.procs" is not sorted. pids will be unique unless they got
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +0530290 * recycled in-between reads.
Tejun Heo9e10a132015-09-18 11:56:28 -0400291 *
292 * - "release_agent" and "notify_on_release" are removed. Replacement
293 * notification mechanism will be implemented.
294 *
295 * - "cgroup.clone_children" is removed.
296 *
297 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
298 * and its descendants contain no task; otherwise, 1. The file also
299 * generates kernfs notification which can be monitored through poll and
300 * [di]notify when the value of the file changes.
301 *
302 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
303 * take masks of ancestors with non-empty cpus/mems, instead of being
304 * moved to an ancestor.
305 *
306 * - cpuset: a task can be moved into an empty cpuset, and again it takes
307 * masks of ancestors.
308 *
Tejun Heo9e10a132015-09-18 11:56:28 -0400309 * - blkcg: blk-throttle becomes properly hierarchical.
310 *
311 * - debug: disallowed on the default hierarchy.
312 */
Tejun Heo0a268db2016-12-27 14:49:06 -0500313bool cgroup_on_dfl(const struct cgroup *cgrp)
Tejun Heo9e10a132015-09-18 11:56:28 -0400314{
315 return cgrp->root == &cgrp_dfl_root;
316}
317
Tejun Heo6fa49182014-05-04 15:09:13 -0400318/* IDR wrappers which synchronize using cgroup_idr_lock */
319static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
320 gfp_t gfp_mask)
321{
322 int ret;
323
324 idr_preload(gfp_mask);
Tejun Heo54504e92014-05-13 12:10:59 -0400325 spin_lock_bh(&cgroup_idr_lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800326 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
Tejun Heo54504e92014-05-13 12:10:59 -0400327 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400328 idr_preload_end();
329 return ret;
330}
331
332static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
333{
334 void *ret;
335
Tejun Heo54504e92014-05-13 12:10:59 -0400336 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400337 ret = idr_replace(idr, ptr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400338 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400339 return ret;
340}
341
342static void cgroup_idr_remove(struct idr *idr, int id)
343{
Tejun Heo54504e92014-05-13 12:10:59 -0400344 spin_lock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400345 idr_remove(idr, id);
Tejun Heo54504e92014-05-13 12:10:59 -0400346 spin_unlock_bh(&cgroup_idr_lock);
Tejun Heo6fa49182014-05-04 15:09:13 -0400347}
348
Tejun Heo27f26752017-07-16 21:44:18 -0400349static bool cgroup_has_tasks(struct cgroup *cgrp)
Tejun Heod51f39b2014-05-16 13:22:48 -0400350{
Tejun Heo27f26752017-07-16 21:44:18 -0400351 return cgrp->nr_populated_csets;
352}
Tejun Heod51f39b2014-05-16 13:22:48 -0400353
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400354bool cgroup_is_threaded(struct cgroup *cgrp)
Tejun Heo454000a2017-05-15 09:34:02 -0400355{
356 return cgrp->dom_cgrp != cgrp;
357}
358
Tejun Heo8cfd8142017-07-21 11:14:51 -0400359/* can @cgrp host both domain and threaded children? */
360static bool cgroup_is_mixable(struct cgroup *cgrp)
361{
362 /*
363 * Root isn't under domain level resource control exempting it from
364 * the no-internal-process constraint, so it can serve as a thread
365 * root and a parent of resource domains at the same time.
366 */
367 return !cgroup_parent(cgrp);
368}
369
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +0530370/* can @cgrp become a thread root? Should always be true for a thread root */
Tejun Heo8cfd8142017-07-21 11:14:51 -0400371static bool cgroup_can_be_thread_root(struct cgroup *cgrp)
372{
373 /* mixables don't care */
374 if (cgroup_is_mixable(cgrp))
375 return true;
376
377 /* domain roots can't be nested under threaded */
378 if (cgroup_is_threaded(cgrp))
379 return false;
380
381 /* can only have either domain or threaded children */
382 if (cgrp->nr_populated_domain_children)
383 return false;
384
385 /* and no domain controllers can be enabled */
386 if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
387 return false;
388
389 return true;
390}
391
392/* is @cgrp root of a threaded subtree? */
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400393bool cgroup_is_thread_root(struct cgroup *cgrp)
Tejun Heo8cfd8142017-07-21 11:14:51 -0400394{
395 /* thread root should be a domain */
396 if (cgroup_is_threaded(cgrp))
397 return false;
398
399 /* a domain w/ threaded children is a thread root */
400 if (cgrp->nr_threaded_children)
401 return true;
402
403 /*
404 * A domain which has tasks and explicit threaded controllers
405 * enabled is a thread root.
406 */
407 if (cgroup_has_tasks(cgrp) &&
408 (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask))
409 return true;
410
411 return false;
412}
413
414/* a domain which isn't connected to the root w/o brekage can't be used */
415static bool cgroup_is_valid_domain(struct cgroup *cgrp)
416{
417 /* the cgroup itself can be a thread root */
418 if (cgroup_is_threaded(cgrp))
419 return false;
420
421 /* but the ancestors can't be unless mixable */
422 while ((cgrp = cgroup_parent(cgrp))) {
423 if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp))
424 return false;
425 if (cgroup_is_threaded(cgrp))
426 return false;
427 }
428
429 return true;
Tejun Heod51f39b2014-05-16 13:22:48 -0400430}
431
Tejun Heo5531dc92016-03-03 09:57:58 -0500432/* subsystems visibly enabled on a cgroup */
433static u16 cgroup_control(struct cgroup *cgrp)
434{
435 struct cgroup *parent = cgroup_parent(cgrp);
436 u16 root_ss_mask = cgrp->root->subsys_mask;
437
Tejun Heo8cfd8142017-07-21 11:14:51 -0400438 if (parent) {
439 u16 ss_mask = parent->subtree_control;
440
441 /* threaded cgroups can only have threaded controllers */
442 if (cgroup_is_threaded(cgrp))
443 ss_mask &= cgrp_dfl_threaded_ss_mask;
444 return ss_mask;
445 }
Tejun Heo5531dc92016-03-03 09:57:58 -0500446
447 if (cgroup_on_dfl(cgrp))
Tejun Heof6d635ad2016-03-08 11:51:26 -0500448 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
449 cgrp_dfl_implicit_ss_mask);
Tejun Heo5531dc92016-03-03 09:57:58 -0500450 return root_ss_mask;
451}
452
453/* subsystems enabled on a cgroup */
454static u16 cgroup_ss_mask(struct cgroup *cgrp)
455{
456 struct cgroup *parent = cgroup_parent(cgrp);
457
Tejun Heo8cfd8142017-07-21 11:14:51 -0400458 if (parent) {
459 u16 ss_mask = parent->subtree_ss_mask;
460
461 /* threaded cgroups can only have threaded controllers */
462 if (cgroup_is_threaded(cgrp))
463 ss_mask &= cgrp_dfl_threaded_ss_mask;
464 return ss_mask;
465 }
Tejun Heo5531dc92016-03-03 09:57:58 -0500466
467 return cgrp->root->subsys_mask;
468}
469
Tejun Heo95109b62013-08-08 20:11:27 -0400470/**
471 * cgroup_css - obtain a cgroup's css for the specified subsystem
472 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400473 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heo95109b62013-08-08 20:11:27 -0400474 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400475 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
476 * function must be called either under cgroup_mutex or rcu_read_lock() and
477 * the caller is responsible for pinning the returned css if it wants to
478 * keep accessing it outside the said locks. This function may return
479 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400480 */
481static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400482 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400483{
Kees Cookd20d30e2021-08-27 17:02:55 -0700484 if (CGROUP_HAS_SUBSYS_CONFIG && ss)
Tejun Heoaec25022014-02-08 10:36:58 -0500485 return rcu_dereference_check(cgrp->subsys[ss->id],
Tejun Heoace2bee2014-02-11 11:52:47 -0500486 lockdep_is_held(&cgroup_mutex));
Tejun Heoca8bdca2013-08-26 18:40:56 -0400487 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400488 return &cgrp->self;
Tejun Heo95109b62013-08-08 20:11:27 -0400489}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700490
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400491/**
Tejun Heod41bf8c2017-10-23 16:18:27 -0700492 * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
493 * @cgrp: the cgroup of interest
494 * @ss: the subsystem of interest
495 *
Zhen Lei08b2b6f2021-05-24 16:29:43 +0800496 * Find and get @cgrp's css associated with @ss. If the css doesn't exist
Tejun Heod41bf8c2017-10-23 16:18:27 -0700497 * or is offline, %NULL is returned.
498 */
499static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp,
500 struct cgroup_subsys *ss)
501{
502 struct cgroup_subsys_state *css;
503
504 rcu_read_lock();
505 css = cgroup_css(cgrp, ss);
Peng Wanga5815632019-07-03 10:07:49 +0800506 if (css && !css_tryget_online(css))
Tejun Heod41bf8c2017-10-23 16:18:27 -0700507 css = NULL;
508 rcu_read_unlock();
509
510 return css;
511}
512
513/**
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500514 * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400515 * @cgrp: the cgroup of interest
Tejun Heo9d800df2014-05-14 09:15:00 -0400516 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400517 *
Chen Hanxiaod0f702e2015-04-23 07:57:33 -0400518 * Similar to cgroup_css() but returns the effective css, which is defined
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400519 * as the matching css of the nearest ancestor including self which has @ss
520 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
521 * function is guaranteed to return non-NULL css.
522 */
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500523static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp,
524 struct cgroup_subsys *ss)
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400525{
526 lockdep_assert_held(&cgroup_mutex);
527
528 if (!ss)
Tejun Heo9d800df2014-05-14 09:15:00 -0400529 return &cgrp->self;
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400530
Tejun Heoeeecbd12014-11-18 02:49:52 -0500531 /*
532 * This function is used while updating css associations and thus
Tejun Heo5531dc92016-03-03 09:57:58 -0500533 * can't test the csses directly. Test ss_mask.
Tejun Heoeeecbd12014-11-18 02:49:52 -0500534 */
Tejun Heo5531dc92016-03-03 09:57:58 -0500535 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
Tejun Heod51f39b2014-05-16 13:22:48 -0400536 cgrp = cgroup_parent(cgrp);
Tejun Heo5531dc92016-03-03 09:57:58 -0500537 if (!cgrp)
538 return NULL;
539 }
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400540
541 return cgroup_css(cgrp, ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700542}
543
Tejun Heoeeecbd12014-11-18 02:49:52 -0500544/**
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500545 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
546 * @cgrp: the cgroup of interest
547 * @ss: the subsystem of interest
548 *
549 * Find and get the effective css of @cgrp for @ss. The effective css is
550 * defined as the matching css of the nearest ancestor including self which
551 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
552 * the root css is returned, so this function always returns a valid css.
553 *
554 * The returned css is not guaranteed to be online, and therefore it is the
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +0530555 * callers responsibility to try get a reference for it.
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500556 */
557struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
558 struct cgroup_subsys *ss)
559{
560 struct cgroup_subsys_state *css;
561
Kees Cookd20d30e2021-08-27 17:02:55 -0700562 if (!CGROUP_HAS_SUBSYS_CONFIG)
563 return NULL;
564
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500565 do {
566 css = cgroup_css(cgrp, ss);
567
568 if (css)
569 return css;
570 cgrp = cgroup_parent(cgrp);
571 } while (cgrp);
572
573 return init_css_set.subsys[ss->id];
574}
575
576/**
Tejun Heoeeecbd12014-11-18 02:49:52 -0500577 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
578 * @cgrp: the cgroup of interest
579 * @ss: the subsystem of interest
580 *
581 * Find and get the effective css of @cgrp for @ss. The effective css is
582 * defined as the matching css of the nearest ancestor including self which
583 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
584 * the root css is returned, so this function always returns a valid css.
585 * The returned css must be put using css_put().
586 */
587struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
588 struct cgroup_subsys *ss)
589{
590 struct cgroup_subsys_state *css;
591
Kees Cookd20d30e2021-08-27 17:02:55 -0700592 if (!CGROUP_HAS_SUBSYS_CONFIG)
593 return NULL;
594
Tejun Heoeeecbd12014-11-18 02:49:52 -0500595 rcu_read_lock();
596
597 do {
598 css = cgroup_css(cgrp, ss);
599
600 if (css && css_tryget_online(css))
601 goto out_unlock;
602 cgrp = cgroup_parent(cgrp);
603 } while (cgrp);
604
605 css = init_css_set.subsys[ss->id];
606 css_get(css);
607out_unlock:
608 rcu_read_unlock();
609 return css;
610}
Dan Schatzbergc74d40e2021-06-28 19:38:21 -0700611EXPORT_SYMBOL_GPL(cgroup_get_e_css);
Tejun Heoeeecbd12014-11-18 02:49:52 -0500612
Tejun Heoa590b902017-04-28 15:14:55 -0400613static void cgroup_get_live(struct cgroup *cgrp)
Tejun Heo052c3f32015-10-15 16:41:50 -0400614{
615 WARN_ON_ONCE(cgroup_is_dead(cgrp));
616 css_get(&cgrp->self);
617}
618
Roman Gushchinaade7f92019-04-19 10:03:02 -0700619/**
620 * __cgroup_task_count - count the number of tasks in a cgroup. The caller
621 * is responsible for taking the css_set_lock.
622 * @cgrp: the cgroup in question
623 */
624int __cgroup_task_count(const struct cgroup *cgrp)
625{
626 int count = 0;
627 struct cgrp_cset_link *link;
628
629 lockdep_assert_held(&css_set_lock);
630
631 list_for_each_entry(link, &cgrp->cset_links, cset_link)
632 count += link->cset->nr_tasks;
633
634 return count;
635}
636
637/**
638 * cgroup_task_count - count the number of tasks in a cgroup.
639 * @cgrp: the cgroup in question
640 */
641int cgroup_task_count(const struct cgroup *cgrp)
642{
643 int count;
644
645 spin_lock_irq(&css_set_lock);
646 count = __cgroup_task_count(cgrp);
647 spin_unlock_irq(&css_set_lock);
648
649 return count;
650}
651
Tejun Heob4168642014-05-13 12:16:21 -0400652struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
Tejun Heo59f52962014-02-11 11:52:49 -0500653{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500654 struct cgroup *cgrp = of->kn->parent->priv;
Tejun Heob4168642014-05-13 12:16:21 -0400655 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -0500656
657 /*
658 * This is open and unprotected implementation of cgroup_css().
659 * seq_css() is only called from a kernfs file operation which has
660 * an active reference on the file. Because all the subsystem
661 * files are drained before a css is disassociated with a cgroup,
662 * the matching css from the cgroup's subsys table is guaranteed to
663 * be and stay valid until the enclosing operation is complete.
664 */
Kees Cookd20d30e2021-08-27 17:02:55 -0700665 if (CGROUP_HAS_SUBSYS_CONFIG && cft->ss)
Tejun Heo2bd59d42014-02-11 11:52:49 -0500666 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
667 else
Tejun Heo9d800df2014-05-14 09:15:00 -0400668 return &cgrp->self;
Tejun Heo59f52962014-02-11 11:52:49 -0500669}
Tejun Heob4168642014-05-13 12:16:21 -0400670EXPORT_SYMBOL_GPL(of_css);
Tejun Heo59f52962014-02-11 11:52:49 -0500671
Tejun Heo30159ec2013-06-25 11:53:37 -0700672/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500673 * for_each_css - iterate all css's of a cgroup
674 * @css: the iteration cursor
675 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
676 * @cgrp: the target cgroup to iterate css's of
Tejun Heo30159ec2013-06-25 11:53:37 -0700677 *
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400678 * Should be called under cgroup_[tree_]mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700679 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500680#define for_each_css(css, ssid, cgrp) \
681 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
682 if (!((css) = rcu_dereference_check( \
683 (cgrp)->subsys[(ssid)], \
684 lockdep_is_held(&cgroup_mutex)))) { } \
685 else
686
687/**
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400688 * for_each_e_css - iterate all effective css's of a cgroup
689 * @css: the iteration cursor
690 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
691 * @cgrp: the target cgroup to iterate css's of
692 *
693 * Should be called under cgroup_[tree_]mutex.
694 */
Dennis Zhoufc5a8282018-12-05 12:10:36 -0500695#define for_each_e_css(css, ssid, cgrp) \
696 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
697 if (!((css) = cgroup_e_css_by_mask(cgrp, \
698 cgroup_subsys[(ssid)]))) \
699 ; \
Tejun Heoaec3dfc2014-04-23 11:13:14 -0400700 else
701
702/**
Tejun Heob4e0eea2016-02-22 22:25:46 -0500703 * do_each_subsys_mask - filter for_each_subsys with a bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000704 * @ss: the iteration cursor
705 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heob4e0eea2016-02-22 22:25:46 -0500706 * @ss_mask: the bitmask
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000707 *
708 * The block will only run for cases where the ssid-th bit (1 << ssid) of
Tejun Heob4e0eea2016-02-22 22:25:46 -0500709 * @ss_mask is set.
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000710 */
Tejun Heob4e0eea2016-02-22 22:25:46 -0500711#define do_each_subsys_mask(ss, ssid, ss_mask) do { \
712 unsigned long __ss_mask = (ss_mask); \
Kees Cookd20d30e2021-08-27 17:02:55 -0700713 if (!CGROUP_HAS_SUBSYS_CONFIG) { \
Aleksa Sarai4a705c52015-06-09 21:32:07 +1000714 (ssid) = 0; \
Tejun Heob4e0eea2016-02-22 22:25:46 -0500715 break; \
716 } \
717 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
718 (ss) = cgroup_subsys[ssid]; \
719 {
720
721#define while_each_subsys_mask() \
722 } \
723 } \
724} while (false)
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000725
Tejun Heof8f22e52014-04-23 11:13:16 -0400726/* iterate over child cgrps, lock should be held throughout iteration */
727#define cgroup_for_each_live_child(child, cgrp) \
Tejun Heod5c419b2014-05-16 13:22:48 -0400728 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
Tejun Heo8353da12014-05-13 12:19:23 -0400729 if (({ lockdep_assert_held(&cgroup_mutex); \
Tejun Heof8f22e52014-04-23 11:13:16 -0400730 cgroup_is_dead(child); })) \
731 ; \
732 else
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700733
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +0530734/* walk live descendants in pre order */
Tejun Heoce3f1d92016-03-03 09:57:59 -0500735#define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
736 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
737 if (({ lockdep_assert_held(&cgroup_mutex); \
738 (dsct) = (d_css)->cgroup; \
739 cgroup_is_dead(dsct); })) \
740 ; \
741 else
742
743/* walk live descendants in postorder */
744#define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
745 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
746 if (({ lockdep_assert_held(&cgroup_mutex); \
747 (dsct) = (d_css)->cgroup; \
748 cgroup_is_dead(dsct); })) \
749 ; \
750 else
751
Tejun Heo172a2c062014-03-19 10:23:53 -0400752/*
753 * The default css_set - used by init and its children prior to any
Paul Menage817929e2007-10-18 23:39:36 -0700754 * hierarchies being mounted. It contains a pointer to the root state
755 * for each subsystem. Also used to anchor the list of css_sets. Not
756 * reference-counted, to improve performance when child cgroups
757 * haven't been created.
758 */
Tejun Heo5024ae22014-05-07 21:31:17 -0400759struct css_set init_css_set = {
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200760 .refcount = REFCOUNT_INIT(1),
Tejun Heo454000a2017-05-15 09:34:02 -0400761 .dom_cset = &init_css_set,
Tejun Heo172a2c062014-03-19 10:23:53 -0400762 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
763 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
Tejun Heoc03cd772019-05-31 10:38:58 -0700764 .dying_tasks = LIST_HEAD_INIT(init_css_set.dying_tasks),
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500765 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
Tejun Heo454000a2017-05-15 09:34:02 -0400766 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500767 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
Tejun Heo172a2c062014-03-19 10:23:53 -0400768 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
769 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
Tejun Heo38683142017-09-25 13:50:20 -0700770
771 /*
772 * The following field is re-initialized when this cset gets linked
773 * in cgroup_init(). However, let's initialize the field
774 * statically too so that the default cgroup can be accessed safely
775 * early during boot.
776 */
777 .dfl_cgrp = &cgrp_dfl_root.cgrp,
Tejun Heo172a2c062014-03-19 10:23:53 -0400778};
Paul Menage817929e2007-10-18 23:39:36 -0700779
Tejun Heo172a2c062014-03-19 10:23:53 -0400780static int css_set_count = 1; /* 1 for init_css_set */
Paul Menage817929e2007-10-18 23:39:36 -0700781
Tejun Heo454000a2017-05-15 09:34:02 -0400782static bool css_set_threaded(struct css_set *cset)
783{
784 return cset->dom_cset != cset;
785}
786
Tejun Heo842b5972014-04-25 18:28:02 -0400787/**
Tejun Heo0de09422015-10-15 16:41:49 -0400788 * css_set_populated - does a css_set contain any tasks?
789 * @cset: target css_set
Waiman Long73a72422017-06-13 17:18:01 -0400790 *
791 * css_set_populated() should be the same as !!cset->nr_tasks at steady
792 * state. However, css_set_populated() can be called while a task is being
793 * added to or removed from the linked list before the nr_tasks is
794 * properly updated. Hence, we can't just look at ->nr_tasks here.
Tejun Heo0de09422015-10-15 16:41:49 -0400795 */
796static bool css_set_populated(struct css_set *cset)
797{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400798 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400799
800 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
801}
802
803/**
Tejun Heo788b9502017-07-16 21:43:33 -0400804 * cgroup_update_populated - update the populated count of a cgroup
Tejun Heo842b5972014-04-25 18:28:02 -0400805 * @cgrp: the target cgroup
806 * @populated: inc or dec populated count
807 *
Tejun Heo0de09422015-10-15 16:41:49 -0400808 * One of the css_sets associated with @cgrp is either getting its first
Tejun Heo788b9502017-07-16 21:43:33 -0400809 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
810 * count is propagated towards root so that a given cgroup's
811 * nr_populated_children is zero iff none of its descendants contain any
812 * tasks.
Tejun Heo842b5972014-04-25 18:28:02 -0400813 *
Tejun Heo788b9502017-07-16 21:43:33 -0400814 * @cgrp's interface file "cgroup.populated" is zero if both
815 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
816 * 1 otherwise. When the sum changes from or to zero, userland is notified
817 * that the content of the interface file has changed. This can be used to
818 * detect when @cgrp and its descendants become populated or empty.
Tejun Heo842b5972014-04-25 18:28:02 -0400819 */
820static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
821{
Tejun Heo788b9502017-07-16 21:43:33 -0400822 struct cgroup *child = NULL;
823 int adj = populated ? 1 : -1;
824
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400825 lockdep_assert_held(&css_set_lock);
Tejun Heo842b5972014-04-25 18:28:02 -0400826
827 do {
Tejun Heo788b9502017-07-16 21:43:33 -0400828 bool was_populated = cgroup_is_populated(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400829
Tejun Heo454000a2017-05-15 09:34:02 -0400830 if (!child) {
Tejun Heo788b9502017-07-16 21:43:33 -0400831 cgrp->nr_populated_csets += adj;
Tejun Heo454000a2017-05-15 09:34:02 -0400832 } else {
833 if (cgroup_is_threaded(child))
834 cgrp->nr_populated_threaded_children += adj;
835 else
836 cgrp->nr_populated_domain_children += adj;
837 }
Tejun Heo842b5972014-04-25 18:28:02 -0400838
Tejun Heo788b9502017-07-16 21:43:33 -0400839 if (was_populated == cgroup_is_populated(cgrp))
Tejun Heo842b5972014-04-25 18:28:02 -0400840 break;
841
Tejun Heod62beb72016-12-27 14:49:08 -0500842 cgroup1_check_for_release(cgrp);
Roman Gushchin4c476d82019-04-19 10:03:08 -0700843 TRACE_CGROUP_PATH(notify_populated, cgrp,
844 cgroup_is_populated(cgrp));
Tejun Heo6f60ead2015-09-18 17:54:23 -0400845 cgroup_file_notify(&cgrp->events_file);
846
Tejun Heo788b9502017-07-16 21:43:33 -0400847 child = cgrp;
Tejun Heod51f39b2014-05-16 13:22:48 -0400848 cgrp = cgroup_parent(cgrp);
Tejun Heo842b5972014-04-25 18:28:02 -0400849 } while (cgrp);
850}
851
Tejun Heo0de09422015-10-15 16:41:49 -0400852/**
853 * css_set_update_populated - update populated state of a css_set
854 * @cset: target css_set
855 * @populated: whether @cset is populated or depopulated
856 *
857 * @cset is either getting the first task or losing the last. Update the
Tejun Heo788b9502017-07-16 21:43:33 -0400858 * populated counters of all associated cgroups accordingly.
Tejun Heo0de09422015-10-15 16:41:49 -0400859 */
860static void css_set_update_populated(struct css_set *cset, bool populated)
861{
862 struct cgrp_cset_link *link;
863
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400864 lockdep_assert_held(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -0400865
866 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
867 cgroup_update_populated(link->cgrp, populated);
868}
869
Tejun Heob636fd32019-05-31 10:38:58 -0700870/*
871 * @task is leaving, advance task iterators which are pointing to it so
872 * that they can resume at the next position. Advancing an iterator might
873 * remove it from the list, use safe walk. See css_task_iter_skip() for
874 * details.
875 */
876static void css_set_skip_task_iters(struct css_set *cset,
877 struct task_struct *task)
878{
879 struct css_task_iter *it, *pos;
880
881 list_for_each_entry_safe(it, pos, &cset->task_iters, iters_node)
882 css_task_iter_skip(it, task);
883}
884
Tejun Heof6d7d042015-10-15 16:41:52 -0400885/**
886 * css_set_move_task - move a task from one css_set to another
887 * @task: task being moved
888 * @from_cset: css_set @task currently belongs to (may be NULL)
889 * @to_cset: new css_set @task is being moved to (may be NULL)
890 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
891 *
892 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
893 * css_set, @from_cset can be NULL. If @task is being disassociated
894 * instead of moved, @to_cset can be NULL.
895 *
Tejun Heo788b9502017-07-16 21:43:33 -0400896 * This function automatically handles populated counter updates and
Tejun Heoed27b9f2015-10-15 16:41:52 -0400897 * css_task_iter adjustments but the caller is responsible for managing
898 * @from_cset and @to_cset's reference counts.
Tejun Heof6d7d042015-10-15 16:41:52 -0400899 */
900static void css_set_move_task(struct task_struct *task,
901 struct css_set *from_cset, struct css_set *to_cset,
902 bool use_mg_tasks)
903{
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400904 lockdep_assert_held(&css_set_lock);
Tejun Heof6d7d042015-10-15 16:41:52 -0400905
Tejun Heo20b454a2016-03-03 09:57:57 -0500906 if (to_cset && !css_set_populated(to_cset))
907 css_set_update_populated(to_cset, true);
908
Tejun Heof6d7d042015-10-15 16:41:52 -0400909 if (from_cset) {
910 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoed27b9f2015-10-15 16:41:52 -0400911
Tejun Heob636fd32019-05-31 10:38:58 -0700912 css_set_skip_task_iters(from_cset, task);
Tejun Heof6d7d042015-10-15 16:41:52 -0400913 list_del_init(&task->cg_list);
914 if (!css_set_populated(from_cset))
915 css_set_update_populated(from_cset, false);
916 } else {
917 WARN_ON_ONCE(!list_empty(&task->cg_list));
918 }
919
920 if (to_cset) {
921 /*
922 * We are synchronized through cgroup_threadgroup_rwsem
923 * against PF_EXITING setting such that we can't race
Michal Koutnýe7c7b1d2019-10-04 12:57:39 +0200924 * against cgroup_exit()/cgroup_free() dropping the css_set.
Tejun Heof6d7d042015-10-15 16:41:52 -0400925 */
926 WARN_ON_ONCE(task->flags & PF_EXITING);
927
Johannes Weiner2ce71352018-10-26 15:06:31 -0700928 cgroup_move_task(task, to_cset);
Tejun Heof6d7d042015-10-15 16:41:52 -0400929 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
930 &to_cset->tasks);
931 }
932}
933
Paul Menage7717f7b2009-09-23 15:56:22 -0700934/*
935 * hash table for cgroup groups. This improves the performance to find
936 * an existing css_set. This hash doesn't (currently) take into
937 * account cgroups in empty hierarchies.
938 */
Li Zefan472b1052008-04-29 01:00:11 -0700939#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800940static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700941
Li Zefan0ac801f2013-01-10 11:49:27 +0800942static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700943{
Li Zefan0ac801f2013-01-10 11:49:27 +0800944 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700945 struct cgroup_subsys *ss;
946 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700947
Tejun Heo30159ec2013-06-25 11:53:37 -0700948 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800949 key += (unsigned long)css[i];
950 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700951
Li Zefan0ac801f2013-01-10 11:49:27 +0800952 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700953}
954
Tejun Heodcfe1492016-12-27 14:49:09 -0500955void put_css_set_locked(struct css_set *cset)
Paul Menageb4f48b62007-10-18 23:39:33 -0700956{
Tejun Heo69d02062013-06-12 21:04:50 -0700957 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo2d8f2432014-04-23 11:13:15 -0400958 struct cgroup_subsys *ss;
959 int ssid;
Tejun Heo5abb8852013-06-12 21:04:49 -0700960
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400961 lockdep_assert_held(&css_set_lock);
Tejun Heo89c55092014-02-13 06:58:40 -0500962
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200963 if (!refcount_dec_and_test(&cset->refcount))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700964 return;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700965
Tejun Heo454000a2017-05-15 09:34:02 -0400966 WARN_ON_ONCE(!list_empty(&cset->threaded_csets));
967
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +0530968 /* This css_set is dead. Unlink it and release cgroup and css refs */
Tejun Heo53254f92015-11-23 14:55:41 -0500969 for_each_subsys(ss, ssid) {
Tejun Heo2d8f2432014-04-23 11:13:15 -0400970 list_del(&cset->e_cset_node[ssid]);
Tejun Heo53254f92015-11-23 14:55:41 -0500971 css_put(cset->subsys[ssid]);
972 }
Tejun Heo5abb8852013-06-12 21:04:49 -0700973 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700974 css_set_count--;
975
Tejun Heo69d02062013-06-12 21:04:50 -0700976 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700977 list_del(&link->cset_link);
978 list_del(&link->cgrp_link);
Tejun Heo2ceb2312015-10-15 16:41:51 -0400979 if (cgroup_parent(link->cgrp))
980 cgroup_put(link->cgrp);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700981 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700982 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700983
Tejun Heo454000a2017-05-15 09:34:02 -0400984 if (css_set_threaded(cset)) {
985 list_del(&cset->threaded_csets_node);
986 put_css_set_locked(cset->dom_cset);
987 }
988
Tejun Heo5abb8852013-06-12 21:04:49 -0700989 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700990}
991
Tejun Heob326f9d2013-06-24 15:21:48 -0700992/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700993 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700994 * @cset: candidate css_set being tested
995 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700996 * @new_cgrp: cgroup that's being entered by the task
997 * @template: desired set of css pointers in css_set (pre-calculated)
998 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800999 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -07001000 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
1001 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001002static bool compare_css_sets(struct css_set *cset,
1003 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -07001004 struct cgroup *new_cgrp,
1005 struct cgroup_subsys_state *template[])
1006{
Tejun Heo454000a2017-05-15 09:34:02 -04001007 struct cgroup *new_dfl_cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001008 struct list_head *l1, *l2;
1009
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001010 /*
1011 * On the default hierarchy, there can be csets which are
1012 * associated with the same set of cgroups but different csses.
1013 * Let's first ensure that csses match.
1014 */
1015 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
Paul Menage7717f7b2009-09-23 15:56:22 -07001016 return false;
Paul Menage7717f7b2009-09-23 15:56:22 -07001017
Tejun Heo454000a2017-05-15 09:34:02 -04001018
1019 /* @cset's domain should match the default cgroup's */
1020 if (cgroup_on_dfl(new_cgrp))
1021 new_dfl_cgrp = new_cgrp;
1022 else
1023 new_dfl_cgrp = old_cset->dfl_cgrp;
1024
1025 if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp)
1026 return false;
1027
Paul Menage7717f7b2009-09-23 15:56:22 -07001028 /*
1029 * Compare cgroup pointers in order to distinguish between
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001030 * different cgroups in hierarchies. As different cgroups may
1031 * share the same effective css, this comparison is always
1032 * necessary.
Paul Menage7717f7b2009-09-23 15:56:22 -07001033 */
Tejun Heo69d02062013-06-12 21:04:50 -07001034 l1 = &cset->cgrp_links;
1035 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -07001036 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -07001037 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -07001038 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -07001039
1040 l1 = l1->next;
1041 l2 = l2->next;
1042 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -07001043 if (l1 == &cset->cgrp_links) {
1044 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -07001045 break;
1046 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001047 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -07001048 }
1049 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -07001050 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
1051 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
1052 cgrp1 = link1->cgrp;
1053 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001054 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -07001055 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -07001056
1057 /*
1058 * If this hierarchy is the hierarchy of the cgroup
1059 * that's changing, then we need to check that this
1060 * css_set points to the new cgroup; if it's any other
1061 * hierarchy, then this css_set should point to the
1062 * same cgroup as the old css_set.
1063 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001064 if (cgrp1->root == new_cgrp->root) {
1065 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -07001066 return false;
1067 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -07001068 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -07001069 return false;
1070 }
1071 }
1072 return true;
1073}
1074
Tejun Heob326f9d2013-06-24 15:21:48 -07001075/**
1076 * find_existing_css_set - init css array and find the matching css_set
1077 * @old_cset: the css_set that we're using before the cgroup transition
1078 * @cgrp: the cgroup that we're moving into
1079 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -07001080 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001081static struct css_set *find_existing_css_set(struct css_set *old_cset,
1082 struct cgroup *cgrp,
1083 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -07001084{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001085 struct cgroup_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -07001086 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07001087 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08001088 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -07001089 int i;
Paul Menage817929e2007-10-18 23:39:36 -07001090
Ben Blumaae8aab2010-03-10 15:22:07 -08001091 /*
1092 * Build the set of subsystem state objects that we want to see in the
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +05301093 * new css_set. While subsystems can change globally, the entries here
Ben Blumaae8aab2010-03-10 15:22:07 -08001094 * won't change, so no need for locking.
1095 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001096 for_each_subsys(ss, i) {
Tejun Heof392e512014-04-23 11:13:14 -04001097 if (root->subsys_mask & (1UL << i)) {
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001098 /*
1099 * @ss is in this hierarchy, so we want the
1100 * effective css from @cgrp.
1101 */
Dennis Zhoufc5a8282018-12-05 12:10:36 -05001102 template[i] = cgroup_e_css_by_mask(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -07001103 } else {
Tejun Heoaec3dfc2014-04-23 11:13:14 -04001104 /*
1105 * @ss is not in this hierarchy, so we don't want
1106 * to change the css.
1107 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001108 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -07001109 }
1110 }
1111
Li Zefan0ac801f2013-01-10 11:49:27 +08001112 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -07001113 hash_for_each_possible(css_set_table, cset, hlist, key) {
1114 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -07001115 continue;
1116
1117 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -07001118 return cset;
Li Zefan472b1052008-04-29 01:00:11 -07001119 }
Paul Menage817929e2007-10-18 23:39:36 -07001120
1121 /* No existing cgroup group matched */
1122 return NULL;
1123}
1124
Tejun Heo69d02062013-06-12 21:04:50 -07001125static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -07001126{
Tejun Heo69d02062013-06-12 21:04:50 -07001127 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001128
Tejun Heo69d02062013-06-12 21:04:50 -07001129 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
1130 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -07001131 kfree(link);
1132 }
1133}
1134
Tejun Heo69d02062013-06-12 21:04:50 -07001135/**
1136 * allocate_cgrp_cset_links - allocate cgrp_cset_links
1137 * @count: the number of links to allocate
1138 * @tmp_links: list_head the allocated links are put on
1139 *
1140 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
1141 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -07001142 */
Tejun Heo69d02062013-06-12 21:04:50 -07001143static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -07001144{
Tejun Heo69d02062013-06-12 21:04:50 -07001145 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -07001146 int i;
Tejun Heo69d02062013-06-12 21:04:50 -07001147
1148 INIT_LIST_HEAD(tmp_links);
1149
Li Zefan36553432008-07-29 22:33:19 -07001150 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -07001151 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -07001152 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -07001153 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -07001154 return -ENOMEM;
1155 }
Tejun Heo69d02062013-06-12 21:04:50 -07001156 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -07001157 }
1158 return 0;
1159}
1160
Li Zefanc12f65d2009-01-07 18:07:42 -08001161/**
1162 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -07001163 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -07001164 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -08001165 * @cgrp: the destination cgroup
1166 */
Tejun Heo69d02062013-06-12 21:04:50 -07001167static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
1168 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -08001169{
Tejun Heo69d02062013-06-12 21:04:50 -07001170 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -08001171
Tejun Heo69d02062013-06-12 21:04:50 -07001172 BUG_ON(list_empty(tmp_links));
Tejun Heo6803c002014-04-23 11:13:16 -04001173
1174 if (cgroup_on_dfl(cgrp))
1175 cset->dfl_cgrp = cgrp;
1176
Tejun Heo69d02062013-06-12 21:04:50 -07001177 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
1178 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07001179 link->cgrp = cgrp;
Tejun Heo842b5972014-04-25 18:28:02 -04001180
Paul Menage7717f7b2009-09-23 15:56:22 -07001181 /*
Tejun Heo389b9c12015-10-15 16:41:51 -04001182 * Always add links to the tail of the lists so that the lists are
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +05301183 * in chronological order.
Paul Menage7717f7b2009-09-23 15:56:22 -07001184 */
Tejun Heo389b9c12015-10-15 16:41:51 -04001185 list_move_tail(&link->cset_link, &cgrp->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07001186 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Tejun Heo2ceb2312015-10-15 16:41:51 -04001187
1188 if (cgroup_parent(cgrp))
Tejun Heoa590b902017-04-28 15:14:55 -04001189 cgroup_get_live(cgrp);
Li Zefanc12f65d2009-01-07 18:07:42 -08001190}
1191
Tejun Heob326f9d2013-06-24 15:21:48 -07001192/**
1193 * find_css_set - return a new css_set with one cgroup updated
1194 * @old_cset: the baseline css_set
1195 * @cgrp: the cgroup to be updated
1196 *
1197 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1198 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -07001199 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001200static struct css_set *find_css_set(struct css_set *old_cset,
1201 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -07001202{
Tejun Heob326f9d2013-06-24 15:21:48 -07001203 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -07001204 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -07001205 struct list_head tmp_links;
1206 struct cgrp_cset_link *link;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001207 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08001208 unsigned long key;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001209 int ssid;
Li Zefan472b1052008-04-29 01:00:11 -07001210
Tejun Heob326f9d2013-06-24 15:21:48 -07001211 lockdep_assert_held(&cgroup_mutex);
1212
Paul Menage817929e2007-10-18 23:39:36 -07001213 /* First see if we already have a cgroup group that matches
1214 * the desired set */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001215 spin_lock_irq(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001216 cset = find_existing_css_set(old_cset, cgrp, template);
1217 if (cset)
1218 get_css_set(cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001219 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001220
Tejun Heo5abb8852013-06-12 21:04:49 -07001221 if (cset)
1222 return cset;
Paul Menage817929e2007-10-18 23:39:36 -07001223
Tejun Heof4f4be22013-06-12 21:04:51 -07001224 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -07001225 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -07001226 return NULL;
1227
Tejun Heo69d02062013-06-12 21:04:50 -07001228 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -07001229 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -07001230 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -07001231 return NULL;
1232 }
1233
Elena Reshetova4b9502e62017-03-08 10:00:40 +02001234 refcount_set(&cset->refcount, 1);
Tejun Heo454000a2017-05-15 09:34:02 -04001235 cset->dom_cset = cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07001236 INIT_LIST_HEAD(&cset->tasks);
Tejun Heoc7561122014-02-25 10:04:01 -05001237 INIT_LIST_HEAD(&cset->mg_tasks);
Tejun Heoc03cd772019-05-31 10:38:58 -07001238 INIT_LIST_HEAD(&cset->dying_tasks);
Tejun Heoed27b9f2015-10-15 16:41:52 -04001239 INIT_LIST_HEAD(&cset->task_iters);
Tejun Heo454000a2017-05-15 09:34:02 -04001240 INIT_LIST_HEAD(&cset->threaded_csets);
Tejun Heo5abb8852013-06-12 21:04:49 -07001241 INIT_HLIST_NODE(&cset->hlist);
Tejun Heo5f617ebb2016-12-27 14:49:05 -05001242 INIT_LIST_HEAD(&cset->cgrp_links);
1243 INIT_LIST_HEAD(&cset->mg_preload_node);
1244 INIT_LIST_HEAD(&cset->mg_node);
Paul Menage817929e2007-10-18 23:39:36 -07001245
1246 /* Copy the set of subsystem state objects generated in
1247 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -07001248 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -07001249
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001250 spin_lock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001251 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -07001252 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001253 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001254
Paul Menage7717f7b2009-09-23 15:56:22 -07001255 if (c->root == cgrp->root)
1256 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001257 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -07001258 }
Paul Menage817929e2007-10-18 23:39:36 -07001259
Tejun Heo69d02062013-06-12 21:04:50 -07001260 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -07001261
Paul Menage817929e2007-10-18 23:39:36 -07001262 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -07001263
Tejun Heo2d8f2432014-04-23 11:13:15 -04001264 /* Add @cset to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -07001265 key = css_set_hash(cset->subsys);
1266 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -07001267
Tejun Heo53254f92015-11-23 14:55:41 -05001268 for_each_subsys(ss, ssid) {
1269 struct cgroup_subsys_state *css = cset->subsys[ssid];
1270
Tejun Heo2d8f2432014-04-23 11:13:15 -04001271 list_add_tail(&cset->e_cset_node[ssid],
Tejun Heo53254f92015-11-23 14:55:41 -05001272 &css->cgroup->e_csets[ssid]);
1273 css_get(css);
1274 }
Tejun Heo2d8f2432014-04-23 11:13:15 -04001275
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001276 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07001277
Tejun Heo454000a2017-05-15 09:34:02 -04001278 /*
1279 * If @cset should be threaded, look up the matching dom_cset and
1280 * link them up. We first fully initialize @cset then look for the
1281 * dom_cset. It's simpler this way and safe as @cset is guaranteed
1282 * to stay empty until we return.
1283 */
1284 if (cgroup_is_threaded(cset->dfl_cgrp)) {
1285 struct css_set *dcset;
1286
1287 dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp);
1288 if (!dcset) {
1289 put_css_set(cset);
1290 return NULL;
1291 }
1292
1293 spin_lock_irq(&css_set_lock);
1294 cset->dom_cset = dcset;
1295 list_add_tail(&cset->threaded_csets_node,
1296 &dcset->threaded_csets);
1297 spin_unlock_irq(&css_set_lock);
1298 }
1299
Tejun Heo5abb8852013-06-12 21:04:49 -07001300 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -07001301}
1302
Tejun Heo0a268db2016-12-27 14:49:06 -05001303struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001304{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001305 struct cgroup *root_cgrp = kf_root->kn->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001306
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001307 return root_cgrp->root;
Tejun Heo2bd59d42014-02-11 11:52:49 -05001308}
1309
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001310static int cgroup_init_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001311{
1312 int id;
1313
1314 lockdep_assert_held(&cgroup_mutex);
1315
Tejun Heo985ed672014-03-19 10:23:53 -04001316 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
Tejun Heof2e85d52014-02-11 11:52:49 -05001317 if (id < 0)
1318 return id;
1319
1320 root->hierarchy_id = id;
1321 return 0;
1322}
1323
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001324static void cgroup_exit_root_id(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001325{
1326 lockdep_assert_held(&cgroup_mutex);
1327
Johannes Weiner8c8a5502016-06-17 12:23:59 -04001328 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heof2e85d52014-02-11 11:52:49 -05001329}
1330
Tejun Heo1592c9b2016-12-27 14:49:08 -05001331void cgroup_free_root(struct cgroup_root *root)
Tejun Heof2e85d52014-02-11 11:52:49 -05001332{
Tejun Heo74321032019-11-04 15:54:30 -08001333 kfree(root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001334}
1335
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001336static void cgroup_destroy_root(struct cgroup_root *root)
Tejun Heo59f52962014-02-11 11:52:49 -05001337{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001338 struct cgroup *cgrp = &root->cgrp;
Tejun Heof2e85d52014-02-11 11:52:49 -05001339 struct cgrp_cset_link *link, *tmp_link;
Tejun Heof2e85d52014-02-11 11:52:49 -05001340
Tejun Heoed1777d2016-08-10 11:23:44 -04001341 trace_cgroup_destroy_root(root);
1342
Tejun Heo334c3672016-03-03 09:58:01 -05001343 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
Tejun Heof2e85d52014-02-11 11:52:49 -05001344
Tejun Heo776f02f2014-02-12 09:29:50 -05001345 BUG_ON(atomic_read(&root->nr_cgrps));
Tejun Heod5c419b2014-05-16 13:22:48 -04001346 BUG_ON(!list_empty(&cgrp->self.children));
Tejun Heof2e85d52014-02-11 11:52:49 -05001347
Tejun Heof2e85d52014-02-11 11:52:49 -05001348 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo334c3672016-03-03 09:58:01 -05001349 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
Tejun Heof2e85d52014-02-11 11:52:49 -05001350
1351 /*
1352 * Release all the links from cset_links to this hierarchy's
1353 * root cgroup
1354 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001355 spin_lock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001356
1357 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1358 list_del(&link->cset_link);
1359 list_del(&link->cgrp_link);
1360 kfree(link);
1361 }
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001362
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001363 spin_unlock_irq(&css_set_lock);
Tejun Heof2e85d52014-02-11 11:52:49 -05001364
1365 if (!list_empty(&root->root_list)) {
1366 list_del(&root->root_list);
1367 cgroup_root_count--;
1368 }
1369
1370 cgroup_exit_root_id(root);
1371
1372 mutex_unlock(&cgroup_mutex);
Tejun Heof2e85d52014-02-11 11:52:49 -05001373
Johannes Weinera7df69b2021-04-29 22:56:20 -07001374 cgroup_rstat_exit(cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05001375 kernfs_destroy_root(root->kf_root);
Tejun Heof2e85d52014-02-11 11:52:49 -05001376 cgroup_free_root(root);
1377}
1378
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001379/*
1380 * look up cgroup associated with current task's cgroup namespace on the
1381 * specified hierarchy
1382 */
1383static struct cgroup *
1384current_cgns_cgroup_from_root(struct cgroup_root *root)
1385{
1386 struct cgroup *res = NULL;
1387 struct css_set *cset;
1388
1389 lockdep_assert_held(&css_set_lock);
1390
1391 rcu_read_lock();
1392
1393 cset = current->nsproxy->cgroup_ns->root_cset;
1394 if (cset == &init_css_set) {
1395 res = &root->cgrp;
Miaohe Lin61e867f2019-09-29 16:06:58 +08001396 } else if (root == &cgrp_dfl_root) {
1397 res = cset->dfl_cgrp;
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001398 } else {
1399 struct cgrp_cset_link *link;
1400
1401 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1402 struct cgroup *c = link->cgrp;
1403
1404 if (c->root == root) {
1405 res = c;
1406 break;
1407 }
1408 }
1409 }
1410 rcu_read_unlock();
1411
1412 BUG_ON(!res);
1413 return res;
1414}
1415
Tejun Heoceb6a082014-02-25 10:04:02 -05001416/* look up cgroup associated with given css_set on the specified hierarchy */
1417static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001418 struct cgroup_root *root)
Paul Menage7717f7b2009-09-23 15:56:22 -07001419{
Paul Menage7717f7b2009-09-23 15:56:22 -07001420 struct cgroup *res = NULL;
1421
Tejun Heo96d365e2014-02-13 06:58:40 -05001422 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001423 lockdep_assert_held(&css_set_lock);
Tejun Heo96d365e2014-02-13 06:58:40 -05001424
Tejun Heo5abb8852013-06-12 21:04:49 -07001425 if (cset == &init_css_set) {
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001426 res = &root->cgrp;
Tejun Heo13d82fb2017-08-02 15:39:38 -07001427 } else if (root == &cgrp_dfl_root) {
1428 res = cset->dfl_cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -07001429 } else {
Tejun Heo69d02062013-06-12 21:04:50 -07001430 struct cgrp_cset_link *link;
1431
1432 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07001433 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07001434
Paul Menage7717f7b2009-09-23 15:56:22 -07001435 if (c->root == root) {
1436 res = c;
1437 break;
1438 }
1439 }
1440 }
Tejun Heo96d365e2014-02-13 06:58:40 -05001441
Paul Menage7717f7b2009-09-23 15:56:22 -07001442 BUG_ON(!res);
1443 return res;
1444}
1445
1446/*
Tejun Heoceb6a082014-02-25 10:04:02 -05001447 * Return the cgroup for "task" from the given hierarchy. Must be
Tejun Heof0d9a5f2015-10-15 16:41:53 -04001448 * called with cgroup_mutex and css_set_lock held.
Tejun Heoceb6a082014-02-25 10:04:02 -05001449 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001450struct cgroup *task_cgroup_from_root(struct task_struct *task,
1451 struct cgroup_root *root)
Tejun Heoceb6a082014-02-25 10:04:02 -05001452{
1453 /*
Michal Koutnýe7c7b1d2019-10-04 12:57:39 +02001454 * No need to lock the task - since we hold css_set_lock the
1455 * task can't change groups.
Tejun Heoceb6a082014-02-25 10:04:02 -05001456 */
1457 return cset_cgroup_from_root(task_css_set(task), root);
1458}
1459
1460/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461 * A task must hold cgroup_mutex to modify cgroups.
1462 *
1463 * Any task can increment and decrement the count field without lock.
1464 * So in general, code holding cgroup_mutex can't rely on the count
1465 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -08001466 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -07001467 * means that no tasks are currently attached, therefore there is no
1468 * way a task attached to that cgroup can fork (the other way to
1469 * increment the count). So code holding cgroup_mutex can safely
1470 * assume that if the count is zero, it will stay zero. Similarly, if
1471 * a task holds cgroup_mutex on a cgroup with zero count, it
1472 * knows that the cgroup won't be removed, as cgroup_rmdir()
1473 * needs that mutex.
1474 *
Paul Menageddbcc7e2007-10-18 23:39:30 -07001475 * A cgroup can only be deleted if both its 'count' of using tasks
1476 * is zero, and its list of 'children' cgroups is empty. Since all
1477 * tasks in the system use _some_ cgroup, and since there is always at
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001478 * least one task in the system (init, pid == 1), therefore, root cgroup
Paul Menageddbcc7e2007-10-18 23:39:30 -07001479 * always has either children cgroups and/or using tasks. So we don't
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001480 * need a special hack to ensure that root cgroup cannot be deleted.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481 *
1482 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001483 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -07001484 */
1485
Tejun Heo2bd59d42014-02-11 11:52:49 -05001486static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
Paul Menagea4243162007-10-18 23:39:35 -07001487
Jens Axboecf892982019-06-10 03:35:41 -06001488static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1489 char *buf)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490{
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07001491 struct cgroup_subsys *ss = cft->ss;
1492
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001493 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
Tejun Heoc1bbd932018-11-13 12:06:41 -08001494 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
1495 const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : "";
1496
1497 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
1498 dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
Jens Axboecf892982019-06-10 03:35:41 -06001499 cft->name);
Tejun Heoc1bbd932018-11-13 12:06:41 -08001500 } else {
Jens Axboecf892982019-06-10 03:35:41 -06001501 strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
Tejun Heoc1bbd932018-11-13 12:06:41 -08001502 }
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05001503 return buf;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504}
1505
Tejun Heof2e85d52014-02-11 11:52:49 -05001506/**
1507 * cgroup_file_mode - deduce file mode of a control file
1508 * @cft: the control file in question
1509 *
Tejun Heo7dbdb192015-09-18 17:54:23 -04001510 * S_IRUGO for read, S_IWUSR for write.
Tejun Heof2e85d52014-02-11 11:52:49 -05001511 */
1512static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan65dff752013-03-01 15:01:56 +08001513{
Tejun Heof2e85d52014-02-11 11:52:49 -05001514 umode_t mode = 0;
Li Zefan65dff752013-03-01 15:01:56 +08001515
Tejun Heof2e85d52014-02-11 11:52:49 -05001516 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1517 mode |= S_IRUGO;
1518
Tejun Heo7dbdb192015-09-18 17:54:23 -04001519 if (cft->write_u64 || cft->write_s64 || cft->write) {
1520 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1521 mode |= S_IWUGO;
1522 else
1523 mode |= S_IWUSR;
1524 }
Tejun Heof2e85d52014-02-11 11:52:49 -05001525
1526 return mode;
Li Zefan65dff752013-03-01 15:01:56 +08001527}
1528
Tejun Heoa9746d82014-05-13 12:19:22 -04001529/**
Tejun Heo8699b772016-02-22 22:25:46 -05001530 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
Tejun Heo0f060de2014-11-18 02:49:50 -05001531 * @subtree_control: the new subtree_control mask to consider
Tejun Heo5ced2512016-03-03 09:58:01 -05001532 * @this_ss_mask: available subsystems
Tejun Heoaf0ba672014-07-08 18:02:57 -04001533 *
1534 * On the default hierarchy, a subsystem may request other subsystems to be
1535 * enabled together through its ->depends_on mask. In such cases, more
1536 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1537 *
Tejun Heo0f060de2014-11-18 02:49:50 -05001538 * This function calculates which subsystems need to be enabled if
Tejun Heo5ced2512016-03-03 09:58:01 -05001539 * @subtree_control is to be applied while restricted to @this_ss_mask.
Tejun Heoaf0ba672014-07-08 18:02:57 -04001540 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001541static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
Tejun Heo667c2492014-07-08 18:02:56 -04001542{
Tejun Heo6e5c8302016-02-22 22:25:47 -05001543 u16 cur_ss_mask = subtree_control;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001544 struct cgroup_subsys *ss;
1545 int ssid;
1546
1547 lockdep_assert_held(&cgroup_mutex);
1548
Tejun Heof6d635ad2016-03-08 11:51:26 -05001549 cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1550
Tejun Heoaf0ba672014-07-08 18:02:57 -04001551 while (true) {
Tejun Heo6e5c8302016-02-22 22:25:47 -05001552 u16 new_ss_mask = cur_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001553
Tejun Heob4e0eea2016-02-22 22:25:46 -05001554 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10001555 new_ss_mask |= ss->depends_on;
Tejun Heob4e0eea2016-02-22 22:25:46 -05001556 } while_each_subsys_mask();
Tejun Heoaf0ba672014-07-08 18:02:57 -04001557
1558 /*
1559 * Mask out subsystems which aren't available. This can
1560 * happen only if some depended-upon subsystems were bound
1561 * to non-default hierarchies.
1562 */
Tejun Heo5ced2512016-03-03 09:58:01 -05001563 new_ss_mask &= this_ss_mask;
Tejun Heoaf0ba672014-07-08 18:02:57 -04001564
1565 if (new_ss_mask == cur_ss_mask)
1566 break;
1567 cur_ss_mask = new_ss_mask;
1568 }
1569
Tejun Heo0f060de2014-11-18 02:49:50 -05001570 return cur_ss_mask;
1571}
1572
1573/**
Tejun Heoa9746d82014-05-13 12:19:22 -04001574 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1575 * @kn: the kernfs_node being serviced
1576 *
1577 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1578 * the method finishes if locking succeeded. Note that once this function
1579 * returns the cgroup returned by cgroup_kn_lock_live() may become
1580 * inaccessible any time. If the caller intends to continue to access the
1581 * cgroup, it should pin it before invoking this function.
1582 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001583void cgroup_kn_unlock(struct kernfs_node *kn)
Tejun Heoa9746d82014-05-13 12:19:22 -04001584{
1585 struct cgroup *cgrp;
1586
1587 if (kernfs_type(kn) == KERNFS_DIR)
1588 cgrp = kn->priv;
1589 else
1590 cgrp = kn->parent->priv;
1591
1592 mutex_unlock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001593
1594 kernfs_unbreak_active_protection(kn);
1595 cgroup_put(cgrp);
1596}
1597
1598/**
1599 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1600 * @kn: the kernfs_node being serviced
Tejun Heo945ba192016-03-03 09:58:00 -05001601 * @drain_offline: perform offline draining on the cgroup
Tejun Heoa9746d82014-05-13 12:19:22 -04001602 *
1603 * This helper is to be used by a cgroup kernfs method currently servicing
1604 * @kn. It breaks the active protection, performs cgroup locking and
1605 * verifies that the associated cgroup is alive. Returns the cgroup if
1606 * alive; otherwise, %NULL. A successful return should be undone by a
Tejun Heo945ba192016-03-03 09:58:00 -05001607 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1608 * cgroup is drained of offlining csses before return.
Tejun Heoa9746d82014-05-13 12:19:22 -04001609 *
1610 * Any cgroup kernfs method implementation which requires locking the
1611 * associated cgroup should use this helper. It avoids nesting cgroup
1612 * locking under kernfs active protection and allows all kernfs operations
1613 * including self-removal.
1614 */
Tejun Heo0a268db2016-12-27 14:49:06 -05001615struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
Tejun Heoa9746d82014-05-13 12:19:22 -04001616{
1617 struct cgroup *cgrp;
1618
1619 if (kernfs_type(kn) == KERNFS_DIR)
1620 cgrp = kn->priv;
1621 else
1622 cgrp = kn->parent->priv;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623
Tejun Heo2bd59d42014-02-11 11:52:49 -05001624 /*
Tejun Heo01f64742014-05-13 12:19:23 -04001625 * We're gonna grab cgroup_mutex which nests outside kernfs
Tejun Heoa9746d82014-05-13 12:19:22 -04001626 * active_ref. cgroup liveliness check alone provides enough
1627 * protection against removal. Ensure @cgrp stays accessible and
1628 * break the active_ref protection.
Tejun Heo2bd59d42014-02-11 11:52:49 -05001629 */
Li Zefanaa323622014-09-04 14:43:38 +08001630 if (!cgroup_tryget(cgrp))
1631 return NULL;
Tejun Heoa9746d82014-05-13 12:19:22 -04001632 kernfs_break_active_protection(kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633
Tejun Heo945ba192016-03-03 09:58:00 -05001634 if (drain_offline)
1635 cgroup_lock_and_drain_offline(cgrp);
1636 else
1637 mutex_lock(&cgroup_mutex);
Tejun Heoa9746d82014-05-13 12:19:22 -04001638
1639 if (!cgroup_is_dead(cgrp))
1640 return cgrp;
1641
1642 cgroup_kn_unlock(kn);
1643 return NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644}
1645
Li Zefan2739d3c2013-01-21 18:18:33 +08001646static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647{
Tejun Heo2bd59d42014-02-11 11:52:49 -05001648 char name[CGROUP_FILE_NAME_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649
Tejun Heo01f64742014-05-13 12:19:23 -04001650 lockdep_assert_held(&cgroup_mutex);
Tejun Heo34c06252015-11-05 00:12:24 -05001651
1652 if (cft->file_offset) {
1653 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1654 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1655
1656 spin_lock_irq(&cgroup_file_kn_lock);
1657 cfile->kn = NULL;
1658 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heob12e3582018-04-26 14:29:04 -07001659
1660 del_timer_sync(&cfile->notify_timer);
Tejun Heo34c06252015-11-05 00:12:24 -05001661 }
1662
Tejun Heo2bd59d42014-02-11 11:52:49 -05001663 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07001664}
1665
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001666/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001667 * css_clear_dir - remove subsys files in a cgroup directory
Zhen Lei08b2b6f2021-05-24 16:29:43 +08001668 * @css: target css
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001669 */
Tejun Heo334c3672016-03-03 09:58:01 -05001670static void css_clear_dir(struct cgroup_subsys_state *css)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001671{
Tejun Heo334c3672016-03-03 09:58:01 -05001672 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001673 struct cftype *cfts;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001674
Tejun Heo88cb04b2016-03-03 09:57:58 -05001675 if (!(css->flags & CSS_VISIBLE))
1676 return;
1677
1678 css->flags &= ~CSS_VISIBLE;
1679
Tejun Heo5faaf052018-04-26 14:29:04 -07001680 if (!css->ss) {
1681 if (cgroup_on_dfl(cgrp))
1682 cfts = cgroup_base_files;
1683 else
1684 cfts = cgroup1_base_files;
1685
Tejun Heo4df8dc92015-09-18 17:54:23 -04001686 cgroup_addrm_files(css, cgrp, cfts, false);
Tejun Heo5faaf052018-04-26 14:29:04 -07001687 } else {
1688 list_for_each_entry(cfts, &css->ss->cfts, node)
1689 cgroup_addrm_files(css, cgrp, cfts, false);
1690 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691}
1692
Tejun Heoccdca212015-09-18 17:54:23 -04001693/**
Tejun Heo4df8dc92015-09-18 17:54:23 -04001694 * css_populate_dir - create subsys files in a cgroup directory
1695 * @css: target css
Tejun Heoccdca212015-09-18 17:54:23 -04001696 *
1697 * On failure, no file is added.
1698 */
Tejun Heo334c3672016-03-03 09:58:01 -05001699static int css_populate_dir(struct cgroup_subsys_state *css)
Tejun Heoccdca212015-09-18 17:54:23 -04001700{
Tejun Heo334c3672016-03-03 09:58:01 -05001701 struct cgroup *cgrp = css->cgroup;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001702 struct cftype *cfts, *failed_cfts;
1703 int ret;
Tejun Heoccdca212015-09-18 17:54:23 -04001704
Tejun Heo03970d32016-03-03 09:58:00 -05001705 if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
Tejun Heo88cb04b2016-03-03 09:57:58 -05001706 return 0;
1707
Tejun Heo4df8dc92015-09-18 17:54:23 -04001708 if (!css->ss) {
1709 if (cgroup_on_dfl(cgrp))
Tejun Heod62beb72016-12-27 14:49:08 -05001710 cfts = cgroup_base_files;
Tejun Heo4df8dc92015-09-18 17:54:23 -04001711 else
Tejun Heod62beb72016-12-27 14:49:08 -05001712 cfts = cgroup1_base_files;
Tejun Heoccdca212015-09-18 17:54:23 -04001713
Tejun Heo5faaf052018-04-26 14:29:04 -07001714 ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1715 if (ret < 0)
1716 return ret;
1717 } else {
1718 list_for_each_entry(cfts, &css->ss->cfts, node) {
1719 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1720 if (ret < 0) {
1721 failed_cfts = cfts;
1722 goto err;
1723 }
Tejun Heoccdca212015-09-18 17:54:23 -04001724 }
1725 }
Tejun Heo88cb04b2016-03-03 09:57:58 -05001726
1727 css->flags |= CSS_VISIBLE;
1728
Tejun Heoccdca212015-09-18 17:54:23 -04001729 return 0;
1730err:
Tejun Heo4df8dc92015-09-18 17:54:23 -04001731 list_for_each_entry(cfts, &css->ss->cfts, node) {
1732 if (cfts == failed_cfts)
1733 break;
1734 cgroup_addrm_files(css, cgrp, cfts, false);
1735 }
Tejun Heoccdca212015-09-18 17:54:23 -04001736 return ret;
1737}
1738
Tejun Heo0a268db2016-12-27 14:49:06 -05001739int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740{
Tejun Heo1ada4832015-09-18 17:54:23 -04001741 struct cgroup *dcgrp = &dst_root->cgrp;
Tejun Heo30159ec2013-06-25 11:53:37 -07001742 struct cgroup_subsys *ss;
Tejun Heo2d8f2432014-04-23 11:13:15 -04001743 int ssid, i, ret;
Waiman Long7ee28532021-09-18 18:53:08 -04001744 u16 dfl_disable_ss_mask = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001745
Tejun Heoace2bee2014-02-11 11:52:47 -05001746 lockdep_assert_held(&cgroup_mutex);
Ben Blumaae8aab2010-03-10 15:22:07 -08001747
Tejun Heob4e0eea2016-02-22 22:25:46 -05001748 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heof6d635ad2016-03-08 11:51:26 -05001749 /*
1750 * If @ss has non-root csses attached to it, can't move.
1751 * If @ss is an implicit controller, it is exempt from this
1752 * rule and can be stolen.
1753 */
1754 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1755 !ss->implicit_on_dfl)
Tejun Heo3ed80a62014-02-08 10:36:58 -05001756 return -EBUSY;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757
Tejun Heo5df36032014-03-19 10:23:54 -04001758 /* can't move between two non-dummy roots either */
Tejun Heo7fd8c562014-04-23 11:13:16 -04001759 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
Tejun Heo5df36032014-03-19 10:23:54 -04001760 return -EBUSY;
Waiman Long7ee28532021-09-18 18:53:08 -04001761
1762 /*
1763 * Collect ssid's that need to be disabled from default
1764 * hierarchy.
1765 */
1766 if (ss->root == &cgrp_dfl_root)
1767 dfl_disable_ss_mask |= 1 << ssid;
1768
Tejun Heob4e0eea2016-02-22 22:25:46 -05001769 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770
Waiman Long7ee28532021-09-18 18:53:08 -04001771 if (dfl_disable_ss_mask) {
1772 struct cgroup *scgrp = &cgrp_dfl_root.cgrp;
1773
1774 /*
1775 * Controllers from default hierarchy that need to be rebound
1776 * are all disabled together in one go.
1777 */
1778 cgrp_dfl_root.subsys_mask &= ~dfl_disable_ss_mask;
1779 WARN_ON(cgroup_apply_control(scgrp));
1780 cgroup_finalize_control(scgrp, 0);
1781 }
1782
Tejun Heob4e0eea2016-02-22 22:25:46 -05001783 do_each_subsys_mask(ss, ssid, ss_mask) {
Tejun Heo1ada4832015-09-18 17:54:23 -04001784 struct cgroup_root *src_root = ss->root;
1785 struct cgroup *scgrp = &src_root->cgrp;
1786 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001787 struct css_set *cset;
Tejun Heo30159ec2013-06-25 11:53:37 -07001788
Tejun Heo1ada4832015-09-18 17:54:23 -04001789 WARN_ON(!css || cgroup_css(dcgrp, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001790
Waiman Long7ee28532021-09-18 18:53:08 -04001791 if (src_root != &cgrp_dfl_root) {
1792 /* disable from the source */
1793 src_root->subsys_mask &= ~(1 << ssid);
1794 WARN_ON(cgroup_apply_control(scgrp));
1795 cgroup_finalize_control(scgrp, 0);
1796 }
Tejun Heo4df8dc92015-09-18 17:54:23 -04001797
Tejun Heo334c3672016-03-03 09:58:01 -05001798 /* rebind */
Tejun Heo1ada4832015-09-18 17:54:23 -04001799 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1800 rcu_assign_pointer(dcgrp->subsys[ssid], css);
Tejun Heo5df36032014-03-19 10:23:54 -04001801 ss->root = dst_root;
Tejun Heo1ada4832015-09-18 17:54:23 -04001802 css->cgroup = dcgrp;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001803
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001804 spin_lock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001805 hash_for_each(css_set_table, i, cset, hlist)
1806 list_move_tail(&cset->e_cset_node[ss->id],
Tejun Heo1ada4832015-09-18 17:54:23 -04001807 &dcgrp->e_csets[ss->id]);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001808 spin_unlock_irq(&css_set_lock);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001809
Johannes Weinera7df69b2021-04-29 22:56:20 -07001810 if (ss->css_rstat_flush) {
1811 list_del_rcu(&css->rstat_css_node);
1812 list_add_rcu(&css->rstat_css_node,
1813 &dcgrp->rstat_css_list);
1814 }
1815
Tejun Heobd53d612014-04-23 11:13:16 -04001816 /* default hierarchy doesn't enable controllers by default */
Tejun Heof392e512014-04-23 11:13:14 -04001817 dst_root->subsys_mask |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001818 if (dst_root == &cgrp_dfl_root) {
1819 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1820 } else {
Tejun Heo1ada4832015-09-18 17:54:23 -04001821 dcgrp->subtree_control |= 1 << ssid;
Tejun Heo49d1dc42015-09-18 11:56:28 -04001822 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
Tejun Heo667c2492014-07-08 18:02:56 -04001823 }
Tejun Heo73e80ed2013-08-13 11:01:55 -04001824
Tejun Heo334c3672016-03-03 09:58:01 -05001825 ret = cgroup_apply_control(dcgrp);
1826 if (ret)
1827 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1828 ss->name, ret);
1829
Tejun Heo5df36032014-03-19 10:23:54 -04001830 if (ss->bind)
1831 ss->bind(css);
Tejun Heob4e0eea2016-02-22 22:25:46 -05001832 } while_each_subsys_mask();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833
Tejun Heo1ada4832015-09-18 17:54:23 -04001834 kernfs_activate(dcgrp->kn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835 return 0;
1836}
1837
Tejun Heo1592c9b2016-12-27 14:49:08 -05001838int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1839 struct kernfs_root *kf_root)
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001840{
Felipe Balbi09be4c82016-05-12 12:34:38 +03001841 int len = 0;
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001842 char *buf = NULL;
1843 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1844 struct cgroup *ns_cgroup;
1845
1846 buf = kmalloc(PATH_MAX, GFP_KERNEL);
1847 if (!buf)
1848 return -ENOMEM;
1849
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001850 spin_lock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001851 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1852 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03001853 spin_unlock_irq(&css_set_lock);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05001854
1855 if (len >= PATH_MAX)
1856 len = -ERANGE;
1857 else if (len > 0) {
1858 seq_escape(sf, buf, " \t\n\\");
1859 len = 0;
1860 }
1861 kfree(buf);
1862 return len;
1863}
1864
Al Viroe34a98d2019-01-17 00:22:58 -05001865enum cgroup2_param {
1866 Opt_nsdelegate,
Chris Down9852ae32019-05-31 22:30:22 -07001867 Opt_memory_localevents,
Johannes Weiner8a931f82020-04-01 21:07:07 -07001868 Opt_memory_recursiveprot,
Al Viroe34a98d2019-01-17 00:22:58 -05001869 nr__cgroup2_params
1870};
1871
Al Virod7167b12019-09-07 07:23:15 -04001872static const struct fs_parameter_spec cgroup2_fs_parameters[] = {
Chris Down9852ae32019-05-31 22:30:22 -07001873 fsparam_flag("nsdelegate", Opt_nsdelegate),
1874 fsparam_flag("memory_localevents", Opt_memory_localevents),
Johannes Weiner8a931f82020-04-01 21:07:07 -07001875 fsparam_flag("memory_recursiveprot", Opt_memory_recursiveprot),
Al Viroe34a98d2019-01-17 00:22:58 -05001876 {}
1877};
1878
Al Viroe34a98d2019-01-17 00:22:58 -05001879static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param)
Tejun Heo5136f632017-06-27 14:30:28 -04001880{
Al Viroe34a98d2019-01-17 00:22:58 -05001881 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
1882 struct fs_parse_result result;
1883 int opt;
Tejun Heo5136f632017-06-27 14:30:28 -04001884
Al Virod7167b12019-09-07 07:23:15 -04001885 opt = fs_parse(fc, cgroup2_fs_parameters, param, &result);
Al Viroe34a98d2019-01-17 00:22:58 -05001886 if (opt < 0)
1887 return opt;
Tejun Heo5136f632017-06-27 14:30:28 -04001888
Al Viroe34a98d2019-01-17 00:22:58 -05001889 switch (opt) {
1890 case Opt_nsdelegate:
1891 ctx->flags |= CGRP_ROOT_NS_DELEGATE;
Tejun Heo5136f632017-06-27 14:30:28 -04001892 return 0;
Chris Down9852ae32019-05-31 22:30:22 -07001893 case Opt_memory_localevents:
1894 ctx->flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
1895 return 0;
Johannes Weiner8a931f82020-04-01 21:07:07 -07001896 case Opt_memory_recursiveprot:
1897 ctx->flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT;
1898 return 0;
Tejun Heo5136f632017-06-27 14:30:28 -04001899 }
Al Viroe34a98d2019-01-17 00:22:58 -05001900 return -EINVAL;
Tejun Heo5136f632017-06-27 14:30:28 -04001901}
1902
1903static void apply_cgroup_root_flags(unsigned int root_flags)
1904{
1905 if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
1906 if (root_flags & CGRP_ROOT_NS_DELEGATE)
1907 cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
1908 else
1909 cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
Chris Down9852ae32019-05-31 22:30:22 -07001910
1911 if (root_flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1912 cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
1913 else
1914 cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_LOCAL_EVENTS;
Johannes Weiner8a931f82020-04-01 21:07:07 -07001915
1916 if (root_flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)
1917 cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT;
1918 else
1919 cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT;
Tejun Heo5136f632017-06-27 14:30:28 -04001920 }
1921}
1922
1923static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
1924{
1925 if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
1926 seq_puts(seq, ",nsdelegate");
Chris Down9852ae32019-05-31 22:30:22 -07001927 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1928 seq_puts(seq, ",memory_localevents");
Johannes Weiner8a931f82020-04-01 21:07:07 -07001929 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)
1930 seq_puts(seq, ",memory_recursiveprot");
Tejun Heo5136f632017-06-27 14:30:28 -04001931 return 0;
1932}
1933
Al Viro90129622019-01-05 00:38:03 -05001934static int cgroup_reconfigure(struct fs_context *fc)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001935{
Al Viro90129622019-01-05 00:38:03 -05001936 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
Tejun Heo5136f632017-06-27 14:30:28 -04001937
Al Virof5dfb532019-01-16 23:42:38 -05001938 apply_cgroup_root_flags(ctx->flags);
Tejun Heo5136f632017-06-27 14:30:28 -04001939 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001940}
1941
Paul Menagecc31edc2008-10-18 20:28:04 -07001942static void init_cgroup_housekeeping(struct cgroup *cgrp)
1943{
Tejun Heo2d8f2432014-04-23 11:13:15 -04001944 struct cgroup_subsys *ss;
1945 int ssid;
1946
Tejun Heod5c419b2014-05-16 13:22:48 -04001947 INIT_LIST_HEAD(&cgrp->self.sibling);
1948 INIT_LIST_HEAD(&cgrp->self.children);
Tejun Heo69d02062013-06-12 21:04:50 -07001949 INIT_LIST_HEAD(&cgrp->cset_links);
Ben Blum72a8cb32009-09-23 15:56:27 -07001950 INIT_LIST_HEAD(&cgrp->pidlists);
1951 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo9d800df2014-05-14 09:15:00 -04001952 cgrp->self.cgroup = cgrp;
Tejun Heo184faf32014-05-16 13:22:51 -04001953 cgrp->self.flags |= CSS_ONLINE;
Tejun Heo454000a2017-05-15 09:34:02 -04001954 cgrp->dom_cgrp = cgrp;
Roman Gushchin1a926e02017-07-28 18:28:44 +01001955 cgrp->max_descendants = INT_MAX;
1956 cgrp->max_depth = INT_MAX;
Tejun Heo8f534702018-04-26 14:29:05 -07001957 INIT_LIST_HEAD(&cgrp->rstat_css_list);
Tejun Heod4ff7492018-04-26 14:29:04 -07001958 prev_cputime_init(&cgrp->prev_cputime);
Tejun Heo2d8f2432014-04-23 11:13:15 -04001959
1960 for_each_subsys(ss, ssid)
1961 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
Tejun Heof8f22e52014-04-23 11:13:16 -04001962
1963 init_waitqueue_head(&cgrp->offline_waitq);
Tejun Heod62beb72016-12-27 14:49:08 -05001964 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
Paul Menagecc31edc2008-10-18 20:28:04 -07001965}
Paul Menagec6d57f32009-09-23 15:56:19 -07001966
Al Virocf6299b12019-01-17 02:25:51 -05001967void init_cgroup_root(struct cgroup_fs_context *ctx)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001968{
Al Virocf6299b12019-01-17 02:25:51 -05001969 struct cgroup_root *root = ctx->root;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001970 struct cgroup *cgrp = &root->cgrp;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001971
Paul Menageddbcc7e2007-10-18 23:39:30 -07001972 INIT_LIST_HEAD(&root->root_list);
Tejun Heo3c9c8252014-02-12 09:29:50 -05001973 atomic_set(&root->nr_cgrps, 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001974 cgrp->root = root;
Paul Menagecc31edc2008-10-18 20:28:04 -07001975 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001976
Al Virof5dfb532019-01-16 23:42:38 -05001977 root->flags = ctx->flags;
1978 if (ctx->release_agent)
1979 strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX);
1980 if (ctx->name)
1981 strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN);
1982 if (ctx->cpuset_clone_children)
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001983 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001984}
1985
Al Viro35ac1182019-01-12 00:20:54 -05001986int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001987{
Tejun Heod427dfe2014-02-11 11:52:48 -05001988 LIST_HEAD(tmp_links);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04001989 struct cgroup *root_cgrp = &root->cgrp;
Tejun Heofa069902016-12-27 14:49:07 -05001990 struct kernfs_syscall_ops *kf_sops;
Tejun Heod427dfe2014-02-11 11:52:48 -05001991 struct css_set *cset;
Tejun Heod427dfe2014-02-11 11:52:48 -05001992 int i, ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001993
Tejun Heod427dfe2014-02-11 11:52:48 -05001994 lockdep_assert_held(&cgroup_mutex);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001995
Zefan Li9732adc2017-04-19 10:15:59 +08001996 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release,
Al Viro35ac1182019-01-12 00:20:54 -05001997 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04001998 if (ret)
1999 goto out;
2000
Tejun Heod427dfe2014-02-11 11:52:48 -05002001 /*
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002002 * We're accessing css_set_count without locking css_set_lock here,
Tejun Heod427dfe2014-02-11 11:52:48 -05002003 * but that's OK - it can only be increased by someone holding
Tejun Heo04313592016-03-03 09:58:01 -05002004 * cgroup_lock, and that's us. Later rebinding may disable
2005 * controllers on the default hierarchy and thus create new csets,
2006 * which can't be more than the existing ones. Allocate 2x.
Tejun Heod427dfe2014-02-11 11:52:48 -05002007 */
Tejun Heo04313592016-03-03 09:58:01 -05002008 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002009 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04002010 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002011
Tejun Heo985ed672014-03-19 10:23:53 -04002012 ret = cgroup_init_root_id(root);
Tejun Heod427dfe2014-02-11 11:52:48 -05002013 if (ret)
Tejun Heo9d755d32014-05-14 09:15:02 -04002014 goto cancel_ref;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002015
Tejun Heofa069902016-12-27 14:49:07 -05002016 kf_sops = root == &cgrp_dfl_root ?
2017 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
2018
2019 root->kf_root = kernfs_create_root(kf_sops,
Shaohua Liaa818822017-07-12 11:49:51 -07002020 KERNFS_ROOT_CREATE_DEACTIVATED |
Daniel Xu38aca302020-03-12 13:03:17 -07002021 KERNFS_ROOT_SUPPORT_EXPORTOP |
2022 KERNFS_ROOT_SUPPORT_USER_XATTR,
Tejun Heo2bd59d42014-02-11 11:52:49 -05002023 root_cgrp);
2024 if (IS_ERR(root->kf_root)) {
2025 ret = PTR_ERR(root->kf_root);
2026 goto exit_root_id;
2027 }
2028 root_cgrp->kn = root->kf_root->kn;
Tejun Heod7495342019-11-14 14:46:51 -08002029 WARN_ON_ONCE(cgroup_ino(root_cgrp) != 1);
Tejun Heo74321032019-11-04 15:54:30 -08002030 root_cgrp->ancestor_ids[0] = cgroup_id(root_cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002031
Tejun Heo334c3672016-03-03 09:58:01 -05002032 ret = css_populate_dir(&root_cgrp->self);
Tejun Heod427dfe2014-02-11 11:52:48 -05002033 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05002034 goto destroy_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002035
Johannes Weinera7df69b2021-04-29 22:56:20 -07002036 ret = cgroup_rstat_init(root_cgrp);
Tejun Heod427dfe2014-02-11 11:52:48 -05002037 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05002038 goto destroy_root;
Al Viro0df6a632010-12-21 13:29:29 -05002039
Johannes Weinera7df69b2021-04-29 22:56:20 -07002040 ret = rebind_subsystems(root, ss_mask);
2041 if (ret)
2042 goto exit_stats;
2043
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002044 ret = cgroup_bpf_inherit(root_cgrp);
2045 WARN_ON_ONCE(ret);
2046
Tejun Heoed1777d2016-08-10 11:23:44 -04002047 trace_cgroup_setup_root(root);
2048
Tejun Heod427dfe2014-02-11 11:52:48 -05002049 /*
2050 * There must be no failure case after here, since rebinding takes
2051 * care of subsystems' refcounts, which are explicitly dropped in
2052 * the failure exit path.
2053 */
2054 list_add(&root->root_list, &cgroup_roots);
2055 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002056
Tejun Heod427dfe2014-02-11 11:52:48 -05002057 /*
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002058 * Link the root cgroup in this hierarchy into all the css_set
Tejun Heod427dfe2014-02-11 11:52:48 -05002059 * objects.
2060 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002061 spin_lock_irq(&css_set_lock);
Tejun Heo0de09422015-10-15 16:41:49 -04002062 hash_for_each(css_set_table, i, cset, hlist) {
Tejun Heod427dfe2014-02-11 11:52:48 -05002063 link_css_set(&tmp_links, cset, root_cgrp);
Tejun Heo0de09422015-10-15 16:41:49 -04002064 if (css_set_populated(cset))
2065 cgroup_update_populated(root_cgrp, true);
2066 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002067 spin_unlock_irq(&css_set_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002068
Tejun Heod5c419b2014-05-16 13:22:48 -04002069 BUG_ON(!list_empty(&root_cgrp->self.children));
Tejun Heo3c9c8252014-02-12 09:29:50 -05002070 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
Tejun Heod427dfe2014-02-11 11:52:48 -05002071
2072 ret = 0;
Tejun Heo2bd59d42014-02-11 11:52:49 -05002073 goto out;
Tejun Heod427dfe2014-02-11 11:52:48 -05002074
Johannes Weinera7df69b2021-04-29 22:56:20 -07002075exit_stats:
2076 cgroup_rstat_exit(root_cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002077destroy_root:
2078 kernfs_destroy_root(root->kf_root);
2079 root->kf_root = NULL;
2080exit_root_id:
Tejun Heod427dfe2014-02-11 11:52:48 -05002081 cgroup_exit_root_id(root);
Tejun Heo9d755d32014-05-14 09:15:02 -04002082cancel_ref:
Tejun Heo9a1049d2014-06-28 08:10:14 -04002083 percpu_ref_exit(&root_cgrp->self.refcnt);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002084out:
Tejun Heod427dfe2014-02-11 11:52:48 -05002085 free_cgrp_cset_links(&tmp_links);
2086 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002087}
2088
Al Virocca8f322019-01-17 10:14:26 -05002089int cgroup_do_get_tree(struct fs_context *fc)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002090{
Al Viro71d883c2019-01-17 02:44:07 -05002091 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
David Howells23bf1b62018-11-01 23:07:26 +00002092 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07002093
David Howells23bf1b62018-11-01 23:07:26 +00002094 ctx->kfc.root = ctx->root->kf_root;
Al Virocca8f322019-01-17 10:14:26 -05002095 if (fc->fs_type == &cgroup2_fs_type)
David Howells23bf1b62018-11-01 23:07:26 +00002096 ctx->kfc.magic = CGROUP2_SUPER_MAGIC;
Al Virocca8f322019-01-17 10:14:26 -05002097 else
David Howells23bf1b62018-11-01 23:07:26 +00002098 ctx->kfc.magic = CGROUP_SUPER_MAGIC;
2099 ret = kernfs_get_tree(fc);
Serge Hallyned825712016-01-29 02:54:09 -06002100
Paul Menagec6d57f32009-09-23 15:56:19 -07002101 /*
Tejun Heo633feee32016-12-27 14:49:07 -05002102 * In non-init cgroup namespace, instead of root cgroup's dentry,
2103 * we return the dentry corresponding to the cgroupns->root_cgrp.
Serge Hallyned825712016-01-29 02:54:09 -06002104 */
Al Virocca8f322019-01-17 10:14:26 -05002105 if (!ret && ctx->ns != &init_cgroup_ns) {
Serge Hallyned825712016-01-29 02:54:09 -06002106 struct dentry *nsdentry;
Al Viro71d883c2019-01-17 02:44:07 -05002107 struct super_block *sb = fc->root->d_sb;
Serge Hallyned825712016-01-29 02:54:09 -06002108 struct cgroup *cgrp;
2109
2110 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002111 spin_lock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002112
Al Virocca8f322019-01-17 10:14:26 -05002113 cgrp = cset_cgroup_from_root(ctx->ns->root_cset, ctx->root);
Serge Hallyned825712016-01-29 02:54:09 -06002114
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002115 spin_unlock_irq(&css_set_lock);
Serge Hallyned825712016-01-29 02:54:09 -06002116 mutex_unlock(&cgroup_mutex);
2117
Al Viro399504e2019-01-06 11:41:29 -05002118 nsdentry = kernfs_node_dentry(cgrp->kn, sb);
Al Viro71d883c2019-01-17 02:44:07 -05002119 dput(fc->root);
Al Viro71d883c2019-01-17 02:44:07 -05002120 if (IS_ERR(nsdentry)) {
Al Viro399504e2019-01-06 11:41:29 -05002121 deactivate_locked_super(sb);
Al Viro630faf82019-11-10 11:53:27 -05002122 ret = PTR_ERR(nsdentry);
2123 nsdentry = NULL;
Al Viro71d883c2019-01-17 02:44:07 -05002124 }
Al Viro630faf82019-11-10 11:53:27 -05002125 fc->root = nsdentry;
Serge Hallyned825712016-01-29 02:54:09 -06002126 }
2127
David Howells23bf1b62018-11-01 23:07:26 +00002128 if (!ctx->kfc.new_sb_created)
Al Viro71d883c2019-01-17 02:44:07 -05002129 cgroup_put(&ctx->root->cgrp);
Li Zefan3a32bd72014-06-30 11:50:59 +08002130
Al Viro71d883c2019-01-17 02:44:07 -05002131 return ret;
Tejun Heo633feee32016-12-27 14:49:07 -05002132}
2133
Al Viro90129622019-01-05 00:38:03 -05002134/*
2135 * Destroy a cgroup filesystem context.
2136 */
2137static void cgroup_fs_context_free(struct fs_context *fc)
Tejun Heo633feee32016-12-27 14:49:07 -05002138{
Al Viro90129622019-01-05 00:38:03 -05002139 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
2140
Al Virof5dfb532019-01-16 23:42:38 -05002141 kfree(ctx->name);
2142 kfree(ctx->release_agent);
Al Virocca8f322019-01-17 10:14:26 -05002143 put_cgroup_ns(ctx->ns);
David Howells23bf1b62018-11-01 23:07:26 +00002144 kernfs_free_fs_context(fc);
Al Viro90129622019-01-05 00:38:03 -05002145 kfree(ctx);
2146}
2147
Al Viro90129622019-01-05 00:38:03 -05002148static int cgroup_get_tree(struct fs_context *fc)
Tejun Heo633feee32016-12-27 14:49:07 -05002149{
Al Viro90129622019-01-05 00:38:03 -05002150 struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
Tejun Heo5136f632017-06-27 14:30:28 -04002151 int ret;
Tejun Heo633feee32016-12-27 14:49:07 -05002152
Al Viro90129622019-01-05 00:38:03 -05002153 cgrp_dfl_visible = true;
2154 cgroup_get_live(&cgrp_dfl_root.cgrp);
Al Virocf6299b12019-01-17 02:25:51 -05002155 ctx->root = &cgrp_dfl_root;
Tejun Heo633feee32016-12-27 14:49:07 -05002156
Al Virocca8f322019-01-17 10:14:26 -05002157 ret = cgroup_do_get_tree(fc);
Al Viro71d883c2019-01-17 02:44:07 -05002158 if (!ret)
2159 apply_cgroup_root_flags(ctx->flags);
2160 return ret;
Al Viro90129622019-01-05 00:38:03 -05002161}
2162
Al Viro90129622019-01-05 00:38:03 -05002163static const struct fs_context_operations cgroup_fs_context_ops = {
2164 .free = cgroup_fs_context_free,
Al Viroe34a98d2019-01-17 00:22:58 -05002165 .parse_param = cgroup2_parse_param,
Al Viro90129622019-01-05 00:38:03 -05002166 .get_tree = cgroup_get_tree,
2167 .reconfigure = cgroup_reconfigure,
2168};
2169
2170static const struct fs_context_operations cgroup1_fs_context_ops = {
2171 .free = cgroup_fs_context_free,
Al Viro8d2451f2019-01-17 00:15:11 -05002172 .parse_param = cgroup1_parse_param,
Al Viro90129622019-01-05 00:38:03 -05002173 .get_tree = cgroup1_get_tree,
2174 .reconfigure = cgroup1_reconfigure,
2175};
2176
2177/*
David Howells23bf1b62018-11-01 23:07:26 +00002178 * Initialise the cgroup filesystem creation/reconfiguration context. Notably,
2179 * we select the namespace we're going to use.
Al Viro90129622019-01-05 00:38:03 -05002180 */
2181static int cgroup_init_fs_context(struct fs_context *fc)
2182{
2183 struct cgroup_fs_context *ctx;
2184
2185 ctx = kzalloc(sizeof(struct cgroup_fs_context), GFP_KERNEL);
2186 if (!ctx)
2187 return -ENOMEM;
Tejun Heo633feee32016-12-27 14:49:07 -05002188
Al Virocca8f322019-01-17 10:14:26 -05002189 ctx->ns = current->nsproxy->cgroup_ns;
2190 get_cgroup_ns(ctx->ns);
David Howells23bf1b62018-11-01 23:07:26 +00002191 fc->fs_private = &ctx->kfc;
Al Viro90129622019-01-05 00:38:03 -05002192 if (fc->fs_type == &cgroup2_fs_type)
2193 fc->ops = &cgroup_fs_context_ops;
2194 else
2195 fc->ops = &cgroup1_fs_context_ops;
Al Virof7a99452019-05-12 12:42:58 -04002196 put_user_ns(fc->user_ns);
David Howells23bf1b62018-11-01 23:07:26 +00002197 fc->user_ns = get_user_ns(ctx->ns->user_ns);
2198 fc->global = true;
Al Viro90129622019-01-05 00:38:03 -05002199 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002200}
2201
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002202static void cgroup_kill_sb(struct super_block *sb)
2203{
Tejun Heo2bd59d42014-02-11 11:52:49 -05002204 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002205 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002206
Tejun Heo9d755d32014-05-14 09:15:02 -04002207 /*
Al Viro35ac1182019-01-12 00:20:54 -05002208 * If @root doesn't have any children, start killing it.
Tejun Heo9d755d32014-05-14 09:15:02 -04002209 * This prevents new mounts by disabling percpu_ref_tryget_live().
Li Zefan1f779fb2014-06-04 16:48:15 +08002210 *
2211 * And don't kill the default root.
Tejun Heo9d755d32014-05-14 09:15:02 -04002212 */
Al Viro35ac1182019-01-12 00:20:54 -05002213 if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root &&
Quanyang Wang04f8ef52021-10-18 15:56:23 +08002214 !percpu_ref_is_dying(&root->cgrp.self.refcnt)) {
2215 cgroup_bpf_offline(&root->cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04002216 percpu_ref_kill(&root->cgrp.self.refcnt);
Quanyang Wang04f8ef52021-10-18 15:56:23 +08002217 }
Al Viro35ac1182019-01-12 00:20:54 -05002218 cgroup_put(&root->cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05002219 kernfs_kill_sb(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002220}
2221
Tejun Heo0a268db2016-12-27 14:49:06 -05002222struct file_system_type cgroup_fs_type = {
Al Viro8d2451f2019-01-17 00:15:11 -05002223 .name = "cgroup",
2224 .init_fs_context = cgroup_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04002225 .parameters = cgroup1_fs_parameters,
Al Viro8d2451f2019-01-17 00:15:11 -05002226 .kill_sb = cgroup_kill_sb,
2227 .fs_flags = FS_USERNS_MOUNT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002228};
2229
Tejun Heo67e9c742015-11-16 11:13:34 -05002230static struct file_system_type cgroup2_fs_type = {
Al Viroe34a98d2019-01-17 00:22:58 -05002231 .name = "cgroup2",
2232 .init_fs_context = cgroup_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04002233 .parameters = cgroup2_fs_parameters,
Al Viroe34a98d2019-01-17 00:22:58 -05002234 .kill_sb = cgroup_kill_sb,
2235 .fs_flags = FS_USERNS_MOUNT,
Tejun Heo67e9c742015-11-16 11:13:34 -05002236};
2237
Al Virod5f68d32019-05-13 12:33:22 -04002238#ifdef CONFIG_CPUSETS
2239static const struct fs_context_operations cpuset_fs_context_ops = {
2240 .get_tree = cgroup1_get_tree,
2241 .free = cgroup_fs_context_free,
2242};
2243
2244/*
2245 * This is ugly, but preserves the userspace API for existing cpuset
2246 * users. If someone tries to mount the "cpuset" filesystem, we
2247 * silently switch it to mount "cgroup" instead
2248 */
2249static int cpuset_init_fs_context(struct fs_context *fc)
2250{
2251 char *agent = kstrdup("/sbin/cpuset_release_agent", GFP_USER);
2252 struct cgroup_fs_context *ctx;
2253 int err;
2254
2255 err = cgroup_init_fs_context(fc);
2256 if (err) {
2257 kfree(agent);
2258 return err;
2259 }
2260
2261 fc->ops = &cpuset_fs_context_ops;
2262
2263 ctx = cgroup_fc2context(fc);
2264 ctx->subsys_mask = 1 << cpuset_cgrp_id;
2265 ctx->flags |= CGRP_ROOT_NOPREFIX;
2266 ctx->release_agent = agent;
2267
2268 get_filesystem(&cgroup_fs_type);
2269 put_filesystem(fc->fs_type);
2270 fc->fs_type = &cgroup_fs_type;
2271
2272 return 0;
2273}
2274
2275static struct file_system_type cpuset_fs_type = {
2276 .name = "cpuset",
2277 .init_fs_context = cpuset_init_fs_context,
2278 .fs_flags = FS_USERNS_MOUNT,
2279};
2280#endif
2281
Tejun Heo0a268db2016-12-27 14:49:06 -05002282int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
2283 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002284{
2285 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
Aditya Kalia79a9082016-01-29 02:54:06 -06002286
Tejun Heo4c737b42016-08-10 11:23:44 -04002287 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
Aditya Kalia79a9082016-01-29 02:54:06 -06002288}
2289
Tejun Heo4c737b42016-08-10 11:23:44 -04002290int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
2291 struct cgroup_namespace *ns)
Aditya Kalia79a9082016-01-29 02:54:06 -06002292{
Tejun Heo4c737b42016-08-10 11:23:44 -04002293 int ret;
Aditya Kalia79a9082016-01-29 02:54:06 -06002294
2295 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002296 spin_lock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002297
2298 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
2299
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002300 spin_unlock_irq(&css_set_lock);
Aditya Kalia79a9082016-01-29 02:54:06 -06002301 mutex_unlock(&cgroup_mutex);
2302
2303 return ret;
2304}
2305EXPORT_SYMBOL_GPL(cgroup_path_ns);
2306
Li Zefana043e3b2008-02-23 15:24:09 -08002307/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07002308 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07002309 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07002310 * @buf: the buffer to write the path into
2311 * @buflen: the length of the buffer
2312 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07002313 * Determine @task's cgroup on the first (the one with the lowest non-zero
2314 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2315 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2316 * cgroup controller callbacks.
2317 *
Tejun Heoe61734c2014-02-12 09:29:50 -05002318 * Return value is the same as kernfs_path().
Tejun Heo857a2be2013-04-14 20:50:08 -07002319 */
Tejun Heo4c737b42016-08-10 11:23:44 -04002320int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07002321{
Tejun Heo3dd06ff2014-03-19 10:23:54 -04002322 struct cgroup_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07002323 struct cgroup *cgrp;
Tejun Heoe61734c2014-02-12 09:29:50 -05002324 int hierarchy_id = 1;
Tejun Heo4c737b42016-08-10 11:23:44 -04002325 int ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002326
2327 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002328 spin_lock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002329
Tejun Heo913ffdb2013-07-11 16:34:48 -07002330 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2331
Tejun Heo857a2be2013-04-14 20:50:08 -07002332 if (root) {
2333 cgrp = task_cgroup_from_root(task, root);
Tejun Heo4c737b42016-08-10 11:23:44 -04002334 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
Tejun Heo913ffdb2013-07-11 16:34:48 -07002335 } else {
2336 /* if no hierarchy exists, everyone is in "/" */
Tejun Heo4c737b42016-08-10 11:23:44 -04002337 ret = strlcpy(buf, "/", buflen);
Tejun Heo857a2be2013-04-14 20:50:08 -07002338 }
2339
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002340 spin_unlock_irq(&css_set_lock);
Tejun Heo857a2be2013-04-14 20:50:08 -07002341 mutex_unlock(&cgroup_mutex);
Tejun Heo4c737b42016-08-10 11:23:44 -04002342 return ret;
Tejun Heo857a2be2013-04-14 20:50:08 -07002343}
Tejun Heo913ffdb2013-07-11 16:34:48 -07002344EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07002345
Tejun Heoadaae5d2015-09-11 15:00:21 -04002346/**
Tejun Heoe595cd72017-01-15 19:03:41 -05002347 * cgroup_migrate_add_task - add a migration target task to a migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002348 * @task: target task
Tejun Heoe595cd72017-01-15 19:03:41 -05002349 * @mgctx: target migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002350 *
Tejun Heoe595cd72017-01-15 19:03:41 -05002351 * Add @task, which is a migration target, to @mgctx->tset. This function
2352 * becomes noop if @task doesn't need to be migrated. @task's css_set
2353 * should have been added as a migration source and @task->cg_list will be
2354 * moved from the css_set's tasks list to mg_tasks one.
Tejun Heoadaae5d2015-09-11 15:00:21 -04002355 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002356static void cgroup_migrate_add_task(struct task_struct *task,
2357 struct cgroup_mgctx *mgctx)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002358{
2359 struct css_set *cset;
2360
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002361 lockdep_assert_held(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002362
2363 /* @task either already exited or can't exit until the end */
2364 if (task->flags & PF_EXITING)
2365 return;
2366
Tejun Heo5153faa2019-10-24 12:03:51 -07002367 /* cgroup_threadgroup_rwsem protects racing against forks */
2368 WARN_ON_ONCE(list_empty(&task->cg_list));
Tejun Heoadaae5d2015-09-11 15:00:21 -04002369
2370 cset = task_css_set(task);
2371 if (!cset->mg_src_cgrp)
2372 return;
2373
Tejun Heo61046722017-07-08 07:17:02 -04002374 mgctx->tset.nr_tasks++;
2375
Tejun Heoadaae5d2015-09-11 15:00:21 -04002376 list_move_tail(&task->cg_list, &cset->mg_tasks);
2377 if (list_empty(&cset->mg_node))
Tejun Heoe595cd72017-01-15 19:03:41 -05002378 list_add_tail(&cset->mg_node,
2379 &mgctx->tset.src_csets);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002380 if (list_empty(&cset->mg_dst_cset->mg_node))
Tejun Heod8ebf512017-01-15 19:03:40 -05002381 list_add_tail(&cset->mg_dst_cset->mg_node,
Tejun Heoe595cd72017-01-15 19:03:41 -05002382 &mgctx->tset.dst_csets);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002383}
2384
Tejun Heo2f7ee562011-12-12 18:12:21 -08002385/**
2386 * cgroup_taskset_first - reset taskset and return the first task
2387 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002388 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002389 *
2390 * @tset iteration is initialized and the first task is returned.
2391 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002392struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2393 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002394{
Tejun Heob3dc0942014-02-25 10:04:01 -05002395 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2396 tset->cur_task = NULL;
2397
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002398 return cgroup_taskset_next(tset, dst_cssp);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002399}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002400
2401/**
2402 * cgroup_taskset_next - iterate to the next task in taskset
2403 * @tset: taskset of interest
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002404 * @dst_cssp: output variable for the destination css
Tejun Heo2f7ee562011-12-12 18:12:21 -08002405 *
2406 * Return the next task in @tset. Iteration must have been initialized
2407 * with cgroup_taskset_first().
2408 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002409struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2410 struct cgroup_subsys_state **dst_cssp)
Tejun Heo2f7ee562011-12-12 18:12:21 -08002411{
Tejun Heob3dc0942014-02-25 10:04:01 -05002412 struct css_set *cset = tset->cur_cset;
2413 struct task_struct *task = tset->cur_task;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002414
Kees Cookd20d30e2021-08-27 17:02:55 -07002415 while (CGROUP_HAS_SUBSYS_CONFIG && &cset->mg_node != tset->csets) {
Tejun Heob3dc0942014-02-25 10:04:01 -05002416 if (!task)
2417 task = list_first_entry(&cset->mg_tasks,
2418 struct task_struct, cg_list);
2419 else
2420 task = list_next_entry(task, cg_list);
Tejun Heo2f7ee562011-12-12 18:12:21 -08002421
Tejun Heob3dc0942014-02-25 10:04:01 -05002422 if (&task->cg_list != &cset->mg_tasks) {
2423 tset->cur_cset = cset;
2424 tset->cur_task = task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002425
2426 /*
2427 * This function may be called both before and
2428 * after cgroup_taskset_migrate(). The two cases
2429 * can be distinguished by looking at whether @cset
2430 * has its ->mg_dst_cset set.
2431 */
2432 if (cset->mg_dst_cset)
2433 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2434 else
2435 *dst_cssp = cset->subsys[tset->ssid];
2436
Tejun Heob3dc0942014-02-25 10:04:01 -05002437 return task;
2438 }
2439
2440 cset = list_next_entry(cset, mg_node);
2441 task = NULL;
2442 }
2443
2444 return NULL;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002445}
Tejun Heo2f7ee562011-12-12 18:12:21 -08002446
2447/**
Yang Li2ca11b02021-05-26 10:49:09 +08002448 * cgroup_migrate_execute - migrate a taskset
Tejun Heoe595cd72017-01-15 19:03:41 -05002449 * @mgctx: migration context
Tejun Heoadaae5d2015-09-11 15:00:21 -04002450 *
Tejun Heoe595cd72017-01-15 19:03:41 -05002451 * Migrate tasks in @mgctx as setup by migration preparation functions.
Tejun Heo37ff9f82016-03-08 11:51:26 -05002452 * This function fails iff one of the ->can_attach callbacks fails and
Tejun Heoe595cd72017-01-15 19:03:41 -05002453 * guarantees that either all or none of the tasks in @mgctx are migrated.
2454 * @mgctx is consumed regardless of success.
Tejun Heoadaae5d2015-09-11 15:00:21 -04002455 */
Tejun Heobfc2cf62017-01-15 19:03:41 -05002456static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
Tejun Heoadaae5d2015-09-11 15:00:21 -04002457{
Tejun Heoe595cd72017-01-15 19:03:41 -05002458 struct cgroup_taskset *tset = &mgctx->tset;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002459 struct cgroup_subsys *ss;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002460 struct task_struct *task, *tmp_task;
2461 struct css_set *cset, *tmp_cset;
Tejun Heo37ff9f82016-03-08 11:51:26 -05002462 int ssid, failed_ssid, ret;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002463
Tejun Heoadaae5d2015-09-11 15:00:21 -04002464 /* check that we can legitimately attach to the cgroup */
Tejun Heo61046722017-07-08 07:17:02 -04002465 if (tset->nr_tasks) {
2466 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2467 if (ss->can_attach) {
2468 tset->ssid = ssid;
2469 ret = ss->can_attach(tset);
2470 if (ret) {
2471 failed_ssid = ssid;
2472 goto out_cancel_attach;
2473 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002474 }
Tejun Heo61046722017-07-08 07:17:02 -04002475 } while_each_subsys_mask();
2476 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002477
2478 /*
2479 * Now that we're guaranteed success, proceed to move all tasks to
2480 * the new cgroup. There are no failure cases after here, so this
2481 * is the commit point.
2482 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002483 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002484 list_for_each_entry(cset, &tset->src_csets, mg_node) {
Tejun Heof6d7d042015-10-15 16:41:52 -04002485 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2486 struct css_set *from_cset = task_css_set(task);
2487 struct css_set *to_cset = cset->mg_dst_cset;
2488
2489 get_css_set(to_cset);
Waiman Long73a72422017-06-13 17:18:01 -04002490 to_cset->nr_tasks++;
Tejun Heof6d7d042015-10-15 16:41:52 -04002491 css_set_move_task(task, from_cset, to_cset, true);
Waiman Long73a72422017-06-13 17:18:01 -04002492 from_cset->nr_tasks--;
Roman Gushchin76f969e2019-04-19 10:03:04 -07002493 /*
2494 * If the source or destination cgroup is frozen,
2495 * the task might require to change its state.
2496 */
2497 cgroup_freezer_migrate_task(task, from_cset->dfl_cgrp,
2498 to_cset->dfl_cgrp);
2499 put_css_set_locked(from_cset);
2500
Tejun Heof6d7d042015-10-15 16:41:52 -04002501 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002502 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002503 spin_unlock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002504
2505 /*
2506 * Migration is committed, all target tasks are now on dst_csets.
2507 * Nothing is sensitive to fork() after this point. Notify
2508 * controllers that migration is complete.
2509 */
2510 tset->csets = &tset->dst_csets;
2511
Tejun Heo61046722017-07-08 07:17:02 -04002512 if (tset->nr_tasks) {
2513 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2514 if (ss->attach) {
2515 tset->ssid = ssid;
2516 ss->attach(tset);
2517 }
2518 } while_each_subsys_mask();
2519 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002520
2521 ret = 0;
2522 goto out_release_tset;
2523
2524out_cancel_attach:
Tejun Heo61046722017-07-08 07:17:02 -04002525 if (tset->nr_tasks) {
2526 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2527 if (ssid == failed_ssid)
2528 break;
2529 if (ss->cancel_attach) {
2530 tset->ssid = ssid;
2531 ss->cancel_attach(tset);
2532 }
2533 } while_each_subsys_mask();
2534 }
Tejun Heoadaae5d2015-09-11 15:00:21 -04002535out_release_tset:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002536 spin_lock_irq(&css_set_lock);
Tejun Heoadaae5d2015-09-11 15:00:21 -04002537 list_splice_init(&tset->dst_csets, &tset->src_csets);
2538 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2539 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2540 list_del_init(&cset->mg_node);
2541 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002542 spin_unlock_irq(&css_set_lock);
Waiman Longc4fa6c42017-09-21 09:54:13 -04002543
2544 /*
2545 * Re-initialize the cgroup_taskset structure in case it is reused
2546 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2547 * iteration.
2548 */
2549 tset->nr_tasks = 0;
2550 tset->csets = &tset->src_csets;
Tejun Heoadaae5d2015-09-11 15:00:21 -04002551 return ret;
2552}
2553
2554/**
Tejun Heo8cfd8142017-07-21 11:14:51 -04002555 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
Tejun Heo6c694c82016-03-08 11:51:25 -05002556 * @dst_cgrp: destination cgroup to test
2557 *
Tejun Heo8cfd8142017-07-21 11:14:51 -04002558 * On the default hierarchy, except for the mixable, (possible) thread root
2559 * and threaded cgroups, subtree_control must be zero for migration
2560 * destination cgroups with tasks so that child cgroups don't compete
2561 * against tasks.
Tejun Heo6c694c82016-03-08 11:51:25 -05002562 */
Tejun Heo8cfd8142017-07-21 11:14:51 -04002563int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
Tejun Heo6c694c82016-03-08 11:51:25 -05002564{
Tejun Heo8cfd8142017-07-21 11:14:51 -04002565 /* v1 doesn't have any restriction */
2566 if (!cgroup_on_dfl(dst_cgrp))
2567 return 0;
2568
2569 /* verify @dst_cgrp can host resources */
2570 if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp))
2571 return -EOPNOTSUPP;
2572
2573 /* mixables don't care */
2574 if (cgroup_is_mixable(dst_cgrp))
2575 return 0;
2576
2577 /*
2578 * If @dst_cgrp is already or can become a thread root or is
2579 * threaded, it doesn't matter.
2580 */
2581 if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp))
2582 return 0;
2583
2584 /* apply no-internal-process constraint */
2585 if (dst_cgrp->subtree_control)
2586 return -EBUSY;
2587
2588 return 0;
Tejun Heo6c694c82016-03-08 11:51:25 -05002589}
2590
2591/**
Tejun Heo1958d2d2014-02-25 10:04:03 -05002592 * cgroup_migrate_finish - cleanup after attach
Tejun Heoe595cd72017-01-15 19:03:41 -05002593 * @mgctx: migration context
Ben Blum74a11662011-05-26 16:25:20 -07002594 *
Tejun Heo1958d2d2014-02-25 10:04:03 -05002595 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2596 * those functions for details.
Ben Blum74a11662011-05-26 16:25:20 -07002597 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002598void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
Ben Blum74a11662011-05-26 16:25:20 -07002599{
Tejun Heoe595cd72017-01-15 19:03:41 -05002600 LIST_HEAD(preloaded);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002601 struct css_set *cset, *tmp_cset;
2602
2603 lockdep_assert_held(&cgroup_mutex);
2604
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002605 spin_lock_irq(&css_set_lock);
Tejun Heoe595cd72017-01-15 19:03:41 -05002606
2607 list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded);
2608 list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded);
2609
2610 list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002611 cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002612 cset->mg_dst_cgrp = NULL;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002613 cset->mg_dst_cset = NULL;
2614 list_del_init(&cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002615 put_css_set_locked(cset);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002616 }
Tejun Heoe595cd72017-01-15 19:03:41 -05002617
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002618 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002619}
2620
2621/**
2622 * cgroup_migrate_add_src - add a migration source css_set
2623 * @src_cset: the source css_set to add
2624 * @dst_cgrp: the destination cgroup
Tejun Heoe595cd72017-01-15 19:03:41 -05002625 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002626 *
2627 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
Tejun Heoe595cd72017-01-15 19:03:41 -05002628 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
Tejun Heo1958d2d2014-02-25 10:04:03 -05002629 * up by cgroup_migrate_finish().
2630 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002631 * This function may be called without holding cgroup_threadgroup_rwsem
2632 * even if the target is a process. Threads may be created and destroyed
2633 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2634 * into play and the preloaded css_sets are guaranteed to cover all
2635 * migrations.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002636 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002637void cgroup_migrate_add_src(struct css_set *src_cset,
2638 struct cgroup *dst_cgrp,
Tejun Heoe595cd72017-01-15 19:03:41 -05002639 struct cgroup_mgctx *mgctx)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002640{
2641 struct cgroup *src_cgrp;
2642
2643 lockdep_assert_held(&cgroup_mutex);
Tejun Heof0d9a5f2015-10-15 16:41:53 -04002644 lockdep_assert_held(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002645
Tejun Heo2b021cb2016-03-15 20:43:04 -04002646 /*
2647 * If ->dead, @src_set is associated with one or more dead cgroups
2648 * and doesn't contain any migratable tasks. Ignore it early so
2649 * that the rest of migration path doesn't get confused by it.
2650 */
2651 if (src_cset->dead)
2652 return;
2653
Tejun Heo1958d2d2014-02-25 10:04:03 -05002654 if (!list_empty(&src_cset->mg_preload_node))
2655 return;
2656
Wei Yang18157752021-12-14 00:46:07 +00002657 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2658
Tejun Heo1958d2d2014-02-25 10:04:03 -05002659 WARN_ON(src_cset->mg_src_cgrp);
Tejun Heoe4857982016-03-08 11:51:26 -05002660 WARN_ON(src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002661 WARN_ON(!list_empty(&src_cset->mg_tasks));
2662 WARN_ON(!list_empty(&src_cset->mg_node));
2663
2664 src_cset->mg_src_cgrp = src_cgrp;
Tejun Heoe4857982016-03-08 11:51:26 -05002665 src_cset->mg_dst_cgrp = dst_cgrp;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002666 get_css_set(src_cset);
Tejun Heoe595cd72017-01-15 19:03:41 -05002667 list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002668}
2669
2670/**
2671 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
Tejun Heoe595cd72017-01-15 19:03:41 -05002672 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002673 *
Tejun Heoe4857982016-03-08 11:51:26 -05002674 * Tasks are about to be moved and all the source css_sets have been
Tejun Heoe595cd72017-01-15 19:03:41 -05002675 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2676 * pins all destination css_sets, links each to its source, and append them
2677 * to @mgctx->preloaded_dst_csets.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002678 *
2679 * This function must be called after cgroup_migrate_add_src() has been
2680 * called on each migration source css_set. After migration is performed
2681 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
Tejun Heoe595cd72017-01-15 19:03:41 -05002682 * @mgctx.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002683 */
Tejun Heoe595cd72017-01-15 19:03:41 -05002684int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002685{
Tejun Heof817de92014-04-23 11:13:16 -04002686 struct css_set *src_cset, *tmp_cset;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002687
2688 lockdep_assert_held(&cgroup_mutex);
2689
2690 /* look up the dst cset for each src cset and link it to src */
Tejun Heoe595cd72017-01-15 19:03:41 -05002691 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
2692 mg_preload_node) {
Tejun Heo1958d2d2014-02-25 10:04:03 -05002693 struct css_set *dst_cset;
Tejun Heobfc2cf62017-01-15 19:03:41 -05002694 struct cgroup_subsys *ss;
2695 int ssid;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002696
Tejun Heoe4857982016-03-08 11:51:26 -05002697 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002698 if (!dst_cset)
Shakeel Buttd6e486e2019-04-03 16:03:54 -07002699 return -ENOMEM;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002700
2701 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002702
2703 /*
2704 * If src cset equals dst, it's noop. Drop the src.
2705 * cgroup_migrate() will skip the cset too. Note that we
2706 * can't handle src == dst as some nodes are used by both.
2707 */
2708 if (src_cset == dst_cset) {
2709 src_cset->mg_src_cgrp = NULL;
Tejun Heoe4857982016-03-08 11:51:26 -05002710 src_cset->mg_dst_cgrp = NULL;
Tejun Heof817de92014-04-23 11:13:16 -04002711 list_del_init(&src_cset->mg_preload_node);
Zefan Lia25eb522014-09-19 16:51:00 +08002712 put_css_set(src_cset);
2713 put_css_set(dst_cset);
Tejun Heof817de92014-04-23 11:13:16 -04002714 continue;
2715 }
2716
Tejun Heo1958d2d2014-02-25 10:04:03 -05002717 src_cset->mg_dst_cset = dst_cset;
2718
2719 if (list_empty(&dst_cset->mg_preload_node))
Tejun Heoe595cd72017-01-15 19:03:41 -05002720 list_add_tail(&dst_cset->mg_preload_node,
2721 &mgctx->preloaded_dst_csets);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002722 else
Zefan Lia25eb522014-09-19 16:51:00 +08002723 put_css_set(dst_cset);
Tejun Heobfc2cf62017-01-15 19:03:41 -05002724
2725 for_each_subsys(ss, ssid)
2726 if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
2727 mgctx->ss_mask |= 1 << ssid;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002728 }
2729
Tejun Heo1958d2d2014-02-25 10:04:03 -05002730 return 0;
Tejun Heo1958d2d2014-02-25 10:04:03 -05002731}
2732
2733/**
2734 * cgroup_migrate - migrate a process or task to a cgroup
Tejun Heo1958d2d2014-02-25 10:04:03 -05002735 * @leader: the leader of the process or the task to migrate
2736 * @threadgroup: whether @leader points to the whole process or a single task
Tejun Heoe595cd72017-01-15 19:03:41 -05002737 * @mgctx: migration context
Tejun Heo1958d2d2014-02-25 10:04:03 -05002738 *
Tejun Heo37ff9f82016-03-08 11:51:26 -05002739 * Migrate a process or task denoted by @leader. If migrating a process,
2740 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2741 * responsible for invoking cgroup_migrate_add_src() and
Tejun Heo1958d2d2014-02-25 10:04:03 -05002742 * cgroup_migrate_prepare_dst() on the targets before invoking this
2743 * function and following up with cgroup_migrate_finish().
2744 *
2745 * As long as a controller's ->can_attach() doesn't fail, this function is
2746 * guaranteed to succeed. This means that, excluding ->can_attach()
2747 * failure, when migrating multiple targets, the success or failure can be
2748 * decided for all targets by invoking group_migrate_prepare_dst() before
2749 * actually starting migrating.
2750 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002751int cgroup_migrate(struct task_struct *leader, bool threadgroup,
Tejun Heobfc2cf62017-01-15 19:03:41 -05002752 struct cgroup_mgctx *mgctx)
Ben Blum74a11662011-05-26 16:25:20 -07002753{
Tejun Heoadaae5d2015-09-11 15:00:21 -04002754 struct task_struct *task;
Ben Blum74a11662011-05-26 16:25:20 -07002755
2756 /*
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002757 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2758 * already PF_EXITING could be freed from underneath us unless we
2759 * take an rcu_read_lock.
2760 */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002761 spin_lock_irq(&css_set_lock);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002762 rcu_read_lock();
Tejun Heo9db8de32014-02-13 06:58:43 -05002763 task = leader;
Ben Blum74a11662011-05-26 16:25:20 -07002764 do {
Tejun Heoe595cd72017-01-15 19:03:41 -05002765 cgroup_migrate_add_task(task, mgctx);
Li Zefan081aa452013-03-13 09:17:09 +08002766 if (!threadgroup)
2767 break;
Tejun Heo9db8de32014-02-13 06:58:43 -05002768 } while_each_thread(leader, task);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002769 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002770 spin_unlock_irq(&css_set_lock);
Ben Blum74a11662011-05-26 16:25:20 -07002771
Tejun Heobfc2cf62017-01-15 19:03:41 -05002772 return cgroup_migrate_execute(mgctx);
Ben Blum74a11662011-05-26 16:25:20 -07002773}
2774
Tejun Heo1958d2d2014-02-25 10:04:03 -05002775/**
2776 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2777 * @dst_cgrp: the cgroup to attach to
2778 * @leader: the task or the leader of the threadgroup to be attached
2779 * @threadgroup: attach the whole threadgroup?
2780 *
Tejun Heo1ed13282015-09-16 12:53:17 -04002781 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
Tejun Heo1958d2d2014-02-25 10:04:03 -05002782 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002783int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
2784 bool threadgroup)
Tejun Heo1958d2d2014-02-25 10:04:03 -05002785{
Tejun Heoe595cd72017-01-15 19:03:41 -05002786 DEFINE_CGROUP_MGCTX(mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002787 struct task_struct *task;
Christian Brauner6df970e2020-02-05 14:26:18 +01002788 int ret = 0;
Tejun Heo6c694c82016-03-08 11:51:25 -05002789
Tejun Heo1958d2d2014-02-25 10:04:03 -05002790 /* look up all src csets */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002791 spin_lock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002792 rcu_read_lock();
2793 task = leader;
2794 do {
Tejun Heoe595cd72017-01-15 19:03:41 -05002795 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002796 if (!threadgroup)
2797 break;
2798 } while_each_thread(leader, task);
2799 rcu_read_unlock();
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002800 spin_unlock_irq(&css_set_lock);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002801
2802 /* prepare dst csets and commit */
Tejun Heoe595cd72017-01-15 19:03:41 -05002803 ret = cgroup_migrate_prepare_dst(&mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002804 if (!ret)
Tejun Heobfc2cf62017-01-15 19:03:41 -05002805 ret = cgroup_migrate(leader, threadgroup, &mgctx);
Tejun Heo1958d2d2014-02-25 10:04:03 -05002806
Tejun Heoe595cd72017-01-15 19:03:41 -05002807 cgroup_migrate_finish(&mgctx);
Tejun Heoed1777d2016-08-10 11:23:44 -04002808
2809 if (!ret)
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04002810 TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup);
Tejun Heoed1777d2016-08-10 11:23:44 -04002811
Tejun Heo1958d2d2014-02-25 10:04:03 -05002812 return ret;
Ben Blum74a11662011-05-26 16:25:20 -07002813}
2814
Michal Koutný9a3284f2019-10-04 12:57:40 +02002815struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
2816 bool *locked)
Tejun Heo715c8092017-05-15 09:34:00 -04002817 __acquires(&cgroup_threadgroup_rwsem)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002818{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002819 struct task_struct *tsk;
Tejun Heoacbef752014-05-13 12:16:22 -04002820 pid_t pid;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002821
Tejun Heoacbef752014-05-13 12:16:22 -04002822 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
Tejun Heo715c8092017-05-15 09:34:00 -04002823 return ERR_PTR(-EINVAL);
Ben Blum74a11662011-05-26 16:25:20 -07002824
Michal Koutný9a3284f2019-10-04 12:57:40 +02002825 /*
2826 * If we migrate a single thread, we don't care about threadgroup
2827 * stability. If the thread is `current`, it won't exit(2) under our
2828 * hands or change PID through exec(2). We exclude
2829 * cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write
2830 * callers by cgroup_mutex.
2831 * Therefore, we can skip the global lock.
2832 */
2833 lockdep_assert_held(&cgroup_mutex);
2834 if (pid || threadgroup) {
2835 percpu_down_write(&cgroup_threadgroup_rwsem);
2836 *locked = true;
2837 } else {
2838 *locked = false;
2839 }
Tejun Heo715c8092017-05-15 09:34:00 -04002840
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002841 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002842 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002843 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002844 if (!tsk) {
Tejun Heo715c8092017-05-15 09:34:00 -04002845 tsk = ERR_PTR(-ESRCH);
2846 goto out_unlock_threadgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002847 }
Tejun Heodedf22e2015-06-18 16:54:28 -04002848 } else {
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002849 tsk = current;
Tejun Heodedf22e2015-06-18 16:54:28 -04002850 }
Tejun Heocd3d0952011-12-12 18:12:21 -08002851
2852 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002853 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002854
2855 /*
Tejun Heo77f88792017-03-16 16:54:24 -04002856 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2857 * If userland migrates such a kthread to a non-root cgroup, it can
2858 * become trapped in a cpuset, or RT kthread may be born in a
2859 * cgroup with no rt_runtime allocated. Just say no.
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002860 */
Tejun Heo77f88792017-03-16 16:54:24 -04002861 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
Tejun Heo715c8092017-05-15 09:34:00 -04002862 tsk = ERR_PTR(-EINVAL);
2863 goto out_unlock_threadgroup;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002864 }
2865
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002866 get_task_struct(tsk);
Tejun Heo715c8092017-05-15 09:34:00 -04002867 goto out_unlock_rcu;
Tejun Heocd3d0952011-12-12 18:12:21 -08002868
Tejun Heo715c8092017-05-15 09:34:00 -04002869out_unlock_threadgroup:
Michal Koutný9a3284f2019-10-04 12:57:40 +02002870 if (*locked) {
2871 percpu_up_write(&cgroup_threadgroup_rwsem);
2872 *locked = false;
2873 }
Tejun Heo3014dde2015-09-16 13:03:02 -04002874out_unlock_rcu:
2875 rcu_read_unlock();
Tejun Heo715c8092017-05-15 09:34:00 -04002876 return tsk;
2877}
2878
Michal Koutný9a3284f2019-10-04 12:57:40 +02002879void cgroup_procs_write_finish(struct task_struct *task, bool locked)
Tejun Heo715c8092017-05-15 09:34:00 -04002880 __releases(&cgroup_threadgroup_rwsem)
2881{
2882 struct cgroup_subsys *ss;
2883 int ssid;
2884
2885 /* release reference from cgroup_procs_write_start() */
2886 put_task_struct(task);
2887
Michal Koutný9a3284f2019-10-04 12:57:40 +02002888 if (locked)
2889 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002890 for_each_subsys(ss, ssid)
2891 if (ss->post_attach)
2892 ss->post_attach();
Paul Menageaf351022008-07-25 01:47:01 -07002893}
2894
Tejun Heo6e5c8302016-02-22 22:25:47 -05002895static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
Tejun Heof8f22e52014-04-23 11:13:16 -04002896{
2897 struct cgroup_subsys *ss;
2898 bool printed = false;
2899 int ssid;
2900
Tejun Heob4e0eea2016-02-22 22:25:46 -05002901 do_each_subsys_mask(ss, ssid, ss_mask) {
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002902 if (printed)
2903 seq_putc(seq, ' ');
Markus Elfring85db0022019-07-02 19:26:59 +02002904 seq_puts(seq, ss->name);
Aleksa Saraia966a4e2015-06-06 10:02:15 +10002905 printed = true;
Tejun Heob4e0eea2016-02-22 22:25:46 -05002906 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04002907 if (printed)
2908 seq_putc(seq, '\n');
2909}
2910
Tejun Heof8f22e52014-04-23 11:13:16 -04002911/* show controllers which are enabled from the parent */
2912static int cgroup_controllers_show(struct seq_file *seq, void *v)
2913{
2914 struct cgroup *cgrp = seq_css(seq)->cgroup;
2915
Tejun Heo5531dc92016-03-03 09:57:58 -05002916 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
Tejun Heof8f22e52014-04-23 11:13:16 -04002917 return 0;
2918}
2919
2920/* show controllers which are enabled for a given cgroup's children */
2921static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2922{
2923 struct cgroup *cgrp = seq_css(seq)->cgroup;
2924
Tejun Heo667c2492014-07-08 18:02:56 -04002925 cgroup_print_ss_mask(seq, cgrp->subtree_control);
Tejun Heof8f22e52014-04-23 11:13:16 -04002926 return 0;
2927}
2928
2929/**
2930 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2931 * @cgrp: root of the subtree to update csses for
2932 *
Tejun Heo54962602016-03-03 09:58:01 -05002933 * @cgrp's control masks have changed and its subtree's css associations
2934 * need to be updated accordingly. This function looks up all css_sets
2935 * which are attached to the subtree, creates the matching updated css_sets
2936 * and migrates the tasks to the new ones.
Tejun Heof8f22e52014-04-23 11:13:16 -04002937 */
2938static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2939{
Tejun Heoe595cd72017-01-15 19:03:41 -05002940 DEFINE_CGROUP_MGCTX(mgctx);
Tejun Heo54962602016-03-03 09:58:01 -05002941 struct cgroup_subsys_state *d_css;
2942 struct cgroup *dsct;
Tejun Heof8f22e52014-04-23 11:13:16 -04002943 struct css_set *src_cset;
2944 int ret;
2945
Tejun Heof8f22e52014-04-23 11:13:16 -04002946 lockdep_assert_held(&cgroup_mutex);
2947
Tejun Heo3014dde2015-09-16 13:03:02 -04002948 percpu_down_write(&cgroup_threadgroup_rwsem);
2949
Tejun Heof8f22e52014-04-23 11:13:16 -04002950 /* look up all csses currently attached to @cgrp's subtree */
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002951 spin_lock_irq(&css_set_lock);
Tejun Heo54962602016-03-03 09:58:01 -05002952 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heof8f22e52014-04-23 11:13:16 -04002953 struct cgrp_cset_link *link;
2954
Tejun Heo54962602016-03-03 09:58:01 -05002955 list_for_each_entry(link, &dsct->cset_links, cset_link)
Tejun Heoe595cd72017-01-15 19:03:41 -05002956 cgroup_migrate_add_src(link->cset, dsct, &mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002957 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002958 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002959
2960 /* NULL dst indicates self on default hierarchy */
Tejun Heoe595cd72017-01-15 19:03:41 -05002961 ret = cgroup_migrate_prepare_dst(&mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002962 if (ret)
2963 goto out_finish;
2964
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002965 spin_lock_irq(&css_set_lock);
Tejun Heoe595cd72017-01-15 19:03:41 -05002966 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) {
Tejun Heo10265072015-09-11 15:00:22 -04002967 struct task_struct *task, *ntask;
Tejun Heof8f22e52014-04-23 11:13:16 -04002968
Tejun Heo10265072015-09-11 15:00:22 -04002969 /* all tasks in src_csets need to be migrated */
2970 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
Tejun Heoe595cd72017-01-15 19:03:41 -05002971 cgroup_migrate_add_task(task, &mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002972 }
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03002973 spin_unlock_irq(&css_set_lock);
Tejun Heof8f22e52014-04-23 11:13:16 -04002974
Tejun Heobfc2cf62017-01-15 19:03:41 -05002975 ret = cgroup_migrate_execute(&mgctx);
Tejun Heof8f22e52014-04-23 11:13:16 -04002976out_finish:
Tejun Heoe595cd72017-01-15 19:03:41 -05002977 cgroup_migrate_finish(&mgctx);
Tejun Heo3014dde2015-09-16 13:03:02 -04002978 percpu_up_write(&cgroup_threadgroup_rwsem);
Tejun Heof8f22e52014-04-23 11:13:16 -04002979 return ret;
2980}
2981
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002982/**
Tejun Heo945ba192016-03-03 09:58:00 -05002983 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
Tejun Heoce3f1d92016-03-03 09:57:59 -05002984 * @cgrp: root of the target subtree
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002985 *
2986 * Because css offlining is asynchronous, userland may try to re-enable a
Tejun Heo945ba192016-03-03 09:58:00 -05002987 * controller while the previous css is still around. This function grabs
2988 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002989 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002990void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
Tejun Heo945ba192016-03-03 09:58:00 -05002991 __acquires(&cgroup_mutex)
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002992{
2993 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05002994 struct cgroup_subsys_state *d_css;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05002995 struct cgroup_subsys *ss;
2996 int ssid;
2997
Tejun Heo945ba192016-03-03 09:58:00 -05002998restart:
2999 mutex_lock(&cgroup_mutex);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003000
Tejun Heoce3f1d92016-03-03 09:57:59 -05003001 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003002 for_each_subsys(ss, ssid) {
3003 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3004 DEFINE_WAIT(wait);
3005
Tejun Heoce3f1d92016-03-03 09:57:59 -05003006 if (!css || !percpu_ref_is_dying(&css->refcnt))
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003007 continue;
3008
Tejun Heoa590b902017-04-28 15:14:55 -04003009 cgroup_get_live(dsct);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003010 prepare_to_wait(&dsct->offline_waitq, &wait,
3011 TASK_UNINTERRUPTIBLE);
3012
3013 mutex_unlock(&cgroup_mutex);
3014 schedule();
3015 finish_wait(&dsct->offline_waitq, &wait);
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003016
3017 cgroup_put(dsct);
Tejun Heo945ba192016-03-03 09:58:00 -05003018 goto restart;
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003019 }
3020 }
Tejun Heo1b9b96a2016-03-03 09:57:59 -05003021}
3022
Tejun Heo12b3bb62016-03-03 09:57:59 -05003023/**
Tejun Heo479adb82018-10-04 13:28:08 -07003024 * cgroup_save_control - save control masks and dom_cgrp of a subtree
Tejun Heo15a27c32016-03-03 09:57:59 -05003025 * @cgrp: root of the target subtree
3026 *
Tejun Heo479adb82018-10-04 13:28:08 -07003027 * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the
3028 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
3029 * itself.
Tejun Heo15a27c32016-03-03 09:57:59 -05003030 */
3031static void cgroup_save_control(struct cgroup *cgrp)
3032{
3033 struct cgroup *dsct;
3034 struct cgroup_subsys_state *d_css;
3035
3036 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
3037 dsct->old_subtree_control = dsct->subtree_control;
3038 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
Tejun Heo479adb82018-10-04 13:28:08 -07003039 dsct->old_dom_cgrp = dsct->dom_cgrp;
Tejun Heo15a27c32016-03-03 09:57:59 -05003040 }
3041}
3042
3043/**
3044 * cgroup_propagate_control - refresh control masks of a subtree
3045 * @cgrp: root of the target subtree
3046 *
3047 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
3048 * ->subtree_control and propagate controller availability through the
3049 * subtree so that descendants don't have unavailable controllers enabled.
3050 */
3051static void cgroup_propagate_control(struct cgroup *cgrp)
3052{
3053 struct cgroup *dsct;
3054 struct cgroup_subsys_state *d_css;
3055
3056 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
3057 dsct->subtree_control &= cgroup_control(dsct);
Tejun Heo5ced2512016-03-03 09:58:01 -05003058 dsct->subtree_ss_mask =
3059 cgroup_calc_subtree_ss_mask(dsct->subtree_control,
3060 cgroup_ss_mask(dsct));
Tejun Heo15a27c32016-03-03 09:57:59 -05003061 }
3062}
3063
3064/**
Tejun Heo479adb82018-10-04 13:28:08 -07003065 * cgroup_restore_control - restore control masks and dom_cgrp of a subtree
Tejun Heo15a27c32016-03-03 09:57:59 -05003066 * @cgrp: root of the target subtree
3067 *
Tejun Heo479adb82018-10-04 13:28:08 -07003068 * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the
3069 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
3070 * itself.
Tejun Heo15a27c32016-03-03 09:57:59 -05003071 */
3072static void cgroup_restore_control(struct cgroup *cgrp)
3073{
3074 struct cgroup *dsct;
3075 struct cgroup_subsys_state *d_css;
3076
3077 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
3078 dsct->subtree_control = dsct->old_subtree_control;
3079 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
Tejun Heo479adb82018-10-04 13:28:08 -07003080 dsct->dom_cgrp = dsct->old_dom_cgrp;
Tejun Heo15a27c32016-03-03 09:57:59 -05003081 }
3082}
3083
Tejun Heof6d635ad2016-03-08 11:51:26 -05003084static bool css_visible(struct cgroup_subsys_state *css)
3085{
3086 struct cgroup_subsys *ss = css->ss;
3087 struct cgroup *cgrp = css->cgroup;
3088
3089 if (cgroup_control(cgrp) & (1 << ss->id))
3090 return true;
3091 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
3092 return false;
3093 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
3094}
3095
Tejun Heo15a27c32016-03-03 09:57:59 -05003096/**
Tejun Heobdb53bd2016-03-03 09:57:59 -05003097 * cgroup_apply_control_enable - enable or show csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05003098 * @cgrp: root of the target subtree
Tejun Heobdb53bd2016-03-03 09:57:59 -05003099 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05003100 * Walk @cgrp's subtree and create new csses or make the existing ones
Tejun Heobdb53bd2016-03-03 09:57:59 -05003101 * visible. A css is created invisible if it's being implicitly enabled
3102 * through dependency. An invisible css is made visible when the userland
3103 * explicitly enables it.
3104 *
3105 * Returns 0 on success, -errno on failure. On failure, csses which have
3106 * been processed already aren't cleaned up. The caller is responsible for
Masahiro Yamada8a1115f2017-03-09 16:16:31 -08003107 * cleaning up with cgroup_apply_control_disable().
Tejun Heobdb53bd2016-03-03 09:57:59 -05003108 */
3109static int cgroup_apply_control_enable(struct cgroup *cgrp)
3110{
3111 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003112 struct cgroup_subsys_state *d_css;
Tejun Heobdb53bd2016-03-03 09:57:59 -05003113 struct cgroup_subsys *ss;
3114 int ssid, ret;
3115
Tejun Heoce3f1d92016-03-03 09:57:59 -05003116 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
Tejun Heobdb53bd2016-03-03 09:57:59 -05003117 for_each_subsys(ss, ssid) {
3118 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3119
3120 if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
3121 continue;
3122
3123 if (!css) {
3124 css = css_create(dsct, ss);
3125 if (IS_ERR(css))
3126 return PTR_ERR(css);
3127 }
3128
Michal Koutný3bc0bb32020-01-09 16:05:59 +01003129 WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt));
3130
Tejun Heof6d635ad2016-03-08 11:51:26 -05003131 if (css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05003132 ret = css_populate_dir(css);
Tejun Heobdb53bd2016-03-03 09:57:59 -05003133 if (ret)
3134 return ret;
3135 }
3136 }
3137 }
3138
3139 return 0;
3140}
3141
3142/**
Tejun Heo12b3bb62016-03-03 09:57:59 -05003143 * cgroup_apply_control_disable - kill or hide csses according to control
Tejun Heoce3f1d92016-03-03 09:57:59 -05003144 * @cgrp: root of the target subtree
Tejun Heo12b3bb62016-03-03 09:57:59 -05003145 *
Tejun Heoce3f1d92016-03-03 09:57:59 -05003146 * Walk @cgrp's subtree and kill and hide csses so that they match
Tejun Heo12b3bb62016-03-03 09:57:59 -05003147 * cgroup_ss_mask() and cgroup_visible_mask().
3148 *
3149 * A css is hidden when the userland requests it to be disabled while other
3150 * subsystems are still depending on it. The css must not actively control
3151 * resources and be in the vanilla state if it's made visible again later.
3152 * Controllers which may be depended upon should provide ->css_reset() for
3153 * this purpose.
3154 */
3155static void cgroup_apply_control_disable(struct cgroup *cgrp)
3156{
3157 struct cgroup *dsct;
Tejun Heoce3f1d92016-03-03 09:57:59 -05003158 struct cgroup_subsys_state *d_css;
Tejun Heo12b3bb62016-03-03 09:57:59 -05003159 struct cgroup_subsys *ss;
3160 int ssid;
3161
Tejun Heoce3f1d92016-03-03 09:57:59 -05003162 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05003163 for_each_subsys(ss, ssid) {
3164 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
3165
3166 if (!css)
3167 continue;
3168
Michal Koutný3bc0bb32020-01-09 16:05:59 +01003169 WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt));
3170
Tejun Heo334c3672016-03-03 09:58:01 -05003171 if (css->parent &&
3172 !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
Tejun Heo12b3bb62016-03-03 09:57:59 -05003173 kill_css(css);
Tejun Heof6d635ad2016-03-08 11:51:26 -05003174 } else if (!css_visible(css)) {
Tejun Heo334c3672016-03-03 09:58:01 -05003175 css_clear_dir(css);
Tejun Heo12b3bb62016-03-03 09:57:59 -05003176 if (ss->css_reset)
3177 ss->css_reset(css);
3178 }
3179 }
3180 }
3181}
3182
Tejun Heof7b28142016-03-03 09:58:00 -05003183/**
3184 * cgroup_apply_control - apply control mask updates to the subtree
3185 * @cgrp: root of the target subtree
3186 *
3187 * subsystems can be enabled and disabled in a subtree using the following
3188 * steps.
3189 *
3190 * 1. Call cgroup_save_control() to stash the current state.
3191 * 2. Update ->subtree_control masks in the subtree as desired.
3192 * 3. Call cgroup_apply_control() to apply the changes.
3193 * 4. Optionally perform other related operations.
3194 * 5. Call cgroup_finalize_control() to finish up.
3195 *
3196 * This function implements step 3 and propagates the mask changes
3197 * throughout @cgrp's subtree, updates csses accordingly and perform
3198 * process migrations.
3199 */
3200static int cgroup_apply_control(struct cgroup *cgrp)
3201{
3202 int ret;
3203
3204 cgroup_propagate_control(cgrp);
3205
3206 ret = cgroup_apply_control_enable(cgrp);
3207 if (ret)
3208 return ret;
3209
3210 /*
Dennis Zhoufc5a8282018-12-05 12:10:36 -05003211 * At this point, cgroup_e_css_by_mask() results reflect the new csses
Tejun Heof7b28142016-03-03 09:58:00 -05003212 * making the following cgroup_update_dfl_csses() properly update
3213 * css associations of all tasks in the subtree.
3214 */
3215 ret = cgroup_update_dfl_csses(cgrp);
3216 if (ret)
3217 return ret;
3218
3219 return 0;
3220}
3221
3222/**
3223 * cgroup_finalize_control - finalize control mask update
3224 * @cgrp: root of the target subtree
3225 * @ret: the result of the update
3226 *
3227 * Finalize control mask update. See cgroup_apply_control() for more info.
3228 */
3229static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
3230{
3231 if (ret) {
3232 cgroup_restore_control(cgrp);
3233 cgroup_propagate_control(cgrp);
3234 }
3235
3236 cgroup_apply_control_disable(cgrp);
3237}
3238
Tejun Heo8cfd8142017-07-21 11:14:51 -04003239static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable)
3240{
3241 u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask;
3242
3243 /* if nothing is getting enabled, nothing to worry about */
3244 if (!enable)
3245 return 0;
3246
3247 /* can @cgrp host any resources? */
3248 if (!cgroup_is_valid_domain(cgrp->dom_cgrp))
3249 return -EOPNOTSUPP;
3250
3251 /* mixables don't care */
3252 if (cgroup_is_mixable(cgrp))
3253 return 0;
3254
3255 if (domain_enable) {
3256 /* can't enable domain controllers inside a thread subtree */
3257 if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3258 return -EOPNOTSUPP;
3259 } else {
3260 /*
3261 * Threaded controllers can handle internal competitions
3262 * and are always allowed inside a (prospective) thread
3263 * subtree.
3264 */
3265 if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3266 return 0;
3267 }
3268
3269 /*
3270 * Controllers can't be enabled for a cgroup with tasks to avoid
3271 * child cgroups competing against tasks.
3272 */
3273 if (cgroup_has_tasks(cgrp))
3274 return -EBUSY;
3275
3276 return 0;
3277}
3278
Tejun Heof8f22e52014-04-23 11:13:16 -04003279/* change the enabled child controllers for a cgroup in the default hierarchy */
Tejun Heo451af502014-05-13 12:16:21 -04003280static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3281 char *buf, size_t nbytes,
3282 loff_t off)
Tejun Heof8f22e52014-04-23 11:13:16 -04003283{
Tejun Heo6e5c8302016-02-22 22:25:47 -05003284 u16 enable = 0, disable = 0;
Tejun Heoa9746d82014-05-13 12:19:22 -04003285 struct cgroup *cgrp, *child;
Tejun Heof8f22e52014-04-23 11:13:16 -04003286 struct cgroup_subsys *ss;
Tejun Heo451af502014-05-13 12:16:21 -04003287 char *tok;
Tejun Heof8f22e52014-04-23 11:13:16 -04003288 int ssid, ret;
3289
3290 /*
Tejun Heod37167a2014-05-13 12:10:59 -04003291 * Parse input - space separated list of subsystem names prefixed
3292 * with either + or -.
Tejun Heof8f22e52014-04-23 11:13:16 -04003293 */
Tejun Heo451af502014-05-13 12:16:21 -04003294 buf = strstrip(buf);
3295 while ((tok = strsep(&buf, " "))) {
Tejun Heod37167a2014-05-13 12:10:59 -04003296 if (tok[0] == '\0')
3297 continue;
Tejun Heoa7165262016-02-23 10:00:50 -05003298 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
Tejun Heofc5ed1e2015-09-18 11:56:28 -04003299 if (!cgroup_ssid_enabled(ssid) ||
3300 strcmp(tok + 1, ss->name))
Tejun Heof8f22e52014-04-23 11:13:16 -04003301 continue;
3302
3303 if (*tok == '+') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003304 enable |= 1 << ssid;
3305 disable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003306 } else if (*tok == '-') {
Tejun Heo7d331fa2014-05-13 12:11:00 -04003307 disable |= 1 << ssid;
3308 enable &= ~(1 << ssid);
Tejun Heof8f22e52014-04-23 11:13:16 -04003309 } else {
3310 return -EINVAL;
3311 }
3312 break;
Tejun Heob4e0eea2016-02-22 22:25:46 -05003313 } while_each_subsys_mask();
Tejun Heof8f22e52014-04-23 11:13:16 -04003314 if (ssid == CGROUP_SUBSYS_COUNT)
3315 return -EINVAL;
3316 }
3317
Tejun Heo945ba192016-03-03 09:58:00 -05003318 cgrp = cgroup_kn_lock_live(of->kn, true);
Tejun Heoa9746d82014-05-13 12:19:22 -04003319 if (!cgrp)
3320 return -ENODEV;
Tejun Heof8f22e52014-04-23 11:13:16 -04003321
3322 for_each_subsys(ss, ssid) {
3323 if (enable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003324 if (cgrp->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003325 enable &= ~(1 << ssid);
3326 continue;
3327 }
3328
Tejun Heo5531dc92016-03-03 09:57:58 -05003329 if (!(cgroup_control(cgrp) & (1 << ssid))) {
Tejun Heoc29adf22014-07-08 18:02:56 -04003330 ret = -ENOENT;
3331 goto out_unlock;
3332 }
Tejun Heof8f22e52014-04-23 11:13:16 -04003333 } else if (disable & (1 << ssid)) {
Tejun Heo667c2492014-07-08 18:02:56 -04003334 if (!(cgrp->subtree_control & (1 << ssid))) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003335 disable &= ~(1 << ssid);
3336 continue;
3337 }
3338
3339 /* a child has it enabled? */
3340 cgroup_for_each_live_child(child, cgrp) {
Tejun Heo667c2492014-07-08 18:02:56 -04003341 if (child->subtree_control & (1 << ssid)) {
Tejun Heof8f22e52014-04-23 11:13:16 -04003342 ret = -EBUSY;
Tejun Heoddab2b62014-05-13 12:19:22 -04003343 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003344 }
3345 }
3346 }
3347 }
3348
3349 if (!enable && !disable) {
3350 ret = 0;
Tejun Heoddab2b62014-05-13 12:19:22 -04003351 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003352 }
3353
Tejun Heo8cfd8142017-07-21 11:14:51 -04003354 ret = cgroup_vet_subtree_control_enable(cgrp, enable);
3355 if (ret)
Tejun Heo27f26752017-07-16 21:44:18 -04003356 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003357
Tejun Heo15a27c32016-03-03 09:57:59 -05003358 /* save and update control masks and prepare csses */
3359 cgroup_save_control(cgrp);
Tejun Heoc29adf22014-07-08 18:02:56 -04003360
Tejun Heo15a27c32016-03-03 09:57:59 -05003361 cgrp->subtree_control |= enable;
3362 cgrp->subtree_control &= ~disable;
Tejun Heof63070d2014-07-08 18:02:57 -04003363
Tejun Heof7b28142016-03-03 09:58:00 -05003364 ret = cgroup_apply_control(cgrp);
Tejun Heof7b28142016-03-03 09:58:00 -05003365 cgroup_finalize_control(cgrp, ret);
Tejun Heo3c745412017-07-23 08:14:15 -04003366 if (ret)
3367 goto out_unlock;
Tejun Heof8f22e52014-04-23 11:13:16 -04003368
3369 kernfs_activate(cgrp->kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04003370out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04003371 cgroup_kn_unlock(of->kn);
Tejun Heo451af502014-05-13 12:16:21 -04003372 return ret ?: nbytes;
Tejun Heof8f22e52014-04-23 11:13:16 -04003373}
3374
Tejun Heoc705a002017-07-25 13:20:18 -04003375/**
3376 * cgroup_enable_threaded - make @cgrp threaded
3377 * @cgrp: the target cgroup
3378 *
3379 * Called when "threaded" is written to the cgroup.type interface file and
3380 * tries to make @cgrp threaded and join the parent's resource domain.
3381 * This function is never called on the root cgroup as cgroup.type doesn't
3382 * exist on it.
3383 */
Tejun Heo8cfd8142017-07-21 11:14:51 -04003384static int cgroup_enable_threaded(struct cgroup *cgrp)
3385{
3386 struct cgroup *parent = cgroup_parent(cgrp);
3387 struct cgroup *dom_cgrp = parent->dom_cgrp;
Tejun Heo479adb82018-10-04 13:28:08 -07003388 struct cgroup *dsct;
3389 struct cgroup_subsys_state *d_css;
Tejun Heo8cfd8142017-07-21 11:14:51 -04003390 int ret;
3391
3392 lockdep_assert_held(&cgroup_mutex);
3393
3394 /* noop if already threaded */
3395 if (cgroup_is_threaded(cgrp))
3396 return 0;
3397
Tejun Heod1897c92018-02-21 11:39:22 -08003398 /*
3399 * If @cgroup is populated or has domain controllers enabled, it
3400 * can't be switched. While the below cgroup_can_be_thread_root()
3401 * test can catch the same conditions, that's only when @parent is
3402 * not mixable, so let's check it explicitly.
3403 */
3404 if (cgroup_is_populated(cgrp) ||
3405 cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
3406 return -EOPNOTSUPP;
3407
Tejun Heo8cfd8142017-07-21 11:14:51 -04003408 /* we're joining the parent's domain, ensure its validity */
3409 if (!cgroup_is_valid_domain(dom_cgrp) ||
3410 !cgroup_can_be_thread_root(dom_cgrp))
3411 return -EOPNOTSUPP;
3412
3413 /*
Tejun Heo8cfd8142017-07-21 11:14:51 -04003414 * The following shouldn't cause actual migrations and should
3415 * always succeed.
3416 */
3417 cgroup_save_control(cgrp);
3418
Tejun Heo479adb82018-10-04 13:28:08 -07003419 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)
3420 if (dsct == cgrp || cgroup_is_threaded(dsct))
3421 dsct->dom_cgrp = dom_cgrp;
3422
Tejun Heo8cfd8142017-07-21 11:14:51 -04003423 ret = cgroup_apply_control(cgrp);
3424 if (!ret)
3425 parent->nr_threaded_children++;
Tejun Heo8cfd8142017-07-21 11:14:51 -04003426
3427 cgroup_finalize_control(cgrp, ret);
3428 return ret;
3429}
3430
3431static int cgroup_type_show(struct seq_file *seq, void *v)
3432{
3433 struct cgroup *cgrp = seq_css(seq)->cgroup;
3434
3435 if (cgroup_is_threaded(cgrp))
3436 seq_puts(seq, "threaded\n");
3437 else if (!cgroup_is_valid_domain(cgrp))
3438 seq_puts(seq, "domain invalid\n");
3439 else if (cgroup_is_thread_root(cgrp))
3440 seq_puts(seq, "domain threaded\n");
3441 else
3442 seq_puts(seq, "domain\n");
3443
3444 return 0;
3445}
3446
3447static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf,
3448 size_t nbytes, loff_t off)
3449{
3450 struct cgroup *cgrp;
3451 int ret;
3452
3453 /* only switching to threaded mode is supported */
3454 if (strcmp(strstrip(buf), "threaded"))
3455 return -EINVAL;
3456
Michal Koutný3bc0bb32020-01-09 16:05:59 +01003457 /* drain dying csses before we re-apply (threaded) subtree control */
3458 cgrp = cgroup_kn_lock_live(of->kn, true);
Tejun Heo8cfd8142017-07-21 11:14:51 -04003459 if (!cgrp)
3460 return -ENOENT;
3461
3462 /* threaded can only be enabled */
3463 ret = cgroup_enable_threaded(cgrp);
3464
3465 cgroup_kn_unlock(of->kn);
3466 return ret ?: nbytes;
3467}
3468
Roman Gushchin1a926e02017-07-28 18:28:44 +01003469static int cgroup_max_descendants_show(struct seq_file *seq, void *v)
3470{
3471 struct cgroup *cgrp = seq_css(seq)->cgroup;
3472 int descendants = READ_ONCE(cgrp->max_descendants);
3473
3474 if (descendants == INT_MAX)
3475 seq_puts(seq, "max\n");
3476 else
3477 seq_printf(seq, "%d\n", descendants);
3478
3479 return 0;
3480}
3481
3482static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of,
3483 char *buf, size_t nbytes, loff_t off)
3484{
3485 struct cgroup *cgrp;
3486 int descendants;
3487 ssize_t ret;
3488
3489 buf = strstrip(buf);
3490 if (!strcmp(buf, "max")) {
3491 descendants = INT_MAX;
3492 } else {
3493 ret = kstrtoint(buf, 0, &descendants);
3494 if (ret)
3495 return ret;
3496 }
3497
Dan Carpenter696b98f2017-08-09 13:25:21 +03003498 if (descendants < 0)
Roman Gushchin1a926e02017-07-28 18:28:44 +01003499 return -ERANGE;
3500
3501 cgrp = cgroup_kn_lock_live(of->kn, false);
3502 if (!cgrp)
3503 return -ENOENT;
3504
3505 cgrp->max_descendants = descendants;
3506
3507 cgroup_kn_unlock(of->kn);
3508
3509 return nbytes;
3510}
3511
3512static int cgroup_max_depth_show(struct seq_file *seq, void *v)
3513{
3514 struct cgroup *cgrp = seq_css(seq)->cgroup;
3515 int depth = READ_ONCE(cgrp->max_depth);
3516
3517 if (depth == INT_MAX)
3518 seq_puts(seq, "max\n");
3519 else
3520 seq_printf(seq, "%d\n", depth);
3521
3522 return 0;
3523}
3524
3525static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of,
3526 char *buf, size_t nbytes, loff_t off)
3527{
3528 struct cgroup *cgrp;
3529 ssize_t ret;
3530 int depth;
3531
3532 buf = strstrip(buf);
3533 if (!strcmp(buf, "max")) {
3534 depth = INT_MAX;
3535 } else {
3536 ret = kstrtoint(buf, 0, &depth);
3537 if (ret)
3538 return ret;
3539 }
3540
Dan Carpenter696b98f2017-08-09 13:25:21 +03003541 if (depth < 0)
Roman Gushchin1a926e02017-07-28 18:28:44 +01003542 return -ERANGE;
3543
3544 cgrp = cgroup_kn_lock_live(of->kn, false);
3545 if (!cgrp)
3546 return -ENOENT;
3547
3548 cgrp->max_depth = depth;
3549
3550 cgroup_kn_unlock(of->kn);
3551
3552 return nbytes;
3553}
3554
Tejun Heo4a07c222015-09-18 17:54:22 -04003555static int cgroup_events_show(struct seq_file *seq, void *v)
Tejun Heo842b5972014-04-25 18:28:02 -04003556{
Roman Gushchin76f969e2019-04-19 10:03:04 -07003557 struct cgroup *cgrp = seq_css(seq)->cgroup;
3558
3559 seq_printf(seq, "populated %d\n", cgroup_is_populated(cgrp));
3560 seq_printf(seq, "frozen %d\n", test_bit(CGRP_FROZEN, &cgrp->flags));
3561
Tejun Heo842b5972014-04-25 18:28:02 -04003562 return 0;
3563}
3564
Tejun Heo3e489302017-08-11 05:49:01 -07003565static int cgroup_stat_show(struct seq_file *seq, void *v)
Roman Gushchinec392252017-08-02 17:55:31 +01003566{
3567 struct cgroup *cgroup = seq_css(seq)->cgroup;
3568
3569 seq_printf(seq, "nr_descendants %d\n",
3570 cgroup->nr_descendants);
3571 seq_printf(seq, "nr_dying_descendants %d\n",
3572 cgroup->nr_dying_descendants);
3573
3574 return 0;
3575}
3576
Tejun Heod41bf8c2017-10-23 16:18:27 -07003577static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq,
3578 struct cgroup *cgrp, int ssid)
3579{
3580 struct cgroup_subsys *ss = cgroup_subsys[ssid];
3581 struct cgroup_subsys_state *css;
3582 int ret;
3583
3584 if (!ss->css_extra_stat_show)
3585 return 0;
3586
3587 css = cgroup_tryget_css(cgrp, ss);
3588 if (!css)
3589 return 0;
3590
3591 ret = ss->css_extra_stat_show(seq, css);
3592 css_put(css);
3593 return ret;
3594}
3595
3596static int cpu_stat_show(struct seq_file *seq, void *v)
3597{
Tejun Heoc3ba1322017-10-30 08:13:14 -07003598 struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
Tejun Heod41bf8c2017-10-23 16:18:27 -07003599 int ret = 0;
3600
Tejun Heod4ff7492018-04-26 14:29:04 -07003601 cgroup_base_stat_cputime_show(seq);
Tejun Heod41bf8c2017-10-23 16:18:27 -07003602#ifdef CONFIG_CGROUP_SCHED
3603 ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id);
3604#endif
3605 return ret;
3606}
3607
Johannes Weiner2ce71352018-10-26 15:06:31 -07003608#ifdef CONFIG_PSI
3609static int cgroup_io_pressure_show(struct seq_file *seq, void *v)
3610{
Tejun Heo74321032019-11-04 15:54:30 -08003611 struct cgroup *cgrp = seq_css(seq)->cgroup;
Qian Cai190ecb12020-02-23 22:00:07 -05003612 struct psi_group *psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi;
Dan Schatzbergdf5ba5b2019-05-14 15:41:18 -07003613
3614 return psi_show(seq, psi, PSI_IO);
Johannes Weiner2ce71352018-10-26 15:06:31 -07003615}
3616static int cgroup_memory_pressure_show(struct seq_file *seq, void *v)
3617{
Tejun Heo74321032019-11-04 15:54:30 -08003618 struct cgroup *cgrp = seq_css(seq)->cgroup;
Qian Cai190ecb12020-02-23 22:00:07 -05003619 struct psi_group *psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi;
Dan Schatzbergdf5ba5b2019-05-14 15:41:18 -07003620
3621 return psi_show(seq, psi, PSI_MEM);
Johannes Weiner2ce71352018-10-26 15:06:31 -07003622}
3623static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v)
3624{
Tejun Heo74321032019-11-04 15:54:30 -08003625 struct cgroup *cgrp = seq_css(seq)->cgroup;
Qian Cai190ecb12020-02-23 22:00:07 -05003626 struct psi_group *psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi;
Dan Schatzbergdf5ba5b2019-05-14 15:41:18 -07003627
3628 return psi_show(seq, psi, PSI_CPU);
Johannes Weiner2ce71352018-10-26 15:06:31 -07003629}
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07003630
3631static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf,
3632 size_t nbytes, enum psi_res res)
3633{
Tejun Heo0d2b59552022-01-06 11:02:29 -10003634 struct cgroup_file_ctx *ctx = of->priv;
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07003635 struct psi_trigger *new;
3636 struct cgroup *cgrp;
Odin Ugedal385aac12021-01-16 18:36:33 +01003637 struct psi_group *psi;
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07003638
3639 cgrp = cgroup_kn_lock_live(of->kn, false);
3640 if (!cgrp)
3641 return -ENODEV;
3642
3643 cgroup_get(cgrp);
3644 cgroup_kn_unlock(of->kn);
3645
Suren Baghdasaryana06247c2022-01-11 15:23:09 -08003646 /* Allow only one trigger per file descriptor */
3647 if (ctx->psi.trigger) {
3648 cgroup_put(cgrp);
3649 return -EBUSY;
3650 }
3651
Odin Ugedal385aac12021-01-16 18:36:33 +01003652 psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi;
3653 new = psi_trigger_create(psi, buf, nbytes, res);
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07003654 if (IS_ERR(new)) {
3655 cgroup_put(cgrp);
3656 return PTR_ERR(new);
3657 }
3658
Suren Baghdasaryana06247c2022-01-11 15:23:09 -08003659 smp_store_release(&ctx->psi.trigger, new);
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07003660 cgroup_put(cgrp);
3661
3662 return nbytes;
3663}
3664
3665static ssize_t cgroup_io_pressure_write(struct kernfs_open_file *of,
3666 char *buf, size_t nbytes,
3667 loff_t off)
3668{
3669 return cgroup_pressure_write(of, buf, nbytes, PSI_IO);
3670}
3671
3672static ssize_t cgroup_memory_pressure_write(struct kernfs_open_file *of,
3673 char *buf, size_t nbytes,
3674 loff_t off)
3675{
3676 return cgroup_pressure_write(of, buf, nbytes, PSI_MEM);
3677}
3678
3679static ssize_t cgroup_cpu_pressure_write(struct kernfs_open_file *of,
3680 char *buf, size_t nbytes,
3681 loff_t off)
3682{
3683 return cgroup_pressure_write(of, buf, nbytes, PSI_CPU);
3684}
3685
3686static __poll_t cgroup_pressure_poll(struct kernfs_open_file *of,
3687 poll_table *pt)
3688{
Tejun Heo0d2b59552022-01-06 11:02:29 -10003689 struct cgroup_file_ctx *ctx = of->priv;
3690
3691 return psi_trigger_poll(&ctx->psi.trigger, of->file, pt);
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07003692}
3693
3694static void cgroup_pressure_release(struct kernfs_open_file *of)
3695{
Tejun Heo0d2b59552022-01-06 11:02:29 -10003696 struct cgroup_file_ctx *ctx = of->priv;
3697
Suren Baghdasaryana06247c2022-01-11 15:23:09 -08003698 psi_trigger_destroy(ctx->psi.trigger);
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07003699}
Suren Baghdasaryan3958e2d2021-05-24 12:53:39 -07003700
3701bool cgroup_psi_enabled(void)
3702{
3703 return (cgroup_feature_disable_mask & (1 << OPT_FEATURE_PRESSURE)) == 0;
3704}
3705
3706#else /* CONFIG_PSI */
3707bool cgroup_psi_enabled(void)
3708{
3709 return false;
3710}
3711
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07003712#endif /* CONFIG_PSI */
Johannes Weiner2ce71352018-10-26 15:06:31 -07003713
Roman Gushchin76f969e2019-04-19 10:03:04 -07003714static int cgroup_freeze_show(struct seq_file *seq, void *v)
3715{
3716 struct cgroup *cgrp = seq_css(seq)->cgroup;
3717
3718 seq_printf(seq, "%d\n", cgrp->freezer.freeze);
3719
3720 return 0;
3721}
3722
3723static ssize_t cgroup_freeze_write(struct kernfs_open_file *of,
3724 char *buf, size_t nbytes, loff_t off)
3725{
3726 struct cgroup *cgrp;
3727 ssize_t ret;
3728 int freeze;
3729
3730 ret = kstrtoint(strstrip(buf), 0, &freeze);
3731 if (ret)
3732 return ret;
3733
3734 if (freeze < 0 || freeze > 1)
3735 return -ERANGE;
3736
3737 cgrp = cgroup_kn_lock_live(of->kn, false);
3738 if (!cgrp)
3739 return -ENOENT;
3740
3741 cgroup_freeze(cgrp, freeze);
3742
3743 cgroup_kn_unlock(of->kn);
3744
3745 return nbytes;
3746}
3747
Christian Brauner661ee622021-05-08 14:15:38 +02003748static void __cgroup_kill(struct cgroup *cgrp)
3749{
3750 struct css_task_iter it;
3751 struct task_struct *task;
3752
3753 lockdep_assert_held(&cgroup_mutex);
3754
3755 spin_lock_irq(&css_set_lock);
3756 set_bit(CGRP_KILL, &cgrp->flags);
3757 spin_unlock_irq(&css_set_lock);
3758
3759 css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED, &it);
3760 while ((task = css_task_iter_next(&it))) {
3761 /* Ignore kernel threads here. */
3762 if (task->flags & PF_KTHREAD)
3763 continue;
3764
3765 /* Skip tasks that are already dying. */
3766 if (__fatal_signal_pending(task))
3767 continue;
3768
3769 send_sig(SIGKILL, task, 0);
3770 }
3771 css_task_iter_end(&it);
3772
3773 spin_lock_irq(&css_set_lock);
3774 clear_bit(CGRP_KILL, &cgrp->flags);
3775 spin_unlock_irq(&css_set_lock);
3776}
3777
3778static void cgroup_kill(struct cgroup *cgrp)
3779{
3780 struct cgroup_subsys_state *css;
3781 struct cgroup *dsct;
3782
3783 lockdep_assert_held(&cgroup_mutex);
3784
3785 cgroup_for_each_live_descendant_pre(dsct, css, cgrp)
3786 __cgroup_kill(dsct);
3787}
3788
3789static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf,
3790 size_t nbytes, loff_t off)
3791{
3792 ssize_t ret = 0;
3793 int kill;
3794 struct cgroup *cgrp;
3795
3796 ret = kstrtoint(strstrip(buf), 0, &kill);
3797 if (ret)
3798 return ret;
3799
3800 if (kill != 1)
3801 return -ERANGE;
3802
3803 cgrp = cgroup_kn_lock_live(of->kn, false);
3804 if (!cgrp)
3805 return -ENOENT;
3806
3807 /*
3808 * Killing is a process directed operation, i.e. the whole thread-group
3809 * is taken down so act like we do for cgroup.procs and only make this
3810 * writable in non-threaded cgroups.
3811 */
3812 if (cgroup_is_threaded(cgrp))
3813 ret = -EOPNOTSUPP;
3814 else
3815 cgroup_kill(cgrp);
3816
3817 cgroup_kn_unlock(of->kn);
3818
3819 return ret ?: nbytes;
3820}
3821
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003822static int cgroup_file_open(struct kernfs_open_file *of)
3823{
Hui Su5a7b5f32020-11-06 22:47:40 +08003824 struct cftype *cft = of_cft(of);
Tejun Heo0d2b59552022-01-06 11:02:29 -10003825 struct cgroup_file_ctx *ctx;
3826 int ret;
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003827
Tejun Heo0d2b59552022-01-06 11:02:29 -10003828 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3829 if (!ctx)
3830 return -ENOMEM;
Tejun Heoe5745762022-01-06 11:02:29 -10003831
3832 ctx->ns = current->nsproxy->cgroup_ns;
3833 get_cgroup_ns(ctx->ns);
Tejun Heo0d2b59552022-01-06 11:02:29 -10003834 of->priv = ctx;
3835
3836 if (!cft->open)
3837 return 0;
3838
3839 ret = cft->open(of);
Tejun Heoe5745762022-01-06 11:02:29 -10003840 if (ret) {
3841 put_cgroup_ns(ctx->ns);
Tejun Heo0d2b59552022-01-06 11:02:29 -10003842 kfree(ctx);
Tejun Heoe5745762022-01-06 11:02:29 -10003843 }
Tejun Heo0d2b59552022-01-06 11:02:29 -10003844 return ret;
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003845}
3846
3847static void cgroup_file_release(struct kernfs_open_file *of)
3848{
Hui Su5a7b5f32020-11-06 22:47:40 +08003849 struct cftype *cft = of_cft(of);
Tejun Heo0d2b59552022-01-06 11:02:29 -10003850 struct cgroup_file_ctx *ctx = of->priv;
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003851
3852 if (cft->release)
3853 cft->release(of);
Tejun Heoe5745762022-01-06 11:02:29 -10003854 put_cgroup_ns(ctx->ns);
Tejun Heo0d2b59552022-01-06 11:02:29 -10003855 kfree(ctx);
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003856}
3857
Tejun Heo2bd59d42014-02-11 11:52:49 -05003858static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3859 size_t nbytes, loff_t off)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003860{
Tejun Heoe5745762022-01-06 11:02:29 -10003861 struct cgroup_file_ctx *ctx = of->priv;
Tejun Heo2bd59d42014-02-11 11:52:49 -05003862 struct cgroup *cgrp = of->kn->parent->priv;
Hui Su5a7b5f32020-11-06 22:47:40 +08003863 struct cftype *cft = of_cft(of);
Tejun Heo2bd59d42014-02-11 11:52:49 -05003864 struct cgroup_subsys_state *css;
Tejun Heoa742c592013-12-05 12:28:03 -05003865 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003866
Jouni Roivas65026da2020-09-30 19:42:42 +03003867 if (!nbytes)
3868 return 0;
3869
Tejun Heo5136f632017-06-27 14:30:28 -04003870 /*
3871 * If namespaces are delegation boundaries, disallow writes to
3872 * files in an non-init namespace root from inside the namespace
3873 * except for the files explicitly marked delegatable -
3874 * cgroup.procs and cgroup.subtree_control.
3875 */
3876 if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) &&
3877 !(cft->flags & CFTYPE_NS_DELEGATABLE) &&
Tejun Heoe5745762022-01-06 11:02:29 -10003878 ctx->ns != &init_cgroup_ns && ctx->ns->root_cset->dfl_cgrp == cgrp)
Tejun Heo5136f632017-06-27 14:30:28 -04003879 return -EPERM;
3880
Tejun Heob4168642014-05-13 12:16:21 -04003881 if (cft->write)
3882 return cft->write(of, buf, nbytes, off);
3883
Tejun Heo2bd59d42014-02-11 11:52:49 -05003884 /*
3885 * kernfs guarantees that a file isn't deleted with operations in
3886 * flight, which means that the matching css is and stays alive and
3887 * doesn't need to be pinned. The RCU locking is not necessary
3888 * either. It's just for the convenience of using cgroup_css().
3889 */
3890 rcu_read_lock();
3891 css = cgroup_css(cgrp, cft->ss);
3892 rcu_read_unlock();
Paul Menageddbcc7e2007-10-18 23:39:30 -07003893
Tejun Heo451af502014-05-13 12:16:21 -04003894 if (cft->write_u64) {
Tejun Heoa742c592013-12-05 12:28:03 -05003895 unsigned long long v;
3896 ret = kstrtoull(buf, 0, &v);
3897 if (!ret)
3898 ret = cft->write_u64(css, cft, v);
3899 } else if (cft->write_s64) {
3900 long long v;
3901 ret = kstrtoll(buf, 0, &v);
3902 if (!ret)
3903 ret = cft->write_s64(css, cft, v);
Tejun Heoa742c592013-12-05 12:28:03 -05003904 } else {
3905 ret = -EINVAL;
3906 }
Tejun Heo2bd59d42014-02-11 11:52:49 -05003907
Tejun Heoa742c592013-12-05 12:28:03 -05003908 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003909}
3910
Johannes Weinerdc505372019-03-05 15:45:48 -08003911static __poll_t cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt)
3912{
Hui Su5a7b5f32020-11-06 22:47:40 +08003913 struct cftype *cft = of_cft(of);
Johannes Weinerdc505372019-03-05 15:45:48 -08003914
3915 if (cft->poll)
3916 return cft->poll(of, pt);
3917
3918 return kernfs_generic_poll(of, pt);
3919}
3920
Tejun Heo6612f052013-12-05 12:28:04 -05003921static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07003922{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003923 return seq_cft(seq)->seq_start(seq, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003924}
3925
3926static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3927{
Tejun Heo2bd59d42014-02-11 11:52:49 -05003928 return seq_cft(seq)->seq_next(seq, v, ppos);
Tejun Heo6612f052013-12-05 12:28:04 -05003929}
3930
3931static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3932{
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003933 if (seq_cft(seq)->seq_stop)
3934 seq_cft(seq)->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07003935}
3936
3937static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3938{
Tejun Heo7da11272013-12-05 12:28:04 -05003939 struct cftype *cft = seq_cft(m);
3940 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08003941
Tejun Heo2da8ca82013-12-05 12:28:04 -05003942 if (cft->seq_show)
3943 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07003944
Tejun Heo896f5192013-12-05 12:28:04 -05003945 if (cft->read_u64)
3946 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3947 else if (cft->read_s64)
3948 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3949 else
3950 return -EINVAL;
3951 return 0;
Paul Menage91796562008-04-29 01:00:01 -07003952}
3953
Tejun Heo2bd59d42014-02-11 11:52:49 -05003954static struct kernfs_ops cgroup_kf_single_ops = {
3955 .atomic_write_len = PAGE_SIZE,
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003956 .open = cgroup_file_open,
3957 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003958 .write = cgroup_file_write,
Johannes Weinerdc505372019-03-05 15:45:48 -08003959 .poll = cgroup_file_poll,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003960 .seq_show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07003961};
3962
Tejun Heo2bd59d42014-02-11 11:52:49 -05003963static struct kernfs_ops cgroup_kf_ops = {
3964 .atomic_write_len = PAGE_SIZE,
Tejun Heoe90cbeb2016-12-27 14:49:03 -05003965 .open = cgroup_file_open,
3966 .release = cgroup_file_release,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003967 .write = cgroup_file_write,
Johannes Weinerdc505372019-03-05 15:45:48 -08003968 .poll = cgroup_file_poll,
Tejun Heo2bd59d42014-02-11 11:52:49 -05003969 .seq_start = cgroup_seqfile_start,
3970 .seq_next = cgroup_seqfile_next,
3971 .seq_stop = cgroup_seqfile_stop,
3972 .seq_show = cgroup_seqfile_show,
3973};
Paul Menageddbcc7e2007-10-18 23:39:30 -07003974
Tejun Heo49957f82014-04-07 16:44:47 -04003975/* set uid and gid of cgroup dirs and files to that of the creator */
3976static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3977{
3978 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3979 .ia_uid = current_fsuid(),
3980 .ia_gid = current_fsgid(), };
3981
3982 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3983 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3984 return 0;
3985
3986 return kernfs_setattr(kn, &iattr);
3987}
3988
Tejun Heob12e3582018-04-26 14:29:04 -07003989static void cgroup_file_notify_timer(struct timer_list *timer)
3990{
3991 cgroup_file_notify(container_of(timer, struct cgroup_file,
3992 notify_timer));
3993}
3994
Tejun Heo4df8dc92015-09-18 17:54:23 -04003995static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3996 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003997{
Tejun Heo8d7e6fb2014-02-11 11:52:48 -05003998 char name[CGROUP_FILE_NAME_MAX];
Tejun Heo2bd59d42014-02-11 11:52:49 -05003999 struct kernfs_node *kn;
4000 struct lock_class_key *key = NULL;
Tejun Heo49957f82014-04-07 16:44:47 -04004001 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004002
Tejun Heo2bd59d42014-02-11 11:52:49 -05004003#ifdef CONFIG_DEBUG_LOCK_ALLOC
4004 key = &cft->lockdep_key;
4005#endif
4006 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
Dmitry Torokhov488dee92018-07-20 21:56:47 +00004007 cgroup_file_mode(cft),
4008 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
4009 0, cft->kf_ops, cft,
Tejun Heodfeb07502015-02-13 14:36:31 -08004010 NULL, key);
Tejun Heo49957f82014-04-07 16:44:47 -04004011 if (IS_ERR(kn))
4012 return PTR_ERR(kn);
4013
4014 ret = cgroup_kn_set_ugid(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04004015 if (ret) {
Tejun Heo49957f82014-04-07 16:44:47 -04004016 kernfs_remove(kn);
Tejun Heof8f22e52014-04-23 11:13:16 -04004017 return ret;
4018 }
4019
Tejun Heo6f60ead2015-09-18 17:54:23 -04004020 if (cft->file_offset) {
4021 struct cgroup_file *cfile = (void *)css + cft->file_offset;
4022
Tejun Heob12e3582018-04-26 14:29:04 -07004023 timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
4024
Tejun Heo34c06252015-11-05 00:12:24 -05004025 spin_lock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04004026 cfile->kn = kn;
Tejun Heo34c06252015-11-05 00:12:24 -05004027 spin_unlock_irq(&cgroup_file_kn_lock);
Tejun Heo6f60ead2015-09-18 17:54:23 -04004028 }
4029
Tejun Heof8f22e52014-04-23 11:13:16 -04004030 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004031}
4032
Tejun Heob1f28d32013-06-28 16:24:10 -07004033/**
4034 * cgroup_addrm_files - add or remove files to a cgroup directory
Tejun Heo4df8dc92015-09-18 17:54:23 -04004035 * @css: the target css
4036 * @cgrp: the target cgroup (usually css->cgroup)
Tejun Heob1f28d32013-06-28 16:24:10 -07004037 * @cfts: array of cftypes to be added
4038 * @is_add: whether to add or remove
4039 *
4040 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo6732ed82015-09-18 17:54:23 -04004041 * For removals, this function never fails.
Tejun Heob1f28d32013-06-28 16:24:10 -07004042 */
Tejun Heo4df8dc92015-09-18 17:54:23 -04004043static int cgroup_addrm_files(struct cgroup_subsys_state *css,
4044 struct cgroup *cgrp, struct cftype cfts[],
Tejun Heo2bb566c2013-08-08 20:11:23 -04004045 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004046{
Tejun Heo6732ed82015-09-18 17:54:23 -04004047 struct cftype *cft, *cft_end = NULL;
Tejun Heob598dde2016-02-22 22:25:45 -05004048 int ret = 0;
Tejun Heob1f28d32013-06-28 16:24:10 -07004049
Tejun Heo01f64742014-05-13 12:19:23 -04004050 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07004051
Tejun Heo6732ed82015-09-18 17:54:23 -04004052restart:
4053 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08004054 /* does cft->flags tell us to skip this file on @cgrp? */
Suren Baghdasaryan3958e2d2021-05-24 12:53:39 -07004055 if ((cft->flags & CFTYPE_PRESSURE) && !cgroup_psi_enabled())
4056 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04004057 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
Tejun Heo8cbbf2c2014-03-19 10:23:55 -04004058 continue;
Tejun Heo05ebb6e2014-07-15 11:05:10 -04004059 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
Tejun Heo873fe092013-04-14 20:15:26 -07004060 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04004061 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08004062 continue;
Tejun Heod51f39b2014-05-16 13:22:48 -04004063 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
Gao fengf33fddc2012-12-06 14:38:57 +08004064 continue;
Waiman Long5cf81142018-11-08 10:08:46 -05004065 if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug)
4066 continue;
Li Zefan2739d3c2013-01-21 18:18:33 +08004067 if (is_add) {
Tejun Heo4df8dc92015-09-18 17:54:23 -04004068 ret = cgroup_add_file(css, cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07004069 if (ret) {
Joe Perchesed3d2612014-04-25 18:28:03 -04004070 pr_warn("%s: failed to add %s, err=%d\n",
4071 __func__, cft->name, ret);
Tejun Heo6732ed82015-09-18 17:54:23 -04004072 cft_end = cft;
4073 is_add = false;
4074 goto restart;
Tejun Heob1f28d32013-06-28 16:24:10 -07004075 }
Li Zefan2739d3c2013-01-21 18:18:33 +08004076 } else {
4077 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07004078 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079 }
Tejun Heob598dde2016-02-22 22:25:45 -05004080 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004081}
4082
Tejun Heo21a2d342014-02-12 09:29:49 -05004083static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07004084{
Tejun Heo2bb566c2013-08-08 20:11:23 -04004085 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04004086 struct cgroup *root = &ss->root->cgrp;
Tejun Heo492eb212013-08-08 20:11:25 -04004087 struct cgroup_subsys_state *css;
Tejun Heo9ccece82013-06-28 16:24:11 -07004088 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004089
Tejun Heo01f64742014-05-13 12:19:23 -04004090 lockdep_assert_held(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08004091
Li Zefane8c82d22013-06-18 18:48:37 +08004092 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004093 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04004094 struct cgroup *cgrp = css->cgroup;
4095
Tejun Heo88cb04b2016-03-03 09:57:58 -05004096 if (!(css->flags & CSS_VISIBLE))
Li Zefane8c82d22013-06-18 18:48:37 +08004097 continue;
4098
Tejun Heo4df8dc92015-09-18 17:54:23 -04004099 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
Tejun Heo9ccece82013-06-28 16:24:11 -07004100 if (ret)
4101 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004102 }
Tejun Heo21a2d342014-02-12 09:29:49 -05004103
4104 if (is_add && !ret)
4105 kernfs_activate(root->kn);
Tejun Heo9ccece82013-06-28 16:24:11 -07004106 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004107}
4108
Tejun Heo2da440a2014-02-11 11:52:48 -05004109static void cgroup_exit_cftypes(struct cftype *cfts)
4110{
4111 struct cftype *cft;
4112
Tejun Heo2bd59d42014-02-11 11:52:49 -05004113 for (cft = cfts; cft->name[0] != '\0'; cft++) {
4114 /* free copy for custom atomic_write_len, see init_cftypes() */
4115 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
4116 kfree(cft->kf_ops);
4117 cft->kf_ops = NULL;
Tejun Heo2da440a2014-02-11 11:52:48 -05004118 cft->ss = NULL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04004119
4120 /* revert flags set by cgroup core while adding @cfts */
Tejun Heo05ebb6e2014-07-15 11:05:10 -04004121 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
Tejun Heo2bd59d42014-02-11 11:52:49 -05004122 }
Tejun Heo2da440a2014-02-11 11:52:48 -05004123}
4124
Tejun Heo2bd59d42014-02-11 11:52:49 -05004125static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo2da440a2014-02-11 11:52:48 -05004126{
4127 struct cftype *cft;
4128
Tejun Heo2bd59d42014-02-11 11:52:49 -05004129 for (cft = cfts; cft->name[0] != '\0'; cft++) {
4130 struct kernfs_ops *kf_ops;
4131
Tejun Heo0adb0702014-02-12 09:29:48 -05004132 WARN_ON(cft->ss || cft->kf_ops);
4133
Suren Baghdasaryan3958e2d2021-05-24 12:53:39 -07004134 if ((cft->flags & CFTYPE_PRESSURE) && !cgroup_psi_enabled())
4135 continue;
4136
Tejun Heo2bd59d42014-02-11 11:52:49 -05004137 if (cft->seq_start)
4138 kf_ops = &cgroup_kf_ops;
4139 else
4140 kf_ops = &cgroup_kf_single_ops;
4141
4142 /*
4143 * Ugh... if @cft wants a custom max_write_len, we need to
4144 * make a copy of kf_ops to set its atomic_write_len.
4145 */
4146 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
4147 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
4148 if (!kf_ops) {
4149 cgroup_exit_cftypes(cfts);
4150 return -ENOMEM;
4151 }
4152 kf_ops->atomic_write_len = cft->max_write_len;
4153 }
4154
4155 cft->kf_ops = kf_ops;
Tejun Heo2da440a2014-02-11 11:52:48 -05004156 cft->ss = ss;
Tejun Heo2bd59d42014-02-11 11:52:49 -05004157 }
4158
4159 return 0;
Tejun Heo2da440a2014-02-11 11:52:48 -05004160}
4161
Tejun Heo21a2d342014-02-12 09:29:49 -05004162static int cgroup_rm_cftypes_locked(struct cftype *cfts)
4163{
Tejun Heo01f64742014-05-13 12:19:23 -04004164 lockdep_assert_held(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05004165
4166 if (!cfts || !cfts[0].ss)
4167 return -ENOENT;
4168
4169 list_del(&cfts->node);
4170 cgroup_apply_cftypes(cfts, false);
4171 cgroup_exit_cftypes(cfts);
4172 return 0;
4173}
4174
Tejun Heo8e3f6542012-04-01 12:09:55 -07004175/**
Tejun Heo80b13582014-02-12 09:29:48 -05004176 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
4177 * @cfts: zero-length name terminated array of cftypes
4178 *
4179 * Unregister @cfts. Files described by @cfts are removed from all
4180 * existing cgroups and all future cgroups won't have them either. This
4181 * function can be called anytime whether @cfts' subsys is attached or not.
4182 *
4183 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
4184 * registered.
4185 */
4186int cgroup_rm_cftypes(struct cftype *cfts)
4187{
Tejun Heo21a2d342014-02-12 09:29:49 -05004188 int ret;
Tejun Heo80b13582014-02-12 09:29:48 -05004189
Tejun Heo01f64742014-05-13 12:19:23 -04004190 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05004191 ret = cgroup_rm_cftypes_locked(cfts);
Tejun Heo01f64742014-05-13 12:19:23 -04004192 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07004193 return ret;
4194}
4195
4196/**
4197 * cgroup_add_cftypes - add an array of cftypes to a subsystem
4198 * @ss: target cgroup subsystem
4199 * @cfts: zero-length name terminated array of cftypes
4200 *
4201 * Register @cfts to @ss. Files described by @cfts are created for all
4202 * existing cgroups to which @ss is attached and all future cgroups will
4203 * have them too. This function can be called anytime whether @ss is
4204 * attached or not.
4205 *
4206 * Returns 0 on successful registration, -errno on failure. Note that this
4207 * function currently returns 0 as long as @cfts registration is successful
4208 * even if some file creation attempts on existing cgroups fail.
4209 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04004210static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07004211{
Tejun Heo9ccece82013-06-28 16:24:11 -07004212 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004213
Tejun Heofc5ed1e2015-09-18 11:56:28 -04004214 if (!cgroup_ssid_enabled(ss->id))
Li Zefanc731ae12014-06-05 17:16:30 +08004215 return 0;
4216
Li Zefandc5736e2014-02-17 10:41:50 +08004217 if (!cfts || cfts[0].name[0] == '\0')
4218 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004219
Tejun Heo2bd59d42014-02-11 11:52:49 -05004220 ret = cgroup_init_cftypes(ss, cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07004221 if (ret)
Tejun Heo2bd59d42014-02-11 11:52:49 -05004222 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004223
Tejun Heo01f64742014-05-13 12:19:23 -04004224 mutex_lock(&cgroup_mutex);
Tejun Heo21a2d342014-02-12 09:29:49 -05004225
Tejun Heo0adb0702014-02-12 09:29:48 -05004226 list_add_tail(&cfts->node, &ss->cfts);
Tejun Heo21a2d342014-02-12 09:29:49 -05004227 ret = cgroup_apply_cftypes(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07004228 if (ret)
Tejun Heo21a2d342014-02-12 09:29:49 -05004229 cgroup_rm_cftypes_locked(cfts);
4230
Tejun Heo01f64742014-05-13 12:19:23 -04004231 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07004232 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004233}
Tejun Heo79578622012-04-01 12:09:56 -07004234
4235/**
Tejun Heoa8ddc822014-07-15 11:05:10 -04004236 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
4237 * @ss: target cgroup subsystem
4238 * @cfts: zero-length name terminated array of cftypes
4239 *
4240 * Similar to cgroup_add_cftypes() but the added files are only used for
4241 * the default hierarchy.
4242 */
4243int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
4244{
4245 struct cftype *cft;
4246
4247 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
Tejun Heo05ebb6e2014-07-15 11:05:10 -04004248 cft->flags |= __CFTYPE_ONLY_ON_DFL;
Tejun Heoa8ddc822014-07-15 11:05:10 -04004249 return cgroup_add_cftypes(ss, cfts);
4250}
4251
4252/**
4253 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
4254 * @ss: target cgroup subsystem
4255 * @cfts: zero-length name terminated array of cftypes
4256 *
4257 * Similar to cgroup_add_cftypes() but the added files are only used for
4258 * the legacy hierarchies.
4259 */
Tejun Heo2cf669a2014-07-15 11:05:09 -04004260int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
4261{
Tejun Heoa8ddc822014-07-15 11:05:10 -04004262 struct cftype *cft;
4263
Tejun Heoe4b70372015-10-15 17:00:43 -04004264 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
4265 cft->flags |= __CFTYPE_NOT_ON_DFL;
Tejun Heo2cf669a2014-07-15 11:05:09 -04004266 return cgroup_add_cftypes(ss, cfts);
4267}
4268
Li Zefana043e3b2008-02-23 15:24:09 -08004269/**
Tejun Heo34c06252015-11-05 00:12:24 -05004270 * cgroup_file_notify - generate a file modified event for a cgroup_file
4271 * @cfile: target cgroup_file
4272 *
4273 * @cfile must have been obtained by setting cftype->file_offset.
4274 */
4275void cgroup_file_notify(struct cgroup_file *cfile)
4276{
4277 unsigned long flags;
4278
4279 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
Tejun Heob12e3582018-04-26 14:29:04 -07004280 if (cfile->kn) {
4281 unsigned long last = cfile->notified_at;
4282 unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
4283
4284 if (time_in_range(jiffies, last, next)) {
4285 timer_reduce(&cfile->notify_timer, next);
4286 } else {
4287 kernfs_notify(cfile->kn);
4288 cfile->notified_at = jiffies;
4289 }
4290 }
Tejun Heo34c06252015-11-05 00:12:24 -05004291 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
4292}
4293
4294/**
Tejun Heo492eb212013-08-08 20:11:25 -04004295 * css_next_child - find the next child of a given css
Tejun Heoc2931b72014-05-16 13:22:51 -04004296 * @pos: the current position (%NULL to initiate traversal)
4297 * @parent: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09004298 *
Tejun Heoc2931b72014-05-16 13:22:51 -04004299 * This function returns the next child of @parent and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05004300 * under either cgroup_mutex or RCU read lock. The only requirement is
Tejun Heoc2931b72014-05-16 13:22:51 -04004301 * that @parent and @pos are accessible. The next sibling is guaranteed to
4302 * be returned regardless of their states.
4303 *
4304 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4305 * css which finished ->css_online() is guaranteed to be visible in the
4306 * future iterations and will stay visible until the last reference is put.
4307 * A css which hasn't finished ->css_online() or already finished
4308 * ->css_offline() may show up during traversal. It's each subsystem's
4309 * responsibility to synchronize against on/offlining.
Tejun Heo53fa5262013-05-24 10:55:38 +09004310 */
Tejun Heoc2931b72014-05-16 13:22:51 -04004311struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
4312 struct cgroup_subsys_state *parent)
Tejun Heo53fa5262013-05-24 10:55:38 +09004313{
Tejun Heoc2931b72014-05-16 13:22:51 -04004314 struct cgroup_subsys_state *next;
Tejun Heo53fa5262013-05-24 10:55:38 +09004315
Tejun Heo8353da12014-05-13 12:19:23 -04004316 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09004317
4318 /*
Tejun Heode3f0342014-05-16 13:22:49 -04004319 * @pos could already have been unlinked from the sibling list.
4320 * Once a cgroup is removed, its ->sibling.next is no longer
4321 * updated when its next sibling changes. CSS_RELEASED is set when
4322 * @pos is taken off list, at which time its next pointer is valid,
4323 * and, as releases are serialized, the one pointed to by the next
4324 * pointer is guaranteed to not have started release yet. This
4325 * implies that if we observe !CSS_RELEASED on @pos in this RCU
4326 * critical section, the one pointed to by its next pointer is
4327 * guaranteed to not have finished its RCU grace period even if we
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +05304328 * have dropped rcu_read_lock() in-between iterations.
Tejun Heo3b287a52013-08-08 20:11:24 -04004329 *
Tejun Heode3f0342014-05-16 13:22:49 -04004330 * If @pos has CSS_RELEASED set, its next pointer can't be
4331 * dereferenced; however, as each css is given a monotonically
4332 * increasing unique serial number and always appended to the
4333 * sibling list, the next one can be found by walking the parent's
4334 * children until the first css with higher serial number than
4335 * @pos's. While this path can be slower, it happens iff iteration
4336 * races against release and the race window is very small.
Tejun Heo53fa5262013-05-24 10:55:38 +09004337 */
Tejun Heo3b287a52013-08-08 20:11:24 -04004338 if (!pos) {
Tejun Heoc2931b72014-05-16 13:22:51 -04004339 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
4340 } else if (likely(!(pos->flags & CSS_RELEASED))) {
4341 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04004342 } else {
Madhuparna Bhowmik3010c5b2020-01-18 08:40:51 +05304343 list_for_each_entry_rcu(next, &parent->children, sibling,
4344 lockdep_is_held(&cgroup_mutex))
Tejun Heo3b287a52013-08-08 20:11:24 -04004345 if (next->serial_nr > pos->serial_nr)
4346 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09004347 }
4348
Tejun Heo3b281af2014-04-23 11:13:15 -04004349 /*
4350 * @next, if not pointing to the head, can be dereferenced and is
Tejun Heoc2931b72014-05-16 13:22:51 -04004351 * the next sibling.
Tejun Heo3b281af2014-04-23 11:13:15 -04004352 */
Tejun Heoc2931b72014-05-16 13:22:51 -04004353 if (&next->sibling != &parent->children)
4354 return next;
Tejun Heo3b281af2014-04-23 11:13:15 -04004355 return NULL;
Tejun Heo53fa5262013-05-24 10:55:38 +09004356}
Tejun Heo53fa5262013-05-24 10:55:38 +09004357
4358/**
Tejun Heo492eb212013-08-08 20:11:25 -04004359 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004360 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04004361 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004362 *
Tejun Heo492eb212013-08-08 20:11:25 -04004363 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04004364 * to visit for pre-order traversal of @root's descendants. @root is
4365 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09004366 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004367 * While this function requires cgroup_mutex or RCU read locking, it
4368 * doesn't require the whole traversal to be contained in a single critical
4369 * section. This function will return the correct next descendant as long
4370 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heoc2931b72014-05-16 13:22:51 -04004371 *
4372 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4373 * css which finished ->css_online() is guaranteed to be visible in the
4374 * future iterations and will stay visible until the last reference is put.
4375 * A css which hasn't finished ->css_online() or already finished
4376 * ->css_offline() may show up during traversal. It's each subsystem's
4377 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08004378 */
Tejun Heo492eb212013-08-08 20:11:25 -04004379struct cgroup_subsys_state *
4380css_next_descendant_pre(struct cgroup_subsys_state *pos,
4381 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004382{
Tejun Heo492eb212013-08-08 20:11:25 -04004383 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004384
Tejun Heo8353da12014-05-13 12:19:23 -04004385 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08004386
Tejun Heobd8815a2013-08-08 20:11:27 -04004387 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09004388 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04004389 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004390
4391 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04004392 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004393 if (next)
4394 return next;
4395
4396 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04004397 while (pos != root) {
Tejun Heo5c9d5352014-05-16 13:22:48 -04004398 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09004399 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004400 return next;
Tejun Heo5c9d5352014-05-16 13:22:48 -04004401 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09004402 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08004403
4404 return NULL;
4405}
Christoph Hellwig474a2802019-06-21 10:22:48 +02004406EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004407
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004408/**
Tejun Heo492eb212013-08-08 20:11:25 -04004409 * css_rightmost_descendant - return the rightmost descendant of a css
4410 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004411 *
Tejun Heo492eb212013-08-08 20:11:25 -04004412 * Return the rightmost descendant of @pos. If there's no descendant, @pos
4413 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004414 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09004415 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004416 * While this function requires cgroup_mutex or RCU read locking, it
4417 * doesn't require the whole traversal to be contained in a single critical
4418 * section. This function will return the correct rightmost descendant as
4419 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004420 */
Tejun Heo492eb212013-08-08 20:11:25 -04004421struct cgroup_subsys_state *
4422css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004423{
Tejun Heo492eb212013-08-08 20:11:25 -04004424 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004425
Tejun Heo8353da12014-05-13 12:19:23 -04004426 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004427
4428 do {
4429 last = pos;
4430 /* ->prev isn't RCU safe, walk ->next till the end */
4431 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04004432 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004433 pos = tmp;
4434 } while (pos);
4435
4436 return last;
4437}
Tejun Heo12a9d2f2013-01-07 08:49:33 -08004438
Tejun Heo492eb212013-08-08 20:11:25 -04004439static struct cgroup_subsys_state *
4440css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004441{
Tejun Heo492eb212013-08-08 20:11:25 -04004442 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004443
4444 do {
4445 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04004446 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004447 } while (pos);
4448
4449 return last;
4450}
4451
4452/**
Tejun Heo492eb212013-08-08 20:11:25 -04004453 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004454 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04004455 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08004456 *
Tejun Heo492eb212013-08-08 20:11:25 -04004457 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04004458 * to visit for post-order traversal of @root's descendants. @root is
4459 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09004460 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05004461 * While this function requires cgroup_mutex or RCU read locking, it
4462 * doesn't require the whole traversal to be contained in a single critical
4463 * section. This function will return the correct next descendant as long
4464 * as both @pos and @cgroup are accessible and @pos is a descendant of
4465 * @cgroup.
Tejun Heoc2931b72014-05-16 13:22:51 -04004466 *
4467 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4468 * css which finished ->css_online() is guaranteed to be visible in the
4469 * future iterations and will stay visible until the last reference is put.
4470 * A css which hasn't finished ->css_online() or already finished
4471 * ->css_offline() may show up during traversal. It's each subsystem's
4472 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -08004473 */
Tejun Heo492eb212013-08-08 20:11:25 -04004474struct cgroup_subsys_state *
4475css_next_descendant_post(struct cgroup_subsys_state *pos,
4476 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08004477{
Tejun Heo492eb212013-08-08 20:11:25 -04004478 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004479
Tejun Heo8353da12014-05-13 12:19:23 -04004480 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08004481
Tejun Heo58b79a92013-09-06 15:31:08 -04004482 /* if first iteration, visit leftmost descendant which may be @root */
4483 if (!pos)
4484 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004485
Tejun Heobd8815a2013-08-08 20:11:27 -04004486 /* if we visited @root, we're done */
4487 if (pos == root)
4488 return NULL;
4489
Tejun Heo574bd9f2012-11-09 09:12:29 -08004490 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004491 next = css_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09004492 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04004493 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08004494
4495 /* no sibling left, visit parent */
Tejun Heo5c9d5352014-05-16 13:22:48 -04004496 return pos->parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -08004497}
Tejun Heo574bd9f2012-11-09 09:12:29 -08004498
Tejun Heof3d46502014-05-16 13:22:52 -04004499/**
4500 * css_has_online_children - does a css have online children
4501 * @css: the target css
4502 *
4503 * Returns %true if @css has any online children; otherwise, %false. This
4504 * function can be called from any context but the caller is responsible
4505 * for synchronizing against on/offlining as necessary.
4506 */
4507bool css_has_online_children(struct cgroup_subsys_state *css)
Tejun Heocbc125e2014-05-14 09:15:01 -04004508{
Tejun Heof3d46502014-05-16 13:22:52 -04004509 struct cgroup_subsys_state *child;
4510 bool ret = false;
Tejun Heocbc125e2014-05-14 09:15:01 -04004511
4512 rcu_read_lock();
Tejun Heof3d46502014-05-16 13:22:52 -04004513 css_for_each_child(child, css) {
Li Zefan99bae5f2014-06-12 14:31:31 +08004514 if (child->flags & CSS_ONLINE) {
Tejun Heof3d46502014-05-16 13:22:52 -04004515 ret = true;
4516 break;
Tejun Heocbc125e2014-05-14 09:15:01 -04004517 }
4518 }
4519 rcu_read_unlock();
Tejun Heof3d46502014-05-16 13:22:52 -04004520 return ret;
Cliff Wickman31a7df02008-02-07 00:14:42 -08004521}
4522
Tejun Heo450ee0c2017-05-15 09:34:03 -04004523static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it)
4524{
4525 struct list_head *l;
4526 struct cgrp_cset_link *link;
4527 struct css_set *cset;
4528
4529 lockdep_assert_held(&css_set_lock);
4530
4531 /* find the next threaded cset */
4532 if (it->tcset_pos) {
4533 l = it->tcset_pos->next;
4534
4535 if (l != it->tcset_head) {
4536 it->tcset_pos = l;
4537 return container_of(l, struct css_set,
4538 threaded_csets_node);
4539 }
4540
4541 it->tcset_pos = NULL;
4542 }
4543
4544 /* find the next cset */
4545 l = it->cset_pos;
4546 l = l->next;
4547 if (l == it->cset_head) {
4548 it->cset_pos = NULL;
4549 return NULL;
4550 }
4551
4552 if (it->ss) {
4553 cset = container_of(l, struct css_set, e_cset_node[it->ss->id]);
4554 } else {
4555 link = list_entry(l, struct cgrp_cset_link, cset_link);
4556 cset = link->cset;
4557 }
4558
4559 it->cset_pos = l;
4560
4561 /* initialize threaded css_set walking */
4562 if (it->flags & CSS_TASK_ITER_THREADED) {
4563 if (it->cur_dcset)
4564 put_css_set_locked(it->cur_dcset);
4565 it->cur_dcset = cset;
4566 get_css_set(cset);
4567
4568 it->tcset_head = &cset->threaded_csets;
4569 it->tcset_pos = &cset->threaded_csets;
4570 }
4571
4572 return cset;
4573}
4574
Tejun Heo0942eee2013-08-08 20:11:26 -04004575/**
Bhaskar Chowdhury58315c92020-11-09 16:01:11 +05304576 * css_task_iter_advance_css_set - advance a task iterator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04004577 * @it: the iterator to advance
4578 *
4579 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04004580 */
Tejun Heoecb9d532015-10-15 16:41:52 -04004581static void css_task_iter_advance_css_set(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04004582{
Tejun Heod5158762013-08-08 20:11:26 -04004583 struct css_set *cset;
4584
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004585 lockdep_assert_held(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004586
Michal Koutnýf43caa22020-01-24 12:40:16 +01004587 /* Advance to the next non-empty css_set and find first non-empty tasks list*/
4588 while ((cset = css_task_iter_next_css_set(it))) {
4589 if (!list_empty(&cset->tasks)) {
4590 it->cur_tasks_head = &cset->tasks;
4591 break;
4592 } else if (!list_empty(&cset->mg_tasks)) {
4593 it->cur_tasks_head = &cset->mg_tasks;
4594 break;
4595 } else if (!list_empty(&cset->dying_tasks)) {
4596 it->cur_tasks_head = &cset->dying_tasks;
4597 break;
Tejun Heod5158762013-08-08 20:11:26 -04004598 }
Michal Koutný9c974c72020-01-24 12:40:15 +01004599 }
Michal Koutnýf43caa22020-01-24 12:40:16 +01004600 if (!cset) {
4601 it->task_pos = NULL;
4602 return;
4603 }
4604 it->task_pos = it->cur_tasks_head->next;
Tejun Heoed27b9f2015-10-15 16:41:52 -04004605
4606 /*
4607 * We don't keep css_sets locked across iteration steps and thus
4608 * need to take steps to ensure that iteration can be resumed after
4609 * the lock is re-acquired. Iteration is performed at two levels -
4610 * css_sets and tasks in them.
4611 *
4612 * Once created, a css_set never leaves its cgroup lists, so a
4613 * pinned css_set is guaranteed to stay put and we can resume
4614 * iteration afterwards.
4615 *
4616 * Tasks may leave @cset across iteration steps. This is resolved
4617 * by registering each iterator with the css_set currently being
4618 * walked and making css_set_move_task() advance iterators whose
4619 * next task is leaving.
4620 */
4621 if (it->cur_cset) {
4622 list_del(&it->iters_node);
4623 put_css_set_locked(it->cur_cset);
4624 }
4625 get_css_set(cset);
4626 it->cur_cset = cset;
4627 list_add(&it->iters_node, &cset->task_iters);
Tejun Heod5158762013-08-08 20:11:26 -04004628}
4629
Tejun Heob636fd32019-05-31 10:38:58 -07004630static void css_task_iter_skip(struct css_task_iter *it,
4631 struct task_struct *task)
4632{
4633 lockdep_assert_held(&css_set_lock);
4634
4635 if (it->task_pos == &task->cg_list) {
4636 it->task_pos = it->task_pos->next;
4637 it->flags |= CSS_TASK_ITER_SKIPPED;
4638 }
4639}
4640
Tejun Heoecb9d532015-10-15 16:41:52 -04004641static void css_task_iter_advance(struct css_task_iter *it)
4642{
Tejun Heoc03cd772019-05-31 10:38:58 -07004643 struct task_struct *task;
Tejun Heoecb9d532015-10-15 16:41:52 -04004644
Tejun Heof0d9a5f2015-10-15 16:41:53 -04004645 lockdep_assert_held(&css_set_lock);
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004646repeat:
Tejun Heoe9d81a12018-11-08 12:15:15 -08004647 if (it->task_pos) {
4648 /*
Michal Koutnýf43caa22020-01-24 12:40:16 +01004649 * Advance iterator to find next entry. We go through cset
4650 * tasks, mg_tasks and dying_tasks, when consumed we move onto
4651 * the next cset.
Tejun Heoe9d81a12018-11-08 12:15:15 -08004652 */
Tejun Heob636fd32019-05-31 10:38:58 -07004653 if (it->flags & CSS_TASK_ITER_SKIPPED)
4654 it->flags &= ~CSS_TASK_ITER_SKIPPED;
Tejun Heoe9d81a12018-11-08 12:15:15 -08004655 else
Tejun Heob636fd32019-05-31 10:38:58 -07004656 it->task_pos = it->task_pos->next;
4657
Michal Koutnýf43caa22020-01-24 12:40:16 +01004658 if (it->task_pos == &it->cur_cset->tasks) {
4659 it->cur_tasks_head = &it->cur_cset->mg_tasks;
4660 it->task_pos = it->cur_tasks_head->next;
Michal Koutný9c974c72020-01-24 12:40:15 +01004661 }
Michal Koutnýf43caa22020-01-24 12:40:16 +01004662 if (it->task_pos == &it->cur_cset->mg_tasks) {
4663 it->cur_tasks_head = &it->cur_cset->dying_tasks;
4664 it->task_pos = it->cur_tasks_head->next;
Michal Koutný9c974c72020-01-24 12:40:15 +01004665 }
Michal Koutnýf43caa22020-01-24 12:40:16 +01004666 if (it->task_pos == &it->cur_cset->dying_tasks)
Tejun Heob636fd32019-05-31 10:38:58 -07004667 css_task_iter_advance_css_set(it);
Tejun Heoe9d81a12018-11-08 12:15:15 -08004668 } else {
4669 /* called from start, proceed to the first cset */
Tejun Heoecb9d532015-10-15 16:41:52 -04004670 css_task_iter_advance_css_set(it);
Tejun Heoe9d81a12018-11-08 12:15:15 -08004671 }
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004672
Tejun Heoc03cd772019-05-31 10:38:58 -07004673 if (!it->task_pos)
4674 return;
4675
4676 task = list_entry(it->task_pos, struct task_struct, cg_list);
4677
4678 if (it->flags & CSS_TASK_ITER_PROCS) {
4679 /* if PROCS, skip over tasks which aren't group leaders */
4680 if (!thread_group_leader(task))
4681 goto repeat;
4682
4683 /* and dying leaders w/o live member threads */
Michal Koutnýf43caa22020-01-24 12:40:16 +01004684 if (it->cur_tasks_head == &it->cur_cset->dying_tasks &&
Michal Koutný9c974c72020-01-24 12:40:15 +01004685 !atomic_read(&task->signal->live))
Tejun Heoc03cd772019-05-31 10:38:58 -07004686 goto repeat;
4687 } else {
4688 /* skip all dying ones */
Michal Koutnýf43caa22020-01-24 12:40:16 +01004689 if (it->cur_tasks_head == &it->cur_cset->dying_tasks)
Tejun Heoc03cd772019-05-31 10:38:58 -07004690 goto repeat;
4691 }
Tejun Heoecb9d532015-10-15 16:41:52 -04004692}
4693
Tejun Heo0942eee2013-08-08 20:11:26 -04004694/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004695 * css_task_iter_start - initiate task iteration
4696 * @css: the css to walk tasks of
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004697 * @flags: CSS_TASK_ITER_* flags
Tejun Heo0942eee2013-08-08 20:11:26 -04004698 * @it: the task iterator to use
4699 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004700 * Initiate iteration through the tasks of @css. The caller can call
4701 * css_task_iter_next() to walk through the tasks until the function
4702 * returns NULL. On completion of iteration, css_task_iter_end() must be
4703 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04004704 */
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004705void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
Tejun Heo72ec7022013-08-08 20:11:26 -04004706 struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004707{
Tejun Heoed27b9f2015-10-15 16:41:52 -04004708 memset(it, 0, sizeof(*it));
4709
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004710 spin_lock_irq(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004711
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004712 it->ss = css->ss;
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004713 it->flags = flags;
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004714
Kees Cookd20d30e2021-08-27 17:02:55 -07004715 if (CGROUP_HAS_SUBSYS_CONFIG && it->ss)
Tejun Heo3ebb2b62014-04-23 11:13:15 -04004716 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
4717 else
4718 it->cset_pos = &css->cgroup->cset_links;
4719
Tejun Heo0f0a2b42014-04-23 11:13:15 -04004720 it->cset_head = it->cset_pos;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04004721
Tejun Heoe9d81a12018-11-08 12:15:15 -08004722 css_task_iter_advance(it);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004723
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004724 spin_unlock_irq(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004725}
Paul Menage817929e2007-10-18 23:39:36 -07004726
Tejun Heo0942eee2013-08-08 20:11:26 -04004727/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004728 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04004729 * @it: the task iterator being iterated
4730 *
4731 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04004732 * initialized via css_task_iter_start(). Returns NULL when the iteration
4733 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04004734 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004735struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004736{
Tejun Heod5745672015-10-29 11:43:05 +09004737 if (it->cur_task) {
Tejun Heoed27b9f2015-10-15 16:41:52 -04004738 put_task_struct(it->cur_task);
Tejun Heod5745672015-10-29 11:43:05 +09004739 it->cur_task = NULL;
4740 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004741
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004742 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004743
Tejun Heocee0c332019-06-05 09:54:34 -07004744 /* @it may be half-advanced by skips, finish advancing */
4745 if (it->flags & CSS_TASK_ITER_SKIPPED)
4746 css_task_iter_advance(it);
4747
Tejun Heod5745672015-10-29 11:43:05 +09004748 if (it->task_pos) {
4749 it->cur_task = list_entry(it->task_pos, struct task_struct,
4750 cg_list);
4751 get_task_struct(it->cur_task);
4752 css_task_iter_advance(it);
4753 }
Tejun Heoed27b9f2015-10-15 16:41:52 -04004754
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004755 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004756
4757 return it->cur_task;
Paul Menage817929e2007-10-18 23:39:36 -07004758}
4759
Tejun Heo0942eee2013-08-08 20:11:26 -04004760/**
Tejun Heo72ec7022013-08-08 20:11:26 -04004761 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04004762 * @it: the task iterator to finish
4763 *
Tejun Heo72ec7022013-08-08 20:11:26 -04004764 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04004765 */
Tejun Heo72ec7022013-08-08 20:11:26 -04004766void css_task_iter_end(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07004767{
Tejun Heoed27b9f2015-10-15 16:41:52 -04004768 if (it->cur_cset) {
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004769 spin_lock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004770 list_del(&it->iters_node);
4771 put_css_set_locked(it->cur_cset);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03004772 spin_unlock_irq(&css_set_lock);
Tejun Heoed27b9f2015-10-15 16:41:52 -04004773 }
4774
Tejun Heo450ee0c2017-05-15 09:34:03 -04004775 if (it->cur_dcset)
4776 put_css_set(it->cur_dcset);
4777
Tejun Heoed27b9f2015-10-15 16:41:52 -04004778 if (it->cur_task)
4779 put_task_struct(it->cur_task);
Tejun Heo8cc99342013-04-07 09:29:50 -07004780}
4781
Tejun Heob4b90a82016-12-27 14:49:04 -05004782static void cgroup_procs_release(struct kernfs_open_file *of)
Tejun Heo8cc99342013-04-07 09:29:50 -07004783{
Tejun Heo0d2b59552022-01-06 11:02:29 -10004784 struct cgroup_file_ctx *ctx = of->priv;
4785
4786 if (ctx->procs.started)
4787 css_task_iter_end(&ctx->procs.iter);
Tejun Heob4b90a82016-12-27 14:49:04 -05004788}
4789
4790static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
4791{
4792 struct kernfs_open_file *of = s->private;
Tejun Heo0d2b59552022-01-06 11:02:29 -10004793 struct cgroup_file_ctx *ctx = of->priv;
Tejun Heoe406d1c2014-02-13 06:58:39 -05004794
Vasily Averin2d4ecb02020-01-30 13:34:59 +03004795 if (pos)
4796 (*pos)++;
4797
Tejun Heo0d2b59552022-01-06 11:02:29 -10004798 return css_task_iter_next(&ctx->procs.iter);
Tejun Heo8cc99342013-04-07 09:29:50 -07004799}
4800
Tejun Heo8cfd8142017-07-21 11:14:51 -04004801static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
4802 unsigned int iter_flags)
Ben Blumd1d9fd32009-09-23 15:56:28 -07004803{
Tejun Heo2bd59d42014-02-11 11:52:49 -05004804 struct kernfs_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05004805 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo0d2b59552022-01-06 11:02:29 -10004806 struct cgroup_file_ctx *ctx = of->priv;
4807 struct css_task_iter *it = &ctx->procs.iter;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004808
4809 /*
Tejun Heob4b90a82016-12-27 14:49:04 -05004810 * When a seq_file is seeked, it's always traversed sequentially
4811 * from position 0, so we can simply keep iterating on !0 *pos.
Tejun Heo4bac00d2013-11-29 10:42:59 -05004812 */
Tejun Heo0d2b59552022-01-06 11:02:29 -10004813 if (!ctx->procs.started) {
Vasily Averin2d4ecb02020-01-30 13:34:59 +03004814 if (WARN_ON_ONCE((*pos)))
Tejun Heob4b90a82016-12-27 14:49:04 -05004815 return ERR_PTR(-EINVAL);
Tejun Heo450ee0c2017-05-15 09:34:03 -04004816 css_task_iter_start(&cgrp->self, iter_flags, it);
Tejun Heo0d2b59552022-01-06 11:02:29 -10004817 ctx->procs.started = true;
Vasily Averin2d4ecb02020-01-30 13:34:59 +03004818 } else if (!(*pos)) {
Tejun Heob4b90a82016-12-27 14:49:04 -05004819 css_task_iter_end(it);
Tejun Heo450ee0c2017-05-15 09:34:03 -04004820 css_task_iter_start(&cgrp->self, iter_flags, it);
Vasily Averin2d4ecb02020-01-30 13:34:59 +03004821 } else
4822 return it->cur_task;
Tejun Heo4bac00d2013-11-29 10:42:59 -05004823
Tejun Heob4b90a82016-12-27 14:49:04 -05004824 return cgroup_procs_next(s, NULL, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07004825}
4826
Tejun Heo8cfd8142017-07-21 11:14:51 -04004827static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
4828{
4829 struct cgroup *cgrp = seq_css(s)->cgroup;
4830
4831 /*
4832 * All processes of a threaded subtree belong to the domain cgroup
4833 * of the subtree. Only threads can be distributed across the
4834 * subtree. Reject reads on cgroup.procs in the subtree proper.
4835 * They're always empty anyway.
4836 */
4837 if (cgroup_is_threaded(cgrp))
4838 return ERR_PTR(-EOPNOTSUPP);
4839
4840 return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS |
4841 CSS_TASK_ITER_THREADED);
4842}
4843
Tejun Heob4b90a82016-12-27 14:49:04 -05004844static int cgroup_procs_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07004845{
Tejun Heobc2fb7e2017-05-15 09:34:01 -04004846 seq_printf(s, "%d\n", task_pid_vnr(v));
Daniel Lezcano97978e62010-10-27 15:33:35 -07004847 return 0;
4848}
4849
Christian Braunerf3553222020-02-05 14:26:21 +01004850static int cgroup_may_write(const struct cgroup *cgrp, struct super_block *sb)
4851{
4852 int ret;
4853 struct inode *inode;
4854
4855 lockdep_assert_held(&cgroup_mutex);
4856
4857 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
4858 if (!inode)
4859 return -ENOMEM;
4860
Christian Brauner47291ba2021-01-21 14:19:24 +01004861 ret = inode_permission(&init_user_ns, inode, MAY_WRITE);
Christian Braunerf3553222020-02-05 14:26:21 +01004862 iput(inode);
4863 return ret;
4864}
4865
Tejun Heo715c8092017-05-15 09:34:00 -04004866static int cgroup_procs_write_permission(struct cgroup *src_cgrp,
4867 struct cgroup *dst_cgrp,
Tejun Heoe5745762022-01-06 11:02:29 -10004868 struct super_block *sb,
4869 struct cgroup_namespace *ns)
Tejun Heo715c8092017-05-15 09:34:00 -04004870{
Tejun Heo715c8092017-05-15 09:34:00 -04004871 struct cgroup *com_cgrp = src_cgrp;
Tejun Heo715c8092017-05-15 09:34:00 -04004872 int ret;
4873
4874 lockdep_assert_held(&cgroup_mutex);
4875
4876 /* find the common ancestor */
4877 while (!cgroup_is_descendant(dst_cgrp, com_cgrp))
4878 com_cgrp = cgroup_parent(com_cgrp);
4879
4880 /* %current should be authorized to migrate to the common ancestor */
Christian Braunerf3553222020-02-05 14:26:21 +01004881 ret = cgroup_may_write(com_cgrp, sb);
Tejun Heo715c8092017-05-15 09:34:00 -04004882 if (ret)
4883 return ret;
4884
4885 /*
4886 * If namespaces are delegation boundaries, %current must be able
4887 * to see both source and destination cgroups from its namespace.
4888 */
4889 if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) &&
4890 (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) ||
4891 !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp)))
4892 return -ENOENT;
4893
4894 return 0;
4895}
4896
Christian Brauner6df970e2020-02-05 14:26:18 +01004897static int cgroup_attach_permissions(struct cgroup *src_cgrp,
4898 struct cgroup *dst_cgrp,
Tejun Heoe5745762022-01-06 11:02:29 -10004899 struct super_block *sb, bool threadgroup,
4900 struct cgroup_namespace *ns)
Christian Brauner6df970e2020-02-05 14:26:18 +01004901{
4902 int ret = 0;
4903
Tejun Heoe5745762022-01-06 11:02:29 -10004904 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, sb, ns);
Christian Brauner6df970e2020-02-05 14:26:18 +01004905 if (ret)
4906 return ret;
4907
4908 ret = cgroup_migrate_vet_dst(dst_cgrp);
4909 if (ret)
4910 return ret;
4911
4912 if (!threadgroup && (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp))
4913 ret = -EOPNOTSUPP;
4914
4915 return ret;
4916}
4917
Michal Koutnýda708622021-01-14 13:44:27 +01004918static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
4919 bool threadgroup)
Tejun Heo715c8092017-05-15 09:34:00 -04004920{
Tejun Heoe5745762022-01-06 11:02:29 -10004921 struct cgroup_file_ctx *ctx = of->priv;
Tejun Heo715c8092017-05-15 09:34:00 -04004922 struct cgroup *src_cgrp, *dst_cgrp;
4923 struct task_struct *task;
Tejun Heo1756d792022-01-06 11:02:28 -10004924 const struct cred *saved_cred;
Tejun Heo715c8092017-05-15 09:34:00 -04004925 ssize_t ret;
Michal Koutný9a3284f2019-10-04 12:57:40 +02004926 bool locked;
Tejun Heo715c8092017-05-15 09:34:00 -04004927
4928 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4929 if (!dst_cgrp)
4930 return -ENODEV;
4931
Michal Koutnýda708622021-01-14 13:44:27 +01004932 task = cgroup_procs_write_start(buf, threadgroup, &locked);
Tejun Heo715c8092017-05-15 09:34:00 -04004933 ret = PTR_ERR_OR_ZERO(task);
4934 if (ret)
4935 goto out_unlock;
4936
4937 /* find the source cgroup */
4938 spin_lock_irq(&css_set_lock);
4939 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4940 spin_unlock_irq(&css_set_lock);
4941
Tejun Heo1756d792022-01-06 11:02:28 -10004942 /*
4943 * Process and thread migrations follow same delegation rule. Check
4944 * permissions using the credentials from file open to protect against
4945 * inherited fd attacks.
4946 */
4947 saved_cred = override_creds(of->file->f_cred);
Christian Brauner6df970e2020-02-05 14:26:18 +01004948 ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
Tejun Heoe5745762022-01-06 11:02:29 -10004949 of->file->f_path.dentry->d_sb,
4950 threadgroup, ctx->ns);
Tejun Heo1756d792022-01-06 11:02:28 -10004951 revert_creds(saved_cred);
Tejun Heo715c8092017-05-15 09:34:00 -04004952 if (ret)
4953 goto out_finish;
4954
Michal Koutnýda708622021-01-14 13:44:27 +01004955 ret = cgroup_attach_task(dst_cgrp, task, threadgroup);
Tejun Heo715c8092017-05-15 09:34:00 -04004956
4957out_finish:
Michal Koutný9a3284f2019-10-04 12:57:40 +02004958 cgroup_procs_write_finish(task, locked);
Tejun Heo715c8092017-05-15 09:34:00 -04004959out_unlock:
4960 cgroup_kn_unlock(of->kn);
4961
Michal Koutnýda708622021-01-14 13:44:27 +01004962 return ret;
4963}
4964
4965static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
4966 char *buf, size_t nbytes, loff_t off)
4967{
4968 return __cgroup_procs_write(of, buf, true) ?: nbytes;
Tejun Heo715c8092017-05-15 09:34:00 -04004969}
4970
Tejun Heo8cfd8142017-07-21 11:14:51 -04004971static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
4972{
4973 return __cgroup_procs_start(s, pos, 0);
4974}
4975
4976static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
4977 char *buf, size_t nbytes, loff_t off)
4978{
Michal Koutnýda708622021-01-14 13:44:27 +01004979 return __cgroup_procs_write(of, buf, false) ?: nbytes;
Tejun Heo8cfd8142017-07-21 11:14:51 -04004980}
4981
Tejun Heoa14c6872014-07-15 11:05:09 -04004982/* cgroup core interface files for the default hierarchy */
Tejun Heod62beb72016-12-27 14:49:08 -05004983static struct cftype cgroup_base_files[] = {
Tejun Heoa14c6872014-07-15 11:05:09 -04004984 {
Tejun Heo8cfd8142017-07-21 11:14:51 -04004985 .name = "cgroup.type",
4986 .flags = CFTYPE_NOT_ON_ROOT,
4987 .seq_show = cgroup_type_show,
4988 .write = cgroup_type_write,
4989 },
4990 {
Tejun Heoa14c6872014-07-15 11:05:09 -04004991 .name = "cgroup.procs",
Tejun Heo5136f632017-06-27 14:30:28 -04004992 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heo6f60ead2015-09-18 17:54:23 -04004993 .file_offset = offsetof(struct cgroup, procs_file),
Tejun Heob4b90a82016-12-27 14:49:04 -05004994 .release = cgroup_procs_release,
4995 .seq_start = cgroup_procs_start,
4996 .seq_next = cgroup_procs_next,
4997 .seq_show = cgroup_procs_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04004998 .write = cgroup_procs_write,
Tejun Heoa14c6872014-07-15 11:05:09 -04004999 },
5000 {
Tejun Heo8cfd8142017-07-21 11:14:51 -04005001 .name = "cgroup.threads",
Roman Gushchin4f584242018-01-10 04:35:12 -08005002 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heo8cfd8142017-07-21 11:14:51 -04005003 .release = cgroup_procs_release,
5004 .seq_start = cgroup_threads_start,
5005 .seq_next = cgroup_procs_next,
5006 .seq_show = cgroup_procs_show,
5007 .write = cgroup_threads_write,
5008 },
5009 {
Tejun Heoa14c6872014-07-15 11:05:09 -04005010 .name = "cgroup.controllers",
Tejun Heoa14c6872014-07-15 11:05:09 -04005011 .seq_show = cgroup_controllers_show,
5012 },
5013 {
5014 .name = "cgroup.subtree_control",
Tejun Heo5136f632017-06-27 14:30:28 -04005015 .flags = CFTYPE_NS_DELEGATABLE,
Tejun Heoa14c6872014-07-15 11:05:09 -04005016 .seq_show = cgroup_subtree_control_show,
5017 .write = cgroup_subtree_control_write,
5018 },
5019 {
Tejun Heo4a07c222015-09-18 17:54:22 -04005020 .name = "cgroup.events",
Tejun Heoa14c6872014-07-15 11:05:09 -04005021 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo6f60ead2015-09-18 17:54:23 -04005022 .file_offset = offsetof(struct cgroup, events_file),
Tejun Heo4a07c222015-09-18 17:54:22 -04005023 .seq_show = cgroup_events_show,
Tejun Heoa14c6872014-07-15 11:05:09 -04005024 },
Roman Gushchin1a926e02017-07-28 18:28:44 +01005025 {
5026 .name = "cgroup.max.descendants",
5027 .seq_show = cgroup_max_descendants_show,
5028 .write = cgroup_max_descendants_write,
5029 },
5030 {
5031 .name = "cgroup.max.depth",
5032 .seq_show = cgroup_max_depth_show,
5033 .write = cgroup_max_depth_write,
5034 },
Roman Gushchinec392252017-08-02 17:55:31 +01005035 {
5036 .name = "cgroup.stat",
Tejun Heo3e489302017-08-11 05:49:01 -07005037 .seq_show = cgroup_stat_show,
Roman Gushchinec392252017-08-02 17:55:31 +01005038 },
Tejun Heod41bf8c2017-10-23 16:18:27 -07005039 {
Roman Gushchin76f969e2019-04-19 10:03:04 -07005040 .name = "cgroup.freeze",
5041 .flags = CFTYPE_NOT_ON_ROOT,
5042 .seq_show = cgroup_freeze_show,
5043 .write = cgroup_freeze_write,
5044 },
5045 {
Christian Brauner661ee622021-05-08 14:15:38 +02005046 .name = "cgroup.kill",
5047 .flags = CFTYPE_NOT_ON_ROOT,
5048 .write = cgroup_kill_write,
5049 },
5050 {
Tejun Heod41bf8c2017-10-23 16:18:27 -07005051 .name = "cpu.stat",
Tejun Heod41bf8c2017-10-23 16:18:27 -07005052 .seq_show = cpu_stat_show,
5053 },
Johannes Weiner2ce71352018-10-26 15:06:31 -07005054#ifdef CONFIG_PSI
5055 {
5056 .name = "io.pressure",
Suren Baghdasaryan3958e2d2021-05-24 12:53:39 -07005057 .flags = CFTYPE_PRESSURE,
Johannes Weiner2ce71352018-10-26 15:06:31 -07005058 .seq_show = cgroup_io_pressure_show,
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07005059 .write = cgroup_io_pressure_write,
5060 .poll = cgroup_pressure_poll,
5061 .release = cgroup_pressure_release,
Johannes Weiner2ce71352018-10-26 15:06:31 -07005062 },
5063 {
5064 .name = "memory.pressure",
Suren Baghdasaryan3958e2d2021-05-24 12:53:39 -07005065 .flags = CFTYPE_PRESSURE,
Johannes Weiner2ce71352018-10-26 15:06:31 -07005066 .seq_show = cgroup_memory_pressure_show,
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07005067 .write = cgroup_memory_pressure_write,
5068 .poll = cgroup_pressure_poll,
5069 .release = cgroup_pressure_release,
Johannes Weiner2ce71352018-10-26 15:06:31 -07005070 },
5071 {
5072 .name = "cpu.pressure",
Suren Baghdasaryan3958e2d2021-05-24 12:53:39 -07005073 .flags = CFTYPE_PRESSURE,
Johannes Weiner2ce71352018-10-26 15:06:31 -07005074 .seq_show = cgroup_cpu_pressure_show,
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07005075 .write = cgroup_cpu_pressure_write,
5076 .poll = cgroup_pressure_poll,
5077 .release = cgroup_pressure_release,
Johannes Weiner2ce71352018-10-26 15:06:31 -07005078 },
Suren Baghdasaryan0e946822019-05-14 15:41:15 -07005079#endif /* CONFIG_PSI */
Tejun Heoa14c6872014-07-15 11:05:09 -04005080 { } /* terminate */
5081};
5082
Tejun Heo0c21ead2013-08-13 20:22:51 -04005083/*
5084 * css destruction is four-stage process.
5085 *
5086 * 1. Destruction starts. Killing of the percpu_ref is initiated.
5087 * Implemented in kill_css().
5088 *
5089 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
Tejun Heoec903c02014-05-13 12:11:01 -04005090 * and thus css_tryget_online() is guaranteed to fail, the css can be
5091 * offlined by invoking offline_css(). After offlining, the base ref is
5092 * put. Implemented in css_killed_work_fn().
Tejun Heo0c21ead2013-08-13 20:22:51 -04005093 *
5094 * 3. When the percpu_ref reaches zero, the only possible remaining
5095 * accessors are inside RCU read sections. css_release() schedules the
5096 * RCU callback.
5097 *
5098 * 4. After the grace period, the css can be freed. Implemented in
5099 * css_free_work_fn().
5100 *
5101 * It is actually hairier because both step 2 and 4 require process context
5102 * and thus involve punting to css->destroy_work adding two additional
5103 * steps to the already complex sequence.
5104 */
Tejun Heo8f36aae2018-03-14 12:45:14 -07005105static void css_free_rwork_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07005106{
Tejun Heo8f36aae2018-03-14 12:45:14 -07005107 struct cgroup_subsys_state *css = container_of(to_rcu_work(work),
5108 struct cgroup_subsys_state, destroy_rwork);
Vladimir Davydov01e58652015-02-12 14:59:26 -08005109 struct cgroup_subsys *ss = css->ss;
Tejun Heo0c21ead2013-08-13 20:22:51 -04005110 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07005111
Tejun Heo9a1049d2014-06-28 08:10:14 -04005112 percpu_ref_exit(&css->refcnt);
5113
Vladimir Davydov01e58652015-02-12 14:59:26 -08005114 if (ss) {
Tejun Heo9d755d32014-05-14 09:15:02 -04005115 /* css free path */
Tejun Heo8bb5ef72016-01-21 15:32:15 -05005116 struct cgroup_subsys_state *parent = css->parent;
Vladimir Davydov01e58652015-02-12 14:59:26 -08005117 int id = css->id;
5118
Vladimir Davydov01e58652015-02-12 14:59:26 -08005119 ss->css_free(css);
5120 cgroup_idr_remove(&ss->css_idr, id);
Tejun Heo9d755d32014-05-14 09:15:02 -04005121 cgroup_put(cgrp);
Tejun Heo8bb5ef72016-01-21 15:32:15 -05005122
5123 if (parent)
5124 css_put(parent);
Tejun Heo9d755d32014-05-14 09:15:02 -04005125 } else {
5126 /* cgroup free path */
5127 atomic_dec(&cgrp->root->nr_cgrps);
Tejun Heod62beb72016-12-27 14:49:08 -05005128 cgroup1_pidlist_destroy_all(cgrp);
Zefan Li971ff492014-09-18 16:06:19 +08005129 cancel_work_sync(&cgrp->release_agent_work);
Tejun Heo9d755d32014-05-14 09:15:02 -04005130
Tejun Heod51f39b2014-05-16 13:22:48 -04005131 if (cgroup_parent(cgrp)) {
Tejun Heo9d755d32014-05-14 09:15:02 -04005132 /*
5133 * We get a ref to the parent, and put the ref when
5134 * this cgroup is being freed, so it's guaranteed
5135 * that the parent won't be destroyed before its
5136 * children.
5137 */
Tejun Heod51f39b2014-05-16 13:22:48 -04005138 cgroup_put(cgroup_parent(cgrp));
Tejun Heo9d755d32014-05-14 09:15:02 -04005139 kernfs_put(cgrp->kn);
Johannes Weiner2ce71352018-10-26 15:06:31 -07005140 psi_cgroup_free(cgrp);
Johannes Weinera7df69b2021-04-29 22:56:20 -07005141 cgroup_rstat_exit(cgrp);
Tejun Heo9d755d32014-05-14 09:15:02 -04005142 kfree(cgrp);
5143 } else {
5144 /*
5145 * This is root cgroup's refcnt reaching zero,
5146 * which indicates that the root should be
5147 * released.
5148 */
5149 cgroup_destroy_root(cgrp->root);
5150 }
5151 }
Tejun Heo0c21ead2013-08-13 20:22:51 -04005152}
5153
Tejun Heo25e15d82014-05-14 09:15:02 -04005154static void css_release_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005155{
5156 struct cgroup_subsys_state *css =
Tejun Heo25e15d82014-05-14 09:15:02 -04005157 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo15a4c832014-05-04 15:09:14 -04005158 struct cgroup_subsys *ss = css->ss;
Tejun Heo9d755d32014-05-14 09:15:02 -04005159 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07005160
Tejun Heo1fed1b22014-05-16 13:22:49 -04005161 mutex_lock(&cgroup_mutex);
5162
Tejun Heode3f0342014-05-16 13:22:49 -04005163 css->flags |= CSS_RELEASED;
Tejun Heo1fed1b22014-05-16 13:22:49 -04005164 list_del_rcu(&css->sibling);
5165
Tejun Heo9d755d32014-05-14 09:15:02 -04005166 if (ss) {
5167 /* css release path */
Tejun Heo8f534702018-04-26 14:29:05 -07005168 if (!list_empty(&css->rstat_css_node)) {
5169 cgroup_rstat_flush(cgrp);
5170 list_del_rcu(&css->rstat_css_node);
5171 }
5172
Vladimir Davydov01e58652015-02-12 14:59:26 -08005173 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
Tejun Heo7d172cc2014-11-18 02:49:51 -05005174 if (ss->css_released)
5175 ss->css_released(css);
Tejun Heo9d755d32014-05-14 09:15:02 -04005176 } else {
Roman Gushchin0679dee2017-08-02 17:55:29 +01005177 struct cgroup *tcgrp;
5178
Tejun Heo9d755d32014-05-14 09:15:02 -04005179 /* cgroup release path */
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04005180 TRACE_CGROUP_PATH(release, cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04005181
Johannes Weinera7df69b2021-04-29 22:56:20 -07005182 cgroup_rstat_flush(cgrp);
Tejun Heo041cd642017-09-25 08:12:05 -07005183
Roman Gushchin4dcabec2019-04-19 10:03:03 -07005184 spin_lock_irq(&css_set_lock);
Roman Gushchin0679dee2017-08-02 17:55:29 +01005185 for (tcgrp = cgroup_parent(cgrp); tcgrp;
5186 tcgrp = cgroup_parent(tcgrp))
5187 tcgrp->nr_dying_descendants--;
Roman Gushchin4dcabec2019-04-19 10:03:03 -07005188 spin_unlock_irq(&css_set_lock);
Roman Gushchin0679dee2017-08-02 17:55:29 +01005189
Li Zefana4189482014-09-04 14:43:07 +08005190 /*
5191 * There are two control paths which try to determine
5192 * cgroup from dentry without going through kernfs -
5193 * cgroupstats_build() and css_tryget_online_from_dir().
5194 * Those are supported by RCU protecting clearing of
5195 * cgrp->kn->priv backpointer.
5196 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005197 if (cgrp->kn)
5198 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
5199 NULL);
Tejun Heo9d755d32014-05-14 09:15:02 -04005200 }
Tejun Heo15a4c832014-05-04 15:09:14 -04005201
Tejun Heo1fed1b22014-05-16 13:22:49 -04005202 mutex_unlock(&cgroup_mutex);
5203
Tejun Heo8f36aae2018-03-14 12:45:14 -07005204 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
5205 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
Tejun Heod3daf282013-06-13 19:39:16 -07005206}
5207
Tejun Heo48ddbe12012-04-01 12:09:56 -07005208static void css_release(struct percpu_ref *ref)
5209{
5210 struct cgroup_subsys_state *css =
5211 container_of(ref, struct cgroup_subsys_state, refcnt);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07005212
Tejun Heo25e15d82014-05-14 09:15:02 -04005213 INIT_WORK(&css->destroy_work, css_release_work_fn);
5214 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005215}
5216
Tejun Heoddfcada2014-05-04 15:09:14 -04005217static void init_and_link_css(struct cgroup_subsys_state *css,
5218 struct cgroup_subsys *ss, struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005219{
Tejun Heo0cb51d72014-05-16 13:22:49 -04005220 lockdep_assert_held(&cgroup_mutex);
5221
Tejun Heoa590b902017-04-28 15:14:55 -04005222 cgroup_get_live(cgrp);
Tejun Heoddfcada2014-05-04 15:09:14 -04005223
Tejun Heod5c419b2014-05-16 13:22:48 -04005224 memset(css, 0, sizeof(*css));
Paul Menagebd89aab2007-10-18 23:40:44 -07005225 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04005226 css->ss = ss;
Tejun Heo8fa3b8d2016-05-26 15:42:13 -04005227 css->id = -1;
Tejun Heod5c419b2014-05-16 13:22:48 -04005228 INIT_LIST_HEAD(&css->sibling);
5229 INIT_LIST_HEAD(&css->children);
Tejun Heo8f534702018-04-26 14:29:05 -07005230 INIT_LIST_HEAD(&css->rstat_css_node);
Tejun Heo0cb51d72014-05-16 13:22:49 -04005231 css->serial_nr = css_serial_nr_next++;
Tejun Heoaa226ff2016-01-21 15:31:11 -05005232 atomic_set(&css->online_cnt, 0);
Tejun Heo48ddbe12012-04-01 12:09:56 -07005233
Tejun Heod51f39b2014-05-16 13:22:48 -04005234 if (cgroup_parent(cgrp)) {
5235 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
Tejun Heoddfcada2014-05-04 15:09:14 -04005236 css_get(css->parent);
Tejun Heoddfcada2014-05-04 15:09:14 -04005237 }
Tejun Heo0ae78e02013-08-13 11:01:54 -04005238
Johannes Weinera7df69b2021-04-29 22:56:20 -07005239 if (ss->css_rstat_flush)
Tejun Heo8f534702018-04-26 14:29:05 -07005240 list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list);
5241
Tejun Heoca8bdca2013-08-26 18:40:56 -04005242 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005243}
5244
Li Zefan2a4ac632013-07-31 16:16:40 +08005245/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04005246static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08005247{
Tejun Heo623f9262013-08-13 11:01:55 -04005248 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08005249 int ret = 0;
5250
Tejun Heoa31f2d32012-11-19 08:13:37 -08005251 lockdep_assert_held(&cgroup_mutex);
5252
Tejun Heo92fb9742012-11-19 08:13:38 -08005253 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04005254 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04005255 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04005256 css->flags |= CSS_ONLINE;
Tejun Heoaec25022014-02-08 10:36:58 -05005257 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
Tejun Heoaa226ff2016-01-21 15:31:11 -05005258
5259 atomic_inc(&css->online_cnt);
5260 if (css->parent)
5261 atomic_inc(&css->parent->online_cnt);
Tejun Heoae7f1642013-08-13 20:22:50 -04005262 }
Tejun Heob1929db2012-11-19 08:13:38 -08005263 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08005264}
5265
Li Zefan2a4ac632013-07-31 16:16:40 +08005266/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04005267static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08005268{
Tejun Heo623f9262013-08-13 11:01:55 -04005269 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08005270
5271 lockdep_assert_held(&cgroup_mutex);
5272
5273 if (!(css->flags & CSS_ONLINE))
5274 return;
5275
Li Zefand7eeac12013-03-12 15:35:59 -07005276 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04005277 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08005278
Tejun Heoeb954192013-08-08 20:11:23 -04005279 css->flags &= ~CSS_ONLINE;
Tejun Heoe3297802014-04-23 11:13:15 -04005280 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
Tejun Heof8f22e52014-04-23 11:13:16 -04005281
5282 wake_up_all(&css->cgroup->offline_waitq);
Tejun Heoa31f2d32012-11-19 08:13:37 -08005283}
5284
Tejun Heoc81c925a2013-12-06 15:11:56 -05005285/**
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005286 * css_create - create a cgroup_subsys_state
Tejun Heoc81c925a2013-12-06 15:11:56 -05005287 * @cgrp: the cgroup new css will be associated with
5288 * @ss: the subsys of new css
5289 *
5290 * Create a new css associated with @cgrp - @ss pair. On success, the new
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005291 * css is online and installed in @cgrp. This function doesn't create the
5292 * interface files. Returns 0 on success, -errno on failure.
Tejun Heoc81c925a2013-12-06 15:11:56 -05005293 */
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005294static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
5295 struct cgroup_subsys *ss)
Tejun Heoc81c925a2013-12-06 15:11:56 -05005296{
Tejun Heod51f39b2014-05-16 13:22:48 -04005297 struct cgroup *parent = cgroup_parent(cgrp);
Tejun Heo1fed1b22014-05-16 13:22:49 -04005298 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005299 struct cgroup_subsys_state *css;
5300 int err;
5301
Tejun Heoc81c925a2013-12-06 15:11:56 -05005302 lockdep_assert_held(&cgroup_mutex);
5303
Tejun Heo1fed1b22014-05-16 13:22:49 -04005304 css = ss->css_alloc(parent_css);
Tejun Heoe7e15b82016-06-21 13:06:24 -04005305 if (!css)
5306 css = ERR_PTR(-ENOMEM);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005307 if (IS_ERR(css))
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005308 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05005309
Tejun Heoddfcada2014-05-04 15:09:14 -04005310 init_and_link_css(css, ss, cgrp);
Tejun Heoa2bed822014-05-04 15:09:14 -04005311
Tejun Heo2aad2a82014-09-24 13:31:50 -04005312 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005313 if (err)
Li Zefan3eb59ec2014-03-18 17:02:36 +08005314 goto err_free_css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05005315
Vladimir Davydovcf780b72015-08-03 15:32:26 +03005316 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
Tejun Heo15a4c832014-05-04 15:09:14 -04005317 if (err < 0)
Wenwei Taob00c52d2016-05-13 22:59:20 +08005318 goto err_free_css;
Tejun Heo15a4c832014-05-04 15:09:14 -04005319 css->id = err;
Tejun Heoc81c925a2013-12-06 15:11:56 -05005320
Tejun Heo15a4c832014-05-04 15:09:14 -04005321 /* @css is ready to be brought online now, make it visible */
Tejun Heo1fed1b22014-05-16 13:22:49 -04005322 list_add_tail_rcu(&css->sibling, &parent_css->children);
Tejun Heo15a4c832014-05-04 15:09:14 -04005323 cgroup_idr_replace(&ss->css_idr, css, css->id);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005324
5325 err = online_css(css);
5326 if (err)
Tejun Heo1fed1b22014-05-16 13:22:49 -04005327 goto err_list_del;
Tejun Heo94419622014-03-19 10:23:54 -04005328
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005329 return css;
Tejun Heoc81c925a2013-12-06 15:11:56 -05005330
Tejun Heo1fed1b22014-05-16 13:22:49 -04005331err_list_del:
5332 list_del_rcu(&css->sibling);
Li Zefan3eb59ec2014-03-18 17:02:36 +08005333err_free_css:
Tejun Heo8f534702018-04-26 14:29:05 -07005334 list_del_rcu(&css->rstat_css_node);
Tejun Heo8f36aae2018-03-14 12:45:14 -07005335 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
5336 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
Tejun Heo6cd0f5b2016-03-03 09:57:58 -05005337 return ERR_PTR(err);
Tejun Heoc81c925a2013-12-06 15:11:56 -05005338}
5339
Tejun Heo07cd1292017-01-26 16:47:28 -05005340/*
5341 * The returned cgroup is fully initialized including its control mask, but
5342 * it isn't associated with its kernfs_node and doesn't have the control
5343 * mask applied.
5344 */
Tejun Heo74321032019-11-04 15:54:30 -08005345static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
5346 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005347{
Tejun Heoa5bca212016-03-03 09:57:58 -05005348 struct cgroup_root *root = parent->root;
Tejun Heoa5bca212016-03-03 09:57:58 -05005349 struct cgroup *cgrp, *tcgrp;
Tejun Heo74321032019-11-04 15:54:30 -08005350 struct kernfs_node *kn;
Tejun Heoa5bca212016-03-03 09:57:58 -05005351 int level = parent->level + 1;
Tejun Heo03970d32016-03-03 09:58:00 -05005352 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005353
Tejun Heo0a950f62012-11-19 09:02:12 -08005354 /* allocate the cgroup and its ID, 0 is reserved for the root */
Kees Cookacafe7e2018-05-08 13:45:50 -07005355 cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)),
5356 GFP_KERNEL);
Tejun Heoa5bca212016-03-03 09:57:58 -05005357 if (!cgrp)
5358 return ERR_PTR(-ENOMEM);
Li Zefan0ab02ca2014-02-11 16:05:46 +08005359
Tejun Heo2aad2a82014-09-24 13:31:50 -04005360 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
Tejun Heo9d755d32014-05-14 09:15:02 -04005361 if (ret)
5362 goto out_free_cgrp;
5363
Johannes Weinera7df69b2021-04-29 22:56:20 -07005364 ret = cgroup_rstat_init(cgrp);
5365 if (ret)
5366 goto out_cancel_ref;
Tejun Heo041cd642017-09-25 08:12:05 -07005367
Tejun Heo74321032019-11-04 15:54:30 -08005368 /* create the directory */
5369 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
5370 if (IS_ERR(kn)) {
5371 ret = PTR_ERR(kn);
Tejun Heo041cd642017-09-25 08:12:05 -07005372 goto out_stat_exit;
Tejun Heo976c06b2012-11-05 09:16:59 -08005373 }
Tejun Heo74321032019-11-04 15:54:30 -08005374 cgrp->kn = kn;
Tejun Heo976c06b2012-11-05 09:16:59 -08005375
Paul Menagecc31edc2008-10-18 20:28:04 -07005376 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005377
Tejun Heo9d800df2014-05-14 09:15:00 -04005378 cgrp->self.parent = &parent->self;
Tejun Heoba0f4d72014-05-13 12:19:22 -04005379 cgrp->root = root;
Tejun Heob11cfb52015-11-20 15:55:52 -05005380 cgrp->level = level;
Johannes Weiner2ce71352018-10-26 15:06:31 -07005381
5382 ret = psi_cgroup_alloc(cgrp);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07005383 if (ret)
Tejun Heo74321032019-11-04 15:54:30 -08005384 goto out_kernfs_remove;
Tejun Heob11cfb52015-11-20 15:55:52 -05005385
Johannes Weiner2ce71352018-10-26 15:06:31 -07005386 ret = cgroup_bpf_inherit(cgrp);
5387 if (ret)
5388 goto out_psi_free;
5389
Roman Gushchin76f969e2019-04-19 10:03:04 -07005390 /*
5391 * New cgroup inherits effective freeze counter, and
5392 * if the parent has to be frozen, the child has too.
5393 */
5394 cgrp->freezer.e_freeze = parent->freezer.e_freeze;
Roman Gushchin97a61362019-09-12 10:56:45 -07005395 if (cgrp->freezer.e_freeze) {
5396 /*
5397 * Set the CGRP_FREEZE flag, so when a process will be
5398 * attached to the child cgroup, it will become frozen.
5399 * At this point the new cgroup is unpopulated, so we can
5400 * consider it frozen immediately.
5401 */
5402 set_bit(CGRP_FREEZE, &cgrp->flags);
Roman Gushchin76f969e2019-04-19 10:03:04 -07005403 set_bit(CGRP_FROZEN, &cgrp->flags);
Roman Gushchin97a61362019-09-12 10:56:45 -07005404 }
Roman Gushchin76f969e2019-04-19 10:03:04 -07005405
Roman Gushchin4dcabec2019-04-19 10:03:03 -07005406 spin_lock_irq(&css_set_lock);
Roman Gushchin0679dee2017-08-02 17:55:29 +01005407 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
Tejun Heo74321032019-11-04 15:54:30 -08005408 cgrp->ancestor_ids[tcgrp->level] = cgroup_id(tcgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005409
Roman Gushchin76f969e2019-04-19 10:03:04 -07005410 if (tcgrp != cgrp) {
Roman Gushchin0679dee2017-08-02 17:55:29 +01005411 tcgrp->nr_descendants++;
Roman Gushchin76f969e2019-04-19 10:03:04 -07005412
5413 /*
5414 * If the new cgroup is frozen, all ancestor cgroups
5415 * get a new frozen descendant, but their state can't
5416 * change because of this.
5417 */
5418 if (cgrp->freezer.e_freeze)
5419 tcgrp->freezer.nr_frozen_descendants++;
5420 }
Roman Gushchin0679dee2017-08-02 17:55:29 +01005421 }
Roman Gushchin4dcabec2019-04-19 10:03:03 -07005422 spin_unlock_irq(&css_set_lock);
Roman Gushchin0679dee2017-08-02 17:55:29 +01005423
Li Zefanb6abdb02008-03-04 14:28:19 -08005424 if (notify_on_release(parent))
5425 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
5426
Tejun Heo2260e7f2012-11-19 08:13:38 -08005427 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
5428 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07005429
Tejun Heo0cb51d72014-05-16 13:22:49 -04005430 cgrp->self.serial_nr = css_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09005431
Tejun Heo4e139af2012-11-19 08:13:36 -08005432 /* allocation complete, commit to creation */
Tejun Heod5c419b2014-05-16 13:22:48 -04005433 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
Tejun Heo3c9c8252014-02-12 09:29:50 -05005434 atomic_inc(&root->nr_cgrps);
Tejun Heoa590b902017-04-28 15:14:55 -04005435 cgroup_get_live(parent);
Li Zefan415cf072013-04-08 14:35:02 +08005436
Tejun Heo0d802552013-12-06 15:11:56 -05005437 /*
Tejun Heobd53d612014-04-23 11:13:16 -04005438 * On the default hierarchy, a child doesn't automatically inherit
Tejun Heo667c2492014-07-08 18:02:56 -04005439 * subtree_control from the parent. Each is configured manually.
Tejun Heobd53d612014-04-23 11:13:16 -04005440 */
Tejun Heo03970d32016-03-03 09:58:00 -05005441 if (!cgroup_on_dfl(cgrp))
Tejun Heo5531dc92016-03-03 09:57:58 -05005442 cgrp->subtree_control = cgroup_control(cgrp);
Tejun Heo03970d32016-03-03 09:58:00 -05005443
5444 cgroup_propagate_control(cgrp);
5445
Tejun Heoa5bca212016-03-03 09:57:58 -05005446 return cgrp;
5447
Johannes Weiner2ce71352018-10-26 15:06:31 -07005448out_psi_free:
5449 psi_cgroup_free(cgrp);
Tejun Heo74321032019-11-04 15:54:30 -08005450out_kernfs_remove:
5451 kernfs_remove(cgrp->kn);
Tejun Heo041cd642017-09-25 08:12:05 -07005452out_stat_exit:
Johannes Weinera7df69b2021-04-29 22:56:20 -07005453 cgroup_rstat_exit(cgrp);
Tejun Heoa5bca212016-03-03 09:57:58 -05005454out_cancel_ref:
5455 percpu_ref_exit(&cgrp->self.refcnt);
5456out_free_cgrp:
5457 kfree(cgrp);
5458 return ERR_PTR(ret);
Tejun Heoa5bca212016-03-03 09:57:58 -05005459}
5460
Roman Gushchin1a926e02017-07-28 18:28:44 +01005461static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
5462{
5463 struct cgroup *cgroup;
5464 int ret = false;
5465 int level = 1;
5466
5467 lockdep_assert_held(&cgroup_mutex);
5468
5469 for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) {
5470 if (cgroup->nr_descendants >= cgroup->max_descendants)
5471 goto fail;
5472
5473 if (level > cgroup->max_depth)
5474 goto fail;
5475
5476 level++;
5477 }
5478
5479 ret = true;
5480fail:
5481 return ret;
5482}
5483
Tejun Heo1592c9b2016-12-27 14:49:08 -05005484int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
Tejun Heoa5bca212016-03-03 09:57:58 -05005485{
5486 struct cgroup *parent, *cgrp;
Tejun Heo03970d32016-03-03 09:58:00 -05005487 int ret;
Tejun Heoa5bca212016-03-03 09:57:58 -05005488
5489 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
5490 if (strchr(name, '\n'))
5491 return -EINVAL;
5492
Tejun Heo945ba192016-03-03 09:58:00 -05005493 parent = cgroup_kn_lock_live(parent_kn, false);
Tejun Heoa5bca212016-03-03 09:57:58 -05005494 if (!parent)
5495 return -ENODEV;
5496
Roman Gushchin1a926e02017-07-28 18:28:44 +01005497 if (!cgroup_check_hierarchy_limits(parent)) {
5498 ret = -EAGAIN;
5499 goto out_unlock;
5500 }
5501
Tejun Heo74321032019-11-04 15:54:30 -08005502 cgrp = cgroup_create(parent, name, mode);
Tejun Heoa5bca212016-03-03 09:57:58 -05005503 if (IS_ERR(cgrp)) {
5504 ret = PTR_ERR(cgrp);
5505 goto out_unlock;
5506 }
5507
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005508 /*
5509 * This extra ref will be put in cgroup_free_fn() and guarantees
5510 * that @cgrp->kn is always accessible.
5511 */
Tejun Heo74321032019-11-04 15:54:30 -08005512 kernfs_get(cgrp->kn);
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005513
Tejun Heo74321032019-11-04 15:54:30 -08005514 ret = cgroup_kn_set_ugid(cgrp->kn);
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005515 if (ret)
5516 goto out_destroy;
5517
Tejun Heo334c3672016-03-03 09:58:01 -05005518 ret = css_populate_dir(&cgrp->self);
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005519 if (ret)
5520 goto out_destroy;
5521
Tejun Heo03970d32016-03-03 09:58:00 -05005522 ret = cgroup_apply_control_enable(cgrp);
5523 if (ret)
5524 goto out_destroy;
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005525
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04005526 TRACE_CGROUP_PATH(mkdir, cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04005527
Tejun Heo195e9b6c2016-03-03 09:57:58 -05005528 /* let's create and online css's */
Tejun Heo74321032019-11-04 15:54:30 -08005529 kernfs_activate(cgrp->kn);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005530
Tejun Heoba0f4d72014-05-13 12:19:22 -04005531 ret = 0;
5532 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005533
Tejun Heoa5bca212016-03-03 09:57:58 -05005534out_destroy:
5535 cgroup_destroy_locked(cgrp);
Tejun Heoba0f4d72014-05-13 12:19:22 -04005536out_unlock:
Tejun Heoa9746d82014-05-13 12:19:22 -04005537 cgroup_kn_unlock(parent_kn);
Tejun Heoe1b2dc12014-03-20 11:10:15 -04005538 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005539}
5540
Tejun Heo223dbc32013-08-13 20:22:50 -04005541/*
5542 * This is called when the refcnt of a css is confirmed to be killed.
Tejun Heo249f3462014-05-14 09:15:01 -04005543 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
Zhen Lei08b2b6f2021-05-24 16:29:43 +08005544 * initiate destruction and put the css ref from kill_css().
Tejun Heo223dbc32013-08-13 20:22:50 -04005545 */
5546static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07005547{
Tejun Heo223dbc32013-08-13 20:22:50 -04005548 struct cgroup_subsys_state *css =
5549 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07005550
Tejun Heof20104d2013-08-13 20:22:50 -04005551 mutex_lock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04005552
Tejun Heoaa226ff2016-01-21 15:31:11 -05005553 do {
5554 offline_css(css);
5555 css_put(css);
5556 /* @css can't go away while we're holding cgroup_mutex */
5557 css = css->parent;
5558 } while (css && atomic_dec_and_test(&css->online_cnt));
5559
5560 mutex_unlock(&cgroup_mutex);
Tejun Heod3daf282013-06-13 19:39:16 -07005561}
5562
Tejun Heo223dbc32013-08-13 20:22:50 -04005563/* css kill confirmation processing requires process context, bounce */
5564static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07005565{
5566 struct cgroup_subsys_state *css =
5567 container_of(ref, struct cgroup_subsys_state, refcnt);
5568
Tejun Heoaa226ff2016-01-21 15:31:11 -05005569 if (atomic_dec_and_test(&css->online_cnt)) {
5570 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5571 queue_work(cgroup_destroy_wq, &css->destroy_work);
5572 }
Tejun Heod3daf282013-06-13 19:39:16 -07005573}
5574
Tejun Heof392e512014-04-23 11:13:14 -04005575/**
5576 * kill_css - destroy a css
5577 * @css: css to destroy
5578 *
5579 * This function initiates destruction of @css by removing cgroup interface
5580 * files and putting its base reference. ->css_offline() will be invoked
Tejun Heoec903c02014-05-13 12:11:01 -04005581 * asynchronously once css_tryget_online() is guaranteed to fail and when
5582 * the reference count reaches zero, @css will be released.
Tejun Heof392e512014-04-23 11:13:14 -04005583 */
5584static void kill_css(struct cgroup_subsys_state *css)
Tejun Heoedae0c32013-08-13 20:22:51 -04005585{
Tejun Heo01f64742014-05-13 12:19:23 -04005586 lockdep_assert_held(&cgroup_mutex);
Tejun Heo94419622014-03-19 10:23:54 -04005587
Waiman Long33c35aa2017-05-15 09:34:06 -04005588 if (css->flags & CSS_DYING)
5589 return;
5590
5591 css->flags |= CSS_DYING;
5592
Tejun Heo2bd59d42014-02-11 11:52:49 -05005593 /*
5594 * This must happen before css is disassociated with its cgroup.
5595 * See seq_css() for details.
5596 */
Tejun Heo334c3672016-03-03 09:58:01 -05005597 css_clear_dir(css);
Tejun Heo3c14f8b2013-08-13 20:22:51 -04005598
Tejun Heoedae0c32013-08-13 20:22:51 -04005599 /*
5600 * Killing would put the base ref, but we need to keep it alive
5601 * until after ->css_offline().
5602 */
5603 css_get(css);
5604
5605 /*
5606 * cgroup core guarantees that, by the time ->css_offline() is
5607 * invoked, no new css reference will be given out via
Tejun Heoec903c02014-05-13 12:11:01 -04005608 * css_tryget_online(). We can't simply call percpu_ref_kill() and
Tejun Heoedae0c32013-08-13 20:22:51 -04005609 * proceed to offlining css's because percpu_ref_kill() doesn't
5610 * guarantee that the ref is seen as killed on all CPUs on return.
5611 *
5612 * Use percpu_ref_kill_and_confirm() to get notifications as each
5613 * css is confirmed to be seen as killed on all CPUs.
5614 */
5615 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07005616}
5617
5618/**
5619 * cgroup_destroy_locked - the first stage of cgroup destruction
5620 * @cgrp: cgroup to be destroyed
5621 *
5622 * css's make use of percpu refcnts whose killing latency shouldn't be
5623 * exposed to userland and are RCU protected. Also, cgroup core needs to
Tejun Heoec903c02014-05-13 12:11:01 -04005624 * guarantee that css_tryget_online() won't succeed by the time
5625 * ->css_offline() is invoked. To satisfy all the requirements,
5626 * destruction is implemented in the following two steps.
Tejun Heod3daf282013-06-13 19:39:16 -07005627 *
5628 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5629 * userland visible parts and start killing the percpu refcnts of
5630 * css's. Set up so that the next stage will be kicked off once all
5631 * the percpu refcnts are confirmed to be killed.
5632 *
5633 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5634 * rest of destruction. Once all cgroup references are gone, the
5635 * cgroup is RCU-freed.
5636 *
5637 * This function implements s1. After this step, @cgrp is gone as far as
5638 * the userland is concerned and a new cgroup with the same name may be
5639 * created. As cgroup doesn't care about the names internally, this
5640 * doesn't cause any problem.
5641 */
Tejun Heo42809dd2012-11-19 08:13:37 -08005642static int cgroup_destroy_locked(struct cgroup *cgrp)
5643 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005644{
Roman Gushchin0679dee2017-08-02 17:55:29 +01005645 struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005646 struct cgroup_subsys_state *css;
Tejun Heo2b021cb2016-03-15 20:43:04 -04005647 struct cgrp_cset_link *link;
Tejun Heo1c6727a2013-12-06 15:11:56 -05005648 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005649
Tejun Heo42809dd2012-11-19 08:13:37 -08005650 lockdep_assert_held(&cgroup_mutex);
5651
Tejun Heo91486f62015-10-15 16:41:51 -04005652 /*
5653 * Only migration can raise populated from zero and we're already
5654 * holding cgroup_mutex.
5655 */
5656 if (cgroup_is_populated(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07005657 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08005658
Tejun Heo1a90dd52012-11-05 09:16:59 -08005659 /*
Tejun Heod5c419b2014-05-16 13:22:48 -04005660 * Make sure there's no live children. We can't test emptiness of
5661 * ->self.children as dead children linger on it while being
5662 * drained; otherwise, "rmdir parent/child parent" may fail.
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005663 */
Tejun Heof3d46502014-05-16 13:22:52 -04005664 if (css_has_online_children(&cgrp->self))
Hugh Dickinsbb78a922013-08-28 16:31:23 -07005665 return -EBUSY;
5666
5667 /*
Tejun Heo2b021cb2016-03-15 20:43:04 -04005668 * Mark @cgrp and the associated csets dead. The former prevents
5669 * further task migration and child creation by disabling
5670 * cgroup_lock_live_group(). The latter makes the csets ignored by
5671 * the migration path.
Tejun Heo455050d2013-06-13 19:27:41 -07005672 */
Tejun Heo184faf32014-05-16 13:22:51 -04005673 cgrp->self.flags &= ~CSS_ONLINE;
Tejun Heo1a90dd52012-11-05 09:16:59 -08005674
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005675 spin_lock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005676 list_for_each_entry(link, &cgrp->cset_links, cset_link)
5677 link->cset->dead = true;
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03005678 spin_unlock_irq(&css_set_lock);
Tejun Heo2b021cb2016-03-15 20:43:04 -04005679
Tejun Heo249f3462014-05-14 09:15:01 -04005680 /* initiate massacre of all css's */
Tejun Heo1a90dd52012-11-05 09:16:59 -08005681 for_each_css(css, ssid, cgrp)
Tejun Heo455050d2013-06-13 19:27:41 -07005682 kill_css(css);
5683
Tejun Heo5faaf052018-04-26 14:29:04 -07005684 /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
5685 css_clear_dir(&cgrp->self);
Tejun Heo01f64742014-05-13 12:19:23 -04005686 kernfs_remove(cgrp->kn);
Tejun Heof20104d2013-08-13 20:22:50 -04005687
Tejun Heo454000a2017-05-15 09:34:02 -04005688 if (parent && cgroup_is_threaded(cgrp))
5689 parent->nr_threaded_children--;
5690
Roman Gushchin4dcabec2019-04-19 10:03:03 -07005691 spin_lock_irq(&css_set_lock);
Roman Gushchin0679dee2017-08-02 17:55:29 +01005692 for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) {
5693 tcgrp->nr_descendants--;
5694 tcgrp->nr_dying_descendants++;
Roman Gushchin76f969e2019-04-19 10:03:04 -07005695 /*
5696 * If the dying cgroup is frozen, decrease frozen descendants
5697 * counters of ancestor cgroups.
5698 */
5699 if (test_bit(CGRP_FROZEN, &cgrp->flags))
5700 tcgrp->freezer.nr_frozen_descendants--;
Roman Gushchin0679dee2017-08-02 17:55:29 +01005701 }
Roman Gushchin4dcabec2019-04-19 10:03:03 -07005702 spin_unlock_irq(&css_set_lock);
Roman Gushchin0679dee2017-08-02 17:55:29 +01005703
Roman Gushchin5a621e62017-08-02 17:55:32 +01005704 cgroup1_check_for_release(parent);
Tejun Heo2bd59d42014-02-11 11:52:49 -05005705
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -07005706 cgroup_bpf_offline(cgrp);
5707
Tejun Heo249f3462014-05-14 09:15:01 -04005708 /* put the base reference */
Tejun Heo9d755d32014-05-14 09:15:02 -04005709 percpu_ref_kill(&cgrp->self.refcnt);
Tejun Heo455050d2013-06-13 19:27:41 -07005710
Tejun Heoea15f8c2013-06-13 19:27:42 -07005711 return 0;
5712};
5713
Tejun Heo1592c9b2016-12-27 14:49:08 -05005714int cgroup_rmdir(struct kernfs_node *kn)
Tejun Heo42809dd2012-11-19 08:13:37 -08005715{
Tejun Heoa9746d82014-05-13 12:19:22 -04005716 struct cgroup *cgrp;
Tejun Heo2bd59d42014-02-11 11:52:49 -05005717 int ret = 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005718
Tejun Heo945ba192016-03-03 09:58:00 -05005719 cgrp = cgroup_kn_lock_live(kn, false);
Tejun Heoa9746d82014-05-13 12:19:22 -04005720 if (!cgrp)
5721 return 0;
Tejun Heo42809dd2012-11-19 08:13:37 -08005722
Tejun Heoa9746d82014-05-13 12:19:22 -04005723 ret = cgroup_destroy_locked(cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04005724 if (!ret)
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -04005725 TRACE_CGROUP_PATH(rmdir, cgrp);
Tejun Heoed1777d2016-08-10 11:23:44 -04005726
Tejun Heoa9746d82014-05-13 12:19:22 -04005727 cgroup_kn_unlock(kn);
Tejun Heo42809dd2012-11-19 08:13:37 -08005728 return ret;
5729}
5730
Tejun Heo2bd59d42014-02-11 11:52:49 -05005731static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
Tejun Heo5136f632017-06-27 14:30:28 -04005732 .show_options = cgroup_show_options,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005733 .mkdir = cgroup_mkdir,
5734 .rmdir = cgroup_rmdir,
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -05005735 .show_path = cgroup_show_path,
Tejun Heo2bd59d42014-02-11 11:52:49 -05005736};
Tejun Heo8e3f6542012-04-01 12:09:55 -07005737
Tejun Heo15a4c832014-05-04 15:09:14 -04005738static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
Paul Menageddbcc7e2007-10-18 23:39:30 -07005739{
Paul Menageddbcc7e2007-10-18 23:39:30 -07005740 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08005741
Tejun Heoa5ae9892015-12-29 14:53:56 -05005742 pr_debug("Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005743
Tejun Heo648bb562012-11-19 08:13:36 -08005744 mutex_lock(&cgroup_mutex);
5745
Tejun Heo15a4c832014-05-04 15:09:14 -04005746 idr_init(&ss->css_idr);
Tejun Heo0adb0702014-02-12 09:29:48 -05005747 INIT_LIST_HEAD(&ss->cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -07005748
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005749 /* Create the root cgroup state for this subsystem */
5750 ss->root = &cgrp_dfl_root;
Wei Yang82914712021-11-27 14:59:19 +00005751 css = ss->css_alloc(NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005752 /* We don't handle early failures gracefully */
5753 BUG_ON(IS_ERR(css));
Tejun Heoddfcada2014-05-04 15:09:14 -04005754 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
Tejun Heo3b514d22014-05-16 13:22:47 -04005755
5756 /*
5757 * Root csses are never destroyed and we can't initialize
5758 * percpu_ref during early init. Disable refcnting.
5759 */
5760 css->flags |= CSS_NO_REF;
5761
Tejun Heo15a4c832014-05-04 15:09:14 -04005762 if (early) {
Tejun Heo9395a452014-05-14 09:15:02 -04005763 /* allocation can't be done safely during early init */
Tejun Heo15a4c832014-05-04 15:09:14 -04005764 css->id = 1;
5765 } else {
5766 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5767 BUG_ON(css->id < 0);
5768 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005769
Li Zefane8d55fd2008-04-29 01:00:13 -07005770 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07005771 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07005772 * newly registered, all tasks and hence the
Tejun Heo3dd06ff2014-03-19 10:23:54 -04005773 * init_css_set is in the subsystem's root cgroup. */
Tejun Heoaec25022014-02-08 10:36:58 -05005774 init_css_set.subsys[ss->id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005775
Aleksa Saraicb4a3162015-06-06 10:02:14 +10005776 have_fork_callback |= (bool)ss->fork << ss->id;
5777 have_exit_callback |= (bool)ss->exit << ss->id;
Oleg Nesterov51bee5a2019-01-28 17:00:13 +01005778 have_release_callback |= (bool)ss->release << ss->id;
Aleksa Sarai7e476822015-06-09 21:32:09 +10005779 have_canfork_callback |= (bool)ss->can_fork << ss->id;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005780
Li Zefane8d55fd2008-04-29 01:00:13 -07005781 /* At system boot, before all subsystems have been
5782 * registered, no tasks have been forked, so we don't
5783 * need to invoke fork callbacks here. */
5784 BUG_ON(!list_empty(&init_task.tasks));
5785
Tejun Heoae7f1642013-08-13 20:22:50 -04005786 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08005787
Tejun Heo648bb562012-11-19 08:13:36 -08005788 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005789}
5790
5791/**
Li Zefana043e3b2008-02-23 15:24:09 -08005792 * cgroup_init_early - cgroup initialization at system boot
5793 *
5794 * Initialize cgroups at system boot, and initialize any
5795 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005796 */
5797int __init cgroup_init_early(void)
5798{
Al Virof5dfb532019-01-16 23:42:38 -05005799 static struct cgroup_fs_context __initdata ctx;
Tejun Heo30159ec2013-06-25 11:53:37 -07005800 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005801 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005802
Al Virocf6299b12019-01-17 02:25:51 -05005803 ctx.root = &cgrp_dfl_root;
5804 init_cgroup_root(&ctx);
Tejun Heo3b514d22014-05-16 13:22:47 -04005805 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5806
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005807 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005808
Tejun Heo3ed80a62014-02-08 10:36:58 -05005809 for_each_subsys(ss, i) {
Tejun Heoaec25022014-02-08 10:36:58 -05005810 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
Xiubo Li63253ad2016-02-26 13:07:38 +08005811 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
Tejun Heo073219e2014-02-08 10:36:58 -05005812 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
Tejun Heoaec25022014-02-08 10:36:58 -05005813 ss->id, ss->name);
Tejun Heo073219e2014-02-08 10:36:58 -05005814 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5815 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005816
Tejun Heoaec25022014-02-08 10:36:58 -05005817 ss->id = i;
Tejun Heo073219e2014-02-08 10:36:58 -05005818 ss->name = cgroup_subsys_name[i];
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07005819 if (!ss->legacy_name)
5820 ss->legacy_name = cgroup_subsys_name[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07005821
5822 if (ss->early_init)
Tejun Heo15a4c832014-05-04 15:09:14 -04005823 cgroup_init_subsys(ss, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005824 }
5825 return 0;
5826}
5827
5828/**
Li Zefana043e3b2008-02-23 15:24:09 -08005829 * cgroup_init - cgroup initialization
5830 *
5831 * Register cgroup filesystem and /proc file, and initialize
5832 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005833 */
5834int __init cgroup_init(void)
5835{
Tejun Heo30159ec2013-06-25 11:53:37 -07005836 struct cgroup_subsys *ss;
Tejun Heo035f4f52015-10-15 17:00:43 -04005837 int ssid;
Paul Menagea4243162007-10-18 23:39:35 -07005838
Tejun Heo6e5c8302016-02-22 22:25:47 -05005839 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
Tejun Heod62beb72016-12-27 14:49:08 -05005840 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
5841 BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
Paul Menageddbcc7e2007-10-18 23:39:30 -07005842
Tejun Heoc58632b2018-04-26 14:29:04 -07005843 cgroup_rstat_boot();
Tejun Heo041cd642017-09-25 08:12:05 -07005844
Peter Zijlstra3942a9b2016-08-11 18:54:13 +02005845 /*
Paul E. McKenney2af30242018-11-07 14:11:40 -08005846 * The latency of the synchronize_rcu() is too high for cgroups,
Peter Zijlstra3942a9b2016-08-11 18:54:13 +02005847 * avoid it at the cost of forcing all readers into the slow path.
5848 */
5849 rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss);
5850
Aditya Kalia79a9082016-01-29 02:54:06 -06005851 get_user_ns(init_cgroup_ns.user_ns);
5852
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005853 mutex_lock(&cgroup_mutex);
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005854
Tejun Heo2378d8b2016-03-03 09:57:57 -05005855 /*
5856 * Add init_css_set to the hash table so that dfl_root can link to
5857 * it during init.
5858 */
5859 hash_add(css_set_table, &init_css_set.hlist,
5860 css_set_hash(init_css_set.subsys));
Tejun Heo82fe9b02013-06-25 11:53:37 -07005861
Al Viro35ac1182019-01-12 00:20:54 -05005862 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
Greg KH676db4a2010-08-05 13:53:35 -07005863
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005864 mutex_unlock(&cgroup_mutex);
5865
Tejun Heo172a2c062014-03-19 10:23:53 -04005866 for_each_subsys(ss, ssid) {
Tejun Heo15a4c832014-05-04 15:09:14 -04005867 if (ss->early_init) {
5868 struct cgroup_subsys_state *css =
5869 init_css_set.subsys[ss->id];
5870
5871 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5872 GFP_KERNEL);
5873 BUG_ON(css->id < 0);
5874 } else {
5875 cgroup_init_subsys(ss, false);
5876 }
Tejun Heo172a2c062014-03-19 10:23:53 -04005877
Tejun Heo2d8f2432014-04-23 11:13:15 -04005878 list_add_tail(&init_css_set.e_cset_node[ssid],
5879 &cgrp_dfl_root.cgrp.e_csets[ssid]);
Tejun Heo172a2c062014-03-19 10:23:53 -04005880
5881 /*
Li Zefanc731ae12014-06-05 17:16:30 +08005882 * Setting dfl_root subsys_mask needs to consider the
5883 * disabled flag and cftype registration needs kmalloc,
5884 * both of which aren't available during early_init.
Tejun Heo172a2c062014-03-19 10:23:53 -04005885 */
Shakeel Butt45e1ba42021-05-12 13:19:46 -07005886 if (!cgroup_ssid_enabled(ssid))
Tejun Heoa8ddc822014-07-15 11:05:10 -04005887 continue;
5888
Tejun Heod62beb72016-12-27 14:49:08 -05005889 if (cgroup1_ssid_disabled(ssid))
Johannes Weiner223ffb22016-02-11 13:34:49 -05005890 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5891 ss->name);
5892
Tejun Heoa8ddc822014-07-15 11:05:10 -04005893 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5894
Tejun Heo8cfd8142017-07-21 11:14:51 -04005895 /* implicit controllers must be threaded too */
5896 WARN_ON(ss->implicit_on_dfl && !ss->threaded);
5897
Tejun Heof6d635ad2016-03-08 11:51:26 -05005898 if (ss->implicit_on_dfl)
5899 cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
5900 else if (!ss->dfl_cftypes)
Tejun Heoa7165262016-02-23 10:00:50 -05005901 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
Tejun Heo5de4fa12014-07-15 11:05:10 -04005902
Tejun Heo8cfd8142017-07-21 11:14:51 -04005903 if (ss->threaded)
5904 cgrp_dfl_threaded_ss_mask |= 1 << ss->id;
5905
Tejun Heoa8ddc822014-07-15 11:05:10 -04005906 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5907 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5908 } else {
5909 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5910 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
Li Zefanc731ae12014-06-05 17:16:30 +08005911 }
Vladimir Davydov295458e2015-02-19 17:34:46 +03005912
5913 if (ss->bind)
5914 ss->bind(init_css_set.subsys[ssid]);
Tejun Heo7af608e2017-07-18 17:57:46 -04005915
5916 mutex_lock(&cgroup_mutex);
5917 css_populate_dir(init_css_set.subsys[ssid]);
5918 mutex_unlock(&cgroup_mutex);
Tejun Heo172a2c062014-03-19 10:23:53 -04005919 }
Greg KH676db4a2010-08-05 13:53:35 -07005920
Tejun Heo2378d8b2016-03-03 09:57:57 -05005921 /* init_css_set.subsys[] has been updated, re-hash */
5922 hash_del(&init_css_set.hlist);
5923 hash_add(css_set_table, &init_css_set.hlist,
5924 css_set_hash(init_css_set.subsys));
5925
Tejun Heo035f4f52015-10-15 17:00:43 -04005926 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5927 WARN_ON(register_filesystem(&cgroup_fs_type));
Tejun Heo67e9c742015-11-16 11:13:34 -05005928 WARN_ON(register_filesystem(&cgroup2_fs_type));
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02005929 WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show));
Al Virod5f68d32019-05-13 12:33:22 -04005930#ifdef CONFIG_CPUSETS
5931 WARN_ON(register_filesystem(&cpuset_fs_type));
5932#endif
Paul Menagea4243162007-10-18 23:39:35 -07005933
Tejun Heo2bd59d42014-02-11 11:52:49 -05005934 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005935}
Paul Menageb4f48b62007-10-18 23:39:33 -07005936
Tejun Heoe5fca242013-11-22 17:14:39 -05005937static int __init cgroup_wq_init(void)
5938{
5939 /*
5940 * There isn't much point in executing destruction path in
5941 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05005942 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05005943 *
5944 * We would prefer to do this in cgroup_init() above, but that
5945 * is called before init_workqueues(): so leave this until after.
5946 */
Tejun Heo1a115332014-02-12 19:06:19 -05005947 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05005948 BUG_ON(!cgroup_destroy_wq);
5949 return 0;
5950}
5951core_initcall(cgroup_wq_init);
5952
Tejun Heo67c04962019-11-04 15:54:30 -08005953void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
Shaohua Li69fd5c32017-07-12 11:49:55 -07005954{
5955 struct kernfs_node *kn;
5956
Tejun Heofe0f7262019-11-04 15:54:30 -08005957 kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
Shaohua Li69fd5c32017-07-12 11:49:55 -07005958 if (!kn)
5959 return;
5960 kernfs_path(kn, buf, buflen);
5961 kernfs_put(kn);
5962}
5963
Paul Menagea4243162007-10-18 23:39:35 -07005964/*
Muneendra Kumar6b658c42021-06-08 10:05:44 +05305965 * cgroup_get_from_id : get the cgroup associated with cgroup id
5966 * @id: cgroup id
5967 * On success return the cgrp, on failure return NULL
5968 */
5969struct cgroup *cgroup_get_from_id(u64 id)
5970{
5971 struct kernfs_node *kn;
5972 struct cgroup *cgrp = NULL;
5973
Muneendra Kumar6b658c42021-06-08 10:05:44 +05305974 kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
5975 if (!kn)
Shakeel Buttbe288162021-10-24 23:19:14 -07005976 goto out;
Muneendra Kumar6b658c42021-06-08 10:05:44 +05305977
Shakeel Buttbe288162021-10-24 23:19:14 -07005978 rcu_read_lock();
5979
5980 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
5981 if (cgrp && !cgroup_tryget(cgrp))
Muneendra Kumar6b658c42021-06-08 10:05:44 +05305982 cgrp = NULL;
Shakeel Buttbe288162021-10-24 23:19:14 -07005983
5984 rcu_read_unlock();
5985
Muneendra Kumar6b658c42021-06-08 10:05:44 +05305986 kernfs_put(kn);
Shakeel Buttbe288162021-10-24 23:19:14 -07005987out:
Muneendra Kumar6b658c42021-06-08 10:05:44 +05305988 return cgrp;
5989}
5990EXPORT_SYMBOL_GPL(cgroup_get_from_id);
5991
5992/*
Paul Menagea4243162007-10-18 23:39:35 -07005993 * proc_cgroup_show()
5994 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5995 * - Used for /proc/<pid>/cgroup.
Paul Menagea4243162007-10-18 23:39:35 -07005996 */
Zefan Li006f4ac2014-09-18 16:03:15 +08005997int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5998 struct pid *pid, struct task_struct *tsk)
Paul Menagea4243162007-10-18 23:39:35 -07005999{
Tejun Heo4c737b42016-08-10 11:23:44 -04006000 char *buf;
Paul Menagea4243162007-10-18 23:39:35 -07006001 int retval;
Tejun Heo3dd06ff2014-03-19 10:23:54 -04006002 struct cgroup_root *root;
Paul Menagea4243162007-10-18 23:39:35 -07006003
6004 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05006005 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Paul Menagea4243162007-10-18 23:39:35 -07006006 if (!buf)
6007 goto out;
6008
Paul Menagea4243162007-10-18 23:39:35 -07006009 mutex_lock(&cgroup_mutex);
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006010 spin_lock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07006011
Tejun Heo985ed672014-03-19 10:23:53 -04006012 for_each_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07006013 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07006014 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05006015 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07006016
Tejun Heoa7165262016-02-23 10:00:50 -05006017 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
Tejun Heo985ed672014-03-19 10:23:53 -04006018 continue;
6019
Paul Menage2c6ab6d2009-09-23 15:56:23 -07006020 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heod98817d2015-08-18 13:58:16 -07006021 if (root != &cgrp_dfl_root)
6022 for_each_subsys(ss, ssid)
6023 if (root->subsys_mask & (1 << ssid))
6024 seq_printf(m, "%s%s", count++ ? "," : "",
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07006025 ss->legacy_name);
Paul Menagec6d57f32009-09-23 15:56:19 -07006026 if (strlen(root->name))
6027 seq_printf(m, "%sname=%s", count ? "," : "",
6028 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07006029 seq_putc(m, ':');
Tejun Heo2e91fa72015-10-15 16:41:53 -04006030
Paul Menage7717f7b2009-09-23 15:56:22 -07006031 cgrp = task_cgroup_from_root(tsk, root);
Tejun Heo2e91fa72015-10-15 16:41:53 -04006032
6033 /*
6034 * On traditional hierarchies, all zombie tasks show up as
6035 * belonging to the root cgroup. On the default hierarchy,
6036 * while a zombie doesn't show up in "cgroup.procs" and
6037 * thus can't be migrated, its /proc/PID/cgroup keeps
6038 * reporting the cgroup it belonged to before exiting. If
6039 * the cgroup is removed before the zombie is reaped,
6040 * " (deleted)" is appended to the cgroup path.
6041 */
6042 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
Tejun Heo4c737b42016-08-10 11:23:44 -04006043 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
Aditya Kalia79a9082016-01-29 02:54:06 -06006044 current->nsproxy->cgroup_ns);
Tejun Heoe0223002016-09-29 15:49:40 +02006045 if (retval >= PATH_MAX)
Tejun Heo2e91fa72015-10-15 16:41:53 -04006046 retval = -ENAMETOOLONG;
Tejun Heoe0223002016-09-29 15:49:40 +02006047 if (retval < 0)
Tejun Heo2e91fa72015-10-15 16:41:53 -04006048 goto out_unlock;
Tejun Heo2e91fa72015-10-15 16:41:53 -04006049
Tejun Heo4c737b42016-08-10 11:23:44 -04006050 seq_puts(m, buf);
6051 } else {
6052 seq_puts(m, "/");
6053 }
Tejun Heo2e91fa72015-10-15 16:41:53 -04006054
6055 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
6056 seq_puts(m, " (deleted)\n");
6057 else
6058 seq_putc(m, '\n');
Paul Menagea4243162007-10-18 23:39:35 -07006059 }
6060
Zefan Li006f4ac2014-09-18 16:03:15 +08006061 retval = 0;
Paul Menagea4243162007-10-18 23:39:35 -07006062out_unlock:
Daniel Bristot de Oliveira82d64892016-06-22 17:28:41 -03006063 spin_unlock_irq(&css_set_lock);
Paul Menagea4243162007-10-18 23:39:35 -07006064 mutex_unlock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07006065 kfree(buf);
6066out:
6067 return retval;
6068}
6069
Paul Menageb4f48b62007-10-18 23:39:33 -07006070/**
Tejun Heoeaf797a2014-02-25 10:04:03 -05006071 * cgroup_fork - initialize cgroup related fields during copy_process()
Li Zefana043e3b2008-02-23 15:24:09 -08006072 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07006073 *
Tejun Heoeaf797a2014-02-25 10:04:03 -05006074 * A task is associated with the init_css_set until cgroup_post_fork()
Christian Brauneref2c41c2020-02-05 14:26:22 +01006075 * attaches it to the target css_set.
Paul Menageb4f48b62007-10-18 23:39:33 -07006076 */
6077void cgroup_fork(struct task_struct *child)
6078{
Tejun Heoeaf797a2014-02-25 10:04:03 -05006079 RCU_INIT_POINTER(child->cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07006080 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07006081}
6082
Christian Brauner17703092020-02-05 14:26:19 +01006083static struct cgroup *cgroup_get_from_file(struct file *f)
6084{
6085 struct cgroup_subsys_state *css;
6086 struct cgroup *cgrp;
6087
6088 css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
6089 if (IS_ERR(css))
6090 return ERR_CAST(css);
6091
6092 cgrp = css->cgroup;
6093 if (!cgroup_on_dfl(cgrp)) {
6094 cgroup_put(cgrp);
6095 return ERR_PTR(-EBADF);
6096 }
6097
6098 return cgrp;
6099}
6100
Paul Menageb4f48b62007-10-18 23:39:33 -07006101/**
Christian Brauneref2c41c2020-02-05 14:26:22 +01006102 * cgroup_css_set_fork - find or create a css_set for a child process
6103 * @kargs: the arguments passed to create the child process
6104 *
6105 * This functions finds or creates a new css_set which the child
6106 * process will be attached to in cgroup_post_fork(). By default,
6107 * the child process will be given the same css_set as its parent.
6108 *
6109 * If CLONE_INTO_CGROUP is specified this function will try to find an
6110 * existing css_set which includes the requested cgroup and if not create
6111 * a new css_set that the child will be attached to later. If this function
6112 * succeeds it will hold cgroup_threadgroup_rwsem on return. If
6113 * CLONE_INTO_CGROUP is requested this function will grab cgroup mutex
6114 * before grabbing cgroup_threadgroup_rwsem and will hold a reference
6115 * to the target cgroup.
6116 */
6117static int cgroup_css_set_fork(struct kernel_clone_args *kargs)
6118 __acquires(&cgroup_mutex) __acquires(&cgroup_threadgroup_rwsem)
6119{
6120 int ret;
6121 struct cgroup *dst_cgrp = NULL;
6122 struct css_set *cset;
6123 struct super_block *sb;
6124 struct file *f;
6125
6126 if (kargs->flags & CLONE_INTO_CGROUP)
6127 mutex_lock(&cgroup_mutex);
6128
6129 cgroup_threadgroup_change_begin(current);
6130
6131 spin_lock_irq(&css_set_lock);
6132 cset = task_css_set(current);
6133 get_css_set(cset);
6134 spin_unlock_irq(&css_set_lock);
6135
6136 if (!(kargs->flags & CLONE_INTO_CGROUP)) {
6137 kargs->cset = cset;
6138 return 0;
6139 }
6140
6141 f = fget_raw(kargs->cgroup);
6142 if (!f) {
6143 ret = -EBADF;
6144 goto err;
6145 }
6146 sb = f->f_path.dentry->d_sb;
6147
6148 dst_cgrp = cgroup_get_from_file(f);
6149 if (IS_ERR(dst_cgrp)) {
6150 ret = PTR_ERR(dst_cgrp);
6151 dst_cgrp = NULL;
6152 goto err;
6153 }
6154
6155 if (cgroup_is_dead(dst_cgrp)) {
6156 ret = -ENODEV;
6157 goto err;
6158 }
6159
6160 /*
6161 * Verify that we the target cgroup is writable for us. This is
6162 * usually done by the vfs layer but since we're not going through
6163 * the vfs layer here we need to do it "manually".
6164 */
6165 ret = cgroup_may_write(dst_cgrp, sb);
6166 if (ret)
6167 goto err;
6168
6169 ret = cgroup_attach_permissions(cset->dfl_cgrp, dst_cgrp, sb,
Tejun Heoe5745762022-01-06 11:02:29 -10006170 !(kargs->flags & CLONE_THREAD),
6171 current->nsproxy->cgroup_ns);
Christian Brauneref2c41c2020-02-05 14:26:22 +01006172 if (ret)
6173 goto err;
6174
6175 kargs->cset = find_css_set(cset, dst_cgrp);
6176 if (!kargs->cset) {
6177 ret = -ENOMEM;
6178 goto err;
6179 }
6180
6181 put_css_set(cset);
6182 fput(f);
6183 kargs->cgrp = dst_cgrp;
6184 return ret;
6185
6186err:
6187 cgroup_threadgroup_change_end(current);
6188 mutex_unlock(&cgroup_mutex);
6189 if (f)
6190 fput(f);
6191 if (dst_cgrp)
6192 cgroup_put(dst_cgrp);
6193 put_css_set(cset);
6194 if (kargs->cset)
6195 put_css_set(kargs->cset);
6196 return ret;
6197}
6198
6199/**
6200 * cgroup_css_set_put_fork - drop references we took during fork
6201 * @kargs: the arguments passed to create the child process
6202 *
6203 * Drop references to the prepared css_set and target cgroup if
6204 * CLONE_INTO_CGROUP was requested.
6205 */
6206static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs)
6207 __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex)
6208{
6209 cgroup_threadgroup_change_end(current);
6210
6211 if (kargs->flags & CLONE_INTO_CGROUP) {
6212 struct cgroup *cgrp = kargs->cgrp;
6213 struct css_set *cset = kargs->cset;
6214
6215 mutex_unlock(&cgroup_mutex);
6216
6217 if (cset) {
6218 put_css_set(cset);
6219 kargs->cset = NULL;
6220 }
6221
6222 if (cgrp) {
6223 cgroup_put(cgrp);
6224 kargs->cgrp = NULL;
6225 }
6226 }
6227}
6228
Paul Menageb4f48b62007-10-18 23:39:33 -07006229/**
Aleksa Sarai7e476822015-06-09 21:32:09 +10006230 * cgroup_can_fork - called on a new task before the process is exposed
Christian Brauner5a5cf5c2020-02-05 14:26:20 +01006231 * @child: the child process
Aleksa Sarai7e476822015-06-09 21:32:09 +10006232 *
Christian Brauneref2c41c2020-02-05 14:26:22 +01006233 * This prepares a new css_set for the child process which the child will
6234 * be attached to in cgroup_post_fork().
Christian Brauner5a5cf5c2020-02-05 14:26:20 +01006235 * This calls the subsystem can_fork() callbacks. If the cgroup_can_fork()
6236 * callback returns an error, the fork aborts with that error code. This
6237 * allows for a cgroup subsystem to conditionally allow or deny new forks.
Aleksa Sarai7e476822015-06-09 21:32:09 +10006238 */
Christian Brauneref2c41c2020-02-05 14:26:22 +01006239int cgroup_can_fork(struct task_struct *child, struct kernel_clone_args *kargs)
Aleksa Sarai7e476822015-06-09 21:32:09 +10006240{
6241 struct cgroup_subsys *ss;
6242 int i, j, ret;
6243
Christian Brauneref2c41c2020-02-05 14:26:22 +01006244 ret = cgroup_css_set_fork(kargs);
6245 if (ret)
6246 return ret;
Christian Brauner5a5cf5c2020-02-05 14:26:20 +01006247
Tejun Heob4e0eea2016-02-22 22:25:46 -05006248 do_each_subsys_mask(ss, i, have_canfork_callback) {
Christian Brauneref2c41c2020-02-05 14:26:22 +01006249 ret = ss->can_fork(child, kargs->cset);
Aleksa Sarai7e476822015-06-09 21:32:09 +10006250 if (ret)
6251 goto out_revert;
Tejun Heob4e0eea2016-02-22 22:25:46 -05006252 } while_each_subsys_mask();
Aleksa Sarai7e476822015-06-09 21:32:09 +10006253
6254 return 0;
6255
6256out_revert:
6257 for_each_subsys(ss, j) {
6258 if (j >= i)
6259 break;
6260 if (ss->cancel_fork)
Christian Brauneref2c41c2020-02-05 14:26:22 +01006261 ss->cancel_fork(child, kargs->cset);
Aleksa Sarai7e476822015-06-09 21:32:09 +10006262 }
6263
Christian Brauneref2c41c2020-02-05 14:26:22 +01006264 cgroup_css_set_put_fork(kargs);
Christian Brauner5a5cf5c2020-02-05 14:26:20 +01006265
Aleksa Sarai7e476822015-06-09 21:32:09 +10006266 return ret;
6267}
6268
6269/**
6270 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
Christian Brauneref2c41c2020-02-05 14:26:22 +01006271 * @child: the child process
6272 * @kargs: the arguments passed to create the child process
Aleksa Sarai7e476822015-06-09 21:32:09 +10006273 *
6274 * This calls the cancel_fork() callbacks if a fork failed *after*
Zhen Lei08b2b6f2021-05-24 16:29:43 +08006275 * cgroup_can_fork() succeeded and cleans up references we took to
Christian Brauneref2c41c2020-02-05 14:26:22 +01006276 * prepare a new css_set for the child process in cgroup_can_fork().
Aleksa Sarai7e476822015-06-09 21:32:09 +10006277 */
Christian Brauneref2c41c2020-02-05 14:26:22 +01006278void cgroup_cancel_fork(struct task_struct *child,
6279 struct kernel_clone_args *kargs)
Aleksa Sarai7e476822015-06-09 21:32:09 +10006280{
6281 struct cgroup_subsys *ss;
6282 int i;
6283
6284 for_each_subsys(ss, i)
6285 if (ss->cancel_fork)
Christian Brauneref2c41c2020-02-05 14:26:22 +01006286 ss->cancel_fork(child, kargs->cset);
Christian Brauner5a5cf5c2020-02-05 14:26:20 +01006287
Christian Brauneref2c41c2020-02-05 14:26:22 +01006288 cgroup_css_set_put_fork(kargs);
Aleksa Sarai7e476822015-06-09 21:32:09 +10006289}
6290
6291/**
Christian Brauner5a5cf5c2020-02-05 14:26:20 +01006292 * cgroup_post_fork - finalize cgroup setup for the child process
6293 * @child: the child process
Li Zefana043e3b2008-02-23 15:24:09 -08006294 *
Christian Brauner5a5cf5c2020-02-05 14:26:20 +01006295 * Attach the child process to its css_set calling the subsystem fork()
6296 * callbacks.
Li Zefana043e3b2008-02-23 15:24:09 -08006297 */
Christian Brauneref2c41c2020-02-05 14:26:22 +01006298void cgroup_post_fork(struct task_struct *child,
6299 struct kernel_clone_args *kargs)
6300 __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex)
Paul Menage817929e2007-10-18 23:39:36 -07006301{
Christian Brauner661ee622021-05-08 14:15:38 +02006302 unsigned long cgrp_flags = 0;
6303 bool kill = false;
Tejun Heo30159ec2013-06-25 11:53:37 -07006304 struct cgroup_subsys *ss;
Tejun Heo5153faa2019-10-24 12:03:51 -07006305 struct css_set *cset;
Tejun Heo5edee612012-10-16 15:03:14 -07006306 int i;
6307
Christian Brauneref2c41c2020-02-05 14:26:22 +01006308 cset = kargs->cset;
6309 kargs->cset = NULL;
6310
Tejun Heo5153faa2019-10-24 12:03:51 -07006311 spin_lock_irq(&css_set_lock);
Tejun Heoeaf797a2014-02-25 10:04:03 -05006312
Tejun Heo0cd9d332020-01-30 11:37:33 -05006313 /* init tasks are special, only link regular threads */
6314 if (likely(child->pid)) {
Christian Brauner661ee622021-05-08 14:15:38 +02006315 if (kargs->cgrp)
6316 cgrp_flags = kargs->cgrp->flags;
6317 else
6318 cgrp_flags = cset->dfl_cgrp->flags;
6319
Tejun Heo0cd9d332020-01-30 11:37:33 -05006320 WARN_ON_ONCE(!list_empty(&child->cg_list));
Tejun Heo0cd9d332020-01-30 11:37:33 -05006321 cset->nr_tasks++;
6322 css_set_move_task(child, NULL, cset, false);
Christian Brauneref2c41c2020-02-05 14:26:22 +01006323 } else {
6324 put_css_set(cset);
6325 cset = NULL;
Tejun Heo0cd9d332020-01-30 11:37:33 -05006326 }
Tejun Heo5153faa2019-10-24 12:03:51 -07006327
Christian Brauner661ee622021-05-08 14:15:38 +02006328 if (!(child->flags & PF_KTHREAD)) {
6329 if (unlikely(test_bit(CGRP_FREEZE, &cgrp_flags))) {
6330 /*
6331 * If the cgroup has to be frozen, the new task has
6332 * too. Let's set the JOBCTL_TRAP_FREEZE jobctl bit to
6333 * get the task into the frozen state.
6334 */
6335 spin_lock(&child->sighand->siglock);
6336 WARN_ON_ONCE(child->frozen);
6337 child->jobctl |= JOBCTL_TRAP_FREEZE;
6338 spin_unlock(&child->sighand->siglock);
6339
6340 /*
6341 * Calling cgroup_update_frozen() isn't required here,
6342 * because it will be called anyway a bit later from
6343 * do_freezer_trap(). So we avoid cgroup's transient
6344 * switch from the frozen state and back.
6345 */
6346 }
Roman Gushchin76f969e2019-04-19 10:03:04 -07006347
6348 /*
Christian Brauner661ee622021-05-08 14:15:38 +02006349 * If the cgroup is to be killed notice it now and take the
6350 * child down right after we finished preparing it for
6351 * userspace.
Roman Gushchin76f969e2019-04-19 10:03:04 -07006352 */
Christian Brauner661ee622021-05-08 14:15:38 +02006353 kill = test_bit(CGRP_KILL, &cgrp_flags);
Paul Menage817929e2007-10-18 23:39:36 -07006354 }
Tejun Heo5edee612012-10-16 15:03:14 -07006355
Tejun Heo5153faa2019-10-24 12:03:51 -07006356 spin_unlock_irq(&css_set_lock);
6357
Tejun Heo5edee612012-10-16 15:03:14 -07006358 /*
6359 * Call ss->fork(). This must happen after @child is linked on
6360 * css_set; otherwise, @child might change state between ->fork()
6361 * and addition to css_set.
6362 */
Tejun Heob4e0eea2016-02-22 22:25:46 -05006363 do_each_subsys_mask(ss, i, have_fork_callback) {
Oleg Nesterovb53202e2015-12-03 10:24:08 -05006364 ss->fork(child);
Tejun Heob4e0eea2016-02-22 22:25:46 -05006365 } while_each_subsys_mask();
Christian Brauner5a5cf5c2020-02-05 14:26:20 +01006366
Christian Brauneref2c41c2020-02-05 14:26:22 +01006367 /* Make the new cset the root_cset of the new cgroup namespace. */
6368 if (kargs->flags & CLONE_NEWCGROUP) {
6369 struct css_set *rcset = child->nsproxy->cgroup_ns->root_cset;
6370
6371 get_css_set(cset);
6372 child->nsproxy->cgroup_ns->root_cset = cset;
6373 put_css_set(rcset);
6374 }
6375
Christian Brauner661ee622021-05-08 14:15:38 +02006376 /* Cgroup has to be killed so take down child immediately. */
6377 if (unlikely(kill))
6378 do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, child, PIDTYPE_TGID);
6379
Christian Brauneref2c41c2020-02-05 14:26:22 +01006380 cgroup_css_set_put_fork(kargs);
Paul Menage817929e2007-10-18 23:39:36 -07006381}
Tejun Heo5edee612012-10-16 15:03:14 -07006382
Paul Menage817929e2007-10-18 23:39:36 -07006383/**
Paul Menageb4f48b62007-10-18 23:39:33 -07006384 * cgroup_exit - detach cgroup from exiting task
6385 * @tsk: pointer to task_struct of exiting process
6386 *
Michal Koutnýe7c7b1d2019-10-04 12:57:39 +02006387 * Description: Detach cgroup from @tsk.
Paul Menageb4f48b62007-10-18 23:39:33 -07006388 *
Paul Menageb4f48b62007-10-18 23:39:33 -07006389 */
Li Zefan1ec41832014-03-28 15:22:19 +08006390void cgroup_exit(struct task_struct *tsk)
Paul Menageb4f48b62007-10-18 23:39:33 -07006391{
Tejun Heo30159ec2013-06-25 11:53:37 -07006392 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07006393 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01006394 int i;
Paul Menage817929e2007-10-18 23:39:36 -07006395
Tejun Heo5153faa2019-10-24 12:03:51 -07006396 spin_lock_irq(&css_set_lock);
6397
6398 WARN_ON_ONCE(list_empty(&tsk->cg_list));
Tejun Heo0de09422015-10-15 16:41:49 -04006399 cset = task_css_set(tsk);
Tejun Heo5153faa2019-10-24 12:03:51 -07006400 css_set_move_task(tsk, cset, NULL, false);
6401 list_add_tail(&tsk->cg_list, &cset->dying_tasks);
6402 cset->nr_tasks--;
Tejun Heo0de09422015-10-15 16:41:49 -04006403
Tejun Heo5153faa2019-10-24 12:03:51 -07006404 WARN_ON_ONCE(cgroup_task_frozen(tsk));
Roman Gushchinf4f809f2021-05-10 14:39:46 -07006405 if (unlikely(!(tsk->flags & PF_KTHREAD) &&
6406 test_bit(CGRP_FREEZE, &task_dfl_cgroup(tsk)->flags)))
Tejun Heo5153faa2019-10-24 12:03:51 -07006407 cgroup_update_frozen(task_dfl_cgroup(tsk));
Roman Gushchin76f969e2019-04-19 10:03:04 -07006408
Tejun Heo5153faa2019-10-24 12:03:51 -07006409 spin_unlock_irq(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -07006410
Aleksa Saraicb4a3162015-06-06 10:02:14 +10006411 /* see cgroup_post_fork() for details */
Tejun Heob4e0eea2016-02-22 22:25:46 -05006412 do_each_subsys_mask(ss, i, have_exit_callback) {
Tejun Heo2e91fa72015-10-15 16:41:53 -04006413 ss->exit(tsk);
Tejun Heob4e0eea2016-02-22 22:25:46 -05006414 } while_each_subsys_mask();
Tejun Heo2e91fa72015-10-15 16:41:53 -04006415}
Tejun Heo30159ec2013-06-25 11:53:37 -07006416
Oleg Nesterov51bee5a2019-01-28 17:00:13 +01006417void cgroup_release(struct task_struct *task)
Tejun Heo2e91fa72015-10-15 16:41:53 -04006418{
Tejun Heoafcf6c82015-10-15 16:41:53 -04006419 struct cgroup_subsys *ss;
6420 int ssid;
6421
Oleg Nesterov51bee5a2019-01-28 17:00:13 +01006422 do_each_subsys_mask(ss, ssid, have_release_callback) {
6423 ss->release(task);
Tejun Heob4e0eea2016-02-22 22:25:46 -05006424 } while_each_subsys_mask();
Tejun Heoc03cd772019-05-31 10:38:58 -07006425
Tejun Heo5153faa2019-10-24 12:03:51 -07006426 spin_lock_irq(&css_set_lock);
6427 css_set_skip_task_iters(task_css_set(task), task);
6428 list_del_init(&task->cg_list);
6429 spin_unlock_irq(&css_set_lock);
Oleg Nesterov51bee5a2019-01-28 17:00:13 +01006430}
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01006431
Oleg Nesterov51bee5a2019-01-28 17:00:13 +01006432void cgroup_free(struct task_struct *task)
6433{
6434 struct css_set *cset = task_css_set(task);
Tejun Heo2e91fa72015-10-15 16:41:53 -04006435 put_css_set(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07006436}
Paul Menage697f4162007-10-18 23:39:34 -07006437
Paul Menage8bab8dd2008-04-04 14:29:57 -07006438static int __init cgroup_disable(char *str)
6439{
Tejun Heo30159ec2013-06-25 11:53:37 -07006440 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07006441 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07006442 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07006443
6444 while ((token = strsep(&str, ",")) != NULL) {
6445 if (!*token)
6446 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07006447
Tejun Heo3ed80a62014-02-08 10:36:58 -05006448 for_each_subsys(ss, i) {
Tejun Heo3e1d2ee2015-08-18 13:58:16 -07006449 if (strcmp(token, ss->name) &&
6450 strcmp(token, ss->legacy_name))
6451 continue;
Shakeel Butt45e1ba42021-05-12 13:19:46 -07006452
6453 static_branch_disable(cgroup_subsys_enabled_key[i]);
6454 pr_info("Disabling %s control group subsystem\n",
6455 ss->name);
Paul Menage8bab8dd2008-04-04 14:29:57 -07006456 }
Suren Baghdasaryan3958e2d2021-05-24 12:53:39 -07006457
6458 for (i = 0; i < OPT_FEATURE_COUNT; i++) {
6459 if (strcmp(token, cgroup_opt_feature_names[i]))
6460 continue;
6461 cgroup_feature_disable_mask |= 1 << i;
6462 pr_info("Disabling %s control group feature\n",
6463 cgroup_opt_feature_names[i]);
6464 break;
6465 }
Paul Menage8bab8dd2008-04-04 14:29:57 -07006466 }
6467 return 1;
6468}
6469__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07006470
Waiman Long5cf81142018-11-08 10:08:46 -05006471void __init __weak enable_debug_cgroup(void) { }
6472
6473static int __init enable_cgroup_debug(char *str)
6474{
6475 cgroup_debug = true;
6476 enable_debug_cgroup();
6477 return 1;
6478}
6479__setup("cgroup_debug", enable_cgroup_debug);
6480
Tejun Heob77d7b62013-08-13 11:01:54 -04006481/**
Tejun Heoec903c02014-05-13 12:11:01 -04006482 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
Tejun Heo35cf0832013-08-26 18:40:56 -04006483 * @dentry: directory dentry of interest
6484 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04006485 *
Tejun Heo5a17f542014-02-11 11:52:47 -05006486 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
6487 * to get the corresponding css and return it. If such css doesn't exist
6488 * or can't be pinned, an ERR_PTR value is returned.
Stephane Eraniane5d13672011-02-14 11:20:01 +02006489 */
Tejun Heoec903c02014-05-13 12:11:01 -04006490struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
6491 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02006492{
Tejun Heo2bd59d42014-02-11 11:52:49 -05006493 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
Tejun Heof17fc252016-02-23 10:00:51 -05006494 struct file_system_type *s_type = dentry->d_sb->s_type;
Tejun Heo2bd59d42014-02-11 11:52:49 -05006495 struct cgroup_subsys_state *css = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02006496 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02006497
Tejun Heo35cf0832013-08-26 18:40:56 -04006498 /* is @dentry a cgroup dir? */
Tejun Heof17fc252016-02-23 10:00:51 -05006499 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
6500 !kn || kernfs_type(kn) != KERNFS_DIR)
Stephane Eraniane5d13672011-02-14 11:20:01 +02006501 return ERR_PTR(-EBADF);
6502
Tejun Heo5a17f542014-02-11 11:52:47 -05006503 rcu_read_lock();
6504
Tejun Heo2bd59d42014-02-11 11:52:49 -05006505 /*
6506 * This path doesn't originate from kernfs and @kn could already
6507 * have been or be removed at any point. @kn->priv is RCU
Li Zefana4189482014-09-04 14:43:07 +08006508 * protected for this access. See css_release_work_fn() for details.
Tejun Heo2bd59d42014-02-11 11:52:49 -05006509 */
Tejun Heoe0aed7c2016-12-27 14:49:09 -05006510 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
Tejun Heo2bd59d42014-02-11 11:52:49 -05006511 if (cgrp)
6512 css = cgroup_css(cgrp, ss);
Tejun Heo5a17f542014-02-11 11:52:47 -05006513
Tejun Heoec903c02014-05-13 12:11:01 -04006514 if (!css || !css_tryget_online(css))
Tejun Heo5a17f542014-02-11 11:52:47 -05006515 css = ERR_PTR(-ENOENT);
6516
6517 rcu_read_unlock();
6518 return css;
Stephane Eraniane5d13672011-02-14 11:20:01 +02006519}
Stephane Eraniane5d13672011-02-14 11:20:01 +02006520
Li Zefan1cb650b2013-08-19 10:05:24 +08006521/**
6522 * css_from_id - lookup css by id
6523 * @id: the cgroup id
6524 * @ss: cgroup subsys to be looked into
6525 *
6526 * Returns the css if there's valid one with @id, otherwise returns NULL.
6527 * Should be called under rcu_read_lock().
6528 */
6529struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
6530{
Tejun Heo6fa49182014-05-04 15:09:13 -04006531 WARN_ON_ONCE(!rcu_read_lock_held());
Johannes Weinerd6ccc552016-06-17 12:24:27 -04006532 return idr_find(&ss->css_idr, id);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006533}
6534
Tejun Heo16af4392015-11-20 15:55:52 -05006535/**
6536 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
6537 * @path: path on the default hierarchy
6538 *
6539 * Find the cgroup at @path on the default hierarchy, increment its
6540 * reference count and return it. Returns pointer to the found cgroup on
Shakeel Buttbe288162021-10-24 23:19:14 -07006541 * success, ERR_PTR(-ENOENT) if @path doesn't exist or if the cgroup has already
6542 * been released and ERR_PTR(-ENOTDIR) if @path points to a non-directory.
Tejun Heo16af4392015-11-20 15:55:52 -05006543 */
6544struct cgroup *cgroup_get_from_path(const char *path)
6545{
6546 struct kernfs_node *kn;
Shakeel Buttbe288162021-10-24 23:19:14 -07006547 struct cgroup *cgrp = ERR_PTR(-ENOENT);
Tejun Heo16af4392015-11-20 15:55:52 -05006548
6549 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
Shakeel Buttbe288162021-10-24 23:19:14 -07006550 if (!kn)
6551 goto out;
6552
6553 if (kernfs_type(kn) != KERNFS_DIR) {
6554 cgrp = ERR_PTR(-ENOTDIR);
6555 goto out_kernfs;
Tejun Heo16af4392015-11-20 15:55:52 -05006556 }
6557
Shakeel Buttbe288162021-10-24 23:19:14 -07006558 rcu_read_lock();
6559
6560 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
6561 if (!cgrp || !cgroup_tryget(cgrp))
6562 cgrp = ERR_PTR(-ENOENT);
6563
6564 rcu_read_unlock();
6565
6566out_kernfs:
6567 kernfs_put(kn);
6568out:
Tejun Heo16af4392015-11-20 15:55:52 -05006569 return cgrp;
6570}
6571EXPORT_SYMBOL_GPL(cgroup_get_from_path);
6572
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -07006573/**
6574 * cgroup_get_from_fd - get a cgroup pointer from a fd
6575 * @fd: fd obtained by open(cgroup2_dir)
6576 *
6577 * Find the cgroup from a fd which should be obtained
6578 * by opening a cgroup directory. Returns a pointer to the
6579 * cgroup on success. ERR_PTR is returned if the cgroup
6580 * cannot be found.
6581 */
6582struct cgroup *cgroup_get_from_fd(int fd)
6583{
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -07006584 struct cgroup *cgrp;
6585 struct file *f;
6586
6587 f = fget_raw(fd);
6588 if (!f)
6589 return ERR_PTR(-EBADF);
6590
Christian Brauner17703092020-02-05 14:26:19 +01006591 cgrp = cgroup_get_from_file(f);
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -07006592 fput(f);
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -07006593 return cgrp;
6594}
6595EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
6596
Tejun Heo38cf3a62019-06-14 10:12:45 -07006597static u64 power_of_ten(int power)
6598{
6599 u64 v = 1;
6600 while (power--)
6601 v *= 10;
6602 return v;
6603}
6604
6605/**
6606 * cgroup_parse_float - parse a floating number
6607 * @input: input string
6608 * @dec_shift: number of decimal digits to shift
6609 * @v: output
6610 *
6611 * Parse a decimal floating point number in @input and store the result in
6612 * @v with decimal point right shifted @dec_shift times. For example, if
6613 * @input is "12.3456" and @dec_shift is 3, *@v will be set to 12345.
6614 * Returns 0 on success, -errno otherwise.
6615 *
6616 * There's nothing cgroup specific about this function except that it's
6617 * currently the only user.
6618 */
6619int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
6620{
6621 s64 whole, frac = 0;
6622 int fstart = 0, fend = 0, flen;
6623
6624 if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend))
6625 return -EINVAL;
6626 if (frac < 0)
6627 return -EINVAL;
6628
6629 flen = fend > fstart ? fend - fstart : 0;
6630 if (flen < dec_shift)
6631 frac *= power_of_ten(dec_shift - flen);
6632 else
6633 frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift));
6634
6635 *v = whole * power_of_ten(dec_shift) + frac;
6636 return 0;
6637}
6638
Tejun Heobd1060a2015-12-07 17:38:53 -05006639/*
6640 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
6641 * definition in cgroup-defs.h.
6642 */
6643#ifdef CONFIG_SOCK_CGROUP_DATA
6644
Tejun Heobd1060a2015-12-07 17:38:53 -05006645void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
6646{
Daniel Borkmann78cc3162021-09-27 14:39:20 +02006647 struct cgroup *cgroup;
Shakeel Butte876ecc2020-03-09 22:16:05 -07006648
Tejun Heobd1060a2015-12-07 17:38:53 -05006649 rcu_read_lock();
Daniel Borkmann78cc3162021-09-27 14:39:20 +02006650 /* Don't associate the sock with unrelated interrupted task's cgroup. */
6651 if (in_interrupt()) {
6652 cgroup = &cgrp_dfl_root.cgrp;
6653 cgroup_get(cgroup);
6654 goto out;
6655 }
6656
Tejun Heobd1060a2015-12-07 17:38:53 -05006657 while (true) {
6658 struct css_set *cset;
6659
6660 cset = task_css_set(current);
6661 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
Daniel Borkmann78cc3162021-09-27 14:39:20 +02006662 cgroup = cset->dfl_cgrp;
Tejun Heobd1060a2015-12-07 17:38:53 -05006663 break;
6664 }
6665 cpu_relax();
6666 }
Daniel Borkmann78cc3162021-09-27 14:39:20 +02006667out:
6668 skcd->cgroup = cgroup;
6669 cgroup_bpf_get(cgroup);
Tejun Heobd1060a2015-12-07 17:38:53 -05006670 rcu_read_unlock();
6671}
6672
Cong Wangad0f75e2020-07-02 11:52:56 -07006673void cgroup_sk_clone(struct sock_cgroup_data *skcd)
6674{
Daniel Borkmann8520e222021-09-14 01:07:57 +02006675 struct cgroup *cgrp = sock_cgroup_ptr(skcd);
6676
6677 /*
6678 * We might be cloning a socket which is left in an empty
6679 * cgroup and the cgroup might have already been rmdir'd.
6680 * Don't use cgroup_get_live().
6681 */
6682 cgroup_get(cgrp);
6683 cgroup_bpf_get(cgrp);
Cong Wangad0f75e2020-07-02 11:52:56 -07006684}
6685
Tejun Heobd1060a2015-12-07 17:38:53 -05006686void cgroup_sk_free(struct sock_cgroup_data *skcd)
6687{
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -07006688 struct cgroup *cgrp = sock_cgroup_ptr(skcd);
6689
6690 cgroup_bpf_put(cgrp);
6691 cgroup_put(cgrp);
Tejun Heobd1060a2015-12-07 17:38:53 -05006692}
6693
6694#endif /* CONFIG_SOCK_CGROUP_DATA */
6695
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006696#ifdef CONFIG_SYSFS
6697static ssize_t show_delegatable_files(struct cftype *files, char *buf,
6698 ssize_t size, const char *prefix)
6699{
6700 struct cftype *cft;
6701 ssize_t ret = 0;
6702
6703 for (cft = files; cft && cft->name[0] != '\0'; cft++) {
6704 if (!(cft->flags & CFTYPE_NS_DELEGATABLE))
6705 continue;
6706
Suren Baghdasaryan3958e2d2021-05-24 12:53:39 -07006707 if ((cft->flags & CFTYPE_PRESSURE) && !cgroup_psi_enabled())
6708 continue;
6709
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006710 if (prefix)
6711 ret += snprintf(buf + ret, size - ret, "%s.", prefix);
6712
6713 ret += snprintf(buf + ret, size - ret, "%s\n", cft->name);
6714
Yangtao Li4d9ebbe2018-11-03 22:27:41 -04006715 if (WARN_ON(ret >= size))
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006716 break;
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006717 }
6718
6719 return ret;
6720}
6721
6722static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr,
6723 char *buf)
6724{
6725 struct cgroup_subsys *ss;
6726 int ssid;
6727 ssize_t ret = 0;
6728
6729 ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret,
6730 NULL);
6731
6732 for_each_subsys(ss, ssid)
6733 ret += show_delegatable_files(ss->dfl_cftypes, buf + ret,
6734 PAGE_SIZE - ret,
6735 cgroup_subsys_name[ssid]);
6736
6737 return ret;
6738}
6739static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
6740
Roman Gushchin5f2e6732017-11-06 13:30:29 -05006741static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
6742 char *buf)
6743{
Johannes Weiner8a931f82020-04-01 21:07:07 -07006744 return snprintf(buf, PAGE_SIZE,
6745 "nsdelegate\n"
6746 "memory_localevents\n"
6747 "memory_recursiveprot\n");
Roman Gushchin5f2e6732017-11-06 13:30:29 -05006748}
6749static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);
6750
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006751static struct attribute *cgroup_sysfs_attrs[] = {
6752 &cgroup_delegate_attr.attr,
Roman Gushchin5f2e6732017-11-06 13:30:29 -05006753 &cgroup_features_attr.attr,
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006754 NULL,
6755};
6756
6757static const struct attribute_group cgroup_sysfs_attr_group = {
6758 .attrs = cgroup_sysfs_attrs,
6759 .name = "cgroup",
6760};
6761
6762static int __init cgroup_sysfs_init(void)
6763{
6764 return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group);
6765}
6766subsys_initcall(cgroup_sysfs_init);
Tejun Heoa5e112e2019-05-13 12:37:17 -07006767
Roman Gushchin01ee6cf2017-11-06 13:30:28 -05006768#endif /* CONFIG_SYSFS */