Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 | * 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. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 | * Notifications support |
| 8 | * Copyright (C) 2009 Nokia Corporation |
| 9 | * Author: Kirill A. Shutemov |
| 10 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 | * 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 Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 31 | #include "cgroup-internal.h" |
| 32 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 33 | #include <linux/cred.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 34 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 35 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 36 | #include <linux/kernel.h> |
Jianyu Zhan | c9482a5 | 2014-04-26 15:40:28 +0800 | [diff] [blame] | 37 | #include <linux/magic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 38 | #include <linux/mutex.h> |
| 39 | #include <linux/mount.h> |
| 40 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 41 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 42 | #include <linux/rcupdate.h> |
| 43 | #include <linux/sched.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 44 | #include <linux/sched/task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 45 | #include <linux/slab.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 46 | #include <linux/spinlock.h> |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 47 | #include <linux/percpu-rwsem.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 48 | #include <linux/string.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 49 | #include <linux/hashtable.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 50 | #include <linux/idr.h> |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 51 | #include <linux/kthread.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 52 | #include <linux/atomic.h> |
Tejun Heo | e93ad19 | 2016-01-19 12:18:41 -0500 | [diff] [blame] | 53 | #include <linux/cpuset.h> |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 54 | #include <linux/proc_ns.h> |
| 55 | #include <linux/nsproxy.h> |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 56 | #include <linux/file.h> |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 57 | #include <linux/fs_parser.h> |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 58 | #include <linux/sched/cputime.h> |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 59 | #include <linux/psi.h> |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 60 | #include <net/sock.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 61 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 62 | #define CREATE_TRACE_POINTS |
| 63 | #include <trace/events/cgroup.h> |
| 64 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 65 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 66 | MAX_CFTYPE_NAME + 2) |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 67 | /* let's not notify more than 100 times per second */ |
| 68 | #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100) |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 69 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 70 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 71 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 72 | * hierarchy must be performed while holding it. |
| 73 | * |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 74 | * css_set_lock protects task->cgroups pointer, the list of css_set |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 75 | * objects, and the chain of tasks off each css_set. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 76 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 77 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in |
| 78 | * cgroup.h can use them for lockdep annotations. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 79 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 80 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 81 | DEFINE_SPINLOCK(css_set_lock); |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 82 | |
| 83 | #ifdef CONFIG_PROVE_RCU |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 84 | EXPORT_SYMBOL_GPL(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 85 | EXPORT_SYMBOL_GPL(css_set_lock); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 86 | #endif |
| 87 | |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 88 | DEFINE_SPINLOCK(trace_cgroup_path_lock); |
| 89 | char trace_cgroup_path[TRACE_CGROUP_PATH_LEN]; |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 90 | bool cgroup_debug __read_mostly; |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 91 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 92 | /* |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 93 | * Protects cgroup_idr and css_idr so that IDs can be released without |
| 94 | * grabbing cgroup_mutex. |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 95 | */ |
| 96 | static DEFINE_SPINLOCK(cgroup_idr_lock); |
| 97 | |
| 98 | /* |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 99 | * Protects cgroup_file->kn for !self csses. It synchronizes notifications |
| 100 | * against file removal/re-creation across css hiding. |
| 101 | */ |
| 102 | static DEFINE_SPINLOCK(cgroup_file_kn_lock); |
| 103 | |
Oleg Nesterov | 3f2947b | 2019-04-23 18:32:41 +0200 | [diff] [blame] | 104 | DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem); |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 105 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 106 | #define cgroup_assert_mutex_or_rcu_locked() \ |
Paul E. McKenney | f78f5b9 | 2015-06-18 15:50:02 -0700 | [diff] [blame] | 107 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
| 108 | !lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 109 | "cgroup_mutex or RCU read lock required"); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 110 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 111 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 112 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 113 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 114 | * destruction work items don't end up filling up max_active of system_wq |
| 115 | * which may lead to deadlock. |
| 116 | */ |
| 117 | static struct workqueue_struct *cgroup_destroy_wq; |
| 118 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 119 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 120 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 121 | struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 122 | #include <linux/cgroup_subsys.h> |
| 123 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 124 | #undef SUBSYS |
| 125 | |
| 126 | /* array of cgroup subsystem names */ |
| 127 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 128 | static const char *cgroup_subsys_name[] = { |
| 129 | #include <linux/cgroup_subsys.h> |
| 130 | }; |
| 131 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 132 | |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 133 | /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */ |
| 134 | #define SUBSYS(_x) \ |
| 135 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \ |
| 136 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \ |
| 137 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \ |
| 138 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key); |
| 139 | #include <linux/cgroup_subsys.h> |
| 140 | #undef SUBSYS |
| 141 | |
| 142 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key, |
| 143 | static struct static_key_true *cgroup_subsys_enabled_key[] = { |
| 144 | #include <linux/cgroup_subsys.h> |
| 145 | }; |
| 146 | #undef SUBSYS |
| 147 | |
| 148 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key, |
| 149 | static struct static_key_true *cgroup_subsys_on_dfl_key[] = { |
| 150 | #include <linux/cgroup_subsys.h> |
| 151 | }; |
| 152 | #undef SUBSYS |
| 153 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 154 | static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 155 | |
Zefan Li | 6b6ebb3 | 2020-05-13 10:13:11 +0800 | [diff] [blame] | 156 | /* the default hierarchy */ |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 157 | struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu }; |
Tejun Heo | d0ec423 | 2015-08-05 16:03:19 -0400 | [diff] [blame] | 158 | EXPORT_SYMBOL_GPL(cgrp_dfl_root); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 159 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 160 | /* |
| 161 | * The default hierarchy always exists but is hidden until mounted for the |
| 162 | * first time. This is for backward compatibility. |
| 163 | */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 164 | static bool cgrp_dfl_visible; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 165 | |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 166 | /* some controllers are not supported in the default hierarchy */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 167 | static u16 cgrp_dfl_inhibit_ss_mask; |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 168 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 169 | /* some controllers are implicitly enabled on the default hierarchy */ |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 170 | static u16 cgrp_dfl_implicit_ss_mask; |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 171 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 172 | /* some controllers can be threaded on the default hierarchy */ |
| 173 | static u16 cgrp_dfl_threaded_ss_mask; |
| 174 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 175 | /* The list of hierarchy roots */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 176 | LIST_HEAD(cgroup_roots); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 177 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 178 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 179 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 180 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 181 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 182 | /* |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 183 | * Assign a monotonically increasing serial number to csses. It guarantees |
| 184 | * cgroups with bigger numbers are newer than those with smaller numbers. |
| 185 | * Also, as csses are always appended to the parent's ->children list, it |
| 186 | * guarantees that sibling csses are always sorted in the ascending serial |
| 187 | * number order on the list. Protected by cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 188 | */ |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 189 | static u64 css_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 190 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 191 | /* |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 192 | * These bitmasks identify subsystems with specific features to avoid |
| 193 | * having to do iterative checks repeatedly. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 194 | */ |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 195 | static u16 have_fork_callback __read_mostly; |
| 196 | static u16 have_exit_callback __read_mostly; |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 197 | static u16 have_release_callback __read_mostly; |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 198 | static u16 have_canfork_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 199 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 200 | /* cgroup namespace for init task */ |
| 201 | struct cgroup_namespace init_cgroup_ns = { |
Kirill Tkhai | f387882 | 2020-08-03 13:16:50 +0300 | [diff] [blame] | 202 | .ns.count = REFCOUNT_INIT(2), |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 203 | .user_ns = &init_user_ns, |
| 204 | .ns.ops = &cgroupns_operations, |
| 205 | .ns.inum = PROC_CGROUP_INIT_INO, |
| 206 | .root_cset = &init_css_set, |
| 207 | }; |
| 208 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 209 | static struct file_system_type cgroup2_fs_type; |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 210 | static struct cftype cgroup_base_files[]; |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 211 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 212 | static int cgroup_apply_control(struct cgroup *cgrp); |
| 213 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret); |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 214 | static void css_task_iter_skip(struct css_task_iter *it, |
| 215 | struct task_struct *task); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 216 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 217 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 218 | struct cgroup_subsys *ss); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 219 | static void css_release(struct percpu_ref *ref); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 220 | static void kill_css(struct cgroup_subsys_state *css); |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 221 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 222 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 223 | bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 224 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 225 | /** |
| 226 | * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID |
| 227 | * @ssid: subsys ID of interest |
| 228 | * |
| 229 | * cgroup_subsys_enabled() can only be used with literal subsys names which |
| 230 | * is fine for individual subsystems but unsuitable for cgroup core. This |
| 231 | * is slower static_key_enabled() based test indexed by @ssid. |
| 232 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 233 | bool cgroup_ssid_enabled(int ssid) |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 234 | { |
Arnd Bergmann | cfe02a8 | 2016-03-15 00:21:06 +0100 | [diff] [blame] | 235 | if (CGROUP_SUBSYS_COUNT == 0) |
| 236 | return false; |
| 237 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 238 | return static_key_enabled(cgroup_subsys_enabled_key[ssid]); |
| 239 | } |
| 240 | |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 241 | /** |
| 242 | * cgroup_on_dfl - test whether a cgroup is on the default hierarchy |
| 243 | * @cgrp: the cgroup of interest |
| 244 | * |
| 245 | * The default hierarchy is the v2 interface of cgroup and this function |
| 246 | * can be used to test whether a cgroup is on the default hierarchy for |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 247 | * cases where a subsystem should behave differently depending on the |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 248 | * interface version. |
| 249 | * |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 250 | * List of changed behaviors: |
| 251 | * |
| 252 | * - Mount options "noprefix", "xattr", "clone_children", "release_agent" |
| 253 | * and "name" are disallowed. |
| 254 | * |
| 255 | * - When mounting an existing superblock, mount options should match. |
| 256 | * |
| 257 | * - Remount is disallowed. |
| 258 | * |
| 259 | * - rename(2) is disallowed. |
| 260 | * |
| 261 | * - "tasks" is removed. Everything should be at process granularity. Use |
| 262 | * "cgroup.procs" instead. |
| 263 | * |
| 264 | * - "cgroup.procs" is not sorted. pids will be unique unless they got |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 265 | * recycled in-between reads. |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 266 | * |
| 267 | * - "release_agent" and "notify_on_release" are removed. Replacement |
| 268 | * notification mechanism will be implemented. |
| 269 | * |
| 270 | * - "cgroup.clone_children" is removed. |
| 271 | * |
| 272 | * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup |
| 273 | * and its descendants contain no task; otherwise, 1. The file also |
| 274 | * generates kernfs notification which can be monitored through poll and |
| 275 | * [di]notify when the value of the file changes. |
| 276 | * |
| 277 | * - cpuset: tasks will be kept in empty cpusets when hotplug happens and |
| 278 | * take masks of ancestors with non-empty cpus/mems, instead of being |
| 279 | * moved to an ancestor. |
| 280 | * |
| 281 | * - cpuset: a task can be moved into an empty cpuset, and again it takes |
| 282 | * masks of ancestors. |
| 283 | * |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 284 | * - blkcg: blk-throttle becomes properly hierarchical. |
| 285 | * |
| 286 | * - debug: disallowed on the default hierarchy. |
| 287 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 288 | bool cgroup_on_dfl(const struct cgroup *cgrp) |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 289 | { |
| 290 | return cgrp->root == &cgrp_dfl_root; |
| 291 | } |
| 292 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 293 | /* IDR wrappers which synchronize using cgroup_idr_lock */ |
| 294 | static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, |
| 295 | gfp_t gfp_mask) |
| 296 | { |
| 297 | int ret; |
| 298 | |
| 299 | idr_preload(gfp_mask); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 300 | spin_lock_bh(&cgroup_idr_lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 301 | ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 302 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 303 | idr_preload_end(); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) |
| 308 | { |
| 309 | void *ret; |
| 310 | |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 311 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 312 | ret = idr_replace(idr, ptr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 313 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | static void cgroup_idr_remove(struct idr *idr, int id) |
| 318 | { |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 319 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 320 | idr_remove(idr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 321 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 324 | static bool cgroup_has_tasks(struct cgroup *cgrp) |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 325 | { |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 326 | return cgrp->nr_populated_csets; |
| 327 | } |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 328 | |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 329 | bool cgroup_is_threaded(struct cgroup *cgrp) |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 330 | { |
| 331 | return cgrp->dom_cgrp != cgrp; |
| 332 | } |
| 333 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 334 | /* can @cgrp host both domain and threaded children? */ |
| 335 | static bool cgroup_is_mixable(struct cgroup *cgrp) |
| 336 | { |
| 337 | /* |
| 338 | * Root isn't under domain level resource control exempting it from |
| 339 | * the no-internal-process constraint, so it can serve as a thread |
| 340 | * root and a parent of resource domains at the same time. |
| 341 | */ |
| 342 | return !cgroup_parent(cgrp); |
| 343 | } |
| 344 | |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 345 | /* can @cgrp become a thread root? Should always be true for a thread root */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 346 | static bool cgroup_can_be_thread_root(struct cgroup *cgrp) |
| 347 | { |
| 348 | /* mixables don't care */ |
| 349 | if (cgroup_is_mixable(cgrp)) |
| 350 | return true; |
| 351 | |
| 352 | /* domain roots can't be nested under threaded */ |
| 353 | if (cgroup_is_threaded(cgrp)) |
| 354 | return false; |
| 355 | |
| 356 | /* can only have either domain or threaded children */ |
| 357 | if (cgrp->nr_populated_domain_children) |
| 358 | return false; |
| 359 | |
| 360 | /* and no domain controllers can be enabled */ |
| 361 | if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 362 | return false; |
| 363 | |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | /* is @cgrp root of a threaded subtree? */ |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 368 | bool cgroup_is_thread_root(struct cgroup *cgrp) |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 369 | { |
| 370 | /* thread root should be a domain */ |
| 371 | if (cgroup_is_threaded(cgrp)) |
| 372 | return false; |
| 373 | |
| 374 | /* a domain w/ threaded children is a thread root */ |
| 375 | if (cgrp->nr_threaded_children) |
| 376 | return true; |
| 377 | |
| 378 | /* |
| 379 | * A domain which has tasks and explicit threaded controllers |
| 380 | * enabled is a thread root. |
| 381 | */ |
| 382 | if (cgroup_has_tasks(cgrp) && |
| 383 | (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask)) |
| 384 | return true; |
| 385 | |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | /* a domain which isn't connected to the root w/o brekage can't be used */ |
| 390 | static bool cgroup_is_valid_domain(struct cgroup *cgrp) |
| 391 | { |
| 392 | /* the cgroup itself can be a thread root */ |
| 393 | if (cgroup_is_threaded(cgrp)) |
| 394 | return false; |
| 395 | |
| 396 | /* but the ancestors can't be unless mixable */ |
| 397 | while ((cgrp = cgroup_parent(cgrp))) { |
| 398 | if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp)) |
| 399 | return false; |
| 400 | if (cgroup_is_threaded(cgrp)) |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | return true; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 405 | } |
| 406 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 407 | /* subsystems visibly enabled on a cgroup */ |
| 408 | static u16 cgroup_control(struct cgroup *cgrp) |
| 409 | { |
| 410 | struct cgroup *parent = cgroup_parent(cgrp); |
| 411 | u16 root_ss_mask = cgrp->root->subsys_mask; |
| 412 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 413 | if (parent) { |
| 414 | u16 ss_mask = parent->subtree_control; |
| 415 | |
| 416 | /* threaded cgroups can only have threaded controllers */ |
| 417 | if (cgroup_is_threaded(cgrp)) |
| 418 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 419 | return ss_mask; |
| 420 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 421 | |
| 422 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 423 | root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask | |
| 424 | cgrp_dfl_implicit_ss_mask); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 425 | return root_ss_mask; |
| 426 | } |
| 427 | |
| 428 | /* subsystems enabled on a cgroup */ |
| 429 | static u16 cgroup_ss_mask(struct cgroup *cgrp) |
| 430 | { |
| 431 | struct cgroup *parent = cgroup_parent(cgrp); |
| 432 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 433 | if (parent) { |
| 434 | u16 ss_mask = parent->subtree_ss_mask; |
| 435 | |
| 436 | /* threaded cgroups can only have threaded controllers */ |
| 437 | if (cgroup_is_threaded(cgrp)) |
| 438 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 439 | return ss_mask; |
| 440 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 441 | |
| 442 | return cgrp->root->subsys_mask; |
| 443 | } |
| 444 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 445 | /** |
| 446 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 447 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 448 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 449 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 450 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 451 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 452 | * the caller is responsible for pinning the returned css if it wants to |
| 453 | * keep accessing it outside the said locks. This function may return |
| 454 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 455 | */ |
| 456 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 457 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 458 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 459 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 460 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 461 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 462 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 463 | return &cgrp->self; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 464 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 465 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 466 | /** |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 467 | * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem |
| 468 | * @cgrp: the cgroup of interest |
| 469 | * @ss: the subsystem of interest |
| 470 | * |
| 471 | * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist |
| 472 | * or is offline, %NULL is returned. |
| 473 | */ |
| 474 | static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp, |
| 475 | struct cgroup_subsys *ss) |
| 476 | { |
| 477 | struct cgroup_subsys_state *css; |
| 478 | |
| 479 | rcu_read_lock(); |
| 480 | css = cgroup_css(cgrp, ss); |
Peng Wang | a581563 | 2019-07-03 10:07:49 +0800 | [diff] [blame] | 481 | if (css && !css_tryget_online(css)) |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 482 | css = NULL; |
| 483 | rcu_read_unlock(); |
| 484 | |
| 485 | return css; |
| 486 | } |
| 487 | |
| 488 | /** |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 489 | * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 490 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 491 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 492 | * |
Chen Hanxiao | d0f702e | 2015-04-23 07:57:33 -0400 | [diff] [blame] | 493 | * Similar to cgroup_css() but returns the effective css, which is defined |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 494 | * as the matching css of the nearest ancestor including self which has @ss |
| 495 | * enabled. If @ss is associated with the hierarchy @cgrp is on, this |
| 496 | * function is guaranteed to return non-NULL css. |
| 497 | */ |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 498 | static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp, |
| 499 | struct cgroup_subsys *ss) |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 500 | { |
| 501 | lockdep_assert_held(&cgroup_mutex); |
| 502 | |
| 503 | if (!ss) |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 504 | return &cgrp->self; |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 505 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 506 | /* |
| 507 | * This function is used while updating css associations and thus |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 508 | * can't test the csses directly. Test ss_mask. |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 509 | */ |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 510 | while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 511 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 512 | if (!cgrp) |
| 513 | return NULL; |
| 514 | } |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 515 | |
| 516 | return cgroup_css(cgrp, ss); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 519 | /** |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 520 | * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem |
| 521 | * @cgrp: the cgroup of interest |
| 522 | * @ss: the subsystem of interest |
| 523 | * |
| 524 | * Find and get the effective css of @cgrp for @ss. The effective css is |
| 525 | * defined as the matching css of the nearest ancestor including self which |
| 526 | * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, |
| 527 | * the root css is returned, so this function always returns a valid css. |
| 528 | * |
| 529 | * The returned css is not guaranteed to be online, and therefore it is the |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 530 | * callers responsibility to try get a reference for it. |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 531 | */ |
| 532 | struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, |
| 533 | struct cgroup_subsys *ss) |
| 534 | { |
| 535 | struct cgroup_subsys_state *css; |
| 536 | |
| 537 | do { |
| 538 | css = cgroup_css(cgrp, ss); |
| 539 | |
| 540 | if (css) |
| 541 | return css; |
| 542 | cgrp = cgroup_parent(cgrp); |
| 543 | } while (cgrp); |
| 544 | |
| 545 | return init_css_set.subsys[ss->id]; |
| 546 | } |
| 547 | |
| 548 | /** |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 549 | * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem |
| 550 | * @cgrp: the cgroup of interest |
| 551 | * @ss: the subsystem of interest |
| 552 | * |
| 553 | * Find and get the effective css of @cgrp for @ss. The effective css is |
| 554 | * defined as the matching css of the nearest ancestor including self which |
| 555 | * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, |
| 556 | * the root css is returned, so this function always returns a valid css. |
| 557 | * The returned css must be put using css_put(). |
| 558 | */ |
| 559 | struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp, |
| 560 | struct cgroup_subsys *ss) |
| 561 | { |
| 562 | struct cgroup_subsys_state *css; |
| 563 | |
| 564 | rcu_read_lock(); |
| 565 | |
| 566 | do { |
| 567 | css = cgroup_css(cgrp, ss); |
| 568 | |
| 569 | if (css && css_tryget_online(css)) |
| 570 | goto out_unlock; |
| 571 | cgrp = cgroup_parent(cgrp); |
| 572 | } while (cgrp); |
| 573 | |
| 574 | css = init_css_set.subsys[ss->id]; |
| 575 | css_get(css); |
| 576 | out_unlock: |
| 577 | rcu_read_unlock(); |
| 578 | return css; |
| 579 | } |
| 580 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 581 | static void cgroup_get_live(struct cgroup *cgrp) |
Tejun Heo | 052c3f3 | 2015-10-15 16:41:50 -0400 | [diff] [blame] | 582 | { |
| 583 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 584 | css_get(&cgrp->self); |
| 585 | } |
| 586 | |
Roman Gushchin | aade7f9 | 2019-04-19 10:03:02 -0700 | [diff] [blame] | 587 | /** |
| 588 | * __cgroup_task_count - count the number of tasks in a cgroup. The caller |
| 589 | * is responsible for taking the css_set_lock. |
| 590 | * @cgrp: the cgroup in question |
| 591 | */ |
| 592 | int __cgroup_task_count(const struct cgroup *cgrp) |
| 593 | { |
| 594 | int count = 0; |
| 595 | struct cgrp_cset_link *link; |
| 596 | |
| 597 | lockdep_assert_held(&css_set_lock); |
| 598 | |
| 599 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 600 | count += link->cset->nr_tasks; |
| 601 | |
| 602 | return count; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 607 | * @cgrp: the cgroup in question |
| 608 | */ |
| 609 | int cgroup_task_count(const struct cgroup *cgrp) |
| 610 | { |
| 611 | int count; |
| 612 | |
| 613 | spin_lock_irq(&css_set_lock); |
| 614 | count = __cgroup_task_count(cgrp); |
| 615 | spin_unlock_irq(&css_set_lock); |
| 616 | |
| 617 | return count; |
| 618 | } |
| 619 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 620 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 621 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 622 | struct cgroup *cgrp = of->kn->parent->priv; |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 623 | struct cftype *cft = of_cft(of); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * This is open and unprotected implementation of cgroup_css(). |
| 627 | * seq_css() is only called from a kernfs file operation which has |
| 628 | * an active reference on the file. Because all the subsystem |
| 629 | * files are drained before a css is disassociated with a cgroup, |
| 630 | * the matching css from the cgroup's subsys table is guaranteed to |
| 631 | * be and stay valid until the enclosing operation is complete. |
| 632 | */ |
| 633 | if (cft->ss) |
| 634 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 635 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 636 | return &cgrp->self; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 637 | } |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 638 | EXPORT_SYMBOL_GPL(of_css); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 639 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 640 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 641 | * for_each_css - iterate all css's of a cgroup |
| 642 | * @css: the iteration cursor |
| 643 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 644 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 645 | * |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 646 | * Should be called under cgroup_[tree_]mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 647 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 648 | #define for_each_css(css, ssid, cgrp) \ |
| 649 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 650 | if (!((css) = rcu_dereference_check( \ |
| 651 | (cgrp)->subsys[(ssid)], \ |
| 652 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 653 | else |
| 654 | |
| 655 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 656 | * for_each_e_css - iterate all effective css's of a cgroup |
| 657 | * @css: the iteration cursor |
| 658 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 659 | * @cgrp: the target cgroup to iterate css's of |
| 660 | * |
| 661 | * Should be called under cgroup_[tree_]mutex. |
| 662 | */ |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 663 | #define for_each_e_css(css, ssid, cgrp) \ |
| 664 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 665 | if (!((css) = cgroup_e_css_by_mask(cgrp, \ |
| 666 | cgroup_subsys[(ssid)]))) \ |
| 667 | ; \ |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 668 | else |
| 669 | |
| 670 | /** |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 671 | * do_each_subsys_mask - filter for_each_subsys with a bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 672 | * @ss: the iteration cursor |
| 673 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 674 | * @ss_mask: the bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 675 | * |
| 676 | * The block will only run for cases where the ssid-th bit (1 << ssid) of |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 677 | * @ss_mask is set. |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 678 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 679 | #define do_each_subsys_mask(ss, ssid, ss_mask) do { \ |
| 680 | unsigned long __ss_mask = (ss_mask); \ |
| 681 | if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \ |
Aleksa Sarai | 4a705c5 | 2015-06-09 21:32:07 +1000 | [diff] [blame] | 682 | (ssid) = 0; \ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 683 | break; \ |
| 684 | } \ |
| 685 | for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \ |
| 686 | (ss) = cgroup_subsys[ssid]; \ |
| 687 | { |
| 688 | |
| 689 | #define while_each_subsys_mask() \ |
| 690 | } \ |
| 691 | } \ |
| 692 | } while (false) |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 693 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 694 | /* iterate over child cgrps, lock should be held throughout iteration */ |
| 695 | #define cgroup_for_each_live_child(child, cgrp) \ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 696 | list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 697 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 698 | cgroup_is_dead(child); })) \ |
| 699 | ; \ |
| 700 | else |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 701 | |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 702 | /* walk live descendants in pre order */ |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 703 | #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \ |
| 704 | css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \ |
| 705 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 706 | (dsct) = (d_css)->cgroup; \ |
| 707 | cgroup_is_dead(dsct); })) \ |
| 708 | ; \ |
| 709 | else |
| 710 | |
| 711 | /* walk live descendants in postorder */ |
| 712 | #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \ |
| 713 | css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \ |
| 714 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 715 | (dsct) = (d_css)->cgroup; \ |
| 716 | cgroup_is_dead(dsct); })) \ |
| 717 | ; \ |
| 718 | else |
| 719 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 720 | /* |
| 721 | * The default css_set - used by init and its children prior to any |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 722 | * hierarchies being mounted. It contains a pointer to the root state |
| 723 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 724 | * reference-counted, to improve performance when child cgroups |
| 725 | * haven't been created. |
| 726 | */ |
Tejun Heo | 5024ae2 | 2014-05-07 21:31:17 -0400 | [diff] [blame] | 727 | struct css_set init_css_set = { |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 728 | .refcount = REFCOUNT_INIT(1), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 729 | .dom_cset = &init_css_set, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 730 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 731 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 732 | .dying_tasks = LIST_HEAD_INIT(init_css_set.dying_tasks), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 733 | .task_iters = LIST_HEAD_INIT(init_css_set.task_iters), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 734 | .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 735 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 736 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 737 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
Tejun Heo | 3868314 | 2017-09-25 13:50:20 -0700 | [diff] [blame] | 738 | |
| 739 | /* |
| 740 | * The following field is re-initialized when this cset gets linked |
| 741 | * in cgroup_init(). However, let's initialize the field |
| 742 | * statically too so that the default cgroup can be accessed safely |
| 743 | * early during boot. |
| 744 | */ |
| 745 | .dfl_cgrp = &cgrp_dfl_root.cgrp, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 746 | }; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 747 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 748 | static int css_set_count = 1; /* 1 for init_css_set */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 749 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 750 | static bool css_set_threaded(struct css_set *cset) |
| 751 | { |
| 752 | return cset->dom_cset != cset; |
| 753 | } |
| 754 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 755 | /** |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 756 | * css_set_populated - does a css_set contain any tasks? |
| 757 | * @cset: target css_set |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 758 | * |
| 759 | * css_set_populated() should be the same as !!cset->nr_tasks at steady |
| 760 | * state. However, css_set_populated() can be called while a task is being |
| 761 | * added to or removed from the linked list before the nr_tasks is |
| 762 | * properly updated. Hence, we can't just look at ->nr_tasks here. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 763 | */ |
| 764 | static bool css_set_populated(struct css_set *cset) |
| 765 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 766 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 767 | |
| 768 | return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks); |
| 769 | } |
| 770 | |
| 771 | /** |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 772 | * cgroup_update_populated - update the populated count of a cgroup |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 773 | * @cgrp: the target cgroup |
| 774 | * @populated: inc or dec populated count |
| 775 | * |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 776 | * One of the css_sets associated with @cgrp is either getting its first |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 777 | * task or losing the last. Update @cgrp->nr_populated_* accordingly. The |
| 778 | * count is propagated towards root so that a given cgroup's |
| 779 | * nr_populated_children is zero iff none of its descendants contain any |
| 780 | * tasks. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 781 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 782 | * @cgrp's interface file "cgroup.populated" is zero if both |
| 783 | * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and |
| 784 | * 1 otherwise. When the sum changes from or to zero, userland is notified |
| 785 | * that the content of the interface file has changed. This can be used to |
| 786 | * detect when @cgrp and its descendants become populated or empty. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 787 | */ |
| 788 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) |
| 789 | { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 790 | struct cgroup *child = NULL; |
| 791 | int adj = populated ? 1 : -1; |
| 792 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 793 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 794 | |
| 795 | do { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 796 | bool was_populated = cgroup_is_populated(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 797 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 798 | if (!child) { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 799 | cgrp->nr_populated_csets += adj; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 800 | } else { |
| 801 | if (cgroup_is_threaded(child)) |
| 802 | cgrp->nr_populated_threaded_children += adj; |
| 803 | else |
| 804 | cgrp->nr_populated_domain_children += adj; |
| 805 | } |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 806 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 807 | if (was_populated == cgroup_is_populated(cgrp)) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 808 | break; |
| 809 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 810 | cgroup1_check_for_release(cgrp); |
Roman Gushchin | 4c476d8 | 2019-04-19 10:03:08 -0700 | [diff] [blame] | 811 | TRACE_CGROUP_PATH(notify_populated, cgrp, |
| 812 | cgroup_is_populated(cgrp)); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 813 | cgroup_file_notify(&cgrp->events_file); |
| 814 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 815 | child = cgrp; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 816 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 817 | } while (cgrp); |
| 818 | } |
| 819 | |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 820 | /** |
| 821 | * css_set_update_populated - update populated state of a css_set |
| 822 | * @cset: target css_set |
| 823 | * @populated: whether @cset is populated or depopulated |
| 824 | * |
| 825 | * @cset is either getting the first task or losing the last. Update the |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 826 | * populated counters of all associated cgroups accordingly. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 827 | */ |
| 828 | static void css_set_update_populated(struct css_set *cset, bool populated) |
| 829 | { |
| 830 | struct cgrp_cset_link *link; |
| 831 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 832 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 833 | |
| 834 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) |
| 835 | cgroup_update_populated(link->cgrp, populated); |
| 836 | } |
| 837 | |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 838 | /* |
| 839 | * @task is leaving, advance task iterators which are pointing to it so |
| 840 | * that they can resume at the next position. Advancing an iterator might |
| 841 | * remove it from the list, use safe walk. See css_task_iter_skip() for |
| 842 | * details. |
| 843 | */ |
| 844 | static void css_set_skip_task_iters(struct css_set *cset, |
| 845 | struct task_struct *task) |
| 846 | { |
| 847 | struct css_task_iter *it, *pos; |
| 848 | |
| 849 | list_for_each_entry_safe(it, pos, &cset->task_iters, iters_node) |
| 850 | css_task_iter_skip(it, task); |
| 851 | } |
| 852 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 853 | /** |
| 854 | * css_set_move_task - move a task from one css_set to another |
| 855 | * @task: task being moved |
| 856 | * @from_cset: css_set @task currently belongs to (may be NULL) |
| 857 | * @to_cset: new css_set @task is being moved to (may be NULL) |
| 858 | * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks |
| 859 | * |
| 860 | * Move @task from @from_cset to @to_cset. If @task didn't belong to any |
| 861 | * css_set, @from_cset can be NULL. If @task is being disassociated |
| 862 | * instead of moved, @to_cset can be NULL. |
| 863 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 864 | * This function automatically handles populated counter updates and |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 865 | * css_task_iter adjustments but the caller is responsible for managing |
| 866 | * @from_cset and @to_cset's reference counts. |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 867 | */ |
| 868 | static void css_set_move_task(struct task_struct *task, |
| 869 | struct css_set *from_cset, struct css_set *to_cset, |
| 870 | bool use_mg_tasks) |
| 871 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 872 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 873 | |
Tejun Heo | 20b454a | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 874 | if (to_cset && !css_set_populated(to_cset)) |
| 875 | css_set_update_populated(to_cset, true); |
| 876 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 877 | if (from_cset) { |
| 878 | WARN_ON_ONCE(list_empty(&task->cg_list)); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 879 | |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 880 | css_set_skip_task_iters(from_cset, task); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 881 | list_del_init(&task->cg_list); |
| 882 | if (!css_set_populated(from_cset)) |
| 883 | css_set_update_populated(from_cset, false); |
| 884 | } else { |
| 885 | WARN_ON_ONCE(!list_empty(&task->cg_list)); |
| 886 | } |
| 887 | |
| 888 | if (to_cset) { |
| 889 | /* |
| 890 | * We are synchronized through cgroup_threadgroup_rwsem |
| 891 | * against PF_EXITING setting such that we can't race |
Michal Koutný | e7c7b1d | 2019-10-04 12:57:39 +0200 | [diff] [blame] | 892 | * against cgroup_exit()/cgroup_free() dropping the css_set. |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 893 | */ |
| 894 | WARN_ON_ONCE(task->flags & PF_EXITING); |
| 895 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 896 | cgroup_move_task(task, to_cset); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 897 | list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks : |
| 898 | &to_cset->tasks); |
| 899 | } |
| 900 | } |
| 901 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 902 | /* |
| 903 | * hash table for cgroup groups. This improves the performance to find |
| 904 | * an existing css_set. This hash doesn't (currently) take into |
| 905 | * account cgroups in empty hierarchies. |
| 906 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 907 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 908 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 909 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 910 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 911 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 912 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 913 | struct cgroup_subsys *ss; |
| 914 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 915 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 916 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 917 | key += (unsigned long)css[i]; |
| 918 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 919 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 920 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Tejun Heo | dcfe149 | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 923 | void put_css_set_locked(struct css_set *cset) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 924 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 925 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 926 | struct cgroup_subsys *ss; |
| 927 | int ssid; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 928 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 929 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 930 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 931 | if (!refcount_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 932 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 933 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 934 | WARN_ON_ONCE(!list_empty(&cset->threaded_csets)); |
| 935 | |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 936 | /* This css_set is dead. Unlink it and release cgroup and css refs */ |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 937 | for_each_subsys(ss, ssid) { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 938 | list_del(&cset->e_cset_node[ssid]); |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 939 | css_put(cset->subsys[ssid]); |
| 940 | } |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 941 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 942 | css_set_count--; |
| 943 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 944 | list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 945 | list_del(&link->cset_link); |
| 946 | list_del(&link->cgrp_link); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 947 | if (cgroup_parent(link->cgrp)) |
| 948 | cgroup_put(link->cgrp); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 949 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 950 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 951 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 952 | if (css_set_threaded(cset)) { |
| 953 | list_del(&cset->threaded_csets_node); |
| 954 | put_css_set_locked(cset->dom_cset); |
| 955 | } |
| 956 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 957 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 960 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 961 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 962 | * @cset: candidate css_set being tested |
| 963 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 964 | * @new_cgrp: cgroup that's being entered by the task |
| 965 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 966 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 967 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 968 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 969 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 970 | static bool compare_css_sets(struct css_set *cset, |
| 971 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 972 | struct cgroup *new_cgrp, |
| 973 | struct cgroup_subsys_state *template[]) |
| 974 | { |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 975 | struct cgroup *new_dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 976 | struct list_head *l1, *l2; |
| 977 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 978 | /* |
| 979 | * On the default hierarchy, there can be csets which are |
| 980 | * associated with the same set of cgroups but different csses. |
| 981 | * Let's first ensure that csses match. |
| 982 | */ |
| 983 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 984 | return false; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 985 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 986 | |
| 987 | /* @cset's domain should match the default cgroup's */ |
| 988 | if (cgroup_on_dfl(new_cgrp)) |
| 989 | new_dfl_cgrp = new_cgrp; |
| 990 | else |
| 991 | new_dfl_cgrp = old_cset->dfl_cgrp; |
| 992 | |
| 993 | if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp) |
| 994 | return false; |
| 995 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 996 | /* |
| 997 | * Compare cgroup pointers in order to distinguish between |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 998 | * different cgroups in hierarchies. As different cgroups may |
| 999 | * share the same effective css, this comparison is always |
| 1000 | * necessary. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1001 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1002 | l1 = &cset->cgrp_links; |
| 1003 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1004 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1005 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1006 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1007 | |
| 1008 | l1 = l1->next; |
| 1009 | l2 = l2->next; |
| 1010 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1011 | if (l1 == &cset->cgrp_links) { |
| 1012 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1013 | break; |
| 1014 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1015 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1016 | } |
| 1017 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1018 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 1019 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 1020 | cgrp1 = link1->cgrp; |
| 1021 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1022 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1023 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1024 | |
| 1025 | /* |
| 1026 | * If this hierarchy is the hierarchy of the cgroup |
| 1027 | * that's changing, then we need to check that this |
| 1028 | * css_set points to the new cgroup; if it's any other |
| 1029 | * hierarchy, then this css_set should point to the |
| 1030 | * same cgroup as the old css_set. |
| 1031 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1032 | if (cgrp1->root == new_cgrp->root) { |
| 1033 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1034 | return false; |
| 1035 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1036 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1037 | return false; |
| 1038 | } |
| 1039 | } |
| 1040 | return true; |
| 1041 | } |
| 1042 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1043 | /** |
| 1044 | * find_existing_css_set - init css array and find the matching css_set |
| 1045 | * @old_cset: the css_set that we're using before the cgroup transition |
| 1046 | * @cgrp: the cgroup that we're moving into |
| 1047 | * @template: out param for the new set of csses, should be clear on entry |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1048 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1049 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 1050 | struct cgroup *cgrp, |
| 1051 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1052 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1053 | struct cgroup_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1054 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1055 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1056 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1057 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1058 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1059 | /* |
| 1060 | * Build the set of subsystem state objects that we want to see in the |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 1061 | * new css_set. While subsystems can change globally, the entries here |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1062 | * won't change, so no need for locking. |
| 1063 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1064 | for_each_subsys(ss, i) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1065 | if (root->subsys_mask & (1UL << i)) { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1066 | /* |
| 1067 | * @ss is in this hierarchy, so we want the |
| 1068 | * effective css from @cgrp. |
| 1069 | */ |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 1070 | template[i] = cgroup_e_css_by_mask(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1071 | } else { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1072 | /* |
| 1073 | * @ss is not in this hierarchy, so we don't want |
| 1074 | * to change the css. |
| 1075 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1076 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1080 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1081 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 1082 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1083 | continue; |
| 1084 | |
| 1085 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1086 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1087 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1088 | |
| 1089 | /* No existing cgroup group matched */ |
| 1090 | return NULL; |
| 1091 | } |
| 1092 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1093 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1094 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1095 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1096 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1097 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 1098 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1099 | kfree(link); |
| 1100 | } |
| 1101 | } |
| 1102 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1103 | /** |
| 1104 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 1105 | * @count: the number of links to allocate |
| 1106 | * @tmp_links: list_head the allocated links are put on |
| 1107 | * |
| 1108 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 1109 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1110 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1111 | static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links) |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1112 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1113 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1114 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1115 | |
| 1116 | INIT_LIST_HEAD(tmp_links); |
| 1117 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1118 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1119 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1120 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1121 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1122 | return -ENOMEM; |
| 1123 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1124 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1125 | } |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1129 | /** |
| 1130 | * link_css_set - a helper function to link a css_set to a cgroup |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1131 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1132 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1133 | * @cgrp: the destination cgroup |
| 1134 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1135 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 1136 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1137 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1138 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1139 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1140 | BUG_ON(list_empty(tmp_links)); |
Tejun Heo | 6803c00 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1141 | |
| 1142 | if (cgroup_on_dfl(cgrp)) |
| 1143 | cset->dfl_cgrp = cgrp; |
| 1144 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1145 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 1146 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1147 | link->cgrp = cgrp; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 1148 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1149 | /* |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1150 | * Always add links to the tail of the lists so that the lists are |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 1151 | * in chronological order. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1152 | */ |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1153 | list_move_tail(&link->cset_link, &cgrp->cset_links); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1154 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1155 | |
| 1156 | if (cgroup_parent(cgrp)) |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 1157 | cgroup_get_live(cgrp); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1160 | /** |
| 1161 | * find_css_set - return a new css_set with one cgroup updated |
| 1162 | * @old_cset: the baseline css_set |
| 1163 | * @cgrp: the cgroup to be updated |
| 1164 | * |
| 1165 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 1166 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1167 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1168 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 1169 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1170 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1171 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1172 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1173 | struct list_head tmp_links; |
| 1174 | struct cgrp_cset_link *link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1175 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1176 | unsigned long key; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1177 | int ssid; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1178 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1179 | lockdep_assert_held(&cgroup_mutex); |
| 1180 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1181 | /* First see if we already have a cgroup group that matches |
| 1182 | * the desired set */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1183 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1184 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 1185 | if (cset) |
| 1186 | get_css_set(cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1187 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1188 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1189 | if (cset) |
| 1190 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1191 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1192 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1193 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1194 | return NULL; |
| 1195 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1196 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1197 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1198 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1199 | return NULL; |
| 1200 | } |
| 1201 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 1202 | refcount_set(&cset->refcount, 1); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1203 | cset->dom_cset = cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1204 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1205 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 1206 | INIT_LIST_HEAD(&cset->dying_tasks); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 1207 | INIT_LIST_HEAD(&cset->task_iters); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1208 | INIT_LIST_HEAD(&cset->threaded_csets); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1209 | INIT_HLIST_NODE(&cset->hlist); |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 1210 | INIT_LIST_HEAD(&cset->cgrp_links); |
| 1211 | INIT_LIST_HEAD(&cset->mg_preload_node); |
| 1212 | INIT_LIST_HEAD(&cset->mg_node); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1213 | |
| 1214 | /* Copy the set of subsystem state objects generated in |
| 1215 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1216 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1217 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1218 | spin_lock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1219 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1220 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1221 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1222 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1223 | if (c->root == cgrp->root) |
| 1224 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1225 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1226 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1227 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1228 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1229 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1230 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1231 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1232 | /* Add @cset to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1233 | key = css_set_hash(cset->subsys); |
| 1234 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1235 | |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1236 | for_each_subsys(ss, ssid) { |
| 1237 | struct cgroup_subsys_state *css = cset->subsys[ssid]; |
| 1238 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1239 | list_add_tail(&cset->e_cset_node[ssid], |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1240 | &css->cgroup->e_csets[ssid]); |
| 1241 | css_get(css); |
| 1242 | } |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1243 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1244 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1245 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1246 | /* |
| 1247 | * If @cset should be threaded, look up the matching dom_cset and |
| 1248 | * link them up. We first fully initialize @cset then look for the |
| 1249 | * dom_cset. It's simpler this way and safe as @cset is guaranteed |
| 1250 | * to stay empty until we return. |
| 1251 | */ |
| 1252 | if (cgroup_is_threaded(cset->dfl_cgrp)) { |
| 1253 | struct css_set *dcset; |
| 1254 | |
| 1255 | dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp); |
| 1256 | if (!dcset) { |
| 1257 | put_css_set(cset); |
| 1258 | return NULL; |
| 1259 | } |
| 1260 | |
| 1261 | spin_lock_irq(&css_set_lock); |
| 1262 | cset->dom_cset = dcset; |
| 1263 | list_add_tail(&cset->threaded_csets_node, |
| 1264 | &dcset->threaded_csets); |
| 1265 | spin_unlock_irq(&css_set_lock); |
| 1266 | } |
| 1267 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1268 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1271 | struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1272 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1273 | struct cgroup *root_cgrp = kf_root->kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1274 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1275 | return root_cgrp->root; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1276 | } |
| 1277 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1278 | static int cgroup_init_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1279 | { |
| 1280 | int id; |
| 1281 | |
| 1282 | lockdep_assert_held(&cgroup_mutex); |
| 1283 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1284 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1285 | if (id < 0) |
| 1286 | return id; |
| 1287 | |
| 1288 | root->hierarchy_id = id; |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1292 | static void cgroup_exit_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1293 | { |
| 1294 | lockdep_assert_held(&cgroup_mutex); |
| 1295 | |
Johannes Weiner | 8c8a550 | 2016-06-17 12:23:59 -0400 | [diff] [blame] | 1296 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1297 | } |
| 1298 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1299 | void cgroup_free_root(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1300 | { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1301 | kfree(root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1302 | } |
| 1303 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1304 | static void cgroup_destroy_root(struct cgroup_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1305 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1306 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1307 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1308 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 1309 | trace_cgroup_destroy_root(root); |
| 1310 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1311 | cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1312 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1313 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1314 | BUG_ON(!list_empty(&cgrp->self.children)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1315 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1316 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1317 | WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1318 | |
| 1319 | /* |
| 1320 | * Release all the links from cset_links to this hierarchy's |
| 1321 | * root cgroup |
| 1322 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1323 | spin_lock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1324 | |
| 1325 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1326 | list_del(&link->cset_link); |
| 1327 | list_del(&link->cgrp_link); |
| 1328 | kfree(link); |
| 1329 | } |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1330 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1331 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1332 | |
| 1333 | if (!list_empty(&root->root_list)) { |
| 1334 | list_del(&root->root_list); |
| 1335 | cgroup_root_count--; |
| 1336 | } |
| 1337 | |
| 1338 | cgroup_exit_root_id(root); |
| 1339 | |
| 1340 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1341 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 1342 | cgroup_rstat_exit(cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1343 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1344 | cgroup_free_root(root); |
| 1345 | } |
| 1346 | |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1347 | /* |
| 1348 | * look up cgroup associated with current task's cgroup namespace on the |
| 1349 | * specified hierarchy |
| 1350 | */ |
| 1351 | static struct cgroup * |
| 1352 | current_cgns_cgroup_from_root(struct cgroup_root *root) |
| 1353 | { |
| 1354 | struct cgroup *res = NULL; |
| 1355 | struct css_set *cset; |
| 1356 | |
| 1357 | lockdep_assert_held(&css_set_lock); |
| 1358 | |
| 1359 | rcu_read_lock(); |
| 1360 | |
| 1361 | cset = current->nsproxy->cgroup_ns->root_cset; |
| 1362 | if (cset == &init_css_set) { |
| 1363 | res = &root->cgrp; |
Miaohe Lin | 61e867f | 2019-09-29 16:06:58 +0800 | [diff] [blame] | 1364 | } else if (root == &cgrp_dfl_root) { |
| 1365 | res = cset->dfl_cgrp; |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1366 | } else { |
| 1367 | struct cgrp_cset_link *link; |
| 1368 | |
| 1369 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
| 1370 | struct cgroup *c = link->cgrp; |
| 1371 | |
| 1372 | if (c->root == root) { |
| 1373 | res = c; |
| 1374 | break; |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | rcu_read_unlock(); |
| 1379 | |
| 1380 | BUG_ON(!res); |
| 1381 | return res; |
| 1382 | } |
| 1383 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1384 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 1385 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1386 | struct cgroup_root *root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1387 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1388 | struct cgroup *res = NULL; |
| 1389 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1390 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1391 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1392 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1393 | if (cset == &init_css_set) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1394 | res = &root->cgrp; |
Tejun Heo | 13d82fb | 2017-08-02 15:39:38 -0700 | [diff] [blame] | 1395 | } else if (root == &cgrp_dfl_root) { |
| 1396 | res = cset->dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1397 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1398 | struct cgrp_cset_link *link; |
| 1399 | |
| 1400 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1401 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1402 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1403 | if (c->root == root) { |
| 1404 | res = c; |
| 1405 | break; |
| 1406 | } |
| 1407 | } |
| 1408 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1409 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1410 | BUG_ON(!res); |
| 1411 | return res; |
| 1412 | } |
| 1413 | |
| 1414 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1415 | * Return the cgroup for "task" from the given hierarchy. Must be |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1416 | * called with cgroup_mutex and css_set_lock held. |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1417 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1418 | struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 1419 | struct cgroup_root *root) |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1420 | { |
| 1421 | /* |
Michal Koutný | e7c7b1d | 2019-10-04 12:57:39 +0200 | [diff] [blame] | 1422 | * No need to lock the task - since we hold css_set_lock the |
| 1423 | * task can't change groups. |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1424 | */ |
| 1425 | return cset_cgroup_from_root(task_css_set(task), root); |
| 1426 | } |
| 1427 | |
| 1428 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1429 | * A task must hold cgroup_mutex to modify cgroups. |
| 1430 | * |
| 1431 | * Any task can increment and decrement the count field without lock. |
| 1432 | * So in general, code holding cgroup_mutex can't rely on the count |
| 1433 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1434 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1435 | * means that no tasks are currently attached, therefore there is no |
| 1436 | * way a task attached to that cgroup can fork (the other way to |
| 1437 | * increment the count). So code holding cgroup_mutex can safely |
| 1438 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 1439 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 1440 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 1441 | * needs that mutex. |
| 1442 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1443 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 1444 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 1445 | * tasks in the system use _some_ cgroup, and since there is always at |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1446 | * least one task in the system (init, pid == 1), therefore, root cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1447 | * always has either children cgroups and/or using tasks. So we don't |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1448 | * need a special hack to ensure that root cgroup cannot be deleted. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1449 | * |
| 1450 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1451 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1452 | */ |
| 1453 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1454 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 1455 | |
Jens Axboe | cf89298 | 2019-06-10 03:35:41 -0600 | [diff] [blame] | 1456 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 1457 | char *buf) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1458 | { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 1459 | struct cgroup_subsys *ss = cft->ss; |
| 1460 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1461 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
Tejun Heo | c1bbd93 | 2018-11-13 12:06:41 -0800 | [diff] [blame] | 1462 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
| 1463 | const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : ""; |
| 1464 | |
| 1465 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s", |
| 1466 | dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name, |
Jens Axboe | cf89298 | 2019-06-10 03:35:41 -0600 | [diff] [blame] | 1467 | cft->name); |
Tejun Heo | c1bbd93 | 2018-11-13 12:06:41 -0800 | [diff] [blame] | 1468 | } else { |
Jens Axboe | cf89298 | 2019-06-10 03:35:41 -0600 | [diff] [blame] | 1469 | strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
Tejun Heo | c1bbd93 | 2018-11-13 12:06:41 -0800 | [diff] [blame] | 1470 | } |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1471 | return buf; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1474 | /** |
| 1475 | * cgroup_file_mode - deduce file mode of a control file |
| 1476 | * @cft: the control file in question |
| 1477 | * |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1478 | * S_IRUGO for read, S_IWUSR for write. |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1479 | */ |
| 1480 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1481 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1482 | umode_t mode = 0; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1483 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1484 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 1485 | mode |= S_IRUGO; |
| 1486 | |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1487 | if (cft->write_u64 || cft->write_s64 || cft->write) { |
| 1488 | if (cft->flags & CFTYPE_WORLD_WRITABLE) |
| 1489 | mode |= S_IWUGO; |
| 1490 | else |
| 1491 | mode |= S_IWUSR; |
| 1492 | } |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1493 | |
| 1494 | return mode; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1495 | } |
| 1496 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1497 | /** |
Tejun Heo | 8699b77 | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1498 | * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1499 | * @subtree_control: the new subtree_control mask to consider |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1500 | * @this_ss_mask: available subsystems |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1501 | * |
| 1502 | * On the default hierarchy, a subsystem may request other subsystems to be |
| 1503 | * enabled together through its ->depends_on mask. In such cases, more |
| 1504 | * subsystems than specified in "cgroup.subtree_control" may be enabled. |
| 1505 | * |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1506 | * This function calculates which subsystems need to be enabled if |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1507 | * @subtree_control is to be applied while restricted to @this_ss_mask. |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1508 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1509 | static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask) |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 1510 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1511 | u16 cur_ss_mask = subtree_control; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1512 | struct cgroup_subsys *ss; |
| 1513 | int ssid; |
| 1514 | |
| 1515 | lockdep_assert_held(&cgroup_mutex); |
| 1516 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1517 | cur_ss_mask |= cgrp_dfl_implicit_ss_mask; |
| 1518 | |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1519 | while (true) { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1520 | u16 new_ss_mask = cur_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1521 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1522 | do_each_subsys_mask(ss, ssid, cur_ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 1523 | new_ss_mask |= ss->depends_on; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1524 | } while_each_subsys_mask(); |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1525 | |
| 1526 | /* |
| 1527 | * Mask out subsystems which aren't available. This can |
| 1528 | * happen only if some depended-upon subsystems were bound |
| 1529 | * to non-default hierarchies. |
| 1530 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1531 | new_ss_mask &= this_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1532 | |
| 1533 | if (new_ss_mask == cur_ss_mask) |
| 1534 | break; |
| 1535 | cur_ss_mask = new_ss_mask; |
| 1536 | } |
| 1537 | |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1538 | return cur_ss_mask; |
| 1539 | } |
| 1540 | |
| 1541 | /** |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1542 | * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods |
| 1543 | * @kn: the kernfs_node being serviced |
| 1544 | * |
| 1545 | * This helper undoes cgroup_kn_lock_live() and should be invoked before |
| 1546 | * the method finishes if locking succeeded. Note that once this function |
| 1547 | * returns the cgroup returned by cgroup_kn_lock_live() may become |
| 1548 | * inaccessible any time. If the caller intends to continue to access the |
| 1549 | * cgroup, it should pin it before invoking this function. |
| 1550 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1551 | void cgroup_kn_unlock(struct kernfs_node *kn) |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1552 | { |
| 1553 | struct cgroup *cgrp; |
| 1554 | |
| 1555 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1556 | cgrp = kn->priv; |
| 1557 | else |
| 1558 | cgrp = kn->parent->priv; |
| 1559 | |
| 1560 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1561 | |
| 1562 | kernfs_unbreak_active_protection(kn); |
| 1563 | cgroup_put(cgrp); |
| 1564 | } |
| 1565 | |
| 1566 | /** |
| 1567 | * cgroup_kn_lock_live - locking helper for cgroup kernfs methods |
| 1568 | * @kn: the kernfs_node being serviced |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1569 | * @drain_offline: perform offline draining on the cgroup |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1570 | * |
| 1571 | * This helper is to be used by a cgroup kernfs method currently servicing |
| 1572 | * @kn. It breaks the active protection, performs cgroup locking and |
| 1573 | * verifies that the associated cgroup is alive. Returns the cgroup if |
| 1574 | * alive; otherwise, %NULL. A successful return should be undone by a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1575 | * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the |
| 1576 | * cgroup is drained of offlining csses before return. |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1577 | * |
| 1578 | * Any cgroup kernfs method implementation which requires locking the |
| 1579 | * associated cgroup should use this helper. It avoids nesting cgroup |
| 1580 | * locking under kernfs active protection and allows all kernfs operations |
| 1581 | * including self-removal. |
| 1582 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1583 | struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline) |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1584 | { |
| 1585 | struct cgroup *cgrp; |
| 1586 | |
| 1587 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1588 | cgrp = kn->priv; |
| 1589 | else |
| 1590 | cgrp = kn->parent->priv; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1591 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1592 | /* |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1593 | * We're gonna grab cgroup_mutex which nests outside kernfs |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1594 | * active_ref. cgroup liveliness check alone provides enough |
| 1595 | * protection against removal. Ensure @cgrp stays accessible and |
| 1596 | * break the active_ref protection. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1597 | */ |
Li Zefan | aa32362 | 2014-09-04 14:43:38 +0800 | [diff] [blame] | 1598 | if (!cgroup_tryget(cgrp)) |
| 1599 | return NULL; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1600 | kernfs_break_active_protection(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1601 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1602 | if (drain_offline) |
| 1603 | cgroup_lock_and_drain_offline(cgrp); |
| 1604 | else |
| 1605 | mutex_lock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1606 | |
| 1607 | if (!cgroup_is_dead(cgrp)) |
| 1608 | return cgrp; |
| 1609 | |
| 1610 | cgroup_kn_unlock(kn); |
| 1611 | return NULL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 1614 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1615 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1616 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1617 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1618 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1619 | |
| 1620 | if (cft->file_offset) { |
| 1621 | struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss); |
| 1622 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 1623 | |
| 1624 | spin_lock_irq(&cgroup_file_kn_lock); |
| 1625 | cfile->kn = NULL; |
| 1626 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1627 | |
| 1628 | del_timer_sync(&cfile->notify_timer); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1629 | } |
| 1630 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1631 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1634 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1635 | * css_clear_dir - remove subsys files in a cgroup directory |
| 1636 | * @css: taget css |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1637 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1638 | static void css_clear_dir(struct cgroup_subsys_state *css) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1639 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1640 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1641 | struct cftype *cfts; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1642 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1643 | if (!(css->flags & CSS_VISIBLE)) |
| 1644 | return; |
| 1645 | |
| 1646 | css->flags &= ~CSS_VISIBLE; |
| 1647 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1648 | if (!css->ss) { |
| 1649 | if (cgroup_on_dfl(cgrp)) |
| 1650 | cfts = cgroup_base_files; |
| 1651 | else |
| 1652 | cfts = cgroup1_base_files; |
| 1653 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1654 | cgroup_addrm_files(css, cgrp, cfts, false); |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1655 | } else { |
| 1656 | list_for_each_entry(cfts, &css->ss->cfts, node) |
| 1657 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1658 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1661 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1662 | * css_populate_dir - create subsys files in a cgroup directory |
| 1663 | * @css: target css |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1664 | * |
| 1665 | * On failure, no file is added. |
| 1666 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1667 | static int css_populate_dir(struct cgroup_subsys_state *css) |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1668 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1669 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1670 | struct cftype *cfts, *failed_cfts; |
| 1671 | int ret; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1672 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1673 | if ((css->flags & CSS_VISIBLE) || !cgrp->kn) |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1674 | return 0; |
| 1675 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1676 | if (!css->ss) { |
| 1677 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1678 | cfts = cgroup_base_files; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1679 | else |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1680 | cfts = cgroup1_base_files; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1681 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1682 | ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true); |
| 1683 | if (ret < 0) |
| 1684 | return ret; |
| 1685 | } else { |
| 1686 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1687 | ret = cgroup_addrm_files(css, cgrp, cfts, true); |
| 1688 | if (ret < 0) { |
| 1689 | failed_cfts = cfts; |
| 1690 | goto err; |
| 1691 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1692 | } |
| 1693 | } |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1694 | |
| 1695 | css->flags |= CSS_VISIBLE; |
| 1696 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1697 | return 0; |
| 1698 | err: |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1699 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1700 | if (cfts == failed_cfts) |
| 1701 | break; |
| 1702 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1703 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1704 | return ret; |
| 1705 | } |
| 1706 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1707 | int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1708 | { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1709 | struct cgroup *dcgrp = &dst_root->cgrp; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1710 | struct cgroup_subsys *ss; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1711 | int ssid, i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1712 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1713 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1714 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1715 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1716 | /* |
| 1717 | * If @ss has non-root csses attached to it, can't move. |
| 1718 | * If @ss is an implicit controller, it is exempt from this |
| 1719 | * rule and can be stolen. |
| 1720 | */ |
| 1721 | if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) && |
| 1722 | !ss->implicit_on_dfl) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1723 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1724 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1725 | /* can't move between two non-dummy roots either */ |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1726 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1727 | return -EBUSY; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1728 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1729 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1730 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1731 | struct cgroup_root *src_root = ss->root; |
| 1732 | struct cgroup *scgrp = &src_root->cgrp; |
| 1733 | struct cgroup_subsys_state *css = cgroup_css(scgrp, ss); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1734 | struct css_set *cset; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1735 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1736 | WARN_ON(!css || cgroup_css(dcgrp, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1737 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1738 | /* disable from the source */ |
| 1739 | src_root->subsys_mask &= ~(1 << ssid); |
| 1740 | WARN_ON(cgroup_apply_control(scgrp)); |
| 1741 | cgroup_finalize_control(scgrp, 0); |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1742 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1743 | /* rebind */ |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1744 | RCU_INIT_POINTER(scgrp->subsys[ssid], NULL); |
| 1745 | rcu_assign_pointer(dcgrp->subsys[ssid], css); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1746 | ss->root = dst_root; |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1747 | css->cgroup = dcgrp; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1748 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1749 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1750 | hash_for_each(css_set_table, i, cset, hlist) |
| 1751 | list_move_tail(&cset->e_cset_node[ss->id], |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1752 | &dcgrp->e_csets[ss->id]); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1753 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1754 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 1755 | if (ss->css_rstat_flush) { |
| 1756 | list_del_rcu(&css->rstat_css_node); |
| 1757 | list_add_rcu(&css->rstat_css_node, |
| 1758 | &dcgrp->rstat_css_list); |
| 1759 | } |
| 1760 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1761 | /* default hierarchy doesn't enable controllers by default */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1762 | dst_root->subsys_mask |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1763 | if (dst_root == &cgrp_dfl_root) { |
| 1764 | static_branch_enable(cgroup_subsys_on_dfl_key[ssid]); |
| 1765 | } else { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1766 | dcgrp->subtree_control |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1767 | static_branch_disable(cgroup_subsys_on_dfl_key[ssid]); |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 1768 | } |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1769 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1770 | ret = cgroup_apply_control(dcgrp); |
| 1771 | if (ret) |
| 1772 | pr_warn("partial failure to rebind %s controller (err=%d)\n", |
| 1773 | ss->name, ret); |
| 1774 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1775 | if (ss->bind) |
| 1776 | ss->bind(css); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1777 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1778 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1779 | kernfs_activate(dcgrp->kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1780 | return 0; |
| 1781 | } |
| 1782 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1783 | int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, |
| 1784 | struct kernfs_root *kf_root) |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1785 | { |
Felipe Balbi | 09be4c8 | 2016-05-12 12:34:38 +0300 | [diff] [blame] | 1786 | int len = 0; |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1787 | char *buf = NULL; |
| 1788 | struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root); |
| 1789 | struct cgroup *ns_cgroup; |
| 1790 | |
| 1791 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 1792 | if (!buf) |
| 1793 | return -ENOMEM; |
| 1794 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1795 | spin_lock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1796 | ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot); |
| 1797 | len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1798 | spin_unlock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1799 | |
| 1800 | if (len >= PATH_MAX) |
| 1801 | len = -ERANGE; |
| 1802 | else if (len > 0) { |
| 1803 | seq_escape(sf, buf, " \t\n\\"); |
| 1804 | len = 0; |
| 1805 | } |
| 1806 | kfree(buf); |
| 1807 | return len; |
| 1808 | } |
| 1809 | |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1810 | enum cgroup2_param { |
| 1811 | Opt_nsdelegate, |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1812 | Opt_memory_localevents, |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1813 | Opt_memory_recursiveprot, |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1814 | nr__cgroup2_params |
| 1815 | }; |
| 1816 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 1817 | static const struct fs_parameter_spec cgroup2_fs_parameters[] = { |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1818 | fsparam_flag("nsdelegate", Opt_nsdelegate), |
| 1819 | fsparam_flag("memory_localevents", Opt_memory_localevents), |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1820 | fsparam_flag("memory_recursiveprot", Opt_memory_recursiveprot), |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1821 | {} |
| 1822 | }; |
| 1823 | |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1824 | static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param) |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1825 | { |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1826 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
| 1827 | struct fs_parse_result result; |
| 1828 | int opt; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1829 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 1830 | opt = fs_parse(fc, cgroup2_fs_parameters, param, &result); |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1831 | if (opt < 0) |
| 1832 | return opt; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1833 | |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1834 | switch (opt) { |
| 1835 | case Opt_nsdelegate: |
| 1836 | ctx->flags |= CGRP_ROOT_NS_DELEGATE; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1837 | return 0; |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1838 | case Opt_memory_localevents: |
| 1839 | ctx->flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS; |
| 1840 | return 0; |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1841 | case Opt_memory_recursiveprot: |
| 1842 | ctx->flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; |
| 1843 | return 0; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1844 | } |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1845 | return -EINVAL; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | static void apply_cgroup_root_flags(unsigned int root_flags) |
| 1849 | { |
| 1850 | if (current->nsproxy->cgroup_ns == &init_cgroup_ns) { |
| 1851 | if (root_flags & CGRP_ROOT_NS_DELEGATE) |
| 1852 | cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE; |
| 1853 | else |
| 1854 | cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE; |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1855 | |
| 1856 | if (root_flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) |
| 1857 | cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS; |
| 1858 | else |
| 1859 | cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_LOCAL_EVENTS; |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1860 | |
| 1861 | if (root_flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT) |
| 1862 | cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; |
| 1863 | else |
| 1864 | cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root) |
| 1869 | { |
| 1870 | if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) |
| 1871 | seq_puts(seq, ",nsdelegate"); |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1872 | if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) |
| 1873 | seq_puts(seq, ",memory_localevents"); |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1874 | if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT) |
| 1875 | seq_puts(seq, ",memory_recursiveprot"); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1876 | return 0; |
| 1877 | } |
| 1878 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 1879 | static int cgroup_reconfigure(struct fs_context *fc) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1880 | { |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 1881 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1882 | |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 1883 | apply_cgroup_root_flags(ctx->flags); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1884 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1887 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1888 | { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1889 | struct cgroup_subsys *ss; |
| 1890 | int ssid; |
| 1891 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1892 | INIT_LIST_HEAD(&cgrp->self.sibling); |
| 1893 | INIT_LIST_HEAD(&cgrp->self.children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1894 | INIT_LIST_HEAD(&cgrp->cset_links); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1895 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1896 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 1897 | cgrp->self.cgroup = cgrp; |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 1898 | cgrp->self.flags |= CSS_ONLINE; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1899 | cgrp->dom_cgrp = cgrp; |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 1900 | cgrp->max_descendants = INT_MAX; |
| 1901 | cgrp->max_depth = INT_MAX; |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 1902 | INIT_LIST_HEAD(&cgrp->rstat_css_list); |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1903 | prev_cputime_init(&cgrp->prev_cputime); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1904 | |
| 1905 | for_each_subsys(ss, ssid) |
| 1906 | INIT_LIST_HEAD(&cgrp->e_csets[ssid]); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1907 | |
| 1908 | init_waitqueue_head(&cgrp->offline_waitq); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1909 | INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1910 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1911 | |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 1912 | void init_cgroup_root(struct cgroup_fs_context *ctx) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1913 | { |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 1914 | struct cgroup_root *root = ctx->root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1915 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1916 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1917 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1918 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1919 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1920 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1921 | |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 1922 | root->flags = ctx->flags; |
| 1923 | if (ctx->release_agent) |
| 1924 | strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX); |
| 1925 | if (ctx->name) |
| 1926 | strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN); |
| 1927 | if (ctx->cpuset_clone_children) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1928 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 1931 | int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1932 | { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1933 | LIST_HEAD(tmp_links); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1934 | struct cgroup *root_cgrp = &root->cgrp; |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1935 | struct kernfs_syscall_ops *kf_sops; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1936 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1937 | int i, ret; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1938 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1939 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1940 | |
Zefan Li | 9732adc | 2017-04-19 10:15:59 +0800 | [diff] [blame] | 1941 | ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 1942 | 0, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1943 | if (ret) |
| 1944 | goto out; |
| 1945 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1946 | /* |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1947 | * We're accessing css_set_count without locking css_set_lock here, |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1948 | * but that's OK - it can only be increased by someone holding |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1949 | * cgroup_lock, and that's us. Later rebinding may disable |
| 1950 | * controllers on the default hierarchy and thus create new csets, |
| 1951 | * which can't be more than the existing ones. Allocate 2x. |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1952 | */ |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1953 | ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1954 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1955 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1956 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1957 | ret = cgroup_init_root_id(root); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1958 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1959 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1960 | |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1961 | kf_sops = root == &cgrp_dfl_root ? |
| 1962 | &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops; |
| 1963 | |
| 1964 | root->kf_root = kernfs_create_root(kf_sops, |
Shaohua Li | aa81882 | 2017-07-12 11:49:51 -0700 | [diff] [blame] | 1965 | KERNFS_ROOT_CREATE_DEACTIVATED | |
Daniel Xu | 38aca30 | 2020-03-12 13:03:17 -0700 | [diff] [blame] | 1966 | KERNFS_ROOT_SUPPORT_EXPORTOP | |
| 1967 | KERNFS_ROOT_SUPPORT_USER_XATTR, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1968 | root_cgrp); |
| 1969 | if (IS_ERR(root->kf_root)) { |
| 1970 | ret = PTR_ERR(root->kf_root); |
| 1971 | goto exit_root_id; |
| 1972 | } |
| 1973 | root_cgrp->kn = root->kf_root->kn; |
Tejun Heo | d749534 | 2019-11-14 14:46:51 -0800 | [diff] [blame] | 1974 | WARN_ON_ONCE(cgroup_ino(root_cgrp) != 1); |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1975 | root_cgrp->ancestor_ids[0] = cgroup_id(root_cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1976 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1977 | ret = css_populate_dir(&root_cgrp->self); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1978 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1979 | goto destroy_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1980 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 1981 | ret = cgroup_rstat_init(root_cgrp); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1982 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1983 | goto destroy_root; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1984 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 1985 | ret = rebind_subsystems(root, ss_mask); |
| 1986 | if (ret) |
| 1987 | goto exit_stats; |
| 1988 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1989 | ret = cgroup_bpf_inherit(root_cgrp); |
| 1990 | WARN_ON_ONCE(ret); |
| 1991 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 1992 | trace_cgroup_setup_root(root); |
| 1993 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1994 | /* |
| 1995 | * There must be no failure case after here, since rebinding takes |
| 1996 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1997 | * the failure exit path. |
| 1998 | */ |
| 1999 | list_add(&root->root_list, &cgroup_roots); |
| 2000 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2001 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2002 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2003 | * Link the root cgroup in this hierarchy into all the css_set |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2004 | * objects. |
| 2005 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2006 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 2007 | hash_for_each(css_set_table, i, cset, hlist) { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2008 | link_css_set(&tmp_links, cset, root_cgrp); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 2009 | if (css_set_populated(cset)) |
| 2010 | cgroup_update_populated(root_cgrp, true); |
| 2011 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2012 | spin_unlock_irq(&css_set_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2013 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 2014 | BUG_ON(!list_empty(&root_cgrp->self.children)); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2015 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2016 | |
| 2017 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2018 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2019 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 2020 | exit_stats: |
| 2021 | cgroup_rstat_exit(root_cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2022 | destroy_root: |
| 2023 | kernfs_destroy_root(root->kf_root); |
| 2024 | root->kf_root = NULL; |
| 2025 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2026 | cgroup_exit_root_id(root); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2027 | cancel_ref: |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 2028 | percpu_ref_exit(&root_cgrp->self.refcnt); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2029 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2030 | free_cgrp_cset_links(&tmp_links); |
| 2031 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2034 | int cgroup_do_get_tree(struct fs_context *fc) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2035 | { |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2036 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2037 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 2038 | |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2039 | ctx->kfc.root = ctx->root->kf_root; |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2040 | if (fc->fs_type == &cgroup2_fs_type) |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2041 | ctx->kfc.magic = CGROUP2_SUPER_MAGIC; |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2042 | else |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2043 | ctx->kfc.magic = CGROUP_SUPER_MAGIC; |
| 2044 | ret = kernfs_get_tree(fc); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2045 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 2046 | /* |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2047 | * In non-init cgroup namespace, instead of root cgroup's dentry, |
| 2048 | * we return the dentry corresponding to the cgroupns->root_cgrp. |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2049 | */ |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2050 | if (!ret && ctx->ns != &init_cgroup_ns) { |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2051 | struct dentry *nsdentry; |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2052 | struct super_block *sb = fc->root->d_sb; |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2053 | struct cgroup *cgrp; |
| 2054 | |
| 2055 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2056 | spin_lock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2057 | |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2058 | cgrp = cset_cgroup_from_root(ctx->ns->root_cset, ctx->root); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2059 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2060 | spin_unlock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2061 | mutex_unlock(&cgroup_mutex); |
| 2062 | |
Al Viro | 399504e | 2019-01-06 11:41:29 -0500 | [diff] [blame] | 2063 | nsdentry = kernfs_node_dentry(cgrp->kn, sb); |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2064 | dput(fc->root); |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2065 | if (IS_ERR(nsdentry)) { |
Al Viro | 399504e | 2019-01-06 11:41:29 -0500 | [diff] [blame] | 2066 | deactivate_locked_super(sb); |
Al Viro | 630faf8 | 2019-11-10 11:53:27 -0500 | [diff] [blame] | 2067 | ret = PTR_ERR(nsdentry); |
| 2068 | nsdentry = NULL; |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2069 | } |
Al Viro | 630faf8 | 2019-11-10 11:53:27 -0500 | [diff] [blame] | 2070 | fc->root = nsdentry; |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2071 | } |
| 2072 | |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2073 | if (!ctx->kfc.new_sb_created) |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2074 | cgroup_put(&ctx->root->cgrp); |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2075 | |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2076 | return ret; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2077 | } |
| 2078 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2079 | /* |
| 2080 | * Destroy a cgroup filesystem context. |
| 2081 | */ |
| 2082 | static void cgroup_fs_context_free(struct fs_context *fc) |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2083 | { |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2084 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
| 2085 | |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 2086 | kfree(ctx->name); |
| 2087 | kfree(ctx->release_agent); |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2088 | put_cgroup_ns(ctx->ns); |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2089 | kernfs_free_fs_context(fc); |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2090 | kfree(ctx); |
| 2091 | } |
| 2092 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2093 | static int cgroup_get_tree(struct fs_context *fc) |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2094 | { |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2095 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2096 | int ret; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2097 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2098 | cgrp_dfl_visible = true; |
| 2099 | cgroup_get_live(&cgrp_dfl_root.cgrp); |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 2100 | ctx->root = &cgrp_dfl_root; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2101 | |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2102 | ret = cgroup_do_get_tree(fc); |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2103 | if (!ret) |
| 2104 | apply_cgroup_root_flags(ctx->flags); |
| 2105 | return ret; |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2106 | } |
| 2107 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2108 | static const struct fs_context_operations cgroup_fs_context_ops = { |
| 2109 | .free = cgroup_fs_context_free, |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 2110 | .parse_param = cgroup2_parse_param, |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2111 | .get_tree = cgroup_get_tree, |
| 2112 | .reconfigure = cgroup_reconfigure, |
| 2113 | }; |
| 2114 | |
| 2115 | static const struct fs_context_operations cgroup1_fs_context_ops = { |
| 2116 | .free = cgroup_fs_context_free, |
Al Viro | 8d2451f | 2019-01-17 00:15:11 -0500 | [diff] [blame] | 2117 | .parse_param = cgroup1_parse_param, |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2118 | .get_tree = cgroup1_get_tree, |
| 2119 | .reconfigure = cgroup1_reconfigure, |
| 2120 | }; |
| 2121 | |
| 2122 | /* |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2123 | * Initialise the cgroup filesystem creation/reconfiguration context. Notably, |
| 2124 | * we select the namespace we're going to use. |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2125 | */ |
| 2126 | static int cgroup_init_fs_context(struct fs_context *fc) |
| 2127 | { |
| 2128 | struct cgroup_fs_context *ctx; |
| 2129 | |
| 2130 | ctx = kzalloc(sizeof(struct cgroup_fs_context), GFP_KERNEL); |
| 2131 | if (!ctx) |
| 2132 | return -ENOMEM; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2133 | |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2134 | ctx->ns = current->nsproxy->cgroup_ns; |
| 2135 | get_cgroup_ns(ctx->ns); |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2136 | fc->fs_private = &ctx->kfc; |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2137 | if (fc->fs_type == &cgroup2_fs_type) |
| 2138 | fc->ops = &cgroup_fs_context_ops; |
| 2139 | else |
| 2140 | fc->ops = &cgroup1_fs_context_ops; |
Al Viro | f7a9945 | 2019-05-12 12:42:58 -0400 | [diff] [blame] | 2141 | put_user_ns(fc->user_ns); |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2142 | fc->user_ns = get_user_ns(ctx->ns->user_ns); |
| 2143 | fc->global = true; |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2144 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2147 | static void cgroup_kill_sb(struct super_block *sb) |
| 2148 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2149 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2150 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2151 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2152 | /* |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 2153 | * If @root doesn't have any children, start killing it. |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2154 | * This prevents new mounts by disabling percpu_ref_tryget_live(). |
| 2155 | * cgroup_mount() may wait for @root's release. |
Li Zefan | 1f779fb | 2014-06-04 16:48:15 +0800 | [diff] [blame] | 2156 | * |
| 2157 | * And don't kill the default root. |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2158 | */ |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 2159 | if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root && |
| 2160 | !percpu_ref_is_dying(&root->cgrp.self.refcnt)) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2161 | percpu_ref_kill(&root->cgrp.self.refcnt); |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 2162 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2163 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2166 | struct file_system_type cgroup_fs_type = { |
Al Viro | 8d2451f | 2019-01-17 00:15:11 -0500 | [diff] [blame] | 2167 | .name = "cgroup", |
| 2168 | .init_fs_context = cgroup_init_fs_context, |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 2169 | .parameters = cgroup1_fs_parameters, |
Al Viro | 8d2451f | 2019-01-17 00:15:11 -0500 | [diff] [blame] | 2170 | .kill_sb = cgroup_kill_sb, |
| 2171 | .fs_flags = FS_USERNS_MOUNT, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2172 | }; |
| 2173 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2174 | static struct file_system_type cgroup2_fs_type = { |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 2175 | .name = "cgroup2", |
| 2176 | .init_fs_context = cgroup_init_fs_context, |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 2177 | .parameters = cgroup2_fs_parameters, |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 2178 | .kill_sb = cgroup_kill_sb, |
| 2179 | .fs_flags = FS_USERNS_MOUNT, |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2180 | }; |
| 2181 | |
Al Viro | d5f68d3 | 2019-05-13 12:33:22 -0400 | [diff] [blame] | 2182 | #ifdef CONFIG_CPUSETS |
| 2183 | static const struct fs_context_operations cpuset_fs_context_ops = { |
| 2184 | .get_tree = cgroup1_get_tree, |
| 2185 | .free = cgroup_fs_context_free, |
| 2186 | }; |
| 2187 | |
| 2188 | /* |
| 2189 | * This is ugly, but preserves the userspace API for existing cpuset |
| 2190 | * users. If someone tries to mount the "cpuset" filesystem, we |
| 2191 | * silently switch it to mount "cgroup" instead |
| 2192 | */ |
| 2193 | static int cpuset_init_fs_context(struct fs_context *fc) |
| 2194 | { |
| 2195 | char *agent = kstrdup("/sbin/cpuset_release_agent", GFP_USER); |
| 2196 | struct cgroup_fs_context *ctx; |
| 2197 | int err; |
| 2198 | |
| 2199 | err = cgroup_init_fs_context(fc); |
| 2200 | if (err) { |
| 2201 | kfree(agent); |
| 2202 | return err; |
| 2203 | } |
| 2204 | |
| 2205 | fc->ops = &cpuset_fs_context_ops; |
| 2206 | |
| 2207 | ctx = cgroup_fc2context(fc); |
| 2208 | ctx->subsys_mask = 1 << cpuset_cgrp_id; |
| 2209 | ctx->flags |= CGRP_ROOT_NOPREFIX; |
| 2210 | ctx->release_agent = agent; |
| 2211 | |
| 2212 | get_filesystem(&cgroup_fs_type); |
| 2213 | put_filesystem(fc->fs_type); |
| 2214 | fc->fs_type = &cgroup_fs_type; |
| 2215 | |
| 2216 | return 0; |
| 2217 | } |
| 2218 | |
| 2219 | static struct file_system_type cpuset_fs_type = { |
| 2220 | .name = "cpuset", |
| 2221 | .init_fs_context = cpuset_init_fs_context, |
| 2222 | .fs_flags = FS_USERNS_MOUNT, |
| 2223 | }; |
| 2224 | #endif |
| 2225 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2226 | int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2227 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2228 | { |
| 2229 | struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2230 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2231 | return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2232 | } |
| 2233 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2234 | int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2235 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2236 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2237 | int ret; |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2238 | |
| 2239 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2240 | spin_lock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2241 | |
| 2242 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns); |
| 2243 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2244 | spin_unlock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2245 | mutex_unlock(&cgroup_mutex); |
| 2246 | |
| 2247 | return ret; |
| 2248 | } |
| 2249 | EXPORT_SYMBOL_GPL(cgroup_path_ns); |
| 2250 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2251 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2252 | * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2253 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2254 | * @buf: the buffer to write the path into |
| 2255 | * @buflen: the length of the buffer |
| 2256 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2257 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 2258 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 2259 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 2260 | * cgroup controller callbacks. |
| 2261 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2262 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2263 | */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2264 | int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2265 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2266 | struct cgroup_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2267 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2268 | int hierarchy_id = 1; |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2269 | int ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2270 | |
| 2271 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2272 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2273 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2274 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 2275 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2276 | if (root) { |
| 2277 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2278 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2279 | } else { |
| 2280 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2281 | ret = strlcpy(buf, "/", buflen); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2284 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2285 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2286 | return ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2287 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2288 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2289 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2290 | /** |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2291 | * cgroup_migrate_add_task - add a migration target task to a migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2292 | * @task: target task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2293 | * @mgctx: target migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2294 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2295 | * Add @task, which is a migration target, to @mgctx->tset. This function |
| 2296 | * becomes noop if @task doesn't need to be migrated. @task's css_set |
| 2297 | * should have been added as a migration source and @task->cg_list will be |
| 2298 | * moved from the css_set's tasks list to mg_tasks one. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2299 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2300 | static void cgroup_migrate_add_task(struct task_struct *task, |
| 2301 | struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2302 | { |
| 2303 | struct css_set *cset; |
| 2304 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2305 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2306 | |
| 2307 | /* @task either already exited or can't exit until the end */ |
| 2308 | if (task->flags & PF_EXITING) |
| 2309 | return; |
| 2310 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 2311 | /* cgroup_threadgroup_rwsem protects racing against forks */ |
| 2312 | WARN_ON_ONCE(list_empty(&task->cg_list)); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2313 | |
| 2314 | cset = task_css_set(task); |
| 2315 | if (!cset->mg_src_cgrp) |
| 2316 | return; |
| 2317 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2318 | mgctx->tset.nr_tasks++; |
| 2319 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2320 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 2321 | if (list_empty(&cset->mg_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2322 | list_add_tail(&cset->mg_node, |
| 2323 | &mgctx->tset.src_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2324 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
Tejun Heo | d8ebf51 | 2017-01-15 19:03:40 -0500 | [diff] [blame] | 2325 | list_add_tail(&cset->mg_dst_cset->mg_node, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2326 | &mgctx->tset.dst_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2327 | } |
| 2328 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2329 | /** |
| 2330 | * cgroup_taskset_first - reset taskset and return the first task |
| 2331 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2332 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2333 | * |
| 2334 | * @tset iteration is initialized and the first task is returned. |
| 2335 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2336 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, |
| 2337 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2338 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2339 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 2340 | tset->cur_task = NULL; |
| 2341 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2342 | return cgroup_taskset_next(tset, dst_cssp); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2343 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2344 | |
| 2345 | /** |
| 2346 | * cgroup_taskset_next - iterate to the next task in taskset |
| 2347 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2348 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2349 | * |
| 2350 | * Return the next task in @tset. Iteration must have been initialized |
| 2351 | * with cgroup_taskset_first(). |
| 2352 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2353 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, |
| 2354 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2355 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2356 | struct css_set *cset = tset->cur_cset; |
| 2357 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2358 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2359 | while (&cset->mg_node != tset->csets) { |
| 2360 | if (!task) |
| 2361 | task = list_first_entry(&cset->mg_tasks, |
| 2362 | struct task_struct, cg_list); |
| 2363 | else |
| 2364 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2365 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2366 | if (&task->cg_list != &cset->mg_tasks) { |
| 2367 | tset->cur_cset = cset; |
| 2368 | tset->cur_task = task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2369 | |
| 2370 | /* |
| 2371 | * This function may be called both before and |
| 2372 | * after cgroup_taskset_migrate(). The two cases |
| 2373 | * can be distinguished by looking at whether @cset |
| 2374 | * has its ->mg_dst_cset set. |
| 2375 | */ |
| 2376 | if (cset->mg_dst_cset) |
| 2377 | *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid]; |
| 2378 | else |
| 2379 | *dst_cssp = cset->subsys[tset->ssid]; |
| 2380 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2381 | return task; |
| 2382 | } |
| 2383 | |
| 2384 | cset = list_next_entry(cset, mg_node); |
| 2385 | task = NULL; |
| 2386 | } |
| 2387 | |
| 2388 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2389 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2390 | |
| 2391 | /** |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2392 | * cgroup_taskset_migrate - migrate a taskset |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2393 | * @mgctx: migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2394 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2395 | * Migrate tasks in @mgctx as setup by migration preparation functions. |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2396 | * This function fails iff one of the ->can_attach callbacks fails and |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2397 | * guarantees that either all or none of the tasks in @mgctx are migrated. |
| 2398 | * @mgctx is consumed regardless of success. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2399 | */ |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2400 | static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2401 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2402 | struct cgroup_taskset *tset = &mgctx->tset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2403 | struct cgroup_subsys *ss; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2404 | struct task_struct *task, *tmp_task; |
| 2405 | struct css_set *cset, *tmp_cset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2406 | int ssid, failed_ssid, ret; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2407 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2408 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2409 | if (tset->nr_tasks) { |
| 2410 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2411 | if (ss->can_attach) { |
| 2412 | tset->ssid = ssid; |
| 2413 | ret = ss->can_attach(tset); |
| 2414 | if (ret) { |
| 2415 | failed_ssid = ssid; |
| 2416 | goto out_cancel_attach; |
| 2417 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2418 | } |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2419 | } while_each_subsys_mask(); |
| 2420 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2421 | |
| 2422 | /* |
| 2423 | * Now that we're guaranteed success, proceed to move all tasks to |
| 2424 | * the new cgroup. There are no failure cases after here, so this |
| 2425 | * is the commit point. |
| 2426 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2427 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2428 | list_for_each_entry(cset, &tset->src_csets, mg_node) { |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2429 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) { |
| 2430 | struct css_set *from_cset = task_css_set(task); |
| 2431 | struct css_set *to_cset = cset->mg_dst_cset; |
| 2432 | |
| 2433 | get_css_set(to_cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2434 | to_cset->nr_tasks++; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2435 | css_set_move_task(task, from_cset, to_cset, true); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2436 | from_cset->nr_tasks--; |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 2437 | /* |
| 2438 | * If the source or destination cgroup is frozen, |
| 2439 | * the task might require to change its state. |
| 2440 | */ |
| 2441 | cgroup_freezer_migrate_task(task, from_cset->dfl_cgrp, |
| 2442 | to_cset->dfl_cgrp); |
| 2443 | put_css_set_locked(from_cset); |
| 2444 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2445 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2446 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2447 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2448 | |
| 2449 | /* |
| 2450 | * Migration is committed, all target tasks are now on dst_csets. |
| 2451 | * Nothing is sensitive to fork() after this point. Notify |
| 2452 | * controllers that migration is complete. |
| 2453 | */ |
| 2454 | tset->csets = &tset->dst_csets; |
| 2455 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2456 | if (tset->nr_tasks) { |
| 2457 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2458 | if (ss->attach) { |
| 2459 | tset->ssid = ssid; |
| 2460 | ss->attach(tset); |
| 2461 | } |
| 2462 | } while_each_subsys_mask(); |
| 2463 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2464 | |
| 2465 | ret = 0; |
| 2466 | goto out_release_tset; |
| 2467 | |
| 2468 | out_cancel_attach: |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2469 | if (tset->nr_tasks) { |
| 2470 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2471 | if (ssid == failed_ssid) |
| 2472 | break; |
| 2473 | if (ss->cancel_attach) { |
| 2474 | tset->ssid = ssid; |
| 2475 | ss->cancel_attach(tset); |
| 2476 | } |
| 2477 | } while_each_subsys_mask(); |
| 2478 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2479 | out_release_tset: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2480 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2481 | list_splice_init(&tset->dst_csets, &tset->src_csets); |
| 2482 | list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { |
| 2483 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
| 2484 | list_del_init(&cset->mg_node); |
| 2485 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2486 | spin_unlock_irq(&css_set_lock); |
Waiman Long | c4fa6c4 | 2017-09-21 09:54:13 -0400 | [diff] [blame] | 2487 | |
| 2488 | /* |
| 2489 | * Re-initialize the cgroup_taskset structure in case it is reused |
| 2490 | * again in another cgroup_migrate_add_task()/cgroup_migrate_execute() |
| 2491 | * iteration. |
| 2492 | */ |
| 2493 | tset->nr_tasks = 0; |
| 2494 | tset->csets = &tset->src_csets; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2495 | return ret; |
| 2496 | } |
| 2497 | |
| 2498 | /** |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2499 | * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2500 | * @dst_cgrp: destination cgroup to test |
| 2501 | * |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2502 | * On the default hierarchy, except for the mixable, (possible) thread root |
| 2503 | * and threaded cgroups, subtree_control must be zero for migration |
| 2504 | * destination cgroups with tasks so that child cgroups don't compete |
| 2505 | * against tasks. |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2506 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2507 | int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp) |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2508 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2509 | /* v1 doesn't have any restriction */ |
| 2510 | if (!cgroup_on_dfl(dst_cgrp)) |
| 2511 | return 0; |
| 2512 | |
| 2513 | /* verify @dst_cgrp can host resources */ |
| 2514 | if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp)) |
| 2515 | return -EOPNOTSUPP; |
| 2516 | |
| 2517 | /* mixables don't care */ |
| 2518 | if (cgroup_is_mixable(dst_cgrp)) |
| 2519 | return 0; |
| 2520 | |
| 2521 | /* |
| 2522 | * If @dst_cgrp is already or can become a thread root or is |
| 2523 | * threaded, it doesn't matter. |
| 2524 | */ |
| 2525 | if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp)) |
| 2526 | return 0; |
| 2527 | |
| 2528 | /* apply no-internal-process constraint */ |
| 2529 | if (dst_cgrp->subtree_control) |
| 2530 | return -EBUSY; |
| 2531 | |
| 2532 | return 0; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2536 | * cgroup_migrate_finish - cleanup after attach |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2537 | * @mgctx: migration context |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2538 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2539 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 2540 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2541 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2542 | void cgroup_migrate_finish(struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2543 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2544 | LIST_HEAD(preloaded); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2545 | struct css_set *cset, *tmp_cset; |
| 2546 | |
| 2547 | lockdep_assert_held(&cgroup_mutex); |
| 2548 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2549 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2550 | |
| 2551 | list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded); |
| 2552 | list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded); |
| 2553 | |
| 2554 | list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2555 | cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2556 | cset->mg_dst_cgrp = NULL; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2557 | cset->mg_dst_cset = NULL; |
| 2558 | list_del_init(&cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2559 | put_css_set_locked(cset); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2560 | } |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2561 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2562 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | /** |
| 2566 | * cgroup_migrate_add_src - add a migration source css_set |
| 2567 | * @src_cset: the source css_set to add |
| 2568 | * @dst_cgrp: the destination cgroup |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2569 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2570 | * |
| 2571 | * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2572 | * @src_cset and add it to @mgctx->src_csets, which should later be cleaned |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2573 | * up by cgroup_migrate_finish(). |
| 2574 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2575 | * This function may be called without holding cgroup_threadgroup_rwsem |
| 2576 | * even if the target is a process. Threads may be created and destroyed |
| 2577 | * but as long as cgroup_mutex is not dropped, no new css_set can be put |
| 2578 | * into play and the preloaded css_sets are guaranteed to cover all |
| 2579 | * migrations. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2580 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2581 | void cgroup_migrate_add_src(struct css_set *src_cset, |
| 2582 | struct cgroup *dst_cgrp, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2583 | struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2584 | { |
| 2585 | struct cgroup *src_cgrp; |
| 2586 | |
| 2587 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2588 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2589 | |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 2590 | /* |
| 2591 | * If ->dead, @src_set is associated with one or more dead cgroups |
| 2592 | * and doesn't contain any migratable tasks. Ignore it early so |
| 2593 | * that the rest of migration path doesn't get confused by it. |
| 2594 | */ |
| 2595 | if (src_cset->dead) |
| 2596 | return; |
| 2597 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2598 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 2599 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2600 | if (!list_empty(&src_cset->mg_preload_node)) |
| 2601 | return; |
| 2602 | |
| 2603 | WARN_ON(src_cset->mg_src_cgrp); |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2604 | WARN_ON(src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2605 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 2606 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 2607 | |
| 2608 | src_cset->mg_src_cgrp = src_cgrp; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2609 | src_cset->mg_dst_cgrp = dst_cgrp; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2610 | get_css_set(src_cset); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2611 | list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | /** |
| 2615 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2616 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2617 | * |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2618 | * Tasks are about to be moved and all the source css_sets have been |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2619 | * preloaded to @mgctx->preloaded_src_csets. This function looks up and |
| 2620 | * pins all destination css_sets, links each to its source, and append them |
| 2621 | * to @mgctx->preloaded_dst_csets. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2622 | * |
| 2623 | * This function must be called after cgroup_migrate_add_src() has been |
| 2624 | * called on each migration source css_set. After migration is performed |
| 2625 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2626 | * @mgctx. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2627 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2628 | int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2629 | { |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2630 | struct css_set *src_cset, *tmp_cset; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2631 | |
| 2632 | lockdep_assert_held(&cgroup_mutex); |
| 2633 | |
| 2634 | /* look up the dst cset for each src cset and link it to src */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2635 | list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets, |
| 2636 | mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2637 | struct css_set *dst_cset; |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2638 | struct cgroup_subsys *ss; |
| 2639 | int ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2640 | |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2641 | dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2642 | if (!dst_cset) |
Shakeel Butt | d6e486e | 2019-04-03 16:03:54 -0700 | [diff] [blame] | 2643 | return -ENOMEM; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2644 | |
| 2645 | WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2646 | |
| 2647 | /* |
| 2648 | * If src cset equals dst, it's noop. Drop the src. |
| 2649 | * cgroup_migrate() will skip the cset too. Note that we |
| 2650 | * can't handle src == dst as some nodes are used by both. |
| 2651 | */ |
| 2652 | if (src_cset == dst_cset) { |
| 2653 | src_cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2654 | src_cset->mg_dst_cgrp = NULL; |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2655 | list_del_init(&src_cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2656 | put_css_set(src_cset); |
| 2657 | put_css_set(dst_cset); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2658 | continue; |
| 2659 | } |
| 2660 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2661 | src_cset->mg_dst_cset = dst_cset; |
| 2662 | |
| 2663 | if (list_empty(&dst_cset->mg_preload_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2664 | list_add_tail(&dst_cset->mg_preload_node, |
| 2665 | &mgctx->preloaded_dst_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2666 | else |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2667 | put_css_set(dst_cset); |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2668 | |
| 2669 | for_each_subsys(ss, ssid) |
| 2670 | if (src_cset->subsys[ssid] != dst_cset->subsys[ssid]) |
| 2671 | mgctx->ss_mask |= 1 << ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2672 | } |
| 2673 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2674 | return 0; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | /** |
| 2678 | * cgroup_migrate - migrate a process or task to a cgroup |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2679 | * @leader: the leader of the process or the task to migrate |
| 2680 | * @threadgroup: whether @leader points to the whole process or a single task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2681 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2682 | * |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2683 | * Migrate a process or task denoted by @leader. If migrating a process, |
| 2684 | * the caller must be holding cgroup_threadgroup_rwsem. The caller is also |
| 2685 | * responsible for invoking cgroup_migrate_add_src() and |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2686 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 2687 | * function and following up with cgroup_migrate_finish(). |
| 2688 | * |
| 2689 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 2690 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 2691 | * failure, when migrating multiple targets, the success or failure can be |
| 2692 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 2693 | * actually starting migrating. |
| 2694 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2695 | int cgroup_migrate(struct task_struct *leader, bool threadgroup, |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2696 | struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2697 | { |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2698 | struct task_struct *task; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2699 | |
| 2700 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2701 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2702 | * already PF_EXITING could be freed from underneath us unless we |
| 2703 | * take an rcu_read_lock. |
| 2704 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2705 | spin_lock_irq(&css_set_lock); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2706 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2707 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2708 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2709 | cgroup_migrate_add_task(task, mgctx); |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2710 | if (!threadgroup) |
| 2711 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2712 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2713 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2714 | spin_unlock_irq(&css_set_lock); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2715 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2716 | return cgroup_migrate_execute(mgctx); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2719 | /** |
| 2720 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2721 | * @dst_cgrp: the cgroup to attach to |
| 2722 | * @leader: the task or the leader of the threadgroup to be attached |
| 2723 | * @threadgroup: attach the whole threadgroup? |
| 2724 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2725 | * Call holding cgroup_mutex and cgroup_threadgroup_rwsem. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2726 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2727 | int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, |
| 2728 | bool threadgroup) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2729 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2730 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2731 | struct task_struct *task; |
Christian Brauner | 6df970e | 2020-02-05 14:26:18 +0100 | [diff] [blame] | 2732 | int ret = 0; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2733 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2734 | /* look up all src csets */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2735 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2736 | rcu_read_lock(); |
| 2737 | task = leader; |
| 2738 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2739 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2740 | if (!threadgroup) |
| 2741 | break; |
| 2742 | } while_each_thread(leader, task); |
| 2743 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2744 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2745 | |
| 2746 | /* prepare dst csets and commit */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2747 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2748 | if (!ret) |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2749 | ret = cgroup_migrate(leader, threadgroup, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2750 | |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2751 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2752 | |
| 2753 | if (!ret) |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 2754 | TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2755 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2756 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2759 | struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, |
| 2760 | bool *locked) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2761 | __acquires(&cgroup_threadgroup_rwsem) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2762 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2763 | struct task_struct *tsk; |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2764 | pid_t pid; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2765 | |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2766 | if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2767 | return ERR_PTR(-EINVAL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2768 | |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2769 | /* |
| 2770 | * If we migrate a single thread, we don't care about threadgroup |
| 2771 | * stability. If the thread is `current`, it won't exit(2) under our |
| 2772 | * hands or change PID through exec(2). We exclude |
| 2773 | * cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write |
| 2774 | * callers by cgroup_mutex. |
| 2775 | * Therefore, we can skip the global lock. |
| 2776 | */ |
| 2777 | lockdep_assert_held(&cgroup_mutex); |
| 2778 | if (pid || threadgroup) { |
| 2779 | percpu_down_write(&cgroup_threadgroup_rwsem); |
| 2780 | *locked = true; |
| 2781 | } else { |
| 2782 | *locked = false; |
| 2783 | } |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2784 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2785 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2786 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2787 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2788 | if (!tsk) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2789 | tsk = ERR_PTR(-ESRCH); |
| 2790 | goto out_unlock_threadgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2791 | } |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2792 | } else { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2793 | tsk = current; |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2794 | } |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2795 | |
| 2796 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2797 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2798 | |
| 2799 | /* |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2800 | * kthreads may acquire PF_NO_SETAFFINITY during initialization. |
| 2801 | * If userland migrates such a kthread to a non-root cgroup, it can |
| 2802 | * become trapped in a cpuset, or RT kthread may be born in a |
| 2803 | * cgroup with no rt_runtime allocated. Just say no. |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2804 | */ |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2805 | if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2806 | tsk = ERR_PTR(-EINVAL); |
| 2807 | goto out_unlock_threadgroup; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2808 | } |
| 2809 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2810 | get_task_struct(tsk); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2811 | goto out_unlock_rcu; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2812 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2813 | out_unlock_threadgroup: |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2814 | if (*locked) { |
| 2815 | percpu_up_write(&cgroup_threadgroup_rwsem); |
| 2816 | *locked = false; |
| 2817 | } |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2818 | out_unlock_rcu: |
| 2819 | rcu_read_unlock(); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2820 | return tsk; |
| 2821 | } |
| 2822 | |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2823 | void cgroup_procs_write_finish(struct task_struct *task, bool locked) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2824 | __releases(&cgroup_threadgroup_rwsem) |
| 2825 | { |
| 2826 | struct cgroup_subsys *ss; |
| 2827 | int ssid; |
| 2828 | |
| 2829 | /* release reference from cgroup_procs_write_start() */ |
| 2830 | put_task_struct(task); |
| 2831 | |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2832 | if (locked) |
| 2833 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 5cf1cac | 2016-04-21 19:06:48 -0400 | [diff] [blame] | 2834 | for_each_subsys(ss, ssid) |
| 2835 | if (ss->post_attach) |
| 2836 | ss->post_attach(); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2837 | } |
| 2838 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 2839 | static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2840 | { |
| 2841 | struct cgroup_subsys *ss; |
| 2842 | bool printed = false; |
| 2843 | int ssid; |
| 2844 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2845 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 2846 | if (printed) |
| 2847 | seq_putc(seq, ' '); |
Markus Elfring | 85db002 | 2019-07-02 19:26:59 +0200 | [diff] [blame] | 2848 | seq_puts(seq, ss->name); |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 2849 | printed = true; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2850 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2851 | if (printed) |
| 2852 | seq_putc(seq, '\n'); |
| 2853 | } |
| 2854 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2855 | /* show controllers which are enabled from the parent */ |
| 2856 | static int cgroup_controllers_show(struct seq_file *seq, void *v) |
| 2857 | { |
| 2858 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2859 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 2860 | cgroup_print_ss_mask(seq, cgroup_control(cgrp)); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2861 | return 0; |
| 2862 | } |
| 2863 | |
| 2864 | /* show controllers which are enabled for a given cgroup's children */ |
| 2865 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) |
| 2866 | { |
| 2867 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2868 | |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 2869 | cgroup_print_ss_mask(seq, cgrp->subtree_control); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2870 | return 0; |
| 2871 | } |
| 2872 | |
| 2873 | /** |
| 2874 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy |
| 2875 | * @cgrp: root of the subtree to update csses for |
| 2876 | * |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2877 | * @cgrp's control masks have changed and its subtree's css associations |
| 2878 | * need to be updated accordingly. This function looks up all css_sets |
| 2879 | * which are attached to the subtree, creates the matching updated css_sets |
| 2880 | * and migrates the tasks to the new ones. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2881 | */ |
| 2882 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) |
| 2883 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2884 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2885 | struct cgroup_subsys_state *d_css; |
| 2886 | struct cgroup *dsct; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2887 | struct css_set *src_cset; |
| 2888 | int ret; |
| 2889 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2890 | lockdep_assert_held(&cgroup_mutex); |
| 2891 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2892 | percpu_down_write(&cgroup_threadgroup_rwsem); |
| 2893 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2894 | /* look up all csses currently attached to @cgrp's subtree */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2895 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2896 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2897 | struct cgrp_cset_link *link; |
| 2898 | |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2899 | list_for_each_entry(link, &dsct->cset_links, cset_link) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2900 | cgroup_migrate_add_src(link->cset, dsct, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2901 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2902 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2903 | |
| 2904 | /* NULL dst indicates self on default hierarchy */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2905 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2906 | if (ret) |
| 2907 | goto out_finish; |
| 2908 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2909 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2910 | list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) { |
Tejun Heo | 1026507 | 2015-09-11 15:00:22 -0400 | [diff] [blame] | 2911 | struct task_struct *task, *ntask; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2912 | |
Tejun Heo | 1026507 | 2015-09-11 15:00:22 -0400 | [diff] [blame] | 2913 | /* all tasks in src_csets need to be migrated */ |
| 2914 | list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2915 | cgroup_migrate_add_task(task, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2916 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2917 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2918 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2919 | ret = cgroup_migrate_execute(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2920 | out_finish: |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2921 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2922 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2923 | return ret; |
| 2924 | } |
| 2925 | |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2926 | /** |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2927 | * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2928 | * @cgrp: root of the target subtree |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2929 | * |
| 2930 | * Because css offlining is asynchronous, userland may try to re-enable a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2931 | * controller while the previous css is still around. This function grabs |
| 2932 | * cgroup_mutex and drains the previous css instances of @cgrp's subtree. |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2933 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2934 | void cgroup_lock_and_drain_offline(struct cgroup *cgrp) |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2935 | __acquires(&cgroup_mutex) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2936 | { |
| 2937 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2938 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2939 | struct cgroup_subsys *ss; |
| 2940 | int ssid; |
| 2941 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2942 | restart: |
| 2943 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2944 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2945 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2946 | for_each_subsys(ss, ssid) { |
| 2947 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 2948 | DEFINE_WAIT(wait); |
| 2949 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2950 | if (!css || !percpu_ref_is_dying(&css->refcnt)) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2951 | continue; |
| 2952 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 2953 | cgroup_get_live(dsct); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2954 | prepare_to_wait(&dsct->offline_waitq, &wait, |
| 2955 | TASK_UNINTERRUPTIBLE); |
| 2956 | |
| 2957 | mutex_unlock(&cgroup_mutex); |
| 2958 | schedule(); |
| 2959 | finish_wait(&dsct->offline_waitq, &wait); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2960 | |
| 2961 | cgroup_put(dsct); |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2962 | goto restart; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2963 | } |
| 2964 | } |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2965 | } |
| 2966 | |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2967 | /** |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 2968 | * cgroup_save_control - save control masks and dom_cgrp of a subtree |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2969 | * @cgrp: root of the target subtree |
| 2970 | * |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 2971 | * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the |
| 2972 | * respective old_ prefixed fields for @cgrp's subtree including @cgrp |
| 2973 | * itself. |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2974 | */ |
| 2975 | static void cgroup_save_control(struct cgroup *cgrp) |
| 2976 | { |
| 2977 | struct cgroup *dsct; |
| 2978 | struct cgroup_subsys_state *d_css; |
| 2979 | |
| 2980 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 2981 | dsct->old_subtree_control = dsct->subtree_control; |
| 2982 | dsct->old_subtree_ss_mask = dsct->subtree_ss_mask; |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 2983 | dsct->old_dom_cgrp = dsct->dom_cgrp; |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | /** |
| 2988 | * cgroup_propagate_control - refresh control masks of a subtree |
| 2989 | * @cgrp: root of the target subtree |
| 2990 | * |
| 2991 | * For @cgrp and its subtree, ensure ->subtree_ss_mask matches |
| 2992 | * ->subtree_control and propagate controller availability through the |
| 2993 | * subtree so that descendants don't have unavailable controllers enabled. |
| 2994 | */ |
| 2995 | static void cgroup_propagate_control(struct cgroup *cgrp) |
| 2996 | { |
| 2997 | struct cgroup *dsct; |
| 2998 | struct cgroup_subsys_state *d_css; |
| 2999 | |
| 3000 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 3001 | dsct->subtree_control &= cgroup_control(dsct); |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3002 | dsct->subtree_ss_mask = |
| 3003 | cgroup_calc_subtree_ss_mask(dsct->subtree_control, |
| 3004 | cgroup_ss_mask(dsct)); |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3005 | } |
| 3006 | } |
| 3007 | |
| 3008 | /** |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3009 | * cgroup_restore_control - restore control masks and dom_cgrp of a subtree |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3010 | * @cgrp: root of the target subtree |
| 3011 | * |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3012 | * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the |
| 3013 | * respective old_ prefixed fields for @cgrp's subtree including @cgrp |
| 3014 | * itself. |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3015 | */ |
| 3016 | static void cgroup_restore_control(struct cgroup *cgrp) |
| 3017 | { |
| 3018 | struct cgroup *dsct; |
| 3019 | struct cgroup_subsys_state *d_css; |
| 3020 | |
| 3021 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
| 3022 | dsct->subtree_control = dsct->old_subtree_control; |
| 3023 | dsct->subtree_ss_mask = dsct->old_subtree_ss_mask; |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3024 | dsct->dom_cgrp = dsct->old_dom_cgrp; |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3025 | } |
| 3026 | } |
| 3027 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 3028 | static bool css_visible(struct cgroup_subsys_state *css) |
| 3029 | { |
| 3030 | struct cgroup_subsys *ss = css->ss; |
| 3031 | struct cgroup *cgrp = css->cgroup; |
| 3032 | |
| 3033 | if (cgroup_control(cgrp) & (1 << ss->id)) |
| 3034 | return true; |
| 3035 | if (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) |
| 3036 | return false; |
| 3037 | return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl; |
| 3038 | } |
| 3039 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3040 | /** |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3041 | * cgroup_apply_control_enable - enable or show csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3042 | * @cgrp: root of the target subtree |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3043 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3044 | * Walk @cgrp's subtree and create new csses or make the existing ones |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3045 | * visible. A css is created invisible if it's being implicitly enabled |
| 3046 | * through dependency. An invisible css is made visible when the userland |
| 3047 | * explicitly enables it. |
| 3048 | * |
| 3049 | * Returns 0 on success, -errno on failure. On failure, csses which have |
| 3050 | * been processed already aren't cleaned up. The caller is responsible for |
Masahiro Yamada | 8a1115f | 2017-03-09 16:16:31 -0800 | [diff] [blame] | 3051 | * cleaning up with cgroup_apply_control_disable(). |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3052 | */ |
| 3053 | static int cgroup_apply_control_enable(struct cgroup *cgrp) |
| 3054 | { |
| 3055 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3056 | struct cgroup_subsys_state *d_css; |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3057 | struct cgroup_subsys *ss; |
| 3058 | int ssid, ret; |
| 3059 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3060 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3061 | for_each_subsys(ss, ssid) { |
| 3062 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3063 | |
| 3064 | if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) |
| 3065 | continue; |
| 3066 | |
| 3067 | if (!css) { |
| 3068 | css = css_create(dsct, ss); |
| 3069 | if (IS_ERR(css)) |
| 3070 | return PTR_ERR(css); |
| 3071 | } |
| 3072 | |
Michal Koutný | 3bc0bb3 | 2020-01-09 16:05:59 +0100 | [diff] [blame] | 3073 | WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); |
| 3074 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 3075 | if (css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3076 | ret = css_populate_dir(css); |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3077 | if (ret) |
| 3078 | return ret; |
| 3079 | } |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | return 0; |
| 3084 | } |
| 3085 | |
| 3086 | /** |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3087 | * cgroup_apply_control_disable - kill or hide csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3088 | * @cgrp: root of the target subtree |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3089 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3090 | * Walk @cgrp's subtree and kill and hide csses so that they match |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3091 | * cgroup_ss_mask() and cgroup_visible_mask(). |
| 3092 | * |
| 3093 | * A css is hidden when the userland requests it to be disabled while other |
| 3094 | * subsystems are still depending on it. The css must not actively control |
| 3095 | * resources and be in the vanilla state if it's made visible again later. |
| 3096 | * Controllers which may be depended upon should provide ->css_reset() for |
| 3097 | * this purpose. |
| 3098 | */ |
| 3099 | static void cgroup_apply_control_disable(struct cgroup *cgrp) |
| 3100 | { |
| 3101 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3102 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3103 | struct cgroup_subsys *ss; |
| 3104 | int ssid; |
| 3105 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3106 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3107 | for_each_subsys(ss, ssid) { |
| 3108 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3109 | |
| 3110 | if (!css) |
| 3111 | continue; |
| 3112 | |
Michal Koutný | 3bc0bb3 | 2020-01-09 16:05:59 +0100 | [diff] [blame] | 3113 | WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); |
| 3114 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3115 | if (css->parent && |
| 3116 | !(cgroup_ss_mask(dsct) & (1 << ss->id))) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3117 | kill_css(css); |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 3118 | } else if (!css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3119 | css_clear_dir(css); |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3120 | if (ss->css_reset) |
| 3121 | ss->css_reset(css); |
| 3122 | } |
| 3123 | } |
| 3124 | } |
| 3125 | } |
| 3126 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3127 | /** |
| 3128 | * cgroup_apply_control - apply control mask updates to the subtree |
| 3129 | * @cgrp: root of the target subtree |
| 3130 | * |
| 3131 | * subsystems can be enabled and disabled in a subtree using the following |
| 3132 | * steps. |
| 3133 | * |
| 3134 | * 1. Call cgroup_save_control() to stash the current state. |
| 3135 | * 2. Update ->subtree_control masks in the subtree as desired. |
| 3136 | * 3. Call cgroup_apply_control() to apply the changes. |
| 3137 | * 4. Optionally perform other related operations. |
| 3138 | * 5. Call cgroup_finalize_control() to finish up. |
| 3139 | * |
| 3140 | * This function implements step 3 and propagates the mask changes |
| 3141 | * throughout @cgrp's subtree, updates csses accordingly and perform |
| 3142 | * process migrations. |
| 3143 | */ |
| 3144 | static int cgroup_apply_control(struct cgroup *cgrp) |
| 3145 | { |
| 3146 | int ret; |
| 3147 | |
| 3148 | cgroup_propagate_control(cgrp); |
| 3149 | |
| 3150 | ret = cgroup_apply_control_enable(cgrp); |
| 3151 | if (ret) |
| 3152 | return ret; |
| 3153 | |
| 3154 | /* |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 3155 | * At this point, cgroup_e_css_by_mask() results reflect the new csses |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3156 | * making the following cgroup_update_dfl_csses() properly update |
| 3157 | * css associations of all tasks in the subtree. |
| 3158 | */ |
| 3159 | ret = cgroup_update_dfl_csses(cgrp); |
| 3160 | if (ret) |
| 3161 | return ret; |
| 3162 | |
| 3163 | return 0; |
| 3164 | } |
| 3165 | |
| 3166 | /** |
| 3167 | * cgroup_finalize_control - finalize control mask update |
| 3168 | * @cgrp: root of the target subtree |
| 3169 | * @ret: the result of the update |
| 3170 | * |
| 3171 | * Finalize control mask update. See cgroup_apply_control() for more info. |
| 3172 | */ |
| 3173 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret) |
| 3174 | { |
| 3175 | if (ret) { |
| 3176 | cgroup_restore_control(cgrp); |
| 3177 | cgroup_propagate_control(cgrp); |
| 3178 | } |
| 3179 | |
| 3180 | cgroup_apply_control_disable(cgrp); |
| 3181 | } |
| 3182 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3183 | static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable) |
| 3184 | { |
| 3185 | u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask; |
| 3186 | |
| 3187 | /* if nothing is getting enabled, nothing to worry about */ |
| 3188 | if (!enable) |
| 3189 | return 0; |
| 3190 | |
| 3191 | /* can @cgrp host any resources? */ |
| 3192 | if (!cgroup_is_valid_domain(cgrp->dom_cgrp)) |
| 3193 | return -EOPNOTSUPP; |
| 3194 | |
| 3195 | /* mixables don't care */ |
| 3196 | if (cgroup_is_mixable(cgrp)) |
| 3197 | return 0; |
| 3198 | |
| 3199 | if (domain_enable) { |
| 3200 | /* can't enable domain controllers inside a thread subtree */ |
| 3201 | if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3202 | return -EOPNOTSUPP; |
| 3203 | } else { |
| 3204 | /* |
| 3205 | * Threaded controllers can handle internal competitions |
| 3206 | * and are always allowed inside a (prospective) thread |
| 3207 | * subtree. |
| 3208 | */ |
| 3209 | if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
| 3213 | /* |
| 3214 | * Controllers can't be enabled for a cgroup with tasks to avoid |
| 3215 | * child cgroups competing against tasks. |
| 3216 | */ |
| 3217 | if (cgroup_has_tasks(cgrp)) |
| 3218 | return -EBUSY; |
| 3219 | |
| 3220 | return 0; |
| 3221 | } |
| 3222 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3223 | /* change the enabled child controllers for a cgroup in the default hierarchy */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3224 | static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, |
| 3225 | char *buf, size_t nbytes, |
| 3226 | loff_t off) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3227 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 3228 | u16 enable = 0, disable = 0; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3229 | struct cgroup *cgrp, *child; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3230 | struct cgroup_subsys *ss; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3231 | char *tok; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3232 | int ssid, ret; |
| 3233 | |
| 3234 | /* |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3235 | * Parse input - space separated list of subsystem names prefixed |
| 3236 | * with either + or -. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3237 | */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3238 | buf = strstrip(buf); |
| 3239 | while ((tok = strsep(&buf, " "))) { |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3240 | if (tok[0] == '\0') |
| 3241 | continue; |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 3242 | do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) { |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 3243 | if (!cgroup_ssid_enabled(ssid) || |
| 3244 | strcmp(tok + 1, ss->name)) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3245 | continue; |
| 3246 | |
| 3247 | if (*tok == '+') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3248 | enable |= 1 << ssid; |
| 3249 | disable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3250 | } else if (*tok == '-') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3251 | disable |= 1 << ssid; |
| 3252 | enable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3253 | } else { |
| 3254 | return -EINVAL; |
| 3255 | } |
| 3256 | break; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 3257 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3258 | if (ssid == CGROUP_SUBSYS_COUNT) |
| 3259 | return -EINVAL; |
| 3260 | } |
| 3261 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3262 | cgrp = cgroup_kn_lock_live(of->kn, true); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3263 | if (!cgrp) |
| 3264 | return -ENODEV; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3265 | |
| 3266 | for_each_subsys(ss, ssid) { |
| 3267 | if (enable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3268 | if (cgrp->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3269 | enable &= ~(1 << ssid); |
| 3270 | continue; |
| 3271 | } |
| 3272 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 3273 | if (!(cgroup_control(cgrp) & (1 << ssid))) { |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3274 | ret = -ENOENT; |
| 3275 | goto out_unlock; |
| 3276 | } |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3277 | } else if (disable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3278 | if (!(cgrp->subtree_control & (1 << ssid))) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3279 | disable &= ~(1 << ssid); |
| 3280 | continue; |
| 3281 | } |
| 3282 | |
| 3283 | /* a child has it enabled? */ |
| 3284 | cgroup_for_each_live_child(child, cgrp) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3285 | if (child->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3286 | ret = -EBUSY; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3287 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | if (!enable && !disable) { |
| 3294 | ret = 0; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3295 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3296 | } |
| 3297 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3298 | ret = cgroup_vet_subtree_control_enable(cgrp, enable); |
| 3299 | if (ret) |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 3300 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3301 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3302 | /* save and update control masks and prepare csses */ |
| 3303 | cgroup_save_control(cgrp); |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3304 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3305 | cgrp->subtree_control |= enable; |
| 3306 | cgrp->subtree_control &= ~disable; |
Tejun Heo | f63070d | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 3307 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3308 | ret = cgroup_apply_control(cgrp); |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3309 | cgroup_finalize_control(cgrp, ret); |
Tejun Heo | 3c74541 | 2017-07-23 08:14:15 -0400 | [diff] [blame] | 3310 | if (ret) |
| 3311 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3312 | |
| 3313 | kernfs_activate(cgrp->kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3314 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3315 | cgroup_kn_unlock(of->kn); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3316 | return ret ?: nbytes; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3317 | } |
| 3318 | |
Tejun Heo | c705a00 | 2017-07-25 13:20:18 -0400 | [diff] [blame] | 3319 | /** |
| 3320 | * cgroup_enable_threaded - make @cgrp threaded |
| 3321 | * @cgrp: the target cgroup |
| 3322 | * |
| 3323 | * Called when "threaded" is written to the cgroup.type interface file and |
| 3324 | * tries to make @cgrp threaded and join the parent's resource domain. |
| 3325 | * This function is never called on the root cgroup as cgroup.type doesn't |
| 3326 | * exist on it. |
| 3327 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3328 | static int cgroup_enable_threaded(struct cgroup *cgrp) |
| 3329 | { |
| 3330 | struct cgroup *parent = cgroup_parent(cgrp); |
| 3331 | struct cgroup *dom_cgrp = parent->dom_cgrp; |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3332 | struct cgroup *dsct; |
| 3333 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3334 | int ret; |
| 3335 | |
| 3336 | lockdep_assert_held(&cgroup_mutex); |
| 3337 | |
| 3338 | /* noop if already threaded */ |
| 3339 | if (cgroup_is_threaded(cgrp)) |
| 3340 | return 0; |
| 3341 | |
Tejun Heo | d1897c9 | 2018-02-21 11:39:22 -0800 | [diff] [blame] | 3342 | /* |
| 3343 | * If @cgroup is populated or has domain controllers enabled, it |
| 3344 | * can't be switched. While the below cgroup_can_be_thread_root() |
| 3345 | * test can catch the same conditions, that's only when @parent is |
| 3346 | * not mixable, so let's check it explicitly. |
| 3347 | */ |
| 3348 | if (cgroup_is_populated(cgrp) || |
| 3349 | cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 3350 | return -EOPNOTSUPP; |
| 3351 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3352 | /* we're joining the parent's domain, ensure its validity */ |
| 3353 | if (!cgroup_is_valid_domain(dom_cgrp) || |
| 3354 | !cgroup_can_be_thread_root(dom_cgrp)) |
| 3355 | return -EOPNOTSUPP; |
| 3356 | |
| 3357 | /* |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3358 | * The following shouldn't cause actual migrations and should |
| 3359 | * always succeed. |
| 3360 | */ |
| 3361 | cgroup_save_control(cgrp); |
| 3362 | |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3363 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) |
| 3364 | if (dsct == cgrp || cgroup_is_threaded(dsct)) |
| 3365 | dsct->dom_cgrp = dom_cgrp; |
| 3366 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3367 | ret = cgroup_apply_control(cgrp); |
| 3368 | if (!ret) |
| 3369 | parent->nr_threaded_children++; |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3370 | |
| 3371 | cgroup_finalize_control(cgrp, ret); |
| 3372 | return ret; |
| 3373 | } |
| 3374 | |
| 3375 | static int cgroup_type_show(struct seq_file *seq, void *v) |
| 3376 | { |
| 3377 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3378 | |
| 3379 | if (cgroup_is_threaded(cgrp)) |
| 3380 | seq_puts(seq, "threaded\n"); |
| 3381 | else if (!cgroup_is_valid_domain(cgrp)) |
| 3382 | seq_puts(seq, "domain invalid\n"); |
| 3383 | else if (cgroup_is_thread_root(cgrp)) |
| 3384 | seq_puts(seq, "domain threaded\n"); |
| 3385 | else |
| 3386 | seq_puts(seq, "domain\n"); |
| 3387 | |
| 3388 | return 0; |
| 3389 | } |
| 3390 | |
| 3391 | static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, |
| 3392 | size_t nbytes, loff_t off) |
| 3393 | { |
| 3394 | struct cgroup *cgrp; |
| 3395 | int ret; |
| 3396 | |
| 3397 | /* only switching to threaded mode is supported */ |
| 3398 | if (strcmp(strstrip(buf), "threaded")) |
| 3399 | return -EINVAL; |
| 3400 | |
Michal Koutný | 3bc0bb3 | 2020-01-09 16:05:59 +0100 | [diff] [blame] | 3401 | /* drain dying csses before we re-apply (threaded) subtree control */ |
| 3402 | cgrp = cgroup_kn_lock_live(of->kn, true); |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3403 | if (!cgrp) |
| 3404 | return -ENOENT; |
| 3405 | |
| 3406 | /* threaded can only be enabled */ |
| 3407 | ret = cgroup_enable_threaded(cgrp); |
| 3408 | |
| 3409 | cgroup_kn_unlock(of->kn); |
| 3410 | return ret ?: nbytes; |
| 3411 | } |
| 3412 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3413 | static int cgroup_max_descendants_show(struct seq_file *seq, void *v) |
| 3414 | { |
| 3415 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3416 | int descendants = READ_ONCE(cgrp->max_descendants); |
| 3417 | |
| 3418 | if (descendants == INT_MAX) |
| 3419 | seq_puts(seq, "max\n"); |
| 3420 | else |
| 3421 | seq_printf(seq, "%d\n", descendants); |
| 3422 | |
| 3423 | return 0; |
| 3424 | } |
| 3425 | |
| 3426 | static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of, |
| 3427 | char *buf, size_t nbytes, loff_t off) |
| 3428 | { |
| 3429 | struct cgroup *cgrp; |
| 3430 | int descendants; |
| 3431 | ssize_t ret; |
| 3432 | |
| 3433 | buf = strstrip(buf); |
| 3434 | if (!strcmp(buf, "max")) { |
| 3435 | descendants = INT_MAX; |
| 3436 | } else { |
| 3437 | ret = kstrtoint(buf, 0, &descendants); |
| 3438 | if (ret) |
| 3439 | return ret; |
| 3440 | } |
| 3441 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3442 | if (descendants < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3443 | return -ERANGE; |
| 3444 | |
| 3445 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3446 | if (!cgrp) |
| 3447 | return -ENOENT; |
| 3448 | |
| 3449 | cgrp->max_descendants = descendants; |
| 3450 | |
| 3451 | cgroup_kn_unlock(of->kn); |
| 3452 | |
| 3453 | return nbytes; |
| 3454 | } |
| 3455 | |
| 3456 | static int cgroup_max_depth_show(struct seq_file *seq, void *v) |
| 3457 | { |
| 3458 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3459 | int depth = READ_ONCE(cgrp->max_depth); |
| 3460 | |
| 3461 | if (depth == INT_MAX) |
| 3462 | seq_puts(seq, "max\n"); |
| 3463 | else |
| 3464 | seq_printf(seq, "%d\n", depth); |
| 3465 | |
| 3466 | return 0; |
| 3467 | } |
| 3468 | |
| 3469 | static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of, |
| 3470 | char *buf, size_t nbytes, loff_t off) |
| 3471 | { |
| 3472 | struct cgroup *cgrp; |
| 3473 | ssize_t ret; |
| 3474 | int depth; |
| 3475 | |
| 3476 | buf = strstrip(buf); |
| 3477 | if (!strcmp(buf, "max")) { |
| 3478 | depth = INT_MAX; |
| 3479 | } else { |
| 3480 | ret = kstrtoint(buf, 0, &depth); |
| 3481 | if (ret) |
| 3482 | return ret; |
| 3483 | } |
| 3484 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3485 | if (depth < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3486 | return -ERANGE; |
| 3487 | |
| 3488 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3489 | if (!cgrp) |
| 3490 | return -ENOENT; |
| 3491 | |
| 3492 | cgrp->max_depth = depth; |
| 3493 | |
| 3494 | cgroup_kn_unlock(of->kn); |
| 3495 | |
| 3496 | return nbytes; |
| 3497 | } |
| 3498 | |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 3499 | static int cgroup_events_show(struct seq_file *seq, void *v) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3500 | { |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 3501 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3502 | |
| 3503 | seq_printf(seq, "populated %d\n", cgroup_is_populated(cgrp)); |
| 3504 | seq_printf(seq, "frozen %d\n", test_bit(CGRP_FROZEN, &cgrp->flags)); |
| 3505 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3506 | return 0; |
| 3507 | } |
| 3508 | |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 3509 | static int cgroup_stat_show(struct seq_file *seq, void *v) |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 3510 | { |
| 3511 | struct cgroup *cgroup = seq_css(seq)->cgroup; |
| 3512 | |
| 3513 | seq_printf(seq, "nr_descendants %d\n", |
| 3514 | cgroup->nr_descendants); |
| 3515 | seq_printf(seq, "nr_dying_descendants %d\n", |
| 3516 | cgroup->nr_dying_descendants); |
| 3517 | |
| 3518 | return 0; |
| 3519 | } |
| 3520 | |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3521 | static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq, |
| 3522 | struct cgroup *cgrp, int ssid) |
| 3523 | { |
| 3524 | struct cgroup_subsys *ss = cgroup_subsys[ssid]; |
| 3525 | struct cgroup_subsys_state *css; |
| 3526 | int ret; |
| 3527 | |
| 3528 | if (!ss->css_extra_stat_show) |
| 3529 | return 0; |
| 3530 | |
| 3531 | css = cgroup_tryget_css(cgrp, ss); |
| 3532 | if (!css) |
| 3533 | return 0; |
| 3534 | |
| 3535 | ret = ss->css_extra_stat_show(seq, css); |
| 3536 | css_put(css); |
| 3537 | return ret; |
| 3538 | } |
| 3539 | |
| 3540 | static int cpu_stat_show(struct seq_file *seq, void *v) |
| 3541 | { |
Tejun Heo | c3ba132 | 2017-10-30 08:13:14 -0700 | [diff] [blame] | 3542 | struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3543 | int ret = 0; |
| 3544 | |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3545 | cgroup_base_stat_cputime_show(seq); |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3546 | #ifdef CONFIG_CGROUP_SCHED |
| 3547 | ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id); |
| 3548 | #endif |
| 3549 | return ret; |
| 3550 | } |
| 3551 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3552 | #ifdef CONFIG_PSI |
| 3553 | static int cgroup_io_pressure_show(struct seq_file *seq, void *v) |
| 3554 | { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 3555 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Qian Cai | 190ecb1 | 2020-02-23 22:00:07 -0500 | [diff] [blame] | 3556 | struct psi_group *psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi; |
Dan Schatzberg | df5ba5b | 2019-05-14 15:41:18 -0700 | [diff] [blame] | 3557 | |
| 3558 | return psi_show(seq, psi, PSI_IO); |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3559 | } |
| 3560 | static int cgroup_memory_pressure_show(struct seq_file *seq, void *v) |
| 3561 | { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 3562 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Qian Cai | 190ecb1 | 2020-02-23 22:00:07 -0500 | [diff] [blame] | 3563 | struct psi_group *psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi; |
Dan Schatzberg | df5ba5b | 2019-05-14 15:41:18 -0700 | [diff] [blame] | 3564 | |
| 3565 | return psi_show(seq, psi, PSI_MEM); |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3566 | } |
| 3567 | static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v) |
| 3568 | { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 3569 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Qian Cai | 190ecb1 | 2020-02-23 22:00:07 -0500 | [diff] [blame] | 3570 | struct psi_group *psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi; |
Dan Schatzberg | df5ba5b | 2019-05-14 15:41:18 -0700 | [diff] [blame] | 3571 | |
| 3572 | return psi_show(seq, psi, PSI_CPU); |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3573 | } |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3574 | |
| 3575 | static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf, |
| 3576 | size_t nbytes, enum psi_res res) |
| 3577 | { |
| 3578 | struct psi_trigger *new; |
| 3579 | struct cgroup *cgrp; |
Odin Ugedal | 385aac1 | 2021-01-16 18:36:33 +0100 | [diff] [blame] | 3580 | struct psi_group *psi; |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3581 | |
| 3582 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3583 | if (!cgrp) |
| 3584 | return -ENODEV; |
| 3585 | |
| 3586 | cgroup_get(cgrp); |
| 3587 | cgroup_kn_unlock(of->kn); |
| 3588 | |
Odin Ugedal | 385aac1 | 2021-01-16 18:36:33 +0100 | [diff] [blame] | 3589 | psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi; |
| 3590 | new = psi_trigger_create(psi, buf, nbytes, res); |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3591 | if (IS_ERR(new)) { |
| 3592 | cgroup_put(cgrp); |
| 3593 | return PTR_ERR(new); |
| 3594 | } |
| 3595 | |
| 3596 | psi_trigger_replace(&of->priv, new); |
| 3597 | |
| 3598 | cgroup_put(cgrp); |
| 3599 | |
| 3600 | return nbytes; |
| 3601 | } |
| 3602 | |
| 3603 | static ssize_t cgroup_io_pressure_write(struct kernfs_open_file *of, |
| 3604 | char *buf, size_t nbytes, |
| 3605 | loff_t off) |
| 3606 | { |
| 3607 | return cgroup_pressure_write(of, buf, nbytes, PSI_IO); |
| 3608 | } |
| 3609 | |
| 3610 | static ssize_t cgroup_memory_pressure_write(struct kernfs_open_file *of, |
| 3611 | char *buf, size_t nbytes, |
| 3612 | loff_t off) |
| 3613 | { |
| 3614 | return cgroup_pressure_write(of, buf, nbytes, PSI_MEM); |
| 3615 | } |
| 3616 | |
| 3617 | static ssize_t cgroup_cpu_pressure_write(struct kernfs_open_file *of, |
| 3618 | char *buf, size_t nbytes, |
| 3619 | loff_t off) |
| 3620 | { |
| 3621 | return cgroup_pressure_write(of, buf, nbytes, PSI_CPU); |
| 3622 | } |
| 3623 | |
| 3624 | static __poll_t cgroup_pressure_poll(struct kernfs_open_file *of, |
| 3625 | poll_table *pt) |
| 3626 | { |
| 3627 | return psi_trigger_poll(&of->priv, of->file, pt); |
| 3628 | } |
| 3629 | |
| 3630 | static void cgroup_pressure_release(struct kernfs_open_file *of) |
| 3631 | { |
| 3632 | psi_trigger_replace(&of->priv, NULL); |
| 3633 | } |
| 3634 | #endif /* CONFIG_PSI */ |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3635 | |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 3636 | static int cgroup_freeze_show(struct seq_file *seq, void *v) |
| 3637 | { |
| 3638 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3639 | |
| 3640 | seq_printf(seq, "%d\n", cgrp->freezer.freeze); |
| 3641 | |
| 3642 | return 0; |
| 3643 | } |
| 3644 | |
| 3645 | static ssize_t cgroup_freeze_write(struct kernfs_open_file *of, |
| 3646 | char *buf, size_t nbytes, loff_t off) |
| 3647 | { |
| 3648 | struct cgroup *cgrp; |
| 3649 | ssize_t ret; |
| 3650 | int freeze; |
| 3651 | |
| 3652 | ret = kstrtoint(strstrip(buf), 0, &freeze); |
| 3653 | if (ret) |
| 3654 | return ret; |
| 3655 | |
| 3656 | if (freeze < 0 || freeze > 1) |
| 3657 | return -ERANGE; |
| 3658 | |
| 3659 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3660 | if (!cgrp) |
| 3661 | return -ENOENT; |
| 3662 | |
| 3663 | cgroup_freeze(cgrp, freeze); |
| 3664 | |
| 3665 | cgroup_kn_unlock(of->kn); |
| 3666 | |
| 3667 | return nbytes; |
| 3668 | } |
| 3669 | |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 3670 | static void __cgroup_kill(struct cgroup *cgrp) |
| 3671 | { |
| 3672 | struct css_task_iter it; |
| 3673 | struct task_struct *task; |
| 3674 | |
| 3675 | lockdep_assert_held(&cgroup_mutex); |
| 3676 | |
| 3677 | spin_lock_irq(&css_set_lock); |
| 3678 | set_bit(CGRP_KILL, &cgrp->flags); |
| 3679 | spin_unlock_irq(&css_set_lock); |
| 3680 | |
| 3681 | css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED, &it); |
| 3682 | while ((task = css_task_iter_next(&it))) { |
| 3683 | /* Ignore kernel threads here. */ |
| 3684 | if (task->flags & PF_KTHREAD) |
| 3685 | continue; |
| 3686 | |
| 3687 | /* Skip tasks that are already dying. */ |
| 3688 | if (__fatal_signal_pending(task)) |
| 3689 | continue; |
| 3690 | |
| 3691 | send_sig(SIGKILL, task, 0); |
| 3692 | } |
| 3693 | css_task_iter_end(&it); |
| 3694 | |
| 3695 | spin_lock_irq(&css_set_lock); |
| 3696 | clear_bit(CGRP_KILL, &cgrp->flags); |
| 3697 | spin_unlock_irq(&css_set_lock); |
| 3698 | } |
| 3699 | |
| 3700 | static void cgroup_kill(struct cgroup *cgrp) |
| 3701 | { |
| 3702 | struct cgroup_subsys_state *css; |
| 3703 | struct cgroup *dsct; |
| 3704 | |
| 3705 | lockdep_assert_held(&cgroup_mutex); |
| 3706 | |
| 3707 | cgroup_for_each_live_descendant_pre(dsct, css, cgrp) |
| 3708 | __cgroup_kill(dsct); |
| 3709 | } |
| 3710 | |
| 3711 | static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf, |
| 3712 | size_t nbytes, loff_t off) |
| 3713 | { |
| 3714 | ssize_t ret = 0; |
| 3715 | int kill; |
| 3716 | struct cgroup *cgrp; |
| 3717 | |
| 3718 | ret = kstrtoint(strstrip(buf), 0, &kill); |
| 3719 | if (ret) |
| 3720 | return ret; |
| 3721 | |
| 3722 | if (kill != 1) |
| 3723 | return -ERANGE; |
| 3724 | |
| 3725 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3726 | if (!cgrp) |
| 3727 | return -ENOENT; |
| 3728 | |
| 3729 | /* |
| 3730 | * Killing is a process directed operation, i.e. the whole thread-group |
| 3731 | * is taken down so act like we do for cgroup.procs and only make this |
| 3732 | * writable in non-threaded cgroups. |
| 3733 | */ |
| 3734 | if (cgroup_is_threaded(cgrp)) |
| 3735 | ret = -EOPNOTSUPP; |
| 3736 | else |
| 3737 | cgroup_kill(cgrp); |
| 3738 | |
| 3739 | cgroup_kn_unlock(of->kn); |
| 3740 | |
| 3741 | return ret ?: nbytes; |
| 3742 | } |
| 3743 | |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3744 | static int cgroup_file_open(struct kernfs_open_file *of) |
| 3745 | { |
Hui Su | 5a7b5f3 | 2020-11-06 22:47:40 +0800 | [diff] [blame] | 3746 | struct cftype *cft = of_cft(of); |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3747 | |
| 3748 | if (cft->open) |
| 3749 | return cft->open(of); |
| 3750 | return 0; |
| 3751 | } |
| 3752 | |
| 3753 | static void cgroup_file_release(struct kernfs_open_file *of) |
| 3754 | { |
Hui Su | 5a7b5f3 | 2020-11-06 22:47:40 +0800 | [diff] [blame] | 3755 | struct cftype *cft = of_cft(of); |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3756 | |
| 3757 | if (cft->release) |
| 3758 | cft->release(of); |
| 3759 | } |
| 3760 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3761 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 3762 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3763 | { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3764 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3765 | struct cgroup *cgrp = of->kn->parent->priv; |
Hui Su | 5a7b5f3 | 2020-11-06 22:47:40 +0800 | [diff] [blame] | 3766 | struct cftype *cft = of_cft(of); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3767 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3768 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3769 | |
Jouni Roivas | 65026da | 2020-09-30 19:42:42 +0300 | [diff] [blame] | 3770 | if (!nbytes) |
| 3771 | return 0; |
| 3772 | |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3773 | /* |
| 3774 | * If namespaces are delegation boundaries, disallow writes to |
| 3775 | * files in an non-init namespace root from inside the namespace |
| 3776 | * except for the files explicitly marked delegatable - |
| 3777 | * cgroup.procs and cgroup.subtree_control. |
| 3778 | */ |
| 3779 | if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) && |
| 3780 | !(cft->flags & CFTYPE_NS_DELEGATABLE) && |
| 3781 | ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp) |
| 3782 | return -EPERM; |
| 3783 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3784 | if (cft->write) |
| 3785 | return cft->write(of, buf, nbytes, off); |
| 3786 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3787 | /* |
| 3788 | * kernfs guarantees that a file isn't deleted with operations in |
| 3789 | * flight, which means that the matching css is and stays alive and |
| 3790 | * doesn't need to be pinned. The RCU locking is not necessary |
| 3791 | * either. It's just for the convenience of using cgroup_css(). |
| 3792 | */ |
| 3793 | rcu_read_lock(); |
| 3794 | css = cgroup_css(cgrp, cft->ss); |
| 3795 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3796 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3797 | if (cft->write_u64) { |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3798 | unsigned long long v; |
| 3799 | ret = kstrtoull(buf, 0, &v); |
| 3800 | if (!ret) |
| 3801 | ret = cft->write_u64(css, cft, v); |
| 3802 | } else if (cft->write_s64) { |
| 3803 | long long v; |
| 3804 | ret = kstrtoll(buf, 0, &v); |
| 3805 | if (!ret) |
| 3806 | ret = cft->write_s64(css, cft, v); |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3807 | } else { |
| 3808 | ret = -EINVAL; |
| 3809 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3810 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3811 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
Johannes Weiner | dc50537 | 2019-03-05 15:45:48 -0800 | [diff] [blame] | 3814 | static __poll_t cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt) |
| 3815 | { |
Hui Su | 5a7b5f3 | 2020-11-06 22:47:40 +0800 | [diff] [blame] | 3816 | struct cftype *cft = of_cft(of); |
Johannes Weiner | dc50537 | 2019-03-05 15:45:48 -0800 | [diff] [blame] | 3817 | |
| 3818 | if (cft->poll) |
| 3819 | return cft->poll(of, pt); |
| 3820 | |
| 3821 | return kernfs_generic_poll(of, pt); |
| 3822 | } |
| 3823 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3824 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3825 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3826 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 3830 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3831 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 3835 | { |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3836 | if (seq_cft(seq)->seq_stop) |
| 3837 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3838 | } |
| 3839 | |
| 3840 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 3841 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3842 | struct cftype *cft = seq_cft(m); |
| 3843 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 3844 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3845 | if (cft->seq_show) |
| 3846 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3847 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3848 | if (cft->read_u64) |
| 3849 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 3850 | else if (cft->read_s64) |
| 3851 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 3852 | else |
| 3853 | return -EINVAL; |
| 3854 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3855 | } |
| 3856 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3857 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 3858 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3859 | .open = cgroup_file_open, |
| 3860 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3861 | .write = cgroup_file_write, |
Johannes Weiner | dc50537 | 2019-03-05 15:45:48 -0800 | [diff] [blame] | 3862 | .poll = cgroup_file_poll, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3863 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3864 | }; |
| 3865 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3866 | static struct kernfs_ops cgroup_kf_ops = { |
| 3867 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3868 | .open = cgroup_file_open, |
| 3869 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3870 | .write = cgroup_file_write, |
Johannes Weiner | dc50537 | 2019-03-05 15:45:48 -0800 | [diff] [blame] | 3871 | .poll = cgroup_file_poll, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3872 | .seq_start = cgroup_seqfile_start, |
| 3873 | .seq_next = cgroup_seqfile_next, |
| 3874 | .seq_stop = cgroup_seqfile_stop, |
| 3875 | .seq_show = cgroup_seqfile_show, |
| 3876 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3877 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3878 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 3879 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 3880 | { |
| 3881 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 3882 | .ia_uid = current_fsuid(), |
| 3883 | .ia_gid = current_fsgid(), }; |
| 3884 | |
| 3885 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 3886 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 3887 | return 0; |
| 3888 | |
| 3889 | return kernfs_setattr(kn, &iattr); |
| 3890 | } |
| 3891 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3892 | static void cgroup_file_notify_timer(struct timer_list *timer) |
| 3893 | { |
| 3894 | cgroup_file_notify(container_of(timer, struct cgroup_file, |
| 3895 | notify_timer)); |
| 3896 | } |
| 3897 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3898 | static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, |
| 3899 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3900 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3901 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3902 | struct kernfs_node *kn; |
| 3903 | struct lock_class_key *key = NULL; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3904 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3905 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3906 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 3907 | key = &cft->lockdep_key; |
| 3908 | #endif |
| 3909 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame] | 3910 | cgroup_file_mode(cft), |
| 3911 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, |
| 3912 | 0, cft->kf_ops, cft, |
Tejun Heo | dfeb0750 | 2015-02-13 14:36:31 -0800 | [diff] [blame] | 3913 | NULL, key); |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3914 | if (IS_ERR(kn)) |
| 3915 | return PTR_ERR(kn); |
| 3916 | |
| 3917 | ret = cgroup_kn_set_ugid(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3918 | if (ret) { |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3919 | kernfs_remove(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3920 | return ret; |
| 3921 | } |
| 3922 | |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3923 | if (cft->file_offset) { |
| 3924 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 3925 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3926 | timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0); |
| 3927 | |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3928 | spin_lock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3929 | cfile->kn = kn; |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3930 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3931 | } |
| 3932 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3933 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3934 | } |
| 3935 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3936 | /** |
| 3937 | * cgroup_addrm_files - add or remove files to a cgroup directory |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3938 | * @css: the target css |
| 3939 | * @cgrp: the target cgroup (usually css->cgroup) |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3940 | * @cfts: array of cftypes to be added |
| 3941 | * @is_add: whether to add or remove |
| 3942 | * |
| 3943 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3944 | * For removals, this function never fails. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3945 | */ |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3946 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 3947 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3948 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3949 | { |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3950 | struct cftype *cft, *cft_end = NULL; |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 3951 | int ret = 0; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3952 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3953 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3954 | |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3955 | restart: |
| 3956 | for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3957 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3958 | if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
Tejun Heo | 8cbbf2c | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 3959 | continue; |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3960 | if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp)) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3961 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3962 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3963 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3964 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3965 | continue; |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 3966 | if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug) |
| 3967 | continue; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 3968 | if (is_add) { |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3969 | ret = cgroup_add_file(css, cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3970 | if (ret) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 3971 | pr_warn("%s: failed to add %s, err=%d\n", |
| 3972 | __func__, cft->name, ret); |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3973 | cft_end = cft; |
| 3974 | is_add = false; |
| 3975 | goto restart; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3976 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 3977 | } else { |
| 3978 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3979 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3980 | } |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 3981 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3982 | } |
| 3983 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3984 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3985 | { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3986 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3987 | struct cgroup *root = &ss->root->cgrp; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3988 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3989 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3990 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3991 | lockdep_assert_held(&cgroup_mutex); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3992 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3993 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3994 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3995 | struct cgroup *cgrp = css->cgroup; |
| 3996 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 3997 | if (!(css->flags & CSS_VISIBLE)) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3998 | continue; |
| 3999 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4000 | ret = cgroup_addrm_files(css, cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4001 | if (ret) |
| 4002 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4003 | } |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4004 | |
| 4005 | if (is_add && !ret) |
| 4006 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4007 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4008 | } |
| 4009 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4010 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 4011 | { |
| 4012 | struct cftype *cft; |
| 4013 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4014 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 4015 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 4016 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 4017 | kfree(cft->kf_ops); |
| 4018 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4019 | cft->ss = NULL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4020 | |
| 4021 | /* revert flags set by cgroup core while adding @cfts */ |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4022 | cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4023 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4024 | } |
| 4025 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4026 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4027 | { |
| 4028 | struct cftype *cft; |
| 4029 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4030 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 4031 | struct kernfs_ops *kf_ops; |
| 4032 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4033 | WARN_ON(cft->ss || cft->kf_ops); |
| 4034 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4035 | if (cft->seq_start) |
| 4036 | kf_ops = &cgroup_kf_ops; |
| 4037 | else |
| 4038 | kf_ops = &cgroup_kf_single_ops; |
| 4039 | |
| 4040 | /* |
| 4041 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 4042 | * make a copy of kf_ops to set its atomic_write_len. |
| 4043 | */ |
| 4044 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 4045 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 4046 | if (!kf_ops) { |
| 4047 | cgroup_exit_cftypes(cfts); |
| 4048 | return -ENOMEM; |
| 4049 | } |
| 4050 | kf_ops->atomic_write_len = cft->max_write_len; |
| 4051 | } |
| 4052 | |
| 4053 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4054 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4058 | } |
| 4059 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4060 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 4061 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4062 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4063 | |
| 4064 | if (!cfts || !cfts[0].ss) |
| 4065 | return -ENOENT; |
| 4066 | |
| 4067 | list_del(&cfts->node); |
| 4068 | cgroup_apply_cftypes(cfts, false); |
| 4069 | cgroup_exit_cftypes(cfts); |
| 4070 | return 0; |
| 4071 | } |
| 4072 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4073 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4074 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 4075 | * @cfts: zero-length name terminated array of cftypes |
| 4076 | * |
| 4077 | * Unregister @cfts. Files described by @cfts are removed from all |
| 4078 | * existing cgroups and all future cgroups won't have them either. This |
| 4079 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 4080 | * |
| 4081 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 4082 | * registered. |
| 4083 | */ |
| 4084 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 4085 | { |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4086 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4087 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4088 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4089 | ret = cgroup_rm_cftypes_locked(cfts); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4090 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4091 | return ret; |
| 4092 | } |
| 4093 | |
| 4094 | /** |
| 4095 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 4096 | * @ss: target cgroup subsystem |
| 4097 | * @cfts: zero-length name terminated array of cftypes |
| 4098 | * |
| 4099 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 4100 | * existing cgroups to which @ss is attached and all future cgroups will |
| 4101 | * have them too. This function can be called anytime whether @ss is |
| 4102 | * attached or not. |
| 4103 | * |
| 4104 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 4105 | * function currently returns 0 as long as @cfts registration is successful |
| 4106 | * even if some file creation attempts on existing cgroups fail. |
| 4107 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4108 | static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4109 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4110 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4111 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 4112 | if (!cgroup_ssid_enabled(ss->id)) |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 4113 | return 0; |
| 4114 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 4115 | if (!cfts || cfts[0].name[0] == '\0') |
| 4116 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4117 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4118 | ret = cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4119 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4120 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4121 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4122 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4123 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4124 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4125 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4126 | if (ret) |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4127 | cgroup_rm_cftypes_locked(cfts); |
| 4128 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4129 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4130 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4131 | } |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4132 | |
| 4133 | /** |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4134 | * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy |
| 4135 | * @ss: target cgroup subsystem |
| 4136 | * @cfts: zero-length name terminated array of cftypes |
| 4137 | * |
| 4138 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 4139 | * the default hierarchy. |
| 4140 | */ |
| 4141 | int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 4142 | { |
| 4143 | struct cftype *cft; |
| 4144 | |
| 4145 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4146 | cft->flags |= __CFTYPE_ONLY_ON_DFL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4147 | return cgroup_add_cftypes(ss, cfts); |
| 4148 | } |
| 4149 | |
| 4150 | /** |
| 4151 | * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies |
| 4152 | * @ss: target cgroup subsystem |
| 4153 | * @cfts: zero-length name terminated array of cftypes |
| 4154 | * |
| 4155 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 4156 | * the legacy hierarchies. |
| 4157 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4158 | int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 4159 | { |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4160 | struct cftype *cft; |
| 4161 | |
Tejun Heo | e4b7037 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 4162 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
| 4163 | cft->flags |= __CFTYPE_NOT_ON_DFL; |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4164 | return cgroup_add_cftypes(ss, cfts); |
| 4165 | } |
| 4166 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4167 | /** |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 4168 | * cgroup_file_notify - generate a file modified event for a cgroup_file |
| 4169 | * @cfile: target cgroup_file |
| 4170 | * |
| 4171 | * @cfile must have been obtained by setting cftype->file_offset. |
| 4172 | */ |
| 4173 | void cgroup_file_notify(struct cgroup_file *cfile) |
| 4174 | { |
| 4175 | unsigned long flags; |
| 4176 | |
| 4177 | spin_lock_irqsave(&cgroup_file_kn_lock, flags); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4178 | if (cfile->kn) { |
| 4179 | unsigned long last = cfile->notified_at; |
| 4180 | unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV; |
| 4181 | |
| 4182 | if (time_in_range(jiffies, last, next)) { |
| 4183 | timer_reduce(&cfile->notify_timer, next); |
| 4184 | } else { |
| 4185 | kernfs_notify(cfile->kn); |
| 4186 | cfile->notified_at = jiffies; |
| 4187 | } |
| 4188 | } |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 4189 | spin_unlock_irqrestore(&cgroup_file_kn_lock, flags); |
| 4190 | } |
| 4191 | |
| 4192 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4193 | * css_next_child - find the next child of a given css |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4194 | * @pos: the current position (%NULL to initiate traversal) |
| 4195 | * @parent: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4196 | * |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4197 | * This function returns the next child of @parent and should be called |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4198 | * under either cgroup_mutex or RCU read lock. The only requirement is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4199 | * that @parent and @pos are accessible. The next sibling is guaranteed to |
| 4200 | * be returned regardless of their states. |
| 4201 | * |
| 4202 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4203 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4204 | * future iterations and will stay visible until the last reference is put. |
| 4205 | * A css which hasn't finished ->css_online() or already finished |
| 4206 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4207 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4208 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4209 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, |
| 4210 | struct cgroup_subsys_state *parent) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4211 | { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4212 | struct cgroup_subsys_state *next; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4213 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4214 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4215 | |
| 4216 | /* |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4217 | * @pos could already have been unlinked from the sibling list. |
| 4218 | * Once a cgroup is removed, its ->sibling.next is no longer |
| 4219 | * updated when its next sibling changes. CSS_RELEASED is set when |
| 4220 | * @pos is taken off list, at which time its next pointer is valid, |
| 4221 | * and, as releases are serialized, the one pointed to by the next |
| 4222 | * pointer is guaranteed to not have started release yet. This |
| 4223 | * implies that if we observe !CSS_RELEASED on @pos in this RCU |
| 4224 | * critical section, the one pointed to by its next pointer is |
| 4225 | * guaranteed to not have finished its RCU grace period even if we |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 4226 | * have dropped rcu_read_lock() in-between iterations. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4227 | * |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4228 | * If @pos has CSS_RELEASED set, its next pointer can't be |
| 4229 | * dereferenced; however, as each css is given a monotonically |
| 4230 | * increasing unique serial number and always appended to the |
| 4231 | * sibling list, the next one can be found by walking the parent's |
| 4232 | * children until the first css with higher serial number than |
| 4233 | * @pos's. While this path can be slower, it happens iff iteration |
| 4234 | * races against release and the race window is very small. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4235 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4236 | if (!pos) { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4237 | next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); |
| 4238 | } else if (likely(!(pos->flags & CSS_RELEASED))) { |
| 4239 | next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4240 | } else { |
Madhuparna Bhowmik | 3010c5b | 2020-01-18 08:40:51 +0530 | [diff] [blame] | 4241 | list_for_each_entry_rcu(next, &parent->children, sibling, |
| 4242 | lockdep_is_held(&cgroup_mutex)) |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4243 | if (next->serial_nr > pos->serial_nr) |
| 4244 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4245 | } |
| 4246 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4247 | /* |
| 4248 | * @next, if not pointing to the head, can be dereferenced and is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4249 | * the next sibling. |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4250 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4251 | if (&next->sibling != &parent->children) |
| 4252 | return next; |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4253 | return NULL; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4254 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4255 | |
| 4256 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4257 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4258 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4259 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4260 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4261 | * To be used by css_for_each_descendant_pre(). Find the next descendant |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4262 | * to visit for pre-order traversal of @root's descendants. @root is |
| 4263 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4264 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4265 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4266 | * doesn't require the whole traversal to be contained in a single critical |
| 4267 | * section. This function will return the correct next descendant as long |
| 4268 | * as both @pos and @root are accessible and @pos is a descendant of @root. |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4269 | * |
| 4270 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4271 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4272 | * future iterations and will stay visible until the last reference is put. |
| 4273 | * A css which hasn't finished ->css_online() or already finished |
| 4274 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4275 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4276 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4277 | struct cgroup_subsys_state * |
| 4278 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 4279 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4280 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4281 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4282 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4283 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4284 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4285 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 4286 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4287 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4288 | |
| 4289 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4290 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4291 | if (next) |
| 4292 | return next; |
| 4293 | |
| 4294 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4295 | while (pos != root) { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4296 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4297 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4298 | return next; |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4299 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 4300 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4301 | |
| 4302 | return NULL; |
| 4303 | } |
Christoph Hellwig | 474a280 | 2019-06-21 10:22:48 +0200 | [diff] [blame] | 4304 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4305 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4306 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4307 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 4308 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4309 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4310 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 4311 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4312 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4313 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4314 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4315 | * doesn't require the whole traversal to be contained in a single critical |
| 4316 | * section. This function will return the correct rightmost descendant as |
| 4317 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4318 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4319 | struct cgroup_subsys_state * |
| 4320 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4321 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4322 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4323 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4324 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4325 | |
| 4326 | do { |
| 4327 | last = pos; |
| 4328 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 4329 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4330 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4331 | pos = tmp; |
| 4332 | } while (pos); |
| 4333 | |
| 4334 | return last; |
| 4335 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4336 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4337 | static struct cgroup_subsys_state * |
| 4338 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4339 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4340 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4341 | |
| 4342 | do { |
| 4343 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4344 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4345 | } while (pos); |
| 4346 | |
| 4347 | return last; |
| 4348 | } |
| 4349 | |
| 4350 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4351 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4352 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4353 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4354 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4355 | * To be used by css_for_each_descendant_post(). Find the next descendant |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4356 | * to visit for post-order traversal of @root's descendants. @root is |
| 4357 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4358 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4359 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4360 | * doesn't require the whole traversal to be contained in a single critical |
| 4361 | * section. This function will return the correct next descendant as long |
| 4362 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 4363 | * @cgroup. |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4364 | * |
| 4365 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4366 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4367 | * future iterations and will stay visible until the last reference is put. |
| 4368 | * A css which hasn't finished ->css_online() or already finished |
| 4369 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4370 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4371 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4372 | struct cgroup_subsys_state * |
| 4373 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 4374 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4375 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4376 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4377 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4378 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4379 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 4380 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 4381 | if (!pos) |
| 4382 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4383 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4384 | /* if we visited @root, we're done */ |
| 4385 | if (pos == root) |
| 4386 | return NULL; |
| 4387 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4388 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4389 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4390 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4391 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4392 | |
| 4393 | /* no sibling left, visit parent */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4394 | return pos->parent; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4395 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4396 | |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4397 | /** |
| 4398 | * css_has_online_children - does a css have online children |
| 4399 | * @css: the target css |
| 4400 | * |
| 4401 | * Returns %true if @css has any online children; otherwise, %false. This |
| 4402 | * function can be called from any context but the caller is responsible |
| 4403 | * for synchronizing against on/offlining as necessary. |
| 4404 | */ |
| 4405 | bool css_has_online_children(struct cgroup_subsys_state *css) |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4406 | { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4407 | struct cgroup_subsys_state *child; |
| 4408 | bool ret = false; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4409 | |
| 4410 | rcu_read_lock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4411 | css_for_each_child(child, css) { |
Li Zefan | 99bae5f | 2014-06-12 14:31:31 +0800 | [diff] [blame] | 4412 | if (child->flags & CSS_ONLINE) { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4413 | ret = true; |
| 4414 | break; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4415 | } |
| 4416 | } |
| 4417 | rcu_read_unlock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4418 | return ret; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 4419 | } |
| 4420 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4421 | static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it) |
| 4422 | { |
| 4423 | struct list_head *l; |
| 4424 | struct cgrp_cset_link *link; |
| 4425 | struct css_set *cset; |
| 4426 | |
| 4427 | lockdep_assert_held(&css_set_lock); |
| 4428 | |
| 4429 | /* find the next threaded cset */ |
| 4430 | if (it->tcset_pos) { |
| 4431 | l = it->tcset_pos->next; |
| 4432 | |
| 4433 | if (l != it->tcset_head) { |
| 4434 | it->tcset_pos = l; |
| 4435 | return container_of(l, struct css_set, |
| 4436 | threaded_csets_node); |
| 4437 | } |
| 4438 | |
| 4439 | it->tcset_pos = NULL; |
| 4440 | } |
| 4441 | |
| 4442 | /* find the next cset */ |
| 4443 | l = it->cset_pos; |
| 4444 | l = l->next; |
| 4445 | if (l == it->cset_head) { |
| 4446 | it->cset_pos = NULL; |
| 4447 | return NULL; |
| 4448 | } |
| 4449 | |
| 4450 | if (it->ss) { |
| 4451 | cset = container_of(l, struct css_set, e_cset_node[it->ss->id]); |
| 4452 | } else { |
| 4453 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 4454 | cset = link->cset; |
| 4455 | } |
| 4456 | |
| 4457 | it->cset_pos = l; |
| 4458 | |
| 4459 | /* initialize threaded css_set walking */ |
| 4460 | if (it->flags & CSS_TASK_ITER_THREADED) { |
| 4461 | if (it->cur_dcset) |
| 4462 | put_css_set_locked(it->cur_dcset); |
| 4463 | it->cur_dcset = cset; |
| 4464 | get_css_set(cset); |
| 4465 | |
| 4466 | it->tcset_head = &cset->threaded_csets; |
| 4467 | it->tcset_pos = &cset->threaded_csets; |
| 4468 | } |
| 4469 | |
| 4470 | return cset; |
| 4471 | } |
| 4472 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4473 | /** |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 4474 | * css_task_iter_advance_css_set - advance a task iterator to the next css_set |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4475 | * @it: the iterator to advance |
| 4476 | * |
| 4477 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4478 | */ |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4479 | static void css_task_iter_advance_css_set(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4480 | { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4481 | struct css_set *cset; |
| 4482 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4483 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4484 | |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4485 | /* Advance to the next non-empty css_set and find first non-empty tasks list*/ |
| 4486 | while ((cset = css_task_iter_next_css_set(it))) { |
| 4487 | if (!list_empty(&cset->tasks)) { |
| 4488 | it->cur_tasks_head = &cset->tasks; |
| 4489 | break; |
| 4490 | } else if (!list_empty(&cset->mg_tasks)) { |
| 4491 | it->cur_tasks_head = &cset->mg_tasks; |
| 4492 | break; |
| 4493 | } else if (!list_empty(&cset->dying_tasks)) { |
| 4494 | it->cur_tasks_head = &cset->dying_tasks; |
| 4495 | break; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4496 | } |
Michal Koutný | 9c974c7 | 2020-01-24 12:40:15 +0100 | [diff] [blame] | 4497 | } |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4498 | if (!cset) { |
| 4499 | it->task_pos = NULL; |
| 4500 | return; |
| 4501 | } |
| 4502 | it->task_pos = it->cur_tasks_head->next; |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4503 | |
| 4504 | /* |
| 4505 | * We don't keep css_sets locked across iteration steps and thus |
| 4506 | * need to take steps to ensure that iteration can be resumed after |
| 4507 | * the lock is re-acquired. Iteration is performed at two levels - |
| 4508 | * css_sets and tasks in them. |
| 4509 | * |
| 4510 | * Once created, a css_set never leaves its cgroup lists, so a |
| 4511 | * pinned css_set is guaranteed to stay put and we can resume |
| 4512 | * iteration afterwards. |
| 4513 | * |
| 4514 | * Tasks may leave @cset across iteration steps. This is resolved |
| 4515 | * by registering each iterator with the css_set currently being |
| 4516 | * walked and making css_set_move_task() advance iterators whose |
| 4517 | * next task is leaving. |
| 4518 | */ |
| 4519 | if (it->cur_cset) { |
| 4520 | list_del(&it->iters_node); |
| 4521 | put_css_set_locked(it->cur_cset); |
| 4522 | } |
| 4523 | get_css_set(cset); |
| 4524 | it->cur_cset = cset; |
| 4525 | list_add(&it->iters_node, &cset->task_iters); |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4526 | } |
| 4527 | |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4528 | static void css_task_iter_skip(struct css_task_iter *it, |
| 4529 | struct task_struct *task) |
| 4530 | { |
| 4531 | lockdep_assert_held(&css_set_lock); |
| 4532 | |
| 4533 | if (it->task_pos == &task->cg_list) { |
| 4534 | it->task_pos = it->task_pos->next; |
| 4535 | it->flags |= CSS_TASK_ITER_SKIPPED; |
| 4536 | } |
| 4537 | } |
| 4538 | |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4539 | static void css_task_iter_advance(struct css_task_iter *it) |
| 4540 | { |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4541 | struct task_struct *task; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4542 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4543 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4544 | repeat: |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4545 | if (it->task_pos) { |
| 4546 | /* |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4547 | * Advance iterator to find next entry. We go through cset |
| 4548 | * tasks, mg_tasks and dying_tasks, when consumed we move onto |
| 4549 | * the next cset. |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4550 | */ |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4551 | if (it->flags & CSS_TASK_ITER_SKIPPED) |
| 4552 | it->flags &= ~CSS_TASK_ITER_SKIPPED; |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4553 | else |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4554 | it->task_pos = it->task_pos->next; |
| 4555 | |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4556 | if (it->task_pos == &it->cur_cset->tasks) { |
| 4557 | it->cur_tasks_head = &it->cur_cset->mg_tasks; |
| 4558 | it->task_pos = it->cur_tasks_head->next; |
Michal Koutný | 9c974c7 | 2020-01-24 12:40:15 +0100 | [diff] [blame] | 4559 | } |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4560 | if (it->task_pos == &it->cur_cset->mg_tasks) { |
| 4561 | it->cur_tasks_head = &it->cur_cset->dying_tasks; |
| 4562 | it->task_pos = it->cur_tasks_head->next; |
Michal Koutný | 9c974c7 | 2020-01-24 12:40:15 +0100 | [diff] [blame] | 4563 | } |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4564 | if (it->task_pos == &it->cur_cset->dying_tasks) |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4565 | css_task_iter_advance_css_set(it); |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4566 | } else { |
| 4567 | /* called from start, proceed to the first cset */ |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4568 | css_task_iter_advance_css_set(it); |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4569 | } |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4570 | |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4571 | if (!it->task_pos) |
| 4572 | return; |
| 4573 | |
| 4574 | task = list_entry(it->task_pos, struct task_struct, cg_list); |
| 4575 | |
| 4576 | if (it->flags & CSS_TASK_ITER_PROCS) { |
| 4577 | /* if PROCS, skip over tasks which aren't group leaders */ |
| 4578 | if (!thread_group_leader(task)) |
| 4579 | goto repeat; |
| 4580 | |
| 4581 | /* and dying leaders w/o live member threads */ |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4582 | if (it->cur_tasks_head == &it->cur_cset->dying_tasks && |
Michal Koutný | 9c974c7 | 2020-01-24 12:40:15 +0100 | [diff] [blame] | 4583 | !atomic_read(&task->signal->live)) |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4584 | goto repeat; |
| 4585 | } else { |
| 4586 | /* skip all dying ones */ |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4587 | if (it->cur_tasks_head == &it->cur_cset->dying_tasks) |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4588 | goto repeat; |
| 4589 | } |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4590 | } |
| 4591 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4592 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4593 | * css_task_iter_start - initiate task iteration |
| 4594 | * @css: the css to walk tasks of |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4595 | * @flags: CSS_TASK_ITER_* flags |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4596 | * @it: the task iterator to use |
| 4597 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4598 | * Initiate iteration through the tasks of @css. The caller can call |
| 4599 | * css_task_iter_next() to walk through the tasks until the function |
| 4600 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 4601 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4602 | */ |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4603 | void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4604 | struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4605 | { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4606 | memset(it, 0, sizeof(*it)); |
| 4607 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4608 | spin_lock_irq(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4609 | |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4610 | it->ss = css->ss; |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4611 | it->flags = flags; |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4612 | |
| 4613 | if (it->ss) |
| 4614 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; |
| 4615 | else |
| 4616 | it->cset_pos = &css->cgroup->cset_links; |
| 4617 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4618 | it->cset_head = it->cset_pos; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4619 | |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4620 | css_task_iter_advance(it); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4621 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4622 | spin_unlock_irq(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4623 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4624 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4625 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4626 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4627 | * @it: the task iterator being iterated |
| 4628 | * |
| 4629 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4630 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 4631 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4632 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4633 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4634 | { |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4635 | if (it->cur_task) { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4636 | put_task_struct(it->cur_task); |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4637 | it->cur_task = NULL; |
| 4638 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4639 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4640 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4641 | |
Tejun Heo | cee0c33 | 2019-06-05 09:54:34 -0700 | [diff] [blame] | 4642 | /* @it may be half-advanced by skips, finish advancing */ |
| 4643 | if (it->flags & CSS_TASK_ITER_SKIPPED) |
| 4644 | css_task_iter_advance(it); |
| 4645 | |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4646 | if (it->task_pos) { |
| 4647 | it->cur_task = list_entry(it->task_pos, struct task_struct, |
| 4648 | cg_list); |
| 4649 | get_task_struct(it->cur_task); |
| 4650 | css_task_iter_advance(it); |
| 4651 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4652 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4653 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4654 | |
| 4655 | return it->cur_task; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4656 | } |
| 4657 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4658 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4659 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4660 | * @it: the task iterator to finish |
| 4661 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4662 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4663 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4664 | void css_task_iter_end(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4665 | { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4666 | if (it->cur_cset) { |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4667 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4668 | list_del(&it->iters_node); |
| 4669 | put_css_set_locked(it->cur_cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4670 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4671 | } |
| 4672 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4673 | if (it->cur_dcset) |
| 4674 | put_css_set(it->cur_dcset); |
| 4675 | |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4676 | if (it->cur_task) |
| 4677 | put_task_struct(it->cur_task); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4678 | } |
| 4679 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4680 | static void cgroup_procs_release(struct kernfs_open_file *of) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4681 | { |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4682 | if (of->priv) { |
| 4683 | css_task_iter_end(of->priv); |
| 4684 | kfree(of->priv); |
| 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos) |
| 4689 | { |
| 4690 | struct kernfs_open_file *of = s->private; |
| 4691 | struct css_task_iter *it = of->priv; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 4692 | |
Vasily Averin | 2d4ecb0 | 2020-01-30 13:34:59 +0300 | [diff] [blame] | 4693 | if (pos) |
| 4694 | (*pos)++; |
| 4695 | |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4696 | return css_task_iter_next(it); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4697 | } |
| 4698 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4699 | static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos, |
| 4700 | unsigned int iter_flags) |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 4701 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4702 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4703 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4704 | struct css_task_iter *it = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4705 | |
| 4706 | /* |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4707 | * When a seq_file is seeked, it's always traversed sequentially |
| 4708 | * from position 0, so we can simply keep iterating on !0 *pos. |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4709 | */ |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4710 | if (!it) { |
Vasily Averin | 2d4ecb0 | 2020-01-30 13:34:59 +0300 | [diff] [blame] | 4711 | if (WARN_ON_ONCE((*pos))) |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4712 | return ERR_PTR(-EINVAL); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4713 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4714 | it = kzalloc(sizeof(*it), GFP_KERNEL); |
| 4715 | if (!it) |
| 4716 | return ERR_PTR(-ENOMEM); |
| 4717 | of->priv = it; |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4718 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Vasily Averin | 2d4ecb0 | 2020-01-30 13:34:59 +0300 | [diff] [blame] | 4719 | } else if (!(*pos)) { |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4720 | css_task_iter_end(it); |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4721 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Vasily Averin | 2d4ecb0 | 2020-01-30 13:34:59 +0300 | [diff] [blame] | 4722 | } else |
| 4723 | return it->cur_task; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4724 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4725 | return cgroup_procs_next(s, NULL, NULL); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4726 | } |
| 4727 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4728 | static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) |
| 4729 | { |
| 4730 | struct cgroup *cgrp = seq_css(s)->cgroup; |
| 4731 | |
| 4732 | /* |
| 4733 | * All processes of a threaded subtree belong to the domain cgroup |
| 4734 | * of the subtree. Only threads can be distributed across the |
| 4735 | * subtree. Reject reads on cgroup.procs in the subtree proper. |
| 4736 | * They're always empty anyway. |
| 4737 | */ |
| 4738 | if (cgroup_is_threaded(cgrp)) |
| 4739 | return ERR_PTR(-EOPNOTSUPP); |
| 4740 | |
| 4741 | return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS | |
| 4742 | CSS_TASK_ITER_THREADED); |
| 4743 | } |
| 4744 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4745 | static int cgroup_procs_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4746 | { |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4747 | seq_printf(s, "%d\n", task_pid_vnr(v)); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4748 | return 0; |
| 4749 | } |
| 4750 | |
Christian Brauner | f355322 | 2020-02-05 14:26:21 +0100 | [diff] [blame] | 4751 | static int cgroup_may_write(const struct cgroup *cgrp, struct super_block *sb) |
| 4752 | { |
| 4753 | int ret; |
| 4754 | struct inode *inode; |
| 4755 | |
| 4756 | lockdep_assert_held(&cgroup_mutex); |
| 4757 | |
| 4758 | inode = kernfs_get_inode(sb, cgrp->procs_file.kn); |
| 4759 | if (!inode) |
| 4760 | return -ENOMEM; |
| 4761 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 4762 | ret = inode_permission(&init_user_ns, inode, MAY_WRITE); |
Christian Brauner | f355322 | 2020-02-05 14:26:21 +0100 | [diff] [blame] | 4763 | iput(inode); |
| 4764 | return ret; |
| 4765 | } |
| 4766 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4767 | static int cgroup_procs_write_permission(struct cgroup *src_cgrp, |
| 4768 | struct cgroup *dst_cgrp, |
| 4769 | struct super_block *sb) |
| 4770 | { |
| 4771 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
| 4772 | struct cgroup *com_cgrp = src_cgrp; |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4773 | int ret; |
| 4774 | |
| 4775 | lockdep_assert_held(&cgroup_mutex); |
| 4776 | |
| 4777 | /* find the common ancestor */ |
| 4778 | while (!cgroup_is_descendant(dst_cgrp, com_cgrp)) |
| 4779 | com_cgrp = cgroup_parent(com_cgrp); |
| 4780 | |
| 4781 | /* %current should be authorized to migrate to the common ancestor */ |
Christian Brauner | f355322 | 2020-02-05 14:26:21 +0100 | [diff] [blame] | 4782 | ret = cgroup_may_write(com_cgrp, sb); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4783 | if (ret) |
| 4784 | return ret; |
| 4785 | |
| 4786 | /* |
| 4787 | * If namespaces are delegation boundaries, %current must be able |
| 4788 | * to see both source and destination cgroups from its namespace. |
| 4789 | */ |
| 4790 | if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) && |
| 4791 | (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) || |
| 4792 | !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp))) |
| 4793 | return -ENOENT; |
| 4794 | |
| 4795 | return 0; |
| 4796 | } |
| 4797 | |
Christian Brauner | 6df970e | 2020-02-05 14:26:18 +0100 | [diff] [blame] | 4798 | static int cgroup_attach_permissions(struct cgroup *src_cgrp, |
| 4799 | struct cgroup *dst_cgrp, |
| 4800 | struct super_block *sb, bool threadgroup) |
| 4801 | { |
| 4802 | int ret = 0; |
| 4803 | |
| 4804 | ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, sb); |
| 4805 | if (ret) |
| 4806 | return ret; |
| 4807 | |
| 4808 | ret = cgroup_migrate_vet_dst(dst_cgrp); |
| 4809 | if (ret) |
| 4810 | return ret; |
| 4811 | |
| 4812 | if (!threadgroup && (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp)) |
| 4813 | ret = -EOPNOTSUPP; |
| 4814 | |
| 4815 | return ret; |
| 4816 | } |
| 4817 | |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4818 | static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf, |
| 4819 | bool threadgroup) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4820 | { |
| 4821 | struct cgroup *src_cgrp, *dst_cgrp; |
| 4822 | struct task_struct *task; |
| 4823 | ssize_t ret; |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 4824 | bool locked; |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4825 | |
| 4826 | dst_cgrp = cgroup_kn_lock_live(of->kn, false); |
| 4827 | if (!dst_cgrp) |
| 4828 | return -ENODEV; |
| 4829 | |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4830 | task = cgroup_procs_write_start(buf, threadgroup, &locked); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4831 | ret = PTR_ERR_OR_ZERO(task); |
| 4832 | if (ret) |
| 4833 | goto out_unlock; |
| 4834 | |
| 4835 | /* find the source cgroup */ |
| 4836 | spin_lock_irq(&css_set_lock); |
| 4837 | src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); |
| 4838 | spin_unlock_irq(&css_set_lock); |
| 4839 | |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4840 | /* process and thread migrations follow same delegation rule */ |
Christian Brauner | 6df970e | 2020-02-05 14:26:18 +0100 | [diff] [blame] | 4841 | ret = cgroup_attach_permissions(src_cgrp, dst_cgrp, |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4842 | of->file->f_path.dentry->d_sb, threadgroup); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4843 | if (ret) |
| 4844 | goto out_finish; |
| 4845 | |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4846 | ret = cgroup_attach_task(dst_cgrp, task, threadgroup); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4847 | |
| 4848 | out_finish: |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 4849 | cgroup_procs_write_finish(task, locked); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4850 | out_unlock: |
| 4851 | cgroup_kn_unlock(of->kn); |
| 4852 | |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4853 | return ret; |
| 4854 | } |
| 4855 | |
| 4856 | static ssize_t cgroup_procs_write(struct kernfs_open_file *of, |
| 4857 | char *buf, size_t nbytes, loff_t off) |
| 4858 | { |
| 4859 | return __cgroup_procs_write(of, buf, true) ?: nbytes; |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4860 | } |
| 4861 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4862 | static void *cgroup_threads_start(struct seq_file *s, loff_t *pos) |
| 4863 | { |
| 4864 | return __cgroup_procs_start(s, pos, 0); |
| 4865 | } |
| 4866 | |
| 4867 | static ssize_t cgroup_threads_write(struct kernfs_open_file *of, |
| 4868 | char *buf, size_t nbytes, loff_t off) |
| 4869 | { |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4870 | return __cgroup_procs_write(of, buf, false) ?: nbytes; |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4871 | } |
| 4872 | |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4873 | /* cgroup core interface files for the default hierarchy */ |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4874 | static struct cftype cgroup_base_files[] = { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4875 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4876 | .name = "cgroup.type", |
| 4877 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4878 | .seq_show = cgroup_type_show, |
| 4879 | .write = cgroup_type_write, |
| 4880 | }, |
| 4881 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4882 | .name = "cgroup.procs", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 4883 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4884 | .file_offset = offsetof(struct cgroup, procs_file), |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4885 | .release = cgroup_procs_release, |
| 4886 | .seq_start = cgroup_procs_start, |
| 4887 | .seq_next = cgroup_procs_next, |
| 4888 | .seq_show = cgroup_procs_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4889 | .write = cgroup_procs_write, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4890 | }, |
| 4891 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4892 | .name = "cgroup.threads", |
Roman Gushchin | 4f58424 | 2018-01-10 04:35:12 -0800 | [diff] [blame] | 4893 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4894 | .release = cgroup_procs_release, |
| 4895 | .seq_start = cgroup_threads_start, |
| 4896 | .seq_next = cgroup_procs_next, |
| 4897 | .seq_show = cgroup_procs_show, |
| 4898 | .write = cgroup_threads_write, |
| 4899 | }, |
| 4900 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4901 | .name = "cgroup.controllers", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4902 | .seq_show = cgroup_controllers_show, |
| 4903 | }, |
| 4904 | { |
| 4905 | .name = "cgroup.subtree_control", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 4906 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4907 | .seq_show = cgroup_subtree_control_show, |
| 4908 | .write = cgroup_subtree_control_write, |
| 4909 | }, |
| 4910 | { |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 4911 | .name = "cgroup.events", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4912 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4913 | .file_offset = offsetof(struct cgroup, events_file), |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 4914 | .seq_show = cgroup_events_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4915 | }, |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 4916 | { |
| 4917 | .name = "cgroup.max.descendants", |
| 4918 | .seq_show = cgroup_max_descendants_show, |
| 4919 | .write = cgroup_max_descendants_write, |
| 4920 | }, |
| 4921 | { |
| 4922 | .name = "cgroup.max.depth", |
| 4923 | .seq_show = cgroup_max_depth_show, |
| 4924 | .write = cgroup_max_depth_write, |
| 4925 | }, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 4926 | { |
| 4927 | .name = "cgroup.stat", |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 4928 | .seq_show = cgroup_stat_show, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 4929 | }, |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 4930 | { |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 4931 | .name = "cgroup.freeze", |
| 4932 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4933 | .seq_show = cgroup_freeze_show, |
| 4934 | .write = cgroup_freeze_write, |
| 4935 | }, |
| 4936 | { |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 4937 | .name = "cgroup.kill", |
| 4938 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4939 | .write = cgroup_kill_write, |
| 4940 | }, |
| 4941 | { |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 4942 | .name = "cpu.stat", |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 4943 | .seq_show = cpu_stat_show, |
| 4944 | }, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4945 | #ifdef CONFIG_PSI |
| 4946 | { |
| 4947 | .name = "io.pressure", |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4948 | .seq_show = cgroup_io_pressure_show, |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 4949 | .write = cgroup_io_pressure_write, |
| 4950 | .poll = cgroup_pressure_poll, |
| 4951 | .release = cgroup_pressure_release, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4952 | }, |
| 4953 | { |
| 4954 | .name = "memory.pressure", |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4955 | .seq_show = cgroup_memory_pressure_show, |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 4956 | .write = cgroup_memory_pressure_write, |
| 4957 | .poll = cgroup_pressure_poll, |
| 4958 | .release = cgroup_pressure_release, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4959 | }, |
| 4960 | { |
| 4961 | .name = "cpu.pressure", |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4962 | .seq_show = cgroup_cpu_pressure_show, |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 4963 | .write = cgroup_cpu_pressure_write, |
| 4964 | .poll = cgroup_pressure_poll, |
| 4965 | .release = cgroup_pressure_release, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4966 | }, |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 4967 | #endif /* CONFIG_PSI */ |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4968 | { } /* terminate */ |
| 4969 | }; |
| 4970 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4971 | /* |
| 4972 | * css destruction is four-stage process. |
| 4973 | * |
| 4974 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 4975 | * Implemented in kill_css(). |
| 4976 | * |
| 4977 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4978 | * and thus css_tryget_online() is guaranteed to fail, the css can be |
| 4979 | * offlined by invoking offline_css(). After offlining, the base ref is |
| 4980 | * put. Implemented in css_killed_work_fn(). |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4981 | * |
| 4982 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 4983 | * accessors are inside RCU read sections. css_release() schedules the |
| 4984 | * RCU callback. |
| 4985 | * |
| 4986 | * 4. After the grace period, the css can be freed. Implemented in |
| 4987 | * css_free_work_fn(). |
| 4988 | * |
| 4989 | * It is actually hairier because both step 2 and 4 require process context |
| 4990 | * and thus involve punting to css->destroy_work adding two additional |
| 4991 | * steps to the already complex sequence. |
| 4992 | */ |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4993 | static void css_free_rwork_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4994 | { |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4995 | struct cgroup_subsys_state *css = container_of(to_rcu_work(work), |
| 4996 | struct cgroup_subsys_state, destroy_rwork); |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4997 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4998 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4999 | |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 5000 | percpu_ref_exit(&css->refcnt); |
| 5001 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 5002 | if (ss) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5003 | /* css free path */ |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 5004 | struct cgroup_subsys_state *parent = css->parent; |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 5005 | int id = css->id; |
| 5006 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 5007 | ss->css_free(css); |
| 5008 | cgroup_idr_remove(&ss->css_idr, id); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5009 | cgroup_put(cgrp); |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 5010 | |
| 5011 | if (parent) |
| 5012 | css_put(parent); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5013 | } else { |
| 5014 | /* cgroup free path */ |
| 5015 | atomic_dec(&cgrp->root->nr_cgrps); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5016 | cgroup1_pidlist_destroy_all(cgrp); |
Zefan Li | 971ff49 | 2014-09-18 16:06:19 +0800 | [diff] [blame] | 5017 | cancel_work_sync(&cgrp->release_agent_work); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5018 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5019 | if (cgroup_parent(cgrp)) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5020 | /* |
| 5021 | * We get a ref to the parent, and put the ref when |
| 5022 | * this cgroup is being freed, so it's guaranteed |
| 5023 | * that the parent won't be destroyed before its |
| 5024 | * children. |
| 5025 | */ |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5026 | cgroup_put(cgroup_parent(cgrp)); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5027 | kernfs_put(cgrp->kn); |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5028 | psi_cgroup_free(cgrp); |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5029 | cgroup_rstat_exit(cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5030 | kfree(cgrp); |
| 5031 | } else { |
| 5032 | /* |
| 5033 | * This is root cgroup's refcnt reaching zero, |
| 5034 | * which indicates that the root should be |
| 5035 | * released. |
| 5036 | */ |
| 5037 | cgroup_destroy_root(cgrp->root); |
| 5038 | } |
| 5039 | } |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5040 | } |
| 5041 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5042 | static void css_release_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5043 | { |
| 5044 | struct cgroup_subsys_state *css = |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5045 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5046 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5047 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5048 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5049 | mutex_lock(&cgroup_mutex); |
| 5050 | |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5051 | css->flags |= CSS_RELEASED; |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5052 | list_del_rcu(&css->sibling); |
| 5053 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5054 | if (ss) { |
| 5055 | /* css release path */ |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 5056 | if (!list_empty(&css->rstat_css_node)) { |
| 5057 | cgroup_rstat_flush(cgrp); |
| 5058 | list_del_rcu(&css->rstat_css_node); |
| 5059 | } |
| 5060 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 5061 | cgroup_idr_replace(&ss->css_idr, NULL, css->id); |
Tejun Heo | 7d172cc | 2014-11-18 02:49:51 -0500 | [diff] [blame] | 5062 | if (ss->css_released) |
| 5063 | ss->css_released(css); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5064 | } else { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5065 | struct cgroup *tcgrp; |
| 5066 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5067 | /* cgroup release path */ |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 5068 | TRACE_CGROUP_PATH(release, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5069 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5070 | cgroup_rstat_flush(cgrp); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5071 | |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5072 | spin_lock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5073 | for (tcgrp = cgroup_parent(cgrp); tcgrp; |
| 5074 | tcgrp = cgroup_parent(tcgrp)) |
| 5075 | tcgrp->nr_dying_descendants--; |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5076 | spin_unlock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5077 | |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 5078 | /* |
| 5079 | * There are two control paths which try to determine |
| 5080 | * cgroup from dentry without going through kernfs - |
| 5081 | * cgroupstats_build() and css_tryget_online_from_dir(). |
| 5082 | * Those are supported by RCU protecting clearing of |
| 5083 | * cgrp->kn->priv backpointer. |
| 5084 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5085 | if (cgrp->kn) |
| 5086 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, |
| 5087 | NULL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5088 | } |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5089 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5090 | mutex_unlock(&cgroup_mutex); |
| 5091 | |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 5092 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 5093 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5094 | } |
| 5095 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5096 | static void css_release(struct percpu_ref *ref) |
| 5097 | { |
| 5098 | struct cgroup_subsys_state *css = |
| 5099 | container_of(ref, struct cgroup_subsys_state, refcnt); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 5100 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5101 | INIT_WORK(&css->destroy_work, css_release_work_fn); |
| 5102 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5103 | } |
| 5104 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5105 | static void init_and_link_css(struct cgroup_subsys_state *css, |
| 5106 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5107 | { |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5108 | lockdep_assert_held(&cgroup_mutex); |
| 5109 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5110 | cgroup_get_live(cgrp); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5111 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5112 | memset(css, 0, sizeof(*css)); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5113 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 5114 | css->ss = ss; |
Tejun Heo | 8fa3b8d | 2016-05-26 15:42:13 -0400 | [diff] [blame] | 5115 | css->id = -1; |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5116 | INIT_LIST_HEAD(&css->sibling); |
| 5117 | INIT_LIST_HEAD(&css->children); |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 5118 | INIT_LIST_HEAD(&css->rstat_css_node); |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5119 | css->serial_nr = css_serial_nr_next++; |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5120 | atomic_set(&css->online_cnt, 0); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5121 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5122 | if (cgroup_parent(cgrp)) { |
| 5123 | css->parent = cgroup_css(cgroup_parent(cgrp), ss); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5124 | css_get(css->parent); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5125 | } |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5126 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5127 | if (ss->css_rstat_flush) |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 5128 | list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list); |
| 5129 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5130 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5131 | } |
| 5132 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 5133 | /* invoke ->css_online() on a new CSS and mark it online if successful */ |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5134 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5135 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5136 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5137 | int ret = 0; |
| 5138 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5139 | lockdep_assert_held(&cgroup_mutex); |
| 5140 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5141 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5142 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5143 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5144 | css->flags |= CSS_ONLINE; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5145 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5146 | |
| 5147 | atomic_inc(&css->online_cnt); |
| 5148 | if (css->parent) |
| 5149 | atomic_inc(&css->parent->online_cnt); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5150 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5151 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5152 | } |
| 5153 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 5154 | /* if the CSS is online, invoke ->css_offline() on it and mark it offline */ |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5155 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5156 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5157 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5158 | |
| 5159 | lockdep_assert_held(&cgroup_mutex); |
| 5160 | |
| 5161 | if (!(css->flags & CSS_ONLINE)) |
| 5162 | return; |
| 5163 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 5164 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5165 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5166 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5167 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | e329780 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 5168 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 5169 | |
| 5170 | wake_up_all(&css->cgroup->offline_waitq); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5171 | } |
| 5172 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5173 | /** |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5174 | * css_create - create a cgroup_subsys_state |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5175 | * @cgrp: the cgroup new css will be associated with |
| 5176 | * @ss: the subsys of new css |
| 5177 | * |
| 5178 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5179 | * css is online and installed in @cgrp. This function doesn't create the |
| 5180 | * interface files. Returns 0 on success, -errno on failure. |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5181 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5182 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 5183 | struct cgroup_subsys *ss) |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5184 | { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5185 | struct cgroup *parent = cgroup_parent(cgrp); |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5186 | struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5187 | struct cgroup_subsys_state *css; |
| 5188 | int err; |
| 5189 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5190 | lockdep_assert_held(&cgroup_mutex); |
| 5191 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5192 | css = ss->css_alloc(parent_css); |
Tejun Heo | e7e15b8 | 2016-06-21 13:06:24 -0400 | [diff] [blame] | 5193 | if (!css) |
| 5194 | css = ERR_PTR(-ENOMEM); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5195 | if (IS_ERR(css)) |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5196 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5197 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5198 | init_and_link_css(css, ss, cgrp); |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5199 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 5200 | err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5201 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 5202 | goto err_free_css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5203 | |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 5204 | err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5205 | if (err < 0) |
Wenwei Tao | b00c52d | 2016-05-13 22:59:20 +0800 | [diff] [blame] | 5206 | goto err_free_css; |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5207 | css->id = err; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5208 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5209 | /* @css is ready to be brought online now, make it visible */ |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5210 | list_add_tail_rcu(&css->sibling, &parent_css->children); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5211 | cgroup_idr_replace(&ss->css_idr, css, css->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5212 | |
| 5213 | err = online_css(css); |
| 5214 | if (err) |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5215 | goto err_list_del; |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5216 | |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5217 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5218 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5219 | err_list_del: |
| 5220 | list_del_rcu(&css->sibling); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 5221 | err_free_css: |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 5222 | list_del_rcu(&css->rstat_css_node); |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 5223 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 5224 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5225 | return ERR_PTR(err); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5226 | } |
| 5227 | |
Tejun Heo | 07cd129 | 2017-01-26 16:47:28 -0500 | [diff] [blame] | 5228 | /* |
| 5229 | * The returned cgroup is fully initialized including its control mask, but |
| 5230 | * it isn't associated with its kernfs_node and doesn't have the control |
| 5231 | * mask applied. |
| 5232 | */ |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5233 | static struct cgroup *cgroup_create(struct cgroup *parent, const char *name, |
| 5234 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5235 | { |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5236 | struct cgroup_root *root = parent->root; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5237 | struct cgroup *cgrp, *tcgrp; |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5238 | struct kernfs_node *kn; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5239 | int level = parent->level + 1; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5240 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5241 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 5242 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Kees Cook | acafe7e | 2018-05-08 13:45:50 -0700 | [diff] [blame] | 5243 | cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)), |
| 5244 | GFP_KERNEL); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5245 | if (!cgrp) |
| 5246 | return ERR_PTR(-ENOMEM); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 5247 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 5248 | ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5249 | if (ret) |
| 5250 | goto out_free_cgrp; |
| 5251 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5252 | ret = cgroup_rstat_init(cgrp); |
| 5253 | if (ret) |
| 5254 | goto out_cancel_ref; |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5255 | |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5256 | /* create the directory */ |
| 5257 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
| 5258 | if (IS_ERR(kn)) { |
| 5259 | ret = PTR_ERR(kn); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5260 | goto out_stat_exit; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5261 | } |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5262 | cgrp->kn = kn; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5263 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 5264 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5265 | |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 5266 | cgrp->self.parent = &parent->self; |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5267 | cgrp->root = root; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5268 | cgrp->level = level; |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5269 | |
| 5270 | ret = psi_cgroup_alloc(cgrp); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 5271 | if (ret) |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5272 | goto out_kernfs_remove; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5273 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5274 | ret = cgroup_bpf_inherit(cgrp); |
| 5275 | if (ret) |
| 5276 | goto out_psi_free; |
| 5277 | |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5278 | /* |
| 5279 | * New cgroup inherits effective freeze counter, and |
| 5280 | * if the parent has to be frozen, the child has too. |
| 5281 | */ |
| 5282 | cgrp->freezer.e_freeze = parent->freezer.e_freeze; |
Roman Gushchin | 97a6136 | 2019-09-12 10:56:45 -0700 | [diff] [blame] | 5283 | if (cgrp->freezer.e_freeze) { |
| 5284 | /* |
| 5285 | * Set the CGRP_FREEZE flag, so when a process will be |
| 5286 | * attached to the child cgroup, it will become frozen. |
| 5287 | * At this point the new cgroup is unpopulated, so we can |
| 5288 | * consider it frozen immediately. |
| 5289 | */ |
| 5290 | set_bit(CGRP_FREEZE, &cgrp->flags); |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5291 | set_bit(CGRP_FROZEN, &cgrp->flags); |
Roman Gushchin | 97a6136 | 2019-09-12 10:56:45 -0700 | [diff] [blame] | 5292 | } |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5293 | |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5294 | spin_lock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5295 | for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5296 | cgrp->ancestor_ids[tcgrp->level] = cgroup_id(tcgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5297 | |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5298 | if (tcgrp != cgrp) { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5299 | tcgrp->nr_descendants++; |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5300 | |
| 5301 | /* |
| 5302 | * If the new cgroup is frozen, all ancestor cgroups |
| 5303 | * get a new frozen descendant, but their state can't |
| 5304 | * change because of this. |
| 5305 | */ |
| 5306 | if (cgrp->freezer.e_freeze) |
| 5307 | tcgrp->freezer.nr_frozen_descendants++; |
| 5308 | } |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5309 | } |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5310 | spin_unlock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5311 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 5312 | if (notify_on_release(parent)) |
| 5313 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 5314 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5315 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 5316 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 5317 | |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5318 | cgrp->self.serial_nr = css_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 5319 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5320 | /* allocation complete, commit to creation */ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5321 | list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5322 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5323 | cgroup_get_live(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 5324 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5325 | /* |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 5326 | * On the default hierarchy, a child doesn't automatically inherit |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 5327 | * subtree_control from the parent. Each is configured manually. |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 5328 | */ |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5329 | if (!cgroup_on_dfl(cgrp)) |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5330 | cgrp->subtree_control = cgroup_control(cgrp); |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5331 | |
| 5332 | cgroup_propagate_control(cgrp); |
| 5333 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5334 | return cgrp; |
| 5335 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5336 | out_psi_free: |
| 5337 | psi_cgroup_free(cgrp); |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5338 | out_kernfs_remove: |
| 5339 | kernfs_remove(cgrp->kn); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5340 | out_stat_exit: |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5341 | cgroup_rstat_exit(cgrp); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5342 | out_cancel_ref: |
| 5343 | percpu_ref_exit(&cgrp->self.refcnt); |
| 5344 | out_free_cgrp: |
| 5345 | kfree(cgrp); |
| 5346 | return ERR_PTR(ret); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5347 | } |
| 5348 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 5349 | static bool cgroup_check_hierarchy_limits(struct cgroup *parent) |
| 5350 | { |
| 5351 | struct cgroup *cgroup; |
| 5352 | int ret = false; |
| 5353 | int level = 1; |
| 5354 | |
| 5355 | lockdep_assert_held(&cgroup_mutex); |
| 5356 | |
| 5357 | for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) { |
| 5358 | if (cgroup->nr_descendants >= cgroup->max_descendants) |
| 5359 | goto fail; |
| 5360 | |
| 5361 | if (level > cgroup->max_depth) |
| 5362 | goto fail; |
| 5363 | |
| 5364 | level++; |
| 5365 | } |
| 5366 | |
| 5367 | ret = true; |
| 5368 | fail: |
| 5369 | return ret; |
| 5370 | } |
| 5371 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5372 | int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode) |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5373 | { |
| 5374 | struct cgroup *parent, *cgrp; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5375 | int ret; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5376 | |
| 5377 | /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */ |
| 5378 | if (strchr(name, '\n')) |
| 5379 | return -EINVAL; |
| 5380 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5381 | parent = cgroup_kn_lock_live(parent_kn, false); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5382 | if (!parent) |
| 5383 | return -ENODEV; |
| 5384 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 5385 | if (!cgroup_check_hierarchy_limits(parent)) { |
| 5386 | ret = -EAGAIN; |
| 5387 | goto out_unlock; |
| 5388 | } |
| 5389 | |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5390 | cgrp = cgroup_create(parent, name, mode); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5391 | if (IS_ERR(cgrp)) { |
| 5392 | ret = PTR_ERR(cgrp); |
| 5393 | goto out_unlock; |
| 5394 | } |
| 5395 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5396 | /* |
| 5397 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 5398 | * that @cgrp->kn is always accessible. |
| 5399 | */ |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5400 | kernfs_get(cgrp->kn); |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5401 | |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5402 | ret = cgroup_kn_set_ugid(cgrp->kn); |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5403 | if (ret) |
| 5404 | goto out_destroy; |
| 5405 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 5406 | ret = css_populate_dir(&cgrp->self); |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5407 | if (ret) |
| 5408 | goto out_destroy; |
| 5409 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5410 | ret = cgroup_apply_control_enable(cgrp); |
| 5411 | if (ret) |
| 5412 | goto out_destroy; |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5413 | |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 5414 | TRACE_CGROUP_PATH(mkdir, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5415 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5416 | /* let's create and online css's */ |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5417 | kernfs_activate(cgrp->kn); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5418 | |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5419 | ret = 0; |
| 5420 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5421 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5422 | out_destroy: |
| 5423 | cgroup_destroy_locked(cgrp); |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5424 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5425 | cgroup_kn_unlock(parent_kn); |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 5426 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5427 | } |
| 5428 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5429 | /* |
| 5430 | * This is called when the refcnt of a css is confirmed to be killed. |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5431 | * css_tryget_online() is now guaranteed to fail. Tell the subsystem to |
| 5432 | * initate destruction and put the css ref from kill_css(). |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5433 | */ |
| 5434 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5435 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5436 | struct cgroup_subsys_state *css = |
| 5437 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5438 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5439 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5440 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5441 | do { |
| 5442 | offline_css(css); |
| 5443 | css_put(css); |
| 5444 | /* @css can't go away while we're holding cgroup_mutex */ |
| 5445 | css = css->parent; |
| 5446 | } while (css && atomic_dec_and_test(&css->online_cnt)); |
| 5447 | |
| 5448 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5449 | } |
| 5450 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5451 | /* css kill confirmation processing requires process context, bounce */ |
| 5452 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5453 | { |
| 5454 | struct cgroup_subsys_state *css = |
| 5455 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 5456 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5457 | if (atomic_dec_and_test(&css->online_cnt)) { |
| 5458 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
| 5459 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
| 5460 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5461 | } |
| 5462 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5463 | /** |
| 5464 | * kill_css - destroy a css |
| 5465 | * @css: css to destroy |
| 5466 | * |
| 5467 | * This function initiates destruction of @css by removing cgroup interface |
| 5468 | * files and putting its base reference. ->css_offline() will be invoked |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5469 | * asynchronously once css_tryget_online() is guaranteed to fail and when |
| 5470 | * the reference count reaches zero, @css will be released. |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5471 | */ |
| 5472 | static void kill_css(struct cgroup_subsys_state *css) |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5473 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5474 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5475 | |
Waiman Long | 33c35aa | 2017-05-15 09:34:06 -0400 | [diff] [blame] | 5476 | if (css->flags & CSS_DYING) |
| 5477 | return; |
| 5478 | |
| 5479 | css->flags |= CSS_DYING; |
| 5480 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5481 | /* |
| 5482 | * This must happen before css is disassociated with its cgroup. |
| 5483 | * See seq_css() for details. |
| 5484 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 5485 | css_clear_dir(css); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5486 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5487 | /* |
| 5488 | * Killing would put the base ref, but we need to keep it alive |
| 5489 | * until after ->css_offline(). |
| 5490 | */ |
| 5491 | css_get(css); |
| 5492 | |
| 5493 | /* |
| 5494 | * cgroup core guarantees that, by the time ->css_offline() is |
| 5495 | * invoked, no new css reference will be given out via |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5496 | * css_tryget_online(). We can't simply call percpu_ref_kill() and |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5497 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 5498 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 5499 | * |
| 5500 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 5501 | * css is confirmed to be seen as killed on all CPUs. |
| 5502 | */ |
| 5503 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | /** |
| 5507 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 5508 | * @cgrp: cgroup to be destroyed |
| 5509 | * |
| 5510 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 5511 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5512 | * guarantee that css_tryget_online() won't succeed by the time |
| 5513 | * ->css_offline() is invoked. To satisfy all the requirements, |
| 5514 | * destruction is implemented in the following two steps. |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5515 | * |
| 5516 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 5517 | * userland visible parts and start killing the percpu refcnts of |
| 5518 | * css's. Set up so that the next stage will be kicked off once all |
| 5519 | * the percpu refcnts are confirmed to be killed. |
| 5520 | * |
| 5521 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 5522 | * rest of destruction. Once all cgroup references are gone, the |
| 5523 | * cgroup is RCU-freed. |
| 5524 | * |
| 5525 | * This function implements s1. After this step, @cgrp is gone as far as |
| 5526 | * the userland is concerned and a new cgroup with the same name may be |
| 5527 | * created. As cgroup doesn't care about the names internally, this |
| 5528 | * doesn't cause any problem. |
| 5529 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5530 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 5531 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5532 | { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5533 | struct cgroup *tcgrp, *parent = cgroup_parent(cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5534 | struct cgroup_subsys_state *css; |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5535 | struct cgrp_cset_link *link; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5536 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5537 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5538 | lockdep_assert_held(&cgroup_mutex); |
| 5539 | |
Tejun Heo | 91486f6 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 5540 | /* |
| 5541 | * Only migration can raise populated from zero and we're already |
| 5542 | * holding cgroup_mutex. |
| 5543 | */ |
| 5544 | if (cgroup_is_populated(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5545 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5546 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5547 | /* |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5548 | * Make sure there's no live children. We can't test emptiness of |
| 5549 | * ->self.children as dead children linger on it while being |
| 5550 | * drained; otherwise, "rmdir parent/child parent" may fail. |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5551 | */ |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 5552 | if (css_has_online_children(&cgrp->self)) |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5553 | return -EBUSY; |
| 5554 | |
| 5555 | /* |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5556 | * Mark @cgrp and the associated csets dead. The former prevents |
| 5557 | * further task migration and child creation by disabling |
| 5558 | * cgroup_lock_live_group(). The latter makes the csets ignored by |
| 5559 | * the migration path. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5560 | */ |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 5561 | cgrp->self.flags &= ~CSS_ONLINE; |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5562 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5563 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5564 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 5565 | link->cset->dead = true; |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5566 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5567 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5568 | /* initiate massacre of all css's */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5569 | for_each_css(css, ssid, cgrp) |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5570 | kill_css(css); |
| 5571 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5572 | /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ |
| 5573 | css_clear_dir(&cgrp->self); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5574 | kernfs_remove(cgrp->kn); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5575 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 5576 | if (parent && cgroup_is_threaded(cgrp)) |
| 5577 | parent->nr_threaded_children--; |
| 5578 | |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5579 | spin_lock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5580 | for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
| 5581 | tcgrp->nr_descendants--; |
| 5582 | tcgrp->nr_dying_descendants++; |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5583 | /* |
| 5584 | * If the dying cgroup is frozen, decrease frozen descendants |
| 5585 | * counters of ancestor cgroups. |
| 5586 | */ |
| 5587 | if (test_bit(CGRP_FROZEN, &cgrp->flags)) |
| 5588 | tcgrp->freezer.nr_frozen_descendants--; |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5589 | } |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5590 | spin_unlock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5591 | |
Roman Gushchin | 5a621e6 | 2017-08-02 17:55:32 +0100 | [diff] [blame] | 5592 | cgroup1_check_for_release(parent); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5593 | |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 5594 | cgroup_bpf_offline(cgrp); |
| 5595 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5596 | /* put the base reference */ |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5597 | percpu_ref_kill(&cgrp->self.refcnt); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5598 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 5599 | return 0; |
| 5600 | }; |
| 5601 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5602 | int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5603 | { |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5604 | struct cgroup *cgrp; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5605 | int ret = 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5606 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5607 | cgrp = cgroup_kn_lock_live(kn, false); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5608 | if (!cgrp) |
| 5609 | return 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5610 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5611 | ret = cgroup_destroy_locked(cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5612 | if (!ret) |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 5613 | TRACE_CGROUP_PATH(rmdir, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5614 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5615 | cgroup_kn_unlock(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5616 | return ret; |
| 5617 | } |
| 5618 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5619 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 5620 | .show_options = cgroup_show_options, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5621 | .mkdir = cgroup_mkdir, |
| 5622 | .rmdir = cgroup_rmdir, |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 5623 | .show_path = cgroup_show_path, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5624 | }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5625 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5626 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5627 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5628 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 5629 | |
Tejun Heo | a5ae989 | 2015-12-29 14:53:56 -0500 | [diff] [blame] | 5630 | pr_debug("Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5631 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5632 | mutex_lock(&cgroup_mutex); |
| 5633 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5634 | idr_init(&ss->css_idr); |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 5635 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5636 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5637 | /* Create the root cgroup state for this subsystem */ |
| 5638 | ss->root = &cgrp_dfl_root; |
| 5639 | css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5640 | /* We don't handle early failures gracefully */ |
| 5641 | BUG_ON(IS_ERR(css)); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5642 | init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5643 | |
| 5644 | /* |
| 5645 | * Root csses are never destroyed and we can't initialize |
| 5646 | * percpu_ref during early init. Disable refcnting. |
| 5647 | */ |
| 5648 | css->flags |= CSS_NO_REF; |
| 5649 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5650 | if (early) { |
Tejun Heo | 9395a45 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5651 | /* allocation can't be done safely during early init */ |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5652 | css->id = 1; |
| 5653 | } else { |
| 5654 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); |
| 5655 | BUG_ON(css->id < 0); |
| 5656 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5657 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5658 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5659 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5660 | * newly registered, all tasks and hence the |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5661 | * init_css_set is in the subsystem's root cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5662 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5663 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 5664 | have_fork_callback |= (bool)ss->fork << ss->id; |
| 5665 | have_exit_callback |= (bool)ss->exit << ss->id; |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 5666 | have_release_callback |= (bool)ss->release << ss->id; |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5667 | have_canfork_callback |= (bool)ss->can_fork << ss->id; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5668 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5669 | /* At system boot, before all subsystems have been |
| 5670 | * registered, no tasks have been forked, so we don't |
| 5671 | * need to invoke fork callbacks here. */ |
| 5672 | BUG_ON(!list_empty(&init_task.tasks)); |
| 5673 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5674 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 5675 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5676 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5677 | } |
| 5678 | |
| 5679 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5680 | * cgroup_init_early - cgroup initialization at system boot |
| 5681 | * |
| 5682 | * Initialize cgroups at system boot, and initialize any |
| 5683 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5684 | */ |
| 5685 | int __init cgroup_init_early(void) |
| 5686 | { |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 5687 | static struct cgroup_fs_context __initdata ctx; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5688 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5689 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5690 | |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 5691 | ctx.root = &cgrp_dfl_root; |
| 5692 | init_cgroup_root(&ctx); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5693 | cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; |
| 5694 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5695 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5696 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5697 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5698 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Xiubo Li | 63253ad | 2016-02-26 13:07:38 +0800 | [diff] [blame] | 5699 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n", |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5700 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5701 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5702 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 5703 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5704 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5705 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5706 | ss->name = cgroup_subsys_name[i]; |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5707 | if (!ss->legacy_name) |
| 5708 | ss->legacy_name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5709 | |
| 5710 | if (ss->early_init) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5711 | cgroup_init_subsys(ss, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5712 | } |
| 5713 | return 0; |
| 5714 | } |
| 5715 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 5716 | static u16 cgroup_disable_mask __initdata; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5717 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5718 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5719 | * cgroup_init - cgroup initialization |
| 5720 | * |
| 5721 | * Register cgroup filesystem and /proc file, and initialize |
| 5722 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5723 | */ |
| 5724 | int __init cgroup_init(void) |
| 5725 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5726 | struct cgroup_subsys *ss; |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5727 | int ssid; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5728 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 5729 | BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5730 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
| 5731 | BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5732 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5733 | cgroup_rstat_boot(); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5734 | |
Peter Zijlstra | 3942a9b | 2016-08-11 18:54:13 +0200 | [diff] [blame] | 5735 | /* |
Paul E. McKenney | 2af3024 | 2018-11-07 14:11:40 -0800 | [diff] [blame] | 5736 | * The latency of the synchronize_rcu() is too high for cgroups, |
Peter Zijlstra | 3942a9b | 2016-08-11 18:54:13 +0200 | [diff] [blame] | 5737 | * avoid it at the cost of forcing all readers into the slow path. |
| 5738 | */ |
| 5739 | rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss); |
| 5740 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 5741 | get_user_ns(init_cgroup_ns.user_ns); |
| 5742 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5743 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5744 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5745 | /* |
| 5746 | * Add init_css_set to the hash table so that dfl_root can link to |
| 5747 | * it during init. |
| 5748 | */ |
| 5749 | hash_add(css_set_table, &init_css_set.hlist, |
| 5750 | css_set_hash(init_css_set.subsys)); |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5751 | |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 5752 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5753 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5754 | mutex_unlock(&cgroup_mutex); |
| 5755 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5756 | for_each_subsys(ss, ssid) { |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5757 | if (ss->early_init) { |
| 5758 | struct cgroup_subsys_state *css = |
| 5759 | init_css_set.subsys[ss->id]; |
| 5760 | |
| 5761 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, |
| 5762 | GFP_KERNEL); |
| 5763 | BUG_ON(css->id < 0); |
| 5764 | } else { |
| 5765 | cgroup_init_subsys(ss, false); |
| 5766 | } |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5767 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 5768 | list_add_tail(&init_css_set.e_cset_node[ssid], |
| 5769 | &cgrp_dfl_root.cgrp.e_csets[ssid]); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5770 | |
| 5771 | /* |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5772 | * Setting dfl_root subsys_mask needs to consider the |
| 5773 | * disabled flag and cftype registration needs kmalloc, |
| 5774 | * both of which aren't available during early_init. |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5775 | */ |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5776 | if (cgroup_disable_mask & (1 << ssid)) { |
| 5777 | static_branch_disable(cgroup_subsys_enabled_key[ssid]); |
| 5778 | printk(KERN_INFO "Disabling %s control group subsystem\n", |
| 5779 | ss->name); |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5780 | continue; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5781 | } |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5782 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5783 | if (cgroup1_ssid_disabled(ssid)) |
Johannes Weiner | 223ffb2 | 2016-02-11 13:34:49 -0500 | [diff] [blame] | 5784 | printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n", |
| 5785 | ss->name); |
| 5786 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5787 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; |
| 5788 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5789 | /* implicit controllers must be threaded too */ |
| 5790 | WARN_ON(ss->implicit_on_dfl && !ss->threaded); |
| 5791 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 5792 | if (ss->implicit_on_dfl) |
| 5793 | cgrp_dfl_implicit_ss_mask |= 1 << ss->id; |
| 5794 | else if (!ss->dfl_cftypes) |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 5795 | cgrp_dfl_inhibit_ss_mask |= 1 << ss->id; |
Tejun Heo | 5de4fa1 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5796 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5797 | if (ss->threaded) |
| 5798 | cgrp_dfl_threaded_ss_mask |= 1 << ss->id; |
| 5799 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5800 | if (ss->dfl_cftypes == ss->legacy_cftypes) { |
| 5801 | WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes)); |
| 5802 | } else { |
| 5803 | WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes)); |
| 5804 | WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes)); |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5805 | } |
Vladimir Davydov | 295458e | 2015-02-19 17:34:46 +0300 | [diff] [blame] | 5806 | |
| 5807 | if (ss->bind) |
| 5808 | ss->bind(init_css_set.subsys[ssid]); |
Tejun Heo | 7af608e | 2017-07-18 17:57:46 -0400 | [diff] [blame] | 5809 | |
| 5810 | mutex_lock(&cgroup_mutex); |
| 5811 | css_populate_dir(init_css_set.subsys[ssid]); |
| 5812 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5813 | } |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5814 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5815 | /* init_css_set.subsys[] has been updated, re-hash */ |
| 5816 | hash_del(&init_css_set.hlist); |
| 5817 | hash_add(css_set_table, &init_css_set.hlist, |
| 5818 | css_set_hash(init_css_set.subsys)); |
| 5819 | |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5820 | WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup")); |
| 5821 | WARN_ON(register_filesystem(&cgroup_fs_type)); |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 5822 | WARN_ON(register_filesystem(&cgroup2_fs_type)); |
Christoph Hellwig | 3f3942a | 2018-05-15 15:57:23 +0200 | [diff] [blame] | 5823 | WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show)); |
Al Viro | d5f68d3 | 2019-05-13 12:33:22 -0400 | [diff] [blame] | 5824 | #ifdef CONFIG_CPUSETS |
| 5825 | WARN_ON(register_filesystem(&cpuset_fs_type)); |
| 5826 | #endif |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5827 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5828 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5829 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5830 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5831 | static int __init cgroup_wq_init(void) |
| 5832 | { |
| 5833 | /* |
| 5834 | * There isn't much point in executing destruction path in |
| 5835 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5836 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5837 | * |
| 5838 | * We would prefer to do this in cgroup_init() above, but that |
| 5839 | * is called before init_workqueues(): so leave this until after. |
| 5840 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5841 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5842 | BUG_ON(!cgroup_destroy_wq); |
| 5843 | return 0; |
| 5844 | } |
| 5845 | core_initcall(cgroup_wq_init); |
| 5846 | |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5847 | void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen) |
Shaohua Li | 69fd5c3 | 2017-07-12 11:49:55 -0700 | [diff] [blame] | 5848 | { |
| 5849 | struct kernfs_node *kn; |
| 5850 | |
Tejun Heo | fe0f726 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5851 | kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id); |
Shaohua Li | 69fd5c3 | 2017-07-12 11:49:55 -0700 | [diff] [blame] | 5852 | if (!kn) |
| 5853 | return; |
| 5854 | kernfs_path(kn, buf, buflen); |
| 5855 | kernfs_put(kn); |
| 5856 | } |
| 5857 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5858 | /* |
| 5859 | * proc_cgroup_show() |
| 5860 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 5861 | * - Used for /proc/<pid>/cgroup. |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5862 | */ |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 5863 | int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, |
| 5864 | struct pid *pid, struct task_struct *tsk) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5865 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5866 | char *buf; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5867 | int retval; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5868 | struct cgroup_root *root; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5869 | |
| 5870 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5871 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5872 | if (!buf) |
| 5873 | goto out; |
| 5874 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5875 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5876 | spin_lock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5877 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5878 | for_each_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5879 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5880 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 5881 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5882 | |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 5883 | if (root == &cgrp_dfl_root && !cgrp_dfl_visible) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5884 | continue; |
| 5885 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5886 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | d98817d | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5887 | if (root != &cgrp_dfl_root) |
| 5888 | for_each_subsys(ss, ssid) |
| 5889 | if (root->subsys_mask & (1 << ssid)) |
| 5890 | seq_printf(m, "%s%s", count++ ? "," : "", |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5891 | ss->legacy_name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 5892 | if (strlen(root->name)) |
| 5893 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 5894 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5895 | seq_putc(m, ':'); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5896 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5897 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5898 | |
| 5899 | /* |
| 5900 | * On traditional hierarchies, all zombie tasks show up as |
| 5901 | * belonging to the root cgroup. On the default hierarchy, |
| 5902 | * while a zombie doesn't show up in "cgroup.procs" and |
| 5903 | * thus can't be migrated, its /proc/PID/cgroup keeps |
| 5904 | * reporting the cgroup it belonged to before exiting. If |
| 5905 | * the cgroup is removed before the zombie is reaped, |
| 5906 | * " (deleted)" is appended to the cgroup path. |
| 5907 | */ |
| 5908 | if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5909 | retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX, |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 5910 | current->nsproxy->cgroup_ns); |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 5911 | if (retval >= PATH_MAX) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5912 | retval = -ENAMETOOLONG; |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 5913 | if (retval < 0) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5914 | goto out_unlock; |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5915 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5916 | seq_puts(m, buf); |
| 5917 | } else { |
| 5918 | seq_puts(m, "/"); |
| 5919 | } |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5920 | |
| 5921 | if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp)) |
| 5922 | seq_puts(m, " (deleted)\n"); |
| 5923 | else |
| 5924 | seq_putc(m, '\n'); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5925 | } |
| 5926 | |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 5927 | retval = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5928 | out_unlock: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5929 | spin_unlock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5930 | mutex_unlock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5931 | kfree(buf); |
| 5932 | out: |
| 5933 | return retval; |
| 5934 | } |
| 5935 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5936 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5937 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5938 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5939 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5940 | * A task is associated with the init_css_set until cgroup_post_fork() |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 5941 | * attaches it to the target css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5942 | */ |
| 5943 | void cgroup_fork(struct task_struct *child) |
| 5944 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5945 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5946 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5947 | } |
| 5948 | |
Christian Brauner | 1770309 | 2020-02-05 14:26:19 +0100 | [diff] [blame] | 5949 | static struct cgroup *cgroup_get_from_file(struct file *f) |
| 5950 | { |
| 5951 | struct cgroup_subsys_state *css; |
| 5952 | struct cgroup *cgrp; |
| 5953 | |
| 5954 | css = css_tryget_online_from_dir(f->f_path.dentry, NULL); |
| 5955 | if (IS_ERR(css)) |
| 5956 | return ERR_CAST(css); |
| 5957 | |
| 5958 | cgrp = css->cgroup; |
| 5959 | if (!cgroup_on_dfl(cgrp)) { |
| 5960 | cgroup_put(cgrp); |
| 5961 | return ERR_PTR(-EBADF); |
| 5962 | } |
| 5963 | |
| 5964 | return cgrp; |
| 5965 | } |
| 5966 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5967 | /** |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 5968 | * cgroup_css_set_fork - find or create a css_set for a child process |
| 5969 | * @kargs: the arguments passed to create the child process |
| 5970 | * |
| 5971 | * This functions finds or creates a new css_set which the child |
| 5972 | * process will be attached to in cgroup_post_fork(). By default, |
| 5973 | * the child process will be given the same css_set as its parent. |
| 5974 | * |
| 5975 | * If CLONE_INTO_CGROUP is specified this function will try to find an |
| 5976 | * existing css_set which includes the requested cgroup and if not create |
| 5977 | * a new css_set that the child will be attached to later. If this function |
| 5978 | * succeeds it will hold cgroup_threadgroup_rwsem on return. If |
| 5979 | * CLONE_INTO_CGROUP is requested this function will grab cgroup mutex |
| 5980 | * before grabbing cgroup_threadgroup_rwsem and will hold a reference |
| 5981 | * to the target cgroup. |
| 5982 | */ |
| 5983 | static int cgroup_css_set_fork(struct kernel_clone_args *kargs) |
| 5984 | __acquires(&cgroup_mutex) __acquires(&cgroup_threadgroup_rwsem) |
| 5985 | { |
| 5986 | int ret; |
| 5987 | struct cgroup *dst_cgrp = NULL; |
| 5988 | struct css_set *cset; |
| 5989 | struct super_block *sb; |
| 5990 | struct file *f; |
| 5991 | |
| 5992 | if (kargs->flags & CLONE_INTO_CGROUP) |
| 5993 | mutex_lock(&cgroup_mutex); |
| 5994 | |
| 5995 | cgroup_threadgroup_change_begin(current); |
| 5996 | |
| 5997 | spin_lock_irq(&css_set_lock); |
| 5998 | cset = task_css_set(current); |
| 5999 | get_css_set(cset); |
| 6000 | spin_unlock_irq(&css_set_lock); |
| 6001 | |
| 6002 | if (!(kargs->flags & CLONE_INTO_CGROUP)) { |
| 6003 | kargs->cset = cset; |
| 6004 | return 0; |
| 6005 | } |
| 6006 | |
| 6007 | f = fget_raw(kargs->cgroup); |
| 6008 | if (!f) { |
| 6009 | ret = -EBADF; |
| 6010 | goto err; |
| 6011 | } |
| 6012 | sb = f->f_path.dentry->d_sb; |
| 6013 | |
| 6014 | dst_cgrp = cgroup_get_from_file(f); |
| 6015 | if (IS_ERR(dst_cgrp)) { |
| 6016 | ret = PTR_ERR(dst_cgrp); |
| 6017 | dst_cgrp = NULL; |
| 6018 | goto err; |
| 6019 | } |
| 6020 | |
| 6021 | if (cgroup_is_dead(dst_cgrp)) { |
| 6022 | ret = -ENODEV; |
| 6023 | goto err; |
| 6024 | } |
| 6025 | |
| 6026 | /* |
| 6027 | * Verify that we the target cgroup is writable for us. This is |
| 6028 | * usually done by the vfs layer but since we're not going through |
| 6029 | * the vfs layer here we need to do it "manually". |
| 6030 | */ |
| 6031 | ret = cgroup_may_write(dst_cgrp, sb); |
| 6032 | if (ret) |
| 6033 | goto err; |
| 6034 | |
| 6035 | ret = cgroup_attach_permissions(cset->dfl_cgrp, dst_cgrp, sb, |
| 6036 | !(kargs->flags & CLONE_THREAD)); |
| 6037 | if (ret) |
| 6038 | goto err; |
| 6039 | |
| 6040 | kargs->cset = find_css_set(cset, dst_cgrp); |
| 6041 | if (!kargs->cset) { |
| 6042 | ret = -ENOMEM; |
| 6043 | goto err; |
| 6044 | } |
| 6045 | |
| 6046 | put_css_set(cset); |
| 6047 | fput(f); |
| 6048 | kargs->cgrp = dst_cgrp; |
| 6049 | return ret; |
| 6050 | |
| 6051 | err: |
| 6052 | cgroup_threadgroup_change_end(current); |
| 6053 | mutex_unlock(&cgroup_mutex); |
| 6054 | if (f) |
| 6055 | fput(f); |
| 6056 | if (dst_cgrp) |
| 6057 | cgroup_put(dst_cgrp); |
| 6058 | put_css_set(cset); |
| 6059 | if (kargs->cset) |
| 6060 | put_css_set(kargs->cset); |
| 6061 | return ret; |
| 6062 | } |
| 6063 | |
| 6064 | /** |
| 6065 | * cgroup_css_set_put_fork - drop references we took during fork |
| 6066 | * @kargs: the arguments passed to create the child process |
| 6067 | * |
| 6068 | * Drop references to the prepared css_set and target cgroup if |
| 6069 | * CLONE_INTO_CGROUP was requested. |
| 6070 | */ |
| 6071 | static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs) |
| 6072 | __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex) |
| 6073 | { |
| 6074 | cgroup_threadgroup_change_end(current); |
| 6075 | |
| 6076 | if (kargs->flags & CLONE_INTO_CGROUP) { |
| 6077 | struct cgroup *cgrp = kargs->cgrp; |
| 6078 | struct css_set *cset = kargs->cset; |
| 6079 | |
| 6080 | mutex_unlock(&cgroup_mutex); |
| 6081 | |
| 6082 | if (cset) { |
| 6083 | put_css_set(cset); |
| 6084 | kargs->cset = NULL; |
| 6085 | } |
| 6086 | |
| 6087 | if (cgrp) { |
| 6088 | cgroup_put(cgrp); |
| 6089 | kargs->cgrp = NULL; |
| 6090 | } |
| 6091 | } |
| 6092 | } |
| 6093 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6094 | /** |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6095 | * cgroup_can_fork - called on a new task before the process is exposed |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6096 | * @child: the child process |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6097 | * |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6098 | * This prepares a new css_set for the child process which the child will |
| 6099 | * be attached to in cgroup_post_fork(). |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6100 | * This calls the subsystem can_fork() callbacks. If the cgroup_can_fork() |
| 6101 | * callback returns an error, the fork aborts with that error code. This |
| 6102 | * allows for a cgroup subsystem to conditionally allow or deny new forks. |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6103 | */ |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6104 | int cgroup_can_fork(struct task_struct *child, struct kernel_clone_args *kargs) |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6105 | { |
| 6106 | struct cgroup_subsys *ss; |
| 6107 | int i, j, ret; |
| 6108 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6109 | ret = cgroup_css_set_fork(kargs); |
| 6110 | if (ret) |
| 6111 | return ret; |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6112 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6113 | do_each_subsys_mask(ss, i, have_canfork_callback) { |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6114 | ret = ss->can_fork(child, kargs->cset); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6115 | if (ret) |
| 6116 | goto out_revert; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6117 | } while_each_subsys_mask(); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6118 | |
| 6119 | return 0; |
| 6120 | |
| 6121 | out_revert: |
| 6122 | for_each_subsys(ss, j) { |
| 6123 | if (j >= i) |
| 6124 | break; |
| 6125 | if (ss->cancel_fork) |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6126 | ss->cancel_fork(child, kargs->cset); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6127 | } |
| 6128 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6129 | cgroup_css_set_put_fork(kargs); |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6130 | |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6131 | return ret; |
| 6132 | } |
| 6133 | |
| 6134 | /** |
| 6135 | * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6136 | * @child: the child process |
| 6137 | * @kargs: the arguments passed to create the child process |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6138 | * |
| 6139 | * This calls the cancel_fork() callbacks if a fork failed *after* |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6140 | * cgroup_can_fork() succeded and cleans up references we took to |
| 6141 | * prepare a new css_set for the child process in cgroup_can_fork(). |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6142 | */ |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6143 | void cgroup_cancel_fork(struct task_struct *child, |
| 6144 | struct kernel_clone_args *kargs) |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6145 | { |
| 6146 | struct cgroup_subsys *ss; |
| 6147 | int i; |
| 6148 | |
| 6149 | for_each_subsys(ss, i) |
| 6150 | if (ss->cancel_fork) |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6151 | ss->cancel_fork(child, kargs->cset); |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6152 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6153 | cgroup_css_set_put_fork(kargs); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6154 | } |
| 6155 | |
| 6156 | /** |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6157 | * cgroup_post_fork - finalize cgroup setup for the child process |
| 6158 | * @child: the child process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 6159 | * |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6160 | * Attach the child process to its css_set calling the subsystem fork() |
| 6161 | * callbacks. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 6162 | */ |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6163 | void cgroup_post_fork(struct task_struct *child, |
| 6164 | struct kernel_clone_args *kargs) |
| 6165 | __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6166 | { |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6167 | unsigned long cgrp_flags = 0; |
| 6168 | bool kill = false; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6169 | struct cgroup_subsys *ss; |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6170 | struct css_set *cset; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 6171 | int i; |
| 6172 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6173 | cset = kargs->cset; |
| 6174 | kargs->cset = NULL; |
| 6175 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6176 | spin_lock_irq(&css_set_lock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 6177 | |
Tejun Heo | 0cd9d33 | 2020-01-30 11:37:33 -0500 | [diff] [blame] | 6178 | /* init tasks are special, only link regular threads */ |
| 6179 | if (likely(child->pid)) { |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6180 | if (kargs->cgrp) |
| 6181 | cgrp_flags = kargs->cgrp->flags; |
| 6182 | else |
| 6183 | cgrp_flags = cset->dfl_cgrp->flags; |
| 6184 | |
Tejun Heo | 0cd9d33 | 2020-01-30 11:37:33 -0500 | [diff] [blame] | 6185 | WARN_ON_ONCE(!list_empty(&child->cg_list)); |
Tejun Heo | 0cd9d33 | 2020-01-30 11:37:33 -0500 | [diff] [blame] | 6186 | cset->nr_tasks++; |
| 6187 | css_set_move_task(child, NULL, cset, false); |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6188 | } else { |
| 6189 | put_css_set(cset); |
| 6190 | cset = NULL; |
Tejun Heo | 0cd9d33 | 2020-01-30 11:37:33 -0500 | [diff] [blame] | 6191 | } |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6192 | |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6193 | if (!(child->flags & PF_KTHREAD)) { |
| 6194 | if (unlikely(test_bit(CGRP_FREEZE, &cgrp_flags))) { |
| 6195 | /* |
| 6196 | * If the cgroup has to be frozen, the new task has |
| 6197 | * too. Let's set the JOBCTL_TRAP_FREEZE jobctl bit to |
| 6198 | * get the task into the frozen state. |
| 6199 | */ |
| 6200 | spin_lock(&child->sighand->siglock); |
| 6201 | WARN_ON_ONCE(child->frozen); |
| 6202 | child->jobctl |= JOBCTL_TRAP_FREEZE; |
| 6203 | spin_unlock(&child->sighand->siglock); |
| 6204 | |
| 6205 | /* |
| 6206 | * Calling cgroup_update_frozen() isn't required here, |
| 6207 | * because it will be called anyway a bit later from |
| 6208 | * do_freezer_trap(). So we avoid cgroup's transient |
| 6209 | * switch from the frozen state and back. |
| 6210 | */ |
| 6211 | } |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 6212 | |
| 6213 | /* |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6214 | * If the cgroup is to be killed notice it now and take the |
| 6215 | * child down right after we finished preparing it for |
| 6216 | * userspace. |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 6217 | */ |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6218 | kill = test_bit(CGRP_KILL, &cgrp_flags); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6219 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 6220 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6221 | spin_unlock_irq(&css_set_lock); |
| 6222 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 6223 | /* |
| 6224 | * Call ss->fork(). This must happen after @child is linked on |
| 6225 | * css_set; otherwise, @child might change state between ->fork() |
| 6226 | * and addition to css_set. |
| 6227 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6228 | do_each_subsys_mask(ss, i, have_fork_callback) { |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 6229 | ss->fork(child); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6230 | } while_each_subsys_mask(); |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6231 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6232 | /* Make the new cset the root_cset of the new cgroup namespace. */ |
| 6233 | if (kargs->flags & CLONE_NEWCGROUP) { |
| 6234 | struct css_set *rcset = child->nsproxy->cgroup_ns->root_cset; |
| 6235 | |
| 6236 | get_css_set(cset); |
| 6237 | child->nsproxy->cgroup_ns->root_cset = cset; |
| 6238 | put_css_set(rcset); |
| 6239 | } |
| 6240 | |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6241 | /* Cgroup has to be killed so take down child immediately. */ |
| 6242 | if (unlikely(kill)) |
| 6243 | do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, child, PIDTYPE_TGID); |
| 6244 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6245 | cgroup_css_set_put_fork(kargs); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6246 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 6247 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6248 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6249 | * cgroup_exit - detach cgroup from exiting task |
| 6250 | * @tsk: pointer to task_struct of exiting process |
| 6251 | * |
Michal Koutný | e7c7b1d | 2019-10-04 12:57:39 +0200 | [diff] [blame] | 6252 | * Description: Detach cgroup from @tsk. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6253 | * |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6254 | */ |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 6255 | void cgroup_exit(struct task_struct *tsk) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6256 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6257 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 6258 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 6259 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6260 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6261 | spin_lock_irq(&css_set_lock); |
| 6262 | |
| 6263 | WARN_ON_ONCE(list_empty(&tsk->cg_list)); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 6264 | cset = task_css_set(tsk); |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6265 | css_set_move_task(tsk, cset, NULL, false); |
| 6266 | list_add_tail(&tsk->cg_list, &cset->dying_tasks); |
| 6267 | cset->nr_tasks--; |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 6268 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6269 | WARN_ON_ONCE(cgroup_task_frozen(tsk)); |
Roman Gushchin | f4f809f | 2021-05-10 14:39:46 -0700 | [diff] [blame^] | 6270 | if (unlikely(!(tsk->flags & PF_KTHREAD) && |
| 6271 | test_bit(CGRP_FREEZE, &task_dfl_cgroup(tsk)->flags))) |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6272 | cgroup_update_frozen(task_dfl_cgroup(tsk)); |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 6273 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6274 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6275 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 6276 | /* see cgroup_post_fork() for details */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6277 | do_each_subsys_mask(ss, i, have_exit_callback) { |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6278 | ss->exit(tsk); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6279 | } while_each_subsys_mask(); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6280 | } |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6281 | |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 6282 | void cgroup_release(struct task_struct *task) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6283 | { |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6284 | struct cgroup_subsys *ss; |
| 6285 | int ssid; |
| 6286 | |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 6287 | do_each_subsys_mask(ss, ssid, have_release_callback) { |
| 6288 | ss->release(task); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6289 | } while_each_subsys_mask(); |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 6290 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6291 | spin_lock_irq(&css_set_lock); |
| 6292 | css_set_skip_task_iters(task_css_set(task), task); |
| 6293 | list_del_init(&task->cg_list); |
| 6294 | spin_unlock_irq(&css_set_lock); |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 6295 | } |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 6296 | |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 6297 | void cgroup_free(struct task_struct *task) |
| 6298 | { |
| 6299 | struct css_set *cset = task_css_set(task); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6300 | put_css_set(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6301 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 6302 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6303 | static int __init cgroup_disable(char *str) |
| 6304 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6305 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6306 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6307 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6308 | |
| 6309 | while ((token = strsep(&str, ",")) != NULL) { |
| 6310 | if (!*token) |
| 6311 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6312 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 6313 | for_each_subsys(ss, i) { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 6314 | if (strcmp(token, ss->name) && |
| 6315 | strcmp(token, ss->legacy_name)) |
| 6316 | continue; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 6317 | cgroup_disable_mask |= 1 << i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6318 | } |
| 6319 | } |
| 6320 | return 1; |
| 6321 | } |
| 6322 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 6323 | |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 6324 | void __init __weak enable_debug_cgroup(void) { } |
| 6325 | |
| 6326 | static int __init enable_cgroup_debug(char *str) |
| 6327 | { |
| 6328 | cgroup_debug = true; |
| 6329 | enable_debug_cgroup(); |
| 6330 | return 1; |
| 6331 | } |
| 6332 | __setup("cgroup_debug", enable_cgroup_debug); |
| 6333 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 6334 | /** |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 6335 | * css_tryget_online_from_dir - get corresponding css from a cgroup dentry |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 6336 | * @dentry: directory dentry of interest |
| 6337 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 6338 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 6339 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 6340 | * to get the corresponding css and return it. If such css doesn't exist |
| 6341 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6342 | */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 6343 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, |
| 6344 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6345 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6346 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 6347 | struct file_system_type *s_type = dentry->d_sb->s_type; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6348 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6349 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6350 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 6351 | /* is @dentry a cgroup dir? */ |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 6352 | if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) || |
| 6353 | !kn || kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6354 | return ERR_PTR(-EBADF); |
| 6355 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 6356 | rcu_read_lock(); |
| 6357 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6358 | /* |
| 6359 | * This path doesn't originate from kernfs and @kn could already |
| 6360 | * have been or be removed at any point. @kn->priv is RCU |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 6361 | * protected for this access. See css_release_work_fn() for details. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6362 | */ |
Tejun Heo | e0aed7c | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 6363 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6364 | if (cgrp) |
| 6365 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 6366 | |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 6367 | if (!css || !css_tryget_online(css)) |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 6368 | css = ERR_PTR(-ENOENT); |
| 6369 | |
| 6370 | rcu_read_unlock(); |
| 6371 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6372 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6373 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 6374 | /** |
| 6375 | * css_from_id - lookup css by id |
| 6376 | * @id: the cgroup id |
| 6377 | * @ss: cgroup subsys to be looked into |
| 6378 | * |
| 6379 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 6380 | * Should be called under rcu_read_lock(). |
| 6381 | */ |
| 6382 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 6383 | { |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 6384 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Johannes Weiner | d6ccc55 | 2016-06-17 12:24:27 -0400 | [diff] [blame] | 6385 | return idr_find(&ss->css_idr, id); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6386 | } |
| 6387 | |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 6388 | /** |
| 6389 | * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path |
| 6390 | * @path: path on the default hierarchy |
| 6391 | * |
| 6392 | * Find the cgroup at @path on the default hierarchy, increment its |
| 6393 | * reference count and return it. Returns pointer to the found cgroup on |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 6394 | * success, ERR_PTR(-ENOENT) if @path doesn't exist and ERR_PTR(-ENOTDIR) |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 6395 | * if @path points to a non-directory. |
| 6396 | */ |
| 6397 | struct cgroup *cgroup_get_from_path(const char *path) |
| 6398 | { |
| 6399 | struct kernfs_node *kn; |
| 6400 | struct cgroup *cgrp; |
| 6401 | |
| 6402 | mutex_lock(&cgroup_mutex); |
| 6403 | |
| 6404 | kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path); |
| 6405 | if (kn) { |
| 6406 | if (kernfs_type(kn) == KERNFS_DIR) { |
| 6407 | cgrp = kn->priv; |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 6408 | cgroup_get_live(cgrp); |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 6409 | } else { |
| 6410 | cgrp = ERR_PTR(-ENOTDIR); |
| 6411 | } |
| 6412 | kernfs_put(kn); |
| 6413 | } else { |
| 6414 | cgrp = ERR_PTR(-ENOENT); |
| 6415 | } |
| 6416 | |
| 6417 | mutex_unlock(&cgroup_mutex); |
| 6418 | return cgrp; |
| 6419 | } |
| 6420 | EXPORT_SYMBOL_GPL(cgroup_get_from_path); |
| 6421 | |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 6422 | /** |
| 6423 | * cgroup_get_from_fd - get a cgroup pointer from a fd |
| 6424 | * @fd: fd obtained by open(cgroup2_dir) |
| 6425 | * |
| 6426 | * Find the cgroup from a fd which should be obtained |
| 6427 | * by opening a cgroup directory. Returns a pointer to the |
| 6428 | * cgroup on success. ERR_PTR is returned if the cgroup |
| 6429 | * cannot be found. |
| 6430 | */ |
| 6431 | struct cgroup *cgroup_get_from_fd(int fd) |
| 6432 | { |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 6433 | struct cgroup *cgrp; |
| 6434 | struct file *f; |
| 6435 | |
| 6436 | f = fget_raw(fd); |
| 6437 | if (!f) |
| 6438 | return ERR_PTR(-EBADF); |
| 6439 | |
Christian Brauner | 1770309 | 2020-02-05 14:26:19 +0100 | [diff] [blame] | 6440 | cgrp = cgroup_get_from_file(f); |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 6441 | fput(f); |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 6442 | return cgrp; |
| 6443 | } |
| 6444 | EXPORT_SYMBOL_GPL(cgroup_get_from_fd); |
| 6445 | |
Tejun Heo | 38cf3a6 | 2019-06-14 10:12:45 -0700 | [diff] [blame] | 6446 | static u64 power_of_ten(int power) |
| 6447 | { |
| 6448 | u64 v = 1; |
| 6449 | while (power--) |
| 6450 | v *= 10; |
| 6451 | return v; |
| 6452 | } |
| 6453 | |
| 6454 | /** |
| 6455 | * cgroup_parse_float - parse a floating number |
| 6456 | * @input: input string |
| 6457 | * @dec_shift: number of decimal digits to shift |
| 6458 | * @v: output |
| 6459 | * |
| 6460 | * Parse a decimal floating point number in @input and store the result in |
| 6461 | * @v with decimal point right shifted @dec_shift times. For example, if |
| 6462 | * @input is "12.3456" and @dec_shift is 3, *@v will be set to 12345. |
| 6463 | * Returns 0 on success, -errno otherwise. |
| 6464 | * |
| 6465 | * There's nothing cgroup specific about this function except that it's |
| 6466 | * currently the only user. |
| 6467 | */ |
| 6468 | int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v) |
| 6469 | { |
| 6470 | s64 whole, frac = 0; |
| 6471 | int fstart = 0, fend = 0, flen; |
| 6472 | |
| 6473 | if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend)) |
| 6474 | return -EINVAL; |
| 6475 | if (frac < 0) |
| 6476 | return -EINVAL; |
| 6477 | |
| 6478 | flen = fend > fstart ? fend - fstart : 0; |
| 6479 | if (flen < dec_shift) |
| 6480 | frac *= power_of_ten(dec_shift - flen); |
| 6481 | else |
| 6482 | frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift)); |
| 6483 | |
| 6484 | *v = whole * power_of_ten(dec_shift) + frac; |
| 6485 | return 0; |
| 6486 | } |
| 6487 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6488 | /* |
| 6489 | * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data |
| 6490 | * definition in cgroup-defs.h. |
| 6491 | */ |
| 6492 | #ifdef CONFIG_SOCK_CGROUP_DATA |
| 6493 | |
| 6494 | #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID) |
| 6495 | |
Tejun Heo | 3fa4cc9 | 2015-12-14 11:24:06 -0500 | [diff] [blame] | 6496 | DEFINE_SPINLOCK(cgroup_sk_update_lock); |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6497 | static bool cgroup_sk_alloc_disabled __read_mostly; |
| 6498 | |
| 6499 | void cgroup_sk_alloc_disable(void) |
| 6500 | { |
| 6501 | if (cgroup_sk_alloc_disabled) |
| 6502 | return; |
| 6503 | pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n"); |
| 6504 | cgroup_sk_alloc_disabled = true; |
| 6505 | } |
| 6506 | |
| 6507 | #else |
| 6508 | |
| 6509 | #define cgroup_sk_alloc_disabled false |
| 6510 | |
| 6511 | #endif |
| 6512 | |
| 6513 | void cgroup_sk_alloc(struct sock_cgroup_data *skcd) |
| 6514 | { |
Cong Wang | ad0f75e | 2020-07-02 11:52:56 -0700 | [diff] [blame] | 6515 | if (cgroup_sk_alloc_disabled) { |
| 6516 | skcd->no_refcnt = 1; |
Johannes Weiner | d979a39 | 2016-09-19 14:44:38 -0700 | [diff] [blame] | 6517 | return; |
| 6518 | } |
| 6519 | |
Shakeel Butt | e876ecc | 2020-03-09 22:16:05 -0700 | [diff] [blame] | 6520 | /* Don't associate the sock with unrelated interrupted task's cgroup. */ |
| 6521 | if (in_interrupt()) |
| 6522 | return; |
| 6523 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6524 | rcu_read_lock(); |
| 6525 | |
| 6526 | while (true) { |
| 6527 | struct css_set *cset; |
| 6528 | |
| 6529 | cset = task_css_set(current); |
| 6530 | if (likely(cgroup_tryget(cset->dfl_cgrp))) { |
| 6531 | skcd->val = (unsigned long)cset->dfl_cgrp; |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 6532 | cgroup_bpf_get(cset->dfl_cgrp); |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6533 | break; |
| 6534 | } |
| 6535 | cpu_relax(); |
| 6536 | } |
| 6537 | |
| 6538 | rcu_read_unlock(); |
| 6539 | } |
| 6540 | |
Cong Wang | ad0f75e | 2020-07-02 11:52:56 -0700 | [diff] [blame] | 6541 | void cgroup_sk_clone(struct sock_cgroup_data *skcd) |
| 6542 | { |
| 6543 | if (skcd->val) { |
| 6544 | if (skcd->no_refcnt) |
| 6545 | return; |
| 6546 | /* |
| 6547 | * We might be cloning a socket which is left in an empty |
| 6548 | * cgroup and the cgroup might have already been rmdir'd. |
| 6549 | * Don't use cgroup_get_live(). |
| 6550 | */ |
| 6551 | cgroup_get(sock_cgroup_ptr(skcd)); |
| 6552 | cgroup_bpf_get(sock_cgroup_ptr(skcd)); |
| 6553 | } |
| 6554 | } |
| 6555 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6556 | void cgroup_sk_free(struct sock_cgroup_data *skcd) |
| 6557 | { |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 6558 | struct cgroup *cgrp = sock_cgroup_ptr(skcd); |
| 6559 | |
Cong Wang | ad0f75e | 2020-07-02 11:52:56 -0700 | [diff] [blame] | 6560 | if (skcd->no_refcnt) |
| 6561 | return; |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 6562 | cgroup_bpf_put(cgrp); |
| 6563 | cgroup_put(cgrp); |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6564 | } |
| 6565 | |
| 6566 | #endif /* CONFIG_SOCK_CGROUP_DATA */ |
| 6567 | |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6568 | #ifdef CONFIG_CGROUP_BPF |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 6569 | int cgroup_bpf_attach(struct cgroup *cgrp, |
| 6570 | struct bpf_prog *prog, struct bpf_prog *replace_prog, |
| 6571 | struct bpf_cgroup_link *link, |
| 6572 | enum bpf_attach_type type, |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 6573 | u32 flags) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6574 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 6575 | int ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6576 | |
| 6577 | mutex_lock(&cgroup_mutex); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 6578 | ret = __cgroup_bpf_attach(cgrp, prog, replace_prog, link, type, flags); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 6579 | mutex_unlock(&cgroup_mutex); |
| 6580 | return ret; |
| 6581 | } |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 6582 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 6583 | int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 6584 | enum bpf_attach_type type) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 6585 | { |
| 6586 | int ret; |
| 6587 | |
| 6588 | mutex_lock(&cgroup_mutex); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 6589 | ret = __cgroup_bpf_detach(cgrp, prog, NULL, type); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6590 | mutex_unlock(&cgroup_mutex); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 6591 | return ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6592 | } |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 6593 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 6594 | int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, |
| 6595 | union bpf_attr __user *uattr) |
| 6596 | { |
| 6597 | int ret; |
| 6598 | |
| 6599 | mutex_lock(&cgroup_mutex); |
| 6600 | ret = __cgroup_bpf_query(cgrp, attr, uattr); |
| 6601 | mutex_unlock(&cgroup_mutex); |
| 6602 | return ret; |
| 6603 | } |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6604 | #endif /* CONFIG_CGROUP_BPF */ |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6605 | |
| 6606 | #ifdef CONFIG_SYSFS |
| 6607 | static ssize_t show_delegatable_files(struct cftype *files, char *buf, |
| 6608 | ssize_t size, const char *prefix) |
| 6609 | { |
| 6610 | struct cftype *cft; |
| 6611 | ssize_t ret = 0; |
| 6612 | |
| 6613 | for (cft = files; cft && cft->name[0] != '\0'; cft++) { |
| 6614 | if (!(cft->flags & CFTYPE_NS_DELEGATABLE)) |
| 6615 | continue; |
| 6616 | |
| 6617 | if (prefix) |
| 6618 | ret += snprintf(buf + ret, size - ret, "%s.", prefix); |
| 6619 | |
| 6620 | ret += snprintf(buf + ret, size - ret, "%s\n", cft->name); |
| 6621 | |
Yangtao Li | 4d9ebbe | 2018-11-03 22:27:41 -0400 | [diff] [blame] | 6622 | if (WARN_ON(ret >= size)) |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6623 | break; |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6624 | } |
| 6625 | |
| 6626 | return ret; |
| 6627 | } |
| 6628 | |
| 6629 | static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 6630 | char *buf) |
| 6631 | { |
| 6632 | struct cgroup_subsys *ss; |
| 6633 | int ssid; |
| 6634 | ssize_t ret = 0; |
| 6635 | |
| 6636 | ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret, |
| 6637 | NULL); |
| 6638 | |
| 6639 | for_each_subsys(ss, ssid) |
| 6640 | ret += show_delegatable_files(ss->dfl_cftypes, buf + ret, |
| 6641 | PAGE_SIZE - ret, |
| 6642 | cgroup_subsys_name[ssid]); |
| 6643 | |
| 6644 | return ret; |
| 6645 | } |
| 6646 | static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate); |
| 6647 | |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 6648 | static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 6649 | char *buf) |
| 6650 | { |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 6651 | return snprintf(buf, PAGE_SIZE, |
| 6652 | "nsdelegate\n" |
| 6653 | "memory_localevents\n" |
| 6654 | "memory_recursiveprot\n"); |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 6655 | } |
| 6656 | static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features); |
| 6657 | |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6658 | static struct attribute *cgroup_sysfs_attrs[] = { |
| 6659 | &cgroup_delegate_attr.attr, |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 6660 | &cgroup_features_attr.attr, |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6661 | NULL, |
| 6662 | }; |
| 6663 | |
| 6664 | static const struct attribute_group cgroup_sysfs_attr_group = { |
| 6665 | .attrs = cgroup_sysfs_attrs, |
| 6666 | .name = "cgroup", |
| 6667 | }; |
| 6668 | |
| 6669 | static int __init cgroup_sysfs_init(void) |
| 6670 | { |
| 6671 | return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group); |
| 6672 | } |
| 6673 | subsys_initcall(cgroup_sysfs_init); |
Tejun Heo | a5e112e | 2019-05-13 12:37:17 -0700 | [diff] [blame] | 6674 | |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6675 | #endif /* CONFIG_SYSFS */ |