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 | |
Jakub Kicinski | aef2fed | 2021-12-15 18:55:37 -0800 | [diff] [blame] | 33 | #include <linux/bpf-cgroup.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 34 | #include <linux/cred.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 35 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 36 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 37 | #include <linux/kernel.h> |
Jianyu Zhan | c9482a5 | 2014-04-26 15:40:28 +0800 | [diff] [blame] | 38 | #include <linux/magic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 39 | #include <linux/mutex.h> |
| 40 | #include <linux/mount.h> |
| 41 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 42 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 43 | #include <linux/rcupdate.h> |
| 44 | #include <linux/sched.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 45 | #include <linux/sched/task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 46 | #include <linux/slab.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 47 | #include <linux/spinlock.h> |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 48 | #include <linux/percpu-rwsem.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 49 | #include <linux/string.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 50 | #include <linux/hashtable.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 51 | #include <linux/idr.h> |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 52 | #include <linux/kthread.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 53 | #include <linux/atomic.h> |
Tejun Heo | e93ad19 | 2016-01-19 12:18:41 -0500 | [diff] [blame] | 54 | #include <linux/cpuset.h> |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 55 | #include <linux/proc_ns.h> |
| 56 | #include <linux/nsproxy.h> |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 57 | #include <linux/file.h> |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 58 | #include <linux/fs_parser.h> |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 59 | #include <linux/sched/cputime.h> |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 60 | #include <linux/psi.h> |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 61 | #include <net/sock.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 62 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 63 | #define CREATE_TRACE_POINTS |
| 64 | #include <trace/events/cgroup.h> |
| 65 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 66 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 67 | MAX_CFTYPE_NAME + 2) |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 68 | /* let's not notify more than 100 times per second */ |
| 69 | #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100) |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 70 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 71 | /* |
Kees Cook | d20d30e | 2021-08-27 17:02:55 -0700 | [diff] [blame] | 72 | * To avoid confusing the compiler (and generating warnings) with code |
| 73 | * that attempts to access what would be a 0-element array (i.e. sized |
| 74 | * to a potentially empty array when CGROUP_SUBSYS_COUNT == 0), this |
| 75 | * constant expression can be added. |
| 76 | */ |
| 77 | #define CGROUP_HAS_SUBSYS_CONFIG (CGROUP_SUBSYS_COUNT > 0) |
| 78 | |
| 79 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 80 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 81 | * hierarchy must be performed while holding it. |
| 82 | * |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 83 | * css_set_lock protects task->cgroups pointer, the list of css_set |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 84 | * objects, and the chain of tasks off each css_set. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 85 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 86 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in |
| 87 | * cgroup.h can use them for lockdep annotations. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 88 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 89 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 90 | DEFINE_SPINLOCK(css_set_lock); |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 91 | |
| 92 | #ifdef CONFIG_PROVE_RCU |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 94 | EXPORT_SYMBOL_GPL(css_set_lock); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 95 | #endif |
| 96 | |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 97 | DEFINE_SPINLOCK(trace_cgroup_path_lock); |
| 98 | char trace_cgroup_path[TRACE_CGROUP_PATH_LEN]; |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 99 | bool cgroup_debug __read_mostly; |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 100 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 101 | /* |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 102 | * Protects cgroup_idr and css_idr so that IDs can be released without |
| 103 | * grabbing cgroup_mutex. |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 104 | */ |
| 105 | static DEFINE_SPINLOCK(cgroup_idr_lock); |
| 106 | |
| 107 | /* |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 108 | * Protects cgroup_file->kn for !self csses. It synchronizes notifications |
| 109 | * against file removal/re-creation across css hiding. |
| 110 | */ |
| 111 | static DEFINE_SPINLOCK(cgroup_file_kn_lock); |
| 112 | |
Oleg Nesterov | 3f2947b | 2019-04-23 18:32:41 +0200 | [diff] [blame] | 113 | DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem); |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 114 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 115 | #define cgroup_assert_mutex_or_rcu_locked() \ |
Paul E. McKenney | f78f5b9 | 2015-06-18 15:50:02 -0700 | [diff] [blame] | 116 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
| 117 | !lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 118 | "cgroup_mutex or RCU read lock required"); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 119 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 120 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 121 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 122 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 123 | * destruction work items don't end up filling up max_active of system_wq |
| 124 | * which may lead to deadlock. |
| 125 | */ |
| 126 | static struct workqueue_struct *cgroup_destroy_wq; |
| 127 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 128 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 129 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 130 | struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 131 | #include <linux/cgroup_subsys.h> |
| 132 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 133 | #undef SUBSYS |
| 134 | |
| 135 | /* array of cgroup subsystem names */ |
| 136 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 137 | static const char *cgroup_subsys_name[] = { |
| 138 | #include <linux/cgroup_subsys.h> |
| 139 | }; |
| 140 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 141 | |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 142 | /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */ |
| 143 | #define SUBSYS(_x) \ |
| 144 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \ |
| 145 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \ |
| 146 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \ |
| 147 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key); |
| 148 | #include <linux/cgroup_subsys.h> |
| 149 | #undef SUBSYS |
| 150 | |
| 151 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key, |
| 152 | static struct static_key_true *cgroup_subsys_enabled_key[] = { |
| 153 | #include <linux/cgroup_subsys.h> |
| 154 | }; |
| 155 | #undef SUBSYS |
| 156 | |
| 157 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key, |
| 158 | static struct static_key_true *cgroup_subsys_on_dfl_key[] = { |
| 159 | #include <linux/cgroup_subsys.h> |
| 160 | }; |
| 161 | #undef SUBSYS |
| 162 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 163 | 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] | 164 | |
Zefan Li | 6b6ebb3 | 2020-05-13 10:13:11 +0800 | [diff] [blame] | 165 | /* the default hierarchy */ |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 166 | 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] | 167 | EXPORT_SYMBOL_GPL(cgrp_dfl_root); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 168 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 169 | /* |
| 170 | * The default hierarchy always exists but is hidden until mounted for the |
| 171 | * first time. This is for backward compatibility. |
| 172 | */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 173 | static bool cgrp_dfl_visible; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 174 | |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 175 | /* some controllers are not supported in the default hierarchy */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 176 | static u16 cgrp_dfl_inhibit_ss_mask; |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 177 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 178 | /* some controllers are implicitly enabled on the default hierarchy */ |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 179 | static u16 cgrp_dfl_implicit_ss_mask; |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 180 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 181 | /* some controllers can be threaded on the default hierarchy */ |
| 182 | static u16 cgrp_dfl_threaded_ss_mask; |
| 183 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 184 | /* The list of hierarchy roots */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 185 | LIST_HEAD(cgroup_roots); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 186 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 187 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 188 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 189 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 190 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 191 | /* |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 192 | * Assign a monotonically increasing serial number to csses. It guarantees |
| 193 | * cgroups with bigger numbers are newer than those with smaller numbers. |
| 194 | * Also, as csses are always appended to the parent's ->children list, it |
| 195 | * guarantees that sibling csses are always sorted in the ascending serial |
| 196 | * number order on the list. Protected by cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 197 | */ |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 198 | static u64 css_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 199 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 200 | /* |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 201 | * These bitmasks identify subsystems with specific features to avoid |
| 202 | * having to do iterative checks repeatedly. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 203 | */ |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 204 | static u16 have_fork_callback __read_mostly; |
| 205 | static u16 have_exit_callback __read_mostly; |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 206 | static u16 have_release_callback __read_mostly; |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 207 | static u16 have_canfork_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 208 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 209 | /* cgroup namespace for init task */ |
| 210 | struct cgroup_namespace init_cgroup_ns = { |
Kirill Tkhai | f387882 | 2020-08-03 13:16:50 +0300 | [diff] [blame] | 211 | .ns.count = REFCOUNT_INIT(2), |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 212 | .user_ns = &init_user_ns, |
| 213 | .ns.ops = &cgroupns_operations, |
| 214 | .ns.inum = PROC_CGROUP_INIT_INO, |
| 215 | .root_cset = &init_css_set, |
| 216 | }; |
| 217 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 218 | static struct file_system_type cgroup2_fs_type; |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 219 | static struct cftype cgroup_base_files[]; |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 220 | |
Suren Baghdasaryan | 3958e2d | 2021-05-24 12:53:39 -0700 | [diff] [blame] | 221 | /* cgroup optional features */ |
| 222 | enum cgroup_opt_features { |
| 223 | #ifdef CONFIG_PSI |
| 224 | OPT_FEATURE_PRESSURE, |
| 225 | #endif |
| 226 | OPT_FEATURE_COUNT |
| 227 | }; |
| 228 | |
| 229 | static const char *cgroup_opt_feature_names[OPT_FEATURE_COUNT] = { |
| 230 | #ifdef CONFIG_PSI |
| 231 | "pressure", |
| 232 | #endif |
| 233 | }; |
| 234 | |
| 235 | static u16 cgroup_feature_disable_mask __read_mostly; |
| 236 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 237 | static int cgroup_apply_control(struct cgroup *cgrp); |
| 238 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret); |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 239 | static void css_task_iter_skip(struct css_task_iter *it, |
| 240 | struct task_struct *task); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 241 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 242 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 243 | struct cgroup_subsys *ss); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 244 | static void css_release(struct percpu_ref *ref); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 245 | static void kill_css(struct cgroup_subsys_state *css); |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 246 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 247 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 248 | bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 249 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 250 | /** |
| 251 | * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID |
| 252 | * @ssid: subsys ID of interest |
| 253 | * |
| 254 | * cgroup_subsys_enabled() can only be used with literal subsys names which |
| 255 | * is fine for individual subsystems but unsuitable for cgroup core. This |
| 256 | * is slower static_key_enabled() based test indexed by @ssid. |
| 257 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 258 | bool cgroup_ssid_enabled(int ssid) |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 259 | { |
Kees Cook | d20d30e | 2021-08-27 17:02:55 -0700 | [diff] [blame] | 260 | if (!CGROUP_HAS_SUBSYS_CONFIG) |
Arnd Bergmann | cfe02a8 | 2016-03-15 00:21:06 +0100 | [diff] [blame] | 261 | return false; |
| 262 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 263 | return static_key_enabled(cgroup_subsys_enabled_key[ssid]); |
| 264 | } |
| 265 | |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 266 | /** |
| 267 | * cgroup_on_dfl - test whether a cgroup is on the default hierarchy |
| 268 | * @cgrp: the cgroup of interest |
| 269 | * |
| 270 | * The default hierarchy is the v2 interface of cgroup and this function |
| 271 | * can be used to test whether a cgroup is on the default hierarchy for |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 272 | * cases where a subsystem should behave differently depending on the |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 273 | * interface version. |
| 274 | * |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 275 | * List of changed behaviors: |
| 276 | * |
| 277 | * - Mount options "noprefix", "xattr", "clone_children", "release_agent" |
| 278 | * and "name" are disallowed. |
| 279 | * |
| 280 | * - When mounting an existing superblock, mount options should match. |
| 281 | * |
| 282 | * - Remount is disallowed. |
| 283 | * |
| 284 | * - rename(2) is disallowed. |
| 285 | * |
| 286 | * - "tasks" is removed. Everything should be at process granularity. Use |
| 287 | * "cgroup.procs" instead. |
| 288 | * |
| 289 | * - "cgroup.procs" is not sorted. pids will be unique unless they got |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 290 | * recycled in-between reads. |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 291 | * |
| 292 | * - "release_agent" and "notify_on_release" are removed. Replacement |
| 293 | * notification mechanism will be implemented. |
| 294 | * |
| 295 | * - "cgroup.clone_children" is removed. |
| 296 | * |
| 297 | * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup |
| 298 | * and its descendants contain no task; otherwise, 1. The file also |
| 299 | * generates kernfs notification which can be monitored through poll and |
| 300 | * [di]notify when the value of the file changes. |
| 301 | * |
| 302 | * - cpuset: tasks will be kept in empty cpusets when hotplug happens and |
| 303 | * take masks of ancestors with non-empty cpus/mems, instead of being |
| 304 | * moved to an ancestor. |
| 305 | * |
| 306 | * - cpuset: a task can be moved into an empty cpuset, and again it takes |
| 307 | * masks of ancestors. |
| 308 | * |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 309 | * - blkcg: blk-throttle becomes properly hierarchical. |
| 310 | * |
| 311 | * - debug: disallowed on the default hierarchy. |
| 312 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 313 | bool cgroup_on_dfl(const struct cgroup *cgrp) |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 314 | { |
| 315 | return cgrp->root == &cgrp_dfl_root; |
| 316 | } |
| 317 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 318 | /* IDR wrappers which synchronize using cgroup_idr_lock */ |
| 319 | static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, |
| 320 | gfp_t gfp_mask) |
| 321 | { |
| 322 | int ret; |
| 323 | |
| 324 | idr_preload(gfp_mask); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 325 | spin_lock_bh(&cgroup_idr_lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 326 | ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 327 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 328 | idr_preload_end(); |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) |
| 333 | { |
| 334 | void *ret; |
| 335 | |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 336 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 337 | ret = idr_replace(idr, ptr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 338 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | static void cgroup_idr_remove(struct idr *idr, int id) |
| 343 | { |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 344 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 345 | idr_remove(idr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 346 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 349 | static bool cgroup_has_tasks(struct cgroup *cgrp) |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 350 | { |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 351 | return cgrp->nr_populated_csets; |
| 352 | } |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 353 | |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 354 | bool cgroup_is_threaded(struct cgroup *cgrp) |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 355 | { |
| 356 | return cgrp->dom_cgrp != cgrp; |
| 357 | } |
| 358 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 359 | /* can @cgrp host both domain and threaded children? */ |
| 360 | static bool cgroup_is_mixable(struct cgroup *cgrp) |
| 361 | { |
| 362 | /* |
| 363 | * Root isn't under domain level resource control exempting it from |
| 364 | * the no-internal-process constraint, so it can serve as a thread |
| 365 | * root and a parent of resource domains at the same time. |
| 366 | */ |
| 367 | return !cgroup_parent(cgrp); |
| 368 | } |
| 369 | |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 370 | /* 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] | 371 | static bool cgroup_can_be_thread_root(struct cgroup *cgrp) |
| 372 | { |
| 373 | /* mixables don't care */ |
| 374 | if (cgroup_is_mixable(cgrp)) |
| 375 | return true; |
| 376 | |
| 377 | /* domain roots can't be nested under threaded */ |
| 378 | if (cgroup_is_threaded(cgrp)) |
| 379 | return false; |
| 380 | |
| 381 | /* can only have either domain or threaded children */ |
| 382 | if (cgrp->nr_populated_domain_children) |
| 383 | return false; |
| 384 | |
| 385 | /* and no domain controllers can be enabled */ |
| 386 | if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 387 | return false; |
| 388 | |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | /* is @cgrp root of a threaded subtree? */ |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 393 | bool cgroup_is_thread_root(struct cgroup *cgrp) |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 394 | { |
| 395 | /* thread root should be a domain */ |
| 396 | if (cgroup_is_threaded(cgrp)) |
| 397 | return false; |
| 398 | |
| 399 | /* a domain w/ threaded children is a thread root */ |
| 400 | if (cgrp->nr_threaded_children) |
| 401 | return true; |
| 402 | |
| 403 | /* |
| 404 | * A domain which has tasks and explicit threaded controllers |
| 405 | * enabled is a thread root. |
| 406 | */ |
| 407 | if (cgroup_has_tasks(cgrp) && |
| 408 | (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask)) |
| 409 | return true; |
| 410 | |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | /* a domain which isn't connected to the root w/o brekage can't be used */ |
| 415 | static bool cgroup_is_valid_domain(struct cgroup *cgrp) |
| 416 | { |
| 417 | /* the cgroup itself can be a thread root */ |
| 418 | if (cgroup_is_threaded(cgrp)) |
| 419 | return false; |
| 420 | |
| 421 | /* but the ancestors can't be unless mixable */ |
| 422 | while ((cgrp = cgroup_parent(cgrp))) { |
| 423 | if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp)) |
| 424 | return false; |
| 425 | if (cgroup_is_threaded(cgrp)) |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | return true; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 430 | } |
| 431 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 432 | /* subsystems visibly enabled on a cgroup */ |
| 433 | static u16 cgroup_control(struct cgroup *cgrp) |
| 434 | { |
| 435 | struct cgroup *parent = cgroup_parent(cgrp); |
| 436 | u16 root_ss_mask = cgrp->root->subsys_mask; |
| 437 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 438 | if (parent) { |
| 439 | u16 ss_mask = parent->subtree_control; |
| 440 | |
| 441 | /* threaded cgroups can only have threaded controllers */ |
| 442 | if (cgroup_is_threaded(cgrp)) |
| 443 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 444 | return ss_mask; |
| 445 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 446 | |
| 447 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 448 | root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask | |
| 449 | cgrp_dfl_implicit_ss_mask); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 450 | return root_ss_mask; |
| 451 | } |
| 452 | |
| 453 | /* subsystems enabled on a cgroup */ |
| 454 | static u16 cgroup_ss_mask(struct cgroup *cgrp) |
| 455 | { |
| 456 | struct cgroup *parent = cgroup_parent(cgrp); |
| 457 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 458 | if (parent) { |
| 459 | u16 ss_mask = parent->subtree_ss_mask; |
| 460 | |
| 461 | /* threaded cgroups can only have threaded controllers */ |
| 462 | if (cgroup_is_threaded(cgrp)) |
| 463 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 464 | return ss_mask; |
| 465 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 466 | |
| 467 | return cgrp->root->subsys_mask; |
| 468 | } |
| 469 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 470 | /** |
| 471 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 472 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 473 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 474 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 475 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 476 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 477 | * the caller is responsible for pinning the returned css if it wants to |
| 478 | * keep accessing it outside the said locks. This function may return |
| 479 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 480 | */ |
| 481 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 482 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 483 | { |
Kees Cook | d20d30e | 2021-08-27 17:02:55 -0700 | [diff] [blame] | 484 | if (CGROUP_HAS_SUBSYS_CONFIG && ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 485 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 486 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 487 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 488 | return &cgrp->self; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 489 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 490 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 491 | /** |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 492 | * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem |
| 493 | * @cgrp: the cgroup of interest |
| 494 | * @ss: the subsystem of interest |
| 495 | * |
Zhen Lei | 08b2b6f | 2021-05-24 16:29:43 +0800 | [diff] [blame] | 496 | * Find and get @cgrp's css associated with @ss. If the css doesn't exist |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 497 | * or is offline, %NULL is returned. |
| 498 | */ |
| 499 | static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp, |
| 500 | struct cgroup_subsys *ss) |
| 501 | { |
| 502 | struct cgroup_subsys_state *css; |
| 503 | |
| 504 | rcu_read_lock(); |
| 505 | css = cgroup_css(cgrp, ss); |
Peng Wang | a581563 | 2019-07-03 10:07:49 +0800 | [diff] [blame] | 506 | if (css && !css_tryget_online(css)) |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 507 | css = NULL; |
| 508 | rcu_read_unlock(); |
| 509 | |
| 510 | return css; |
| 511 | } |
| 512 | |
| 513 | /** |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 514 | * 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] | 515 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 516 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 517 | * |
Chen Hanxiao | d0f702e | 2015-04-23 07:57:33 -0400 | [diff] [blame] | 518 | * Similar to cgroup_css() but returns the effective css, which is defined |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 519 | * as the matching css of the nearest ancestor including self which has @ss |
| 520 | * enabled. If @ss is associated with the hierarchy @cgrp is on, this |
| 521 | * function is guaranteed to return non-NULL css. |
| 522 | */ |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 523 | static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp, |
| 524 | struct cgroup_subsys *ss) |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 525 | { |
| 526 | lockdep_assert_held(&cgroup_mutex); |
| 527 | |
| 528 | if (!ss) |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 529 | return &cgrp->self; |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 530 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 531 | /* |
| 532 | * This function is used while updating css associations and thus |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 533 | * can't test the csses directly. Test ss_mask. |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 534 | */ |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 535 | while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 536 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 537 | if (!cgrp) |
| 538 | return NULL; |
| 539 | } |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 540 | |
| 541 | return cgroup_css(cgrp, ss); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 544 | /** |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 545 | * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem |
| 546 | * @cgrp: the cgroup of interest |
| 547 | * @ss: the subsystem of interest |
| 548 | * |
| 549 | * Find and get the effective css of @cgrp for @ss. The effective css is |
| 550 | * defined as the matching css of the nearest ancestor including self which |
| 551 | * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, |
| 552 | * the root css is returned, so this function always returns a valid css. |
| 553 | * |
| 554 | * The returned css is not guaranteed to be online, and therefore it is the |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 555 | * callers responsibility to try get a reference for it. |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 556 | */ |
| 557 | struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, |
| 558 | struct cgroup_subsys *ss) |
| 559 | { |
| 560 | struct cgroup_subsys_state *css; |
| 561 | |
Kees Cook | d20d30e | 2021-08-27 17:02:55 -0700 | [diff] [blame] | 562 | if (!CGROUP_HAS_SUBSYS_CONFIG) |
| 563 | return NULL; |
| 564 | |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 565 | do { |
| 566 | css = cgroup_css(cgrp, ss); |
| 567 | |
| 568 | if (css) |
| 569 | return css; |
| 570 | cgrp = cgroup_parent(cgrp); |
| 571 | } while (cgrp); |
| 572 | |
| 573 | return init_css_set.subsys[ss->id]; |
| 574 | } |
| 575 | |
| 576 | /** |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 577 | * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem |
| 578 | * @cgrp: the cgroup of interest |
| 579 | * @ss: the subsystem of interest |
| 580 | * |
| 581 | * Find and get the effective css of @cgrp for @ss. The effective css is |
| 582 | * defined as the matching css of the nearest ancestor including self which |
| 583 | * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, |
| 584 | * the root css is returned, so this function always returns a valid css. |
| 585 | * The returned css must be put using css_put(). |
| 586 | */ |
| 587 | struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp, |
| 588 | struct cgroup_subsys *ss) |
| 589 | { |
| 590 | struct cgroup_subsys_state *css; |
| 591 | |
Kees Cook | d20d30e | 2021-08-27 17:02:55 -0700 | [diff] [blame] | 592 | if (!CGROUP_HAS_SUBSYS_CONFIG) |
| 593 | return NULL; |
| 594 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 595 | rcu_read_lock(); |
| 596 | |
| 597 | do { |
| 598 | css = cgroup_css(cgrp, ss); |
| 599 | |
| 600 | if (css && css_tryget_online(css)) |
| 601 | goto out_unlock; |
| 602 | cgrp = cgroup_parent(cgrp); |
| 603 | } while (cgrp); |
| 604 | |
| 605 | css = init_css_set.subsys[ss->id]; |
| 606 | css_get(css); |
| 607 | out_unlock: |
| 608 | rcu_read_unlock(); |
| 609 | return css; |
| 610 | } |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 611 | EXPORT_SYMBOL_GPL(cgroup_get_e_css); |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 612 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 613 | static void cgroup_get_live(struct cgroup *cgrp) |
Tejun Heo | 052c3f3 | 2015-10-15 16:41:50 -0400 | [diff] [blame] | 614 | { |
| 615 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 616 | css_get(&cgrp->self); |
| 617 | } |
| 618 | |
Roman Gushchin | aade7f9 | 2019-04-19 10:03:02 -0700 | [diff] [blame] | 619 | /** |
| 620 | * __cgroup_task_count - count the number of tasks in a cgroup. The caller |
| 621 | * is responsible for taking the css_set_lock. |
| 622 | * @cgrp: the cgroup in question |
| 623 | */ |
| 624 | int __cgroup_task_count(const struct cgroup *cgrp) |
| 625 | { |
| 626 | int count = 0; |
| 627 | struct cgrp_cset_link *link; |
| 628 | |
| 629 | lockdep_assert_held(&css_set_lock); |
| 630 | |
| 631 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 632 | count += link->cset->nr_tasks; |
| 633 | |
| 634 | return count; |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 639 | * @cgrp: the cgroup in question |
| 640 | */ |
| 641 | int cgroup_task_count(const struct cgroup *cgrp) |
| 642 | { |
| 643 | int count; |
| 644 | |
| 645 | spin_lock_irq(&css_set_lock); |
| 646 | count = __cgroup_task_count(cgrp); |
| 647 | spin_unlock_irq(&css_set_lock); |
| 648 | |
| 649 | return count; |
| 650 | } |
| 651 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 652 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 653 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 654 | struct cgroup *cgrp = of->kn->parent->priv; |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 655 | struct cftype *cft = of_cft(of); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 656 | |
| 657 | /* |
| 658 | * This is open and unprotected implementation of cgroup_css(). |
| 659 | * seq_css() is only called from a kernfs file operation which has |
| 660 | * an active reference on the file. Because all the subsystem |
| 661 | * files are drained before a css is disassociated with a cgroup, |
| 662 | * the matching css from the cgroup's subsys table is guaranteed to |
| 663 | * be and stay valid until the enclosing operation is complete. |
| 664 | */ |
Kees Cook | d20d30e | 2021-08-27 17:02:55 -0700 | [diff] [blame] | 665 | if (CGROUP_HAS_SUBSYS_CONFIG && cft->ss) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 666 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 667 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 668 | return &cgrp->self; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 669 | } |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 670 | EXPORT_SYMBOL_GPL(of_css); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 671 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 672 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 673 | * for_each_css - iterate all css's of a cgroup |
| 674 | * @css: the iteration cursor |
| 675 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 676 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 677 | * |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 678 | * Should be called under cgroup_[tree_]mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 679 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 680 | #define for_each_css(css, ssid, cgrp) \ |
| 681 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 682 | if (!((css) = rcu_dereference_check( \ |
| 683 | (cgrp)->subsys[(ssid)], \ |
| 684 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 685 | else |
| 686 | |
| 687 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 688 | * for_each_e_css - iterate all effective css's of a cgroup |
| 689 | * @css: the iteration cursor |
| 690 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 691 | * @cgrp: the target cgroup to iterate css's of |
| 692 | * |
| 693 | * Should be called under cgroup_[tree_]mutex. |
| 694 | */ |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 695 | #define for_each_e_css(css, ssid, cgrp) \ |
| 696 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 697 | if (!((css) = cgroup_e_css_by_mask(cgrp, \ |
| 698 | cgroup_subsys[(ssid)]))) \ |
| 699 | ; \ |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 700 | else |
| 701 | |
| 702 | /** |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 703 | * do_each_subsys_mask - filter for_each_subsys with a bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 704 | * @ss: the iteration cursor |
| 705 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 706 | * @ss_mask: the bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 707 | * |
| 708 | * 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] | 709 | * @ss_mask is set. |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 710 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 711 | #define do_each_subsys_mask(ss, ssid, ss_mask) do { \ |
| 712 | unsigned long __ss_mask = (ss_mask); \ |
Kees Cook | d20d30e | 2021-08-27 17:02:55 -0700 | [diff] [blame] | 713 | if (!CGROUP_HAS_SUBSYS_CONFIG) { \ |
Aleksa Sarai | 4a705c5 | 2015-06-09 21:32:07 +1000 | [diff] [blame] | 714 | (ssid) = 0; \ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 715 | break; \ |
| 716 | } \ |
| 717 | for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \ |
| 718 | (ss) = cgroup_subsys[ssid]; \ |
| 719 | { |
| 720 | |
| 721 | #define while_each_subsys_mask() \ |
| 722 | } \ |
| 723 | } \ |
| 724 | } while (false) |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 725 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 726 | /* iterate over child cgrps, lock should be held throughout iteration */ |
| 727 | #define cgroup_for_each_live_child(child, cgrp) \ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 728 | list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 729 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 730 | cgroup_is_dead(child); })) \ |
| 731 | ; \ |
| 732 | else |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 733 | |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 734 | /* walk live descendants in pre order */ |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 735 | #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \ |
| 736 | css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \ |
| 737 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 738 | (dsct) = (d_css)->cgroup; \ |
| 739 | cgroup_is_dead(dsct); })) \ |
| 740 | ; \ |
| 741 | else |
| 742 | |
| 743 | /* walk live descendants in postorder */ |
| 744 | #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \ |
| 745 | css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \ |
| 746 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 747 | (dsct) = (d_css)->cgroup; \ |
| 748 | cgroup_is_dead(dsct); })) \ |
| 749 | ; \ |
| 750 | else |
| 751 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 752 | /* |
| 753 | * 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] | 754 | * hierarchies being mounted. It contains a pointer to the root state |
| 755 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 756 | * reference-counted, to improve performance when child cgroups |
| 757 | * haven't been created. |
| 758 | */ |
Tejun Heo | 5024ae2 | 2014-05-07 21:31:17 -0400 | [diff] [blame] | 759 | struct css_set init_css_set = { |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 760 | .refcount = REFCOUNT_INIT(1), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 761 | .dom_cset = &init_css_set, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 762 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 763 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 764 | .dying_tasks = LIST_HEAD_INIT(init_css_set.dying_tasks), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 765 | .task_iters = LIST_HEAD_INIT(init_css_set.task_iters), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 766 | .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 767 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 768 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 769 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
Tejun Heo | 3868314 | 2017-09-25 13:50:20 -0700 | [diff] [blame] | 770 | |
| 771 | /* |
| 772 | * The following field is re-initialized when this cset gets linked |
| 773 | * in cgroup_init(). However, let's initialize the field |
| 774 | * statically too so that the default cgroup can be accessed safely |
| 775 | * early during boot. |
| 776 | */ |
| 777 | .dfl_cgrp = &cgrp_dfl_root.cgrp, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 778 | }; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 779 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 780 | static int css_set_count = 1; /* 1 for init_css_set */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 781 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 782 | static bool css_set_threaded(struct css_set *cset) |
| 783 | { |
| 784 | return cset->dom_cset != cset; |
| 785 | } |
| 786 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 787 | /** |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 788 | * css_set_populated - does a css_set contain any tasks? |
| 789 | * @cset: target css_set |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 790 | * |
| 791 | * css_set_populated() should be the same as !!cset->nr_tasks at steady |
| 792 | * state. However, css_set_populated() can be called while a task is being |
| 793 | * added to or removed from the linked list before the nr_tasks is |
| 794 | * properly updated. Hence, we can't just look at ->nr_tasks here. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 795 | */ |
| 796 | static bool css_set_populated(struct css_set *cset) |
| 797 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 798 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 799 | |
| 800 | return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks); |
| 801 | } |
| 802 | |
| 803 | /** |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 804 | * cgroup_update_populated - update the populated count of a cgroup |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 805 | * @cgrp: the target cgroup |
| 806 | * @populated: inc or dec populated count |
| 807 | * |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 808 | * 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] | 809 | * task or losing the last. Update @cgrp->nr_populated_* accordingly. The |
| 810 | * count is propagated towards root so that a given cgroup's |
| 811 | * nr_populated_children is zero iff none of its descendants contain any |
| 812 | * tasks. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 813 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 814 | * @cgrp's interface file "cgroup.populated" is zero if both |
| 815 | * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and |
| 816 | * 1 otherwise. When the sum changes from or to zero, userland is notified |
| 817 | * that the content of the interface file has changed. This can be used to |
| 818 | * detect when @cgrp and its descendants become populated or empty. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 819 | */ |
| 820 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) |
| 821 | { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 822 | struct cgroup *child = NULL; |
| 823 | int adj = populated ? 1 : -1; |
| 824 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 825 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 826 | |
| 827 | do { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 828 | bool was_populated = cgroup_is_populated(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 829 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 830 | if (!child) { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 831 | cgrp->nr_populated_csets += adj; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 832 | } else { |
| 833 | if (cgroup_is_threaded(child)) |
| 834 | cgrp->nr_populated_threaded_children += adj; |
| 835 | else |
| 836 | cgrp->nr_populated_domain_children += adj; |
| 837 | } |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 838 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 839 | if (was_populated == cgroup_is_populated(cgrp)) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 840 | break; |
| 841 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 842 | cgroup1_check_for_release(cgrp); |
Roman Gushchin | 4c476d8 | 2019-04-19 10:03:08 -0700 | [diff] [blame] | 843 | TRACE_CGROUP_PATH(notify_populated, cgrp, |
| 844 | cgroup_is_populated(cgrp)); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 845 | cgroup_file_notify(&cgrp->events_file); |
| 846 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 847 | child = cgrp; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 848 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 849 | } while (cgrp); |
| 850 | } |
| 851 | |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 852 | /** |
| 853 | * css_set_update_populated - update populated state of a css_set |
| 854 | * @cset: target css_set |
| 855 | * @populated: whether @cset is populated or depopulated |
| 856 | * |
| 857 | * @cset is either getting the first task or losing the last. Update the |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 858 | * populated counters of all associated cgroups accordingly. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 859 | */ |
| 860 | static void css_set_update_populated(struct css_set *cset, bool populated) |
| 861 | { |
| 862 | struct cgrp_cset_link *link; |
| 863 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 864 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 865 | |
| 866 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) |
| 867 | cgroup_update_populated(link->cgrp, populated); |
| 868 | } |
| 869 | |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 870 | /* |
| 871 | * @task is leaving, advance task iterators which are pointing to it so |
| 872 | * that they can resume at the next position. Advancing an iterator might |
| 873 | * remove it from the list, use safe walk. See css_task_iter_skip() for |
| 874 | * details. |
| 875 | */ |
| 876 | static void css_set_skip_task_iters(struct css_set *cset, |
| 877 | struct task_struct *task) |
| 878 | { |
| 879 | struct css_task_iter *it, *pos; |
| 880 | |
| 881 | list_for_each_entry_safe(it, pos, &cset->task_iters, iters_node) |
| 882 | css_task_iter_skip(it, task); |
| 883 | } |
| 884 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 885 | /** |
| 886 | * css_set_move_task - move a task from one css_set to another |
| 887 | * @task: task being moved |
| 888 | * @from_cset: css_set @task currently belongs to (may be NULL) |
| 889 | * @to_cset: new css_set @task is being moved to (may be NULL) |
| 890 | * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks |
| 891 | * |
| 892 | * Move @task from @from_cset to @to_cset. If @task didn't belong to any |
| 893 | * css_set, @from_cset can be NULL. If @task is being disassociated |
| 894 | * instead of moved, @to_cset can be NULL. |
| 895 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 896 | * This function automatically handles populated counter updates and |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 897 | * css_task_iter adjustments but the caller is responsible for managing |
| 898 | * @from_cset and @to_cset's reference counts. |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 899 | */ |
| 900 | static void css_set_move_task(struct task_struct *task, |
| 901 | struct css_set *from_cset, struct css_set *to_cset, |
| 902 | bool use_mg_tasks) |
| 903 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 904 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 905 | |
Tejun Heo | 20b454a | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 906 | if (to_cset && !css_set_populated(to_cset)) |
| 907 | css_set_update_populated(to_cset, true); |
| 908 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 909 | if (from_cset) { |
| 910 | WARN_ON_ONCE(list_empty(&task->cg_list)); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 911 | |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 912 | css_set_skip_task_iters(from_cset, task); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 913 | list_del_init(&task->cg_list); |
| 914 | if (!css_set_populated(from_cset)) |
| 915 | css_set_update_populated(from_cset, false); |
| 916 | } else { |
| 917 | WARN_ON_ONCE(!list_empty(&task->cg_list)); |
| 918 | } |
| 919 | |
| 920 | if (to_cset) { |
| 921 | /* |
| 922 | * We are synchronized through cgroup_threadgroup_rwsem |
| 923 | * against PF_EXITING setting such that we can't race |
Michal Koutný | e7c7b1d | 2019-10-04 12:57:39 +0200 | [diff] [blame] | 924 | * against cgroup_exit()/cgroup_free() dropping the css_set. |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 925 | */ |
| 926 | WARN_ON_ONCE(task->flags & PF_EXITING); |
| 927 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 928 | cgroup_move_task(task, to_cset); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 929 | list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks : |
| 930 | &to_cset->tasks); |
| 931 | } |
| 932 | } |
| 933 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 934 | /* |
| 935 | * hash table for cgroup groups. This improves the performance to find |
| 936 | * an existing css_set. This hash doesn't (currently) take into |
| 937 | * account cgroups in empty hierarchies. |
| 938 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 939 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 940 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 941 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 942 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 943 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 944 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 945 | struct cgroup_subsys *ss; |
| 946 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 947 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 948 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 949 | key += (unsigned long)css[i]; |
| 950 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 951 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 952 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Tejun Heo | dcfe149 | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 955 | void put_css_set_locked(struct css_set *cset) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 956 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 957 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 958 | struct cgroup_subsys *ss; |
| 959 | int ssid; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 960 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 961 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 962 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 963 | if (!refcount_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 964 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 965 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 966 | WARN_ON_ONCE(!list_empty(&cset->threaded_csets)); |
| 967 | |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 968 | /* 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] | 969 | for_each_subsys(ss, ssid) { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 970 | list_del(&cset->e_cset_node[ssid]); |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 971 | css_put(cset->subsys[ssid]); |
| 972 | } |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 973 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 974 | css_set_count--; |
| 975 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 976 | 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] | 977 | list_del(&link->cset_link); |
| 978 | list_del(&link->cgrp_link); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 979 | if (cgroup_parent(link->cgrp)) |
| 980 | cgroup_put(link->cgrp); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 981 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 982 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 983 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 984 | if (css_set_threaded(cset)) { |
| 985 | list_del(&cset->threaded_csets_node); |
| 986 | put_css_set_locked(cset->dom_cset); |
| 987 | } |
| 988 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 989 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 992 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 993 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 994 | * @cset: candidate css_set being tested |
| 995 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 996 | * @new_cgrp: cgroup that's being entered by the task |
| 997 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 998 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 999 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1000 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 1001 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1002 | static bool compare_css_sets(struct css_set *cset, |
| 1003 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1004 | struct cgroup *new_cgrp, |
| 1005 | struct cgroup_subsys_state *template[]) |
| 1006 | { |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1007 | struct cgroup *new_dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1008 | struct list_head *l1, *l2; |
| 1009 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1010 | /* |
| 1011 | * On the default hierarchy, there can be csets which are |
| 1012 | * associated with the same set of cgroups but different csses. |
| 1013 | * Let's first ensure that csses match. |
| 1014 | */ |
| 1015 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1016 | return false; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1017 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1018 | |
| 1019 | /* @cset's domain should match the default cgroup's */ |
| 1020 | if (cgroup_on_dfl(new_cgrp)) |
| 1021 | new_dfl_cgrp = new_cgrp; |
| 1022 | else |
| 1023 | new_dfl_cgrp = old_cset->dfl_cgrp; |
| 1024 | |
| 1025 | if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp) |
| 1026 | return false; |
| 1027 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1028 | /* |
| 1029 | * Compare cgroup pointers in order to distinguish between |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1030 | * different cgroups in hierarchies. As different cgroups may |
| 1031 | * share the same effective css, this comparison is always |
| 1032 | * necessary. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1033 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1034 | l1 = &cset->cgrp_links; |
| 1035 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1036 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1037 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1038 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1039 | |
| 1040 | l1 = l1->next; |
| 1041 | l2 = l2->next; |
| 1042 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1043 | if (l1 == &cset->cgrp_links) { |
| 1044 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1045 | break; |
| 1046 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1047 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1048 | } |
| 1049 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1050 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 1051 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 1052 | cgrp1 = link1->cgrp; |
| 1053 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1054 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1055 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* |
| 1058 | * If this hierarchy is the hierarchy of the cgroup |
| 1059 | * that's changing, then we need to check that this |
| 1060 | * css_set points to the new cgroup; if it's any other |
| 1061 | * hierarchy, then this css_set should point to the |
| 1062 | * same cgroup as the old css_set. |
| 1063 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1064 | if (cgrp1->root == new_cgrp->root) { |
| 1065 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1066 | return false; |
| 1067 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1068 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
| 1071 | } |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1075 | /** |
| 1076 | * find_existing_css_set - init css array and find the matching css_set |
| 1077 | * @old_cset: the css_set that we're using before the cgroup transition |
| 1078 | * @cgrp: the cgroup that we're moving into |
| 1079 | * @template: out param for the new set of csses, should be clear on entry |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1080 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1081 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 1082 | struct cgroup *cgrp, |
| 1083 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1084 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1085 | struct cgroup_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1086 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1087 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1088 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1089 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1090 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1091 | /* |
| 1092 | * 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] | 1093 | * new css_set. While subsystems can change globally, the entries here |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1094 | * won't change, so no need for locking. |
| 1095 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1096 | for_each_subsys(ss, i) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1097 | if (root->subsys_mask & (1UL << i)) { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1098 | /* |
| 1099 | * @ss is in this hierarchy, so we want the |
| 1100 | * effective css from @cgrp. |
| 1101 | */ |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 1102 | template[i] = cgroup_e_css_by_mask(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1103 | } else { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1104 | /* |
| 1105 | * @ss is not in this hierarchy, so we don't want |
| 1106 | * to change the css. |
| 1107 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1108 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1112 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1113 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 1114 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1115 | continue; |
| 1116 | |
| 1117 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1118 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1119 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1120 | |
| 1121 | /* No existing cgroup group matched */ |
| 1122 | return NULL; |
| 1123 | } |
| 1124 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1125 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1126 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1127 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1128 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1129 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 1130 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1131 | kfree(link); |
| 1132 | } |
| 1133 | } |
| 1134 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1135 | /** |
| 1136 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 1137 | * @count: the number of links to allocate |
| 1138 | * @tmp_links: list_head the allocated links are put on |
| 1139 | * |
| 1140 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 1141 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1142 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1143 | 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] | 1144 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1145 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1146 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1147 | |
| 1148 | INIT_LIST_HEAD(tmp_links); |
| 1149 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1150 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1151 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1152 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1153 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1154 | return -ENOMEM; |
| 1155 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1156 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1157 | } |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1161 | /** |
| 1162 | * 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] | 1163 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1164 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1165 | * @cgrp: the destination cgroup |
| 1166 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1167 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 1168 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1169 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1170 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1171 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1172 | BUG_ON(list_empty(tmp_links)); |
Tejun Heo | 6803c00 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1173 | |
| 1174 | if (cgroup_on_dfl(cgrp)) |
| 1175 | cset->dfl_cgrp = cgrp; |
| 1176 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1177 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 1178 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1179 | link->cgrp = cgrp; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 1180 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1181 | /* |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1182 | * 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] | 1183 | * in chronological order. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1184 | */ |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1185 | list_move_tail(&link->cset_link, &cgrp->cset_links); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1186 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1187 | |
| 1188 | if (cgroup_parent(cgrp)) |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 1189 | cgroup_get_live(cgrp); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1192 | /** |
| 1193 | * find_css_set - return a new css_set with one cgroup updated |
| 1194 | * @old_cset: the baseline css_set |
| 1195 | * @cgrp: the cgroup to be updated |
| 1196 | * |
| 1197 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 1198 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1199 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1200 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 1201 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1202 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1203 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1204 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1205 | struct list_head tmp_links; |
| 1206 | struct cgrp_cset_link *link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1207 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1208 | unsigned long key; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1209 | int ssid; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1210 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1211 | lockdep_assert_held(&cgroup_mutex); |
| 1212 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1213 | /* First see if we already have a cgroup group that matches |
| 1214 | * the desired set */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1215 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1216 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 1217 | if (cset) |
| 1218 | get_css_set(cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1219 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1220 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1221 | if (cset) |
| 1222 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1223 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1224 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1225 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1226 | return NULL; |
| 1227 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1228 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1229 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1230 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1231 | return NULL; |
| 1232 | } |
| 1233 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 1234 | refcount_set(&cset->refcount, 1); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1235 | cset->dom_cset = cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1236 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1237 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 1238 | INIT_LIST_HEAD(&cset->dying_tasks); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 1239 | INIT_LIST_HEAD(&cset->task_iters); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1240 | INIT_LIST_HEAD(&cset->threaded_csets); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1241 | INIT_HLIST_NODE(&cset->hlist); |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 1242 | INIT_LIST_HEAD(&cset->cgrp_links); |
| 1243 | INIT_LIST_HEAD(&cset->mg_preload_node); |
| 1244 | INIT_LIST_HEAD(&cset->mg_node); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1245 | |
| 1246 | /* Copy the set of subsystem state objects generated in |
| 1247 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1248 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1249 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1250 | spin_lock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1251 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1252 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1253 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1254 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1255 | if (c->root == cgrp->root) |
| 1256 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1257 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1258 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1259 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1260 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1261 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1262 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1263 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1264 | /* Add @cset to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1265 | key = css_set_hash(cset->subsys); |
| 1266 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1267 | |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1268 | for_each_subsys(ss, ssid) { |
| 1269 | struct cgroup_subsys_state *css = cset->subsys[ssid]; |
| 1270 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1271 | list_add_tail(&cset->e_cset_node[ssid], |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1272 | &css->cgroup->e_csets[ssid]); |
| 1273 | css_get(css); |
| 1274 | } |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1275 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1276 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1277 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1278 | /* |
| 1279 | * If @cset should be threaded, look up the matching dom_cset and |
| 1280 | * link them up. We first fully initialize @cset then look for the |
| 1281 | * dom_cset. It's simpler this way and safe as @cset is guaranteed |
| 1282 | * to stay empty until we return. |
| 1283 | */ |
| 1284 | if (cgroup_is_threaded(cset->dfl_cgrp)) { |
| 1285 | struct css_set *dcset; |
| 1286 | |
| 1287 | dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp); |
| 1288 | if (!dcset) { |
| 1289 | put_css_set(cset); |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | |
| 1293 | spin_lock_irq(&css_set_lock); |
| 1294 | cset->dom_cset = dcset; |
| 1295 | list_add_tail(&cset->threaded_csets_node, |
| 1296 | &dcset->threaded_csets); |
| 1297 | spin_unlock_irq(&css_set_lock); |
| 1298 | } |
| 1299 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1300 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1303 | struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1304 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1305 | struct cgroup *root_cgrp = kf_root->kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1306 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1307 | return root_cgrp->root; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1308 | } |
| 1309 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1310 | static int cgroup_init_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1311 | { |
| 1312 | int id; |
| 1313 | |
| 1314 | lockdep_assert_held(&cgroup_mutex); |
| 1315 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1316 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1317 | if (id < 0) |
| 1318 | return id; |
| 1319 | |
| 1320 | root->hierarchy_id = id; |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1324 | static void cgroup_exit_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1325 | { |
| 1326 | lockdep_assert_held(&cgroup_mutex); |
| 1327 | |
Johannes Weiner | 8c8a550 | 2016-06-17 12:23:59 -0400 | [diff] [blame] | 1328 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1329 | } |
| 1330 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1331 | void cgroup_free_root(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1332 | { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1333 | kfree(root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1334 | } |
| 1335 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1336 | static void cgroup_destroy_root(struct cgroup_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1337 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1338 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1339 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1340 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 1341 | trace_cgroup_destroy_root(root); |
| 1342 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1343 | cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1344 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1345 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1346 | BUG_ON(!list_empty(&cgrp->self.children)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1347 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1348 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1349 | WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1350 | |
| 1351 | /* |
| 1352 | * Release all the links from cset_links to this hierarchy's |
| 1353 | * root cgroup |
| 1354 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1355 | spin_lock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1356 | |
| 1357 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1358 | list_del(&link->cset_link); |
| 1359 | list_del(&link->cgrp_link); |
| 1360 | kfree(link); |
| 1361 | } |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1362 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1363 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1364 | |
| 1365 | if (!list_empty(&root->root_list)) { |
| 1366 | list_del(&root->root_list); |
| 1367 | cgroup_root_count--; |
| 1368 | } |
| 1369 | |
| 1370 | cgroup_exit_root_id(root); |
| 1371 | |
| 1372 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1373 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 1374 | cgroup_rstat_exit(cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1375 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1376 | cgroup_free_root(root); |
| 1377 | } |
| 1378 | |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1379 | /* |
| 1380 | * look up cgroup associated with current task's cgroup namespace on the |
| 1381 | * specified hierarchy |
| 1382 | */ |
| 1383 | static struct cgroup * |
| 1384 | current_cgns_cgroup_from_root(struct cgroup_root *root) |
| 1385 | { |
| 1386 | struct cgroup *res = NULL; |
| 1387 | struct css_set *cset; |
| 1388 | |
| 1389 | lockdep_assert_held(&css_set_lock); |
| 1390 | |
| 1391 | rcu_read_lock(); |
| 1392 | |
| 1393 | cset = current->nsproxy->cgroup_ns->root_cset; |
| 1394 | if (cset == &init_css_set) { |
| 1395 | res = &root->cgrp; |
Miaohe Lin | 61e867f | 2019-09-29 16:06:58 +0800 | [diff] [blame] | 1396 | } else if (root == &cgrp_dfl_root) { |
| 1397 | res = cset->dfl_cgrp; |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1398 | } else { |
| 1399 | struct cgrp_cset_link *link; |
| 1400 | |
| 1401 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
| 1402 | struct cgroup *c = link->cgrp; |
| 1403 | |
| 1404 | if (c->root == root) { |
| 1405 | res = c; |
| 1406 | break; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | rcu_read_unlock(); |
| 1411 | |
| 1412 | BUG_ON(!res); |
| 1413 | return res; |
| 1414 | } |
| 1415 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1416 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 1417 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1418 | struct cgroup_root *root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1419 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1420 | struct cgroup *res = NULL; |
| 1421 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1422 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1423 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1424 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1425 | if (cset == &init_css_set) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1426 | res = &root->cgrp; |
Tejun Heo | 13d82fb | 2017-08-02 15:39:38 -0700 | [diff] [blame] | 1427 | } else if (root == &cgrp_dfl_root) { |
| 1428 | res = cset->dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1429 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1430 | struct cgrp_cset_link *link; |
| 1431 | |
| 1432 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1433 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1434 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1435 | if (c->root == root) { |
| 1436 | res = c; |
| 1437 | break; |
| 1438 | } |
| 1439 | } |
| 1440 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1441 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1442 | BUG_ON(!res); |
| 1443 | return res; |
| 1444 | } |
| 1445 | |
| 1446 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1447 | * Return the cgroup for "task" from the given hierarchy. Must be |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1448 | * called with cgroup_mutex and css_set_lock held. |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1449 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1450 | struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 1451 | struct cgroup_root *root) |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1452 | { |
| 1453 | /* |
Michal Koutný | e7c7b1d | 2019-10-04 12:57:39 +0200 | [diff] [blame] | 1454 | * No need to lock the task - since we hold css_set_lock the |
| 1455 | * task can't change groups. |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1456 | */ |
| 1457 | return cset_cgroup_from_root(task_css_set(task), root); |
| 1458 | } |
| 1459 | |
| 1460 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1461 | * A task must hold cgroup_mutex to modify cgroups. |
| 1462 | * |
| 1463 | * Any task can increment and decrement the count field without lock. |
| 1464 | * So in general, code holding cgroup_mutex can't rely on the count |
| 1465 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1466 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1467 | * means that no tasks are currently attached, therefore there is no |
| 1468 | * way a task attached to that cgroup can fork (the other way to |
| 1469 | * increment the count). So code holding cgroup_mutex can safely |
| 1470 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 1471 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 1472 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 1473 | * needs that mutex. |
| 1474 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1475 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 1476 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 1477 | * tasks in the system use _some_ cgroup, and since there is always at |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1478 | * least one task in the system (init, pid == 1), therefore, root cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1479 | * 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] | 1480 | * need a special hack to ensure that root cgroup cannot be deleted. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1481 | * |
| 1482 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1483 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1484 | */ |
| 1485 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1486 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 1487 | |
Jens Axboe | cf89298 | 2019-06-10 03:35:41 -0600 | [diff] [blame] | 1488 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 1489 | char *buf) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1490 | { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 1491 | struct cgroup_subsys *ss = cft->ss; |
| 1492 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1493 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
Tejun Heo | c1bbd93 | 2018-11-13 12:06:41 -0800 | [diff] [blame] | 1494 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
| 1495 | const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : ""; |
| 1496 | |
| 1497 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s", |
| 1498 | dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name, |
Jens Axboe | cf89298 | 2019-06-10 03:35:41 -0600 | [diff] [blame] | 1499 | cft->name); |
Tejun Heo | c1bbd93 | 2018-11-13 12:06:41 -0800 | [diff] [blame] | 1500 | } else { |
Jens Axboe | cf89298 | 2019-06-10 03:35:41 -0600 | [diff] [blame] | 1501 | strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
Tejun Heo | c1bbd93 | 2018-11-13 12:06:41 -0800 | [diff] [blame] | 1502 | } |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1503 | return buf; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1506 | /** |
| 1507 | * cgroup_file_mode - deduce file mode of a control file |
| 1508 | * @cft: the control file in question |
| 1509 | * |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1510 | * S_IRUGO for read, S_IWUSR for write. |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1511 | */ |
| 1512 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1513 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1514 | umode_t mode = 0; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1515 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1516 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 1517 | mode |= S_IRUGO; |
| 1518 | |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1519 | if (cft->write_u64 || cft->write_s64 || cft->write) { |
| 1520 | if (cft->flags & CFTYPE_WORLD_WRITABLE) |
| 1521 | mode |= S_IWUGO; |
| 1522 | else |
| 1523 | mode |= S_IWUSR; |
| 1524 | } |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1525 | |
| 1526 | return mode; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1527 | } |
| 1528 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1529 | /** |
Tejun Heo | 8699b77 | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1530 | * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1531 | * @subtree_control: the new subtree_control mask to consider |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1532 | * @this_ss_mask: available subsystems |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1533 | * |
| 1534 | * On the default hierarchy, a subsystem may request other subsystems to be |
| 1535 | * enabled together through its ->depends_on mask. In such cases, more |
| 1536 | * subsystems than specified in "cgroup.subtree_control" may be enabled. |
| 1537 | * |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1538 | * This function calculates which subsystems need to be enabled if |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1539 | * @subtree_control is to be applied while restricted to @this_ss_mask. |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1540 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1541 | 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] | 1542 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1543 | u16 cur_ss_mask = subtree_control; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1544 | struct cgroup_subsys *ss; |
| 1545 | int ssid; |
| 1546 | |
| 1547 | lockdep_assert_held(&cgroup_mutex); |
| 1548 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1549 | cur_ss_mask |= cgrp_dfl_implicit_ss_mask; |
| 1550 | |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1551 | while (true) { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1552 | u16 new_ss_mask = cur_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1553 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1554 | do_each_subsys_mask(ss, ssid, cur_ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 1555 | new_ss_mask |= ss->depends_on; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1556 | } while_each_subsys_mask(); |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1557 | |
| 1558 | /* |
| 1559 | * Mask out subsystems which aren't available. This can |
| 1560 | * happen only if some depended-upon subsystems were bound |
| 1561 | * to non-default hierarchies. |
| 1562 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1563 | new_ss_mask &= this_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1564 | |
| 1565 | if (new_ss_mask == cur_ss_mask) |
| 1566 | break; |
| 1567 | cur_ss_mask = new_ss_mask; |
| 1568 | } |
| 1569 | |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1570 | return cur_ss_mask; |
| 1571 | } |
| 1572 | |
| 1573 | /** |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1574 | * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods |
| 1575 | * @kn: the kernfs_node being serviced |
| 1576 | * |
| 1577 | * This helper undoes cgroup_kn_lock_live() and should be invoked before |
| 1578 | * the method finishes if locking succeeded. Note that once this function |
| 1579 | * returns the cgroup returned by cgroup_kn_lock_live() may become |
| 1580 | * inaccessible any time. If the caller intends to continue to access the |
| 1581 | * cgroup, it should pin it before invoking this function. |
| 1582 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1583 | void cgroup_kn_unlock(struct kernfs_node *kn) |
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; |
| 1591 | |
| 1592 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1593 | |
| 1594 | kernfs_unbreak_active_protection(kn); |
| 1595 | cgroup_put(cgrp); |
| 1596 | } |
| 1597 | |
| 1598 | /** |
| 1599 | * cgroup_kn_lock_live - locking helper for cgroup kernfs methods |
| 1600 | * @kn: the kernfs_node being serviced |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1601 | * @drain_offline: perform offline draining on the cgroup |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1602 | * |
| 1603 | * This helper is to be used by a cgroup kernfs method currently servicing |
| 1604 | * @kn. It breaks the active protection, performs cgroup locking and |
| 1605 | * verifies that the associated cgroup is alive. Returns the cgroup if |
| 1606 | * alive; otherwise, %NULL. A successful return should be undone by a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1607 | * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the |
| 1608 | * cgroup is drained of offlining csses before return. |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1609 | * |
| 1610 | * Any cgroup kernfs method implementation which requires locking the |
| 1611 | * associated cgroup should use this helper. It avoids nesting cgroup |
| 1612 | * locking under kernfs active protection and allows all kernfs operations |
| 1613 | * including self-removal. |
| 1614 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1615 | 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] | 1616 | { |
| 1617 | struct cgroup *cgrp; |
| 1618 | |
| 1619 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1620 | cgrp = kn->priv; |
| 1621 | else |
| 1622 | cgrp = kn->parent->priv; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1623 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1624 | /* |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1625 | * We're gonna grab cgroup_mutex which nests outside kernfs |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1626 | * active_ref. cgroup liveliness check alone provides enough |
| 1627 | * protection against removal. Ensure @cgrp stays accessible and |
| 1628 | * break the active_ref protection. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1629 | */ |
Li Zefan | aa32362 | 2014-09-04 14:43:38 +0800 | [diff] [blame] | 1630 | if (!cgroup_tryget(cgrp)) |
| 1631 | return NULL; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1632 | kernfs_break_active_protection(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1633 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1634 | if (drain_offline) |
| 1635 | cgroup_lock_and_drain_offline(cgrp); |
| 1636 | else |
| 1637 | mutex_lock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1638 | |
| 1639 | if (!cgroup_is_dead(cgrp)) |
| 1640 | return cgrp; |
| 1641 | |
| 1642 | cgroup_kn_unlock(kn); |
| 1643 | return NULL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 1646 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1647 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1648 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1649 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1650 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1651 | |
| 1652 | if (cft->file_offset) { |
| 1653 | struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss); |
| 1654 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 1655 | |
| 1656 | spin_lock_irq(&cgroup_file_kn_lock); |
| 1657 | cfile->kn = NULL; |
| 1658 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1659 | |
| 1660 | del_timer_sync(&cfile->notify_timer); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1661 | } |
| 1662 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1663 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1666 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1667 | * css_clear_dir - remove subsys files in a cgroup directory |
Zhen Lei | 08b2b6f | 2021-05-24 16:29:43 +0800 | [diff] [blame] | 1668 | * @css: target css |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1669 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1670 | static void css_clear_dir(struct cgroup_subsys_state *css) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1671 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1672 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1673 | struct cftype *cfts; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1674 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1675 | if (!(css->flags & CSS_VISIBLE)) |
| 1676 | return; |
| 1677 | |
| 1678 | css->flags &= ~CSS_VISIBLE; |
| 1679 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1680 | if (!css->ss) { |
| 1681 | if (cgroup_on_dfl(cgrp)) |
| 1682 | cfts = cgroup_base_files; |
| 1683 | else |
| 1684 | cfts = cgroup1_base_files; |
| 1685 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1686 | cgroup_addrm_files(css, cgrp, cfts, false); |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1687 | } else { |
| 1688 | list_for_each_entry(cfts, &css->ss->cfts, node) |
| 1689 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1690 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1693 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1694 | * css_populate_dir - create subsys files in a cgroup directory |
| 1695 | * @css: target css |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1696 | * |
| 1697 | * On failure, no file is added. |
| 1698 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1699 | static int css_populate_dir(struct cgroup_subsys_state *css) |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1700 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1701 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1702 | struct cftype *cfts, *failed_cfts; |
| 1703 | int ret; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1704 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1705 | if ((css->flags & CSS_VISIBLE) || !cgrp->kn) |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1706 | return 0; |
| 1707 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1708 | if (!css->ss) { |
| 1709 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1710 | cfts = cgroup_base_files; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1711 | else |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1712 | cfts = cgroup1_base_files; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1713 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1714 | ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true); |
| 1715 | if (ret < 0) |
| 1716 | return ret; |
| 1717 | } else { |
| 1718 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1719 | ret = cgroup_addrm_files(css, cgrp, cfts, true); |
| 1720 | if (ret < 0) { |
| 1721 | failed_cfts = cfts; |
| 1722 | goto err; |
| 1723 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1724 | } |
| 1725 | } |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1726 | |
| 1727 | css->flags |= CSS_VISIBLE; |
| 1728 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1729 | return 0; |
| 1730 | err: |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1731 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1732 | if (cfts == failed_cfts) |
| 1733 | break; |
| 1734 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1735 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1736 | return ret; |
| 1737 | } |
| 1738 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1739 | int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1740 | { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1741 | struct cgroup *dcgrp = &dst_root->cgrp; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1742 | struct cgroup_subsys *ss; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1743 | int ssid, i, ret; |
Waiman Long | 7ee2853 | 2021-09-18 18:53:08 -0400 | [diff] [blame] | 1744 | u16 dfl_disable_ss_mask = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1745 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1746 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1747 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1748 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1749 | /* |
| 1750 | * If @ss has non-root csses attached to it, can't move. |
| 1751 | * If @ss is an implicit controller, it is exempt from this |
| 1752 | * rule and can be stolen. |
| 1753 | */ |
| 1754 | if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) && |
| 1755 | !ss->implicit_on_dfl) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1756 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1757 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1758 | /* can't move between two non-dummy roots either */ |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1759 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1760 | return -EBUSY; |
Waiman Long | 7ee2853 | 2021-09-18 18:53:08 -0400 | [diff] [blame] | 1761 | |
| 1762 | /* |
| 1763 | * Collect ssid's that need to be disabled from default |
| 1764 | * hierarchy. |
| 1765 | */ |
| 1766 | if (ss->root == &cgrp_dfl_root) |
| 1767 | dfl_disable_ss_mask |= 1 << ssid; |
| 1768 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1769 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1770 | |
Waiman Long | 7ee2853 | 2021-09-18 18:53:08 -0400 | [diff] [blame] | 1771 | if (dfl_disable_ss_mask) { |
| 1772 | struct cgroup *scgrp = &cgrp_dfl_root.cgrp; |
| 1773 | |
| 1774 | /* |
| 1775 | * Controllers from default hierarchy that need to be rebound |
| 1776 | * are all disabled together in one go. |
| 1777 | */ |
| 1778 | cgrp_dfl_root.subsys_mask &= ~dfl_disable_ss_mask; |
| 1779 | WARN_ON(cgroup_apply_control(scgrp)); |
| 1780 | cgroup_finalize_control(scgrp, 0); |
| 1781 | } |
| 1782 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1783 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1784 | struct cgroup_root *src_root = ss->root; |
| 1785 | struct cgroup *scgrp = &src_root->cgrp; |
| 1786 | struct cgroup_subsys_state *css = cgroup_css(scgrp, ss); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1787 | struct css_set *cset; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1788 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1789 | WARN_ON(!css || cgroup_css(dcgrp, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1790 | |
Waiman Long | 7ee2853 | 2021-09-18 18:53:08 -0400 | [diff] [blame] | 1791 | if (src_root != &cgrp_dfl_root) { |
| 1792 | /* disable from the source */ |
| 1793 | src_root->subsys_mask &= ~(1 << ssid); |
| 1794 | WARN_ON(cgroup_apply_control(scgrp)); |
| 1795 | cgroup_finalize_control(scgrp, 0); |
| 1796 | } |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1797 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1798 | /* rebind */ |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1799 | RCU_INIT_POINTER(scgrp->subsys[ssid], NULL); |
| 1800 | rcu_assign_pointer(dcgrp->subsys[ssid], css); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1801 | ss->root = dst_root; |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1802 | css->cgroup = dcgrp; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1803 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1804 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1805 | hash_for_each(css_set_table, i, cset, hlist) |
| 1806 | list_move_tail(&cset->e_cset_node[ss->id], |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1807 | &dcgrp->e_csets[ss->id]); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1808 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1809 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 1810 | if (ss->css_rstat_flush) { |
| 1811 | list_del_rcu(&css->rstat_css_node); |
| 1812 | list_add_rcu(&css->rstat_css_node, |
| 1813 | &dcgrp->rstat_css_list); |
| 1814 | } |
| 1815 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1816 | /* default hierarchy doesn't enable controllers by default */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1817 | dst_root->subsys_mask |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1818 | if (dst_root == &cgrp_dfl_root) { |
| 1819 | static_branch_enable(cgroup_subsys_on_dfl_key[ssid]); |
| 1820 | } else { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1821 | dcgrp->subtree_control |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1822 | static_branch_disable(cgroup_subsys_on_dfl_key[ssid]); |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 1823 | } |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1824 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1825 | ret = cgroup_apply_control(dcgrp); |
| 1826 | if (ret) |
| 1827 | pr_warn("partial failure to rebind %s controller (err=%d)\n", |
| 1828 | ss->name, ret); |
| 1829 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1830 | if (ss->bind) |
| 1831 | ss->bind(css); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1832 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1833 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1834 | kernfs_activate(dcgrp->kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1835 | return 0; |
| 1836 | } |
| 1837 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1838 | int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, |
| 1839 | struct kernfs_root *kf_root) |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1840 | { |
Felipe Balbi | 09be4c8 | 2016-05-12 12:34:38 +0300 | [diff] [blame] | 1841 | int len = 0; |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1842 | char *buf = NULL; |
| 1843 | struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root); |
| 1844 | struct cgroup *ns_cgroup; |
| 1845 | |
| 1846 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 1847 | if (!buf) |
| 1848 | return -ENOMEM; |
| 1849 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1850 | spin_lock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1851 | ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot); |
| 1852 | len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1853 | spin_unlock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1854 | |
| 1855 | if (len >= PATH_MAX) |
| 1856 | len = -ERANGE; |
| 1857 | else if (len > 0) { |
| 1858 | seq_escape(sf, buf, " \t\n\\"); |
| 1859 | len = 0; |
| 1860 | } |
| 1861 | kfree(buf); |
| 1862 | return len; |
| 1863 | } |
| 1864 | |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1865 | enum cgroup2_param { |
| 1866 | Opt_nsdelegate, |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1867 | Opt_memory_localevents, |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1868 | Opt_memory_recursiveprot, |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1869 | nr__cgroup2_params |
| 1870 | }; |
| 1871 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 1872 | static const struct fs_parameter_spec cgroup2_fs_parameters[] = { |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1873 | fsparam_flag("nsdelegate", Opt_nsdelegate), |
| 1874 | fsparam_flag("memory_localevents", Opt_memory_localevents), |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1875 | fsparam_flag("memory_recursiveprot", Opt_memory_recursiveprot), |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1876 | {} |
| 1877 | }; |
| 1878 | |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1879 | 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] | 1880 | { |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1881 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
| 1882 | struct fs_parse_result result; |
| 1883 | int opt; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1884 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 1885 | opt = fs_parse(fc, cgroup2_fs_parameters, param, &result); |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1886 | if (opt < 0) |
| 1887 | return opt; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1888 | |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1889 | switch (opt) { |
| 1890 | case Opt_nsdelegate: |
| 1891 | ctx->flags |= CGRP_ROOT_NS_DELEGATE; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1892 | return 0; |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1893 | case Opt_memory_localevents: |
| 1894 | ctx->flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS; |
| 1895 | return 0; |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1896 | case Opt_memory_recursiveprot: |
| 1897 | ctx->flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; |
| 1898 | return 0; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1899 | } |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1900 | return -EINVAL; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | static void apply_cgroup_root_flags(unsigned int root_flags) |
| 1904 | { |
| 1905 | if (current->nsproxy->cgroup_ns == &init_cgroup_ns) { |
| 1906 | if (root_flags & CGRP_ROOT_NS_DELEGATE) |
| 1907 | cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE; |
| 1908 | else |
| 1909 | cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE; |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1910 | |
| 1911 | if (root_flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) |
| 1912 | cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS; |
| 1913 | else |
| 1914 | cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_LOCAL_EVENTS; |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1915 | |
| 1916 | if (root_flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT) |
| 1917 | cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; |
| 1918 | else |
| 1919 | cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root) |
| 1924 | { |
| 1925 | if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) |
| 1926 | seq_puts(seq, ",nsdelegate"); |
Chris Down | 9852ae3 | 2019-05-31 22:30:22 -0700 | [diff] [blame] | 1927 | if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) |
| 1928 | seq_puts(seq, ",memory_localevents"); |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 1929 | if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT) |
| 1930 | seq_puts(seq, ",memory_recursiveprot"); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1931 | return 0; |
| 1932 | } |
| 1933 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 1934 | static int cgroup_reconfigure(struct fs_context *fc) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1935 | { |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 1936 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1937 | |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 1938 | apply_cgroup_root_flags(ctx->flags); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1939 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1942 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1943 | { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1944 | struct cgroup_subsys *ss; |
| 1945 | int ssid; |
| 1946 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1947 | INIT_LIST_HEAD(&cgrp->self.sibling); |
| 1948 | INIT_LIST_HEAD(&cgrp->self.children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1949 | INIT_LIST_HEAD(&cgrp->cset_links); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1950 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1951 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 1952 | cgrp->self.cgroup = cgrp; |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 1953 | cgrp->self.flags |= CSS_ONLINE; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1954 | cgrp->dom_cgrp = cgrp; |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 1955 | cgrp->max_descendants = INT_MAX; |
| 1956 | cgrp->max_depth = INT_MAX; |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 1957 | INIT_LIST_HEAD(&cgrp->rstat_css_list); |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1958 | prev_cputime_init(&cgrp->prev_cputime); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1959 | |
| 1960 | for_each_subsys(ss, ssid) |
| 1961 | INIT_LIST_HEAD(&cgrp->e_csets[ssid]); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1962 | |
| 1963 | init_waitqueue_head(&cgrp->offline_waitq); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1964 | INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1965 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1966 | |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 1967 | void init_cgroup_root(struct cgroup_fs_context *ctx) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1968 | { |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 1969 | struct cgroup_root *root = ctx->root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1970 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1971 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1972 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1973 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1974 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1975 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1976 | |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 1977 | root->flags = ctx->flags; |
| 1978 | if (ctx->release_agent) |
| 1979 | strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX); |
| 1980 | if (ctx->name) |
| 1981 | strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN); |
| 1982 | if (ctx->cpuset_clone_children) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1983 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 1986 | int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1987 | { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1988 | LIST_HEAD(tmp_links); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1989 | struct cgroup *root_cgrp = &root->cgrp; |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1990 | struct kernfs_syscall_ops *kf_sops; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1991 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1992 | int i, ret; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1993 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1994 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1995 | |
Zefan Li | 9732adc | 2017-04-19 10:15:59 +0800 | [diff] [blame] | 1996 | ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 1997 | 0, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1998 | if (ret) |
| 1999 | goto out; |
| 2000 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2001 | /* |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2002 | * We're accessing css_set_count without locking css_set_lock here, |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2003 | * but that's OK - it can only be increased by someone holding |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2004 | * cgroup_lock, and that's us. Later rebinding may disable |
| 2005 | * controllers on the default hierarchy and thus create new csets, |
| 2006 | * which can't be more than the existing ones. Allocate 2x. |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2007 | */ |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2008 | ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2009 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2010 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2011 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 2012 | ret = cgroup_init_root_id(root); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2013 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2014 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2015 | |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2016 | kf_sops = root == &cgrp_dfl_root ? |
| 2017 | &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops; |
| 2018 | |
| 2019 | root->kf_root = kernfs_create_root(kf_sops, |
Shaohua Li | aa81882 | 2017-07-12 11:49:51 -0700 | [diff] [blame] | 2020 | KERNFS_ROOT_CREATE_DEACTIVATED | |
Daniel Xu | 38aca30 | 2020-03-12 13:03:17 -0700 | [diff] [blame] | 2021 | KERNFS_ROOT_SUPPORT_EXPORTOP | |
| 2022 | KERNFS_ROOT_SUPPORT_USER_XATTR, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2023 | root_cgrp); |
| 2024 | if (IS_ERR(root->kf_root)) { |
| 2025 | ret = PTR_ERR(root->kf_root); |
| 2026 | goto exit_root_id; |
| 2027 | } |
| 2028 | root_cgrp->kn = root->kf_root->kn; |
Tejun Heo | d749534 | 2019-11-14 14:46:51 -0800 | [diff] [blame] | 2029 | WARN_ON_ONCE(cgroup_ino(root_cgrp) != 1); |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 2030 | root_cgrp->ancestor_ids[0] = cgroup_id(root_cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2031 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2032 | ret = css_populate_dir(&root_cgrp->self); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2033 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2034 | goto destroy_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2035 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 2036 | ret = cgroup_rstat_init(root_cgrp); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2037 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2038 | goto destroy_root; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 2039 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 2040 | ret = rebind_subsystems(root, ss_mask); |
| 2041 | if (ret) |
| 2042 | goto exit_stats; |
| 2043 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2044 | ret = cgroup_bpf_inherit(root_cgrp); |
| 2045 | WARN_ON_ONCE(ret); |
| 2046 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2047 | trace_cgroup_setup_root(root); |
| 2048 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2049 | /* |
| 2050 | * There must be no failure case after here, since rebinding takes |
| 2051 | * care of subsystems' refcounts, which are explicitly dropped in |
| 2052 | * the failure exit path. |
| 2053 | */ |
| 2054 | list_add(&root->root_list, &cgroup_roots); |
| 2055 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2056 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2057 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2058 | * Link the root cgroup in this hierarchy into all the css_set |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2059 | * objects. |
| 2060 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2061 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 2062 | hash_for_each(css_set_table, i, cset, hlist) { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2063 | link_css_set(&tmp_links, cset, root_cgrp); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 2064 | if (css_set_populated(cset)) |
| 2065 | cgroup_update_populated(root_cgrp, true); |
| 2066 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2067 | spin_unlock_irq(&css_set_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2068 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 2069 | BUG_ON(!list_empty(&root_cgrp->self.children)); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2070 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2071 | |
| 2072 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2073 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2074 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 2075 | exit_stats: |
| 2076 | cgroup_rstat_exit(root_cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2077 | destroy_root: |
| 2078 | kernfs_destroy_root(root->kf_root); |
| 2079 | root->kf_root = NULL; |
| 2080 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2081 | cgroup_exit_root_id(root); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2082 | cancel_ref: |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 2083 | percpu_ref_exit(&root_cgrp->self.refcnt); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2084 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2085 | free_cgrp_cset_links(&tmp_links); |
| 2086 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2089 | int cgroup_do_get_tree(struct fs_context *fc) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2090 | { |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2091 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2092 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 2093 | |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2094 | ctx->kfc.root = ctx->root->kf_root; |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2095 | if (fc->fs_type == &cgroup2_fs_type) |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2096 | ctx->kfc.magic = CGROUP2_SUPER_MAGIC; |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2097 | else |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2098 | ctx->kfc.magic = CGROUP_SUPER_MAGIC; |
| 2099 | ret = kernfs_get_tree(fc); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2100 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 2101 | /* |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2102 | * In non-init cgroup namespace, instead of root cgroup's dentry, |
| 2103 | * we return the dentry corresponding to the cgroupns->root_cgrp. |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2104 | */ |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2105 | if (!ret && ctx->ns != &init_cgroup_ns) { |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2106 | struct dentry *nsdentry; |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2107 | struct super_block *sb = fc->root->d_sb; |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2108 | struct cgroup *cgrp; |
| 2109 | |
| 2110 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2111 | spin_lock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2112 | |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2113 | cgrp = cset_cgroup_from_root(ctx->ns->root_cset, ctx->root); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2114 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2115 | spin_unlock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2116 | mutex_unlock(&cgroup_mutex); |
| 2117 | |
Al Viro | 399504e | 2019-01-06 11:41:29 -0500 | [diff] [blame] | 2118 | nsdentry = kernfs_node_dentry(cgrp->kn, sb); |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2119 | dput(fc->root); |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2120 | if (IS_ERR(nsdentry)) { |
Al Viro | 399504e | 2019-01-06 11:41:29 -0500 | [diff] [blame] | 2121 | deactivate_locked_super(sb); |
Al Viro | 630faf8 | 2019-11-10 11:53:27 -0500 | [diff] [blame] | 2122 | ret = PTR_ERR(nsdentry); |
| 2123 | nsdentry = NULL; |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2124 | } |
Al Viro | 630faf8 | 2019-11-10 11:53:27 -0500 | [diff] [blame] | 2125 | fc->root = nsdentry; |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2126 | } |
| 2127 | |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2128 | if (!ctx->kfc.new_sb_created) |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2129 | cgroup_put(&ctx->root->cgrp); |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2130 | |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2131 | return ret; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2132 | } |
| 2133 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2134 | /* |
| 2135 | * Destroy a cgroup filesystem context. |
| 2136 | */ |
| 2137 | static void cgroup_fs_context_free(struct fs_context *fc) |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2138 | { |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2139 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
| 2140 | |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 2141 | kfree(ctx->name); |
| 2142 | kfree(ctx->release_agent); |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2143 | put_cgroup_ns(ctx->ns); |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2144 | kernfs_free_fs_context(fc); |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2145 | kfree(ctx); |
| 2146 | } |
| 2147 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2148 | static int cgroup_get_tree(struct fs_context *fc) |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2149 | { |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2150 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2151 | int ret; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2152 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2153 | cgrp_dfl_visible = true; |
| 2154 | cgroup_get_live(&cgrp_dfl_root.cgrp); |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 2155 | ctx->root = &cgrp_dfl_root; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2156 | |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2157 | ret = cgroup_do_get_tree(fc); |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame] | 2158 | if (!ret) |
| 2159 | apply_cgroup_root_flags(ctx->flags); |
| 2160 | return ret; |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2161 | } |
| 2162 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2163 | static const struct fs_context_operations cgroup_fs_context_ops = { |
| 2164 | .free = cgroup_fs_context_free, |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 2165 | .parse_param = cgroup2_parse_param, |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2166 | .get_tree = cgroup_get_tree, |
| 2167 | .reconfigure = cgroup_reconfigure, |
| 2168 | }; |
| 2169 | |
| 2170 | static const struct fs_context_operations cgroup1_fs_context_ops = { |
| 2171 | .free = cgroup_fs_context_free, |
Al Viro | 8d2451f | 2019-01-17 00:15:11 -0500 | [diff] [blame] | 2172 | .parse_param = cgroup1_parse_param, |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2173 | .get_tree = cgroup1_get_tree, |
| 2174 | .reconfigure = cgroup1_reconfigure, |
| 2175 | }; |
| 2176 | |
| 2177 | /* |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2178 | * Initialise the cgroup filesystem creation/reconfiguration context. Notably, |
| 2179 | * we select the namespace we're going to use. |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2180 | */ |
| 2181 | static int cgroup_init_fs_context(struct fs_context *fc) |
| 2182 | { |
| 2183 | struct cgroup_fs_context *ctx; |
| 2184 | |
| 2185 | ctx = kzalloc(sizeof(struct cgroup_fs_context), GFP_KERNEL); |
| 2186 | if (!ctx) |
| 2187 | return -ENOMEM; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2188 | |
Al Viro | cca8f32 | 2019-01-17 10:14:26 -0500 | [diff] [blame] | 2189 | ctx->ns = current->nsproxy->cgroup_ns; |
| 2190 | get_cgroup_ns(ctx->ns); |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2191 | fc->fs_private = &ctx->kfc; |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2192 | if (fc->fs_type == &cgroup2_fs_type) |
| 2193 | fc->ops = &cgroup_fs_context_ops; |
| 2194 | else |
| 2195 | fc->ops = &cgroup1_fs_context_ops; |
Al Viro | f7a9945 | 2019-05-12 12:42:58 -0400 | [diff] [blame] | 2196 | put_user_ns(fc->user_ns); |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 2197 | fc->user_ns = get_user_ns(ctx->ns->user_ns); |
| 2198 | fc->global = true; |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2199 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2200 | } |
| 2201 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2202 | static void cgroup_kill_sb(struct super_block *sb) |
| 2203 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2204 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2205 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2206 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2207 | /* |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 2208 | * If @root doesn't have any children, start killing it. |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2209 | * This prevents new mounts by disabling percpu_ref_tryget_live(). |
Li Zefan | 1f779fb | 2014-06-04 16:48:15 +0800 | [diff] [blame] | 2210 | * |
| 2211 | * And don't kill the default root. |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2212 | */ |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 2213 | if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root && |
Quanyang Wang | 04f8ef5 | 2021-10-18 15:56:23 +0800 | [diff] [blame] | 2214 | !percpu_ref_is_dying(&root->cgrp.self.refcnt)) { |
| 2215 | cgroup_bpf_offline(&root->cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2216 | percpu_ref_kill(&root->cgrp.self.refcnt); |
Quanyang Wang | 04f8ef5 | 2021-10-18 15:56:23 +0800 | [diff] [blame] | 2217 | } |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 2218 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2219 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2222 | struct file_system_type cgroup_fs_type = { |
Al Viro | 8d2451f | 2019-01-17 00:15:11 -0500 | [diff] [blame] | 2223 | .name = "cgroup", |
| 2224 | .init_fs_context = cgroup_init_fs_context, |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 2225 | .parameters = cgroup1_fs_parameters, |
Al Viro | 8d2451f | 2019-01-17 00:15:11 -0500 | [diff] [blame] | 2226 | .kill_sb = cgroup_kill_sb, |
| 2227 | .fs_flags = FS_USERNS_MOUNT, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2228 | }; |
| 2229 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2230 | static struct file_system_type cgroup2_fs_type = { |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 2231 | .name = "cgroup2", |
| 2232 | .init_fs_context = cgroup_init_fs_context, |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 2233 | .parameters = cgroup2_fs_parameters, |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 2234 | .kill_sb = cgroup_kill_sb, |
| 2235 | .fs_flags = FS_USERNS_MOUNT, |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2236 | }; |
| 2237 | |
Al Viro | d5f68d3 | 2019-05-13 12:33:22 -0400 | [diff] [blame] | 2238 | #ifdef CONFIG_CPUSETS |
| 2239 | static const struct fs_context_operations cpuset_fs_context_ops = { |
| 2240 | .get_tree = cgroup1_get_tree, |
| 2241 | .free = cgroup_fs_context_free, |
| 2242 | }; |
| 2243 | |
| 2244 | /* |
| 2245 | * This is ugly, but preserves the userspace API for existing cpuset |
| 2246 | * users. If someone tries to mount the "cpuset" filesystem, we |
| 2247 | * silently switch it to mount "cgroup" instead |
| 2248 | */ |
| 2249 | static int cpuset_init_fs_context(struct fs_context *fc) |
| 2250 | { |
| 2251 | char *agent = kstrdup("/sbin/cpuset_release_agent", GFP_USER); |
| 2252 | struct cgroup_fs_context *ctx; |
| 2253 | int err; |
| 2254 | |
| 2255 | err = cgroup_init_fs_context(fc); |
| 2256 | if (err) { |
| 2257 | kfree(agent); |
| 2258 | return err; |
| 2259 | } |
| 2260 | |
| 2261 | fc->ops = &cpuset_fs_context_ops; |
| 2262 | |
| 2263 | ctx = cgroup_fc2context(fc); |
| 2264 | ctx->subsys_mask = 1 << cpuset_cgrp_id; |
| 2265 | ctx->flags |= CGRP_ROOT_NOPREFIX; |
| 2266 | ctx->release_agent = agent; |
| 2267 | |
| 2268 | get_filesystem(&cgroup_fs_type); |
| 2269 | put_filesystem(fc->fs_type); |
| 2270 | fc->fs_type = &cgroup_fs_type; |
| 2271 | |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
| 2275 | static struct file_system_type cpuset_fs_type = { |
| 2276 | .name = "cpuset", |
| 2277 | .init_fs_context = cpuset_init_fs_context, |
| 2278 | .fs_flags = FS_USERNS_MOUNT, |
| 2279 | }; |
| 2280 | #endif |
| 2281 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2282 | int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2283 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2284 | { |
| 2285 | struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2286 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2287 | return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2288 | } |
| 2289 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2290 | int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2291 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2292 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2293 | int ret; |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2294 | |
| 2295 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2296 | spin_lock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2297 | |
| 2298 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns); |
| 2299 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2300 | spin_unlock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2301 | mutex_unlock(&cgroup_mutex); |
| 2302 | |
| 2303 | return ret; |
| 2304 | } |
| 2305 | EXPORT_SYMBOL_GPL(cgroup_path_ns); |
| 2306 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2307 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2308 | * 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] | 2309 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2310 | * @buf: the buffer to write the path into |
| 2311 | * @buflen: the length of the buffer |
| 2312 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2313 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 2314 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 2315 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 2316 | * cgroup controller callbacks. |
| 2317 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2318 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2319 | */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2320 | 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] | 2321 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2322 | struct cgroup_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2323 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2324 | int hierarchy_id = 1; |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2325 | int ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2326 | |
| 2327 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2328 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2329 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2330 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 2331 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2332 | if (root) { |
| 2333 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2334 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2335 | } else { |
| 2336 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2337 | ret = strlcpy(buf, "/", buflen); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2338 | } |
| 2339 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2340 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2341 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2342 | return ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2343 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2344 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2345 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2346 | /** |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2347 | * 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] | 2348 | * @task: target task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2349 | * @mgctx: target migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2350 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2351 | * Add @task, which is a migration target, to @mgctx->tset. This function |
| 2352 | * becomes noop if @task doesn't need to be migrated. @task's css_set |
| 2353 | * should have been added as a migration source and @task->cg_list will be |
| 2354 | * moved from the css_set's tasks list to mg_tasks one. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2355 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2356 | static void cgroup_migrate_add_task(struct task_struct *task, |
| 2357 | struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2358 | { |
| 2359 | struct css_set *cset; |
| 2360 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2361 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2362 | |
| 2363 | /* @task either already exited or can't exit until the end */ |
| 2364 | if (task->flags & PF_EXITING) |
| 2365 | return; |
| 2366 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 2367 | /* cgroup_threadgroup_rwsem protects racing against forks */ |
| 2368 | WARN_ON_ONCE(list_empty(&task->cg_list)); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2369 | |
| 2370 | cset = task_css_set(task); |
| 2371 | if (!cset->mg_src_cgrp) |
| 2372 | return; |
| 2373 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2374 | mgctx->tset.nr_tasks++; |
| 2375 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2376 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 2377 | if (list_empty(&cset->mg_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2378 | list_add_tail(&cset->mg_node, |
| 2379 | &mgctx->tset.src_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2380 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
Tejun Heo | d8ebf51 | 2017-01-15 19:03:40 -0500 | [diff] [blame] | 2381 | list_add_tail(&cset->mg_dst_cset->mg_node, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2382 | &mgctx->tset.dst_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2383 | } |
| 2384 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2385 | /** |
| 2386 | * cgroup_taskset_first - reset taskset and return the first task |
| 2387 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2388 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2389 | * |
| 2390 | * @tset iteration is initialized and the first task is returned. |
| 2391 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2392 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, |
| 2393 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2394 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2395 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 2396 | tset->cur_task = NULL; |
| 2397 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2398 | return cgroup_taskset_next(tset, dst_cssp); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2399 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2400 | |
| 2401 | /** |
| 2402 | * cgroup_taskset_next - iterate to the next task in taskset |
| 2403 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2404 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2405 | * |
| 2406 | * Return the next task in @tset. Iteration must have been initialized |
| 2407 | * with cgroup_taskset_first(). |
| 2408 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2409 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, |
| 2410 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2411 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2412 | struct css_set *cset = tset->cur_cset; |
| 2413 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2414 | |
Kees Cook | d20d30e | 2021-08-27 17:02:55 -0700 | [diff] [blame] | 2415 | while (CGROUP_HAS_SUBSYS_CONFIG && &cset->mg_node != tset->csets) { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2416 | if (!task) |
| 2417 | task = list_first_entry(&cset->mg_tasks, |
| 2418 | struct task_struct, cg_list); |
| 2419 | else |
| 2420 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2421 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2422 | if (&task->cg_list != &cset->mg_tasks) { |
| 2423 | tset->cur_cset = cset; |
| 2424 | tset->cur_task = task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2425 | |
| 2426 | /* |
| 2427 | * This function may be called both before and |
| 2428 | * after cgroup_taskset_migrate(). The two cases |
| 2429 | * can be distinguished by looking at whether @cset |
| 2430 | * has its ->mg_dst_cset set. |
| 2431 | */ |
| 2432 | if (cset->mg_dst_cset) |
| 2433 | *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid]; |
| 2434 | else |
| 2435 | *dst_cssp = cset->subsys[tset->ssid]; |
| 2436 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2437 | return task; |
| 2438 | } |
| 2439 | |
| 2440 | cset = list_next_entry(cset, mg_node); |
| 2441 | task = NULL; |
| 2442 | } |
| 2443 | |
| 2444 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2445 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2446 | |
| 2447 | /** |
Yang Li | 2ca11b0 | 2021-05-26 10:49:09 +0800 | [diff] [blame] | 2448 | * cgroup_migrate_execute - migrate a taskset |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2449 | * @mgctx: migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2450 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2451 | * Migrate tasks in @mgctx as setup by migration preparation functions. |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2452 | * This function fails iff one of the ->can_attach callbacks fails and |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2453 | * guarantees that either all or none of the tasks in @mgctx are migrated. |
| 2454 | * @mgctx is consumed regardless of success. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2455 | */ |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2456 | static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2457 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2458 | struct cgroup_taskset *tset = &mgctx->tset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2459 | struct cgroup_subsys *ss; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2460 | struct task_struct *task, *tmp_task; |
| 2461 | struct css_set *cset, *tmp_cset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2462 | int ssid, failed_ssid, ret; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2463 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2464 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2465 | if (tset->nr_tasks) { |
| 2466 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2467 | if (ss->can_attach) { |
| 2468 | tset->ssid = ssid; |
| 2469 | ret = ss->can_attach(tset); |
| 2470 | if (ret) { |
| 2471 | failed_ssid = ssid; |
| 2472 | goto out_cancel_attach; |
| 2473 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2474 | } |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2475 | } while_each_subsys_mask(); |
| 2476 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2477 | |
| 2478 | /* |
| 2479 | * Now that we're guaranteed success, proceed to move all tasks to |
| 2480 | * the new cgroup. There are no failure cases after here, so this |
| 2481 | * is the commit point. |
| 2482 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2483 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2484 | list_for_each_entry(cset, &tset->src_csets, mg_node) { |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2485 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) { |
| 2486 | struct css_set *from_cset = task_css_set(task); |
| 2487 | struct css_set *to_cset = cset->mg_dst_cset; |
| 2488 | |
| 2489 | get_css_set(to_cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2490 | to_cset->nr_tasks++; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2491 | css_set_move_task(task, from_cset, to_cset, true); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2492 | from_cset->nr_tasks--; |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 2493 | /* |
| 2494 | * If the source or destination cgroup is frozen, |
| 2495 | * the task might require to change its state. |
| 2496 | */ |
| 2497 | cgroup_freezer_migrate_task(task, from_cset->dfl_cgrp, |
| 2498 | to_cset->dfl_cgrp); |
| 2499 | put_css_set_locked(from_cset); |
| 2500 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2501 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2502 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2503 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2504 | |
| 2505 | /* |
| 2506 | * Migration is committed, all target tasks are now on dst_csets. |
| 2507 | * Nothing is sensitive to fork() after this point. Notify |
| 2508 | * controllers that migration is complete. |
| 2509 | */ |
| 2510 | tset->csets = &tset->dst_csets; |
| 2511 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2512 | if (tset->nr_tasks) { |
| 2513 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2514 | if (ss->attach) { |
| 2515 | tset->ssid = ssid; |
| 2516 | ss->attach(tset); |
| 2517 | } |
| 2518 | } while_each_subsys_mask(); |
| 2519 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2520 | |
| 2521 | ret = 0; |
| 2522 | goto out_release_tset; |
| 2523 | |
| 2524 | out_cancel_attach: |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2525 | if (tset->nr_tasks) { |
| 2526 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2527 | if (ssid == failed_ssid) |
| 2528 | break; |
| 2529 | if (ss->cancel_attach) { |
| 2530 | tset->ssid = ssid; |
| 2531 | ss->cancel_attach(tset); |
| 2532 | } |
| 2533 | } while_each_subsys_mask(); |
| 2534 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2535 | out_release_tset: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2536 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2537 | list_splice_init(&tset->dst_csets, &tset->src_csets); |
| 2538 | list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { |
| 2539 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
| 2540 | list_del_init(&cset->mg_node); |
| 2541 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2542 | spin_unlock_irq(&css_set_lock); |
Waiman Long | c4fa6c4 | 2017-09-21 09:54:13 -0400 | [diff] [blame] | 2543 | |
| 2544 | /* |
| 2545 | * Re-initialize the cgroup_taskset structure in case it is reused |
| 2546 | * again in another cgroup_migrate_add_task()/cgroup_migrate_execute() |
| 2547 | * iteration. |
| 2548 | */ |
| 2549 | tset->nr_tasks = 0; |
| 2550 | tset->csets = &tset->src_csets; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2551 | return ret; |
| 2552 | } |
| 2553 | |
| 2554 | /** |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2555 | * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2556 | * @dst_cgrp: destination cgroup to test |
| 2557 | * |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2558 | * On the default hierarchy, except for the mixable, (possible) thread root |
| 2559 | * and threaded cgroups, subtree_control must be zero for migration |
| 2560 | * destination cgroups with tasks so that child cgroups don't compete |
| 2561 | * against tasks. |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2562 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2563 | int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp) |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2564 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2565 | /* v1 doesn't have any restriction */ |
| 2566 | if (!cgroup_on_dfl(dst_cgrp)) |
| 2567 | return 0; |
| 2568 | |
| 2569 | /* verify @dst_cgrp can host resources */ |
| 2570 | if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp)) |
| 2571 | return -EOPNOTSUPP; |
| 2572 | |
| 2573 | /* mixables don't care */ |
| 2574 | if (cgroup_is_mixable(dst_cgrp)) |
| 2575 | return 0; |
| 2576 | |
| 2577 | /* |
| 2578 | * If @dst_cgrp is already or can become a thread root or is |
| 2579 | * threaded, it doesn't matter. |
| 2580 | */ |
| 2581 | if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp)) |
| 2582 | return 0; |
| 2583 | |
| 2584 | /* apply no-internal-process constraint */ |
| 2585 | if (dst_cgrp->subtree_control) |
| 2586 | return -EBUSY; |
| 2587 | |
| 2588 | return 0; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2592 | * cgroup_migrate_finish - cleanup after attach |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2593 | * @mgctx: migration context |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2594 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2595 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 2596 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2597 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2598 | void cgroup_migrate_finish(struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2599 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2600 | LIST_HEAD(preloaded); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2601 | struct css_set *cset, *tmp_cset; |
| 2602 | |
| 2603 | lockdep_assert_held(&cgroup_mutex); |
| 2604 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2605 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2606 | |
| 2607 | list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded); |
| 2608 | list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded); |
| 2609 | |
| 2610 | list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2611 | cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2612 | cset->mg_dst_cgrp = NULL; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2613 | cset->mg_dst_cset = NULL; |
| 2614 | list_del_init(&cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2615 | put_css_set_locked(cset); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2616 | } |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2617 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2618 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /** |
| 2622 | * cgroup_migrate_add_src - add a migration source css_set |
| 2623 | * @src_cset: the source css_set to add |
| 2624 | * @dst_cgrp: the destination cgroup |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2625 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2626 | * |
| 2627 | * 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] | 2628 | * @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] | 2629 | * up by cgroup_migrate_finish(). |
| 2630 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2631 | * This function may be called without holding cgroup_threadgroup_rwsem |
| 2632 | * even if the target is a process. Threads may be created and destroyed |
| 2633 | * but as long as cgroup_mutex is not dropped, no new css_set can be put |
| 2634 | * into play and the preloaded css_sets are guaranteed to cover all |
| 2635 | * migrations. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2636 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2637 | void cgroup_migrate_add_src(struct css_set *src_cset, |
| 2638 | struct cgroup *dst_cgrp, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2639 | struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2640 | { |
| 2641 | struct cgroup *src_cgrp; |
| 2642 | |
| 2643 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2644 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2645 | |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 2646 | /* |
| 2647 | * If ->dead, @src_set is associated with one or more dead cgroups |
| 2648 | * and doesn't contain any migratable tasks. Ignore it early so |
| 2649 | * that the rest of migration path doesn't get confused by it. |
| 2650 | */ |
| 2651 | if (src_cset->dead) |
| 2652 | return; |
| 2653 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2654 | if (!list_empty(&src_cset->mg_preload_node)) |
| 2655 | return; |
| 2656 | |
Wei Yang | 1815775 | 2021-12-14 00:46:07 +0000 | [diff] [blame] | 2657 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 2658 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2659 | WARN_ON(src_cset->mg_src_cgrp); |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2660 | WARN_ON(src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2661 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 2662 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 2663 | |
| 2664 | src_cset->mg_src_cgrp = src_cgrp; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2665 | src_cset->mg_dst_cgrp = dst_cgrp; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2666 | get_css_set(src_cset); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2667 | list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | /** |
| 2671 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2672 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2673 | * |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2674 | * 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] | 2675 | * preloaded to @mgctx->preloaded_src_csets. This function looks up and |
| 2676 | * pins all destination css_sets, links each to its source, and append them |
| 2677 | * to @mgctx->preloaded_dst_csets. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2678 | * |
| 2679 | * This function must be called after cgroup_migrate_add_src() has been |
| 2680 | * called on each migration source css_set. After migration is performed |
| 2681 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2682 | * @mgctx. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2683 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2684 | int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2685 | { |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2686 | struct css_set *src_cset, *tmp_cset; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2687 | |
| 2688 | lockdep_assert_held(&cgroup_mutex); |
| 2689 | |
| 2690 | /* 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] | 2691 | list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets, |
| 2692 | mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2693 | struct css_set *dst_cset; |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2694 | struct cgroup_subsys *ss; |
| 2695 | int ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2696 | |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2697 | dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2698 | if (!dst_cset) |
Shakeel Butt | d6e486e | 2019-04-03 16:03:54 -0700 | [diff] [blame] | 2699 | return -ENOMEM; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2700 | |
| 2701 | 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] | 2702 | |
| 2703 | /* |
| 2704 | * If src cset equals dst, it's noop. Drop the src. |
| 2705 | * cgroup_migrate() will skip the cset too. Note that we |
| 2706 | * can't handle src == dst as some nodes are used by both. |
| 2707 | */ |
| 2708 | if (src_cset == dst_cset) { |
| 2709 | src_cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2710 | src_cset->mg_dst_cgrp = NULL; |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2711 | list_del_init(&src_cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2712 | put_css_set(src_cset); |
| 2713 | put_css_set(dst_cset); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2714 | continue; |
| 2715 | } |
| 2716 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2717 | src_cset->mg_dst_cset = dst_cset; |
| 2718 | |
| 2719 | if (list_empty(&dst_cset->mg_preload_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2720 | list_add_tail(&dst_cset->mg_preload_node, |
| 2721 | &mgctx->preloaded_dst_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2722 | else |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2723 | put_css_set(dst_cset); |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2724 | |
| 2725 | for_each_subsys(ss, ssid) |
| 2726 | if (src_cset->subsys[ssid] != dst_cset->subsys[ssid]) |
| 2727 | mgctx->ss_mask |= 1 << ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2728 | } |
| 2729 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2730 | return 0; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | /** |
| 2734 | * cgroup_migrate - migrate a process or task to a cgroup |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2735 | * @leader: the leader of the process or the task to migrate |
| 2736 | * @threadgroup: whether @leader points to the whole process or a single task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2737 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2738 | * |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2739 | * Migrate a process or task denoted by @leader. If migrating a process, |
| 2740 | * the caller must be holding cgroup_threadgroup_rwsem. The caller is also |
| 2741 | * responsible for invoking cgroup_migrate_add_src() and |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2742 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 2743 | * function and following up with cgroup_migrate_finish(). |
| 2744 | * |
| 2745 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 2746 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 2747 | * failure, when migrating multiple targets, the success or failure can be |
| 2748 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 2749 | * actually starting migrating. |
| 2750 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2751 | int cgroup_migrate(struct task_struct *leader, bool threadgroup, |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2752 | struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2753 | { |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2754 | struct task_struct *task; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2755 | |
| 2756 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2757 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2758 | * already PF_EXITING could be freed from underneath us unless we |
| 2759 | * take an rcu_read_lock. |
| 2760 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2761 | spin_lock_irq(&css_set_lock); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2762 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2763 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2764 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2765 | cgroup_migrate_add_task(task, mgctx); |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2766 | if (!threadgroup) |
| 2767 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2768 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2769 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2770 | spin_unlock_irq(&css_set_lock); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2771 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2772 | return cgroup_migrate_execute(mgctx); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2775 | /** |
| 2776 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2777 | * @dst_cgrp: the cgroup to attach to |
| 2778 | * @leader: the task or the leader of the threadgroup to be attached |
| 2779 | * @threadgroup: attach the whole threadgroup? |
| 2780 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2781 | * Call holding cgroup_mutex and cgroup_threadgroup_rwsem. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2782 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2783 | int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, |
| 2784 | bool threadgroup) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2785 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2786 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2787 | struct task_struct *task; |
Christian Brauner | 6df970e | 2020-02-05 14:26:18 +0100 | [diff] [blame] | 2788 | int ret = 0; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2789 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2790 | /* look up all src csets */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2791 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2792 | rcu_read_lock(); |
| 2793 | task = leader; |
| 2794 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2795 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2796 | if (!threadgroup) |
| 2797 | break; |
| 2798 | } while_each_thread(leader, task); |
| 2799 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2800 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2801 | |
| 2802 | /* prepare dst csets and commit */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2803 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2804 | if (!ret) |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2805 | ret = cgroup_migrate(leader, threadgroup, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2806 | |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2807 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2808 | |
| 2809 | if (!ret) |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 2810 | TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2811 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2812 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2815 | struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, |
| 2816 | bool *locked) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2817 | __acquires(&cgroup_threadgroup_rwsem) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2818 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2819 | struct task_struct *tsk; |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2820 | pid_t pid; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2821 | |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2822 | if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2823 | return ERR_PTR(-EINVAL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2824 | |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2825 | /* |
| 2826 | * If we migrate a single thread, we don't care about threadgroup |
| 2827 | * stability. If the thread is `current`, it won't exit(2) under our |
| 2828 | * hands or change PID through exec(2). We exclude |
| 2829 | * cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write |
| 2830 | * callers by cgroup_mutex. |
| 2831 | * Therefore, we can skip the global lock. |
| 2832 | */ |
| 2833 | lockdep_assert_held(&cgroup_mutex); |
| 2834 | if (pid || threadgroup) { |
| 2835 | percpu_down_write(&cgroup_threadgroup_rwsem); |
| 2836 | *locked = true; |
| 2837 | } else { |
| 2838 | *locked = false; |
| 2839 | } |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2840 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2841 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2842 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2843 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2844 | if (!tsk) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2845 | tsk = ERR_PTR(-ESRCH); |
| 2846 | goto out_unlock_threadgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2847 | } |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2848 | } else { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2849 | tsk = current; |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2850 | } |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2851 | |
| 2852 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2853 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2854 | |
| 2855 | /* |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2856 | * kthreads may acquire PF_NO_SETAFFINITY during initialization. |
| 2857 | * If userland migrates such a kthread to a non-root cgroup, it can |
| 2858 | * become trapped in a cpuset, or RT kthread may be born in a |
| 2859 | * cgroup with no rt_runtime allocated. Just say no. |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2860 | */ |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2861 | if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2862 | tsk = ERR_PTR(-EINVAL); |
| 2863 | goto out_unlock_threadgroup; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2864 | } |
| 2865 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2866 | get_task_struct(tsk); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2867 | goto out_unlock_rcu; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2868 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2869 | out_unlock_threadgroup: |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2870 | if (*locked) { |
| 2871 | percpu_up_write(&cgroup_threadgroup_rwsem); |
| 2872 | *locked = false; |
| 2873 | } |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2874 | out_unlock_rcu: |
| 2875 | rcu_read_unlock(); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2876 | return tsk; |
| 2877 | } |
| 2878 | |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2879 | void cgroup_procs_write_finish(struct task_struct *task, bool locked) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2880 | __releases(&cgroup_threadgroup_rwsem) |
| 2881 | { |
| 2882 | struct cgroup_subsys *ss; |
| 2883 | int ssid; |
| 2884 | |
| 2885 | /* release reference from cgroup_procs_write_start() */ |
| 2886 | put_task_struct(task); |
| 2887 | |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 2888 | if (locked) |
| 2889 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 5cf1cac | 2016-04-21 19:06:48 -0400 | [diff] [blame] | 2890 | for_each_subsys(ss, ssid) |
| 2891 | if (ss->post_attach) |
| 2892 | ss->post_attach(); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2893 | } |
| 2894 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 2895 | 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] | 2896 | { |
| 2897 | struct cgroup_subsys *ss; |
| 2898 | bool printed = false; |
| 2899 | int ssid; |
| 2900 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2901 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 2902 | if (printed) |
| 2903 | seq_putc(seq, ' '); |
Markus Elfring | 85db002 | 2019-07-02 19:26:59 +0200 | [diff] [blame] | 2904 | seq_puts(seq, ss->name); |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 2905 | printed = true; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2906 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2907 | if (printed) |
| 2908 | seq_putc(seq, '\n'); |
| 2909 | } |
| 2910 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2911 | /* show controllers which are enabled from the parent */ |
| 2912 | static int cgroup_controllers_show(struct seq_file *seq, void *v) |
| 2913 | { |
| 2914 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2915 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 2916 | cgroup_print_ss_mask(seq, cgroup_control(cgrp)); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2917 | return 0; |
| 2918 | } |
| 2919 | |
| 2920 | /* show controllers which are enabled for a given cgroup's children */ |
| 2921 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) |
| 2922 | { |
| 2923 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2924 | |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 2925 | cgroup_print_ss_mask(seq, cgrp->subtree_control); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2926 | return 0; |
| 2927 | } |
| 2928 | |
| 2929 | /** |
| 2930 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy |
| 2931 | * @cgrp: root of the subtree to update csses for |
| 2932 | * |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2933 | * @cgrp's control masks have changed and its subtree's css associations |
| 2934 | * need to be updated accordingly. This function looks up all css_sets |
| 2935 | * which are attached to the subtree, creates the matching updated css_sets |
| 2936 | * and migrates the tasks to the new ones. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2937 | */ |
| 2938 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) |
| 2939 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2940 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2941 | struct cgroup_subsys_state *d_css; |
| 2942 | struct cgroup *dsct; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2943 | struct css_set *src_cset; |
| 2944 | int ret; |
| 2945 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2946 | lockdep_assert_held(&cgroup_mutex); |
| 2947 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2948 | percpu_down_write(&cgroup_threadgroup_rwsem); |
| 2949 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2950 | /* look up all csses currently attached to @cgrp's subtree */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2951 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2952 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2953 | struct cgrp_cset_link *link; |
| 2954 | |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2955 | list_for_each_entry(link, &dsct->cset_links, cset_link) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2956 | cgroup_migrate_add_src(link->cset, dsct, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2957 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2958 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2959 | |
| 2960 | /* NULL dst indicates self on default hierarchy */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2961 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2962 | if (ret) |
| 2963 | goto out_finish; |
| 2964 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2965 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2966 | 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] | 2967 | struct task_struct *task, *ntask; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2968 | |
Tejun Heo | 1026507 | 2015-09-11 15:00:22 -0400 | [diff] [blame] | 2969 | /* all tasks in src_csets need to be migrated */ |
| 2970 | list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2971 | cgroup_migrate_add_task(task, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2972 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2973 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2974 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2975 | ret = cgroup_migrate_execute(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2976 | out_finish: |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2977 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2978 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2979 | return ret; |
| 2980 | } |
| 2981 | |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2982 | /** |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2983 | * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2984 | * @cgrp: root of the target subtree |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2985 | * |
| 2986 | * Because css offlining is asynchronous, userland may try to re-enable a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2987 | * controller while the previous css is still around. This function grabs |
| 2988 | * cgroup_mutex and drains the previous css instances of @cgrp's subtree. |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2989 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2990 | void cgroup_lock_and_drain_offline(struct cgroup *cgrp) |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2991 | __acquires(&cgroup_mutex) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2992 | { |
| 2993 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2994 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2995 | struct cgroup_subsys *ss; |
| 2996 | int ssid; |
| 2997 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2998 | restart: |
| 2999 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3000 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3001 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3002 | for_each_subsys(ss, ssid) { |
| 3003 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3004 | DEFINE_WAIT(wait); |
| 3005 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3006 | if (!css || !percpu_ref_is_dying(&css->refcnt)) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3007 | continue; |
| 3008 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 3009 | cgroup_get_live(dsct); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3010 | prepare_to_wait(&dsct->offline_waitq, &wait, |
| 3011 | TASK_UNINTERRUPTIBLE); |
| 3012 | |
| 3013 | mutex_unlock(&cgroup_mutex); |
| 3014 | schedule(); |
| 3015 | finish_wait(&dsct->offline_waitq, &wait); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3016 | |
| 3017 | cgroup_put(dsct); |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3018 | goto restart; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3019 | } |
| 3020 | } |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3021 | } |
| 3022 | |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3023 | /** |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3024 | * cgroup_save_control - save control masks and dom_cgrp of a subtree |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3025 | * @cgrp: root of the target subtree |
| 3026 | * |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3027 | * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the |
| 3028 | * respective old_ prefixed fields for @cgrp's subtree including @cgrp |
| 3029 | * itself. |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3030 | */ |
| 3031 | static void cgroup_save_control(struct cgroup *cgrp) |
| 3032 | { |
| 3033 | struct cgroup *dsct; |
| 3034 | struct cgroup_subsys_state *d_css; |
| 3035 | |
| 3036 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 3037 | dsct->old_subtree_control = dsct->subtree_control; |
| 3038 | dsct->old_subtree_ss_mask = dsct->subtree_ss_mask; |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3039 | dsct->old_dom_cgrp = dsct->dom_cgrp; |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | /** |
| 3044 | * cgroup_propagate_control - refresh control masks of a subtree |
| 3045 | * @cgrp: root of the target subtree |
| 3046 | * |
| 3047 | * For @cgrp and its subtree, ensure ->subtree_ss_mask matches |
| 3048 | * ->subtree_control and propagate controller availability through the |
| 3049 | * subtree so that descendants don't have unavailable controllers enabled. |
| 3050 | */ |
| 3051 | static void cgroup_propagate_control(struct cgroup *cgrp) |
| 3052 | { |
| 3053 | struct cgroup *dsct; |
| 3054 | struct cgroup_subsys_state *d_css; |
| 3055 | |
| 3056 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 3057 | dsct->subtree_control &= cgroup_control(dsct); |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3058 | dsct->subtree_ss_mask = |
| 3059 | cgroup_calc_subtree_ss_mask(dsct->subtree_control, |
| 3060 | cgroup_ss_mask(dsct)); |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | |
| 3064 | /** |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3065 | * cgroup_restore_control - restore control masks and dom_cgrp of a subtree |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3066 | * @cgrp: root of the target subtree |
| 3067 | * |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3068 | * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the |
| 3069 | * respective old_ prefixed fields for @cgrp's subtree including @cgrp |
| 3070 | * itself. |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3071 | */ |
| 3072 | static void cgroup_restore_control(struct cgroup *cgrp) |
| 3073 | { |
| 3074 | struct cgroup *dsct; |
| 3075 | struct cgroup_subsys_state *d_css; |
| 3076 | |
| 3077 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
| 3078 | dsct->subtree_control = dsct->old_subtree_control; |
| 3079 | dsct->subtree_ss_mask = dsct->old_subtree_ss_mask; |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3080 | dsct->dom_cgrp = dsct->old_dom_cgrp; |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3081 | } |
| 3082 | } |
| 3083 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 3084 | static bool css_visible(struct cgroup_subsys_state *css) |
| 3085 | { |
| 3086 | struct cgroup_subsys *ss = css->ss; |
| 3087 | struct cgroup *cgrp = css->cgroup; |
| 3088 | |
| 3089 | if (cgroup_control(cgrp) & (1 << ss->id)) |
| 3090 | return true; |
| 3091 | if (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) |
| 3092 | return false; |
| 3093 | return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl; |
| 3094 | } |
| 3095 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3096 | /** |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3097 | * cgroup_apply_control_enable - enable or show csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3098 | * @cgrp: root of the target subtree |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3099 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3100 | * 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] | 3101 | * visible. A css is created invisible if it's being implicitly enabled |
| 3102 | * through dependency. An invisible css is made visible when the userland |
| 3103 | * explicitly enables it. |
| 3104 | * |
| 3105 | * Returns 0 on success, -errno on failure. On failure, csses which have |
| 3106 | * been processed already aren't cleaned up. The caller is responsible for |
Masahiro Yamada | 8a1115f | 2017-03-09 16:16:31 -0800 | [diff] [blame] | 3107 | * cleaning up with cgroup_apply_control_disable(). |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3108 | */ |
| 3109 | static int cgroup_apply_control_enable(struct cgroup *cgrp) |
| 3110 | { |
| 3111 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3112 | struct cgroup_subsys_state *d_css; |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3113 | struct cgroup_subsys *ss; |
| 3114 | int ssid, ret; |
| 3115 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3116 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3117 | for_each_subsys(ss, ssid) { |
| 3118 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3119 | |
| 3120 | if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) |
| 3121 | continue; |
| 3122 | |
| 3123 | if (!css) { |
| 3124 | css = css_create(dsct, ss); |
| 3125 | if (IS_ERR(css)) |
| 3126 | return PTR_ERR(css); |
| 3127 | } |
| 3128 | |
Michal Koutný | 3bc0bb3 | 2020-01-09 16:05:59 +0100 | [diff] [blame] | 3129 | WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); |
| 3130 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 3131 | if (css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3132 | ret = css_populate_dir(css); |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3133 | if (ret) |
| 3134 | return ret; |
| 3135 | } |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | return 0; |
| 3140 | } |
| 3141 | |
| 3142 | /** |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3143 | * cgroup_apply_control_disable - kill or hide csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3144 | * @cgrp: root of the target subtree |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3145 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3146 | * 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] | 3147 | * cgroup_ss_mask() and cgroup_visible_mask(). |
| 3148 | * |
| 3149 | * A css is hidden when the userland requests it to be disabled while other |
| 3150 | * subsystems are still depending on it. The css must not actively control |
| 3151 | * resources and be in the vanilla state if it's made visible again later. |
| 3152 | * Controllers which may be depended upon should provide ->css_reset() for |
| 3153 | * this purpose. |
| 3154 | */ |
| 3155 | static void cgroup_apply_control_disable(struct cgroup *cgrp) |
| 3156 | { |
| 3157 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3158 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3159 | struct cgroup_subsys *ss; |
| 3160 | int ssid; |
| 3161 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3162 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3163 | for_each_subsys(ss, ssid) { |
| 3164 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3165 | |
| 3166 | if (!css) |
| 3167 | continue; |
| 3168 | |
Michal Koutný | 3bc0bb3 | 2020-01-09 16:05:59 +0100 | [diff] [blame] | 3169 | WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); |
| 3170 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3171 | if (css->parent && |
| 3172 | !(cgroup_ss_mask(dsct) & (1 << ss->id))) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3173 | kill_css(css); |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 3174 | } else if (!css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3175 | css_clear_dir(css); |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3176 | if (ss->css_reset) |
| 3177 | ss->css_reset(css); |
| 3178 | } |
| 3179 | } |
| 3180 | } |
| 3181 | } |
| 3182 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3183 | /** |
| 3184 | * cgroup_apply_control - apply control mask updates to the subtree |
| 3185 | * @cgrp: root of the target subtree |
| 3186 | * |
| 3187 | * subsystems can be enabled and disabled in a subtree using the following |
| 3188 | * steps. |
| 3189 | * |
| 3190 | * 1. Call cgroup_save_control() to stash the current state. |
| 3191 | * 2. Update ->subtree_control masks in the subtree as desired. |
| 3192 | * 3. Call cgroup_apply_control() to apply the changes. |
| 3193 | * 4. Optionally perform other related operations. |
| 3194 | * 5. Call cgroup_finalize_control() to finish up. |
| 3195 | * |
| 3196 | * This function implements step 3 and propagates the mask changes |
| 3197 | * throughout @cgrp's subtree, updates csses accordingly and perform |
| 3198 | * process migrations. |
| 3199 | */ |
| 3200 | static int cgroup_apply_control(struct cgroup *cgrp) |
| 3201 | { |
| 3202 | int ret; |
| 3203 | |
| 3204 | cgroup_propagate_control(cgrp); |
| 3205 | |
| 3206 | ret = cgroup_apply_control_enable(cgrp); |
| 3207 | if (ret) |
| 3208 | return ret; |
| 3209 | |
| 3210 | /* |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 3211 | * 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] | 3212 | * making the following cgroup_update_dfl_csses() properly update |
| 3213 | * css associations of all tasks in the subtree. |
| 3214 | */ |
| 3215 | ret = cgroup_update_dfl_csses(cgrp); |
| 3216 | if (ret) |
| 3217 | return ret; |
| 3218 | |
| 3219 | return 0; |
| 3220 | } |
| 3221 | |
| 3222 | /** |
| 3223 | * cgroup_finalize_control - finalize control mask update |
| 3224 | * @cgrp: root of the target subtree |
| 3225 | * @ret: the result of the update |
| 3226 | * |
| 3227 | * Finalize control mask update. See cgroup_apply_control() for more info. |
| 3228 | */ |
| 3229 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret) |
| 3230 | { |
| 3231 | if (ret) { |
| 3232 | cgroup_restore_control(cgrp); |
| 3233 | cgroup_propagate_control(cgrp); |
| 3234 | } |
| 3235 | |
| 3236 | cgroup_apply_control_disable(cgrp); |
| 3237 | } |
| 3238 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3239 | static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable) |
| 3240 | { |
| 3241 | u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask; |
| 3242 | |
| 3243 | /* if nothing is getting enabled, nothing to worry about */ |
| 3244 | if (!enable) |
| 3245 | return 0; |
| 3246 | |
| 3247 | /* can @cgrp host any resources? */ |
| 3248 | if (!cgroup_is_valid_domain(cgrp->dom_cgrp)) |
| 3249 | return -EOPNOTSUPP; |
| 3250 | |
| 3251 | /* mixables don't care */ |
| 3252 | if (cgroup_is_mixable(cgrp)) |
| 3253 | return 0; |
| 3254 | |
| 3255 | if (domain_enable) { |
| 3256 | /* can't enable domain controllers inside a thread subtree */ |
| 3257 | if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3258 | return -EOPNOTSUPP; |
| 3259 | } else { |
| 3260 | /* |
| 3261 | * Threaded controllers can handle internal competitions |
| 3262 | * and are always allowed inside a (prospective) thread |
| 3263 | * subtree. |
| 3264 | */ |
| 3265 | if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3266 | return 0; |
| 3267 | } |
| 3268 | |
| 3269 | /* |
| 3270 | * Controllers can't be enabled for a cgroup with tasks to avoid |
| 3271 | * child cgroups competing against tasks. |
| 3272 | */ |
| 3273 | if (cgroup_has_tasks(cgrp)) |
| 3274 | return -EBUSY; |
| 3275 | |
| 3276 | return 0; |
| 3277 | } |
| 3278 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3279 | /* change the enabled child controllers for a cgroup in the default hierarchy */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3280 | static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, |
| 3281 | char *buf, size_t nbytes, |
| 3282 | loff_t off) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3283 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 3284 | u16 enable = 0, disable = 0; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3285 | struct cgroup *cgrp, *child; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3286 | struct cgroup_subsys *ss; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3287 | char *tok; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3288 | int ssid, ret; |
| 3289 | |
| 3290 | /* |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3291 | * Parse input - space separated list of subsystem names prefixed |
| 3292 | * with either + or -. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3293 | */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3294 | buf = strstrip(buf); |
| 3295 | while ((tok = strsep(&buf, " "))) { |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3296 | if (tok[0] == '\0') |
| 3297 | continue; |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 3298 | do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) { |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 3299 | if (!cgroup_ssid_enabled(ssid) || |
| 3300 | strcmp(tok + 1, ss->name)) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3301 | continue; |
| 3302 | |
| 3303 | if (*tok == '+') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3304 | enable |= 1 << ssid; |
| 3305 | disable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3306 | } else if (*tok == '-') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3307 | disable |= 1 << ssid; |
| 3308 | enable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3309 | } else { |
| 3310 | return -EINVAL; |
| 3311 | } |
| 3312 | break; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 3313 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3314 | if (ssid == CGROUP_SUBSYS_COUNT) |
| 3315 | return -EINVAL; |
| 3316 | } |
| 3317 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3318 | cgrp = cgroup_kn_lock_live(of->kn, true); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3319 | if (!cgrp) |
| 3320 | return -ENODEV; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3321 | |
| 3322 | for_each_subsys(ss, ssid) { |
| 3323 | if (enable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3324 | if (cgrp->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3325 | enable &= ~(1 << ssid); |
| 3326 | continue; |
| 3327 | } |
| 3328 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 3329 | if (!(cgroup_control(cgrp) & (1 << ssid))) { |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3330 | ret = -ENOENT; |
| 3331 | goto out_unlock; |
| 3332 | } |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3333 | } else if (disable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3334 | if (!(cgrp->subtree_control & (1 << ssid))) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3335 | disable &= ~(1 << ssid); |
| 3336 | continue; |
| 3337 | } |
| 3338 | |
| 3339 | /* a child has it enabled? */ |
| 3340 | cgroup_for_each_live_child(child, cgrp) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3341 | if (child->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3342 | ret = -EBUSY; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3343 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3344 | } |
| 3345 | } |
| 3346 | } |
| 3347 | } |
| 3348 | |
| 3349 | if (!enable && !disable) { |
| 3350 | ret = 0; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3351 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3352 | } |
| 3353 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3354 | ret = cgroup_vet_subtree_control_enable(cgrp, enable); |
| 3355 | if (ret) |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 3356 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3357 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3358 | /* save and update control masks and prepare csses */ |
| 3359 | cgroup_save_control(cgrp); |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3360 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3361 | cgrp->subtree_control |= enable; |
| 3362 | cgrp->subtree_control &= ~disable; |
Tejun Heo | f63070d | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 3363 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3364 | ret = cgroup_apply_control(cgrp); |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3365 | cgroup_finalize_control(cgrp, ret); |
Tejun Heo | 3c74541 | 2017-07-23 08:14:15 -0400 | [diff] [blame] | 3366 | if (ret) |
| 3367 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3368 | |
| 3369 | kernfs_activate(cgrp->kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3370 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3371 | cgroup_kn_unlock(of->kn); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3372 | return ret ?: nbytes; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3373 | } |
| 3374 | |
Tejun Heo | c705a00 | 2017-07-25 13:20:18 -0400 | [diff] [blame] | 3375 | /** |
| 3376 | * cgroup_enable_threaded - make @cgrp threaded |
| 3377 | * @cgrp: the target cgroup |
| 3378 | * |
| 3379 | * Called when "threaded" is written to the cgroup.type interface file and |
| 3380 | * tries to make @cgrp threaded and join the parent's resource domain. |
| 3381 | * This function is never called on the root cgroup as cgroup.type doesn't |
| 3382 | * exist on it. |
| 3383 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3384 | static int cgroup_enable_threaded(struct cgroup *cgrp) |
| 3385 | { |
| 3386 | struct cgroup *parent = cgroup_parent(cgrp); |
| 3387 | struct cgroup *dom_cgrp = parent->dom_cgrp; |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3388 | struct cgroup *dsct; |
| 3389 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3390 | int ret; |
| 3391 | |
| 3392 | lockdep_assert_held(&cgroup_mutex); |
| 3393 | |
| 3394 | /* noop if already threaded */ |
| 3395 | if (cgroup_is_threaded(cgrp)) |
| 3396 | return 0; |
| 3397 | |
Tejun Heo | d1897c9 | 2018-02-21 11:39:22 -0800 | [diff] [blame] | 3398 | /* |
| 3399 | * If @cgroup is populated or has domain controllers enabled, it |
| 3400 | * can't be switched. While the below cgroup_can_be_thread_root() |
| 3401 | * test can catch the same conditions, that's only when @parent is |
| 3402 | * not mixable, so let's check it explicitly. |
| 3403 | */ |
| 3404 | if (cgroup_is_populated(cgrp) || |
| 3405 | cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 3406 | return -EOPNOTSUPP; |
| 3407 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3408 | /* we're joining the parent's domain, ensure its validity */ |
| 3409 | if (!cgroup_is_valid_domain(dom_cgrp) || |
| 3410 | !cgroup_can_be_thread_root(dom_cgrp)) |
| 3411 | return -EOPNOTSUPP; |
| 3412 | |
| 3413 | /* |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3414 | * The following shouldn't cause actual migrations and should |
| 3415 | * always succeed. |
| 3416 | */ |
| 3417 | cgroup_save_control(cgrp); |
| 3418 | |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3419 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) |
| 3420 | if (dsct == cgrp || cgroup_is_threaded(dsct)) |
| 3421 | dsct->dom_cgrp = dom_cgrp; |
| 3422 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3423 | ret = cgroup_apply_control(cgrp); |
| 3424 | if (!ret) |
| 3425 | parent->nr_threaded_children++; |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3426 | |
| 3427 | cgroup_finalize_control(cgrp, ret); |
| 3428 | return ret; |
| 3429 | } |
| 3430 | |
| 3431 | static int cgroup_type_show(struct seq_file *seq, void *v) |
| 3432 | { |
| 3433 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3434 | |
| 3435 | if (cgroup_is_threaded(cgrp)) |
| 3436 | seq_puts(seq, "threaded\n"); |
| 3437 | else if (!cgroup_is_valid_domain(cgrp)) |
| 3438 | seq_puts(seq, "domain invalid\n"); |
| 3439 | else if (cgroup_is_thread_root(cgrp)) |
| 3440 | seq_puts(seq, "domain threaded\n"); |
| 3441 | else |
| 3442 | seq_puts(seq, "domain\n"); |
| 3443 | |
| 3444 | return 0; |
| 3445 | } |
| 3446 | |
| 3447 | static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, |
| 3448 | size_t nbytes, loff_t off) |
| 3449 | { |
| 3450 | struct cgroup *cgrp; |
| 3451 | int ret; |
| 3452 | |
| 3453 | /* only switching to threaded mode is supported */ |
| 3454 | if (strcmp(strstrip(buf), "threaded")) |
| 3455 | return -EINVAL; |
| 3456 | |
Michal Koutný | 3bc0bb3 | 2020-01-09 16:05:59 +0100 | [diff] [blame] | 3457 | /* drain dying csses before we re-apply (threaded) subtree control */ |
| 3458 | cgrp = cgroup_kn_lock_live(of->kn, true); |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3459 | if (!cgrp) |
| 3460 | return -ENOENT; |
| 3461 | |
| 3462 | /* threaded can only be enabled */ |
| 3463 | ret = cgroup_enable_threaded(cgrp); |
| 3464 | |
| 3465 | cgroup_kn_unlock(of->kn); |
| 3466 | return ret ?: nbytes; |
| 3467 | } |
| 3468 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3469 | static int cgroup_max_descendants_show(struct seq_file *seq, void *v) |
| 3470 | { |
| 3471 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3472 | int descendants = READ_ONCE(cgrp->max_descendants); |
| 3473 | |
| 3474 | if (descendants == INT_MAX) |
| 3475 | seq_puts(seq, "max\n"); |
| 3476 | else |
| 3477 | seq_printf(seq, "%d\n", descendants); |
| 3478 | |
| 3479 | return 0; |
| 3480 | } |
| 3481 | |
| 3482 | static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of, |
| 3483 | char *buf, size_t nbytes, loff_t off) |
| 3484 | { |
| 3485 | struct cgroup *cgrp; |
| 3486 | int descendants; |
| 3487 | ssize_t ret; |
| 3488 | |
| 3489 | buf = strstrip(buf); |
| 3490 | if (!strcmp(buf, "max")) { |
| 3491 | descendants = INT_MAX; |
| 3492 | } else { |
| 3493 | ret = kstrtoint(buf, 0, &descendants); |
| 3494 | if (ret) |
| 3495 | return ret; |
| 3496 | } |
| 3497 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3498 | if (descendants < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3499 | return -ERANGE; |
| 3500 | |
| 3501 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3502 | if (!cgrp) |
| 3503 | return -ENOENT; |
| 3504 | |
| 3505 | cgrp->max_descendants = descendants; |
| 3506 | |
| 3507 | cgroup_kn_unlock(of->kn); |
| 3508 | |
| 3509 | return nbytes; |
| 3510 | } |
| 3511 | |
| 3512 | static int cgroup_max_depth_show(struct seq_file *seq, void *v) |
| 3513 | { |
| 3514 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3515 | int depth = READ_ONCE(cgrp->max_depth); |
| 3516 | |
| 3517 | if (depth == INT_MAX) |
| 3518 | seq_puts(seq, "max\n"); |
| 3519 | else |
| 3520 | seq_printf(seq, "%d\n", depth); |
| 3521 | |
| 3522 | return 0; |
| 3523 | } |
| 3524 | |
| 3525 | static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of, |
| 3526 | char *buf, size_t nbytes, loff_t off) |
| 3527 | { |
| 3528 | struct cgroup *cgrp; |
| 3529 | ssize_t ret; |
| 3530 | int depth; |
| 3531 | |
| 3532 | buf = strstrip(buf); |
| 3533 | if (!strcmp(buf, "max")) { |
| 3534 | depth = INT_MAX; |
| 3535 | } else { |
| 3536 | ret = kstrtoint(buf, 0, &depth); |
| 3537 | if (ret) |
| 3538 | return ret; |
| 3539 | } |
| 3540 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3541 | if (depth < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3542 | return -ERANGE; |
| 3543 | |
| 3544 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3545 | if (!cgrp) |
| 3546 | return -ENOENT; |
| 3547 | |
| 3548 | cgrp->max_depth = depth; |
| 3549 | |
| 3550 | cgroup_kn_unlock(of->kn); |
| 3551 | |
| 3552 | return nbytes; |
| 3553 | } |
| 3554 | |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 3555 | static int cgroup_events_show(struct seq_file *seq, void *v) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3556 | { |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 3557 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3558 | |
| 3559 | seq_printf(seq, "populated %d\n", cgroup_is_populated(cgrp)); |
| 3560 | seq_printf(seq, "frozen %d\n", test_bit(CGRP_FROZEN, &cgrp->flags)); |
| 3561 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3562 | return 0; |
| 3563 | } |
| 3564 | |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 3565 | static int cgroup_stat_show(struct seq_file *seq, void *v) |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 3566 | { |
| 3567 | struct cgroup *cgroup = seq_css(seq)->cgroup; |
| 3568 | |
| 3569 | seq_printf(seq, "nr_descendants %d\n", |
| 3570 | cgroup->nr_descendants); |
| 3571 | seq_printf(seq, "nr_dying_descendants %d\n", |
| 3572 | cgroup->nr_dying_descendants); |
| 3573 | |
| 3574 | return 0; |
| 3575 | } |
| 3576 | |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3577 | static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq, |
| 3578 | struct cgroup *cgrp, int ssid) |
| 3579 | { |
| 3580 | struct cgroup_subsys *ss = cgroup_subsys[ssid]; |
| 3581 | struct cgroup_subsys_state *css; |
| 3582 | int ret; |
| 3583 | |
| 3584 | if (!ss->css_extra_stat_show) |
| 3585 | return 0; |
| 3586 | |
| 3587 | css = cgroup_tryget_css(cgrp, ss); |
| 3588 | if (!css) |
| 3589 | return 0; |
| 3590 | |
| 3591 | ret = ss->css_extra_stat_show(seq, css); |
| 3592 | css_put(css); |
| 3593 | return ret; |
| 3594 | } |
| 3595 | |
| 3596 | static int cpu_stat_show(struct seq_file *seq, void *v) |
| 3597 | { |
Tejun Heo | c3ba132 | 2017-10-30 08:13:14 -0700 | [diff] [blame] | 3598 | struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3599 | int ret = 0; |
| 3600 | |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3601 | cgroup_base_stat_cputime_show(seq); |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3602 | #ifdef CONFIG_CGROUP_SCHED |
| 3603 | ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id); |
| 3604 | #endif |
| 3605 | return ret; |
| 3606 | } |
| 3607 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3608 | #ifdef CONFIG_PSI |
| 3609 | static int cgroup_io_pressure_show(struct seq_file *seq, void *v) |
| 3610 | { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 3611 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Qian Cai | 190ecb1 | 2020-02-23 22:00:07 -0500 | [diff] [blame] | 3612 | struct psi_group *psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi; |
Dan Schatzberg | df5ba5b | 2019-05-14 15:41:18 -0700 | [diff] [blame] | 3613 | |
| 3614 | return psi_show(seq, psi, PSI_IO); |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3615 | } |
| 3616 | static int cgroup_memory_pressure_show(struct seq_file *seq, void *v) |
| 3617 | { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 3618 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Qian Cai | 190ecb1 | 2020-02-23 22:00:07 -0500 | [diff] [blame] | 3619 | struct psi_group *psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi; |
Dan Schatzberg | df5ba5b | 2019-05-14 15:41:18 -0700 | [diff] [blame] | 3620 | |
| 3621 | return psi_show(seq, psi, PSI_MEM); |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3622 | } |
| 3623 | static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v) |
| 3624 | { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 3625 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Qian Cai | 190ecb1 | 2020-02-23 22:00:07 -0500 | [diff] [blame] | 3626 | struct psi_group *psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi; |
Dan Schatzberg | df5ba5b | 2019-05-14 15:41:18 -0700 | [diff] [blame] | 3627 | |
| 3628 | return psi_show(seq, psi, PSI_CPU); |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3629 | } |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3630 | |
| 3631 | static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf, |
| 3632 | size_t nbytes, enum psi_res res) |
| 3633 | { |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3634 | struct cgroup_file_ctx *ctx = of->priv; |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3635 | struct psi_trigger *new; |
| 3636 | struct cgroup *cgrp; |
Odin Ugedal | 385aac1 | 2021-01-16 18:36:33 +0100 | [diff] [blame] | 3637 | struct psi_group *psi; |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3638 | |
| 3639 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3640 | if (!cgrp) |
| 3641 | return -ENODEV; |
| 3642 | |
| 3643 | cgroup_get(cgrp); |
| 3644 | cgroup_kn_unlock(of->kn); |
| 3645 | |
Suren Baghdasaryan | a06247c | 2022-01-11 15:23:09 -0800 | [diff] [blame] | 3646 | /* Allow only one trigger per file descriptor */ |
| 3647 | if (ctx->psi.trigger) { |
| 3648 | cgroup_put(cgrp); |
| 3649 | return -EBUSY; |
| 3650 | } |
| 3651 | |
Odin Ugedal | 385aac1 | 2021-01-16 18:36:33 +0100 | [diff] [blame] | 3652 | psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi; |
| 3653 | new = psi_trigger_create(psi, buf, nbytes, res); |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3654 | if (IS_ERR(new)) { |
| 3655 | cgroup_put(cgrp); |
| 3656 | return PTR_ERR(new); |
| 3657 | } |
| 3658 | |
Suren Baghdasaryan | a06247c | 2022-01-11 15:23:09 -0800 | [diff] [blame] | 3659 | smp_store_release(&ctx->psi.trigger, new); |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3660 | cgroup_put(cgrp); |
| 3661 | |
| 3662 | return nbytes; |
| 3663 | } |
| 3664 | |
| 3665 | static ssize_t cgroup_io_pressure_write(struct kernfs_open_file *of, |
| 3666 | char *buf, size_t nbytes, |
| 3667 | loff_t off) |
| 3668 | { |
| 3669 | return cgroup_pressure_write(of, buf, nbytes, PSI_IO); |
| 3670 | } |
| 3671 | |
| 3672 | static ssize_t cgroup_memory_pressure_write(struct kernfs_open_file *of, |
| 3673 | char *buf, size_t nbytes, |
| 3674 | loff_t off) |
| 3675 | { |
| 3676 | return cgroup_pressure_write(of, buf, nbytes, PSI_MEM); |
| 3677 | } |
| 3678 | |
| 3679 | static ssize_t cgroup_cpu_pressure_write(struct kernfs_open_file *of, |
| 3680 | char *buf, size_t nbytes, |
| 3681 | loff_t off) |
| 3682 | { |
| 3683 | return cgroup_pressure_write(of, buf, nbytes, PSI_CPU); |
| 3684 | } |
| 3685 | |
| 3686 | static __poll_t cgroup_pressure_poll(struct kernfs_open_file *of, |
| 3687 | poll_table *pt) |
| 3688 | { |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3689 | struct cgroup_file_ctx *ctx = of->priv; |
| 3690 | |
| 3691 | return psi_trigger_poll(&ctx->psi.trigger, of->file, pt); |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | static void cgroup_pressure_release(struct kernfs_open_file *of) |
| 3695 | { |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3696 | struct cgroup_file_ctx *ctx = of->priv; |
| 3697 | |
Suren Baghdasaryan | a06247c | 2022-01-11 15:23:09 -0800 | [diff] [blame] | 3698 | psi_trigger_destroy(ctx->psi.trigger); |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3699 | } |
Suren Baghdasaryan | 3958e2d | 2021-05-24 12:53:39 -0700 | [diff] [blame] | 3700 | |
| 3701 | bool cgroup_psi_enabled(void) |
| 3702 | { |
| 3703 | return (cgroup_feature_disable_mask & (1 << OPT_FEATURE_PRESSURE)) == 0; |
| 3704 | } |
| 3705 | |
| 3706 | #else /* CONFIG_PSI */ |
| 3707 | bool cgroup_psi_enabled(void) |
| 3708 | { |
| 3709 | return false; |
| 3710 | } |
| 3711 | |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 3712 | #endif /* CONFIG_PSI */ |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3713 | |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 3714 | static int cgroup_freeze_show(struct seq_file *seq, void *v) |
| 3715 | { |
| 3716 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3717 | |
| 3718 | seq_printf(seq, "%d\n", cgrp->freezer.freeze); |
| 3719 | |
| 3720 | return 0; |
| 3721 | } |
| 3722 | |
| 3723 | static ssize_t cgroup_freeze_write(struct kernfs_open_file *of, |
| 3724 | char *buf, size_t nbytes, loff_t off) |
| 3725 | { |
| 3726 | struct cgroup *cgrp; |
| 3727 | ssize_t ret; |
| 3728 | int freeze; |
| 3729 | |
| 3730 | ret = kstrtoint(strstrip(buf), 0, &freeze); |
| 3731 | if (ret) |
| 3732 | return ret; |
| 3733 | |
| 3734 | if (freeze < 0 || freeze > 1) |
| 3735 | return -ERANGE; |
| 3736 | |
| 3737 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3738 | if (!cgrp) |
| 3739 | return -ENOENT; |
| 3740 | |
| 3741 | cgroup_freeze(cgrp, freeze); |
| 3742 | |
| 3743 | cgroup_kn_unlock(of->kn); |
| 3744 | |
| 3745 | return nbytes; |
| 3746 | } |
| 3747 | |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 3748 | static void __cgroup_kill(struct cgroup *cgrp) |
| 3749 | { |
| 3750 | struct css_task_iter it; |
| 3751 | struct task_struct *task; |
| 3752 | |
| 3753 | lockdep_assert_held(&cgroup_mutex); |
| 3754 | |
| 3755 | spin_lock_irq(&css_set_lock); |
| 3756 | set_bit(CGRP_KILL, &cgrp->flags); |
| 3757 | spin_unlock_irq(&css_set_lock); |
| 3758 | |
| 3759 | css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED, &it); |
| 3760 | while ((task = css_task_iter_next(&it))) { |
| 3761 | /* Ignore kernel threads here. */ |
| 3762 | if (task->flags & PF_KTHREAD) |
| 3763 | continue; |
| 3764 | |
| 3765 | /* Skip tasks that are already dying. */ |
| 3766 | if (__fatal_signal_pending(task)) |
| 3767 | continue; |
| 3768 | |
| 3769 | send_sig(SIGKILL, task, 0); |
| 3770 | } |
| 3771 | css_task_iter_end(&it); |
| 3772 | |
| 3773 | spin_lock_irq(&css_set_lock); |
| 3774 | clear_bit(CGRP_KILL, &cgrp->flags); |
| 3775 | spin_unlock_irq(&css_set_lock); |
| 3776 | } |
| 3777 | |
| 3778 | static void cgroup_kill(struct cgroup *cgrp) |
| 3779 | { |
| 3780 | struct cgroup_subsys_state *css; |
| 3781 | struct cgroup *dsct; |
| 3782 | |
| 3783 | lockdep_assert_held(&cgroup_mutex); |
| 3784 | |
| 3785 | cgroup_for_each_live_descendant_pre(dsct, css, cgrp) |
| 3786 | __cgroup_kill(dsct); |
| 3787 | } |
| 3788 | |
| 3789 | static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf, |
| 3790 | size_t nbytes, loff_t off) |
| 3791 | { |
| 3792 | ssize_t ret = 0; |
| 3793 | int kill; |
| 3794 | struct cgroup *cgrp; |
| 3795 | |
| 3796 | ret = kstrtoint(strstrip(buf), 0, &kill); |
| 3797 | if (ret) |
| 3798 | return ret; |
| 3799 | |
| 3800 | if (kill != 1) |
| 3801 | return -ERANGE; |
| 3802 | |
| 3803 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3804 | if (!cgrp) |
| 3805 | return -ENOENT; |
| 3806 | |
| 3807 | /* |
| 3808 | * Killing is a process directed operation, i.e. the whole thread-group |
| 3809 | * is taken down so act like we do for cgroup.procs and only make this |
| 3810 | * writable in non-threaded cgroups. |
| 3811 | */ |
| 3812 | if (cgroup_is_threaded(cgrp)) |
| 3813 | ret = -EOPNOTSUPP; |
| 3814 | else |
| 3815 | cgroup_kill(cgrp); |
| 3816 | |
| 3817 | cgroup_kn_unlock(of->kn); |
| 3818 | |
| 3819 | return ret ?: nbytes; |
| 3820 | } |
| 3821 | |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3822 | static int cgroup_file_open(struct kernfs_open_file *of) |
| 3823 | { |
Hui Su | 5a7b5f3 | 2020-11-06 22:47:40 +0800 | [diff] [blame] | 3824 | struct cftype *cft = of_cft(of); |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3825 | struct cgroup_file_ctx *ctx; |
| 3826 | int ret; |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3827 | |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3828 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 3829 | if (!ctx) |
| 3830 | return -ENOMEM; |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3831 | |
| 3832 | ctx->ns = current->nsproxy->cgroup_ns; |
| 3833 | get_cgroup_ns(ctx->ns); |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3834 | of->priv = ctx; |
| 3835 | |
| 3836 | if (!cft->open) |
| 3837 | return 0; |
| 3838 | |
| 3839 | ret = cft->open(of); |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3840 | if (ret) { |
| 3841 | put_cgroup_ns(ctx->ns); |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3842 | kfree(ctx); |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3843 | } |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3844 | return ret; |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | static void cgroup_file_release(struct kernfs_open_file *of) |
| 3848 | { |
Hui Su | 5a7b5f3 | 2020-11-06 22:47:40 +0800 | [diff] [blame] | 3849 | struct cftype *cft = of_cft(of); |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3850 | struct cgroup_file_ctx *ctx = of->priv; |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3851 | |
| 3852 | if (cft->release) |
| 3853 | cft->release(of); |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3854 | put_cgroup_ns(ctx->ns); |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3855 | kfree(ctx); |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3856 | } |
| 3857 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3858 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 3859 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3860 | { |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3861 | struct cgroup_file_ctx *ctx = of->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3862 | struct cgroup *cgrp = of->kn->parent->priv; |
Hui Su | 5a7b5f3 | 2020-11-06 22:47:40 +0800 | [diff] [blame] | 3863 | struct cftype *cft = of_cft(of); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3864 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3865 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3866 | |
Jouni Roivas | 65026da | 2020-09-30 19:42:42 +0300 | [diff] [blame] | 3867 | if (!nbytes) |
| 3868 | return 0; |
| 3869 | |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3870 | /* |
| 3871 | * If namespaces are delegation boundaries, disallow writes to |
| 3872 | * files in an non-init namespace root from inside the namespace |
| 3873 | * except for the files explicitly marked delegatable - |
| 3874 | * cgroup.procs and cgroup.subtree_control. |
| 3875 | */ |
| 3876 | if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) && |
| 3877 | !(cft->flags & CFTYPE_NS_DELEGATABLE) && |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 3878 | ctx->ns != &init_cgroup_ns && ctx->ns->root_cset->dfl_cgrp == cgrp) |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3879 | return -EPERM; |
| 3880 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3881 | if (cft->write) |
| 3882 | return cft->write(of, buf, nbytes, off); |
| 3883 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3884 | /* |
| 3885 | * kernfs guarantees that a file isn't deleted with operations in |
| 3886 | * flight, which means that the matching css is and stays alive and |
| 3887 | * doesn't need to be pinned. The RCU locking is not necessary |
| 3888 | * either. It's just for the convenience of using cgroup_css(). |
| 3889 | */ |
| 3890 | rcu_read_lock(); |
| 3891 | css = cgroup_css(cgrp, cft->ss); |
| 3892 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3893 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3894 | if (cft->write_u64) { |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3895 | unsigned long long v; |
| 3896 | ret = kstrtoull(buf, 0, &v); |
| 3897 | if (!ret) |
| 3898 | ret = cft->write_u64(css, cft, v); |
| 3899 | } else if (cft->write_s64) { |
| 3900 | long long v; |
| 3901 | ret = kstrtoll(buf, 0, &v); |
| 3902 | if (!ret) |
| 3903 | ret = cft->write_s64(css, cft, v); |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3904 | } else { |
| 3905 | ret = -EINVAL; |
| 3906 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3907 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3908 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3909 | } |
| 3910 | |
Johannes Weiner | dc50537 | 2019-03-05 15:45:48 -0800 | [diff] [blame] | 3911 | static __poll_t cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt) |
| 3912 | { |
Hui Su | 5a7b5f3 | 2020-11-06 22:47:40 +0800 | [diff] [blame] | 3913 | struct cftype *cft = of_cft(of); |
Johannes Weiner | dc50537 | 2019-03-05 15:45:48 -0800 | [diff] [blame] | 3914 | |
| 3915 | if (cft->poll) |
| 3916 | return cft->poll(of, pt); |
| 3917 | |
| 3918 | return kernfs_generic_poll(of, pt); |
| 3919 | } |
| 3920 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3921 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3922 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3923 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 3927 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3928 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 3932 | { |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3933 | if (seq_cft(seq)->seq_stop) |
| 3934 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 3938 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3939 | struct cftype *cft = seq_cft(m); |
| 3940 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 3941 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3942 | if (cft->seq_show) |
| 3943 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3944 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3945 | if (cft->read_u64) |
| 3946 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 3947 | else if (cft->read_s64) |
| 3948 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 3949 | else |
| 3950 | return -EINVAL; |
| 3951 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3952 | } |
| 3953 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3954 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 3955 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3956 | .open = cgroup_file_open, |
| 3957 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3958 | .write = cgroup_file_write, |
Johannes Weiner | dc50537 | 2019-03-05 15:45:48 -0800 | [diff] [blame] | 3959 | .poll = cgroup_file_poll, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3960 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3961 | }; |
| 3962 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3963 | static struct kernfs_ops cgroup_kf_ops = { |
| 3964 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3965 | .open = cgroup_file_open, |
| 3966 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3967 | .write = cgroup_file_write, |
Johannes Weiner | dc50537 | 2019-03-05 15:45:48 -0800 | [diff] [blame] | 3968 | .poll = cgroup_file_poll, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3969 | .seq_start = cgroup_seqfile_start, |
| 3970 | .seq_next = cgroup_seqfile_next, |
| 3971 | .seq_stop = cgroup_seqfile_stop, |
| 3972 | .seq_show = cgroup_seqfile_show, |
| 3973 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3974 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3975 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 3976 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 3977 | { |
| 3978 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 3979 | .ia_uid = current_fsuid(), |
| 3980 | .ia_gid = current_fsgid(), }; |
| 3981 | |
| 3982 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 3983 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 3984 | return 0; |
| 3985 | |
| 3986 | return kernfs_setattr(kn, &iattr); |
| 3987 | } |
| 3988 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3989 | static void cgroup_file_notify_timer(struct timer_list *timer) |
| 3990 | { |
| 3991 | cgroup_file_notify(container_of(timer, struct cgroup_file, |
| 3992 | notify_timer)); |
| 3993 | } |
| 3994 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3995 | static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, |
| 3996 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3997 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3998 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3999 | struct kernfs_node *kn; |
| 4000 | struct lock_class_key *key = NULL; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 4001 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4002 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4003 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 4004 | key = &cft->lockdep_key; |
| 4005 | #endif |
| 4006 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame] | 4007 | cgroup_file_mode(cft), |
| 4008 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, |
| 4009 | 0, cft->kf_ops, cft, |
Tejun Heo | dfeb0750 | 2015-02-13 14:36:31 -0800 | [diff] [blame] | 4010 | NULL, key); |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 4011 | if (IS_ERR(kn)) |
| 4012 | return PTR_ERR(kn); |
| 4013 | |
| 4014 | ret = cgroup_kn_set_ugid(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4015 | if (ret) { |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 4016 | kernfs_remove(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4017 | return ret; |
| 4018 | } |
| 4019 | |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4020 | if (cft->file_offset) { |
| 4021 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 4022 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4023 | timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0); |
| 4024 | |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 4025 | spin_lock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4026 | cfile->kn = kn; |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 4027 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4028 | } |
| 4029 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4030 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4031 | } |
| 4032 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4033 | /** |
| 4034 | * cgroup_addrm_files - add or remove files to a cgroup directory |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4035 | * @css: the target css |
| 4036 | * @cgrp: the target cgroup (usually css->cgroup) |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4037 | * @cfts: array of cftypes to be added |
| 4038 | * @is_add: whether to add or remove |
| 4039 | * |
| 4040 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4041 | * For removals, this function never fails. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4042 | */ |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4043 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 4044 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4045 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4046 | { |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4047 | struct cftype *cft, *cft_end = NULL; |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 4048 | int ret = 0; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4049 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4050 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4051 | |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4052 | restart: |
| 4053 | for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 4054 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Suren Baghdasaryan | 3958e2d | 2021-05-24 12:53:39 -0700 | [diff] [blame] | 4055 | if ((cft->flags & CFTYPE_PRESSURE) && !cgroup_psi_enabled()) |
| 4056 | continue; |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4057 | if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
Tejun Heo | 8cbbf2c | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4058 | continue; |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4059 | if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp)) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4060 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4061 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 4062 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4063 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 4064 | continue; |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 4065 | if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug) |
| 4066 | continue; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 4067 | if (is_add) { |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4068 | ret = cgroup_add_file(css, cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4069 | if (ret) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4070 | pr_warn("%s: failed to add %s, err=%d\n", |
| 4071 | __func__, cft->name, ret); |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4072 | cft_end = cft; |
| 4073 | is_add = false; |
| 4074 | goto restart; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4075 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 4076 | } else { |
| 4077 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4078 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4079 | } |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 4080 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4081 | } |
| 4082 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4083 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4084 | { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4085 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4086 | struct cgroup *root = &ss->root->cgrp; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4087 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4088 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4089 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4090 | lockdep_assert_held(&cgroup_mutex); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 4091 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 4092 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4093 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4094 | struct cgroup *cgrp = css->cgroup; |
| 4095 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4096 | if (!(css->flags & CSS_VISIBLE)) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 4097 | continue; |
| 4098 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4099 | ret = cgroup_addrm_files(css, cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4100 | if (ret) |
| 4101 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4102 | } |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4103 | |
| 4104 | if (is_add && !ret) |
| 4105 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4106 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4107 | } |
| 4108 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4109 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 4110 | { |
| 4111 | struct cftype *cft; |
| 4112 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4113 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 4114 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 4115 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 4116 | kfree(cft->kf_ops); |
| 4117 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4118 | cft->ss = NULL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4119 | |
| 4120 | /* revert flags set by cgroup core while adding @cfts */ |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4121 | cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4122 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4123 | } |
| 4124 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4125 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4126 | { |
| 4127 | struct cftype *cft; |
| 4128 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4129 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 4130 | struct kernfs_ops *kf_ops; |
| 4131 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4132 | WARN_ON(cft->ss || cft->kf_ops); |
| 4133 | |
Suren Baghdasaryan | 3958e2d | 2021-05-24 12:53:39 -0700 | [diff] [blame] | 4134 | if ((cft->flags & CFTYPE_PRESSURE) && !cgroup_psi_enabled()) |
| 4135 | continue; |
| 4136 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4137 | if (cft->seq_start) |
| 4138 | kf_ops = &cgroup_kf_ops; |
| 4139 | else |
| 4140 | kf_ops = &cgroup_kf_single_ops; |
| 4141 | |
| 4142 | /* |
| 4143 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 4144 | * make a copy of kf_ops to set its atomic_write_len. |
| 4145 | */ |
| 4146 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 4147 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 4148 | if (!kf_ops) { |
| 4149 | cgroup_exit_cftypes(cfts); |
| 4150 | return -ENOMEM; |
| 4151 | } |
| 4152 | kf_ops->atomic_write_len = cft->max_write_len; |
| 4153 | } |
| 4154 | |
| 4155 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4156 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4160 | } |
| 4161 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4162 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 4163 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4164 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4165 | |
| 4166 | if (!cfts || !cfts[0].ss) |
| 4167 | return -ENOENT; |
| 4168 | |
| 4169 | list_del(&cfts->node); |
| 4170 | cgroup_apply_cftypes(cfts, false); |
| 4171 | cgroup_exit_cftypes(cfts); |
| 4172 | return 0; |
| 4173 | } |
| 4174 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4175 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4176 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 4177 | * @cfts: zero-length name terminated array of cftypes |
| 4178 | * |
| 4179 | * Unregister @cfts. Files described by @cfts are removed from all |
| 4180 | * existing cgroups and all future cgroups won't have them either. This |
| 4181 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 4182 | * |
| 4183 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 4184 | * registered. |
| 4185 | */ |
| 4186 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 4187 | { |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4188 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4189 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4190 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4191 | ret = cgroup_rm_cftypes_locked(cfts); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4192 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4193 | return ret; |
| 4194 | } |
| 4195 | |
| 4196 | /** |
| 4197 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 4198 | * @ss: target cgroup subsystem |
| 4199 | * @cfts: zero-length name terminated array of cftypes |
| 4200 | * |
| 4201 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 4202 | * existing cgroups to which @ss is attached and all future cgroups will |
| 4203 | * have them too. This function can be called anytime whether @ss is |
| 4204 | * attached or not. |
| 4205 | * |
| 4206 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 4207 | * function currently returns 0 as long as @cfts registration is successful |
| 4208 | * even if some file creation attempts on existing cgroups fail. |
| 4209 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4210 | static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4211 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4212 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4213 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 4214 | if (!cgroup_ssid_enabled(ss->id)) |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 4215 | return 0; |
| 4216 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 4217 | if (!cfts || cfts[0].name[0] == '\0') |
| 4218 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4219 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4220 | ret = cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4221 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4222 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4223 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4224 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4225 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4226 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4227 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4228 | if (ret) |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 4229 | cgroup_rm_cftypes_locked(cfts); |
| 4230 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4231 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4232 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4233 | } |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4234 | |
| 4235 | /** |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4236 | * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy |
| 4237 | * @ss: target cgroup subsystem |
| 4238 | * @cfts: zero-length name terminated array of cftypes |
| 4239 | * |
| 4240 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 4241 | * the default hierarchy. |
| 4242 | */ |
| 4243 | int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 4244 | { |
| 4245 | struct cftype *cft; |
| 4246 | |
| 4247 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4248 | cft->flags |= __CFTYPE_ONLY_ON_DFL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4249 | return cgroup_add_cftypes(ss, cfts); |
| 4250 | } |
| 4251 | |
| 4252 | /** |
| 4253 | * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies |
| 4254 | * @ss: target cgroup subsystem |
| 4255 | * @cfts: zero-length name terminated array of cftypes |
| 4256 | * |
| 4257 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 4258 | * the legacy hierarchies. |
| 4259 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4260 | int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 4261 | { |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 4262 | struct cftype *cft; |
| 4263 | |
Tejun Heo | e4b7037 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 4264 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
| 4265 | cft->flags |= __CFTYPE_NOT_ON_DFL; |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4266 | return cgroup_add_cftypes(ss, cfts); |
| 4267 | } |
| 4268 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4269 | /** |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 4270 | * cgroup_file_notify - generate a file modified event for a cgroup_file |
| 4271 | * @cfile: target cgroup_file |
| 4272 | * |
| 4273 | * @cfile must have been obtained by setting cftype->file_offset. |
| 4274 | */ |
| 4275 | void cgroup_file_notify(struct cgroup_file *cfile) |
| 4276 | { |
| 4277 | unsigned long flags; |
| 4278 | |
| 4279 | spin_lock_irqsave(&cgroup_file_kn_lock, flags); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4280 | if (cfile->kn) { |
| 4281 | unsigned long last = cfile->notified_at; |
| 4282 | unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV; |
| 4283 | |
| 4284 | if (time_in_range(jiffies, last, next)) { |
| 4285 | timer_reduce(&cfile->notify_timer, next); |
| 4286 | } else { |
| 4287 | kernfs_notify(cfile->kn); |
| 4288 | cfile->notified_at = jiffies; |
| 4289 | } |
| 4290 | } |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 4291 | spin_unlock_irqrestore(&cgroup_file_kn_lock, flags); |
| 4292 | } |
| 4293 | |
| 4294 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4295 | * css_next_child - find the next child of a given css |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4296 | * @pos: the current position (%NULL to initiate traversal) |
| 4297 | * @parent: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4298 | * |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4299 | * This function returns the next child of @parent and should be called |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4300 | * under either cgroup_mutex or RCU read lock. The only requirement is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4301 | * that @parent and @pos are accessible. The next sibling is guaranteed to |
| 4302 | * be returned regardless of their states. |
| 4303 | * |
| 4304 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4305 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4306 | * future iterations and will stay visible until the last reference is put. |
| 4307 | * A css which hasn't finished ->css_online() or already finished |
| 4308 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4309 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4310 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4311 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, |
| 4312 | struct cgroup_subsys_state *parent) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4313 | { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4314 | struct cgroup_subsys_state *next; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4315 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4316 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4317 | |
| 4318 | /* |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4319 | * @pos could already have been unlinked from the sibling list. |
| 4320 | * Once a cgroup is removed, its ->sibling.next is no longer |
| 4321 | * updated when its next sibling changes. CSS_RELEASED is set when |
| 4322 | * @pos is taken off list, at which time its next pointer is valid, |
| 4323 | * and, as releases are serialized, the one pointed to by the next |
| 4324 | * pointer is guaranteed to not have started release yet. This |
| 4325 | * implies that if we observe !CSS_RELEASED on @pos in this RCU |
| 4326 | * critical section, the one pointed to by its next pointer is |
| 4327 | * guaranteed to not have finished its RCU grace period even if we |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 4328 | * have dropped rcu_read_lock() in-between iterations. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4329 | * |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4330 | * If @pos has CSS_RELEASED set, its next pointer can't be |
| 4331 | * dereferenced; however, as each css is given a monotonically |
| 4332 | * increasing unique serial number and always appended to the |
| 4333 | * sibling list, the next one can be found by walking the parent's |
| 4334 | * children until the first css with higher serial number than |
| 4335 | * @pos's. While this path can be slower, it happens iff iteration |
| 4336 | * races against release and the race window is very small. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4337 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4338 | if (!pos) { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4339 | next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); |
| 4340 | } else if (likely(!(pos->flags & CSS_RELEASED))) { |
| 4341 | next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4342 | } else { |
Madhuparna Bhowmik | 3010c5b | 2020-01-18 08:40:51 +0530 | [diff] [blame] | 4343 | list_for_each_entry_rcu(next, &parent->children, sibling, |
| 4344 | lockdep_is_held(&cgroup_mutex)) |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4345 | if (next->serial_nr > pos->serial_nr) |
| 4346 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4347 | } |
| 4348 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4349 | /* |
| 4350 | * @next, if not pointing to the head, can be dereferenced and is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4351 | * the next sibling. |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4352 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4353 | if (&next->sibling != &parent->children) |
| 4354 | return next; |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4355 | return NULL; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4356 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4357 | |
| 4358 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4359 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4360 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4361 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4362 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4363 | * 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] | 4364 | * to visit for pre-order traversal of @root's descendants. @root is |
| 4365 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4366 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4367 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4368 | * doesn't require the whole traversal to be contained in a single critical |
| 4369 | * section. This function will return the correct next descendant as long |
| 4370 | * as both @pos and @root are accessible and @pos is a descendant of @root. |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4371 | * |
| 4372 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4373 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4374 | * future iterations and will stay visible until the last reference is put. |
| 4375 | * A css which hasn't finished ->css_online() or already finished |
| 4376 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4377 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4378 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4379 | struct cgroup_subsys_state * |
| 4380 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 4381 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4382 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4383 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4384 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4385 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4386 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4387 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 4388 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4389 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4390 | |
| 4391 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4392 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4393 | if (next) |
| 4394 | return next; |
| 4395 | |
| 4396 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4397 | while (pos != root) { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4398 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4399 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4400 | return next; |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4401 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 4402 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4403 | |
| 4404 | return NULL; |
| 4405 | } |
Christoph Hellwig | 474a280 | 2019-06-21 10:22:48 +0200 | [diff] [blame] | 4406 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4407 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4408 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4409 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 4410 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4411 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4412 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 4413 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4414 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4415 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4416 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4417 | * doesn't require the whole traversal to be contained in a single critical |
| 4418 | * section. This function will return the correct rightmost descendant as |
| 4419 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4420 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4421 | struct cgroup_subsys_state * |
| 4422 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4423 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4424 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4425 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4426 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4427 | |
| 4428 | do { |
| 4429 | last = pos; |
| 4430 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 4431 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4432 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4433 | pos = tmp; |
| 4434 | } while (pos); |
| 4435 | |
| 4436 | return last; |
| 4437 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4438 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4439 | static struct cgroup_subsys_state * |
| 4440 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4441 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4442 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4443 | |
| 4444 | do { |
| 4445 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4446 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4447 | } while (pos); |
| 4448 | |
| 4449 | return last; |
| 4450 | } |
| 4451 | |
| 4452 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4453 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4454 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4455 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4456 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4457 | * 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] | 4458 | * to visit for post-order traversal of @root's descendants. @root is |
| 4459 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4460 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4461 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4462 | * doesn't require the whole traversal to be contained in a single critical |
| 4463 | * section. This function will return the correct next descendant as long |
| 4464 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 4465 | * @cgroup. |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4466 | * |
| 4467 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4468 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4469 | * future iterations and will stay visible until the last reference is put. |
| 4470 | * A css which hasn't finished ->css_online() or already finished |
| 4471 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4472 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4473 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4474 | struct cgroup_subsys_state * |
| 4475 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 4476 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4477 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4478 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4479 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4480 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4481 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 4482 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 4483 | if (!pos) |
| 4484 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4485 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4486 | /* if we visited @root, we're done */ |
| 4487 | if (pos == root) |
| 4488 | return NULL; |
| 4489 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4490 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4491 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4492 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4493 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4494 | |
| 4495 | /* no sibling left, visit parent */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4496 | return pos->parent; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4497 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4498 | |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4499 | /** |
| 4500 | * css_has_online_children - does a css have online children |
| 4501 | * @css: the target css |
| 4502 | * |
| 4503 | * Returns %true if @css has any online children; otherwise, %false. This |
| 4504 | * function can be called from any context but the caller is responsible |
| 4505 | * for synchronizing against on/offlining as necessary. |
| 4506 | */ |
| 4507 | bool css_has_online_children(struct cgroup_subsys_state *css) |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4508 | { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4509 | struct cgroup_subsys_state *child; |
| 4510 | bool ret = false; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4511 | |
| 4512 | rcu_read_lock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4513 | css_for_each_child(child, css) { |
Li Zefan | 99bae5f | 2014-06-12 14:31:31 +0800 | [diff] [blame] | 4514 | if (child->flags & CSS_ONLINE) { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4515 | ret = true; |
| 4516 | break; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | rcu_read_unlock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4520 | return ret; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 4521 | } |
| 4522 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4523 | static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it) |
| 4524 | { |
| 4525 | struct list_head *l; |
| 4526 | struct cgrp_cset_link *link; |
| 4527 | struct css_set *cset; |
| 4528 | |
| 4529 | lockdep_assert_held(&css_set_lock); |
| 4530 | |
| 4531 | /* find the next threaded cset */ |
| 4532 | if (it->tcset_pos) { |
| 4533 | l = it->tcset_pos->next; |
| 4534 | |
| 4535 | if (l != it->tcset_head) { |
| 4536 | it->tcset_pos = l; |
| 4537 | return container_of(l, struct css_set, |
| 4538 | threaded_csets_node); |
| 4539 | } |
| 4540 | |
| 4541 | it->tcset_pos = NULL; |
| 4542 | } |
| 4543 | |
| 4544 | /* find the next cset */ |
| 4545 | l = it->cset_pos; |
| 4546 | l = l->next; |
| 4547 | if (l == it->cset_head) { |
| 4548 | it->cset_pos = NULL; |
| 4549 | return NULL; |
| 4550 | } |
| 4551 | |
| 4552 | if (it->ss) { |
| 4553 | cset = container_of(l, struct css_set, e_cset_node[it->ss->id]); |
| 4554 | } else { |
| 4555 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 4556 | cset = link->cset; |
| 4557 | } |
| 4558 | |
| 4559 | it->cset_pos = l; |
| 4560 | |
| 4561 | /* initialize threaded css_set walking */ |
| 4562 | if (it->flags & CSS_TASK_ITER_THREADED) { |
| 4563 | if (it->cur_dcset) |
| 4564 | put_css_set_locked(it->cur_dcset); |
| 4565 | it->cur_dcset = cset; |
| 4566 | get_css_set(cset); |
| 4567 | |
| 4568 | it->tcset_head = &cset->threaded_csets; |
| 4569 | it->tcset_pos = &cset->threaded_csets; |
| 4570 | } |
| 4571 | |
| 4572 | return cset; |
| 4573 | } |
| 4574 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4575 | /** |
Bhaskar Chowdhury | 58315c9 | 2020-11-09 16:01:11 +0530 | [diff] [blame] | 4576 | * 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] | 4577 | * @it: the iterator to advance |
| 4578 | * |
| 4579 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4580 | */ |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4581 | 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] | 4582 | { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4583 | struct css_set *cset; |
| 4584 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4585 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4586 | |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4587 | /* Advance to the next non-empty css_set and find first non-empty tasks list*/ |
| 4588 | while ((cset = css_task_iter_next_css_set(it))) { |
| 4589 | if (!list_empty(&cset->tasks)) { |
| 4590 | it->cur_tasks_head = &cset->tasks; |
| 4591 | break; |
| 4592 | } else if (!list_empty(&cset->mg_tasks)) { |
| 4593 | it->cur_tasks_head = &cset->mg_tasks; |
| 4594 | break; |
| 4595 | } else if (!list_empty(&cset->dying_tasks)) { |
| 4596 | it->cur_tasks_head = &cset->dying_tasks; |
| 4597 | break; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4598 | } |
Michal Koutný | 9c974c7 | 2020-01-24 12:40:15 +0100 | [diff] [blame] | 4599 | } |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4600 | if (!cset) { |
| 4601 | it->task_pos = NULL; |
| 4602 | return; |
| 4603 | } |
| 4604 | it->task_pos = it->cur_tasks_head->next; |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4605 | |
| 4606 | /* |
| 4607 | * We don't keep css_sets locked across iteration steps and thus |
| 4608 | * need to take steps to ensure that iteration can be resumed after |
| 4609 | * the lock is re-acquired. Iteration is performed at two levels - |
| 4610 | * css_sets and tasks in them. |
| 4611 | * |
| 4612 | * Once created, a css_set never leaves its cgroup lists, so a |
| 4613 | * pinned css_set is guaranteed to stay put and we can resume |
| 4614 | * iteration afterwards. |
| 4615 | * |
| 4616 | * Tasks may leave @cset across iteration steps. This is resolved |
| 4617 | * by registering each iterator with the css_set currently being |
| 4618 | * walked and making css_set_move_task() advance iterators whose |
| 4619 | * next task is leaving. |
| 4620 | */ |
| 4621 | if (it->cur_cset) { |
| 4622 | list_del(&it->iters_node); |
| 4623 | put_css_set_locked(it->cur_cset); |
| 4624 | } |
| 4625 | get_css_set(cset); |
| 4626 | it->cur_cset = cset; |
| 4627 | list_add(&it->iters_node, &cset->task_iters); |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4628 | } |
| 4629 | |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4630 | static void css_task_iter_skip(struct css_task_iter *it, |
| 4631 | struct task_struct *task) |
| 4632 | { |
| 4633 | lockdep_assert_held(&css_set_lock); |
| 4634 | |
| 4635 | if (it->task_pos == &task->cg_list) { |
| 4636 | it->task_pos = it->task_pos->next; |
| 4637 | it->flags |= CSS_TASK_ITER_SKIPPED; |
| 4638 | } |
| 4639 | } |
| 4640 | |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4641 | static void css_task_iter_advance(struct css_task_iter *it) |
| 4642 | { |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4643 | struct task_struct *task; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4644 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4645 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4646 | repeat: |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4647 | if (it->task_pos) { |
| 4648 | /* |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4649 | * Advance iterator to find next entry. We go through cset |
| 4650 | * tasks, mg_tasks and dying_tasks, when consumed we move onto |
| 4651 | * the next cset. |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4652 | */ |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4653 | if (it->flags & CSS_TASK_ITER_SKIPPED) |
| 4654 | it->flags &= ~CSS_TASK_ITER_SKIPPED; |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4655 | else |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4656 | it->task_pos = it->task_pos->next; |
| 4657 | |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4658 | if (it->task_pos == &it->cur_cset->tasks) { |
| 4659 | it->cur_tasks_head = &it->cur_cset->mg_tasks; |
| 4660 | it->task_pos = it->cur_tasks_head->next; |
Michal Koutný | 9c974c7 | 2020-01-24 12:40:15 +0100 | [diff] [blame] | 4661 | } |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4662 | if (it->task_pos == &it->cur_cset->mg_tasks) { |
| 4663 | it->cur_tasks_head = &it->cur_cset->dying_tasks; |
| 4664 | it->task_pos = it->cur_tasks_head->next; |
Michal Koutný | 9c974c7 | 2020-01-24 12:40:15 +0100 | [diff] [blame] | 4665 | } |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4666 | if (it->task_pos == &it->cur_cset->dying_tasks) |
Tejun Heo | b636fd3 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4667 | css_task_iter_advance_css_set(it); |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4668 | } else { |
| 4669 | /* called from start, proceed to the first cset */ |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4670 | css_task_iter_advance_css_set(it); |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4671 | } |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4672 | |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4673 | if (!it->task_pos) |
| 4674 | return; |
| 4675 | |
| 4676 | task = list_entry(it->task_pos, struct task_struct, cg_list); |
| 4677 | |
| 4678 | if (it->flags & CSS_TASK_ITER_PROCS) { |
| 4679 | /* if PROCS, skip over tasks which aren't group leaders */ |
| 4680 | if (!thread_group_leader(task)) |
| 4681 | goto repeat; |
| 4682 | |
| 4683 | /* and dying leaders w/o live member threads */ |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4684 | if (it->cur_tasks_head == &it->cur_cset->dying_tasks && |
Michal Koutný | 9c974c7 | 2020-01-24 12:40:15 +0100 | [diff] [blame] | 4685 | !atomic_read(&task->signal->live)) |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4686 | goto repeat; |
| 4687 | } else { |
| 4688 | /* skip all dying ones */ |
Michal Koutný | f43caa2 | 2020-01-24 12:40:16 +0100 | [diff] [blame] | 4689 | if (it->cur_tasks_head == &it->cur_cset->dying_tasks) |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 4690 | goto repeat; |
| 4691 | } |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4692 | } |
| 4693 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4694 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4695 | * css_task_iter_start - initiate task iteration |
| 4696 | * @css: the css to walk tasks of |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4697 | * @flags: CSS_TASK_ITER_* flags |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4698 | * @it: the task iterator to use |
| 4699 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4700 | * Initiate iteration through the tasks of @css. The caller can call |
| 4701 | * css_task_iter_next() to walk through the tasks until the function |
| 4702 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 4703 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4704 | */ |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4705 | 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] | 4706 | struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4707 | { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4708 | memset(it, 0, sizeof(*it)); |
| 4709 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4710 | spin_lock_irq(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4711 | |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4712 | it->ss = css->ss; |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4713 | it->flags = flags; |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4714 | |
Kees Cook | d20d30e | 2021-08-27 17:02:55 -0700 | [diff] [blame] | 4715 | if (CGROUP_HAS_SUBSYS_CONFIG && it->ss) |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4716 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; |
| 4717 | else |
| 4718 | it->cset_pos = &css->cgroup->cset_links; |
| 4719 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4720 | it->cset_head = it->cset_pos; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4721 | |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4722 | css_task_iter_advance(it); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4723 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4724 | spin_unlock_irq(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4725 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4726 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4727 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4728 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4729 | * @it: the task iterator being iterated |
| 4730 | * |
| 4731 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4732 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 4733 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4734 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4735 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4736 | { |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4737 | if (it->cur_task) { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4738 | put_task_struct(it->cur_task); |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4739 | it->cur_task = NULL; |
| 4740 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4741 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4742 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4743 | |
Tejun Heo | cee0c33 | 2019-06-05 09:54:34 -0700 | [diff] [blame] | 4744 | /* @it may be half-advanced by skips, finish advancing */ |
| 4745 | if (it->flags & CSS_TASK_ITER_SKIPPED) |
| 4746 | css_task_iter_advance(it); |
| 4747 | |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4748 | if (it->task_pos) { |
| 4749 | it->cur_task = list_entry(it->task_pos, struct task_struct, |
| 4750 | cg_list); |
| 4751 | get_task_struct(it->cur_task); |
| 4752 | css_task_iter_advance(it); |
| 4753 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4754 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4755 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4756 | |
| 4757 | return it->cur_task; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4758 | } |
| 4759 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4760 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4761 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4762 | * @it: the task iterator to finish |
| 4763 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4764 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4765 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4766 | void css_task_iter_end(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4767 | { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4768 | if (it->cur_cset) { |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4769 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4770 | list_del(&it->iters_node); |
| 4771 | put_css_set_locked(it->cur_cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4772 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4773 | } |
| 4774 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4775 | if (it->cur_dcset) |
| 4776 | put_css_set(it->cur_dcset); |
| 4777 | |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4778 | if (it->cur_task) |
| 4779 | put_task_struct(it->cur_task); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4780 | } |
| 4781 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4782 | static void cgroup_procs_release(struct kernfs_open_file *of) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4783 | { |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4784 | struct cgroup_file_ctx *ctx = of->priv; |
| 4785 | |
| 4786 | if (ctx->procs.started) |
| 4787 | css_task_iter_end(&ctx->procs.iter); |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos) |
| 4791 | { |
| 4792 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4793 | struct cgroup_file_ctx *ctx = of->priv; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 4794 | |
Vasily Averin | 2d4ecb0 | 2020-01-30 13:34:59 +0300 | [diff] [blame] | 4795 | if (pos) |
| 4796 | (*pos)++; |
| 4797 | |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4798 | return css_task_iter_next(&ctx->procs.iter); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4799 | } |
| 4800 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4801 | static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos, |
| 4802 | unsigned int iter_flags) |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 4803 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4804 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4805 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4806 | struct cgroup_file_ctx *ctx = of->priv; |
| 4807 | struct css_task_iter *it = &ctx->procs.iter; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4808 | |
| 4809 | /* |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4810 | * When a seq_file is seeked, it's always traversed sequentially |
| 4811 | * from position 0, so we can simply keep iterating on !0 *pos. |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4812 | */ |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4813 | if (!ctx->procs.started) { |
Vasily Averin | 2d4ecb0 | 2020-01-30 13:34:59 +0300 | [diff] [blame] | 4814 | if (WARN_ON_ONCE((*pos))) |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4815 | return ERR_PTR(-EINVAL); |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4816 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Tejun Heo | 0d2b5955 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4817 | ctx->procs.started = true; |
Vasily Averin | 2d4ecb0 | 2020-01-30 13:34:59 +0300 | [diff] [blame] | 4818 | } else if (!(*pos)) { |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4819 | css_task_iter_end(it); |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4820 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Vasily Averin | 2d4ecb0 | 2020-01-30 13:34:59 +0300 | [diff] [blame] | 4821 | } else |
| 4822 | return it->cur_task; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4823 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4824 | return cgroup_procs_next(s, NULL, NULL); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4825 | } |
| 4826 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4827 | static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) |
| 4828 | { |
| 4829 | struct cgroup *cgrp = seq_css(s)->cgroup; |
| 4830 | |
| 4831 | /* |
| 4832 | * All processes of a threaded subtree belong to the domain cgroup |
| 4833 | * of the subtree. Only threads can be distributed across the |
| 4834 | * subtree. Reject reads on cgroup.procs in the subtree proper. |
| 4835 | * They're always empty anyway. |
| 4836 | */ |
| 4837 | if (cgroup_is_threaded(cgrp)) |
| 4838 | return ERR_PTR(-EOPNOTSUPP); |
| 4839 | |
| 4840 | return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS | |
| 4841 | CSS_TASK_ITER_THREADED); |
| 4842 | } |
| 4843 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4844 | static int cgroup_procs_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4845 | { |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4846 | seq_printf(s, "%d\n", task_pid_vnr(v)); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4847 | return 0; |
| 4848 | } |
| 4849 | |
Christian Brauner | f355322 | 2020-02-05 14:26:21 +0100 | [diff] [blame] | 4850 | static int cgroup_may_write(const struct cgroup *cgrp, struct super_block *sb) |
| 4851 | { |
| 4852 | int ret; |
| 4853 | struct inode *inode; |
| 4854 | |
| 4855 | lockdep_assert_held(&cgroup_mutex); |
| 4856 | |
| 4857 | inode = kernfs_get_inode(sb, cgrp->procs_file.kn); |
| 4858 | if (!inode) |
| 4859 | return -ENOMEM; |
| 4860 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 4861 | ret = inode_permission(&init_user_ns, inode, MAY_WRITE); |
Christian Brauner | f355322 | 2020-02-05 14:26:21 +0100 | [diff] [blame] | 4862 | iput(inode); |
| 4863 | return ret; |
| 4864 | } |
| 4865 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4866 | static int cgroup_procs_write_permission(struct cgroup *src_cgrp, |
| 4867 | struct cgroup *dst_cgrp, |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4868 | struct super_block *sb, |
| 4869 | struct cgroup_namespace *ns) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4870 | { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4871 | struct cgroup *com_cgrp = src_cgrp; |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4872 | int ret; |
| 4873 | |
| 4874 | lockdep_assert_held(&cgroup_mutex); |
| 4875 | |
| 4876 | /* find the common ancestor */ |
| 4877 | while (!cgroup_is_descendant(dst_cgrp, com_cgrp)) |
| 4878 | com_cgrp = cgroup_parent(com_cgrp); |
| 4879 | |
| 4880 | /* %current should be authorized to migrate to the common ancestor */ |
Christian Brauner | f355322 | 2020-02-05 14:26:21 +0100 | [diff] [blame] | 4881 | ret = cgroup_may_write(com_cgrp, sb); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4882 | if (ret) |
| 4883 | return ret; |
| 4884 | |
| 4885 | /* |
| 4886 | * If namespaces are delegation boundaries, %current must be able |
| 4887 | * to see both source and destination cgroups from its namespace. |
| 4888 | */ |
| 4889 | if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) && |
| 4890 | (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) || |
| 4891 | !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp))) |
| 4892 | return -ENOENT; |
| 4893 | |
| 4894 | return 0; |
| 4895 | } |
| 4896 | |
Christian Brauner | 6df970e | 2020-02-05 14:26:18 +0100 | [diff] [blame] | 4897 | static int cgroup_attach_permissions(struct cgroup *src_cgrp, |
| 4898 | struct cgroup *dst_cgrp, |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4899 | struct super_block *sb, bool threadgroup, |
| 4900 | struct cgroup_namespace *ns) |
Christian Brauner | 6df970e | 2020-02-05 14:26:18 +0100 | [diff] [blame] | 4901 | { |
| 4902 | int ret = 0; |
| 4903 | |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4904 | ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, sb, ns); |
Christian Brauner | 6df970e | 2020-02-05 14:26:18 +0100 | [diff] [blame] | 4905 | if (ret) |
| 4906 | return ret; |
| 4907 | |
| 4908 | ret = cgroup_migrate_vet_dst(dst_cgrp); |
| 4909 | if (ret) |
| 4910 | return ret; |
| 4911 | |
| 4912 | if (!threadgroup && (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp)) |
| 4913 | ret = -EOPNOTSUPP; |
| 4914 | |
| 4915 | return ret; |
| 4916 | } |
| 4917 | |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4918 | static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf, |
| 4919 | bool threadgroup) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4920 | { |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4921 | struct cgroup_file_ctx *ctx = of->priv; |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4922 | struct cgroup *src_cgrp, *dst_cgrp; |
| 4923 | struct task_struct *task; |
Tejun Heo | 1756d79 | 2022-01-06 11:02:28 -1000 | [diff] [blame] | 4924 | const struct cred *saved_cred; |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4925 | ssize_t ret; |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 4926 | bool locked; |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4927 | |
| 4928 | dst_cgrp = cgroup_kn_lock_live(of->kn, false); |
| 4929 | if (!dst_cgrp) |
| 4930 | return -ENODEV; |
| 4931 | |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4932 | task = cgroup_procs_write_start(buf, threadgroup, &locked); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4933 | ret = PTR_ERR_OR_ZERO(task); |
| 4934 | if (ret) |
| 4935 | goto out_unlock; |
| 4936 | |
| 4937 | /* find the source cgroup */ |
| 4938 | spin_lock_irq(&css_set_lock); |
| 4939 | src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); |
| 4940 | spin_unlock_irq(&css_set_lock); |
| 4941 | |
Tejun Heo | 1756d79 | 2022-01-06 11:02:28 -1000 | [diff] [blame] | 4942 | /* |
| 4943 | * Process and thread migrations follow same delegation rule. Check |
| 4944 | * permissions using the credentials from file open to protect against |
| 4945 | * inherited fd attacks. |
| 4946 | */ |
| 4947 | saved_cred = override_creds(of->file->f_cred); |
Christian Brauner | 6df970e | 2020-02-05 14:26:18 +0100 | [diff] [blame] | 4948 | ret = cgroup_attach_permissions(src_cgrp, dst_cgrp, |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 4949 | of->file->f_path.dentry->d_sb, |
| 4950 | threadgroup, ctx->ns); |
Tejun Heo | 1756d79 | 2022-01-06 11:02:28 -1000 | [diff] [blame] | 4951 | revert_creds(saved_cred); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4952 | if (ret) |
| 4953 | goto out_finish; |
| 4954 | |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4955 | ret = cgroup_attach_task(dst_cgrp, task, threadgroup); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4956 | |
| 4957 | out_finish: |
Michal Koutný | 9a3284f | 2019-10-04 12:57:40 +0200 | [diff] [blame] | 4958 | cgroup_procs_write_finish(task, locked); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4959 | out_unlock: |
| 4960 | cgroup_kn_unlock(of->kn); |
| 4961 | |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4962 | return ret; |
| 4963 | } |
| 4964 | |
| 4965 | static ssize_t cgroup_procs_write(struct kernfs_open_file *of, |
| 4966 | char *buf, size_t nbytes, loff_t off) |
| 4967 | { |
| 4968 | return __cgroup_procs_write(of, buf, true) ?: nbytes; |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4969 | } |
| 4970 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4971 | static void *cgroup_threads_start(struct seq_file *s, loff_t *pos) |
| 4972 | { |
| 4973 | return __cgroup_procs_start(s, pos, 0); |
| 4974 | } |
| 4975 | |
| 4976 | static ssize_t cgroup_threads_write(struct kernfs_open_file *of, |
| 4977 | char *buf, size_t nbytes, loff_t off) |
| 4978 | { |
Michal Koutný | da70862 | 2021-01-14 13:44:27 +0100 | [diff] [blame] | 4979 | return __cgroup_procs_write(of, buf, false) ?: nbytes; |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4980 | } |
| 4981 | |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4982 | /* cgroup core interface files for the default hierarchy */ |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4983 | static struct cftype cgroup_base_files[] = { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4984 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4985 | .name = "cgroup.type", |
| 4986 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4987 | .seq_show = cgroup_type_show, |
| 4988 | .write = cgroup_type_write, |
| 4989 | }, |
| 4990 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4991 | .name = "cgroup.procs", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 4992 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4993 | .file_offset = offsetof(struct cgroup, procs_file), |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4994 | .release = cgroup_procs_release, |
| 4995 | .seq_start = cgroup_procs_start, |
| 4996 | .seq_next = cgroup_procs_next, |
| 4997 | .seq_show = cgroup_procs_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4998 | .write = cgroup_procs_write, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4999 | }, |
| 5000 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5001 | .name = "cgroup.threads", |
Roman Gushchin | 4f58424 | 2018-01-10 04:35:12 -0800 | [diff] [blame] | 5002 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5003 | .release = cgroup_procs_release, |
| 5004 | .seq_start = cgroup_threads_start, |
| 5005 | .seq_next = cgroup_procs_next, |
| 5006 | .seq_show = cgroup_procs_show, |
| 5007 | .write = cgroup_threads_write, |
| 5008 | }, |
| 5009 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 5010 | .name = "cgroup.controllers", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 5011 | .seq_show = cgroup_controllers_show, |
| 5012 | }, |
| 5013 | { |
| 5014 | .name = "cgroup.subtree_control", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 5015 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 5016 | .seq_show = cgroup_subtree_control_show, |
| 5017 | .write = cgroup_subtree_control_write, |
| 5018 | }, |
| 5019 | { |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 5020 | .name = "cgroup.events", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 5021 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 5022 | .file_offset = offsetof(struct cgroup, events_file), |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 5023 | .seq_show = cgroup_events_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 5024 | }, |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 5025 | { |
| 5026 | .name = "cgroup.max.descendants", |
| 5027 | .seq_show = cgroup_max_descendants_show, |
| 5028 | .write = cgroup_max_descendants_write, |
| 5029 | }, |
| 5030 | { |
| 5031 | .name = "cgroup.max.depth", |
| 5032 | .seq_show = cgroup_max_depth_show, |
| 5033 | .write = cgroup_max_depth_write, |
| 5034 | }, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 5035 | { |
| 5036 | .name = "cgroup.stat", |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 5037 | .seq_show = cgroup_stat_show, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 5038 | }, |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 5039 | { |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5040 | .name = "cgroup.freeze", |
| 5041 | .flags = CFTYPE_NOT_ON_ROOT, |
| 5042 | .seq_show = cgroup_freeze_show, |
| 5043 | .write = cgroup_freeze_write, |
| 5044 | }, |
| 5045 | { |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 5046 | .name = "cgroup.kill", |
| 5047 | .flags = CFTYPE_NOT_ON_ROOT, |
| 5048 | .write = cgroup_kill_write, |
| 5049 | }, |
| 5050 | { |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 5051 | .name = "cpu.stat", |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 5052 | .seq_show = cpu_stat_show, |
| 5053 | }, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5054 | #ifdef CONFIG_PSI |
| 5055 | { |
| 5056 | .name = "io.pressure", |
Suren Baghdasaryan | 3958e2d | 2021-05-24 12:53:39 -0700 | [diff] [blame] | 5057 | .flags = CFTYPE_PRESSURE, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5058 | .seq_show = cgroup_io_pressure_show, |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 5059 | .write = cgroup_io_pressure_write, |
| 5060 | .poll = cgroup_pressure_poll, |
| 5061 | .release = cgroup_pressure_release, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5062 | }, |
| 5063 | { |
| 5064 | .name = "memory.pressure", |
Suren Baghdasaryan | 3958e2d | 2021-05-24 12:53:39 -0700 | [diff] [blame] | 5065 | .flags = CFTYPE_PRESSURE, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5066 | .seq_show = cgroup_memory_pressure_show, |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 5067 | .write = cgroup_memory_pressure_write, |
| 5068 | .poll = cgroup_pressure_poll, |
| 5069 | .release = cgroup_pressure_release, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5070 | }, |
| 5071 | { |
| 5072 | .name = "cpu.pressure", |
Suren Baghdasaryan | 3958e2d | 2021-05-24 12:53:39 -0700 | [diff] [blame] | 5073 | .flags = CFTYPE_PRESSURE, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5074 | .seq_show = cgroup_cpu_pressure_show, |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 5075 | .write = cgroup_cpu_pressure_write, |
| 5076 | .poll = cgroup_pressure_poll, |
| 5077 | .release = cgroup_pressure_release, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5078 | }, |
Suren Baghdasaryan | 0e94682 | 2019-05-14 15:41:15 -0700 | [diff] [blame] | 5079 | #endif /* CONFIG_PSI */ |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 5080 | { } /* terminate */ |
| 5081 | }; |
| 5082 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5083 | /* |
| 5084 | * css destruction is four-stage process. |
| 5085 | * |
| 5086 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 5087 | * Implemented in kill_css(). |
| 5088 | * |
| 5089 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5090 | * and thus css_tryget_online() is guaranteed to fail, the css can be |
| 5091 | * offlined by invoking offline_css(). After offlining, the base ref is |
| 5092 | * put. Implemented in css_killed_work_fn(). |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5093 | * |
| 5094 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 5095 | * accessors are inside RCU read sections. css_release() schedules the |
| 5096 | * RCU callback. |
| 5097 | * |
| 5098 | * 4. After the grace period, the css can be freed. Implemented in |
| 5099 | * css_free_work_fn(). |
| 5100 | * |
| 5101 | * It is actually hairier because both step 2 and 4 require process context |
| 5102 | * and thus involve punting to css->destroy_work adding two additional |
| 5103 | * steps to the already complex sequence. |
| 5104 | */ |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 5105 | static void css_free_rwork_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5106 | { |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 5107 | struct cgroup_subsys_state *css = container_of(to_rcu_work(work), |
| 5108 | struct cgroup_subsys_state, destroy_rwork); |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 5109 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5110 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5111 | |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 5112 | percpu_ref_exit(&css->refcnt); |
| 5113 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 5114 | if (ss) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5115 | /* css free path */ |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 5116 | struct cgroup_subsys_state *parent = css->parent; |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 5117 | int id = css->id; |
| 5118 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 5119 | ss->css_free(css); |
| 5120 | cgroup_idr_remove(&ss->css_idr, id); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5121 | cgroup_put(cgrp); |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 5122 | |
| 5123 | if (parent) |
| 5124 | css_put(parent); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5125 | } else { |
| 5126 | /* cgroup free path */ |
| 5127 | atomic_dec(&cgrp->root->nr_cgrps); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5128 | cgroup1_pidlist_destroy_all(cgrp); |
Zefan Li | 971ff49 | 2014-09-18 16:06:19 +0800 | [diff] [blame] | 5129 | cancel_work_sync(&cgrp->release_agent_work); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5130 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5131 | if (cgroup_parent(cgrp)) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5132 | /* |
| 5133 | * We get a ref to the parent, and put the ref when |
| 5134 | * this cgroup is being freed, so it's guaranteed |
| 5135 | * that the parent won't be destroyed before its |
| 5136 | * children. |
| 5137 | */ |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5138 | cgroup_put(cgroup_parent(cgrp)); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5139 | kernfs_put(cgrp->kn); |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5140 | psi_cgroup_free(cgrp); |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5141 | cgroup_rstat_exit(cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5142 | kfree(cgrp); |
| 5143 | } else { |
| 5144 | /* |
| 5145 | * This is root cgroup's refcnt reaching zero, |
| 5146 | * which indicates that the root should be |
| 5147 | * released. |
| 5148 | */ |
| 5149 | cgroup_destroy_root(cgrp->root); |
| 5150 | } |
| 5151 | } |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5152 | } |
| 5153 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5154 | static void css_release_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5155 | { |
| 5156 | struct cgroup_subsys_state *css = |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5157 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5158 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5159 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5160 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5161 | mutex_lock(&cgroup_mutex); |
| 5162 | |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5163 | css->flags |= CSS_RELEASED; |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5164 | list_del_rcu(&css->sibling); |
| 5165 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5166 | if (ss) { |
| 5167 | /* css release path */ |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 5168 | if (!list_empty(&css->rstat_css_node)) { |
| 5169 | cgroup_rstat_flush(cgrp); |
| 5170 | list_del_rcu(&css->rstat_css_node); |
| 5171 | } |
| 5172 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 5173 | cgroup_idr_replace(&ss->css_idr, NULL, css->id); |
Tejun Heo | 7d172cc | 2014-11-18 02:49:51 -0500 | [diff] [blame] | 5174 | if (ss->css_released) |
| 5175 | ss->css_released(css); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5176 | } else { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5177 | struct cgroup *tcgrp; |
| 5178 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5179 | /* cgroup release path */ |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 5180 | TRACE_CGROUP_PATH(release, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5181 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5182 | cgroup_rstat_flush(cgrp); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5183 | |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5184 | spin_lock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5185 | for (tcgrp = cgroup_parent(cgrp); tcgrp; |
| 5186 | tcgrp = cgroup_parent(tcgrp)) |
| 5187 | tcgrp->nr_dying_descendants--; |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5188 | spin_unlock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5189 | |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 5190 | /* |
| 5191 | * There are two control paths which try to determine |
| 5192 | * cgroup from dentry without going through kernfs - |
| 5193 | * cgroupstats_build() and css_tryget_online_from_dir(). |
| 5194 | * Those are supported by RCU protecting clearing of |
| 5195 | * cgrp->kn->priv backpointer. |
| 5196 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5197 | if (cgrp->kn) |
| 5198 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, |
| 5199 | NULL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5200 | } |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5201 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5202 | mutex_unlock(&cgroup_mutex); |
| 5203 | |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 5204 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 5205 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5206 | } |
| 5207 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5208 | static void css_release(struct percpu_ref *ref) |
| 5209 | { |
| 5210 | struct cgroup_subsys_state *css = |
| 5211 | container_of(ref, struct cgroup_subsys_state, refcnt); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 5212 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5213 | INIT_WORK(&css->destroy_work, css_release_work_fn); |
| 5214 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5215 | } |
| 5216 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5217 | static void init_and_link_css(struct cgroup_subsys_state *css, |
| 5218 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5219 | { |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5220 | lockdep_assert_held(&cgroup_mutex); |
| 5221 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5222 | cgroup_get_live(cgrp); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5223 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5224 | memset(css, 0, sizeof(*css)); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5225 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 5226 | css->ss = ss; |
Tejun Heo | 8fa3b8d | 2016-05-26 15:42:13 -0400 | [diff] [blame] | 5227 | css->id = -1; |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5228 | INIT_LIST_HEAD(&css->sibling); |
| 5229 | INIT_LIST_HEAD(&css->children); |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 5230 | INIT_LIST_HEAD(&css->rstat_css_node); |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5231 | css->serial_nr = css_serial_nr_next++; |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5232 | atomic_set(&css->online_cnt, 0); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5233 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5234 | if (cgroup_parent(cgrp)) { |
| 5235 | css->parent = cgroup_css(cgroup_parent(cgrp), ss); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5236 | css_get(css->parent); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5237 | } |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5238 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5239 | if (ss->css_rstat_flush) |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 5240 | list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list); |
| 5241 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5242 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5243 | } |
| 5244 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 5245 | /* 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] | 5246 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5247 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5248 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5249 | int ret = 0; |
| 5250 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5251 | lockdep_assert_held(&cgroup_mutex); |
| 5252 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5253 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5254 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5255 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5256 | css->flags |= CSS_ONLINE; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5257 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5258 | |
| 5259 | atomic_inc(&css->online_cnt); |
| 5260 | if (css->parent) |
| 5261 | atomic_inc(&css->parent->online_cnt); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5262 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5263 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5264 | } |
| 5265 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 5266 | /* 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] | 5267 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5268 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5269 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5270 | |
| 5271 | lockdep_assert_held(&cgroup_mutex); |
| 5272 | |
| 5273 | if (!(css->flags & CSS_ONLINE)) |
| 5274 | return; |
| 5275 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 5276 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5277 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5278 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5279 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | e329780 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 5280 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 5281 | |
| 5282 | wake_up_all(&css->cgroup->offline_waitq); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5283 | } |
| 5284 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5285 | /** |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5286 | * css_create - create a cgroup_subsys_state |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5287 | * @cgrp: the cgroup new css will be associated with |
| 5288 | * @ss: the subsys of new css |
| 5289 | * |
| 5290 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5291 | * css is online and installed in @cgrp. This function doesn't create the |
| 5292 | * interface files. Returns 0 on success, -errno on failure. |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5293 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5294 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 5295 | struct cgroup_subsys *ss) |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5296 | { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5297 | struct cgroup *parent = cgroup_parent(cgrp); |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5298 | struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5299 | struct cgroup_subsys_state *css; |
| 5300 | int err; |
| 5301 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5302 | lockdep_assert_held(&cgroup_mutex); |
| 5303 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5304 | css = ss->css_alloc(parent_css); |
Tejun Heo | e7e15b8 | 2016-06-21 13:06:24 -0400 | [diff] [blame] | 5305 | if (!css) |
| 5306 | css = ERR_PTR(-ENOMEM); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5307 | if (IS_ERR(css)) |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5308 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5309 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5310 | init_and_link_css(css, ss, cgrp); |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5311 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 5312 | err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5313 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 5314 | goto err_free_css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5315 | |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 5316 | err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5317 | if (err < 0) |
Wenwei Tao | b00c52d | 2016-05-13 22:59:20 +0800 | [diff] [blame] | 5318 | goto err_free_css; |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5319 | css->id = err; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5320 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5321 | /* @css is ready to be brought online now, make it visible */ |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5322 | list_add_tail_rcu(&css->sibling, &parent_css->children); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5323 | cgroup_idr_replace(&ss->css_idr, css, css->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5324 | |
| 5325 | err = online_css(css); |
| 5326 | if (err) |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5327 | goto err_list_del; |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5328 | |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5329 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5330 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5331 | err_list_del: |
| 5332 | list_del_rcu(&css->sibling); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 5333 | err_free_css: |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 5334 | list_del_rcu(&css->rstat_css_node); |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 5335 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 5336 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5337 | return ERR_PTR(err); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5338 | } |
| 5339 | |
Tejun Heo | 07cd129 | 2017-01-26 16:47:28 -0500 | [diff] [blame] | 5340 | /* |
| 5341 | * The returned cgroup is fully initialized including its control mask, but |
| 5342 | * it isn't associated with its kernfs_node and doesn't have the control |
| 5343 | * mask applied. |
| 5344 | */ |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5345 | static struct cgroup *cgroup_create(struct cgroup *parent, const char *name, |
| 5346 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5347 | { |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5348 | struct cgroup_root *root = parent->root; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5349 | struct cgroup *cgrp, *tcgrp; |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5350 | struct kernfs_node *kn; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5351 | int level = parent->level + 1; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5352 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5353 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 5354 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Kees Cook | acafe7e | 2018-05-08 13:45:50 -0700 | [diff] [blame] | 5355 | cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)), |
| 5356 | GFP_KERNEL); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5357 | if (!cgrp) |
| 5358 | return ERR_PTR(-ENOMEM); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 5359 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 5360 | ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5361 | if (ret) |
| 5362 | goto out_free_cgrp; |
| 5363 | |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5364 | ret = cgroup_rstat_init(cgrp); |
| 5365 | if (ret) |
| 5366 | goto out_cancel_ref; |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5367 | |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5368 | /* create the directory */ |
| 5369 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
| 5370 | if (IS_ERR(kn)) { |
| 5371 | ret = PTR_ERR(kn); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5372 | goto out_stat_exit; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5373 | } |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5374 | cgrp->kn = kn; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5375 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 5376 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5377 | |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 5378 | cgrp->self.parent = &parent->self; |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5379 | cgrp->root = root; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5380 | cgrp->level = level; |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5381 | |
| 5382 | ret = psi_cgroup_alloc(cgrp); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 5383 | if (ret) |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5384 | goto out_kernfs_remove; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5385 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5386 | ret = cgroup_bpf_inherit(cgrp); |
| 5387 | if (ret) |
| 5388 | goto out_psi_free; |
| 5389 | |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5390 | /* |
| 5391 | * New cgroup inherits effective freeze counter, and |
| 5392 | * if the parent has to be frozen, the child has too. |
| 5393 | */ |
| 5394 | cgrp->freezer.e_freeze = parent->freezer.e_freeze; |
Roman Gushchin | 97a6136 | 2019-09-12 10:56:45 -0700 | [diff] [blame] | 5395 | if (cgrp->freezer.e_freeze) { |
| 5396 | /* |
| 5397 | * Set the CGRP_FREEZE flag, so when a process will be |
| 5398 | * attached to the child cgroup, it will become frozen. |
| 5399 | * At this point the new cgroup is unpopulated, so we can |
| 5400 | * consider it frozen immediately. |
| 5401 | */ |
| 5402 | set_bit(CGRP_FREEZE, &cgrp->flags); |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5403 | set_bit(CGRP_FROZEN, &cgrp->flags); |
Roman Gushchin | 97a6136 | 2019-09-12 10:56:45 -0700 | [diff] [blame] | 5404 | } |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5405 | |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5406 | spin_lock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5407 | for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5408 | cgrp->ancestor_ids[tcgrp->level] = cgroup_id(tcgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5409 | |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5410 | if (tcgrp != cgrp) { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5411 | tcgrp->nr_descendants++; |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5412 | |
| 5413 | /* |
| 5414 | * If the new cgroup is frozen, all ancestor cgroups |
| 5415 | * get a new frozen descendant, but their state can't |
| 5416 | * change because of this. |
| 5417 | */ |
| 5418 | if (cgrp->freezer.e_freeze) |
| 5419 | tcgrp->freezer.nr_frozen_descendants++; |
| 5420 | } |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5421 | } |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5422 | spin_unlock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5423 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 5424 | if (notify_on_release(parent)) |
| 5425 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 5426 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5427 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 5428 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 5429 | |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5430 | cgrp->self.serial_nr = css_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 5431 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5432 | /* allocation complete, commit to creation */ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5433 | list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5434 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5435 | cgroup_get_live(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 5436 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5437 | /* |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 5438 | * On the default hierarchy, a child doesn't automatically inherit |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 5439 | * subtree_control from the parent. Each is configured manually. |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 5440 | */ |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5441 | if (!cgroup_on_dfl(cgrp)) |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5442 | cgrp->subtree_control = cgroup_control(cgrp); |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5443 | |
| 5444 | cgroup_propagate_control(cgrp); |
| 5445 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5446 | return cgrp; |
| 5447 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5448 | out_psi_free: |
| 5449 | psi_cgroup_free(cgrp); |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5450 | out_kernfs_remove: |
| 5451 | kernfs_remove(cgrp->kn); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5452 | out_stat_exit: |
Johannes Weiner | a7df69b | 2021-04-29 22:56:20 -0700 | [diff] [blame] | 5453 | cgroup_rstat_exit(cgrp); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5454 | out_cancel_ref: |
| 5455 | percpu_ref_exit(&cgrp->self.refcnt); |
| 5456 | out_free_cgrp: |
| 5457 | kfree(cgrp); |
| 5458 | return ERR_PTR(ret); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5459 | } |
| 5460 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 5461 | static bool cgroup_check_hierarchy_limits(struct cgroup *parent) |
| 5462 | { |
| 5463 | struct cgroup *cgroup; |
| 5464 | int ret = false; |
| 5465 | int level = 1; |
| 5466 | |
| 5467 | lockdep_assert_held(&cgroup_mutex); |
| 5468 | |
| 5469 | for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) { |
| 5470 | if (cgroup->nr_descendants >= cgroup->max_descendants) |
| 5471 | goto fail; |
| 5472 | |
| 5473 | if (level > cgroup->max_depth) |
| 5474 | goto fail; |
| 5475 | |
| 5476 | level++; |
| 5477 | } |
| 5478 | |
| 5479 | ret = true; |
| 5480 | fail: |
| 5481 | return ret; |
| 5482 | } |
| 5483 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5484 | 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] | 5485 | { |
| 5486 | struct cgroup *parent, *cgrp; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5487 | int ret; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5488 | |
| 5489 | /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */ |
| 5490 | if (strchr(name, '\n')) |
| 5491 | return -EINVAL; |
| 5492 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5493 | parent = cgroup_kn_lock_live(parent_kn, false); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5494 | if (!parent) |
| 5495 | return -ENODEV; |
| 5496 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 5497 | if (!cgroup_check_hierarchy_limits(parent)) { |
| 5498 | ret = -EAGAIN; |
| 5499 | goto out_unlock; |
| 5500 | } |
| 5501 | |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5502 | cgrp = cgroup_create(parent, name, mode); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5503 | if (IS_ERR(cgrp)) { |
| 5504 | ret = PTR_ERR(cgrp); |
| 5505 | goto out_unlock; |
| 5506 | } |
| 5507 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5508 | /* |
| 5509 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 5510 | * that @cgrp->kn is always accessible. |
| 5511 | */ |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5512 | kernfs_get(cgrp->kn); |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5513 | |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5514 | ret = cgroup_kn_set_ugid(cgrp->kn); |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5515 | if (ret) |
| 5516 | goto out_destroy; |
| 5517 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 5518 | ret = css_populate_dir(&cgrp->self); |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5519 | if (ret) |
| 5520 | goto out_destroy; |
| 5521 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5522 | ret = cgroup_apply_control_enable(cgrp); |
| 5523 | if (ret) |
| 5524 | goto out_destroy; |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5525 | |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 5526 | TRACE_CGROUP_PATH(mkdir, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5527 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5528 | /* let's create and online css's */ |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5529 | kernfs_activate(cgrp->kn); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5530 | |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5531 | ret = 0; |
| 5532 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5533 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5534 | out_destroy: |
| 5535 | cgroup_destroy_locked(cgrp); |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5536 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5537 | cgroup_kn_unlock(parent_kn); |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 5538 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5539 | } |
| 5540 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5541 | /* |
| 5542 | * 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] | 5543 | * css_tryget_online() is now guaranteed to fail. Tell the subsystem to |
Zhen Lei | 08b2b6f | 2021-05-24 16:29:43 +0800 | [diff] [blame] | 5544 | * initiate destruction and put the css ref from kill_css(). |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5545 | */ |
| 5546 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5547 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5548 | struct cgroup_subsys_state *css = |
| 5549 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5550 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5551 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5552 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5553 | do { |
| 5554 | offline_css(css); |
| 5555 | css_put(css); |
| 5556 | /* @css can't go away while we're holding cgroup_mutex */ |
| 5557 | css = css->parent; |
| 5558 | } while (css && atomic_dec_and_test(&css->online_cnt)); |
| 5559 | |
| 5560 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5561 | } |
| 5562 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5563 | /* css kill confirmation processing requires process context, bounce */ |
| 5564 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5565 | { |
| 5566 | struct cgroup_subsys_state *css = |
| 5567 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 5568 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5569 | if (atomic_dec_and_test(&css->online_cnt)) { |
| 5570 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
| 5571 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
| 5572 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5573 | } |
| 5574 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5575 | /** |
| 5576 | * kill_css - destroy a css |
| 5577 | * @css: css to destroy |
| 5578 | * |
| 5579 | * This function initiates destruction of @css by removing cgroup interface |
| 5580 | * files and putting its base reference. ->css_offline() will be invoked |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5581 | * asynchronously once css_tryget_online() is guaranteed to fail and when |
| 5582 | * the reference count reaches zero, @css will be released. |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5583 | */ |
| 5584 | static void kill_css(struct cgroup_subsys_state *css) |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5585 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5586 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5587 | |
Waiman Long | 33c35aa | 2017-05-15 09:34:06 -0400 | [diff] [blame] | 5588 | if (css->flags & CSS_DYING) |
| 5589 | return; |
| 5590 | |
| 5591 | css->flags |= CSS_DYING; |
| 5592 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5593 | /* |
| 5594 | * This must happen before css is disassociated with its cgroup. |
| 5595 | * See seq_css() for details. |
| 5596 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 5597 | css_clear_dir(css); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5598 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5599 | /* |
| 5600 | * Killing would put the base ref, but we need to keep it alive |
| 5601 | * until after ->css_offline(). |
| 5602 | */ |
| 5603 | css_get(css); |
| 5604 | |
| 5605 | /* |
| 5606 | * cgroup core guarantees that, by the time ->css_offline() is |
| 5607 | * invoked, no new css reference will be given out via |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5608 | * css_tryget_online(). We can't simply call percpu_ref_kill() and |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5609 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 5610 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 5611 | * |
| 5612 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 5613 | * css is confirmed to be seen as killed on all CPUs. |
| 5614 | */ |
| 5615 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | /** |
| 5619 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 5620 | * @cgrp: cgroup to be destroyed |
| 5621 | * |
| 5622 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 5623 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5624 | * guarantee that css_tryget_online() won't succeed by the time |
| 5625 | * ->css_offline() is invoked. To satisfy all the requirements, |
| 5626 | * destruction is implemented in the following two steps. |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5627 | * |
| 5628 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 5629 | * userland visible parts and start killing the percpu refcnts of |
| 5630 | * css's. Set up so that the next stage will be kicked off once all |
| 5631 | * the percpu refcnts are confirmed to be killed. |
| 5632 | * |
| 5633 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 5634 | * rest of destruction. Once all cgroup references are gone, the |
| 5635 | * cgroup is RCU-freed. |
| 5636 | * |
| 5637 | * This function implements s1. After this step, @cgrp is gone as far as |
| 5638 | * the userland is concerned and a new cgroup with the same name may be |
| 5639 | * created. As cgroup doesn't care about the names internally, this |
| 5640 | * doesn't cause any problem. |
| 5641 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5642 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 5643 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5644 | { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5645 | struct cgroup *tcgrp, *parent = cgroup_parent(cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5646 | struct cgroup_subsys_state *css; |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5647 | struct cgrp_cset_link *link; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5648 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5649 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5650 | lockdep_assert_held(&cgroup_mutex); |
| 5651 | |
Tejun Heo | 91486f6 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 5652 | /* |
| 5653 | * Only migration can raise populated from zero and we're already |
| 5654 | * holding cgroup_mutex. |
| 5655 | */ |
| 5656 | if (cgroup_is_populated(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5657 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5658 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5659 | /* |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5660 | * Make sure there's no live children. We can't test emptiness of |
| 5661 | * ->self.children as dead children linger on it while being |
| 5662 | * drained; otherwise, "rmdir parent/child parent" may fail. |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5663 | */ |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 5664 | if (css_has_online_children(&cgrp->self)) |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5665 | return -EBUSY; |
| 5666 | |
| 5667 | /* |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5668 | * Mark @cgrp and the associated csets dead. The former prevents |
| 5669 | * further task migration and child creation by disabling |
| 5670 | * cgroup_lock_live_group(). The latter makes the csets ignored by |
| 5671 | * the migration path. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5672 | */ |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 5673 | cgrp->self.flags &= ~CSS_ONLINE; |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5674 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5675 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5676 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 5677 | link->cset->dead = true; |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5678 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5679 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5680 | /* initiate massacre of all css's */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5681 | for_each_css(css, ssid, cgrp) |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5682 | kill_css(css); |
| 5683 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5684 | /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ |
| 5685 | css_clear_dir(&cgrp->self); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5686 | kernfs_remove(cgrp->kn); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5687 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 5688 | if (parent && cgroup_is_threaded(cgrp)) |
| 5689 | parent->nr_threaded_children--; |
| 5690 | |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5691 | spin_lock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5692 | for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
| 5693 | tcgrp->nr_descendants--; |
| 5694 | tcgrp->nr_dying_descendants++; |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 5695 | /* |
| 5696 | * If the dying cgroup is frozen, decrease frozen descendants |
| 5697 | * counters of ancestor cgroups. |
| 5698 | */ |
| 5699 | if (test_bit(CGRP_FROZEN, &cgrp->flags)) |
| 5700 | tcgrp->freezer.nr_frozen_descendants--; |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5701 | } |
Roman Gushchin | 4dcabec | 2019-04-19 10:03:03 -0700 | [diff] [blame] | 5702 | spin_unlock_irq(&css_set_lock); |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5703 | |
Roman Gushchin | 5a621e6 | 2017-08-02 17:55:32 +0100 | [diff] [blame] | 5704 | cgroup1_check_for_release(parent); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5705 | |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 5706 | cgroup_bpf_offline(cgrp); |
| 5707 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5708 | /* put the base reference */ |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5709 | percpu_ref_kill(&cgrp->self.refcnt); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5710 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 5711 | return 0; |
| 5712 | }; |
| 5713 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5714 | int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5715 | { |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5716 | struct cgroup *cgrp; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5717 | int ret = 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5718 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5719 | cgrp = cgroup_kn_lock_live(kn, false); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5720 | if (!cgrp) |
| 5721 | return 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5722 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5723 | ret = cgroup_destroy_locked(cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5724 | if (!ret) |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 5725 | TRACE_CGROUP_PATH(rmdir, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5726 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5727 | cgroup_kn_unlock(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5728 | return ret; |
| 5729 | } |
| 5730 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5731 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 5732 | .show_options = cgroup_show_options, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5733 | .mkdir = cgroup_mkdir, |
| 5734 | .rmdir = cgroup_rmdir, |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 5735 | .show_path = cgroup_show_path, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5736 | }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5737 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5738 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5739 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5740 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 5741 | |
Tejun Heo | a5ae989 | 2015-12-29 14:53:56 -0500 | [diff] [blame] | 5742 | pr_debug("Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5743 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5744 | mutex_lock(&cgroup_mutex); |
| 5745 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5746 | idr_init(&ss->css_idr); |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 5747 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5748 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5749 | /* Create the root cgroup state for this subsystem */ |
| 5750 | ss->root = &cgrp_dfl_root; |
Wei Yang | 8291471 | 2021-11-27 14:59:19 +0000 | [diff] [blame] | 5751 | css = ss->css_alloc(NULL); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5752 | /* We don't handle early failures gracefully */ |
| 5753 | BUG_ON(IS_ERR(css)); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5754 | init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5755 | |
| 5756 | /* |
| 5757 | * Root csses are never destroyed and we can't initialize |
| 5758 | * percpu_ref during early init. Disable refcnting. |
| 5759 | */ |
| 5760 | css->flags |= CSS_NO_REF; |
| 5761 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5762 | if (early) { |
Tejun Heo | 9395a45 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5763 | /* allocation can't be done safely during early init */ |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5764 | css->id = 1; |
| 5765 | } else { |
| 5766 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); |
| 5767 | BUG_ON(css->id < 0); |
| 5768 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5769 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5770 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5771 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5772 | * newly registered, all tasks and hence the |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5773 | * init_css_set is in the subsystem's root cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5774 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5775 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 5776 | have_fork_callback |= (bool)ss->fork << ss->id; |
| 5777 | have_exit_callback |= (bool)ss->exit << ss->id; |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 5778 | have_release_callback |= (bool)ss->release << ss->id; |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5779 | have_canfork_callback |= (bool)ss->can_fork << ss->id; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5780 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5781 | /* At system boot, before all subsystems have been |
| 5782 | * registered, no tasks have been forked, so we don't |
| 5783 | * need to invoke fork callbacks here. */ |
| 5784 | BUG_ON(!list_empty(&init_task.tasks)); |
| 5785 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5786 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 5787 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5788 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5789 | } |
| 5790 | |
| 5791 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5792 | * cgroup_init_early - cgroup initialization at system boot |
| 5793 | * |
| 5794 | * Initialize cgroups at system boot, and initialize any |
| 5795 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5796 | */ |
| 5797 | int __init cgroup_init_early(void) |
| 5798 | { |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 5799 | static struct cgroup_fs_context __initdata ctx; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5800 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5801 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5802 | |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 5803 | ctx.root = &cgrp_dfl_root; |
| 5804 | init_cgroup_root(&ctx); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5805 | cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; |
| 5806 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5807 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5808 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5809 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5810 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Xiubo Li | 63253ad | 2016-02-26 13:07:38 +0800 | [diff] [blame] | 5811 | "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] | 5812 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5813 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5814 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 5815 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5816 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5817 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5818 | ss->name = cgroup_subsys_name[i]; |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5819 | if (!ss->legacy_name) |
| 5820 | ss->legacy_name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5821 | |
| 5822 | if (ss->early_init) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5823 | cgroup_init_subsys(ss, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5824 | } |
| 5825 | return 0; |
| 5826 | } |
| 5827 | |
| 5828 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5829 | * cgroup_init - cgroup initialization |
| 5830 | * |
| 5831 | * Register cgroup filesystem and /proc file, and initialize |
| 5832 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5833 | */ |
| 5834 | int __init cgroup_init(void) |
| 5835 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5836 | struct cgroup_subsys *ss; |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5837 | int ssid; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5838 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 5839 | BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5840 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
| 5841 | BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5842 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5843 | cgroup_rstat_boot(); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5844 | |
Peter Zijlstra | 3942a9b | 2016-08-11 18:54:13 +0200 | [diff] [blame] | 5845 | /* |
Paul E. McKenney | 2af3024 | 2018-11-07 14:11:40 -0800 | [diff] [blame] | 5846 | * The latency of the synchronize_rcu() is too high for cgroups, |
Peter Zijlstra | 3942a9b | 2016-08-11 18:54:13 +0200 | [diff] [blame] | 5847 | * avoid it at the cost of forcing all readers into the slow path. |
| 5848 | */ |
| 5849 | rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss); |
| 5850 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 5851 | get_user_ns(init_cgroup_ns.user_ns); |
| 5852 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5853 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5854 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5855 | /* |
| 5856 | * Add init_css_set to the hash table so that dfl_root can link to |
| 5857 | * it during init. |
| 5858 | */ |
| 5859 | hash_add(css_set_table, &init_css_set.hlist, |
| 5860 | css_set_hash(init_css_set.subsys)); |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5861 | |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 5862 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5863 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5864 | mutex_unlock(&cgroup_mutex); |
| 5865 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5866 | for_each_subsys(ss, ssid) { |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5867 | if (ss->early_init) { |
| 5868 | struct cgroup_subsys_state *css = |
| 5869 | init_css_set.subsys[ss->id]; |
| 5870 | |
| 5871 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, |
| 5872 | GFP_KERNEL); |
| 5873 | BUG_ON(css->id < 0); |
| 5874 | } else { |
| 5875 | cgroup_init_subsys(ss, false); |
| 5876 | } |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5877 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 5878 | list_add_tail(&init_css_set.e_cset_node[ssid], |
| 5879 | &cgrp_dfl_root.cgrp.e_csets[ssid]); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5880 | |
| 5881 | /* |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5882 | * Setting dfl_root subsys_mask needs to consider the |
| 5883 | * disabled flag and cftype registration needs kmalloc, |
| 5884 | * both of which aren't available during early_init. |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5885 | */ |
Shakeel Butt | 45e1ba4 | 2021-05-12 13:19:46 -0700 | [diff] [blame] | 5886 | if (!cgroup_ssid_enabled(ssid)) |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5887 | continue; |
| 5888 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5889 | if (cgroup1_ssid_disabled(ssid)) |
Johannes Weiner | 223ffb2 | 2016-02-11 13:34:49 -0500 | [diff] [blame] | 5890 | printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n", |
| 5891 | ss->name); |
| 5892 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5893 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; |
| 5894 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5895 | /* implicit controllers must be threaded too */ |
| 5896 | WARN_ON(ss->implicit_on_dfl && !ss->threaded); |
| 5897 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 5898 | if (ss->implicit_on_dfl) |
| 5899 | cgrp_dfl_implicit_ss_mask |= 1 << ss->id; |
| 5900 | else if (!ss->dfl_cftypes) |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 5901 | cgrp_dfl_inhibit_ss_mask |= 1 << ss->id; |
Tejun Heo | 5de4fa1 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5902 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5903 | if (ss->threaded) |
| 5904 | cgrp_dfl_threaded_ss_mask |= 1 << ss->id; |
| 5905 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5906 | if (ss->dfl_cftypes == ss->legacy_cftypes) { |
| 5907 | WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes)); |
| 5908 | } else { |
| 5909 | WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes)); |
| 5910 | WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes)); |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5911 | } |
Vladimir Davydov | 295458e | 2015-02-19 17:34:46 +0300 | [diff] [blame] | 5912 | |
| 5913 | if (ss->bind) |
| 5914 | ss->bind(init_css_set.subsys[ssid]); |
Tejun Heo | 7af608e | 2017-07-18 17:57:46 -0400 | [diff] [blame] | 5915 | |
| 5916 | mutex_lock(&cgroup_mutex); |
| 5917 | css_populate_dir(init_css_set.subsys[ssid]); |
| 5918 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5919 | } |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5920 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5921 | /* init_css_set.subsys[] has been updated, re-hash */ |
| 5922 | hash_del(&init_css_set.hlist); |
| 5923 | hash_add(css_set_table, &init_css_set.hlist, |
| 5924 | css_set_hash(init_css_set.subsys)); |
| 5925 | |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5926 | WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup")); |
| 5927 | WARN_ON(register_filesystem(&cgroup_fs_type)); |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 5928 | WARN_ON(register_filesystem(&cgroup2_fs_type)); |
Christoph Hellwig | 3f3942a | 2018-05-15 15:57:23 +0200 | [diff] [blame] | 5929 | WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show)); |
Al Viro | d5f68d3 | 2019-05-13 12:33:22 -0400 | [diff] [blame] | 5930 | #ifdef CONFIG_CPUSETS |
| 5931 | WARN_ON(register_filesystem(&cpuset_fs_type)); |
| 5932 | #endif |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5933 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5934 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5935 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5936 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5937 | static int __init cgroup_wq_init(void) |
| 5938 | { |
| 5939 | /* |
| 5940 | * There isn't much point in executing destruction path in |
| 5941 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5942 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5943 | * |
| 5944 | * We would prefer to do this in cgroup_init() above, but that |
| 5945 | * is called before init_workqueues(): so leave this until after. |
| 5946 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5947 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5948 | BUG_ON(!cgroup_destroy_wq); |
| 5949 | return 0; |
| 5950 | } |
| 5951 | core_initcall(cgroup_wq_init); |
| 5952 | |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5953 | 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] | 5954 | { |
| 5955 | struct kernfs_node *kn; |
| 5956 | |
Tejun Heo | fe0f726 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 5957 | 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] | 5958 | if (!kn) |
| 5959 | return; |
| 5960 | kernfs_path(kn, buf, buflen); |
| 5961 | kernfs_put(kn); |
| 5962 | } |
| 5963 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5964 | /* |
Muneendra Kumar | 6b658c4 | 2021-06-08 10:05:44 +0530 | [diff] [blame] | 5965 | * cgroup_get_from_id : get the cgroup associated with cgroup id |
| 5966 | * @id: cgroup id |
| 5967 | * On success return the cgrp, on failure return NULL |
| 5968 | */ |
| 5969 | struct cgroup *cgroup_get_from_id(u64 id) |
| 5970 | { |
| 5971 | struct kernfs_node *kn; |
| 5972 | struct cgroup *cgrp = NULL; |
| 5973 | |
Muneendra Kumar | 6b658c4 | 2021-06-08 10:05:44 +0530 | [diff] [blame] | 5974 | kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id); |
| 5975 | if (!kn) |
Shakeel Butt | be28816 | 2021-10-24 23:19:14 -0700 | [diff] [blame] | 5976 | goto out; |
Muneendra Kumar | 6b658c4 | 2021-06-08 10:05:44 +0530 | [diff] [blame] | 5977 | |
Shakeel Butt | be28816 | 2021-10-24 23:19:14 -0700 | [diff] [blame] | 5978 | rcu_read_lock(); |
| 5979 | |
| 5980 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); |
| 5981 | if (cgrp && !cgroup_tryget(cgrp)) |
Muneendra Kumar | 6b658c4 | 2021-06-08 10:05:44 +0530 | [diff] [blame] | 5982 | cgrp = NULL; |
Shakeel Butt | be28816 | 2021-10-24 23:19:14 -0700 | [diff] [blame] | 5983 | |
| 5984 | rcu_read_unlock(); |
| 5985 | |
Muneendra Kumar | 6b658c4 | 2021-06-08 10:05:44 +0530 | [diff] [blame] | 5986 | kernfs_put(kn); |
Shakeel Butt | be28816 | 2021-10-24 23:19:14 -0700 | [diff] [blame] | 5987 | out: |
Muneendra Kumar | 6b658c4 | 2021-06-08 10:05:44 +0530 | [diff] [blame] | 5988 | return cgrp; |
| 5989 | } |
| 5990 | EXPORT_SYMBOL_GPL(cgroup_get_from_id); |
| 5991 | |
| 5992 | /* |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5993 | * proc_cgroup_show() |
| 5994 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 5995 | * - Used for /proc/<pid>/cgroup. |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5996 | */ |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 5997 | int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, |
| 5998 | struct pid *pid, struct task_struct *tsk) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5999 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 6000 | char *buf; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6001 | int retval; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 6002 | struct cgroup_root *root; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6003 | |
| 6004 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 6005 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6006 | if (!buf) |
| 6007 | goto out; |
| 6008 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6009 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 6010 | spin_lock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6011 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 6012 | for_each_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6013 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 6014 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 6015 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6016 | |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 6017 | if (root == &cgrp_dfl_root && !cgrp_dfl_visible) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 6018 | continue; |
| 6019 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 6020 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | d98817d | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 6021 | if (root != &cgrp_dfl_root) |
| 6022 | for_each_subsys(ss, ssid) |
| 6023 | if (root->subsys_mask & (1 << ssid)) |
| 6024 | seq_printf(m, "%s%s", count++ ? "," : "", |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 6025 | ss->legacy_name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 6026 | if (strlen(root->name)) |
| 6027 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 6028 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6029 | seq_putc(m, ':'); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6030 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 6031 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6032 | |
| 6033 | /* |
| 6034 | * On traditional hierarchies, all zombie tasks show up as |
| 6035 | * belonging to the root cgroup. On the default hierarchy, |
| 6036 | * while a zombie doesn't show up in "cgroup.procs" and |
| 6037 | * thus can't be migrated, its /proc/PID/cgroup keeps |
| 6038 | * reporting the cgroup it belonged to before exiting. If |
| 6039 | * the cgroup is removed before the zombie is reaped, |
| 6040 | * " (deleted)" is appended to the cgroup path. |
| 6041 | */ |
| 6042 | if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 6043 | retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX, |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 6044 | current->nsproxy->cgroup_ns); |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 6045 | if (retval >= PATH_MAX) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6046 | retval = -ENAMETOOLONG; |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 6047 | if (retval < 0) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6048 | goto out_unlock; |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6049 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 6050 | seq_puts(m, buf); |
| 6051 | } else { |
| 6052 | seq_puts(m, "/"); |
| 6053 | } |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6054 | |
| 6055 | if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp)) |
| 6056 | seq_puts(m, " (deleted)\n"); |
| 6057 | else |
| 6058 | seq_putc(m, '\n'); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6059 | } |
| 6060 | |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 6061 | retval = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6062 | out_unlock: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 6063 | spin_unlock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6064 | mutex_unlock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 6065 | kfree(buf); |
| 6066 | out: |
| 6067 | return retval; |
| 6068 | } |
| 6069 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6070 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 6071 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 6072 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6073 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 6074 | * 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] | 6075 | * attaches it to the target css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6076 | */ |
| 6077 | void cgroup_fork(struct task_struct *child) |
| 6078 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 6079 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6080 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6081 | } |
| 6082 | |
Christian Brauner | 1770309 | 2020-02-05 14:26:19 +0100 | [diff] [blame] | 6083 | static struct cgroup *cgroup_get_from_file(struct file *f) |
| 6084 | { |
| 6085 | struct cgroup_subsys_state *css; |
| 6086 | struct cgroup *cgrp; |
| 6087 | |
| 6088 | css = css_tryget_online_from_dir(f->f_path.dentry, NULL); |
| 6089 | if (IS_ERR(css)) |
| 6090 | return ERR_CAST(css); |
| 6091 | |
| 6092 | cgrp = css->cgroup; |
| 6093 | if (!cgroup_on_dfl(cgrp)) { |
| 6094 | cgroup_put(cgrp); |
| 6095 | return ERR_PTR(-EBADF); |
| 6096 | } |
| 6097 | |
| 6098 | return cgrp; |
| 6099 | } |
| 6100 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6101 | /** |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6102 | * cgroup_css_set_fork - find or create a css_set for a child process |
| 6103 | * @kargs: the arguments passed to create the child process |
| 6104 | * |
| 6105 | * This functions finds or creates a new css_set which the child |
| 6106 | * process will be attached to in cgroup_post_fork(). By default, |
| 6107 | * the child process will be given the same css_set as its parent. |
| 6108 | * |
| 6109 | * If CLONE_INTO_CGROUP is specified this function will try to find an |
| 6110 | * existing css_set which includes the requested cgroup and if not create |
| 6111 | * a new css_set that the child will be attached to later. If this function |
| 6112 | * succeeds it will hold cgroup_threadgroup_rwsem on return. If |
| 6113 | * CLONE_INTO_CGROUP is requested this function will grab cgroup mutex |
| 6114 | * before grabbing cgroup_threadgroup_rwsem and will hold a reference |
| 6115 | * to the target cgroup. |
| 6116 | */ |
| 6117 | static int cgroup_css_set_fork(struct kernel_clone_args *kargs) |
| 6118 | __acquires(&cgroup_mutex) __acquires(&cgroup_threadgroup_rwsem) |
| 6119 | { |
| 6120 | int ret; |
| 6121 | struct cgroup *dst_cgrp = NULL; |
| 6122 | struct css_set *cset; |
| 6123 | struct super_block *sb; |
| 6124 | struct file *f; |
| 6125 | |
| 6126 | if (kargs->flags & CLONE_INTO_CGROUP) |
| 6127 | mutex_lock(&cgroup_mutex); |
| 6128 | |
| 6129 | cgroup_threadgroup_change_begin(current); |
| 6130 | |
| 6131 | spin_lock_irq(&css_set_lock); |
| 6132 | cset = task_css_set(current); |
| 6133 | get_css_set(cset); |
| 6134 | spin_unlock_irq(&css_set_lock); |
| 6135 | |
| 6136 | if (!(kargs->flags & CLONE_INTO_CGROUP)) { |
| 6137 | kargs->cset = cset; |
| 6138 | return 0; |
| 6139 | } |
| 6140 | |
| 6141 | f = fget_raw(kargs->cgroup); |
| 6142 | if (!f) { |
| 6143 | ret = -EBADF; |
| 6144 | goto err; |
| 6145 | } |
| 6146 | sb = f->f_path.dentry->d_sb; |
| 6147 | |
| 6148 | dst_cgrp = cgroup_get_from_file(f); |
| 6149 | if (IS_ERR(dst_cgrp)) { |
| 6150 | ret = PTR_ERR(dst_cgrp); |
| 6151 | dst_cgrp = NULL; |
| 6152 | goto err; |
| 6153 | } |
| 6154 | |
| 6155 | if (cgroup_is_dead(dst_cgrp)) { |
| 6156 | ret = -ENODEV; |
| 6157 | goto err; |
| 6158 | } |
| 6159 | |
| 6160 | /* |
| 6161 | * Verify that we the target cgroup is writable for us. This is |
| 6162 | * usually done by the vfs layer but since we're not going through |
| 6163 | * the vfs layer here we need to do it "manually". |
| 6164 | */ |
| 6165 | ret = cgroup_may_write(dst_cgrp, sb); |
| 6166 | if (ret) |
| 6167 | goto err; |
| 6168 | |
| 6169 | ret = cgroup_attach_permissions(cset->dfl_cgrp, dst_cgrp, sb, |
Tejun Heo | e574576 | 2022-01-06 11:02:29 -1000 | [diff] [blame] | 6170 | !(kargs->flags & CLONE_THREAD), |
| 6171 | current->nsproxy->cgroup_ns); |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6172 | if (ret) |
| 6173 | goto err; |
| 6174 | |
| 6175 | kargs->cset = find_css_set(cset, dst_cgrp); |
| 6176 | if (!kargs->cset) { |
| 6177 | ret = -ENOMEM; |
| 6178 | goto err; |
| 6179 | } |
| 6180 | |
| 6181 | put_css_set(cset); |
| 6182 | fput(f); |
| 6183 | kargs->cgrp = dst_cgrp; |
| 6184 | return ret; |
| 6185 | |
| 6186 | err: |
| 6187 | cgroup_threadgroup_change_end(current); |
| 6188 | mutex_unlock(&cgroup_mutex); |
| 6189 | if (f) |
| 6190 | fput(f); |
| 6191 | if (dst_cgrp) |
| 6192 | cgroup_put(dst_cgrp); |
| 6193 | put_css_set(cset); |
| 6194 | if (kargs->cset) |
| 6195 | put_css_set(kargs->cset); |
| 6196 | return ret; |
| 6197 | } |
| 6198 | |
| 6199 | /** |
| 6200 | * cgroup_css_set_put_fork - drop references we took during fork |
| 6201 | * @kargs: the arguments passed to create the child process |
| 6202 | * |
| 6203 | * Drop references to the prepared css_set and target cgroup if |
| 6204 | * CLONE_INTO_CGROUP was requested. |
| 6205 | */ |
| 6206 | static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs) |
| 6207 | __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex) |
| 6208 | { |
| 6209 | cgroup_threadgroup_change_end(current); |
| 6210 | |
| 6211 | if (kargs->flags & CLONE_INTO_CGROUP) { |
| 6212 | struct cgroup *cgrp = kargs->cgrp; |
| 6213 | struct css_set *cset = kargs->cset; |
| 6214 | |
| 6215 | mutex_unlock(&cgroup_mutex); |
| 6216 | |
| 6217 | if (cset) { |
| 6218 | put_css_set(cset); |
| 6219 | kargs->cset = NULL; |
| 6220 | } |
| 6221 | |
| 6222 | if (cgrp) { |
| 6223 | cgroup_put(cgrp); |
| 6224 | kargs->cgrp = NULL; |
| 6225 | } |
| 6226 | } |
| 6227 | } |
| 6228 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6229 | /** |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6230 | * 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] | 6231 | * @child: the child process |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6232 | * |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6233 | * This prepares a new css_set for the child process which the child will |
| 6234 | * be attached to in cgroup_post_fork(). |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6235 | * This calls the subsystem can_fork() callbacks. If the cgroup_can_fork() |
| 6236 | * callback returns an error, the fork aborts with that error code. This |
| 6237 | * allows for a cgroup subsystem to conditionally allow or deny new forks. |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6238 | */ |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6239 | 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] | 6240 | { |
| 6241 | struct cgroup_subsys *ss; |
| 6242 | int i, j, ret; |
| 6243 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6244 | ret = cgroup_css_set_fork(kargs); |
| 6245 | if (ret) |
| 6246 | return ret; |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6247 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6248 | do_each_subsys_mask(ss, i, have_canfork_callback) { |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6249 | ret = ss->can_fork(child, kargs->cset); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6250 | if (ret) |
| 6251 | goto out_revert; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6252 | } while_each_subsys_mask(); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6253 | |
| 6254 | return 0; |
| 6255 | |
| 6256 | out_revert: |
| 6257 | for_each_subsys(ss, j) { |
| 6258 | if (j >= i) |
| 6259 | break; |
| 6260 | if (ss->cancel_fork) |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6261 | ss->cancel_fork(child, kargs->cset); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6262 | } |
| 6263 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6264 | cgroup_css_set_put_fork(kargs); |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6265 | |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6266 | return ret; |
| 6267 | } |
| 6268 | |
| 6269 | /** |
| 6270 | * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6271 | * @child: the child process |
| 6272 | * @kargs: the arguments passed to create the child process |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6273 | * |
| 6274 | * This calls the cancel_fork() callbacks if a fork failed *after* |
Zhen Lei | 08b2b6f | 2021-05-24 16:29:43 +0800 | [diff] [blame] | 6275 | * cgroup_can_fork() succeeded and cleans up references we took to |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6276 | * 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] | 6277 | */ |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6278 | void cgroup_cancel_fork(struct task_struct *child, |
| 6279 | struct kernel_clone_args *kargs) |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6280 | { |
| 6281 | struct cgroup_subsys *ss; |
| 6282 | int i; |
| 6283 | |
| 6284 | for_each_subsys(ss, i) |
| 6285 | if (ss->cancel_fork) |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6286 | ss->cancel_fork(child, kargs->cset); |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6287 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6288 | cgroup_css_set_put_fork(kargs); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 6289 | } |
| 6290 | |
| 6291 | /** |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6292 | * cgroup_post_fork - finalize cgroup setup for the child process |
| 6293 | * @child: the child process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 6294 | * |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6295 | * Attach the child process to its css_set calling the subsystem fork() |
| 6296 | * callbacks. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 6297 | */ |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6298 | void cgroup_post_fork(struct task_struct *child, |
| 6299 | struct kernel_clone_args *kargs) |
| 6300 | __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6301 | { |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6302 | unsigned long cgrp_flags = 0; |
| 6303 | bool kill = false; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6304 | struct cgroup_subsys *ss; |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6305 | struct css_set *cset; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 6306 | int i; |
| 6307 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6308 | cset = kargs->cset; |
| 6309 | kargs->cset = NULL; |
| 6310 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6311 | spin_lock_irq(&css_set_lock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 6312 | |
Tejun Heo | 0cd9d33 | 2020-01-30 11:37:33 -0500 | [diff] [blame] | 6313 | /* init tasks are special, only link regular threads */ |
| 6314 | if (likely(child->pid)) { |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6315 | if (kargs->cgrp) |
| 6316 | cgrp_flags = kargs->cgrp->flags; |
| 6317 | else |
| 6318 | cgrp_flags = cset->dfl_cgrp->flags; |
| 6319 | |
Tejun Heo | 0cd9d33 | 2020-01-30 11:37:33 -0500 | [diff] [blame] | 6320 | WARN_ON_ONCE(!list_empty(&child->cg_list)); |
Tejun Heo | 0cd9d33 | 2020-01-30 11:37:33 -0500 | [diff] [blame] | 6321 | cset->nr_tasks++; |
| 6322 | css_set_move_task(child, NULL, cset, false); |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6323 | } else { |
| 6324 | put_css_set(cset); |
| 6325 | cset = NULL; |
Tejun Heo | 0cd9d33 | 2020-01-30 11:37:33 -0500 | [diff] [blame] | 6326 | } |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6327 | |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6328 | if (!(child->flags & PF_KTHREAD)) { |
| 6329 | if (unlikely(test_bit(CGRP_FREEZE, &cgrp_flags))) { |
| 6330 | /* |
| 6331 | * If the cgroup has to be frozen, the new task has |
| 6332 | * too. Let's set the JOBCTL_TRAP_FREEZE jobctl bit to |
| 6333 | * get the task into the frozen state. |
| 6334 | */ |
| 6335 | spin_lock(&child->sighand->siglock); |
| 6336 | WARN_ON_ONCE(child->frozen); |
| 6337 | child->jobctl |= JOBCTL_TRAP_FREEZE; |
| 6338 | spin_unlock(&child->sighand->siglock); |
| 6339 | |
| 6340 | /* |
| 6341 | * Calling cgroup_update_frozen() isn't required here, |
| 6342 | * because it will be called anyway a bit later from |
| 6343 | * do_freezer_trap(). So we avoid cgroup's transient |
| 6344 | * switch from the frozen state and back. |
| 6345 | */ |
| 6346 | } |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 6347 | |
| 6348 | /* |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6349 | * If the cgroup is to be killed notice it now and take the |
| 6350 | * child down right after we finished preparing it for |
| 6351 | * userspace. |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 6352 | */ |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6353 | kill = test_bit(CGRP_KILL, &cgrp_flags); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6354 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 6355 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6356 | spin_unlock_irq(&css_set_lock); |
| 6357 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 6358 | /* |
| 6359 | * Call ss->fork(). This must happen after @child is linked on |
| 6360 | * css_set; otherwise, @child might change state between ->fork() |
| 6361 | * and addition to css_set. |
| 6362 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6363 | do_each_subsys_mask(ss, i, have_fork_callback) { |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 6364 | ss->fork(child); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6365 | } while_each_subsys_mask(); |
Christian Brauner | 5a5cf5c | 2020-02-05 14:26:20 +0100 | [diff] [blame] | 6366 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6367 | /* Make the new cset the root_cset of the new cgroup namespace. */ |
| 6368 | if (kargs->flags & CLONE_NEWCGROUP) { |
| 6369 | struct css_set *rcset = child->nsproxy->cgroup_ns->root_cset; |
| 6370 | |
| 6371 | get_css_set(cset); |
| 6372 | child->nsproxy->cgroup_ns->root_cset = cset; |
| 6373 | put_css_set(rcset); |
| 6374 | } |
| 6375 | |
Christian Brauner | 661ee62 | 2021-05-08 14:15:38 +0200 | [diff] [blame] | 6376 | /* Cgroup has to be killed so take down child immediately. */ |
| 6377 | if (unlikely(kill)) |
| 6378 | do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, child, PIDTYPE_TGID); |
| 6379 | |
Christian Brauner | ef2c41c | 2020-02-05 14:26:22 +0100 | [diff] [blame] | 6380 | cgroup_css_set_put_fork(kargs); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6381 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 6382 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6383 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6384 | * cgroup_exit - detach cgroup from exiting task |
| 6385 | * @tsk: pointer to task_struct of exiting process |
| 6386 | * |
Michal Koutný | e7c7b1d | 2019-10-04 12:57:39 +0200 | [diff] [blame] | 6387 | * Description: Detach cgroup from @tsk. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6388 | * |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6389 | */ |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 6390 | void cgroup_exit(struct task_struct *tsk) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6391 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6392 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 6393 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 6394 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6395 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6396 | spin_lock_irq(&css_set_lock); |
| 6397 | |
| 6398 | WARN_ON_ONCE(list_empty(&tsk->cg_list)); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 6399 | cset = task_css_set(tsk); |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6400 | css_set_move_task(tsk, cset, NULL, false); |
| 6401 | list_add_tail(&tsk->cg_list, &cset->dying_tasks); |
| 6402 | cset->nr_tasks--; |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 6403 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6404 | WARN_ON_ONCE(cgroup_task_frozen(tsk)); |
Roman Gushchin | f4f809f | 2021-05-10 14:39:46 -0700 | [diff] [blame] | 6405 | if (unlikely(!(tsk->flags & PF_KTHREAD) && |
| 6406 | test_bit(CGRP_FREEZE, &task_dfl_cgroup(tsk)->flags))) |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6407 | cgroup_update_frozen(task_dfl_cgroup(tsk)); |
Roman Gushchin | 76f969e | 2019-04-19 10:03:04 -0700 | [diff] [blame] | 6408 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6409 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 6410 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 6411 | /* see cgroup_post_fork() for details */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6412 | do_each_subsys_mask(ss, i, have_exit_callback) { |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6413 | ss->exit(tsk); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6414 | } while_each_subsys_mask(); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6415 | } |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6416 | |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 6417 | void cgroup_release(struct task_struct *task) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6418 | { |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6419 | struct cgroup_subsys *ss; |
| 6420 | int ssid; |
| 6421 | |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 6422 | do_each_subsys_mask(ss, ssid, have_release_callback) { |
| 6423 | ss->release(task); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 6424 | } while_each_subsys_mask(); |
Tejun Heo | c03cd77 | 2019-05-31 10:38:58 -0700 | [diff] [blame] | 6425 | |
Tejun Heo | 5153faa | 2019-10-24 12:03:51 -0700 | [diff] [blame] | 6426 | spin_lock_irq(&css_set_lock); |
| 6427 | css_set_skip_task_iters(task_css_set(task), task); |
| 6428 | list_del_init(&task->cg_list); |
| 6429 | spin_unlock_irq(&css_set_lock); |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 6430 | } |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 6431 | |
Oleg Nesterov | 51bee5a | 2019-01-28 17:00:13 +0100 | [diff] [blame] | 6432 | void cgroup_free(struct task_struct *task) |
| 6433 | { |
| 6434 | struct css_set *cset = task_css_set(task); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 6435 | put_css_set(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 6436 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 6437 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6438 | static int __init cgroup_disable(char *str) |
| 6439 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6440 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6441 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 6442 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6443 | |
| 6444 | while ((token = strsep(&str, ",")) != NULL) { |
| 6445 | if (!*token) |
| 6446 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6447 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 6448 | for_each_subsys(ss, i) { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 6449 | if (strcmp(token, ss->name) && |
| 6450 | strcmp(token, ss->legacy_name)) |
| 6451 | continue; |
Shakeel Butt | 45e1ba4 | 2021-05-12 13:19:46 -0700 | [diff] [blame] | 6452 | |
| 6453 | static_branch_disable(cgroup_subsys_enabled_key[i]); |
| 6454 | pr_info("Disabling %s control group subsystem\n", |
| 6455 | ss->name); |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6456 | } |
Suren Baghdasaryan | 3958e2d | 2021-05-24 12:53:39 -0700 | [diff] [blame] | 6457 | |
| 6458 | for (i = 0; i < OPT_FEATURE_COUNT; i++) { |
| 6459 | if (strcmp(token, cgroup_opt_feature_names[i])) |
| 6460 | continue; |
| 6461 | cgroup_feature_disable_mask |= 1 << i; |
| 6462 | pr_info("Disabling %s control group feature\n", |
| 6463 | cgroup_opt_feature_names[i]); |
| 6464 | break; |
| 6465 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 6466 | } |
| 6467 | return 1; |
| 6468 | } |
| 6469 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 6470 | |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 6471 | void __init __weak enable_debug_cgroup(void) { } |
| 6472 | |
| 6473 | static int __init enable_cgroup_debug(char *str) |
| 6474 | { |
| 6475 | cgroup_debug = true; |
| 6476 | enable_debug_cgroup(); |
| 6477 | return 1; |
| 6478 | } |
| 6479 | __setup("cgroup_debug", enable_cgroup_debug); |
| 6480 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 6481 | /** |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 6482 | * css_tryget_online_from_dir - get corresponding css from a cgroup dentry |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 6483 | * @dentry: directory dentry of interest |
| 6484 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 6485 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 6486 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 6487 | * to get the corresponding css and return it. If such css doesn't exist |
| 6488 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6489 | */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 6490 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, |
| 6491 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6492 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6493 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 6494 | struct file_system_type *s_type = dentry->d_sb->s_type; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6495 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6496 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6497 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 6498 | /* is @dentry a cgroup dir? */ |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 6499 | if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) || |
| 6500 | !kn || kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6501 | return ERR_PTR(-EBADF); |
| 6502 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 6503 | rcu_read_lock(); |
| 6504 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6505 | /* |
| 6506 | * This path doesn't originate from kernfs and @kn could already |
| 6507 | * have been or be removed at any point. @kn->priv is RCU |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 6508 | * protected for this access. See css_release_work_fn() for details. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6509 | */ |
Tejun Heo | e0aed7c | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 6510 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 6511 | if (cgrp) |
| 6512 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 6513 | |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 6514 | if (!css || !css_tryget_online(css)) |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 6515 | css = ERR_PTR(-ENOENT); |
| 6516 | |
| 6517 | rcu_read_unlock(); |
| 6518 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6519 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6520 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 6521 | /** |
| 6522 | * css_from_id - lookup css by id |
| 6523 | * @id: the cgroup id |
| 6524 | * @ss: cgroup subsys to be looked into |
| 6525 | * |
| 6526 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 6527 | * Should be called under rcu_read_lock(). |
| 6528 | */ |
| 6529 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 6530 | { |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 6531 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Johannes Weiner | d6ccc55 | 2016-06-17 12:24:27 -0400 | [diff] [blame] | 6532 | return idr_find(&ss->css_idr, id); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 6533 | } |
| 6534 | |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 6535 | /** |
| 6536 | * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path |
| 6537 | * @path: path on the default hierarchy |
| 6538 | * |
| 6539 | * Find the cgroup at @path on the default hierarchy, increment its |
| 6540 | * reference count and return it. Returns pointer to the found cgroup on |
Shakeel Butt | be28816 | 2021-10-24 23:19:14 -0700 | [diff] [blame] | 6541 | * success, ERR_PTR(-ENOENT) if @path doesn't exist or if the cgroup has already |
| 6542 | * been released and ERR_PTR(-ENOTDIR) if @path points to a non-directory. |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 6543 | */ |
| 6544 | struct cgroup *cgroup_get_from_path(const char *path) |
| 6545 | { |
| 6546 | struct kernfs_node *kn; |
Shakeel Butt | be28816 | 2021-10-24 23:19:14 -0700 | [diff] [blame] | 6547 | struct cgroup *cgrp = ERR_PTR(-ENOENT); |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 6548 | |
| 6549 | kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path); |
Shakeel Butt | be28816 | 2021-10-24 23:19:14 -0700 | [diff] [blame] | 6550 | if (!kn) |
| 6551 | goto out; |
| 6552 | |
| 6553 | if (kernfs_type(kn) != KERNFS_DIR) { |
| 6554 | cgrp = ERR_PTR(-ENOTDIR); |
| 6555 | goto out_kernfs; |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 6556 | } |
| 6557 | |
Shakeel Butt | be28816 | 2021-10-24 23:19:14 -0700 | [diff] [blame] | 6558 | rcu_read_lock(); |
| 6559 | |
| 6560 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); |
| 6561 | if (!cgrp || !cgroup_tryget(cgrp)) |
| 6562 | cgrp = ERR_PTR(-ENOENT); |
| 6563 | |
| 6564 | rcu_read_unlock(); |
| 6565 | |
| 6566 | out_kernfs: |
| 6567 | kernfs_put(kn); |
| 6568 | out: |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 6569 | return cgrp; |
| 6570 | } |
| 6571 | EXPORT_SYMBOL_GPL(cgroup_get_from_path); |
| 6572 | |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 6573 | /** |
| 6574 | * cgroup_get_from_fd - get a cgroup pointer from a fd |
| 6575 | * @fd: fd obtained by open(cgroup2_dir) |
| 6576 | * |
| 6577 | * Find the cgroup from a fd which should be obtained |
| 6578 | * by opening a cgroup directory. Returns a pointer to the |
| 6579 | * cgroup on success. ERR_PTR is returned if the cgroup |
| 6580 | * cannot be found. |
| 6581 | */ |
| 6582 | struct cgroup *cgroup_get_from_fd(int fd) |
| 6583 | { |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 6584 | struct cgroup *cgrp; |
| 6585 | struct file *f; |
| 6586 | |
| 6587 | f = fget_raw(fd); |
| 6588 | if (!f) |
| 6589 | return ERR_PTR(-EBADF); |
| 6590 | |
Christian Brauner | 1770309 | 2020-02-05 14:26:19 +0100 | [diff] [blame] | 6591 | cgrp = cgroup_get_from_file(f); |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 6592 | fput(f); |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 6593 | return cgrp; |
| 6594 | } |
| 6595 | EXPORT_SYMBOL_GPL(cgroup_get_from_fd); |
| 6596 | |
Tejun Heo | 38cf3a6 | 2019-06-14 10:12:45 -0700 | [diff] [blame] | 6597 | static u64 power_of_ten(int power) |
| 6598 | { |
| 6599 | u64 v = 1; |
| 6600 | while (power--) |
| 6601 | v *= 10; |
| 6602 | return v; |
| 6603 | } |
| 6604 | |
| 6605 | /** |
| 6606 | * cgroup_parse_float - parse a floating number |
| 6607 | * @input: input string |
| 6608 | * @dec_shift: number of decimal digits to shift |
| 6609 | * @v: output |
| 6610 | * |
| 6611 | * Parse a decimal floating point number in @input and store the result in |
| 6612 | * @v with decimal point right shifted @dec_shift times. For example, if |
| 6613 | * @input is "12.3456" and @dec_shift is 3, *@v will be set to 12345. |
| 6614 | * Returns 0 on success, -errno otherwise. |
| 6615 | * |
| 6616 | * There's nothing cgroup specific about this function except that it's |
| 6617 | * currently the only user. |
| 6618 | */ |
| 6619 | int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v) |
| 6620 | { |
| 6621 | s64 whole, frac = 0; |
| 6622 | int fstart = 0, fend = 0, flen; |
| 6623 | |
| 6624 | if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend)) |
| 6625 | return -EINVAL; |
| 6626 | if (frac < 0) |
| 6627 | return -EINVAL; |
| 6628 | |
| 6629 | flen = fend > fstart ? fend - fstart : 0; |
| 6630 | if (flen < dec_shift) |
| 6631 | frac *= power_of_ten(dec_shift - flen); |
| 6632 | else |
| 6633 | frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift)); |
| 6634 | |
| 6635 | *v = whole * power_of_ten(dec_shift) + frac; |
| 6636 | return 0; |
| 6637 | } |
| 6638 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6639 | /* |
| 6640 | * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data |
| 6641 | * definition in cgroup-defs.h. |
| 6642 | */ |
| 6643 | #ifdef CONFIG_SOCK_CGROUP_DATA |
| 6644 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6645 | void cgroup_sk_alloc(struct sock_cgroup_data *skcd) |
| 6646 | { |
Daniel Borkmann | 78cc316 | 2021-09-27 14:39:20 +0200 | [diff] [blame] | 6647 | struct cgroup *cgroup; |
Shakeel Butt | e876ecc | 2020-03-09 22:16:05 -0700 | [diff] [blame] | 6648 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6649 | rcu_read_lock(); |
Daniel Borkmann | 78cc316 | 2021-09-27 14:39:20 +0200 | [diff] [blame] | 6650 | /* Don't associate the sock with unrelated interrupted task's cgroup. */ |
| 6651 | if (in_interrupt()) { |
| 6652 | cgroup = &cgrp_dfl_root.cgrp; |
| 6653 | cgroup_get(cgroup); |
| 6654 | goto out; |
| 6655 | } |
| 6656 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6657 | while (true) { |
| 6658 | struct css_set *cset; |
| 6659 | |
| 6660 | cset = task_css_set(current); |
| 6661 | if (likely(cgroup_tryget(cset->dfl_cgrp))) { |
Daniel Borkmann | 78cc316 | 2021-09-27 14:39:20 +0200 | [diff] [blame] | 6662 | cgroup = cset->dfl_cgrp; |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6663 | break; |
| 6664 | } |
| 6665 | cpu_relax(); |
| 6666 | } |
Daniel Borkmann | 78cc316 | 2021-09-27 14:39:20 +0200 | [diff] [blame] | 6667 | out: |
| 6668 | skcd->cgroup = cgroup; |
| 6669 | cgroup_bpf_get(cgroup); |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6670 | rcu_read_unlock(); |
| 6671 | } |
| 6672 | |
Cong Wang | ad0f75e | 2020-07-02 11:52:56 -0700 | [diff] [blame] | 6673 | void cgroup_sk_clone(struct sock_cgroup_data *skcd) |
| 6674 | { |
Daniel Borkmann | 8520e22 | 2021-09-14 01:07:57 +0200 | [diff] [blame] | 6675 | struct cgroup *cgrp = sock_cgroup_ptr(skcd); |
| 6676 | |
| 6677 | /* |
| 6678 | * We might be cloning a socket which is left in an empty |
| 6679 | * cgroup and the cgroup might have already been rmdir'd. |
| 6680 | * Don't use cgroup_get_live(). |
| 6681 | */ |
| 6682 | cgroup_get(cgrp); |
| 6683 | cgroup_bpf_get(cgrp); |
Cong Wang | ad0f75e | 2020-07-02 11:52:56 -0700 | [diff] [blame] | 6684 | } |
| 6685 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6686 | void cgroup_sk_free(struct sock_cgroup_data *skcd) |
| 6687 | { |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 6688 | struct cgroup *cgrp = sock_cgroup_ptr(skcd); |
| 6689 | |
| 6690 | cgroup_bpf_put(cgrp); |
| 6691 | cgroup_put(cgrp); |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 6692 | } |
| 6693 | |
| 6694 | #endif /* CONFIG_SOCK_CGROUP_DATA */ |
| 6695 | |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6696 | #ifdef CONFIG_SYSFS |
| 6697 | static ssize_t show_delegatable_files(struct cftype *files, char *buf, |
| 6698 | ssize_t size, const char *prefix) |
| 6699 | { |
| 6700 | struct cftype *cft; |
| 6701 | ssize_t ret = 0; |
| 6702 | |
| 6703 | for (cft = files; cft && cft->name[0] != '\0'; cft++) { |
| 6704 | if (!(cft->flags & CFTYPE_NS_DELEGATABLE)) |
| 6705 | continue; |
| 6706 | |
Suren Baghdasaryan | 3958e2d | 2021-05-24 12:53:39 -0700 | [diff] [blame] | 6707 | if ((cft->flags & CFTYPE_PRESSURE) && !cgroup_psi_enabled()) |
| 6708 | continue; |
| 6709 | |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6710 | if (prefix) |
| 6711 | ret += snprintf(buf + ret, size - ret, "%s.", prefix); |
| 6712 | |
| 6713 | ret += snprintf(buf + ret, size - ret, "%s\n", cft->name); |
| 6714 | |
Yangtao Li | 4d9ebbe | 2018-11-03 22:27:41 -0400 | [diff] [blame] | 6715 | if (WARN_ON(ret >= size)) |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6716 | break; |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6717 | } |
| 6718 | |
| 6719 | return ret; |
| 6720 | } |
| 6721 | |
| 6722 | static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 6723 | char *buf) |
| 6724 | { |
| 6725 | struct cgroup_subsys *ss; |
| 6726 | int ssid; |
| 6727 | ssize_t ret = 0; |
| 6728 | |
| 6729 | ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret, |
| 6730 | NULL); |
| 6731 | |
| 6732 | for_each_subsys(ss, ssid) |
| 6733 | ret += show_delegatable_files(ss->dfl_cftypes, buf + ret, |
| 6734 | PAGE_SIZE - ret, |
| 6735 | cgroup_subsys_name[ssid]); |
| 6736 | |
| 6737 | return ret; |
| 6738 | } |
| 6739 | static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate); |
| 6740 | |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 6741 | static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 6742 | char *buf) |
| 6743 | { |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 6744 | return snprintf(buf, PAGE_SIZE, |
| 6745 | "nsdelegate\n" |
| 6746 | "memory_localevents\n" |
| 6747 | "memory_recursiveprot\n"); |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 6748 | } |
| 6749 | static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features); |
| 6750 | |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6751 | static struct attribute *cgroup_sysfs_attrs[] = { |
| 6752 | &cgroup_delegate_attr.attr, |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 6753 | &cgroup_features_attr.attr, |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6754 | NULL, |
| 6755 | }; |
| 6756 | |
| 6757 | static const struct attribute_group cgroup_sysfs_attr_group = { |
| 6758 | .attrs = cgroup_sysfs_attrs, |
| 6759 | .name = "cgroup", |
| 6760 | }; |
| 6761 | |
| 6762 | static int __init cgroup_sysfs_init(void) |
| 6763 | { |
| 6764 | return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group); |
| 6765 | } |
| 6766 | subsys_initcall(cgroup_sysfs_init); |
Tejun Heo | a5e112e | 2019-05-13 12:37:17 -0700 | [diff] [blame] | 6767 | |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6768 | #endif /* CONFIG_SYSFS */ |