Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Peter Zijlstra | 391e43d | 2011-11-15 17:14:39 +0100 | [diff] [blame] | 3 | * kernel/sched/core.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 5 | * Core kernel scheduler code and related syscalls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 1991-2002 Linus Torvalds |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
Phil Auld | 9d24605 | 2020-06-29 15:23:03 -0400 | [diff] [blame] | 9 | #define CREATE_TRACE_POINTS |
| 10 | #include <trace/events/sched.h> |
| 11 | #undef CREATE_TRACE_POINTS |
| 12 | |
Ingo Molnar | 325ea10 | 2018-03-03 12:20:47 +0100 | [diff] [blame] | 13 | #include "sched.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Peter Zijlstra | 7281c8d | 2018-04-20 14:29:51 +0200 | [diff] [blame] | 15 | #include <linux/nospec.h> |
Peter Zijlstra | 85f1abe | 2018-05-01 18:14:45 +0200 | [diff] [blame] | 16 | |
Mark Rutland | 0ed557a | 2018-06-14 15:27:41 -0700 | [diff] [blame] | 17 | #include <linux/kcov.h> |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 18 | #include <linux/scs.h> |
Mark Rutland | 0ed557a | 2018-06-14 15:27:41 -0700 | [diff] [blame] | 19 | |
David Howells | 96f951e | 2012-03-28 18:30:03 +0100 | [diff] [blame] | 20 | #include <asm/switch_to.h> |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 21 | #include <asm/tlb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Tejun Heo | ea13844 | 2013-01-18 14:05:55 -0800 | [diff] [blame] | 23 | #include "../workqueue_internal.h" |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 24 | #include "../../fs/io-wq.h" |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 25 | #include "../smpboot.h" |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 26 | |
Vincent Guittot | 91c2749 | 2018-06-28 17:45:09 +0200 | [diff] [blame] | 27 | #include "pelt.h" |
Ingo Molnar | 1f8db41 | 2020-05-28 11:01:34 +0200 | [diff] [blame] | 28 | #include "smp.h" |
Vincent Guittot | 91c2749 | 2018-06-28 17:45:09 +0200 | [diff] [blame] | 29 | |
Qais Yousef | a056a5b | 2019-06-04 12:14:59 +0100 | [diff] [blame] | 30 | /* |
| 31 | * Export tracepoints that act as a bare tracehook (ie: have no trace event |
| 32 | * associated with them) to allow external modules to probe them. |
| 33 | */ |
| 34 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_cfs_tp); |
| 35 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_rt_tp); |
| 36 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_dl_tp); |
| 37 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_irq_tp); |
| 38 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_se_tp); |
Vincent Donnefort | 51cf18c | 2020-08-28 10:00:49 +0100 | [diff] [blame] | 39 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_cpu_capacity_tp); |
Qais Yousef | a056a5b | 2019-06-04 12:14:59 +0100 | [diff] [blame] | 40 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_overutilized_tp); |
Vincent Donnefort | 4581bea | 2020-05-27 17:39:14 +0100 | [diff] [blame] | 41 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_cfs_tp); |
| 42 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_se_tp); |
Phil Auld | 9d24605 | 2020-06-29 15:23:03 -0400 | [diff] [blame] | 43 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_update_nr_running_tp); |
Qais Yousef | a056a5b | 2019-06-04 12:14:59 +0100 | [diff] [blame] | 44 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 45 | DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
Peter Zijlstra | dc61b1d | 2010-06-08 11:40:42 +0200 | [diff] [blame] | 46 | |
Juri Lelli | a73f863 | 2020-10-13 07:31:14 +0200 | [diff] [blame] | 47 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | e436d80 | 2007-07-19 21:28:35 +0200 | [diff] [blame] | 48 | /* |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 49 | * Debugging: various feature bits |
Patrick Bellasi | 765cc3a | 2017-11-08 18:41:01 +0000 | [diff] [blame] | 50 | * |
| 51 | * If SCHED_DEBUG is disabled, each compilation unit has its own copy of |
| 52 | * sysctl_sched_features, defined in sched.h, to allow constants propagation |
| 53 | * at compile time and compiler optimization based on features default. |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 54 | */ |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 55 | #define SCHED_FEAT(name, enabled) \ |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 56 | (1UL << __SCHED_FEAT_##name) * enabled | |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 57 | const_debug unsigned int sysctl_sched_features = |
Peter Zijlstra | 391e43d | 2011-11-15 17:14:39 +0100 | [diff] [blame] | 58 | #include "features.h" |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 59 | 0; |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 60 | #undef SCHED_FEAT |
Paul Turner | c006fac | 2021-04-16 14:29:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Print a warning if need_resched is set for the given duration (if |
| 64 | * LATENCY_WARN is enabled). |
| 65 | * |
| 66 | * If sysctl_resched_latency_warn_once is set, only one warning will be shown |
| 67 | * per boot. |
| 68 | */ |
| 69 | __read_mostly int sysctl_resched_latency_warn_ms = 100; |
| 70 | __read_mostly int sysctl_resched_latency_warn_once = 1; |
| 71 | #endif /* CONFIG_SCHED_DEBUG */ |
Peter Zijlstra | f00b45c | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 72 | |
Ingo Molnar | bf5c91b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 73 | /* |
Peter Zijlstra | b82d9fd | 2007-11-09 22:39:39 +0100 | [diff] [blame] | 74 | * Number of tasks to iterate in a single balance run. |
| 75 | * Limited because this is done with IRQs disabled. |
| 76 | */ |
| 77 | const_debug unsigned int sysctl_sched_nr_migrate = 32; |
| 78 | |
| 79 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 80 | * period over which we measure -rt task CPU usage in us. |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 81 | * default: 1s |
| 82 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 83 | unsigned int sysctl_sched_rt_period = 1000000; |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 84 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 85 | __read_mostly int scheduler_running; |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 86 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 87 | #ifdef CONFIG_SCHED_CORE |
| 88 | |
| 89 | DEFINE_STATIC_KEY_FALSE(__sched_core_enabled); |
| 90 | |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 91 | /* kernel prio, less is more */ |
| 92 | static inline int __task_prio(struct task_struct *p) |
| 93 | { |
| 94 | if (p->sched_class == &stop_sched_class) /* trumps deadline */ |
| 95 | return -2; |
| 96 | |
| 97 | if (rt_prio(p->prio)) /* includes deadline */ |
| 98 | return p->prio; /* [-1, 99] */ |
| 99 | |
| 100 | if (p->sched_class == &idle_sched_class) |
| 101 | return MAX_RT_PRIO + NICE_WIDTH; /* 140 */ |
| 102 | |
| 103 | return MAX_RT_PRIO + MAX_NICE; /* 120, squash fair */ |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * l(a,b) |
| 108 | * le(a,b) := !l(b,a) |
| 109 | * g(a,b) := l(b,a) |
| 110 | * ge(a,b) := !l(a,b) |
| 111 | */ |
| 112 | |
| 113 | /* real prio, less is less */ |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 114 | static inline bool prio_less(struct task_struct *a, struct task_struct *b, bool in_fi) |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 115 | { |
| 116 | |
| 117 | int pa = __task_prio(a), pb = __task_prio(b); |
| 118 | |
| 119 | if (-pa < -pb) |
| 120 | return true; |
| 121 | |
| 122 | if (-pb < -pa) |
| 123 | return false; |
| 124 | |
| 125 | if (pa == -1) /* dl_prio() doesn't work because of stop_class above */ |
| 126 | return !dl_time_before(a->dl.deadline, b->dl.deadline); |
| 127 | |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 128 | if (pa == MAX_RT_PRIO + MAX_NICE) /* fair */ |
| 129 | return cfs_prio_less(a, b, in_fi); |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | static inline bool __sched_core_less(struct task_struct *a, struct task_struct *b) |
| 135 | { |
| 136 | if (a->core_cookie < b->core_cookie) |
| 137 | return true; |
| 138 | |
| 139 | if (a->core_cookie > b->core_cookie) |
| 140 | return false; |
| 141 | |
| 142 | /* flip prio, so high prio is leftmost */ |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 143 | if (prio_less(b, a, task_rq(a)->core->core_forceidle)) |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 144 | return true; |
| 145 | |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | #define __node_2_sc(node) rb_entry((node), struct task_struct, core_node) |
| 150 | |
| 151 | static inline bool rb_sched_core_less(struct rb_node *a, const struct rb_node *b) |
| 152 | { |
| 153 | return __sched_core_less(__node_2_sc(a), __node_2_sc(b)); |
| 154 | } |
| 155 | |
| 156 | static inline int rb_sched_core_cmp(const void *key, const struct rb_node *node) |
| 157 | { |
| 158 | const struct task_struct *p = __node_2_sc(node); |
| 159 | unsigned long cookie = (unsigned long)key; |
| 160 | |
| 161 | if (cookie < p->core_cookie) |
| 162 | return -1; |
| 163 | |
| 164 | if (cookie > p->core_cookie) |
| 165 | return 1; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Peter Zijlstra | 6e33cad | 2021-03-26 18:55:06 +0100 | [diff] [blame] | 170 | void sched_core_enqueue(struct rq *rq, struct task_struct *p) |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 171 | { |
| 172 | rq->core->core_task_seq++; |
| 173 | |
| 174 | if (!p->core_cookie) |
| 175 | return; |
| 176 | |
| 177 | rb_add(&p->core_node, &rq->core_tree, rb_sched_core_less); |
| 178 | } |
| 179 | |
Peter Zijlstra | 6e33cad | 2021-03-26 18:55:06 +0100 | [diff] [blame] | 180 | void sched_core_dequeue(struct rq *rq, struct task_struct *p) |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 181 | { |
| 182 | rq->core->core_task_seq++; |
| 183 | |
Peter Zijlstra | 6e33cad | 2021-03-26 18:55:06 +0100 | [diff] [blame] | 184 | if (!sched_core_enqueued(p)) |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 185 | return; |
| 186 | |
| 187 | rb_erase(&p->core_node, &rq->core_tree); |
Peter Zijlstra | 6e33cad | 2021-03-26 18:55:06 +0100 | [diff] [blame] | 188 | RB_CLEAR_NODE(&p->core_node); |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Find left-most (aka, highest priority) task matching @cookie. |
| 193 | */ |
| 194 | static struct task_struct *sched_core_find(struct rq *rq, unsigned long cookie) |
| 195 | { |
| 196 | struct rb_node *node; |
| 197 | |
| 198 | node = rb_find_first((void *)cookie, &rq->core_tree, rb_sched_core_cmp); |
| 199 | /* |
| 200 | * The idle task always matches any cookie! |
| 201 | */ |
| 202 | if (!node) |
| 203 | return idle_sched_class.pick_task(rq); |
| 204 | |
| 205 | return __node_2_sc(node); |
| 206 | } |
| 207 | |
Peter Zijlstra | d2dfa17 | 2020-11-17 18:19:43 -0500 | [diff] [blame] | 208 | static struct task_struct *sched_core_next(struct task_struct *p, unsigned long cookie) |
| 209 | { |
| 210 | struct rb_node *node = &p->core_node; |
| 211 | |
| 212 | node = rb_next(node); |
| 213 | if (!node) |
| 214 | return NULL; |
| 215 | |
| 216 | p = container_of(node, struct task_struct, core_node); |
| 217 | if (p->core_cookie != cookie) |
| 218 | return NULL; |
| 219 | |
| 220 | return p; |
| 221 | } |
| 222 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 223 | /* |
| 224 | * Magic required such that: |
| 225 | * |
| 226 | * raw_spin_rq_lock(rq); |
| 227 | * ... |
| 228 | * raw_spin_rq_unlock(rq); |
| 229 | * |
| 230 | * ends up locking and unlocking the _same_ lock, and all CPUs |
| 231 | * always agree on what rq has what lock. |
| 232 | * |
| 233 | * XXX entirely possible to selectively enable cores, don't bother for now. |
| 234 | */ |
| 235 | |
| 236 | static DEFINE_MUTEX(sched_core_mutex); |
Peter Zijlstra | 875feb4 | 2021-03-29 10:08:58 +0200 | [diff] [blame] | 237 | static atomic_t sched_core_count; |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 238 | static struct cpumask sched_core_mask; |
| 239 | |
| 240 | static void __sched_core_flip(bool enabled) |
| 241 | { |
| 242 | int cpu, t, i; |
| 243 | |
| 244 | cpus_read_lock(); |
| 245 | |
| 246 | /* |
| 247 | * Toggle the online cores, one by one. |
| 248 | */ |
| 249 | cpumask_copy(&sched_core_mask, cpu_online_mask); |
| 250 | for_each_cpu(cpu, &sched_core_mask) { |
| 251 | const struct cpumask *smt_mask = cpu_smt_mask(cpu); |
| 252 | |
| 253 | i = 0; |
| 254 | local_irq_disable(); |
| 255 | for_each_cpu(t, smt_mask) { |
| 256 | /* supports up to SMT8 */ |
| 257 | raw_spin_lock_nested(&cpu_rq(t)->__lock, i++); |
| 258 | } |
| 259 | |
| 260 | for_each_cpu(t, smt_mask) |
| 261 | cpu_rq(t)->core_enabled = enabled; |
| 262 | |
| 263 | for_each_cpu(t, smt_mask) |
| 264 | raw_spin_unlock(&cpu_rq(t)->__lock); |
| 265 | local_irq_enable(); |
| 266 | |
| 267 | cpumask_andnot(&sched_core_mask, &sched_core_mask, smt_mask); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Toggle the offline CPUs. |
| 272 | */ |
| 273 | cpumask_copy(&sched_core_mask, cpu_possible_mask); |
| 274 | cpumask_andnot(&sched_core_mask, &sched_core_mask, cpu_online_mask); |
| 275 | |
| 276 | for_each_cpu(cpu, &sched_core_mask) |
| 277 | cpu_rq(cpu)->core_enabled = enabled; |
| 278 | |
| 279 | cpus_read_unlock(); |
| 280 | } |
| 281 | |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 282 | static void sched_core_assert_empty(void) |
| 283 | { |
| 284 | int cpu; |
| 285 | |
| 286 | for_each_possible_cpu(cpu) |
| 287 | WARN_ON_ONCE(!RB_EMPTY_ROOT(&cpu_rq(cpu)->core_tree)); |
| 288 | } |
| 289 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 290 | static void __sched_core_enable(void) |
| 291 | { |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 292 | static_branch_enable(&__sched_core_enabled); |
| 293 | /* |
| 294 | * Ensure all previous instances of raw_spin_rq_*lock() have finished |
| 295 | * and future ones will observe !sched_core_disabled(). |
| 296 | */ |
| 297 | synchronize_rcu(); |
| 298 | __sched_core_flip(true); |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 299 | sched_core_assert_empty(); |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static void __sched_core_disable(void) |
| 303 | { |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 304 | sched_core_assert_empty(); |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 305 | __sched_core_flip(false); |
| 306 | static_branch_disable(&__sched_core_enabled); |
| 307 | } |
| 308 | |
| 309 | void sched_core_get(void) |
| 310 | { |
Peter Zijlstra | 875feb4 | 2021-03-29 10:08:58 +0200 | [diff] [blame] | 311 | if (atomic_inc_not_zero(&sched_core_count)) |
| 312 | return; |
| 313 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 314 | mutex_lock(&sched_core_mutex); |
Peter Zijlstra | 875feb4 | 2021-03-29 10:08:58 +0200 | [diff] [blame] | 315 | if (!atomic_read(&sched_core_count)) |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 316 | __sched_core_enable(); |
Peter Zijlstra | 875feb4 | 2021-03-29 10:08:58 +0200 | [diff] [blame] | 317 | |
| 318 | smp_mb__before_atomic(); |
| 319 | atomic_inc(&sched_core_count); |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 320 | mutex_unlock(&sched_core_mutex); |
| 321 | } |
| 322 | |
Peter Zijlstra | 875feb4 | 2021-03-29 10:08:58 +0200 | [diff] [blame] | 323 | static void __sched_core_put(struct work_struct *work) |
| 324 | { |
| 325 | if (atomic_dec_and_mutex_lock(&sched_core_count, &sched_core_mutex)) { |
| 326 | __sched_core_disable(); |
| 327 | mutex_unlock(&sched_core_mutex); |
| 328 | } |
| 329 | } |
| 330 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 331 | void sched_core_put(void) |
| 332 | { |
Peter Zijlstra | 875feb4 | 2021-03-29 10:08:58 +0200 | [diff] [blame] | 333 | static DECLARE_WORK(_work, __sched_core_put); |
| 334 | |
| 335 | /* |
| 336 | * "There can be only one" |
| 337 | * |
| 338 | * Either this is the last one, or we don't actually need to do any |
| 339 | * 'work'. If it is the last *again*, we rely on |
| 340 | * WORK_STRUCT_PENDING_BIT. |
| 341 | */ |
| 342 | if (!atomic_add_unless(&sched_core_count, -1, 1)) |
| 343 | schedule_work(&_work); |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 344 | } |
| 345 | |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 346 | #else /* !CONFIG_SCHED_CORE */ |
| 347 | |
| 348 | static inline void sched_core_enqueue(struct rq *rq, struct task_struct *p) { } |
| 349 | static inline void sched_core_dequeue(struct rq *rq, struct task_struct *p) { } |
| 350 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 351 | #endif /* CONFIG_SCHED_CORE */ |
| 352 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 353 | /* |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 354 | * part of the period that we allow rt tasks to run in us. |
| 355 | * default: 0.95s |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 356 | */ |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 357 | int sysctl_sched_rt_runtime = 950000; |
| 358 | |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * Serialization rules: |
| 362 | * |
| 363 | * Lock order: |
| 364 | * |
| 365 | * p->pi_lock |
| 366 | * rq->lock |
| 367 | * hrtimer_cpu_base->lock (hrtimer_start() for bandwidth controls) |
| 368 | * |
| 369 | * rq1->lock |
| 370 | * rq2->lock where: rq1 < rq2 |
| 371 | * |
| 372 | * Regular state: |
| 373 | * |
| 374 | * Normal scheduling state is serialized by rq->lock. __schedule() takes the |
| 375 | * local CPU's rq->lock, it optionally removes the task from the runqueue and |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 376 | * always looks at the local rq data structures to find the most eligible task |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 377 | * to run next. |
| 378 | * |
| 379 | * Task enqueue is also under rq->lock, possibly taken from another CPU. |
| 380 | * Wakeups from another LLC domain might use an IPI to transfer the enqueue to |
| 381 | * the local CPU to avoid bouncing the runqueue state around [ see |
| 382 | * ttwu_queue_wakelist() ] |
| 383 | * |
| 384 | * Task wakeup, specifically wakeups that involve migration, are horribly |
| 385 | * complicated to avoid having to take two rq->locks. |
| 386 | * |
| 387 | * Special state: |
| 388 | * |
| 389 | * System-calls and anything external will use task_rq_lock() which acquires |
| 390 | * both p->pi_lock and rq->lock. As a consequence the state they change is |
| 391 | * stable while holding either lock: |
| 392 | * |
| 393 | * - sched_setaffinity()/ |
| 394 | * set_cpus_allowed_ptr(): p->cpus_ptr, p->nr_cpus_allowed |
| 395 | * - set_user_nice(): p->se.load, p->*prio |
| 396 | * - __sched_setscheduler(): p->sched_class, p->policy, p->*prio, |
| 397 | * p->se.load, p->rt_priority, |
| 398 | * p->dl.dl_{runtime, deadline, period, flags, bw, density} |
| 399 | * - sched_setnuma(): p->numa_preferred_nid |
| 400 | * - sched_move_task()/ |
| 401 | * cpu_cgroup_fork(): p->sched_task_group |
| 402 | * - uclamp_update_active() p->uclamp* |
| 403 | * |
| 404 | * p->state <- TASK_*: |
| 405 | * |
| 406 | * is changed locklessly using set_current_state(), __set_current_state() or |
| 407 | * set_special_state(), see their respective comments, or by |
| 408 | * try_to_wake_up(). This latter uses p->pi_lock to serialize against |
| 409 | * concurrent self. |
| 410 | * |
| 411 | * p->on_rq <- { 0, 1 = TASK_ON_RQ_QUEUED, 2 = TASK_ON_RQ_MIGRATING }: |
| 412 | * |
| 413 | * is set by activate_task() and cleared by deactivate_task(), under |
| 414 | * rq->lock. Non-zero indicates the task is runnable, the special |
| 415 | * ON_RQ_MIGRATING state is used for migration without holding both |
| 416 | * rq->locks. It indicates task_cpu() is not stable, see task_rq_lock(). |
| 417 | * |
| 418 | * p->on_cpu <- { 0, 1 }: |
| 419 | * |
| 420 | * is set by prepare_task() and cleared by finish_task() such that it will be |
| 421 | * set before p is scheduled-in and cleared after p is scheduled-out, both |
| 422 | * under rq->lock. Non-zero indicates the task is running on its CPU. |
| 423 | * |
| 424 | * [ The astute reader will observe that it is possible for two tasks on one |
| 425 | * CPU to have ->on_cpu = 1 at the same time. ] |
| 426 | * |
| 427 | * task_cpu(p): is changed by set_task_cpu(), the rules are: |
| 428 | * |
| 429 | * - Don't call set_task_cpu() on a blocked task: |
| 430 | * |
| 431 | * We don't care what CPU we're not running on, this simplifies hotplug, |
| 432 | * the CPU assignment of blocked tasks isn't required to be valid. |
| 433 | * |
| 434 | * - for try_to_wake_up(), called under p->pi_lock: |
| 435 | * |
| 436 | * This allows try_to_wake_up() to only take one rq->lock, see its comment. |
| 437 | * |
| 438 | * - for migration called under rq->lock: |
| 439 | * [ see task_on_rq_migrating() in task_rq_lock() ] |
| 440 | * |
| 441 | * o move_queued_task() |
| 442 | * o detach_task() |
| 443 | * |
| 444 | * - for migration called under double_rq_lock(): |
| 445 | * |
| 446 | * o __migrate_swap_task() |
| 447 | * o push_rt_task() / pull_rt_task() |
| 448 | * o push_dl_task() / pull_dl_task() |
| 449 | * o dl_task_offline_migration() |
| 450 | * |
| 451 | */ |
| 452 | |
Peter Zijlstra | 39d371b | 2021-03-02 12:13:13 +0100 | [diff] [blame] | 453 | void raw_spin_rq_lock_nested(struct rq *rq, int subclass) |
| 454 | { |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 455 | raw_spinlock_t *lock; |
| 456 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 457 | /* Matches synchronize_rcu() in __sched_core_enable() */ |
| 458 | preempt_disable(); |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 459 | if (sched_core_disabled()) { |
| 460 | raw_spin_lock_nested(&rq->__lock, subclass); |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 461 | /* preempt_count *MUST* be > 1 */ |
| 462 | preempt_enable_no_resched(); |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
| 466 | for (;;) { |
Peter Zijlstra | 9ef7e7e | 2021-03-03 16:45:41 +0100 | [diff] [blame] | 467 | lock = __rq_lockp(rq); |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 468 | raw_spin_lock_nested(lock, subclass); |
Peter Zijlstra | 9ef7e7e | 2021-03-03 16:45:41 +0100 | [diff] [blame] | 469 | if (likely(lock == __rq_lockp(rq))) { |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 470 | /* preempt_count *MUST* be > 1 */ |
| 471 | preempt_enable_no_resched(); |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 472 | return; |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 473 | } |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 474 | raw_spin_unlock(lock); |
| 475 | } |
Peter Zijlstra | 39d371b | 2021-03-02 12:13:13 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | bool raw_spin_rq_trylock(struct rq *rq) |
| 479 | { |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 480 | raw_spinlock_t *lock; |
| 481 | bool ret; |
| 482 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 483 | /* Matches synchronize_rcu() in __sched_core_enable() */ |
| 484 | preempt_disable(); |
| 485 | if (sched_core_disabled()) { |
| 486 | ret = raw_spin_trylock(&rq->__lock); |
| 487 | preempt_enable(); |
| 488 | return ret; |
| 489 | } |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 490 | |
| 491 | for (;;) { |
Peter Zijlstra | 9ef7e7e | 2021-03-03 16:45:41 +0100 | [diff] [blame] | 492 | lock = __rq_lockp(rq); |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 493 | ret = raw_spin_trylock(lock); |
Peter Zijlstra | 9ef7e7e | 2021-03-03 16:45:41 +0100 | [diff] [blame] | 494 | if (!ret || (likely(lock == __rq_lockp(rq)))) { |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 495 | preempt_enable(); |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 496 | return ret; |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 497 | } |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 498 | raw_spin_unlock(lock); |
| 499 | } |
Peter Zijlstra | 39d371b | 2021-03-02 12:13:13 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | void raw_spin_rq_unlock(struct rq *rq) |
| 503 | { |
| 504 | raw_spin_unlock(rq_lockp(rq)); |
| 505 | } |
| 506 | |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 507 | #ifdef CONFIG_SMP |
| 508 | /* |
| 509 | * double_rq_lock - safely lock two runqueues |
| 510 | */ |
| 511 | void double_rq_lock(struct rq *rq1, struct rq *rq2) |
| 512 | { |
| 513 | lockdep_assert_irqs_disabled(); |
| 514 | |
| 515 | if (rq_order_less(rq2, rq1)) |
| 516 | swap(rq1, rq2); |
| 517 | |
| 518 | raw_spin_rq_lock(rq1); |
Peter Zijlstra | 9ef7e7e | 2021-03-03 16:45:41 +0100 | [diff] [blame] | 519 | if (__rq_lockp(rq1) == __rq_lockp(rq2)) |
Peter Zijlstra | d66f1b0 | 2021-03-02 12:16:48 +0100 | [diff] [blame] | 520 | return; |
| 521 | |
| 522 | raw_spin_rq_lock_nested(rq2, SINGLE_DEPTH_NESTING); |
| 523 | } |
| 524 | #endif |
| 525 | |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 526 | /* |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 527 | * __task_rq_lock - lock the rq @p resides on. |
| 528 | */ |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 529 | struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 530 | __acquires(rq->lock) |
| 531 | { |
| 532 | struct rq *rq; |
| 533 | |
| 534 | lockdep_assert_held(&p->pi_lock); |
| 535 | |
| 536 | for (;;) { |
| 537 | rq = task_rq(p); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 538 | raw_spin_rq_lock(rq); |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 539 | if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 540 | rq_pin_lock(rq, rf); |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 541 | return rq; |
| 542 | } |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 543 | raw_spin_rq_unlock(rq); |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 544 | |
| 545 | while (unlikely(task_on_rq_migrating(p))) |
| 546 | cpu_relax(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * task_rq_lock - lock p->pi_lock and lock the rq @p resides on. |
| 552 | */ |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 553 | struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 554 | __acquires(p->pi_lock) |
| 555 | __acquires(rq->lock) |
| 556 | { |
| 557 | struct rq *rq; |
| 558 | |
| 559 | for (;;) { |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 560 | raw_spin_lock_irqsave(&p->pi_lock, rf->flags); |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 561 | rq = task_rq(p); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 562 | raw_spin_rq_lock(rq); |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 563 | /* |
| 564 | * move_queued_task() task_rq_lock() |
| 565 | * |
| 566 | * ACQUIRE (rq->lock) |
| 567 | * [S] ->on_rq = MIGRATING [L] rq = task_rq() |
| 568 | * WMB (__set_task_cpu()) ACQUIRE (rq->lock); |
| 569 | * [S] ->cpu = new_cpu [L] task_rq() |
| 570 | * [L] ->on_rq |
| 571 | * RELEASE (rq->lock) |
| 572 | * |
Andrea Parri | c546951 | 2019-01-21 16:52:40 +0100 | [diff] [blame] | 573 | * If we observe the old CPU in task_rq_lock(), the acquire of |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 574 | * the old rq->lock will fully serialize against the stores. |
| 575 | * |
Andrea Parri | c546951 | 2019-01-21 16:52:40 +0100 | [diff] [blame] | 576 | * If we observe the new CPU in task_rq_lock(), the address |
| 577 | * dependency headed by '[L] rq = task_rq()' and the acquire |
| 578 | * will pair with the WMB to ensure we then also see migrating. |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 579 | */ |
| 580 | if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 581 | rq_pin_lock(rq, rf); |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 582 | return rq; |
| 583 | } |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 584 | raw_spin_rq_unlock(rq); |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 585 | raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 586 | |
| 587 | while (unlikely(task_on_rq_migrating(p))) |
| 588 | cpu_relax(); |
| 589 | } |
| 590 | } |
| 591 | |
Ingo Molnar | 535b955 | 2017-02-01 12:29:21 +0100 | [diff] [blame] | 592 | /* |
| 593 | * RQ-clock updating methods: |
| 594 | */ |
| 595 | |
| 596 | static void update_rq_clock_task(struct rq *rq, s64 delta) |
| 597 | { |
| 598 | /* |
| 599 | * In theory, the compile should just see 0 here, and optimize out the call |
| 600 | * to sched_rt_avg_update. But I don't trust it... |
| 601 | */ |
Vincent Guittot | 11d4afd | 2018-09-25 11:17:42 +0200 | [diff] [blame] | 602 | s64 __maybe_unused steal = 0, irq_delta = 0; |
| 603 | |
Ingo Molnar | 535b955 | 2017-02-01 12:29:21 +0100 | [diff] [blame] | 604 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
| 605 | irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time; |
| 606 | |
| 607 | /* |
| 608 | * Since irq_time is only updated on {soft,}irq_exit, we might run into |
| 609 | * this case when a previous update_rq_clock() happened inside a |
| 610 | * {soft,}irq region. |
| 611 | * |
| 612 | * When this happens, we stop ->clock_task and only update the |
| 613 | * prev_irq_time stamp to account for the part that fit, so that a next |
| 614 | * update will consume the rest. This ensures ->clock_task is |
| 615 | * monotonic. |
| 616 | * |
| 617 | * It does however cause some slight miss-attribution of {soft,}irq |
| 618 | * time, a more accurate solution would be to update the irq_time using |
| 619 | * the current rq->clock timestamp, except that would require using |
| 620 | * atomic ops. |
| 621 | */ |
| 622 | if (irq_delta > delta) |
| 623 | irq_delta = delta; |
| 624 | |
| 625 | rq->prev_irq_time += irq_delta; |
| 626 | delta -= irq_delta; |
| 627 | #endif |
| 628 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING |
| 629 | if (static_key_false((¶virt_steal_rq_enabled))) { |
| 630 | steal = paravirt_steal_clock(cpu_of(rq)); |
| 631 | steal -= rq->prev_steal_time_rq; |
| 632 | |
| 633 | if (unlikely(steal > delta)) |
| 634 | steal = delta; |
| 635 | |
| 636 | rq->prev_steal_time_rq += steal; |
| 637 | delta -= steal; |
| 638 | } |
| 639 | #endif |
| 640 | |
| 641 | rq->clock_task += delta; |
| 642 | |
Vincent Guittot | 11d4afd | 2018-09-25 11:17:42 +0200 | [diff] [blame] | 643 | #ifdef CONFIG_HAVE_SCHED_AVG_IRQ |
Ingo Molnar | 535b955 | 2017-02-01 12:29:21 +0100 | [diff] [blame] | 644 | if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY)) |
Vincent Guittot | 91c2749 | 2018-06-28 17:45:09 +0200 | [diff] [blame] | 645 | update_irq_load_avg(rq, irq_delta + steal); |
Ingo Molnar | 535b955 | 2017-02-01 12:29:21 +0100 | [diff] [blame] | 646 | #endif |
Vincent Guittot | 2312729 | 2019-01-23 16:26:53 +0100 | [diff] [blame] | 647 | update_rq_clock_pelt(rq, delta); |
Ingo Molnar | 535b955 | 2017-02-01 12:29:21 +0100 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void update_rq_clock(struct rq *rq) |
| 651 | { |
| 652 | s64 delta; |
| 653 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 654 | lockdep_assert_rq_held(rq); |
Ingo Molnar | 535b955 | 2017-02-01 12:29:21 +0100 | [diff] [blame] | 655 | |
| 656 | if (rq->clock_update_flags & RQCF_ACT_SKIP) |
| 657 | return; |
| 658 | |
| 659 | #ifdef CONFIG_SCHED_DEBUG |
Peter Zijlstra | 26ae58d | 2016-10-03 16:53:49 +0200 | [diff] [blame] | 660 | if (sched_feat(WARN_DOUBLE_CLOCK)) |
| 661 | SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED); |
Ingo Molnar | 535b955 | 2017-02-01 12:29:21 +0100 | [diff] [blame] | 662 | rq->clock_update_flags |= RQCF_UPDATED; |
| 663 | #endif |
Peter Zijlstra | 26ae58d | 2016-10-03 16:53:49 +0200 | [diff] [blame] | 664 | |
Ingo Molnar | 535b955 | 2017-02-01 12:29:21 +0100 | [diff] [blame] | 665 | delta = sched_clock_cpu(cpu_of(rq)) - rq->clock; |
| 666 | if (delta < 0) |
| 667 | return; |
| 668 | rq->clock += delta; |
| 669 | update_rq_clock_task(rq, delta); |
| 670 | } |
| 671 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 672 | #ifdef CONFIG_SCHED_HRTICK |
| 673 | /* |
| 674 | * Use HR-timers to deliver accurate preemption points. |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 675 | */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 676 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 677 | static void hrtick_clear(struct rq *rq) |
| 678 | { |
| 679 | if (hrtimer_active(&rq->hrtick_timer)) |
| 680 | hrtimer_cancel(&rq->hrtick_timer); |
| 681 | } |
| 682 | |
| 683 | /* |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 684 | * High-resolution timer tick. |
| 685 | * Runs from hardirq context with interrupts disabled. |
| 686 | */ |
| 687 | static enum hrtimer_restart hrtick(struct hrtimer *timer) |
| 688 | { |
| 689 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 690 | struct rq_flags rf; |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 691 | |
| 692 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| 693 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 694 | rq_lock(rq, &rf); |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 695 | update_rq_clock(rq); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 696 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 697 | rq_unlock(rq, &rf); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 698 | |
| 699 | return HRTIMER_NORESTART; |
| 700 | } |
| 701 | |
Rabin Vincent | 95e904c | 2008-05-11 05:55:33 +0530 | [diff] [blame] | 702 | #ifdef CONFIG_SMP |
Peter Zijlstra | 971ee28 | 2013-06-28 11:18:53 +0200 | [diff] [blame] | 703 | |
Thomas Gleixner | 4961b6e | 2015-04-14 21:09:05 +0000 | [diff] [blame] | 704 | static void __hrtick_restart(struct rq *rq) |
Peter Zijlstra | 971ee28 | 2013-06-28 11:18:53 +0200 | [diff] [blame] | 705 | { |
| 706 | struct hrtimer *timer = &rq->hrtick_timer; |
Juri Lelli | 156ec6f | 2021-02-08 08:35:53 +0100 | [diff] [blame] | 707 | ktime_t time = rq->hrtick_time; |
Peter Zijlstra | 971ee28 | 2013-06-28 11:18:53 +0200 | [diff] [blame] | 708 | |
Juri Lelli | 156ec6f | 2021-02-08 08:35:53 +0100 | [diff] [blame] | 709 | hrtimer_start(timer, time, HRTIMER_MODE_ABS_PINNED_HARD); |
Peter Zijlstra | 971ee28 | 2013-06-28 11:18:53 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 712 | /* |
| 713 | * called from hardirq (IPI) context |
| 714 | */ |
| 715 | static void __hrtick_start(void *arg) |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 716 | { |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 717 | struct rq *rq = arg; |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 718 | struct rq_flags rf; |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 719 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 720 | rq_lock(rq, &rf); |
Peter Zijlstra | 971ee28 | 2013-06-28 11:18:53 +0200 | [diff] [blame] | 721 | __hrtick_restart(rq); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 722 | rq_unlock(rq, &rf); |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 723 | } |
| 724 | |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 725 | /* |
| 726 | * Called to set the hrtick timer state. |
| 727 | * |
| 728 | * called with rq->lock held and irqs disabled |
| 729 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 730 | void hrtick_start(struct rq *rq, u64 delay) |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 731 | { |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 732 | struct hrtimer *timer = &rq->hrtick_timer; |
xiaofeng.yan | 177ef2a | 2014-08-26 03:15:41 +0000 | [diff] [blame] | 733 | s64 delta; |
| 734 | |
| 735 | /* |
| 736 | * Don't schedule slices shorter than 10000ns, that just |
| 737 | * doesn't make sense and can cause timer DoS. |
| 738 | */ |
| 739 | delta = max_t(s64, delay, 10000LL); |
Juri Lelli | 156ec6f | 2021-02-08 08:35:53 +0100 | [diff] [blame] | 740 | rq->hrtick_time = ktime_add_ns(timer->base->get_time(), delta); |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 741 | |
Peter Xu | fd3eafd | 2019-12-16 16:31:25 -0500 | [diff] [blame] | 742 | if (rq == this_rq()) |
Peter Zijlstra | 971ee28 | 2013-06-28 11:18:53 +0200 | [diff] [blame] | 743 | __hrtick_restart(rq); |
Peter Xu | fd3eafd | 2019-12-16 16:31:25 -0500 | [diff] [blame] | 744 | else |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 745 | smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd); |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 748 | #else |
| 749 | /* |
| 750 | * Called to set the hrtick timer state. |
| 751 | * |
| 752 | * called with rq->lock held and irqs disabled |
| 753 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 754 | void hrtick_start(struct rq *rq, u64 delay) |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 755 | { |
Wanpeng Li | 8689333 | 2014-11-26 08:44:06 +0800 | [diff] [blame] | 756 | /* |
| 757 | * Don't schedule slices shorter than 10000ns, that just |
| 758 | * doesn't make sense. Rely on vruntime for fairness. |
| 759 | */ |
| 760 | delay = max_t(u64, delay, 10000LL); |
Thomas Gleixner | 4961b6e | 2015-04-14 21:09:05 +0000 | [diff] [blame] | 761 | hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), |
Sebastian Andrzej Siewior | d5096aa | 2019-07-26 20:30:52 +0200 | [diff] [blame] | 762 | HRTIMER_MODE_REL_PINNED_HARD); |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 763 | } |
Peter Zijlstra (Intel) | 90b5363 | 2020-03-27 11:44:56 +0100 | [diff] [blame] | 764 | |
Rabin Vincent | 95e904c | 2008-05-11 05:55:33 +0530 | [diff] [blame] | 765 | #endif /* CONFIG_SMP */ |
Peter Zijlstra | b328ca1 | 2008-04-29 10:02:46 +0200 | [diff] [blame] | 766 | |
Frederic Weisbecker | 77a021b | 2018-02-21 05:17:23 +0100 | [diff] [blame] | 767 | static void hrtick_rq_init(struct rq *rq) |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 768 | { |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 769 | #ifdef CONFIG_SMP |
Peter Zijlstra | 545b8c8 | 2020-06-15 11:29:31 +0200 | [diff] [blame] | 770 | INIT_CSD(&rq->hrtick_csd, __hrtick_start, rq); |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 771 | #endif |
Sebastian Andrzej Siewior | d5096aa | 2019-07-26 20:30:52 +0200 | [diff] [blame] | 772 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 773 | rq->hrtick_timer.function = hrtick; |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 774 | } |
Andrew Morton | 006c75f | 2008-09-22 14:55:46 -0700 | [diff] [blame] | 775 | #else /* CONFIG_SCHED_HRTICK */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 776 | static inline void hrtick_clear(struct rq *rq) |
| 777 | { |
| 778 | } |
| 779 | |
Frederic Weisbecker | 77a021b | 2018-02-21 05:17:23 +0100 | [diff] [blame] | 780 | static inline void hrtick_rq_init(struct rq *rq) |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 781 | { |
| 782 | } |
Andrew Morton | 006c75f | 2008-09-22 14:55:46 -0700 | [diff] [blame] | 783 | #endif /* CONFIG_SCHED_HRTICK */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 784 | |
Frederic Weisbecker | 5529578 | 2016-03-24 15:38:01 +0100 | [diff] [blame] | 785 | /* |
| 786 | * cmpxchg based fetch_or, macro so it works for different integer types |
| 787 | */ |
| 788 | #define fetch_or(ptr, mask) \ |
| 789 | ({ \ |
| 790 | typeof(ptr) _ptr = (ptr); \ |
| 791 | typeof(mask) _mask = (mask); \ |
| 792 | typeof(*_ptr) _old, _val = *_ptr; \ |
| 793 | \ |
| 794 | for (;;) { \ |
| 795 | _old = cmpxchg(_ptr, _val, _val | _mask); \ |
| 796 | if (_old == _val) \ |
| 797 | break; \ |
| 798 | _val = _old; \ |
| 799 | } \ |
| 800 | _old; \ |
| 801 | }) |
| 802 | |
Peter Zijlstra | e3baac4 | 2014-06-04 10:31:18 -0700 | [diff] [blame] | 803 | #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG) |
Peter Zijlstra | fd99f91 | 2014-04-09 15:35:08 +0200 | [diff] [blame] | 804 | /* |
| 805 | * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG, |
| 806 | * this avoids any races wrt polling state changes and thereby avoids |
| 807 | * spurious IPIs. |
| 808 | */ |
| 809 | static bool set_nr_and_not_polling(struct task_struct *p) |
| 810 | { |
| 811 | struct thread_info *ti = task_thread_info(p); |
| 812 | return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG); |
| 813 | } |
Peter Zijlstra | e3baac4 | 2014-06-04 10:31:18 -0700 | [diff] [blame] | 814 | |
| 815 | /* |
| 816 | * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set. |
| 817 | * |
| 818 | * If this returns true, then the idle task promises to call |
| 819 | * sched_ttwu_pending() and reschedule soon. |
| 820 | */ |
| 821 | static bool set_nr_if_polling(struct task_struct *p) |
| 822 | { |
| 823 | struct thread_info *ti = task_thread_info(p); |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 824 | typeof(ti->flags) old, val = READ_ONCE(ti->flags); |
Peter Zijlstra | e3baac4 | 2014-06-04 10:31:18 -0700 | [diff] [blame] | 825 | |
| 826 | for (;;) { |
| 827 | if (!(val & _TIF_POLLING_NRFLAG)) |
| 828 | return false; |
| 829 | if (val & _TIF_NEED_RESCHED) |
| 830 | return true; |
| 831 | old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED); |
| 832 | if (old == val) |
| 833 | break; |
| 834 | val = old; |
| 835 | } |
| 836 | return true; |
| 837 | } |
| 838 | |
Peter Zijlstra | fd99f91 | 2014-04-09 15:35:08 +0200 | [diff] [blame] | 839 | #else |
| 840 | static bool set_nr_and_not_polling(struct task_struct *p) |
| 841 | { |
| 842 | set_tsk_need_resched(p); |
| 843 | return true; |
| 844 | } |
Peter Zijlstra | e3baac4 | 2014-06-04 10:31:18 -0700 | [diff] [blame] | 845 | |
| 846 | #ifdef CONFIG_SMP |
| 847 | static bool set_nr_if_polling(struct task_struct *p) |
| 848 | { |
| 849 | return false; |
| 850 | } |
| 851 | #endif |
Peter Zijlstra | fd99f91 | 2014-04-09 15:35:08 +0200 | [diff] [blame] | 852 | #endif |
| 853 | |
Davidlohr Bueso | 07879c6 | 2018-12-18 11:53:52 -0800 | [diff] [blame] | 854 | static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task) |
| 855 | { |
| 856 | struct wake_q_node *node = &task->wake_q; |
| 857 | |
| 858 | /* |
| 859 | * Atomically grab the task, if ->wake_q is !nil already it means |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 860 | * it's already queued (either by us or someone else) and will get the |
Davidlohr Bueso | 07879c6 | 2018-12-18 11:53:52 -0800 | [diff] [blame] | 861 | * wakeup due to that. |
| 862 | * |
| 863 | * In order to ensure that a pending wakeup will observe our pending |
| 864 | * state, even in the failed case, an explicit smp_mb() must be used. |
| 865 | */ |
| 866 | smp_mb__before_atomic(); |
| 867 | if (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL))) |
| 868 | return false; |
| 869 | |
| 870 | /* |
| 871 | * The head is context local, there can be no concurrency. |
| 872 | */ |
| 873 | *head->lastp = node; |
| 874 | head->lastp = &node->next; |
| 875 | return true; |
| 876 | } |
| 877 | |
Peter Zijlstra | e6018c0 | 2018-12-17 10:14:53 +0100 | [diff] [blame] | 878 | /** |
| 879 | * wake_q_add() - queue a wakeup for 'later' waking. |
| 880 | * @head: the wake_q_head to add @task to |
| 881 | * @task: the task to queue for 'later' wakeup |
| 882 | * |
| 883 | * Queue a task for later wakeup, most likely by the wake_up_q() call in the |
| 884 | * same context, _HOWEVER_ this is not guaranteed, the wakeup can come |
| 885 | * instantly. |
| 886 | * |
| 887 | * This function must be used as-if it were wake_up_process(); IOW the task |
| 888 | * must be ready to be woken at this location. |
| 889 | */ |
Peter Zijlstra | 7675104 | 2015-05-01 08:27:50 -0700 | [diff] [blame] | 890 | void wake_q_add(struct wake_q_head *head, struct task_struct *task) |
| 891 | { |
Davidlohr Bueso | 07879c6 | 2018-12-18 11:53:52 -0800 | [diff] [blame] | 892 | if (__wake_q_add(head, task)) |
| 893 | get_task_struct(task); |
| 894 | } |
Peter Zijlstra | 7675104 | 2015-05-01 08:27:50 -0700 | [diff] [blame] | 895 | |
Davidlohr Bueso | 07879c6 | 2018-12-18 11:53:52 -0800 | [diff] [blame] | 896 | /** |
| 897 | * wake_q_add_safe() - safely queue a wakeup for 'later' waking. |
| 898 | * @head: the wake_q_head to add @task to |
| 899 | * @task: the task to queue for 'later' wakeup |
| 900 | * |
| 901 | * Queue a task for later wakeup, most likely by the wake_up_q() call in the |
| 902 | * same context, _HOWEVER_ this is not guaranteed, the wakeup can come |
| 903 | * instantly. |
| 904 | * |
| 905 | * This function must be used as-if it were wake_up_process(); IOW the task |
| 906 | * must be ready to be woken at this location. |
| 907 | * |
| 908 | * This function is essentially a task-safe equivalent to wake_q_add(). Callers |
| 909 | * that already hold reference to @task can call the 'safe' version and trust |
| 910 | * wake_q to do the right thing depending whether or not the @task is already |
| 911 | * queued for wakeup. |
| 912 | */ |
| 913 | void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task) |
| 914 | { |
| 915 | if (!__wake_q_add(head, task)) |
| 916 | put_task_struct(task); |
Peter Zijlstra | 7675104 | 2015-05-01 08:27:50 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | void wake_up_q(struct wake_q_head *head) |
| 920 | { |
| 921 | struct wake_q_node *node = head->first; |
| 922 | |
| 923 | while (node != WAKE_Q_TAIL) { |
| 924 | struct task_struct *task; |
| 925 | |
| 926 | task = container_of(node, struct task_struct, wake_q); |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 927 | /* Task can safely be re-inserted now: */ |
Peter Zijlstra | 7675104 | 2015-05-01 08:27:50 -0700 | [diff] [blame] | 928 | node = node->next; |
| 929 | task->wake_q.next = NULL; |
| 930 | |
| 931 | /* |
Andrea Parri | 7696f99 | 2018-07-16 11:06:03 -0700 | [diff] [blame] | 932 | * wake_up_process() executes a full barrier, which pairs with |
| 933 | * the queueing in wake_q_add() so as not to miss wakeups. |
Peter Zijlstra | 7675104 | 2015-05-01 08:27:50 -0700 | [diff] [blame] | 934 | */ |
| 935 | wake_up_process(task); |
| 936 | put_task_struct(task); |
| 937 | } |
| 938 | } |
| 939 | |
Peter Zijlstra | fd99f91 | 2014-04-09 15:35:08 +0200 | [diff] [blame] | 940 | /* |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 941 | * resched_curr - mark rq's current task 'to be rescheduled now'. |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 942 | * |
| 943 | * On UP this means the setting of the need_resched flag, on SMP it |
| 944 | * might also involve a cross-CPU call to trigger the scheduler on |
| 945 | * the target CPU. |
| 946 | */ |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 947 | void resched_curr(struct rq *rq) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 948 | { |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 949 | struct task_struct *curr = rq->curr; |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 950 | int cpu; |
| 951 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 952 | lockdep_assert_rq_held(rq); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 953 | |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 954 | if (test_tsk_need_resched(curr)) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 955 | return; |
| 956 | |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 957 | cpu = cpu_of(rq); |
Peter Zijlstra | fd99f91 | 2014-04-09 15:35:08 +0200 | [diff] [blame] | 958 | |
Peter Zijlstra | f27dde8 | 2013-08-14 14:55:31 +0200 | [diff] [blame] | 959 | if (cpu == smp_processor_id()) { |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 960 | set_tsk_need_resched(curr); |
Peter Zijlstra | f27dde8 | 2013-08-14 14:55:31 +0200 | [diff] [blame] | 961 | set_preempt_need_resched(); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 962 | return; |
Peter Zijlstra | f27dde8 | 2013-08-14 14:55:31 +0200 | [diff] [blame] | 963 | } |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 964 | |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 965 | if (set_nr_and_not_polling(curr)) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 966 | smp_send_reschedule(cpu); |
Andy Lutomirski | dfc68f2 | 2014-06-04 10:31:15 -0700 | [diff] [blame] | 967 | else |
| 968 | trace_sched_wake_idle_without_ipi(cpu); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 971 | void resched_cpu(int cpu) |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 972 | { |
| 973 | struct rq *rq = cpu_rq(cpu); |
| 974 | unsigned long flags; |
| 975 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 976 | raw_spin_rq_lock_irqsave(rq, flags); |
Paul E. McKenney | a0982df | 2017-10-13 16:24:28 -0700 | [diff] [blame] | 977 | if (cpu_online(cpu) || cpu == smp_processor_id()) |
| 978 | resched_curr(rq); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 979 | raw_spin_rq_unlock_irqrestore(rq, flags); |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 980 | } |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 981 | |
Peter Zijlstra | b021fe3 | 2013-09-17 09:30:55 +0200 | [diff] [blame] | 982 | #ifdef CONFIG_SMP |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 983 | #ifdef CONFIG_NO_HZ_COMMON |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 984 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 985 | * In the semi idle case, use the nearest busy CPU for migrating timers |
| 986 | * from an idle CPU. This is good for power-savings. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 987 | * |
| 988 | * We don't do similar optimization for completely idle system, as |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 989 | * selecting an idle CPU will add more delays to the timers than intended |
| 990 | * (as that CPU's timer base may not be uptodate wrt jiffies etc). |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 991 | */ |
Thomas Gleixner | bc7a34b | 2015-05-26 22:50:33 +0000 | [diff] [blame] | 992 | int get_nohz_timer_target(void) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 993 | { |
Wanpeng Li | e938b9c | 2020-01-13 08:50:27 +0800 | [diff] [blame] | 994 | int i, cpu = smp_processor_id(), default_cpu = -1; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 995 | struct sched_domain *sd; |
| 996 | |
Wanpeng Li | e938b9c | 2020-01-13 08:50:27 +0800 | [diff] [blame] | 997 | if (housekeeping_cpu(cpu, HK_FLAG_TIMER)) { |
| 998 | if (!idle_cpu(cpu)) |
| 999 | return cpu; |
| 1000 | default_cpu = cpu; |
| 1001 | } |
Viresh Kumar | 6201b4d | 2014-03-18 16:26:07 +0530 | [diff] [blame] | 1002 | |
Peter Zijlstra | 057f3fa | 2011-04-18 11:24:34 +0200 | [diff] [blame] | 1003 | rcu_read_lock(); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 1004 | for_each_domain(cpu, sd) { |
Wanpeng Li | e938b9c | 2020-01-13 08:50:27 +0800 | [diff] [blame] | 1005 | for_each_cpu_and(i, sched_domain_span(sd), |
| 1006 | housekeeping_cpumask(HK_FLAG_TIMER)) { |
Wanpeng Li | 4449692 | 2016-05-04 14:45:34 +0800 | [diff] [blame] | 1007 | if (cpu == i) |
| 1008 | continue; |
| 1009 | |
Wanpeng Li | e938b9c | 2020-01-13 08:50:27 +0800 | [diff] [blame] | 1010 | if (!idle_cpu(i)) { |
Peter Zijlstra | 057f3fa | 2011-04-18 11:24:34 +0200 | [diff] [blame] | 1011 | cpu = i; |
| 1012 | goto unlock; |
| 1013 | } |
| 1014 | } |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 1015 | } |
Vatika Harlalka | 9642d18 | 2015-09-01 16:50:59 +0200 | [diff] [blame] | 1016 | |
Wanpeng Li | e938b9c | 2020-01-13 08:50:27 +0800 | [diff] [blame] | 1017 | if (default_cpu == -1) |
| 1018 | default_cpu = housekeeping_any_cpu(HK_FLAG_TIMER); |
| 1019 | cpu = default_cpu; |
Peter Zijlstra | 057f3fa | 2011-04-18 11:24:34 +0200 | [diff] [blame] | 1020 | unlock: |
| 1021 | rcu_read_unlock(); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 1022 | return cpu; |
| 1023 | } |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 1024 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 1025 | /* |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 1026 | * When add_timer_on() enqueues a timer into the timer wheel of an |
| 1027 | * idle CPU then this timer might expire before the next timer event |
| 1028 | * which is scheduled to wake up that CPU. In case of a completely |
| 1029 | * idle system the next event might even be infinite time into the |
| 1030 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and |
| 1031 | * leaves the inner idle loop so the newly added timer is taken into |
| 1032 | * account when the CPU goes back to idle and evaluates the timer |
| 1033 | * wheel for the next timer event. |
| 1034 | */ |
Frederic Weisbecker | 1c20091 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 1035 | static void wake_up_idle_cpu(int cpu) |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 1036 | { |
| 1037 | struct rq *rq = cpu_rq(cpu); |
| 1038 | |
| 1039 | if (cpu == smp_processor_id()) |
| 1040 | return; |
| 1041 | |
Andy Lutomirski | 67b9ca7 | 2014-06-04 10:31:17 -0700 | [diff] [blame] | 1042 | if (set_nr_and_not_polling(rq->idle)) |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 1043 | smp_send_reschedule(cpu); |
Andy Lutomirski | dfc68f2 | 2014-06-04 10:31:15 -0700 | [diff] [blame] | 1044 | else |
| 1045 | trace_sched_wake_idle_without_ipi(cpu); |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 1046 | } |
Mike Galbraith | 39c0cbe | 2010-03-11 17:17:13 +0100 | [diff] [blame] | 1047 | |
Frederic Weisbecker | c5bfece | 2013-04-12 16:45:34 +0200 | [diff] [blame] | 1048 | static bool wake_up_full_nohz_cpu(int cpu) |
Frederic Weisbecker | 1c20091 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 1049 | { |
Frederic Weisbecker | 53c5fa1 | 2014-06-04 16:20:21 +0200 | [diff] [blame] | 1050 | /* |
| 1051 | * We just need the target to call irq_exit() and re-evaluate |
| 1052 | * the next tick. The nohz full kick at least implies that. |
| 1053 | * If needed we can still optimize that later with an |
| 1054 | * empty IRQ. |
| 1055 | */ |
Paul E. McKenney | 379d9ec | 2016-06-30 10:37:20 -0700 | [diff] [blame] | 1056 | if (cpu_is_offline(cpu)) |
| 1057 | return true; /* Don't try to wake offline CPUs. */ |
Frederic Weisbecker | c5bfece | 2013-04-12 16:45:34 +0200 | [diff] [blame] | 1058 | if (tick_nohz_full_cpu(cpu)) { |
Frederic Weisbecker | 1c20091 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 1059 | if (cpu != smp_processor_id() || |
| 1060 | tick_nohz_tick_stopped()) |
Frederic Weisbecker | 53c5fa1 | 2014-06-04 16:20:21 +0200 | [diff] [blame] | 1061 | tick_nohz_full_kick_cpu(cpu); |
Frederic Weisbecker | 1c20091 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 1062 | return true; |
| 1063 | } |
| 1064 | |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
Paul E. McKenney | 379d9ec | 2016-06-30 10:37:20 -0700 | [diff] [blame] | 1068 | /* |
| 1069 | * Wake up the specified CPU. If the CPU is going offline, it is the |
| 1070 | * caller's responsibility to deal with the lost wakeup, for example, |
| 1071 | * by hooking into the CPU_DEAD notifier like timers and hrtimers do. |
| 1072 | */ |
Frederic Weisbecker | 1c20091 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 1073 | void wake_up_nohz_cpu(int cpu) |
| 1074 | { |
Frederic Weisbecker | c5bfece | 2013-04-12 16:45:34 +0200 | [diff] [blame] | 1075 | if (!wake_up_full_nohz_cpu(cpu)) |
Frederic Weisbecker | 1c20091 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 1076 | wake_up_idle_cpu(cpu); |
| 1077 | } |
| 1078 | |
Peter Zijlstra (Intel) | 90b5363 | 2020-03-27 11:44:56 +0100 | [diff] [blame] | 1079 | static void nohz_csd_func(void *info) |
Suresh Siddha | ca38062 | 2011-10-03 15:09:00 -0700 | [diff] [blame] | 1080 | { |
Peter Zijlstra (Intel) | 90b5363 | 2020-03-27 11:44:56 +0100 | [diff] [blame] | 1081 | struct rq *rq = info; |
Peter Zijlstra | 19a1f5e | 2020-05-26 18:10:58 +0200 | [diff] [blame] | 1082 | int cpu = cpu_of(rq); |
| 1083 | unsigned int flags; |
Vincent Guittot | 873b4c6 | 2013-06-05 10:13:11 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* |
Peter Zijlstra | 19a1f5e | 2020-05-26 18:10:58 +0200 | [diff] [blame] | 1086 | * Release the rq::nohz_csd. |
Vincent Guittot | 873b4c6 | 2013-06-05 10:13:11 +0200 | [diff] [blame] | 1087 | */ |
Vincent Guittot | c6f8865 | 2021-02-24 14:30:06 +0100 | [diff] [blame] | 1088 | flags = atomic_fetch_andnot(NOHZ_KICK_MASK | NOHZ_NEWILB_KICK, nohz_flags(cpu)); |
Peter Zijlstra | 19a1f5e | 2020-05-26 18:10:58 +0200 | [diff] [blame] | 1089 | WARN_ON(!(flags & NOHZ_KICK_MASK)); |
Suresh Siddha | ca38062 | 2011-10-03 15:09:00 -0700 | [diff] [blame] | 1090 | |
Peter Zijlstra | 19a1f5e | 2020-05-26 18:10:58 +0200 | [diff] [blame] | 1091 | rq->idle_balance = idle_cpu(cpu); |
| 1092 | if (rq->idle_balance && !need_resched()) { |
| 1093 | rq->nohz_idle_balance = flags; |
Peter Zijlstra (Intel) | 90b5363 | 2020-03-27 11:44:56 +0100 | [diff] [blame] | 1094 | raise_softirq_irqoff(SCHED_SOFTIRQ); |
| 1095 | } |
Suresh Siddha | ca38062 | 2011-10-03 15:09:00 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 1098 | #endif /* CONFIG_NO_HZ_COMMON */ |
Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 1099 | |
Frederic Weisbecker | ce831b3 | 2013-04-20 15:15:35 +0200 | [diff] [blame] | 1100 | #ifdef CONFIG_NO_HZ_FULL |
Frederic Weisbecker | 76d92ac | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1101 | bool sched_can_stop_tick(struct rq *rq) |
Frederic Weisbecker | ce831b3 | 2013-04-20 15:15:35 +0200 | [diff] [blame] | 1102 | { |
Frederic Weisbecker | 76d92ac | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1103 | int fifo_nr_running; |
| 1104 | |
| 1105 | /* Deadline tasks, even if single, need the tick */ |
| 1106 | if (rq->dl.dl_nr_running) |
| 1107 | return false; |
| 1108 | |
Frederic Weisbecker | 3882ec6 | 2014-03-18 22:54:04 +0100 | [diff] [blame] | 1109 | /* |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 1110 | * If there are more than one RR tasks, we need the tick to affect the |
Peter Zijlstra | 2548d54 | 2016-04-21 18:03:15 +0200 | [diff] [blame] | 1111 | * actual RR behaviour. |
Rik van Riel | 1e78cdb | 2015-02-16 15:23:49 -0500 | [diff] [blame] | 1112 | */ |
Frederic Weisbecker | 76d92ac | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1113 | if (rq->rt.rr_nr_running) { |
| 1114 | if (rq->rt.rr_nr_running == 1) |
| 1115 | return true; |
| 1116 | else |
| 1117 | return false; |
Rik van Riel | 1e78cdb | 2015-02-16 15:23:49 -0500 | [diff] [blame] | 1118 | } |
| 1119 | |
Peter Zijlstra | 2548d54 | 2016-04-21 18:03:15 +0200 | [diff] [blame] | 1120 | /* |
| 1121 | * If there's no RR tasks, but FIFO tasks, we can skip the tick, no |
| 1122 | * forced preemption between FIFO tasks. |
| 1123 | */ |
| 1124 | fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running; |
| 1125 | if (fifo_nr_running) |
| 1126 | return true; |
| 1127 | |
| 1128 | /* |
| 1129 | * If there are no DL,RR/FIFO tasks, there must only be CFS tasks left; |
| 1130 | * if there's more than one we need the tick for involuntary |
| 1131 | * preemption. |
| 1132 | */ |
| 1133 | if (rq->nr_running > 1) |
Viresh Kumar | 541b826 | 2014-06-24 14:04:12 +0530 | [diff] [blame] | 1134 | return false; |
Frederic Weisbecker | ce831b3 | 2013-04-20 15:15:35 +0200 | [diff] [blame] | 1135 | |
Viresh Kumar | 541b826 | 2014-06-24 14:04:12 +0530 | [diff] [blame] | 1136 | return true; |
Frederic Weisbecker | ce831b3 | 2013-04-20 15:15:35 +0200 | [diff] [blame] | 1137 | } |
| 1138 | #endif /* CONFIG_NO_HZ_FULL */ |
Dhaval Giani | 6d6bc0a | 2008-05-30 14:23:45 +0200 | [diff] [blame] | 1139 | #endif /* CONFIG_SMP */ |
Ingo Molnar | c24d20d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1140 | |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 1141 | #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \ |
| 1142 | (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH))) |
Peter Zijlstra | eb75580 | 2008-08-19 12:33:05 +0200 | [diff] [blame] | 1143 | /* |
Paul Turner | 8277434 | 2011-07-21 09:43:35 -0700 | [diff] [blame] | 1144 | * Iterate task_group tree rooted at *from, calling @down when first entering a |
| 1145 | * node and @up when leaving it for the final time. |
| 1146 | * |
| 1147 | * Caller must hold rcu_lock or sufficient equivalent. |
Peter Zijlstra | eb75580 | 2008-08-19 12:33:05 +0200 | [diff] [blame] | 1148 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1149 | int walk_tg_tree_from(struct task_group *from, |
Paul Turner | 8277434 | 2011-07-21 09:43:35 -0700 | [diff] [blame] | 1150 | tg_visitor down, tg_visitor up, void *data) |
Peter Zijlstra | eb75580 | 2008-08-19 12:33:05 +0200 | [diff] [blame] | 1151 | { |
| 1152 | struct task_group *parent, *child; |
| 1153 | int ret; |
| 1154 | |
Paul Turner | 8277434 | 2011-07-21 09:43:35 -0700 | [diff] [blame] | 1155 | parent = from; |
| 1156 | |
Peter Zijlstra | eb75580 | 2008-08-19 12:33:05 +0200 | [diff] [blame] | 1157 | down: |
| 1158 | ret = (*down)(parent, data); |
| 1159 | if (ret) |
Paul Turner | 8277434 | 2011-07-21 09:43:35 -0700 | [diff] [blame] | 1160 | goto out; |
Peter Zijlstra | eb75580 | 2008-08-19 12:33:05 +0200 | [diff] [blame] | 1161 | list_for_each_entry_rcu(child, &parent->children, siblings) { |
| 1162 | parent = child; |
| 1163 | goto down; |
| 1164 | |
| 1165 | up: |
| 1166 | continue; |
| 1167 | } |
| 1168 | ret = (*up)(parent, data); |
Paul Turner | 8277434 | 2011-07-21 09:43:35 -0700 | [diff] [blame] | 1169 | if (ret || parent == from) |
| 1170 | goto out; |
Peter Zijlstra | eb75580 | 2008-08-19 12:33:05 +0200 | [diff] [blame] | 1171 | |
| 1172 | child = parent; |
| 1173 | parent = parent->parent; |
| 1174 | if (parent) |
| 1175 | goto up; |
Paul Turner | 8277434 | 2011-07-21 09:43:35 -0700 | [diff] [blame] | 1176 | out: |
Peter Zijlstra | eb75580 | 2008-08-19 12:33:05 +0200 | [diff] [blame] | 1177 | return ret; |
| 1178 | } |
| 1179 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1180 | int tg_nop(struct task_group *tg, void *data) |
Peter Zijlstra | eb75580 | 2008-08-19 12:33:05 +0200 | [diff] [blame] | 1181 | { |
| 1182 | return 0; |
| 1183 | } |
| 1184 | #endif |
| 1185 | |
Vincent Guittot | 9059393 | 2017-05-17 11:50:45 +0200 | [diff] [blame] | 1186 | static void set_load_weight(struct task_struct *p, bool update_load) |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1187 | { |
Nikhil Rao | f05998d | 2011-05-18 10:09:38 -0700 | [diff] [blame] | 1188 | int prio = p->static_prio - MAX_RT_PRIO; |
| 1189 | struct load_weight *load = &p->se.load; |
| 1190 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1191 | /* |
| 1192 | * SCHED_IDLE tasks get minimal weight: |
| 1193 | */ |
Viresh Kumar | 1da1843 | 2018-11-05 16:51:55 +0530 | [diff] [blame] | 1194 | if (task_has_idle_policy(p)) { |
Nikhil Rao | c8b2811 | 2011-05-18 14:37:48 -0700 | [diff] [blame] | 1195 | load->weight = scale_load(WEIGHT_IDLEPRIO); |
Nikhil Rao | f05998d | 2011-05-18 10:09:38 -0700 | [diff] [blame] | 1196 | load->inv_weight = WMULT_IDLEPRIO; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1197 | return; |
| 1198 | } |
| 1199 | |
Vincent Guittot | 9059393 | 2017-05-17 11:50:45 +0200 | [diff] [blame] | 1200 | /* |
| 1201 | * SCHED_OTHER tasks have to update their load when changing their |
| 1202 | * weight |
| 1203 | */ |
| 1204 | if (update_load && p->sched_class == &fair_sched_class) { |
| 1205 | reweight_task(p, prio); |
| 1206 | } else { |
| 1207 | load->weight = scale_load(sched_prio_to_weight[prio]); |
| 1208 | load->inv_weight = sched_prio_to_wmult[prio]; |
| 1209 | } |
Ingo Molnar | 45bf76d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1212 | #ifdef CONFIG_UCLAMP_TASK |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 1213 | /* |
| 1214 | * Serializes updates of utilization clamp values |
| 1215 | * |
| 1216 | * The (slow-path) user-space triggers utilization clamp value updates which |
| 1217 | * can require updates on (fast-path) scheduler's data structures used to |
| 1218 | * support enqueue/dequeue operations. |
| 1219 | * While the per-CPU rq lock protects fast-path update operations, user-space |
| 1220 | * requests are serialized using a mutex to reduce the risk of conflicting |
| 1221 | * updates or API abuses. |
| 1222 | */ |
| 1223 | static DEFINE_MUTEX(uclamp_mutex); |
| 1224 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1225 | /* Max allowed minimum utilization */ |
| 1226 | unsigned int sysctl_sched_uclamp_util_min = SCHED_CAPACITY_SCALE; |
| 1227 | |
| 1228 | /* Max allowed maximum utilization */ |
| 1229 | unsigned int sysctl_sched_uclamp_util_max = SCHED_CAPACITY_SCALE; |
| 1230 | |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1231 | /* |
| 1232 | * By default RT tasks run at the maximum performance point/capacity of the |
| 1233 | * system. Uclamp enforces this by always setting UCLAMP_MIN of RT tasks to |
| 1234 | * SCHED_CAPACITY_SCALE. |
| 1235 | * |
| 1236 | * This knob allows admins to change the default behavior when uclamp is being |
| 1237 | * used. In battery powered devices, particularly, running at the maximum |
| 1238 | * capacity and frequency will increase energy consumption and shorten the |
| 1239 | * battery life. |
| 1240 | * |
| 1241 | * This knob only affects RT tasks that their uclamp_se->user_defined == false. |
| 1242 | * |
| 1243 | * This knob will not override the system default sched_util_clamp_min defined |
| 1244 | * above. |
| 1245 | */ |
| 1246 | unsigned int sysctl_sched_uclamp_util_min_rt_default = SCHED_CAPACITY_SCALE; |
| 1247 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1248 | /* All clamps are required to be less or equal than these values */ |
| 1249 | static struct uclamp_se uclamp_default[UCLAMP_CNT]; |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1250 | |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1251 | /* |
| 1252 | * This static key is used to reduce the uclamp overhead in the fast path. It |
| 1253 | * primarily disables the call to uclamp_rq_{inc, dec}() in |
| 1254 | * enqueue/dequeue_task(). |
| 1255 | * |
| 1256 | * This allows users to continue to enable uclamp in their kernel config with |
| 1257 | * minimum uclamp overhead in the fast path. |
| 1258 | * |
| 1259 | * As soon as userspace modifies any of the uclamp knobs, the static key is |
| 1260 | * enabled, since we have an actual users that make use of uclamp |
| 1261 | * functionality. |
| 1262 | * |
| 1263 | * The knobs that would enable this static key are: |
| 1264 | * |
| 1265 | * * A task modifying its uclamp value with sched_setattr(). |
| 1266 | * * An admin modifying the sysctl_sched_uclamp_{min, max} via procfs. |
| 1267 | * * An admin modifying the cgroup cpu.uclamp.{min, max} |
| 1268 | */ |
| 1269 | DEFINE_STATIC_KEY_FALSE(sched_uclamp_used); |
| 1270 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1271 | /* Integer rounded range for each bucket */ |
| 1272 | #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS) |
| 1273 | |
| 1274 | #define for_each_clamp_id(clamp_id) \ |
| 1275 | for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++) |
| 1276 | |
| 1277 | static inline unsigned int uclamp_bucket_id(unsigned int clamp_value) |
| 1278 | { |
Quentin Perret | 6d2f890 | 2021-04-30 15:14:12 +0000 | [diff] [blame] | 1279 | return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1); |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1280 | } |
| 1281 | |
Valentin Schneider | 7763baa | 2019-11-15 10:39:08 +0000 | [diff] [blame] | 1282 | static inline unsigned int uclamp_none(enum uclamp_id clamp_id) |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1283 | { |
| 1284 | if (clamp_id == UCLAMP_MIN) |
| 1285 | return 0; |
| 1286 | return SCHED_CAPACITY_SCALE; |
| 1287 | } |
| 1288 | |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1289 | static inline void uclamp_se_set(struct uclamp_se *uc_se, |
| 1290 | unsigned int value, bool user_defined) |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1291 | { |
| 1292 | uc_se->value = value; |
| 1293 | uc_se->bucket_id = uclamp_bucket_id(value); |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1294 | uc_se->user_defined = user_defined; |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1295 | } |
| 1296 | |
Patrick Bellasi | e496187 | 2019-06-21 09:42:04 +0100 | [diff] [blame] | 1297 | static inline unsigned int |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1298 | uclamp_idle_value(struct rq *rq, enum uclamp_id clamp_id, |
Patrick Bellasi | e496187 | 2019-06-21 09:42:04 +0100 | [diff] [blame] | 1299 | unsigned int clamp_value) |
| 1300 | { |
| 1301 | /* |
| 1302 | * Avoid blocked utilization pushing up the frequency when we go |
| 1303 | * idle (which drops the max-clamp) by retaining the last known |
| 1304 | * max-clamp. |
| 1305 | */ |
| 1306 | if (clamp_id == UCLAMP_MAX) { |
| 1307 | rq->uclamp_flags |= UCLAMP_FLAG_IDLE; |
| 1308 | return clamp_value; |
| 1309 | } |
| 1310 | |
| 1311 | return uclamp_none(UCLAMP_MIN); |
| 1312 | } |
| 1313 | |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1314 | static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id, |
Patrick Bellasi | e496187 | 2019-06-21 09:42:04 +0100 | [diff] [blame] | 1315 | unsigned int clamp_value) |
| 1316 | { |
| 1317 | /* Reset max-clamp retention only on idle exit */ |
| 1318 | if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE)) |
| 1319 | return; |
| 1320 | |
| 1321 | WRITE_ONCE(rq->uclamp[clamp_id].value, clamp_value); |
| 1322 | } |
| 1323 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1324 | static inline |
Valentin Schneider | 7763baa | 2019-11-15 10:39:08 +0000 | [diff] [blame] | 1325 | unsigned int uclamp_rq_max_value(struct rq *rq, enum uclamp_id clamp_id, |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1326 | unsigned int clamp_value) |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1327 | { |
| 1328 | struct uclamp_bucket *bucket = rq->uclamp[clamp_id].bucket; |
| 1329 | int bucket_id = UCLAMP_BUCKETS - 1; |
| 1330 | |
| 1331 | /* |
| 1332 | * Since both min and max clamps are max aggregated, find the |
| 1333 | * top most bucket with tasks in. |
| 1334 | */ |
| 1335 | for ( ; bucket_id >= 0; bucket_id--) { |
| 1336 | if (!bucket[bucket_id].tasks) |
| 1337 | continue; |
| 1338 | return bucket[bucket_id].value; |
| 1339 | } |
| 1340 | |
| 1341 | /* No tasks -- default clamp values */ |
Patrick Bellasi | e496187 | 2019-06-21 09:42:04 +0100 | [diff] [blame] | 1342 | return uclamp_idle_value(rq, clamp_id, clamp_value); |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1343 | } |
| 1344 | |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1345 | static void __uclamp_update_util_min_rt_default(struct task_struct *p) |
| 1346 | { |
| 1347 | unsigned int default_util_min; |
| 1348 | struct uclamp_se *uc_se; |
| 1349 | |
| 1350 | lockdep_assert_held(&p->pi_lock); |
| 1351 | |
| 1352 | uc_se = &p->uclamp_req[UCLAMP_MIN]; |
| 1353 | |
| 1354 | /* Only sync if user didn't override the default */ |
| 1355 | if (uc_se->user_defined) |
| 1356 | return; |
| 1357 | |
| 1358 | default_util_min = sysctl_sched_uclamp_util_min_rt_default; |
| 1359 | uclamp_se_set(uc_se, default_util_min, false); |
| 1360 | } |
| 1361 | |
| 1362 | static void uclamp_update_util_min_rt_default(struct task_struct *p) |
| 1363 | { |
| 1364 | struct rq_flags rf; |
| 1365 | struct rq *rq; |
| 1366 | |
| 1367 | if (!rt_task(p)) |
| 1368 | return; |
| 1369 | |
| 1370 | /* Protect updates to p->uclamp_* */ |
| 1371 | rq = task_rq_lock(p, &rf); |
| 1372 | __uclamp_update_util_min_rt_default(p); |
| 1373 | task_rq_unlock(rq, p, &rf); |
| 1374 | } |
| 1375 | |
| 1376 | static void uclamp_sync_util_min_rt_default(void) |
| 1377 | { |
| 1378 | struct task_struct *g, *p; |
| 1379 | |
| 1380 | /* |
| 1381 | * copy_process() sysctl_uclamp |
| 1382 | * uclamp_min_rt = X; |
| 1383 | * write_lock(&tasklist_lock) read_lock(&tasklist_lock) |
| 1384 | * // link thread smp_mb__after_spinlock() |
| 1385 | * write_unlock(&tasklist_lock) read_unlock(&tasklist_lock); |
| 1386 | * sched_post_fork() for_each_process_thread() |
| 1387 | * __uclamp_sync_rt() __uclamp_sync_rt() |
| 1388 | * |
| 1389 | * Ensures that either sched_post_fork() will observe the new |
| 1390 | * uclamp_min_rt or for_each_process_thread() will observe the new |
| 1391 | * task. |
| 1392 | */ |
| 1393 | read_lock(&tasklist_lock); |
| 1394 | smp_mb__after_spinlock(); |
| 1395 | read_unlock(&tasklist_lock); |
| 1396 | |
| 1397 | rcu_read_lock(); |
| 1398 | for_each_process_thread(g, p) |
| 1399 | uclamp_update_util_min_rt_default(p); |
| 1400 | rcu_read_unlock(); |
| 1401 | } |
| 1402 | |
Patrick Bellasi | 3eac870 | 2019-08-22 14:28:09 +0100 | [diff] [blame] | 1403 | static inline struct uclamp_se |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1404 | uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id) |
Patrick Bellasi | 3eac870 | 2019-08-22 14:28:09 +0100 | [diff] [blame] | 1405 | { |
| 1406 | struct uclamp_se uc_req = p->uclamp_req[clamp_id]; |
| 1407 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
Patrick Bellasi | 3eac870 | 2019-08-22 14:28:09 +0100 | [diff] [blame] | 1408 | |
| 1409 | /* |
| 1410 | * Tasks in autogroups or root task group will be |
| 1411 | * restricted by system defaults. |
| 1412 | */ |
| 1413 | if (task_group_is_autogroup(task_group(p))) |
| 1414 | return uc_req; |
| 1415 | if (task_group(p) == &root_task_group) |
| 1416 | return uc_req; |
| 1417 | |
Qais Yousef | 0c18f2e | 2021-05-10 15:50:31 +0100 | [diff] [blame] | 1418 | switch (clamp_id) { |
| 1419 | case UCLAMP_MIN: { |
| 1420 | struct uclamp_se uc_min = task_group(p)->uclamp[clamp_id]; |
| 1421 | if (uc_req.value < uc_min.value) |
| 1422 | return uc_min; |
| 1423 | break; |
| 1424 | } |
| 1425 | case UCLAMP_MAX: { |
| 1426 | struct uclamp_se uc_max = task_group(p)->uclamp[clamp_id]; |
| 1427 | if (uc_req.value > uc_max.value) |
| 1428 | return uc_max; |
| 1429 | break; |
| 1430 | } |
| 1431 | default: |
| 1432 | WARN_ON_ONCE(1); |
| 1433 | break; |
| 1434 | } |
Patrick Bellasi | 3eac870 | 2019-08-22 14:28:09 +0100 | [diff] [blame] | 1435 | #endif |
| 1436 | |
| 1437 | return uc_req; |
| 1438 | } |
| 1439 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1440 | /* |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1441 | * The effective clamp bucket index of a task depends on, by increasing |
| 1442 | * priority: |
| 1443 | * - the task specific clamp value, when explicitly requested from userspace |
Patrick Bellasi | 3eac870 | 2019-08-22 14:28:09 +0100 | [diff] [blame] | 1444 | * - the task group effective clamp value, for tasks not either in the root |
| 1445 | * group or in an autogroup |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1446 | * - the system default clamp value, defined by the sysadmin |
| 1447 | */ |
| 1448 | static inline struct uclamp_se |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1449 | uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id) |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1450 | { |
Patrick Bellasi | 3eac870 | 2019-08-22 14:28:09 +0100 | [diff] [blame] | 1451 | struct uclamp_se uc_req = uclamp_tg_restrict(p, clamp_id); |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1452 | struct uclamp_se uc_max = uclamp_default[clamp_id]; |
| 1453 | |
| 1454 | /* System default restrictions always apply */ |
| 1455 | if (unlikely(uc_req.value > uc_max.value)) |
| 1456 | return uc_max; |
| 1457 | |
| 1458 | return uc_req; |
| 1459 | } |
| 1460 | |
Valentin Schneider | 686516b | 2019-12-11 11:38:48 +0000 | [diff] [blame] | 1461 | unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id) |
Patrick Bellasi | 9d20ad7 | 2019-06-21 09:42:11 +0100 | [diff] [blame] | 1462 | { |
| 1463 | struct uclamp_se uc_eff; |
| 1464 | |
| 1465 | /* Task currently refcounted: use back-annotated (effective) value */ |
| 1466 | if (p->uclamp[clamp_id].active) |
Valentin Schneider | 686516b | 2019-12-11 11:38:48 +0000 | [diff] [blame] | 1467 | return (unsigned long)p->uclamp[clamp_id].value; |
Patrick Bellasi | 9d20ad7 | 2019-06-21 09:42:11 +0100 | [diff] [blame] | 1468 | |
| 1469 | uc_eff = uclamp_eff_get(p, clamp_id); |
| 1470 | |
Valentin Schneider | 686516b | 2019-12-11 11:38:48 +0000 | [diff] [blame] | 1471 | return (unsigned long)uc_eff.value; |
Patrick Bellasi | 9d20ad7 | 2019-06-21 09:42:11 +0100 | [diff] [blame] | 1472 | } |
| 1473 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1474 | /* |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1475 | * When a task is enqueued on a rq, the clamp bucket currently defined by the |
| 1476 | * task's uclamp::bucket_id is refcounted on that rq. This also immediately |
| 1477 | * updates the rq's clamp value if required. |
Patrick Bellasi | 60daf9c | 2019-06-21 09:42:03 +0100 | [diff] [blame] | 1478 | * |
| 1479 | * Tasks can have a task-specific value requested from user-space, track |
| 1480 | * within each bucket the maximum value for tasks refcounted in it. |
| 1481 | * This "local max aggregation" allows to track the exact "requested" value |
| 1482 | * for each bucket when all its RUNNABLE tasks require the same clamp. |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1483 | */ |
| 1484 | static inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p, |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1485 | enum uclamp_id clamp_id) |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1486 | { |
| 1487 | struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id]; |
| 1488 | struct uclamp_se *uc_se = &p->uclamp[clamp_id]; |
| 1489 | struct uclamp_bucket *bucket; |
| 1490 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 1491 | lockdep_assert_rq_held(rq); |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1492 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1493 | /* Update task effective clamp */ |
| 1494 | p->uclamp[clamp_id] = uclamp_eff_get(p, clamp_id); |
| 1495 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1496 | bucket = &uc_rq->bucket[uc_se->bucket_id]; |
| 1497 | bucket->tasks++; |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1498 | uc_se->active = true; |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1499 | |
Patrick Bellasi | e496187 | 2019-06-21 09:42:04 +0100 | [diff] [blame] | 1500 | uclamp_idle_reset(rq, clamp_id, uc_se->value); |
| 1501 | |
Patrick Bellasi | 60daf9c | 2019-06-21 09:42:03 +0100 | [diff] [blame] | 1502 | /* |
| 1503 | * Local max aggregation: rq buckets always track the max |
| 1504 | * "requested" clamp value of its RUNNABLE tasks. |
| 1505 | */ |
| 1506 | if (bucket->tasks == 1 || uc_se->value > bucket->value) |
| 1507 | bucket->value = uc_se->value; |
| 1508 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1509 | if (uc_se->value > READ_ONCE(uc_rq->value)) |
Patrick Bellasi | 60daf9c | 2019-06-21 09:42:03 +0100 | [diff] [blame] | 1510 | WRITE_ONCE(uc_rq->value, uc_se->value); |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /* |
| 1514 | * When a task is dequeued from a rq, the clamp bucket refcounted by the task |
| 1515 | * is released. If this is the last task reference counting the rq's max |
| 1516 | * active clamp value, then the rq's clamp value is updated. |
| 1517 | * |
| 1518 | * Both refcounted tasks and rq's cached clamp values are expected to be |
| 1519 | * always valid. If it's detected they are not, as defensive programming, |
| 1520 | * enforce the expected state and warn. |
| 1521 | */ |
| 1522 | static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p, |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1523 | enum uclamp_id clamp_id) |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1524 | { |
| 1525 | struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id]; |
| 1526 | struct uclamp_se *uc_se = &p->uclamp[clamp_id]; |
| 1527 | struct uclamp_bucket *bucket; |
Patrick Bellasi | e496187 | 2019-06-21 09:42:04 +0100 | [diff] [blame] | 1528 | unsigned int bkt_clamp; |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1529 | unsigned int rq_clamp; |
| 1530 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 1531 | lockdep_assert_rq_held(rq); |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1532 | |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1533 | /* |
| 1534 | * If sched_uclamp_used was enabled after task @p was enqueued, |
| 1535 | * we could end up with unbalanced call to uclamp_rq_dec_id(). |
| 1536 | * |
| 1537 | * In this case the uc_se->active flag should be false since no uclamp |
| 1538 | * accounting was performed at enqueue time and we can just return |
| 1539 | * here. |
| 1540 | * |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 1541 | * Need to be careful of the following enqueue/dequeue ordering |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1542 | * problem too |
| 1543 | * |
| 1544 | * enqueue(taskA) |
| 1545 | * // sched_uclamp_used gets enabled |
| 1546 | * enqueue(taskB) |
| 1547 | * dequeue(taskA) |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 1548 | * // Must not decrement bucket->tasks here |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1549 | * dequeue(taskB) |
| 1550 | * |
| 1551 | * where we could end up with stale data in uc_se and |
| 1552 | * bucket[uc_se->bucket_id]. |
| 1553 | * |
| 1554 | * The following check here eliminates the possibility of such race. |
| 1555 | */ |
| 1556 | if (unlikely(!uc_se->active)) |
| 1557 | return; |
| 1558 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1559 | bucket = &uc_rq->bucket[uc_se->bucket_id]; |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1560 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1561 | SCHED_WARN_ON(!bucket->tasks); |
| 1562 | if (likely(bucket->tasks)) |
| 1563 | bucket->tasks--; |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1564 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1565 | uc_se->active = false; |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1566 | |
Patrick Bellasi | 60daf9c | 2019-06-21 09:42:03 +0100 | [diff] [blame] | 1567 | /* |
| 1568 | * Keep "local max aggregation" simple and accept to (possibly) |
| 1569 | * overboost some RUNNABLE tasks in the same bucket. |
| 1570 | * The rq clamp bucket value is reset to its base value whenever |
| 1571 | * there are no more RUNNABLE tasks refcounting it. |
| 1572 | */ |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1573 | if (likely(bucket->tasks)) |
| 1574 | return; |
| 1575 | |
| 1576 | rq_clamp = READ_ONCE(uc_rq->value); |
| 1577 | /* |
| 1578 | * Defensive programming: this should never happen. If it happens, |
| 1579 | * e.g. due to future modification, warn and fixup the expected value. |
| 1580 | */ |
| 1581 | SCHED_WARN_ON(bucket->value > rq_clamp); |
Patrick Bellasi | e496187 | 2019-06-21 09:42:04 +0100 | [diff] [blame] | 1582 | if (bucket->value >= rq_clamp) { |
| 1583 | bkt_clamp = uclamp_rq_max_value(rq, clamp_id, uc_se->value); |
| 1584 | WRITE_ONCE(uc_rq->value, bkt_clamp); |
| 1585 | } |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) |
| 1589 | { |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1590 | enum uclamp_id clamp_id; |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1591 | |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1592 | /* |
| 1593 | * Avoid any overhead until uclamp is actually used by the userspace. |
| 1594 | * |
| 1595 | * The condition is constructed such that a NOP is generated when |
| 1596 | * sched_uclamp_used is disabled. |
| 1597 | */ |
| 1598 | if (!static_branch_unlikely(&sched_uclamp_used)) |
| 1599 | return; |
| 1600 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1601 | if (unlikely(!p->sched_class->uclamp_enabled)) |
| 1602 | return; |
| 1603 | |
| 1604 | for_each_clamp_id(clamp_id) |
| 1605 | uclamp_rq_inc_id(rq, p, clamp_id); |
Patrick Bellasi | e496187 | 2019-06-21 09:42:04 +0100 | [diff] [blame] | 1606 | |
| 1607 | /* Reset clamp idle holding when there is one RUNNABLE task */ |
| 1608 | if (rq->uclamp_flags & UCLAMP_FLAG_IDLE) |
| 1609 | rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE; |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) |
| 1613 | { |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1614 | enum uclamp_id clamp_id; |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1615 | |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1616 | /* |
| 1617 | * Avoid any overhead until uclamp is actually used by the userspace. |
| 1618 | * |
| 1619 | * The condition is constructed such that a NOP is generated when |
| 1620 | * sched_uclamp_used is disabled. |
| 1621 | */ |
| 1622 | if (!static_branch_unlikely(&sched_uclamp_used)) |
| 1623 | return; |
| 1624 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1625 | if (unlikely(!p->sched_class->uclamp_enabled)) |
| 1626 | return; |
| 1627 | |
| 1628 | for_each_clamp_id(clamp_id) |
| 1629 | uclamp_rq_dec_id(rq, p, clamp_id); |
| 1630 | } |
| 1631 | |
Patrick Bellasi | babbe17 | 2019-08-22 14:28:10 +0100 | [diff] [blame] | 1632 | static inline void |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1633 | uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id) |
Patrick Bellasi | babbe17 | 2019-08-22 14:28:10 +0100 | [diff] [blame] | 1634 | { |
| 1635 | struct rq_flags rf; |
| 1636 | struct rq *rq; |
| 1637 | |
| 1638 | /* |
| 1639 | * Lock the task and the rq where the task is (or was) queued. |
| 1640 | * |
| 1641 | * We might lock the (previous) rq of a !RUNNABLE task, but that's the |
| 1642 | * price to pay to safely serialize util_{min,max} updates with |
| 1643 | * enqueues, dequeues and migration operations. |
| 1644 | * This is the same locking schema used by __set_cpus_allowed_ptr(). |
| 1645 | */ |
| 1646 | rq = task_rq_lock(p, &rf); |
| 1647 | |
| 1648 | /* |
| 1649 | * Setting the clamp bucket is serialized by task_rq_lock(). |
| 1650 | * If the task is not yet RUNNABLE and its task_struct is not |
| 1651 | * affecting a valid clamp bucket, the next time it's enqueued, |
| 1652 | * it will already see the updated clamp bucket value. |
| 1653 | */ |
Qais Yousef | 6e1ff07 | 2019-11-14 21:10:52 +0000 | [diff] [blame] | 1654 | if (p->uclamp[clamp_id].active) { |
Patrick Bellasi | babbe17 | 2019-08-22 14:28:10 +0100 | [diff] [blame] | 1655 | uclamp_rq_dec_id(rq, p, clamp_id); |
| 1656 | uclamp_rq_inc_id(rq, p, clamp_id); |
| 1657 | } |
| 1658 | |
| 1659 | task_rq_unlock(rq, p, &rf); |
| 1660 | } |
| 1661 | |
Qais Yousef | e3b8b6a | 2019-11-05 11:22:12 +0000 | [diff] [blame] | 1662 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
Patrick Bellasi | babbe17 | 2019-08-22 14:28:10 +0100 | [diff] [blame] | 1663 | static inline void |
| 1664 | uclamp_update_active_tasks(struct cgroup_subsys_state *css, |
| 1665 | unsigned int clamps) |
| 1666 | { |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1667 | enum uclamp_id clamp_id; |
Patrick Bellasi | babbe17 | 2019-08-22 14:28:10 +0100 | [diff] [blame] | 1668 | struct css_task_iter it; |
| 1669 | struct task_struct *p; |
Patrick Bellasi | babbe17 | 2019-08-22 14:28:10 +0100 | [diff] [blame] | 1670 | |
| 1671 | css_task_iter_start(css, 0, &it); |
| 1672 | while ((p = css_task_iter_next(&it))) { |
| 1673 | for_each_clamp_id(clamp_id) { |
| 1674 | if ((0x1 << clamp_id) & clamps) |
| 1675 | uclamp_update_active(p, clamp_id); |
| 1676 | } |
| 1677 | } |
| 1678 | css_task_iter_end(&it); |
| 1679 | } |
| 1680 | |
Patrick Bellasi | 7274a5c | 2019-08-22 14:28:08 +0100 | [diff] [blame] | 1681 | static void cpu_util_update_eff(struct cgroup_subsys_state *css); |
| 1682 | static void uclamp_update_root_tg(void) |
| 1683 | { |
| 1684 | struct task_group *tg = &root_task_group; |
| 1685 | |
| 1686 | uclamp_se_set(&tg->uclamp_req[UCLAMP_MIN], |
| 1687 | sysctl_sched_uclamp_util_min, false); |
| 1688 | uclamp_se_set(&tg->uclamp_req[UCLAMP_MAX], |
| 1689 | sysctl_sched_uclamp_util_max, false); |
| 1690 | |
| 1691 | rcu_read_lock(); |
| 1692 | cpu_util_update_eff(&root_task_group.css); |
| 1693 | rcu_read_unlock(); |
| 1694 | } |
| 1695 | #else |
| 1696 | static void uclamp_update_root_tg(void) { } |
| 1697 | #endif |
| 1698 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1699 | int sysctl_sched_uclamp_handler(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 1700 | void *buffer, size_t *lenp, loff_t *ppos) |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1701 | { |
Patrick Bellasi | 7274a5c | 2019-08-22 14:28:08 +0100 | [diff] [blame] | 1702 | bool update_root_tg = false; |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1703 | int old_min, old_max, old_min_rt; |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1704 | int result; |
| 1705 | |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 1706 | mutex_lock(&uclamp_mutex); |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1707 | old_min = sysctl_sched_uclamp_util_min; |
| 1708 | old_max = sysctl_sched_uclamp_util_max; |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1709 | old_min_rt = sysctl_sched_uclamp_util_min_rt_default; |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1710 | |
| 1711 | result = proc_dointvec(table, write, buffer, lenp, ppos); |
| 1712 | if (result) |
| 1713 | goto undo; |
| 1714 | if (!write) |
| 1715 | goto done; |
| 1716 | |
| 1717 | if (sysctl_sched_uclamp_util_min > sysctl_sched_uclamp_util_max || |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1718 | sysctl_sched_uclamp_util_max > SCHED_CAPACITY_SCALE || |
| 1719 | sysctl_sched_uclamp_util_min_rt_default > SCHED_CAPACITY_SCALE) { |
| 1720 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1721 | result = -EINVAL; |
| 1722 | goto undo; |
| 1723 | } |
| 1724 | |
| 1725 | if (old_min != sysctl_sched_uclamp_util_min) { |
| 1726 | uclamp_se_set(&uclamp_default[UCLAMP_MIN], |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1727 | sysctl_sched_uclamp_util_min, false); |
Patrick Bellasi | 7274a5c | 2019-08-22 14:28:08 +0100 | [diff] [blame] | 1728 | update_root_tg = true; |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1729 | } |
| 1730 | if (old_max != sysctl_sched_uclamp_util_max) { |
| 1731 | uclamp_se_set(&uclamp_default[UCLAMP_MAX], |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1732 | sysctl_sched_uclamp_util_max, false); |
Patrick Bellasi | 7274a5c | 2019-08-22 14:28:08 +0100 | [diff] [blame] | 1733 | update_root_tg = true; |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1734 | } |
| 1735 | |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1736 | if (update_root_tg) { |
| 1737 | static_branch_enable(&sched_uclamp_used); |
Patrick Bellasi | 7274a5c | 2019-08-22 14:28:08 +0100 | [diff] [blame] | 1738 | uclamp_update_root_tg(); |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 1739 | } |
Patrick Bellasi | 7274a5c | 2019-08-22 14:28:08 +0100 | [diff] [blame] | 1740 | |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1741 | if (old_min_rt != sysctl_sched_uclamp_util_min_rt_default) { |
| 1742 | static_branch_enable(&sched_uclamp_used); |
| 1743 | uclamp_sync_util_min_rt_default(); |
| 1744 | } |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1745 | |
| 1746 | /* |
Patrick Bellasi | 7274a5c | 2019-08-22 14:28:08 +0100 | [diff] [blame] | 1747 | * We update all RUNNABLE tasks only when task groups are in use. |
| 1748 | * Otherwise, keep it simple and do just a lazy update at each next |
| 1749 | * task enqueue time. |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1750 | */ |
Patrick Bellasi | 7274a5c | 2019-08-22 14:28:08 +0100 | [diff] [blame] | 1751 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1752 | goto done; |
| 1753 | |
| 1754 | undo: |
| 1755 | sysctl_sched_uclamp_util_min = old_min; |
| 1756 | sysctl_sched_uclamp_util_max = old_max; |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1757 | sysctl_sched_uclamp_util_min_rt_default = old_min_rt; |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1758 | done: |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 1759 | mutex_unlock(&uclamp_mutex); |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1760 | |
| 1761 | return result; |
| 1762 | } |
| 1763 | |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1764 | static int uclamp_validate(struct task_struct *p, |
| 1765 | const struct sched_attr *attr) |
| 1766 | { |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1767 | int util_min = p->uclamp_req[UCLAMP_MIN].value; |
| 1768 | int util_max = p->uclamp_req[UCLAMP_MAX].value; |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1769 | |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1770 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) { |
| 1771 | util_min = attr->sched_util_min; |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1772 | |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1773 | if (util_min + 1 > SCHED_CAPACITY_SCALE + 1) |
| 1774 | return -EINVAL; |
| 1775 | } |
| 1776 | |
| 1777 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) { |
| 1778 | util_max = attr->sched_util_max; |
| 1779 | |
| 1780 | if (util_max + 1 > SCHED_CAPACITY_SCALE + 1) |
| 1781 | return -EINVAL; |
| 1782 | } |
| 1783 | |
| 1784 | if (util_min != -1 && util_max != -1 && util_min > util_max) |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1785 | return -EINVAL; |
| 1786 | |
Qais Yousef | e65855a | 2020-07-16 12:03:47 +0100 | [diff] [blame] | 1787 | /* |
| 1788 | * We have valid uclamp attributes; make sure uclamp is enabled. |
| 1789 | * |
| 1790 | * We need to do that here, because enabling static branches is a |
| 1791 | * blocking operation which obviously cannot be done while holding |
| 1792 | * scheduler locks. |
| 1793 | */ |
| 1794 | static_branch_enable(&sched_uclamp_used); |
| 1795 | |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
| 1798 | |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1799 | static bool uclamp_reset(const struct sched_attr *attr, |
| 1800 | enum uclamp_id clamp_id, |
| 1801 | struct uclamp_se *uc_se) |
| 1802 | { |
| 1803 | /* Reset on sched class change for a non user-defined clamp value. */ |
| 1804 | if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)) && |
| 1805 | !uc_se->user_defined) |
| 1806 | return true; |
| 1807 | |
| 1808 | /* Reset on sched_util_{min,max} == -1. */ |
| 1809 | if (clamp_id == UCLAMP_MIN && |
| 1810 | attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN && |
| 1811 | attr->sched_util_min == -1) { |
| 1812 | return true; |
| 1813 | } |
| 1814 | |
| 1815 | if (clamp_id == UCLAMP_MAX && |
| 1816 | attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX && |
| 1817 | attr->sched_util_max == -1) { |
| 1818 | return true; |
| 1819 | } |
| 1820 | |
| 1821 | return false; |
| 1822 | } |
| 1823 | |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1824 | static void __setscheduler_uclamp(struct task_struct *p, |
| 1825 | const struct sched_attr *attr) |
| 1826 | { |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1827 | enum uclamp_id clamp_id; |
Patrick Bellasi | 1a00d99 | 2019-06-21 09:42:09 +0100 | [diff] [blame] | 1828 | |
Patrick Bellasi | 1a00d99 | 2019-06-21 09:42:09 +0100 | [diff] [blame] | 1829 | for_each_clamp_id(clamp_id) { |
| 1830 | struct uclamp_se *uc_se = &p->uclamp_req[clamp_id]; |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1831 | unsigned int value; |
Patrick Bellasi | 1a00d99 | 2019-06-21 09:42:09 +0100 | [diff] [blame] | 1832 | |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1833 | if (!uclamp_reset(attr, clamp_id, uc_se)) |
Patrick Bellasi | 1a00d99 | 2019-06-21 09:42:09 +0100 | [diff] [blame] | 1834 | continue; |
| 1835 | |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1836 | /* |
| 1837 | * RT by default have a 100% boost value that could be modified |
| 1838 | * at runtime. |
| 1839 | */ |
Patrick Bellasi | 1a00d99 | 2019-06-21 09:42:09 +0100 | [diff] [blame] | 1840 | if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN)) |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1841 | value = sysctl_sched_uclamp_util_min_rt_default; |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1842 | else |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1843 | value = uclamp_none(clamp_id); |
| 1844 | |
| 1845 | uclamp_se_set(uc_se, value, false); |
Patrick Bellasi | 1a00d99 | 2019-06-21 09:42:09 +0100 | [diff] [blame] | 1846 | |
Patrick Bellasi | 1a00d99 | 2019-06-21 09:42:09 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1849 | if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP))) |
| 1850 | return; |
| 1851 | |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1852 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN && |
| 1853 | attr->sched_util_min != -1) { |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1854 | uclamp_se_set(&p->uclamp_req[UCLAMP_MIN], |
| 1855 | attr->sched_util_min, true); |
| 1856 | } |
| 1857 | |
Dietmar Eggemann | 480a6ca | 2020-11-13 12:34:54 +0100 | [diff] [blame] | 1858 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX && |
| 1859 | attr->sched_util_max != -1) { |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1860 | uclamp_se_set(&p->uclamp_req[UCLAMP_MAX], |
| 1861 | attr->sched_util_max, true); |
| 1862 | } |
| 1863 | } |
| 1864 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1865 | static void uclamp_fork(struct task_struct *p) |
| 1866 | { |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1867 | enum uclamp_id clamp_id; |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1868 | |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1869 | /* |
| 1870 | * We don't need to hold task_rq_lock() when updating p->uclamp_* here |
| 1871 | * as the task is still at its early fork stages. |
| 1872 | */ |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1873 | for_each_clamp_id(clamp_id) |
| 1874 | p->uclamp[clamp_id].active = false; |
Patrick Bellasi | a87498a | 2019-06-21 09:42:08 +0100 | [diff] [blame] | 1875 | |
| 1876 | if (likely(!p->sched_reset_on_fork)) |
| 1877 | return; |
| 1878 | |
| 1879 | for_each_clamp_id(clamp_id) { |
Quentin Perret | eaf5a92 | 2020-04-16 09:59:56 +0100 | [diff] [blame] | 1880 | uclamp_se_set(&p->uclamp_req[clamp_id], |
| 1881 | uclamp_none(clamp_id), false); |
Patrick Bellasi | a87498a | 2019-06-21 09:42:08 +0100 | [diff] [blame] | 1882 | } |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1883 | } |
| 1884 | |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1885 | static void uclamp_post_fork(struct task_struct *p) |
| 1886 | { |
| 1887 | uclamp_update_util_min_rt_default(p); |
| 1888 | } |
| 1889 | |
Qais Yousef | d81ae8a | 2020-06-30 12:21:22 +0100 | [diff] [blame] | 1890 | static void __init init_uclamp_rq(struct rq *rq) |
| 1891 | { |
| 1892 | enum uclamp_id clamp_id; |
| 1893 | struct uclamp_rq *uc_rq = rq->uclamp; |
| 1894 | |
| 1895 | for_each_clamp_id(clamp_id) { |
| 1896 | uc_rq[clamp_id] = (struct uclamp_rq) { |
| 1897 | .value = uclamp_none(clamp_id) |
| 1898 | }; |
| 1899 | } |
| 1900 | |
| 1901 | rq->uclamp_flags = 0; |
| 1902 | } |
| 1903 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1904 | static void __init init_uclamp(void) |
| 1905 | { |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1906 | struct uclamp_se uc_max = {}; |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 1907 | enum uclamp_id clamp_id; |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1908 | int cpu; |
| 1909 | |
Qais Yousef | d81ae8a | 2020-06-30 12:21:22 +0100 | [diff] [blame] | 1910 | for_each_possible_cpu(cpu) |
| 1911 | init_uclamp_rq(cpu_rq(cpu)); |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1912 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1913 | for_each_clamp_id(clamp_id) { |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1914 | uclamp_se_set(&init_task.uclamp_req[clamp_id], |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1915 | uclamp_none(clamp_id), false); |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1916 | } |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1917 | |
| 1918 | /* System defaults allow max clamp values for both indexes */ |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1919 | uclamp_se_set(&uc_max, uclamp_none(UCLAMP_MAX), false); |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 1920 | for_each_clamp_id(clamp_id) { |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1921 | uclamp_default[clamp_id] = uc_max; |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 1922 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
| 1923 | root_task_group.uclamp_req[clamp_id] = uc_max; |
Patrick Bellasi | 0b60ba2 | 2019-08-22 14:28:07 +0100 | [diff] [blame] | 1924 | root_task_group.uclamp[clamp_id] = uc_max; |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 1925 | #endif |
| 1926 | } |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | #else /* CONFIG_UCLAMP_TASK */ |
| 1930 | static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) { } |
| 1931 | static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { } |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 1932 | static inline int uclamp_validate(struct task_struct *p, |
| 1933 | const struct sched_attr *attr) |
| 1934 | { |
| 1935 | return -EOPNOTSUPP; |
| 1936 | } |
| 1937 | static void __setscheduler_uclamp(struct task_struct *p, |
| 1938 | const struct sched_attr *attr) { } |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 1939 | static inline void uclamp_fork(struct task_struct *p) { } |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 1940 | static inline void uclamp_post_fork(struct task_struct *p) { } |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1941 | static inline void init_uclamp(void) { } |
| 1942 | #endif /* CONFIG_UCLAMP_TASK */ |
| 1943 | |
Peter Zijlstra | 1de6444 | 2015-09-30 17:44:13 +0200 | [diff] [blame] | 1944 | static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags) |
Gregory Haskins | 2087a1a | 2008-06-27 14:30:00 -0600 | [diff] [blame] | 1945 | { |
Peter Zijlstra | 0a67d1e | 2016-10-04 16:29:45 +0200 | [diff] [blame] | 1946 | if (!(flags & ENQUEUE_NOCLOCK)) |
| 1947 | update_rq_clock(rq); |
| 1948 | |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1949 | if (!(flags & ENQUEUE_RESTORE)) { |
Peter Zijlstra | 4e29fb7 | 2021-05-04 22:43:45 +0200 | [diff] [blame] | 1950 | sched_info_enqueue(rq, p); |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1951 | psi_enqueue(p, flags & ENQUEUE_WAKEUP); |
| 1952 | } |
Peter Zijlstra | 0a67d1e | 2016-10-04 16:29:45 +0200 | [diff] [blame] | 1953 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1954 | uclamp_rq_inc(rq, p); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1955 | p->sched_class->enqueue_task(rq, p, flags); |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 1956 | |
| 1957 | if (sched_core_enabled(rq)) |
| 1958 | sched_core_enqueue(rq, p); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1959 | } |
| 1960 | |
Peter Zijlstra | 1de6444 | 2015-09-30 17:44:13 +0200 | [diff] [blame] | 1961 | static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1962 | { |
Peter Zijlstra | 8a311c7 | 2020-11-17 18:19:36 -0500 | [diff] [blame] | 1963 | if (sched_core_enabled(rq)) |
| 1964 | sched_core_dequeue(rq, p); |
| 1965 | |
Peter Zijlstra | 0a67d1e | 2016-10-04 16:29:45 +0200 | [diff] [blame] | 1966 | if (!(flags & DEQUEUE_NOCLOCK)) |
| 1967 | update_rq_clock(rq); |
| 1968 | |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1969 | if (!(flags & DEQUEUE_SAVE)) { |
Peter Zijlstra | 4e29fb7 | 2021-05-04 22:43:45 +0200 | [diff] [blame] | 1970 | sched_info_dequeue(rq, p); |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1971 | psi_dequeue(p, flags & DEQUEUE_SLEEP); |
| 1972 | } |
Peter Zijlstra | 0a67d1e | 2016-10-04 16:29:45 +0200 | [diff] [blame] | 1973 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 1974 | uclamp_rq_dec(rq, p); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1975 | p->sched_class->dequeue_task(rq, p, flags); |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1976 | } |
| 1977 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1978 | void activate_task(struct rq *rq, struct task_struct *p, int flags) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1979 | { |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1980 | enqueue_task(rq, p, flags); |
Peter Zijlstra | 7dd7788 | 2019-04-09 09:59:05 +0200 | [diff] [blame] | 1981 | |
| 1982 | p->on_rq = TASK_ON_RQ_QUEUED; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1983 | } |
| 1984 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1985 | void deactivate_task(struct rq *rq, struct task_struct *p, int flags) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1986 | { |
Peter Zijlstra | 7dd7788 | 2019-04-09 09:59:05 +0200 | [diff] [blame] | 1987 | p->on_rq = (flags & DEQUEUE_SLEEP) ? 0 : TASK_ON_RQ_MIGRATING; |
| 1988 | |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1989 | dequeue_task(rq, p, flags); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1990 | } |
| 1991 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1992 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1993 | * __normal_prio - return the priority that is based on the static prio |
Ingo Molnar | 71f8bd4 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1994 | */ |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1995 | static inline int __normal_prio(struct task_struct *p) |
| 1996 | { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1997 | return p->static_prio; |
Ingo Molnar | 1453118 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2001 | * Calculate the expected normal priority: i.e. priority |
| 2002 | * without taking RT-inheritance into account. Might be |
| 2003 | * boosted by interactivity modifiers. Changes upon fork, |
| 2004 | * setprio syscalls, and whenever the interactivity |
| 2005 | * estimator recalculates. |
| 2006 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2007 | static inline int normal_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2008 | { |
| 2009 | int prio; |
| 2010 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2011 | if (task_has_dl_policy(p)) |
| 2012 | prio = MAX_DL_PRIO-1; |
| 2013 | else if (task_has_rt_policy(p)) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2014 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
| 2015 | else |
| 2016 | prio = __normal_prio(p); |
| 2017 | return prio; |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * Calculate the current priority, i.e. the priority |
| 2022 | * taken into account by the scheduler. This value might |
| 2023 | * be boosted by RT tasks, or might be boosted by |
| 2024 | * interactivity modifiers. Will be RT if the task got |
| 2025 | * RT-boosted. If not then it returns p->normal_prio. |
| 2026 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2027 | static int effective_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 2028 | { |
| 2029 | p->normal_prio = normal_prio(p); |
| 2030 | /* |
| 2031 | * If we are RT tasks or we were boosted to RT priority, |
| 2032 | * keep the priority unchanged. Otherwise, update priority |
| 2033 | * to the normal priority: |
| 2034 | */ |
| 2035 | if (!rt_prio(p->prio)) |
| 2036 | return p->normal_prio; |
| 2037 | return p->prio; |
| 2038 | } |
| 2039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | /** |
| 2041 | * task_curr - is this task currently executing on a CPU? |
| 2042 | * @p: the task in question. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 2043 | * |
| 2044 | * Return: 1 if the task is currently executing. 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2046 | inline int task_curr(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | { |
| 2048 | return cpu_curr(task_cpu(p)) == p; |
| 2049 | } |
| 2050 | |
Kirill Tkhai | 67dfa1b | 2014-10-27 17:40:52 +0300 | [diff] [blame] | 2051 | /* |
Peter Zijlstra | 4c9a4bc | 2015-06-11 14:46:39 +0200 | [diff] [blame] | 2052 | * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock, |
| 2053 | * use the balance_callback list if you want balancing. |
| 2054 | * |
| 2055 | * this means any call to check_class_changed() must be followed by a call to |
| 2056 | * balance_callback(). |
Kirill Tkhai | 67dfa1b | 2014-10-27 17:40:52 +0300 | [diff] [blame] | 2057 | */ |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2058 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
| 2059 | const struct sched_class *prev_class, |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 2060 | int oldprio) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2061 | { |
| 2062 | if (prev_class != p->sched_class) { |
| 2063 | if (prev_class->switched_from) |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 2064 | prev_class->switched_from(rq, p); |
Peter Zijlstra | 4c9a4bc | 2015-06-11 14:46:39 +0200 | [diff] [blame] | 2065 | |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 2066 | p->sched_class->switched_to(rq, p); |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 2067 | } else if (oldprio != p->prio || dl_task(p)) |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 2068 | p->sched_class->prio_changed(rq, p, oldprio); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2069 | } |
| 2070 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2071 | void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) |
Peter Zijlstra | 1e5a740 | 2010-10-31 12:37:04 +0100 | [diff] [blame] | 2072 | { |
Kirill Tkhai | aa93cd5 | 2019-12-19 16:44:55 -0500 | [diff] [blame] | 2073 | if (p->sched_class == rq->curr->sched_class) |
Peter Zijlstra | 1e5a740 | 2010-10-31 12:37:04 +0100 | [diff] [blame] | 2074 | rq->curr->sched_class->check_preempt_curr(rq, p, flags); |
Kirill Tkhai | aa93cd5 | 2019-12-19 16:44:55 -0500 | [diff] [blame] | 2075 | else if (p->sched_class > rq->curr->sched_class) |
| 2076 | resched_curr(rq); |
Peter Zijlstra | 1e5a740 | 2010-10-31 12:37:04 +0100 | [diff] [blame] | 2077 | |
| 2078 | /* |
| 2079 | * A queue event has occurred, and we're going to schedule. In |
| 2080 | * this case, we can save a useless back to back clock update. |
| 2081 | */ |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2082 | if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr)) |
Davidlohr Bueso | adcc8da | 2018-04-04 09:15:39 -0700 | [diff] [blame] | 2083 | rq_clock_skip_update(rq); |
Peter Zijlstra | 1e5a740 | 2010-10-31 12:37:04 +0100 | [diff] [blame] | 2084 | } |
| 2085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | #ifdef CONFIG_SMP |
Peter Zijlstra | 175f0e2 | 2017-07-25 18:58:21 +0200 | [diff] [blame] | 2087 | |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2088 | static void |
| 2089 | __do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask, u32 flags); |
| 2090 | |
| 2091 | static int __set_cpus_allowed_ptr(struct task_struct *p, |
| 2092 | const struct cpumask *new_mask, |
| 2093 | u32 flags); |
| 2094 | |
| 2095 | static void migrate_disable_switch(struct rq *rq, struct task_struct *p) |
| 2096 | { |
| 2097 | if (likely(!p->migration_disabled)) |
| 2098 | return; |
| 2099 | |
| 2100 | if (p->cpus_ptr != &p->cpus_mask) |
| 2101 | return; |
| 2102 | |
| 2103 | /* |
| 2104 | * Violates locking rules! see comment in __do_set_cpus_allowed(). |
| 2105 | */ |
| 2106 | __do_set_cpus_allowed(p, cpumask_of(rq->cpu), SCA_MIGRATE_DISABLE); |
| 2107 | } |
| 2108 | |
| 2109 | void migrate_disable(void) |
| 2110 | { |
Thomas Gleixner | 3015ef4 | 2020-08-26 14:08:10 +0200 | [diff] [blame] | 2111 | struct task_struct *p = current; |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2112 | |
Thomas Gleixner | 3015ef4 | 2020-08-26 14:08:10 +0200 | [diff] [blame] | 2113 | if (p->migration_disabled) { |
| 2114 | p->migration_disabled++; |
| 2115 | return; |
| 2116 | } |
| 2117 | |
| 2118 | preempt_disable(); |
| 2119 | this_rq()->nr_pinned++; |
| 2120 | p->migration_disabled = 1; |
| 2121 | preempt_enable(); |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2122 | } |
| 2123 | EXPORT_SYMBOL_GPL(migrate_disable); |
| 2124 | |
| 2125 | void migrate_enable(void) |
| 2126 | { |
| 2127 | struct task_struct *p = current; |
| 2128 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2129 | if (p->migration_disabled > 1) { |
| 2130 | p->migration_disabled--; |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2131 | return; |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2132 | } |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2133 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2134 | /* |
| 2135 | * Ensure stop_task runs either before or after this, and that |
| 2136 | * __set_cpus_allowed_ptr(SCA_MIGRATE_ENABLE) doesn't schedule(). |
| 2137 | */ |
| 2138 | preempt_disable(); |
| 2139 | if (p->cpus_ptr != &p->cpus_mask) |
| 2140 | __set_cpus_allowed_ptr(p, &p->cpus_mask, SCA_MIGRATE_ENABLE); |
| 2141 | /* |
| 2142 | * Mustn't clear migration_disabled() until cpus_ptr points back at the |
| 2143 | * regular cpus_mask, otherwise things that race (eg. |
| 2144 | * select_fallback_rq) get confused. |
| 2145 | */ |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2146 | barrier(); |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2147 | p->migration_disabled = 0; |
Thomas Gleixner | 3015ef4 | 2020-08-26 14:08:10 +0200 | [diff] [blame] | 2148 | this_rq()->nr_pinned--; |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2149 | preempt_enable(); |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2150 | } |
| 2151 | EXPORT_SYMBOL_GPL(migrate_enable); |
| 2152 | |
Thomas Gleixner | 3015ef4 | 2020-08-26 14:08:10 +0200 | [diff] [blame] | 2153 | static inline bool rq_has_pinned_tasks(struct rq *rq) |
| 2154 | { |
| 2155 | return rq->nr_pinned; |
| 2156 | } |
| 2157 | |
Peter Zijlstra | 175f0e2 | 2017-07-25 18:58:21 +0200 | [diff] [blame] | 2158 | /* |
Joel Savitz | bee9853 | 2019-03-06 20:13:33 -0500 | [diff] [blame] | 2159 | * Per-CPU kthreads are allowed to run on !active && online CPUs, see |
Peter Zijlstra | 175f0e2 | 2017-07-25 18:58:21 +0200 | [diff] [blame] | 2160 | * __set_cpus_allowed_ptr() and select_fallback_rq(). |
| 2161 | */ |
| 2162 | static inline bool is_cpu_allowed(struct task_struct *p, int cpu) |
| 2163 | { |
Peter Zijlstra | 5ba2ffba | 2021-01-12 11:28:16 +0100 | [diff] [blame] | 2164 | /* When not in the task's cpumask, no point in looking further. */ |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 2165 | if (!cpumask_test_cpu(cpu, p->cpus_ptr)) |
Peter Zijlstra | 175f0e2 | 2017-07-25 18:58:21 +0200 | [diff] [blame] | 2166 | return false; |
| 2167 | |
Peter Zijlstra | 5ba2ffba | 2021-01-12 11:28:16 +0100 | [diff] [blame] | 2168 | /* migrate_disabled() must be allowed to finish. */ |
| 2169 | if (is_migration_disabled(p)) |
Peter Zijlstra | 175f0e2 | 2017-07-25 18:58:21 +0200 | [diff] [blame] | 2170 | return cpu_online(cpu); |
| 2171 | |
Peter Zijlstra | 5ba2ffba | 2021-01-12 11:28:16 +0100 | [diff] [blame] | 2172 | /* Non kernel threads are not allowed during either online or offline. */ |
| 2173 | if (!(p->flags & PF_KTHREAD)) |
| 2174 | return cpu_active(cpu); |
| 2175 | |
| 2176 | /* KTHREAD_IS_PER_CPU is always allowed. */ |
| 2177 | if (kthread_is_per_cpu(p)) |
| 2178 | return cpu_online(cpu); |
| 2179 | |
| 2180 | /* Regular kernel threads don't get to stay during offline. */ |
Peter Zijlstra | b5c4477 | 2021-01-21 16:09:32 +0100 | [diff] [blame] | 2181 | if (cpu_dying(cpu)) |
Peter Zijlstra | 5ba2ffba | 2021-01-12 11:28:16 +0100 | [diff] [blame] | 2182 | return false; |
| 2183 | |
| 2184 | /* But are allowed during online. */ |
| 2185 | return cpu_online(cpu); |
Peter Zijlstra | 175f0e2 | 2017-07-25 18:58:21 +0200 | [diff] [blame] | 2186 | } |
| 2187 | |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2188 | /* |
| 2189 | * This is how migration works: |
| 2190 | * |
| 2191 | * 1) we invoke migration_cpu_stop() on the target CPU using |
| 2192 | * stop_one_cpu(). |
| 2193 | * 2) stopper starts to run (implicitly forcing the migrated thread |
| 2194 | * off the CPU) |
| 2195 | * 3) it checks whether the migrated task is still in the wrong runqueue. |
| 2196 | * 4) if it's in the wrong runqueue then the migration thread removes |
| 2197 | * it and puts it into the right queue. |
| 2198 | * 5) stopper completes and stop_one_cpu() returns and the migration |
| 2199 | * is done. |
| 2200 | */ |
| 2201 | |
| 2202 | /* |
| 2203 | * move_queued_task - move a queued task to new rq. |
| 2204 | * |
| 2205 | * Returns (locked) new rq. Old rq's lock is released. |
| 2206 | */ |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2207 | static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf, |
| 2208 | struct task_struct *p, int new_cpu) |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2209 | { |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 2210 | lockdep_assert_rq_held(rq); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2211 | |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 2212 | deactivate_task(rq, p, DEQUEUE_NOCLOCK); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2213 | set_task_cpu(p, new_cpu); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2214 | rq_unlock(rq, rf); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2215 | |
| 2216 | rq = cpu_rq(new_cpu); |
| 2217 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2218 | rq_lock(rq, rf); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2219 | BUG_ON(task_cpu(p) != new_cpu); |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 2220 | activate_task(rq, p, 0); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2221 | check_preempt_curr(rq, p, 0); |
| 2222 | |
| 2223 | return rq; |
| 2224 | } |
| 2225 | |
| 2226 | struct migration_arg { |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2227 | struct task_struct *task; |
| 2228 | int dest_cpu; |
| 2229 | struct set_affinity_pending *pending; |
| 2230 | }; |
| 2231 | |
Peter Zijlstra | 50caf9c | 2021-02-24 11:42:08 +0100 | [diff] [blame] | 2232 | /* |
| 2233 | * @refs: number of wait_for_completion() |
| 2234 | * @stop_pending: is @stop_work in use |
| 2235 | */ |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2236 | struct set_affinity_pending { |
| 2237 | refcount_t refs; |
Peter Zijlstra | 9e81889 | 2021-02-24 11:31:09 +0100 | [diff] [blame] | 2238 | unsigned int stop_pending; |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2239 | struct completion done; |
| 2240 | struct cpu_stop_work stop_work; |
| 2241 | struct migration_arg arg; |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2242 | }; |
| 2243 | |
| 2244 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 2245 | * Move (not current) task off this CPU, onto the destination CPU. We're doing |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2246 | * this because either it can't run here any more (set_cpus_allowed() |
| 2247 | * away from this CPU, or CPU going down), or because we're |
| 2248 | * attempting to rebalance this task on exec (sched_exec). |
| 2249 | * |
| 2250 | * So we race with normal scheduler movements, but that's OK, as long |
| 2251 | * as the task is no longer on this CPU. |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2252 | */ |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2253 | static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf, |
| 2254 | struct task_struct *p, int dest_cpu) |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2255 | { |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2256 | /* Affinity changed (again). */ |
Peter Zijlstra | 175f0e2 | 2017-07-25 18:58:21 +0200 | [diff] [blame] | 2257 | if (!is_cpu_allowed(p, dest_cpu)) |
Peter Zijlstra | 5e16bbc | 2015-06-11 14:46:51 +0200 | [diff] [blame] | 2258 | return rq; |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2259 | |
Peter Zijlstra | 15ff991 | 2016-10-05 17:59:32 +0200 | [diff] [blame] | 2260 | update_rq_clock(rq); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2261 | rq = move_queued_task(rq, rf, p, dest_cpu); |
Peter Zijlstra | 5e16bbc | 2015-06-11 14:46:51 +0200 | [diff] [blame] | 2262 | |
| 2263 | return rq; |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | /* |
| 2267 | * migration_cpu_stop - this will be executed by a highprio stopper thread |
| 2268 | * and performs thread migration by bumping thread off CPU then |
| 2269 | * 'pushing' onto another runqueue. |
| 2270 | */ |
| 2271 | static int migration_cpu_stop(void *data) |
| 2272 | { |
| 2273 | struct migration_arg *arg = data; |
Peter Zijlstra | c20cf06 | 2021-02-24 11:50:39 +0100 | [diff] [blame] | 2274 | struct set_affinity_pending *pending = arg->pending; |
Peter Zijlstra | 5e16bbc | 2015-06-11 14:46:51 +0200 | [diff] [blame] | 2275 | struct task_struct *p = arg->task; |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2276 | int dest_cpu = arg->dest_cpu; |
Peter Zijlstra | 5e16bbc | 2015-06-11 14:46:51 +0200 | [diff] [blame] | 2277 | struct rq *rq = this_rq(); |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2278 | bool complete = false; |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2279 | struct rq_flags rf; |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2280 | |
| 2281 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 2282 | * The original target CPU might have gone down and we might |
| 2283 | * be on another CPU but it doesn't matter. |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2284 | */ |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2285 | local_irq_save(rf.flags); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2286 | /* |
| 2287 | * We need to explicitly wake pending tasks before running |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 2288 | * __migrate_task() such that we will not miss enforcing cpus_ptr |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2289 | * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test. |
| 2290 | */ |
Peter Zijlstra | a1488664 | 2020-05-26 18:11:04 +0200 | [diff] [blame] | 2291 | flush_smp_call_function_from_idle(); |
Peter Zijlstra | 5e16bbc | 2015-06-11 14:46:51 +0200 | [diff] [blame] | 2292 | |
| 2293 | raw_spin_lock(&p->pi_lock); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2294 | rq_lock(rq, &rf); |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2295 | |
Peter Zijlstra | 5e16bbc | 2015-06-11 14:46:51 +0200 | [diff] [blame] | 2296 | /* |
Valentin Schneider | e140749 | 2021-02-25 10:22:30 +0100 | [diff] [blame] | 2297 | * If we were passed a pending, then ->stop_pending was set, thus |
| 2298 | * p->migration_pending must have remained stable. |
| 2299 | */ |
| 2300 | WARN_ON_ONCE(pending && pending != p->migration_pending); |
| 2301 | |
| 2302 | /* |
Peter Zijlstra | 5e16bbc | 2015-06-11 14:46:51 +0200 | [diff] [blame] | 2303 | * If task_rq(p) != rq, it cannot be migrated here, because we're |
| 2304 | * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because |
| 2305 | * we're holding p->pi_lock. |
| 2306 | */ |
Cheng Chao | bf89a30 | 2016-09-14 10:01:50 +0800 | [diff] [blame] | 2307 | if (task_rq(p) == rq) { |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2308 | if (is_migration_disabled(p)) |
| 2309 | goto out; |
Peter Zijlstra | 5e16bbc | 2015-06-11 14:46:51 +0200 | [diff] [blame] | 2310 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2311 | if (pending) { |
Valentin Schneider | e140749 | 2021-02-25 10:22:30 +0100 | [diff] [blame] | 2312 | p->migration_pending = NULL; |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2313 | complete = true; |
| 2314 | } |
| 2315 | |
Peter Zijlstra | 3f1bc11 | 2021-02-24 11:21:35 +0100 | [diff] [blame] | 2316 | if (dest_cpu < 0) { |
| 2317 | if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) |
| 2318 | goto out; |
| 2319 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2320 | dest_cpu = cpumask_any_distribute(&p->cpus_mask); |
Peter Zijlstra | 3f1bc11 | 2021-02-24 11:21:35 +0100 | [diff] [blame] | 2321 | } |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2322 | |
| 2323 | if (task_on_rq_queued(p)) |
| 2324 | rq = __migrate_task(rq, &rf, p, dest_cpu); |
| 2325 | else |
| 2326 | p->wake_cpu = dest_cpu; |
| 2327 | |
Peter Zijlstra | 3f1bc11 | 2021-02-24 11:21:35 +0100 | [diff] [blame] | 2328 | /* |
| 2329 | * XXX __migrate_task() can fail, at which point we might end |
| 2330 | * up running on a dodgy CPU, AFAICT this can only happen |
| 2331 | * during CPU hotplug, at which point we'll get pushed out |
| 2332 | * anyway, so it's probably not a big deal. |
| 2333 | */ |
| 2334 | |
Peter Zijlstra | c20cf06 | 2021-02-24 11:50:39 +0100 | [diff] [blame] | 2335 | } else if (pending) { |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2336 | /* |
| 2337 | * This happens when we get migrated between migrate_enable()'s |
| 2338 | * preempt_enable() and scheduling the stopper task. At that |
| 2339 | * point we're a regular task again and not current anymore. |
| 2340 | * |
| 2341 | * A !PREEMPT kernel has a giant hole here, which makes it far |
| 2342 | * more likely. |
| 2343 | */ |
| 2344 | |
| 2345 | /* |
Valentin Schneider | d707faa | 2020-11-13 11:24:14 +0000 | [diff] [blame] | 2346 | * The task moved before the stopper got to run. We're holding |
| 2347 | * ->pi_lock, so the allowed mask is stable - if it got |
| 2348 | * somewhere allowed, we're done. |
| 2349 | */ |
Peter Zijlstra | c20cf06 | 2021-02-24 11:50:39 +0100 | [diff] [blame] | 2350 | if (cpumask_test_cpu(task_cpu(p), p->cpus_ptr)) { |
Valentin Schneider | e140749 | 2021-02-25 10:22:30 +0100 | [diff] [blame] | 2351 | p->migration_pending = NULL; |
Valentin Schneider | d707faa | 2020-11-13 11:24:14 +0000 | [diff] [blame] | 2352 | complete = true; |
| 2353 | goto out; |
| 2354 | } |
| 2355 | |
| 2356 | /* |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2357 | * When migrate_enable() hits a rq mis-match we can't reliably |
| 2358 | * determine is_migration_disabled() and so have to chase after |
| 2359 | * it. |
| 2360 | */ |
Peter Zijlstra | 9e81889 | 2021-02-24 11:31:09 +0100 | [diff] [blame] | 2361 | WARN_ON_ONCE(!pending->stop_pending); |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2362 | task_rq_unlock(rq, p, &rf); |
| 2363 | stop_one_cpu_nowait(task_cpu(p), migration_cpu_stop, |
| 2364 | &pending->arg, &pending->stop_work); |
| 2365 | return 0; |
| 2366 | } |
| 2367 | out: |
Peter Zijlstra | 9e81889 | 2021-02-24 11:31:09 +0100 | [diff] [blame] | 2368 | if (pending) |
| 2369 | pending->stop_pending = false; |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2370 | task_rq_unlock(rq, p, &rf); |
| 2371 | |
| 2372 | if (complete) |
| 2373 | complete_all(&pending->done); |
| 2374 | |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2375 | return 0; |
| 2376 | } |
| 2377 | |
Peter Zijlstra | a7c8155 | 2020-09-28 17:06:07 +0200 | [diff] [blame] | 2378 | int push_cpu_stop(void *arg) |
| 2379 | { |
| 2380 | struct rq *lowest_rq = NULL, *rq = this_rq(); |
| 2381 | struct task_struct *p = arg; |
| 2382 | |
| 2383 | raw_spin_lock_irq(&p->pi_lock); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 2384 | raw_spin_rq_lock(rq); |
Peter Zijlstra | a7c8155 | 2020-09-28 17:06:07 +0200 | [diff] [blame] | 2385 | |
| 2386 | if (task_rq(p) != rq) |
| 2387 | goto out_unlock; |
| 2388 | |
| 2389 | if (is_migration_disabled(p)) { |
| 2390 | p->migration_flags |= MDF_PUSH; |
| 2391 | goto out_unlock; |
| 2392 | } |
| 2393 | |
| 2394 | p->migration_flags &= ~MDF_PUSH; |
| 2395 | |
| 2396 | if (p->sched_class->find_lock_rq) |
| 2397 | lowest_rq = p->sched_class->find_lock_rq(p, rq); |
| 2398 | |
| 2399 | if (!lowest_rq) |
| 2400 | goto out_unlock; |
| 2401 | |
| 2402 | // XXX validate p is still the highest prio task |
| 2403 | if (task_rq(p) == rq) { |
| 2404 | deactivate_task(rq, p, 0); |
| 2405 | set_task_cpu(p, lowest_rq->cpu); |
| 2406 | activate_task(lowest_rq, p, 0); |
| 2407 | resched_curr(lowest_rq); |
| 2408 | } |
| 2409 | |
| 2410 | double_unlock_balance(rq, lowest_rq); |
| 2411 | |
| 2412 | out_unlock: |
| 2413 | rq->push_busy = false; |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 2414 | raw_spin_rq_unlock(rq); |
Peter Zijlstra | a7c8155 | 2020-09-28 17:06:07 +0200 | [diff] [blame] | 2415 | raw_spin_unlock_irq(&p->pi_lock); |
| 2416 | |
| 2417 | put_task_struct(p); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2418 | return 0; |
| 2419 | } |
| 2420 | |
Peter Zijlstra | c5b2803 | 2015-05-15 17:43:35 +0200 | [diff] [blame] | 2421 | /* |
| 2422 | * sched_class::set_cpus_allowed must do the below, but is not required to |
| 2423 | * actually call this function. |
| 2424 | */ |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 2425 | void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask, u32 flags) |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2426 | { |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2427 | if (flags & (SCA_MIGRATE_ENABLE | SCA_MIGRATE_DISABLE)) { |
| 2428 | p->cpus_ptr = new_mask; |
| 2429 | return; |
| 2430 | } |
| 2431 | |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 2432 | cpumask_copy(&p->cpus_mask, new_mask); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2433 | p->nr_cpus_allowed = cpumask_weight(new_mask); |
| 2434 | } |
| 2435 | |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 2436 | static void |
| 2437 | __do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask, u32 flags) |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2438 | { |
Peter Zijlstra | 6c37067 | 2015-05-15 17:43:36 +0200 | [diff] [blame] | 2439 | struct rq *rq = task_rq(p); |
| 2440 | bool queued, running; |
| 2441 | |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2442 | /* |
| 2443 | * This here violates the locking rules for affinity, since we're only |
| 2444 | * supposed to change these variables while holding both rq->lock and |
| 2445 | * p->pi_lock. |
| 2446 | * |
| 2447 | * HOWEVER, it magically works, because ttwu() is the only code that |
| 2448 | * accesses these variables under p->pi_lock and only does so after |
| 2449 | * smp_cond_load_acquire(&p->on_cpu, !VAL), and we're in __schedule() |
| 2450 | * before finish_task(). |
| 2451 | * |
| 2452 | * XXX do further audits, this smells like something putrid. |
| 2453 | */ |
| 2454 | if (flags & SCA_MIGRATE_DISABLE) |
| 2455 | SCHED_WARN_ON(!p->on_cpu); |
| 2456 | else |
| 2457 | lockdep_assert_held(&p->pi_lock); |
Peter Zijlstra | 6c37067 | 2015-05-15 17:43:36 +0200 | [diff] [blame] | 2458 | |
| 2459 | queued = task_on_rq_queued(p); |
| 2460 | running = task_current(rq, p); |
| 2461 | |
| 2462 | if (queued) { |
| 2463 | /* |
| 2464 | * Because __kthread_bind() calls this on blocked tasks without |
| 2465 | * holding rq->lock. |
| 2466 | */ |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 2467 | lockdep_assert_rq_held(rq); |
Peter Zijlstra | 7a57f32 | 2017-02-21 14:47:02 +0100 | [diff] [blame] | 2468 | dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK); |
Peter Zijlstra | 6c37067 | 2015-05-15 17:43:36 +0200 | [diff] [blame] | 2469 | } |
| 2470 | if (running) |
| 2471 | put_prev_task(rq, p); |
| 2472 | |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 2473 | p->sched_class->set_cpus_allowed(p, new_mask, flags); |
Peter Zijlstra | 6c37067 | 2015-05-15 17:43:36 +0200 | [diff] [blame] | 2474 | |
Peter Zijlstra | 6c37067 | 2015-05-15 17:43:36 +0200 | [diff] [blame] | 2475 | if (queued) |
Peter Zijlstra | 7134b3e | 2017-02-21 14:23:38 +0100 | [diff] [blame] | 2476 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
Vincent Guittot | a399d23 | 2016-09-12 09:47:52 +0200 | [diff] [blame] | 2477 | if (running) |
Peter Zijlstra | 03b7fad | 2019-05-29 20:36:41 +0000 | [diff] [blame] | 2478 | set_next_task(rq, p); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2479 | } |
| 2480 | |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 2481 | void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) |
| 2482 | { |
| 2483 | __do_set_cpus_allowed(p, new_mask, 0); |
| 2484 | } |
| 2485 | |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2486 | /* |
Valentin Schneider | c777d84 | 2020-10-13 15:01:16 +0100 | [diff] [blame] | 2487 | * This function is wildly self concurrent; here be dragons. |
| 2488 | * |
| 2489 | * |
| 2490 | * When given a valid mask, __set_cpus_allowed_ptr() must block until the |
| 2491 | * designated task is enqueued on an allowed CPU. If that task is currently |
| 2492 | * running, we have to kick it out using the CPU stopper. |
| 2493 | * |
| 2494 | * Migrate-Disable comes along and tramples all over our nice sandcastle. |
| 2495 | * Consider: |
| 2496 | * |
| 2497 | * Initial conditions: P0->cpus_mask = [0, 1] |
| 2498 | * |
| 2499 | * P0@CPU0 P1 |
| 2500 | * |
| 2501 | * migrate_disable(); |
| 2502 | * <preempted> |
| 2503 | * set_cpus_allowed_ptr(P0, [1]); |
| 2504 | * |
| 2505 | * P1 *cannot* return from this set_cpus_allowed_ptr() call until P0 executes |
| 2506 | * its outermost migrate_enable() (i.e. it exits its Migrate-Disable region). |
| 2507 | * This means we need the following scheme: |
| 2508 | * |
| 2509 | * P0@CPU0 P1 |
| 2510 | * |
| 2511 | * migrate_disable(); |
| 2512 | * <preempted> |
| 2513 | * set_cpus_allowed_ptr(P0, [1]); |
| 2514 | * <blocks> |
| 2515 | * <resumes> |
| 2516 | * migrate_enable(); |
| 2517 | * __set_cpus_allowed_ptr(); |
| 2518 | * <wakes local stopper> |
| 2519 | * `--> <woken on migration completion> |
| 2520 | * |
| 2521 | * Now the fun stuff: there may be several P1-like tasks, i.e. multiple |
| 2522 | * concurrent set_cpus_allowed_ptr(P0, [*]) calls. CPU affinity changes of any |
| 2523 | * task p are serialized by p->pi_lock, which we can leverage: the one that |
| 2524 | * should come into effect at the end of the Migrate-Disable region is the last |
| 2525 | * one. This means we only need to track a single cpumask (i.e. p->cpus_mask), |
| 2526 | * but we still need to properly signal those waiting tasks at the appropriate |
| 2527 | * moment. |
| 2528 | * |
| 2529 | * This is implemented using struct set_affinity_pending. The first |
| 2530 | * __set_cpus_allowed_ptr() caller within a given Migrate-Disable region will |
| 2531 | * setup an instance of that struct and install it on the targeted task_struct. |
| 2532 | * Any and all further callers will reuse that instance. Those then wait for |
| 2533 | * a completion signaled at the tail of the CPU stopper callback (1), triggered |
| 2534 | * on the end of the Migrate-Disable region (i.e. outermost migrate_enable()). |
| 2535 | * |
| 2536 | * |
| 2537 | * (1) In the cases covered above. There is one more where the completion is |
| 2538 | * signaled within affine_move_task() itself: when a subsequent affinity request |
Valentin Schneider | e140749 | 2021-02-25 10:22:30 +0100 | [diff] [blame] | 2539 | * occurs after the stopper bailed out due to the targeted task still being |
| 2540 | * Migrate-Disable. Consider: |
Valentin Schneider | c777d84 | 2020-10-13 15:01:16 +0100 | [diff] [blame] | 2541 | * |
| 2542 | * Initial conditions: P0->cpus_mask = [0, 1] |
| 2543 | * |
Valentin Schneider | e140749 | 2021-02-25 10:22:30 +0100 | [diff] [blame] | 2544 | * CPU0 P1 P2 |
| 2545 | * <P0> |
| 2546 | * migrate_disable(); |
| 2547 | * <preempted> |
Valentin Schneider | c777d84 | 2020-10-13 15:01:16 +0100 | [diff] [blame] | 2548 | * set_cpus_allowed_ptr(P0, [1]); |
| 2549 | * <blocks> |
Valentin Schneider | e140749 | 2021-02-25 10:22:30 +0100 | [diff] [blame] | 2550 | * <migration/0> |
| 2551 | * migration_cpu_stop() |
| 2552 | * is_migration_disabled() |
| 2553 | * <bails> |
Valentin Schneider | c777d84 | 2020-10-13 15:01:16 +0100 | [diff] [blame] | 2554 | * set_cpus_allowed_ptr(P0, [0, 1]); |
| 2555 | * <signal completion> |
| 2556 | * <awakes> |
| 2557 | * |
| 2558 | * Note that the above is safe vs a concurrent migrate_enable(), as any |
| 2559 | * pending affinity completion is preceded by an uninstallation of |
| 2560 | * p->migration_pending done with p->pi_lock held. |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2561 | */ |
| 2562 | static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flags *rf, |
| 2563 | int dest_cpu, unsigned int flags) |
| 2564 | { |
| 2565 | struct set_affinity_pending my_pending = { }, *pending = NULL; |
Peter Zijlstra | 9e81889 | 2021-02-24 11:31:09 +0100 | [diff] [blame] | 2566 | bool stop_pending, complete = false; |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2567 | |
| 2568 | /* Can the task run on the task's current CPU? If so, we're done */ |
| 2569 | if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) { |
Peter Zijlstra | a7c8155 | 2020-09-28 17:06:07 +0200 | [diff] [blame] | 2570 | struct task_struct *push_task = NULL; |
| 2571 | |
| 2572 | if ((flags & SCA_MIGRATE_ENABLE) && |
| 2573 | (p->migration_flags & MDF_PUSH) && !rq->push_busy) { |
| 2574 | rq->push_busy = true; |
| 2575 | push_task = get_task_struct(p); |
| 2576 | } |
| 2577 | |
Peter Zijlstra | 50caf9c | 2021-02-24 11:42:08 +0100 | [diff] [blame] | 2578 | /* |
| 2579 | * If there are pending waiters, but no pending stop_work, |
| 2580 | * then complete now. |
| 2581 | */ |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2582 | pending = p->migration_pending; |
Peter Zijlstra | 50caf9c | 2021-02-24 11:42:08 +0100 | [diff] [blame] | 2583 | if (pending && !pending->stop_pending) { |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2584 | p->migration_pending = NULL; |
| 2585 | complete = true; |
| 2586 | } |
Peter Zijlstra | 50caf9c | 2021-02-24 11:42:08 +0100 | [diff] [blame] | 2587 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2588 | task_rq_unlock(rq, p, rf); |
| 2589 | |
Peter Zijlstra | a7c8155 | 2020-09-28 17:06:07 +0200 | [diff] [blame] | 2590 | if (push_task) { |
| 2591 | stop_one_cpu_nowait(rq->cpu, push_cpu_stop, |
| 2592 | p, &rq->push_work); |
| 2593 | } |
| 2594 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2595 | if (complete) |
Peter Zijlstra | 50caf9c | 2021-02-24 11:42:08 +0100 | [diff] [blame] | 2596 | complete_all(&pending->done); |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2597 | |
| 2598 | return 0; |
| 2599 | } |
| 2600 | |
| 2601 | if (!(flags & SCA_MIGRATE_ENABLE)) { |
| 2602 | /* serialized by p->pi_lock */ |
| 2603 | if (!p->migration_pending) { |
Valentin Schneider | c777d84 | 2020-10-13 15:01:16 +0100 | [diff] [blame] | 2604 | /* Install the request */ |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2605 | refcount_set(&my_pending.refs, 1); |
| 2606 | init_completion(&my_pending.done); |
Peter Zijlstra | 8a6edb5 | 2021-02-13 13:10:35 +0100 | [diff] [blame] | 2607 | my_pending.arg = (struct migration_arg) { |
| 2608 | .task = p, |
| 2609 | .dest_cpu = -1, /* any */ |
| 2610 | .pending = &my_pending, |
| 2611 | }; |
| 2612 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2613 | p->migration_pending = &my_pending; |
| 2614 | } else { |
| 2615 | pending = p->migration_pending; |
| 2616 | refcount_inc(&pending->refs); |
| 2617 | } |
| 2618 | } |
| 2619 | pending = p->migration_pending; |
| 2620 | /* |
| 2621 | * - !MIGRATE_ENABLE: |
| 2622 | * we'll have installed a pending if there wasn't one already. |
| 2623 | * |
| 2624 | * - MIGRATE_ENABLE: |
| 2625 | * we're here because the current CPU isn't matching anymore, |
| 2626 | * the only way that can happen is because of a concurrent |
| 2627 | * set_cpus_allowed_ptr() call, which should then still be |
| 2628 | * pending completion. |
| 2629 | * |
| 2630 | * Either way, we really should have a @pending here. |
| 2631 | */ |
| 2632 | if (WARN_ON_ONCE(!pending)) { |
| 2633 | task_rq_unlock(rq, p, rf); |
| 2634 | return -EINVAL; |
| 2635 | } |
| 2636 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2637 | if (task_running(rq, p) || p->state == TASK_WAKING) { |
Valentin Schneider | c777d84 | 2020-10-13 15:01:16 +0100 | [diff] [blame] | 2638 | /* |
Peter Zijlstra | 58b1a45 | 2021-02-24 11:15:23 +0100 | [diff] [blame] | 2639 | * MIGRATE_ENABLE gets here because 'p == current', but for |
| 2640 | * anything else we cannot do is_migration_disabled(), punt |
| 2641 | * and have the stopper function handle it all race-free. |
Valentin Schneider | c777d84 | 2020-10-13 15:01:16 +0100 | [diff] [blame] | 2642 | */ |
Peter Zijlstra | 9e81889 | 2021-02-24 11:31:09 +0100 | [diff] [blame] | 2643 | stop_pending = pending->stop_pending; |
| 2644 | if (!stop_pending) |
| 2645 | pending->stop_pending = true; |
Peter Zijlstra | 58b1a45 | 2021-02-24 11:15:23 +0100 | [diff] [blame] | 2646 | |
Peter Zijlstra | 58b1a45 | 2021-02-24 11:15:23 +0100 | [diff] [blame] | 2647 | if (flags & SCA_MIGRATE_ENABLE) |
| 2648 | p->migration_flags &= ~MDF_PUSH; |
Peter Zijlstra | 50caf9c | 2021-02-24 11:42:08 +0100 | [diff] [blame] | 2649 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2650 | task_rq_unlock(rq, p, rf); |
Peter Zijlstra | 8a6edb5 | 2021-02-13 13:10:35 +0100 | [diff] [blame] | 2651 | |
Peter Zijlstra | 9e81889 | 2021-02-24 11:31:09 +0100 | [diff] [blame] | 2652 | if (!stop_pending) { |
| 2653 | stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop, |
| 2654 | &pending->arg, &pending->stop_work); |
| 2655 | } |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2656 | |
Peter Zijlstra | 58b1a45 | 2021-02-24 11:15:23 +0100 | [diff] [blame] | 2657 | if (flags & SCA_MIGRATE_ENABLE) |
| 2658 | return 0; |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2659 | } else { |
| 2660 | |
| 2661 | if (!is_migration_disabled(p)) { |
| 2662 | if (task_on_rq_queued(p)) |
| 2663 | rq = move_queued_task(rq, rf, p, dest_cpu); |
| 2664 | |
Peter Zijlstra | 50caf9c | 2021-02-24 11:42:08 +0100 | [diff] [blame] | 2665 | if (!pending->stop_pending) { |
| 2666 | p->migration_pending = NULL; |
| 2667 | complete = true; |
| 2668 | } |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2669 | } |
| 2670 | task_rq_unlock(rq, p, rf); |
| 2671 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2672 | if (complete) |
| 2673 | complete_all(&pending->done); |
| 2674 | } |
| 2675 | |
| 2676 | wait_for_completion(&pending->done); |
| 2677 | |
| 2678 | if (refcount_dec_and_test(&pending->refs)) |
Peter Zijlstra | 50caf9c | 2021-02-24 11:42:08 +0100 | [diff] [blame] | 2679 | wake_up_var(&pending->refs); /* No UaF, just an address */ |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2680 | |
Valentin Schneider | c777d84 | 2020-10-13 15:01:16 +0100 | [diff] [blame] | 2681 | /* |
| 2682 | * Block the original owner of &pending until all subsequent callers |
| 2683 | * have seen the completion and decremented the refcount |
| 2684 | */ |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2685 | wait_var_event(&my_pending.refs, !refcount_read(&my_pending.refs)); |
| 2686 | |
Peter Zijlstra | 50caf9c | 2021-02-24 11:42:08 +0100 | [diff] [blame] | 2687 | /* ARGH */ |
| 2688 | WARN_ON_ONCE(my_pending.stop_pending); |
| 2689 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2690 | return 0; |
| 2691 | } |
| 2692 | |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2693 | /* |
| 2694 | * Change a given task's CPU affinity. Migrate the thread to a |
| 2695 | * proper CPU and schedule it away if the CPU it's executing on |
| 2696 | * is removed from the allowed bitmask. |
| 2697 | * |
| 2698 | * NOTE: the caller must have a valid reference to the task, the |
| 2699 | * task must not exit() & deallocate itself prematurely. The |
| 2700 | * call is not atomic; no spinlocks may be held. |
| 2701 | */ |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 2702 | static int __set_cpus_allowed_ptr(struct task_struct *p, |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 2703 | const struct cpumask *new_mask, |
| 2704 | u32 flags) |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2705 | { |
Peter Zijlstra (Intel) | e9d867a | 2016-03-10 12:54:08 +0100 | [diff] [blame] | 2706 | const struct cpumask *cpu_valid_mask = cpu_active_mask; |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2707 | unsigned int dest_cpu; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 2708 | struct rq_flags rf; |
| 2709 | struct rq *rq; |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2710 | int ret = 0; |
| 2711 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 2712 | rq = task_rq_lock(p, &rf); |
Wanpeng Li | a499c3e | 2017-02-21 23:52:55 -0800 | [diff] [blame] | 2713 | update_rq_clock(rq); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2714 | |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2715 | if (p->flags & PF_KTHREAD || is_migration_disabled(p)) { |
Peter Zijlstra (Intel) | e9d867a | 2016-03-10 12:54:08 +0100 | [diff] [blame] | 2716 | /* |
Peter Zijlstra | 741ba80 | 2021-01-16 11:56:37 +0100 | [diff] [blame] | 2717 | * Kernel threads are allowed on online && !active CPUs, |
| 2718 | * however, during cpu-hot-unplug, even these might get pushed |
| 2719 | * away if not KTHREAD_IS_PER_CPU. |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2720 | * |
| 2721 | * Specifically, migration_disabled() tasks must not fail the |
| 2722 | * cpumask_any_and_distribute() pick below, esp. so on |
| 2723 | * SCA_MIGRATE_ENABLE, otherwise we'll not call |
| 2724 | * set_cpus_allowed_common() and actually reset p->cpus_ptr. |
Peter Zijlstra (Intel) | e9d867a | 2016-03-10 12:54:08 +0100 | [diff] [blame] | 2725 | */ |
| 2726 | cpu_valid_mask = cpu_online_mask; |
| 2727 | } |
| 2728 | |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 2729 | /* |
| 2730 | * Must re-check here, to close a race against __kthread_bind(), |
| 2731 | * sched_setaffinity() is not guaranteed to observe the flag. |
| 2732 | */ |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 2733 | if ((flags & SCA_CHECK) && (p->flags & PF_NO_SETAFFINITY)) { |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 2734 | ret = -EINVAL; |
| 2735 | goto out; |
| 2736 | } |
| 2737 | |
Valentin Schneider | 885b3ba | 2020-10-13 15:01:15 +0100 | [diff] [blame] | 2738 | if (!(flags & SCA_MIGRATE_ENABLE)) { |
| 2739 | if (cpumask_equal(&p->cpus_mask, new_mask)) |
| 2740 | goto out; |
| 2741 | |
| 2742 | if (WARN_ON_ONCE(p == current && |
| 2743 | is_migration_disabled(p) && |
| 2744 | !cpumask_test_cpu(task_cpu(p), new_mask))) { |
| 2745 | ret = -EBUSY; |
| 2746 | goto out; |
| 2747 | } |
| 2748 | } |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2749 | |
Paul Turner | 46a87b3 | 2020-03-10 18:01:13 -0700 | [diff] [blame] | 2750 | /* |
| 2751 | * Picking a ~random cpu helps in cases where we are changing affinity |
| 2752 | * for groups of tasks (ie. cpuset), so that load balancing is not |
| 2753 | * immediately required to distribute the tasks within their new mask. |
| 2754 | */ |
| 2755 | dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, new_mask); |
KeMeng Shi | 714e501 | 2019-09-16 06:53:28 +0000 | [diff] [blame] | 2756 | if (dest_cpu >= nr_cpu_ids) { |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2757 | ret = -EINVAL; |
| 2758 | goto out; |
| 2759 | } |
| 2760 | |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 2761 | __do_set_cpus_allowed(p, new_mask, flags); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2762 | |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 2763 | return affine_move_task(rq, p, &rf, dest_cpu, flags); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2764 | |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2765 | out: |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 2766 | task_rq_unlock(rq, p, &rf); |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2767 | |
| 2768 | return ret; |
| 2769 | } |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 2770 | |
| 2771 | int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) |
| 2772 | { |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 2773 | return __set_cpus_allowed_ptr(p, new_mask, 0); |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 2774 | } |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 2775 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); |
| 2776 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2777 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2778 | { |
Peter Zijlstra | e291200 | 2009-12-16 18:04:36 +0100 | [diff] [blame] | 2779 | #ifdef CONFIG_SCHED_DEBUG |
| 2780 | /* |
| 2781 | * We should never call set_task_cpu() on a blocked task, |
| 2782 | * ttwu() will sort out the placement. |
| 2783 | */ |
Peter Zijlstra | 077614e | 2009-12-17 13:16:31 +0100 | [diff] [blame] | 2784 | WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING && |
Oleg Nesterov | e2336f6 | 2014-10-08 20:33:48 +0200 | [diff] [blame] | 2785 | !p->on_rq); |
Peter Zijlstra | 0122ec5 | 2011-04-05 17:23:51 +0200 | [diff] [blame] | 2786 | |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 2787 | /* |
| 2788 | * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING, |
| 2789 | * because schedstat_wait_{start,end} rebase migrating task's wait_start |
| 2790 | * time relying on p->on_rq. |
| 2791 | */ |
| 2792 | WARN_ON_ONCE(p->state == TASK_RUNNING && |
| 2793 | p->sched_class == &fair_sched_class && |
| 2794 | (p->on_rq && !task_on_rq_migrating(p))); |
| 2795 | |
Peter Zijlstra | 0122ec5 | 2011-04-05 17:23:51 +0200 | [diff] [blame] | 2796 | #ifdef CONFIG_LOCKDEP |
Peter Zijlstra | 6c6c54e | 2011-06-03 17:37:07 +0200 | [diff] [blame] | 2797 | /* |
| 2798 | * The caller should hold either p->pi_lock or rq->lock, when changing |
| 2799 | * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks. |
| 2800 | * |
| 2801 | * sched_move_task() holds both and thus holding either pins the cgroup, |
Peter Zijlstra | 8323f26 | 2012-06-22 13:36:05 +0200 | [diff] [blame] | 2802 | * see task_group(). |
Peter Zijlstra | 6c6c54e | 2011-06-03 17:37:07 +0200 | [diff] [blame] | 2803 | * |
| 2804 | * Furthermore, all task_rq users should acquire both locks, see |
| 2805 | * task_rq_lock(). |
| 2806 | */ |
Peter Zijlstra | 0122ec5 | 2011-04-05 17:23:51 +0200 | [diff] [blame] | 2807 | WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) || |
Peter Zijlstra | 9ef7e7e | 2021-03-03 16:45:41 +0100 | [diff] [blame] | 2808 | lockdep_is_held(__rq_lockp(task_rq(p))))); |
Peter Zijlstra | 0122ec5 | 2011-04-05 17:23:51 +0200 | [diff] [blame] | 2809 | #endif |
Peter Zijlstra | 4ff9083 | 2017-09-07 17:03:52 +0200 | [diff] [blame] | 2810 | /* |
| 2811 | * Clearly, migrating tasks to offline CPUs is a fairly daft thing. |
| 2812 | */ |
| 2813 | WARN_ON_ONCE(!cpu_online(new_cpu)); |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 2814 | |
| 2815 | WARN_ON_ONCE(is_migration_disabled(p)); |
Peter Zijlstra | e291200 | 2009-12-16 18:04:36 +0100 | [diff] [blame] | 2816 | #endif |
| 2817 | |
Mathieu Desnoyers | de1d728 | 2009-05-05 16:49:59 +0800 | [diff] [blame] | 2818 | trace_sched_migrate_task(p, new_cpu); |
Peter Zijlstra | cbc34ed | 2008-12-10 08:08:22 +0100 | [diff] [blame] | 2819 | |
Peter Zijlstra | 0c69774 | 2009-12-22 15:43:19 +0100 | [diff] [blame] | 2820 | if (task_cpu(p) != new_cpu) { |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2821 | if (p->sched_class->migrate_task_rq) |
Srikar Dronamraju | 1327237a5 | 2018-09-21 23:18:57 +0530 | [diff] [blame] | 2822 | p->sched_class->migrate_task_rq(p, new_cpu); |
Peter Zijlstra | 0c69774 | 2009-12-22 15:43:19 +0100 | [diff] [blame] | 2823 | p->se.nr_migrations++; |
Mathieu Desnoyers | d7822b1 | 2018-06-02 08:43:54 -0400 | [diff] [blame] | 2824 | rseq_migrate(p); |
Peter Zijlstra | ff303e6 | 2015-04-17 20:05:30 +0200 | [diff] [blame] | 2825 | perf_event_task_migrate(p); |
Peter Zijlstra | 0c69774 | 2009-12-22 15:43:19 +0100 | [diff] [blame] | 2826 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 2827 | |
| 2828 | __set_task_cpu(p, new_cpu); |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2829 | } |
| 2830 | |
Srikar Dronamraju | 0ad4e3d | 2018-06-20 22:32:50 +0530 | [diff] [blame] | 2831 | #ifdef CONFIG_NUMA_BALANCING |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2832 | static void __migrate_swap_task(struct task_struct *p, int cpu) |
| 2833 | { |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2834 | if (task_on_rq_queued(p)) { |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2835 | struct rq *src_rq, *dst_rq; |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2836 | struct rq_flags srf, drf; |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2837 | |
| 2838 | src_rq = task_rq(p); |
| 2839 | dst_rq = cpu_rq(cpu); |
| 2840 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2841 | rq_pin_lock(src_rq, &srf); |
| 2842 | rq_pin_lock(dst_rq, &drf); |
| 2843 | |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2844 | deactivate_task(src_rq, p, 0); |
| 2845 | set_task_cpu(p, cpu); |
| 2846 | activate_task(dst_rq, p, 0); |
| 2847 | check_preempt_curr(dst_rq, p, 0); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 2848 | |
| 2849 | rq_unpin_lock(dst_rq, &drf); |
| 2850 | rq_unpin_lock(src_rq, &srf); |
| 2851 | |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2852 | } else { |
| 2853 | /* |
| 2854 | * Task isn't running anymore; make it appear like we migrated |
| 2855 | * it before it went to sleep. This means on wakeup we make the |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 2856 | * previous CPU our target instead of where it really is. |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2857 | */ |
| 2858 | p->wake_cpu = cpu; |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | struct migration_swap_arg { |
| 2863 | struct task_struct *src_task, *dst_task; |
| 2864 | int src_cpu, dst_cpu; |
| 2865 | }; |
| 2866 | |
| 2867 | static int migrate_swap_stop(void *data) |
| 2868 | { |
| 2869 | struct migration_swap_arg *arg = data; |
| 2870 | struct rq *src_rq, *dst_rq; |
| 2871 | int ret = -EAGAIN; |
| 2872 | |
Peter Zijlstra | 62694cd | 2015-10-09 18:36:29 +0200 | [diff] [blame] | 2873 | if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu)) |
| 2874 | return -EAGAIN; |
| 2875 | |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2876 | src_rq = cpu_rq(arg->src_cpu); |
| 2877 | dst_rq = cpu_rq(arg->dst_cpu); |
| 2878 | |
Peter Zijlstra | 7460231 | 2013-10-10 20:17:22 +0200 | [diff] [blame] | 2879 | double_raw_lock(&arg->src_task->pi_lock, |
| 2880 | &arg->dst_task->pi_lock); |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2881 | double_rq_lock(src_rq, dst_rq); |
Peter Zijlstra | 62694cd | 2015-10-09 18:36:29 +0200 | [diff] [blame] | 2882 | |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2883 | if (task_cpu(arg->dst_task) != arg->dst_cpu) |
| 2884 | goto unlock; |
| 2885 | |
| 2886 | if (task_cpu(arg->src_task) != arg->src_cpu) |
| 2887 | goto unlock; |
| 2888 | |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 2889 | if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr)) |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2890 | goto unlock; |
| 2891 | |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 2892 | if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr)) |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2893 | goto unlock; |
| 2894 | |
| 2895 | __migrate_swap_task(arg->src_task, arg->dst_cpu); |
| 2896 | __migrate_swap_task(arg->dst_task, arg->src_cpu); |
| 2897 | |
| 2898 | ret = 0; |
| 2899 | |
| 2900 | unlock: |
| 2901 | double_rq_unlock(src_rq, dst_rq); |
Peter Zijlstra | 7460231 | 2013-10-10 20:17:22 +0200 | [diff] [blame] | 2902 | raw_spin_unlock(&arg->dst_task->pi_lock); |
| 2903 | raw_spin_unlock(&arg->src_task->pi_lock); |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2904 | |
| 2905 | return ret; |
| 2906 | } |
| 2907 | |
| 2908 | /* |
| 2909 | * Cross migrate two tasks |
| 2910 | */ |
Srikar Dronamraju | 0ad4e3d | 2018-06-20 22:32:50 +0530 | [diff] [blame] | 2911 | int migrate_swap(struct task_struct *cur, struct task_struct *p, |
| 2912 | int target_cpu, int curr_cpu) |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2913 | { |
| 2914 | struct migration_swap_arg arg; |
| 2915 | int ret = -EINVAL; |
| 2916 | |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2917 | arg = (struct migration_swap_arg){ |
| 2918 | .src_task = cur, |
Srikar Dronamraju | 0ad4e3d | 2018-06-20 22:32:50 +0530 | [diff] [blame] | 2919 | .src_cpu = curr_cpu, |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2920 | .dst_task = p, |
Srikar Dronamraju | 0ad4e3d | 2018-06-20 22:32:50 +0530 | [diff] [blame] | 2921 | .dst_cpu = target_cpu, |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2922 | }; |
| 2923 | |
| 2924 | if (arg.src_cpu == arg.dst_cpu) |
| 2925 | goto out; |
| 2926 | |
Peter Zijlstra | 6acce3e | 2013-10-11 14:38:20 +0200 | [diff] [blame] | 2927 | /* |
| 2928 | * These three tests are all lockless; this is OK since all of them |
| 2929 | * will be re-checked with proper locks held further down the line. |
| 2930 | */ |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2931 | if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu)) |
| 2932 | goto out; |
| 2933 | |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 2934 | if (!cpumask_test_cpu(arg.dst_cpu, arg.src_task->cpus_ptr)) |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2935 | goto out; |
| 2936 | |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 2937 | if (!cpumask_test_cpu(arg.src_cpu, arg.dst_task->cpus_ptr)) |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2938 | goto out; |
| 2939 | |
Mel Gorman | 286549d | 2014-01-21 15:51:03 -0800 | [diff] [blame] | 2940 | trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu); |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2941 | ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg); |
| 2942 | |
| 2943 | out: |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2944 | return ret; |
| 2945 | } |
Srikar Dronamraju | 0ad4e3d | 2018-06-20 22:32:50 +0530 | [diff] [blame] | 2946 | #endif /* CONFIG_NUMA_BALANCING */ |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 2947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | * wait_task_inactive - wait for a thread to unschedule. |
| 2950 | * |
Roland McGrath | 85ba2d8 | 2008-07-25 19:45:58 -0700 | [diff] [blame] | 2951 | * If @match_state is nonzero, it's the @p->state value just checked and |
| 2952 | * not expected to change. If it changes, i.e. @p might have woken up, |
| 2953 | * then return zero. When we succeed in waiting for @p to be off its CPU, |
| 2954 | * we return a positive number (its total switch count). If a second call |
| 2955 | * a short while later returns the same number, the caller can be sure that |
| 2956 | * @p has remained unscheduled the whole time. |
| 2957 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | * The caller must ensure that the task *will* unschedule sometime soon, |
| 2959 | * else this function might spin for a *long* time. This function can't |
| 2960 | * be called with interrupts off, or it may introduce deadlock with |
| 2961 | * smp_call_function() if an IPI is sent by the same process we are |
| 2962 | * waiting to become inactive. |
| 2963 | */ |
Roland McGrath | 85ba2d8 | 2008-07-25 19:45:58 -0700 | [diff] [blame] | 2964 | unsigned long wait_task_inactive(struct task_struct *p, long match_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | { |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2966 | int running, queued; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 2967 | struct rq_flags rf; |
Roland McGrath | 85ba2d8 | 2008-07-25 19:45:58 -0700 | [diff] [blame] | 2968 | unsigned long ncsw; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2969 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2971 | for (;;) { |
| 2972 | /* |
| 2973 | * We do the initial early heuristics without holding |
| 2974 | * any task-queue locks at all. We'll only try to get |
| 2975 | * the runqueue lock when things look like they will |
| 2976 | * work out! |
| 2977 | */ |
| 2978 | rq = task_rq(p); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 2979 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2980 | /* |
| 2981 | * If the task is actively running on another CPU |
| 2982 | * still, just relax and busy-wait without holding |
| 2983 | * any locks. |
| 2984 | * |
| 2985 | * NOTE! Since we don't hold any locks, it's not |
| 2986 | * even sure that "rq" stays as the right runqueue! |
| 2987 | * But we don't care, since "task_running()" will |
| 2988 | * return false if the runqueue has changed and p |
| 2989 | * is actually now running somewhere else! |
| 2990 | */ |
Roland McGrath | 85ba2d8 | 2008-07-25 19:45:58 -0700 | [diff] [blame] | 2991 | while (task_running(rq, p)) { |
| 2992 | if (match_state && unlikely(p->state != match_state)) |
| 2993 | return 0; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2994 | cpu_relax(); |
Roland McGrath | 85ba2d8 | 2008-07-25 19:45:58 -0700 | [diff] [blame] | 2995 | } |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 2996 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 2997 | /* |
| 2998 | * Ok, time to look more closely! We need the rq |
| 2999 | * lock now, to be *sure*. If we're wrong, we'll |
| 3000 | * just go back and repeat. |
| 3001 | */ |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 3002 | rq = task_rq_lock(p, &rf); |
Peter Zijlstra | 27a9da6 | 2010-05-04 20:36:56 +0200 | [diff] [blame] | 3003 | trace_sched_wait_task(p); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3004 | running = task_running(rq, p); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 3005 | queued = task_on_rq_queued(p); |
Roland McGrath | 85ba2d8 | 2008-07-25 19:45:58 -0700 | [diff] [blame] | 3006 | ncsw = 0; |
Oleg Nesterov | f31e11d | 2008-08-20 16:54:44 -0700 | [diff] [blame] | 3007 | if (!match_state || p->state == match_state) |
Oleg Nesterov | 93dcf55 | 2008-08-20 16:54:44 -0700 | [diff] [blame] | 3008 | ncsw = p->nvcsw | LONG_MIN; /* sets MSB */ |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 3009 | task_rq_unlock(rq, p, &rf); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 3010 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3011 | /* |
Roland McGrath | 85ba2d8 | 2008-07-25 19:45:58 -0700 | [diff] [blame] | 3012 | * If it changed from the expected state, bail out now. |
| 3013 | */ |
| 3014 | if (unlikely(!ncsw)) |
| 3015 | break; |
| 3016 | |
| 3017 | /* |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3018 | * Was it really running after all now that we |
| 3019 | * checked with the proper locks actually held? |
| 3020 | * |
| 3021 | * Oops. Go back and try again.. |
| 3022 | */ |
| 3023 | if (unlikely(running)) { |
| 3024 | cpu_relax(); |
| 3025 | continue; |
| 3026 | } |
| 3027 | |
| 3028 | /* |
| 3029 | * It's not enough that it's not actively running, |
| 3030 | * it must be off the runqueue _entirely_, and not |
| 3031 | * preempted! |
| 3032 | * |
Luis Henriques | 80dd99b | 2009-03-16 19:58:09 +0000 | [diff] [blame] | 3033 | * So if it was still runnable (but just not actively |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3034 | * running right now), it's preempted, and we should |
| 3035 | * yield - it could be a while. |
| 3036 | */ |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 3037 | if (unlikely(queued)) { |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 3038 | ktime_t to = NSEC_PER_SEC / HZ; |
Thomas Gleixner | 8eb90c3 | 2011-02-23 23:52:21 +0000 | [diff] [blame] | 3039 | |
| 3040 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 3041 | schedule_hrtimeout(&to, HRTIMER_MODE_REL); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3042 | continue; |
| 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | * Ahh, all good. It wasn't running, and it wasn't |
| 3047 | * runnable, which means that it will never become |
| 3048 | * running in the future either. We're all done! |
| 3049 | */ |
| 3050 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | } |
Roland McGrath | 85ba2d8 | 2008-07-25 19:45:58 -0700 | [diff] [blame] | 3052 | |
| 3053 | return ncsw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | /*** |
| 3057 | * kick_process - kick a running thread to enter/exit the kernel |
| 3058 | * @p: the to-be-kicked thread |
| 3059 | * |
| 3060 | * Cause a process which is running on another CPU to enter |
| 3061 | * kernel-mode, without any delay. (to get signals handled.) |
| 3062 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3063 | * NOTE: this function doesn't have to take the runqueue lock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | * because all it wants to ensure is that the remote task enters |
| 3065 | * the kernel. If the IPI races and the task has been migrated |
| 3066 | * to another CPU then no harm is done and the purpose has been |
| 3067 | * achieved as well. |
| 3068 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3069 | void kick_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | { |
| 3071 | int cpu; |
| 3072 | |
| 3073 | preempt_disable(); |
| 3074 | cpu = task_cpu(p); |
| 3075 | if ((cpu != smp_processor_id()) && task_curr(p)) |
| 3076 | smp_send_reschedule(cpu); |
| 3077 | preempt_enable(); |
| 3078 | } |
Rusty Russell | b43e352 | 2009-06-12 22:27:00 -0600 | [diff] [blame] | 3079 | EXPORT_SYMBOL_GPL(kick_process); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | |
Oleg Nesterov | 30da688 | 2010-03-15 10:10:19 +0100 | [diff] [blame] | 3081 | /* |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 3082 | * ->cpus_ptr is protected by both rq->lock and p->pi_lock |
Peter Zijlstra (Intel) | e9d867a | 2016-03-10 12:54:08 +0100 | [diff] [blame] | 3083 | * |
| 3084 | * A few notes on cpu_active vs cpu_online: |
| 3085 | * |
| 3086 | * - cpu_active must be a subset of cpu_online |
| 3087 | * |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 3088 | * - on CPU-up we allow per-CPU kthreads on the online && !active CPU, |
Peter Zijlstra (Intel) | e9d867a | 2016-03-10 12:54:08 +0100 | [diff] [blame] | 3089 | * see __set_cpus_allowed_ptr(). At this point the newly online |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3090 | * CPU isn't yet part of the sched domains, and balancing will not |
Peter Zijlstra (Intel) | e9d867a | 2016-03-10 12:54:08 +0100 | [diff] [blame] | 3091 | * see it. |
| 3092 | * |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3093 | * - on CPU-down we clear cpu_active() to mask the sched domains and |
Peter Zijlstra (Intel) | e9d867a | 2016-03-10 12:54:08 +0100 | [diff] [blame] | 3094 | * avoid the load balancer to place new tasks on the to be removed |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3095 | * CPU. Existing tasks will remain running there and will be taken |
Peter Zijlstra (Intel) | e9d867a | 2016-03-10 12:54:08 +0100 | [diff] [blame] | 3096 | * off. |
| 3097 | * |
| 3098 | * This means that fallback selection must not select !active CPUs. |
| 3099 | * And can assume that any active CPU must be online. Conversely |
| 3100 | * select_task_rq() below may allow selection of !active CPUs in order |
| 3101 | * to satisfy the above rules. |
Oleg Nesterov | 30da688 | 2010-03-15 10:10:19 +0100 | [diff] [blame] | 3102 | */ |
Peter Zijlstra | 5da9a0f | 2009-12-16 18:04:38 +0100 | [diff] [blame] | 3103 | static int select_fallback_rq(int cpu, struct task_struct *p) |
| 3104 | { |
Tang Chen | aa00d89 | 2013-02-22 16:33:33 -0800 | [diff] [blame] | 3105 | int nid = cpu_to_node(cpu); |
| 3106 | const struct cpumask *nodemask = NULL; |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 3107 | enum { cpuset, possible, fail } state = cpuset; |
| 3108 | int dest_cpu; |
Peter Zijlstra | 5da9a0f | 2009-12-16 18:04:38 +0100 | [diff] [blame] | 3109 | |
Tang Chen | aa00d89 | 2013-02-22 16:33:33 -0800 | [diff] [blame] | 3110 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3111 | * If the node that the CPU is on has been offlined, cpu_to_node() |
| 3112 | * will return -1. There is no CPU on the node, and we should |
| 3113 | * select the CPU on the other node. |
Tang Chen | aa00d89 | 2013-02-22 16:33:33 -0800 | [diff] [blame] | 3114 | */ |
| 3115 | if (nid != -1) { |
| 3116 | nodemask = cpumask_of_node(nid); |
| 3117 | |
| 3118 | /* Look for allowed, online CPU in same node. */ |
| 3119 | for_each_cpu(dest_cpu, nodemask) { |
Tang Chen | aa00d89 | 2013-02-22 16:33:33 -0800 | [diff] [blame] | 3120 | if (!cpu_active(dest_cpu)) |
| 3121 | continue; |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 3122 | if (cpumask_test_cpu(dest_cpu, p->cpus_ptr)) |
Tang Chen | aa00d89 | 2013-02-22 16:33:33 -0800 | [diff] [blame] | 3123 | return dest_cpu; |
| 3124 | } |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 3125 | } |
Peter Zijlstra | 5da9a0f | 2009-12-16 18:04:38 +0100 | [diff] [blame] | 3126 | |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 3127 | for (;;) { |
| 3128 | /* Any allowed, online CPU? */ |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 3129 | for_each_cpu(dest_cpu, p->cpus_ptr) { |
Peter Zijlstra | 175f0e2 | 2017-07-25 18:58:21 +0200 | [diff] [blame] | 3130 | if (!is_cpu_allowed(p, dest_cpu)) |
Tejun Heo | feb245e | 2016-06-16 15:35:04 -0400 | [diff] [blame] | 3131 | continue; |
Peter Zijlstra | 175f0e2 | 2017-07-25 18:58:21 +0200 | [diff] [blame] | 3132 | |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 3133 | goto out; |
| 3134 | } |
Peter Zijlstra | 5da9a0f | 2009-12-16 18:04:38 +0100 | [diff] [blame] | 3135 | |
Oleg Nesterov | e73e85f | 2015-10-10 20:53:15 +0200 | [diff] [blame] | 3136 | /* No more Mr. Nice Guy. */ |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 3137 | switch (state) { |
| 3138 | case cpuset: |
Oleg Nesterov | e73e85f | 2015-10-10 20:53:15 +0200 | [diff] [blame] | 3139 | if (IS_ENABLED(CONFIG_CPUSETS)) { |
| 3140 | cpuset_cpus_allowed_fallback(p); |
| 3141 | state = possible; |
| 3142 | break; |
| 3143 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3144 | fallthrough; |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 3145 | case possible: |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 3146 | /* |
| 3147 | * XXX When called from select_task_rq() we only |
| 3148 | * hold p->pi_lock and again violate locking order. |
| 3149 | * |
| 3150 | * More yuck to audit. |
| 3151 | */ |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 3152 | do_set_cpus_allowed(p, cpu_possible_mask); |
| 3153 | state = fail; |
| 3154 | break; |
| 3155 | |
| 3156 | case fail: |
| 3157 | BUG(); |
| 3158 | break; |
| 3159 | } |
| 3160 | } |
| 3161 | |
| 3162 | out: |
| 3163 | if (state != cpuset) { |
| 3164 | /* |
| 3165 | * Don't tell them about moving exiting tasks or |
| 3166 | * kernel threads (both mm NULL), since they never |
| 3167 | * leave kernel. |
| 3168 | */ |
| 3169 | if (p->mm && printk_ratelimit()) { |
John Stultz | aac74dc | 2014-06-04 16:11:40 -0700 | [diff] [blame] | 3170 | printk_deferred("process %d (%s) no longer affine to cpu%d\n", |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 3171 | task_pid_nr(p), p->comm, cpu); |
| 3172 | } |
Peter Zijlstra | 5da9a0f | 2009-12-16 18:04:38 +0100 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | return dest_cpu; |
| 3176 | } |
| 3177 | |
Peter Zijlstra | e291200 | 2009-12-16 18:04:36 +0100 | [diff] [blame] | 3178 | /* |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 3179 | * The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable. |
Peter Zijlstra | e291200 | 2009-12-16 18:04:36 +0100 | [diff] [blame] | 3180 | */ |
Peter Zijlstra | 970b13b | 2009-11-25 13:31:39 +0100 | [diff] [blame] | 3181 | static inline |
Valentin Schneider | 3aef155 | 2020-11-02 18:45:13 +0000 | [diff] [blame] | 3182 | int select_task_rq(struct task_struct *p, int cpu, int wake_flags) |
Peter Zijlstra | 970b13b | 2009-11-25 13:31:39 +0100 | [diff] [blame] | 3183 | { |
Peter Zijlstra | cbce1a6 | 2015-06-11 14:46:54 +0200 | [diff] [blame] | 3184 | lockdep_assert_held(&p->pi_lock); |
| 3185 | |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 3186 | if (p->nr_cpus_allowed > 1 && !is_migration_disabled(p)) |
Valentin Schneider | 3aef155 | 2020-11-02 18:45:13 +0000 | [diff] [blame] | 3187 | cpu = p->sched_class->select_task_rq(p, cpu, wake_flags); |
Peter Zijlstra (Intel) | e9d867a | 2016-03-10 12:54:08 +0100 | [diff] [blame] | 3188 | else |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 3189 | cpu = cpumask_any(p->cpus_ptr); |
Peter Zijlstra | e291200 | 2009-12-16 18:04:36 +0100 | [diff] [blame] | 3190 | |
| 3191 | /* |
| 3192 | * In order not to call set_task_cpu() on a blocking task we need |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 3193 | * to rely on ttwu() to place the task on a valid ->cpus_ptr |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3194 | * CPU. |
Peter Zijlstra | e291200 | 2009-12-16 18:04:36 +0100 | [diff] [blame] | 3195 | * |
| 3196 | * Since this is common to all placement strategies, this lives here. |
| 3197 | * |
| 3198 | * [ this allows ->select_task() to simply return task_cpu(p) and |
| 3199 | * not worry about this generic constraint ] |
| 3200 | */ |
Paul Burton | 7af443e | 2018-05-26 08:46:47 -0700 | [diff] [blame] | 3201 | if (unlikely(!is_cpu_allowed(p, cpu))) |
Peter Zijlstra | 5da9a0f | 2009-12-16 18:04:38 +0100 | [diff] [blame] | 3202 | cpu = select_fallback_rq(task_cpu(p), p); |
Peter Zijlstra | e291200 | 2009-12-16 18:04:36 +0100 | [diff] [blame] | 3203 | |
| 3204 | return cpu; |
Peter Zijlstra | 970b13b | 2009-11-25 13:31:39 +0100 | [diff] [blame] | 3205 | } |
Mike Galbraith | 09a40af | 2010-04-15 07:29:59 +0200 | [diff] [blame] | 3206 | |
Nicolas Pitre | f5832c1 | 2017-05-29 17:02:57 -0400 | [diff] [blame] | 3207 | void sched_set_stop_task(int cpu, struct task_struct *stop) |
| 3208 | { |
Peter Zijlstra | ded467d | 2020-10-01 16:13:01 +0200 | [diff] [blame] | 3209 | static struct lock_class_key stop_pi_lock; |
Nicolas Pitre | f5832c1 | 2017-05-29 17:02:57 -0400 | [diff] [blame] | 3210 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; |
| 3211 | struct task_struct *old_stop = cpu_rq(cpu)->stop; |
| 3212 | |
| 3213 | if (stop) { |
| 3214 | /* |
| 3215 | * Make it appear like a SCHED_FIFO task, its something |
| 3216 | * userspace knows about and won't get confused about. |
| 3217 | * |
| 3218 | * Also, it will make PI more or less work without too |
| 3219 | * much confusion -- but then, stop work should not |
| 3220 | * rely on PI working anyway. |
| 3221 | */ |
| 3222 | sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m); |
| 3223 | |
| 3224 | stop->sched_class = &stop_sched_class; |
Peter Zijlstra | ded467d | 2020-10-01 16:13:01 +0200 | [diff] [blame] | 3225 | |
| 3226 | /* |
| 3227 | * The PI code calls rt_mutex_setprio() with ->pi_lock held to |
| 3228 | * adjust the effective priority of a task. As a result, |
| 3229 | * rt_mutex_setprio() can trigger (RT) balancing operations, |
| 3230 | * which can then trigger wakeups of the stop thread to push |
| 3231 | * around the current task. |
| 3232 | * |
| 3233 | * The stop task itself will never be part of the PI-chain, it |
| 3234 | * never blocks, therefore that ->pi_lock recursion is safe. |
| 3235 | * Tell lockdep about this by placing the stop->pi_lock in its |
| 3236 | * own class. |
| 3237 | */ |
| 3238 | lockdep_set_class(&stop->pi_lock, &stop_pi_lock); |
Nicolas Pitre | f5832c1 | 2017-05-29 17:02:57 -0400 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | cpu_rq(cpu)->stop = stop; |
| 3242 | |
| 3243 | if (old_stop) { |
| 3244 | /* |
| 3245 | * Reset it back to a normal scheduling class so that |
| 3246 | * it can die in pieces. |
| 3247 | */ |
| 3248 | old_stop->sched_class = &rt_sched_class; |
| 3249 | } |
| 3250 | } |
| 3251 | |
Thomas Gleixner | 74d862b6 | 2020-11-18 20:48:42 +0100 | [diff] [blame] | 3252 | #else /* CONFIG_SMP */ |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 3253 | |
| 3254 | static inline int __set_cpus_allowed_ptr(struct task_struct *p, |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 3255 | const struct cpumask *new_mask, |
| 3256 | u32 flags) |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 3257 | { |
| 3258 | return set_cpus_allowed_ptr(p, new_mask); |
| 3259 | } |
| 3260 | |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 3261 | static inline void migrate_disable_switch(struct rq *rq, struct task_struct *p) { } |
| 3262 | |
Thomas Gleixner | 3015ef4 | 2020-08-26 14:08:10 +0200 | [diff] [blame] | 3263 | static inline bool rq_has_pinned_tasks(struct rq *rq) |
| 3264 | { |
| 3265 | return false; |
| 3266 | } |
| 3267 | |
Thomas Gleixner | 74d862b6 | 2020-11-18 20:48:42 +0100 | [diff] [blame] | 3268 | #endif /* !CONFIG_SMP */ |
Peter Zijlstra | 970b13b | 2009-11-25 13:31:39 +0100 | [diff] [blame] | 3269 | |
Peter Zijlstra | d7c01d2 | 2011-04-05 17:23:43 +0200 | [diff] [blame] | 3270 | static void |
Peter Zijlstra | b84cb5d | 2011-04-05 17:23:55 +0200 | [diff] [blame] | 3271 | ttwu_stat(struct task_struct *p, int cpu, int wake_flags) |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3272 | { |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3273 | struct rq *rq; |
| 3274 | |
| 3275 | if (!schedstat_enabled()) |
| 3276 | return; |
| 3277 | |
| 3278 | rq = this_rq(); |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3279 | |
Peter Zijlstra | d7c01d2 | 2011-04-05 17:23:43 +0200 | [diff] [blame] | 3280 | #ifdef CONFIG_SMP |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3281 | if (cpu == rq->cpu) { |
Peter Zijlstra | b85c8b7 | 2018-01-16 20:51:06 +0100 | [diff] [blame] | 3282 | __schedstat_inc(rq->ttwu_local); |
| 3283 | __schedstat_inc(p->se.statistics.nr_wakeups_local); |
Peter Zijlstra | d7c01d2 | 2011-04-05 17:23:43 +0200 | [diff] [blame] | 3284 | } else { |
| 3285 | struct sched_domain *sd; |
| 3286 | |
Peter Zijlstra | b85c8b7 | 2018-01-16 20:51:06 +0100 | [diff] [blame] | 3287 | __schedstat_inc(p->se.statistics.nr_wakeups_remote); |
Peter Zijlstra | 057f3fa | 2011-04-18 11:24:34 +0200 | [diff] [blame] | 3288 | rcu_read_lock(); |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3289 | for_each_domain(rq->cpu, sd) { |
Peter Zijlstra | d7c01d2 | 2011-04-05 17:23:43 +0200 | [diff] [blame] | 3290 | if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { |
Peter Zijlstra | b85c8b7 | 2018-01-16 20:51:06 +0100 | [diff] [blame] | 3291 | __schedstat_inc(sd->ttwu_wake_remote); |
Peter Zijlstra | d7c01d2 | 2011-04-05 17:23:43 +0200 | [diff] [blame] | 3292 | break; |
| 3293 | } |
| 3294 | } |
Peter Zijlstra | 057f3fa | 2011-04-18 11:24:34 +0200 | [diff] [blame] | 3295 | rcu_read_unlock(); |
Peter Zijlstra | d7c01d2 | 2011-04-05 17:23:43 +0200 | [diff] [blame] | 3296 | } |
Peter Zijlstra | f339b9d | 2011-05-31 10:49:20 +0200 | [diff] [blame] | 3297 | |
| 3298 | if (wake_flags & WF_MIGRATED) |
Peter Zijlstra | b85c8b7 | 2018-01-16 20:51:06 +0100 | [diff] [blame] | 3299 | __schedstat_inc(p->se.statistics.nr_wakeups_migrate); |
Peter Zijlstra | d7c01d2 | 2011-04-05 17:23:43 +0200 | [diff] [blame] | 3300 | #endif /* CONFIG_SMP */ |
| 3301 | |
Peter Zijlstra | b85c8b7 | 2018-01-16 20:51:06 +0100 | [diff] [blame] | 3302 | __schedstat_inc(rq->ttwu_count); |
| 3303 | __schedstat_inc(p->se.statistics.nr_wakeups); |
Peter Zijlstra | d7c01d2 | 2011-04-05 17:23:43 +0200 | [diff] [blame] | 3304 | |
| 3305 | if (wake_flags & WF_SYNC) |
Peter Zijlstra | b85c8b7 | 2018-01-16 20:51:06 +0100 | [diff] [blame] | 3306 | __schedstat_inc(p->se.statistics.nr_wakeups_sync); |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3307 | } |
| 3308 | |
Peter Zijlstra | 23f41ee | 2011-04-05 17:23:56 +0200 | [diff] [blame] | 3309 | /* |
| 3310 | * Mark the task runnable and perform wakeup-preemption. |
| 3311 | */ |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3312 | static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags, |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 3313 | struct rq_flags *rf) |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3314 | { |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3315 | check_preempt_curr(rq, p, wake_flags); |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3316 | p->state = TASK_RUNNING; |
Peter Zijlstra | fbd705a | 2015-06-09 11:13:36 +0200 | [diff] [blame] | 3317 | trace_sched_wakeup(p); |
| 3318 | |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3319 | #ifdef CONFIG_SMP |
Peter Zijlstra | 4c9a4bc | 2015-06-11 14:46:39 +0200 | [diff] [blame] | 3320 | if (p->sched_class->task_woken) { |
| 3321 | /* |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 3322 | * Our task @p is fully woken up and running; so it's safe to |
Peter Zijlstra | cbce1a6 | 2015-06-11 14:46:54 +0200 | [diff] [blame] | 3323 | * drop the rq->lock, hereafter rq is only used for statistics. |
Peter Zijlstra | 4c9a4bc | 2015-06-11 14:46:39 +0200 | [diff] [blame] | 3324 | */ |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 3325 | rq_unpin_lock(rq, rf); |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3326 | p->sched_class->task_woken(rq, p); |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 3327 | rq_repin_lock(rq, rf); |
Peter Zijlstra | 4c9a4bc | 2015-06-11 14:46:39 +0200 | [diff] [blame] | 3328 | } |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3329 | |
Steven Rostedt | e69c634 | 2010-12-06 17:10:31 -0500 | [diff] [blame] | 3330 | if (rq->idle_stamp) { |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 3331 | u64 delta = rq_clock(rq) - rq->idle_stamp; |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 3332 | u64 max = 2*rq->max_idle_balance_cost; |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3333 | |
Jason Low | abfafa5 | 2013-09-13 11:26:51 -0700 | [diff] [blame] | 3334 | update_avg(&rq->avg_idle, delta); |
| 3335 | |
| 3336 | if (rq->avg_idle > max) |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3337 | rq->avg_idle = max; |
Jason Low | abfafa5 | 2013-09-13 11:26:51 -0700 | [diff] [blame] | 3338 | |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3339 | rq->idle_stamp = 0; |
| 3340 | } |
| 3341 | #endif |
| 3342 | } |
| 3343 | |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3344 | static void |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3345 | ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 3346 | struct rq_flags *rf) |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3347 | { |
Peter Zijlstra | 77558e4 | 2017-02-21 14:36:23 +0100 | [diff] [blame] | 3348 | int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; |
Peter Zijlstra | b5179ac | 2016-05-11 16:10:34 +0200 | [diff] [blame] | 3349 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 3350 | lockdep_assert_rq_held(rq); |
Peter Zijlstra | cbce1a6 | 2015-06-11 14:46:54 +0200 | [diff] [blame] | 3351 | |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3352 | if (p->sched_contributes_to_load) |
| 3353 | rq->nr_uninterruptible--; |
Peter Zijlstra | b5179ac | 2016-05-11 16:10:34 +0200 | [diff] [blame] | 3354 | |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 3355 | #ifdef CONFIG_SMP |
Peter Zijlstra | b5179ac | 2016-05-11 16:10:34 +0200 | [diff] [blame] | 3356 | if (wake_flags & WF_MIGRATED) |
Peter Zijlstra | 59efa0b | 2016-05-10 18:24:37 +0200 | [diff] [blame] | 3357 | en_flags |= ENQUEUE_MIGRATED; |
Peter Zijlstra | ec618b8 | 2020-09-24 13:50:42 +0200 | [diff] [blame] | 3358 | else |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3359 | #endif |
Peter Zijlstra | ec618b8 | 2020-09-24 13:50:42 +0200 | [diff] [blame] | 3360 | if (p->in_iowait) { |
| 3361 | delayacct_blkio_end(p); |
| 3362 | atomic_dec(&task_rq(p)->nr_iowait); |
| 3363 | } |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3364 | |
Peter Zijlstra | 1b174a2 | 2019-04-09 09:53:13 +0200 | [diff] [blame] | 3365 | activate_task(rq, p, en_flags); |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 3366 | ttwu_do_wakeup(rq, p, wake_flags, rf); |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | /* |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3370 | * Consider @p being inside a wait loop: |
| 3371 | * |
| 3372 | * for (;;) { |
| 3373 | * set_current_state(TASK_UNINTERRUPTIBLE); |
| 3374 | * |
| 3375 | * if (CONDITION) |
| 3376 | * break; |
| 3377 | * |
| 3378 | * schedule(); |
| 3379 | * } |
| 3380 | * __set_current_state(TASK_RUNNING); |
| 3381 | * |
| 3382 | * between set_current_state() and schedule(). In this case @p is still |
| 3383 | * runnable, so all that needs doing is change p->state back to TASK_RUNNING in |
| 3384 | * an atomic manner. |
| 3385 | * |
| 3386 | * By taking task_rq(p)->lock we serialize against schedule(), if @p->on_rq |
| 3387 | * then schedule() must still happen and p->state can be changed to |
| 3388 | * TASK_RUNNING. Otherwise we lost the race, schedule() has happened, and we |
| 3389 | * need to do a full wakeup with enqueue. |
| 3390 | * |
| 3391 | * Returns: %true when the wakeup is done, |
| 3392 | * %false otherwise. |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3393 | */ |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3394 | static int ttwu_runnable(struct task_struct *p, int wake_flags) |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3395 | { |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 3396 | struct rq_flags rf; |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3397 | struct rq *rq; |
| 3398 | int ret = 0; |
| 3399 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 3400 | rq = __task_rq_lock(p, &rf); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 3401 | if (task_on_rq_queued(p)) { |
Frederic Weisbecker | 1ad4ec0 | 2013-04-12 01:51:00 +0200 | [diff] [blame] | 3402 | /* check_preempt_curr() may use rq clock */ |
| 3403 | update_rq_clock(rq); |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 3404 | ttwu_do_wakeup(rq, p, wake_flags, &rf); |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3405 | ret = 1; |
| 3406 | } |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 3407 | __task_rq_unlock(rq, &rf); |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3408 | |
| 3409 | return ret; |
| 3410 | } |
| 3411 | |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3412 | #ifdef CONFIG_SMP |
Peter Zijlstra | a1488664 | 2020-05-26 18:11:04 +0200 | [diff] [blame] | 3413 | void sched_ttwu_pending(void *arg) |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3414 | { |
Peter Zijlstra | a1488664 | 2020-05-26 18:11:04 +0200 | [diff] [blame] | 3415 | struct llist_node *llist = arg; |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3416 | struct rq *rq = this_rq(); |
Byungchul Park | 7321584 | 2017-05-12 09:39:44 +0900 | [diff] [blame] | 3417 | struct task_struct *p, *t; |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 3418 | struct rq_flags rf; |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3419 | |
Peter Zijlstra | e3baac4 | 2014-06-04 10:31:18 -0700 | [diff] [blame] | 3420 | if (!llist) |
| 3421 | return; |
| 3422 | |
Peter Zijlstra | 126c209 | 2020-05-26 18:11:03 +0200 | [diff] [blame] | 3423 | /* |
| 3424 | * rq::ttwu_pending racy indication of out-standing wakeups. |
| 3425 | * Races such that false-negatives are possible, since they |
| 3426 | * are shorter lived that false-positives would be. |
| 3427 | */ |
| 3428 | WRITE_ONCE(rq->ttwu_pending, 0); |
| 3429 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 3430 | rq_lock_irqsave(rq, &rf); |
Peter Zijlstra | 77558e4 | 2017-02-21 14:36:23 +0100 | [diff] [blame] | 3431 | update_rq_clock(rq); |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3432 | |
Peter Zijlstra | 8c4890d | 2020-06-22 12:01:25 +0200 | [diff] [blame] | 3433 | llist_for_each_entry_safe(p, t, llist, wake_entry.llist) { |
Peter Zijlstra | b6e13e8 | 2020-06-22 12:01:23 +0200 | [diff] [blame] | 3434 | if (WARN_ON_ONCE(p->on_cpu)) |
| 3435 | smp_cond_load_acquire(&p->on_cpu, !VAL); |
| 3436 | |
| 3437 | if (WARN_ON_ONCE(task_cpu(p) != cpu_of(rq))) |
| 3438 | set_task_cpu(p, cpu_of(rq)); |
| 3439 | |
Byungchul Park | 7321584 | 2017-05-12 09:39:44 +0900 | [diff] [blame] | 3440 | ttwu_do_activate(rq, p, p->sched_remote_wakeup ? WF_MIGRATED : 0, &rf); |
Peter Zijlstra | b6e13e8 | 2020-06-22 12:01:23 +0200 | [diff] [blame] | 3441 | } |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3442 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 3443 | rq_unlock_irqrestore(rq, &rf); |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3444 | } |
| 3445 | |
Peter Zijlstra | b2a02fc | 2020-05-26 18:11:01 +0200 | [diff] [blame] | 3446 | void send_call_function_single_ipi(int cpu) |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3447 | { |
Peter Zijlstra | b2a02fc | 2020-05-26 18:11:01 +0200 | [diff] [blame] | 3448 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | f27dde8 | 2013-08-14 14:55:31 +0200 | [diff] [blame] | 3449 | |
Peter Zijlstra | b2a02fc | 2020-05-26 18:11:01 +0200 | [diff] [blame] | 3450 | if (!set_nr_if_polling(rq->idle)) |
| 3451 | arch_send_call_function_single_ipi(cpu); |
| 3452 | else |
| 3453 | trace_sched_wake_idle_without_ipi(cpu); |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3454 | } |
| 3455 | |
Mel Gorman | 2ebb177 | 2020-05-24 21:29:56 +0100 | [diff] [blame] | 3456 | /* |
| 3457 | * Queue a task on the target CPUs wake_list and wake the CPU via IPI if |
| 3458 | * necessary. The wakee CPU on receipt of the IPI will queue the task |
| 3459 | * via sched_ttwu_wakeup() for activation so the wakee incurs the cost |
| 3460 | * of the wakeup instead of the waker. |
| 3461 | */ |
| 3462 | static void __ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags) |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3463 | { |
Peter Zijlstra | e3baac4 | 2014-06-04 10:31:18 -0700 | [diff] [blame] | 3464 | struct rq *rq = cpu_rq(cpu); |
| 3465 | |
Peter Zijlstra | b7e7ade | 2016-05-23 11:19:07 +0200 | [diff] [blame] | 3466 | p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED); |
| 3467 | |
Peter Zijlstra | 126c209 | 2020-05-26 18:11:03 +0200 | [diff] [blame] | 3468 | WRITE_ONCE(rq->ttwu_pending, 1); |
Peter Zijlstra | 8c4890d | 2020-06-22 12:01:25 +0200 | [diff] [blame] | 3469 | __smp_call_single_queue(cpu, &p->wake_entry.llist); |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3470 | } |
Peter Zijlstra | d6aa8f8 | 2011-05-26 14:21:33 +0200 | [diff] [blame] | 3471 | |
Chuansheng Liu | f6be8af | 2014-09-04 15:17:53 +0800 | [diff] [blame] | 3472 | void wake_up_if_idle(int cpu) |
| 3473 | { |
| 3474 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 3475 | struct rq_flags rf; |
Chuansheng Liu | f6be8af | 2014-09-04 15:17:53 +0800 | [diff] [blame] | 3476 | |
Andy Lutomirski | fd7de1e8 | 2014-11-29 08:13:51 -0800 | [diff] [blame] | 3477 | rcu_read_lock(); |
| 3478 | |
| 3479 | if (!is_idle_task(rcu_dereference(rq->curr))) |
| 3480 | goto out; |
Chuansheng Liu | f6be8af | 2014-09-04 15:17:53 +0800 | [diff] [blame] | 3481 | |
| 3482 | if (set_nr_if_polling(rq->idle)) { |
| 3483 | trace_sched_wake_idle_without_ipi(cpu); |
| 3484 | } else { |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 3485 | rq_lock_irqsave(rq, &rf); |
Chuansheng Liu | f6be8af | 2014-09-04 15:17:53 +0800 | [diff] [blame] | 3486 | if (is_idle_task(rq->curr)) |
| 3487 | smp_send_reschedule(cpu); |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3488 | /* Else CPU is not idle, do nothing here: */ |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 3489 | rq_unlock_irqrestore(rq, &rf); |
Chuansheng Liu | f6be8af | 2014-09-04 15:17:53 +0800 | [diff] [blame] | 3490 | } |
Andy Lutomirski | fd7de1e8 | 2014-11-29 08:13:51 -0800 | [diff] [blame] | 3491 | |
| 3492 | out: |
| 3493 | rcu_read_unlock(); |
Chuansheng Liu | f6be8af | 2014-09-04 15:17:53 +0800 | [diff] [blame] | 3494 | } |
| 3495 | |
Peter Zijlstra | 39be350 | 2012-01-26 12:44:34 +0100 | [diff] [blame] | 3496 | bool cpus_share_cache(int this_cpu, int that_cpu) |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 3497 | { |
| 3498 | return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu); |
| 3499 | } |
Peter Zijlstra | c6e7bd7 | 2020-05-24 21:29:55 +0100 | [diff] [blame] | 3500 | |
Mel Gorman | 2ebb177 | 2020-05-24 21:29:56 +0100 | [diff] [blame] | 3501 | static inline bool ttwu_queue_cond(int cpu, int wake_flags) |
Peter Zijlstra | c6e7bd7 | 2020-05-24 21:29:55 +0100 | [diff] [blame] | 3502 | { |
Mel Gorman | 2ebb177 | 2020-05-24 21:29:56 +0100 | [diff] [blame] | 3503 | /* |
Peter Zijlstra | 5ba2ffba | 2021-01-12 11:28:16 +0100 | [diff] [blame] | 3504 | * Do not complicate things with the async wake_list while the CPU is |
| 3505 | * in hotplug state. |
| 3506 | */ |
| 3507 | if (!cpu_active(cpu)) |
| 3508 | return false; |
| 3509 | |
| 3510 | /* |
Mel Gorman | 2ebb177 | 2020-05-24 21:29:56 +0100 | [diff] [blame] | 3511 | * If the CPU does not share cache, then queue the task on the |
| 3512 | * remote rqs wakelist to avoid accessing remote data. |
| 3513 | */ |
| 3514 | if (!cpus_share_cache(smp_processor_id(), cpu)) |
| 3515 | return true; |
| 3516 | |
| 3517 | /* |
| 3518 | * If the task is descheduling and the only running task on the |
| 3519 | * CPU then use the wakelist to offload the task activation to |
| 3520 | * the soon-to-be-idle CPU as the current CPU is likely busy. |
| 3521 | * nr_running is checked to avoid unnecessary task stacking. |
| 3522 | */ |
Peter Zijlstra | 739f70b | 2020-06-22 12:01:24 +0200 | [diff] [blame] | 3523 | if ((wake_flags & WF_ON_CPU) && cpu_rq(cpu)->nr_running <= 1) |
Mel Gorman | 2ebb177 | 2020-05-24 21:29:56 +0100 | [diff] [blame] | 3524 | return true; |
| 3525 | |
| 3526 | return false; |
| 3527 | } |
| 3528 | |
| 3529 | static bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags) |
| 3530 | { |
| 3531 | if (sched_feat(TTWU_QUEUE) && ttwu_queue_cond(cpu, wake_flags)) { |
Peter Zijlstra | b6e13e8 | 2020-06-22 12:01:23 +0200 | [diff] [blame] | 3532 | if (WARN_ON_ONCE(cpu == smp_processor_id())) |
| 3533 | return false; |
| 3534 | |
Peter Zijlstra | c6e7bd7 | 2020-05-24 21:29:55 +0100 | [diff] [blame] | 3535 | sched_clock_cpu(cpu); /* Sync clocks across CPUs */ |
Mel Gorman | 2ebb177 | 2020-05-24 21:29:56 +0100 | [diff] [blame] | 3536 | __ttwu_queue_wakelist(p, cpu, wake_flags); |
Peter Zijlstra | c6e7bd7 | 2020-05-24 21:29:55 +0100 | [diff] [blame] | 3537 | return true; |
| 3538 | } |
| 3539 | |
| 3540 | return false; |
| 3541 | } |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3542 | |
| 3543 | #else /* !CONFIG_SMP */ |
| 3544 | |
| 3545 | static inline bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags) |
| 3546 | { |
| 3547 | return false; |
| 3548 | } |
| 3549 | |
Peter Zijlstra | d6aa8f8 | 2011-05-26 14:21:33 +0200 | [diff] [blame] | 3550 | #endif /* CONFIG_SMP */ |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3551 | |
Peter Zijlstra | b5179ac | 2016-05-11 16:10:34 +0200 | [diff] [blame] | 3552 | static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags) |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3553 | { |
| 3554 | struct rq *rq = cpu_rq(cpu); |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 3555 | struct rq_flags rf; |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3556 | |
Mel Gorman | 2ebb177 | 2020-05-24 21:29:56 +0100 | [diff] [blame] | 3557 | if (ttwu_queue_wakelist(p, cpu, wake_flags)) |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3558 | return; |
Peter Zijlstra | 317f394 | 2011-04-05 17:23:58 +0200 | [diff] [blame] | 3559 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 3560 | rq_lock(rq, &rf); |
Peter Zijlstra | 77558e4 | 2017-02-21 14:36:23 +0100 | [diff] [blame] | 3561 | update_rq_clock(rq); |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 3562 | ttwu_do_activate(rq, p, wake_flags, &rf); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 3563 | rq_unlock(rq, &rf); |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3564 | } |
| 3565 | |
Peter Zijlstra | 8643cda | 2015-11-17 19:01:11 +0100 | [diff] [blame] | 3566 | /* |
| 3567 | * Notes on Program-Order guarantees on SMP systems. |
| 3568 | * |
| 3569 | * MIGRATION |
| 3570 | * |
| 3571 | * The basic program-order guarantee on SMP systems is that when a task [t] |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3572 | * migrates, all its activity on its old CPU [c0] happens-before any subsequent |
| 3573 | * execution on its new CPU [c1]. |
Peter Zijlstra | 8643cda | 2015-11-17 19:01:11 +0100 | [diff] [blame] | 3574 | * |
| 3575 | * For migration (of runnable tasks) this is provided by the following means: |
| 3576 | * |
| 3577 | * A) UNLOCK of the rq(c0)->lock scheduling out task t |
| 3578 | * B) migration for t is required to synchronize *both* rq(c0)->lock and |
| 3579 | * rq(c1)->lock (if not at the same time, then in that order). |
| 3580 | * C) LOCK of the rq(c1)->lock scheduling in task |
| 3581 | * |
Andrea Parri | 7696f99 | 2018-07-16 11:06:03 -0700 | [diff] [blame] | 3582 | * Release/acquire chaining guarantees that B happens after A and C after B. |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3583 | * Note: the CPU doing B need not be c0 or c1 |
Peter Zijlstra | 8643cda | 2015-11-17 19:01:11 +0100 | [diff] [blame] | 3584 | * |
| 3585 | * Example: |
| 3586 | * |
| 3587 | * CPU0 CPU1 CPU2 |
| 3588 | * |
| 3589 | * LOCK rq(0)->lock |
| 3590 | * sched-out X |
| 3591 | * sched-in Y |
| 3592 | * UNLOCK rq(0)->lock |
| 3593 | * |
| 3594 | * LOCK rq(0)->lock // orders against CPU0 |
| 3595 | * dequeue X |
| 3596 | * UNLOCK rq(0)->lock |
| 3597 | * |
| 3598 | * LOCK rq(1)->lock |
| 3599 | * enqueue X |
| 3600 | * UNLOCK rq(1)->lock |
| 3601 | * |
| 3602 | * LOCK rq(1)->lock // orders against CPU2 |
| 3603 | * sched-out Z |
| 3604 | * sched-in X |
| 3605 | * UNLOCK rq(1)->lock |
| 3606 | * |
| 3607 | * |
| 3608 | * BLOCKING -- aka. SLEEP + WAKEUP |
| 3609 | * |
| 3610 | * For blocking we (obviously) need to provide the same guarantee as for |
| 3611 | * migration. However the means are completely different as there is no lock |
| 3612 | * chain to provide order. Instead we do: |
| 3613 | * |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3614 | * 1) smp_store_release(X->on_cpu, 0) -- finish_task() |
| 3615 | * 2) smp_cond_load_acquire(!X->on_cpu) -- try_to_wake_up() |
Peter Zijlstra | 8643cda | 2015-11-17 19:01:11 +0100 | [diff] [blame] | 3616 | * |
| 3617 | * Example: |
| 3618 | * |
| 3619 | * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule) |
| 3620 | * |
| 3621 | * LOCK rq(0)->lock LOCK X->pi_lock |
| 3622 | * dequeue X |
| 3623 | * sched-out X |
| 3624 | * smp_store_release(X->on_cpu, 0); |
| 3625 | * |
Peter Zijlstra | 1f03e8d | 2016-04-04 10:57:12 +0200 | [diff] [blame] | 3626 | * smp_cond_load_acquire(&X->on_cpu, !VAL); |
Peter Zijlstra | 8643cda | 2015-11-17 19:01:11 +0100 | [diff] [blame] | 3627 | * X->state = WAKING |
| 3628 | * set_task_cpu(X,2) |
| 3629 | * |
| 3630 | * LOCK rq(2)->lock |
| 3631 | * enqueue X |
| 3632 | * X->state = RUNNING |
| 3633 | * UNLOCK rq(2)->lock |
| 3634 | * |
| 3635 | * LOCK rq(2)->lock // orders against CPU1 |
| 3636 | * sched-out Z |
| 3637 | * sched-in X |
| 3638 | * UNLOCK rq(2)->lock |
| 3639 | * |
| 3640 | * UNLOCK X->pi_lock |
| 3641 | * UNLOCK rq(0)->lock |
| 3642 | * |
| 3643 | * |
Andrea Parri | 7696f99 | 2018-07-16 11:06:03 -0700 | [diff] [blame] | 3644 | * However, for wakeups there is a second guarantee we must provide, namely we |
| 3645 | * must ensure that CONDITION=1 done by the caller can not be reordered with |
| 3646 | * accesses to the task state; see try_to_wake_up() and set_current_state(). |
Peter Zijlstra | 8643cda | 2015-11-17 19:01:11 +0100 | [diff] [blame] | 3647 | */ |
| 3648 | |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3649 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | * try_to_wake_up - wake up a thread |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3651 | * @p: the thread to be awakened |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3652 | * @state: the mask of task states that can be woken |
Tejun Heo | 9ed3811 | 2009-12-03 15:08:03 +0900 | [diff] [blame] | 3653 | * @wake_flags: wake modifier flags (WF_*) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | * |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3655 | * Conceptually does: |
| 3656 | * |
| 3657 | * If (@state & @p->state) @p->state = TASK_RUNNING. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | * |
Peter Zijlstra | a225023 | 2016-10-19 15:45:27 +0200 | [diff] [blame] | 3659 | * If the task was not queued/runnable, also place it back on a runqueue. |
| 3660 | * |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3661 | * This function is atomic against schedule() which would dequeue the task. |
Peter Zijlstra | a225023 | 2016-10-19 15:45:27 +0200 | [diff] [blame] | 3662 | * |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3663 | * It issues a full memory barrier before accessing @p->state, see the comment |
| 3664 | * with set_current_state(). |
| 3665 | * |
| 3666 | * Uses p->pi_lock to serialize against concurrent wake-ups. |
| 3667 | * |
| 3668 | * Relies on p->pi_lock stabilizing: |
| 3669 | * - p->sched_class |
| 3670 | * - p->cpus_ptr |
| 3671 | * - p->sched_task_group |
| 3672 | * in order to do migration, see its use of select_task_rq()/set_task_cpu(). |
| 3673 | * |
| 3674 | * Tries really hard to only take one task_rq(p)->lock for performance. |
| 3675 | * Takes rq->lock in: |
| 3676 | * - ttwu_runnable() -- old rq, unavoidable, see comment there; |
| 3677 | * - ttwu_queue() -- new rq, for enqueue of the task; |
| 3678 | * - psi_ttwu_dequeue() -- much sadness :-( accounting will kill us. |
| 3679 | * |
| 3680 | * As a consequence we race really badly with just about everything. See the |
| 3681 | * many memory barriers and their comments for details. |
Andrea Parri | 7696f99 | 2018-07-16 11:06:03 -0700 | [diff] [blame] | 3682 | * |
Peter Zijlstra | a225023 | 2016-10-19 15:45:27 +0200 | [diff] [blame] | 3683 | * Return: %true if @p->state changes (an actual wakeup was done), |
| 3684 | * %false otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | */ |
Peter Zijlstra | e4a52bc | 2011-04-05 17:23:54 +0200 | [diff] [blame] | 3686 | static int |
| 3687 | try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3689 | unsigned long flags; |
Peter Zijlstra | c05fbaf | 2011-04-05 17:23:57 +0200 | [diff] [blame] | 3690 | int cpu, success = 0; |
Peter Zijlstra | 2398f2c | 2008-06-27 13:41:35 +0200 | [diff] [blame] | 3691 | |
Peter Zijlstra | e3d8548 | 2019-07-10 12:57:36 +0200 | [diff] [blame] | 3692 | preempt_disable(); |
Peter Zijlstra | aacedf2 | 2019-06-07 15:39:49 +0200 | [diff] [blame] | 3693 | if (p == current) { |
| 3694 | /* |
| 3695 | * We're waking current, this means 'p->on_rq' and 'task_cpu(p) |
| 3696 | * == smp_processor_id()'. Together this means we can special |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3697 | * case the whole 'p->on_rq && ttwu_runnable()' case below |
Peter Zijlstra | aacedf2 | 2019-06-07 15:39:49 +0200 | [diff] [blame] | 3698 | * without taking any locks. |
| 3699 | * |
| 3700 | * In particular: |
| 3701 | * - we rely on Program-Order guarantees for all the ordering, |
| 3702 | * - we're serialized against set_special_state() by virtue of |
| 3703 | * it disabling IRQs (this allows not taking ->pi_lock). |
| 3704 | */ |
| 3705 | if (!(p->state & state)) |
Peter Zijlstra | e3d8548 | 2019-07-10 12:57:36 +0200 | [diff] [blame] | 3706 | goto out; |
Peter Zijlstra | aacedf2 | 2019-06-07 15:39:49 +0200 | [diff] [blame] | 3707 | |
| 3708 | success = 1; |
Peter Zijlstra | aacedf2 | 2019-06-07 15:39:49 +0200 | [diff] [blame] | 3709 | trace_sched_waking(p); |
| 3710 | p->state = TASK_RUNNING; |
| 3711 | trace_sched_wakeup(p); |
| 3712 | goto out; |
| 3713 | } |
| 3714 | |
Oleg Nesterov | e0acd0a | 2013-08-12 18:14:00 +0200 | [diff] [blame] | 3715 | /* |
| 3716 | * If we are going to wake up a thread waiting for CONDITION we |
| 3717 | * need to ensure that CONDITION=1 done by the caller can not be |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3718 | * reordered with p->state check below. This pairs with smp_store_mb() |
| 3719 | * in set_current_state() that the waiting thread does. |
Oleg Nesterov | e0acd0a | 2013-08-12 18:14:00 +0200 | [diff] [blame] | 3720 | */ |
Peter Zijlstra | 013fdb8 | 2011-04-05 17:23:45 +0200 | [diff] [blame] | 3721 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
Peter Zijlstra | d89e588c | 2016-09-05 11:37:53 +0200 | [diff] [blame] | 3722 | smp_mb__after_spinlock(); |
Peter Zijlstra | e9c8431 | 2009-09-15 14:43:03 +0200 | [diff] [blame] | 3723 | if (!(p->state & state)) |
Peter Zijlstra | aacedf2 | 2019-06-07 15:39:49 +0200 | [diff] [blame] | 3724 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | |
Peter Zijlstra | fbd705a | 2015-06-09 11:13:36 +0200 | [diff] [blame] | 3726 | trace_sched_waking(p); |
| 3727 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3728 | /* We're going to change ->state: */ |
| 3729 | success = 1; |
Peter Zijlstra | d7c01d2 | 2011-04-05 17:23:43 +0200 | [diff] [blame] | 3730 | |
Balbir Singh | 135e8c9 | 2016-09-05 13:16:40 +1000 | [diff] [blame] | 3731 | /* |
| 3732 | * Ensure we load p->on_rq _after_ p->state, otherwise it would |
| 3733 | * be possible to, falsely, observe p->on_rq == 0 and get stuck |
| 3734 | * in smp_cond_load_acquire() below. |
| 3735 | * |
Andrea Parri | 3d85b27 | 2018-07-16 11:06:02 -0700 | [diff] [blame] | 3736 | * sched_ttwu_pending() try_to_wake_up() |
| 3737 | * STORE p->on_rq = 1 LOAD p->state |
| 3738 | * UNLOCK rq->lock |
| 3739 | * |
| 3740 | * __schedule() (switch to task 'p') |
| 3741 | * LOCK rq->lock smp_rmb(); |
| 3742 | * smp_mb__after_spinlock(); |
| 3743 | * UNLOCK rq->lock |
Balbir Singh | 135e8c9 | 2016-09-05 13:16:40 +1000 | [diff] [blame] | 3744 | * |
| 3745 | * [task p] |
Andrea Parri | 3d85b27 | 2018-07-16 11:06:02 -0700 | [diff] [blame] | 3746 | * STORE p->state = UNINTERRUPTIBLE LOAD p->on_rq |
Balbir Singh | 135e8c9 | 2016-09-05 13:16:40 +1000 | [diff] [blame] | 3747 | * |
Andrea Parri | 3d85b27 | 2018-07-16 11:06:02 -0700 | [diff] [blame] | 3748 | * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in |
| 3749 | * __schedule(). See the comment for smp_mb__after_spinlock(). |
Paul E. McKenney | 2beaf32 | 2020-03-11 14:23:21 -0700 | [diff] [blame] | 3750 | * |
| 3751 | * A similar smb_rmb() lives in try_invoke_on_locked_down_task(). |
Balbir Singh | 135e8c9 | 2016-09-05 13:16:40 +1000 | [diff] [blame] | 3752 | */ |
| 3753 | smp_rmb(); |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 3754 | if (READ_ONCE(p->on_rq) && ttwu_runnable(p, wake_flags)) |
Peter Zijlstra | aacedf2 | 2019-06-07 15:39:49 +0200 | [diff] [blame] | 3755 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | |
| 3757 | #ifdef CONFIG_SMP |
Peter Zijlstra | e9c8431 | 2009-09-15 14:43:03 +0200 | [diff] [blame] | 3758 | /* |
Peter Zijlstra | ecf7d01 | 2015-10-07 14:14:13 +0200 | [diff] [blame] | 3759 | * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be |
| 3760 | * possible to, falsely, observe p->on_cpu == 0. |
| 3761 | * |
| 3762 | * One must be running (->on_cpu == 1) in order to remove oneself |
| 3763 | * from the runqueue. |
| 3764 | * |
Andrea Parri | 3d85b27 | 2018-07-16 11:06:02 -0700 | [diff] [blame] | 3765 | * __schedule() (switch to task 'p') try_to_wake_up() |
| 3766 | * STORE p->on_cpu = 1 LOAD p->on_rq |
| 3767 | * UNLOCK rq->lock |
Peter Zijlstra | ecf7d01 | 2015-10-07 14:14:13 +0200 | [diff] [blame] | 3768 | * |
Andrea Parri | 3d85b27 | 2018-07-16 11:06:02 -0700 | [diff] [blame] | 3769 | * __schedule() (put 'p' to sleep) |
| 3770 | * LOCK rq->lock smp_rmb(); |
| 3771 | * smp_mb__after_spinlock(); |
| 3772 | * STORE p->on_rq = 0 LOAD p->on_cpu |
| 3773 | * |
| 3774 | * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in |
| 3775 | * __schedule(). See the comment for smp_mb__after_spinlock(). |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 3776 | * |
| 3777 | * Form a control-dep-acquire with p->on_rq == 0 above, to ensure |
| 3778 | * schedule()'s deactivate_task() has 'happened' and p will no longer |
| 3779 | * care about it's own p->state. See the comment in __schedule(). |
Peter Zijlstra | ecf7d01 | 2015-10-07 14:14:13 +0200 | [diff] [blame] | 3780 | */ |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 3781 | smp_acquire__after_ctrl_dep(); |
| 3782 | |
| 3783 | /* |
| 3784 | * We're doing the wakeup (@success == 1), they did a dequeue (p->on_rq |
| 3785 | * == 0), which means we need to do an enqueue, change p->state to |
| 3786 | * TASK_WAKING such that we can unlock p->pi_lock before doing the |
| 3787 | * enqueue, such as ttwu_queue_wakelist(). |
| 3788 | */ |
| 3789 | p->state = TASK_WAKING; |
Peter Zijlstra | ecf7d01 | 2015-10-07 14:14:13 +0200 | [diff] [blame] | 3790 | |
| 3791 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 3792 | * If the owning (remote) CPU is still in the middle of schedule() with |
Peter Zijlstra | c6e7bd7 | 2020-05-24 21:29:55 +0100 | [diff] [blame] | 3793 | * this task as prev, considering queueing p on the remote CPUs wake_list |
| 3794 | * which potentially sends an IPI instead of spinning on p->on_cpu to |
| 3795 | * let the waker make forward progress. This is safe because IRQs are |
| 3796 | * disabled and the IPI will deliver after on_cpu is cleared. |
Peter Zijlstra | b6e13e8 | 2020-06-22 12:01:23 +0200 | [diff] [blame] | 3797 | * |
| 3798 | * Ensure we load task_cpu(p) after p->on_cpu: |
| 3799 | * |
| 3800 | * set_task_cpu(p, cpu); |
| 3801 | * STORE p->cpu = @cpu |
| 3802 | * __schedule() (switch to task 'p') |
| 3803 | * LOCK rq->lock |
| 3804 | * smp_mb__after_spin_lock() smp_cond_load_acquire(&p->on_cpu) |
| 3805 | * STORE p->on_cpu = 1 LOAD p->cpu |
| 3806 | * |
| 3807 | * to ensure we observe the correct CPU on which the task is currently |
| 3808 | * scheduling. |
Peter Zijlstra | c6e7bd7 | 2020-05-24 21:29:55 +0100 | [diff] [blame] | 3809 | */ |
Peter Zijlstra | b6e13e8 | 2020-06-22 12:01:23 +0200 | [diff] [blame] | 3810 | if (smp_load_acquire(&p->on_cpu) && |
Peter Zijlstra | 739f70b | 2020-06-22 12:01:24 +0200 | [diff] [blame] | 3811 | ttwu_queue_wakelist(p, task_cpu(p), wake_flags | WF_ON_CPU)) |
Peter Zijlstra | c6e7bd7 | 2020-05-24 21:29:55 +0100 | [diff] [blame] | 3812 | goto unlock; |
| 3813 | |
| 3814 | /* |
| 3815 | * If the owning (remote) CPU is still in the middle of schedule() with |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 3816 | * this task as prev, wait until it's done referencing the task. |
Peter Zijlstra | b75a225 | 2015-10-06 14:36:17 +0200 | [diff] [blame] | 3817 | * |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 3818 | * Pairs with the smp_store_release() in finish_task(). |
Peter Zijlstra | b75a225 | 2015-10-06 14:36:17 +0200 | [diff] [blame] | 3819 | * |
| 3820 | * This ensures that tasks getting woken will be fully ordered against |
| 3821 | * their previous state and preserve Program Order. |
Peter Zijlstra | e4a52bc | 2011-04-05 17:23:54 +0200 | [diff] [blame] | 3822 | */ |
Peter Zijlstra | 1f03e8d | 2016-04-04 10:57:12 +0200 | [diff] [blame] | 3823 | smp_cond_load_acquire(&p->on_cpu, !VAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | |
Valentin Schneider | 3aef155 | 2020-11-02 18:45:13 +0000 | [diff] [blame] | 3825 | cpu = select_task_rq(p, p->wake_cpu, wake_flags | WF_TTWU); |
Peter Zijlstra | f339b9d | 2011-05-31 10:49:20 +0200 | [diff] [blame] | 3826 | if (task_cpu(p) != cpu) { |
Peter Zijlstra | ec618b8 | 2020-09-24 13:50:42 +0200 | [diff] [blame] | 3827 | if (p->in_iowait) { |
| 3828 | delayacct_blkio_end(p); |
| 3829 | atomic_dec(&task_rq(p)->nr_iowait); |
| 3830 | } |
| 3831 | |
Peter Zijlstra | f339b9d | 2011-05-31 10:49:20 +0200 | [diff] [blame] | 3832 | wake_flags |= WF_MIGRATED; |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3833 | psi_ttwu_dequeue(p); |
Mike Galbraith | 055a008 | 2009-11-12 11:07:44 +0100 | [diff] [blame] | 3834 | set_task_cpu(p, cpu); |
Peter Zijlstra | f339b9d | 2011-05-31 10:49:20 +0200 | [diff] [blame] | 3835 | } |
Peter Zijlstra | b6e13e8 | 2020-06-22 12:01:23 +0200 | [diff] [blame] | 3836 | #else |
| 3837 | cpu = task_cpu(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | #endif /* CONFIG_SMP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | |
Peter Zijlstra | b5179ac | 2016-05-11 16:10:34 +0200 | [diff] [blame] | 3840 | ttwu_queue(p, cpu, wake_flags); |
Peter Zijlstra | aacedf2 | 2019-06-07 15:39:49 +0200 | [diff] [blame] | 3841 | unlock: |
Peter Zijlstra | 013fdb8 | 2011-04-05 17:23:45 +0200 | [diff] [blame] | 3842 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
Peter Zijlstra | aacedf2 | 2019-06-07 15:39:49 +0200 | [diff] [blame] | 3843 | out: |
| 3844 | if (success) |
Peter Zijlstra | b6e13e8 | 2020-06-22 12:01:23 +0200 | [diff] [blame] | 3845 | ttwu_stat(p, task_cpu(p), wake_flags); |
Peter Zijlstra | e3d8548 | 2019-07-10 12:57:36 +0200 | [diff] [blame] | 3846 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3847 | |
| 3848 | return success; |
| 3849 | } |
| 3850 | |
David Howells | 50fa610 | 2009-04-28 15:01:38 +0100 | [diff] [blame] | 3851 | /** |
Paul E. McKenney | 2beaf32 | 2020-03-11 14:23:21 -0700 | [diff] [blame] | 3852 | * try_invoke_on_locked_down_task - Invoke a function on task in fixed state |
Peter Zijlstra | 1b7af29 | 2020-08-29 10:22:24 -0700 | [diff] [blame] | 3853 | * @p: Process for which the function is to be invoked, can be @current. |
Paul E. McKenney | 2beaf32 | 2020-03-11 14:23:21 -0700 | [diff] [blame] | 3854 | * @func: Function to invoke. |
| 3855 | * @arg: Argument to function. |
| 3856 | * |
| 3857 | * If the specified task can be quickly locked into a definite state |
| 3858 | * (either sleeping or on a given runqueue), arrange to keep it in that |
| 3859 | * state while invoking @func(@arg). This function can use ->on_rq and |
| 3860 | * task_curr() to work out what the state is, if required. Given that |
| 3861 | * @func can be invoked with a runqueue lock held, it had better be quite |
| 3862 | * lightweight. |
| 3863 | * |
| 3864 | * Returns: |
| 3865 | * @false if the task slipped out from under the locks. |
| 3866 | * @true if the task was locked onto a runqueue or is sleeping. |
| 3867 | * However, @func can override this by returning @false. |
| 3868 | */ |
| 3869 | bool try_invoke_on_locked_down_task(struct task_struct *p, bool (*func)(struct task_struct *t, void *arg), void *arg) |
| 3870 | { |
Paul E. McKenney | 2beaf32 | 2020-03-11 14:23:21 -0700 | [diff] [blame] | 3871 | struct rq_flags rf; |
Peter Zijlstra | 1b7af29 | 2020-08-29 10:22:24 -0700 | [diff] [blame] | 3872 | bool ret = false; |
Paul E. McKenney | 2beaf32 | 2020-03-11 14:23:21 -0700 | [diff] [blame] | 3873 | struct rq *rq; |
| 3874 | |
Peter Zijlstra | 1b7af29 | 2020-08-29 10:22:24 -0700 | [diff] [blame] | 3875 | raw_spin_lock_irqsave(&p->pi_lock, rf.flags); |
Paul E. McKenney | 2beaf32 | 2020-03-11 14:23:21 -0700 | [diff] [blame] | 3876 | if (p->on_rq) { |
| 3877 | rq = __task_rq_lock(p, &rf); |
| 3878 | if (task_rq(p) == rq) |
| 3879 | ret = func(p, arg); |
| 3880 | rq_unlock(rq, &rf); |
| 3881 | } else { |
| 3882 | switch (p->state) { |
| 3883 | case TASK_RUNNING: |
| 3884 | case TASK_WAKING: |
| 3885 | break; |
| 3886 | default: |
| 3887 | smp_rmb(); // See smp_rmb() comment in try_to_wake_up(). |
| 3888 | if (!p->on_rq) |
| 3889 | ret = func(p, arg); |
| 3890 | } |
| 3891 | } |
Peter Zijlstra | 1b7af29 | 2020-08-29 10:22:24 -0700 | [diff] [blame] | 3892 | raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags); |
Paul E. McKenney | 2beaf32 | 2020-03-11 14:23:21 -0700 | [diff] [blame] | 3893 | return ret; |
| 3894 | } |
| 3895 | |
| 3896 | /** |
David Howells | 50fa610 | 2009-04-28 15:01:38 +0100 | [diff] [blame] | 3897 | * wake_up_process - Wake up a specific process |
| 3898 | * @p: The process to be woken up. |
| 3899 | * |
| 3900 | * Attempt to wake up the nominated process and move it to the set of runnable |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 3901 | * processes. |
| 3902 | * |
| 3903 | * Return: 1 if the process was woken up, 0 if it was already running. |
David Howells | 50fa610 | 2009-04-28 15:01:38 +0100 | [diff] [blame] | 3904 | * |
Andrea Parri | 7696f99 | 2018-07-16 11:06:03 -0700 | [diff] [blame] | 3905 | * This function executes a full memory barrier before accessing the task state. |
David Howells | 50fa610 | 2009-04-28 15:01:38 +0100 | [diff] [blame] | 3906 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 3907 | int wake_up_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | { |
Oleg Nesterov | 9067ac8 | 2013-01-21 20:48:17 +0100 | [diff] [blame] | 3909 | return try_to_wake_up(p, TASK_NORMAL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3910 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | EXPORT_SYMBOL(wake_up_process); |
| 3912 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 3913 | int wake_up_state(struct task_struct *p, unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | { |
| 3915 | return try_to_wake_up(p, state, 0); |
| 3916 | } |
| 3917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | /* |
| 3919 | * Perform scheduler related setup for a newly forked process p. |
| 3920 | * p is forked by current. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3921 | * |
| 3922 | * __sched_fork() is basic setup used by init_idle() too: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | */ |
Rik van Riel | 5e1576e | 2013-10-07 11:29:26 +0100 | [diff] [blame] | 3924 | static void __sched_fork(unsigned long clone_flags, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | { |
Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 3926 | p->on_rq = 0; |
| 3927 | |
| 3928 | p->se.on_rq = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3929 | p->se.exec_start = 0; |
| 3930 | p->se.sum_exec_runtime = 0; |
Ingo Molnar | f6cf891 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 3931 | p->se.prev_sum_exec_runtime = 0; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3932 | p->se.nr_migrations = 0; |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 3933 | p->se.vruntime = 0; |
Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 3934 | INIT_LIST_HEAD(&p->se.group_node); |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 3935 | |
Byungchul Park | ad936d8 | 2015-10-24 01:16:19 +0900 | [diff] [blame] | 3936 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 3937 | p->se.cfs_rq = NULL; |
| 3938 | #endif |
| 3939 | |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 3940 | #ifdef CONFIG_SCHEDSTATS |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 3941 | /* Even if schedstat is disabled, there should not be garbage */ |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 3942 | memset(&p->se.statistics, 0, sizeof(p->se.statistics)); |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 3943 | #endif |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 3944 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 3945 | RB_CLEAR_NODE(&p->dl.rb_node); |
Peter Zijlstra | 40767b0 | 2015-01-28 15:08:03 +0100 | [diff] [blame] | 3946 | init_dl_task_timer(&p->dl); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 3947 | init_dl_inactive_task_timer(&p->dl); |
Juri Lelli | a5e7be3 | 2014-09-19 10:22:39 +0100 | [diff] [blame] | 3948 | __dl_clear_params(p); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 3949 | |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 3950 | INIT_LIST_HEAD(&p->rt.run_list); |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 3951 | p->rt.timeout = 0; |
| 3952 | p->rt.time_slice = sched_rr_timeslice; |
| 3953 | p->rt.on_rq = 0; |
| 3954 | p->rt.on_list = 0; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 3955 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 3956 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 3957 | INIT_HLIST_HEAD(&p->preempt_notifiers); |
| 3958 | #endif |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 3959 | |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 3960 | #ifdef CONFIG_COMPACTION |
| 3961 | p->capture_control = NULL; |
| 3962 | #endif |
Mel Gorman | 1378447 | 2018-05-04 16:41:09 +0100 | [diff] [blame] | 3963 | init_numa_balancing(clone_flags, p); |
Peter Zijlstra | a1488664 | 2020-05-26 18:11:04 +0200 | [diff] [blame] | 3964 | #ifdef CONFIG_SMP |
Peter Zijlstra | 8c4890d | 2020-06-22 12:01:25 +0200 | [diff] [blame] | 3965 | p->wake_entry.u_flags = CSD_TYPE_TTWU; |
Peter Zijlstra | 6d337ea | 2020-09-18 17:24:31 +0200 | [diff] [blame] | 3966 | p->migration_pending = NULL; |
Peter Zijlstra | a1488664 | 2020-05-26 18:11:04 +0200 | [diff] [blame] | 3967 | #endif |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 3968 | } |
| 3969 | |
Srikar Dronamraju | 2a59572 | 2015-08-11 21:54:21 +0530 | [diff] [blame] | 3970 | DEFINE_STATIC_KEY_FALSE(sched_numa_balancing); |
| 3971 | |
Mel Gorman | 1a687c2 | 2012-11-22 11:16:36 +0000 | [diff] [blame] | 3972 | #ifdef CONFIG_NUMA_BALANCING |
Mel Gorman | 3105b86 | 2012-11-23 11:23:49 +0000 | [diff] [blame] | 3973 | |
| 3974 | void set_numabalancing_state(bool enabled) |
| 3975 | { |
Srikar Dronamraju | 2a59572 | 2015-08-11 21:54:21 +0530 | [diff] [blame] | 3976 | if (enabled) |
| 3977 | static_branch_enable(&sched_numa_balancing); |
| 3978 | else |
| 3979 | static_branch_disable(&sched_numa_balancing); |
Mel Gorman | 3105b86 | 2012-11-23 11:23:49 +0000 | [diff] [blame] | 3980 | } |
Andi Kleen | 54a43d5 | 2014-01-23 15:53:13 -0800 | [diff] [blame] | 3981 | |
| 3982 | #ifdef CONFIG_PROC_SYSCTL |
| 3983 | int sysctl_numa_balancing(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 3984 | void *buffer, size_t *lenp, loff_t *ppos) |
Andi Kleen | 54a43d5 | 2014-01-23 15:53:13 -0800 | [diff] [blame] | 3985 | { |
| 3986 | struct ctl_table t; |
| 3987 | int err; |
Srikar Dronamraju | 2a59572 | 2015-08-11 21:54:21 +0530 | [diff] [blame] | 3988 | int state = static_branch_likely(&sched_numa_balancing); |
Andi Kleen | 54a43d5 | 2014-01-23 15:53:13 -0800 | [diff] [blame] | 3989 | |
| 3990 | if (write && !capable(CAP_SYS_ADMIN)) |
| 3991 | return -EPERM; |
| 3992 | |
| 3993 | t = *table; |
| 3994 | t.data = &state; |
| 3995 | err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); |
| 3996 | if (err < 0) |
| 3997 | return err; |
| 3998 | if (write) |
| 3999 | set_numabalancing_state(state); |
| 4000 | return err; |
| 4001 | } |
| 4002 | #endif |
| 4003 | #endif |
Mel Gorman | 1a687c2 | 2012-11-22 11:16:36 +0000 | [diff] [blame] | 4004 | |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 4005 | #ifdef CONFIG_SCHEDSTATS |
Josh Poimboeuf | 4698f88 | 2016-06-07 14:43:16 -0500 | [diff] [blame] | 4006 | |
| 4007 | DEFINE_STATIC_KEY_FALSE(sched_schedstats); |
| 4008 | static bool __initdata __sched_schedstats = false; |
| 4009 | |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 4010 | static void set_schedstats(bool enabled) |
| 4011 | { |
| 4012 | if (enabled) |
| 4013 | static_branch_enable(&sched_schedstats); |
| 4014 | else |
| 4015 | static_branch_disable(&sched_schedstats); |
| 4016 | } |
| 4017 | |
| 4018 | void force_schedstat_enabled(void) |
| 4019 | { |
| 4020 | if (!schedstat_enabled()) { |
| 4021 | pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n"); |
| 4022 | static_branch_enable(&sched_schedstats); |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | static int __init setup_schedstats(char *str) |
| 4027 | { |
| 4028 | int ret = 0; |
| 4029 | if (!str) |
| 4030 | goto out; |
| 4031 | |
Josh Poimboeuf | 4698f88 | 2016-06-07 14:43:16 -0500 | [diff] [blame] | 4032 | /* |
| 4033 | * This code is called before jump labels have been set up, so we can't |
| 4034 | * change the static branch directly just yet. Instead set a temporary |
| 4035 | * variable so init_schedstats() can do it later. |
| 4036 | */ |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 4037 | if (!strcmp(str, "enable")) { |
Josh Poimboeuf | 4698f88 | 2016-06-07 14:43:16 -0500 | [diff] [blame] | 4038 | __sched_schedstats = true; |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 4039 | ret = 1; |
| 4040 | } else if (!strcmp(str, "disable")) { |
Josh Poimboeuf | 4698f88 | 2016-06-07 14:43:16 -0500 | [diff] [blame] | 4041 | __sched_schedstats = false; |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 4042 | ret = 1; |
| 4043 | } |
| 4044 | out: |
| 4045 | if (!ret) |
| 4046 | pr_warn("Unable to parse schedstats=\n"); |
| 4047 | |
| 4048 | return ret; |
| 4049 | } |
| 4050 | __setup("schedstats=", setup_schedstats); |
| 4051 | |
Josh Poimboeuf | 4698f88 | 2016-06-07 14:43:16 -0500 | [diff] [blame] | 4052 | static void __init init_schedstats(void) |
| 4053 | { |
| 4054 | set_schedstats(__sched_schedstats); |
| 4055 | } |
| 4056 | |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 4057 | #ifdef CONFIG_PROC_SYSCTL |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 4058 | int sysctl_schedstats(struct ctl_table *table, int write, void *buffer, |
| 4059 | size_t *lenp, loff_t *ppos) |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 4060 | { |
| 4061 | struct ctl_table t; |
| 4062 | int err; |
| 4063 | int state = static_branch_likely(&sched_schedstats); |
| 4064 | |
| 4065 | if (write && !capable(CAP_SYS_ADMIN)) |
| 4066 | return -EPERM; |
| 4067 | |
| 4068 | t = *table; |
| 4069 | t.data = &state; |
| 4070 | err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); |
| 4071 | if (err < 0) |
| 4072 | return err; |
| 4073 | if (write) |
| 4074 | set_schedstats(state); |
| 4075 | return err; |
| 4076 | } |
Josh Poimboeuf | 4698f88 | 2016-06-07 14:43:16 -0500 | [diff] [blame] | 4077 | #endif /* CONFIG_PROC_SYSCTL */ |
| 4078 | #else /* !CONFIG_SCHEDSTATS */ |
| 4079 | static inline void init_schedstats(void) {} |
| 4080 | #endif /* CONFIG_SCHEDSTATS */ |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 4081 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4082 | /* |
| 4083 | * fork()/clone()-time setup: |
| 4084 | */ |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 4085 | int sched_fork(unsigned long clone_flags, struct task_struct *p) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4086 | { |
Peter Zijlstra | 0122ec5 | 2011-04-05 17:23:51 +0200 | [diff] [blame] | 4087 | unsigned long flags; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4088 | |
Rik van Riel | 5e1576e | 2013-10-07 11:29:26 +0100 | [diff] [blame] | 4089 | __sched_fork(clone_flags, p); |
Peter Zijlstra | 06b83b5 | 2009-12-16 18:04:35 +0100 | [diff] [blame] | 4090 | /* |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 4091 | * We mark the process as NEW here. This guarantees that |
Peter Zijlstra | 06b83b5 | 2009-12-16 18:04:35 +0100 | [diff] [blame] | 4092 | * nobody will actually run it, and a signal or other external |
| 4093 | * event cannot wake it up and insert it on the runqueue either. |
| 4094 | */ |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 4095 | p->state = TASK_NEW; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4096 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4097 | /* |
Mike Galbraith | c350a04 | 2011-07-27 17:14:55 +0200 | [diff] [blame] | 4098 | * Make sure we do not leak PI boosting priority to the child. |
| 4099 | */ |
| 4100 | p->prio = current->normal_prio; |
| 4101 | |
Patrick Bellasi | e8f1417 | 2019-06-21 09:42:05 +0100 | [diff] [blame] | 4102 | uclamp_fork(p); |
| 4103 | |
Mike Galbraith | c350a04 | 2011-07-27 17:14:55 +0200 | [diff] [blame] | 4104 | /* |
Mike Galbraith | b9dc29e | 2009-06-17 10:46:01 +0200 | [diff] [blame] | 4105 | * Revert to default priority/policy on fork if requested. |
| 4106 | */ |
| 4107 | if (unlikely(p->sched_reset_on_fork)) { |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 4108 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
Mike Galbraith | b9dc29e | 2009-06-17 10:46:01 +0200 | [diff] [blame] | 4109 | p->policy = SCHED_NORMAL; |
Mike Galbraith | 6c697bd | 2009-06-17 10:48:02 +0200 | [diff] [blame] | 4110 | p->static_prio = NICE_TO_PRIO(0); |
Mike Galbraith | c350a04 | 2011-07-27 17:14:55 +0200 | [diff] [blame] | 4111 | p->rt_priority = 0; |
| 4112 | } else if (PRIO_TO_NICE(p->static_prio) < 0) |
| 4113 | p->static_prio = NICE_TO_PRIO(0); |
| 4114 | |
| 4115 | p->prio = p->normal_prio = __normal_prio(p); |
Vincent Guittot | 9059393 | 2017-05-17 11:50:45 +0200 | [diff] [blame] | 4116 | set_load_weight(p, false); |
Mike Galbraith | 6c697bd | 2009-06-17 10:48:02 +0200 | [diff] [blame] | 4117 | |
Mike Galbraith | b9dc29e | 2009-06-17 10:46:01 +0200 | [diff] [blame] | 4118 | /* |
| 4119 | * We don't need the reset flag anymore after the fork. It has |
| 4120 | * fulfilled its duty: |
| 4121 | */ |
| 4122 | p->sched_reset_on_fork = 0; |
| 4123 | } |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 4124 | |
Sebastian Andrzej Siewior | af0fffd | 2018-07-06 15:06:15 +0200 | [diff] [blame] | 4125 | if (dl_prio(p->prio)) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 4126 | return -EAGAIN; |
Sebastian Andrzej Siewior | af0fffd | 2018-07-06 15:06:15 +0200 | [diff] [blame] | 4127 | else if (rt_prio(p->prio)) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 4128 | p->sched_class = &rt_sched_class; |
Sebastian Andrzej Siewior | af0fffd | 2018-07-06 15:06:15 +0200 | [diff] [blame] | 4129 | else |
Hiroshi Shimamoto | 2ddbf95 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 4130 | p->sched_class = &fair_sched_class; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4131 | |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 4132 | init_entity_runnable_average(&p->se); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 4133 | |
Peter Zijlstra | 8695159 | 2010-06-22 11:44:53 +0200 | [diff] [blame] | 4134 | /* |
| 4135 | * The child is not yet in the pid-hash so no cgroup attach races, |
| 4136 | * and the cgroup is pinned to this child due to cgroup_fork() |
| 4137 | * is ran before sched_fork(). |
| 4138 | * |
| 4139 | * Silence PROVE_RCU. |
| 4140 | */ |
Peter Zijlstra | 0122ec5 | 2011-04-05 17:23:51 +0200 | [diff] [blame] | 4141 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
Mathieu Desnoyers | ce3614d | 2020-07-06 16:49:10 -0400 | [diff] [blame] | 4142 | rseq_migrate(p); |
Peter Zijlstra | e210bff | 2016-06-16 18:51:48 +0200 | [diff] [blame] | 4143 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 4144 | * We're setting the CPU for the first time, we don't migrate, |
Peter Zijlstra | e210bff | 2016-06-16 18:51:48 +0200 | [diff] [blame] | 4145 | * so use __set_task_cpu(). |
| 4146 | */ |
Sebastian Andrzej Siewior | af0fffd | 2018-07-06 15:06:15 +0200 | [diff] [blame] | 4147 | __set_task_cpu(p, smp_processor_id()); |
Peter Zijlstra | e210bff | 2016-06-16 18:51:48 +0200 | [diff] [blame] | 4148 | if (p->sched_class->task_fork) |
| 4149 | p->sched_class->task_fork(p); |
Peter Zijlstra | 0122ec5 | 2011-04-05 17:23:51 +0200 | [diff] [blame] | 4150 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
Peter Zijlstra | 5f3edc1 | 2009-09-10 13:42:00 +0200 | [diff] [blame] | 4151 | |
Naveen N. Rao | f6db834 | 2015-06-25 23:53:37 +0530 | [diff] [blame] | 4152 | #ifdef CONFIG_SCHED_INFO |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4153 | if (likely(sched_info_on())) |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 4154 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | #endif |
Peter Zijlstra | 3ca7a44 | 2011-04-05 17:23:40 +0200 | [diff] [blame] | 4156 | #if defined(CONFIG_SMP) |
| 4157 | p->on_cpu = 0; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 4158 | #endif |
Peter Zijlstra | 0102874 | 2013-08-14 14:55:46 +0200 | [diff] [blame] | 4159 | init_task_preempt_count(p); |
Dario Faggioli | 806c09a | 2010-11-30 19:51:33 +0100 | [diff] [blame] | 4160 | #ifdef CONFIG_SMP |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 4161 | plist_node_init(&p->pushable_tasks, MAX_PRIO); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 4162 | RB_CLEAR_NODE(&p->pushable_dl_tasks); |
Dario Faggioli | 806c09a | 2010-11-30 19:51:33 +0100 | [diff] [blame] | 4163 | #endif |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 4164 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | } |
| 4166 | |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 4167 | void sched_post_fork(struct task_struct *p) |
| 4168 | { |
| 4169 | uclamp_post_fork(p); |
| 4170 | } |
| 4171 | |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 4172 | unsigned long to_ratio(u64 period, u64 runtime) |
| 4173 | { |
| 4174 | if (runtime == RUNTIME_INF) |
Luca Abeni | c52f14d | 2017-05-18 22:13:31 +0200 | [diff] [blame] | 4175 | return BW_UNIT; |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 4176 | |
| 4177 | /* |
| 4178 | * Doing this here saves a lot of checks in all |
| 4179 | * the calling paths, and returning zero seems |
| 4180 | * safe for them anyway. |
| 4181 | */ |
| 4182 | if (period == 0) |
| 4183 | return 0; |
| 4184 | |
Luca Abeni | c52f14d | 2017-05-18 22:13:31 +0200 | [diff] [blame] | 4185 | return div64_u64(runtime << BW_SHIFT, period); |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 4186 | } |
| 4187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | /* |
| 4189 | * wake_up_new_task - wake up a newly created task for the first time. |
| 4190 | * |
| 4191 | * This function will do some initial scheduler statistics housekeeping |
| 4192 | * that must be done for every newly created context, then puts the task |
| 4193 | * on the runqueue and wakes it. |
| 4194 | */ |
Samir Bellabes | 3e51e3e | 2011-05-11 18:18:05 +0200 | [diff] [blame] | 4195 | void wake_up_new_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | { |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 4197 | struct rq_flags rf; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4198 | struct rq *rq; |
Peter Zijlstra | fabf318 | 2010-01-21 21:04:57 +0100 | [diff] [blame] | 4199 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 4200 | raw_spin_lock_irqsave(&p->pi_lock, rf.flags); |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 4201 | p->state = TASK_RUNNING; |
Peter Zijlstra | fabf318 | 2010-01-21 21:04:57 +0100 | [diff] [blame] | 4202 | #ifdef CONFIG_SMP |
| 4203 | /* |
| 4204 | * Fork balancing, do it here and not earlier because: |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 4205 | * - cpus_ptr can change in the fork path |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 4206 | * - any previously selected CPU might disappear through hotplug |
Peter Zijlstra | e210bff | 2016-06-16 18:51:48 +0200 | [diff] [blame] | 4207 | * |
| 4208 | * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq, |
| 4209 | * as we're not fully set-up yet. |
Peter Zijlstra | fabf318 | 2010-01-21 21:04:57 +0100 | [diff] [blame] | 4210 | */ |
Mel Gorman | 32e839d | 2018-01-30 10:45:55 +0000 | [diff] [blame] | 4211 | p->recent_used_cpu = task_cpu(p); |
Mathieu Desnoyers | ce3614d | 2020-07-06 16:49:10 -0400 | [diff] [blame] | 4212 | rseq_migrate(p); |
Valentin Schneider | 3aef155 | 2020-11-02 18:45:13 +0000 | [diff] [blame] | 4213 | __set_task_cpu(p, select_task_rq(p, task_cpu(p), WF_FORK)); |
Peter Zijlstra | fabf318 | 2010-01-21 21:04:57 +0100 | [diff] [blame] | 4214 | #endif |
Peter Zijlstra | b7fa30c | 2016-06-09 15:07:50 +0200 | [diff] [blame] | 4215 | rq = __task_rq_lock(p, &rf); |
Peter Zijlstra | 4126bad | 2016-10-03 16:20:59 +0200 | [diff] [blame] | 4216 | update_rq_clock(rq); |
Dietmar Eggemann | d0fe0b9 | 2019-01-22 16:25:01 +0000 | [diff] [blame] | 4217 | post_init_entity_util_avg(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | |
Peter Zijlstra | 7a57f32 | 2017-02-21 14:47:02 +0100 | [diff] [blame] | 4219 | activate_task(rq, p, ENQUEUE_NOCLOCK); |
Peter Zijlstra | fbd705a | 2015-06-09 11:13:36 +0200 | [diff] [blame] | 4220 | trace_sched_wakeup_new(p); |
Peter Zijlstra | a7558e0 | 2009-09-14 20:02:34 +0200 | [diff] [blame] | 4221 | check_preempt_curr(rq, p, WF_FORK); |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4222 | #ifdef CONFIG_SMP |
Peter Zijlstra | 0aaafaa | 2015-10-23 11:50:08 +0200 | [diff] [blame] | 4223 | if (p->sched_class->task_woken) { |
| 4224 | /* |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 4225 | * Nothing relies on rq->lock after this, so it's fine to |
Peter Zijlstra | 0aaafaa | 2015-10-23 11:50:08 +0200 | [diff] [blame] | 4226 | * drop it. |
| 4227 | */ |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 4228 | rq_unpin_lock(rq, &rf); |
Peter Zijlstra | efbbd05 | 2009-12-16 18:04:40 +0100 | [diff] [blame] | 4229 | p->sched_class->task_woken(rq, p); |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 4230 | rq_repin_lock(rq, &rf); |
Peter Zijlstra | 0aaafaa | 2015-10-23 11:50:08 +0200 | [diff] [blame] | 4231 | } |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4232 | #endif |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 4233 | task_rq_unlock(rq, p, &rf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | } |
| 4235 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4236 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 4237 | |
Davidlohr Bueso | b720342 | 2018-03-26 14:09:26 -0700 | [diff] [blame] | 4238 | static DEFINE_STATIC_KEY_FALSE(preempt_notifier_key); |
Peter Zijlstra | 1cde293 | 2015-06-08 16:00:30 +0200 | [diff] [blame] | 4239 | |
Peter Zijlstra | 2ecd9d2 | 2015-07-03 18:53:58 +0200 | [diff] [blame] | 4240 | void preempt_notifier_inc(void) |
| 4241 | { |
Davidlohr Bueso | b720342 | 2018-03-26 14:09:26 -0700 | [diff] [blame] | 4242 | static_branch_inc(&preempt_notifier_key); |
Peter Zijlstra | 2ecd9d2 | 2015-07-03 18:53:58 +0200 | [diff] [blame] | 4243 | } |
| 4244 | EXPORT_SYMBOL_GPL(preempt_notifier_inc); |
| 4245 | |
| 4246 | void preempt_notifier_dec(void) |
| 4247 | { |
Davidlohr Bueso | b720342 | 2018-03-26 14:09:26 -0700 | [diff] [blame] | 4248 | static_branch_dec(&preempt_notifier_key); |
Peter Zijlstra | 2ecd9d2 | 2015-07-03 18:53:58 +0200 | [diff] [blame] | 4249 | } |
| 4250 | EXPORT_SYMBOL_GPL(preempt_notifier_dec); |
| 4251 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4252 | /** |
Luis Henriques | 80dd99b | 2009-03-16 19:58:09 +0000 | [diff] [blame] | 4253 | * preempt_notifier_register - tell me when current is being preempted & rescheduled |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 4254 | * @notifier: notifier struct to register |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4255 | */ |
| 4256 | void preempt_notifier_register(struct preempt_notifier *notifier) |
| 4257 | { |
Davidlohr Bueso | b720342 | 2018-03-26 14:09:26 -0700 | [diff] [blame] | 4258 | if (!static_branch_unlikely(&preempt_notifier_key)) |
Peter Zijlstra | 2ecd9d2 | 2015-07-03 18:53:58 +0200 | [diff] [blame] | 4259 | WARN(1, "registering preempt_notifier while notifiers disabled\n"); |
| 4260 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4261 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
| 4262 | } |
| 4263 | EXPORT_SYMBOL_GPL(preempt_notifier_register); |
| 4264 | |
| 4265 | /** |
| 4266 | * preempt_notifier_unregister - no longer interested in preemption notifications |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 4267 | * @notifier: notifier struct to unregister |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4268 | * |
Mathieu Desnoyers | d84525a | 2015-05-17 12:53:10 -0400 | [diff] [blame] | 4269 | * This is *not* safe to call from within a preemption notifier. |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4270 | */ |
| 4271 | void preempt_notifier_unregister(struct preempt_notifier *notifier) |
| 4272 | { |
| 4273 | hlist_del(¬ifier->link); |
| 4274 | } |
| 4275 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); |
| 4276 | |
Peter Zijlstra | 1cde293 | 2015-06-08 16:00:30 +0200 | [diff] [blame] | 4277 | static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4278 | { |
| 4279 | struct preempt_notifier *notifier; |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4280 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4281 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4282 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
| 4283 | } |
| 4284 | |
Peter Zijlstra | 1cde293 | 2015-06-08 16:00:30 +0200 | [diff] [blame] | 4285 | static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| 4286 | { |
Davidlohr Bueso | b720342 | 2018-03-26 14:09:26 -0700 | [diff] [blame] | 4287 | if (static_branch_unlikely(&preempt_notifier_key)) |
Peter Zijlstra | 1cde293 | 2015-06-08 16:00:30 +0200 | [diff] [blame] | 4288 | __fire_sched_in_preempt_notifiers(curr); |
| 4289 | } |
| 4290 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4291 | static void |
Peter Zijlstra | 1cde293 | 2015-06-08 16:00:30 +0200 | [diff] [blame] | 4292 | __fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 4293 | struct task_struct *next) |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4294 | { |
| 4295 | struct preempt_notifier *notifier; |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4296 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4297 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4298 | notifier->ops->sched_out(notifier, next); |
| 4299 | } |
| 4300 | |
Peter Zijlstra | 1cde293 | 2015-06-08 16:00:30 +0200 | [diff] [blame] | 4301 | static __always_inline void |
| 4302 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 4303 | struct task_struct *next) |
| 4304 | { |
Davidlohr Bueso | b720342 | 2018-03-26 14:09:26 -0700 | [diff] [blame] | 4305 | if (static_branch_unlikely(&preempt_notifier_key)) |
Peter Zijlstra | 1cde293 | 2015-06-08 16:00:30 +0200 | [diff] [blame] | 4306 | __fire_sched_out_preempt_notifiers(curr, next); |
| 4307 | } |
| 4308 | |
Dhaval Giani | 6d6bc0a | 2008-05-30 14:23:45 +0200 | [diff] [blame] | 4309 | #else /* !CONFIG_PREEMPT_NOTIFIERS */ |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4310 | |
Peter Zijlstra | 1cde293 | 2015-06-08 16:00:30 +0200 | [diff] [blame] | 4311 | static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4312 | { |
| 4313 | } |
| 4314 | |
Peter Zijlstra | 1cde293 | 2015-06-08 16:00:30 +0200 | [diff] [blame] | 4315 | static inline void |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4316 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| 4317 | struct task_struct *next) |
| 4318 | { |
| 4319 | } |
| 4320 | |
Dhaval Giani | 6d6bc0a | 2008-05-30 14:23:45 +0200 | [diff] [blame] | 4321 | #endif /* CONFIG_PREEMPT_NOTIFIERS */ |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4322 | |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4323 | static inline void prepare_task(struct task_struct *next) |
| 4324 | { |
| 4325 | #ifdef CONFIG_SMP |
| 4326 | /* |
| 4327 | * Claim the task as running, we do this before switching to it |
| 4328 | * such that any running task will have this set. |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 4329 | * |
| 4330 | * See the ttwu() WF_ON_CPU case and its ordering comment. |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4331 | */ |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 4332 | WRITE_ONCE(next->on_cpu, 1); |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4333 | #endif |
| 4334 | } |
| 4335 | |
| 4336 | static inline void finish_task(struct task_struct *prev) |
| 4337 | { |
| 4338 | #ifdef CONFIG_SMP |
| 4339 | /* |
Peter Zijlstra | 58877d3 | 2020-07-02 14:52:11 +0200 | [diff] [blame] | 4340 | * This must be the very last reference to @prev from this CPU. After |
| 4341 | * p->on_cpu is cleared, the task can be moved to a different CPU. We |
| 4342 | * must ensure this doesn't happen until the switch is completely |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4343 | * finished. |
| 4344 | * |
| 4345 | * In particular, the load of prev->state in finish_task_switch() must |
| 4346 | * happen before this. |
| 4347 | * |
| 4348 | * Pairs with the smp_cond_load_acquire() in try_to_wake_up(). |
| 4349 | */ |
| 4350 | smp_store_release(&prev->on_cpu, 0); |
| 4351 | #endif |
| 4352 | } |
| 4353 | |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 4354 | #ifdef CONFIG_SMP |
| 4355 | |
| 4356 | static void do_balance_callbacks(struct rq *rq, struct callback_head *head) |
| 4357 | { |
| 4358 | void (*func)(struct rq *rq); |
| 4359 | struct callback_head *next; |
| 4360 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 4361 | lockdep_assert_rq_held(rq); |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 4362 | |
| 4363 | while (head) { |
| 4364 | func = (void (*)(struct rq *))head->func; |
| 4365 | next = head->next; |
| 4366 | head->next = NULL; |
| 4367 | head = next; |
| 4368 | |
| 4369 | func(rq); |
| 4370 | } |
| 4371 | } |
| 4372 | |
Peter Zijlstra | ae79270 | 2020-12-10 17:14:08 +0100 | [diff] [blame] | 4373 | static void balance_push(struct rq *rq); |
| 4374 | |
| 4375 | struct callback_head balance_push_callback = { |
| 4376 | .next = NULL, |
| 4377 | .func = (void (*)(struct callback_head *))balance_push, |
| 4378 | }; |
| 4379 | |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 4380 | static inline struct callback_head *splice_balance_callbacks(struct rq *rq) |
| 4381 | { |
| 4382 | struct callback_head *head = rq->balance_callback; |
| 4383 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 4384 | lockdep_assert_rq_held(rq); |
Peter Zijlstra | ae79270 | 2020-12-10 17:14:08 +0100 | [diff] [blame] | 4385 | if (head) |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 4386 | rq->balance_callback = NULL; |
| 4387 | |
| 4388 | return head; |
| 4389 | } |
| 4390 | |
| 4391 | static void __balance_callbacks(struct rq *rq) |
| 4392 | { |
| 4393 | do_balance_callbacks(rq, splice_balance_callbacks(rq)); |
| 4394 | } |
| 4395 | |
| 4396 | static inline void balance_callbacks(struct rq *rq, struct callback_head *head) |
| 4397 | { |
| 4398 | unsigned long flags; |
| 4399 | |
| 4400 | if (unlikely(head)) { |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 4401 | raw_spin_rq_lock_irqsave(rq, flags); |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 4402 | do_balance_callbacks(rq, head); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 4403 | raw_spin_rq_unlock_irqrestore(rq, flags); |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | #else |
| 4408 | |
| 4409 | static inline void __balance_callbacks(struct rq *rq) |
| 4410 | { |
| 4411 | } |
| 4412 | |
| 4413 | static inline struct callback_head *splice_balance_callbacks(struct rq *rq) |
| 4414 | { |
| 4415 | return NULL; |
| 4416 | } |
| 4417 | |
| 4418 | static inline void balance_callbacks(struct rq *rq, struct callback_head *head) |
| 4419 | { |
| 4420 | } |
| 4421 | |
| 4422 | #endif |
| 4423 | |
Peter Zijlstra | 269d599 | 2018-02-06 17:52:13 +0100 | [diff] [blame] | 4424 | static inline void |
| 4425 | prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf) |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4426 | { |
Peter Zijlstra | 269d599 | 2018-02-06 17:52:13 +0100 | [diff] [blame] | 4427 | /* |
| 4428 | * Since the runqueue lock will be released by the next |
| 4429 | * task (which is an invalid locking op but in the case |
| 4430 | * of the scheduler it's an obvious special-case), so we |
| 4431 | * do an early lockdep release here: |
| 4432 | */ |
| 4433 | rq_unpin_lock(rq, rf); |
Peter Zijlstra | 9ef7e7e | 2021-03-03 16:45:41 +0100 | [diff] [blame] | 4434 | spin_release(&__rq_lockp(rq)->dep_map, _THIS_IP_); |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4435 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 4436 | /* this is a valid case when another task releases the spinlock */ |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 4437 | rq_lockp(rq)->owner = next; |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4438 | #endif |
Peter Zijlstra | 269d599 | 2018-02-06 17:52:13 +0100 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | static inline void finish_lock_switch(struct rq *rq) |
| 4442 | { |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4443 | /* |
| 4444 | * If we are tracking spinlock dependencies then we have to |
| 4445 | * fix up the runqueue lock - which gets 'carried over' from |
| 4446 | * prev into current: |
| 4447 | */ |
Peter Zijlstra | 9ef7e7e | 2021-03-03 16:45:41 +0100 | [diff] [blame] | 4448 | spin_acquire(&__rq_lockp(rq)->dep_map, 0, 0, _THIS_IP_); |
Peter Zijlstra | ae79270 | 2020-12-10 17:14:08 +0100 | [diff] [blame] | 4449 | __balance_callbacks(rq); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 4450 | raw_spin_rq_unlock_irq(rq); |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4451 | } |
| 4452 | |
Ingo Molnar | 325ea10 | 2018-03-03 12:20:47 +0100 | [diff] [blame] | 4453 | /* |
| 4454 | * NOP if the arch has not defined these: |
| 4455 | */ |
| 4456 | |
| 4457 | #ifndef prepare_arch_switch |
| 4458 | # define prepare_arch_switch(next) do { } while (0) |
| 4459 | #endif |
| 4460 | |
| 4461 | #ifndef finish_arch_post_lock_switch |
| 4462 | # define finish_arch_post_lock_switch() do { } while (0) |
| 4463 | #endif |
| 4464 | |
Thomas Gleixner | 5fbda3e | 2020-11-18 20:48:43 +0100 | [diff] [blame] | 4465 | static inline void kmap_local_sched_out(void) |
| 4466 | { |
| 4467 | #ifdef CONFIG_KMAP_LOCAL |
| 4468 | if (unlikely(current->kmap_ctrl.idx)) |
| 4469 | __kmap_local_sched_out(); |
| 4470 | #endif |
| 4471 | } |
| 4472 | |
| 4473 | static inline void kmap_local_sched_in(void) |
| 4474 | { |
| 4475 | #ifdef CONFIG_KMAP_LOCAL |
| 4476 | if (unlikely(current->kmap_ctrl.idx)) |
| 4477 | __kmap_local_sched_in(); |
| 4478 | #endif |
| 4479 | } |
| 4480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | /** |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 4482 | * prepare_task_switch - prepare to switch tasks |
| 4483 | * @rq: the runqueue preparing to switch |
Randy Dunlap | 421cee2 | 2007-07-31 00:37:50 -0700 | [diff] [blame] | 4484 | * @prev: the current task that is being switched out |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 4485 | * @next: the task we are going to switch to. |
| 4486 | * |
| 4487 | * This is called with the rq lock held and interrupts off. It must |
| 4488 | * be paired with a subsequent finish_task_switch after the context |
| 4489 | * switch. |
| 4490 | * |
| 4491 | * prepare_task_switch sets up locking and calls architecture specific |
| 4492 | * hooks. |
| 4493 | */ |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4494 | static inline void |
| 4495 | prepare_task_switch(struct rq *rq, struct task_struct *prev, |
| 4496 | struct task_struct *next) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 4497 | { |
Mark Rutland | 0ed557a | 2018-06-14 15:27:41 -0700 | [diff] [blame] | 4498 | kcov_prepare_switch(prev); |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 4499 | sched_info_switch(rq, prev, next); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 4500 | perf_event_task_sched_out(prev, next); |
Mathieu Desnoyers | d7822b1 | 2018-06-02 08:43:54 -0400 | [diff] [blame] | 4501 | rseq_preempt(prev); |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4502 | fire_sched_out_preempt_notifiers(prev, next); |
Thomas Gleixner | 5fbda3e | 2020-11-18 20:48:43 +0100 | [diff] [blame] | 4503 | kmap_local_sched_out(); |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4504 | prepare_task(next); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 4505 | prepare_arch_switch(next); |
| 4506 | } |
| 4507 | |
| 4508 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | * finish_task_switch - clean up after a task-switch |
| 4510 | * @prev: the thread we just switched away from. |
| 4511 | * |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 4512 | * finish_task_switch must be called after the context switch, paired |
| 4513 | * with a prepare_task_switch call before the context switch. |
| 4514 | * finish_task_switch will reconcile locking set up by prepare_task_switch, |
| 4515 | * and do any other architecture-specific cleanup actions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | * |
| 4517 | * Note that we may have delayed dropping an mm in context_switch(). If |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4518 | * so, we finish that here outside of the runqueue lock. (Doing it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | * with the lock held can cause deadlocks; see schedule() for |
| 4520 | * details.) |
Oleg Nesterov | dfa50b6 | 2014-10-09 21:32:32 +0200 | [diff] [blame] | 4521 | * |
| 4522 | * The context switch have flipped the stack from under us and restored the |
| 4523 | * local variables which were saved when this task called schedule() in the |
| 4524 | * past. prev == current is still correct but we need to recalculate this_rq |
| 4525 | * because prev may have moved to another CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | */ |
Oleg Nesterov | dfa50b6 | 2014-10-09 21:32:32 +0200 | [diff] [blame] | 4527 | static struct rq *finish_task_switch(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 | __releases(rq->lock) |
| 4529 | { |
Oleg Nesterov | dfa50b6 | 2014-10-09 21:32:32 +0200 | [diff] [blame] | 4530 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | struct mm_struct *mm = rq->prev_mm; |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 4532 | long prev_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | |
Peter Zijlstra | 609ca06 | 2015-09-28 17:52:18 +0200 | [diff] [blame] | 4534 | /* |
| 4535 | * The previous task will have left us with a preempt_count of 2 |
| 4536 | * because it left us after: |
| 4537 | * |
| 4538 | * schedule() |
| 4539 | * preempt_disable(); // 1 |
| 4540 | * __schedule() |
| 4541 | * raw_spin_lock_irq(&rq->lock) // 2 |
| 4542 | * |
| 4543 | * Also, see FORK_PREEMPT_COUNT. |
| 4544 | */ |
Peter Zijlstra | e2bf1c4b | 2015-09-29 12:18:46 +0200 | [diff] [blame] | 4545 | if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET, |
| 4546 | "corrupted preempt_count: %s/%d/0x%x\n", |
| 4547 | current->comm, current->pid, preempt_count())) |
| 4548 | preempt_count_set(FORK_PREEMPT_COUNT); |
Peter Zijlstra | 609ca06 | 2015-09-28 17:52:18 +0200 | [diff] [blame] | 4549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | rq->prev_mm = NULL; |
| 4551 | |
| 4552 | /* |
| 4553 | * A task struct has one reference for the use as "current". |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 4554 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 4555 | * schedule one last time. The schedule call will never return, and |
| 4556 | * the scheduled task must drop that reference. |
Peter Zijlstra | 95913d9 | 2015-09-29 14:45:09 +0200 | [diff] [blame] | 4557 | * |
| 4558 | * We must observe prev->state before clearing prev->on_cpu (in |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4559 | * finish_task), otherwise a concurrent wakeup can get prev |
Peter Zijlstra | 95913d9 | 2015-09-29 14:45:09 +0200 | [diff] [blame] | 4560 | * running on another CPU and we could rave with its RUNNING -> DEAD |
| 4561 | * transition, resulting in a double drop. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | */ |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 4563 | prev_state = prev->state; |
Frederic Weisbecker | bf9fae9 | 2012-09-08 15:23:11 +0200 | [diff] [blame] | 4564 | vtime_task_switch(prev); |
Stephane Eranian | a8d757e | 2011-08-25 15:58:03 +0200 | [diff] [blame] | 4565 | perf_event_task_sched_in(prev, current); |
rodrigosiqueira | 31cb1bc | 2017-12-15 12:06:03 -0200 | [diff] [blame] | 4566 | finish_task(prev); |
| 4567 | finish_lock_switch(rq); |
Catalin Marinas | 01f23e1 | 2011-11-27 21:43:10 +0000 | [diff] [blame] | 4568 | finish_arch_post_lock_switch(); |
Mark Rutland | 0ed557a | 2018-06-14 15:27:41 -0700 | [diff] [blame] | 4569 | kcov_finish_switch(current); |
Thomas Gleixner | 5fbda3e | 2020-11-18 20:48:43 +0100 | [diff] [blame] | 4570 | /* |
| 4571 | * kmap_local_sched_out() is invoked with rq::lock held and |
| 4572 | * interrupts disabled. There is no requirement for that, but the |
| 4573 | * sched out code does not have an interrupt enabled section. |
| 4574 | * Restoring the maps on sched in does not require interrupts being |
| 4575 | * disabled either. |
| 4576 | */ |
| 4577 | kmap_local_sched_in(); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 4578 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4579 | fire_sched_in_preempt_notifiers(current); |
Mathieu Desnoyers | 306e060 | 2018-01-29 15:20:12 -0500 | [diff] [blame] | 4580 | /* |
Mathieu Desnoyers | 70216e1 | 2018-01-29 15:20:17 -0500 | [diff] [blame] | 4581 | * When switching through a kernel thread, the loop in |
| 4582 | * membarrier_{private,global}_expedited() may have observed that |
| 4583 | * kernel thread and not issued an IPI. It is therefore possible to |
| 4584 | * schedule between user->kernel->user threads without passing though |
| 4585 | * switch_mm(). Membarrier requires a barrier after storing to |
| 4586 | * rq->curr, before returning to userspace, so provide them here: |
| 4587 | * |
| 4588 | * - a full memory barrier for {PRIVATE,GLOBAL}_EXPEDITED, implicitly |
| 4589 | * provided by mmdrop(), |
| 4590 | * - a sync_core for SYNC_CORE. |
Mathieu Desnoyers | 306e060 | 2018-01-29 15:20:12 -0500 | [diff] [blame] | 4591 | */ |
Mathieu Desnoyers | 70216e1 | 2018-01-29 15:20:17 -0500 | [diff] [blame] | 4592 | if (mm) { |
| 4593 | membarrier_mm_sync_core_before_usermode(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | mmdrop(mm); |
Mathieu Desnoyers | 70216e1 | 2018-01-29 15:20:17 -0500 | [diff] [blame] | 4595 | } |
Peter Zijlstra | 1cef115 | 2018-06-07 11:45:49 +0200 | [diff] [blame] | 4596 | if (unlikely(prev_state == TASK_DEAD)) { |
| 4597 | if (prev->sched_class->task_dead) |
| 4598 | prev->sched_class->task_dead(prev); |
Dario Faggioli | e6c390f | 2013-11-07 14:43:35 +0100 | [diff] [blame] | 4599 | |
Peter Zijlstra | 1cef115 | 2018-06-07 11:45:49 +0200 | [diff] [blame] | 4600 | /* |
| 4601 | * Remove function-return probe instances associated with this |
| 4602 | * task and put them back on the free list. |
| 4603 | */ |
| 4604 | kprobe_flush_task(prev); |
Andy Lutomirski | 68f24b08 | 2016-09-15 22:45:48 -0700 | [diff] [blame] | 4605 | |
Peter Zijlstra | 1cef115 | 2018-06-07 11:45:49 +0200 | [diff] [blame] | 4606 | /* Task is done with its stack. */ |
| 4607 | put_task_stack(prev); |
Andy Lutomirski | 68f24b08 | 2016-09-15 22:45:48 -0700 | [diff] [blame] | 4608 | |
Eric W. Biederman | 0ff7b2c | 2019-09-14 07:33:58 -0500 | [diff] [blame] | 4609 | put_task_struct_rcu_user(prev); |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 4610 | } |
Frederic Weisbecker | 99e5ada | 2013-04-20 17:11:50 +0200 | [diff] [blame] | 4611 | |
Frederic Weisbecker | de734f8 | 2015-06-11 18:07:12 +0200 | [diff] [blame] | 4612 | tick_nohz_task_switch(); |
Oleg Nesterov | dfa50b6 | 2014-10-09 21:32:32 +0200 | [diff] [blame] | 4613 | return rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | /** |
| 4617 | * schedule_tail - first thing a freshly forked thread must call. |
| 4618 | * @prev: the thread we just switched away from. |
| 4619 | */ |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 4620 | asmlinkage __visible void schedule_tail(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | __releases(rq->lock) |
| 4622 | { |
Peter Zijlstra | 609ca06 | 2015-09-28 17:52:18 +0200 | [diff] [blame] | 4623 | /* |
| 4624 | * New tasks start with FORK_PREEMPT_COUNT, see there and |
| 4625 | * finish_task_switch() for details. |
| 4626 | * |
| 4627 | * finish_task_switch() will drop rq->lock() and lower preempt_count |
| 4628 | * and the preempt_enable() will end up enabling preemption (on |
| 4629 | * PREEMPT_COUNT kernels). |
| 4630 | */ |
| 4631 | |
Edmundo Carmona Antoranz | 13c2235 | 2021-03-06 15:07:39 -0600 | [diff] [blame] | 4632 | finish_task_switch(prev); |
Oleg Nesterov | 1a43a14 | 2014-10-08 21:36:44 +0200 | [diff] [blame] | 4633 | preempt_enable(); |
Steven Rostedt | da19ab5 | 2009-07-29 00:21:22 -0400 | [diff] [blame] | 4634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | if (current->set_child_tid) |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 4636 | put_user(task_pid_vnr(current), current->set_child_tid); |
Eric W. Biederman | 088fe47 | 2018-07-23 17:26:49 -0500 | [diff] [blame] | 4637 | |
| 4638 | calculate_sigpending(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | /* |
Oleg Nesterov | dfa50b6 | 2014-10-09 21:32:32 +0200 | [diff] [blame] | 4642 | * context_switch - switch to the new MM and the new thread's register state. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | */ |
Josh Poimboeuf | 0493694 | 2016-02-28 22:22:39 -0600 | [diff] [blame] | 4644 | static __always_inline struct rq * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4645 | context_switch(struct rq *rq, struct task_struct *prev, |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 4646 | struct task_struct *next, struct rq_flags *rf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | { |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 4648 | prepare_task_switch(rq, prev, next); |
Peter Zijlstra | fe4b04f | 2011-02-02 13:19:09 +0100 | [diff] [blame] | 4649 | |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 4650 | /* |
| 4651 | * For paravirt, this is coupled with an exit in switch_to to |
| 4652 | * combine the page table reload and the switch backend into |
| 4653 | * one hypercall. |
| 4654 | */ |
Jeremy Fitzhardinge | 224101e | 2009-02-18 11:18:57 -0800 | [diff] [blame] | 4655 | arch_start_context_switch(prev); |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 4656 | |
Mathieu Desnoyers | 306e060 | 2018-01-29 15:20:12 -0500 | [diff] [blame] | 4657 | /* |
Peter Zijlstra | 139d025 | 2019-07-29 16:05:15 +0200 | [diff] [blame] | 4658 | * kernel -> kernel lazy + transfer active |
| 4659 | * user -> kernel lazy + mmgrab() active |
| 4660 | * |
| 4661 | * kernel -> user switch + mmdrop() active |
| 4662 | * user -> user switch |
Mathieu Desnoyers | 306e060 | 2018-01-29 15:20:12 -0500 | [diff] [blame] | 4663 | */ |
Peter Zijlstra | 139d025 | 2019-07-29 16:05:15 +0200 | [diff] [blame] | 4664 | if (!next->mm) { // to kernel |
| 4665 | enter_lazy_tlb(prev->active_mm, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | |
Peter Zijlstra | 139d025 | 2019-07-29 16:05:15 +0200 | [diff] [blame] | 4667 | next->active_mm = prev->active_mm; |
| 4668 | if (prev->mm) // from user |
| 4669 | mmgrab(prev->active_mm); |
| 4670 | else |
| 4671 | prev->active_mm = NULL; |
| 4672 | } else { // to user |
Mathieu Desnoyers | 227a4aa | 2019-09-19 13:37:02 -0400 | [diff] [blame] | 4673 | membarrier_switch_mm(rq, prev->active_mm, next->mm); |
Peter Zijlstra | 139d025 | 2019-07-29 16:05:15 +0200 | [diff] [blame] | 4674 | /* |
| 4675 | * sys_membarrier() requires an smp_mb() between setting |
Mathieu Desnoyers | 227a4aa | 2019-09-19 13:37:02 -0400 | [diff] [blame] | 4676 | * rq->curr / membarrier_switch_mm() and returning to userspace. |
Peter Zijlstra | 139d025 | 2019-07-29 16:05:15 +0200 | [diff] [blame] | 4677 | * |
| 4678 | * The below provides this either through switch_mm(), or in |
| 4679 | * case 'prev->active_mm == next->mm' through |
| 4680 | * finish_task_switch()'s mmdrop(). |
| 4681 | */ |
Peter Zijlstra | 139d025 | 2019-07-29 16:05:15 +0200 | [diff] [blame] | 4682 | switch_mm_irqs_off(prev->active_mm, next->mm, next); |
| 4683 | |
| 4684 | if (!prev->mm) { // from kernel |
| 4685 | /* will mmdrop() in finish_task_switch(). */ |
| 4686 | rq->prev_mm = prev->active_mm; |
| 4687 | prev->active_mm = NULL; |
| 4688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | } |
Matt Fleming | 92509b7 | 2016-09-21 14:38:11 +0100 | [diff] [blame] | 4690 | |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 4691 | rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP); |
Matt Fleming | 92509b7 | 2016-09-21 14:38:11 +0100 | [diff] [blame] | 4692 | |
Peter Zijlstra | 269d599 | 2018-02-06 17:52:13 +0100 | [diff] [blame] | 4693 | prepare_lock_switch(rq, next, rf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | |
| 4695 | /* Here we just switch the register state and the stack. */ |
| 4696 | switch_to(prev, next, prev); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4697 | barrier(); |
Oleg Nesterov | dfa50b6 | 2014-10-09 21:32:32 +0200 | [diff] [blame] | 4698 | |
| 4699 | return finish_task_switch(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | /* |
Sha Zhengju | 1c3e826 | 2013-02-20 17:14:38 +0800 | [diff] [blame] | 4703 | * nr_running and nr_context_switches: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | * |
| 4705 | * externally visible scheduler statistics: current number of runnable |
Sha Zhengju | 1c3e826 | 2013-02-20 17:14:38 +0800 | [diff] [blame] | 4706 | * threads, total number of context switches performed since bootup. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4707 | */ |
Alexey Dobriyan | 01aee8f | 2021-04-22 23:02:25 +0300 | [diff] [blame] | 4708 | unsigned int nr_running(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | { |
Alexey Dobriyan | 01aee8f | 2021-04-22 23:02:25 +0300 | [diff] [blame] | 4710 | unsigned int i, sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | |
| 4712 | for_each_online_cpu(i) |
| 4713 | sum += cpu_rq(i)->nr_running; |
| 4714 | |
| 4715 | return sum; |
| 4716 | } |
| 4717 | |
Tim Chen | 2ee507c | 2014-07-31 10:29:48 -0700 | [diff] [blame] | 4718 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 4719 | * Check if only the current task is running on the CPU. |
Dominik Dingel | 00cc1633 | 2015-09-18 11:27:45 +0200 | [diff] [blame] | 4720 | * |
| 4721 | * Caution: this function does not check that the caller has disabled |
| 4722 | * preemption, thus the result might have a time-of-check-to-time-of-use |
| 4723 | * race. The caller is responsible to use it correctly, for example: |
| 4724 | * |
Ingo Molnar | dfcb245 | 2018-12-03 10:05:56 +0100 | [diff] [blame] | 4725 | * - from a non-preemptible section (of course) |
Dominik Dingel | 00cc1633 | 2015-09-18 11:27:45 +0200 | [diff] [blame] | 4726 | * |
| 4727 | * - from a thread that is bound to a single CPU |
| 4728 | * |
| 4729 | * - in a loop with very short iterations (e.g. a polling loop) |
Tim Chen | 2ee507c | 2014-07-31 10:29:48 -0700 | [diff] [blame] | 4730 | */ |
| 4731 | bool single_task_running(void) |
| 4732 | { |
Dominik Dingel | 00cc1633 | 2015-09-18 11:27:45 +0200 | [diff] [blame] | 4733 | return raw_rq()->nr_running == 1; |
Tim Chen | 2ee507c | 2014-07-31 10:29:48 -0700 | [diff] [blame] | 4734 | } |
| 4735 | EXPORT_SYMBOL(single_task_running); |
| 4736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | unsigned long long nr_context_switches(void) |
| 4738 | { |
Steven Rostedt | cc94abf | 2006-06-27 02:54:31 -0700 | [diff] [blame] | 4739 | int i; |
| 4740 | unsigned long long sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 4742 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | sum += cpu_rq(i)->nr_switches; |
| 4744 | |
| 4745 | return sum; |
| 4746 | } |
| 4747 | |
Tejun Heo | e33a9bb | 2016-12-07 15:48:41 -0500 | [diff] [blame] | 4748 | /* |
Daniel Lezcano | 145d952 | 2018-10-04 14:04:02 +0200 | [diff] [blame] | 4749 | * Consumers of these two interfaces, like for example the cpuidle menu |
| 4750 | * governor, are using nonsensical data. Preferring shallow idle state selection |
| 4751 | * for a CPU that has IO-wait which might not even end up running the task when |
| 4752 | * it does become runnable. |
| 4753 | */ |
| 4754 | |
Alexey Dobriyan | 8fc2858 | 2021-04-22 23:02:27 +0300 | [diff] [blame] | 4755 | unsigned int nr_iowait_cpu(int cpu) |
Daniel Lezcano | 145d952 | 2018-10-04 14:04:02 +0200 | [diff] [blame] | 4756 | { |
| 4757 | return atomic_read(&cpu_rq(cpu)->nr_iowait); |
| 4758 | } |
| 4759 | |
Daniel Lezcano | 145d952 | 2018-10-04 14:04:02 +0200 | [diff] [blame] | 4760 | /* |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 4761 | * IO-wait accounting, and how it's mostly bollocks (on SMP). |
Tejun Heo | e33a9bb | 2016-12-07 15:48:41 -0500 | [diff] [blame] | 4762 | * |
| 4763 | * The idea behind IO-wait account is to account the idle time that we could |
| 4764 | * have spend running if it were not for IO. That is, if we were to improve the |
| 4765 | * storage performance, we'd have a proportional reduction in IO-wait time. |
| 4766 | * |
| 4767 | * This all works nicely on UP, where, when a task blocks on IO, we account |
| 4768 | * idle time as IO-wait, because if the storage were faster, it could've been |
| 4769 | * running and we'd not be idle. |
| 4770 | * |
| 4771 | * This has been extended to SMP, by doing the same for each CPU. This however |
| 4772 | * is broken. |
| 4773 | * |
| 4774 | * Imagine for instance the case where two tasks block on one CPU, only the one |
| 4775 | * CPU will have IO-wait accounted, while the other has regular idle. Even |
| 4776 | * though, if the storage were faster, both could've ran at the same time, |
| 4777 | * utilising both CPUs. |
| 4778 | * |
| 4779 | * This means, that when looking globally, the current IO-wait accounting on |
| 4780 | * SMP is a lower bound, by reason of under accounting. |
| 4781 | * |
| 4782 | * Worse, since the numbers are provided per CPU, they are sometimes |
| 4783 | * interpreted per CPU, and that is nonsensical. A blocked task isn't strictly |
| 4784 | * associated with any one particular CPU, it can wake to another CPU than it |
| 4785 | * blocked on. This means the per CPU IO-wait number is meaningless. |
| 4786 | * |
| 4787 | * Task CPU affinities can make all that even more 'interesting'. |
| 4788 | */ |
| 4789 | |
Alexey Dobriyan | 9745516 | 2021-04-22 23:02:26 +0300 | [diff] [blame] | 4790 | unsigned int nr_iowait(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | { |
Alexey Dobriyan | 9745516 | 2021-04-22 23:02:26 +0300 | [diff] [blame] | 4792 | unsigned int i, sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4793 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 4794 | for_each_possible_cpu(i) |
Daniel Lezcano | 145d952 | 2018-10-04 14:04:02 +0200 | [diff] [blame] | 4795 | sum += nr_iowait_cpu(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | |
| 4797 | return sum; |
| 4798 | } |
| 4799 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4800 | #ifdef CONFIG_SMP |
| 4801 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4802 | /* |
Peter Zijlstra | 3802290 | 2009-12-16 18:04:37 +0100 | [diff] [blame] | 4803 | * sched_exec - execve() is a valuable balancing opportunity, because at |
| 4804 | * this point the task has the smallest effective memory and cache footprint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | */ |
Peter Zijlstra | 3802290 | 2009-12-16 18:04:37 +0100 | [diff] [blame] | 4806 | void sched_exec(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | { |
Peter Zijlstra | 3802290 | 2009-12-16 18:04:37 +0100 | [diff] [blame] | 4808 | struct task_struct *p = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | unsigned long flags; |
Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 4810 | int dest_cpu; |
Peter Zijlstra | 3802290 | 2009-12-16 18:04:37 +0100 | [diff] [blame] | 4811 | |
Peter Zijlstra | 8f42ced | 2011-04-05 17:23:53 +0200 | [diff] [blame] | 4812 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
Valentin Schneider | 3aef155 | 2020-11-02 18:45:13 +0000 | [diff] [blame] | 4813 | dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), WF_EXEC); |
Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 4814 | if (dest_cpu == smp_processor_id()) |
| 4815 | goto unlock; |
Peter Zijlstra | 3802290 | 2009-12-16 18:04:37 +0100 | [diff] [blame] | 4816 | |
Peter Zijlstra | 8f42ced | 2011-04-05 17:23:53 +0200 | [diff] [blame] | 4817 | if (likely(cpu_active(dest_cpu))) { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4818 | struct migration_arg arg = { p, dest_cpu }; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4819 | |
Peter Zijlstra | 8f42ced | 2011-04-05 17:23:53 +0200 | [diff] [blame] | 4820 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
| 4821 | stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | return; |
| 4823 | } |
Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 4824 | unlock: |
Peter Zijlstra | 8f42ced | 2011-04-05 17:23:53 +0200 | [diff] [blame] | 4825 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | } |
| 4827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | #endif |
| 4829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 4831 | DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | |
| 4833 | EXPORT_PER_CPU_SYMBOL(kstat); |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 4834 | EXPORT_PER_CPU_SYMBOL(kernel_cpustat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | |
| 4836 | /* |
Giovanni Gherdovich | 6075620 | 2016-08-05 10:21:56 +0200 | [diff] [blame] | 4837 | * The function fair_sched_class.update_curr accesses the struct curr |
| 4838 | * and its field curr->exec_start; when called from task_sched_runtime(), |
| 4839 | * we observe a high rate of cache misses in practice. |
| 4840 | * Prefetching this data results in improved performance. |
| 4841 | */ |
| 4842 | static inline void prefetch_curr_exec_start(struct task_struct *p) |
| 4843 | { |
| 4844 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 4845 | struct sched_entity *curr = (&p->se)->cfs_rq->curr; |
| 4846 | #else |
| 4847 | struct sched_entity *curr = (&task_rq(p)->cfs)->curr; |
| 4848 | #endif |
| 4849 | prefetch(curr); |
| 4850 | prefetch(&curr->exec_start); |
| 4851 | } |
| 4852 | |
| 4853 | /* |
Hidetoshi Seto | c5f8d99 | 2009-03-31 16:56:03 +0900 | [diff] [blame] | 4854 | * Return accounted runtime for the task. |
| 4855 | * In case the task is currently running, return the runtime plus current's |
| 4856 | * pending runtime that have not been accounted yet. |
| 4857 | */ |
| 4858 | unsigned long long task_sched_runtime(struct task_struct *p) |
| 4859 | { |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 4860 | struct rq_flags rf; |
Hidetoshi Seto | c5f8d99 | 2009-03-31 16:56:03 +0900 | [diff] [blame] | 4861 | struct rq *rq; |
Stanislaw Gruszka | 6e99891 | 2014-11-12 16:58:44 +0100 | [diff] [blame] | 4862 | u64 ns; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4863 | |
Peter Zijlstra | 911b289 | 2013-11-11 18:21:56 +0100 | [diff] [blame] | 4864 | #if defined(CONFIG_64BIT) && defined(CONFIG_SMP) |
| 4865 | /* |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 4866 | * 64-bit doesn't need locks to atomically read a 64-bit value. |
Peter Zijlstra | 911b289 | 2013-11-11 18:21:56 +0100 | [diff] [blame] | 4867 | * So we have a optimization chance when the task's delta_exec is 0. |
| 4868 | * Reading ->on_cpu is racy, but this is ok. |
| 4869 | * |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 4870 | * If we race with it leaving CPU, we'll take a lock. So we're correct. |
| 4871 | * If we race with it entering CPU, unaccounted time is 0. This is |
Peter Zijlstra | 911b289 | 2013-11-11 18:21:56 +0100 | [diff] [blame] | 4872 | * indistinguishable from the read occurring a few cycles earlier. |
Mike Galbraith | 4036ac1 | 2014-06-24 07:49:40 +0200 | [diff] [blame] | 4873 | * If we see ->on_cpu without ->on_rq, the task is leaving, and has |
| 4874 | * been accounted, so we're correct here as well. |
Peter Zijlstra | 911b289 | 2013-11-11 18:21:56 +0100 | [diff] [blame] | 4875 | */ |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 4876 | if (!p->on_cpu || !task_on_rq_queued(p)) |
Peter Zijlstra | 911b289 | 2013-11-11 18:21:56 +0100 | [diff] [blame] | 4877 | return p->se.sum_exec_runtime; |
| 4878 | #endif |
| 4879 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 4880 | rq = task_rq_lock(p, &rf); |
Stanislaw Gruszka | 6e99891 | 2014-11-12 16:58:44 +0100 | [diff] [blame] | 4881 | /* |
| 4882 | * Must be ->curr _and_ ->on_rq. If dequeued, we would |
| 4883 | * project cycles that may never be accounted to this |
| 4884 | * thread, breaking clock_gettime(). |
| 4885 | */ |
| 4886 | if (task_current(rq, p) && task_on_rq_queued(p)) { |
Giovanni Gherdovich | 6075620 | 2016-08-05 10:21:56 +0200 | [diff] [blame] | 4887 | prefetch_curr_exec_start(p); |
Stanislaw Gruszka | 6e99891 | 2014-11-12 16:58:44 +0100 | [diff] [blame] | 4888 | update_rq_clock(rq); |
| 4889 | p->sched_class->update_curr(rq); |
| 4890 | } |
| 4891 | ns = p->se.sum_exec_runtime; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 4892 | task_rq_unlock(rq, p, &rf); |
Hidetoshi Seto | c5f8d99 | 2009-03-31 16:56:03 +0900 | [diff] [blame] | 4893 | |
| 4894 | return ns; |
| 4895 | } |
| 4896 | |
Paul Turner | c006fac | 2021-04-16 14:29:36 -0700 | [diff] [blame] | 4897 | #ifdef CONFIG_SCHED_DEBUG |
| 4898 | static u64 cpu_resched_latency(struct rq *rq) |
| 4899 | { |
| 4900 | int latency_warn_ms = READ_ONCE(sysctl_resched_latency_warn_ms); |
| 4901 | u64 resched_latency, now = rq_clock(rq); |
| 4902 | static bool warned_once; |
| 4903 | |
| 4904 | if (sysctl_resched_latency_warn_once && warned_once) |
| 4905 | return 0; |
| 4906 | |
| 4907 | if (!need_resched() || !latency_warn_ms) |
| 4908 | return 0; |
| 4909 | |
| 4910 | if (system_state == SYSTEM_BOOTING) |
| 4911 | return 0; |
| 4912 | |
| 4913 | if (!rq->last_seen_need_resched_ns) { |
| 4914 | rq->last_seen_need_resched_ns = now; |
| 4915 | rq->ticks_without_resched = 0; |
| 4916 | return 0; |
| 4917 | } |
| 4918 | |
| 4919 | rq->ticks_without_resched++; |
| 4920 | resched_latency = now - rq->last_seen_need_resched_ns; |
| 4921 | if (resched_latency <= latency_warn_ms * NSEC_PER_MSEC) |
| 4922 | return 0; |
| 4923 | |
| 4924 | warned_once = true; |
| 4925 | |
| 4926 | return resched_latency; |
| 4927 | } |
| 4928 | |
| 4929 | static int __init setup_resched_latency_warn_ms(char *str) |
| 4930 | { |
| 4931 | long val; |
| 4932 | |
| 4933 | if ((kstrtol(str, 0, &val))) { |
| 4934 | pr_warn("Unable to set resched_latency_warn_ms\n"); |
| 4935 | return 1; |
| 4936 | } |
| 4937 | |
| 4938 | sysctl_resched_latency_warn_ms = val; |
| 4939 | return 1; |
| 4940 | } |
| 4941 | __setup("resched_latency_warn_ms=", setup_resched_latency_warn_ms); |
| 4942 | #else |
| 4943 | static inline u64 cpu_resched_latency(struct rq *rq) { return 0; } |
| 4944 | #endif /* CONFIG_SCHED_DEBUG */ |
| 4945 | |
Balbir Singh | 4904862 | 2008-09-05 18:12:23 +0200 | [diff] [blame] | 4946 | /* |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4947 | * This function gets called by the timer code, with HZ frequency. |
| 4948 | * We call it with interrupts disabled. |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4949 | */ |
| 4950 | void scheduler_tick(void) |
| 4951 | { |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4952 | int cpu = smp_processor_id(); |
| 4953 | struct rq *rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4954 | struct task_struct *curr = rq->curr; |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 4955 | struct rq_flags rf; |
Thara Gopinath | b4eccf5 | 2020-02-21 19:52:10 -0500 | [diff] [blame] | 4956 | unsigned long thermal_pressure; |
Paul Turner | c006fac | 2021-04-16 14:29:36 -0700 | [diff] [blame] | 4957 | u64 resched_latency; |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 4958 | |
Giovanni Gherdovich | 1567c3e3 | 2020-01-22 16:16:12 +0100 | [diff] [blame] | 4959 | arch_scale_freq_tick(); |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 4960 | sched_clock_tick(); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 4961 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 4962 | rq_lock(rq, &rf); |
| 4963 | |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 4964 | update_rq_clock(rq); |
Thara Gopinath | b4eccf5 | 2020-02-21 19:52:10 -0500 | [diff] [blame] | 4965 | thermal_pressure = arch_scale_thermal_pressure(cpu_of(rq)); |
Thara Gopinath | 05289b9 | 2020-02-21 19:52:13 -0500 | [diff] [blame] | 4966 | update_thermal_load_avg(rq_clock_thermal(rq), rq, thermal_pressure); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4967 | curr->sched_class->task_tick(rq, curr, 0); |
Paul Turner | c006fac | 2021-04-16 14:29:36 -0700 | [diff] [blame] | 4968 | if (sched_feat(LATENCY_WARN)) |
| 4969 | resched_latency = cpu_resched_latency(rq); |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4970 | calc_global_load_tick(rq); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 4971 | |
| 4972 | rq_unlock(rq, &rf); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 4973 | |
Paul Turner | c006fac | 2021-04-16 14:29:36 -0700 | [diff] [blame] | 4974 | if (sched_feat(LATENCY_WARN) && resched_latency) |
| 4975 | resched_latency_warn(cpu, resched_latency); |
| 4976 | |
Peter Zijlstra | e9d2b06 | 2010-09-17 11:28:50 +0200 | [diff] [blame] | 4977 | perf_event_task_tick(); |
Peter Zijlstra | e220d2d | 2009-05-23 18:28:55 +0200 | [diff] [blame] | 4978 | |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 4979 | #ifdef CONFIG_SMP |
Suresh Siddha | 6eb57e0 | 2011-10-03 15:09:01 -0700 | [diff] [blame] | 4980 | rq->idle_balance = idle_cpu(cpu); |
Daniel Lezcano | 7caff66 | 2014-01-06 12:34:38 +0100 | [diff] [blame] | 4981 | trigger_load_balance(rq); |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 4982 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | } |
| 4984 | |
Frederic Weisbecker | 265f22a | 2013-05-03 03:39:05 +0200 | [diff] [blame] | 4985 | #ifdef CONFIG_NO_HZ_FULL |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 4986 | |
| 4987 | struct tick_work { |
| 4988 | int cpu; |
Paul E. McKenney | b55bd58 | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 4989 | atomic_t state; |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 4990 | struct delayed_work work; |
| 4991 | }; |
Paul E. McKenney | b55bd58 | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 4992 | /* Values for ->state, see diagram below. */ |
| 4993 | #define TICK_SCHED_REMOTE_OFFLINE 0 |
| 4994 | #define TICK_SCHED_REMOTE_OFFLINING 1 |
| 4995 | #define TICK_SCHED_REMOTE_RUNNING 2 |
| 4996 | |
| 4997 | /* |
| 4998 | * State diagram for ->state: |
| 4999 | * |
| 5000 | * |
| 5001 | * TICK_SCHED_REMOTE_OFFLINE |
| 5002 | * | ^ |
| 5003 | * | | |
| 5004 | * | | sched_tick_remote() |
| 5005 | * | | |
| 5006 | * | | |
| 5007 | * +--TICK_SCHED_REMOTE_OFFLINING |
| 5008 | * | ^ |
| 5009 | * | | |
| 5010 | * sched_tick_start() | | sched_tick_stop() |
| 5011 | * | | |
| 5012 | * V | |
| 5013 | * TICK_SCHED_REMOTE_RUNNING |
| 5014 | * |
| 5015 | * |
| 5016 | * Other transitions get WARN_ON_ONCE(), except that sched_tick_remote() |
| 5017 | * and sched_tick_start() are happy to leave the state in RUNNING. |
| 5018 | */ |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5019 | |
| 5020 | static struct tick_work __percpu *tick_work_cpu; |
| 5021 | |
| 5022 | static void sched_tick_remote(struct work_struct *work) |
| 5023 | { |
| 5024 | struct delayed_work *dwork = to_delayed_work(work); |
| 5025 | struct tick_work *twork = container_of(dwork, struct tick_work, work); |
| 5026 | int cpu = twork->cpu; |
| 5027 | struct rq *rq = cpu_rq(cpu); |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5028 | struct task_struct *curr; |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5029 | struct rq_flags rf; |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5030 | u64 delta; |
Paul E. McKenney | b55bd58 | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 5031 | int os; |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5032 | |
| 5033 | /* |
| 5034 | * Handle the tick only if it appears the remote CPU is running in full |
| 5035 | * dynticks mode. The check is racy by nature, but missing a tick or |
| 5036 | * having one too much is no big deal because the scheduler tick updates |
| 5037 | * statistics and checks timeslices in a time-independent way, regardless |
| 5038 | * of when exactly it is running. |
| 5039 | */ |
Scott Wood | 488603b | 2020-01-11 04:53:38 -0500 | [diff] [blame] | 5040 | if (!tick_nohz_tick_stopped_cpu(cpu)) |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5041 | goto out_requeue; |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5042 | |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5043 | rq_lock_irq(rq, &rf); |
| 5044 | curr = rq->curr; |
Scott Wood | 488603b | 2020-01-11 04:53:38 -0500 | [diff] [blame] | 5045 | if (cpu_is_offline(cpu)) |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5046 | goto out_unlock; |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5047 | |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5048 | update_rq_clock(rq); |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5049 | |
Scott Wood | 488603b | 2020-01-11 04:53:38 -0500 | [diff] [blame] | 5050 | if (!is_idle_task(curr)) { |
| 5051 | /* |
| 5052 | * Make sure the next tick runs within a reasonable |
| 5053 | * amount of time. |
| 5054 | */ |
| 5055 | delta = rq_clock_task(rq) - curr->se.exec_start; |
| 5056 | WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 3); |
| 5057 | } |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5058 | curr->sched_class->task_tick(rq, curr, 0); |
| 5059 | |
Peter Zijlstra (Intel) | ebc0f83 | 2020-01-11 04:53:39 -0500 | [diff] [blame] | 5060 | calc_load_nohz_remote(rq); |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5061 | out_unlock: |
| 5062 | rq_unlock_irq(rq, &rf); |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5063 | out_requeue: |
Peter Zijlstra (Intel) | ebc0f83 | 2020-01-11 04:53:39 -0500 | [diff] [blame] | 5064 | |
Frederic Weisbecker | d9c0ffc | 2018-06-28 18:29:41 +0200 | [diff] [blame] | 5065 | /* |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5066 | * Run the remote tick once per second (1Hz). This arbitrary |
| 5067 | * frequency is large enough to avoid overload but short enough |
Paul E. McKenney | b55bd58 | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 5068 | * to keep scheduler internal stats reasonably up to date. But |
| 5069 | * first update state to reflect hotplug activity if required. |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5070 | */ |
Paul E. McKenney | b55bd58 | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 5071 | os = atomic_fetch_add_unless(&twork->state, -1, TICK_SCHED_REMOTE_RUNNING); |
| 5072 | WARN_ON_ONCE(os == TICK_SCHED_REMOTE_OFFLINE); |
| 5073 | if (os == TICK_SCHED_REMOTE_RUNNING) |
| 5074 | queue_delayed_work(system_unbound_wq, dwork, HZ); |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5075 | } |
| 5076 | |
| 5077 | static void sched_tick_start(int cpu) |
| 5078 | { |
Paul E. McKenney | b55bd58 | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 5079 | int os; |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5080 | struct tick_work *twork; |
| 5081 | |
| 5082 | if (housekeeping_cpu(cpu, HK_FLAG_TICK)) |
| 5083 | return; |
| 5084 | |
| 5085 | WARN_ON_ONCE(!tick_work_cpu); |
| 5086 | |
| 5087 | twork = per_cpu_ptr(tick_work_cpu, cpu); |
Paul E. McKenney | b55bd58 | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 5088 | os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_RUNNING); |
| 5089 | WARN_ON_ONCE(os == TICK_SCHED_REMOTE_RUNNING); |
| 5090 | if (os == TICK_SCHED_REMOTE_OFFLINE) { |
| 5091 | twork->cpu = cpu; |
| 5092 | INIT_DELAYED_WORK(&twork->work, sched_tick_remote); |
| 5093 | queue_delayed_work(system_unbound_wq, &twork->work, HZ); |
| 5094 | } |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | #ifdef CONFIG_HOTPLUG_CPU |
| 5098 | static void sched_tick_stop(int cpu) |
| 5099 | { |
| 5100 | struct tick_work *twork; |
Paul E. McKenney | b55bd58 | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 5101 | int os; |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5102 | |
| 5103 | if (housekeeping_cpu(cpu, HK_FLAG_TICK)) |
| 5104 | return; |
| 5105 | |
| 5106 | WARN_ON_ONCE(!tick_work_cpu); |
| 5107 | |
| 5108 | twork = per_cpu_ptr(tick_work_cpu, cpu); |
Paul E. McKenney | b55bd58 | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 5109 | /* There cannot be competing actions, but don't rely on stop-machine. */ |
| 5110 | os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_OFFLINING); |
| 5111 | WARN_ON_ONCE(os != TICK_SCHED_REMOTE_RUNNING); |
| 5112 | /* Don't cancel, as this would mess up the state machine. */ |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5113 | } |
| 5114 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 5115 | |
| 5116 | int __init sched_tick_offload_init(void) |
| 5117 | { |
| 5118 | tick_work_cpu = alloc_percpu(struct tick_work); |
| 5119 | BUG_ON(!tick_work_cpu); |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 5120 | return 0; |
| 5121 | } |
| 5122 | |
| 5123 | #else /* !CONFIG_NO_HZ_FULL */ |
| 5124 | static inline void sched_tick_start(int cpu) { } |
| 5125 | static inline void sched_tick_stop(int cpu) { } |
Frederic Weisbecker | 265f22a | 2013-05-03 03:39:05 +0200 | [diff] [blame] | 5126 | #endif |
| 5127 | |
Thomas Gleixner | c1a280b | 2019-07-26 23:19:37 +0200 | [diff] [blame] | 5128 | #if defined(CONFIG_PREEMPTION) && (defined(CONFIG_DEBUG_PREEMPT) || \ |
Joel Fernandes (Google) | c3bc8fd | 2018-07-30 15:24:23 -0700 | [diff] [blame] | 5129 | defined(CONFIG_TRACE_PREEMPT_TOGGLE)) |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 5130 | /* |
| 5131 | * If the value passed in is equal to the current preempt count |
| 5132 | * then we just disabled preemption. Start timing the latency. |
| 5133 | */ |
| 5134 | static inline void preempt_latency_start(int val) |
| 5135 | { |
| 5136 | if (preempt_count() == val) { |
| 5137 | unsigned long ip = get_lock_parent_ip(); |
| 5138 | #ifdef CONFIG_DEBUG_PREEMPT |
| 5139 | current->preempt_disable_ip = ip; |
| 5140 | #endif |
| 5141 | trace_preempt_off(CALLER_ADDR0, ip); |
| 5142 | } |
| 5143 | } |
Steven Rostedt | 7e49fcc | 2009-01-22 19:01:40 -0500 | [diff] [blame] | 5144 | |
Masami Hiramatsu | edafe3a | 2014-04-17 17:18:42 +0900 | [diff] [blame] | 5145 | void preempt_count_add(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | { |
Steven Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5147 | #ifdef CONFIG_DEBUG_PREEMPT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | /* |
| 5149 | * Underflow? |
| 5150 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 5151 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
| 5152 | return; |
Steven Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5153 | #endif |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 5154 | __preempt_count_add(val); |
Steven Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5155 | #ifdef CONFIG_DEBUG_PREEMPT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | /* |
| 5157 | * Spinlock count overflowing soon? |
| 5158 | */ |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 5159 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
| 5160 | PREEMPT_MASK - 10); |
Steven Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5161 | #endif |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 5162 | preempt_latency_start(val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | } |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 5164 | EXPORT_SYMBOL(preempt_count_add); |
Masami Hiramatsu | edafe3a | 2014-04-17 17:18:42 +0900 | [diff] [blame] | 5165 | NOKPROBE_SYMBOL(preempt_count_add); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 5167 | /* |
| 5168 | * If the value passed in equals to the current preempt count |
| 5169 | * then we just enabled preemption. Stop timing the latency. |
| 5170 | */ |
| 5171 | static inline void preempt_latency_stop(int val) |
| 5172 | { |
| 5173 | if (preempt_count() == val) |
| 5174 | trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip()); |
| 5175 | } |
| 5176 | |
Masami Hiramatsu | edafe3a | 2014-04-17 17:18:42 +0900 | [diff] [blame] | 5177 | void preempt_count_sub(int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | { |
Steven Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5179 | #ifdef CONFIG_DEBUG_PREEMPT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | /* |
| 5181 | * Underflow? |
| 5182 | */ |
Ingo Molnar | 01e3eb8 | 2009-01-12 13:00:50 +0100 | [diff] [blame] | 5183 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 5184 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | /* |
| 5186 | * Is the spinlock portion underflowing? |
| 5187 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 5188 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
| 5189 | !(preempt_count() & PREEMPT_MASK))) |
| 5190 | return; |
Steven Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5191 | #endif |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 5192 | |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 5193 | preempt_latency_stop(val); |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 5194 | __preempt_count_sub(val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | } |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 5196 | EXPORT_SYMBOL(preempt_count_sub); |
Masami Hiramatsu | edafe3a | 2014-04-17 17:18:42 +0900 | [diff] [blame] | 5197 | NOKPROBE_SYMBOL(preempt_count_sub); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 5199 | #else |
| 5200 | static inline void preempt_latency_start(int val) { } |
| 5201 | static inline void preempt_latency_stop(int val) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | #endif |
| 5203 | |
Ingo Molnar | 59ddbcb | 2017-02-03 23:37:48 +0100 | [diff] [blame] | 5204 | static inline unsigned long get_preempt_disable_ip(struct task_struct *p) |
| 5205 | { |
| 5206 | #ifdef CONFIG_DEBUG_PREEMPT |
| 5207 | return p->preempt_disable_ip; |
| 5208 | #else |
| 5209 | return 0; |
| 5210 | #endif |
| 5211 | } |
| 5212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | /* |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5214 | * Print scheduling while atomic bug: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | */ |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5216 | static noinline void __schedule_bug(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | { |
Vegard Nossum | d1c6d14 | 2016-07-23 09:46:39 +0200 | [diff] [blame] | 5218 | /* Save this before calling printk(), since that will clobber it */ |
| 5219 | unsigned long preempt_disable_ip = get_preempt_disable_ip(current); |
| 5220 | |
Dave Jones | 664dfa6 | 2011-12-22 16:39:30 -0500 | [diff] [blame] | 5221 | if (oops_in_progress) |
| 5222 | return; |
| 5223 | |
Peter Zijlstra | 3df0fc5 | 2009-12-20 14:23:57 +0100 | [diff] [blame] | 5224 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
| 5225 | prev->comm, prev->pid, preempt_count()); |
Satyam Sharma | 838225b | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 5226 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5227 | debug_show_held_locks(prev); |
Arjan van de Ven | e21f5b1 | 2008-05-23 09:05:58 -0700 | [diff] [blame] | 5228 | print_modules(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5229 | if (irqs_disabled()) |
| 5230 | print_irqtrace_events(prev); |
Vegard Nossum | d1c6d14 | 2016-07-23 09:46:39 +0200 | [diff] [blame] | 5231 | if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) |
| 5232 | && in_atomic_preempt_off()) { |
Thomas Gleixner | 8f47b18 | 2014-02-07 20:58:39 +0100 | [diff] [blame] | 5233 | pr_err("Preemption disabled at:"); |
Dmitry Safonov | 2062a4e | 2020-06-08 21:29:56 -0700 | [diff] [blame] | 5234 | print_ip_sym(KERN_ERR, preempt_disable_ip); |
Thomas Gleixner | 8f47b18 | 2014-02-07 20:58:39 +0100 | [diff] [blame] | 5235 | } |
Daniel Bristot de Oliveira | 748c720 | 2016-06-03 17:10:18 -0300 | [diff] [blame] | 5236 | if (panic_on_warn) |
| 5237 | panic("scheduling while atomic\n"); |
| 5238 | |
Stephen Boyd | 6135fc1 | 2012-03-28 17:10:47 -0700 | [diff] [blame] | 5239 | dump_stack(); |
Rusty Russell | 373d4d0 | 2013-01-21 17:17:39 +1030 | [diff] [blame] | 5240 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5243 | /* |
| 5244 | * Various schedule()-time debugging checks and statistics: |
| 5245 | */ |
Daniel Vetter | 312364f3 | 2019-08-26 22:14:23 +0200 | [diff] [blame] | 5246 | static inline void schedule_debug(struct task_struct *prev, bool preempt) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5247 | { |
Aaron Tomlin | 0d9e263 | 2014-09-12 14:16:19 +0100 | [diff] [blame] | 5248 | #ifdef CONFIG_SCHED_STACK_END_CHECK |
Jann Horn | 29d6455 | 2016-06-01 11:55:07 +0200 | [diff] [blame] | 5249 | if (task_stack_end_corrupted(prev)) |
| 5250 | panic("corrupted stack end detected inside scheduler\n"); |
Will Deacon | 88485be | 2020-05-15 14:56:05 +0100 | [diff] [blame] | 5251 | |
| 5252 | if (task_scs_end_corrupted(prev)) |
| 5253 | panic("corrupted shadow stack detected inside scheduler\n"); |
Aaron Tomlin | 0d9e263 | 2014-09-12 14:16:19 +0100 | [diff] [blame] | 5254 | #endif |
Peter Zijlstra | b99def8 | 2015-09-28 18:02:03 +0200 | [diff] [blame] | 5255 | |
Daniel Vetter | 312364f3 | 2019-08-26 22:14:23 +0200 | [diff] [blame] | 5256 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
| 5257 | if (!preempt && prev->state && prev->non_block_count) { |
| 5258 | printk(KERN_ERR "BUG: scheduling in a non-blocking section: %s/%d/%i\n", |
| 5259 | prev->comm, prev->pid, prev->non_block_count); |
| 5260 | dump_stack(); |
| 5261 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
| 5262 | } |
| 5263 | #endif |
| 5264 | |
Peter Zijlstra | 1dc0fff | 2015-09-28 17:57:39 +0200 | [diff] [blame] | 5265 | if (unlikely(in_atomic_preempt_off())) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5266 | __schedule_bug(prev); |
Peter Zijlstra | 1dc0fff | 2015-09-28 17:57:39 +0200 | [diff] [blame] | 5267 | preempt_count_set(PREEMPT_DISABLED); |
| 5268 | } |
Paul E. McKenney | b3fbab0 | 2011-05-24 08:31:09 -0700 | [diff] [blame] | 5269 | rcu_sleep_check(); |
Frederic Weisbecker | 9f68b5b | 2020-11-17 16:16:35 +0100 | [diff] [blame] | 5270 | SCHED_WARN_ON(ct_state() == CONTEXT_USER); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| 5273 | |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 5274 | schedstat_inc(this_rq()->sched_count); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5275 | } |
| 5276 | |
Chen Yu | 457d1f4 | 2020-04-21 18:50:43 +0800 | [diff] [blame] | 5277 | static void put_prev_task_balance(struct rq *rq, struct task_struct *prev, |
| 5278 | struct rq_flags *rf) |
| 5279 | { |
| 5280 | #ifdef CONFIG_SMP |
| 5281 | const struct sched_class *class; |
| 5282 | /* |
| 5283 | * We must do the balancing pass before put_prev_task(), such |
| 5284 | * that when we release the rq->lock the task is in the same |
| 5285 | * state as before we took rq->lock. |
| 5286 | * |
| 5287 | * We can terminate the balance pass as soon as we know there is |
| 5288 | * a runnable task of @class priority or higher. |
| 5289 | */ |
| 5290 | for_class_range(class, prev->sched_class, &idle_sched_class) { |
| 5291 | if (class->balance(rq, prev, rf)) |
| 5292 | break; |
| 5293 | } |
| 5294 | #endif |
| 5295 | |
| 5296 | put_prev_task(rq, prev); |
| 5297 | } |
| 5298 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5299 | /* |
| 5300 | * Pick up the highest-prio task: |
| 5301 | */ |
| 5302 | static inline struct task_struct * |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5303 | __pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5304 | { |
Peter Zijlstra | 49ee576 | 2017-01-19 18:44:08 +0100 | [diff] [blame] | 5305 | const struct sched_class *class; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5306 | struct task_struct *p; |
| 5307 | |
| 5308 | /* |
Peter Zijlstra | 0ba87bb | 2017-03-01 10:51:47 +0100 | [diff] [blame] | 5309 | * Optimization: we know that if all tasks are in the fair class we can |
| 5310 | * call that function directly, but only if the @prev task wasn't of a |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 5311 | * higher scheduling class, because otherwise those lose the |
Peter Zijlstra | 0ba87bb | 2017-03-01 10:51:47 +0100 | [diff] [blame] | 5312 | * opportunity to pull in more work from other CPUs. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5313 | */ |
Kirill Tkhai | aa93cd5 | 2019-12-19 16:44:55 -0500 | [diff] [blame] | 5314 | if (likely(prev->sched_class <= &fair_sched_class && |
Peter Zijlstra | 0ba87bb | 2017-03-01 10:51:47 +0100 | [diff] [blame] | 5315 | rq->nr_running == rq->cfs.h_nr_running)) { |
| 5316 | |
Peter Zijlstra | 5d7d605 | 2019-11-08 14:15:57 +0100 | [diff] [blame] | 5317 | p = pick_next_task_fair(rq, prev, rf); |
Peter Zijlstra | 6ccdc84 | 2014-04-24 12:00:47 +0200 | [diff] [blame] | 5318 | if (unlikely(p == RETRY_TASK)) |
Peter Zijlstra | 6769243 | 2019-05-29 20:36:44 +0000 | [diff] [blame] | 5319 | goto restart; |
Peter Zijlstra | 6ccdc84 | 2014-04-24 12:00:47 +0200 | [diff] [blame] | 5320 | |
Masahiro Yamada | 1699949 | 2021-05-19 15:37:09 +0900 | [diff] [blame] | 5321 | /* Assume the next prioritized class is idle_sched_class */ |
Peter Zijlstra | 5d7d605 | 2019-11-08 14:15:57 +0100 | [diff] [blame] | 5322 | if (!p) { |
Peter Zijlstra | f488e10 | 2019-11-08 14:15:56 +0100 | [diff] [blame] | 5323 | put_prev_task(rq, prev); |
Peter Zijlstra | 98c2f70 | 2019-11-08 14:15:58 +0100 | [diff] [blame] | 5324 | p = pick_next_task_idle(rq); |
Peter Zijlstra | f488e10 | 2019-11-08 14:15:56 +0100 | [diff] [blame] | 5325 | } |
Peter Zijlstra | 6ccdc84 | 2014-04-24 12:00:47 +0200 | [diff] [blame] | 5326 | |
| 5327 | return p; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5328 | } |
| 5329 | |
Peter Zijlstra | 6769243 | 2019-05-29 20:36:44 +0000 | [diff] [blame] | 5330 | restart: |
Chen Yu | 457d1f4 | 2020-04-21 18:50:43 +0800 | [diff] [blame] | 5331 | put_prev_task_balance(rq, prev, rf); |
Peter Zijlstra | 6769243 | 2019-05-29 20:36:44 +0000 | [diff] [blame] | 5332 | |
Peter Zijlstra | 34f971f | 2010-09-22 13:53:15 +0200 | [diff] [blame] | 5333 | for_each_class(class) { |
Peter Zijlstra | 98c2f70 | 2019-11-08 14:15:58 +0100 | [diff] [blame] | 5334 | p = class->pick_next_task(rq); |
Peter Zijlstra | 6769243 | 2019-05-29 20:36:44 +0000 | [diff] [blame] | 5335 | if (p) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5336 | return p; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5337 | } |
Peter Zijlstra | 34f971f | 2010-09-22 13:53:15 +0200 | [diff] [blame] | 5338 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 5339 | /* The idle class should always have a runnable task: */ |
| 5340 | BUG(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5341 | } |
| 5342 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 5343 | #ifdef CONFIG_SCHED_CORE |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5344 | static inline bool is_task_rq_idle(struct task_struct *t) |
| 5345 | { |
| 5346 | return (task_rq(t)->idle == t); |
| 5347 | } |
| 5348 | |
| 5349 | static inline bool cookie_equals(struct task_struct *a, unsigned long cookie) |
| 5350 | { |
| 5351 | return is_task_rq_idle(a) || (a->core_cookie == cookie); |
| 5352 | } |
| 5353 | |
| 5354 | static inline bool cookie_match(struct task_struct *a, struct task_struct *b) |
| 5355 | { |
| 5356 | if (is_task_rq_idle(a) || is_task_rq_idle(b)) |
| 5357 | return true; |
| 5358 | |
| 5359 | return a->core_cookie == b->core_cookie; |
| 5360 | } |
| 5361 | |
| 5362 | // XXX fairness/fwd progress conditions |
| 5363 | /* |
| 5364 | * Returns |
| 5365 | * - NULL if there is no runnable task for this class. |
| 5366 | * - the highest priority task for this runqueue if it matches |
| 5367 | * rq->core->core_cookie or its priority is greater than max. |
| 5368 | * - Else returns idle_task. |
| 5369 | */ |
| 5370 | static struct task_struct * |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5371 | pick_task(struct rq *rq, const struct sched_class *class, struct task_struct *max, bool in_fi) |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5372 | { |
| 5373 | struct task_struct *class_pick, *cookie_pick; |
| 5374 | unsigned long cookie = rq->core->core_cookie; |
| 5375 | |
| 5376 | class_pick = class->pick_task(rq); |
| 5377 | if (!class_pick) |
| 5378 | return NULL; |
| 5379 | |
| 5380 | if (!cookie) { |
| 5381 | /* |
| 5382 | * If class_pick is tagged, return it only if it has |
| 5383 | * higher priority than max. |
| 5384 | */ |
| 5385 | if (max && class_pick->core_cookie && |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5386 | prio_less(class_pick, max, in_fi)) |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5387 | return idle_sched_class.pick_task(rq); |
| 5388 | |
| 5389 | return class_pick; |
| 5390 | } |
| 5391 | |
| 5392 | /* |
| 5393 | * If class_pick is idle or matches cookie, return early. |
| 5394 | */ |
| 5395 | if (cookie_equals(class_pick, cookie)) |
| 5396 | return class_pick; |
| 5397 | |
| 5398 | cookie_pick = sched_core_find(rq, cookie); |
| 5399 | |
| 5400 | /* |
| 5401 | * If class > max && class > cookie, it is the highest priority task on |
| 5402 | * the core (so far) and it must be selected, otherwise we must go with |
| 5403 | * the cookie pick in order to satisfy the constraint. |
| 5404 | */ |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5405 | if (prio_less(cookie_pick, class_pick, in_fi) && |
| 5406 | (!max || prio_less(max, class_pick, in_fi))) |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5407 | return class_pick; |
| 5408 | |
| 5409 | return cookie_pick; |
| 5410 | } |
| 5411 | |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5412 | extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi); |
| 5413 | |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5414 | static struct task_struct * |
| 5415 | pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
| 5416 | { |
| 5417 | struct task_struct *next, *max = NULL; |
| 5418 | const struct sched_class *class; |
| 5419 | const struct cpumask *smt_mask; |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5420 | bool fi_before = false; |
Peter Zijlstra | d2dfa17 | 2020-11-17 18:19:43 -0500 | [diff] [blame] | 5421 | int i, j, cpu, occ = 0; |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5422 | bool need_sync; |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5423 | |
| 5424 | if (!sched_core_enabled(rq)) |
| 5425 | return __pick_next_task(rq, prev, rf); |
| 5426 | |
| 5427 | cpu = cpu_of(rq); |
| 5428 | |
| 5429 | /* Stopper task is switching into idle, no need core-wide selection. */ |
| 5430 | if (cpu_is_offline(cpu)) { |
| 5431 | /* |
| 5432 | * Reset core_pick so that we don't enter the fastpath when |
| 5433 | * coming online. core_pick would already be migrated to |
| 5434 | * another cpu during offline. |
| 5435 | */ |
| 5436 | rq->core_pick = NULL; |
| 5437 | return __pick_next_task(rq, prev, rf); |
| 5438 | } |
| 5439 | |
| 5440 | /* |
| 5441 | * If there were no {en,de}queues since we picked (IOW, the task |
| 5442 | * pointers are all still valid), and we haven't scheduled the last |
| 5443 | * pick yet, do so now. |
| 5444 | * |
| 5445 | * rq->core_pick can be NULL if no selection was made for a CPU because |
| 5446 | * it was either offline or went offline during a sibling's core-wide |
| 5447 | * selection. In this case, do a core-wide selection. |
| 5448 | */ |
| 5449 | if (rq->core->core_pick_seq == rq->core->core_task_seq && |
| 5450 | rq->core->core_pick_seq != rq->core_sched_seq && |
| 5451 | rq->core_pick) { |
| 5452 | WRITE_ONCE(rq->core_sched_seq, rq->core->core_pick_seq); |
| 5453 | |
| 5454 | next = rq->core_pick; |
| 5455 | if (next != prev) { |
| 5456 | put_prev_task(rq, prev); |
| 5457 | set_next_task(rq, next); |
| 5458 | } |
| 5459 | |
| 5460 | rq->core_pick = NULL; |
| 5461 | return next; |
| 5462 | } |
| 5463 | |
| 5464 | put_prev_task_balance(rq, prev, rf); |
| 5465 | |
| 5466 | smt_mask = cpu_smt_mask(cpu); |
Joel Fernandes (Google) | 7afbba1 | 2020-11-17 18:19:42 -0500 | [diff] [blame] | 5467 | need_sync = !!rq->core->core_cookie; |
| 5468 | |
| 5469 | /* reset state */ |
| 5470 | rq->core->core_cookie = 0UL; |
| 5471 | if (rq->core->core_forceidle) { |
| 5472 | need_sync = true; |
| 5473 | fi_before = true; |
| 5474 | rq->core->core_forceidle = false; |
| 5475 | } |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5476 | |
| 5477 | /* |
| 5478 | * core->core_task_seq, core->core_pick_seq, rq->core_sched_seq |
| 5479 | * |
| 5480 | * @task_seq guards the task state ({en,de}queues) |
| 5481 | * @pick_seq is the @task_seq we did a selection on |
| 5482 | * @sched_seq is the @pick_seq we scheduled |
| 5483 | * |
| 5484 | * However, preemptions can cause multiple picks on the same task set. |
| 5485 | * 'Fix' this by also increasing @task_seq for every pick. |
| 5486 | */ |
| 5487 | rq->core->core_task_seq++; |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5488 | |
Joel Fernandes (Google) | 7afbba1 | 2020-11-17 18:19:42 -0500 | [diff] [blame] | 5489 | /* |
| 5490 | * Optimize for common case where this CPU has no cookies |
| 5491 | * and there are no cookied tasks running on siblings. |
| 5492 | */ |
| 5493 | if (!need_sync) { |
| 5494 | for_each_class(class) { |
| 5495 | next = class->pick_task(rq); |
| 5496 | if (next) |
| 5497 | break; |
| 5498 | } |
| 5499 | |
| 5500 | if (!next->core_cookie) { |
| 5501 | rq->core_pick = NULL; |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5502 | /* |
| 5503 | * For robustness, update the min_vruntime_fi for |
| 5504 | * unconstrained picks as well. |
| 5505 | */ |
| 5506 | WARN_ON_ONCE(fi_before); |
| 5507 | task_vruntime_update(rq, next, false); |
Joel Fernandes (Google) | 7afbba1 | 2020-11-17 18:19:42 -0500 | [diff] [blame] | 5508 | goto done; |
| 5509 | } |
Vineeth Pillai | 8039e96f | 2020-11-17 18:19:38 -0500 | [diff] [blame] | 5510 | } |
Joel Fernandes (Google) | 7afbba1 | 2020-11-17 18:19:42 -0500 | [diff] [blame] | 5511 | |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5512 | for_each_cpu(i, smt_mask) { |
| 5513 | struct rq *rq_i = cpu_rq(i); |
| 5514 | |
| 5515 | rq_i->core_pick = NULL; |
| 5516 | |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5517 | if (i != cpu) |
| 5518 | update_rq_clock(rq_i); |
| 5519 | } |
| 5520 | |
| 5521 | /* |
Ingo Molnar | cc00c19 | 2021-05-12 19:51:31 +0200 | [diff] [blame] | 5522 | * Try and select tasks for each sibling in descending sched_class |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5523 | * order. |
| 5524 | */ |
| 5525 | for_each_class(class) { |
| 5526 | again: |
| 5527 | for_each_cpu_wrap(i, smt_mask, cpu) { |
| 5528 | struct rq *rq_i = cpu_rq(i); |
| 5529 | struct task_struct *p; |
| 5530 | |
| 5531 | if (rq_i->core_pick) |
| 5532 | continue; |
| 5533 | |
| 5534 | /* |
| 5535 | * If this sibling doesn't yet have a suitable task to |
Ingo Molnar | cc00c19 | 2021-05-12 19:51:31 +0200 | [diff] [blame] | 5536 | * run; ask for the most eligible task, given the |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5537 | * highest priority task already selected for this |
| 5538 | * core. |
| 5539 | */ |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5540 | p = pick_task(rq_i, class, max, fi_before); |
Joel Fernandes (Google) | 7afbba1 | 2020-11-17 18:19:42 -0500 | [diff] [blame] | 5541 | if (!p) |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5542 | continue; |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5543 | |
Peter Zijlstra | d2dfa17 | 2020-11-17 18:19:43 -0500 | [diff] [blame] | 5544 | if (!is_task_rq_idle(p)) |
| 5545 | occ++; |
| 5546 | |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5547 | rq_i->core_pick = p; |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5548 | if (rq_i->idle == p && rq_i->nr_running) { |
| 5549 | rq->core->core_forceidle = true; |
| 5550 | if (!fi_before) |
| 5551 | rq->core->core_forceidle_seq++; |
| 5552 | } |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5553 | |
| 5554 | /* |
| 5555 | * If this new candidate is of higher priority than the |
| 5556 | * previous; and they're incompatible; we need to wipe |
| 5557 | * the slate and start over. pick_task makes sure that |
| 5558 | * p's priority is more than max if it doesn't match |
| 5559 | * max's cookie. |
| 5560 | * |
| 5561 | * NOTE: this is a linear max-filter and is thus bounded |
| 5562 | * in execution time. |
| 5563 | */ |
| 5564 | if (!max || !cookie_match(max, p)) { |
| 5565 | struct task_struct *old_max = max; |
| 5566 | |
| 5567 | rq->core->core_cookie = p->core_cookie; |
| 5568 | max = p; |
| 5569 | |
| 5570 | if (old_max) { |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5571 | rq->core->core_forceidle = false; |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5572 | for_each_cpu(j, smt_mask) { |
| 5573 | if (j == i) |
| 5574 | continue; |
| 5575 | |
| 5576 | cpu_rq(j)->core_pick = NULL; |
| 5577 | } |
Peter Zijlstra | d2dfa17 | 2020-11-17 18:19:43 -0500 | [diff] [blame] | 5578 | occ = 1; |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5579 | goto again; |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5580 | } |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5581 | } |
| 5582 | } |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | rq->core->core_pick_seq = rq->core->core_task_seq; |
| 5586 | next = rq->core_pick; |
| 5587 | rq->core_sched_seq = rq->core->core_pick_seq; |
| 5588 | |
| 5589 | /* Something should have been selected for current CPU */ |
| 5590 | WARN_ON_ONCE(!next); |
| 5591 | |
| 5592 | /* |
| 5593 | * Reschedule siblings |
| 5594 | * |
| 5595 | * NOTE: L1TF -- at this point we're no longer running the old task and |
| 5596 | * sending an IPI (below) ensures the sibling will no longer be running |
| 5597 | * their task. This ensures there is no inter-sibling overlap between |
| 5598 | * non-matching user state. |
| 5599 | */ |
| 5600 | for_each_cpu(i, smt_mask) { |
| 5601 | struct rq *rq_i = cpu_rq(i); |
| 5602 | |
| 5603 | /* |
| 5604 | * An online sibling might have gone offline before a task |
| 5605 | * could be picked for it, or it might be offline but later |
| 5606 | * happen to come online, but its too late and nothing was |
| 5607 | * picked for it. That's Ok - it will pick tasks for itself, |
| 5608 | * so ignore it. |
| 5609 | */ |
| 5610 | if (!rq_i->core_pick) |
| 5611 | continue; |
| 5612 | |
Joel Fernandes (Google) | c6047c2 | 2020-11-17 18:19:39 -0500 | [diff] [blame] | 5613 | /* |
| 5614 | * Update for new !FI->FI transitions, or if continuing to be in !FI: |
| 5615 | * fi_before fi update? |
| 5616 | * 0 0 1 |
| 5617 | * 0 1 1 |
| 5618 | * 1 0 1 |
| 5619 | * 1 1 0 |
| 5620 | */ |
| 5621 | if (!(fi_before && rq->core->core_forceidle)) |
| 5622 | task_vruntime_update(rq_i, rq_i->core_pick, rq->core->core_forceidle); |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5623 | |
Peter Zijlstra | d2dfa17 | 2020-11-17 18:19:43 -0500 | [diff] [blame] | 5624 | rq_i->core_pick->core_occupation = occ; |
| 5625 | |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5626 | if (i == cpu) { |
| 5627 | rq_i->core_pick = NULL; |
| 5628 | continue; |
| 5629 | } |
| 5630 | |
| 5631 | /* Did we break L1TF mitigation requirements? */ |
| 5632 | WARN_ON_ONCE(!cookie_match(next, rq_i->core_pick)); |
| 5633 | |
| 5634 | if (rq_i->curr == rq_i->core_pick) { |
| 5635 | rq_i->core_pick = NULL; |
| 5636 | continue; |
| 5637 | } |
| 5638 | |
| 5639 | resched_curr(rq_i); |
| 5640 | } |
| 5641 | |
| 5642 | done: |
| 5643 | set_next_task(rq, next); |
| 5644 | return next; |
| 5645 | } |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 5646 | |
Peter Zijlstra | d2dfa17 | 2020-11-17 18:19:43 -0500 | [diff] [blame] | 5647 | static bool try_steal_cookie(int this, int that) |
| 5648 | { |
| 5649 | struct rq *dst = cpu_rq(this), *src = cpu_rq(that); |
| 5650 | struct task_struct *p; |
| 5651 | unsigned long cookie; |
| 5652 | bool success = false; |
| 5653 | |
| 5654 | local_irq_disable(); |
| 5655 | double_rq_lock(dst, src); |
| 5656 | |
| 5657 | cookie = dst->core->core_cookie; |
| 5658 | if (!cookie) |
| 5659 | goto unlock; |
| 5660 | |
| 5661 | if (dst->curr != dst->idle) |
| 5662 | goto unlock; |
| 5663 | |
| 5664 | p = sched_core_find(src, cookie); |
| 5665 | if (p == src->idle) |
| 5666 | goto unlock; |
| 5667 | |
| 5668 | do { |
| 5669 | if (p == src->core_pick || p == src->curr) |
| 5670 | goto next; |
| 5671 | |
| 5672 | if (!cpumask_test_cpu(this, &p->cpus_mask)) |
| 5673 | goto next; |
| 5674 | |
| 5675 | if (p->core_occupation > dst->idle->core_occupation) |
| 5676 | goto next; |
| 5677 | |
| 5678 | p->on_rq = TASK_ON_RQ_MIGRATING; |
| 5679 | deactivate_task(src, p, 0); |
| 5680 | set_task_cpu(p, this); |
| 5681 | activate_task(dst, p, 0); |
| 5682 | p->on_rq = TASK_ON_RQ_QUEUED; |
| 5683 | |
| 5684 | resched_curr(dst); |
| 5685 | |
| 5686 | success = true; |
| 5687 | break; |
| 5688 | |
| 5689 | next: |
| 5690 | p = sched_core_next(p, cookie); |
| 5691 | } while (p); |
| 5692 | |
| 5693 | unlock: |
| 5694 | double_rq_unlock(dst, src); |
| 5695 | local_irq_enable(); |
| 5696 | |
| 5697 | return success; |
| 5698 | } |
| 5699 | |
| 5700 | static bool steal_cookie_task(int cpu, struct sched_domain *sd) |
| 5701 | { |
| 5702 | int i; |
| 5703 | |
| 5704 | for_each_cpu_wrap(i, sched_domain_span(sd), cpu) { |
| 5705 | if (i == cpu) |
| 5706 | continue; |
| 5707 | |
| 5708 | if (need_resched()) |
| 5709 | break; |
| 5710 | |
| 5711 | if (try_steal_cookie(cpu, i)) |
| 5712 | return true; |
| 5713 | } |
| 5714 | |
| 5715 | return false; |
| 5716 | } |
| 5717 | |
| 5718 | static void sched_core_balance(struct rq *rq) |
| 5719 | { |
| 5720 | struct sched_domain *sd; |
| 5721 | int cpu = cpu_of(rq); |
| 5722 | |
| 5723 | preempt_disable(); |
| 5724 | rcu_read_lock(); |
| 5725 | raw_spin_rq_unlock_irq(rq); |
| 5726 | for_each_domain(cpu, sd) { |
| 5727 | if (need_resched()) |
| 5728 | break; |
| 5729 | |
| 5730 | if (steal_cookie_task(cpu, sd)) |
| 5731 | break; |
| 5732 | } |
| 5733 | raw_spin_rq_lock_irq(rq); |
| 5734 | rcu_read_unlock(); |
| 5735 | preempt_enable(); |
| 5736 | } |
| 5737 | |
| 5738 | static DEFINE_PER_CPU(struct callback_head, core_balance_head); |
| 5739 | |
| 5740 | void queue_core_balance(struct rq *rq) |
| 5741 | { |
| 5742 | if (!sched_core_enabled(rq)) |
| 5743 | return; |
| 5744 | |
| 5745 | if (!rq->core->core_cookie) |
| 5746 | return; |
| 5747 | |
| 5748 | if (!rq->nr_running) /* not forced idle */ |
| 5749 | return; |
| 5750 | |
| 5751 | queue_balance_callback(rq, &per_cpu(core_balance_head, rq->cpu), sched_core_balance); |
| 5752 | } |
| 5753 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 5754 | static inline void sched_core_cpu_starting(unsigned int cpu) |
| 5755 | { |
| 5756 | const struct cpumask *smt_mask = cpu_smt_mask(cpu); |
| 5757 | struct rq *rq, *core_rq = NULL; |
| 5758 | int i; |
| 5759 | |
| 5760 | core_rq = cpu_rq(cpu)->core; |
| 5761 | |
| 5762 | if (!core_rq) { |
| 5763 | for_each_cpu(i, smt_mask) { |
| 5764 | rq = cpu_rq(i); |
| 5765 | if (rq->core && rq->core == rq) |
| 5766 | core_rq = rq; |
| 5767 | } |
| 5768 | |
| 5769 | if (!core_rq) |
| 5770 | core_rq = cpu_rq(cpu); |
| 5771 | |
| 5772 | for_each_cpu(i, smt_mask) { |
| 5773 | rq = cpu_rq(i); |
| 5774 | |
| 5775 | WARN_ON_ONCE(rq->core && rq->core != core_rq); |
| 5776 | rq->core = core_rq; |
| 5777 | } |
| 5778 | } |
| 5779 | } |
| 5780 | #else /* !CONFIG_SCHED_CORE */ |
| 5781 | |
| 5782 | static inline void sched_core_cpu_starting(unsigned int cpu) {} |
| 5783 | |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 5784 | static struct task_struct * |
| 5785 | pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
| 5786 | { |
| 5787 | return __pick_next_task(rq, prev, rf); |
| 5788 | } |
| 5789 | |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 5790 | #endif /* CONFIG_SCHED_CORE */ |
| 5791 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5792 | /* |
Thomas Gleixner | c259e01 | 2011-06-22 19:47:00 +0200 | [diff] [blame] | 5793 | * __schedule() is the main scheduler function. |
Pekka Enberg | edde96e | 2012-08-04 11:49:47 +0300 | [diff] [blame] | 5794 | * |
| 5795 | * The main means of driving the scheduler and thus entering this function are: |
| 5796 | * |
| 5797 | * 1. Explicit blocking: mutex, semaphore, waitqueue, etc. |
| 5798 | * |
| 5799 | * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return |
| 5800 | * paths. For example, see arch/x86/entry_64.S. |
| 5801 | * |
| 5802 | * To drive preemption between tasks, the scheduler sets the flag in timer |
| 5803 | * interrupt handler scheduler_tick(). |
| 5804 | * |
| 5805 | * 3. Wakeups don't really cause entry into schedule(). They add a |
| 5806 | * task to the run-queue and that's it. |
| 5807 | * |
| 5808 | * Now, if the new task added to the run-queue preempts the current |
| 5809 | * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets |
| 5810 | * called on the nearest possible occasion: |
| 5811 | * |
Thomas Gleixner | c1a280b | 2019-07-26 23:19:37 +0200 | [diff] [blame] | 5812 | * - If the kernel is preemptible (CONFIG_PREEMPTION=y): |
Pekka Enberg | edde96e | 2012-08-04 11:49:47 +0300 | [diff] [blame] | 5813 | * |
| 5814 | * - in syscall or exception context, at the next outmost |
| 5815 | * preempt_enable(). (this might be as soon as the wake_up()'s |
| 5816 | * spin_unlock()!) |
| 5817 | * |
| 5818 | * - in IRQ context, return from interrupt-handler to |
| 5819 | * preemptible context |
| 5820 | * |
Thomas Gleixner | c1a280b | 2019-07-26 23:19:37 +0200 | [diff] [blame] | 5821 | * - If the kernel is not preemptible (CONFIG_PREEMPTION is not set) |
Pekka Enberg | edde96e | 2012-08-04 11:49:47 +0300 | [diff] [blame] | 5822 | * then at the next: |
| 5823 | * |
| 5824 | * - cond_resched() call |
| 5825 | * - explicit schedule() call |
| 5826 | * - return from syscall or exception to user-space |
| 5827 | * - return from interrupt-handler to user-space |
Frederic Weisbecker | bfd9b2b | 2015-01-28 01:24:09 +0100 | [diff] [blame] | 5828 | * |
Frederic Weisbecker | b30f0e3 | 2015-05-12 16:41:49 +0200 | [diff] [blame] | 5829 | * WARNING: must be called with preemption disabled! |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5830 | */ |
Peter Zijlstra | 499d795 | 2015-09-28 18:52:36 +0200 | [diff] [blame] | 5831 | static void __sched notrace __schedule(bool preempt) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5832 | { |
| 5833 | struct task_struct *prev, *next; |
Harvey Harrison | 67ca7bd | 2008-02-15 09:56:36 -0800 | [diff] [blame] | 5834 | unsigned long *switch_count; |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 5835 | unsigned long prev_state; |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 5836 | struct rq_flags rf; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5837 | struct rq *rq; |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 5838 | int cpu; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5839 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5840 | cpu = smp_processor_id(); |
| 5841 | rq = cpu_rq(cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5842 | prev = rq->curr; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5843 | |
Daniel Vetter | 312364f3 | 2019-08-26 22:14:23 +0200 | [diff] [blame] | 5844 | schedule_debug(prev, preempt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | |
Juri Lelli | e0ee463 | 2021-02-08 08:35:54 +0100 | [diff] [blame] | 5846 | if (sched_feat(HRTICK) || sched_feat(HRTICK_DL)) |
Mike Galbraith | f333fdc | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 5847 | hrtick_clear(rq); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 5848 | |
Paul E. McKenney | 46a5d16 | 2015-10-07 09:10:48 -0700 | [diff] [blame] | 5849 | local_irq_disable(); |
Paul E. McKenney | bcbfdd0 | 2017-04-11 15:50:41 -0700 | [diff] [blame] | 5850 | rcu_note_context_switch(preempt); |
Paul E. McKenney | 46a5d16 | 2015-10-07 09:10:48 -0700 | [diff] [blame] | 5851 | |
Oleg Nesterov | e0acd0a | 2013-08-12 18:14:00 +0200 | [diff] [blame] | 5852 | /* |
| 5853 | * Make sure that signal_pending_state()->signal_pending() below |
| 5854 | * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE) |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 5855 | * done by the caller to avoid the race with signal_wake_up(): |
Mathieu Desnoyers | 306e060 | 2018-01-29 15:20:12 -0500 | [diff] [blame] | 5856 | * |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 5857 | * __set_current_state(@state) signal_wake_up() |
| 5858 | * schedule() set_tsk_thread_flag(p, TIF_SIGPENDING) |
| 5859 | * wake_up_state(p, state) |
| 5860 | * LOCK rq->lock LOCK p->pi_state |
| 5861 | * smp_mb__after_spinlock() smp_mb__after_spinlock() |
| 5862 | * if (signal_pending_state()) if (p->state & @state) |
| 5863 | * |
| 5864 | * Also, the membarrier system call requires a full memory barrier |
Mathieu Desnoyers | 306e060 | 2018-01-29 15:20:12 -0500 | [diff] [blame] | 5865 | * after coming from user-space, before storing to rq->curr. |
Oleg Nesterov | e0acd0a | 2013-08-12 18:14:00 +0200 | [diff] [blame] | 5866 | */ |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 5867 | rq_lock(rq, &rf); |
Peter Zijlstra | d89e588c | 2016-09-05 11:37:53 +0200 | [diff] [blame] | 5868 | smp_mb__after_spinlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 5870 | /* Promote REQ to ACT */ |
| 5871 | rq->clock_update_flags <<= 1; |
Peter Zijlstra | bce4dc8 | 2017-02-21 14:40:35 +0100 | [diff] [blame] | 5872 | update_rq_clock(rq); |
Peter Zijlstra | 9edfbfe | 2015-01-05 11:18:11 +0100 | [diff] [blame] | 5873 | |
Oleg Nesterov | 246d86b | 2010-05-19 14:57:11 +0200 | [diff] [blame] | 5874 | switch_count = &prev->nivcsw; |
Peter Zijlstra | d136122 | 2020-07-20 17:20:21 +0200 | [diff] [blame] | 5875 | |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 5876 | /* |
Peter Zijlstra | d136122 | 2020-07-20 17:20:21 +0200 | [diff] [blame] | 5877 | * We must load prev->state once (task_struct::state is volatile), such |
| 5878 | * that: |
| 5879 | * |
| 5880 | * - we form a control dependency vs deactivate_task() below. |
| 5881 | * - ptrace_{,un}freeze_traced() can change ->state underneath us. |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 5882 | */ |
Peter Zijlstra | d136122 | 2020-07-20 17:20:21 +0200 | [diff] [blame] | 5883 | prev_state = prev->state; |
| 5884 | if (!preempt && prev_state) { |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 5885 | if (signal_pending_state(prev_state, prev)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5886 | prev->state = TASK_RUNNING; |
Tejun Heo | 21aa9af | 2010-06-08 21:40:37 +0200 | [diff] [blame] | 5887 | } else { |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 5888 | prev->sched_contributes_to_load = |
| 5889 | (prev_state & TASK_UNINTERRUPTIBLE) && |
| 5890 | !(prev_state & TASK_NOLOAD) && |
| 5891 | !(prev->flags & PF_FROZEN); |
| 5892 | |
| 5893 | if (prev->sched_contributes_to_load) |
| 5894 | rq->nr_uninterruptible++; |
| 5895 | |
| 5896 | /* |
| 5897 | * __schedule() ttwu() |
Peter Zijlstra | d136122 | 2020-07-20 17:20:21 +0200 | [diff] [blame] | 5898 | * prev_state = prev->state; if (p->on_rq && ...) |
| 5899 | * if (prev_state) goto out; |
| 5900 | * p->on_rq = 0; smp_acquire__after_ctrl_dep(); |
| 5901 | * p->state = TASK_WAKING |
| 5902 | * |
| 5903 | * Where __schedule() and ttwu() have matching control dependencies. |
Peter Zijlstra | dbfb089 | 2020-07-03 12:40:33 +0200 | [diff] [blame] | 5904 | * |
| 5905 | * After this, schedule() must not care about p->state any more. |
| 5906 | */ |
Peter Zijlstra | bce4dc8 | 2017-02-21 14:40:35 +0100 | [diff] [blame] | 5907 | deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK); |
Peter Zijlstra | 2acca55 | 2011-04-05 17:23:50 +0200 | [diff] [blame] | 5908 | |
Tejun Heo | e33a9bb | 2016-12-07 15:48:41 -0500 | [diff] [blame] | 5909 | if (prev->in_iowait) { |
| 5910 | atomic_inc(&rq->nr_iowait); |
| 5911 | delayacct_blkio_start(); |
| 5912 | } |
Tejun Heo | 21aa9af | 2010-06-08 21:40:37 +0200 | [diff] [blame] | 5913 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 5914 | switch_count = &prev->nvcsw; |
| 5915 | } |
| 5916 | |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 5917 | next = pick_next_task(rq, prev, &rf); |
Mike Galbraith | f26f9af | 2010-12-08 11:05:42 +0100 | [diff] [blame] | 5918 | clear_tsk_need_resched(prev); |
Peter Zijlstra | f27dde8 | 2013-08-14 14:55:31 +0200 | [diff] [blame] | 5919 | clear_preempt_need_resched(); |
Paul Turner | c006fac | 2021-04-16 14:29:36 -0700 | [diff] [blame] | 5920 | #ifdef CONFIG_SCHED_DEBUG |
| 5921 | rq->last_seen_need_resched_ns = 0; |
| 5922 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | if (likely(prev != next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | rq->nr_switches++; |
Eric W. Biederman | 5311a98 | 2019-09-14 07:35:02 -0500 | [diff] [blame] | 5926 | /* |
| 5927 | * RCU users of rcu_dereference(rq->curr) may not see |
| 5928 | * changes to task_struct made by pick_next_task(). |
| 5929 | */ |
| 5930 | RCU_INIT_POINTER(rq->curr, next); |
Mathieu Desnoyers | 22e4ebb | 2017-07-28 16:40:40 -0400 | [diff] [blame] | 5931 | /* |
| 5932 | * The membarrier system call requires each architecture |
| 5933 | * to have a full memory barrier after updating |
Mathieu Desnoyers | 306e060 | 2018-01-29 15:20:12 -0500 | [diff] [blame] | 5934 | * rq->curr, before returning to user-space. |
| 5935 | * |
| 5936 | * Here are the schemes providing that barrier on the |
| 5937 | * various architectures: |
| 5938 | * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC. |
| 5939 | * switch_mm() rely on membarrier_arch_switch_mm() on PowerPC. |
| 5940 | * - finish_lock_switch() for weakly-ordered |
| 5941 | * architectures where spin_unlock is a full barrier, |
| 5942 | * - switch_to() for arm64 (weakly-ordered, spin_unlock |
| 5943 | * is a RELEASE barrier), |
Mathieu Desnoyers | 22e4ebb | 2017-07-28 16:40:40 -0400 | [diff] [blame] | 5944 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | ++*switch_count; |
| 5946 | |
Peter Zijlstra | af44990 | 2020-09-17 10:38:30 +0200 | [diff] [blame] | 5947 | migrate_disable_switch(rq, prev); |
Johannes Weiner | b05e75d | 2020-03-16 15:13:31 -0400 | [diff] [blame] | 5948 | psi_sched_switch(prev, next, !task_on_rq_queued(prev)); |
| 5949 | |
Peter Zijlstra | c73464b | 2015-09-28 18:06:56 +0200 | [diff] [blame] | 5950 | trace_sched_switch(preempt, prev, next); |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 5951 | |
| 5952 | /* Also unlocks the rq: */ |
| 5953 | rq = context_switch(rq, prev, next, &rf); |
Peter Zijlstra | cbce1a6 | 2015-06-11 14:46:54 +0200 | [diff] [blame] | 5954 | } else { |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 5955 | rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 5957 | rq_unpin_lock(rq, &rf); |
| 5958 | __balance_callbacks(rq); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 5959 | raw_spin_rq_unlock_irq(rq); |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 5960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | } |
Thomas Gleixner | c259e01 | 2011-06-22 19:47:00 +0200 | [diff] [blame] | 5962 | |
Peter Zijlstra | 9af6528 | 2016-09-13 18:37:29 +0200 | [diff] [blame] | 5963 | void __noreturn do_task_dead(void) |
| 5964 | { |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 5965 | /* Causes final put_task_struct in finish_task_switch(): */ |
Peter Zijlstra | b5bf9a9 | 2018-04-30 14:51:01 +0200 | [diff] [blame] | 5966 | set_special_state(TASK_DEAD); |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 5967 | |
| 5968 | /* Tell freezer to ignore us: */ |
| 5969 | current->flags |= PF_NOFREEZE; |
| 5970 | |
Peter Zijlstra | 9af6528 | 2016-09-13 18:37:29 +0200 | [diff] [blame] | 5971 | __schedule(false); |
| 5972 | BUG(); |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 5973 | |
| 5974 | /* Avoid "noreturn function does return" - but don't continue if BUG() is a NOP: */ |
Peter Zijlstra | 9af6528 | 2016-09-13 18:37:29 +0200 | [diff] [blame] | 5975 | for (;;) |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 5976 | cpu_relax(); |
Peter Zijlstra | 9af6528 | 2016-09-13 18:37:29 +0200 | [diff] [blame] | 5977 | } |
| 5978 | |
Thomas Gleixner | 9c40cef2 | 2011-06-22 19:47:01 +0200 | [diff] [blame] | 5979 | static inline void sched_submit_work(struct task_struct *tsk) |
| 5980 | { |
Sebastian Andrzej Siewior | c1cecf8 | 2020-08-19 22:00:25 +0200 | [diff] [blame] | 5981 | unsigned int task_flags; |
| 5982 | |
Sebastian Andrzej Siewior | b0fdc01 | 2019-08-16 18:06:26 +0200 | [diff] [blame] | 5983 | if (!tsk->state) |
Thomas Gleixner | 9c40cef2 | 2011-06-22 19:47:01 +0200 | [diff] [blame] | 5984 | return; |
Thomas Gleixner | 6d25be5 | 2019-03-13 17:55:48 +0100 | [diff] [blame] | 5985 | |
Sebastian Andrzej Siewior | c1cecf8 | 2020-08-19 22:00:25 +0200 | [diff] [blame] | 5986 | task_flags = tsk->flags; |
Thomas Gleixner | 6d25be5 | 2019-03-13 17:55:48 +0100 | [diff] [blame] | 5987 | /* |
| 5988 | * If a worker went to sleep, notify and ask workqueue whether |
| 5989 | * it wants to wake up a task to maintain concurrency. |
| 5990 | * As this function is called inside the schedule() context, |
| 5991 | * we disable preemption to avoid it calling schedule() again |
Sebastian Andrzej Siewior | 62849a9 | 2020-03-28 00:29:59 +0100 | [diff] [blame] | 5992 | * in the possible wakeup of a kworker and because wq_worker_sleeping() |
| 5993 | * requires it. |
Thomas Gleixner | 6d25be5 | 2019-03-13 17:55:48 +0100 | [diff] [blame] | 5994 | */ |
Sebastian Andrzej Siewior | c1cecf8 | 2020-08-19 22:00:25 +0200 | [diff] [blame] | 5995 | if (task_flags & (PF_WQ_WORKER | PF_IO_WORKER)) { |
Thomas Gleixner | 6d25be5 | 2019-03-13 17:55:48 +0100 | [diff] [blame] | 5996 | preempt_disable(); |
Sebastian Andrzej Siewior | c1cecf8 | 2020-08-19 22:00:25 +0200 | [diff] [blame] | 5997 | if (task_flags & PF_WQ_WORKER) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 5998 | wq_worker_sleeping(tsk); |
| 5999 | else |
| 6000 | io_wq_worker_sleeping(tsk); |
Thomas Gleixner | 6d25be5 | 2019-03-13 17:55:48 +0100 | [diff] [blame] | 6001 | preempt_enable_no_resched(); |
| 6002 | } |
| 6003 | |
Sebastian Andrzej Siewior | b0fdc01 | 2019-08-16 18:06:26 +0200 | [diff] [blame] | 6004 | if (tsk_is_pi_blocked(tsk)) |
| 6005 | return; |
| 6006 | |
Thomas Gleixner | 9c40cef2 | 2011-06-22 19:47:01 +0200 | [diff] [blame] | 6007 | /* |
| 6008 | * If we are going to sleep and we have plugged IO queued, |
| 6009 | * make sure to submit it to avoid deadlocks. |
| 6010 | */ |
| 6011 | if (blk_needs_flush_plug(tsk)) |
| 6012 | blk_schedule_flush_plug(tsk); |
| 6013 | } |
| 6014 | |
Thomas Gleixner | 6d25be5 | 2019-03-13 17:55:48 +0100 | [diff] [blame] | 6015 | static void sched_update_worker(struct task_struct *tsk) |
| 6016 | { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 6017 | if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER)) { |
| 6018 | if (tsk->flags & PF_WQ_WORKER) |
| 6019 | wq_worker_running(tsk); |
| 6020 | else |
| 6021 | io_wq_worker_running(tsk); |
| 6022 | } |
Thomas Gleixner | 6d25be5 | 2019-03-13 17:55:48 +0100 | [diff] [blame] | 6023 | } |
| 6024 | |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 6025 | asmlinkage __visible void __sched schedule(void) |
Thomas Gleixner | c259e01 | 2011-06-22 19:47:00 +0200 | [diff] [blame] | 6026 | { |
Thomas Gleixner | 9c40cef2 | 2011-06-22 19:47:01 +0200 | [diff] [blame] | 6027 | struct task_struct *tsk = current; |
| 6028 | |
| 6029 | sched_submit_work(tsk); |
Frederic Weisbecker | bfd9b2b | 2015-01-28 01:24:09 +0100 | [diff] [blame] | 6030 | do { |
Frederic Weisbecker | b30f0e3 | 2015-05-12 16:41:49 +0200 | [diff] [blame] | 6031 | preempt_disable(); |
Peter Zijlstra | fc13aeb | 2015-09-28 18:05:34 +0200 | [diff] [blame] | 6032 | __schedule(false); |
Frederic Weisbecker | b30f0e3 | 2015-05-12 16:41:49 +0200 | [diff] [blame] | 6033 | sched_preempt_enable_no_resched(); |
Frederic Weisbecker | bfd9b2b | 2015-01-28 01:24:09 +0100 | [diff] [blame] | 6034 | } while (need_resched()); |
Thomas Gleixner | 6d25be5 | 2019-03-13 17:55:48 +0100 | [diff] [blame] | 6035 | sched_update_worker(tsk); |
Thomas Gleixner | c259e01 | 2011-06-22 19:47:00 +0200 | [diff] [blame] | 6036 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | EXPORT_SYMBOL(schedule); |
| 6038 | |
Steven Rostedt (VMware) | 8663eff | 2017-04-14 08:48:09 -0400 | [diff] [blame] | 6039 | /* |
| 6040 | * synchronize_rcu_tasks() makes sure that no task is stuck in preempted |
| 6041 | * state (have scheduled out non-voluntarily) by making sure that all |
| 6042 | * tasks have either left the run queue or have gone into user space. |
| 6043 | * As idle tasks do not do either, they must not ever be preempted |
| 6044 | * (schedule out non-voluntarily). |
| 6045 | * |
| 6046 | * schedule_idle() is similar to schedule_preempt_disable() except that it |
| 6047 | * never enables preemption because it does not call sched_submit_work(). |
| 6048 | */ |
| 6049 | void __sched schedule_idle(void) |
| 6050 | { |
| 6051 | /* |
| 6052 | * As this skips calling sched_submit_work(), which the idle task does |
| 6053 | * regardless because that function is a nop when the task is in a |
| 6054 | * TASK_RUNNING state, make sure this isn't used someplace that the |
| 6055 | * current task can be in any other state. Note, idle is always in the |
| 6056 | * TASK_RUNNING state. |
| 6057 | */ |
| 6058 | WARN_ON_ONCE(current->state); |
| 6059 | do { |
| 6060 | __schedule(false); |
| 6061 | } while (need_resched()); |
| 6062 | } |
| 6063 | |
Frederic Weisbecker | 6775de4 | 2020-11-17 16:16:36 +0100 | [diff] [blame] | 6064 | #if defined(CONFIG_CONTEXT_TRACKING) && !defined(CONFIG_HAVE_CONTEXT_TRACKING_OFFSTACK) |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 6065 | asmlinkage __visible void __sched schedule_user(void) |
Frederic Weisbecker | 20ab65e3 | 2012-07-11 20:26:37 +0200 | [diff] [blame] | 6066 | { |
| 6067 | /* |
| 6068 | * If we come here after a random call to set_need_resched(), |
| 6069 | * or we have been woken up remotely but the IPI has not yet arrived, |
| 6070 | * we haven't yet exited the RCU idle mode. Do it here manually until |
| 6071 | * we find a better solution. |
Andy Lutomirski | 7cc78f8 | 2014-12-03 15:37:08 -0800 | [diff] [blame] | 6072 | * |
| 6073 | * NB: There are buggy callers of this function. Ideally we |
Frederic Weisbecker | c467ea7 | 2015-03-04 18:06:33 +0100 | [diff] [blame] | 6074 | * should warn if prev_state != CONTEXT_USER, but that will trigger |
Andy Lutomirski | 7cc78f8 | 2014-12-03 15:37:08 -0800 | [diff] [blame] | 6075 | * too frequently to make sense yet. |
Frederic Weisbecker | 20ab65e3 | 2012-07-11 20:26:37 +0200 | [diff] [blame] | 6076 | */ |
Andy Lutomirski | 7cc78f8 | 2014-12-03 15:37:08 -0800 | [diff] [blame] | 6077 | enum ctx_state prev_state = exception_enter(); |
Frederic Weisbecker | 20ab65e3 | 2012-07-11 20:26:37 +0200 | [diff] [blame] | 6078 | schedule(); |
Andy Lutomirski | 7cc78f8 | 2014-12-03 15:37:08 -0800 | [diff] [blame] | 6079 | exception_exit(prev_state); |
Frederic Weisbecker | 20ab65e3 | 2012-07-11 20:26:37 +0200 | [diff] [blame] | 6080 | } |
| 6081 | #endif |
| 6082 | |
Thomas Gleixner | c5491ea | 2011-03-21 12:09:35 +0100 | [diff] [blame] | 6083 | /** |
| 6084 | * schedule_preempt_disabled - called with preemption disabled |
| 6085 | * |
| 6086 | * Returns with preemption disabled. Note: preempt_count must be 1 |
| 6087 | */ |
| 6088 | void __sched schedule_preempt_disabled(void) |
| 6089 | { |
Thomas Gleixner | ba74c14 | 2011-03-21 13:32:17 +0100 | [diff] [blame] | 6090 | sched_preempt_enable_no_resched(); |
Thomas Gleixner | c5491ea | 2011-03-21 12:09:35 +0100 | [diff] [blame] | 6091 | schedule(); |
| 6092 | preempt_disable(); |
| 6093 | } |
| 6094 | |
Frederic Weisbecker | 06b1f80 | 2015-02-16 19:20:07 +0100 | [diff] [blame] | 6095 | static void __sched notrace preempt_schedule_common(void) |
Frederic Weisbecker | a18b5d0 | 2015-01-22 18:08:04 +0100 | [diff] [blame] | 6096 | { |
| 6097 | do { |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 6098 | /* |
| 6099 | * Because the function tracer can trace preempt_count_sub() |
| 6100 | * and it also uses preempt_enable/disable_notrace(), if |
| 6101 | * NEED_RESCHED is set, the preempt_enable_notrace() called |
| 6102 | * by the function tracer will call this function again and |
| 6103 | * cause infinite recursion. |
| 6104 | * |
| 6105 | * Preemption must be disabled here before the function |
| 6106 | * tracer can trace. Break up preempt_disable() into two |
| 6107 | * calls. One to disable preemption without fear of being |
| 6108 | * traced. The other to still record the preemption latency, |
| 6109 | * which can also be traced by the function tracer. |
| 6110 | */ |
Peter Zijlstra | 499d795 | 2015-09-28 18:52:36 +0200 | [diff] [blame] | 6111 | preempt_disable_notrace(); |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 6112 | preempt_latency_start(1); |
Peter Zijlstra | fc13aeb | 2015-09-28 18:05:34 +0200 | [diff] [blame] | 6113 | __schedule(true); |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 6114 | preempt_latency_stop(1); |
Peter Zijlstra | 499d795 | 2015-09-28 18:52:36 +0200 | [diff] [blame] | 6115 | preempt_enable_no_resched_notrace(); |
Frederic Weisbecker | a18b5d0 | 2015-01-22 18:08:04 +0100 | [diff] [blame] | 6116 | |
| 6117 | /* |
| 6118 | * Check again in case we missed a preemption opportunity |
| 6119 | * between schedule and now. |
| 6120 | */ |
Frederic Weisbecker | a18b5d0 | 2015-01-22 18:08:04 +0100 | [diff] [blame] | 6121 | } while (need_resched()); |
| 6122 | } |
| 6123 | |
Thomas Gleixner | c1a280b | 2019-07-26 23:19:37 +0200 | [diff] [blame] | 6124 | #ifdef CONFIG_PREEMPTION |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | /* |
Valentin Schneider | a49b4f4 | 2019-09-23 15:36:12 +0100 | [diff] [blame] | 6126 | * This is the entry point to schedule() from in-kernel preemption |
| 6127 | * off of preempt_enable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | */ |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 6129 | asmlinkage __visible void __sched notrace preempt_schedule(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | /* |
| 6132 | * If there is a non-zero preempt_count or interrupts are disabled, |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6133 | * we do not want to preempt the current task. Just return.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | */ |
Frederic Weisbecker | fbb00b5 | 2013-06-19 23:56:22 +0200 | [diff] [blame] | 6135 | if (likely(!preemptible())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | return; |
| 6137 | |
Frederic Weisbecker | a18b5d0 | 2015-01-22 18:08:04 +0100 | [diff] [blame] | 6138 | preempt_schedule_common(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | } |
Masami Hiramatsu | 376e242 | 2014-04-17 17:17:05 +0900 | [diff] [blame] | 6140 | NOKPROBE_SYMBOL(preempt_schedule); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | EXPORT_SYMBOL(preempt_schedule); |
Oleg Nesterov | 009f60e | 2014-10-05 22:23:22 +0200 | [diff] [blame] | 6142 | |
Peter Zijlstra (Intel) | 2c9a98d | 2021-01-18 15:12:21 +0100 | [diff] [blame] | 6143 | #ifdef CONFIG_PREEMPT_DYNAMIC |
| 6144 | DEFINE_STATIC_CALL(preempt_schedule, __preempt_schedule_func); |
Peter Zijlstra | ef72661 | 2021-01-25 16:26:50 +0100 | [diff] [blame] | 6145 | EXPORT_STATIC_CALL_TRAMP(preempt_schedule); |
Peter Zijlstra (Intel) | 2c9a98d | 2021-01-18 15:12:21 +0100 | [diff] [blame] | 6146 | #endif |
| 6147 | |
| 6148 | |
Oleg Nesterov | 009f60e | 2014-10-05 22:23:22 +0200 | [diff] [blame] | 6149 | /** |
Frederic Weisbecker | 4eaca0a | 2015-06-04 17:39:08 +0200 | [diff] [blame] | 6150 | * preempt_schedule_notrace - preempt_schedule called by tracing |
Oleg Nesterov | 009f60e | 2014-10-05 22:23:22 +0200 | [diff] [blame] | 6151 | * |
| 6152 | * The tracing infrastructure uses preempt_enable_notrace to prevent |
| 6153 | * recursion and tracing preempt enabling caused by the tracing |
| 6154 | * infrastructure itself. But as tracing can happen in areas coming |
| 6155 | * from userspace or just about to enter userspace, a preempt enable |
| 6156 | * can occur before user_exit() is called. This will cause the scheduler |
| 6157 | * to be called when the system is still in usermode. |
| 6158 | * |
| 6159 | * To prevent this, the preempt_enable_notrace will use this function |
| 6160 | * instead of preempt_schedule() to exit user context if needed before |
| 6161 | * calling the scheduler. |
| 6162 | */ |
Frederic Weisbecker | 4eaca0a | 2015-06-04 17:39:08 +0200 | [diff] [blame] | 6163 | asmlinkage __visible void __sched notrace preempt_schedule_notrace(void) |
Oleg Nesterov | 009f60e | 2014-10-05 22:23:22 +0200 | [diff] [blame] | 6164 | { |
| 6165 | enum ctx_state prev_ctx; |
| 6166 | |
| 6167 | if (likely(!preemptible())) |
| 6168 | return; |
| 6169 | |
| 6170 | do { |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 6171 | /* |
| 6172 | * Because the function tracer can trace preempt_count_sub() |
| 6173 | * and it also uses preempt_enable/disable_notrace(), if |
| 6174 | * NEED_RESCHED is set, the preempt_enable_notrace() called |
| 6175 | * by the function tracer will call this function again and |
| 6176 | * cause infinite recursion. |
| 6177 | * |
| 6178 | * Preemption must be disabled here before the function |
| 6179 | * tracer can trace. Break up preempt_disable() into two |
| 6180 | * calls. One to disable preemption without fear of being |
| 6181 | * traced. The other to still record the preemption latency, |
| 6182 | * which can also be traced by the function tracer. |
| 6183 | */ |
Peter Zijlstra | 3d8f74d | 2015-09-28 18:09:19 +0200 | [diff] [blame] | 6184 | preempt_disable_notrace(); |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 6185 | preempt_latency_start(1); |
Oleg Nesterov | 009f60e | 2014-10-05 22:23:22 +0200 | [diff] [blame] | 6186 | /* |
| 6187 | * Needs preempt disabled in case user_exit() is traced |
| 6188 | * and the tracer calls preempt_enable_notrace() causing |
| 6189 | * an infinite recursion. |
| 6190 | */ |
| 6191 | prev_ctx = exception_enter(); |
Peter Zijlstra | fc13aeb | 2015-09-28 18:05:34 +0200 | [diff] [blame] | 6192 | __schedule(true); |
Oleg Nesterov | 009f60e | 2014-10-05 22:23:22 +0200 | [diff] [blame] | 6193 | exception_exit(prev_ctx); |
| 6194 | |
Steven Rostedt | 47252cf | 2016-03-21 11:23:39 -0400 | [diff] [blame] | 6195 | preempt_latency_stop(1); |
Peter Zijlstra | 3d8f74d | 2015-09-28 18:09:19 +0200 | [diff] [blame] | 6196 | preempt_enable_no_resched_notrace(); |
Oleg Nesterov | 009f60e | 2014-10-05 22:23:22 +0200 | [diff] [blame] | 6197 | } while (need_resched()); |
| 6198 | } |
Frederic Weisbecker | 4eaca0a | 2015-06-04 17:39:08 +0200 | [diff] [blame] | 6199 | EXPORT_SYMBOL_GPL(preempt_schedule_notrace); |
Oleg Nesterov | 009f60e | 2014-10-05 22:23:22 +0200 | [diff] [blame] | 6200 | |
Peter Zijlstra (Intel) | 2c9a98d | 2021-01-18 15:12:21 +0100 | [diff] [blame] | 6201 | #ifdef CONFIG_PREEMPT_DYNAMIC |
| 6202 | DEFINE_STATIC_CALL(preempt_schedule_notrace, __preempt_schedule_notrace_func); |
Peter Zijlstra | ef72661 | 2021-01-25 16:26:50 +0100 | [diff] [blame] | 6203 | EXPORT_STATIC_CALL_TRAMP(preempt_schedule_notrace); |
Peter Zijlstra (Intel) | 2c9a98d | 2021-01-18 15:12:21 +0100 | [diff] [blame] | 6204 | #endif |
| 6205 | |
Thomas Gleixner | c1a280b | 2019-07-26 23:19:37 +0200 | [diff] [blame] | 6206 | #endif /* CONFIG_PREEMPTION */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | |
Peter Zijlstra (Intel) | 826bfeb | 2021-01-18 15:12:23 +0100 | [diff] [blame] | 6208 | #ifdef CONFIG_PREEMPT_DYNAMIC |
| 6209 | |
| 6210 | #include <linux/entry-common.h> |
| 6211 | |
| 6212 | /* |
| 6213 | * SC:cond_resched |
| 6214 | * SC:might_resched |
| 6215 | * SC:preempt_schedule |
| 6216 | * SC:preempt_schedule_notrace |
| 6217 | * SC:irqentry_exit_cond_resched |
| 6218 | * |
| 6219 | * |
| 6220 | * NONE: |
| 6221 | * cond_resched <- __cond_resched |
| 6222 | * might_resched <- RET0 |
| 6223 | * preempt_schedule <- NOP |
| 6224 | * preempt_schedule_notrace <- NOP |
| 6225 | * irqentry_exit_cond_resched <- NOP |
| 6226 | * |
| 6227 | * VOLUNTARY: |
| 6228 | * cond_resched <- __cond_resched |
| 6229 | * might_resched <- __cond_resched |
| 6230 | * preempt_schedule <- NOP |
| 6231 | * preempt_schedule_notrace <- NOP |
| 6232 | * irqentry_exit_cond_resched <- NOP |
| 6233 | * |
| 6234 | * FULL: |
| 6235 | * cond_resched <- RET0 |
| 6236 | * might_resched <- RET0 |
| 6237 | * preempt_schedule <- preempt_schedule |
| 6238 | * preempt_schedule_notrace <- preempt_schedule_notrace |
| 6239 | * irqentry_exit_cond_resched <- irqentry_exit_cond_resched |
| 6240 | */ |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6241 | |
| 6242 | enum { |
| 6243 | preempt_dynamic_none = 0, |
| 6244 | preempt_dynamic_voluntary, |
| 6245 | preempt_dynamic_full, |
| 6246 | }; |
| 6247 | |
Peter Zijlstra | 1011dcc | 2021-03-25 12:21:38 +0100 | [diff] [blame] | 6248 | int preempt_dynamic_mode = preempt_dynamic_full; |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6249 | |
Peter Zijlstra | 1011dcc | 2021-03-25 12:21:38 +0100 | [diff] [blame] | 6250 | int sched_dynamic_mode(const char *str) |
Peter Zijlstra (Intel) | 826bfeb | 2021-01-18 15:12:23 +0100 | [diff] [blame] | 6251 | { |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6252 | if (!strcmp(str, "none")) |
Rasmus Villemoes | 7e1b2eb | 2021-03-25 01:45:14 +0100 | [diff] [blame] | 6253 | return preempt_dynamic_none; |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6254 | |
| 6255 | if (!strcmp(str, "voluntary")) |
Rasmus Villemoes | 7e1b2eb | 2021-03-25 01:45:14 +0100 | [diff] [blame] | 6256 | return preempt_dynamic_voluntary; |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6257 | |
| 6258 | if (!strcmp(str, "full")) |
Rasmus Villemoes | 7e1b2eb | 2021-03-25 01:45:14 +0100 | [diff] [blame] | 6259 | return preempt_dynamic_full; |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6260 | |
Rasmus Villemoes | c4681f3 | 2021-03-25 01:45:15 +0100 | [diff] [blame] | 6261 | return -EINVAL; |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6262 | } |
| 6263 | |
Peter Zijlstra | 1011dcc | 2021-03-25 12:21:38 +0100 | [diff] [blame] | 6264 | void sched_dynamic_update(int mode) |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6265 | { |
| 6266 | /* |
| 6267 | * Avoid {NONE,VOLUNTARY} -> FULL transitions from ever ending up in |
| 6268 | * the ZERO state, which is invalid. |
| 6269 | */ |
| 6270 | static_call_update(cond_resched, __cond_resched); |
| 6271 | static_call_update(might_resched, __cond_resched); |
| 6272 | static_call_update(preempt_schedule, __preempt_schedule_func); |
| 6273 | static_call_update(preempt_schedule_notrace, __preempt_schedule_notrace_func); |
| 6274 | static_call_update(irqentry_exit_cond_resched, irqentry_exit_cond_resched); |
| 6275 | |
| 6276 | switch (mode) { |
| 6277 | case preempt_dynamic_none: |
Peter Zijlstra (Intel) | 826bfeb | 2021-01-18 15:12:23 +0100 | [diff] [blame] | 6278 | static_call_update(cond_resched, __cond_resched); |
Peter Zijlstra | 9432bbd | 2021-03-23 16:49:03 +0100 | [diff] [blame] | 6279 | static_call_update(might_resched, (void *)&__static_call_return0); |
| 6280 | static_call_update(preempt_schedule, NULL); |
| 6281 | static_call_update(preempt_schedule_notrace, NULL); |
| 6282 | static_call_update(irqentry_exit_cond_resched, NULL); |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6283 | pr_info("Dynamic Preempt: none\n"); |
| 6284 | break; |
| 6285 | |
| 6286 | case preempt_dynamic_voluntary: |
Peter Zijlstra (Intel) | 826bfeb | 2021-01-18 15:12:23 +0100 | [diff] [blame] | 6287 | static_call_update(cond_resched, __cond_resched); |
| 6288 | static_call_update(might_resched, __cond_resched); |
Peter Zijlstra | 9432bbd | 2021-03-23 16:49:03 +0100 | [diff] [blame] | 6289 | static_call_update(preempt_schedule, NULL); |
| 6290 | static_call_update(preempt_schedule_notrace, NULL); |
| 6291 | static_call_update(irqentry_exit_cond_resched, NULL); |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6292 | pr_info("Dynamic Preempt: voluntary\n"); |
| 6293 | break; |
| 6294 | |
| 6295 | case preempt_dynamic_full: |
Peter Zijlstra | 9432bbd | 2021-03-23 16:49:03 +0100 | [diff] [blame] | 6296 | static_call_update(cond_resched, (void *)&__static_call_return0); |
| 6297 | static_call_update(might_resched, (void *)&__static_call_return0); |
Peter Zijlstra (Intel) | 826bfeb | 2021-01-18 15:12:23 +0100 | [diff] [blame] | 6298 | static_call_update(preempt_schedule, __preempt_schedule_func); |
| 6299 | static_call_update(preempt_schedule_notrace, __preempt_schedule_notrace_func); |
| 6300 | static_call_update(irqentry_exit_cond_resched, irqentry_exit_cond_resched); |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6301 | pr_info("Dynamic Preempt: full\n"); |
| 6302 | break; |
| 6303 | } |
| 6304 | |
| 6305 | preempt_dynamic_mode = mode; |
| 6306 | } |
| 6307 | |
| 6308 | static int __init setup_preempt_mode(char *str) |
| 6309 | { |
| 6310 | int mode = sched_dynamic_mode(str); |
| 6311 | if (mode < 0) { |
| 6312 | pr_warn("Dynamic Preempt: unsupported mode: %s\n", str); |
Peter Zijlstra (Intel) | 826bfeb | 2021-01-18 15:12:23 +0100 | [diff] [blame] | 6313 | return 1; |
| 6314 | } |
Peter Zijlstra | e59e10f | 2021-01-22 13:01:58 +0100 | [diff] [blame] | 6315 | |
| 6316 | sched_dynamic_update(mode); |
Peter Zijlstra (Intel) | 826bfeb | 2021-01-18 15:12:23 +0100 | [diff] [blame] | 6317 | return 0; |
| 6318 | } |
| 6319 | __setup("preempt=", setup_preempt_mode); |
| 6320 | |
| 6321 | #endif /* CONFIG_PREEMPT_DYNAMIC */ |
| 6322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | /* |
Valentin Schneider | a49b4f4 | 2019-09-23 15:36:12 +0100 | [diff] [blame] | 6324 | * This is the entry point to schedule() from kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | * off of irq context. |
| 6326 | * Note, that this is called and return with irqs disabled. This will |
| 6327 | * protect us against recursive calling from irq. |
| 6328 | */ |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 6329 | asmlinkage __visible void __sched preempt_schedule_irq(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | { |
Frederic Weisbecker | b22366c | 2013-02-24 12:59:30 +0100 | [diff] [blame] | 6331 | enum ctx_state prev_state; |
Ingo Molnar | 6478d88 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 6332 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 6333 | /* Catch callers which need to be fixed */ |
Peter Zijlstra | f27dde8 | 2013-08-14 14:55:31 +0200 | [diff] [blame] | 6334 | BUG_ON(preempt_count() || !irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6335 | |
Frederic Weisbecker | b22366c | 2013-02-24 12:59:30 +0100 | [diff] [blame] | 6336 | prev_state = exception_enter(); |
| 6337 | |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6338 | do { |
Peter Zijlstra | 3d8f74d | 2015-09-28 18:09:19 +0200 | [diff] [blame] | 6339 | preempt_disable(); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6340 | local_irq_enable(); |
Peter Zijlstra | fc13aeb | 2015-09-28 18:05:34 +0200 | [diff] [blame] | 6341 | __schedule(true); |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6342 | local_irq_disable(); |
Peter Zijlstra | 3d8f74d | 2015-09-28 18:09:19 +0200 | [diff] [blame] | 6343 | sched_preempt_enable_no_resched(); |
Lai Jiangshan | 5ed0cec | 2009-03-06 19:40:20 +0800 | [diff] [blame] | 6344 | } while (need_resched()); |
Frederic Weisbecker | b22366c | 2013-02-24 12:59:30 +0100 | [diff] [blame] | 6345 | |
| 6346 | exception_exit(prev_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6347 | } |
| 6348 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 6349 | int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 6350 | void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | { |
Chris Wilson | 062d3f9 | 2020-07-23 21:10:42 +0100 | [diff] [blame] | 6352 | WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~WF_SYNC); |
Peter Zijlstra | 63859d4 | 2009-09-15 19:14:42 +0200 | [diff] [blame] | 6353 | return try_to_wake_up(curr->private, mode, wake_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6355 | EXPORT_SYMBOL(default_wake_function); |
| 6356 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6357 | #ifdef CONFIG_RT_MUTEXES |
| 6358 | |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6359 | static inline int __rt_effective_prio(struct task_struct *pi_task, int prio) |
| 6360 | { |
| 6361 | if (pi_task) |
| 6362 | prio = min(prio, pi_task->prio); |
| 6363 | |
| 6364 | return prio; |
| 6365 | } |
| 6366 | |
| 6367 | static inline int rt_effective_prio(struct task_struct *p, int prio) |
| 6368 | { |
| 6369 | struct task_struct *pi_task = rt_mutex_get_top_task(p); |
| 6370 | |
| 6371 | return __rt_effective_prio(pi_task, prio); |
| 6372 | } |
| 6373 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6374 | /* |
| 6375 | * rt_mutex_setprio - set the current priority of a task |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6376 | * @p: task to boost |
| 6377 | * @pi_task: donor task |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6378 | * |
| 6379 | * This function changes the 'effective' priority of a task. It does |
| 6380 | * not touch ->normal_prio like __setscheduler(). |
| 6381 | * |
Thomas Gleixner | c365c29 | 2014-02-07 20:58:42 +0100 | [diff] [blame] | 6382 | * Used by the rt_mutex code to implement priority inheritance |
| 6383 | * logic. Call site only calls if the priority of the task changed. |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6384 | */ |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6385 | void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6386 | { |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6387 | int prio, oldprio, queued, running, queue_flag = |
Peter Zijlstra | 7a57f32 | 2017-02-21 14:47:02 +0100 | [diff] [blame] | 6388 | DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; |
Thomas Gleixner | 83ab0aa | 2010-02-17 09:05:48 +0100 | [diff] [blame] | 6389 | const struct sched_class *prev_class; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 6390 | struct rq_flags rf; |
| 6391 | struct rq *rq; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6392 | |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6393 | /* XXX used to be waiter->prio, not waiter->task->prio */ |
| 6394 | prio = __rt_effective_prio(pi_task, p->normal_prio); |
| 6395 | |
| 6396 | /* |
| 6397 | * If nothing changed; bail early. |
| 6398 | */ |
| 6399 | if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio)) |
| 6400 | return; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6401 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 6402 | rq = __task_rq_lock(p, &rf); |
Peter Zijlstra | 80f5c1b | 2016-10-03 16:28:37 +0200 | [diff] [blame] | 6403 | update_rq_clock(rq); |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6404 | /* |
| 6405 | * Set under pi_lock && rq->lock, such that the value can be used under |
| 6406 | * either lock. |
| 6407 | * |
| 6408 | * Note that there is loads of tricky to make this pointer cache work |
| 6409 | * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to |
| 6410 | * ensure a task is de-boosted (pi_task is set to NULL) before the |
| 6411 | * task is allowed to run again (and can exit). This ensures the pointer |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 6412 | * points to a blocked task -- which guarantees the task is present. |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6413 | */ |
| 6414 | p->pi_top_task = pi_task; |
| 6415 | |
| 6416 | /* |
| 6417 | * For FIFO/RR we only need to set prio, if that matches we're done. |
| 6418 | */ |
| 6419 | if (prio == p->prio && !dl_prio(prio)) |
| 6420 | goto out_unlock; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6421 | |
Thomas Gleixner | 1c4dd99 | 2011-06-06 20:07:38 +0200 | [diff] [blame] | 6422 | /* |
| 6423 | * Idle task boosting is a nono in general. There is one |
| 6424 | * exception, when PREEMPT_RT and NOHZ is active: |
| 6425 | * |
| 6426 | * The idle task calls get_next_timer_interrupt() and holds |
| 6427 | * the timer wheel base->lock on the CPU and another CPU wants |
| 6428 | * to access the timer (probably to cancel it). We can safely |
| 6429 | * ignore the boosting request, as the idle CPU runs this code |
| 6430 | * with interrupts disabled and will complete the lock |
| 6431 | * protected section without being interrupted. So there is no |
| 6432 | * real need to boost. |
| 6433 | */ |
| 6434 | if (unlikely(p == rq->idle)) { |
| 6435 | WARN_ON(p != rq->curr); |
| 6436 | WARN_ON(p->pi_blocked_on); |
| 6437 | goto out_unlock; |
| 6438 | } |
| 6439 | |
Peter Zijlstra | b91473f | 2017-03-23 15:56:12 +0100 | [diff] [blame] | 6440 | trace_sched_pi_setprio(p, pi_task); |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 6441 | oldprio = p->prio; |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 6442 | |
| 6443 | if (oldprio == prio) |
| 6444 | queue_flag &= ~DEQUEUE_MOVE; |
| 6445 | |
Thomas Gleixner | 83ab0aa | 2010-02-17 09:05:48 +0100 | [diff] [blame] | 6446 | prev_class = p->sched_class; |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 6447 | queued = task_on_rq_queued(p); |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 6448 | running = task_current(rq, p); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 6449 | if (queued) |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 6450 | dequeue_task(rq, p, queue_flag); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 6451 | if (running) |
Kirill Tkhai | f3cd1c4 | 2014-09-12 17:41:40 +0400 | [diff] [blame] | 6452 | put_prev_task(rq, p); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6453 | |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 6454 | /* |
| 6455 | * Boosting condition are: |
| 6456 | * 1. -rt task is running and holds mutex A |
| 6457 | * --> -dl task blocks on mutex A |
| 6458 | * |
| 6459 | * 2. -dl task is running and holds mutex A |
| 6460 | * --> -dl task blocks on mutex A and could preempt the |
| 6461 | * running task |
| 6462 | */ |
| 6463 | if (dl_prio(prio)) { |
Oleg Nesterov | 466af29 | 2014-06-06 18:52:06 +0200 | [diff] [blame] | 6464 | if (!dl_prio(p->normal_prio) || |
Juri Lelli | 740797c | 2018-11-19 16:32:01 +0100 | [diff] [blame] | 6465 | (pi_task && dl_prio(pi_task->prio) && |
| 6466 | dl_entity_preempt(&pi_task->dl, &p->dl))) { |
Juri Lelli | 2279f54 | 2020-11-17 07:14:32 +0100 | [diff] [blame] | 6467 | p->dl.pi_se = pi_task->dl.pi_se; |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 6468 | queue_flag |= ENQUEUE_REPLENISH; |
Juri Lelli | 2279f54 | 2020-11-17 07:14:32 +0100 | [diff] [blame] | 6469 | } else { |
| 6470 | p->dl.pi_se = &p->dl; |
| 6471 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 6472 | p->sched_class = &dl_sched_class; |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 6473 | } else if (rt_prio(prio)) { |
| 6474 | if (dl_prio(oldprio)) |
Juri Lelli | 2279f54 | 2020-11-17 07:14:32 +0100 | [diff] [blame] | 6475 | p->dl.pi_se = &p->dl; |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 6476 | if (oldprio < prio) |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 6477 | queue_flag |= ENQUEUE_HEAD; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6478 | p->sched_class = &rt_sched_class; |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 6479 | } else { |
| 6480 | if (dl_prio(oldprio)) |
Juri Lelli | 2279f54 | 2020-11-17 07:14:32 +0100 | [diff] [blame] | 6481 | p->dl.pi_se = &p->dl; |
Brian Silverman | 746db94 | 2015-02-18 16:23:56 -0800 | [diff] [blame] | 6482 | if (rt_prio(oldprio)) |
| 6483 | p->rt.timeout = 0; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6484 | p->sched_class = &fair_sched_class; |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 6485 | } |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6486 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6487 | p->prio = prio; |
| 6488 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 6489 | if (queued) |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 6490 | enqueue_task(rq, p, queue_flag); |
Vincent Guittot | a399d23 | 2016-09-12 09:47:52 +0200 | [diff] [blame] | 6491 | if (running) |
Peter Zijlstra | 03b7fad | 2019-05-29 20:36:41 +0000 | [diff] [blame] | 6492 | set_next_task(rq, p); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 6493 | |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 6494 | check_class_changed(rq, p, prev_class, oldprio); |
Thomas Gleixner | 1c4dd99 | 2011-06-06 20:07:38 +0200 | [diff] [blame] | 6495 | out_unlock: |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6496 | /* Avoid rq from going away on us: */ |
| 6497 | preempt_disable(); |
Peter Zijlstra | 4c9a4bc | 2015-06-11 14:46:39 +0200 | [diff] [blame] | 6498 | |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 6499 | rq_unpin_lock(rq, &rf); |
| 6500 | __balance_callbacks(rq); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 6501 | raw_spin_rq_unlock(rq); |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 6502 | |
Peter Zijlstra | 4c9a4bc | 2015-06-11 14:46:39 +0200 | [diff] [blame] | 6503 | preempt_enable(); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6504 | } |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6505 | #else |
| 6506 | static inline int rt_effective_prio(struct task_struct *p, int prio) |
| 6507 | { |
| 6508 | return prio; |
| 6509 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6510 | #endif |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 6511 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6512 | void set_user_nice(struct task_struct *p, long nice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6513 | { |
Peter Zijlstra | 49bd21e | 2016-09-20 22:06:01 +0200 | [diff] [blame] | 6514 | bool queued, running; |
Qian Cai | 53a2336 | 2019-12-19 09:03:14 -0500 | [diff] [blame] | 6515 | int old_prio; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 6516 | struct rq_flags rf; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6517 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | |
Dongsheng Yang | 75e45d5 | 2014-02-11 15:34:50 +0800 | [diff] [blame] | 6519 | if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | return; |
| 6521 | /* |
| 6522 | * We have to be careful, if called from sys_setpriority(), |
| 6523 | * the task might be in the middle of scheduling on another CPU. |
| 6524 | */ |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 6525 | rq = task_rq_lock(p, &rf); |
Peter Zijlstra | 2fb8d36 | 2016-10-03 16:44:25 +0200 | [diff] [blame] | 6526 | update_rq_clock(rq); |
| 6527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | /* |
| 6529 | * The RT priorities are set via sched_setscheduler(), but we still |
| 6530 | * allow the 'normal' nice value to be set - but as expected |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 6531 | * it won't have any effect on scheduling until the task is |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 6532 | * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | */ |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 6534 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | p->static_prio = NICE_TO_PRIO(nice); |
| 6536 | goto out_unlock; |
| 6537 | } |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 6538 | queued = task_on_rq_queued(p); |
Peter Zijlstra | 49bd21e | 2016-09-20 22:06:01 +0200 | [diff] [blame] | 6539 | running = task_current(rq, p); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 6540 | if (queued) |
Peter Zijlstra | 7a57f32 | 2017-02-21 14:47:02 +0100 | [diff] [blame] | 6541 | dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK); |
Peter Zijlstra | 49bd21e | 2016-09-20 22:06:01 +0200 | [diff] [blame] | 6542 | if (running) |
| 6543 | put_prev_task(rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | p->static_prio = NICE_TO_PRIO(nice); |
Vincent Guittot | 9059393 | 2017-05-17 11:50:45 +0200 | [diff] [blame] | 6546 | set_load_weight(p, true); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6547 | old_prio = p->prio; |
| 6548 | p->prio = effective_prio(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | |
Frederic Weisbecker | 5443a0b | 2019-12-03 17:01:06 +0100 | [diff] [blame] | 6550 | if (queued) |
Peter Zijlstra | 7134b3e | 2017-02-21 14:23:38 +0100 | [diff] [blame] | 6551 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
Peter Zijlstra | 49bd21e | 2016-09-20 22:06:01 +0200 | [diff] [blame] | 6552 | if (running) |
Peter Zijlstra | 03b7fad | 2019-05-29 20:36:41 +0000 | [diff] [blame] | 6553 | set_next_task(rq, p); |
Frederic Weisbecker | 5443a0b | 2019-12-03 17:01:06 +0100 | [diff] [blame] | 6554 | |
| 6555 | /* |
| 6556 | * If the task increased its priority or is running and |
| 6557 | * lowered its priority, then reschedule its CPU: |
| 6558 | */ |
| 6559 | p->sched_class->prio_changed(rq, p, old_prio); |
| 6560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | out_unlock: |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 6562 | task_rq_unlock(rq, p, &rf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6563 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | EXPORT_SYMBOL(set_user_nice); |
| 6565 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 6566 | /* |
| 6567 | * can_nice - check if a task can reduce its nice value |
| 6568 | * @p: task |
| 6569 | * @nice: nice value |
| 6570 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6571 | int can_nice(const struct task_struct *p, const int nice) |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 6572 | { |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6573 | /* Convert nice value [19,-20] to rlimit style value [1,40]: */ |
Dongsheng Yang | 7aa2c01 | 2014-05-08 18:33:49 +0900 | [diff] [blame] | 6574 | int nice_rlim = nice_to_rlimit(nice); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6575 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 6576 | return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) || |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 6577 | capable(CAP_SYS_NICE)); |
| 6578 | } |
| 6579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6580 | #ifdef __ARCH_WANT_SYS_NICE |
| 6581 | |
| 6582 | /* |
| 6583 | * sys_nice - change the priority of the current process. |
| 6584 | * @increment: priority increment |
| 6585 | * |
| 6586 | * sys_setpriority is a more generic, but much slower function that |
| 6587 | * does similar things. |
| 6588 | */ |
Heiko Carstens | 5add95d | 2009-01-14 14:14:08 +0100 | [diff] [blame] | 6589 | SYSCALL_DEFINE1(nice, int, increment) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6590 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6591 | long nice, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | |
| 6593 | /* |
| 6594 | * Setpriority might change our priority at the same moment. |
| 6595 | * We don't have to worry. Conceptually one call occurs first |
| 6596 | * and we have a single winner. |
| 6597 | */ |
Dongsheng Yang | a9467fa | 2014-05-08 18:35:15 +0900 | [diff] [blame] | 6598 | increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH); |
Dongsheng Yang | d0ea026 | 2014-01-27 22:00:45 -0500 | [diff] [blame] | 6599 | nice = task_nice(current) + increment; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6600 | |
Dongsheng Yang | a9467fa | 2014-05-08 18:35:15 +0900 | [diff] [blame] | 6601 | nice = clamp_val(nice, MIN_NICE, MAX_NICE); |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 6602 | if (increment < 0 && !can_nice(current, nice)) |
| 6603 | return -EPERM; |
| 6604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | retval = security_task_setnice(current, nice); |
| 6606 | if (retval) |
| 6607 | return retval; |
| 6608 | |
| 6609 | set_user_nice(current, nice); |
| 6610 | return 0; |
| 6611 | } |
| 6612 | |
| 6613 | #endif |
| 6614 | |
| 6615 | /** |
| 6616 | * task_prio - return the priority value of a given task. |
| 6617 | * @p: the task in question. |
| 6618 | * |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 6619 | * Return: The priority value as seen by users in /proc. |
Dietmar Eggemann | c541bb7 | 2021-01-28 14:10:40 +0100 | [diff] [blame] | 6620 | * |
| 6621 | * sched policy return value kernel prio user prio/nice |
| 6622 | * |
| 6623 | * normal, batch, idle [0 ... 39] [100 ... 139] 0/[-20 ... 19] |
| 6624 | * fifo, rr [-2 ... -100] [98 ... 0] [1 ... 99] |
| 6625 | * deadline -101 -1 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6626 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6627 | int task_prio(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6628 | { |
| 6629 | return p->prio - MAX_RT_PRIO; |
| 6630 | } |
| 6631 | |
| 6632 | /** |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6633 | * idle_cpu - is a given CPU idle currently? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6634 | * @cpu: the processor in question. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 6635 | * |
| 6636 | * Return: 1 if the CPU is currently idle. 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | */ |
| 6638 | int idle_cpu(int cpu) |
| 6639 | { |
Thomas Gleixner | 908a328 | 2011-09-15 15:32:06 +0200 | [diff] [blame] | 6640 | struct rq *rq = cpu_rq(cpu); |
| 6641 | |
| 6642 | if (rq->curr != rq->idle) |
| 6643 | return 0; |
| 6644 | |
| 6645 | if (rq->nr_running) |
| 6646 | return 0; |
| 6647 | |
| 6648 | #ifdef CONFIG_SMP |
Peter Zijlstra | 126c209 | 2020-05-26 18:11:03 +0200 | [diff] [blame] | 6649 | if (rq->ttwu_pending) |
Thomas Gleixner | 908a328 | 2011-09-15 15:32:06 +0200 | [diff] [blame] | 6650 | return 0; |
| 6651 | #endif |
| 6652 | |
| 6653 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6654 | } |
| 6655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | /** |
Rohit Jain | 943d355 | 2018-05-09 09:39:48 -0700 | [diff] [blame] | 6657 | * available_idle_cpu - is a given CPU idle for enqueuing work. |
| 6658 | * @cpu: the CPU in question. |
| 6659 | * |
| 6660 | * Return: 1 if the CPU is currently idle. 0 otherwise. |
| 6661 | */ |
| 6662 | int available_idle_cpu(int cpu) |
| 6663 | { |
| 6664 | if (!idle_cpu(cpu)) |
| 6665 | return 0; |
| 6666 | |
Rohit Jain | 247f2f6 | 2018-05-02 13:52:10 -0700 | [diff] [blame] | 6667 | if (vcpu_is_preempted(cpu)) |
| 6668 | return 0; |
| 6669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | return 1; |
| 6671 | } |
| 6672 | |
| 6673 | /** |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6674 | * idle_task - return the idle task for a given CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6675 | * @cpu: the processor in question. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 6676 | * |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6677 | * Return: The idle task for the CPU @cpu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6678 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6679 | struct task_struct *idle_task(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6680 | { |
| 6681 | return cpu_rq(cpu)->idle; |
| 6682 | } |
| 6683 | |
Viresh Kumar | 7d6a905f | 2020-12-08 09:46:55 +0530 | [diff] [blame] | 6684 | #ifdef CONFIG_SMP |
| 6685 | /* |
| 6686 | * This function computes an effective utilization for the given CPU, to be |
| 6687 | * used for frequency selection given the linear relation: f = u * f_max. |
| 6688 | * |
| 6689 | * The scheduler tracks the following metrics: |
| 6690 | * |
| 6691 | * cpu_util_{cfs,rt,dl,irq}() |
| 6692 | * cpu_bw_dl() |
| 6693 | * |
| 6694 | * Where the cfs,rt and dl util numbers are tracked with the same metric and |
| 6695 | * synchronized windows and are thus directly comparable. |
| 6696 | * |
| 6697 | * The cfs,rt,dl utilization are the running times measured with rq->clock_task |
| 6698 | * which excludes things like IRQ and steal-time. These latter are then accrued |
| 6699 | * in the irq utilization. |
| 6700 | * |
| 6701 | * The DL bandwidth number otoh is not a measured metric but a value computed |
| 6702 | * based on the task model parameters and gives the minimal utilization |
| 6703 | * required to meet deadlines. |
| 6704 | */ |
Viresh Kumar | a5418be | 2020-12-08 09:46:56 +0530 | [diff] [blame] | 6705 | unsigned long effective_cpu_util(int cpu, unsigned long util_cfs, |
| 6706 | unsigned long max, enum cpu_util_type type, |
Viresh Kumar | 7d6a905f | 2020-12-08 09:46:55 +0530 | [diff] [blame] | 6707 | struct task_struct *p) |
| 6708 | { |
| 6709 | unsigned long dl_util, util, irq; |
| 6710 | struct rq *rq = cpu_rq(cpu); |
| 6711 | |
| 6712 | if (!uclamp_is_used() && |
| 6713 | type == FREQUENCY_UTIL && rt_rq_is_runnable(&rq->rt)) { |
| 6714 | return max; |
| 6715 | } |
| 6716 | |
| 6717 | /* |
| 6718 | * Early check to see if IRQ/steal time saturates the CPU, can be |
| 6719 | * because of inaccuracies in how we track these -- see |
| 6720 | * update_irq_load_avg(). |
| 6721 | */ |
| 6722 | irq = cpu_util_irq(rq); |
| 6723 | if (unlikely(irq >= max)) |
| 6724 | return max; |
| 6725 | |
| 6726 | /* |
| 6727 | * Because the time spend on RT/DL tasks is visible as 'lost' time to |
| 6728 | * CFS tasks and we use the same metric to track the effective |
| 6729 | * utilization (PELT windows are synchronized) we can directly add them |
| 6730 | * to obtain the CPU's actual utilization. |
| 6731 | * |
| 6732 | * CFS and RT utilization can be boosted or capped, depending on |
| 6733 | * utilization clamp constraints requested by currently RUNNABLE |
| 6734 | * tasks. |
| 6735 | * When there are no CFS RUNNABLE tasks, clamps are released and |
| 6736 | * frequency will be gracefully reduced with the utilization decay. |
| 6737 | */ |
| 6738 | util = util_cfs + cpu_util_rt(rq); |
| 6739 | if (type == FREQUENCY_UTIL) |
| 6740 | util = uclamp_rq_util_with(rq, util, p); |
| 6741 | |
| 6742 | dl_util = cpu_util_dl(rq); |
| 6743 | |
| 6744 | /* |
| 6745 | * For frequency selection we do not make cpu_util_dl() a permanent part |
| 6746 | * of this sum because we want to use cpu_bw_dl() later on, but we need |
| 6747 | * to check if the CFS+RT+DL sum is saturated (ie. no idle time) such |
| 6748 | * that we select f_max when there is no idle time. |
| 6749 | * |
| 6750 | * NOTE: numerical errors or stop class might cause us to not quite hit |
| 6751 | * saturation when we should -- something for later. |
| 6752 | */ |
| 6753 | if (util + dl_util >= max) |
| 6754 | return max; |
| 6755 | |
| 6756 | /* |
| 6757 | * OTOH, for energy computation we need the estimated running time, so |
| 6758 | * include util_dl and ignore dl_bw. |
| 6759 | */ |
| 6760 | if (type == ENERGY_UTIL) |
| 6761 | util += dl_util; |
| 6762 | |
| 6763 | /* |
| 6764 | * There is still idle time; further improve the number by using the |
| 6765 | * irq metric. Because IRQ/steal time is hidden from the task clock we |
| 6766 | * need to scale the task numbers: |
| 6767 | * |
| 6768 | * max - irq |
| 6769 | * U' = irq + --------- * U |
| 6770 | * max |
| 6771 | */ |
| 6772 | util = scale_irq_capacity(util, irq, max); |
| 6773 | util += irq; |
| 6774 | |
| 6775 | /* |
| 6776 | * Bandwidth required by DEADLINE must always be granted while, for |
| 6777 | * FAIR and RT, we use blocked utilization of IDLE CPUs as a mechanism |
| 6778 | * to gracefully reduce the frequency when no tasks show up for longer |
| 6779 | * periods of time. |
| 6780 | * |
| 6781 | * Ideally we would like to set bw_dl as min/guaranteed freq and util + |
| 6782 | * bw_dl as requested freq. However, cpufreq is not yet ready for such |
| 6783 | * an interface. So, we only do the latter for now. |
| 6784 | */ |
| 6785 | if (type == FREQUENCY_UTIL) |
| 6786 | util += cpu_bw_dl(rq); |
| 6787 | |
| 6788 | return min(max, util); |
| 6789 | } |
Viresh Kumar | a5418be | 2020-12-08 09:46:56 +0530 | [diff] [blame] | 6790 | |
| 6791 | unsigned long sched_cpu_util(int cpu, unsigned long max) |
| 6792 | { |
| 6793 | return effective_cpu_util(cpu, cpu_util_cfs(cpu_rq(cpu)), max, |
| 6794 | ENERGY_UTIL, NULL); |
| 6795 | } |
Viresh Kumar | 7d6a905f | 2020-12-08 09:46:55 +0530 | [diff] [blame] | 6796 | #endif /* CONFIG_SMP */ |
| 6797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | /** |
| 6799 | * find_process_by_pid - find a process with a matching PID value. |
| 6800 | * @pid: the pid in question. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 6801 | * |
| 6802 | * The task of @pid, if found. %NULL otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6803 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 6804 | static struct task_struct *find_process_by_pid(pid_t pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 6806 | return pid ? find_task_by_vpid(pid) : current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | } |
| 6808 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 6809 | /* |
Steven Rostedt | c13db6b | 2014-07-23 11:28:26 -0400 | [diff] [blame] | 6810 | * sched_setparam() passes in -1 for its policy, to let the functions |
| 6811 | * it calls know not to change it. |
| 6812 | */ |
| 6813 | #define SETPARAM_POLICY -1 |
| 6814 | |
Thomas Gleixner | c365c29 | 2014-02-07 20:58:42 +0100 | [diff] [blame] | 6815 | static void __setscheduler_params(struct task_struct *p, |
| 6816 | const struct sched_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | { |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 6818 | int policy = attr->sched_policy; |
| 6819 | |
Steven Rostedt | c13db6b | 2014-07-23 11:28:26 -0400 | [diff] [blame] | 6820 | if (policy == SETPARAM_POLICY) |
Peter Zijlstra | 39fd8fd | 2014-01-15 16:33:20 +0100 | [diff] [blame] | 6821 | policy = p->policy; |
| 6822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6823 | p->policy = policy; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 6824 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 6825 | if (dl_policy(policy)) |
| 6826 | __setparam_dl(p, attr); |
Peter Zijlstra | 39fd8fd | 2014-01-15 16:33:20 +0100 | [diff] [blame] | 6827 | else if (fair_policy(policy)) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 6828 | p->static_prio = NICE_TO_PRIO(attr->sched_nice); |
| 6829 | |
Peter Zijlstra | 39fd8fd | 2014-01-15 16:33:20 +0100 | [diff] [blame] | 6830 | /* |
| 6831 | * __sched_setscheduler() ensures attr->sched_priority == 0 when |
| 6832 | * !rt_policy. Always setting this ensures that things like |
| 6833 | * getparam()/getattr() don't report silly values for !rt tasks. |
| 6834 | */ |
| 6835 | p->rt_priority = attr->sched_priority; |
Steven Rostedt | 383afd0 | 2014-03-11 19:24:20 -0400 | [diff] [blame] | 6836 | p->normal_prio = normal_prio(p); |
Vincent Guittot | 9059393 | 2017-05-17 11:50:45 +0200 | [diff] [blame] | 6837 | set_load_weight(p, true); |
Thomas Gleixner | c365c29 | 2014-02-07 20:58:42 +0100 | [diff] [blame] | 6838 | } |
Peter Zijlstra | 39fd8fd | 2014-01-15 16:33:20 +0100 | [diff] [blame] | 6839 | |
Thomas Gleixner | c365c29 | 2014-02-07 20:58:42 +0100 | [diff] [blame] | 6840 | /* Actually do priority change: must hold pi & rq lock. */ |
| 6841 | static void __setscheduler(struct rq *rq, struct task_struct *p, |
Thomas Gleixner | 0782e63 | 2015-05-05 19:49:49 +0200 | [diff] [blame] | 6842 | const struct sched_attr *attr, bool keep_boost) |
Thomas Gleixner | c365c29 | 2014-02-07 20:58:42 +0100 | [diff] [blame] | 6843 | { |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 6844 | /* |
| 6845 | * If params can't change scheduling class changes aren't allowed |
| 6846 | * either. |
| 6847 | */ |
| 6848 | if (attr->sched_flags & SCHED_FLAG_KEEP_PARAMS) |
| 6849 | return; |
| 6850 | |
Thomas Gleixner | c365c29 | 2014-02-07 20:58:42 +0100 | [diff] [blame] | 6851 | __setscheduler_params(p, attr); |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 6852 | |
Steven Rostedt | 383afd0 | 2014-03-11 19:24:20 -0400 | [diff] [blame] | 6853 | /* |
Thomas Gleixner | 0782e63 | 2015-05-05 19:49:49 +0200 | [diff] [blame] | 6854 | * Keep a potential priority boosting if called from |
| 6855 | * sched_setscheduler(). |
Steven Rostedt | 383afd0 | 2014-03-11 19:24:20 -0400 | [diff] [blame] | 6856 | */ |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6857 | p->prio = normal_prio(p); |
Thomas Gleixner | 0782e63 | 2015-05-05 19:49:49 +0200 | [diff] [blame] | 6858 | if (keep_boost) |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 6859 | p->prio = rt_effective_prio(p, p->prio); |
Steven Rostedt | 383afd0 | 2014-03-11 19:24:20 -0400 | [diff] [blame] | 6860 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 6861 | if (dl_prio(p->prio)) |
| 6862 | p->sched_class = &dl_sched_class; |
| 6863 | else if (rt_prio(p->prio)) |
Peter Zijlstra | ffd44db | 2009-11-10 20:12:01 +0100 | [diff] [blame] | 6864 | p->sched_class = &rt_sched_class; |
| 6865 | else |
| 6866 | p->sched_class = &fair_sched_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6867 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 6868 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6869 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6870 | * Check the target process has a UID that matches the current process's: |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6871 | */ |
| 6872 | static bool check_same_owner(struct task_struct *p) |
| 6873 | { |
| 6874 | const struct cred *cred = current_cred(), *pcred; |
| 6875 | bool match; |
| 6876 | |
| 6877 | rcu_read_lock(); |
| 6878 | pcred = __task_cred(p); |
Eric W. Biederman | 9c806aa | 2012-02-02 18:54:02 -0800 | [diff] [blame] | 6879 | match = (uid_eq(cred->euid, pcred->euid) || |
| 6880 | uid_eq(cred->euid, pcred->uid)); |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6881 | rcu_read_unlock(); |
| 6882 | return match; |
| 6883 | } |
| 6884 | |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 6885 | static int __sched_setscheduler(struct task_struct *p, |
| 6886 | const struct sched_attr *attr, |
Peter Zijlstra | dbc7f06 | 2015-06-11 14:46:38 +0200 | [diff] [blame] | 6887 | bool user, bool pi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6888 | { |
Steven Rostedt | 383afd0 | 2014-03-11 19:24:20 -0400 | [diff] [blame] | 6889 | int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 : |
| 6890 | MAX_RT_PRIO - 1 - attr->sched_priority; |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 6891 | int retval, oldprio, oldpolicy = -1, queued, running; |
Thomas Gleixner | 0782e63 | 2015-05-05 19:49:49 +0200 | [diff] [blame] | 6892 | int new_effective_prio, policy = attr->sched_policy; |
Thomas Gleixner | 83ab0aa | 2010-02-17 09:05:48 +0100 | [diff] [blame] | 6893 | const struct sched_class *prev_class; |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 6894 | struct callback_head *head; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 6895 | struct rq_flags rf; |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 6896 | int reset_on_fork; |
Peter Zijlstra | 7a57f32 | 2017-02-21 14:47:02 +0100 | [diff] [blame] | 6897 | int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 6898 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6899 | |
Steven Rostedt (VMware) | 896bbb2 | 2017-03-09 10:18:42 -0500 | [diff] [blame] | 6900 | /* The pi code expects interrupts enabled */ |
| 6901 | BUG_ON(pi && in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6902 | recheck: |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6903 | /* Double check policy once rq lock held: */ |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 6904 | if (policy < 0) { |
| 6905 | reset_on_fork = p->sched_reset_on_fork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | policy = oldpolicy = p->policy; |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 6907 | } else { |
Peter Zijlstra | 7479f3c9c | 2014-01-15 17:05:04 +0100 | [diff] [blame] | 6908 | reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK); |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 6909 | |
Henrik Austad | 20f9cd2 | 2015-09-09 17:00:41 +0200 | [diff] [blame] | 6910 | if (!valid_policy(policy)) |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 6911 | return -EINVAL; |
| 6912 | } |
| 6913 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 6914 | if (attr->sched_flags & ~(SCHED_FLAG_ALL | SCHED_FLAG_SUGOV)) |
Peter Zijlstra | 7479f3c9c | 2014-01-15 17:05:04 +0100 | [diff] [blame] | 6915 | return -EINVAL; |
| 6916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6917 | /* |
| 6918 | * Valid priorities for SCHED_FIFO and SCHED_RR are |
Dietmar Eggemann | ae18ad2 | 2021-01-28 14:10:38 +0100 | [diff] [blame] | 6919 | * 1..MAX_RT_PRIO-1, valid priority for SCHED_NORMAL, |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6920 | * SCHED_BATCH and SCHED_IDLE is 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | */ |
Dietmar Eggemann | ae18ad2 | 2021-01-28 14:10:38 +0100 | [diff] [blame] | 6922 | if (attr->sched_priority > MAX_RT_PRIO-1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | return -EINVAL; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 6924 | if ((dl_policy(policy) && !__checkparam_dl(attr)) || |
| 6925 | (rt_policy(policy) != (attr->sched_priority != 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | return -EINVAL; |
| 6927 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 6928 | /* |
| 6929 | * Allow unprivileged RT tasks to decrease priority: |
| 6930 | */ |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 6931 | if (user && !capable(CAP_SYS_NICE)) { |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 6932 | if (fair_policy(policy)) { |
Dongsheng Yang | d0ea026 | 2014-01-27 22:00:45 -0500 | [diff] [blame] | 6933 | if (attr->sched_nice < task_nice(p) && |
Peter Zijlstra | eaad451 | 2014-01-16 17:54:25 +0100 | [diff] [blame] | 6934 | !can_nice(p, attr->sched_nice)) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 6935 | return -EPERM; |
| 6936 | } |
| 6937 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6938 | if (rt_policy(policy)) { |
Oleg Nesterov | a44702e8 | 2010-06-11 01:09:44 +0200 | [diff] [blame] | 6939 | unsigned long rlim_rtprio = |
| 6940 | task_rlimit(p, RLIMIT_RTPRIO); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 6941 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6942 | /* Can't set/change the rt policy: */ |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 6943 | if (policy != p->policy && !rlim_rtprio) |
| 6944 | return -EPERM; |
| 6945 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6946 | /* Can't increase priority: */ |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 6947 | if (attr->sched_priority > p->rt_priority && |
| 6948 | attr->sched_priority > rlim_rtprio) |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 6949 | return -EPERM; |
| 6950 | } |
Darren Hart | c02aa73 | 2011-02-17 15:37:07 -0800 | [diff] [blame] | 6951 | |
Juri Lelli | d44753b | 2014-03-03 12:09:21 +0100 | [diff] [blame] | 6952 | /* |
| 6953 | * Can't set/change SCHED_DEADLINE policy at all for now |
| 6954 | * (safest behavior); in the future we would like to allow |
| 6955 | * unprivileged DL tasks to increase their relative deadline |
| 6956 | * or reduce their runtime (both ways reducing utilization) |
| 6957 | */ |
| 6958 | if (dl_policy(policy)) |
| 6959 | return -EPERM; |
| 6960 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6961 | /* |
Darren Hart | c02aa73 | 2011-02-17 15:37:07 -0800 | [diff] [blame] | 6962 | * Treat SCHED_IDLE as nice 20. Only allow a switch to |
| 6963 | * SCHED_NORMAL if the RLIMIT_NICE would normally permit it. |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 6964 | */ |
Viresh Kumar | 1da1843 | 2018-11-05 16:51:55 +0530 | [diff] [blame] | 6965 | if (task_has_idle_policy(p) && !idle_policy(policy)) { |
Dongsheng Yang | d0ea026 | 2014-01-27 22:00:45 -0500 | [diff] [blame] | 6966 | if (!can_nice(p, task_nice(p))) |
Darren Hart | c02aa73 | 2011-02-17 15:37:07 -0800 | [diff] [blame] | 6967 | return -EPERM; |
| 6968 | } |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 6969 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6970 | /* Can't change other user's priorities: */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6971 | if (!check_same_owner(p)) |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 6972 | return -EPERM; |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 6973 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6974 | /* Normal users shall not reset the sched_reset_on_fork flag: */ |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 6975 | if (p->sched_reset_on_fork && !reset_on_fork) |
| 6976 | return -EPERM; |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 6977 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | |
Jeremy Fitzhardinge | 725aad2 | 2008-08-03 09:33:03 -0700 | [diff] [blame] | 6979 | if (user) { |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 6980 | if (attr->sched_flags & SCHED_FLAG_SUGOV) |
| 6981 | return -EINVAL; |
| 6982 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 6983 | retval = security_task_setscheduler(p); |
Jeremy Fitzhardinge | 725aad2 | 2008-08-03 09:33:03 -0700 | [diff] [blame] | 6984 | if (retval) |
| 6985 | return retval; |
| 6986 | } |
| 6987 | |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 6988 | /* Update task specific "requested" clamps */ |
| 6989 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) { |
| 6990 | retval = uclamp_validate(p, attr); |
| 6991 | if (retval) |
| 6992 | return retval; |
| 6993 | } |
| 6994 | |
Juri Lelli | 710da3c | 2019-07-19 16:00:00 +0200 | [diff] [blame] | 6995 | if (pi) |
| 6996 | cpuset_read_lock(); |
| 6997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 6999 | * Make sure no PI-waiters arrive (or leave) while we are |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 7000 | * changing the priority of the task: |
Peter Zijlstra | 0122ec5 | 2011-04-05 17:23:51 +0200 | [diff] [blame] | 7001 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7002 | * To be able to change p->policy safely, the appropriate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | * runqueue lock must be held. |
| 7004 | */ |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 7005 | rq = task_rq_lock(p, &rf); |
Peter Zijlstra | 80f5c1b | 2016-10-03 16:28:37 +0200 | [diff] [blame] | 7006 | update_rq_clock(rq); |
Peter Zijlstra | dc61b1d | 2010-06-08 11:40:42 +0200 | [diff] [blame] | 7007 | |
Peter Zijlstra | 34f971f | 2010-09-22 13:53:15 +0200 | [diff] [blame] | 7008 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7009 | * Changing the policy of the stop threads its a very bad idea: |
Peter Zijlstra | 34f971f | 2010-09-22 13:53:15 +0200 | [diff] [blame] | 7010 | */ |
| 7011 | if (p == rq->stop) { |
Mathieu Poirier | 4b211f2 | 2019-07-19 15:59:54 +0200 | [diff] [blame] | 7012 | retval = -EINVAL; |
| 7013 | goto unlock; |
Peter Zijlstra | 34f971f | 2010-09-22 13:53:15 +0200 | [diff] [blame] | 7014 | } |
| 7015 | |
Dario Faggioli | a51e919 | 2011-03-24 14:00:18 +0100 | [diff] [blame] | 7016 | /* |
Thomas Gleixner | d6b1e91 | 2014-02-07 20:58:40 +0100 | [diff] [blame] | 7017 | * If not changing anything there's no need to proceed further, |
| 7018 | * but store a possible modification of reset_on_fork. |
Dario Faggioli | a51e919 | 2011-03-24 14:00:18 +0100 | [diff] [blame] | 7019 | */ |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7020 | if (unlikely(policy == p->policy)) { |
Dongsheng Yang | d0ea026 | 2014-01-27 22:00:45 -0500 | [diff] [blame] | 7021 | if (fair_policy(policy) && attr->sched_nice != task_nice(p)) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7022 | goto change; |
| 7023 | if (rt_policy(policy) && attr->sched_priority != p->rt_priority) |
| 7024 | goto change; |
Wanpeng Li | 7538160 | 2014-11-26 08:44:04 +0800 | [diff] [blame] | 7025 | if (dl_policy(policy) && dl_param_changed(p, attr)) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 7026 | goto change; |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 7027 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) |
| 7028 | goto change; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7029 | |
Thomas Gleixner | d6b1e91 | 2014-02-07 20:58:40 +0100 | [diff] [blame] | 7030 | p->sched_reset_on_fork = reset_on_fork; |
Mathieu Poirier | 4b211f2 | 2019-07-19 15:59:54 +0200 | [diff] [blame] | 7031 | retval = 0; |
| 7032 | goto unlock; |
Dario Faggioli | a51e919 | 2011-03-24 14:00:18 +0100 | [diff] [blame] | 7033 | } |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7034 | change: |
Dario Faggioli | a51e919 | 2011-03-24 14:00:18 +0100 | [diff] [blame] | 7035 | |
Peter Zijlstra | dc61b1d | 2010-06-08 11:40:42 +0200 | [diff] [blame] | 7036 | if (user) { |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7037 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | dc61b1d | 2010-06-08 11:40:42 +0200 | [diff] [blame] | 7038 | /* |
| 7039 | * Do not allow realtime tasks into groups that have no runtime |
| 7040 | * assigned. |
| 7041 | */ |
| 7042 | if (rt_bandwidth_enabled() && rt_policy(policy) && |
Mike Galbraith | f449377 | 2011-01-13 04:54:50 +0100 | [diff] [blame] | 7043 | task_group(p)->rt_bandwidth.rt_runtime == 0 && |
| 7044 | !task_group_is_autogroup(task_group(p))) { |
Mathieu Poirier | 4b211f2 | 2019-07-19 15:59:54 +0200 | [diff] [blame] | 7045 | retval = -EPERM; |
| 7046 | goto unlock; |
Peter Zijlstra | dc61b1d | 2010-06-08 11:40:42 +0200 | [diff] [blame] | 7047 | } |
Peter Zijlstra | dc61b1d | 2010-06-08 11:40:42 +0200 | [diff] [blame] | 7048 | #endif |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7049 | #ifdef CONFIG_SMP |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 7050 | if (dl_bandwidth_enabled() && dl_policy(policy) && |
| 7051 | !(attr->sched_flags & SCHED_FLAG_SUGOV)) { |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7052 | cpumask_t *span = rq->rd->span; |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7053 | |
| 7054 | /* |
| 7055 | * Don't allow tasks with an affinity mask smaller than |
| 7056 | * the entire root_domain to become SCHED_DEADLINE. We |
| 7057 | * will also fail if there's no bandwidth available. |
| 7058 | */ |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 7059 | if (!cpumask_subset(span, p->cpus_ptr) || |
Peter Zijlstra | e4099a5 | 2013-12-17 10:03:34 +0100 | [diff] [blame] | 7060 | rq->rd->dl_bw.bw == 0) { |
Mathieu Poirier | 4b211f2 | 2019-07-19 15:59:54 +0200 | [diff] [blame] | 7061 | retval = -EPERM; |
| 7062 | goto unlock; |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7063 | } |
| 7064 | } |
| 7065 | #endif |
| 7066 | } |
Peter Zijlstra | dc61b1d | 2010-06-08 11:40:42 +0200 | [diff] [blame] | 7067 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7068 | /* Re-check policy now with rq lock held: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| 7070 | policy = oldpolicy = -1; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 7071 | task_rq_unlock(rq, p, &rf); |
Juri Lelli | 710da3c | 2019-07-19 16:00:00 +0200 | [diff] [blame] | 7072 | if (pi) |
| 7073 | cpuset_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7074 | goto recheck; |
| 7075 | } |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7076 | |
| 7077 | /* |
| 7078 | * If setscheduling to SCHED_DEADLINE (or changing the parameters |
| 7079 | * of a SCHED_DEADLINE task) we need to check if enough bandwidth |
| 7080 | * is available. |
| 7081 | */ |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 7082 | if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) { |
Mathieu Poirier | 4b211f2 | 2019-07-19 15:59:54 +0200 | [diff] [blame] | 7083 | retval = -EBUSY; |
| 7084 | goto unlock; |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7085 | } |
| 7086 | |
Thomas Gleixner | c365c29 | 2014-02-07 20:58:42 +0100 | [diff] [blame] | 7087 | p->sched_reset_on_fork = reset_on_fork; |
| 7088 | oldprio = p->prio; |
| 7089 | |
Peter Zijlstra | dbc7f06 | 2015-06-11 14:46:38 +0200 | [diff] [blame] | 7090 | if (pi) { |
| 7091 | /* |
| 7092 | * Take priority boosted tasks into account. If the new |
| 7093 | * effective priority is unchanged, we just store the new |
| 7094 | * normal parameters and do not touch the scheduler class and |
| 7095 | * the runqueue. This will be done when the task deboost |
| 7096 | * itself. |
| 7097 | */ |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 7098 | new_effective_prio = rt_effective_prio(p, newprio); |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 7099 | if (new_effective_prio == oldprio) |
| 7100 | queue_flags &= ~DEQUEUE_MOVE; |
Thomas Gleixner | c365c29 | 2014-02-07 20:58:42 +0100 | [diff] [blame] | 7101 | } |
| 7102 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 7103 | queued = task_on_rq_queued(p); |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 7104 | running = task_current(rq, p); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 7105 | if (queued) |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 7106 | dequeue_task(rq, p, queue_flags); |
Hiroshi Shimamoto | 0e1f348 | 2008-03-10 11:01:20 -0700 | [diff] [blame] | 7107 | if (running) |
Kirill Tkhai | f3cd1c4 | 2014-09-12 17:41:40 +0400 | [diff] [blame] | 7108 | put_prev_task(rq, p); |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7109 | |
Thomas Gleixner | 83ab0aa | 2010-02-17 09:05:48 +0100 | [diff] [blame] | 7110 | prev_class = p->sched_class; |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 7111 | |
Peter Zijlstra | dbc7f06 | 2015-06-11 14:46:38 +0200 | [diff] [blame] | 7112 | __setscheduler(rq, p, attr, pi); |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 7113 | __setscheduler_uclamp(p, attr); |
Dmitry Adamushko | f6b53205 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7114 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 7115 | if (queued) { |
Thomas Gleixner | 81a44c5 | 2014-02-07 20:58:41 +0100 | [diff] [blame] | 7116 | /* |
| 7117 | * We enqueue to tail when the priority of a task is |
| 7118 | * increased (user space view). |
| 7119 | */ |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 7120 | if (oldprio < p->prio) |
| 7121 | queue_flags |= ENQUEUE_HEAD; |
Peter Zijlstra | 1de6444 | 2015-09-30 17:44:13 +0200 | [diff] [blame] | 7122 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 7123 | enqueue_task(rq, p, queue_flags); |
Thomas Gleixner | 81a44c5 | 2014-02-07 20:58:41 +0100 | [diff] [blame] | 7124 | } |
Vincent Guittot | a399d23 | 2016-09-12 09:47:52 +0200 | [diff] [blame] | 7125 | if (running) |
Peter Zijlstra | 03b7fad | 2019-05-29 20:36:41 +0000 | [diff] [blame] | 7126 | set_next_task(rq, p); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 7127 | |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 7128 | check_class_changed(rq, p, prev_class, oldprio); |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7129 | |
| 7130 | /* Avoid rq from going away on us: */ |
| 7131 | preempt_disable(); |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 7132 | head = splice_balance_callbacks(rq); |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 7133 | task_rq_unlock(rq, p, &rf); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 7134 | |
Juri Lelli | 710da3c | 2019-07-19 16:00:00 +0200 | [diff] [blame] | 7135 | if (pi) { |
| 7136 | cpuset_read_unlock(); |
Peter Zijlstra | dbc7f06 | 2015-06-11 14:46:38 +0200 | [diff] [blame] | 7137 | rt_mutex_adjust_pi(p); |
Juri Lelli | 710da3c | 2019-07-19 16:00:00 +0200 | [diff] [blame] | 7138 | } |
Thomas Gleixner | 95e02ca | 2006-06-27 02:55:02 -0700 | [diff] [blame] | 7139 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7140 | /* Run balance callbacks after we've adjusted the PI chain: */ |
Peter Zijlstra | 565790d2 | 2020-05-11 14:13:00 +0200 | [diff] [blame] | 7141 | balance_callbacks(rq, head); |
Peter Zijlstra | 4c9a4bc | 2015-06-11 14:46:39 +0200 | [diff] [blame] | 7142 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | |
| 7144 | return 0; |
Mathieu Poirier | 4b211f2 | 2019-07-19 15:59:54 +0200 | [diff] [blame] | 7145 | |
| 7146 | unlock: |
| 7147 | task_rq_unlock(rq, p, &rf); |
Juri Lelli | 710da3c | 2019-07-19 16:00:00 +0200 | [diff] [blame] | 7148 | if (pi) |
| 7149 | cpuset_read_unlock(); |
Mathieu Poirier | 4b211f2 | 2019-07-19 15:59:54 +0200 | [diff] [blame] | 7150 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7151 | } |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 7152 | |
Peter Zijlstra | 7479f3c9c | 2014-01-15 17:05:04 +0100 | [diff] [blame] | 7153 | static int _sched_setscheduler(struct task_struct *p, int policy, |
| 7154 | const struct sched_param *param, bool check) |
| 7155 | { |
| 7156 | struct sched_attr attr = { |
| 7157 | .sched_policy = policy, |
| 7158 | .sched_priority = param->sched_priority, |
| 7159 | .sched_nice = PRIO_TO_NICE(p->static_prio), |
| 7160 | }; |
| 7161 | |
Steven Rostedt | c13db6b | 2014-07-23 11:28:26 -0400 | [diff] [blame] | 7162 | /* Fixup the legacy SCHED_RESET_ON_FORK hack. */ |
| 7163 | if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) { |
Peter Zijlstra | 7479f3c9c | 2014-01-15 17:05:04 +0100 | [diff] [blame] | 7164 | attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
| 7165 | policy &= ~SCHED_RESET_ON_FORK; |
| 7166 | attr.sched_policy = policy; |
| 7167 | } |
| 7168 | |
Peter Zijlstra | dbc7f06 | 2015-06-11 14:46:38 +0200 | [diff] [blame] | 7169 | return __sched_setscheduler(p, &attr, check, true); |
Peter Zijlstra | 7479f3c9c | 2014-01-15 17:05:04 +0100 | [diff] [blame] | 7170 | } |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 7171 | /** |
| 7172 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. |
| 7173 | * @p: the task in question. |
| 7174 | * @policy: new policy. |
| 7175 | * @param: structure containing the new RT priority. |
| 7176 | * |
Peter Zijlstra | 7318d4c | 2020-04-21 12:09:13 +0200 | [diff] [blame] | 7177 | * Use sched_set_fifo(), read its comment. |
| 7178 | * |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7179 | * Return: 0 on success. An error code otherwise. |
| 7180 | * |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 7181 | * NOTE that the task may be already dead. |
| 7182 | */ |
| 7183 | int sched_setscheduler(struct task_struct *p, int policy, |
KOSAKI Motohiro | fe7de49 | 2010-10-20 16:01:12 -0700 | [diff] [blame] | 7184 | const struct sched_param *param) |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 7185 | { |
Peter Zijlstra | 7479f3c9c | 2014-01-15 17:05:04 +0100 | [diff] [blame] | 7186 | return _sched_setscheduler(p, policy, param, true); |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 7187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7189 | int sched_setattr(struct task_struct *p, const struct sched_attr *attr) |
| 7190 | { |
Peter Zijlstra | dbc7f06 | 2015-06-11 14:46:38 +0200 | [diff] [blame] | 7191 | return __sched_setscheduler(p, attr, true, true); |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7192 | } |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7193 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 7194 | int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr) |
| 7195 | { |
| 7196 | return __sched_setscheduler(p, attr, false, true); |
| 7197 | } |
Viresh Kumar | 4c38f2d | 2020-06-23 15:49:40 +0530 | [diff] [blame] | 7198 | EXPORT_SYMBOL_GPL(sched_setattr_nocheck); |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 7199 | |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 7200 | /** |
| 7201 | * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace. |
| 7202 | * @p: the task in question. |
| 7203 | * @policy: new policy. |
| 7204 | * @param: structure containing the new RT priority. |
| 7205 | * |
| 7206 | * Just like sched_setscheduler, only don't bother checking if the |
| 7207 | * current context has permission. For example, this is needed in |
| 7208 | * stop_machine(): we create temporary high priority worker threads, |
| 7209 | * but our caller might not have that capability. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7210 | * |
| 7211 | * Return: 0 on success. An error code otherwise. |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 7212 | */ |
| 7213 | int sched_setscheduler_nocheck(struct task_struct *p, int policy, |
KOSAKI Motohiro | fe7de49 | 2010-10-20 16:01:12 -0700 | [diff] [blame] | 7214 | const struct sched_param *param) |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 7215 | { |
Peter Zijlstra | 7479f3c9c | 2014-01-15 17:05:04 +0100 | [diff] [blame] | 7216 | return _sched_setscheduler(p, policy, param, false); |
Rusty Russell | 961ccdd | 2008-06-23 13:55:38 +1000 | [diff] [blame] | 7217 | } |
| 7218 | |
Peter Zijlstra | 7318d4c | 2020-04-21 12:09:13 +0200 | [diff] [blame] | 7219 | /* |
| 7220 | * SCHED_FIFO is a broken scheduler model; that is, it is fundamentally |
| 7221 | * incapable of resource management, which is the one thing an OS really should |
| 7222 | * be doing. |
| 7223 | * |
| 7224 | * This is of course the reason it is limited to privileged users only. |
| 7225 | * |
| 7226 | * Worse still; it is fundamentally impossible to compose static priority |
| 7227 | * workloads. You cannot take two correctly working static prio workloads |
| 7228 | * and smash them together and still expect them to work. |
| 7229 | * |
| 7230 | * For this reason 'all' FIFO tasks the kernel creates are basically at: |
| 7231 | * |
| 7232 | * MAX_RT_PRIO / 2 |
| 7233 | * |
| 7234 | * The administrator _MUST_ configure the system, the kernel simply doesn't |
| 7235 | * know enough information to make a sensible choice. |
| 7236 | */ |
Peter Zijlstra | 8b70098 | 2020-04-22 13:10:04 +0200 | [diff] [blame] | 7237 | void sched_set_fifo(struct task_struct *p) |
Peter Zijlstra | 7318d4c | 2020-04-21 12:09:13 +0200 | [diff] [blame] | 7238 | { |
| 7239 | struct sched_param sp = { .sched_priority = MAX_RT_PRIO / 2 }; |
Peter Zijlstra | 8b70098 | 2020-04-22 13:10:04 +0200 | [diff] [blame] | 7240 | WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0); |
Peter Zijlstra | 7318d4c | 2020-04-21 12:09:13 +0200 | [diff] [blame] | 7241 | } |
| 7242 | EXPORT_SYMBOL_GPL(sched_set_fifo); |
| 7243 | |
| 7244 | /* |
| 7245 | * For when you don't much care about FIFO, but want to be above SCHED_NORMAL. |
| 7246 | */ |
Peter Zijlstra | 8b70098 | 2020-04-22 13:10:04 +0200 | [diff] [blame] | 7247 | void sched_set_fifo_low(struct task_struct *p) |
Peter Zijlstra | 7318d4c | 2020-04-21 12:09:13 +0200 | [diff] [blame] | 7248 | { |
| 7249 | struct sched_param sp = { .sched_priority = 1 }; |
Peter Zijlstra | 8b70098 | 2020-04-22 13:10:04 +0200 | [diff] [blame] | 7250 | WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0); |
Peter Zijlstra | 7318d4c | 2020-04-21 12:09:13 +0200 | [diff] [blame] | 7251 | } |
| 7252 | EXPORT_SYMBOL_GPL(sched_set_fifo_low); |
| 7253 | |
Peter Zijlstra | 8b70098 | 2020-04-22 13:10:04 +0200 | [diff] [blame] | 7254 | void sched_set_normal(struct task_struct *p, int nice) |
Peter Zijlstra | 7318d4c | 2020-04-21 12:09:13 +0200 | [diff] [blame] | 7255 | { |
| 7256 | struct sched_attr attr = { |
| 7257 | .sched_policy = SCHED_NORMAL, |
| 7258 | .sched_nice = nice, |
| 7259 | }; |
Peter Zijlstra | 8b70098 | 2020-04-22 13:10:04 +0200 | [diff] [blame] | 7260 | WARN_ON_ONCE(sched_setattr_nocheck(p, &attr) != 0); |
Peter Zijlstra | 7318d4c | 2020-04-21 12:09:13 +0200 | [diff] [blame] | 7261 | } |
| 7262 | EXPORT_SYMBOL_GPL(sched_set_normal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7263 | |
| 7264 | static int |
| 7265 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) |
| 7266 | { |
| 7267 | struct sched_param lparam; |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 7268 | struct task_struct *p; |
| 7269 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7270 | |
Jason Baron | c21761f | 2006-01-18 17:43:03 -0800 | [diff] [blame] | 7271 | if (!param || pid < 0) |
| 7272 | return -EINVAL; |
| 7273 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| 7274 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7275 | |
| 7276 | rcu_read_lock(); |
| 7277 | retval = -ESRCH; |
| 7278 | p = find_process_by_pid(pid); |
Juri Lelli | 710da3c | 2019-07-19 16:00:00 +0200 | [diff] [blame] | 7279 | if (likely(p)) |
| 7280 | get_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | rcu_read_unlock(); |
| 7282 | |
Juri Lelli | 710da3c | 2019-07-19 16:00:00 +0200 | [diff] [blame] | 7283 | if (likely(p)) { |
| 7284 | retval = sched_setscheduler(p, policy, &lparam); |
| 7285 | put_task_struct(p); |
| 7286 | } |
| 7287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | return retval; |
| 7289 | } |
| 7290 | |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7291 | /* |
| 7292 | * Mimics kernel/events/core.c perf_copy_attr(). |
| 7293 | */ |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7294 | static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7295 | { |
| 7296 | u32 size; |
| 7297 | int ret; |
| 7298 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7299 | /* Zero the full structure, so that a short copy will be nice: */ |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7300 | memset(attr, 0, sizeof(*attr)); |
| 7301 | |
| 7302 | ret = get_user(size, &uattr->size); |
| 7303 | if (ret) |
| 7304 | return ret; |
| 7305 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7306 | /* ABI compatibility quirk: */ |
| 7307 | if (!size) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7308 | size = SCHED_ATTR_SIZE_VER0; |
Aleksa Sarai | dff3a85 | 2019-10-01 11:10:54 +1000 | [diff] [blame] | 7309 | if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7310 | goto err_size; |
| 7311 | |
Aleksa Sarai | dff3a85 | 2019-10-01 11:10:54 +1000 | [diff] [blame] | 7312 | ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size); |
| 7313 | if (ret) { |
| 7314 | if (ret == -E2BIG) |
| 7315 | goto err_size; |
| 7316 | return ret; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7317 | } |
| 7318 | |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 7319 | if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) && |
| 7320 | size < SCHED_ATTR_SIZE_VER1) |
| 7321 | return -EINVAL; |
| 7322 | |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7323 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7324 | * XXX: Do we want to be lenient like existing syscalls; or do we want |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7325 | * to be strict and return an error on out-of-bounds values? |
| 7326 | */ |
Dongsheng Yang | 75e45d5 | 2014-02-11 15:34:50 +0800 | [diff] [blame] | 7327 | attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE); |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7328 | |
Michael Kerrisk | e78c7bc | 2014-05-09 16:54:28 +0200 | [diff] [blame] | 7329 | return 0; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7330 | |
| 7331 | err_size: |
| 7332 | put_user(sizeof(*attr), &uattr->size); |
Michael Kerrisk | e78c7bc | 2014-05-09 16:54:28 +0200 | [diff] [blame] | 7333 | return -E2BIG; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7334 | } |
| 7335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7336 | /** |
| 7337 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| 7338 | * @pid: the pid in question. |
| 7339 | * @policy: new policy. |
| 7340 | * @param: structure containing the new RT priority. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7341 | * |
| 7342 | * Return: 0 on success. An error code otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | */ |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7344 | SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7345 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7346 | if (policy < 0) |
| 7347 | return -EINVAL; |
| 7348 | |
| 7349 | return do_sched_setscheduler(pid, policy, param); |
| 7350 | } |
| 7351 | |
| 7352 | /** |
| 7353 | * sys_sched_setparam - set/change the RT priority of a thread |
| 7354 | * @pid: the pid in question. |
| 7355 | * @param: structure containing the new RT priority. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7356 | * |
| 7357 | * Return: 0 on success. An error code otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | */ |
Heiko Carstens | 5add95d | 2009-01-14 14:14:08 +0100 | [diff] [blame] | 7359 | SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7360 | { |
Steven Rostedt | c13db6b | 2014-07-23 11:28:26 -0400 | [diff] [blame] | 7361 | return do_sched_setscheduler(pid, SETPARAM_POLICY, param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | } |
| 7363 | |
| 7364 | /** |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7365 | * sys_sched_setattr - same as above, but with extended sched_attr |
| 7366 | * @pid: the pid in question. |
Juri Lelli | 5778fcc | 2014-01-14 16:10:39 +0100 | [diff] [blame] | 7367 | * @uattr: structure containing the extended parameters. |
Masanari Iida | db66d75 | 2014-04-18 01:59:15 +0900 | [diff] [blame] | 7368 | * @flags: for future extension. |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7369 | */ |
Peter Zijlstra | 6d35ab4 | 2014-02-14 17:19:29 +0100 | [diff] [blame] | 7370 | SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr, |
| 7371 | unsigned int, flags) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7372 | { |
| 7373 | struct sched_attr attr; |
| 7374 | struct task_struct *p; |
| 7375 | int retval; |
| 7376 | |
Peter Zijlstra | 6d35ab4 | 2014-02-14 17:19:29 +0100 | [diff] [blame] | 7377 | if (!uattr || pid < 0 || flags) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7378 | return -EINVAL; |
| 7379 | |
Michael Kerrisk | 143cf23 | 2014-05-09 16:54:15 +0200 | [diff] [blame] | 7380 | retval = sched_copy_attr(uattr, &attr); |
| 7381 | if (retval) |
| 7382 | return retval; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7383 | |
Richard Weinberger | b14ed2c | 2014-06-02 22:38:34 +0200 | [diff] [blame] | 7384 | if ((int)attr.sched_policy < 0) |
Peter Zijlstra | dbdb227 | 2014-05-09 10:49:03 +0200 | [diff] [blame] | 7385 | return -EINVAL; |
Patrick Bellasi | 1d6362f | 2019-06-21 09:42:06 +0100 | [diff] [blame] | 7386 | if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY) |
| 7387 | attr.sched_policy = SETPARAM_POLICY; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7388 | |
| 7389 | rcu_read_lock(); |
| 7390 | retval = -ESRCH; |
| 7391 | p = find_process_by_pid(pid); |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 7392 | if (likely(p)) |
| 7393 | get_task_struct(p); |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7394 | rcu_read_unlock(); |
| 7395 | |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 7396 | if (likely(p)) { |
| 7397 | retval = sched_setattr(p, &attr); |
| 7398 | put_task_struct(p); |
| 7399 | } |
| 7400 | |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7401 | return retval; |
| 7402 | } |
| 7403 | |
| 7404 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7405 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| 7406 | * @pid: the pid in question. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7407 | * |
| 7408 | * Return: On success, the policy of the thread. Otherwise, a negative error |
| 7409 | * code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | */ |
Heiko Carstens | 5add95d | 2009-01-14 14:14:08 +0100 | [diff] [blame] | 7411 | SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7412 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7413 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7414 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7415 | |
| 7416 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7417 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7418 | |
| 7419 | retval = -ESRCH; |
Thomas Gleixner | 5fe85be | 2009-12-09 10:14:58 +0000 | [diff] [blame] | 7420 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | p = find_process_by_pid(pid); |
| 7422 | if (p) { |
| 7423 | retval = security_task_getscheduler(p); |
| 7424 | if (!retval) |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 7425 | retval = p->policy |
| 7426 | | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7427 | } |
Thomas Gleixner | 5fe85be | 2009-12-09 10:14:58 +0000 | [diff] [blame] | 7428 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7429 | return retval; |
| 7430 | } |
| 7431 | |
| 7432 | /** |
Lennart Poettering | ca94c44 | 2009-06-15 17:17:47 +0200 | [diff] [blame] | 7433 | * sys_sched_getparam - get the RT priority of a thread |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7434 | * @pid: the pid in question. |
| 7435 | * @param: structure containing the RT priority. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7436 | * |
| 7437 | * Return: On success, 0 and the RT priority is in @param. Otherwise, an error |
| 7438 | * code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7439 | */ |
Heiko Carstens | 5add95d | 2009-01-14 14:14:08 +0100 | [diff] [blame] | 7440 | SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7441 | { |
Peter Zijlstra | ce5f7f8 | 2014-05-12 22:50:34 +0200 | [diff] [blame] | 7442 | struct sched_param lp = { .sched_priority = 0 }; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7443 | struct task_struct *p; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7444 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7445 | |
| 7446 | if (!param || pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 7447 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7448 | |
Thomas Gleixner | 5fe85be | 2009-12-09 10:14:58 +0000 | [diff] [blame] | 7449 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7450 | p = find_process_by_pid(pid); |
| 7451 | retval = -ESRCH; |
| 7452 | if (!p) |
| 7453 | goto out_unlock; |
| 7454 | |
| 7455 | retval = security_task_getscheduler(p); |
| 7456 | if (retval) |
| 7457 | goto out_unlock; |
| 7458 | |
Peter Zijlstra | ce5f7f8 | 2014-05-12 22:50:34 +0200 | [diff] [blame] | 7459 | if (task_has_rt_policy(p)) |
| 7460 | lp.sched_priority = p->rt_priority; |
Thomas Gleixner | 5fe85be | 2009-12-09 10:14:58 +0000 | [diff] [blame] | 7461 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7462 | |
| 7463 | /* |
| 7464 | * This one might sleep, we cannot do it with a spinlock held ... |
| 7465 | */ |
| 7466 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| 7467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7468 | return retval; |
| 7469 | |
| 7470 | out_unlock: |
Thomas Gleixner | 5fe85be | 2009-12-09 10:14:58 +0000 | [diff] [blame] | 7471 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | return retval; |
| 7473 | } |
| 7474 | |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7475 | /* |
| 7476 | * Copy the kernel size attribute structure (which might be larger |
| 7477 | * than what user-space knows about) to user-space. |
| 7478 | * |
| 7479 | * Note that all cases are valid: user-space buffer can be larger or |
| 7480 | * smaller than the kernel-space buffer. The usual case is that both |
| 7481 | * have the same size. |
| 7482 | */ |
| 7483 | static int |
| 7484 | sched_attr_copy_to_user(struct sched_attr __user *uattr, |
| 7485 | struct sched_attr *kattr, |
| 7486 | unsigned int usize) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7487 | { |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7488 | unsigned int ksize = sizeof(*kattr); |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7489 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 7490 | if (!access_ok(uattr, usize)) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7491 | return -EFAULT; |
| 7492 | |
| 7493 | /* |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7494 | * sched_getattr() ABI forwards and backwards compatibility: |
| 7495 | * |
| 7496 | * If usize == ksize then we just copy everything to user-space and all is good. |
| 7497 | * |
| 7498 | * If usize < ksize then we only copy as much as user-space has space for, |
| 7499 | * this keeps ABI compatibility as well. We skip the rest. |
| 7500 | * |
| 7501 | * If usize > ksize then user-space is using a newer version of the ABI, |
| 7502 | * which part the kernel doesn't know about. Just ignore it - tooling can |
| 7503 | * detect the kernel's knowledge of attributes from the attr->size value |
| 7504 | * which is set to ksize in this case. |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7505 | */ |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7506 | kattr->size = min(usize, ksize); |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7507 | |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7508 | if (copy_to_user(uattr, kattr, kattr->size)) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7509 | return -EFAULT; |
| 7510 | |
Michael Kerrisk | 2240067 | 2014-05-09 16:54:33 +0200 | [diff] [blame] | 7511 | return 0; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7512 | } |
| 7513 | |
| 7514 | /** |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 7515 | * sys_sched_getattr - similar to sched_getparam, but with sched_attr |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7516 | * @pid: the pid in question. |
Juri Lelli | 5778fcc | 2014-01-14 16:10:39 +0100 | [diff] [blame] | 7517 | * @uattr: structure containing the extended parameters. |
Aleksa Sarai | dff3a85 | 2019-10-01 11:10:54 +1000 | [diff] [blame] | 7518 | * @usize: sizeof(attr) for fwd/bwd comp. |
Masanari Iida | db66d75 | 2014-04-18 01:59:15 +0900 | [diff] [blame] | 7519 | * @flags: for future extension. |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7520 | */ |
Peter Zijlstra | 6d35ab4 | 2014-02-14 17:19:29 +0100 | [diff] [blame] | 7521 | SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr, |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7522 | unsigned int, usize, unsigned int, flags) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7523 | { |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7524 | struct sched_attr kattr = { }; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7525 | struct task_struct *p; |
| 7526 | int retval; |
| 7527 | |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7528 | if (!uattr || pid < 0 || usize > PAGE_SIZE || |
| 7529 | usize < SCHED_ATTR_SIZE_VER0 || flags) |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7530 | return -EINVAL; |
| 7531 | |
| 7532 | rcu_read_lock(); |
| 7533 | p = find_process_by_pid(pid); |
| 7534 | retval = -ESRCH; |
| 7535 | if (!p) |
| 7536 | goto out_unlock; |
| 7537 | |
| 7538 | retval = security_task_getscheduler(p); |
| 7539 | if (retval) |
| 7540 | goto out_unlock; |
| 7541 | |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7542 | kattr.sched_policy = p->policy; |
Peter Zijlstra | 7479f3c9c | 2014-01-15 17:05:04 +0100 | [diff] [blame] | 7543 | if (p->sched_reset_on_fork) |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7544 | kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 7545 | if (task_has_dl_policy(p)) |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7546 | __getparam_dl(p, &kattr); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 7547 | else if (task_has_rt_policy(p)) |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7548 | kattr.sched_priority = p->rt_priority; |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7549 | else |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7550 | kattr.sched_nice = task_nice(p); |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7551 | |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 7552 | #ifdef CONFIG_UCLAMP_TASK |
Qais Yousef | 13685c4 | 2020-07-16 12:03:45 +0100 | [diff] [blame] | 7553 | /* |
| 7554 | * This could race with another potential updater, but this is fine |
| 7555 | * because it'll correctly read the old or the new value. We don't need |
| 7556 | * to guarantee who wins the race as long as it doesn't return garbage. |
| 7557 | */ |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7558 | kattr.sched_util_min = p->uclamp_req[UCLAMP_MIN].value; |
| 7559 | kattr.sched_util_max = p->uclamp_req[UCLAMP_MAX].value; |
Patrick Bellasi | a509a7c | 2019-06-21 09:42:07 +0100 | [diff] [blame] | 7560 | #endif |
| 7561 | |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7562 | rcu_read_unlock(); |
| 7563 | |
Ingo Molnar | 1251201 | 2019-09-04 09:55:32 +0200 | [diff] [blame] | 7564 | return sched_attr_copy_to_user(uattr, &kattr, usize); |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 7565 | |
| 7566 | out_unlock: |
| 7567 | rcu_read_unlock(); |
| 7568 | return retval; |
| 7569 | } |
| 7570 | |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 7571 | long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7572 | { |
Rusty Russell | 5a16f3d | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7573 | cpumask_var_t cpus_allowed, new_mask; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7574 | struct task_struct *p; |
| 7575 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7576 | |
Thomas Gleixner | 23f5d14 | 2009-12-09 10:15:01 +0000 | [diff] [blame] | 7577 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7578 | |
| 7579 | p = find_process_by_pid(pid); |
| 7580 | if (!p) { |
Thomas Gleixner | 23f5d14 | 2009-12-09 10:15:01 +0000 | [diff] [blame] | 7581 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7582 | return -ESRCH; |
| 7583 | } |
| 7584 | |
Thomas Gleixner | 23f5d14 | 2009-12-09 10:15:01 +0000 | [diff] [blame] | 7585 | /* Prevent p going away */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7586 | get_task_struct(p); |
Thomas Gleixner | 23f5d14 | 2009-12-09 10:15:01 +0000 | [diff] [blame] | 7587 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7588 | |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 7589 | if (p->flags & PF_NO_SETAFFINITY) { |
| 7590 | retval = -EINVAL; |
| 7591 | goto out_put_task; |
| 7592 | } |
Rusty Russell | 5a16f3d | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7593 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { |
| 7594 | retval = -ENOMEM; |
| 7595 | goto out_put_task; |
| 7596 | } |
| 7597 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { |
| 7598 | retval = -ENOMEM; |
| 7599 | goto out_free_cpus_allowed; |
| 7600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7601 | retval = -EPERM; |
Eric W. Biederman | 4c44aaa | 2012-07-26 05:05:21 -0700 | [diff] [blame] | 7602 | if (!check_same_owner(p)) { |
| 7603 | rcu_read_lock(); |
| 7604 | if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { |
| 7605 | rcu_read_unlock(); |
Kirill Tkhai | 16303ab | 2014-09-22 22:36:30 +0400 | [diff] [blame] | 7606 | goto out_free_new_mask; |
Eric W. Biederman | 4c44aaa | 2012-07-26 05:05:21 -0700 | [diff] [blame] | 7607 | } |
| 7608 | rcu_read_unlock(); |
| 7609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7610 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 7611 | retval = security_task_setscheduler(p); |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 7612 | if (retval) |
Kirill Tkhai | 16303ab | 2014-09-22 22:36:30 +0400 | [diff] [blame] | 7613 | goto out_free_new_mask; |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 7614 | |
Peter Zijlstra | e4099a5 | 2013-12-17 10:03:34 +0100 | [diff] [blame] | 7615 | |
| 7616 | cpuset_cpus_allowed(p, cpus_allowed); |
| 7617 | cpumask_and(new_mask, in_mask, cpus_allowed); |
| 7618 | |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7619 | /* |
| 7620 | * Since bandwidth control happens on root_domain basis, |
| 7621 | * if admission test is enabled, we only admit -deadline |
| 7622 | * tasks allowed to run on all the CPUs in the task's |
| 7623 | * root_domain. |
| 7624 | */ |
| 7625 | #ifdef CONFIG_SMP |
Kirill Tkhai | f1e3a09 | 2014-09-22 22:36:36 +0400 | [diff] [blame] | 7626 | if (task_has_dl_policy(p) && dl_bandwidth_enabled()) { |
| 7627 | rcu_read_lock(); |
| 7628 | if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) { |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7629 | retval = -EBUSY; |
Kirill Tkhai | f1e3a09 | 2014-09-22 22:36:36 +0400 | [diff] [blame] | 7630 | rcu_read_unlock(); |
Kirill Tkhai | 16303ab | 2014-09-22 22:36:30 +0400 | [diff] [blame] | 7631 | goto out_free_new_mask; |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7632 | } |
Kirill Tkhai | f1e3a09 | 2014-09-22 22:36:36 +0400 | [diff] [blame] | 7633 | rcu_read_unlock(); |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 7634 | } |
| 7635 | #endif |
Peter Zijlstra | 4924627 | 2010-10-17 21:46:10 +0200 | [diff] [blame] | 7636 | again: |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 7637 | retval = __set_cpus_allowed_ptr(p, new_mask, SCA_CHECK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7638 | |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 7639 | if (!retval) { |
Rusty Russell | 5a16f3d | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7640 | cpuset_cpus_allowed(p, cpus_allowed); |
| 7641 | if (!cpumask_subset(new_mask, cpus_allowed)) { |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 7642 | /* |
| 7643 | * We must have raced with a concurrent cpuset |
| 7644 | * update. Just reset the cpus_allowed to the |
| 7645 | * cpuset's cpus_allowed |
| 7646 | */ |
Rusty Russell | 5a16f3d | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7647 | cpumask_copy(new_mask, cpus_allowed); |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 7648 | goto again; |
| 7649 | } |
| 7650 | } |
Kirill Tkhai | 16303ab | 2014-09-22 22:36:30 +0400 | [diff] [blame] | 7651 | out_free_new_mask: |
Rusty Russell | 5a16f3d | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7652 | free_cpumask_var(new_mask); |
| 7653 | out_free_cpus_allowed: |
| 7654 | free_cpumask_var(cpus_allowed); |
| 7655 | out_put_task: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7656 | put_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7657 | return retval; |
| 7658 | } |
| 7659 | |
| 7660 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 7661 | struct cpumask *new_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7662 | { |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 7663 | if (len < cpumask_size()) |
| 7664 | cpumask_clear(new_mask); |
| 7665 | else if (len > cpumask_size()) |
| 7666 | len = cpumask_size(); |
| 7667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7668 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| 7669 | } |
| 7670 | |
| 7671 | /** |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7672 | * sys_sched_setaffinity - set the CPU affinity of a process |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7673 | * @pid: pid of the process |
| 7674 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7675 | * @user_mask_ptr: user-space pointer to the new CPU mask |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7676 | * |
| 7677 | * Return: 0 on success. An error code otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7678 | */ |
Heiko Carstens | 5add95d | 2009-01-14 14:14:08 +0100 | [diff] [blame] | 7679 | SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, |
| 7680 | unsigned long __user *, user_mask_ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7681 | { |
Rusty Russell | 5a16f3d | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7682 | cpumask_var_t new_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7683 | int retval; |
| 7684 | |
Rusty Russell | 5a16f3d | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7685 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
| 7686 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7687 | |
Rusty Russell | 5a16f3d | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7688 | retval = get_user_cpu_mask(user_mask_ptr, len, new_mask); |
| 7689 | if (retval == 0) |
| 7690 | retval = sched_setaffinity(pid, new_mask); |
| 7691 | free_cpumask_var(new_mask); |
| 7692 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7693 | } |
| 7694 | |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 7695 | long sched_getaffinity(pid_t pid, struct cpumask *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7696 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 7697 | struct task_struct *p; |
Thomas Gleixner | 3160568 | 2009-12-08 20:24:16 +0000 | [diff] [blame] | 7698 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7699 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7700 | |
Thomas Gleixner | 23f5d14 | 2009-12-09 10:15:01 +0000 | [diff] [blame] | 7701 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7702 | |
| 7703 | retval = -ESRCH; |
| 7704 | p = find_process_by_pid(pid); |
| 7705 | if (!p) |
| 7706 | goto out_unlock; |
| 7707 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 7708 | retval = security_task_getscheduler(p); |
| 7709 | if (retval) |
| 7710 | goto out_unlock; |
| 7711 | |
Peter Zijlstra | 013fdb8 | 2011-04-05 17:23:45 +0200 | [diff] [blame] | 7712 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 7713 | cpumask_and(mask, &p->cpus_mask, cpu_active_mask); |
Peter Zijlstra | 013fdb8 | 2011-04-05 17:23:45 +0200 | [diff] [blame] | 7714 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7715 | |
| 7716 | out_unlock: |
Thomas Gleixner | 23f5d14 | 2009-12-09 10:15:01 +0000 | [diff] [blame] | 7717 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7718 | |
Ulrich Drepper | 9531b62 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 7719 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7720 | } |
| 7721 | |
| 7722 | /** |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7723 | * sys_sched_getaffinity - get the CPU affinity of a process |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7724 | * @pid: pid of the process |
| 7725 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7726 | * @user_mask_ptr: user-space pointer to hold the current CPU mask |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7727 | * |
Zev Weiss | 599b484 | 2016-06-26 16:13:23 -0500 | [diff] [blame] | 7728 | * Return: size of CPU mask copied to user_mask_ptr on success. An |
| 7729 | * error code otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7730 | */ |
Heiko Carstens | 5add95d | 2009-01-14 14:14:08 +0100 | [diff] [blame] | 7731 | SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len, |
| 7732 | unsigned long __user *, user_mask_ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7733 | { |
| 7734 | int ret; |
Rusty Russell | f17c860 | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7735 | cpumask_var_t mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7736 | |
Anton Blanchard | 84fba5e | 2010-04-06 17:02:19 +1000 | [diff] [blame] | 7737 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) |
KOSAKI Motohiro | cd3d803 | 2010-03-12 16:15:36 +0900 | [diff] [blame] | 7738 | return -EINVAL; |
| 7739 | if (len & (sizeof(unsigned long)-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7740 | return -EINVAL; |
| 7741 | |
Rusty Russell | f17c860 | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7742 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
| 7743 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7744 | |
Rusty Russell | f17c860 | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7745 | ret = sched_getaffinity(pid, mask); |
| 7746 | if (ret == 0) { |
Alexey Dobriyan | 4de373a | 2018-02-06 15:39:37 -0800 | [diff] [blame] | 7747 | unsigned int retlen = min(len, cpumask_size()); |
KOSAKI Motohiro | cd3d803 | 2010-03-12 16:15:36 +0900 | [diff] [blame] | 7748 | |
| 7749 | if (copy_to_user(user_mask_ptr, mask, retlen)) |
Rusty Russell | f17c860 | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7750 | ret = -EFAULT; |
| 7751 | else |
KOSAKI Motohiro | cd3d803 | 2010-03-12 16:15:36 +0900 | [diff] [blame] | 7752 | ret = retlen; |
Rusty Russell | f17c860 | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7753 | } |
| 7754 | free_cpumask_var(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7755 | |
Rusty Russell | f17c860 | 2008-11-25 02:35:11 +1030 | [diff] [blame] | 7756 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7757 | } |
| 7758 | |
Dominik Brodowski | 7d4dd4f | 2018-03-14 22:40:35 +0100 | [diff] [blame] | 7759 | static void do_sched_yield(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7760 | { |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 7761 | struct rq_flags rf; |
| 7762 | struct rq *rq; |
| 7763 | |
Johannes Weiner | 246b3b3 | 2018-10-26 15:06:23 -0700 | [diff] [blame] | 7764 | rq = this_rq_lock_irq(&rf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7765 | |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 7766 | schedstat_inc(rq->yld_count); |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7767 | current->sched_class->yield_task(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7768 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 7769 | preempt_disable(); |
Thomas Gleixner | 345a957 | 2020-10-20 16:46:55 +0200 | [diff] [blame] | 7770 | rq_unlock_irq(rq, &rf); |
Thomas Gleixner | ba74c14 | 2011-03-21 13:32:17 +0100 | [diff] [blame] | 7771 | sched_preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7772 | |
| 7773 | schedule(); |
Dominik Brodowski | 7d4dd4f | 2018-03-14 22:40:35 +0100 | [diff] [blame] | 7774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7775 | |
Mauro Carvalho Chehab | 59a74b1 | 2020-12-01 13:09:06 +0100 | [diff] [blame] | 7776 | /** |
| 7777 | * sys_sched_yield - yield the current processor to other threads. |
| 7778 | * |
| 7779 | * This function yields the current CPU to other tasks. If there are no |
| 7780 | * other threads running on this CPU then this function will return. |
| 7781 | * |
| 7782 | * Return: 0. |
| 7783 | */ |
Dominik Brodowski | 7d4dd4f | 2018-03-14 22:40:35 +0100 | [diff] [blame] | 7784 | SYSCALL_DEFINE0(sched_yield) |
| 7785 | { |
| 7786 | do_sched_yield(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7787 | return 0; |
| 7788 | } |
| 7789 | |
Peter Zijlstra (Intel) | b965f1d | 2021-01-18 15:12:20 +0100 | [diff] [blame] | 7790 | #if !defined(CONFIG_PREEMPTION) || defined(CONFIG_PREEMPT_DYNAMIC) |
| 7791 | int __sched __cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7792 | { |
Konstantin Khlebnikov | fe32d3c | 2015-07-15 12:52:04 +0300 | [diff] [blame] | 7793 | if (should_resched(0)) { |
Frederic Weisbecker | a18b5d0 | 2015-01-22 18:08:04 +0100 | [diff] [blame] | 7794 | preempt_schedule_common(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7795 | return 1; |
| 7796 | } |
Peter Zijlstra (Intel) | b965f1d | 2021-01-18 15:12:20 +0100 | [diff] [blame] | 7797 | #ifndef CONFIG_PREEMPT_RCU |
Paul E. McKenney | f79c3ad | 2016-11-30 06:24:30 -0800 | [diff] [blame] | 7798 | rcu_all_qs(); |
Peter Zijlstra (Intel) | b965f1d | 2021-01-18 15:12:20 +0100 | [diff] [blame] | 7799 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7800 | return 0; |
| 7801 | } |
Peter Zijlstra (Intel) | b965f1d | 2021-01-18 15:12:20 +0100 | [diff] [blame] | 7802 | EXPORT_SYMBOL(__cond_resched); |
| 7803 | #endif |
| 7804 | |
| 7805 | #ifdef CONFIG_PREEMPT_DYNAMIC |
| 7806 | DEFINE_STATIC_CALL_RET0(cond_resched, __cond_resched); |
Peter Zijlstra | ef72661 | 2021-01-25 16:26:50 +0100 | [diff] [blame] | 7807 | EXPORT_STATIC_CALL_TRAMP(cond_resched); |
Peter Zijlstra (Intel) | b965f1d | 2021-01-18 15:12:20 +0100 | [diff] [blame] | 7808 | |
| 7809 | DEFINE_STATIC_CALL_RET0(might_resched, __cond_resched); |
Peter Zijlstra | ef72661 | 2021-01-25 16:26:50 +0100 | [diff] [blame] | 7810 | EXPORT_STATIC_CALL_TRAMP(might_resched); |
Peter Zijlstra | 35a773a | 2016-09-19 12:57:53 +0200 | [diff] [blame] | 7811 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7812 | |
| 7813 | /* |
Frederic Weisbecker | 613afbf | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 7814 | * __cond_resched_lock() - if a reschedule is pending, drop the given lock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7815 | * call schedule, and on return reacquire the lock. |
| 7816 | * |
Thomas Gleixner | c1a280b | 2019-07-26 23:19:37 +0200 | [diff] [blame] | 7817 | * This works OK both with and without CONFIG_PREEMPTION. We do strange low-level |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7818 | * operations here to prevent schedule() from being called twice (once via |
| 7819 | * spin_unlock(), once by hand). |
| 7820 | */ |
Frederic Weisbecker | 613afbf | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 7821 | int __cond_resched_lock(spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7822 | { |
Konstantin Khlebnikov | fe32d3c | 2015-07-15 12:52:04 +0300 | [diff] [blame] | 7823 | int resched = should_resched(PREEMPT_LOCK_OFFSET); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 7824 | int ret = 0; |
| 7825 | |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 7826 | lockdep_assert_held(lock); |
| 7827 | |
Paul E. McKenney | 4a81e83 | 2014-06-20 16:49:01 -0700 | [diff] [blame] | 7828 | if (spin_needbreak(lock) || resched) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7829 | spin_unlock(lock); |
Peter Zijlstra | d86ee48 | 2009-07-10 14:57:57 +0200 | [diff] [blame] | 7830 | if (resched) |
Frederic Weisbecker | a18b5d0 | 2015-01-22 18:08:04 +0100 | [diff] [blame] | 7831 | preempt_schedule_common(); |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 7832 | else |
| 7833 | cpu_relax(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 7834 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7835 | spin_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7836 | } |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 7837 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7838 | } |
Frederic Weisbecker | 613afbf | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 7839 | EXPORT_SYMBOL(__cond_resched_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7840 | |
Ben Gardon | f3d4b4b | 2021-02-02 10:57:14 -0800 | [diff] [blame] | 7841 | int __cond_resched_rwlock_read(rwlock_t *lock) |
| 7842 | { |
| 7843 | int resched = should_resched(PREEMPT_LOCK_OFFSET); |
| 7844 | int ret = 0; |
| 7845 | |
| 7846 | lockdep_assert_held_read(lock); |
| 7847 | |
| 7848 | if (rwlock_needbreak(lock) || resched) { |
| 7849 | read_unlock(lock); |
| 7850 | if (resched) |
| 7851 | preempt_schedule_common(); |
| 7852 | else |
| 7853 | cpu_relax(); |
| 7854 | ret = 1; |
| 7855 | read_lock(lock); |
| 7856 | } |
| 7857 | return ret; |
| 7858 | } |
| 7859 | EXPORT_SYMBOL(__cond_resched_rwlock_read); |
| 7860 | |
| 7861 | int __cond_resched_rwlock_write(rwlock_t *lock) |
| 7862 | { |
| 7863 | int resched = should_resched(PREEMPT_LOCK_OFFSET); |
| 7864 | int ret = 0; |
| 7865 | |
| 7866 | lockdep_assert_held_write(lock); |
| 7867 | |
| 7868 | if (rwlock_needbreak(lock) || resched) { |
| 7869 | write_unlock(lock); |
| 7870 | if (resched) |
| 7871 | preempt_schedule_common(); |
| 7872 | else |
| 7873 | cpu_relax(); |
| 7874 | ret = 1; |
| 7875 | write_lock(lock); |
| 7876 | } |
| 7877 | return ret; |
| 7878 | } |
| 7879 | EXPORT_SYMBOL(__cond_resched_rwlock_write); |
| 7880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7881 | /** |
| 7882 | * yield - yield the current processor to other threads. |
| 7883 | * |
Peter Zijlstra | 8e3fabf | 2012-03-06 18:54:26 +0100 | [diff] [blame] | 7884 | * Do not ever use this function, there's a 99% chance you're doing it wrong. |
| 7885 | * |
| 7886 | * The scheduler is at all times free to pick the calling task as the most |
| 7887 | * eligible task to run, if removing the yield() call from your code breaks |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 7888 | * it, it's already broken. |
Peter Zijlstra | 8e3fabf | 2012-03-06 18:54:26 +0100 | [diff] [blame] | 7889 | * |
| 7890 | * Typical broken usage is: |
| 7891 | * |
| 7892 | * while (!event) |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 7893 | * yield(); |
Peter Zijlstra | 8e3fabf | 2012-03-06 18:54:26 +0100 | [diff] [blame] | 7894 | * |
| 7895 | * where one assumes that yield() will let 'the other' process run that will |
| 7896 | * make event true. If the current task is a SCHED_FIFO task that will never |
| 7897 | * happen. Never use yield() as a progress guarantee!! |
| 7898 | * |
| 7899 | * If you want to use yield() to wait for something, use wait_event(). |
| 7900 | * If you want to use yield() to be 'nice' for others, use cond_resched(). |
| 7901 | * If you still want to use yield(), do not! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7902 | */ |
| 7903 | void __sched yield(void) |
| 7904 | { |
| 7905 | set_current_state(TASK_RUNNING); |
Dominik Brodowski | 7d4dd4f | 2018-03-14 22:40:35 +0100 | [diff] [blame] | 7906 | do_sched_yield(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7907 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7908 | EXPORT_SYMBOL(yield); |
| 7909 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7910 | /** |
| 7911 | * yield_to - yield the current processor to another thread in |
| 7912 | * your thread group, or accelerate that thread toward the |
| 7913 | * processor it's on. |
Randy Dunlap | 16addf9 | 2011-03-18 09:34:53 -0700 | [diff] [blame] | 7914 | * @p: target task |
| 7915 | * @preempt: whether task preemption is allowed or not |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7916 | * |
| 7917 | * It's the caller's job to ensure that the target task struct |
| 7918 | * can't go away on us before we can do any checks. |
| 7919 | * |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7920 | * Return: |
Peter Zijlstra | 7b270f6 | 2013-01-22 13:09:13 +0530 | [diff] [blame] | 7921 | * true (>0) if we indeed boosted the target task. |
| 7922 | * false (0) if we failed to boost the target. |
| 7923 | * -ESRCH if there's no task to yield to. |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7924 | */ |
Dan Carpenter | fa93384 | 2014-05-23 13:20:42 +0300 | [diff] [blame] | 7925 | int __sched yield_to(struct task_struct *p, bool preempt) |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7926 | { |
| 7927 | struct task_struct *curr = current; |
| 7928 | struct rq *rq, *p_rq; |
| 7929 | unsigned long flags; |
Dan Carpenter | c3c1864 | 2013-02-05 14:37:51 +0300 | [diff] [blame] | 7930 | int yielded = 0; |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7931 | |
| 7932 | local_irq_save(flags); |
| 7933 | rq = this_rq(); |
| 7934 | |
| 7935 | again: |
| 7936 | p_rq = task_rq(p); |
Peter Zijlstra | 7b270f6 | 2013-01-22 13:09:13 +0530 | [diff] [blame] | 7937 | /* |
| 7938 | * If we're the only runnable task on the rq and target rq also |
| 7939 | * has only one task, there's absolutely no point in yielding. |
| 7940 | */ |
| 7941 | if (rq->nr_running == 1 && p_rq->nr_running == 1) { |
| 7942 | yielded = -ESRCH; |
| 7943 | goto out_irq; |
| 7944 | } |
| 7945 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7946 | double_rq_lock(rq, p_rq); |
Shigeru Yoshida | 39e24d8f | 2013-11-23 18:38:01 +0900 | [diff] [blame] | 7947 | if (task_rq(p) != p_rq) { |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7948 | double_rq_unlock(rq, p_rq); |
| 7949 | goto again; |
| 7950 | } |
| 7951 | |
| 7952 | if (!curr->sched_class->yield_to_task) |
Peter Zijlstra | 7b270f6 | 2013-01-22 13:09:13 +0530 | [diff] [blame] | 7953 | goto out_unlock; |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7954 | |
| 7955 | if (curr->sched_class != p->sched_class) |
Peter Zijlstra | 7b270f6 | 2013-01-22 13:09:13 +0530 | [diff] [blame] | 7956 | goto out_unlock; |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7957 | |
| 7958 | if (task_running(p_rq, p) || p->state) |
Peter Zijlstra | 7b270f6 | 2013-01-22 13:09:13 +0530 | [diff] [blame] | 7959 | goto out_unlock; |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7960 | |
Dietmar Eggemann | 0900acf | 2020-06-03 10:03:02 +0200 | [diff] [blame] | 7961 | yielded = curr->sched_class->yield_to_task(rq, p); |
Venkatesh Pallipadi | 6d1cafd | 2011-03-01 16:28:21 -0800 | [diff] [blame] | 7962 | if (yielded) { |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 7963 | schedstat_inc(rq->yld_count); |
Venkatesh Pallipadi | 6d1cafd | 2011-03-01 16:28:21 -0800 | [diff] [blame] | 7964 | /* |
| 7965 | * Make p's CPU reschedule; pick_next_entity takes care of |
| 7966 | * fairness. |
| 7967 | */ |
| 7968 | if (preempt && rq != p_rq) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 7969 | resched_curr(p_rq); |
Venkatesh Pallipadi | 6d1cafd | 2011-03-01 16:28:21 -0800 | [diff] [blame] | 7970 | } |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7971 | |
Peter Zijlstra | 7b270f6 | 2013-01-22 13:09:13 +0530 | [diff] [blame] | 7972 | out_unlock: |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7973 | double_rq_unlock(rq, p_rq); |
Peter Zijlstra | 7b270f6 | 2013-01-22 13:09:13 +0530 | [diff] [blame] | 7974 | out_irq: |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7975 | local_irq_restore(flags); |
| 7976 | |
Peter Zijlstra | 7b270f6 | 2013-01-22 13:09:13 +0530 | [diff] [blame] | 7977 | if (yielded > 0) |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7978 | schedule(); |
| 7979 | |
| 7980 | return yielded; |
| 7981 | } |
| 7982 | EXPORT_SYMBOL_GPL(yield_to); |
| 7983 | |
Tejun Heo | 10ab564 | 2016-10-28 12:58:10 -0400 | [diff] [blame] | 7984 | int io_schedule_prepare(void) |
| 7985 | { |
| 7986 | int old_iowait = current->in_iowait; |
| 7987 | |
| 7988 | current->in_iowait = 1; |
| 7989 | blk_schedule_flush_plug(current); |
| 7990 | |
| 7991 | return old_iowait; |
| 7992 | } |
| 7993 | |
| 7994 | void io_schedule_finish(int token) |
| 7995 | { |
| 7996 | current->in_iowait = token; |
| 7997 | } |
| 7998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7999 | /* |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 8000 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8001 | * that process accounting knows that this is a task in IO wait state. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8002 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8003 | long __sched io_schedule_timeout(long timeout) |
| 8004 | { |
Tejun Heo | 10ab564 | 2016-10-28 12:58:10 -0400 | [diff] [blame] | 8005 | int token; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8006 | long ret; |
| 8007 | |
Tejun Heo | 10ab564 | 2016-10-28 12:58:10 -0400 | [diff] [blame] | 8008 | token = io_schedule_prepare(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8009 | ret = schedule_timeout(timeout); |
Tejun Heo | 10ab564 | 2016-10-28 12:58:10 -0400 | [diff] [blame] | 8010 | io_schedule_finish(token); |
NeilBrown | 9cff8ad | 2015-02-13 15:49:17 +1100 | [diff] [blame] | 8011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8012 | return ret; |
| 8013 | } |
NeilBrown | 9cff8ad | 2015-02-13 15:49:17 +1100 | [diff] [blame] | 8014 | EXPORT_SYMBOL(io_schedule_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8015 | |
Gao Xiang | e3b929b | 2019-06-03 17:13:38 +0800 | [diff] [blame] | 8016 | void __sched io_schedule(void) |
Tejun Heo | 10ab564 | 2016-10-28 12:58:10 -0400 | [diff] [blame] | 8017 | { |
| 8018 | int token; |
| 8019 | |
| 8020 | token = io_schedule_prepare(); |
| 8021 | schedule(); |
| 8022 | io_schedule_finish(token); |
| 8023 | } |
| 8024 | EXPORT_SYMBOL(io_schedule); |
| 8025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8026 | /** |
| 8027 | * sys_sched_get_priority_max - return maximum RT priority. |
| 8028 | * @policy: scheduling class. |
| 8029 | * |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 8030 | * Return: On success, this syscall returns the maximum |
| 8031 | * rt_priority that can be used by a given scheduling class. |
| 8032 | * On failure, a negative error code is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8033 | */ |
Heiko Carstens | 5add95d | 2009-01-14 14:14:08 +0100 | [diff] [blame] | 8034 | SYSCALL_DEFINE1(sched_get_priority_max, int, policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8035 | { |
| 8036 | int ret = -EINVAL; |
| 8037 | |
| 8038 | switch (policy) { |
| 8039 | case SCHED_FIFO: |
| 8040 | case SCHED_RR: |
Dietmar Eggemann | ae18ad2 | 2021-01-28 14:10:38 +0100 | [diff] [blame] | 8041 | ret = MAX_RT_PRIO-1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8042 | break; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 8043 | case SCHED_DEADLINE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8044 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 8045 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8046 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8047 | ret = 0; |
| 8048 | break; |
| 8049 | } |
| 8050 | return ret; |
| 8051 | } |
| 8052 | |
| 8053 | /** |
| 8054 | * sys_sched_get_priority_min - return minimum RT priority. |
| 8055 | * @policy: scheduling class. |
| 8056 | * |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 8057 | * Return: On success, this syscall returns the minimum |
| 8058 | * rt_priority that can be used by a given scheduling class. |
| 8059 | * On failure, a negative error code is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8060 | */ |
Heiko Carstens | 5add95d | 2009-01-14 14:14:08 +0100 | [diff] [blame] | 8061 | SYSCALL_DEFINE1(sched_get_priority_min, int, policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8062 | { |
| 8063 | int ret = -EINVAL; |
| 8064 | |
| 8065 | switch (policy) { |
| 8066 | case SCHED_FIFO: |
| 8067 | case SCHED_RR: |
| 8068 | ret = 1; |
| 8069 | break; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 8070 | case SCHED_DEADLINE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8071 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 8072 | case SCHED_BATCH: |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8073 | case SCHED_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8074 | ret = 0; |
| 8075 | } |
| 8076 | return ret; |
| 8077 | } |
| 8078 | |
Al Viro | abca5fc | 2017-09-19 18:17:46 -0400 | [diff] [blame] | 8079 | static int sched_rr_get_interval(pid_t pid, struct timespec64 *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8080 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 8081 | struct task_struct *p; |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 8082 | unsigned int time_slice; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 8083 | struct rq_flags rf; |
Thomas Gleixner | dba091b | 2009-12-09 09:32:03 +0100 | [diff] [blame] | 8084 | struct rq *rq; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8085 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8086 | |
| 8087 | if (pid < 0) |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8088 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8089 | |
| 8090 | retval = -ESRCH; |
Thomas Gleixner | 1a551ae | 2009-12-09 10:15:11 +0000 | [diff] [blame] | 8091 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8092 | p = find_process_by_pid(pid); |
| 8093 | if (!p) |
| 8094 | goto out_unlock; |
| 8095 | |
| 8096 | retval = security_task_getscheduler(p); |
| 8097 | if (retval) |
| 8098 | goto out_unlock; |
| 8099 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 8100 | rq = task_rq_lock(p, &rf); |
Peter Zijlstra | a57beec | 2014-01-27 11:54:13 +0100 | [diff] [blame] | 8101 | time_slice = 0; |
| 8102 | if (p->sched_class->get_rr_interval) |
| 8103 | time_slice = p->sched_class->get_rr_interval(rq, p); |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 8104 | task_rq_unlock(rq, p, &rf); |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 8105 | |
Thomas Gleixner | 1a551ae | 2009-12-09 10:15:11 +0000 | [diff] [blame] | 8106 | rcu_read_unlock(); |
Al Viro | abca5fc | 2017-09-19 18:17:46 -0400 | [diff] [blame] | 8107 | jiffies_to_timespec64(time_slice, t); |
| 8108 | return 0; |
Andi Kleen | 3a5c359 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 8109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8110 | out_unlock: |
Thomas Gleixner | 1a551ae | 2009-12-09 10:15:11 +0000 | [diff] [blame] | 8111 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8112 | return retval; |
| 8113 | } |
| 8114 | |
Randy Dunlap | 2064a5a | 2017-12-03 13:19:00 -0800 | [diff] [blame] | 8115 | /** |
| 8116 | * sys_sched_rr_get_interval - return the default timeslice of a process. |
| 8117 | * @pid: pid of the process. |
| 8118 | * @interval: userspace pointer to the timeslice value. |
| 8119 | * |
| 8120 | * this syscall writes the default timeslice value of a given process |
| 8121 | * into the user-space timespec buffer. A value of '0' means infinity. |
| 8122 | * |
| 8123 | * Return: On success, 0 and the timeslice is in @interval. Otherwise, |
| 8124 | * an error code. |
| 8125 | */ |
Al Viro | abca5fc | 2017-09-19 18:17:46 -0400 | [diff] [blame] | 8126 | SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid, |
Arnd Bergmann | 474b9c7 | 2018-04-17 21:59:47 +0200 | [diff] [blame] | 8127 | struct __kernel_timespec __user *, interval) |
Al Viro | abca5fc | 2017-09-19 18:17:46 -0400 | [diff] [blame] | 8128 | { |
| 8129 | struct timespec64 t; |
| 8130 | int retval = sched_rr_get_interval(pid, &t); |
| 8131 | |
| 8132 | if (retval == 0) |
| 8133 | retval = put_timespec64(&t, interval); |
| 8134 | |
| 8135 | return retval; |
| 8136 | } |
| 8137 | |
Arnd Bergmann | 474b9c7 | 2018-04-17 21:59:47 +0200 | [diff] [blame] | 8138 | #ifdef CONFIG_COMPAT_32BIT_TIME |
Arnd Bergmann | 8dabe72 | 2019-01-07 00:33:08 +0100 | [diff] [blame] | 8139 | SYSCALL_DEFINE2(sched_rr_get_interval_time32, pid_t, pid, |
| 8140 | struct old_timespec32 __user *, interval) |
Al Viro | abca5fc | 2017-09-19 18:17:46 -0400 | [diff] [blame] | 8141 | { |
| 8142 | struct timespec64 t; |
| 8143 | int retval = sched_rr_get_interval(pid, &t); |
| 8144 | |
| 8145 | if (retval == 0) |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 8146 | retval = put_old_timespec32(&t, interval); |
Al Viro | abca5fc | 2017-09-19 18:17:46 -0400 | [diff] [blame] | 8147 | return retval; |
| 8148 | } |
| 8149 | #endif |
| 8150 | |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 8151 | void sched_show_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8152 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8153 | unsigned long free = 0; |
Paul E. McKenney | 4e79752 | 2012-11-07 13:35:32 -0800 | [diff] [blame] | 8154 | int ppid; |
Ingo Molnar | c930b2c | 2017-02-03 12:22:54 +0100 | [diff] [blame] | 8155 | |
Tetsuo Handa | 3820050 | 2016-11-02 19:50:29 +0900 | [diff] [blame] | 8156 | if (!try_get_task_stack(p)) |
| 8157 | return; |
Xie XiuQi | 20435d8 | 2017-08-07 16:44:23 +0800 | [diff] [blame] | 8158 | |
Libing Zhou | cc172ff | 2020-08-14 11:02:36 +0800 | [diff] [blame] | 8159 | pr_info("task:%-15.15s state:%c", p->comm, task_state_to_char(p)); |
Xie XiuQi | 20435d8 | 2017-08-07 16:44:23 +0800 | [diff] [blame] | 8160 | |
| 8161 | if (p->state == TASK_RUNNING) |
Libing Zhou | cc172ff | 2020-08-14 11:02:36 +0800 | [diff] [blame] | 8162 | pr_cont(" running task "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8163 | #ifdef CONFIG_DEBUG_STACK_USAGE |
Eric Sandeen | 7c9f886 | 2008-04-22 16:38:23 -0500 | [diff] [blame] | 8164 | free = stack_not_used(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8165 | #endif |
Oleg Nesterov | a90e984 | 2014-12-10 15:45:21 -0800 | [diff] [blame] | 8166 | ppid = 0; |
Paul E. McKenney | 4e79752 | 2012-11-07 13:35:32 -0800 | [diff] [blame] | 8167 | rcu_read_lock(); |
Oleg Nesterov | a90e984 | 2014-12-10 15:45:21 -0800 | [diff] [blame] | 8168 | if (pid_alive(p)) |
| 8169 | ppid = task_pid_nr(rcu_dereference(p->real_parent)); |
Paul E. McKenney | 4e79752 | 2012-11-07 13:35:32 -0800 | [diff] [blame] | 8170 | rcu_read_unlock(); |
Libing Zhou | cc172ff | 2020-08-14 11:02:36 +0800 | [diff] [blame] | 8171 | pr_cont(" stack:%5lu pid:%5d ppid:%6d flags:0x%08lx\n", |
| 8172 | free, task_pid_nr(p), ppid, |
David Rientjes | aa47b7e | 2009-05-04 01:38:05 -0700 | [diff] [blame] | 8173 | (unsigned long)task_thread_info(p)->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8174 | |
Tejun Heo | 3d1cb20 | 2013-04-30 15:27:22 -0700 | [diff] [blame] | 8175 | print_worker_info(KERN_INFO, p); |
Peter Zijlstra | a8b62fd | 2020-09-21 12:58:17 +0200 | [diff] [blame] | 8176 | print_stop_info(KERN_INFO, p); |
Dmitry Safonov | 9cb8f06 | 2020-06-08 21:32:29 -0700 | [diff] [blame] | 8177 | show_stack(p, NULL, KERN_INFO); |
Tetsuo Handa | 3820050 | 2016-11-02 19:50:29 +0900 | [diff] [blame] | 8178 | put_task_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8179 | } |
Paul E. McKenney | 0032f4e | 2017-08-30 10:40:17 -0700 | [diff] [blame] | 8180 | EXPORT_SYMBOL_GPL(sched_show_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8181 | |
Peter Zijlstra | 5d68cc9 | 2017-09-22 18:32:41 +0200 | [diff] [blame] | 8182 | static inline bool |
| 8183 | state_filter_match(unsigned long state_filter, struct task_struct *p) |
| 8184 | { |
| 8185 | /* no filter, everything matches */ |
| 8186 | if (!state_filter) |
| 8187 | return true; |
| 8188 | |
| 8189 | /* filter, but doesn't match */ |
| 8190 | if (!(p->state & state_filter)) |
| 8191 | return false; |
| 8192 | |
| 8193 | /* |
| 8194 | * When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows |
| 8195 | * TASK_KILLABLE). |
| 8196 | */ |
| 8197 | if (state_filter == TASK_UNINTERRUPTIBLE && p->state == TASK_IDLE) |
| 8198 | return false; |
| 8199 | |
| 8200 | return true; |
| 8201 | } |
| 8202 | |
| 8203 | |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 8204 | void show_state_filter(unsigned long state_filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8205 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 8206 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8207 | |
Thomas Gleixner | 510f5ac | 2011-07-17 20:47:54 +0200 | [diff] [blame] | 8208 | rcu_read_lock(); |
Oleg Nesterov | 5d07f42 | 2014-08-13 21:19:53 +0200 | [diff] [blame] | 8209 | for_each_process_thread(g, p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8210 | /* |
| 8211 | * reset the NMI-timeout, listing all files on a slow |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 8212 | * console might take a lot of time: |
Andrey Ryabinin | 57675cb | 2016-06-09 15:20:05 +0300 | [diff] [blame] | 8213 | * Also, reset softlockup watchdogs on all CPUs, because |
| 8214 | * another CPU might be blocked waiting for us to process |
| 8215 | * an IPI. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8216 | */ |
| 8217 | touch_nmi_watchdog(); |
Andrey Ryabinin | 57675cb | 2016-06-09 15:20:05 +0300 | [diff] [blame] | 8218 | touch_all_softlockup_watchdogs(); |
Peter Zijlstra | 5d68cc9 | 2017-09-22 18:32:41 +0200 | [diff] [blame] | 8219 | if (state_filter_match(state_filter, p)) |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 8220 | sched_show_task(p); |
Oleg Nesterov | 5d07f42 | 2014-08-13 21:19:53 +0200 | [diff] [blame] | 8221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8222 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8223 | #ifdef CONFIG_SCHED_DEBUG |
Rabin Vincent | fb90a6e | 2016-04-04 15:42:02 +0200 | [diff] [blame] | 8224 | if (!state_filter) |
| 8225 | sysrq_sched_debug_show(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8226 | #endif |
Thomas Gleixner | 510f5ac | 2011-07-17 20:47:54 +0200 | [diff] [blame] | 8227 | rcu_read_unlock(); |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 8228 | /* |
| 8229 | * Only show locks if all tasks are dumped: |
| 8230 | */ |
Shmulik Ladkani | 93335a2 | 2009-11-25 15:23:41 +0200 | [diff] [blame] | 8231 | if (!state_filter) |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 8232 | debug_show_all_locks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8233 | } |
| 8234 | |
Ingo Molnar | f340c0d | 2005-06-28 16:40:42 +0200 | [diff] [blame] | 8235 | /** |
| 8236 | * init_idle - set up an idle thread for a given CPU |
| 8237 | * @idle: task in question |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 8238 | * @cpu: CPU the idle task belongs to |
Ingo Molnar | f340c0d | 2005-06-28 16:40:42 +0200 | [diff] [blame] | 8239 | * |
| 8240 | * NOTE: this function does not set the idle thread's NEED_RESCHED |
| 8241 | * flag, to make booting more robust. |
| 8242 | */ |
Valentin Schneider | f1a0a37 | 2021-05-12 10:46:36 +0100 | [diff] [blame] | 8243 | void __init init_idle(struct task_struct *idle, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8244 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 8245 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8246 | unsigned long flags; |
| 8247 | |
Peter Zijlstra | ff51ff8 | 2019-10-01 11:18:37 +0200 | [diff] [blame] | 8248 | __sched_fork(0, idle); |
| 8249 | |
Valentin Schneider | 00b89fe | 2021-05-10 16:10:23 +0100 | [diff] [blame] | 8250 | /* |
| 8251 | * The idle task doesn't need the kthread struct to function, but it |
| 8252 | * is dressed up as a per-CPU kthread and thus needs to play the part |
| 8253 | * if we want to avoid special-casing it in code that deals with per-CPU |
| 8254 | * kthreads. |
| 8255 | */ |
| 8256 | set_kthread_struct(idle); |
| 8257 | |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 8258 | raw_spin_lock_irqsave(&idle->pi_lock, flags); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 8259 | raw_spin_rq_lock(rq); |
Ingo Molnar | 5cbd54e | 2008-11-12 20:05:50 +0100 | [diff] [blame] | 8260 | |
Peter Zijlstra | 06b83b5 | 2009-12-16 18:04:35 +0100 | [diff] [blame] | 8261 | idle->state = TASK_RUNNING; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8262 | idle->se.exec_start = sched_clock(); |
Valentin Schneider | 00b89fe | 2021-05-10 16:10:23 +0100 | [diff] [blame] | 8263 | /* |
| 8264 | * PF_KTHREAD should already be set at this point; regardless, make it |
| 8265 | * look like a proper per-CPU kthread. |
| 8266 | */ |
| 8267 | idle->flags |= PF_IDLE | PF_KTHREAD | PF_NO_SETAFFINITY; |
| 8268 | kthread_set_per_cpu(idle, cpu); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8269 | |
Sami Tolvanen | d08b9f0 | 2020-04-27 09:00:07 -0700 | [diff] [blame] | 8270 | scs_task_reset(idle); |
Mark Rutland | e1b77c9 | 2016-03-09 14:08:18 -0800 | [diff] [blame] | 8271 | kasan_unpoison_task_stack(idle); |
| 8272 | |
Peter Zijlstra | de9b8f5 | 2015-08-13 23:09:29 +0200 | [diff] [blame] | 8273 | #ifdef CONFIG_SMP |
| 8274 | /* |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 8275 | * It's possible that init_idle() gets called multiple times on a task, |
Peter Zijlstra | de9b8f5 | 2015-08-13 23:09:29 +0200 | [diff] [blame] | 8276 | * in that case do_set_cpus_allowed() will not do the right thing. |
| 8277 | * |
| 8278 | * And since this is boot we can forgo the serialization. |
| 8279 | */ |
Peter Zijlstra | 9cfc3e1 | 2020-09-16 14:59:08 +0200 | [diff] [blame] | 8280 | set_cpus_allowed_common(idle, cpumask_of(cpu), 0); |
Peter Zijlstra | de9b8f5 | 2015-08-13 23:09:29 +0200 | [diff] [blame] | 8281 | #endif |
Peter Zijlstra | 6506cf6c | 2010-09-16 17:50:31 +0200 | [diff] [blame] | 8282 | /* |
| 8283 | * We're having a chicken and egg problem, even though we are |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 8284 | * holding rq->lock, the CPU isn't yet set to this CPU so the |
Peter Zijlstra | 6506cf6c | 2010-09-16 17:50:31 +0200 | [diff] [blame] | 8285 | * lockdep check in task_group() will fail. |
| 8286 | * |
| 8287 | * Similar case to sched_fork(). / Alternatively we could |
| 8288 | * use task_rq_lock() here and obtain the other rq->lock. |
| 8289 | * |
| 8290 | * Silence PROVE_RCU |
| 8291 | */ |
| 8292 | rcu_read_lock(); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8293 | __set_task_cpu(idle, cpu); |
Peter Zijlstra | 6506cf6c | 2010-09-16 17:50:31 +0200 | [diff] [blame] | 8294 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8295 | |
Eric W. Biederman | 5311a98 | 2019-09-14 07:35:02 -0500 | [diff] [blame] | 8296 | rq->idle = idle; |
| 8297 | rcu_assign_pointer(rq->curr, idle); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 8298 | idle->on_rq = TASK_ON_RQ_QUEUED; |
Peter Zijlstra | de9b8f5 | 2015-08-13 23:09:29 +0200 | [diff] [blame] | 8299 | #ifdef CONFIG_SMP |
Peter Zijlstra | 3ca7a44 | 2011-04-05 17:23:40 +0200 | [diff] [blame] | 8300 | idle->on_cpu = 1; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 8301 | #endif |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 8302 | raw_spin_rq_unlock(rq); |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 8303 | raw_spin_unlock_irqrestore(&idle->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8304 | |
| 8305 | /* Set the preempt count _outside_ the spinlocks! */ |
Peter Zijlstra | 0102874 | 2013-08-14 14:55:46 +0200 | [diff] [blame] | 8306 | init_idle_preempt_count(idle, cpu); |
Jonathan Corbet | 625f2a3 | 2011-04-22 11:19:10 -0600 | [diff] [blame] | 8307 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8308 | /* |
| 8309 | * The idle tasks have their own, simple scheduling class: |
| 8310 | */ |
| 8311 | idle->sched_class = &idle_sched_class; |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 8312 | ftrace_graph_init_idle_task(idle, cpu); |
Frederic Weisbecker | 45eacc6 | 2013-05-15 22:16:32 +0200 | [diff] [blame] | 8313 | vtime_init_idle(idle, cpu); |
Peter Zijlstra | de9b8f5 | 2015-08-13 23:09:29 +0200 | [diff] [blame] | 8314 | #ifdef CONFIG_SMP |
Carsten Emde | f1c6f1a | 2011-10-26 23:14:16 +0200 | [diff] [blame] | 8315 | sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu); |
| 8316 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8317 | } |
| 8318 | |
Nicolas Pitre | e1d4eee | 2017-06-14 13:19:23 -0400 | [diff] [blame] | 8319 | #ifdef CONFIG_SMP |
| 8320 | |
Juri Lelli | f82f804 | 2014-10-07 09:52:11 +0100 | [diff] [blame] | 8321 | int cpuset_cpumask_can_shrink(const struct cpumask *cur, |
| 8322 | const struct cpumask *trial) |
| 8323 | { |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 8324 | int ret = 1; |
Juri Lelli | f82f804 | 2014-10-07 09:52:11 +0100 | [diff] [blame] | 8325 | |
Mike Galbraith | bb2bc55 | 2015-01-28 04:53:55 +0100 | [diff] [blame] | 8326 | if (!cpumask_weight(cur)) |
| 8327 | return ret; |
| 8328 | |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 8329 | ret = dl_cpuset_cpumask_can_shrink(cur, trial); |
Juri Lelli | f82f804 | 2014-10-07 09:52:11 +0100 | [diff] [blame] | 8330 | |
| 8331 | return ret; |
| 8332 | } |
| 8333 | |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 8334 | int task_can_attach(struct task_struct *p, |
| 8335 | const struct cpumask *cs_cpus_allowed) |
| 8336 | { |
| 8337 | int ret = 0; |
| 8338 | |
| 8339 | /* |
| 8340 | * Kthreads which disallow setaffinity shouldn't be moved |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 8341 | * to a new cpuset; we don't want to change their CPU |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 8342 | * affinity and isolating such threads by their set of |
| 8343 | * allowed nodes is unnecessary. Thus, cpusets are not |
| 8344 | * applicable for such threads. This prevents checking for |
| 8345 | * success of set_cpus_allowed_ptr() on all attached tasks |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 8346 | * before cpus_mask may be changed. |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 8347 | */ |
| 8348 | if (p->flags & PF_NO_SETAFFINITY) { |
| 8349 | ret = -EINVAL; |
| 8350 | goto out; |
| 8351 | } |
| 8352 | |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 8353 | if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span, |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 8354 | cs_cpus_allowed)) |
| 8355 | ret = dl_task_can_attach(p, cs_cpus_allowed); |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 8356 | |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 8357 | out: |
| 8358 | return ret; |
| 8359 | } |
| 8360 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 8361 | bool sched_smp_initialized __read_mostly; |
Thomas Gleixner | e26fbff | 2016-03-10 12:54:10 +0100 | [diff] [blame] | 8362 | |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 8363 | #ifdef CONFIG_NUMA_BALANCING |
| 8364 | /* Migrate current task p to target_cpu */ |
| 8365 | int migrate_task_to(struct task_struct *p, int target_cpu) |
| 8366 | { |
| 8367 | struct migration_arg arg = { p, target_cpu }; |
| 8368 | int curr_cpu = task_cpu(p); |
| 8369 | |
| 8370 | if (curr_cpu == target_cpu) |
| 8371 | return 0; |
| 8372 | |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 8373 | if (!cpumask_test_cpu(target_cpu, p->cpus_ptr)) |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 8374 | return -EINVAL; |
| 8375 | |
| 8376 | /* TODO: This is not properly updating schedstats */ |
| 8377 | |
Mel Gorman | 286549d | 2014-01-21 15:51:03 -0800 | [diff] [blame] | 8378 | trace_sched_move_numa(p, curr_cpu, target_cpu); |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 8379 | return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg); |
| 8380 | } |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 8381 | |
| 8382 | /* |
| 8383 | * Requeue a task on a given node and accurately track the number of NUMA |
| 8384 | * tasks on the runqueues |
| 8385 | */ |
| 8386 | void sched_setnuma(struct task_struct *p, int nid) |
| 8387 | { |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 8388 | bool queued, running; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 8389 | struct rq_flags rf; |
| 8390 | struct rq *rq; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 8391 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 8392 | rq = task_rq_lock(p, &rf); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 8393 | queued = task_on_rq_queued(p); |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 8394 | running = task_current(rq, p); |
| 8395 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 8396 | if (queued) |
Peter Zijlstra | 1de6444 | 2015-09-30 17:44:13 +0200 | [diff] [blame] | 8397 | dequeue_task(rq, p, DEQUEUE_SAVE); |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 8398 | if (running) |
Kirill Tkhai | f3cd1c4 | 2014-09-12 17:41:40 +0400 | [diff] [blame] | 8399 | put_prev_task(rq, p); |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 8400 | |
| 8401 | p->numa_preferred_nid = nid; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 8402 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 8403 | if (queued) |
Peter Zijlstra | 7134b3e | 2017-02-21 14:23:38 +0100 | [diff] [blame] | 8404 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
Vincent Guittot | a399d23 | 2016-09-12 09:47:52 +0200 | [diff] [blame] | 8405 | if (running) |
Peter Zijlstra | 03b7fad | 2019-05-29 20:36:41 +0000 | [diff] [blame] | 8406 | set_next_task(rq, p); |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 8407 | task_rq_unlock(rq, p, &rf); |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 8408 | } |
Peter Zijlstra | 5cc389b | 2015-06-11 14:46:50 +0200 | [diff] [blame] | 8409 | #endif /* CONFIG_NUMA_BALANCING */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8410 | |
| 8411 | #ifdef CONFIG_HOTPLUG_CPU |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 8412 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 8413 | * Ensure that the idle task is using init_mm right before its CPU goes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8414 | * offline. |
| 8415 | */ |
| 8416 | void idle_task_exit(void) |
| 8417 | { |
| 8418 | struct mm_struct *mm = current->active_mm; |
| 8419 | |
| 8420 | BUG_ON(cpu_online(smp_processor_id())); |
Peter Zijlstra | bf2c59f | 2020-04-01 17:40:33 -0400 | [diff] [blame] | 8421 | BUG_ON(current != this_rq()->idle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8422 | |
Martin Schwidefsky | a53efe5 | 2012-10-26 17:17:44 +0200 | [diff] [blame] | 8423 | if (mm != &init_mm) { |
Andy Lutomirski | 252d2a4 | 2017-06-09 11:49:15 -0700 | [diff] [blame] | 8424 | switch_mm(mm, &init_mm, current); |
Martin Schwidefsky | a53efe5 | 2012-10-26 17:17:44 +0200 | [diff] [blame] | 8425 | finish_arch_post_lock_switch(); |
| 8426 | } |
Peter Zijlstra | bf2c59f | 2020-04-01 17:40:33 -0400 | [diff] [blame] | 8427 | |
| 8428 | /* finish_cpu(), as ran on the BP, will clean up the active_mm state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8429 | } |
| 8430 | |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8431 | static int __balance_push_cpu_stop(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8432 | { |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8433 | struct task_struct *p = arg; |
| 8434 | struct rq *rq = this_rq(); |
| 8435 | struct rq_flags rf; |
| 8436 | int cpu; |
Peter Zijlstra | 48c5ccae | 2010-11-13 19:32:29 +0100 | [diff] [blame] | 8437 | |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8438 | raw_spin_lock_irq(&p->pi_lock); |
| 8439 | rq_lock(rq, &rf); |
Peter Zijlstra | 10e7071 | 2019-08-06 15:13:17 +0200 | [diff] [blame] | 8440 | |
Frederic Weisbecker | 77bd397 | 2013-04-12 01:50:58 +0200 | [diff] [blame] | 8441 | update_rq_clock(rq); |
| 8442 | |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8443 | if (task_rq(p) == rq && task_on_rq_queued(p)) { |
| 8444 | cpu = select_fallback_rq(rq->cpu, p); |
| 8445 | rq = __migrate_task(rq, &rf, p, cpu); |
Peter Zijlstra | 48c5ccae | 2010-11-13 19:32:29 +0100 | [diff] [blame] | 8446 | } |
| 8447 | |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8448 | rq_unlock(rq, &rf); |
| 8449 | raw_spin_unlock_irq(&p->pi_lock); |
| 8450 | |
| 8451 | put_task_struct(p); |
| 8452 | |
| 8453 | return 0; |
Peter Zijlstra | 48c5ccae | 2010-11-13 19:32:29 +0100 | [diff] [blame] | 8454 | } |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8455 | |
| 8456 | static DEFINE_PER_CPU(struct cpu_stop_work, push_work); |
| 8457 | |
| 8458 | /* |
| 8459 | * Ensure we only run per-cpu kthreads once the CPU goes !active. |
Peter Zijlstra | b5c4477 | 2021-01-21 16:09:32 +0100 | [diff] [blame] | 8460 | * |
| 8461 | * This is enabled below SCHED_AP_ACTIVE; when !cpu_active(), but only |
| 8462 | * effective when the hotplug motion is down. |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8463 | */ |
| 8464 | static void balance_push(struct rq *rq) |
| 8465 | { |
| 8466 | struct task_struct *push_task = rq->curr; |
| 8467 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 8468 | lockdep_assert_rq_held(rq); |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8469 | SCHED_WARN_ON(rq->cpu != smp_processor_id()); |
Peter Zijlstra | b5c4477 | 2021-01-21 16:09:32 +0100 | [diff] [blame] | 8470 | |
Peter Zijlstra | ae79270 | 2020-12-10 17:14:08 +0100 | [diff] [blame] | 8471 | /* |
| 8472 | * Ensure the thing is persistent until balance_push_set(.on = false); |
| 8473 | */ |
| 8474 | rq->balance_callback = &balance_push_callback; |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8475 | |
| 8476 | /* |
Peter Zijlstra | b5c4477 | 2021-01-21 16:09:32 +0100 | [diff] [blame] | 8477 | * Only active while going offline. |
| 8478 | */ |
| 8479 | if (!cpu_dying(rq->cpu)) |
| 8480 | return; |
| 8481 | |
| 8482 | /* |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8483 | * Both the cpu-hotplug and stop task are in this case and are |
| 8484 | * required to complete the hotplug process. |
| 8485 | */ |
Valentin Schneider | 00b89fe | 2021-05-10 16:10:23 +0100 | [diff] [blame] | 8486 | if (kthread_is_per_cpu(push_task) || |
Peter Zijlstra | 5ba2ffba | 2021-01-12 11:28:16 +0100 | [diff] [blame] | 8487 | is_migration_disabled(push_task)) { |
| 8488 | |
Thomas Gleixner | f2469a1 | 2020-09-14 14:47:28 +0200 | [diff] [blame] | 8489 | /* |
| 8490 | * If this is the idle task on the outgoing CPU try to wake |
| 8491 | * up the hotplug control thread which might wait for the |
| 8492 | * last task to vanish. The rcuwait_active() check is |
| 8493 | * accurate here because the waiter is pinned on this CPU |
| 8494 | * and can't obviously be running in parallel. |
Thomas Gleixner | 3015ef4 | 2020-08-26 14:08:10 +0200 | [diff] [blame] | 8495 | * |
| 8496 | * On RT kernels this also has to check whether there are |
| 8497 | * pinned and scheduled out tasks on the runqueue. They |
| 8498 | * need to leave the migrate disabled section first. |
Thomas Gleixner | f2469a1 | 2020-09-14 14:47:28 +0200 | [diff] [blame] | 8499 | */ |
Thomas Gleixner | 3015ef4 | 2020-08-26 14:08:10 +0200 | [diff] [blame] | 8500 | if (!rq->nr_running && !rq_has_pinned_tasks(rq) && |
| 8501 | rcuwait_active(&rq->hotplug_wait)) { |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 8502 | raw_spin_rq_unlock(rq); |
Thomas Gleixner | f2469a1 | 2020-09-14 14:47:28 +0200 | [diff] [blame] | 8503 | rcuwait_wake_up(&rq->hotplug_wait); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 8504 | raw_spin_rq_lock(rq); |
Thomas Gleixner | f2469a1 | 2020-09-14 14:47:28 +0200 | [diff] [blame] | 8505 | } |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8506 | return; |
Thomas Gleixner | f2469a1 | 2020-09-14 14:47:28 +0200 | [diff] [blame] | 8507 | } |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8508 | |
| 8509 | get_task_struct(push_task); |
| 8510 | /* |
| 8511 | * Temporarily drop rq->lock such that we can wake-up the stop task. |
| 8512 | * Both preemption and IRQs are still disabled. |
| 8513 | */ |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 8514 | raw_spin_rq_unlock(rq); |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8515 | stop_one_cpu_nowait(rq->cpu, __balance_push_cpu_stop, push_task, |
| 8516 | this_cpu_ptr(&push_work)); |
| 8517 | /* |
| 8518 | * At this point need_resched() is true and we'll take the loop in |
| 8519 | * schedule(). The next pick is obviously going to be the stop task |
Peter Zijlstra | 5ba2ffba | 2021-01-12 11:28:16 +0100 | [diff] [blame] | 8520 | * which kthread_is_per_cpu() and will push this task away. |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8521 | */ |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 8522 | raw_spin_rq_lock(rq); |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8523 | } |
| 8524 | |
| 8525 | static void balance_push_set(int cpu, bool on) |
| 8526 | { |
| 8527 | struct rq *rq = cpu_rq(cpu); |
| 8528 | struct rq_flags rf; |
| 8529 | |
| 8530 | rq_lock_irqsave(rq, &rf); |
Peter Zijlstra | 22f667c | 2021-01-15 18:17:45 +0100 | [diff] [blame] | 8531 | if (on) { |
| 8532 | WARN_ON_ONCE(rq->balance_callback); |
Peter Zijlstra | ae79270 | 2020-12-10 17:14:08 +0100 | [diff] [blame] | 8533 | rq->balance_callback = &balance_push_callback; |
Peter Zijlstra | 22f667c | 2021-01-15 18:17:45 +0100 | [diff] [blame] | 8534 | } else if (rq->balance_callback == &balance_push_callback) { |
Peter Zijlstra | ae79270 | 2020-12-10 17:14:08 +0100 | [diff] [blame] | 8535 | rq->balance_callback = NULL; |
Peter Zijlstra | 22f667c | 2021-01-15 18:17:45 +0100 | [diff] [blame] | 8536 | } |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8537 | rq_unlock_irqrestore(rq, &rf); |
| 8538 | } |
| 8539 | |
Thomas Gleixner | f2469a1 | 2020-09-14 14:47:28 +0200 | [diff] [blame] | 8540 | /* |
| 8541 | * Invoked from a CPUs hotplug control thread after the CPU has been marked |
| 8542 | * inactive. All tasks which are not per CPU kernel threads are either |
| 8543 | * pushed off this CPU now via balance_push() or placed on a different CPU |
| 8544 | * during wakeup. Wait until the CPU is quiescent. |
| 8545 | */ |
| 8546 | static void balance_hotplug_wait(void) |
| 8547 | { |
| 8548 | struct rq *rq = this_rq(); |
| 8549 | |
Thomas Gleixner | 3015ef4 | 2020-08-26 14:08:10 +0200 | [diff] [blame] | 8550 | rcuwait_wait_event(&rq->hotplug_wait, |
| 8551 | rq->nr_running == 1 && !rq_has_pinned_tasks(rq), |
Thomas Gleixner | f2469a1 | 2020-09-14 14:47:28 +0200 | [diff] [blame] | 8552 | TASK_UNINTERRUPTIBLE); |
| 8553 | } |
| 8554 | |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8555 | #else |
| 8556 | |
| 8557 | static inline void balance_push(struct rq *rq) |
| 8558 | { |
| 8559 | } |
| 8560 | |
| 8561 | static inline void balance_push_set(int cpu, bool on) |
| 8562 | { |
| 8563 | } |
| 8564 | |
Thomas Gleixner | f2469a1 | 2020-09-14 14:47:28 +0200 | [diff] [blame] | 8565 | static inline void balance_hotplug_wait(void) |
| 8566 | { |
| 8567 | } |
| 8568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8569 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 8570 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 8571 | void set_rq_online(struct rq *rq) |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 8572 | { |
| 8573 | if (!rq->online) { |
| 8574 | const struct sched_class *class; |
| 8575 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 8576 | cpumask_set_cpu(rq->cpu, rq->rd->online); |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 8577 | rq->online = 1; |
| 8578 | |
| 8579 | for_each_class(class) { |
| 8580 | if (class->rq_online) |
| 8581 | class->rq_online(rq); |
| 8582 | } |
| 8583 | } |
| 8584 | } |
| 8585 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 8586 | void set_rq_offline(struct rq *rq) |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 8587 | { |
| 8588 | if (rq->online) { |
| 8589 | const struct sched_class *class; |
| 8590 | |
| 8591 | for_each_class(class) { |
| 8592 | if (class->rq_offline) |
| 8593 | class->rq_offline(rq); |
| 8594 | } |
| 8595 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 8596 | cpumask_clear_cpu(rq->cpu, rq->rd->online); |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 8597 | rq->online = 0; |
| 8598 | } |
| 8599 | } |
| 8600 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 8601 | /* |
| 8602 | * used to mark begin/end of suspend/resume: |
| 8603 | */ |
| 8604 | static int num_cpus_frozen; |
Srivatsa S. Bhat | d35be8b | 2012-05-24 19:46:26 +0530 | [diff] [blame] | 8605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8606 | /* |
Tejun Heo | 3a101d0 | 2010-06-08 21:40:36 +0200 | [diff] [blame] | 8607 | * Update cpusets according to cpu_active mask. If cpusets are |
| 8608 | * disabled, cpuset_update_active_cpus() becomes a simple wrapper |
| 8609 | * around partition_sched_domains(). |
Srivatsa S. Bhat | d35be8b | 2012-05-24 19:46:26 +0530 | [diff] [blame] | 8610 | * |
| 8611 | * If we come here as part of a suspend/resume, don't touch cpusets because we |
| 8612 | * want to restore it back to its original state upon resume anyway. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8613 | */ |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8614 | static void cpuset_cpu_active(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8615 | { |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8616 | if (cpuhp_tasks_frozen) { |
Srivatsa S. Bhat | d35be8b | 2012-05-24 19:46:26 +0530 | [diff] [blame] | 8617 | /* |
| 8618 | * num_cpus_frozen tracks how many CPUs are involved in suspend |
| 8619 | * resume sequence. As long as this is not the last online |
| 8620 | * operation in the resume sequence, just build a single sched |
| 8621 | * domain, ignoring cpusets. |
| 8622 | */ |
Peter Zijlstra | 50e7663 | 2017-09-07 11:13:38 +0200 | [diff] [blame] | 8623 | partition_sched_domains(1, NULL, NULL); |
| 8624 | if (--num_cpus_frozen) |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8625 | return; |
Srivatsa S. Bhat | d35be8b | 2012-05-24 19:46:26 +0530 | [diff] [blame] | 8626 | /* |
| 8627 | * This is the last CPU online operation. So fall through and |
| 8628 | * restore the original sched domains by considering the |
| 8629 | * cpuset configurations. |
| 8630 | */ |
Peter Zijlstra | 50e7663 | 2017-09-07 11:13:38 +0200 | [diff] [blame] | 8631 | cpuset_force_rebuild(); |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 8632 | } |
Rakib Mullick | 30e03ac | 2017-04-09 07:36:14 +0600 | [diff] [blame] | 8633 | cpuset_update_active_cpus(); |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 8634 | } |
Tejun Heo | 3a101d0 | 2010-06-08 21:40:36 +0200 | [diff] [blame] | 8635 | |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8636 | static int cpuset_cpu_inactive(unsigned int cpu) |
Tejun Heo | 3a101d0 | 2010-06-08 21:40:36 +0200 | [diff] [blame] | 8637 | { |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8638 | if (!cpuhp_tasks_frozen) { |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 8639 | if (dl_cpu_busy(cpu)) |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8640 | return -EBUSY; |
Rakib Mullick | 30e03ac | 2017-04-09 07:36:14 +0600 | [diff] [blame] | 8641 | cpuset_update_active_cpus(); |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8642 | } else { |
Srivatsa S. Bhat | d35be8b | 2012-05-24 19:46:26 +0530 | [diff] [blame] | 8643 | num_cpus_frozen++; |
| 8644 | partition_sched_domains(1, NULL, NULL); |
Tejun Heo | 3a101d0 | 2010-06-08 21:40:36 +0200 | [diff] [blame] | 8645 | } |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8646 | return 0; |
Tejun Heo | 3a101d0 | 2010-06-08 21:40:36 +0200 | [diff] [blame] | 8647 | } |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 8648 | |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8649 | int sched_cpu_activate(unsigned int cpu) |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8650 | { |
Thomas Gleixner | 7d97669 | 2016-03-10 12:54:17 +0100 | [diff] [blame] | 8651 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 8652 | struct rq_flags rf; |
Thomas Gleixner | 7d97669 | 2016-03-10 12:54:17 +0100 | [diff] [blame] | 8653 | |
Peter Zijlstra | 22f667c | 2021-01-15 18:17:45 +0100 | [diff] [blame] | 8654 | /* |
Peter Zijlstra | b5c4477 | 2021-01-21 16:09:32 +0100 | [diff] [blame] | 8655 | * Clear the balance_push callback and prepare to schedule |
| 8656 | * regular tasks. |
Peter Zijlstra | 22f667c | 2021-01-15 18:17:45 +0100 | [diff] [blame] | 8657 | */ |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8658 | balance_push_set(cpu, false); |
| 8659 | |
Peter Zijlstra | ba2591a | 2018-05-29 16:43:46 +0200 | [diff] [blame] | 8660 | #ifdef CONFIG_SCHED_SMT |
| 8661 | /* |
Peter Zijlstra (Intel) | c5511d0 | 2018-11-25 19:33:36 +0100 | [diff] [blame] | 8662 | * When going up, increment the number of cores with SMT present. |
Peter Zijlstra | ba2591a | 2018-05-29 16:43:46 +0200 | [diff] [blame] | 8663 | */ |
Peter Zijlstra (Intel) | c5511d0 | 2018-11-25 19:33:36 +0100 | [diff] [blame] | 8664 | if (cpumask_weight(cpu_smt_mask(cpu)) == 2) |
| 8665 | static_branch_inc_cpuslocked(&sched_smt_present); |
Peter Zijlstra | ba2591a | 2018-05-29 16:43:46 +0200 | [diff] [blame] | 8666 | #endif |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8667 | set_cpu_active(cpu, true); |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8668 | |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8669 | if (sched_smp_initialized) { |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8670 | sched_domains_numa_masks_set(cpu); |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8671 | cpuset_cpu_active(); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 8672 | } |
Thomas Gleixner | 7d97669 | 2016-03-10 12:54:17 +0100 | [diff] [blame] | 8673 | |
| 8674 | /* |
| 8675 | * Put the rq online, if not already. This happens: |
| 8676 | * |
| 8677 | * 1) In the early boot process, because we build the real domains |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 8678 | * after all CPUs have been brought up. |
Thomas Gleixner | 7d97669 | 2016-03-10 12:54:17 +0100 | [diff] [blame] | 8679 | * |
| 8680 | * 2) At runtime, if cpuset_cpu_active() fails to rebuild the |
| 8681 | * domains. |
| 8682 | */ |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 8683 | rq_lock_irqsave(rq, &rf); |
Thomas Gleixner | 7d97669 | 2016-03-10 12:54:17 +0100 | [diff] [blame] | 8684 | if (rq->rd) { |
| 8685 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); |
| 8686 | set_rq_online(rq); |
| 8687 | } |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 8688 | rq_unlock_irqrestore(rq, &rf); |
Thomas Gleixner | 7d97669 | 2016-03-10 12:54:17 +0100 | [diff] [blame] | 8689 | |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8690 | return 0; |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8691 | } |
| 8692 | |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8693 | int sched_cpu_deactivate(unsigned int cpu) |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8694 | { |
Peter Zijlstra | 120455c | 2020-09-25 16:42:31 +0200 | [diff] [blame] | 8695 | struct rq *rq = cpu_rq(cpu); |
| 8696 | struct rq_flags rf; |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8697 | int ret; |
| 8698 | |
Anna-Maria Behnsen | e0b257c | 2020-12-15 11:44:00 +0100 | [diff] [blame] | 8699 | /* |
| 8700 | * Remove CPU from nohz.idle_cpus_mask to prevent participating in |
| 8701 | * load balancing when not active |
| 8702 | */ |
| 8703 | nohz_balance_exit_idle(rq); |
| 8704 | |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8705 | set_cpu_active(cpu, false); |
Peter Zijlstra | 741ba80 | 2021-01-16 11:56:37 +0100 | [diff] [blame] | 8706 | |
| 8707 | /* |
| 8708 | * From this point forward, this CPU will refuse to run any task that |
| 8709 | * is not: migrate_disable() or KTHREAD_IS_PER_CPU, and will actively |
| 8710 | * push those tasks away until this gets cleared, see |
| 8711 | * sched_cpu_dying(). |
| 8712 | */ |
Peter Zijlstra | 975707f | 2021-01-20 15:05:41 +0100 | [diff] [blame] | 8713 | balance_push_set(cpu, true); |
| 8714 | |
Peter Zijlstra | b2454ca | 2016-03-10 12:54:14 +0100 | [diff] [blame] | 8715 | /* |
Peter Zijlstra | 975707f | 2021-01-20 15:05:41 +0100 | [diff] [blame] | 8716 | * We've cleared cpu_active_mask / set balance_push, wait for all |
| 8717 | * preempt-disabled and RCU users of this state to go away such that |
| 8718 | * all new such users will observe it. |
Peter Zijlstra | b2454ca | 2016-03-10 12:54:14 +0100 | [diff] [blame] | 8719 | * |
Peter Zijlstra | 5ba2ffba | 2021-01-12 11:28:16 +0100 | [diff] [blame] | 8720 | * Specifically, we rely on ttwu to no longer target this CPU, see |
| 8721 | * ttwu_queue_cond() and is_cpu_allowed(). |
| 8722 | * |
Peter Zijlstra | b2454ca | 2016-03-10 12:54:14 +0100 | [diff] [blame] | 8723 | * Do sync before park smpboot threads to take care the rcu boost case. |
| 8724 | */ |
Paul E. McKenney | 309ba85 | 2018-07-11 14:36:49 -0700 | [diff] [blame] | 8725 | synchronize_rcu(); |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8726 | |
Peter Zijlstra | 120455c | 2020-09-25 16:42:31 +0200 | [diff] [blame] | 8727 | rq_lock_irqsave(rq, &rf); |
| 8728 | if (rq->rd) { |
| 8729 | update_rq_clock(rq); |
| 8730 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); |
| 8731 | set_rq_offline(rq); |
| 8732 | } |
| 8733 | rq_unlock_irqrestore(rq, &rf); |
| 8734 | |
Peter Zijlstra (Intel) | c5511d0 | 2018-11-25 19:33:36 +0100 | [diff] [blame] | 8735 | #ifdef CONFIG_SCHED_SMT |
| 8736 | /* |
| 8737 | * When going down, decrement the number of cores with SMT present. |
| 8738 | */ |
| 8739 | if (cpumask_weight(cpu_smt_mask(cpu)) == 2) |
| 8740 | static_branch_dec_cpuslocked(&sched_smt_present); |
| 8741 | #endif |
| 8742 | |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8743 | if (!sched_smp_initialized) |
| 8744 | return 0; |
| 8745 | |
| 8746 | ret = cpuset_cpu_inactive(cpu); |
| 8747 | if (ret) { |
Peter Zijlstra | 2558aac | 2020-09-11 09:54:27 +0200 | [diff] [blame] | 8748 | balance_push_set(cpu, false); |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8749 | set_cpu_active(cpu, true); |
| 8750 | return ret; |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8751 | } |
Thomas Gleixner | 40190a7 | 2016-03-10 12:54:13 +0100 | [diff] [blame] | 8752 | sched_domains_numa_masks_clear(cpu); |
| 8753 | return 0; |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8754 | } |
| 8755 | |
Thomas Gleixner | 94baf7a | 2016-03-10 12:54:15 +0100 | [diff] [blame] | 8756 | static void sched_rq_cpu_starting(unsigned int cpu) |
| 8757 | { |
| 8758 | struct rq *rq = cpu_rq(cpu); |
| 8759 | |
| 8760 | rq->calc_load_update = calc_load_update; |
Thomas Gleixner | 94baf7a | 2016-03-10 12:54:15 +0100 | [diff] [blame] | 8761 | update_max_interval(); |
| 8762 | } |
| 8763 | |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8764 | int sched_cpu_starting(unsigned int cpu) |
| 8765 | { |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 8766 | sched_core_cpu_starting(cpu); |
Thomas Gleixner | 94baf7a | 2016-03-10 12:54:15 +0100 | [diff] [blame] | 8767 | sched_rq_cpu_starting(cpu); |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 8768 | sched_tick_start(cpu); |
Thomas Gleixner | 135fb3e | 2016-03-10 12:54:11 +0100 | [diff] [blame] | 8769 | return 0; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8770 | } |
| 8771 | |
Thomas Gleixner | f2785dd | 2016-03-10 12:54:18 +0100 | [diff] [blame] | 8772 | #ifdef CONFIG_HOTPLUG_CPU |
Thomas Gleixner | 1cf12e0 | 2020-09-16 09:27:18 +0200 | [diff] [blame] | 8773 | |
| 8774 | /* |
| 8775 | * Invoked immediately before the stopper thread is invoked to bring the |
| 8776 | * CPU down completely. At this point all per CPU kthreads except the |
| 8777 | * hotplug thread (current) and the stopper thread (inactive) have been |
| 8778 | * either parked or have been unbound from the outgoing CPU. Ensure that |
| 8779 | * any of those which might be on the way out are gone. |
| 8780 | * |
| 8781 | * If after this point a bound task is being woken on this CPU then the |
| 8782 | * responsible hotplug callback has failed to do it's job. |
| 8783 | * sched_cpu_dying() will catch it with the appropriate fireworks. |
| 8784 | */ |
| 8785 | int sched_cpu_wait_empty(unsigned int cpu) |
| 8786 | { |
| 8787 | balance_hotplug_wait(); |
| 8788 | return 0; |
| 8789 | } |
| 8790 | |
| 8791 | /* |
| 8792 | * Since this CPU is going 'away' for a while, fold any nr_active delta we |
| 8793 | * might have. Called from the CPU stopper task after ensuring that the |
| 8794 | * stopper is the last running task on the CPU, so nr_active count is |
| 8795 | * stable. We need to take the teardown thread which is calling this into |
| 8796 | * account, so we hand in adjust = 1 to the load calculation. |
| 8797 | * |
| 8798 | * Also see the comment "Global load-average calculations". |
| 8799 | */ |
| 8800 | static void calc_load_migrate(struct rq *rq) |
| 8801 | { |
| 8802 | long delta = calc_load_fold_active(rq, 1); |
| 8803 | |
| 8804 | if (delta) |
| 8805 | atomic_long_add(delta, &calc_load_tasks); |
| 8806 | } |
| 8807 | |
Valentin Schneider | 36c6e17 | 2021-01-13 18:31:41 +0000 | [diff] [blame] | 8808 | static void dump_rq_tasks(struct rq *rq, const char *loglvl) |
| 8809 | { |
| 8810 | struct task_struct *g, *p; |
| 8811 | int cpu = cpu_of(rq); |
| 8812 | |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 8813 | lockdep_assert_rq_held(rq); |
Valentin Schneider | 36c6e17 | 2021-01-13 18:31:41 +0000 | [diff] [blame] | 8814 | |
| 8815 | printk("%sCPU%d enqueued tasks (%u total):\n", loglvl, cpu, rq->nr_running); |
| 8816 | for_each_process_thread(g, p) { |
| 8817 | if (task_cpu(p) != cpu) |
| 8818 | continue; |
| 8819 | |
| 8820 | if (!task_on_rq_queued(p)) |
| 8821 | continue; |
| 8822 | |
| 8823 | printk("%s\tpid: %d, name: %s\n", loglvl, p->pid, p->comm); |
| 8824 | } |
| 8825 | } |
| 8826 | |
Thomas Gleixner | f2785dd | 2016-03-10 12:54:18 +0100 | [diff] [blame] | 8827 | int sched_cpu_dying(unsigned int cpu) |
| 8828 | { |
| 8829 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 8830 | struct rq_flags rf; |
Thomas Gleixner | f2785dd | 2016-03-10 12:54:18 +0100 | [diff] [blame] | 8831 | |
| 8832 | /* Handle pending wakeups and then migrate everything off */ |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 8833 | sched_tick_stop(cpu); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 8834 | |
| 8835 | rq_lock_irqsave(rq, &rf); |
Valentin Schneider | 36c6e17 | 2021-01-13 18:31:41 +0000 | [diff] [blame] | 8836 | if (rq->nr_running != 1 || rq_has_pinned_tasks(rq)) { |
| 8837 | WARN(true, "Dying CPU not properly vacated!"); |
| 8838 | dump_rq_tasks(rq, KERN_WARNING); |
| 8839 | } |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 8840 | rq_unlock_irqrestore(rq, &rf); |
| 8841 | |
Thomas Gleixner | f2785dd | 2016-03-10 12:54:18 +0100 | [diff] [blame] | 8842 | calc_load_migrate(rq); |
| 8843 | update_max_interval(); |
Thomas Gleixner | e5ef27d | 2016-03-10 12:54:21 +0100 | [diff] [blame] | 8844 | hrtick_clear(rq); |
Thomas Gleixner | f2785dd | 2016-03-10 12:54:18 +0100 | [diff] [blame] | 8845 | return 0; |
| 8846 | } |
| 8847 | #endif |
| 8848 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8849 | void __init sched_init_smp(void) |
| 8850 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8851 | sched_init_numa(); |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 8852 | |
Peter Zijlstra | 6acce3e | 2013-10-11 14:38:20 +0200 | [diff] [blame] | 8853 | /* |
| 8854 | * There's no userspace yet to cause hotplug operations; hence all the |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 8855 | * CPU masks are stable and all blatant races in the below code cannot |
Valentin Schneider | b5a4e2b | 2018-12-19 18:23:16 +0000 | [diff] [blame] | 8856 | * happen. |
Peter Zijlstra | 6acce3e | 2013-10-11 14:38:20 +0200 | [diff] [blame] | 8857 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8858 | mutex_lock(&sched_domains_mutex); |
Peter Zijlstra | 8d5dc51 | 2017-04-25 15:29:40 +0200 | [diff] [blame] | 8859 | sched_init_domains(cpu_active_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8860 | mutex_unlock(&sched_domains_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8861 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8862 | /* Move init over to a non-isolated CPU */ |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 8863 | if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8864 | BUG(); |
Peter Zijlstra | 15faafc | 2021-05-31 12:21:13 +0200 | [diff] [blame^] | 8865 | current->flags &= ~PF_NO_SETAFFINITY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8866 | sched_init_granularity(); |
Rusty Russell | 4212823 | 2008-11-25 02:35:12 +1030 | [diff] [blame] | 8867 | |
Rusty Russell | 0e3900e | 2008-11-25 02:35:13 +1030 | [diff] [blame] | 8868 | init_sched_rt_class(); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 8869 | init_sched_dl_class(); |
Peter Zijlstra | 1b568f0 | 2016-05-09 10:38:41 +0200 | [diff] [blame] | 8870 | |
Thomas Gleixner | e26fbff | 2016-03-10 12:54:10 +0100 | [diff] [blame] | 8871 | sched_smp_initialized = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8872 | } |
Thomas Gleixner | e26fbff | 2016-03-10 12:54:10 +0100 | [diff] [blame] | 8873 | |
| 8874 | static int __init migration_init(void) |
| 8875 | { |
Nicholas Piggin | 77a5352 | 2019-04-11 13:34:44 +1000 | [diff] [blame] | 8876 | sched_cpu_starting(smp_processor_id()); |
Thomas Gleixner | e26fbff | 2016-03-10 12:54:10 +0100 | [diff] [blame] | 8877 | return 0; |
| 8878 | } |
| 8879 | early_initcall(migration_init); |
| 8880 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8881 | #else |
| 8882 | void __init sched_init_smp(void) |
| 8883 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8884 | sched_init_granularity(); |
| 8885 | } |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 8886 | #endif /* CONFIG_SMP */ |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 8887 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 8888 | int in_sched_functions(unsigned long addr) |
| 8889 | { |
| 8890 | return in_lock_functions(addr) || |
| 8891 | (addr >= (unsigned long)__sched_text_start |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 8892 | && addr < (unsigned long)__sched_text_end); |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 8893 | } |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 8894 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 8895 | #ifdef CONFIG_CGROUP_SCHED |
Li Zefan | 27b4b93 | 2013-03-05 16:07:52 +0800 | [diff] [blame] | 8896 | /* |
| 8897 | * Default task group. |
| 8898 | * Every task in system belongs to this group at bootup. |
| 8899 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 8900 | struct task_group root_task_group; |
Mike Galbraith | 35cf4e5 | 2012-08-07 05:00:13 +0200 | [diff] [blame] | 8901 | LIST_HEAD(task_groups); |
Waiman Long | b036762 | 2015-12-02 13:41:49 -0500 | [diff] [blame] | 8902 | |
| 8903 | /* Cacheline aligned slab cache for task_group */ |
| 8904 | static struct kmem_cache *task_group_cache __read_mostly; |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 8905 | #endif |
| 8906 | |
Joonsoo Kim | e6252c3 | 2013-04-23 17:27:41 +0900 | [diff] [blame] | 8907 | DECLARE_PER_CPU(cpumask_var_t, load_balance_mask); |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 8908 | DECLARE_PER_CPU(cpumask_var_t, select_idle_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8909 | |
| 8910 | void __init sched_init(void) |
| 8911 | { |
Qian Cai | a1dc044 | 2019-07-19 21:23:19 -0400 | [diff] [blame] | 8912 | unsigned long ptr = 0; |
Dietmar Eggemann | 55627e3 | 2019-05-27 07:21:13 +0100 | [diff] [blame] | 8913 | int i; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8914 | |
Steven Rostedt (VMware) | c3a340f | 2019-12-19 16:44:53 -0500 | [diff] [blame] | 8915 | /* Make sure the linker didn't screw up */ |
| 8916 | BUG_ON(&idle_sched_class + 1 != &fair_sched_class || |
| 8917 | &fair_sched_class + 1 != &rt_sched_class || |
| 8918 | &rt_sched_class + 1 != &dl_sched_class); |
| 8919 | #ifdef CONFIG_SMP |
| 8920 | BUG_ON(&dl_sched_class + 1 != &stop_sched_class); |
| 8921 | #endif |
| 8922 | |
Ingo Molnar | 5822a45 | 2017-03-05 13:09:07 +0100 | [diff] [blame] | 8923 | wait_bit_init(); |
Linus Torvalds | 9dcb8b6 | 2016-10-26 10:15:30 -0700 | [diff] [blame] | 8924 | |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8925 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Qian Cai | a1dc044 | 2019-07-19 21:23:19 -0400 | [diff] [blame] | 8926 | ptr += 2 * nr_cpu_ids * sizeof(void **); |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8927 | #endif |
| 8928 | #ifdef CONFIG_RT_GROUP_SCHED |
Qian Cai | a1dc044 | 2019-07-19 21:23:19 -0400 | [diff] [blame] | 8929 | ptr += 2 * nr_cpu_ids * sizeof(void **); |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8930 | #endif |
Qian Cai | a1dc044 | 2019-07-19 21:23:19 -0400 | [diff] [blame] | 8931 | if (ptr) { |
| 8932 | ptr = (unsigned long)kzalloc(ptr, GFP_NOWAIT); |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8933 | |
| 8934 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 8935 | root_task_group.se = (struct sched_entity **)ptr; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8936 | ptr += nr_cpu_ids * sizeof(void **); |
| 8937 | |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 8938 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8939 | ptr += nr_cpu_ids * sizeof(void **); |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8940 | |
Wei Yang | b1d1779 | 2020-04-23 21:44:43 +0000 | [diff] [blame] | 8941 | root_task_group.shares = ROOT_TASK_GROUP_LOAD; |
| 8942 | init_cfs_bandwidth(&root_task_group.cfs_bandwidth); |
Dhaval Giani | 6d6bc0a | 2008-05-30 14:23:45 +0200 | [diff] [blame] | 8943 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8944 | #ifdef CONFIG_RT_GROUP_SCHED |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 8945 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8946 | ptr += nr_cpu_ids * sizeof(void **); |
| 8947 | |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 8948 | root_task_group.rt_rq = (struct rt_rq **)ptr; |
Peter Zijlstra | eff766a | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 8949 | ptr += nr_cpu_ids * sizeof(void **); |
| 8950 | |
Dhaval Giani | 6d6bc0a | 2008-05-30 14:23:45 +0200 | [diff] [blame] | 8951 | #endif /* CONFIG_RT_GROUP_SCHED */ |
Mike Travis | 434d53b | 2008-04-04 18:11:04 -0700 | [diff] [blame] | 8952 | } |
Alex Thorlton | b74e627 | 2014-12-18 12:44:30 -0600 | [diff] [blame] | 8953 | #ifdef CONFIG_CPUMASK_OFFSTACK |
| 8954 | for_each_possible_cpu(i) { |
| 8955 | per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node( |
| 8956 | cpumask_size(), GFP_KERNEL, cpu_to_node(i)); |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 8957 | per_cpu(select_idle_mask, i) = (cpumask_var_t)kzalloc_node( |
| 8958 | cpumask_size(), GFP_KERNEL, cpu_to_node(i)); |
Alex Thorlton | b74e627 | 2014-12-18 12:44:30 -0600 | [diff] [blame] | 8959 | } |
| 8960 | #endif /* CONFIG_CPUMASK_OFFSTACK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8961 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 8962 | init_rt_bandwidth(&def_rt_bandwidth, global_rt_period(), global_rt_runtime()); |
| 8963 | init_dl_bandwidth(&def_dl_bandwidth, global_rt_period(), global_rt_runtime()); |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 8964 | |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 8965 | #ifdef CONFIG_SMP |
| 8966 | init_defrootdomain(); |
| 8967 | #endif |
| 8968 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8969 | #ifdef CONFIG_RT_GROUP_SCHED |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 8970 | init_rt_bandwidth(&root_task_group.rt_bandwidth, |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8971 | global_rt_period(), global_rt_runtime()); |
Dhaval Giani | 6d6bc0a | 2008-05-30 14:23:45 +0200 | [diff] [blame] | 8972 | #endif /* CONFIG_RT_GROUP_SCHED */ |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 8973 | |
Dhaval Giani | 7c94143 | 2010-01-20 13:26:18 +0100 | [diff] [blame] | 8974 | #ifdef CONFIG_CGROUP_SCHED |
Waiman Long | b036762 | 2015-12-02 13:41:49 -0500 | [diff] [blame] | 8975 | task_group_cache = KMEM_CACHE(task_group, 0); |
| 8976 | |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 8977 | list_add(&root_task_group.list, &task_groups); |
| 8978 | INIT_LIST_HEAD(&root_task_group.children); |
Glauber Costa | f4d6f6c | 2011-11-01 19:19:07 -0200 | [diff] [blame] | 8979 | INIT_LIST_HEAD(&root_task_group.siblings); |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 8980 | autogroup_init(&init_task); |
Dhaval Giani | 7c94143 | 2010-01-20 13:26:18 +0100 | [diff] [blame] | 8981 | #endif /* CONFIG_CGROUP_SCHED */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8982 | |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8983 | for_each_possible_cpu(i) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 8984 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8985 | |
| 8986 | rq = cpu_rq(i); |
Peter Zijlstra | 5cb9eaa | 2020-11-17 18:19:31 -0500 | [diff] [blame] | 8987 | raw_spin_lock_init(&rq->__lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8988 | rq->nr_running = 0; |
Thomas Gleixner | dce48a8 | 2009-04-11 10:43:41 +0200 | [diff] [blame] | 8989 | rq->calc_load_active = 0; |
| 8990 | rq->calc_load_update = jiffies + LOAD_FREQ; |
Jan H. Schönherr | acb5a9b | 2011-07-14 18:32:43 +0200 | [diff] [blame] | 8991 | init_cfs_rq(&rq->cfs); |
Abel Vesa | 07c54f7 | 2015-03-03 13:50:27 +0200 | [diff] [blame] | 8992 | init_rt_rq(&rq->rt); |
| 8993 | init_dl_rq(&rq->dl); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8994 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 8995 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
Vincent Guittot | 9c2791f | 2016-11-08 10:53:43 +0100 | [diff] [blame] | 8996 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8997 | /* |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 8998 | * How much CPU bandwidth does root_task_group get? |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 8999 | * |
| 9000 | * In case of task-groups formed thr' the cgroup filesystem, it |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9001 | * gets 100% of the CPU resources in the system. This overall |
| 9002 | * system CPU resource is divided among the tasks of |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 9003 | * root_task_group and its child task-groups in a fair manner, |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9004 | * based on each entity's (task or task-group's) weight |
| 9005 | * (se->load.weight). |
| 9006 | * |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 9007 | * In other words, if root_task_group has 10 tasks of weight |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9008 | * 1024) and two child groups A0 and A1 (of weight 1024 each), |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9009 | * then A0's share of the CPU resource is: |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9010 | * |
Ingo Molnar | 0d905bc | 2009-05-04 19:13:30 +0200 | [diff] [blame] | 9011 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9012 | * |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 9013 | * We achieve this by letting root_task_group's tasks sit |
| 9014 | * directly in rq->cfs (i.e root_task_group->se[] = NULL). |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9015 | */ |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 9016 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9017 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 9018 | |
| 9019 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9020 | #ifdef CONFIG_RT_GROUP_SCHED |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 9021 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9022 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9023 | #ifdef CONFIG_SMP |
Peter Zijlstra | a4c410f | 2006-12-06 20:37:21 -0800 | [diff] [blame] | 9024 | rq->sd = NULL; |
Gregory Haskins | 57d885f | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 9025 | rq->rd = NULL; |
Vincent Guittot | ca6d75e | 2015-02-27 16:54:09 +0100 | [diff] [blame] | 9026 | rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE; |
Peter Zijlstra | b5c4477 | 2021-01-21 16:09:32 +0100 | [diff] [blame] | 9027 | rq->balance_callback = &balance_push_callback; |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 9028 | rq->active_balance = 0; |
| 9029 | rq->next_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9030 | rq->push_cpu = 0; |
| 9031 | rq->cpu = i; |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 9032 | rq->online = 0; |
Mike Galbraith | eae0c9d | 2009-11-10 03:50:02 +0100 | [diff] [blame] | 9033 | rq->idle_stamp = 0; |
| 9034 | rq->avg_idle = 2*sysctl_sched_migration_cost; |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 9035 | rq->max_idle_balance_cost = sysctl_sched_migration_cost; |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 9036 | |
| 9037 | INIT_LIST_HEAD(&rq->cfs_tasks); |
| 9038 | |
Gregory Haskins | dc93852 | 2008-01-25 21:08:26 +0100 | [diff] [blame] | 9039 | rq_attach_root(rq, &def_root_domain); |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 9040 | #ifdef CONFIG_NO_HZ_COMMON |
Peter Zijlstra | e022e0d | 2017-12-21 11:20:23 +0100 | [diff] [blame] | 9041 | rq->last_blocked_load_update_tick = jiffies; |
Peter Zijlstra | a22e47a | 2017-12-21 10:01:24 +0100 | [diff] [blame] | 9042 | atomic_set(&rq->nohz_flags, 0); |
Peter Zijlstra (Intel) | 90b5363 | 2020-03-27 11:44:56 +0100 | [diff] [blame] | 9043 | |
Peter Zijlstra | 545b8c8 | 2020-06-15 11:29:31 +0200 | [diff] [blame] | 9044 | INIT_CSD(&rq->nohz_csd, nohz_csd_func, rq); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 9045 | #endif |
Thomas Gleixner | f2469a1 | 2020-09-14 14:47:28 +0200 | [diff] [blame] | 9046 | #ifdef CONFIG_HOTPLUG_CPU |
| 9047 | rcuwait_init(&rq->hotplug_wait); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 9048 | #endif |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 9049 | #endif /* CONFIG_SMP */ |
Frederic Weisbecker | 77a021b | 2018-02-21 05:17:23 +0100 | [diff] [blame] | 9050 | hrtick_rq_init(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9051 | atomic_set(&rq->nr_iowait, 0); |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 9052 | |
| 9053 | #ifdef CONFIG_SCHED_CORE |
| 9054 | rq->core = NULL; |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 9055 | rq->core_pick = NULL; |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 9056 | rq->core_enabled = 0; |
Peter Zijlstra | 539f651 | 2020-11-17 18:19:37 -0500 | [diff] [blame] | 9057 | rq->core_tree = RB_ROOT; |
| 9058 | rq->core_forceidle = false; |
| 9059 | |
| 9060 | rq->core_cookie = 0UL; |
Peter Zijlstra | 9edeaea | 2020-11-17 18:19:34 -0500 | [diff] [blame] | 9061 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9062 | } |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 9063 | |
Vincent Guittot | 9059393 | 2017-05-17 11:50:45 +0200 | [diff] [blame] | 9064 | set_load_weight(&init_task, false); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 9065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9066 | /* |
| 9067 | * The boot idle thread does lazy MMU switching as well: |
| 9068 | */ |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 9069 | mmgrab(&init_mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9070 | enter_lazy_tlb(&init_mm, current); |
| 9071 | |
| 9072 | /* |
| 9073 | * Make us the idle thread. Technically, schedule() should not be |
| 9074 | * called from this thread, however somewhere below it might be, |
| 9075 | * but because we are the idle thread, we just pick up running again |
| 9076 | * when this runqueue becomes "idle". |
| 9077 | */ |
| 9078 | init_idle(current, smp_processor_id()); |
Thomas Gleixner | dce48a8 | 2009-04-11 10:43:41 +0200 | [diff] [blame] | 9079 | |
| 9080 | calc_load_update = jiffies + LOAD_FREQ; |
| 9081 | |
Rusty Russell | bf4d83f | 2008-11-25 09:57:51 +1030 | [diff] [blame] | 9082 | #ifdef CONFIG_SMP |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 9083 | idle_thread_set_boot_cpu(); |
Peter Zijlstra | b5c4477 | 2021-01-21 16:09:32 +0100 | [diff] [blame] | 9084 | balance_push_set(smp_processor_id(), false); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9085 | #endif |
| 9086 | init_sched_fair_class(); |
Rusty Russell | 6a7b3dc | 2008-11-25 02:35:04 +1030 | [diff] [blame] | 9087 | |
Josh Poimboeuf | 4698f88 | 2016-06-07 14:43:16 -0500 | [diff] [blame] | 9088 | init_schedstats(); |
| 9089 | |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 9090 | psi_init(); |
| 9091 | |
Patrick Bellasi | 69842cb | 2019-06-21 09:42:02 +0100 | [diff] [blame] | 9092 | init_uclamp(); |
| 9093 | |
Ingo Molnar | 6892b75 | 2008-02-13 14:02:36 +0100 | [diff] [blame] | 9094 | scheduler_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9095 | } |
| 9096 | |
Frederic Weisbecker | d902db1 | 2011-06-08 19:31:56 +0200 | [diff] [blame] | 9097 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
Frederic Weisbecker | e4aafea | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 9098 | static inline int preempt_count_equals(int preempt_offset) |
| 9099 | { |
Peter Zijlstra | da7142e | 2015-09-28 18:11:45 +0200 | [diff] [blame] | 9100 | int nested = preempt_count() + rcu_preempt_depth(); |
Frederic Weisbecker | e4aafea | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 9101 | |
Arnd Bergmann | 4ba8216 | 2011-01-25 22:52:22 +0100 | [diff] [blame] | 9102 | return (nested == preempt_offset); |
Frederic Weisbecker | e4aafea | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 9103 | } |
| 9104 | |
Simon Kagstrom | d894837 | 2009-12-23 11:08:18 +0100 | [diff] [blame] | 9105 | void __might_sleep(const char *file, int line, int preempt_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9106 | { |
Peter Zijlstra | 8eb23b9 | 2014-09-24 10:18:55 +0200 | [diff] [blame] | 9107 | /* |
| 9108 | * Blocking primitives will set (and therefore destroy) current->state, |
| 9109 | * since we will exit with TASK_RUNNING make sure we enter with it, |
| 9110 | * otherwise we will destroy state. |
| 9111 | */ |
Linus Torvalds | 00845eb | 2015-02-01 12:23:32 -0800 | [diff] [blame] | 9112 | WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change, |
Peter Zijlstra | 8eb23b9 | 2014-09-24 10:18:55 +0200 | [diff] [blame] | 9113 | "do not call blocking ops when !TASK_RUNNING; " |
| 9114 | "state=%lx set at [<%p>] %pS\n", |
| 9115 | current->state, |
| 9116 | (void *)current->task_state_change, |
Linus Torvalds | 00845eb | 2015-02-01 12:23:32 -0800 | [diff] [blame] | 9117 | (void *)current->task_state_change); |
Peter Zijlstra | 8eb23b9 | 2014-09-24 10:18:55 +0200 | [diff] [blame] | 9118 | |
Peter Zijlstra | 3427445 | 2014-09-24 10:18:56 +0200 | [diff] [blame] | 9119 | ___might_sleep(file, line, preempt_offset); |
| 9120 | } |
| 9121 | EXPORT_SYMBOL(__might_sleep); |
| 9122 | |
| 9123 | void ___might_sleep(const char *file, int line, int preempt_offset) |
| 9124 | { |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9125 | /* Ratelimiting timestamp: */ |
| 9126 | static unsigned long prev_jiffy; |
| 9127 | |
Vegard Nossum | d1c6d14 | 2016-07-23 09:46:39 +0200 | [diff] [blame] | 9128 | unsigned long preempt_disable_ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9129 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9130 | /* WARN_ON_ONCE() by default, no rate limit required: */ |
| 9131 | rcu_sleep_check(); |
| 9132 | |
Thomas Gleixner | db273be | 2014-02-07 20:58:38 +0100 | [diff] [blame] | 9133 | if ((preempt_count_equals(preempt_offset) && !irqs_disabled() && |
Daniel Vetter | 312364f3 | 2019-08-26 22:14:23 +0200 | [diff] [blame] | 9134 | !is_idle_task(current) && !current->non_block_count) || |
Thomas Gleixner | 1c3c5ea | 2017-05-16 20:42:48 +0200 | [diff] [blame] | 9135 | system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING || |
| 9136 | oops_in_progress) |
Ingo Molnar | aef745f | 2008-08-28 11:34:43 +0200 | [diff] [blame] | 9137 | return; |
Thomas Gleixner | 1c3c5ea | 2017-05-16 20:42:48 +0200 | [diff] [blame] | 9138 | |
Ingo Molnar | aef745f | 2008-08-28 11:34:43 +0200 | [diff] [blame] | 9139 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 9140 | return; |
| 9141 | prev_jiffy = jiffies; |
| 9142 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9143 | /* Save this before calling printk(), since that will clobber it: */ |
Vegard Nossum | d1c6d14 | 2016-07-23 09:46:39 +0200 | [diff] [blame] | 9144 | preempt_disable_ip = get_preempt_disable_ip(current); |
| 9145 | |
Peter Zijlstra | 3df0fc5 | 2009-12-20 14:23:57 +0100 | [diff] [blame] | 9146 | printk(KERN_ERR |
| 9147 | "BUG: sleeping function called from invalid context at %s:%d\n", |
| 9148 | file, line); |
| 9149 | printk(KERN_ERR |
Daniel Vetter | 312364f3 | 2019-08-26 22:14:23 +0200 | [diff] [blame] | 9150 | "in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n", |
| 9151 | in_atomic(), irqs_disabled(), current->non_block_count, |
Peter Zijlstra | 3df0fc5 | 2009-12-20 14:23:57 +0100 | [diff] [blame] | 9152 | current->pid, current->comm); |
Ingo Molnar | aef745f | 2008-08-28 11:34:43 +0200 | [diff] [blame] | 9153 | |
Eric Sandeen | a8b686b | 2014-12-16 16:25:28 -0600 | [diff] [blame] | 9154 | if (task_stack_end_corrupted(current)) |
| 9155 | printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); |
| 9156 | |
Ingo Molnar | aef745f | 2008-08-28 11:34:43 +0200 | [diff] [blame] | 9157 | debug_show_held_locks(current); |
| 9158 | if (irqs_disabled()) |
| 9159 | print_irqtrace_events(current); |
Vegard Nossum | d1c6d14 | 2016-07-23 09:46:39 +0200 | [diff] [blame] | 9160 | if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) |
| 9161 | && !preempt_count_equals(preempt_offset)) { |
Thomas Gleixner | 8f47b18 | 2014-02-07 20:58:39 +0100 | [diff] [blame] | 9162 | pr_err("Preemption disabled at:"); |
Dmitry Safonov | 2062a4e | 2020-06-08 21:29:56 -0700 | [diff] [blame] | 9163 | print_ip_sym(KERN_ERR, preempt_disable_ip); |
Thomas Gleixner | 8f47b18 | 2014-02-07 20:58:39 +0100 | [diff] [blame] | 9164 | } |
Ingo Molnar | aef745f | 2008-08-28 11:34:43 +0200 | [diff] [blame] | 9165 | dump_stack(); |
Vegard Nossum | f0b22e3 | 2016-07-22 21:46:02 +0200 | [diff] [blame] | 9166 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9167 | } |
Peter Zijlstra | 3427445 | 2014-09-24 10:18:56 +0200 | [diff] [blame] | 9168 | EXPORT_SYMBOL(___might_sleep); |
Peter Zijlstra | 568f196 | 2019-01-28 17:21:52 -0800 | [diff] [blame] | 9169 | |
| 9170 | void __cant_sleep(const char *file, int line, int preempt_offset) |
| 9171 | { |
| 9172 | static unsigned long prev_jiffy; |
| 9173 | |
| 9174 | if (irqs_disabled()) |
| 9175 | return; |
| 9176 | |
| 9177 | if (!IS_ENABLED(CONFIG_PREEMPT_COUNT)) |
| 9178 | return; |
| 9179 | |
| 9180 | if (preempt_count() > preempt_offset) |
| 9181 | return; |
| 9182 | |
| 9183 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 9184 | return; |
| 9185 | prev_jiffy = jiffies; |
| 9186 | |
| 9187 | printk(KERN_ERR "BUG: assuming atomic context at %s:%d\n", file, line); |
| 9188 | printk(KERN_ERR "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n", |
| 9189 | in_atomic(), irqs_disabled(), |
| 9190 | current->pid, current->comm); |
| 9191 | |
| 9192 | debug_show_held_locks(current); |
| 9193 | dump_stack(); |
| 9194 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
| 9195 | } |
| 9196 | EXPORT_SYMBOL_GPL(__cant_sleep); |
Thomas Gleixner | 74d862b6 | 2020-11-18 20:48:42 +0100 | [diff] [blame] | 9197 | |
| 9198 | #ifdef CONFIG_SMP |
| 9199 | void __cant_migrate(const char *file, int line) |
| 9200 | { |
| 9201 | static unsigned long prev_jiffy; |
| 9202 | |
| 9203 | if (irqs_disabled()) |
| 9204 | return; |
| 9205 | |
| 9206 | if (is_migration_disabled(current)) |
| 9207 | return; |
| 9208 | |
| 9209 | if (!IS_ENABLED(CONFIG_PREEMPT_COUNT)) |
| 9210 | return; |
| 9211 | |
| 9212 | if (preempt_count() > 0) |
| 9213 | return; |
| 9214 | |
| 9215 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 9216 | return; |
| 9217 | prev_jiffy = jiffies; |
| 9218 | |
| 9219 | pr_err("BUG: assuming non migratable context at %s:%d\n", file, line); |
| 9220 | pr_err("in_atomic(): %d, irqs_disabled(): %d, migration_disabled() %u pid: %d, name: %s\n", |
| 9221 | in_atomic(), irqs_disabled(), is_migration_disabled(current), |
| 9222 | current->pid, current->comm); |
| 9223 | |
| 9224 | debug_show_held_locks(current); |
| 9225 | dump_stack(); |
| 9226 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
| 9227 | } |
| 9228 | EXPORT_SYMBOL_GPL(__cant_migrate); |
| 9229 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9230 | #endif |
| 9231 | |
| 9232 | #ifdef CONFIG_MAGIC_SYSRQ |
| 9233 | void normalize_rt_tasks(void) |
| 9234 | { |
| 9235 | struct task_struct *g, *p; |
Peter Zijlstra | dbc7f06 | 2015-06-11 14:46:38 +0200 | [diff] [blame] | 9236 | struct sched_attr attr = { |
| 9237 | .sched_policy = SCHED_NORMAL, |
| 9238 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9239 | |
Oleg Nesterov | 3472eaa | 2014-09-21 21:33:38 +0200 | [diff] [blame] | 9240 | read_lock(&tasklist_lock); |
Oleg Nesterov | 5d07f42 | 2014-08-13 21:19:53 +0200 | [diff] [blame] | 9241 | for_each_process_thread(g, p) { |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 9242 | /* |
| 9243 | * Only normalize user tasks: |
| 9244 | */ |
Oleg Nesterov | 3472eaa | 2014-09-21 21:33:38 +0200 | [diff] [blame] | 9245 | if (p->flags & PF_KTHREAD) |
Ingo Molnar | 178be79 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 9246 | continue; |
| 9247 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 9248 | p->se.exec_start = 0; |
| 9249 | schedstat_set(p->se.statistics.wait_start, 0); |
| 9250 | schedstat_set(p->se.statistics.sleep_start, 0); |
| 9251 | schedstat_set(p->se.statistics.block_start, 0); |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 9252 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 9253 | if (!dl_task(p) && !rt_task(p)) { |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 9254 | /* |
| 9255 | * Renice negative nice level userspace |
| 9256 | * tasks back to 0: |
| 9257 | */ |
Oleg Nesterov | 3472eaa | 2014-09-21 21:33:38 +0200 | [diff] [blame] | 9258 | if (task_nice(p) < 0) |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 9259 | set_user_nice(p, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9260 | continue; |
Ingo Molnar | dd41f59 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 9261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9262 | |
Peter Zijlstra | dbc7f06 | 2015-06-11 14:46:38 +0200 | [diff] [blame] | 9263 | __sched_setscheduler(p, &attr, false, false); |
Oleg Nesterov | 5d07f42 | 2014-08-13 21:19:53 +0200 | [diff] [blame] | 9264 | } |
Oleg Nesterov | 3472eaa | 2014-09-21 21:33:38 +0200 | [diff] [blame] | 9265 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9266 | } |
| 9267 | |
| 9268 | #endif /* CONFIG_MAGIC_SYSRQ */ |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9269 | |
Jason Wessel | 67fc4e0 | 2010-05-20 21:04:21 -0500 | [diff] [blame] | 9270 | #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9271 | /* |
Jason Wessel | 67fc4e0 | 2010-05-20 21:04:21 -0500 | [diff] [blame] | 9272 | * These functions are only useful for the IA64 MCA handling, or kdb. |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9273 | * |
| 9274 | * They can only be called when the whole system has been |
| 9275 | * stopped - every CPU needs to be quiescent, and no scheduling |
| 9276 | * activity can take place. Using them for anything else would |
| 9277 | * be a serious bug, and as a result, they aren't even visible |
| 9278 | * under any other configuration. |
| 9279 | */ |
| 9280 | |
| 9281 | /** |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9282 | * curr_task - return the current task for a given CPU. |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9283 | * @cpu: the processor in question. |
| 9284 | * |
| 9285 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 9286 | * |
| 9287 | * Return: The current task for @cpu. |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9288 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 9289 | struct task_struct *curr_task(int cpu) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9290 | { |
| 9291 | return cpu_curr(cpu); |
| 9292 | } |
| 9293 | |
Jason Wessel | 67fc4e0 | 2010-05-20 21:04:21 -0500 | [diff] [blame] | 9294 | #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */ |
| 9295 | |
| 9296 | #ifdef CONFIG_IA64 |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9297 | /** |
Peter Zijlstra | 5feeb78 | 2019-05-29 20:36:38 +0000 | [diff] [blame] | 9298 | * ia64_set_curr_task - set the current task for a given CPU. |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9299 | * @cpu: the processor in question. |
| 9300 | * @p: the task pointer to set. |
| 9301 | * |
| 9302 | * Description: This function must only be used when non-maskable interrupts |
Ingo Molnar | 41a2d6c | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 9303 | * are serviced on a separate stack. It allows the architecture to switch the |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9304 | * notion of the current task on a CPU in a non-blocking manner. This function |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9305 | * must be called with all CPU's synchronized, and interrupts disabled, the |
| 9306 | * and caller must save the original value of the current task (see |
| 9307 | * curr_task() above) and restore that value before reenabling interrupts and |
| 9308 | * re-starting the system. |
| 9309 | * |
| 9310 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 9311 | */ |
Peter Zijlstra | a458ae2 | 2016-09-20 20:29:40 +0200 | [diff] [blame] | 9312 | void ia64_set_curr_task(int cpu, struct task_struct *p) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 9313 | { |
| 9314 | cpu_curr(cpu) = p; |
| 9315 | } |
| 9316 | |
| 9317 | #endif |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9318 | |
Dhaval Giani | 7c94143 | 2010-01-20 13:26:18 +0100 | [diff] [blame] | 9319 | #ifdef CONFIG_CGROUP_SCHED |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9320 | /* task_group_lock serializes the addition/removal of task groups */ |
| 9321 | static DEFINE_SPINLOCK(task_group_lock); |
| 9322 | |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 9323 | static inline void alloc_uclamp_sched_group(struct task_group *tg, |
| 9324 | struct task_group *parent) |
| 9325 | { |
| 9326 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 9327 | enum uclamp_id clamp_id; |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 9328 | |
| 9329 | for_each_clamp_id(clamp_id) { |
| 9330 | uclamp_se_set(&tg->uclamp_req[clamp_id], |
| 9331 | uclamp_none(clamp_id), false); |
Patrick Bellasi | 0b60ba2 | 2019-08-22 14:28:07 +0100 | [diff] [blame] | 9332 | tg->uclamp[clamp_id] = parent->uclamp[clamp_id]; |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 9333 | } |
| 9334 | #endif |
| 9335 | } |
| 9336 | |
Peter Zijlstra | 2f5177f | 2016-03-16 16:22:45 +0100 | [diff] [blame] | 9337 | static void sched_free_group(struct task_group *tg) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9338 | { |
| 9339 | free_fair_sched_group(tg); |
| 9340 | free_rt_sched_group(tg); |
Mike Galbraith | e9aa1dd | 2011-01-05 11:11:25 +0100 | [diff] [blame] | 9341 | autogroup_free(tg); |
Waiman Long | b036762 | 2015-12-02 13:41:49 -0500 | [diff] [blame] | 9342 | kmem_cache_free(task_group_cache, tg); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9343 | } |
| 9344 | |
| 9345 | /* allocate runqueue etc for a new task group */ |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9346 | struct task_group *sched_create_group(struct task_group *parent) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9347 | { |
| 9348 | struct task_group *tg; |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9349 | |
Waiman Long | b036762 | 2015-12-02 13:41:49 -0500 | [diff] [blame] | 9350 | tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO); |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9351 | if (!tg) |
| 9352 | return ERR_PTR(-ENOMEM); |
| 9353 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9354 | if (!alloc_fair_sched_group(tg, parent)) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9355 | goto err; |
| 9356 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9357 | if (!alloc_rt_sched_group(tg, parent)) |
Peter Zijlstra | bccbe08 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9358 | goto err; |
| 9359 | |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 9360 | alloc_uclamp_sched_group(tg, parent); |
| 9361 | |
Li Zefan | ace783b | 2013-01-24 14:30:48 +0800 | [diff] [blame] | 9362 | return tg; |
| 9363 | |
| 9364 | err: |
Peter Zijlstra | 2f5177f | 2016-03-16 16:22:45 +0100 | [diff] [blame] | 9365 | sched_free_group(tg); |
Li Zefan | ace783b | 2013-01-24 14:30:48 +0800 | [diff] [blame] | 9366 | return ERR_PTR(-ENOMEM); |
| 9367 | } |
| 9368 | |
| 9369 | void sched_online_group(struct task_group *tg, struct task_group *parent) |
| 9370 | { |
| 9371 | unsigned long flags; |
| 9372 | |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9373 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9374 | list_add_rcu(&tg->list, &task_groups); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 9375 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9376 | /* Root should already exist: */ |
| 9377 | WARN_ON(!parent); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 9378 | |
| 9379 | tg->parent = parent; |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 9380 | INIT_LIST_HEAD(&tg->children); |
Zhang, Yanmin | 09f2724 | 2030-08-14 15:56:40 +0800 | [diff] [blame] | 9381 | list_add_rcu(&tg->siblings, &parent->children); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9382 | spin_unlock_irqrestore(&task_group_lock, flags); |
Peter Zijlstra | 8663e24 | 2016-06-22 14:58:02 +0200 | [diff] [blame] | 9383 | |
| 9384 | online_fair_sched_group(tg); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9385 | } |
| 9386 | |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 9387 | /* rcu callback to free various structures associated with a task group */ |
Peter Zijlstra | 2f5177f | 2016-03-16 16:22:45 +0100 | [diff] [blame] | 9388 | static void sched_free_group_rcu(struct rcu_head *rhp) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9389 | { |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9390 | /* Now it should be safe to free those cfs_rqs: */ |
Peter Zijlstra | 2f5177f | 2016-03-16 16:22:45 +0100 | [diff] [blame] | 9391 | sched_free_group(container_of(rhp, struct task_group, rcu)); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9392 | } |
| 9393 | |
Ingo Molnar | 4cf86d7 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 9394 | void sched_destroy_group(struct task_group *tg) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9395 | { |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9396 | /* Wait for possible concurrent references to cfs_rqs complete: */ |
Peter Zijlstra | 2f5177f | 2016-03-16 16:22:45 +0100 | [diff] [blame] | 9397 | call_rcu(&tg->rcu, sched_free_group_rcu); |
Li Zefan | ace783b | 2013-01-24 14:30:48 +0800 | [diff] [blame] | 9398 | } |
| 9399 | |
| 9400 | void sched_offline_group(struct task_group *tg) |
| 9401 | { |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9402 | unsigned long flags; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9403 | |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9404 | /* End participation in shares distribution: */ |
Peter Zijlstra | 6fe1f34 | 2016-01-21 22:24:16 +0100 | [diff] [blame] | 9405 | unregister_fair_sched_group(tg); |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 9406 | |
| 9407 | spin_lock_irqsave(&task_group_lock, flags); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 9408 | list_del_rcu(&tg->list); |
Peter Zijlstra | f473aa5 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 9409 | list_del_rcu(&tg->siblings); |
Peter Zijlstra | 8ed3699 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 9410 | spin_unlock_irqrestore(&task_group_lock, flags); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9411 | } |
| 9412 | |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 9413 | static void sched_change_group(struct task_struct *tsk, int type) |
| 9414 | { |
| 9415 | struct task_group *tg; |
| 9416 | |
| 9417 | /* |
| 9418 | * All callers are synchronized by task_rq_lock(); we do not use RCU |
| 9419 | * which is pointless here. Thus, we pass "true" to task_css_check() |
| 9420 | * to prevent lockdep warnings. |
| 9421 | */ |
| 9422 | tg = container_of(task_css_check(tsk, cpu_cgrp_id, true), |
| 9423 | struct task_group, css); |
| 9424 | tg = autogroup_task_group(tsk, tg); |
| 9425 | tsk->sched_task_group = tg; |
| 9426 | |
| 9427 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 9428 | if (tsk->sched_class->task_change_group) |
| 9429 | tsk->sched_class->task_change_group(tsk, type); |
| 9430 | else |
| 9431 | #endif |
| 9432 | set_task_rq(tsk, task_cpu(tsk)); |
| 9433 | } |
| 9434 | |
| 9435 | /* |
| 9436 | * Change task's runqueue when it moves between groups. |
| 9437 | * |
| 9438 | * The caller of this function should have put the task in its new group by |
| 9439 | * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect |
| 9440 | * its new group. |
Srivatsa Vaddagiri | 9b5b775 | 2007-10-15 17:00:09 +0200 | [diff] [blame] | 9441 | */ |
| 9442 | void sched_move_task(struct task_struct *tsk) |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9443 | { |
Peter Zijlstra | 7a57f32 | 2017-02-21 14:47:02 +0100 | [diff] [blame] | 9444 | int queued, running, queue_flags = |
| 9445 | DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 9446 | struct rq_flags rf; |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9447 | struct rq *rq; |
| 9448 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 9449 | rq = task_rq_lock(tsk, &rf); |
Peter Zijlstra | 1b1d622 | 2017-01-23 16:05:55 +0100 | [diff] [blame] | 9450 | update_rq_clock(rq); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9451 | |
Dmitry Adamushko | 051a1d1 | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 9452 | running = task_current(rq, tsk); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 9453 | queued = task_on_rq_queued(tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9454 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 9455 | if (queued) |
Peter Zijlstra | 7a57f32 | 2017-02-21 14:47:02 +0100 | [diff] [blame] | 9456 | dequeue_task(rq, tsk, queue_flags); |
Steven Rostedt (VMware) | bb3bac2 | 2017-02-06 11:04:26 -0500 | [diff] [blame] | 9457 | if (running) |
Kirill Tkhai | f3cd1c4 | 2014-09-12 17:41:40 +0400 | [diff] [blame] | 9458 | put_prev_task(rq, tsk); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9459 | |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 9460 | sched_change_group(tsk, TASK_MOVE_GROUP); |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 9461 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 9462 | if (queued) |
Peter Zijlstra | 7a57f32 | 2017-02-21 14:47:02 +0100 | [diff] [blame] | 9463 | enqueue_task(rq, tsk, queue_flags); |
Vincent Guittot | 2a4b03f | 2020-01-14 15:13:56 +0100 | [diff] [blame] | 9464 | if (running) { |
Peter Zijlstra | 03b7fad | 2019-05-29 20:36:41 +0000 | [diff] [blame] | 9465 | set_next_task(rq, tsk); |
Vincent Guittot | 2a4b03f | 2020-01-14 15:13:56 +0100 | [diff] [blame] | 9466 | /* |
| 9467 | * After changing group, the running task may have joined a |
| 9468 | * throttled one but it's still the running task. Trigger a |
| 9469 | * resched to make sure that task can still run. |
| 9470 | */ |
| 9471 | resched_curr(rq); |
| 9472 | } |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9473 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 9474 | task_rq_unlock(rq, tsk, &rf); |
Srivatsa Vaddagiri | 29f59db | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 9475 | } |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9476 | |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9477 | static inline struct task_group *css_tg(struct cgroup_subsys_state *css) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9478 | { |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9479 | return css ? container_of(css, struct task_group, css) : NULL; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9480 | } |
| 9481 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9482 | static struct cgroup_subsys_state * |
| 9483 | cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9484 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9485 | struct task_group *parent = css_tg(parent_css); |
| 9486 | struct task_group *tg; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9487 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9488 | if (!parent) { |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9489 | /* This is early initialization for the top cgroup */ |
Yong Zhang | 07e06b0 | 2011-01-07 15:17:36 +0800 | [diff] [blame] | 9490 | return &root_task_group.css; |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9491 | } |
| 9492 | |
Dhaval Giani | ec7dc8a | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 9493 | tg = sched_create_group(parent); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9494 | if (IS_ERR(tg)) |
| 9495 | return ERR_PTR(-ENOMEM); |
| 9496 | |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9497 | return &tg->css; |
| 9498 | } |
| 9499 | |
Konstantin Khlebnikov | 96b7774 | 2017-02-08 14:27:27 +0300 | [diff] [blame] | 9500 | /* Expose task group only after completing cgroup initialization */ |
| 9501 | static int cpu_cgroup_css_online(struct cgroup_subsys_state *css) |
| 9502 | { |
| 9503 | struct task_group *tg = css_tg(css); |
| 9504 | struct task_group *parent = css_tg(css->parent); |
| 9505 | |
| 9506 | if (parent) |
| 9507 | sched_online_group(tg, parent); |
Qais Yousef | 7226017 | 2019-12-24 11:54:04 +0000 | [diff] [blame] | 9508 | |
| 9509 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
| 9510 | /* Propagate the effective uclamp value for the new group */ |
Qais Yousef | 93b7385 | 2021-05-10 15:50:32 +0100 | [diff] [blame] | 9511 | mutex_lock(&uclamp_mutex); |
| 9512 | rcu_read_lock(); |
Qais Yousef | 7226017 | 2019-12-24 11:54:04 +0000 | [diff] [blame] | 9513 | cpu_util_update_eff(css); |
Qais Yousef | 93b7385 | 2021-05-10 15:50:32 +0100 | [diff] [blame] | 9514 | rcu_read_unlock(); |
| 9515 | mutex_unlock(&uclamp_mutex); |
Qais Yousef | 7226017 | 2019-12-24 11:54:04 +0000 | [diff] [blame] | 9516 | #endif |
| 9517 | |
Konstantin Khlebnikov | 96b7774 | 2017-02-08 14:27:27 +0300 | [diff] [blame] | 9518 | return 0; |
| 9519 | } |
| 9520 | |
Peter Zijlstra | 2f5177f | 2016-03-16 16:22:45 +0100 | [diff] [blame] | 9521 | static void cpu_cgroup_css_released(struct cgroup_subsys_state *css) |
Li Zefan | ace783b | 2013-01-24 14:30:48 +0800 | [diff] [blame] | 9522 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9523 | struct task_group *tg = css_tg(css); |
Li Zefan | ace783b | 2013-01-24 14:30:48 +0800 | [diff] [blame] | 9524 | |
Peter Zijlstra | 2f5177f | 2016-03-16 16:22:45 +0100 | [diff] [blame] | 9525 | sched_offline_group(tg); |
Li Zefan | ace783b | 2013-01-24 14:30:48 +0800 | [diff] [blame] | 9526 | } |
| 9527 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9528 | static void cpu_cgroup_css_free(struct cgroup_subsys_state *css) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9529 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9530 | struct task_group *tg = css_tg(css); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9531 | |
Peter Zijlstra | 2f5177f | 2016-03-16 16:22:45 +0100 | [diff] [blame] | 9532 | /* |
| 9533 | * Relies on the RCU grace period between css_released() and this. |
| 9534 | */ |
| 9535 | sched_free_group(tg); |
Li Zefan | ace783b | 2013-01-24 14:30:48 +0800 | [diff] [blame] | 9536 | } |
| 9537 | |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 9538 | /* |
| 9539 | * This is called before wake_up_new_task(), therefore we really only |
| 9540 | * have to set its group bits, all the other stuff does not apply. |
| 9541 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 9542 | static void cpu_cgroup_fork(struct task_struct *task) |
Kirill Tkhai | eeb61e5 | 2014-10-27 14:18:25 +0400 | [diff] [blame] | 9543 | { |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 9544 | struct rq_flags rf; |
| 9545 | struct rq *rq; |
| 9546 | |
| 9547 | rq = task_rq_lock(task, &rf); |
| 9548 | |
Peter Zijlstra | 80f5c1b | 2016-10-03 16:28:37 +0200 | [diff] [blame] | 9549 | update_rq_clock(rq); |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 9550 | sched_change_group(task, TASK_SET_GROUP); |
| 9551 | |
| 9552 | task_rq_unlock(rq, task, &rf); |
Kirill Tkhai | eeb61e5 | 2014-10-27 14:18:25 +0400 | [diff] [blame] | 9553 | } |
| 9554 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 9555 | static int cpu_cgroup_can_attach(struct cgroup_taskset *tset) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9556 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9557 | struct task_struct *task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 9558 | struct cgroup_subsys_state *css; |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 9559 | int ret = 0; |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9560 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 9561 | cgroup_taskset_for_each(task, css, tset) { |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9562 | #ifdef CONFIG_RT_GROUP_SCHED |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 9563 | if (!sched_rt_can_attach(css_tg(css), task)) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9564 | return -EINVAL; |
Peter Zijlstra | b68aa23 | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9565 | #endif |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 9566 | /* |
Tal Zussman | b19a888 | 2020-11-12 19:51:56 -0500 | [diff] [blame] | 9567 | * Serialize against wake_up_new_task() such that if it's |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 9568 | * running, we're sure to observe its full state. |
| 9569 | */ |
| 9570 | raw_spin_lock_irq(&task->pi_lock); |
| 9571 | /* |
| 9572 | * Avoid calling sched_move_task() before wake_up_new_task() |
| 9573 | * has happened. This would lead to problems with PELT, due to |
| 9574 | * move wanting to detach+attach while we're not attached yet. |
| 9575 | */ |
| 9576 | if (task->state == TASK_NEW) |
| 9577 | ret = -EINVAL; |
| 9578 | raw_spin_unlock_irq(&task->pi_lock); |
| 9579 | |
| 9580 | if (ret) |
| 9581 | break; |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9582 | } |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 9583 | return ret; |
Ben Blum | be367d0 | 2009-09-23 15:56:31 -0700 | [diff] [blame] | 9584 | } |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9585 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 9586 | static void cpu_cgroup_attach(struct cgroup_taskset *tset) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9587 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9588 | struct task_struct *task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 9589 | struct cgroup_subsys_state *css; |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9590 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 9591 | cgroup_taskset_for_each(task, css, tset) |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 9592 | sched_move_task(task); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9593 | } |
| 9594 | |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 9595 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
Patrick Bellasi | 0b60ba2 | 2019-08-22 14:28:07 +0100 | [diff] [blame] | 9596 | static void cpu_util_update_eff(struct cgroup_subsys_state *css) |
| 9597 | { |
| 9598 | struct cgroup_subsys_state *top_css = css; |
| 9599 | struct uclamp_se *uc_parent = NULL; |
| 9600 | struct uclamp_se *uc_se = NULL; |
| 9601 | unsigned int eff[UCLAMP_CNT]; |
Patrick Bellasi | 0413d7f | 2019-08-22 14:28:11 +0100 | [diff] [blame] | 9602 | enum uclamp_id clamp_id; |
Patrick Bellasi | 0b60ba2 | 2019-08-22 14:28:07 +0100 | [diff] [blame] | 9603 | unsigned int clamps; |
| 9604 | |
Qais Yousef | 93b7385 | 2021-05-10 15:50:32 +0100 | [diff] [blame] | 9605 | lockdep_assert_held(&uclamp_mutex); |
| 9606 | SCHED_WARN_ON(!rcu_read_lock_held()); |
| 9607 | |
Patrick Bellasi | 0b60ba2 | 2019-08-22 14:28:07 +0100 | [diff] [blame] | 9608 | css_for_each_descendant_pre(css, top_css) { |
| 9609 | uc_parent = css_tg(css)->parent |
| 9610 | ? css_tg(css)->parent->uclamp : NULL; |
| 9611 | |
| 9612 | for_each_clamp_id(clamp_id) { |
| 9613 | /* Assume effective clamps matches requested clamps */ |
| 9614 | eff[clamp_id] = css_tg(css)->uclamp_req[clamp_id].value; |
| 9615 | /* Cap effective clamps with parent's effective clamps */ |
| 9616 | if (uc_parent && |
| 9617 | eff[clamp_id] > uc_parent[clamp_id].value) { |
| 9618 | eff[clamp_id] = uc_parent[clamp_id].value; |
| 9619 | } |
| 9620 | } |
| 9621 | /* Ensure protection is always capped by limit */ |
| 9622 | eff[UCLAMP_MIN] = min(eff[UCLAMP_MIN], eff[UCLAMP_MAX]); |
| 9623 | |
| 9624 | /* Propagate most restrictive effective clamps */ |
| 9625 | clamps = 0x0; |
| 9626 | uc_se = css_tg(css)->uclamp; |
| 9627 | for_each_clamp_id(clamp_id) { |
| 9628 | if (eff[clamp_id] == uc_se[clamp_id].value) |
| 9629 | continue; |
| 9630 | uc_se[clamp_id].value = eff[clamp_id]; |
| 9631 | uc_se[clamp_id].bucket_id = uclamp_bucket_id(eff[clamp_id]); |
| 9632 | clamps |= (0x1 << clamp_id); |
| 9633 | } |
Patrick Bellasi | babbe17 | 2019-08-22 14:28:10 +0100 | [diff] [blame] | 9634 | if (!clamps) { |
Patrick Bellasi | 0b60ba2 | 2019-08-22 14:28:07 +0100 | [diff] [blame] | 9635 | css = css_rightmost_descendant(css); |
Patrick Bellasi | babbe17 | 2019-08-22 14:28:10 +0100 | [diff] [blame] | 9636 | continue; |
| 9637 | } |
| 9638 | |
| 9639 | /* Immediately update descendants RUNNABLE tasks */ |
| 9640 | uclamp_update_active_tasks(css, clamps); |
Patrick Bellasi | 0b60ba2 | 2019-08-22 14:28:07 +0100 | [diff] [blame] | 9641 | } |
| 9642 | } |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 9643 | |
| 9644 | /* |
| 9645 | * Integer 10^N with a given N exponent by casting to integer the literal "1eN" |
| 9646 | * C expression. Since there is no way to convert a macro argument (N) into a |
| 9647 | * character constant, use two levels of macros. |
| 9648 | */ |
| 9649 | #define _POW10(exp) ((unsigned int)1e##exp) |
| 9650 | #define POW10(exp) _POW10(exp) |
| 9651 | |
| 9652 | struct uclamp_request { |
| 9653 | #define UCLAMP_PERCENT_SHIFT 2 |
| 9654 | #define UCLAMP_PERCENT_SCALE (100 * POW10(UCLAMP_PERCENT_SHIFT)) |
| 9655 | s64 percent; |
| 9656 | u64 util; |
| 9657 | int ret; |
| 9658 | }; |
| 9659 | |
| 9660 | static inline struct uclamp_request |
| 9661 | capacity_from_percent(char *buf) |
| 9662 | { |
| 9663 | struct uclamp_request req = { |
| 9664 | .percent = UCLAMP_PERCENT_SCALE, |
| 9665 | .util = SCHED_CAPACITY_SCALE, |
| 9666 | .ret = 0, |
| 9667 | }; |
| 9668 | |
| 9669 | buf = strim(buf); |
| 9670 | if (strcmp(buf, "max")) { |
| 9671 | req.ret = cgroup_parse_float(buf, UCLAMP_PERCENT_SHIFT, |
| 9672 | &req.percent); |
| 9673 | if (req.ret) |
| 9674 | return req; |
Qais Yousef | b562d14 | 2020-01-14 21:09:47 +0000 | [diff] [blame] | 9675 | if ((u64)req.percent > UCLAMP_PERCENT_SCALE) { |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 9676 | req.ret = -ERANGE; |
| 9677 | return req; |
| 9678 | } |
| 9679 | |
| 9680 | req.util = req.percent << SCHED_CAPACITY_SHIFT; |
| 9681 | req.util = DIV_ROUND_CLOSEST_ULL(req.util, UCLAMP_PERCENT_SCALE); |
| 9682 | } |
| 9683 | |
| 9684 | return req; |
| 9685 | } |
| 9686 | |
| 9687 | static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf, |
| 9688 | size_t nbytes, loff_t off, |
| 9689 | enum uclamp_id clamp_id) |
| 9690 | { |
| 9691 | struct uclamp_request req; |
| 9692 | struct task_group *tg; |
| 9693 | |
| 9694 | req = capacity_from_percent(buf); |
| 9695 | if (req.ret) |
| 9696 | return req.ret; |
| 9697 | |
Qais Yousef | 46609ce | 2020-06-30 12:21:23 +0100 | [diff] [blame] | 9698 | static_branch_enable(&sched_uclamp_used); |
| 9699 | |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 9700 | mutex_lock(&uclamp_mutex); |
| 9701 | rcu_read_lock(); |
| 9702 | |
| 9703 | tg = css_tg(of_css(of)); |
| 9704 | if (tg->uclamp_req[clamp_id].value != req.util) |
| 9705 | uclamp_se_set(&tg->uclamp_req[clamp_id], req.util, false); |
| 9706 | |
| 9707 | /* |
| 9708 | * Because of not recoverable conversion rounding we keep track of the |
| 9709 | * exact requested value |
| 9710 | */ |
| 9711 | tg->uclamp_pct[clamp_id] = req.percent; |
| 9712 | |
Patrick Bellasi | 0b60ba2 | 2019-08-22 14:28:07 +0100 | [diff] [blame] | 9713 | /* Update effective clamps to track the most restrictive value */ |
| 9714 | cpu_util_update_eff(of_css(of)); |
| 9715 | |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 9716 | rcu_read_unlock(); |
| 9717 | mutex_unlock(&uclamp_mutex); |
| 9718 | |
| 9719 | return nbytes; |
| 9720 | } |
| 9721 | |
| 9722 | static ssize_t cpu_uclamp_min_write(struct kernfs_open_file *of, |
| 9723 | char *buf, size_t nbytes, |
| 9724 | loff_t off) |
| 9725 | { |
| 9726 | return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MIN); |
| 9727 | } |
| 9728 | |
| 9729 | static ssize_t cpu_uclamp_max_write(struct kernfs_open_file *of, |
| 9730 | char *buf, size_t nbytes, |
| 9731 | loff_t off) |
| 9732 | { |
| 9733 | return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MAX); |
| 9734 | } |
| 9735 | |
| 9736 | static inline void cpu_uclamp_print(struct seq_file *sf, |
| 9737 | enum uclamp_id clamp_id) |
| 9738 | { |
| 9739 | struct task_group *tg; |
| 9740 | u64 util_clamp; |
| 9741 | u64 percent; |
| 9742 | u32 rem; |
| 9743 | |
| 9744 | rcu_read_lock(); |
| 9745 | tg = css_tg(seq_css(sf)); |
| 9746 | util_clamp = tg->uclamp_req[clamp_id].value; |
| 9747 | rcu_read_unlock(); |
| 9748 | |
| 9749 | if (util_clamp == SCHED_CAPACITY_SCALE) { |
| 9750 | seq_puts(sf, "max\n"); |
| 9751 | return; |
| 9752 | } |
| 9753 | |
| 9754 | percent = tg->uclamp_pct[clamp_id]; |
| 9755 | percent = div_u64_rem(percent, POW10(UCLAMP_PERCENT_SHIFT), &rem); |
| 9756 | seq_printf(sf, "%llu.%0*u\n", percent, UCLAMP_PERCENT_SHIFT, rem); |
| 9757 | } |
| 9758 | |
| 9759 | static int cpu_uclamp_min_show(struct seq_file *sf, void *v) |
| 9760 | { |
| 9761 | cpu_uclamp_print(sf, UCLAMP_MIN); |
| 9762 | return 0; |
| 9763 | } |
| 9764 | |
| 9765 | static int cpu_uclamp_max_show(struct seq_file *sf, void *v) |
| 9766 | { |
| 9767 | cpu_uclamp_print(sf, UCLAMP_MAX); |
| 9768 | return 0; |
| 9769 | } |
| 9770 | #endif /* CONFIG_UCLAMP_TASK_GROUP */ |
| 9771 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 9772 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9773 | static int cpu_shares_write_u64(struct cgroup_subsys_state *css, |
| 9774 | struct cftype *cftype, u64 shareval) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9775 | { |
Konstantin Khlebnikov | 5b61d50 | 2019-02-27 11:10:18 +0300 | [diff] [blame] | 9776 | if (shareval > scale_load_down(ULONG_MAX)) |
| 9777 | shareval = MAX_SHARES; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9778 | return sched_group_set_shares(css_tg(css), scale_load(shareval)); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9779 | } |
| 9780 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9781 | static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css, |
| 9782 | struct cftype *cft) |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9783 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9784 | struct task_group *tg = css_tg(css); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9785 | |
Nikhil Rao | c8b2811 | 2011-05-18 14:37:48 -0700 | [diff] [blame] | 9786 | return (u64) scale_load_down(tg->shares); |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 9787 | } |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9788 | |
| 9789 | #ifdef CONFIG_CFS_BANDWIDTH |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 9790 | static DEFINE_MUTEX(cfs_constraints_mutex); |
| 9791 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9792 | const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */ |
YueHaibing | b1546ed | 2019-04-18 22:47:13 +0800 | [diff] [blame] | 9793 | static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */ |
Huaixin Chang | d505b8a | 2020-04-25 18:52:48 +0800 | [diff] [blame] | 9794 | /* More than 203 days if BW_SHIFT equals 20. */ |
| 9795 | static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC; |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9796 | |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 9797 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime); |
| 9798 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9799 | static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota) |
| 9800 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 9801 | int i, ret = 0, runtime_enabled, runtime_was_enabled; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9802 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9803 | |
| 9804 | if (tg == &root_task_group) |
| 9805 | return -EINVAL; |
| 9806 | |
| 9807 | /* |
| 9808 | * Ensure we have at some amount of bandwidth every period. This is |
| 9809 | * to prevent reaching a state of large arrears when throttled via |
| 9810 | * entity_tick() resulting in prolonged exit starvation. |
| 9811 | */ |
| 9812 | if (quota < min_cfs_quota_period || period < min_cfs_quota_period) |
| 9813 | return -EINVAL; |
| 9814 | |
| 9815 | /* |
Ingo Molnar | 3b03706 | 2021-03-18 13:38:50 +0100 | [diff] [blame] | 9816 | * Likewise, bound things on the other side by preventing insane quota |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9817 | * periods. This also allows us to normalize in computing quota |
| 9818 | * feasibility. |
| 9819 | */ |
| 9820 | if (period > max_cfs_quota_period) |
| 9821 | return -EINVAL; |
| 9822 | |
Kirill Tkhai | 0e59bda | 2014-06-25 12:19:42 +0400 | [diff] [blame] | 9823 | /* |
Huaixin Chang | d505b8a | 2020-04-25 18:52:48 +0800 | [diff] [blame] | 9824 | * Bound quota to defend quota against overflow during bandwidth shift. |
| 9825 | */ |
| 9826 | if (quota != RUNTIME_INF && quota > max_cfs_runtime) |
| 9827 | return -EINVAL; |
| 9828 | |
| 9829 | /* |
Kirill Tkhai | 0e59bda | 2014-06-25 12:19:42 +0400 | [diff] [blame] | 9830 | * Prevent race between setting of cfs_rq->runtime_enabled and |
| 9831 | * unthrottle_offline_cfs_rqs(). |
| 9832 | */ |
| 9833 | get_online_cpus(); |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 9834 | mutex_lock(&cfs_constraints_mutex); |
| 9835 | ret = __cfs_schedulable(tg, period, quota); |
| 9836 | if (ret) |
| 9837 | goto out_unlock; |
| 9838 | |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 9839 | runtime_enabled = quota != RUNTIME_INF; |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 9840 | runtime_was_enabled = cfs_b->quota != RUNTIME_INF; |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 9841 | /* |
| 9842 | * If we need to toggle cfs_bandwidth_used, off->on must occur |
| 9843 | * before making related changes, and on->off must occur afterwards |
| 9844 | */ |
| 9845 | if (runtime_enabled && !runtime_was_enabled) |
| 9846 | cfs_bandwidth_usage_inc(); |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9847 | raw_spin_lock_irq(&cfs_b->lock); |
| 9848 | cfs_b->period = ns_to_ktime(period); |
| 9849 | cfs_b->quota = quota; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 9850 | |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 9851 | __refill_cfs_bandwidth_runtime(cfs_b); |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9852 | |
| 9853 | /* Restart the period timer (if active) to handle new period expiry: */ |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 9854 | if (runtime_enabled) |
| 9855 | start_cfs_bandwidth(cfs_b); |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 9856 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9857 | raw_spin_unlock_irq(&cfs_b->lock); |
| 9858 | |
Kirill Tkhai | 0e59bda | 2014-06-25 12:19:42 +0400 | [diff] [blame] | 9859 | for_each_online_cpu(i) { |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9860 | struct cfs_rq *cfs_rq = tg->cfs_rq[i]; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9861 | struct rq *rq = cfs_rq->rq; |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 9862 | struct rq_flags rf; |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9863 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 9864 | rq_lock_irq(rq, &rf); |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 9865 | cfs_rq->runtime_enabled = runtime_enabled; |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9866 | cfs_rq->runtime_remaining = 0; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 9867 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9868 | if (cfs_rq->throttled) |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 9869 | unthrottle_cfs_rq(cfs_rq); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 9870 | rq_unlock_irq(rq, &rf); |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9871 | } |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 9872 | if (runtime_was_enabled && !runtime_enabled) |
| 9873 | cfs_bandwidth_usage_dec(); |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 9874 | out_unlock: |
| 9875 | mutex_unlock(&cfs_constraints_mutex); |
Kirill Tkhai | 0e59bda | 2014-06-25 12:19:42 +0400 | [diff] [blame] | 9876 | put_online_cpus(); |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9877 | |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 9878 | return ret; |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9879 | } |
| 9880 | |
YueHaibing | b1546ed | 2019-04-18 22:47:13 +0800 | [diff] [blame] | 9881 | static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9882 | { |
| 9883 | u64 quota, period; |
| 9884 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9885 | period = ktime_to_ns(tg->cfs_bandwidth.period); |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9886 | if (cfs_quota_us < 0) |
| 9887 | quota = RUNTIME_INF; |
Konstantin Khlebnikov | 1a8b454 | 2019-02-27 11:10:20 +0300 | [diff] [blame] | 9888 | else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9889 | quota = (u64)cfs_quota_us * NSEC_PER_USEC; |
Konstantin Khlebnikov | 1a8b454 | 2019-02-27 11:10:20 +0300 | [diff] [blame] | 9890 | else |
| 9891 | return -EINVAL; |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9892 | |
| 9893 | return tg_set_cfs_bandwidth(tg, period, quota); |
| 9894 | } |
| 9895 | |
YueHaibing | b1546ed | 2019-04-18 22:47:13 +0800 | [diff] [blame] | 9896 | static long tg_get_cfs_quota(struct task_group *tg) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9897 | { |
| 9898 | u64 quota_us; |
| 9899 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9900 | if (tg->cfs_bandwidth.quota == RUNTIME_INF) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9901 | return -1; |
| 9902 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9903 | quota_us = tg->cfs_bandwidth.quota; |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9904 | do_div(quota_us, NSEC_PER_USEC); |
| 9905 | |
| 9906 | return quota_us; |
| 9907 | } |
| 9908 | |
YueHaibing | b1546ed | 2019-04-18 22:47:13 +0800 | [diff] [blame] | 9909 | static int tg_set_cfs_period(struct task_group *tg, long cfs_period_us) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9910 | { |
| 9911 | u64 quota, period; |
| 9912 | |
Konstantin Khlebnikov | 1a8b454 | 2019-02-27 11:10:20 +0300 | [diff] [blame] | 9913 | if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC) |
| 9914 | return -EINVAL; |
| 9915 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9916 | period = (u64)cfs_period_us * NSEC_PER_USEC; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9917 | quota = tg->cfs_bandwidth.quota; |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9918 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9919 | return tg_set_cfs_bandwidth(tg, period, quota); |
| 9920 | } |
| 9921 | |
YueHaibing | b1546ed | 2019-04-18 22:47:13 +0800 | [diff] [blame] | 9922 | static long tg_get_cfs_period(struct task_group *tg) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9923 | { |
| 9924 | u64 cfs_period_us; |
| 9925 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9926 | cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period); |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9927 | do_div(cfs_period_us, NSEC_PER_USEC); |
| 9928 | |
| 9929 | return cfs_period_us; |
| 9930 | } |
| 9931 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9932 | static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css, |
| 9933 | struct cftype *cft) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9934 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9935 | return tg_get_cfs_quota(css_tg(css)); |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9936 | } |
| 9937 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9938 | static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css, |
| 9939 | struct cftype *cftype, s64 cfs_quota_us) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9940 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9941 | return tg_set_cfs_quota(css_tg(css), cfs_quota_us); |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9942 | } |
| 9943 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9944 | static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css, |
| 9945 | struct cftype *cft) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9946 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9947 | return tg_get_cfs_period(css_tg(css)); |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9948 | } |
| 9949 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9950 | static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css, |
| 9951 | struct cftype *cftype, u64 cfs_period_us) |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9952 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 9953 | return tg_set_cfs_period(css_tg(css), cfs_period_us); |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 9954 | } |
| 9955 | |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 9956 | struct cfs_schedulable_data { |
| 9957 | struct task_group *tg; |
| 9958 | u64 period, quota; |
| 9959 | }; |
| 9960 | |
| 9961 | /* |
| 9962 | * normalize group quota/period to be quota/max_period |
| 9963 | * note: units are usecs |
| 9964 | */ |
| 9965 | static u64 normalize_cfs_quota(struct task_group *tg, |
| 9966 | struct cfs_schedulable_data *d) |
| 9967 | { |
| 9968 | u64 quota, period; |
| 9969 | |
| 9970 | if (tg == d->tg) { |
| 9971 | period = d->period; |
| 9972 | quota = d->quota; |
| 9973 | } else { |
| 9974 | period = tg_get_cfs_period(tg); |
| 9975 | quota = tg_get_cfs_quota(tg); |
| 9976 | } |
| 9977 | |
| 9978 | /* note: these should typically be equivalent */ |
| 9979 | if (quota == RUNTIME_INF || quota == -1) |
| 9980 | return RUNTIME_INF; |
| 9981 | |
| 9982 | return to_ratio(period, quota); |
| 9983 | } |
| 9984 | |
| 9985 | static int tg_cfs_schedulable_down(struct task_group *tg, void *data) |
| 9986 | { |
| 9987 | struct cfs_schedulable_data *d = data; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9988 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 9989 | s64 quota = 0, parent_quota = -1; |
| 9990 | |
| 9991 | if (!tg->parent) { |
| 9992 | quota = RUNTIME_INF; |
| 9993 | } else { |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9994 | struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth; |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 9995 | |
| 9996 | quota = normalize_cfs_quota(tg, d); |
Zhihui Zhang | 9c58c79 | 2014-09-20 21:24:36 -0400 | [diff] [blame] | 9997 | parent_quota = parent_b->hierarchical_quota; |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 9998 | |
| 9999 | /* |
Tejun Heo | c53593e | 2018-01-22 11:26:18 -0800 | [diff] [blame] | 10000 | * Ensure max(child_quota) <= parent_quota. On cgroup2, |
| 10001 | * always take the min. On cgroup1, only inherit when no |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 10002 | * limit is set: |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 10003 | */ |
Tejun Heo | c53593e | 2018-01-22 11:26:18 -0800 | [diff] [blame] | 10004 | if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) { |
| 10005 | quota = min(quota, parent_quota); |
| 10006 | } else { |
| 10007 | if (quota == RUNTIME_INF) |
| 10008 | quota = parent_quota; |
| 10009 | else if (parent_quota != RUNTIME_INF && quota > parent_quota) |
| 10010 | return -EINVAL; |
| 10011 | } |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 10012 | } |
Zhihui Zhang | 9c58c79 | 2014-09-20 21:24:36 -0400 | [diff] [blame] | 10013 | cfs_b->hierarchical_quota = quota; |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 10014 | |
| 10015 | return 0; |
| 10016 | } |
| 10017 | |
| 10018 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota) |
| 10019 | { |
Paul Turner | 8277434 | 2011-07-21 09:43:35 -0700 | [diff] [blame] | 10020 | int ret; |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 10021 | struct cfs_schedulable_data data = { |
| 10022 | .tg = tg, |
| 10023 | .period = period, |
| 10024 | .quota = quota, |
| 10025 | }; |
| 10026 | |
| 10027 | if (quota != RUNTIME_INF) { |
| 10028 | do_div(data.period, NSEC_PER_USEC); |
| 10029 | do_div(data.quota, NSEC_PER_USEC); |
| 10030 | } |
| 10031 | |
Paul Turner | 8277434 | 2011-07-21 09:43:35 -0700 | [diff] [blame] | 10032 | rcu_read_lock(); |
| 10033 | ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data); |
| 10034 | rcu_read_unlock(); |
| 10035 | |
| 10036 | return ret; |
Paul Turner | a790de9 | 2011-07-21 09:43:29 -0700 | [diff] [blame] | 10037 | } |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 10038 | |
Tejun Heo | a1f7164 | 2017-09-25 09:00:18 -0700 | [diff] [blame] | 10039 | static int cpu_cfs_stat_show(struct seq_file *sf, void *v) |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 10040 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 10041 | struct task_group *tg = css_tg(seq_css(sf)); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 10042 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 10043 | |
Tejun Heo | 44ffc75 | 2013-12-05 12:28:01 -0500 | [diff] [blame] | 10044 | seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods); |
| 10045 | seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled); |
| 10046 | seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time); |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 10047 | |
Yun Wang | 3d6c50c | 2018-07-04 11:27:27 +0800 | [diff] [blame] | 10048 | if (schedstat_enabled() && tg != &root_task_group) { |
| 10049 | u64 ws = 0; |
| 10050 | int i; |
| 10051 | |
| 10052 | for_each_possible_cpu(i) |
| 10053 | ws += schedstat_val(tg->se[i]->statistics.wait_sum); |
| 10054 | |
| 10055 | seq_printf(sf, "wait_sum %llu\n", ws); |
| 10056 | } |
| 10057 | |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 10058 | return 0; |
| 10059 | } |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 10060 | #endif /* CONFIG_CFS_BANDWIDTH */ |
Dhaval Giani | 6d6bc0a | 2008-05-30 14:23:45 +0200 | [diff] [blame] | 10061 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 10062 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 10063 | #ifdef CONFIG_RT_GROUP_SCHED |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 10064 | static int cpu_rt_runtime_write(struct cgroup_subsys_state *css, |
| 10065 | struct cftype *cft, s64 val) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 10066 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 10067 | return sched_group_set_rt_runtime(css_tg(css), val); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 10068 | } |
| 10069 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 10070 | static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css, |
| 10071 | struct cftype *cft) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 10072 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 10073 | return sched_group_rt_runtime(css_tg(css)); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 10074 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 10075 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 10076 | static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css, |
| 10077 | struct cftype *cftype, u64 rt_period_us) |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 10078 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 10079 | return sched_group_set_rt_period(css_tg(css), rt_period_us); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 10080 | } |
| 10081 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 10082 | static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css, |
| 10083 | struct cftype *cft) |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 10084 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 10085 | return sched_group_rt_period(css_tg(css)); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 10086 | } |
Dhaval Giani | 6d6bc0a | 2008-05-30 14:23:45 +0200 | [diff] [blame] | 10087 | #endif /* CONFIG_RT_GROUP_SCHED */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 10088 | |
Tejun Heo | a1f7164 | 2017-09-25 09:00:18 -0700 | [diff] [blame] | 10089 | static struct cftype cpu_legacy_files[] = { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 10090 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 10091 | { |
| 10092 | .name = "shares", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 10093 | .read_u64 = cpu_shares_read_u64, |
| 10094 | .write_u64 = cpu_shares_write_u64, |
Paul Menage | fe5c7cc | 2007-10-29 21:18:11 +0100 | [diff] [blame] | 10095 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 10096 | #endif |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 10097 | #ifdef CONFIG_CFS_BANDWIDTH |
| 10098 | { |
| 10099 | .name = "cfs_quota_us", |
| 10100 | .read_s64 = cpu_cfs_quota_read_s64, |
| 10101 | .write_s64 = cpu_cfs_quota_write_s64, |
| 10102 | }, |
| 10103 | { |
| 10104 | .name = "cfs_period_us", |
| 10105 | .read_u64 = cpu_cfs_period_read_u64, |
| 10106 | .write_u64 = cpu_cfs_period_write_u64, |
| 10107 | }, |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 10108 | { |
| 10109 | .name = "stat", |
Tejun Heo | a1f7164 | 2017-09-25 09:00:18 -0700 | [diff] [blame] | 10110 | .seq_show = cpu_cfs_stat_show, |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 10111 | }, |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 10112 | #endif |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 10113 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 10114 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 10115 | .name = "rt_runtime_us", |
Paul Menage | 06ecb27 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 10116 | .read_s64 = cpu_rt_runtime_read, |
| 10117 | .write_s64 = cpu_rt_runtime_write, |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 10118 | }, |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 10119 | { |
| 10120 | .name = "rt_period_us", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 10121 | .read_u64 = cpu_rt_period_read_uint, |
| 10122 | .write_u64 = cpu_rt_period_write_uint, |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 10123 | }, |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 10124 | #endif |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 10125 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
| 10126 | { |
| 10127 | .name = "uclamp.min", |
| 10128 | .flags = CFTYPE_NOT_ON_ROOT, |
| 10129 | .seq_show = cpu_uclamp_min_show, |
| 10130 | .write = cpu_uclamp_min_write, |
| 10131 | }, |
| 10132 | { |
| 10133 | .name = "uclamp.max", |
| 10134 | .flags = CFTYPE_NOT_ON_ROOT, |
| 10135 | .seq_show = cpu_uclamp_max_show, |
| 10136 | .write = cpu_uclamp_max_write, |
| 10137 | }, |
| 10138 | #endif |
Ingo Molnar | d1ccc66 | 2017-02-01 11:46:42 +0100 | [diff] [blame] | 10139 | { } /* Terminate */ |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 10140 | }; |
| 10141 | |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 10142 | static int cpu_extra_stat_show(struct seq_file *sf, |
| 10143 | struct cgroup_subsys_state *css) |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10144 | { |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10145 | #ifdef CONFIG_CFS_BANDWIDTH |
| 10146 | { |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 10147 | struct task_group *tg = css_tg(css); |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10148 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
| 10149 | u64 throttled_usec; |
| 10150 | |
| 10151 | throttled_usec = cfs_b->throttled_time; |
| 10152 | do_div(throttled_usec, NSEC_PER_USEC); |
| 10153 | |
| 10154 | seq_printf(sf, "nr_periods %d\n" |
| 10155 | "nr_throttled %d\n" |
| 10156 | "throttled_usec %llu\n", |
| 10157 | cfs_b->nr_periods, cfs_b->nr_throttled, |
| 10158 | throttled_usec); |
| 10159 | } |
| 10160 | #endif |
| 10161 | return 0; |
| 10162 | } |
| 10163 | |
| 10164 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 10165 | static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css, |
| 10166 | struct cftype *cft) |
| 10167 | { |
| 10168 | struct task_group *tg = css_tg(css); |
| 10169 | u64 weight = scale_load_down(tg->shares); |
| 10170 | |
| 10171 | return DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024); |
| 10172 | } |
| 10173 | |
| 10174 | static int cpu_weight_write_u64(struct cgroup_subsys_state *css, |
| 10175 | struct cftype *cft, u64 weight) |
| 10176 | { |
| 10177 | /* |
| 10178 | * cgroup weight knobs should use the common MIN, DFL and MAX |
| 10179 | * values which are 1, 100 and 10000 respectively. While it loses |
| 10180 | * a bit of range on both ends, it maps pretty well onto the shares |
| 10181 | * value used by scheduler and the round-trip conversions preserve |
| 10182 | * the original value over the entire range. |
| 10183 | */ |
| 10184 | if (weight < CGROUP_WEIGHT_MIN || weight > CGROUP_WEIGHT_MAX) |
| 10185 | return -ERANGE; |
| 10186 | |
| 10187 | weight = DIV_ROUND_CLOSEST_ULL(weight * 1024, CGROUP_WEIGHT_DFL); |
| 10188 | |
| 10189 | return sched_group_set_shares(css_tg(css), scale_load(weight)); |
| 10190 | } |
| 10191 | |
| 10192 | static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css, |
| 10193 | struct cftype *cft) |
| 10194 | { |
| 10195 | unsigned long weight = scale_load_down(css_tg(css)->shares); |
| 10196 | int last_delta = INT_MAX; |
| 10197 | int prio, delta; |
| 10198 | |
| 10199 | /* find the closest nice value to the current weight */ |
| 10200 | for (prio = 0; prio < ARRAY_SIZE(sched_prio_to_weight); prio++) { |
| 10201 | delta = abs(sched_prio_to_weight[prio] - weight); |
| 10202 | if (delta >= last_delta) |
| 10203 | break; |
| 10204 | last_delta = delta; |
| 10205 | } |
| 10206 | |
| 10207 | return PRIO_TO_NICE(prio - 1 + MAX_RT_PRIO); |
| 10208 | } |
| 10209 | |
| 10210 | static int cpu_weight_nice_write_s64(struct cgroup_subsys_state *css, |
| 10211 | struct cftype *cft, s64 nice) |
| 10212 | { |
| 10213 | unsigned long weight; |
Peter Zijlstra | 7281c8d | 2018-04-20 14:29:51 +0200 | [diff] [blame] | 10214 | int idx; |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10215 | |
| 10216 | if (nice < MIN_NICE || nice > MAX_NICE) |
| 10217 | return -ERANGE; |
| 10218 | |
Peter Zijlstra | 7281c8d | 2018-04-20 14:29:51 +0200 | [diff] [blame] | 10219 | idx = NICE_TO_PRIO(nice) - MAX_RT_PRIO; |
| 10220 | idx = array_index_nospec(idx, 40); |
| 10221 | weight = sched_prio_to_weight[idx]; |
| 10222 | |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10223 | return sched_group_set_shares(css_tg(css), scale_load(weight)); |
| 10224 | } |
| 10225 | #endif |
| 10226 | |
| 10227 | static void __maybe_unused cpu_period_quota_print(struct seq_file *sf, |
| 10228 | long period, long quota) |
| 10229 | { |
| 10230 | if (quota < 0) |
| 10231 | seq_puts(sf, "max"); |
| 10232 | else |
| 10233 | seq_printf(sf, "%ld", quota); |
| 10234 | |
| 10235 | seq_printf(sf, " %ld\n", period); |
| 10236 | } |
| 10237 | |
| 10238 | /* caller should put the current value in *@periodp before calling */ |
| 10239 | static int __maybe_unused cpu_period_quota_parse(char *buf, |
| 10240 | u64 *periodp, u64 *quotap) |
| 10241 | { |
| 10242 | char tok[21]; /* U64_MAX */ |
| 10243 | |
Konstantin Khlebnikov | 4c47acd | 2019-03-06 20:11:42 +0300 | [diff] [blame] | 10244 | if (sscanf(buf, "%20s %llu", tok, periodp) < 1) |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10245 | return -EINVAL; |
| 10246 | |
| 10247 | *periodp *= NSEC_PER_USEC; |
| 10248 | |
| 10249 | if (sscanf(tok, "%llu", quotap)) |
| 10250 | *quotap *= NSEC_PER_USEC; |
| 10251 | else if (!strcmp(tok, "max")) |
| 10252 | *quotap = RUNTIME_INF; |
| 10253 | else |
| 10254 | return -EINVAL; |
| 10255 | |
| 10256 | return 0; |
| 10257 | } |
| 10258 | |
| 10259 | #ifdef CONFIG_CFS_BANDWIDTH |
| 10260 | static int cpu_max_show(struct seq_file *sf, void *v) |
| 10261 | { |
| 10262 | struct task_group *tg = css_tg(seq_css(sf)); |
| 10263 | |
| 10264 | cpu_period_quota_print(sf, tg_get_cfs_period(tg), tg_get_cfs_quota(tg)); |
| 10265 | return 0; |
| 10266 | } |
| 10267 | |
| 10268 | static ssize_t cpu_max_write(struct kernfs_open_file *of, |
| 10269 | char *buf, size_t nbytes, loff_t off) |
| 10270 | { |
| 10271 | struct task_group *tg = css_tg(of_css(of)); |
| 10272 | u64 period = tg_get_cfs_period(tg); |
| 10273 | u64 quota; |
| 10274 | int ret; |
| 10275 | |
| 10276 | ret = cpu_period_quota_parse(buf, &period, "a); |
| 10277 | if (!ret) |
| 10278 | ret = tg_set_cfs_bandwidth(tg, period, quota); |
| 10279 | return ret ?: nbytes; |
| 10280 | } |
| 10281 | #endif |
| 10282 | |
| 10283 | static struct cftype cpu_files[] = { |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10284 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 10285 | { |
| 10286 | .name = "weight", |
| 10287 | .flags = CFTYPE_NOT_ON_ROOT, |
| 10288 | .read_u64 = cpu_weight_read_u64, |
| 10289 | .write_u64 = cpu_weight_write_u64, |
| 10290 | }, |
| 10291 | { |
| 10292 | .name = "weight.nice", |
| 10293 | .flags = CFTYPE_NOT_ON_ROOT, |
| 10294 | .read_s64 = cpu_weight_nice_read_s64, |
| 10295 | .write_s64 = cpu_weight_nice_write_s64, |
| 10296 | }, |
| 10297 | #endif |
| 10298 | #ifdef CONFIG_CFS_BANDWIDTH |
| 10299 | { |
| 10300 | .name = "max", |
| 10301 | .flags = CFTYPE_NOT_ON_ROOT, |
| 10302 | .seq_show = cpu_max_show, |
| 10303 | .write = cpu_max_write, |
| 10304 | }, |
| 10305 | #endif |
Patrick Bellasi | 2480c09 | 2019-08-22 14:28:06 +0100 | [diff] [blame] | 10306 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
| 10307 | { |
| 10308 | .name = "uclamp.min", |
| 10309 | .flags = CFTYPE_NOT_ON_ROOT, |
| 10310 | .seq_show = cpu_uclamp_min_show, |
| 10311 | .write = cpu_uclamp_min_write, |
| 10312 | }, |
| 10313 | { |
| 10314 | .name = "uclamp.max", |
| 10315 | .flags = CFTYPE_NOT_ON_ROOT, |
| 10316 | .seq_show = cpu_uclamp_max_show, |
| 10317 | .write = cpu_uclamp_max_write, |
| 10318 | }, |
| 10319 | #endif |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10320 | { } /* terminate */ |
| 10321 | }; |
| 10322 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 10323 | struct cgroup_subsys cpu_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 10324 | .css_alloc = cpu_cgroup_css_alloc, |
Konstantin Khlebnikov | 96b7774 | 2017-02-08 14:27:27 +0300 | [diff] [blame] | 10325 | .css_online = cpu_cgroup_css_online, |
Peter Zijlstra | 2f5177f | 2016-03-16 16:22:45 +0100 | [diff] [blame] | 10326 | .css_released = cpu_cgroup_css_released, |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 10327 | .css_free = cpu_cgroup_css_free, |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 10328 | .css_extra_stat_show = cpu_extra_stat_show, |
Kirill Tkhai | eeb61e5 | 2014-10-27 14:18:25 +0400 | [diff] [blame] | 10329 | .fork = cpu_cgroup_fork, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 10330 | .can_attach = cpu_cgroup_can_attach, |
| 10331 | .attach = cpu_cgroup_attach, |
Tejun Heo | a1f7164 | 2017-09-25 09:00:18 -0700 | [diff] [blame] | 10332 | .legacy_cftypes = cpu_legacy_files, |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10333 | .dfl_cftypes = cpu_files, |
Tejun Heo | b38e42e | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 10334 | .early_init = true, |
Tejun Heo | 0d59363 | 2017-09-25 09:00:19 -0700 | [diff] [blame] | 10335 | .threaded = true, |
Srivatsa Vaddagiri | 68318b8 | 2007-10-18 23:41:03 -0700 | [diff] [blame] | 10336 | }; |
| 10337 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 10338 | #endif /* CONFIG_CGROUP_SCHED */ |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 10339 | |
Paul E. McKenney | b637a32 | 2012-09-19 16:58:38 -0700 | [diff] [blame] | 10340 | void dump_cpu_task(int cpu) |
| 10341 | { |
| 10342 | pr_info("Task dump for CPU %d:\n", cpu); |
| 10343 | sched_show_task(cpu_curr(cpu)); |
| 10344 | } |
Andi Kleen | ed82b8a | 2015-11-29 20:59:43 -0800 | [diff] [blame] | 10345 | |
| 10346 | /* |
| 10347 | * Nice levels are multiplicative, with a gentle 10% change for every |
| 10348 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to |
| 10349 | * nice 1, it will get ~10% less CPU time than another CPU-bound task |
| 10350 | * that remained on nice 0. |
| 10351 | * |
| 10352 | * The "10% effect" is relative and cumulative: from _any_ nice level, |
| 10353 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level |
| 10354 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
| 10355 | * If a task goes up by ~10% and another task goes down by ~10% then |
| 10356 | * the relative distance between them is ~25%.) |
| 10357 | */ |
| 10358 | const int sched_prio_to_weight[40] = { |
| 10359 | /* -20 */ 88761, 71755, 56483, 46273, 36291, |
| 10360 | /* -15 */ 29154, 23254, 18705, 14949, 11916, |
| 10361 | /* -10 */ 9548, 7620, 6100, 4904, 3906, |
| 10362 | /* -5 */ 3121, 2501, 1991, 1586, 1277, |
| 10363 | /* 0 */ 1024, 820, 655, 526, 423, |
| 10364 | /* 5 */ 335, 272, 215, 172, 137, |
| 10365 | /* 10 */ 110, 87, 70, 56, 45, |
| 10366 | /* 15 */ 36, 29, 23, 18, 15, |
| 10367 | }; |
| 10368 | |
| 10369 | /* |
| 10370 | * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated. |
| 10371 | * |
| 10372 | * In cases where the weight does not change often, we can use the |
| 10373 | * precalculated inverse to speed up arithmetics by turning divisions |
| 10374 | * into multiplications: |
| 10375 | */ |
| 10376 | const u32 sched_prio_to_wmult[40] = { |
| 10377 | /* -20 */ 48388, 59856, 76040, 92818, 118348, |
| 10378 | /* -15 */ 147320, 184698, 229616, 287308, 360437, |
| 10379 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, |
| 10380 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, |
| 10381 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, |
| 10382 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, |
| 10383 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, |
| 10384 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, |
| 10385 | }; |
Ingo Molnar | 14a7405 | 2018-03-03 16:32:24 +0100 | [diff] [blame] | 10386 | |
Phil Auld | 9d24605 | 2020-06-29 15:23:03 -0400 | [diff] [blame] | 10387 | void call_trace_sched_update_nr_running(struct rq *rq, int count) |
| 10388 | { |
| 10389 | trace_sched_update_nr_running_tp(rq, count); |
| 10390 | } |