Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Deadline Scheduling Class (SCHED_DEADLINE) |
| 4 | * |
| 5 | * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS). |
| 6 | * |
| 7 | * Tasks that periodically executes their instances for less than their |
| 8 | * runtime won't miss any of their deadlines. |
| 9 | * Tasks that are not periodic or sporadic or that tries to execute more |
| 10 | * than their reserved bandwidth will be slowed down (and may potentially |
| 11 | * miss some of their deadlines), and won't affect any other task. |
| 12 | * |
| 13 | * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>, |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 14 | * Juri Lelli <juri.lelli@gmail.com>, |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 15 | * Michael Trimarchi <michael@amarulasolutions.com>, |
| 16 | * Fabio Checconi <fchecconi@gmail.com> |
| 17 | */ |
| 18 | #include "sched.h" |
Vincent Guittot | 3727e0e | 2018-06-28 17:45:07 +0200 | [diff] [blame] | 19 | #include "pelt.h" |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 20 | |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 21 | struct dl_bandwidth def_dl_bandwidth; |
| 22 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 23 | static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se) |
| 24 | { |
| 25 | return container_of(dl_se, struct task_struct, dl); |
| 26 | } |
| 27 | |
| 28 | static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq) |
| 29 | { |
| 30 | return container_of(dl_rq, struct rq, dl); |
| 31 | } |
| 32 | |
| 33 | static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se) |
| 34 | { |
| 35 | struct task_struct *p = dl_task_of(dl_se); |
| 36 | struct rq *rq = task_rq(p); |
| 37 | |
| 38 | return &rq->dl; |
| 39 | } |
| 40 | |
| 41 | static inline int on_dl_rq(struct sched_dl_entity *dl_se) |
| 42 | { |
| 43 | return !RB_EMPTY_NODE(&dl_se->rb_node); |
| 44 | } |
| 45 | |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 46 | #ifdef CONFIG_SMP |
| 47 | static inline struct dl_bw *dl_bw_of(int i) |
| 48 | { |
| 49 | RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(), |
| 50 | "sched RCU must be held"); |
| 51 | return &cpu_rq(i)->rd->dl_bw; |
| 52 | } |
| 53 | |
| 54 | static inline int dl_bw_cpus(int i) |
| 55 | { |
| 56 | struct root_domain *rd = cpu_rq(i)->rd; |
| 57 | int cpus = 0; |
| 58 | |
| 59 | RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(), |
| 60 | "sched RCU must be held"); |
| 61 | for_each_cpu_and(i, rd->span, cpu_active_mask) |
| 62 | cpus++; |
| 63 | |
| 64 | return cpus; |
| 65 | } |
| 66 | #else |
| 67 | static inline struct dl_bw *dl_bw_of(int i) |
| 68 | { |
| 69 | return &cpu_rq(i)->dl.dl_bw; |
| 70 | } |
| 71 | |
| 72 | static inline int dl_bw_cpus(int i) |
| 73 | { |
| 74 | return 1; |
| 75 | } |
| 76 | #endif |
| 77 | |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 78 | static inline |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 79 | void __add_running_bw(u64 dl_bw, struct dl_rq *dl_rq) |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 80 | { |
| 81 | u64 old = dl_rq->running_bw; |
| 82 | |
| 83 | lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock); |
| 84 | dl_rq->running_bw += dl_bw; |
| 85 | SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */ |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 86 | SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw); |
Juri Lelli | e0367b1 | 2017-12-04 11:23:19 +0100 | [diff] [blame] | 87 | /* kick cpufreq (see the comment in kernel/sched/sched.h). */ |
Peter Zijlstra | 4042d003 | 2017-12-20 15:37:26 +0100 | [diff] [blame] | 88 | cpufreq_update_util(rq_of_dl_rq(dl_rq), 0); |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static inline |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 92 | void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq) |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 93 | { |
| 94 | u64 old = dl_rq->running_bw; |
| 95 | |
| 96 | lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock); |
| 97 | dl_rq->running_bw -= dl_bw; |
| 98 | SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */ |
| 99 | if (dl_rq->running_bw > old) |
| 100 | dl_rq->running_bw = 0; |
Juri Lelli | e0367b1 | 2017-12-04 11:23:19 +0100 | [diff] [blame] | 101 | /* kick cpufreq (see the comment in kernel/sched/sched.h). */ |
Peter Zijlstra | 4042d003 | 2017-12-20 15:37:26 +0100 | [diff] [blame] | 102 | cpufreq_update_util(rq_of_dl_rq(dl_rq), 0); |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 105 | static inline |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 106 | void __add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq) |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 107 | { |
| 108 | u64 old = dl_rq->this_bw; |
| 109 | |
| 110 | lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock); |
| 111 | dl_rq->this_bw += dl_bw; |
| 112 | SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */ |
| 113 | } |
| 114 | |
| 115 | static inline |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 116 | void __sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq) |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 117 | { |
| 118 | u64 old = dl_rq->this_bw; |
| 119 | |
| 120 | lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock); |
| 121 | dl_rq->this_bw -= dl_bw; |
| 122 | SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */ |
| 123 | if (dl_rq->this_bw > old) |
| 124 | dl_rq->this_bw = 0; |
| 125 | SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw); |
| 126 | } |
| 127 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 128 | static inline |
| 129 | void add_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 130 | { |
| 131 | if (!dl_entity_is_special(dl_se)) |
| 132 | __add_rq_bw(dl_se->dl_bw, dl_rq); |
| 133 | } |
| 134 | |
| 135 | static inline |
| 136 | void sub_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 137 | { |
| 138 | if (!dl_entity_is_special(dl_se)) |
| 139 | __sub_rq_bw(dl_se->dl_bw, dl_rq); |
| 140 | } |
| 141 | |
| 142 | static inline |
| 143 | void add_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 144 | { |
| 145 | if (!dl_entity_is_special(dl_se)) |
| 146 | __add_running_bw(dl_se->dl_bw, dl_rq); |
| 147 | } |
| 148 | |
| 149 | static inline |
| 150 | void sub_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 151 | { |
| 152 | if (!dl_entity_is_special(dl_se)) |
| 153 | __sub_running_bw(dl_se->dl_bw, dl_rq); |
| 154 | } |
| 155 | |
Yu Chen | ba4f7bc | 2020-02-28 18:03:29 +0800 | [diff] [blame] | 156 | static void dl_change_utilization(struct task_struct *p, u64 new_bw) |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 157 | { |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 158 | struct rq *rq; |
| 159 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 160 | BUG_ON(p->dl.flags & SCHED_FLAG_SUGOV); |
| 161 | |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 162 | if (task_on_rq_queued(p)) |
| 163 | return; |
| 164 | |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 165 | rq = task_rq(p); |
| 166 | if (p->dl.dl_non_contending) { |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 167 | sub_running_bw(&p->dl, &rq->dl); |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 168 | p->dl.dl_non_contending = 0; |
| 169 | /* |
| 170 | * If the timer handler is currently running and the |
| 171 | * timer cannot be cancelled, inactive_task_timer() |
| 172 | * will see that dl_not_contending is not set, and |
| 173 | * will not touch the rq's active utilization, |
| 174 | * so we are still safe. |
| 175 | */ |
| 176 | if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) |
| 177 | put_task_struct(p); |
| 178 | } |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 179 | __sub_rq_bw(p->dl.dl_bw, &rq->dl); |
| 180 | __add_rq_bw(new_bw, &rq->dl); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* |
| 184 | * The utilization of a task cannot be immediately removed from |
| 185 | * the rq active utilization (running_bw) when the task blocks. |
| 186 | * Instead, we have to wait for the so called "0-lag time". |
| 187 | * |
| 188 | * If a task blocks before the "0-lag time", a timer (the inactive |
| 189 | * timer) is armed, and running_bw is decreased when the timer |
| 190 | * fires. |
| 191 | * |
| 192 | * If the task wakes up again before the inactive timer fires, |
| 193 | * the timer is cancelled, whereas if the task wakes up after the |
| 194 | * inactive timer fired (and running_bw has been decreased) the |
| 195 | * task's utilization has to be added to running_bw again. |
| 196 | * A flag in the deadline scheduling entity (dl_non_contending) |
| 197 | * is used to avoid race conditions between the inactive timer handler |
| 198 | * and task wakeups. |
| 199 | * |
| 200 | * The following diagram shows how running_bw is updated. A task is |
| 201 | * "ACTIVE" when its utilization contributes to running_bw; an |
| 202 | * "ACTIVE contending" task is in the TASK_RUNNING state, while an |
| 203 | * "ACTIVE non contending" task is a blocked task for which the "0-lag time" |
| 204 | * has not passed yet. An "INACTIVE" task is a task for which the "0-lag" |
| 205 | * time already passed, which does not contribute to running_bw anymore. |
| 206 | * +------------------+ |
| 207 | * wakeup | ACTIVE | |
| 208 | * +------------------>+ contending | |
| 209 | * | add_running_bw | | |
| 210 | * | +----+------+------+ |
| 211 | * | | ^ |
| 212 | * | dequeue | | |
| 213 | * +--------+-------+ | | |
| 214 | * | | t >= 0-lag | | wakeup |
| 215 | * | INACTIVE |<---------------+ | |
| 216 | * | | sub_running_bw | | |
| 217 | * +--------+-------+ | | |
| 218 | * ^ | | |
| 219 | * | t < 0-lag | | |
| 220 | * | | | |
| 221 | * | V | |
| 222 | * | +----+------+------+ |
| 223 | * | sub_running_bw | ACTIVE | |
| 224 | * +-------------------+ | |
| 225 | * inactive timer | non contending | |
| 226 | * fired +------------------+ |
| 227 | * |
| 228 | * The task_non_contending() function is invoked when a task |
| 229 | * blocks, and checks if the 0-lag time already passed or |
| 230 | * not (in the first case, it directly updates running_bw; |
| 231 | * in the second case, it arms the inactive timer). |
| 232 | * |
| 233 | * The task_contending() function is invoked when a task wakes |
| 234 | * up, and checks if the task is still in the "ACTIVE non contending" |
| 235 | * state or not (in the second case, it updates running_bw). |
| 236 | */ |
| 237 | static void task_non_contending(struct task_struct *p) |
| 238 | { |
| 239 | struct sched_dl_entity *dl_se = &p->dl; |
| 240 | struct hrtimer *timer = &dl_se->inactive_timer; |
| 241 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); |
| 242 | struct rq *rq = rq_of_dl_rq(dl_rq); |
| 243 | s64 zerolag_time; |
| 244 | |
| 245 | /* |
| 246 | * If this is a non-deadline task that has been boosted, |
| 247 | * do nothing |
| 248 | */ |
| 249 | if (dl_se->dl_runtime == 0) |
| 250 | return; |
| 251 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 252 | if (dl_entity_is_special(dl_se)) |
| 253 | return; |
| 254 | |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 255 | WARN_ON(dl_se->dl_non_contending); |
| 256 | |
| 257 | zerolag_time = dl_se->deadline - |
| 258 | div64_long((dl_se->runtime * dl_se->dl_period), |
| 259 | dl_se->dl_runtime); |
| 260 | |
| 261 | /* |
| 262 | * Using relative times instead of the absolute "0-lag time" |
| 263 | * allows to simplify the code |
| 264 | */ |
| 265 | zerolag_time -= rq_clock(rq); |
| 266 | |
| 267 | /* |
| 268 | * If the "0-lag time" already passed, decrease the active |
| 269 | * utilization now, instead of starting a timer |
| 270 | */ |
luca abeni | 1b02cd6 | 2019-03-25 14:15:30 +0100 | [diff] [blame] | 271 | if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) { |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 272 | if (dl_task(p)) |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 273 | sub_running_bw(dl_se, dl_rq); |
Luca Abeni | 387e313 | 2017-05-18 22:13:30 +0200 | [diff] [blame] | 274 | if (!dl_task(p) || p->state == TASK_DEAD) { |
| 275 | struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); |
| 276 | |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 277 | if (p->state == TASK_DEAD) |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 278 | sub_rq_bw(&p->dl, &rq->dl); |
Luca Abeni | 387e313 | 2017-05-18 22:13:30 +0200 | [diff] [blame] | 279 | raw_spin_lock(&dl_b->lock); |
Peter Zijlstra | 8c0944ce | 2017-09-07 12:09:30 +0200 | [diff] [blame] | 280 | __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 281 | __dl_clear_params(p); |
Luca Abeni | 387e313 | 2017-05-18 22:13:30 +0200 | [diff] [blame] | 282 | raw_spin_unlock(&dl_b->lock); |
| 283 | } |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 284 | |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | dl_se->dl_non_contending = 1; |
| 289 | get_task_struct(p); |
Juri Lelli | 850377a | 2019-07-31 12:37:15 +0200 | [diff] [blame] | 290 | hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 293 | static void task_contending(struct sched_dl_entity *dl_se, int flags) |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 294 | { |
| 295 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); |
| 296 | |
| 297 | /* |
| 298 | * If this is a non-deadline task that has been boosted, |
| 299 | * do nothing |
| 300 | */ |
| 301 | if (dl_se->dl_runtime == 0) |
| 302 | return; |
| 303 | |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 304 | if (flags & ENQUEUE_MIGRATED) |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 305 | add_rq_bw(dl_se, dl_rq); |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 306 | |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 307 | if (dl_se->dl_non_contending) { |
| 308 | dl_se->dl_non_contending = 0; |
| 309 | /* |
| 310 | * If the timer handler is currently running and the |
| 311 | * timer cannot be cancelled, inactive_task_timer() |
| 312 | * will see that dl_not_contending is not set, and |
| 313 | * will not touch the rq's active utilization, |
| 314 | * so we are still safe. |
| 315 | */ |
| 316 | if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1) |
| 317 | put_task_struct(dl_task_of(dl_se)); |
| 318 | } else { |
| 319 | /* |
| 320 | * Since "dl_non_contending" is not set, the |
| 321 | * task's utilization has already been removed from |
| 322 | * active utilization (either when the task blocked, |
| 323 | * when the "inactive timer" fired). |
| 324 | * So, add it back. |
| 325 | */ |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 326 | add_running_bw(dl_se, dl_rq); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 330 | static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) |
| 331 | { |
| 332 | struct sched_dl_entity *dl_se = &p->dl; |
| 333 | |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 334 | return dl_rq->root.rb_leftmost == &dl_se->rb_node; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 335 | } |
| 336 | |
Yu Chen | ba4f7bc | 2020-02-28 18:03:29 +0800 | [diff] [blame] | 337 | static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq); |
| 338 | |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 339 | void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime) |
| 340 | { |
| 341 | raw_spin_lock_init(&dl_b->dl_runtime_lock); |
| 342 | dl_b->dl_period = period; |
| 343 | dl_b->dl_runtime = runtime; |
| 344 | } |
| 345 | |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 346 | void init_dl_bw(struct dl_bw *dl_b) |
| 347 | { |
| 348 | raw_spin_lock_init(&dl_b->lock); |
| 349 | raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock); |
Peter Zijlstra | 1724813 | 2013-12-17 12:44:49 +0100 | [diff] [blame] | 350 | if (global_rt_runtime() == RUNTIME_INF) |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 351 | dl_b->bw = -1; |
| 352 | else |
Peter Zijlstra | 1724813 | 2013-12-17 12:44:49 +0100 | [diff] [blame] | 353 | dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime()); |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 354 | raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock); |
| 355 | dl_b->total_bw = 0; |
| 356 | } |
| 357 | |
Abel Vesa | 07c54f7 | 2015-03-03 13:50:27 +0200 | [diff] [blame] | 358 | void init_dl_rq(struct dl_rq *dl_rq) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 359 | { |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 360 | dl_rq->root = RB_ROOT_CACHED; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 361 | |
| 362 | #ifdef CONFIG_SMP |
| 363 | /* zero means no -deadline tasks */ |
| 364 | dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0; |
| 365 | |
| 366 | dl_rq->dl_nr_migratory = 0; |
| 367 | dl_rq->overloaded = 0; |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 368 | dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED; |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 369 | #else |
| 370 | init_dl_bw(&dl_rq->dl_bw); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 371 | #endif |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 372 | |
| 373 | dl_rq->running_bw = 0; |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 374 | dl_rq->this_bw = 0; |
Luca Abeni | 4da3abc | 2017-05-18 22:13:32 +0200 | [diff] [blame] | 375 | init_dl_rq_bw_ratio(dl_rq); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 378 | #ifdef CONFIG_SMP |
| 379 | |
| 380 | static inline int dl_overloaded(struct rq *rq) |
| 381 | { |
| 382 | return atomic_read(&rq->rd->dlo_count); |
| 383 | } |
| 384 | |
| 385 | static inline void dl_set_overload(struct rq *rq) |
| 386 | { |
| 387 | if (!rq->online) |
| 388 | return; |
| 389 | |
| 390 | cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask); |
| 391 | /* |
| 392 | * Must be visible before the overload count is |
| 393 | * set (as in sched_rt.c). |
| 394 | * |
| 395 | * Matched by the barrier in pull_dl_task(). |
| 396 | */ |
| 397 | smp_wmb(); |
| 398 | atomic_inc(&rq->rd->dlo_count); |
| 399 | } |
| 400 | |
| 401 | static inline void dl_clear_overload(struct rq *rq) |
| 402 | { |
| 403 | if (!rq->online) |
| 404 | return; |
| 405 | |
| 406 | atomic_dec(&rq->rd->dlo_count); |
| 407 | cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask); |
| 408 | } |
| 409 | |
| 410 | static void update_dl_migration(struct dl_rq *dl_rq) |
| 411 | { |
Kirill Tkhai | 995b9ea | 2014-02-18 02:24:13 +0400 | [diff] [blame] | 412 | if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 413 | if (!dl_rq->overloaded) { |
| 414 | dl_set_overload(rq_of_dl_rq(dl_rq)); |
| 415 | dl_rq->overloaded = 1; |
| 416 | } |
| 417 | } else if (dl_rq->overloaded) { |
| 418 | dl_clear_overload(rq_of_dl_rq(dl_rq)); |
| 419 | dl_rq->overloaded = 0; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 424 | { |
| 425 | struct task_struct *p = dl_task_of(dl_se); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 426 | |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 427 | if (p->nr_cpus_allowed > 1) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 428 | dl_rq->dl_nr_migratory++; |
| 429 | |
| 430 | update_dl_migration(dl_rq); |
| 431 | } |
| 432 | |
| 433 | static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 434 | { |
| 435 | struct task_struct *p = dl_task_of(dl_se); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 436 | |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 437 | if (p->nr_cpus_allowed > 1) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 438 | dl_rq->dl_nr_migratory--; |
| 439 | |
| 440 | update_dl_migration(dl_rq); |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * The list of pushable -deadline task is not a plist, like in |
| 445 | * sched_rt.c, it is an rb-tree with tasks ordered by deadline. |
| 446 | */ |
| 447 | static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) |
| 448 | { |
| 449 | struct dl_rq *dl_rq = &rq->dl; |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 450 | struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_root.rb_node; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 451 | struct rb_node *parent = NULL; |
| 452 | struct task_struct *entry; |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 453 | bool leftmost = true; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 454 | |
| 455 | BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks)); |
| 456 | |
| 457 | while (*link) { |
| 458 | parent = *link; |
| 459 | entry = rb_entry(parent, struct task_struct, |
| 460 | pushable_dl_tasks); |
| 461 | if (dl_entity_preempt(&p->dl, &entry->dl)) |
| 462 | link = &parent->rb_left; |
| 463 | else { |
| 464 | link = &parent->rb_right; |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 465 | leftmost = false; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 469 | if (leftmost) |
Wanpeng Li | 7d92de3 | 2015-12-03 17:42:10 +0800 | [diff] [blame] | 470 | dl_rq->earliest_dl.next = p->dl.deadline; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 471 | |
| 472 | rb_link_node(&p->pushable_dl_tasks, parent, link); |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 473 | rb_insert_color_cached(&p->pushable_dl_tasks, |
| 474 | &dl_rq->pushable_dl_tasks_root, leftmost); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p) |
| 478 | { |
| 479 | struct dl_rq *dl_rq = &rq->dl; |
| 480 | |
| 481 | if (RB_EMPTY_NODE(&p->pushable_dl_tasks)) |
| 482 | return; |
| 483 | |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 484 | if (dl_rq->pushable_dl_tasks_root.rb_leftmost == &p->pushable_dl_tasks) { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 485 | struct rb_node *next_node; |
| 486 | |
| 487 | next_node = rb_next(&p->pushable_dl_tasks); |
Wanpeng Li | 7d92de3 | 2015-12-03 17:42:10 +0800 | [diff] [blame] | 488 | if (next_node) { |
| 489 | dl_rq->earliest_dl.next = rb_entry(next_node, |
| 490 | struct task_struct, pushable_dl_tasks)->dl.deadline; |
| 491 | } |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 494 | rb_erase_cached(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 495 | RB_CLEAR_NODE(&p->pushable_dl_tasks); |
| 496 | } |
| 497 | |
| 498 | static inline int has_pushable_dl_tasks(struct rq *rq) |
| 499 | { |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 500 | return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static int push_dl_task(struct rq *rq); |
| 504 | |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 505 | static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev) |
| 506 | { |
| 507 | return dl_task(prev); |
| 508 | } |
| 509 | |
Peter Zijlstra | 9916e21 | 2015-06-11 14:46:43 +0200 | [diff] [blame] | 510 | static DEFINE_PER_CPU(struct callback_head, dl_push_head); |
| 511 | static DEFINE_PER_CPU(struct callback_head, dl_pull_head); |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 512 | |
| 513 | static void push_dl_tasks(struct rq *); |
Peter Zijlstra | 9916e21 | 2015-06-11 14:46:43 +0200 | [diff] [blame] | 514 | static void pull_dl_task(struct rq *); |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 515 | |
Ingo Molnar | 02d8ec9 | 2018-03-03 16:27:54 +0100 | [diff] [blame] | 516 | static inline void deadline_queue_push_tasks(struct rq *rq) |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 517 | { |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 518 | if (!has_pushable_dl_tasks(rq)) |
| 519 | return; |
| 520 | |
Peter Zijlstra | 9916e21 | 2015-06-11 14:46:43 +0200 | [diff] [blame] | 521 | queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks); |
| 522 | } |
| 523 | |
Ingo Molnar | 02d8ec9 | 2018-03-03 16:27:54 +0100 | [diff] [blame] | 524 | static inline void deadline_queue_pull_task(struct rq *rq) |
Peter Zijlstra | 9916e21 | 2015-06-11 14:46:43 +0200 | [diff] [blame] | 525 | { |
| 526 | queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task); |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Wanpeng Li | fa9c9d1 | 2015-03-27 07:08:35 +0800 | [diff] [blame] | 529 | static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq); |
| 530 | |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 531 | static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p) |
Wanpeng Li | fa9c9d1 | 2015-03-27 07:08:35 +0800 | [diff] [blame] | 532 | { |
| 533 | struct rq *later_rq = NULL; |
Juri Lelli | 59d06ce | 2019-07-19 15:59:56 +0200 | [diff] [blame] | 534 | struct dl_bw *dl_b; |
Wanpeng Li | fa9c9d1 | 2015-03-27 07:08:35 +0800 | [diff] [blame] | 535 | |
| 536 | later_rq = find_lock_later_rq(p, rq); |
Wanpeng Li | fa9c9d1 | 2015-03-27 07:08:35 +0800 | [diff] [blame] | 537 | if (!later_rq) { |
| 538 | int cpu; |
| 539 | |
| 540 | /* |
| 541 | * If we cannot preempt any rq, fall back to pick any |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 542 | * online CPU: |
Wanpeng Li | fa9c9d1 | 2015-03-27 07:08:35 +0800 | [diff] [blame] | 543 | */ |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 544 | cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr); |
Wanpeng Li | fa9c9d1 | 2015-03-27 07:08:35 +0800 | [diff] [blame] | 545 | if (cpu >= nr_cpu_ids) { |
| 546 | /* |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 547 | * Failed to find any suitable CPU. |
Wanpeng Li | fa9c9d1 | 2015-03-27 07:08:35 +0800 | [diff] [blame] | 548 | * The task will never come back! |
| 549 | */ |
| 550 | BUG_ON(dl_bandwidth_enabled()); |
| 551 | |
| 552 | /* |
| 553 | * If admission control is disabled we |
| 554 | * try a little harder to let the task |
| 555 | * run. |
| 556 | */ |
| 557 | cpu = cpumask_any(cpu_active_mask); |
| 558 | } |
| 559 | later_rq = cpu_rq(cpu); |
| 560 | double_lock_balance(rq, later_rq); |
| 561 | } |
| 562 | |
Juri Lelli | 59d06ce | 2019-07-19 15:59:56 +0200 | [diff] [blame] | 563 | if (p->dl.dl_non_contending || p->dl.dl_throttled) { |
| 564 | /* |
| 565 | * Inactive timer is armed (or callback is running, but |
| 566 | * waiting for us to release rq locks). In any case, when it |
| 567 | * will fire (or continue), it will see running_bw of this |
| 568 | * task migrated to later_rq (and correctly handle it). |
| 569 | */ |
| 570 | sub_running_bw(&p->dl, &rq->dl); |
| 571 | sub_rq_bw(&p->dl, &rq->dl); |
| 572 | |
| 573 | add_rq_bw(&p->dl, &later_rq->dl); |
| 574 | add_running_bw(&p->dl, &later_rq->dl); |
| 575 | } else { |
| 576 | sub_rq_bw(&p->dl, &rq->dl); |
| 577 | add_rq_bw(&p->dl, &later_rq->dl); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * And we finally need to fixup root_domain(s) bandwidth accounting, |
| 582 | * since p is still hanging out in the old (now moved to default) root |
| 583 | * domain. |
| 584 | */ |
| 585 | dl_b = &rq->rd->dl_bw; |
| 586 | raw_spin_lock(&dl_b->lock); |
| 587 | __dl_sub(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span)); |
| 588 | raw_spin_unlock(&dl_b->lock); |
| 589 | |
| 590 | dl_b = &later_rq->rd->dl_bw; |
| 591 | raw_spin_lock(&dl_b->lock); |
| 592 | __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(later_rq->rd->span)); |
| 593 | raw_spin_unlock(&dl_b->lock); |
| 594 | |
Wanpeng Li | fa9c9d1 | 2015-03-27 07:08:35 +0800 | [diff] [blame] | 595 | set_task_cpu(p, later_rq->cpu); |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 596 | double_unlock_balance(later_rq, rq); |
| 597 | |
| 598 | return later_rq; |
Wanpeng Li | fa9c9d1 | 2015-03-27 07:08:35 +0800 | [diff] [blame] | 599 | } |
| 600 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 601 | #else |
| 602 | |
| 603 | static inline |
| 604 | void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) |
| 605 | { |
| 606 | } |
| 607 | |
| 608 | static inline |
| 609 | void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p) |
| 610 | { |
| 611 | } |
| 612 | |
| 613 | static inline |
| 614 | void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 615 | { |
| 616 | } |
| 617 | |
| 618 | static inline |
| 619 | void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 620 | { |
| 621 | } |
| 622 | |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 623 | static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev) |
| 624 | { |
| 625 | return false; |
| 626 | } |
| 627 | |
Peter Zijlstra | 0ea60c2 | 2015-06-11 14:46:42 +0200 | [diff] [blame] | 628 | static inline void pull_dl_task(struct rq *rq) |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 629 | { |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Ingo Molnar | 02d8ec9 | 2018-03-03 16:27:54 +0100 | [diff] [blame] | 632 | static inline void deadline_queue_push_tasks(struct rq *rq) |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 633 | { |
| 634 | } |
Peter Zijlstra | 9916e21 | 2015-06-11 14:46:43 +0200 | [diff] [blame] | 635 | |
Ingo Molnar | 02d8ec9 | 2018-03-03 16:27:54 +0100 | [diff] [blame] | 636 | static inline void deadline_queue_pull_task(struct rq *rq) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 637 | { |
| 638 | } |
| 639 | #endif /* CONFIG_SMP */ |
| 640 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 641 | static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags); |
| 642 | static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags); |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 643 | static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, int flags); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 644 | |
| 645 | /* |
| 646 | * We are being explicitly informed that a new instance is starting, |
| 647 | * and this means that: |
| 648 | * - the absolute deadline of the entity has to be placed at |
| 649 | * current time + relative deadline; |
| 650 | * - the runtime of the entity has to be set to the maximum value. |
| 651 | * |
| 652 | * The capability of specifying such event is useful whenever a -deadline |
| 653 | * entity wants to (try to!) synchronize its behaviour with the scheduler's |
| 654 | * one, and to (try to!) reconcile itself with its own scheduling |
| 655 | * parameters. |
| 656 | */ |
Juri Lelli | 98b0a85 | 2016-08-05 16:07:55 +0100 | [diff] [blame] | 657 | static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 658 | { |
| 659 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); |
| 660 | struct rq *rq = rq_of_dl_rq(dl_rq); |
| 661 | |
Juri Lelli | 98b0a85 | 2016-08-05 16:07:55 +0100 | [diff] [blame] | 662 | WARN_ON(dl_se->dl_boosted); |
Luca Abeni | 72f9f3f | 2016-03-07 12:27:04 +0100 | [diff] [blame] | 663 | WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline)); |
| 664 | |
| 665 | /* |
| 666 | * We are racing with the deadline timer. So, do nothing because |
| 667 | * the deadline timer handler will take care of properly recharging |
| 668 | * the runtime and postponing the deadline |
| 669 | */ |
| 670 | if (dl_se->dl_throttled) |
| 671 | return; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 672 | |
| 673 | /* |
| 674 | * We use the regular wall clock time to set deadlines in the |
| 675 | * future; in fact, we must consider execution overheads (time |
| 676 | * spent on hardirq context, etc.). |
| 677 | */ |
Juri Lelli | 98b0a85 | 2016-08-05 16:07:55 +0100 | [diff] [blame] | 678 | dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline; |
| 679 | dl_se->runtime = dl_se->dl_runtime; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* |
| 683 | * Pure Earliest Deadline First (EDF) scheduling does not deal with the |
| 684 | * possibility of a entity lasting more than what it declared, and thus |
| 685 | * exhausting its runtime. |
| 686 | * |
| 687 | * Here we are interested in making runtime overrun possible, but we do |
| 688 | * not want a entity which is misbehaving to affect the scheduling of all |
| 689 | * other entities. |
| 690 | * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS) |
| 691 | * is used, in order to confine each entity within its own bandwidth. |
| 692 | * |
| 693 | * This function deals exactly with that, and ensures that when the runtime |
| 694 | * of a entity is replenished, its deadline is also postponed. That ensures |
| 695 | * the overrunning entity can't interfere with other entity in the system and |
| 696 | * can't make them miss their deadlines. Reasons why this kind of overruns |
| 697 | * could happen are, typically, a entity voluntarily trying to overcome its |
xiaofeng.yan | 1b09d29 | 2014-07-07 05:59:04 +0000 | [diff] [blame] | 698 | * runtime, or it just underestimated it during sched_setattr(). |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 699 | */ |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 700 | static void replenish_dl_entity(struct sched_dl_entity *dl_se, |
| 701 | struct sched_dl_entity *pi_se) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 702 | { |
| 703 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); |
| 704 | struct rq *rq = rq_of_dl_rq(dl_rq); |
| 705 | |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 706 | BUG_ON(pi_se->dl_runtime <= 0); |
| 707 | |
| 708 | /* |
| 709 | * This could be the case for a !-dl task that is boosted. |
| 710 | * Just go with full inherited parameters. |
| 711 | */ |
| 712 | if (dl_se->dl_deadline == 0) { |
| 713 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; |
| 714 | dl_se->runtime = pi_se->dl_runtime; |
| 715 | } |
| 716 | |
Peter Zijlstra | 48be3a6 | 2016-02-23 13:28:22 +0100 | [diff] [blame] | 717 | if (dl_se->dl_yielded && dl_se->runtime > 0) |
| 718 | dl_se->runtime = 0; |
| 719 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 720 | /* |
| 721 | * We keep moving the deadline away until we get some |
| 722 | * available runtime for the entity. This ensures correct |
| 723 | * handling of situations where the runtime overrun is |
| 724 | * arbitrary large. |
| 725 | */ |
| 726 | while (dl_se->runtime <= 0) { |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 727 | dl_se->deadline += pi_se->dl_period; |
| 728 | dl_se->runtime += pi_se->dl_runtime; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /* |
| 732 | * At this point, the deadline really should be "in |
| 733 | * the future" with respect to rq->clock. If it's |
| 734 | * not, we are, for some reason, lagging too much! |
| 735 | * Anyway, after having warn userspace abut that, |
| 736 | * we still try to keep the things running by |
| 737 | * resetting the deadline and the budget of the |
| 738 | * entity. |
| 739 | */ |
| 740 | if (dl_time_before(dl_se->deadline, rq_clock(rq))) { |
Steven Rostedt | c219b7d | 2016-02-10 12:04:22 -0500 | [diff] [blame] | 741 | printk_deferred_once("sched: DL replenish lagged too much\n"); |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 742 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; |
| 743 | dl_se->runtime = pi_se->dl_runtime; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 744 | } |
Peter Zijlstra | 1019a35 | 2014-11-26 08:44:03 +0800 | [diff] [blame] | 745 | |
| 746 | if (dl_se->dl_yielded) |
| 747 | dl_se->dl_yielded = 0; |
| 748 | if (dl_se->dl_throttled) |
| 749 | dl_se->dl_throttled = 0; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Here we check if --at time t-- an entity (which is probably being |
| 754 | * [re]activated or, in general, enqueued) can use its remaining runtime |
| 755 | * and its current deadline _without_ exceeding the bandwidth it is |
| 756 | * assigned (function returns true if it can't). We are in fact applying |
| 757 | * one of the CBS rules: when a task wakes up, if the residual runtime |
| 758 | * over residual deadline fits within the allocated bandwidth, then we |
| 759 | * can keep the current (absolute) deadline and residual budget without |
| 760 | * disrupting the schedulability of the system. Otherwise, we should |
| 761 | * refill the runtime and set the deadline a period in the future, |
| 762 | * because keeping the current (absolute) deadline of the task would |
Dario Faggioli | 712e5e3 | 2014-01-27 12:20:15 +0100 | [diff] [blame] | 763 | * result in breaking guarantees promised to other tasks (refer to |
Mauro Carvalho Chehab | d6a3b24 | 2019-06-12 14:53:03 -0300 | [diff] [blame] | 764 | * Documentation/scheduler/sched-deadline.rst for more information). |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 765 | * |
| 766 | * This function returns true if: |
| 767 | * |
Steven Rostedt (VMware) | 2317d5f | 2017-03-02 15:10:59 +0100 | [diff] [blame] | 768 | * runtime / (deadline - t) > dl_runtime / dl_deadline , |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 769 | * |
| 770 | * IOW we can't recycle current parameters. |
Harald Gustafsson | 755378a | 2013-11-07 14:43:40 +0100 | [diff] [blame] | 771 | * |
Steven Rostedt (VMware) | 2317d5f | 2017-03-02 15:10:59 +0100 | [diff] [blame] | 772 | * Notice that the bandwidth check is done against the deadline. For |
Harald Gustafsson | 755378a | 2013-11-07 14:43:40 +0100 | [diff] [blame] | 773 | * task with deadline equal to period this is the same of using |
Steven Rostedt (VMware) | 2317d5f | 2017-03-02 15:10:59 +0100 | [diff] [blame] | 774 | * dl_period instead of dl_deadline in the equation above. |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 775 | */ |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 776 | static bool dl_entity_overflow(struct sched_dl_entity *dl_se, |
| 777 | struct sched_dl_entity *pi_se, u64 t) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 778 | { |
| 779 | u64 left, right; |
| 780 | |
| 781 | /* |
| 782 | * left and right are the two sides of the equation above, |
| 783 | * after a bit of shuffling to use multiplications instead |
| 784 | * of divisions. |
| 785 | * |
| 786 | * Note that none of the time values involved in the two |
| 787 | * multiplications are absolute: dl_deadline and dl_runtime |
| 788 | * are the relative deadline and the maximum runtime of each |
| 789 | * instance, runtime is the runtime left for the last instance |
| 790 | * and (deadline - t), since t is rq->clock, is the time left |
| 791 | * to the (absolute) deadline. Even if overflowing the u64 type |
| 792 | * is very unlikely to occur in both cases, here we scale down |
| 793 | * as we want to avoid that risk at all. Scaling down by 10 |
| 794 | * means that we reduce granularity to 1us. We are fine with it, |
| 795 | * since this is only a true/false check and, anyway, thinking |
| 796 | * of anything below microseconds resolution is actually fiction |
| 797 | * (but still we want to give the user that illusion >;). |
| 798 | */ |
Steven Rostedt (VMware) | 2317d5f | 2017-03-02 15:10:59 +0100 | [diff] [blame] | 799 | left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE); |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 800 | right = ((dl_se->deadline - t) >> DL_SCALE) * |
| 801 | (pi_se->dl_runtime >> DL_SCALE); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 802 | |
| 803 | return dl_time_before(right, left); |
| 804 | } |
| 805 | |
| 806 | /* |
Daniel Bristot de Oliveira | 3effcb4 | 2017-05-29 16:24:03 +0200 | [diff] [blame] | 807 | * Revised wakeup rule [1]: For self-suspending tasks, rather then |
| 808 | * re-initializing task's runtime and deadline, the revised wakeup |
| 809 | * rule adjusts the task's runtime to avoid the task to overrun its |
| 810 | * density. |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 811 | * |
Daniel Bristot de Oliveira | 3effcb4 | 2017-05-29 16:24:03 +0200 | [diff] [blame] | 812 | * Reasoning: a task may overrun the density if: |
| 813 | * runtime / (deadline - t) > dl_runtime / dl_deadline |
| 814 | * |
| 815 | * Therefore, runtime can be adjusted to: |
| 816 | * runtime = (dl_runtime / dl_deadline) * (deadline - t) |
| 817 | * |
| 818 | * In such way that runtime will be equal to the maximum density |
| 819 | * the task can use without breaking any rule. |
| 820 | * |
| 821 | * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant |
| 822 | * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24. |
| 823 | */ |
| 824 | static void |
| 825 | update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq) |
| 826 | { |
| 827 | u64 laxity = dl_se->deadline - rq_clock(rq); |
| 828 | |
| 829 | /* |
| 830 | * If the task has deadline < period, and the deadline is in the past, |
| 831 | * it should already be throttled before this check. |
| 832 | * |
| 833 | * See update_dl_entity() comments for further details. |
| 834 | */ |
| 835 | WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq))); |
| 836 | |
| 837 | dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Regarding the deadline, a task with implicit deadline has a relative |
| 842 | * deadline == relative period. A task with constrained deadline has a |
| 843 | * relative deadline <= relative period. |
| 844 | * |
| 845 | * We support constrained deadline tasks. However, there are some restrictions |
| 846 | * applied only for tasks which do not have an implicit deadline. See |
| 847 | * update_dl_entity() to know more about such restrictions. |
| 848 | * |
| 849 | * The dl_is_implicit() returns true if the task has an implicit deadline. |
| 850 | */ |
| 851 | static inline bool dl_is_implicit(struct sched_dl_entity *dl_se) |
| 852 | { |
| 853 | return dl_se->dl_deadline == dl_se->dl_period; |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * When a deadline entity is placed in the runqueue, its runtime and deadline |
| 858 | * might need to be updated. This is done by a CBS wake up rule. There are two |
| 859 | * different rules: 1) the original CBS; and 2) the Revisited CBS. |
| 860 | * |
| 861 | * When the task is starting a new period, the Original CBS is used. In this |
| 862 | * case, the runtime is replenished and a new absolute deadline is set. |
| 863 | * |
| 864 | * When a task is queued before the begin of the next period, using the |
| 865 | * remaining runtime and deadline could make the entity to overflow, see |
| 866 | * dl_entity_overflow() to find more about runtime overflow. When such case |
| 867 | * is detected, the runtime and deadline need to be updated. |
| 868 | * |
| 869 | * If the task has an implicit deadline, i.e., deadline == period, the Original |
| 870 | * CBS is applied. the runtime is replenished and a new absolute deadline is |
| 871 | * set, as in the previous cases. |
| 872 | * |
| 873 | * However, the Original CBS does not work properly for tasks with |
| 874 | * deadline < period, which are said to have a constrained deadline. By |
| 875 | * applying the Original CBS, a constrained deadline task would be able to run |
| 876 | * runtime/deadline in a period. With deadline < period, the task would |
| 877 | * overrun the runtime/period allowed bandwidth, breaking the admission test. |
| 878 | * |
| 879 | * In order to prevent this misbehave, the Revisited CBS is used for |
| 880 | * constrained deadline tasks when a runtime overflow is detected. In the |
| 881 | * Revisited CBS, rather than replenishing & setting a new absolute deadline, |
| 882 | * the remaining runtime of the task is reduced to avoid runtime overflow. |
| 883 | * Please refer to the comments update_dl_revised_wakeup() function to find |
| 884 | * more about the Revised CBS rule. |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 885 | */ |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 886 | static void update_dl_entity(struct sched_dl_entity *dl_se, |
| 887 | struct sched_dl_entity *pi_se) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 888 | { |
| 889 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); |
| 890 | struct rq *rq = rq_of_dl_rq(dl_rq); |
| 891 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 892 | if (dl_time_before(dl_se->deadline, rq_clock(rq)) || |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 893 | dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) { |
Daniel Bristot de Oliveira | 3effcb4 | 2017-05-29 16:24:03 +0200 | [diff] [blame] | 894 | |
| 895 | if (unlikely(!dl_is_implicit(dl_se) && |
| 896 | !dl_time_before(dl_se->deadline, rq_clock(rq)) && |
| 897 | !dl_se->dl_boosted)){ |
| 898 | update_dl_revised_wakeup(dl_se, rq); |
| 899 | return; |
| 900 | } |
| 901 | |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 902 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; |
| 903 | dl_se->runtime = pi_se->dl_runtime; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
Daniel Bristot de Oliveira | 5ac69d3 | 2017-03-02 15:10:57 +0100 | [diff] [blame] | 907 | static inline u64 dl_next_period(struct sched_dl_entity *dl_se) |
| 908 | { |
| 909 | return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period; |
| 910 | } |
| 911 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 912 | /* |
| 913 | * If the entity depleted all its runtime, and if we want it to sleep |
| 914 | * while waiting for some new execution time to become available, we |
Daniel Bristot de Oliveira | 5ac69d3 | 2017-03-02 15:10:57 +0100 | [diff] [blame] | 915 | * set the bandwidth replenishment timer to the replenishment instant |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 916 | * and try to activate it. |
| 917 | * |
| 918 | * Notice that it is important for the caller to know if the timer |
| 919 | * actually started or not (i.e., the replenishment instant is in |
| 920 | * the future or in the past). |
| 921 | */ |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 922 | static int start_dl_timer(struct task_struct *p) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 923 | { |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 924 | struct sched_dl_entity *dl_se = &p->dl; |
| 925 | struct hrtimer *timer = &dl_se->dl_timer; |
| 926 | struct rq *rq = task_rq(p); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 927 | ktime_t now, act; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 928 | s64 delta; |
| 929 | |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 930 | lockdep_assert_held(&rq->lock); |
| 931 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 932 | /* |
| 933 | * We want the timer to fire at the deadline, but considering |
| 934 | * that it is actually coming from rq->clock and not from |
| 935 | * hrtimer's time base reading. |
| 936 | */ |
Daniel Bristot de Oliveira | 5ac69d3 | 2017-03-02 15:10:57 +0100 | [diff] [blame] | 937 | act = ns_to_ktime(dl_next_period(dl_se)); |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 938 | now = hrtimer_cb_get_time(timer); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 939 | delta = ktime_to_ns(now) - rq_clock(rq); |
| 940 | act = ktime_add_ns(act, delta); |
| 941 | |
| 942 | /* |
| 943 | * If the expiry time already passed, e.g., because the value |
| 944 | * chosen as the deadline is too small, don't even try to |
| 945 | * start the timer in the past! |
| 946 | */ |
| 947 | if (ktime_us_delta(act, now) < 0) |
| 948 | return 0; |
| 949 | |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 950 | /* |
| 951 | * !enqueued will guarantee another callback; even if one is already in |
| 952 | * progress. This ensures a balanced {get,put}_task_struct(). |
| 953 | * |
| 954 | * The race against __run_timer() clearing the enqueued state is |
| 955 | * harmless because we're holding task_rq()->lock, therefore the timer |
| 956 | * expiring after we've done the check will wait on its task_rq_lock() |
| 957 | * and observe our state. |
| 958 | */ |
| 959 | if (!hrtimer_is_queued(timer)) { |
| 960 | get_task_struct(p); |
Sebastian Andrzej Siewior | d5096aa | 2019-07-26 20:30:52 +0200 | [diff] [blame] | 961 | hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD); |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 962 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 963 | |
Thomas Gleixner | cc9684d | 2015-04-14 21:09:06 +0000 | [diff] [blame] | 964 | return 1; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /* |
| 968 | * This is the bandwidth enforcement timer callback. If here, we know |
| 969 | * a task is not on its dl_rq, since the fact that the timer was running |
| 970 | * means the task is throttled and needs a runtime replenishment. |
| 971 | * |
| 972 | * However, what we actually do depends on the fact the task is active, |
| 973 | * (it is on its rq) or has been removed from there by a call to |
| 974 | * dequeue_task_dl(). In the former case we must issue the runtime |
| 975 | * replenishment and add the task back to the dl_rq; in the latter, we just |
| 976 | * do nothing but clearing dl_throttled, so that runtime and deadline |
| 977 | * updating (and the queueing back to dl_rq) will be done by the |
| 978 | * next call to enqueue_task_dl(). |
| 979 | */ |
| 980 | static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) |
| 981 | { |
| 982 | struct sched_dl_entity *dl_se = container_of(timer, |
| 983 | struct sched_dl_entity, |
| 984 | dl_timer); |
| 985 | struct task_struct *p = dl_task_of(dl_se); |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 986 | struct rq_flags rf; |
Kirill Tkhai | 0f397f2 | 2014-05-20 13:33:42 +0400 | [diff] [blame] | 987 | struct rq *rq; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 988 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 989 | rq = task_rq_lock(p, &rf); |
Kirill Tkhai | 0f397f2 | 2014-05-20 13:33:42 +0400 | [diff] [blame] | 990 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 991 | /* |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 992 | * The task might have changed its scheduling policy to something |
Daniel Bristot de Oliveira | 9846d50 | 2016-11-08 11:15:23 +0100 | [diff] [blame] | 993 | * different than SCHED_DEADLINE (through switched_from_dl()). |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 994 | */ |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 995 | if (!dl_task(p)) |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 996 | goto unlock; |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 997 | |
| 998 | /* |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 999 | * The task might have been boosted by someone else and might be in the |
| 1000 | * boosting/deboosting path, its not throttled. |
| 1001 | */ |
| 1002 | if (dl_se->dl_boosted) |
| 1003 | goto unlock; |
| 1004 | |
| 1005 | /* |
| 1006 | * Spurious timer due to start_dl_timer() race; or we already received |
| 1007 | * a replenishment from rt_mutex_setprio(). |
| 1008 | */ |
| 1009 | if (!dl_se->dl_throttled) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1010 | goto unlock; |
| 1011 | |
| 1012 | sched_clock_tick(); |
| 1013 | update_rq_clock(rq); |
Kirill Tkhai | a79ec89 | 2015-02-16 15:38:34 +0300 | [diff] [blame] | 1014 | |
| 1015 | /* |
| 1016 | * If the throttle happened during sched-out; like: |
| 1017 | * |
| 1018 | * schedule() |
| 1019 | * deactivate_task() |
| 1020 | * dequeue_task_dl() |
| 1021 | * update_curr_dl() |
| 1022 | * start_dl_timer() |
| 1023 | * __dequeue_task_dl() |
| 1024 | * prev->on_rq = 0; |
| 1025 | * |
| 1026 | * We can be both throttled and !queued. Replenish the counter |
| 1027 | * but do not enqueue -- wait for our wakeup to do that. |
| 1028 | */ |
| 1029 | if (!task_on_rq_queued(p)) { |
| 1030 | replenish_dl_entity(dl_se, dl_se); |
| 1031 | goto unlock; |
| 1032 | } |
| 1033 | |
Wanpeng Li | 61c7aca | 2016-08-31 18:27:44 +0800 | [diff] [blame] | 1034 | #ifdef CONFIG_SMP |
| 1035 | if (unlikely(!rq->online)) { |
| 1036 | /* |
| 1037 | * If the runqueue is no longer available, migrate the |
| 1038 | * task elsewhere. This necessarily changes rq. |
| 1039 | */ |
| 1040 | lockdep_unpin_lock(&rq->lock, rf.cookie); |
| 1041 | rq = dl_task_offline_migration(rq, p); |
| 1042 | rf.cookie = lockdep_pin_lock(&rq->lock); |
Wanpeng Li | dcc3b5f | 2017-03-06 21:51:28 -0800 | [diff] [blame] | 1043 | update_rq_clock(rq); |
Wanpeng Li | 61c7aca | 2016-08-31 18:27:44 +0800 | [diff] [blame] | 1044 | |
| 1045 | /* |
| 1046 | * Now that the task has been migrated to the new RQ and we |
| 1047 | * have that locked, proceed as normal and enqueue the task |
| 1048 | * there. |
| 1049 | */ |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
Peter Zijlstra | 1019a35 | 2014-11-26 08:44:03 +0800 | [diff] [blame] | 1053 | enqueue_task_dl(rq, p, ENQUEUE_REPLENISH); |
| 1054 | if (dl_task(rq->curr)) |
| 1055 | check_preempt_curr_dl(rq, p, 0); |
| 1056 | else |
| 1057 | resched_curr(rq); |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 1058 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1059 | #ifdef CONFIG_SMP |
Peter Zijlstra | 1019a35 | 2014-11-26 08:44:03 +0800 | [diff] [blame] | 1060 | /* |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 1061 | * Queueing this task back might have overloaded rq, check if we need |
| 1062 | * to kick someone away. |
Peter Zijlstra | 1019a35 | 2014-11-26 08:44:03 +0800 | [diff] [blame] | 1063 | */ |
Peter Zijlstra | 0aaafaa | 2015-10-23 11:50:08 +0200 | [diff] [blame] | 1064 | if (has_pushable_dl_tasks(rq)) { |
| 1065 | /* |
| 1066 | * Nothing relies on rq->lock after this, so its safe to drop |
| 1067 | * rq->lock. |
| 1068 | */ |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 1069 | rq_unpin_lock(rq, &rf); |
Peter Zijlstra | 1019a35 | 2014-11-26 08:44:03 +0800 | [diff] [blame] | 1070 | push_dl_task(rq); |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 1071 | rq_repin_lock(rq, &rf); |
Peter Zijlstra | 0aaafaa | 2015-10-23 11:50:08 +0200 | [diff] [blame] | 1072 | } |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1073 | #endif |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 1074 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1075 | unlock: |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 1076 | task_rq_unlock(rq, p, &rf); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1077 | |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 1078 | /* |
| 1079 | * This can free the task_struct, including this hrtimer, do not touch |
| 1080 | * anything related to that after this. |
| 1081 | */ |
| 1082 | put_task_struct(p); |
| 1083 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1084 | return HRTIMER_NORESTART; |
| 1085 | } |
| 1086 | |
| 1087 | void init_dl_task_timer(struct sched_dl_entity *dl_se) |
| 1088 | { |
| 1089 | struct hrtimer *timer = &dl_se->dl_timer; |
| 1090 | |
Sebastian Andrzej Siewior | d5096aa | 2019-07-26 20:30:52 +0200 | [diff] [blame] | 1091 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1092 | timer->function = dl_task_timer; |
| 1093 | } |
| 1094 | |
Daniel Bristot de Oliveira | df8eac8 | 2017-03-02 15:10:58 +0100 | [diff] [blame] | 1095 | /* |
| 1096 | * During the activation, CBS checks if it can reuse the current task's |
| 1097 | * runtime and period. If the deadline of the task is in the past, CBS |
| 1098 | * cannot use the runtime, and so it replenishes the task. This rule |
| 1099 | * works fine for implicit deadline tasks (deadline == period), and the |
| 1100 | * CBS was designed for implicit deadline tasks. However, a task with |
| 1101 | * constrained deadline (deadine < period) might be awakened after the |
| 1102 | * deadline, but before the next period. In this case, replenishing the |
| 1103 | * task would allow it to run for runtime / deadline. As in this case |
| 1104 | * deadline < period, CBS enables a task to run for more than the |
| 1105 | * runtime / period. In a very loaded system, this can cause a domino |
| 1106 | * effect, making other tasks miss their deadlines. |
| 1107 | * |
| 1108 | * To avoid this problem, in the activation of a constrained deadline |
| 1109 | * task after the deadline but before the next period, throttle the |
| 1110 | * task and set the replenishing timer to the begin of the next period, |
| 1111 | * unless it is boosted. |
| 1112 | */ |
| 1113 | static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se) |
| 1114 | { |
| 1115 | struct task_struct *p = dl_task_of(dl_se); |
| 1116 | struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se)); |
| 1117 | |
| 1118 | if (dl_time_before(dl_se->deadline, rq_clock(rq)) && |
| 1119 | dl_time_before(rq_clock(rq), dl_next_period(dl_se))) { |
| 1120 | if (unlikely(dl_se->dl_boosted || !start_dl_timer(p))) |
| 1121 | return; |
| 1122 | dl_se->dl_throttled = 1; |
Xunlei Pang | ae83b56 | 2017-05-10 21:03:37 +0800 | [diff] [blame] | 1123 | if (dl_se->runtime > 0) |
| 1124 | dl_se->runtime = 0; |
Daniel Bristot de Oliveira | df8eac8 | 2017-03-02 15:10:58 +0100 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1128 | static |
Zhiqiang Zhang | 6fab541 | 2015-06-15 11:15:20 +0800 | [diff] [blame] | 1129 | int dl_runtime_exceeded(struct sched_dl_entity *dl_se) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1130 | { |
Luca Abeni | 269ad80 | 2014-12-17 11:50:32 +0100 | [diff] [blame] | 1131 | return (dl_se->runtime <= 0); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1132 | } |
| 1133 | |
Juri Lelli | faa5993 | 2014-02-21 11:37:15 +0100 | [diff] [blame] | 1134 | extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq); |
| 1135 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1136 | /* |
Luca Abeni | c52f14d | 2017-05-18 22:13:31 +0200 | [diff] [blame] | 1137 | * This function implements the GRUB accounting rule: |
| 1138 | * according to the GRUB reclaiming algorithm, the runtime is |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 1139 | * not decreased as "dq = -dt", but as |
| 1140 | * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt", |
| 1141 | * where u is the utilization of the task, Umax is the maximum reclaimable |
| 1142 | * utilization, Uinact is the (per-runqueue) inactive utilization, computed |
| 1143 | * as the difference between the "total runqueue utilization" and the |
| 1144 | * runqueue active utilization, and Uextra is the (per runqueue) extra |
| 1145 | * reclaimable utilization. |
Luca Abeni | 9f0d1a5 | 2017-05-18 22:13:35 +0200 | [diff] [blame] | 1146 | * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 1147 | * multiplied by 2^BW_SHIFT, the result has to be shifted right by |
| 1148 | * BW_SHIFT. |
| 1149 | * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT, |
| 1150 | * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT. |
| 1151 | * Since delta is a 64 bit variable, to have an overflow its value |
| 1152 | * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds. |
| 1153 | * So, overflow is not an issue here. |
Luca Abeni | c52f14d | 2017-05-18 22:13:31 +0200 | [diff] [blame] | 1154 | */ |
Mathieu Malaterre | 3febfc8a | 2018-05-16 22:09:02 +0200 | [diff] [blame] | 1155 | static u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se) |
Luca Abeni | c52f14d | 2017-05-18 22:13:31 +0200 | [diff] [blame] | 1156 | { |
Luca Abeni | 9f0d1a5 | 2017-05-18 22:13:35 +0200 | [diff] [blame] | 1157 | u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */ |
| 1158 | u64 u_act; |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 1159 | u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT; |
Luca Abeni | c52f14d | 2017-05-18 22:13:31 +0200 | [diff] [blame] | 1160 | |
Luca Abeni | 9f0d1a5 | 2017-05-18 22:13:35 +0200 | [diff] [blame] | 1161 | /* |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 1162 | * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)}, |
| 1163 | * we compare u_inact + rq->dl.extra_bw with |
| 1164 | * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because |
| 1165 | * u_inact + rq->dl.extra_bw can be larger than |
| 1166 | * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative |
| 1167 | * leading to wrong results) |
Luca Abeni | 9f0d1a5 | 2017-05-18 22:13:35 +0200 | [diff] [blame] | 1168 | */ |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 1169 | if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min) |
| 1170 | u_act = u_act_min; |
Luca Abeni | 9f0d1a5 | 2017-05-18 22:13:35 +0200 | [diff] [blame] | 1171 | else |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 1172 | u_act = BW_UNIT - u_inact - rq->dl.extra_bw; |
Luca Abeni | 9f0d1a5 | 2017-05-18 22:13:35 +0200 | [diff] [blame] | 1173 | |
| 1174 | return (delta * u_act) >> BW_SHIFT; |
Luca Abeni | c52f14d | 2017-05-18 22:13:31 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /* |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1178 | * Update the current task's runtime statistics (provided it is still |
| 1179 | * a -deadline task and has not been removed from the dl_rq). |
| 1180 | */ |
| 1181 | static void update_curr_dl(struct rq *rq) |
| 1182 | { |
| 1183 | struct task_struct *curr = rq->curr; |
| 1184 | struct sched_dl_entity *dl_se = &curr->dl; |
Juri Lelli | 0788116 | 2017-12-04 11:23:25 +0100 | [diff] [blame] | 1185 | u64 delta_exec, scaled_delta_exec; |
| 1186 | int cpu = cpu_of(rq); |
Wen Yang | 6fe0ce1 | 2018-02-06 09:55:48 +0800 | [diff] [blame] | 1187 | u64 now; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1188 | |
| 1189 | if (!dl_task(curr) || !on_dl_rq(dl_se)) |
| 1190 | return; |
| 1191 | |
| 1192 | /* |
| 1193 | * Consumed budget is computed considering the time as |
| 1194 | * observed by schedulable tasks (excluding time spent |
| 1195 | * in hardirq context, etc.). Deadlines are instead |
| 1196 | * computed using hard walltime. This seems to be the more |
| 1197 | * natural solution, but the full ramifications of this |
| 1198 | * approach need further study. |
| 1199 | */ |
Wen Yang | 6fe0ce1 | 2018-02-06 09:55:48 +0800 | [diff] [blame] | 1200 | now = rq_clock_task(rq); |
| 1201 | delta_exec = now - curr->se.exec_start; |
Peter Zijlstra | 48be3a6 | 2016-02-23 13:28:22 +0100 | [diff] [blame] | 1202 | if (unlikely((s64)delta_exec <= 0)) { |
| 1203 | if (unlikely(dl_se->dl_yielded)) |
| 1204 | goto throttle; |
Kirill Tkhai | 734ff2a | 2014-03-04 19:25:46 +0400 | [diff] [blame] | 1205 | return; |
Peter Zijlstra | 48be3a6 | 2016-02-23 13:28:22 +0100 | [diff] [blame] | 1206 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1207 | |
| 1208 | schedstat_set(curr->se.statistics.exec_max, |
| 1209 | max(curr->se.statistics.exec_max, delta_exec)); |
| 1210 | |
| 1211 | curr->se.sum_exec_runtime += delta_exec; |
| 1212 | account_group_exec_runtime(curr, delta_exec); |
| 1213 | |
Wen Yang | 6fe0ce1 | 2018-02-06 09:55:48 +0800 | [diff] [blame] | 1214 | curr->se.exec_start = now; |
Tejun Heo | d2cc5ed | 2017-09-25 08:12:04 -0700 | [diff] [blame] | 1215 | cgroup_account_cputime(curr, delta_exec); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1216 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 1217 | if (dl_entity_is_special(dl_se)) |
| 1218 | return; |
| 1219 | |
Juri Lelli | 0788116 | 2017-12-04 11:23:25 +0100 | [diff] [blame] | 1220 | /* |
| 1221 | * For tasks that participate in GRUB, we implement GRUB-PA: the |
| 1222 | * spare reclaimed bandwidth is used to clock down frequency. |
| 1223 | * |
| 1224 | * For the others, we still need to scale reservation parameters |
| 1225 | * according to current frequency and CPU maximum capacity. |
| 1226 | */ |
| 1227 | if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) { |
| 1228 | scaled_delta_exec = grub_reclaim(delta_exec, |
| 1229 | rq, |
| 1230 | &curr->dl); |
| 1231 | } else { |
| 1232 | unsigned long scale_freq = arch_scale_freq_capacity(cpu); |
Vincent Guittot | 8ec59c0 | 2019-06-17 17:00:17 +0200 | [diff] [blame] | 1233 | unsigned long scale_cpu = arch_scale_cpu_capacity(cpu); |
Juri Lelli | 0788116 | 2017-12-04 11:23:25 +0100 | [diff] [blame] | 1234 | |
| 1235 | scaled_delta_exec = cap_scale(delta_exec, scale_freq); |
| 1236 | scaled_delta_exec = cap_scale(scaled_delta_exec, scale_cpu); |
| 1237 | } |
| 1238 | |
| 1239 | dl_se->runtime -= scaled_delta_exec; |
Peter Zijlstra | 48be3a6 | 2016-02-23 13:28:22 +0100 | [diff] [blame] | 1240 | |
| 1241 | throttle: |
| 1242 | if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) { |
Peter Zijlstra | 1019a35 | 2014-11-26 08:44:03 +0800 | [diff] [blame] | 1243 | dl_se->dl_throttled = 1; |
Juri Lelli | 34be393 | 2017-12-12 12:10:24 +0100 | [diff] [blame] | 1244 | |
| 1245 | /* If requested, inform the user about runtime overruns. */ |
| 1246 | if (dl_runtime_exceeded(dl_se) && |
| 1247 | (dl_se->flags & SCHED_FLAG_DL_OVERRUN)) |
| 1248 | dl_se->dl_overrun = 1; |
| 1249 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1250 | __dequeue_task_dl(rq, curr, 0); |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 1251 | if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr))) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1252 | enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH); |
| 1253 | |
| 1254 | if (!is_leftmost(curr, &rq->dl)) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 1255 | resched_curr(rq); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1256 | } |
Peter Zijlstra | 1724813 | 2013-12-17 12:44:49 +0100 | [diff] [blame] | 1257 | |
| 1258 | /* |
| 1259 | * Because -- for now -- we share the rt bandwidth, we need to |
| 1260 | * account our runtime there too, otherwise actual rt tasks |
| 1261 | * would be able to exceed the shared quota. |
| 1262 | * |
| 1263 | * Account to the root rt group for now. |
| 1264 | * |
| 1265 | * The solution we're working towards is having the RT groups scheduled |
| 1266 | * using deadline servers -- however there's a few nasties to figure |
| 1267 | * out before that can happen. |
| 1268 | */ |
| 1269 | if (rt_bandwidth_enabled()) { |
| 1270 | struct rt_rq *rt_rq = &rq->rt; |
| 1271 | |
| 1272 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 1724813 | 2013-12-17 12:44:49 +0100 | [diff] [blame] | 1273 | /* |
| 1274 | * We'll let actual RT tasks worry about the overflow here, we |
Juri Lelli | faa5993 | 2014-02-21 11:37:15 +0100 | [diff] [blame] | 1275 | * have our own CBS to keep us inline; only account when RT |
| 1276 | * bandwidth is relevant. |
Peter Zijlstra | 1724813 | 2013-12-17 12:44:49 +0100 | [diff] [blame] | 1277 | */ |
Juri Lelli | faa5993 | 2014-02-21 11:37:15 +0100 | [diff] [blame] | 1278 | if (sched_rt_bandwidth_account(rt_rq)) |
| 1279 | rt_rq->rt_time += delta_exec; |
Peter Zijlstra | 1724813 | 2013-12-17 12:44:49 +0100 | [diff] [blame] | 1280 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
| 1281 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1284 | static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer) |
| 1285 | { |
| 1286 | struct sched_dl_entity *dl_se = container_of(timer, |
| 1287 | struct sched_dl_entity, |
| 1288 | inactive_timer); |
| 1289 | struct task_struct *p = dl_task_of(dl_se); |
| 1290 | struct rq_flags rf; |
| 1291 | struct rq *rq; |
| 1292 | |
| 1293 | rq = task_rq_lock(p, &rf); |
| 1294 | |
Juri Lelli | ecda2b6 | 2018-05-30 18:08:09 +0200 | [diff] [blame] | 1295 | sched_clock_tick(); |
| 1296 | update_rq_clock(rq); |
| 1297 | |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1298 | if (!dl_task(p) || p->state == TASK_DEAD) { |
Luca Abeni | 387e313 | 2017-05-18 22:13:30 +0200 | [diff] [blame] | 1299 | struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); |
| 1300 | |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1301 | if (p->state == TASK_DEAD && dl_se->dl_non_contending) { |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 1302 | sub_running_bw(&p->dl, dl_rq_of_se(&p->dl)); |
| 1303 | sub_rq_bw(&p->dl, dl_rq_of_se(&p->dl)); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1304 | dl_se->dl_non_contending = 0; |
| 1305 | } |
Luca Abeni | 387e313 | 2017-05-18 22:13:30 +0200 | [diff] [blame] | 1306 | |
| 1307 | raw_spin_lock(&dl_b->lock); |
Peter Zijlstra | 8c0944ce | 2017-09-07 12:09:30 +0200 | [diff] [blame] | 1308 | __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); |
Luca Abeni | 387e313 | 2017-05-18 22:13:30 +0200 | [diff] [blame] | 1309 | raw_spin_unlock(&dl_b->lock); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1310 | __dl_clear_params(p); |
| 1311 | |
| 1312 | goto unlock; |
| 1313 | } |
| 1314 | if (dl_se->dl_non_contending == 0) |
| 1315 | goto unlock; |
| 1316 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 1317 | sub_running_bw(dl_se, &rq->dl); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1318 | dl_se->dl_non_contending = 0; |
| 1319 | unlock: |
| 1320 | task_rq_unlock(rq, p, &rf); |
| 1321 | put_task_struct(p); |
| 1322 | |
| 1323 | return HRTIMER_NORESTART; |
| 1324 | } |
| 1325 | |
| 1326 | void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se) |
| 1327 | { |
| 1328 | struct hrtimer *timer = &dl_se->inactive_timer; |
| 1329 | |
Juri Lelli | 850377a | 2019-07-31 12:37:15 +0200 | [diff] [blame] | 1330 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1331 | timer->function = inactive_task_timer; |
| 1332 | } |
| 1333 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1334 | #ifdef CONFIG_SMP |
| 1335 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1336 | static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) |
| 1337 | { |
| 1338 | struct rq *rq = rq_of_dl_rq(dl_rq); |
| 1339 | |
| 1340 | if (dl_rq->earliest_dl.curr == 0 || |
| 1341 | dl_time_before(deadline, dl_rq->earliest_dl.curr)) { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1342 | dl_rq->earliest_dl.curr = deadline; |
Tommaso Cucinotta | d8206bb | 2016-08-14 16:27:08 +0200 | [diff] [blame] | 1343 | cpudl_set(&rq->rd->cpudl, rq->cpu, deadline); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) |
| 1348 | { |
| 1349 | struct rq *rq = rq_of_dl_rq(dl_rq); |
| 1350 | |
| 1351 | /* |
| 1352 | * Since we may have removed our earliest (and/or next earliest) |
| 1353 | * task we must recompute them. |
| 1354 | */ |
| 1355 | if (!dl_rq->dl_nr_running) { |
| 1356 | dl_rq->earliest_dl.curr = 0; |
| 1357 | dl_rq->earliest_dl.next = 0; |
Tommaso Cucinotta | d8206bb | 2016-08-14 16:27:08 +0200 | [diff] [blame] | 1358 | cpudl_clear(&rq->rd->cpudl, rq->cpu); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1359 | } else { |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 1360 | struct rb_node *leftmost = dl_rq->root.rb_leftmost; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1361 | struct sched_dl_entity *entry; |
| 1362 | |
| 1363 | entry = rb_entry(leftmost, struct sched_dl_entity, rb_node); |
| 1364 | dl_rq->earliest_dl.curr = entry->deadline; |
Tommaso Cucinotta | d8206bb | 2016-08-14 16:27:08 +0200 | [diff] [blame] | 1365 | cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | #else |
| 1370 | |
| 1371 | static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} |
| 1372 | static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} |
| 1373 | |
| 1374 | #endif /* CONFIG_SMP */ |
| 1375 | |
| 1376 | static inline |
| 1377 | void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 1378 | { |
| 1379 | int prio = dl_task_of(dl_se)->prio; |
| 1380 | u64 deadline = dl_se->deadline; |
| 1381 | |
| 1382 | WARN_ON(!dl_prio(prio)); |
| 1383 | dl_rq->dl_nr_running++; |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 1384 | add_nr_running(rq_of_dl_rq(dl_rq), 1); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1385 | |
| 1386 | inc_dl_deadline(dl_rq, deadline); |
| 1387 | inc_dl_migration(dl_se, dl_rq); |
| 1388 | } |
| 1389 | |
| 1390 | static inline |
| 1391 | void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) |
| 1392 | { |
| 1393 | int prio = dl_task_of(dl_se)->prio; |
| 1394 | |
| 1395 | WARN_ON(!dl_prio(prio)); |
| 1396 | WARN_ON(!dl_rq->dl_nr_running); |
| 1397 | dl_rq->dl_nr_running--; |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 1398 | sub_nr_running(rq_of_dl_rq(dl_rq), 1); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1399 | |
| 1400 | dec_dl_deadline(dl_rq, dl_se->deadline); |
| 1401 | dec_dl_migration(dl_se, dl_rq); |
| 1402 | } |
| 1403 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1404 | static void __enqueue_dl_entity(struct sched_dl_entity *dl_se) |
| 1405 | { |
| 1406 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 1407 | struct rb_node **link = &dl_rq->root.rb_root.rb_node; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1408 | struct rb_node *parent = NULL; |
| 1409 | struct sched_dl_entity *entry; |
| 1410 | int leftmost = 1; |
| 1411 | |
| 1412 | BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node)); |
| 1413 | |
| 1414 | while (*link) { |
| 1415 | parent = *link; |
| 1416 | entry = rb_entry(parent, struct sched_dl_entity, rb_node); |
| 1417 | if (dl_time_before(dl_se->deadline, entry->deadline)) |
| 1418 | link = &parent->rb_left; |
| 1419 | else { |
| 1420 | link = &parent->rb_right; |
| 1421 | leftmost = 0; |
| 1422 | } |
| 1423 | } |
| 1424 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1425 | rb_link_node(&dl_se->rb_node, parent, link); |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 1426 | rb_insert_color_cached(&dl_se->rb_node, &dl_rq->root, leftmost); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1427 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1428 | inc_dl_tasks(dl_se, dl_rq); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | static void __dequeue_dl_entity(struct sched_dl_entity *dl_se) |
| 1432 | { |
| 1433 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); |
| 1434 | |
| 1435 | if (RB_EMPTY_NODE(&dl_se->rb_node)) |
| 1436 | return; |
| 1437 | |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 1438 | rb_erase_cached(&dl_se->rb_node, &dl_rq->root); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1439 | RB_CLEAR_NODE(&dl_se->rb_node); |
| 1440 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1441 | dec_dl_tasks(dl_se, dl_rq); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static void |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 1445 | enqueue_dl_entity(struct sched_dl_entity *dl_se, |
| 1446 | struct sched_dl_entity *pi_se, int flags) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1447 | { |
| 1448 | BUG_ON(on_dl_rq(dl_se)); |
| 1449 | |
| 1450 | /* |
| 1451 | * If this is a wakeup or a new instance, the scheduling |
| 1452 | * parameters of the task might need updating. Otherwise, |
| 1453 | * we want a replenishment of its runtime. |
| 1454 | */ |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1455 | if (flags & ENQUEUE_WAKEUP) { |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 1456 | task_contending(dl_se, flags); |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 1457 | update_dl_entity(dl_se, pi_se); |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1458 | } else if (flags & ENQUEUE_REPLENISH) { |
Luca Abeni | 6a503c3 | 2014-12-17 11:50:31 +0100 | [diff] [blame] | 1459 | replenish_dl_entity(dl_se, pi_se); |
Luca Abeni | 295d6d5 | 2017-09-07 12:09:29 +0200 | [diff] [blame] | 1460 | } else if ((flags & ENQUEUE_RESTORE) && |
| 1461 | dl_time_before(dl_se->deadline, |
| 1462 | rq_clock(rq_of_dl_rq(dl_rq_of_se(dl_se))))) { |
| 1463 | setup_new_dl_entity(dl_se); |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1464 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1465 | |
| 1466 | __enqueue_dl_entity(dl_se); |
| 1467 | } |
| 1468 | |
| 1469 | static void dequeue_dl_entity(struct sched_dl_entity *dl_se) |
| 1470 | { |
| 1471 | __dequeue_dl_entity(dl_se); |
| 1472 | } |
| 1473 | |
| 1474 | static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags) |
| 1475 | { |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 1476 | struct task_struct *pi_task = rt_mutex_get_top_task(p); |
| 1477 | struct sched_dl_entity *pi_se = &p->dl; |
| 1478 | |
| 1479 | /* |
Joel Fernandes | 193be41 | 2017-07-12 19:24:29 -0700 | [diff] [blame] | 1480 | * Use the scheduling parameters of the top pi-waiter task if: |
| 1481 | * - we have a top pi-waiter which is a SCHED_DEADLINE task AND |
| 1482 | * - our dl_boosted is set (i.e. the pi-waiter's (absolute) deadline is |
| 1483 | * smaller than our deadline OR we are a !SCHED_DEADLINE task getting |
| 1484 | * boosted due to a SCHED_DEADLINE pi-waiter). |
| 1485 | * Otherwise we keep our runtime and deadline. |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 1486 | */ |
Joel Fernandes | 193be41 | 2017-07-12 19:24:29 -0700 | [diff] [blame] | 1487 | if (pi_task && dl_prio(pi_task->normal_prio) && p->dl.dl_boosted) { |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 1488 | pi_se = &pi_task->dl; |
Juri Lelli | 64be6f1 | 2014-10-24 10:16:37 +0100 | [diff] [blame] | 1489 | } else if (!dl_prio(p->normal_prio)) { |
| 1490 | /* |
| 1491 | * Special case in which we have a !SCHED_DEADLINE task |
Joel Fernandes | 193be41 | 2017-07-12 19:24:29 -0700 | [diff] [blame] | 1492 | * that is going to be deboosted, but exceeds its |
Juri Lelli | 64be6f1 | 2014-10-24 10:16:37 +0100 | [diff] [blame] | 1493 | * runtime while doing so. No point in replenishing |
| 1494 | * it, as it's going to return back to its original |
| 1495 | * scheduling class after this. |
| 1496 | */ |
| 1497 | BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH); |
| 1498 | return; |
| 1499 | } |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 1500 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1501 | /* |
Daniel Bristot de Oliveira | df8eac8 | 2017-03-02 15:10:58 +0100 | [diff] [blame] | 1502 | * Check if a constrained deadline task was activated |
| 1503 | * after the deadline but before the next period. |
| 1504 | * If that is the case, the task will be throttled and |
| 1505 | * the replenishment timer will be set to the next period. |
| 1506 | */ |
Daniel Bristot de Oliveira | 3effcb4 | 2017-05-29 16:24:03 +0200 | [diff] [blame] | 1507 | if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl)) |
Daniel Bristot de Oliveira | df8eac8 | 2017-03-02 15:10:58 +0100 | [diff] [blame] | 1508 | dl_check_constrained_dl(&p->dl); |
| 1509 | |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 1510 | if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) { |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 1511 | add_rq_bw(&p->dl, &rq->dl); |
| 1512 | add_running_bw(&p->dl, &rq->dl); |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 1513 | } |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1514 | |
Daniel Bristot de Oliveira | df8eac8 | 2017-03-02 15:10:58 +0100 | [diff] [blame] | 1515 | /* |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1516 | * If p is throttled, we do not enqueue it. In fact, if it exhausted |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1517 | * its budget it needs a replenishment and, since it now is on |
| 1518 | * its rq, the bandwidth timer callback (which clearly has not |
| 1519 | * run yet) will take care of this. |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1520 | * However, the active utilization does not depend on the fact |
| 1521 | * that the task is on the runqueue or not (but depends on the |
| 1522 | * task's state - in GRUB parlance, "inactive" vs "active contending"). |
| 1523 | * In other words, even if a task is throttled its utilization must |
| 1524 | * be counted in the active utilization; hence, we need to call |
| 1525 | * add_running_bw(). |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1526 | */ |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1527 | if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) { |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1528 | if (flags & ENQUEUE_WAKEUP) |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 1529 | task_contending(&p->dl, flags); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1530 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1531 | return; |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1532 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1533 | |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 1534 | enqueue_dl_entity(&p->dl, pi_se, flags); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1535 | |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 1536 | if (!task_current(rq, p) && p->nr_cpus_allowed > 1) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1537 | enqueue_pushable_dl_task(rq, p); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) |
| 1541 | { |
| 1542 | dequeue_dl_entity(&p->dl); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1543 | dequeue_pushable_dl_task(rq, p); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) |
| 1547 | { |
| 1548 | update_curr_dl(rq); |
| 1549 | __dequeue_task_dl(rq, p, flags); |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1550 | |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 1551 | if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) { |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 1552 | sub_running_bw(&p->dl, &rq->dl); |
| 1553 | sub_rq_bw(&p->dl, &rq->dl); |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 1554 | } |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1555 | |
| 1556 | /* |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1557 | * This check allows to start the inactive timer (or to immediately |
| 1558 | * decrease the active utilization, if needed) in two cases: |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 1559 | * when the task blocks and when it is terminating |
| 1560 | * (p->state == TASK_DEAD). We can handle the two cases in the same |
| 1561 | * way, because from GRUB's point of view the same thing is happening |
| 1562 | * (the task moves from "active contending" to "active non contending" |
| 1563 | * or "inactive") |
| 1564 | */ |
| 1565 | if (flags & DEQUEUE_SLEEP) |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1566 | task_non_contending(p); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * Yield task semantic for -deadline tasks is: |
| 1571 | * |
| 1572 | * get off from the CPU until our next instance, with |
| 1573 | * a new runtime. This is of little use now, since we |
| 1574 | * don't have a bandwidth reclaiming mechanism. Anyway, |
| 1575 | * bandwidth reclaiming is planned for the future, and |
| 1576 | * yield_task_dl will indicate that some spare budget |
| 1577 | * is available for other task instances to use it. |
| 1578 | */ |
| 1579 | static void yield_task_dl(struct rq *rq) |
| 1580 | { |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1581 | /* |
| 1582 | * We make the task go to sleep until its current deadline by |
| 1583 | * forcing its runtime to zero. This way, update_curr_dl() stops |
| 1584 | * it and the bandwidth timer will wake it up and will give it |
Juri Lelli | 5bfd126 | 2014-04-15 13:49:04 +0200 | [diff] [blame] | 1585 | * new scheduling parameters (thanks to dl_yielded=1). |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1586 | */ |
Peter Zijlstra | 48be3a6 | 2016-02-23 13:28:22 +0100 | [diff] [blame] | 1587 | rq->curr->dl.dl_yielded = 1; |
| 1588 | |
Kirill Tkhai | 6f1607f | 2015-02-04 12:09:32 +0300 | [diff] [blame] | 1589 | update_rq_clock(rq); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1590 | update_curr_dl(rq); |
Wanpeng Li | 44fb085 | 2015-03-10 12:20:00 +0800 | [diff] [blame] | 1591 | /* |
| 1592 | * Tell update_rq_clock() that we've just updated, |
| 1593 | * so we don't do microscopic update in schedule() |
| 1594 | * and double the fastpath cost. |
| 1595 | */ |
Davidlohr Bueso | adcc8da | 2018-04-04 09:15:39 -0700 | [diff] [blame] | 1596 | rq_clock_skip_update(rq); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1597 | } |
| 1598 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1599 | #ifdef CONFIG_SMP |
| 1600 | |
| 1601 | static int find_later_rq(struct task_struct *task); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1602 | |
| 1603 | static int |
| 1604 | select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags) |
| 1605 | { |
| 1606 | struct task_struct *curr; |
| 1607 | struct rq *rq; |
| 1608 | |
Wanpeng Li | 1d7e974 | 2014-10-14 10:22:39 +0800 | [diff] [blame] | 1609 | if (sd_flag != SD_BALANCE_WAKE) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1610 | goto out; |
| 1611 | |
| 1612 | rq = cpu_rq(cpu); |
| 1613 | |
| 1614 | rcu_read_lock(); |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 1615 | curr = READ_ONCE(rq->curr); /* unlocked access */ |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1616 | |
| 1617 | /* |
| 1618 | * If we are dealing with a -deadline task, we must |
| 1619 | * decide where to wake it up. |
| 1620 | * If it has a later deadline and the current task |
| 1621 | * on this rq can't move (provided the waking task |
| 1622 | * can!) we prefer to send it somewhere else. On the |
| 1623 | * other hand, if it has a shorter deadline, we |
| 1624 | * try to make it stay here, it might be important. |
| 1625 | */ |
| 1626 | if (unlikely(dl_task(curr)) && |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 1627 | (curr->nr_cpus_allowed < 2 || |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1628 | !dl_entity_preempt(&p->dl, &curr->dl)) && |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 1629 | (p->nr_cpus_allowed > 1)) { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1630 | int target = find_later_rq(p); |
| 1631 | |
Wanpeng Li | 9d51426 | 2015-05-13 14:01:03 +0800 | [diff] [blame] | 1632 | if (target != -1 && |
Luca Abeni | 5aa5050 | 2015-10-16 10:06:21 +0200 | [diff] [blame] | 1633 | (dl_time_before(p->dl.deadline, |
| 1634 | cpu_rq(target)->dl.earliest_dl.curr) || |
| 1635 | (cpu_rq(target)->dl.dl_nr_running == 0))) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1636 | cpu = target; |
| 1637 | } |
| 1638 | rcu_read_unlock(); |
| 1639 | |
| 1640 | out: |
| 1641 | return cpu; |
| 1642 | } |
| 1643 | |
Srikar Dronamraju | 1327237a5 | 2018-09-21 23:18:57 +0530 | [diff] [blame] | 1644 | static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused) |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1645 | { |
| 1646 | struct rq *rq; |
| 1647 | |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 1648 | if (p->state != TASK_WAKING) |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1649 | return; |
| 1650 | |
| 1651 | rq = task_rq(p); |
| 1652 | /* |
| 1653 | * Since p->state == TASK_WAKING, set_task_cpu() has been called |
| 1654 | * from try_to_wake_up(). Hence, p->pi_lock is locked, but |
| 1655 | * rq->lock is not... So, lock it |
| 1656 | */ |
| 1657 | raw_spin_lock(&rq->lock); |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 1658 | if (p->dl.dl_non_contending) { |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 1659 | sub_running_bw(&p->dl, &rq->dl); |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 1660 | p->dl.dl_non_contending = 0; |
| 1661 | /* |
| 1662 | * If the timer handler is currently running and the |
| 1663 | * timer cannot be cancelled, inactive_task_timer() |
| 1664 | * will see that dl_not_contending is not set, and |
| 1665 | * will not touch the rq's active utilization, |
| 1666 | * so we are still safe. |
| 1667 | */ |
| 1668 | if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) |
| 1669 | put_task_struct(p); |
| 1670 | } |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 1671 | sub_rq_bw(&p->dl, &rq->dl); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1672 | raw_spin_unlock(&rq->lock); |
| 1673 | } |
| 1674 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1675 | static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p) |
| 1676 | { |
| 1677 | /* |
| 1678 | * Current can't be migrated, useless to reschedule, |
| 1679 | * let's hope p can move out. |
| 1680 | */ |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 1681 | if (rq->curr->nr_cpus_allowed == 1 || |
Byungchul Park | 3261ed0 | 2017-05-23 11:00:57 +0900 | [diff] [blame] | 1682 | !cpudl_find(&rq->rd->cpudl, rq->curr, NULL)) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1683 | return; |
| 1684 | |
| 1685 | /* |
| 1686 | * p is migratable, so let's not schedule it and |
| 1687 | * see if it is pushed or pulled somewhere else. |
| 1688 | */ |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 1689 | if (p->nr_cpus_allowed != 1 && |
Byungchul Park | 3261ed0 | 2017-05-23 11:00:57 +0900 | [diff] [blame] | 1690 | cpudl_find(&rq->rd->cpudl, p, NULL)) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1691 | return; |
| 1692 | |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 1693 | resched_curr(rq); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1694 | } |
| 1695 | |
Peter Zijlstra | 6e2df05 | 2019-11-08 11:11:52 +0100 | [diff] [blame] | 1696 | static int balance_dl(struct rq *rq, struct task_struct *p, struct rq_flags *rf) |
| 1697 | { |
| 1698 | if (!on_dl_rq(&p->dl) && need_pull_dl_task(rq, p)) { |
| 1699 | /* |
| 1700 | * This is OK, because current is on_cpu, which avoids it being |
| 1701 | * picked for load-balance and preemption/IRQs are still |
| 1702 | * disabled avoiding further scheduler activity on it and we've |
| 1703 | * not yet started the picking loop. |
| 1704 | */ |
| 1705 | rq_unpin_lock(rq, rf); |
| 1706 | pull_dl_task(rq); |
| 1707 | rq_repin_lock(rq, rf); |
| 1708 | } |
| 1709 | |
| 1710 | return sched_stop_runnable(rq) || sched_dl_runnable(rq); |
| 1711 | } |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1712 | #endif /* CONFIG_SMP */ |
| 1713 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1714 | /* |
| 1715 | * Only called when both the current and waking task are -deadline |
| 1716 | * tasks. |
| 1717 | */ |
| 1718 | static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, |
| 1719 | int flags) |
| 1720 | { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1721 | if (dl_entity_preempt(&p->dl, &rq->curr->dl)) { |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 1722 | resched_curr(rq); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1723 | return; |
| 1724 | } |
| 1725 | |
| 1726 | #ifdef CONFIG_SMP |
| 1727 | /* |
| 1728 | * In the unlikely case current and p have the same deadline |
| 1729 | * let us try to decide what's the best thing to do... |
| 1730 | */ |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 1731 | if ((p->dl.deadline == rq->curr->dl.deadline) && |
| 1732 | !test_tsk_need_resched(rq->curr)) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1733 | check_preempt_equal_dl(rq, p); |
| 1734 | #endif /* CONFIG_SMP */ |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | #ifdef CONFIG_SCHED_HRTICK |
| 1738 | static void start_hrtick_dl(struct rq *rq, struct task_struct *p) |
| 1739 | { |
xiaofeng.yan | 177ef2a | 2014-08-26 03:15:41 +0000 | [diff] [blame] | 1740 | hrtick_start(rq, p->dl.runtime); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1741 | } |
Wanpeng Li | 36ce988 | 2014-11-11 09:52:26 +0800 | [diff] [blame] | 1742 | #else /* !CONFIG_SCHED_HRTICK */ |
| 1743 | static void start_hrtick_dl(struct rq *rq, struct task_struct *p) |
| 1744 | { |
| 1745 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1746 | #endif |
| 1747 | |
Peter Zijlstra | a0e813f | 2019-11-08 14:16:00 +0100 | [diff] [blame] | 1748 | static void set_next_task_dl(struct rq *rq, struct task_struct *p, bool first) |
Muchun Song | ff1cdc9 | 2018-10-26 21:17:43 +0800 | [diff] [blame] | 1749 | { |
| 1750 | p->se.exec_start = rq_clock_task(rq); |
| 1751 | |
| 1752 | /* You can't push away the running task */ |
| 1753 | dequeue_pushable_dl_task(rq, p); |
Peter Zijlstra | f95d4ea | 2019-05-29 20:36:40 +0000 | [diff] [blame] | 1754 | |
Peter Zijlstra | a0e813f | 2019-11-08 14:16:00 +0100 | [diff] [blame] | 1755 | if (!first) |
| 1756 | return; |
| 1757 | |
Peter Zijlstra | f95d4ea | 2019-05-29 20:36:40 +0000 | [diff] [blame] | 1758 | if (hrtick_enabled(rq)) |
| 1759 | start_hrtick_dl(rq, p); |
| 1760 | |
| 1761 | if (rq->curr->sched_class != &dl_sched_class) |
| 1762 | update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0); |
| 1763 | |
| 1764 | deadline_queue_push_tasks(rq); |
Muchun Song | ff1cdc9 | 2018-10-26 21:17:43 +0800 | [diff] [blame] | 1765 | } |
| 1766 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1767 | static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq, |
| 1768 | struct dl_rq *dl_rq) |
| 1769 | { |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 1770 | struct rb_node *left = rb_first_cached(&dl_rq->root); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1771 | |
| 1772 | if (!left) |
| 1773 | return NULL; |
| 1774 | |
| 1775 | return rb_entry(left, struct sched_dl_entity, rb_node); |
| 1776 | } |
| 1777 | |
Peter Zijlstra | 98c2f70 | 2019-11-08 14:15:58 +0100 | [diff] [blame] | 1778 | static struct task_struct *pick_next_task_dl(struct rq *rq) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1779 | { |
| 1780 | struct sched_dl_entity *dl_se; |
Peter Zijlstra | 6e2df05 | 2019-11-08 11:11:52 +0100 | [diff] [blame] | 1781 | struct dl_rq *dl_rq = &rq->dl; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1782 | struct task_struct *p; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1783 | |
Peter Zijlstra | 6e2df05 | 2019-11-08 11:11:52 +0100 | [diff] [blame] | 1784 | if (!sched_dl_runnable(rq)) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1785 | return NULL; |
| 1786 | |
| 1787 | dl_se = pick_next_dl_entity(rq, dl_rq); |
| 1788 | BUG_ON(!dl_se); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1789 | p = dl_task_of(dl_se); |
Peter Zijlstra | a0e813f | 2019-11-08 14:16:00 +0100 | [diff] [blame] | 1790 | set_next_task_dl(rq, p, true); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1791 | return p; |
| 1792 | } |
| 1793 | |
Peter Zijlstra | 6e2df05 | 2019-11-08 11:11:52 +0100 | [diff] [blame] | 1794 | static void put_prev_task_dl(struct rq *rq, struct task_struct *p) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1795 | { |
| 1796 | update_curr_dl(rq); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1797 | |
Vincent Guittot | 2312729 | 2019-01-23 16:26:53 +0100 | [diff] [blame] | 1798 | update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1); |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 1799 | if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1800 | enqueue_pushable_dl_task(rq, p); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 1803 | /* |
| 1804 | * scheduler tick hitting a task of our scheduling class. |
| 1805 | * |
| 1806 | * NOTE: This function can be called remotely by the tick offload that |
| 1807 | * goes along full dynticks. Therefore no local assumption can be made |
| 1808 | * and everything must be accessed through the @rq and @curr passed in |
| 1809 | * parameters. |
| 1810 | */ |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1811 | static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued) |
| 1812 | { |
| 1813 | update_curr_dl(rq); |
| 1814 | |
Vincent Guittot | 2312729 | 2019-01-23 16:26:53 +0100 | [diff] [blame] | 1815 | update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1); |
Wanpeng Li | a7bebf4 | 2014-11-26 08:44:01 +0800 | [diff] [blame] | 1816 | /* |
| 1817 | * Even when we have runtime, update_curr_dl() might have resulted in us |
| 1818 | * not being the leftmost task anymore. In that case NEED_RESCHED will |
| 1819 | * be set and schedule() will start a new hrtick for the next task. |
| 1820 | */ |
| 1821 | if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 && |
| 1822 | is_leftmost(p, &rq->dl)) |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1823 | start_hrtick_dl(rq, p); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | static void task_fork_dl(struct task_struct *p) |
| 1827 | { |
| 1828 | /* |
| 1829 | * SCHED_DEADLINE tasks cannot fork and this is achieved through |
| 1830 | * sched_fork() |
| 1831 | */ |
| 1832 | } |
| 1833 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1834 | #ifdef CONFIG_SMP |
| 1835 | |
| 1836 | /* Only try algorithms three times */ |
| 1837 | #define DL_MAX_TRIES 3 |
| 1838 | |
| 1839 | static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu) |
| 1840 | { |
| 1841 | if (!task_running(rq, p) && |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 1842 | cpumask_test_cpu(cpu, p->cpus_ptr)) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1843 | return 1; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1844 | return 0; |
| 1845 | } |
| 1846 | |
Wanpeng Li | 8b5e770 | 2015-05-13 14:01:01 +0800 | [diff] [blame] | 1847 | /* |
| 1848 | * Return the earliest pushable rq's task, which is suitable to be executed |
| 1849 | * on the CPU, NULL otherwise: |
| 1850 | */ |
| 1851 | static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu) |
| 1852 | { |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 1853 | struct rb_node *next_node = rq->dl.pushable_dl_tasks_root.rb_leftmost; |
Wanpeng Li | 8b5e770 | 2015-05-13 14:01:01 +0800 | [diff] [blame] | 1854 | struct task_struct *p = NULL; |
| 1855 | |
| 1856 | if (!has_pushable_dl_tasks(rq)) |
| 1857 | return NULL; |
| 1858 | |
| 1859 | next_node: |
| 1860 | if (next_node) { |
| 1861 | p = rb_entry(next_node, struct task_struct, pushable_dl_tasks); |
| 1862 | |
| 1863 | if (pick_dl_task(rq, p, cpu)) |
| 1864 | return p; |
| 1865 | |
| 1866 | next_node = rb_next(next_node); |
| 1867 | goto next_node; |
| 1868 | } |
| 1869 | |
| 1870 | return NULL; |
| 1871 | } |
| 1872 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1873 | static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl); |
| 1874 | |
| 1875 | static int find_later_rq(struct task_struct *task) |
| 1876 | { |
| 1877 | struct sched_domain *sd; |
Christoph Lameter | 4ba2968 | 2014-08-26 19:12:21 -0500 | [diff] [blame] | 1878 | struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1879 | int this_cpu = smp_processor_id(); |
Byungchul Park | b18c3ca | 2017-05-23 11:00:56 +0900 | [diff] [blame] | 1880 | int cpu = task_cpu(task); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1881 | |
| 1882 | /* Make sure the mask is initialized first */ |
| 1883 | if (unlikely(!later_mask)) |
| 1884 | return -1; |
| 1885 | |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 1886 | if (task->nr_cpus_allowed == 1) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1887 | return -1; |
| 1888 | |
Juri Lelli | 91ec677 | 2014-09-19 10:22:41 +0100 | [diff] [blame] | 1889 | /* |
| 1890 | * We have to consider system topology and task affinity |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 1891 | * first, then we can look for a suitable CPU. |
Juri Lelli | 91ec677 | 2014-09-19 10:22:41 +0100 | [diff] [blame] | 1892 | */ |
Byungchul Park | 3261ed0 | 2017-05-23 11:00:57 +0900 | [diff] [blame] | 1893 | if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask)) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1894 | return -1; |
| 1895 | |
| 1896 | /* |
Byungchul Park | b18c3ca | 2017-05-23 11:00:56 +0900 | [diff] [blame] | 1897 | * If we are here, some targets have been found, including |
| 1898 | * the most suitable which is, among the runqueues where the |
| 1899 | * current tasks have later deadlines than the task's one, the |
| 1900 | * rq with the latest possible one. |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1901 | * |
| 1902 | * Now we check how well this matches with task's |
| 1903 | * affinity and system topology. |
| 1904 | * |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 1905 | * The last CPU where the task run is our first |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1906 | * guess, since it is most likely cache-hot there. |
| 1907 | */ |
| 1908 | if (cpumask_test_cpu(cpu, later_mask)) |
| 1909 | return cpu; |
| 1910 | /* |
| 1911 | * Check if this_cpu is to be skipped (i.e., it is |
| 1912 | * not in the mask) or not. |
| 1913 | */ |
| 1914 | if (!cpumask_test_cpu(this_cpu, later_mask)) |
| 1915 | this_cpu = -1; |
| 1916 | |
| 1917 | rcu_read_lock(); |
| 1918 | for_each_domain(cpu, sd) { |
| 1919 | if (sd->flags & SD_WAKE_AFFINE) { |
Byungchul Park | b18c3ca | 2017-05-23 11:00:56 +0900 | [diff] [blame] | 1920 | int best_cpu; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1921 | |
| 1922 | /* |
| 1923 | * If possible, preempting this_cpu is |
| 1924 | * cheaper than migrating. |
| 1925 | */ |
| 1926 | if (this_cpu != -1 && |
| 1927 | cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { |
| 1928 | rcu_read_unlock(); |
| 1929 | return this_cpu; |
| 1930 | } |
| 1931 | |
Byungchul Park | b18c3ca | 2017-05-23 11:00:56 +0900 | [diff] [blame] | 1932 | best_cpu = cpumask_first_and(later_mask, |
| 1933 | sched_domain_span(sd)); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1934 | /* |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 1935 | * Last chance: if a CPU being in both later_mask |
Byungchul Park | b18c3ca | 2017-05-23 11:00:56 +0900 | [diff] [blame] | 1936 | * and current sd span is valid, that becomes our |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 1937 | * choice. Of course, the latest possible CPU is |
Byungchul Park | b18c3ca | 2017-05-23 11:00:56 +0900 | [diff] [blame] | 1938 | * already under consideration through later_mask. |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1939 | */ |
Byungchul Park | b18c3ca | 2017-05-23 11:00:56 +0900 | [diff] [blame] | 1940 | if (best_cpu < nr_cpu_ids) { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1941 | rcu_read_unlock(); |
| 1942 | return best_cpu; |
| 1943 | } |
| 1944 | } |
| 1945 | } |
| 1946 | rcu_read_unlock(); |
| 1947 | |
| 1948 | /* |
| 1949 | * At this point, all our guesses failed, we just return |
| 1950 | * 'something', and let the caller sort the things out. |
| 1951 | */ |
| 1952 | if (this_cpu != -1) |
| 1953 | return this_cpu; |
| 1954 | |
| 1955 | cpu = cpumask_any(later_mask); |
| 1956 | if (cpu < nr_cpu_ids) |
| 1957 | return cpu; |
| 1958 | |
| 1959 | return -1; |
| 1960 | } |
| 1961 | |
| 1962 | /* Locks the rq it finds */ |
| 1963 | static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq) |
| 1964 | { |
| 1965 | struct rq *later_rq = NULL; |
| 1966 | int tries; |
| 1967 | int cpu; |
| 1968 | |
| 1969 | for (tries = 0; tries < DL_MAX_TRIES; tries++) { |
| 1970 | cpu = find_later_rq(task); |
| 1971 | |
| 1972 | if ((cpu == -1) || (cpu == rq->cpu)) |
| 1973 | break; |
| 1974 | |
| 1975 | later_rq = cpu_rq(cpu); |
| 1976 | |
Luca Abeni | 5aa5050 | 2015-10-16 10:06:21 +0200 | [diff] [blame] | 1977 | if (later_rq->dl.dl_nr_running && |
| 1978 | !dl_time_before(task->dl.deadline, |
Wanpeng Li | 9d51426 | 2015-05-13 14:01:03 +0800 | [diff] [blame] | 1979 | later_rq->dl.earliest_dl.curr)) { |
| 1980 | /* |
| 1981 | * Target rq has tasks of equal or earlier deadline, |
| 1982 | * retrying does not release any lock and is unlikely |
| 1983 | * to yield a different result. |
| 1984 | */ |
| 1985 | later_rq = NULL; |
| 1986 | break; |
| 1987 | } |
| 1988 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1989 | /* Retry if something changed. */ |
| 1990 | if (double_lock_balance(rq, later_rq)) { |
| 1991 | if (unlikely(task_rq(task) != rq || |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 1992 | !cpumask_test_cpu(later_rq->cpu, task->cpus_ptr) || |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 1993 | task_running(rq, task) || |
Xunlei Pang | 13b5ab0 | 2016-05-09 12:11:31 +0800 | [diff] [blame] | 1994 | !dl_task(task) || |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 1995 | !task_on_rq_queued(task))) { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1996 | double_unlock_balance(rq, later_rq); |
| 1997 | later_rq = NULL; |
| 1998 | break; |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * If the rq we found has no -deadline task, or |
| 2004 | * its earliest one has a later deadline than our |
| 2005 | * task, the rq is a good one. |
| 2006 | */ |
| 2007 | if (!later_rq->dl.dl_nr_running || |
| 2008 | dl_time_before(task->dl.deadline, |
| 2009 | later_rq->dl.earliest_dl.curr)) |
| 2010 | break; |
| 2011 | |
| 2012 | /* Otherwise we try again. */ |
| 2013 | double_unlock_balance(rq, later_rq); |
| 2014 | later_rq = NULL; |
| 2015 | } |
| 2016 | |
| 2017 | return later_rq; |
| 2018 | } |
| 2019 | |
| 2020 | static struct task_struct *pick_next_pushable_dl_task(struct rq *rq) |
| 2021 | { |
| 2022 | struct task_struct *p; |
| 2023 | |
| 2024 | if (!has_pushable_dl_tasks(rq)) |
| 2025 | return NULL; |
| 2026 | |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 2027 | p = rb_entry(rq->dl.pushable_dl_tasks_root.rb_leftmost, |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2028 | struct task_struct, pushable_dl_tasks); |
| 2029 | |
| 2030 | BUG_ON(rq->cpu != task_cpu(p)); |
| 2031 | BUG_ON(task_current(rq, p)); |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 2032 | BUG_ON(p->nr_cpus_allowed <= 1); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2033 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2034 | BUG_ON(!task_on_rq_queued(p)); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2035 | BUG_ON(!dl_task(p)); |
| 2036 | |
| 2037 | return p; |
| 2038 | } |
| 2039 | |
| 2040 | /* |
| 2041 | * See if the non running -deadline tasks on this rq |
| 2042 | * can be sent to some other CPU where they can preempt |
| 2043 | * and start executing. |
| 2044 | */ |
| 2045 | static int push_dl_task(struct rq *rq) |
| 2046 | { |
| 2047 | struct task_struct *next_task; |
| 2048 | struct rq *later_rq; |
Wanpeng Li | c51b8ab | 2014-11-06 15:22:44 +0800 | [diff] [blame] | 2049 | int ret = 0; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2050 | |
| 2051 | if (!rq->dl.overloaded) |
| 2052 | return 0; |
| 2053 | |
| 2054 | next_task = pick_next_pushable_dl_task(rq); |
| 2055 | if (!next_task) |
| 2056 | return 0; |
| 2057 | |
| 2058 | retry: |
Yangtao Li | 9ebc605 | 2018-11-03 13:26:02 -0400 | [diff] [blame] | 2059 | if (WARN_ON(next_task == rq->curr)) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2060 | return 0; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2061 | |
| 2062 | /* |
| 2063 | * If next_task preempts rq->curr, and rq->curr |
| 2064 | * can move away, it makes sense to just reschedule |
| 2065 | * without going further in pushing next_task. |
| 2066 | */ |
| 2067 | if (dl_task(rq->curr) && |
| 2068 | dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) && |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 2069 | rq->curr->nr_cpus_allowed > 1) { |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 2070 | resched_curr(rq); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2071 | return 0; |
| 2072 | } |
| 2073 | |
| 2074 | /* We might release rq lock */ |
| 2075 | get_task_struct(next_task); |
| 2076 | |
| 2077 | /* Will lock the rq it'll find */ |
| 2078 | later_rq = find_lock_later_rq(next_task, rq); |
| 2079 | if (!later_rq) { |
| 2080 | struct task_struct *task; |
| 2081 | |
| 2082 | /* |
| 2083 | * We must check all this again, since |
| 2084 | * find_lock_later_rq releases rq->lock and it is |
| 2085 | * then possible that next_task has migrated. |
| 2086 | */ |
| 2087 | task = pick_next_pushable_dl_task(rq); |
Byungchul Park | a776b96 | 2017-05-12 10:05:59 +0900 | [diff] [blame] | 2088 | if (task == next_task) { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2089 | /* |
| 2090 | * The task is still there. We don't try |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2091 | * again, some other CPU will pull it when ready. |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2092 | */ |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2093 | goto out; |
| 2094 | } |
| 2095 | |
| 2096 | if (!task) |
| 2097 | /* No more tasks */ |
| 2098 | goto out; |
| 2099 | |
| 2100 | put_task_struct(next_task); |
| 2101 | next_task = task; |
| 2102 | goto retry; |
| 2103 | } |
| 2104 | |
| 2105 | deactivate_task(rq, next_task, 0); |
| 2106 | set_task_cpu(next_task, later_rq->cpu); |
Daniel Bristot de Oliveira | 840d719 | 2018-07-20 11:16:30 +0200 | [diff] [blame] | 2107 | |
| 2108 | /* |
| 2109 | * Update the later_rq clock here, because the clock is used |
| 2110 | * by the cpufreq_update_util() inside __add_running_bw(). |
| 2111 | */ |
| 2112 | update_rq_clock(later_rq); |
Daniel Bristot de Oliveira | 840d719 | 2018-07-20 11:16:30 +0200 | [diff] [blame] | 2113 | activate_task(later_rq, next_task, ENQUEUE_NOCLOCK); |
Wanpeng Li | c51b8ab | 2014-11-06 15:22:44 +0800 | [diff] [blame] | 2114 | ret = 1; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2115 | |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 2116 | resched_curr(later_rq); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2117 | |
| 2118 | double_unlock_balance(rq, later_rq); |
| 2119 | |
| 2120 | out: |
| 2121 | put_task_struct(next_task); |
| 2122 | |
Wanpeng Li | c51b8ab | 2014-11-06 15:22:44 +0800 | [diff] [blame] | 2123 | return ret; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | static void push_dl_tasks(struct rq *rq) |
| 2127 | { |
Andrea Parri | 4ffa08e | 2015-08-05 15:56:18 +0200 | [diff] [blame] | 2128 | /* push_dl_task() will return true if it moved a -deadline task */ |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2129 | while (push_dl_task(rq)) |
| 2130 | ; |
| 2131 | } |
| 2132 | |
Peter Zijlstra | 0ea60c2 | 2015-06-11 14:46:42 +0200 | [diff] [blame] | 2133 | static void pull_dl_task(struct rq *this_rq) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2134 | { |
Peter Zijlstra | 0ea60c2 | 2015-06-11 14:46:42 +0200 | [diff] [blame] | 2135 | int this_cpu = this_rq->cpu, cpu; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2136 | struct task_struct *p; |
Peter Zijlstra | 0ea60c2 | 2015-06-11 14:46:42 +0200 | [diff] [blame] | 2137 | bool resched = false; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2138 | struct rq *src_rq; |
| 2139 | u64 dmin = LONG_MAX; |
| 2140 | |
| 2141 | if (likely(!dl_overloaded(this_rq))) |
Peter Zijlstra | 0ea60c2 | 2015-06-11 14:46:42 +0200 | [diff] [blame] | 2142 | return; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2143 | |
| 2144 | /* |
| 2145 | * Match the barrier from dl_set_overloaded; this guarantees that if we |
| 2146 | * see overloaded we must also see the dlo_mask bit. |
| 2147 | */ |
| 2148 | smp_rmb(); |
| 2149 | |
| 2150 | for_each_cpu(cpu, this_rq->rd->dlo_mask) { |
| 2151 | if (this_cpu == cpu) |
| 2152 | continue; |
| 2153 | |
| 2154 | src_rq = cpu_rq(cpu); |
| 2155 | |
| 2156 | /* |
| 2157 | * It looks racy, abd it is! However, as in sched_rt.c, |
| 2158 | * we are fine with this. |
| 2159 | */ |
| 2160 | if (this_rq->dl.dl_nr_running && |
| 2161 | dl_time_before(this_rq->dl.earliest_dl.curr, |
| 2162 | src_rq->dl.earliest_dl.next)) |
| 2163 | continue; |
| 2164 | |
| 2165 | /* Might drop this_rq->lock */ |
| 2166 | double_lock_balance(this_rq, src_rq); |
| 2167 | |
| 2168 | /* |
| 2169 | * If there are no more pullable tasks on the |
| 2170 | * rq, we're done with it. |
| 2171 | */ |
| 2172 | if (src_rq->dl.dl_nr_running <= 1) |
| 2173 | goto skip; |
| 2174 | |
Wanpeng Li | 8b5e770 | 2015-05-13 14:01:01 +0800 | [diff] [blame] | 2175 | p = pick_earliest_pushable_dl_task(src_rq, this_cpu); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2176 | |
| 2177 | /* |
| 2178 | * We found a task to be pulled if: |
| 2179 | * - it preempts our current (if there's one), |
| 2180 | * - it will preempt the last one we pulled (if any). |
| 2181 | */ |
| 2182 | if (p && dl_time_before(p->dl.deadline, dmin) && |
| 2183 | (!this_rq->dl.dl_nr_running || |
| 2184 | dl_time_before(p->dl.deadline, |
| 2185 | this_rq->dl.earliest_dl.curr))) { |
| 2186 | WARN_ON(p == src_rq->curr); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2187 | WARN_ON(!task_on_rq_queued(p)); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2188 | |
| 2189 | /* |
| 2190 | * Then we pull iff p has actually an earlier |
| 2191 | * deadline than the current task of its runqueue. |
| 2192 | */ |
| 2193 | if (dl_time_before(p->dl.deadline, |
| 2194 | src_rq->curr->dl.deadline)) |
| 2195 | goto skip; |
| 2196 | |
Peter Zijlstra | 0ea60c2 | 2015-06-11 14:46:42 +0200 | [diff] [blame] | 2197 | resched = true; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2198 | |
| 2199 | deactivate_task(src_rq, p, 0); |
| 2200 | set_task_cpu(p, this_cpu); |
| 2201 | activate_task(this_rq, p, 0); |
| 2202 | dmin = p->dl.deadline; |
| 2203 | |
| 2204 | /* Is there any other task even earlier? */ |
| 2205 | } |
| 2206 | skip: |
| 2207 | double_unlock_balance(this_rq, src_rq); |
| 2208 | } |
| 2209 | |
Peter Zijlstra | 0ea60c2 | 2015-06-11 14:46:42 +0200 | [diff] [blame] | 2210 | if (resched) |
| 2211 | resched_curr(this_rq); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | /* |
| 2215 | * Since the task is not running and a reschedule is not going to happen |
| 2216 | * anytime soon on its runqueue, we try pushing it away now. |
| 2217 | */ |
| 2218 | static void task_woken_dl(struct rq *rq, struct task_struct *p) |
| 2219 | { |
| 2220 | if (!task_running(rq, p) && |
| 2221 | !test_tsk_need_resched(rq->curr) && |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 2222 | p->nr_cpus_allowed > 1 && |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2223 | dl_task(rq->curr) && |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 2224 | (rq->curr->nr_cpus_allowed < 2 || |
Wanpeng Li | 6b0a563 | 2014-10-31 06:39:34 +0800 | [diff] [blame] | 2225 | !dl_entity_preempt(&p->dl, &rq->curr->dl))) { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2226 | push_dl_tasks(rq); |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | static void set_cpus_allowed_dl(struct task_struct *p, |
| 2231 | const struct cpumask *new_mask) |
| 2232 | { |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 2233 | struct root_domain *src_rd; |
Peter Zijlstra | 6c37067 | 2015-05-15 17:43:36 +0200 | [diff] [blame] | 2234 | struct rq *rq; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2235 | |
| 2236 | BUG_ON(!dl_task(p)); |
| 2237 | |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 2238 | rq = task_rq(p); |
| 2239 | src_rd = rq->rd; |
| 2240 | /* |
| 2241 | * Migrating a SCHED_DEADLINE task between exclusive |
| 2242 | * cpusets (different root_domains) entails a bandwidth |
| 2243 | * update. We already made space for us in the destination |
| 2244 | * domain (see cpuset_can_attach()). |
| 2245 | */ |
| 2246 | if (!cpumask_intersects(src_rd->span, new_mask)) { |
| 2247 | struct dl_bw *src_dl_b; |
| 2248 | |
| 2249 | src_dl_b = dl_bw_of(cpu_of(rq)); |
| 2250 | /* |
| 2251 | * We now free resources of the root_domain we are migrating |
| 2252 | * off. In the worst case, sched_setattr() may temporary fail |
| 2253 | * until we complete the update. |
| 2254 | */ |
| 2255 | raw_spin_lock(&src_dl_b->lock); |
Peter Zijlstra | 8c0944ce | 2017-09-07 12:09:30 +0200 | [diff] [blame] | 2256 | __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 2257 | raw_spin_unlock(&src_dl_b->lock); |
| 2258 | } |
| 2259 | |
Peter Zijlstra | 6c37067 | 2015-05-15 17:43:36 +0200 | [diff] [blame] | 2260 | set_cpus_allowed_common(p, new_mask); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | /* Assumes rq->lock is held */ |
| 2264 | static void rq_online_dl(struct rq *rq) |
| 2265 | { |
| 2266 | if (rq->dl.overloaded) |
| 2267 | dl_set_overload(rq); |
Juri Lelli | 6bfd6d7 | 2013-11-07 14:43:47 +0100 | [diff] [blame] | 2268 | |
Xunlei Pang | 16b2694 | 2015-01-19 04:49:36 +0000 | [diff] [blame] | 2269 | cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu); |
Juri Lelli | 6bfd6d7 | 2013-11-07 14:43:47 +0100 | [diff] [blame] | 2270 | if (rq->dl.dl_nr_running > 0) |
Tommaso Cucinotta | d8206bb | 2016-08-14 16:27:08 +0200 | [diff] [blame] | 2271 | cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | /* Assumes rq->lock is held */ |
| 2275 | static void rq_offline_dl(struct rq *rq) |
| 2276 | { |
| 2277 | if (rq->dl.overloaded) |
| 2278 | dl_clear_overload(rq); |
Juri Lelli | 6bfd6d7 | 2013-11-07 14:43:47 +0100 | [diff] [blame] | 2279 | |
Tommaso Cucinotta | d8206bb | 2016-08-14 16:27:08 +0200 | [diff] [blame] | 2280 | cpudl_clear(&rq->rd->cpudl, rq->cpu); |
Xunlei Pang | 16b2694 | 2015-01-19 04:49:36 +0000 | [diff] [blame] | 2281 | cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2282 | } |
| 2283 | |
Wanpeng Li | a6c0e74 | 2015-05-13 14:01:02 +0800 | [diff] [blame] | 2284 | void __init init_sched_dl_class(void) |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2285 | { |
| 2286 | unsigned int i; |
| 2287 | |
| 2288 | for_each_possible_cpu(i) |
| 2289 | zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i), |
| 2290 | GFP_KERNEL, cpu_to_node(i)); |
| 2291 | } |
| 2292 | |
Mathieu Poirier | f9a25f7 | 2019-07-19 15:59:55 +0200 | [diff] [blame] | 2293 | void dl_add_task_root_domain(struct task_struct *p) |
| 2294 | { |
| 2295 | struct rq_flags rf; |
| 2296 | struct rq *rq; |
| 2297 | struct dl_bw *dl_b; |
| 2298 | |
| 2299 | rq = task_rq_lock(p, &rf); |
| 2300 | if (!dl_task(p)) |
| 2301 | goto unlock; |
| 2302 | |
| 2303 | dl_b = &rq->rd->dl_bw; |
| 2304 | raw_spin_lock(&dl_b->lock); |
| 2305 | |
| 2306 | __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span)); |
| 2307 | |
| 2308 | raw_spin_unlock(&dl_b->lock); |
| 2309 | |
| 2310 | unlock: |
| 2311 | task_rq_unlock(rq, p, &rf); |
| 2312 | } |
| 2313 | |
| 2314 | void dl_clear_root_domain(struct root_domain *rd) |
| 2315 | { |
| 2316 | unsigned long flags; |
| 2317 | |
| 2318 | raw_spin_lock_irqsave(&rd->dl_bw.lock, flags); |
| 2319 | rd->dl_bw.total_bw = 0; |
| 2320 | raw_spin_unlock_irqrestore(&rd->dl_bw.lock, flags); |
| 2321 | } |
| 2322 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2323 | #endif /* CONFIG_SMP */ |
| 2324 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2325 | static void switched_from_dl(struct rq *rq, struct task_struct *p) |
| 2326 | { |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 2327 | /* |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 2328 | * task_non_contending() can start the "inactive timer" (if the 0-lag |
| 2329 | * time is in the future). If the task switches back to dl before |
| 2330 | * the "inactive timer" fires, it can continue to consume its current |
| 2331 | * runtime using its current deadline. If it stays outside of |
| 2332 | * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer() |
| 2333 | * will reset the task parameters. |
Peter Zijlstra | a649f23 | 2015-06-11 14:46:49 +0200 | [diff] [blame] | 2334 | */ |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 2335 | if (task_on_rq_queued(p) && p->dl.dl_runtime) |
| 2336 | task_non_contending(p); |
| 2337 | |
Juri Lelli | e117cb5 | 2018-07-11 09:29:48 +0200 | [diff] [blame] | 2338 | if (!task_on_rq_queued(p)) { |
| 2339 | /* |
| 2340 | * Inactive timer is armed. However, p is leaving DEADLINE and |
| 2341 | * might migrate away from this rq while continuing to run on |
| 2342 | * some other class. We need to remove its contribution from |
| 2343 | * this rq running_bw now, or sub_rq_bw (below) will complain. |
| 2344 | */ |
| 2345 | if (p->dl.dl_non_contending) |
| 2346 | sub_running_bw(&p->dl, &rq->dl); |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 2347 | sub_rq_bw(&p->dl, &rq->dl); |
Juri Lelli | e117cb5 | 2018-07-11 09:29:48 +0200 | [diff] [blame] | 2348 | } |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 2349 | |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 2350 | /* |
| 2351 | * We cannot use inactive_task_timer() to invoke sub_running_bw() |
| 2352 | * at the 0-lag time, because the task could have been migrated |
| 2353 | * while SCHED_OTHER in the meanwhile. |
| 2354 | */ |
| 2355 | if (p->dl.dl_non_contending) |
| 2356 | p->dl.dl_non_contending = 0; |
Juri Lelli | a5e7be3 | 2014-09-19 10:22:39 +0100 | [diff] [blame] | 2357 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2358 | /* |
| 2359 | * Since this might be the only -deadline task on the rq, |
| 2360 | * this is the right place to try to pull some other one |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2361 | * from an overloaded CPU, if any. |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2362 | */ |
Wanpeng Li | cd66091 | 2014-10-31 06:39:35 +0800 | [diff] [blame] | 2363 | if (!task_on_rq_queued(p) || rq->dl.dl_nr_running) |
| 2364 | return; |
| 2365 | |
Ingo Molnar | 02d8ec9 | 2018-03-03 16:27:54 +0100 | [diff] [blame] | 2366 | deadline_queue_pull_task(rq); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2367 | } |
| 2368 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2369 | /* |
| 2370 | * When switching to -deadline, we may overload the rq, then |
| 2371 | * we try to push someone off, if possible. |
| 2372 | */ |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2373 | static void switched_to_dl(struct rq *rq, struct task_struct *p) |
| 2374 | { |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 2375 | if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) |
| 2376 | put_task_struct(p); |
Luca Abeni | 72f9f3f | 2016-03-07 12:27:04 +0100 | [diff] [blame] | 2377 | |
Juri Lelli | 98b0a85 | 2016-08-05 16:07:55 +0100 | [diff] [blame] | 2378 | /* If p is not queued we will update its parameters at next wakeup. */ |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 2379 | if (!task_on_rq_queued(p)) { |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 2380 | add_rq_bw(&p->dl, &rq->dl); |
Juri Lelli | 98b0a85 | 2016-08-05 16:07:55 +0100 | [diff] [blame] | 2381 | |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 2382 | return; |
| 2383 | } |
Juri Lelli | 98b0a85 | 2016-08-05 16:07:55 +0100 | [diff] [blame] | 2384 | |
| 2385 | if (rq->curr != p) { |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2386 | #ifdef CONFIG_SMP |
Ingo Molnar | 4b53a34 | 2017-02-05 15:41:03 +0100 | [diff] [blame] | 2387 | if (p->nr_cpus_allowed > 1 && rq->dl.overloaded) |
Ingo Molnar | 02d8ec9 | 2018-03-03 16:27:54 +0100 | [diff] [blame] | 2388 | deadline_queue_push_tasks(rq); |
Sebastian Andrzej Siewior | 619bd4a | 2017-01-24 15:40:06 +0100 | [diff] [blame] | 2389 | #endif |
Peter Zijlstra | 9916e21 | 2015-06-11 14:46:43 +0200 | [diff] [blame] | 2390 | if (dl_task(rq->curr)) |
| 2391 | check_preempt_curr_dl(rq, p, 0); |
| 2392 | else |
| 2393 | resched_curr(rq); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2397 | /* |
| 2398 | * If the scheduling parameters of a -deadline task changed, |
| 2399 | * a push or pull operation might be needed. |
| 2400 | */ |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2401 | static void prio_changed_dl(struct rq *rq, struct task_struct *p, |
| 2402 | int oldprio) |
| 2403 | { |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2404 | if (task_on_rq_queued(p) || rq->curr == p) { |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2405 | #ifdef CONFIG_SMP |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2406 | /* |
| 2407 | * This might be too much, but unfortunately |
| 2408 | * we don't have the old deadline value, and |
| 2409 | * we can't argue if the task is increasing |
| 2410 | * or lowering its prio, so... |
| 2411 | */ |
| 2412 | if (!rq->dl.overloaded) |
Ingo Molnar | 02d8ec9 | 2018-03-03 16:27:54 +0100 | [diff] [blame] | 2413 | deadline_queue_pull_task(rq); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2414 | |
| 2415 | /* |
| 2416 | * If we now have a earlier deadline task than p, |
| 2417 | * then reschedule, provided p is still on this |
| 2418 | * runqueue. |
| 2419 | */ |
Peter Zijlstra | 9916e21 | 2015-06-11 14:46:43 +0200 | [diff] [blame] | 2420 | if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline)) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 2421 | resched_curr(rq); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2422 | #else |
| 2423 | /* |
| 2424 | * Again, we don't know if p has a earlier |
| 2425 | * or later deadline, so let's blindly set a |
| 2426 | * (maybe not needed) rescheduling point. |
| 2427 | */ |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 2428 | resched_curr(rq); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2429 | #endif /* CONFIG_SMP */ |
Peter Zijlstra | 801ccdb | 2016-02-25 15:01:49 +0100 | [diff] [blame] | 2430 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2431 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2432 | |
| 2433 | const struct sched_class dl_sched_class = { |
| 2434 | .next = &rt_sched_class, |
| 2435 | .enqueue_task = enqueue_task_dl, |
| 2436 | .dequeue_task = dequeue_task_dl, |
| 2437 | .yield_task = yield_task_dl, |
| 2438 | |
| 2439 | .check_preempt_curr = check_preempt_curr_dl, |
| 2440 | |
| 2441 | .pick_next_task = pick_next_task_dl, |
| 2442 | .put_prev_task = put_prev_task_dl, |
Peter Zijlstra | 03b7fad | 2019-05-29 20:36:41 +0000 | [diff] [blame] | 2443 | .set_next_task = set_next_task_dl, |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2444 | |
| 2445 | #ifdef CONFIG_SMP |
Peter Zijlstra | 6e2df05 | 2019-11-08 11:11:52 +0100 | [diff] [blame] | 2446 | .balance = balance_dl, |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2447 | .select_task_rq = select_task_rq_dl, |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 2448 | .migrate_task_rq = migrate_task_rq_dl, |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2449 | .set_cpus_allowed = set_cpus_allowed_dl, |
| 2450 | .rq_online = rq_online_dl, |
| 2451 | .rq_offline = rq_offline_dl, |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2452 | .task_woken = task_woken_dl, |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2453 | #endif |
| 2454 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2455 | .task_tick = task_tick_dl, |
| 2456 | .task_fork = task_fork_dl, |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2457 | |
| 2458 | .prio_changed = prio_changed_dl, |
| 2459 | .switched_from = switched_from_dl, |
| 2460 | .switched_to = switched_to_dl, |
Stanislaw Gruszka | 6e99891 | 2014-11-12 16:58:44 +0100 | [diff] [blame] | 2461 | |
| 2462 | .update_curr = update_curr_dl, |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 2463 | }; |
Wanpeng Li | acb3213 | 2014-10-31 06:39:33 +0800 | [diff] [blame] | 2464 | |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2465 | int sched_dl_global_validate(void) |
| 2466 | { |
| 2467 | u64 runtime = global_rt_runtime(); |
| 2468 | u64 period = global_rt_period(); |
| 2469 | u64 new_bw = to_ratio(period, runtime); |
| 2470 | struct dl_bw *dl_b; |
| 2471 | int cpu, ret = 0; |
| 2472 | unsigned long flags; |
| 2473 | |
| 2474 | /* |
| 2475 | * Here we want to check the bandwidth not being set to some |
| 2476 | * value smaller than the currently allocated bandwidth in |
| 2477 | * any of the root_domains. |
| 2478 | * |
| 2479 | * FIXME: Cycling on all the CPUs is overdoing, but simpler than |
| 2480 | * cycling on root_domains... Discussion on different/better |
| 2481 | * solutions is welcome! |
| 2482 | */ |
| 2483 | for_each_possible_cpu(cpu) { |
| 2484 | rcu_read_lock_sched(); |
| 2485 | dl_b = dl_bw_of(cpu); |
| 2486 | |
| 2487 | raw_spin_lock_irqsave(&dl_b->lock, flags); |
| 2488 | if (new_bw < dl_b->total_bw) |
| 2489 | ret = -EBUSY; |
| 2490 | raw_spin_unlock_irqrestore(&dl_b->lock, flags); |
| 2491 | |
| 2492 | rcu_read_unlock_sched(); |
| 2493 | |
| 2494 | if (ret) |
| 2495 | break; |
| 2496 | } |
| 2497 | |
| 2498 | return ret; |
| 2499 | } |
| 2500 | |
Yu Chen | ba4f7bc | 2020-02-28 18:03:29 +0800 | [diff] [blame] | 2501 | static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq) |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2502 | { |
| 2503 | if (global_rt_runtime() == RUNTIME_INF) { |
| 2504 | dl_rq->bw_ratio = 1 << RATIO_SHIFT; |
| 2505 | dl_rq->extra_bw = 1 << BW_SHIFT; |
| 2506 | } else { |
| 2507 | dl_rq->bw_ratio = to_ratio(global_rt_runtime(), |
| 2508 | global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT); |
| 2509 | dl_rq->extra_bw = to_ratio(global_rt_period(), |
| 2510 | global_rt_runtime()); |
| 2511 | } |
| 2512 | } |
| 2513 | |
| 2514 | void sched_dl_do_global(void) |
| 2515 | { |
| 2516 | u64 new_bw = -1; |
| 2517 | struct dl_bw *dl_b; |
| 2518 | int cpu; |
| 2519 | unsigned long flags; |
| 2520 | |
| 2521 | def_dl_bandwidth.dl_period = global_rt_period(); |
| 2522 | def_dl_bandwidth.dl_runtime = global_rt_runtime(); |
| 2523 | |
| 2524 | if (global_rt_runtime() != RUNTIME_INF) |
| 2525 | new_bw = to_ratio(global_rt_period(), global_rt_runtime()); |
| 2526 | |
| 2527 | /* |
| 2528 | * FIXME: As above... |
| 2529 | */ |
| 2530 | for_each_possible_cpu(cpu) { |
| 2531 | rcu_read_lock_sched(); |
| 2532 | dl_b = dl_bw_of(cpu); |
| 2533 | |
| 2534 | raw_spin_lock_irqsave(&dl_b->lock, flags); |
| 2535 | dl_b->bw = new_bw; |
| 2536 | raw_spin_unlock_irqrestore(&dl_b->lock, flags); |
| 2537 | |
| 2538 | rcu_read_unlock_sched(); |
| 2539 | init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl); |
| 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * We must be sure that accepting a new task (or allowing changing the |
| 2545 | * parameters of an existing one) is consistent with the bandwidth |
| 2546 | * constraints. If yes, this function also accordingly updates the currently |
| 2547 | * allocated bandwidth to reflect the new situation. |
| 2548 | * |
| 2549 | * This function is called while holding p's rq->lock. |
| 2550 | */ |
| 2551 | int sched_dl_overflow(struct task_struct *p, int policy, |
| 2552 | const struct sched_attr *attr) |
| 2553 | { |
| 2554 | struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); |
| 2555 | u64 period = attr->sched_period ?: attr->sched_deadline; |
| 2556 | u64 runtime = attr->sched_runtime; |
| 2557 | u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0; |
| 2558 | int cpus, err = -1; |
| 2559 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 2560 | if (attr->sched_flags & SCHED_FLAG_SUGOV) |
| 2561 | return 0; |
| 2562 | |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2563 | /* !deadline task may carry old deadline bandwidth */ |
| 2564 | if (new_bw == p->dl.dl_bw && task_has_dl_policy(p)) |
| 2565 | return 0; |
| 2566 | |
| 2567 | /* |
| 2568 | * Either if a task, enters, leave, or stays -deadline but changes |
| 2569 | * its parameters, we may need to update accordingly the total |
| 2570 | * allocated bandwidth of the container. |
| 2571 | */ |
| 2572 | raw_spin_lock(&dl_b->lock); |
| 2573 | cpus = dl_bw_cpus(task_cpu(p)); |
| 2574 | if (dl_policy(policy) && !task_has_dl_policy(p) && |
| 2575 | !__dl_overflow(dl_b, cpus, 0, new_bw)) { |
| 2576 | if (hrtimer_active(&p->dl.inactive_timer)) |
Peter Zijlstra | 8c0944ce | 2017-09-07 12:09:30 +0200 | [diff] [blame] | 2577 | __dl_sub(dl_b, p->dl.dl_bw, cpus); |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2578 | __dl_add(dl_b, new_bw, cpus); |
| 2579 | err = 0; |
| 2580 | } else if (dl_policy(policy) && task_has_dl_policy(p) && |
| 2581 | !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) { |
| 2582 | /* |
| 2583 | * XXX this is slightly incorrect: when the task |
| 2584 | * utilization decreases, we should delay the total |
| 2585 | * utilization change until the task's 0-lag point. |
| 2586 | * But this would require to set the task's "inactive |
| 2587 | * timer" when the task is not inactive. |
| 2588 | */ |
Peter Zijlstra | 8c0944ce | 2017-09-07 12:09:30 +0200 | [diff] [blame] | 2589 | __dl_sub(dl_b, p->dl.dl_bw, cpus); |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2590 | __dl_add(dl_b, new_bw, cpus); |
| 2591 | dl_change_utilization(p, new_bw); |
| 2592 | err = 0; |
| 2593 | } else if (!dl_policy(policy) && task_has_dl_policy(p)) { |
| 2594 | /* |
| 2595 | * Do not decrease the total deadline utilization here, |
| 2596 | * switched_from_dl() will take care to do it at the correct |
| 2597 | * (0-lag) time. |
| 2598 | */ |
| 2599 | err = 0; |
| 2600 | } |
| 2601 | raw_spin_unlock(&dl_b->lock); |
| 2602 | |
| 2603 | return err; |
| 2604 | } |
| 2605 | |
| 2606 | /* |
| 2607 | * This function initializes the sched_dl_entity of a newly becoming |
| 2608 | * SCHED_DEADLINE task. |
| 2609 | * |
| 2610 | * Only the static values are considered here, the actual runtime and the |
| 2611 | * absolute deadline will be properly calculated when the task is enqueued |
| 2612 | * for the first time with its new policy. |
| 2613 | */ |
| 2614 | void __setparam_dl(struct task_struct *p, const struct sched_attr *attr) |
| 2615 | { |
| 2616 | struct sched_dl_entity *dl_se = &p->dl; |
| 2617 | |
| 2618 | dl_se->dl_runtime = attr->sched_runtime; |
| 2619 | dl_se->dl_deadline = attr->sched_deadline; |
| 2620 | dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline; |
| 2621 | dl_se->flags = attr->sched_flags; |
| 2622 | dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime); |
| 2623 | dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime); |
| 2624 | } |
| 2625 | |
| 2626 | void __getparam_dl(struct task_struct *p, struct sched_attr *attr) |
| 2627 | { |
| 2628 | struct sched_dl_entity *dl_se = &p->dl; |
| 2629 | |
| 2630 | attr->sched_priority = p->rt_priority; |
| 2631 | attr->sched_runtime = dl_se->dl_runtime; |
| 2632 | attr->sched_deadline = dl_se->dl_deadline; |
| 2633 | attr->sched_period = dl_se->dl_period; |
| 2634 | attr->sched_flags = dl_se->flags; |
| 2635 | } |
| 2636 | |
| 2637 | /* |
| 2638 | * This function validates the new parameters of a -deadline task. |
| 2639 | * We ask for the deadline not being zero, and greater or equal |
| 2640 | * than the runtime, as well as the period of being zero or |
| 2641 | * greater than deadline. Furthermore, we have to be sure that |
| 2642 | * user parameters are above the internal resolution of 1us (we |
| 2643 | * check sched_runtime only since it is always the smaller one) and |
| 2644 | * below 2^63 ns (we have to check both sched_deadline and |
| 2645 | * sched_period, as the latter can be zero). |
| 2646 | */ |
| 2647 | bool __checkparam_dl(const struct sched_attr *attr) |
| 2648 | { |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 2649 | /* special dl tasks don't actually use any parameter */ |
| 2650 | if (attr->sched_flags & SCHED_FLAG_SUGOV) |
| 2651 | return true; |
| 2652 | |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2653 | /* deadline != 0 */ |
| 2654 | if (attr->sched_deadline == 0) |
| 2655 | return false; |
| 2656 | |
| 2657 | /* |
| 2658 | * Since we truncate DL_SCALE bits, make sure we're at least |
| 2659 | * that big. |
| 2660 | */ |
| 2661 | if (attr->sched_runtime < (1ULL << DL_SCALE)) |
| 2662 | return false; |
| 2663 | |
| 2664 | /* |
| 2665 | * Since we use the MSB for wrap-around and sign issues, make |
| 2666 | * sure it's not set (mind that period can be equal to zero). |
| 2667 | */ |
| 2668 | if (attr->sched_deadline & (1ULL << 63) || |
| 2669 | attr->sched_period & (1ULL << 63)) |
| 2670 | return false; |
| 2671 | |
| 2672 | /* runtime <= deadline <= period (if period != 0) */ |
| 2673 | if ((attr->sched_period != 0 && |
| 2674 | attr->sched_period < attr->sched_deadline) || |
| 2675 | attr->sched_deadline < attr->sched_runtime) |
| 2676 | return false; |
| 2677 | |
| 2678 | return true; |
| 2679 | } |
| 2680 | |
| 2681 | /* |
| 2682 | * This function clears the sched_dl_entity static params. |
| 2683 | */ |
| 2684 | void __dl_clear_params(struct task_struct *p) |
| 2685 | { |
| 2686 | struct sched_dl_entity *dl_se = &p->dl; |
| 2687 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2688 | dl_se->dl_runtime = 0; |
| 2689 | dl_se->dl_deadline = 0; |
| 2690 | dl_se->dl_period = 0; |
| 2691 | dl_se->flags = 0; |
| 2692 | dl_se->dl_bw = 0; |
| 2693 | dl_se->dl_density = 0; |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2694 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2695 | dl_se->dl_throttled = 0; |
| 2696 | dl_se->dl_yielded = 0; |
| 2697 | dl_se->dl_non_contending = 0; |
| 2698 | dl_se->dl_overrun = 0; |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr) |
| 2702 | { |
| 2703 | struct sched_dl_entity *dl_se = &p->dl; |
| 2704 | |
| 2705 | if (dl_se->dl_runtime != attr->sched_runtime || |
| 2706 | dl_se->dl_deadline != attr->sched_deadline || |
| 2707 | dl_se->dl_period != attr->sched_period || |
| 2708 | dl_se->flags != attr->sched_flags) |
| 2709 | return true; |
| 2710 | |
| 2711 | return false; |
| 2712 | } |
| 2713 | |
| 2714 | #ifdef CONFIG_SMP |
| 2715 | int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed) |
| 2716 | { |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2717 | unsigned int dest_cpu; |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2718 | struct dl_bw *dl_b; |
| 2719 | bool overflow; |
| 2720 | int cpus, ret; |
| 2721 | unsigned long flags; |
| 2722 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2723 | dest_cpu = cpumask_any_and(cpu_active_mask, cs_cpus_allowed); |
| 2724 | |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2725 | rcu_read_lock_sched(); |
| 2726 | dl_b = dl_bw_of(dest_cpu); |
| 2727 | raw_spin_lock_irqsave(&dl_b->lock, flags); |
| 2728 | cpus = dl_bw_cpus(dest_cpu); |
| 2729 | overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw); |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2730 | if (overflow) { |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2731 | ret = -EBUSY; |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2732 | } else { |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2733 | /* |
| 2734 | * We reserve space for this task in the destination |
| 2735 | * root_domain, as we can't fail after this point. |
| 2736 | * We will free resources in the source root_domain |
| 2737 | * later on (see set_cpus_allowed_dl()). |
| 2738 | */ |
| 2739 | __dl_add(dl_b, p->dl.dl_bw, cpus); |
| 2740 | ret = 0; |
| 2741 | } |
| 2742 | raw_spin_unlock_irqrestore(&dl_b->lock, flags); |
| 2743 | rcu_read_unlock_sched(); |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2744 | |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2745 | return ret; |
| 2746 | } |
| 2747 | |
| 2748 | int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, |
| 2749 | const struct cpumask *trial) |
| 2750 | { |
| 2751 | int ret = 1, trial_cpus; |
| 2752 | struct dl_bw *cur_dl_b; |
| 2753 | unsigned long flags; |
| 2754 | |
| 2755 | rcu_read_lock_sched(); |
| 2756 | cur_dl_b = dl_bw_of(cpumask_any(cur)); |
| 2757 | trial_cpus = cpumask_weight(trial); |
| 2758 | |
| 2759 | raw_spin_lock_irqsave(&cur_dl_b->lock, flags); |
| 2760 | if (cur_dl_b->bw != -1 && |
| 2761 | cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw) |
| 2762 | ret = 0; |
| 2763 | raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags); |
| 2764 | rcu_read_unlock_sched(); |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2765 | |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2766 | return ret; |
| 2767 | } |
| 2768 | |
| 2769 | bool dl_cpu_busy(unsigned int cpu) |
| 2770 | { |
| 2771 | unsigned long flags; |
| 2772 | struct dl_bw *dl_b; |
| 2773 | bool overflow; |
| 2774 | int cpus; |
| 2775 | |
| 2776 | rcu_read_lock_sched(); |
| 2777 | dl_b = dl_bw_of(cpu); |
| 2778 | raw_spin_lock_irqsave(&dl_b->lock, flags); |
| 2779 | cpus = dl_bw_cpus(cpu); |
| 2780 | overflow = __dl_overflow(dl_b, cpus, 0, 0); |
| 2781 | raw_spin_unlock_irqrestore(&dl_b->lock, flags); |
| 2782 | rcu_read_unlock_sched(); |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 2783 | |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 2784 | return overflow; |
| 2785 | } |
| 2786 | #endif |
| 2787 | |
Wanpeng Li | acb3213 | 2014-10-31 06:39:33 +0800 | [diff] [blame] | 2788 | #ifdef CONFIG_SCHED_DEBUG |
Wanpeng Li | acb3213 | 2014-10-31 06:39:33 +0800 | [diff] [blame] | 2789 | void print_dl_stats(struct seq_file *m, int cpu) |
| 2790 | { |
| 2791 | print_dl_rq(m, cpu, &cpu_rq(cpu)->dl); |
| 2792 | } |
| 2793 | #endif /* CONFIG_SCHED_DEBUG */ |