1 // SPDX-License-Identifier: GPL-2.0 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>, 14 * Juri Lelli <juri.lelli@gmail.com>, 15 * Michael Trimarchi <michael@amarulasolutions.com>, 16 * Fabio Checconi <fchecconi@gmail.com> 17 */ 18 19 #include <linux/cpuset.h> 20 #include <linux/sched/clock.h> 21 #include <uapi/linux/sched/types.h> 22 #include "sched.h" 23 #include "pelt.h" 24 25 /* 26 * Default limits for DL period; on the top end we guard against small util 27 * tasks still getting ridiculously long effective runtimes, on the bottom end we 28 * guard against timer DoS. 29 */ 30 static unsigned int sysctl_sched_dl_period_max = 1 << 22; /* ~4 seconds */ 31 static unsigned int sysctl_sched_dl_period_min = 100; /* 100 us */ 32 #ifdef CONFIG_SYSCTL 33 static const struct ctl_table sched_dl_sysctls[] = { 34 { 35 .procname = "sched_deadline_period_max_us", 36 .data = &sysctl_sched_dl_period_max, 37 .maxlen = sizeof(unsigned int), 38 .mode = 0644, 39 .proc_handler = proc_douintvec_minmax, 40 .extra1 = (void *)&sysctl_sched_dl_period_min, 41 }, 42 { 43 .procname = "sched_deadline_period_min_us", 44 .data = &sysctl_sched_dl_period_min, 45 .maxlen = sizeof(unsigned int), 46 .mode = 0644, 47 .proc_handler = proc_douintvec_minmax, 48 .extra2 = (void *)&sysctl_sched_dl_period_max, 49 }, 50 }; 51 52 static int __init sched_dl_sysctl_init(void) 53 { 54 register_sysctl_init("kernel", sched_dl_sysctls); 55 return 0; 56 } 57 late_initcall(sched_dl_sysctl_init); 58 #endif /* CONFIG_SYSCTL */ 59 60 static bool dl_server(struct sched_dl_entity *dl_se) 61 { 62 return dl_se->dl_server; 63 } 64 65 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se) 66 { 67 BUG_ON(dl_server(dl_se)); 68 return container_of(dl_se, struct task_struct, dl); 69 } 70 71 static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq) 72 { 73 return container_of(dl_rq, struct rq, dl); 74 } 75 76 static inline struct rq *rq_of_dl_se(struct sched_dl_entity *dl_se) 77 { 78 struct rq *rq = dl_se->rq; 79 80 if (!dl_server(dl_se)) 81 rq = task_rq(dl_task_of(dl_se)); 82 83 return rq; 84 } 85 86 static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se) 87 { 88 return &rq_of_dl_se(dl_se)->dl; 89 } 90 91 static inline int on_dl_rq(struct sched_dl_entity *dl_se) 92 { 93 return !RB_EMPTY_NODE(&dl_se->rb_node); 94 } 95 96 #ifdef CONFIG_RT_MUTEXES 97 static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se) 98 { 99 return dl_se->pi_se; 100 } 101 102 static inline bool is_dl_boosted(struct sched_dl_entity *dl_se) 103 { 104 return pi_of(dl_se) != dl_se; 105 } 106 #else /* !CONFIG_RT_MUTEXES: */ 107 static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se) 108 { 109 return dl_se; 110 } 111 112 static inline bool is_dl_boosted(struct sched_dl_entity *dl_se) 113 { 114 return false; 115 } 116 #endif /* !CONFIG_RT_MUTEXES */ 117 118 static inline struct dl_bw *dl_bw_of(int i) 119 { 120 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(), 121 "sched RCU must be held"); 122 return &cpu_rq(i)->rd->dl_bw; 123 } 124 125 static inline int dl_bw_cpus(int i) 126 { 127 struct root_domain *rd = cpu_rq(i)->rd; 128 129 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(), 130 "sched RCU must be held"); 131 132 return cpumask_weight_and(rd->span, cpu_active_mask); 133 } 134 135 static inline unsigned long __dl_bw_capacity(const struct cpumask *mask) 136 { 137 unsigned long cap = 0; 138 int i; 139 140 for_each_cpu_and(i, mask, cpu_active_mask) 141 cap += arch_scale_cpu_capacity(i); 142 143 return cap; 144 } 145 146 /* 147 * XXX Fix: If 'rq->rd == def_root_domain' perform AC against capacity 148 * of the CPU the task is running on rather rd's \Sum CPU capacity. 149 */ 150 static inline unsigned long dl_bw_capacity(int i) 151 { 152 if (!sched_asym_cpucap_active() && 153 arch_scale_cpu_capacity(i) == SCHED_CAPACITY_SCALE) { 154 return dl_bw_cpus(i) << SCHED_CAPACITY_SHIFT; 155 } else { 156 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(), 157 "sched RCU must be held"); 158 159 return __dl_bw_capacity(cpu_rq(i)->rd->span); 160 } 161 } 162 163 bool dl_bw_visited(int cpu, u64 cookie) 164 { 165 struct root_domain *rd = cpu_rq(cpu)->rd; 166 167 if (rd->visit_cookie == cookie) 168 return true; 169 170 rd->visit_cookie = cookie; 171 return false; 172 } 173 174 static inline 175 void __dl_update(struct dl_bw *dl_b, s64 bw) 176 { 177 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw); 178 int i; 179 180 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(), 181 "sched RCU must be held"); 182 for_each_cpu_and(i, rd->span, cpu_active_mask) { 183 struct rq *rq = cpu_rq(i); 184 185 rq->dl.extra_bw += bw; 186 } 187 } 188 189 static inline 190 void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus) 191 { 192 dl_b->total_bw -= tsk_bw; 193 __dl_update(dl_b, (s32)tsk_bw / cpus); 194 } 195 196 static inline 197 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus) 198 { 199 dl_b->total_bw += tsk_bw; 200 __dl_update(dl_b, -((s32)tsk_bw / cpus)); 201 } 202 203 static inline bool 204 __dl_overflow(struct dl_bw *dl_b, unsigned long cap, u64 old_bw, u64 new_bw) 205 { 206 return dl_b->bw != -1 && 207 cap_scale(dl_b->bw, cap) < dl_b->total_bw - old_bw + new_bw; 208 } 209 210 static inline 211 void __add_running_bw(u64 dl_bw, struct dl_rq *dl_rq) 212 { 213 u64 old = dl_rq->running_bw; 214 215 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq)); 216 dl_rq->running_bw += dl_bw; 217 WARN_ON_ONCE(dl_rq->running_bw < old); /* overflow */ 218 WARN_ON_ONCE(dl_rq->running_bw > dl_rq->this_bw); 219 /* kick cpufreq (see the comment in kernel/sched/sched.h). */ 220 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0); 221 } 222 223 static inline 224 void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq) 225 { 226 u64 old = dl_rq->running_bw; 227 228 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq)); 229 dl_rq->running_bw -= dl_bw; 230 WARN_ON_ONCE(dl_rq->running_bw > old); /* underflow */ 231 if (dl_rq->running_bw > old) 232 dl_rq->running_bw = 0; 233 /* kick cpufreq (see the comment in kernel/sched/sched.h). */ 234 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0); 235 } 236 237 static inline 238 void __add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq) 239 { 240 u64 old = dl_rq->this_bw; 241 242 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq)); 243 dl_rq->this_bw += dl_bw; 244 WARN_ON_ONCE(dl_rq->this_bw < old); /* overflow */ 245 } 246 247 static inline 248 void __sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq) 249 { 250 u64 old = dl_rq->this_bw; 251 252 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq)); 253 dl_rq->this_bw -= dl_bw; 254 WARN_ON_ONCE(dl_rq->this_bw > old); /* underflow */ 255 if (dl_rq->this_bw > old) 256 dl_rq->this_bw = 0; 257 WARN_ON_ONCE(dl_rq->running_bw > dl_rq->this_bw); 258 } 259 260 static inline 261 void add_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 262 { 263 if (!dl_entity_is_special(dl_se)) 264 __add_rq_bw(dl_se->dl_bw, dl_rq); 265 } 266 267 static inline 268 void sub_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 269 { 270 if (!dl_entity_is_special(dl_se)) 271 __sub_rq_bw(dl_se->dl_bw, dl_rq); 272 } 273 274 static inline 275 void add_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 276 { 277 if (!dl_entity_is_special(dl_se)) 278 __add_running_bw(dl_se->dl_bw, dl_rq); 279 } 280 281 static inline 282 void sub_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 283 { 284 if (!dl_entity_is_special(dl_se)) 285 __sub_running_bw(dl_se->dl_bw, dl_rq); 286 } 287 288 static void dl_rq_change_utilization(struct rq *rq, struct sched_dl_entity *dl_se, u64 new_bw) 289 { 290 if (dl_se->dl_non_contending) { 291 sub_running_bw(dl_se, &rq->dl); 292 dl_se->dl_non_contending = 0; 293 294 /* 295 * If the timer handler is currently running and the 296 * timer cannot be canceled, inactive_task_timer() 297 * will see that dl_not_contending is not set, and 298 * will not touch the rq's active utilization, 299 * so we are still safe. 300 */ 301 if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1) { 302 if (!dl_server(dl_se)) 303 put_task_struct(dl_task_of(dl_se)); 304 } 305 } 306 __sub_rq_bw(dl_se->dl_bw, &rq->dl); 307 __add_rq_bw(new_bw, &rq->dl); 308 } 309 310 static __always_inline 311 void cancel_dl_timer(struct sched_dl_entity *dl_se, struct hrtimer *timer) 312 { 313 /* 314 * If the timer callback was running (hrtimer_try_to_cancel == -1), 315 * it will eventually call put_task_struct(). 316 */ 317 if (hrtimer_try_to_cancel(timer) == 1 && !dl_server(dl_se)) 318 put_task_struct(dl_task_of(dl_se)); 319 } 320 321 static __always_inline 322 void cancel_replenish_timer(struct sched_dl_entity *dl_se) 323 { 324 cancel_dl_timer(dl_se, &dl_se->dl_timer); 325 } 326 327 static __always_inline 328 void cancel_inactive_timer(struct sched_dl_entity *dl_se) 329 { 330 cancel_dl_timer(dl_se, &dl_se->inactive_timer); 331 } 332 333 static void dl_change_utilization(struct task_struct *p, u64 new_bw) 334 { 335 WARN_ON_ONCE(p->dl.flags & SCHED_FLAG_SUGOV); 336 337 if (task_on_rq_queued(p)) 338 return; 339 340 dl_rq_change_utilization(task_rq(p), &p->dl, new_bw); 341 } 342 343 static void __dl_clear_params(struct sched_dl_entity *dl_se); 344 345 /* 346 * The utilization of a task cannot be immediately removed from 347 * the rq active utilization (running_bw) when the task blocks. 348 * Instead, we have to wait for the so called "0-lag time". 349 * 350 * If a task blocks before the "0-lag time", a timer (the inactive 351 * timer) is armed, and running_bw is decreased when the timer 352 * fires. 353 * 354 * If the task wakes up again before the inactive timer fires, 355 * the timer is canceled, whereas if the task wakes up after the 356 * inactive timer fired (and running_bw has been decreased) the 357 * task's utilization has to be added to running_bw again. 358 * A flag in the deadline scheduling entity (dl_non_contending) 359 * is used to avoid race conditions between the inactive timer handler 360 * and task wakeups. 361 * 362 * The following diagram shows how running_bw is updated. A task is 363 * "ACTIVE" when its utilization contributes to running_bw; an 364 * "ACTIVE contending" task is in the TASK_RUNNING state, while an 365 * "ACTIVE non contending" task is a blocked task for which the "0-lag time" 366 * has not passed yet. An "INACTIVE" task is a task for which the "0-lag" 367 * time already passed, which does not contribute to running_bw anymore. 368 * +------------------+ 369 * wakeup | ACTIVE | 370 * +------------------>+ contending | 371 * | add_running_bw | | 372 * | +----+------+------+ 373 * | | ^ 374 * | dequeue | | 375 * +--------+-------+ | | 376 * | | t >= 0-lag | | wakeup 377 * | INACTIVE |<---------------+ | 378 * | | sub_running_bw | | 379 * +--------+-------+ | | 380 * ^ | | 381 * | t < 0-lag | | 382 * | | | 383 * | V | 384 * | +----+------+------+ 385 * | sub_running_bw | ACTIVE | 386 * +-------------------+ | 387 * inactive timer | non contending | 388 * fired +------------------+ 389 * 390 * The task_non_contending() function is invoked when a task 391 * blocks, and checks if the 0-lag time already passed or 392 * not (in the first case, it directly updates running_bw; 393 * in the second case, it arms the inactive timer). 394 * 395 * The task_contending() function is invoked when a task wakes 396 * up, and checks if the task is still in the "ACTIVE non contending" 397 * state or not (in the second case, it updates running_bw). 398 */ 399 static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task) 400 { 401 struct hrtimer *timer = &dl_se->inactive_timer; 402 struct rq *rq = rq_of_dl_se(dl_se); 403 struct dl_rq *dl_rq = &rq->dl; 404 s64 zerolag_time; 405 406 /* 407 * If this is a non-deadline task that has been boosted, 408 * do nothing 409 */ 410 if (dl_se->dl_runtime == 0) 411 return; 412 413 if (dl_entity_is_special(dl_se)) 414 return; 415 416 WARN_ON(dl_se->dl_non_contending); 417 418 zerolag_time = dl_se->deadline - 419 div64_long((dl_se->runtime * dl_se->dl_period), 420 dl_se->dl_runtime); 421 422 /* 423 * Using relative times instead of the absolute "0-lag time" 424 * allows to simplify the code 425 */ 426 zerolag_time -= rq_clock(rq); 427 428 /* 429 * If the "0-lag time" already passed, decrease the active 430 * utilization now, instead of starting a timer 431 */ 432 if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) { 433 if (dl_server(dl_se)) { 434 sub_running_bw(dl_se, dl_rq); 435 } else { 436 struct task_struct *p = dl_task_of(dl_se); 437 438 if (dl_task) 439 sub_running_bw(dl_se, dl_rq); 440 441 if (!dl_task || READ_ONCE(p->__state) == TASK_DEAD) { 442 struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); 443 444 if (READ_ONCE(p->__state) == TASK_DEAD) 445 sub_rq_bw(dl_se, &rq->dl); 446 raw_spin_lock(&dl_b->lock); 447 __dl_sub(dl_b, dl_se->dl_bw, dl_bw_cpus(task_cpu(p))); 448 raw_spin_unlock(&dl_b->lock); 449 __dl_clear_params(dl_se); 450 } 451 } 452 453 return; 454 } 455 456 dl_se->dl_non_contending = 1; 457 if (!dl_server(dl_se)) 458 get_task_struct(dl_task_of(dl_se)); 459 460 hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD); 461 } 462 463 static void task_contending(struct sched_dl_entity *dl_se, int flags) 464 { 465 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 466 467 /* 468 * If this is a non-deadline task that has been boosted, 469 * do nothing 470 */ 471 if (dl_se->dl_runtime == 0) 472 return; 473 474 if (flags & ENQUEUE_MIGRATED) 475 add_rq_bw(dl_se, dl_rq); 476 477 if (dl_se->dl_non_contending) { 478 dl_se->dl_non_contending = 0; 479 /* 480 * If the timer handler is currently running and the 481 * timer cannot be canceled, inactive_task_timer() 482 * will see that dl_not_contending is not set, and 483 * will not touch the rq's active utilization, 484 * so we are still safe. 485 */ 486 cancel_inactive_timer(dl_se); 487 } else { 488 /* 489 * Since "dl_non_contending" is not set, the 490 * task's utilization has already been removed from 491 * active utilization (either when the task blocked, 492 * when the "inactive timer" fired). 493 * So, add it back. 494 */ 495 add_running_bw(dl_se, dl_rq); 496 } 497 } 498 499 static inline int is_leftmost(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 500 { 501 return rb_first_cached(&dl_rq->root) == &dl_se->rb_node; 502 } 503 504 static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq); 505 506 void init_dl_bw(struct dl_bw *dl_b) 507 { 508 raw_spin_lock_init(&dl_b->lock); 509 if (global_rt_runtime() == RUNTIME_INF) 510 dl_b->bw = -1; 511 else 512 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime()); 513 dl_b->total_bw = 0; 514 } 515 516 void init_dl_rq(struct dl_rq *dl_rq) 517 { 518 dl_rq->root = RB_ROOT_CACHED; 519 520 /* zero means no -deadline tasks */ 521 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0; 522 523 dl_rq->overloaded = 0; 524 dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED; 525 526 dl_rq->running_bw = 0; 527 dl_rq->this_bw = 0; 528 init_dl_rq_bw_ratio(dl_rq); 529 } 530 531 static inline int dl_overloaded(struct rq *rq) 532 { 533 return atomic_read(&rq->rd->dlo_count); 534 } 535 536 static inline void dl_set_overload(struct rq *rq) 537 { 538 if (!rq->online) 539 return; 540 541 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask); 542 /* 543 * Must be visible before the overload count is 544 * set (as in sched_rt.c). 545 * 546 * Matched by the barrier in pull_dl_task(). 547 */ 548 smp_wmb(); 549 atomic_inc(&rq->rd->dlo_count); 550 } 551 552 static inline void dl_clear_overload(struct rq *rq) 553 { 554 if (!rq->online) 555 return; 556 557 atomic_dec(&rq->rd->dlo_count); 558 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask); 559 } 560 561 #define __node_2_pdl(node) \ 562 rb_entry((node), struct task_struct, pushable_dl_tasks) 563 564 static inline bool __pushable_less(struct rb_node *a, const struct rb_node *b) 565 { 566 return dl_entity_preempt(&__node_2_pdl(a)->dl, &__node_2_pdl(b)->dl); 567 } 568 569 static inline int has_pushable_dl_tasks(struct rq *rq) 570 { 571 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root); 572 } 573 574 /* 575 * The list of pushable -deadline task is not a plist, like in 576 * sched_rt.c, it is an rb-tree with tasks ordered by deadline. 577 */ 578 static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) 579 { 580 struct rb_node *leftmost; 581 582 WARN_ON_ONCE(!RB_EMPTY_NODE(&p->pushable_dl_tasks)); 583 584 leftmost = rb_add_cached(&p->pushable_dl_tasks, 585 &rq->dl.pushable_dl_tasks_root, 586 __pushable_less); 587 if (leftmost) 588 rq->dl.earliest_dl.next = p->dl.deadline; 589 590 if (!rq->dl.overloaded) { 591 dl_set_overload(rq); 592 rq->dl.overloaded = 1; 593 } 594 } 595 596 static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p) 597 { 598 struct dl_rq *dl_rq = &rq->dl; 599 struct rb_root_cached *root = &dl_rq->pushable_dl_tasks_root; 600 struct rb_node *leftmost; 601 602 if (RB_EMPTY_NODE(&p->pushable_dl_tasks)) 603 return; 604 605 leftmost = rb_erase_cached(&p->pushable_dl_tasks, root); 606 if (leftmost) 607 dl_rq->earliest_dl.next = __node_2_pdl(leftmost)->dl.deadline; 608 609 RB_CLEAR_NODE(&p->pushable_dl_tasks); 610 611 if (!has_pushable_dl_tasks(rq) && rq->dl.overloaded) { 612 dl_clear_overload(rq); 613 rq->dl.overloaded = 0; 614 } 615 } 616 617 static int push_dl_task(struct rq *rq); 618 619 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev) 620 { 621 return rq->online && dl_task(prev); 622 } 623 624 static DEFINE_PER_CPU(struct balance_callback, dl_push_head); 625 static DEFINE_PER_CPU(struct balance_callback, dl_pull_head); 626 627 static void push_dl_tasks(struct rq *); 628 static void pull_dl_task(struct rq *); 629 630 static inline void deadline_queue_push_tasks(struct rq *rq) 631 { 632 if (!has_pushable_dl_tasks(rq)) 633 return; 634 635 queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks); 636 } 637 638 static inline void deadline_queue_pull_task(struct rq *rq) 639 { 640 queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task); 641 } 642 643 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq); 644 645 static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p) 646 { 647 struct rq *later_rq = NULL; 648 struct dl_bw *dl_b; 649 650 later_rq = find_lock_later_rq(p, rq); 651 if (!later_rq) { 652 int cpu; 653 654 /* 655 * If we cannot preempt any rq, fall back to pick any 656 * online CPU: 657 */ 658 cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr); 659 if (cpu >= nr_cpu_ids) { 660 /* 661 * Failed to find any suitable CPU. 662 * The task will never come back! 663 */ 664 WARN_ON_ONCE(dl_bandwidth_enabled()); 665 666 /* 667 * If admission control is disabled we 668 * try a little harder to let the task 669 * run. 670 */ 671 cpu = cpumask_any(cpu_active_mask); 672 } 673 later_rq = cpu_rq(cpu); 674 double_lock_balance(rq, later_rq); 675 } 676 677 if (p->dl.dl_non_contending || p->dl.dl_throttled) { 678 /* 679 * Inactive timer is armed (or callback is running, but 680 * waiting for us to release rq locks). In any case, when it 681 * will fire (or continue), it will see running_bw of this 682 * task migrated to later_rq (and correctly handle it). 683 */ 684 sub_running_bw(&p->dl, &rq->dl); 685 sub_rq_bw(&p->dl, &rq->dl); 686 687 add_rq_bw(&p->dl, &later_rq->dl); 688 add_running_bw(&p->dl, &later_rq->dl); 689 } else { 690 sub_rq_bw(&p->dl, &rq->dl); 691 add_rq_bw(&p->dl, &later_rq->dl); 692 } 693 694 /* 695 * And we finally need to fix up root_domain(s) bandwidth accounting, 696 * since p is still hanging out in the old (now moved to default) root 697 * domain. 698 */ 699 dl_b = &rq->rd->dl_bw; 700 raw_spin_lock(&dl_b->lock); 701 __dl_sub(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span)); 702 raw_spin_unlock(&dl_b->lock); 703 704 dl_b = &later_rq->rd->dl_bw; 705 raw_spin_lock(&dl_b->lock); 706 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(later_rq->rd->span)); 707 raw_spin_unlock(&dl_b->lock); 708 709 set_task_cpu(p, later_rq->cpu); 710 double_unlock_balance(later_rq, rq); 711 712 return later_rq; 713 } 714 715 static void 716 enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags); 717 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags); 718 static void dequeue_dl_entity(struct sched_dl_entity *dl_se, int flags); 719 static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p, int flags); 720 721 static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se, 722 struct rq *rq) 723 { 724 /* for non-boosted task, pi_of(dl_se) == dl_se */ 725 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline; 726 dl_se->runtime = pi_of(dl_se)->dl_runtime; 727 728 /* 729 * If it is a deferred reservation, and the server 730 * is not handling an starvation case, defer it. 731 */ 732 if (dl_se->dl_defer && !dl_se->dl_defer_running) { 733 dl_se->dl_throttled = 1; 734 dl_se->dl_defer_armed = 1; 735 } 736 } 737 738 /* 739 * We are being explicitly informed that a new instance is starting, 740 * and this means that: 741 * - the absolute deadline of the entity has to be placed at 742 * current time + relative deadline; 743 * - the runtime of the entity has to be set to the maximum value. 744 * 745 * The capability of specifying such event is useful whenever a -deadline 746 * entity wants to (try to!) synchronize its behaviour with the scheduler's 747 * one, and to (try to!) reconcile itself with its own scheduling 748 * parameters. 749 */ 750 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se) 751 { 752 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 753 struct rq *rq = rq_of_dl_rq(dl_rq); 754 755 update_rq_clock(rq); 756 757 WARN_ON(is_dl_boosted(dl_se)); 758 WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline)); 759 760 /* 761 * We are racing with the deadline timer. So, do nothing because 762 * the deadline timer handler will take care of properly recharging 763 * the runtime and postponing the deadline 764 */ 765 if (dl_se->dl_throttled) 766 return; 767 768 /* 769 * We use the regular wall clock time to set deadlines in the 770 * future; in fact, we must consider execution overheads (time 771 * spent on hardirq context, etc.). 772 */ 773 replenish_dl_new_period(dl_se, rq); 774 } 775 776 static int start_dl_timer(struct sched_dl_entity *dl_se); 777 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t); 778 779 /* 780 * Pure Earliest Deadline First (EDF) scheduling does not deal with the 781 * possibility of a entity lasting more than what it declared, and thus 782 * exhausting its runtime. 783 * 784 * Here we are interested in making runtime overrun possible, but we do 785 * not want a entity which is misbehaving to affect the scheduling of all 786 * other entities. 787 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS) 788 * is used, in order to confine each entity within its own bandwidth. 789 * 790 * This function deals exactly with that, and ensures that when the runtime 791 * of a entity is replenished, its deadline is also postponed. That ensures 792 * the overrunning entity can't interfere with other entity in the system and 793 * can't make them miss their deadlines. Reasons why this kind of overruns 794 * could happen are, typically, a entity voluntarily trying to overcome its 795 * runtime, or it just underestimated it during sched_setattr(). 796 */ 797 static void replenish_dl_entity(struct sched_dl_entity *dl_se) 798 { 799 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 800 struct rq *rq = rq_of_dl_rq(dl_rq); 801 802 WARN_ON_ONCE(pi_of(dl_se)->dl_runtime <= 0); 803 804 /* 805 * This could be the case for a !-dl task that is boosted. 806 * Just go with full inherited parameters. 807 * 808 * Or, it could be the case of a deferred reservation that 809 * was not able to consume its runtime in background and 810 * reached this point with current u > U. 811 * 812 * In both cases, set a new period. 813 */ 814 if (dl_se->dl_deadline == 0 || 815 (dl_se->dl_defer_armed && dl_entity_overflow(dl_se, rq_clock(rq)))) { 816 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline; 817 dl_se->runtime = pi_of(dl_se)->dl_runtime; 818 } 819 820 if (dl_se->dl_yielded && dl_se->runtime > 0) 821 dl_se->runtime = 0; 822 823 /* 824 * We keep moving the deadline away until we get some 825 * available runtime for the entity. This ensures correct 826 * handling of situations where the runtime overrun is 827 * arbitrary large. 828 */ 829 while (dl_se->runtime <= 0) { 830 dl_se->deadline += pi_of(dl_se)->dl_period; 831 dl_se->runtime += pi_of(dl_se)->dl_runtime; 832 } 833 834 /* 835 * At this point, the deadline really should be "in 836 * the future" with respect to rq->clock. If it's 837 * not, we are, for some reason, lagging too much! 838 * Anyway, after having warn userspace abut that, 839 * we still try to keep the things running by 840 * resetting the deadline and the budget of the 841 * entity. 842 */ 843 if (dl_time_before(dl_se->deadline, rq_clock(rq))) { 844 printk_deferred_once("sched: DL replenish lagged too much\n"); 845 replenish_dl_new_period(dl_se, rq); 846 } 847 848 if (dl_se->dl_yielded) 849 dl_se->dl_yielded = 0; 850 if (dl_se->dl_throttled) 851 dl_se->dl_throttled = 0; 852 853 /* 854 * If this is the replenishment of a deferred reservation, 855 * clear the flag and return. 856 */ 857 if (dl_se->dl_defer_armed) { 858 dl_se->dl_defer_armed = 0; 859 return; 860 } 861 862 /* 863 * A this point, if the deferred server is not armed, and the deadline 864 * is in the future, if it is not running already, throttle the server 865 * and arm the defer timer. 866 */ 867 if (dl_se->dl_defer && !dl_se->dl_defer_running && 868 dl_time_before(rq_clock(dl_se->rq), dl_se->deadline - dl_se->runtime)) { 869 if (!is_dl_boosted(dl_se)) { 870 871 /* 872 * Set dl_se->dl_defer_armed and dl_throttled variables to 873 * inform the start_dl_timer() that this is a deferred 874 * activation. 875 */ 876 dl_se->dl_defer_armed = 1; 877 dl_se->dl_throttled = 1; 878 if (!start_dl_timer(dl_se)) { 879 /* 880 * If for whatever reason (delays), a previous timer was 881 * queued but not serviced, cancel it and clean the 882 * deferrable server variables intended for start_dl_timer(). 883 */ 884 hrtimer_try_to_cancel(&dl_se->dl_timer); 885 dl_se->dl_defer_armed = 0; 886 dl_se->dl_throttled = 0; 887 } 888 } 889 } 890 } 891 892 /* 893 * Here we check if --at time t-- an entity (which is probably being 894 * [re]activated or, in general, enqueued) can use its remaining runtime 895 * and its current deadline _without_ exceeding the bandwidth it is 896 * assigned (function returns true if it can't). We are in fact applying 897 * one of the CBS rules: when a task wakes up, if the residual runtime 898 * over residual deadline fits within the allocated bandwidth, then we 899 * can keep the current (absolute) deadline and residual budget without 900 * disrupting the schedulability of the system. Otherwise, we should 901 * refill the runtime and set the deadline a period in the future, 902 * because keeping the current (absolute) deadline of the task would 903 * result in breaking guarantees promised to other tasks (refer to 904 * Documentation/scheduler/sched-deadline.rst for more information). 905 * 906 * This function returns true if: 907 * 908 * runtime / (deadline - t) > dl_runtime / dl_deadline , 909 * 910 * IOW we can't recycle current parameters. 911 * 912 * Notice that the bandwidth check is done against the deadline. For 913 * task with deadline equal to period this is the same of using 914 * dl_period instead of dl_deadline in the equation above. 915 */ 916 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t) 917 { 918 u64 left, right; 919 920 /* 921 * left and right are the two sides of the equation above, 922 * after a bit of shuffling to use multiplications instead 923 * of divisions. 924 * 925 * Note that none of the time values involved in the two 926 * multiplications are absolute: dl_deadline and dl_runtime 927 * are the relative deadline and the maximum runtime of each 928 * instance, runtime is the runtime left for the last instance 929 * and (deadline - t), since t is rq->clock, is the time left 930 * to the (absolute) deadline. Even if overflowing the u64 type 931 * is very unlikely to occur in both cases, here we scale down 932 * as we want to avoid that risk at all. Scaling down by 10 933 * means that we reduce granularity to 1us. We are fine with it, 934 * since this is only a true/false check and, anyway, thinking 935 * of anything below microseconds resolution is actually fiction 936 * (but still we want to give the user that illusion >;). 937 */ 938 left = (pi_of(dl_se)->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE); 939 right = ((dl_se->deadline - t) >> DL_SCALE) * 940 (pi_of(dl_se)->dl_runtime >> DL_SCALE); 941 942 return dl_time_before(right, left); 943 } 944 945 /* 946 * Revised wakeup rule [1]: For self-suspending tasks, rather then 947 * re-initializing task's runtime and deadline, the revised wakeup 948 * rule adjusts the task's runtime to avoid the task to overrun its 949 * density. 950 * 951 * Reasoning: a task may overrun the density if: 952 * runtime / (deadline - t) > dl_runtime / dl_deadline 953 * 954 * Therefore, runtime can be adjusted to: 955 * runtime = (dl_runtime / dl_deadline) * (deadline - t) 956 * 957 * In such way that runtime will be equal to the maximum density 958 * the task can use without breaking any rule. 959 * 960 * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant 961 * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24. 962 */ 963 static void 964 update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq) 965 { 966 u64 laxity = dl_se->deadline - rq_clock(rq); 967 968 /* 969 * If the task has deadline < period, and the deadline is in the past, 970 * it should already be throttled before this check. 971 * 972 * See update_dl_entity() comments for further details. 973 */ 974 WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq))); 975 976 dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT; 977 } 978 979 /* 980 * Regarding the deadline, a task with implicit deadline has a relative 981 * deadline == relative period. A task with constrained deadline has a 982 * relative deadline <= relative period. 983 * 984 * We support constrained deadline tasks. However, there are some restrictions 985 * applied only for tasks which do not have an implicit deadline. See 986 * update_dl_entity() to know more about such restrictions. 987 * 988 * The dl_is_implicit() returns true if the task has an implicit deadline. 989 */ 990 static inline bool dl_is_implicit(struct sched_dl_entity *dl_se) 991 { 992 return dl_se->dl_deadline == dl_se->dl_period; 993 } 994 995 /* 996 * When a deadline entity is placed in the runqueue, its runtime and deadline 997 * might need to be updated. This is done by a CBS wake up rule. There are two 998 * different rules: 1) the original CBS; and 2) the Revisited CBS. 999 * 1000 * When the task is starting a new period, the Original CBS is used. In this 1001 * case, the runtime is replenished and a new absolute deadline is set. 1002 * 1003 * When a task is queued before the begin of the next period, using the 1004 * remaining runtime and deadline could make the entity to overflow, see 1005 * dl_entity_overflow() to find more about runtime overflow. When such case 1006 * is detected, the runtime and deadline need to be updated. 1007 * 1008 * If the task has an implicit deadline, i.e., deadline == period, the Original 1009 * CBS is applied. The runtime is replenished and a new absolute deadline is 1010 * set, as in the previous cases. 1011 * 1012 * However, the Original CBS does not work properly for tasks with 1013 * deadline < period, which are said to have a constrained deadline. By 1014 * applying the Original CBS, a constrained deadline task would be able to run 1015 * runtime/deadline in a period. With deadline < period, the task would 1016 * overrun the runtime/period allowed bandwidth, breaking the admission test. 1017 * 1018 * In order to prevent this misbehave, the Revisited CBS is used for 1019 * constrained deadline tasks when a runtime overflow is detected. In the 1020 * Revisited CBS, rather than replenishing & setting a new absolute deadline, 1021 * the remaining runtime of the task is reduced to avoid runtime overflow. 1022 * Please refer to the comments update_dl_revised_wakeup() function to find 1023 * more about the Revised CBS rule. 1024 */ 1025 static void update_dl_entity(struct sched_dl_entity *dl_se) 1026 { 1027 struct rq *rq = rq_of_dl_se(dl_se); 1028 1029 if (dl_time_before(dl_se->deadline, rq_clock(rq)) || 1030 dl_entity_overflow(dl_se, rq_clock(rq))) { 1031 1032 if (unlikely(!dl_is_implicit(dl_se) && 1033 !dl_time_before(dl_se->deadline, rq_clock(rq)) && 1034 !is_dl_boosted(dl_se))) { 1035 update_dl_revised_wakeup(dl_se, rq); 1036 return; 1037 } 1038 1039 replenish_dl_new_period(dl_se, rq); 1040 } else if (dl_server(dl_se) && dl_se->dl_defer) { 1041 /* 1042 * The server can still use its previous deadline, so check if 1043 * it left the dl_defer_running state. 1044 */ 1045 if (!dl_se->dl_defer_running) { 1046 dl_se->dl_defer_armed = 1; 1047 dl_se->dl_throttled = 1; 1048 } 1049 } 1050 } 1051 1052 static inline u64 dl_next_period(struct sched_dl_entity *dl_se) 1053 { 1054 return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period; 1055 } 1056 1057 /* 1058 * If the entity depleted all its runtime, and if we want it to sleep 1059 * while waiting for some new execution time to become available, we 1060 * set the bandwidth replenishment timer to the replenishment instant 1061 * and try to activate it. 1062 * 1063 * Notice that it is important for the caller to know if the timer 1064 * actually started or not (i.e., the replenishment instant is in 1065 * the future or in the past). 1066 */ 1067 static int start_dl_timer(struct sched_dl_entity *dl_se) 1068 { 1069 struct hrtimer *timer = &dl_se->dl_timer; 1070 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 1071 struct rq *rq = rq_of_dl_rq(dl_rq); 1072 ktime_t now, act; 1073 s64 delta; 1074 1075 lockdep_assert_rq_held(rq); 1076 1077 /* 1078 * We want the timer to fire at the deadline, but considering 1079 * that it is actually coming from rq->clock and not from 1080 * hrtimer's time base reading. 1081 * 1082 * The deferred reservation will have its timer set to 1083 * (deadline - runtime). At that point, the CBS rule will decide 1084 * if the current deadline can be used, or if a replenishment is 1085 * required to avoid add too much pressure on the system 1086 * (current u > U). 1087 */ 1088 if (dl_se->dl_defer_armed) { 1089 WARN_ON_ONCE(!dl_se->dl_throttled); 1090 act = ns_to_ktime(dl_se->deadline - dl_se->runtime); 1091 } else { 1092 /* act = deadline - rel-deadline + period */ 1093 act = ns_to_ktime(dl_next_period(dl_se)); 1094 } 1095 1096 now = hrtimer_cb_get_time(timer); 1097 delta = ktime_to_ns(now) - rq_clock(rq); 1098 act = ktime_add_ns(act, delta); 1099 1100 /* 1101 * If the expiry time already passed, e.g., because the value 1102 * chosen as the deadline is too small, don't even try to 1103 * start the timer in the past! 1104 */ 1105 if (ktime_us_delta(act, now) < 0) 1106 return 0; 1107 1108 /* 1109 * !enqueued will guarantee another callback; even if one is already in 1110 * progress. This ensures a balanced {get,put}_task_struct(). 1111 * 1112 * The race against __run_timer() clearing the enqueued state is 1113 * harmless because we're holding task_rq()->lock, therefore the timer 1114 * expiring after we've done the check will wait on its task_rq_lock() 1115 * and observe our state. 1116 */ 1117 if (!hrtimer_is_queued(timer)) { 1118 if (!dl_server(dl_se)) 1119 get_task_struct(dl_task_of(dl_se)); 1120 hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD); 1121 } 1122 1123 return 1; 1124 } 1125 1126 static void __push_dl_task(struct rq *rq, struct rq_flags *rf) 1127 { 1128 /* 1129 * Queueing this task back might have overloaded rq, check if we need 1130 * to kick someone away. 1131 */ 1132 if (has_pushable_dl_tasks(rq)) { 1133 /* 1134 * Nothing relies on rq->lock after this, so its safe to drop 1135 * rq->lock. 1136 */ 1137 rq_unpin_lock(rq, rf); 1138 push_dl_task(rq); 1139 rq_repin_lock(rq, rf); 1140 } 1141 } 1142 1143 /* a defer timer will not be reset if the runtime consumed was < dl_server_min_res */ 1144 static const u64 dl_server_min_res = 1 * NSEC_PER_MSEC; 1145 1146 static enum hrtimer_restart dl_server_timer(struct hrtimer *timer, struct sched_dl_entity *dl_se) 1147 { 1148 struct rq *rq = rq_of_dl_se(dl_se); 1149 u64 fw; 1150 1151 scoped_guard (rq_lock, rq) { 1152 struct rq_flags *rf = &scope.rf; 1153 1154 if (!dl_se->dl_throttled || !dl_se->dl_runtime) 1155 return HRTIMER_NORESTART; 1156 1157 sched_clock_tick(); 1158 update_rq_clock(rq); 1159 1160 /* 1161 * Make sure current has propagated its pending runtime into 1162 * any relevant server through calling dl_server_update() and 1163 * friends. 1164 */ 1165 rq->donor->sched_class->update_curr(rq); 1166 1167 if (dl_se->dl_defer_idle) { 1168 dl_server_stop(dl_se); 1169 return HRTIMER_NORESTART; 1170 } 1171 1172 if (dl_se->dl_defer_armed) { 1173 /* 1174 * First check if the server could consume runtime in background. 1175 * If so, it is possible to push the defer timer for this amount 1176 * of time. The dl_server_min_res serves as a limit to avoid 1177 * forwarding the timer for a too small amount of time. 1178 */ 1179 if (dl_time_before(rq_clock(dl_se->rq), 1180 (dl_se->deadline - dl_se->runtime - dl_server_min_res))) { 1181 1182 /* reset the defer timer */ 1183 fw = dl_se->deadline - rq_clock(dl_se->rq) - dl_se->runtime; 1184 1185 hrtimer_forward_now(timer, ns_to_ktime(fw)); 1186 return HRTIMER_RESTART; 1187 } 1188 1189 dl_se->dl_defer_running = 1; 1190 } 1191 1192 enqueue_dl_entity(dl_se, ENQUEUE_REPLENISH); 1193 1194 if (!dl_task(dl_se->rq->curr) || dl_entity_preempt(dl_se, &dl_se->rq->curr->dl)) 1195 resched_curr(rq); 1196 1197 __push_dl_task(rq, rf); 1198 } 1199 1200 return HRTIMER_NORESTART; 1201 } 1202 1203 /* 1204 * This is the bandwidth enforcement timer callback. If here, we know 1205 * a task is not on its dl_rq, since the fact that the timer was running 1206 * means the task is throttled and needs a runtime replenishment. 1207 * 1208 * However, what we actually do depends on the fact the task is active, 1209 * (it is on its rq) or has been removed from there by a call to 1210 * dequeue_task_dl(). In the former case we must issue the runtime 1211 * replenishment and add the task back to the dl_rq; in the latter, we just 1212 * do nothing but clearing dl_throttled, so that runtime and deadline 1213 * updating (and the queueing back to dl_rq) will be done by the 1214 * next call to enqueue_task_dl(). 1215 */ 1216 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) 1217 { 1218 struct sched_dl_entity *dl_se = container_of(timer, 1219 struct sched_dl_entity, 1220 dl_timer); 1221 struct task_struct *p; 1222 struct rq_flags rf; 1223 struct rq *rq; 1224 1225 if (dl_server(dl_se)) 1226 return dl_server_timer(timer, dl_se); 1227 1228 p = dl_task_of(dl_se); 1229 rq = task_rq_lock(p, &rf); 1230 1231 /* 1232 * The task might have changed its scheduling policy to something 1233 * different than SCHED_DEADLINE (through switched_from_dl()). 1234 */ 1235 if (!dl_task(p)) 1236 goto unlock; 1237 1238 /* 1239 * The task might have been boosted by someone else and might be in the 1240 * boosting/deboosting path, its not throttled. 1241 */ 1242 if (is_dl_boosted(dl_se)) 1243 goto unlock; 1244 1245 /* 1246 * Spurious timer due to start_dl_timer() race; or we already received 1247 * a replenishment from rt_mutex_setprio(). 1248 */ 1249 if (!dl_se->dl_throttled) 1250 goto unlock; 1251 1252 sched_clock_tick(); 1253 update_rq_clock(rq); 1254 1255 /* 1256 * If the throttle happened during sched-out; like: 1257 * 1258 * schedule() 1259 * deactivate_task() 1260 * dequeue_task_dl() 1261 * update_curr_dl() 1262 * start_dl_timer() 1263 * __dequeue_task_dl() 1264 * prev->on_rq = 0; 1265 * 1266 * We can be both throttled and !queued. Replenish the counter 1267 * but do not enqueue -- wait for our wakeup to do that. 1268 */ 1269 if (!task_on_rq_queued(p)) { 1270 replenish_dl_entity(dl_se); 1271 goto unlock; 1272 } 1273 1274 if (unlikely(!rq->online)) { 1275 /* 1276 * If the runqueue is no longer available, migrate the 1277 * task elsewhere. This necessarily changes rq. 1278 */ 1279 lockdep_unpin_lock(__rq_lockp(rq), rf.cookie); 1280 rq = dl_task_offline_migration(rq, p); 1281 rf.cookie = lockdep_pin_lock(__rq_lockp(rq)); 1282 update_rq_clock(rq); 1283 1284 /* 1285 * Now that the task has been migrated to the new RQ and we 1286 * have that locked, proceed as normal and enqueue the task 1287 * there. 1288 */ 1289 } 1290 1291 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH); 1292 if (dl_task(rq->donor)) 1293 wakeup_preempt_dl(rq, p, 0); 1294 else 1295 resched_curr(rq); 1296 1297 __push_dl_task(rq, &rf); 1298 1299 unlock: 1300 task_rq_unlock(rq, p, &rf); 1301 1302 /* 1303 * This can free the task_struct, including this hrtimer, do not touch 1304 * anything related to that after this. 1305 */ 1306 put_task_struct(p); 1307 1308 return HRTIMER_NORESTART; 1309 } 1310 1311 static void init_dl_task_timer(struct sched_dl_entity *dl_se) 1312 { 1313 struct hrtimer *timer = &dl_se->dl_timer; 1314 1315 hrtimer_setup(timer, dl_task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 1316 } 1317 1318 /* 1319 * During the activation, CBS checks if it can reuse the current task's 1320 * runtime and period. If the deadline of the task is in the past, CBS 1321 * cannot use the runtime, and so it replenishes the task. This rule 1322 * works fine for implicit deadline tasks (deadline == period), and the 1323 * CBS was designed for implicit deadline tasks. However, a task with 1324 * constrained deadline (deadline < period) might be awakened after the 1325 * deadline, but before the next period. In this case, replenishing the 1326 * task would allow it to run for runtime / deadline. As in this case 1327 * deadline < period, CBS enables a task to run for more than the 1328 * runtime / period. In a very loaded system, this can cause a domino 1329 * effect, making other tasks miss their deadlines. 1330 * 1331 * To avoid this problem, in the activation of a constrained deadline 1332 * task after the deadline but before the next period, throttle the 1333 * task and set the replenishing timer to the begin of the next period, 1334 * unless it is boosted. 1335 */ 1336 static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se) 1337 { 1338 struct rq *rq = rq_of_dl_se(dl_se); 1339 1340 if (dl_time_before(dl_se->deadline, rq_clock(rq)) && 1341 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) { 1342 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(dl_se))) 1343 return; 1344 dl_se->dl_throttled = 1; 1345 if (dl_se->runtime > 0) 1346 dl_se->runtime = 0; 1347 } 1348 } 1349 1350 static 1351 int dl_runtime_exceeded(struct sched_dl_entity *dl_se) 1352 { 1353 return (dl_se->runtime <= 0); 1354 } 1355 1356 /* 1357 * This function implements the GRUB accounting rule. According to the 1358 * GRUB reclaiming algorithm, the runtime is not decreased as "dq = -dt", 1359 * but as "dq = -(max{u, (Umax - Uinact - Uextra)} / Umax) dt", 1360 * where u is the utilization of the task, Umax is the maximum reclaimable 1361 * utilization, Uinact is the (per-runqueue) inactive utilization, computed 1362 * as the difference between the "total runqueue utilization" and the 1363 * "runqueue active utilization", and Uextra is the (per runqueue) extra 1364 * reclaimable utilization. 1365 * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations multiplied 1366 * by 2^BW_SHIFT, the result has to be shifted right by BW_SHIFT. 1367 * Since rq->dl.bw_ratio contains 1 / Umax multiplied by 2^RATIO_SHIFT, dl_bw 1368 * is multiplied by rq->dl.bw_ratio and shifted right by RATIO_SHIFT. 1369 * Since delta is a 64 bit variable, to have an overflow its value should be 1370 * larger than 2^(64 - 20 - 8), which is more than 64 seconds. So, overflow is 1371 * not an issue here. 1372 */ 1373 static u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se) 1374 { 1375 u64 u_act; 1376 u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */ 1377 1378 /* 1379 * Instead of computing max{u, (u_max - u_inact - u_extra)}, we 1380 * compare u_inact + u_extra with u_max - u, because u_inact + u_extra 1381 * can be larger than u_max. So, u_max - u_inact - u_extra would be 1382 * negative leading to wrong results. 1383 */ 1384 if (u_inact + rq->dl.extra_bw > rq->dl.max_bw - dl_se->dl_bw) 1385 u_act = dl_se->dl_bw; 1386 else 1387 u_act = rq->dl.max_bw - u_inact - rq->dl.extra_bw; 1388 1389 u_act = (u_act * rq->dl.bw_ratio) >> RATIO_SHIFT; 1390 return (delta * u_act) >> BW_SHIFT; 1391 } 1392 1393 s64 dl_scaled_delta_exec(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec) 1394 { 1395 s64 scaled_delta_exec; 1396 1397 /* 1398 * For tasks that participate in GRUB, we implement GRUB-PA: the 1399 * spare reclaimed bandwidth is used to clock down frequency. 1400 * 1401 * For the others, we still need to scale reservation parameters 1402 * according to current frequency and CPU maximum capacity. 1403 */ 1404 if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) { 1405 scaled_delta_exec = grub_reclaim(delta_exec, rq, dl_se); 1406 } else { 1407 int cpu = cpu_of(rq); 1408 unsigned long scale_freq = arch_scale_freq_capacity(cpu); 1409 unsigned long scale_cpu = arch_scale_cpu_capacity(cpu); 1410 1411 scaled_delta_exec = cap_scale(delta_exec, scale_freq); 1412 scaled_delta_exec = cap_scale(scaled_delta_exec, scale_cpu); 1413 } 1414 1415 return scaled_delta_exec; 1416 } 1417 1418 static inline void 1419 update_stats_dequeue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se, int flags); 1420 1421 static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec) 1422 { 1423 bool idle = rq->curr == rq->idle; 1424 s64 scaled_delta_exec; 1425 1426 if (unlikely(delta_exec <= 0)) { 1427 if (unlikely(dl_se->dl_yielded)) 1428 goto throttle; 1429 return; 1430 } 1431 1432 if (dl_server(dl_se) && dl_se->dl_throttled && !dl_se->dl_defer) 1433 return; 1434 1435 if (dl_entity_is_special(dl_se)) 1436 return; 1437 1438 scaled_delta_exec = delta_exec; 1439 if (!dl_server(dl_se)) 1440 scaled_delta_exec = dl_scaled_delta_exec(rq, dl_se, delta_exec); 1441 1442 dl_se->runtime -= scaled_delta_exec; 1443 1444 if (dl_se->dl_defer_idle && !idle) 1445 dl_se->dl_defer_idle = 0; 1446 1447 /* 1448 * The fair server can consume its runtime while throttled (not queued/ 1449 * running as regular CFS). 1450 * 1451 * If the server consumes its entire runtime in this state. The server 1452 * is not required for the current period. Thus, reset the server by 1453 * starting a new period, pushing the activation. 1454 */ 1455 if (dl_se->dl_defer && dl_se->dl_throttled && dl_runtime_exceeded(dl_se)) { 1456 /* 1457 * Non-servers would never get time accounted while throttled. 1458 */ 1459 WARN_ON_ONCE(!dl_server(dl_se)); 1460 1461 /* 1462 * While the server is marked idle, do not push out the 1463 * activation further, instead wait for the period timer 1464 * to lapse and stop the server. 1465 */ 1466 if (dl_se->dl_defer_idle && idle) { 1467 /* 1468 * The timer is at the zero-laxity point, this means 1469 * dl_server_stop() / dl_server_start() can happen 1470 * while now < deadline. This means update_dl_entity() 1471 * will not replenish. Additionally start_dl_timer() 1472 * will be set for 'deadline - runtime'. Negative 1473 * runtime will not do. 1474 */ 1475 dl_se->runtime = 0; 1476 return; 1477 } 1478 1479 /* 1480 * If the server was previously activated - the starving condition 1481 * took place, it this point it went away because the fair scheduler 1482 * was able to get runtime in background. So return to the initial 1483 * state. 1484 */ 1485 dl_se->dl_defer_running = 0; 1486 1487 hrtimer_try_to_cancel(&dl_se->dl_timer); 1488 1489 replenish_dl_new_period(dl_se, dl_se->rq); 1490 1491 if (idle) 1492 dl_se->dl_defer_idle = 1; 1493 1494 /* 1495 * Not being able to start the timer seems problematic. If it could not 1496 * be started for whatever reason, we need to "unthrottle" the DL server 1497 * and queue right away. Otherwise nothing might queue it. That's similar 1498 * to what enqueue_dl_entity() does on start_dl_timer==0. For now, just warn. 1499 */ 1500 WARN_ON_ONCE(!start_dl_timer(dl_se)); 1501 1502 return; 1503 } 1504 1505 throttle: 1506 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) { 1507 dl_se->dl_throttled = 1; 1508 1509 /* If requested, inform the user about runtime overruns. */ 1510 if (dl_runtime_exceeded(dl_se) && 1511 (dl_se->flags & SCHED_FLAG_DL_OVERRUN)) 1512 dl_se->dl_overrun = 1; 1513 1514 dequeue_dl_entity(dl_se, 0); 1515 if (!dl_server(dl_se)) { 1516 update_stats_dequeue_dl(&rq->dl, dl_se, 0); 1517 dequeue_pushable_dl_task(rq, dl_task_of(dl_se)); 1518 } 1519 1520 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(dl_se))) { 1521 if (dl_server(dl_se)) { 1522 replenish_dl_new_period(dl_se, rq); 1523 start_dl_timer(dl_se); 1524 } else { 1525 enqueue_task_dl(rq, dl_task_of(dl_se), ENQUEUE_REPLENISH); 1526 } 1527 } 1528 1529 if (!is_leftmost(dl_se, &rq->dl)) 1530 resched_curr(rq); 1531 } 1532 1533 /* 1534 * The fair server (sole dl_server) does not account for real-time 1535 * workload because it is running fair work. 1536 */ 1537 if (dl_se == &rq->fair_server) 1538 return; 1539 1540 #ifdef CONFIG_RT_GROUP_SCHED 1541 /* 1542 * Because -- for now -- we share the rt bandwidth, we need to 1543 * account our runtime there too, otherwise actual rt tasks 1544 * would be able to exceed the shared quota. 1545 * 1546 * Account to the root rt group for now. 1547 * 1548 * The solution we're working towards is having the RT groups scheduled 1549 * using deadline servers -- however there's a few nasties to figure 1550 * out before that can happen. 1551 */ 1552 if (rt_bandwidth_enabled()) { 1553 struct rt_rq *rt_rq = &rq->rt; 1554 1555 raw_spin_lock(&rt_rq->rt_runtime_lock); 1556 /* 1557 * We'll let actual RT tasks worry about the overflow here, we 1558 * have our own CBS to keep us inline; only account when RT 1559 * bandwidth is relevant. 1560 */ 1561 if (sched_rt_bandwidth_account(rt_rq)) 1562 rt_rq->rt_time += delta_exec; 1563 raw_spin_unlock(&rt_rq->rt_runtime_lock); 1564 } 1565 #endif /* CONFIG_RT_GROUP_SCHED */ 1566 } 1567 1568 /* 1569 * In the non-defer mode, the idle time is not accounted, as the 1570 * server provides a guarantee. 1571 * 1572 * If the dl_server is in defer mode, the idle time is also considered 1573 * as time available for the fair server, avoiding a penalty for the 1574 * rt scheduler that did not consumed that time. 1575 */ 1576 void dl_server_update_idle(struct sched_dl_entity *dl_se, s64 delta_exec) 1577 { 1578 if (dl_se->dl_server_active && dl_se->dl_runtime && dl_se->dl_defer) 1579 update_curr_dl_se(dl_se->rq, dl_se, delta_exec); 1580 } 1581 1582 void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec) 1583 { 1584 /* 0 runtime = fair server disabled */ 1585 if (dl_se->dl_server_active && dl_se->dl_runtime) 1586 update_curr_dl_se(dl_se->rq, dl_se, delta_exec); 1587 } 1588 1589 /* 1590 * dl_server && dl_defer: 1591 * 1592 * 6 1593 * +--------------------+ 1594 * v | 1595 * +-------------+ 4 +-----------+ 5 +------------------+ 1596 * +-> | A:init | <--- | D:running | -----> | E:replenish-wait | 1597 * | +-------------+ +-----------+ +------------------+ 1598 * | | | 1 ^ ^ | 1599 * | | 1 +----------+ | 3 | 1600 * | v | | 1601 * | +--------------------------------+ 2 | 1602 * | | | ----+ | 1603 * | 8 | B:zero_laxity-wait | | | 1604 * | | | <---+ | 1605 * | +--------------------------------+ | 1606 * | | ^ ^ 2 | 1607 * | | 7 | 2 +--------------------+ 1608 * | v | 1609 * | +-------------+ | 1610 * +-- | C:idle-wait | -+ 1611 * +-------------+ 1612 * ^ 7 | 1613 * +---------+ 1614 * 1615 * 1616 * [A] - init 1617 * dl_server_active = 0 1618 * dl_throttled = 0 1619 * dl_defer_armed = 0 1620 * dl_defer_running = 0/1 1621 * dl_defer_idle = 0 1622 * 1623 * [B] - zero_laxity-wait 1624 * dl_server_active = 1 1625 * dl_throttled = 1 1626 * dl_defer_armed = 1 1627 * dl_defer_running = 0 1628 * dl_defer_idle = 0 1629 * 1630 * [C] - idle-wait 1631 * dl_server_active = 1 1632 * dl_throttled = 1 1633 * dl_defer_armed = 1 1634 * dl_defer_running = 0 1635 * dl_defer_idle = 1 1636 * 1637 * [D] - running 1638 * dl_server_active = 1 1639 * dl_throttled = 0 1640 * dl_defer_armed = 0 1641 * dl_defer_running = 1 1642 * dl_defer_idle = 0 1643 * 1644 * [E] - replenish-wait 1645 * dl_server_active = 1 1646 * dl_throttled = 1 1647 * dl_defer_armed = 0 1648 * dl_defer_running = 1 1649 * dl_defer_idle = 0 1650 * 1651 * 1652 * [1] A->B, A->D 1653 * dl_server_start() 1654 * dl_server_active = 1; 1655 * enqueue_dl_entity() 1656 * update_dl_entity(WAKEUP) 1657 * if (!dl_defer_running) 1658 * dl_defer_armed = 1; 1659 * dl_throttled = 1; 1660 * if (dl_throttled && start_dl_timer()) 1661 * return; // [B] 1662 * __enqueue_dl_entity(); 1663 * // [D] 1664 * 1665 * // deplete server runtime from client-class 1666 * [2] B->B, C->B, E->B 1667 * dl_server_update() 1668 * update_curr_dl_se() // idle = false 1669 * if (dl_defer_idle) 1670 * dl_defer_idle = 0; 1671 * if (dl_defer && dl_throttled && dl_runtime_exceeded()) 1672 * dl_defer_running = 0; 1673 * hrtimer_try_to_cancel(); // stop timer 1674 * replenish_dl_new_period() 1675 * // fwd period 1676 * dl_throttled = 1; 1677 * dl_defer_armed = 1; 1678 * start_dl_timer(); // restart timer 1679 * // [B] 1680 * 1681 * // timer actually fires means we have runtime 1682 * [3] B->D 1683 * dl_server_timer() 1684 * if (dl_defer_armed) 1685 * dl_defer_running = 1; 1686 * enqueue_dl_entity(REPLENISH) 1687 * replenish_dl_entity() 1688 * // fwd period 1689 * if (dl_throttled) 1690 * dl_throttled = 0; 1691 * if (dl_defer_armed) 1692 * dl_defer_armed = 0; 1693 * __enqueue_dl_entity(); 1694 * // [D] 1695 * 1696 * // schedule server 1697 * [4] D->A 1698 * pick_task_dl() 1699 * p = server_pick_task(); 1700 * if (!p) 1701 * dl_server_stop() 1702 * dequeue_dl_entity(); 1703 * hrtimer_try_to_cancel(); 1704 * dl_defer_armed = 0; 1705 * dl_throttled = 0; 1706 * dl_server_active = 0; 1707 * // [A] 1708 * return p; 1709 * 1710 * // server running 1711 * [5] D->E 1712 * update_curr_dl_se() 1713 * if (dl_runtime_exceeded()) 1714 * dl_throttled = 1; 1715 * dequeue_dl_entity(); 1716 * start_dl_timer(); 1717 * // [E] 1718 * 1719 * // server replenished 1720 * [6] E->D 1721 * dl_server_timer() 1722 * enqueue_dl_entity(REPLENISH) 1723 * replenish_dl_entity() 1724 * fwd-period 1725 * if (dl_throttled) 1726 * dl_throttled = 0; 1727 * __enqueue_dl_entity(); 1728 * // [D] 1729 * 1730 * // deplete server runtime from idle 1731 * [7] B->C, C->C 1732 * dl_server_update_idle() 1733 * update_curr_dl_se() // idle = true 1734 * if (dl_defer && dl_throttled && dl_runtime_exceeded()) 1735 * if (dl_defer_idle) 1736 * return; 1737 * dl_defer_running = 0; 1738 * hrtimer_try_to_cancel(); 1739 * replenish_dl_new_period() 1740 * // fwd period 1741 * dl_throttled = 1; 1742 * dl_defer_armed = 1; 1743 * dl_defer_idle = 1; 1744 * start_dl_timer(); // restart timer 1745 * // [C] 1746 * 1747 * // stop idle server 1748 * [8] C->A 1749 * dl_server_timer() 1750 * if (dl_defer_idle) 1751 * dl_server_stop(); 1752 * // [A] 1753 * 1754 * 1755 * digraph dl_server { 1756 * "A:init" -> "B:zero_laxity-wait" [label="1:dl_server_start"] 1757 * "A:init" -> "D:running" [label="1:dl_server_start"] 1758 * "B:zero_laxity-wait" -> "B:zero_laxity-wait" [label="2:dl_server_update"] 1759 * "B:zero_laxity-wait" -> "C:idle-wait" [label="7:dl_server_update_idle"] 1760 * "B:zero_laxity-wait" -> "D:running" [label="3:dl_server_timer"] 1761 * "C:idle-wait" -> "A:init" [label="8:dl_server_timer"] 1762 * "C:idle-wait" -> "B:zero_laxity-wait" [label="2:dl_server_update"] 1763 * "C:idle-wait" -> "C:idle-wait" [label="7:dl_server_update_idle"] 1764 * "D:running" -> "A:init" [label="4:pick_task_dl"] 1765 * "D:running" -> "E:replenish-wait" [label="5:update_curr_dl_se"] 1766 * "E:replenish-wait" -> "B:zero_laxity-wait" [label="2:dl_server_update"] 1767 * "E:replenish-wait" -> "D:running" [label="6:dl_server_timer"] 1768 * } 1769 * 1770 * 1771 * Notes: 1772 * 1773 * - When there are fair tasks running the most likely loop is [2]->[2]. 1774 * the dl_server never actually runs, the timer never fires. 1775 * 1776 * - When there is actual fair starvation; the timer fires and starts the 1777 * dl_server. This will then throttle and replenish like a normal DL 1778 * task. Notably it will not 'defer' again. 1779 * 1780 * - When idle it will push the actication forward once, and then wait 1781 * for the timer to hit or a non-idle update to restart things. 1782 */ 1783 void dl_server_start(struct sched_dl_entity *dl_se) 1784 { 1785 struct rq *rq = dl_se->rq; 1786 1787 if (!dl_server(dl_se) || dl_se->dl_server_active) 1788 return; 1789 1790 /* 1791 * Update the current task to 'now'. 1792 */ 1793 rq->donor->sched_class->update_curr(rq); 1794 1795 if (WARN_ON_ONCE(!cpu_online(cpu_of(rq)))) 1796 return; 1797 1798 dl_se->dl_server_active = 1; 1799 enqueue_dl_entity(dl_se, ENQUEUE_WAKEUP); 1800 if (!dl_task(dl_se->rq->curr) || dl_entity_preempt(dl_se, &rq->curr->dl)) 1801 resched_curr(dl_se->rq); 1802 } 1803 1804 void dl_server_stop(struct sched_dl_entity *dl_se) 1805 { 1806 if (!dl_server(dl_se) || !dl_server_active(dl_se)) 1807 return; 1808 1809 dequeue_dl_entity(dl_se, DEQUEUE_SLEEP); 1810 hrtimer_try_to_cancel(&dl_se->dl_timer); 1811 dl_se->dl_defer_armed = 0; 1812 dl_se->dl_throttled = 0; 1813 dl_se->dl_defer_idle = 0; 1814 dl_se->dl_server_active = 0; 1815 } 1816 1817 void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq, 1818 dl_server_pick_f pick_task) 1819 { 1820 dl_se->rq = rq; 1821 dl_se->server_pick_task = pick_task; 1822 } 1823 1824 void sched_init_dl_servers(void) 1825 { 1826 int cpu; 1827 struct rq *rq; 1828 struct sched_dl_entity *dl_se; 1829 1830 for_each_online_cpu(cpu) { 1831 u64 runtime = 50 * NSEC_PER_MSEC; 1832 u64 period = 1000 * NSEC_PER_MSEC; 1833 1834 rq = cpu_rq(cpu); 1835 1836 guard(rq_lock_irq)(rq); 1837 1838 dl_se = &rq->fair_server; 1839 1840 WARN_ON(dl_server(dl_se)); 1841 1842 dl_server_apply_params(dl_se, runtime, period, 1); 1843 1844 dl_se->dl_server = 1; 1845 dl_se->dl_defer = 1; 1846 setup_new_dl_entity(dl_se); 1847 } 1848 } 1849 1850 void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq) 1851 { 1852 u64 new_bw = dl_se->dl_bw; 1853 int cpu = cpu_of(rq); 1854 struct dl_bw *dl_b; 1855 1856 dl_b = dl_bw_of(cpu_of(rq)); 1857 guard(raw_spinlock)(&dl_b->lock); 1858 1859 if (!dl_bw_cpus(cpu)) 1860 return; 1861 1862 __dl_add(dl_b, new_bw, dl_bw_cpus(cpu)); 1863 } 1864 1865 int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 period, bool init) 1866 { 1867 u64 old_bw = init ? 0 : to_ratio(dl_se->dl_period, dl_se->dl_runtime); 1868 u64 new_bw = to_ratio(period, runtime); 1869 struct rq *rq = dl_se->rq; 1870 int cpu = cpu_of(rq); 1871 struct dl_bw *dl_b; 1872 unsigned long cap; 1873 int retval = 0; 1874 int cpus; 1875 1876 dl_b = dl_bw_of(cpu); 1877 guard(raw_spinlock)(&dl_b->lock); 1878 1879 cpus = dl_bw_cpus(cpu); 1880 cap = dl_bw_capacity(cpu); 1881 1882 if (__dl_overflow(dl_b, cap, old_bw, new_bw)) 1883 return -EBUSY; 1884 1885 if (init) { 1886 __add_rq_bw(new_bw, &rq->dl); 1887 __dl_add(dl_b, new_bw, cpus); 1888 } else { 1889 __dl_sub(dl_b, dl_se->dl_bw, cpus); 1890 __dl_add(dl_b, new_bw, cpus); 1891 1892 dl_rq_change_utilization(rq, dl_se, new_bw); 1893 } 1894 1895 dl_se->dl_runtime = runtime; 1896 dl_se->dl_deadline = period; 1897 dl_se->dl_period = period; 1898 1899 dl_se->runtime = 0; 1900 dl_se->deadline = 0; 1901 1902 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime); 1903 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime); 1904 1905 return retval; 1906 } 1907 1908 /* 1909 * Update the current task's runtime statistics (provided it is still 1910 * a -deadline task and has not been removed from the dl_rq). 1911 */ 1912 static void update_curr_dl(struct rq *rq) 1913 { 1914 struct task_struct *donor = rq->donor; 1915 struct sched_dl_entity *dl_se = &donor->dl; 1916 s64 delta_exec; 1917 1918 if (!dl_task(donor) || !on_dl_rq(dl_se)) 1919 return; 1920 1921 /* 1922 * Consumed budget is computed considering the time as 1923 * observed by schedulable tasks (excluding time spent 1924 * in hardirq context, etc.). Deadlines are instead 1925 * computed using hard walltime. This seems to be the more 1926 * natural solution, but the full ramifications of this 1927 * approach need further study. 1928 */ 1929 delta_exec = update_curr_common(rq); 1930 update_curr_dl_se(rq, dl_se, delta_exec); 1931 } 1932 1933 static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer) 1934 { 1935 struct sched_dl_entity *dl_se = container_of(timer, 1936 struct sched_dl_entity, 1937 inactive_timer); 1938 struct task_struct *p = NULL; 1939 struct rq_flags rf; 1940 struct rq *rq; 1941 1942 if (!dl_server(dl_se)) { 1943 p = dl_task_of(dl_se); 1944 rq = task_rq_lock(p, &rf); 1945 } else { 1946 rq = dl_se->rq; 1947 rq_lock(rq, &rf); 1948 } 1949 1950 sched_clock_tick(); 1951 update_rq_clock(rq); 1952 1953 if (dl_server(dl_se)) 1954 goto no_task; 1955 1956 if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD) { 1957 struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); 1958 1959 if (READ_ONCE(p->__state) == TASK_DEAD && dl_se->dl_non_contending) { 1960 sub_running_bw(&p->dl, dl_rq_of_se(&p->dl)); 1961 sub_rq_bw(&p->dl, dl_rq_of_se(&p->dl)); 1962 dl_se->dl_non_contending = 0; 1963 } 1964 1965 raw_spin_lock(&dl_b->lock); 1966 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); 1967 raw_spin_unlock(&dl_b->lock); 1968 __dl_clear_params(dl_se); 1969 1970 goto unlock; 1971 } 1972 1973 no_task: 1974 if (dl_se->dl_non_contending == 0) 1975 goto unlock; 1976 1977 sub_running_bw(dl_se, &rq->dl); 1978 dl_se->dl_non_contending = 0; 1979 unlock: 1980 1981 if (!dl_server(dl_se)) { 1982 task_rq_unlock(rq, p, &rf); 1983 put_task_struct(p); 1984 } else { 1985 rq_unlock(rq, &rf); 1986 } 1987 1988 return HRTIMER_NORESTART; 1989 } 1990 1991 static void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se) 1992 { 1993 struct hrtimer *timer = &dl_se->inactive_timer; 1994 1995 hrtimer_setup(timer, inactive_task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 1996 } 1997 1998 #define __node_2_dle(node) \ 1999 rb_entry((node), struct sched_dl_entity, rb_node) 2000 2001 static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) 2002 { 2003 struct rq *rq = rq_of_dl_rq(dl_rq); 2004 2005 if (dl_rq->earliest_dl.curr == 0 || 2006 dl_time_before(deadline, dl_rq->earliest_dl.curr)) { 2007 if (dl_rq->earliest_dl.curr == 0) 2008 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_HIGHER); 2009 dl_rq->earliest_dl.curr = deadline; 2010 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline); 2011 } 2012 } 2013 2014 static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) 2015 { 2016 struct rq *rq = rq_of_dl_rq(dl_rq); 2017 2018 /* 2019 * Since we may have removed our earliest (and/or next earliest) 2020 * task we must recompute them. 2021 */ 2022 if (!dl_rq->dl_nr_running) { 2023 dl_rq->earliest_dl.curr = 0; 2024 dl_rq->earliest_dl.next = 0; 2025 cpudl_clear(&rq->rd->cpudl, rq->cpu, rq->online); 2026 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr); 2027 } else { 2028 struct rb_node *leftmost = rb_first_cached(&dl_rq->root); 2029 struct sched_dl_entity *entry = __node_2_dle(leftmost); 2030 2031 dl_rq->earliest_dl.curr = entry->deadline; 2032 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline); 2033 } 2034 } 2035 2036 static inline 2037 void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 2038 { 2039 u64 deadline = dl_se->deadline; 2040 2041 dl_rq->dl_nr_running++; 2042 2043 if (!dl_server(dl_se)) 2044 add_nr_running(rq_of_dl_rq(dl_rq), 1); 2045 2046 inc_dl_deadline(dl_rq, deadline); 2047 } 2048 2049 static inline 2050 void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 2051 { 2052 WARN_ON(!dl_rq->dl_nr_running); 2053 dl_rq->dl_nr_running--; 2054 2055 if (!dl_server(dl_se)) 2056 sub_nr_running(rq_of_dl_rq(dl_rq), 1); 2057 2058 dec_dl_deadline(dl_rq, dl_se->deadline); 2059 } 2060 2061 static inline bool __dl_less(struct rb_node *a, const struct rb_node *b) 2062 { 2063 return dl_time_before(__node_2_dle(a)->deadline, __node_2_dle(b)->deadline); 2064 } 2065 2066 static __always_inline struct sched_statistics * 2067 __schedstats_from_dl_se(struct sched_dl_entity *dl_se) 2068 { 2069 if (!schedstat_enabled()) 2070 return NULL; 2071 2072 if (dl_server(dl_se)) 2073 return NULL; 2074 2075 return &dl_task_of(dl_se)->stats; 2076 } 2077 2078 static inline void 2079 update_stats_wait_start_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se) 2080 { 2081 struct sched_statistics *stats = __schedstats_from_dl_se(dl_se); 2082 if (stats) 2083 __update_stats_wait_start(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats); 2084 } 2085 2086 static inline void 2087 update_stats_wait_end_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se) 2088 { 2089 struct sched_statistics *stats = __schedstats_from_dl_se(dl_se); 2090 if (stats) 2091 __update_stats_wait_end(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats); 2092 } 2093 2094 static inline void 2095 update_stats_enqueue_sleeper_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se) 2096 { 2097 struct sched_statistics *stats = __schedstats_from_dl_se(dl_se); 2098 if (stats) 2099 __update_stats_enqueue_sleeper(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats); 2100 } 2101 2102 static inline void 2103 update_stats_enqueue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se, 2104 int flags) 2105 { 2106 if (!schedstat_enabled()) 2107 return; 2108 2109 if (flags & ENQUEUE_WAKEUP) 2110 update_stats_enqueue_sleeper_dl(dl_rq, dl_se); 2111 } 2112 2113 static inline void 2114 update_stats_dequeue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se, 2115 int flags) 2116 { 2117 struct task_struct *p = dl_task_of(dl_se); 2118 2119 if (!schedstat_enabled()) 2120 return; 2121 2122 if ((flags & DEQUEUE_SLEEP)) { 2123 unsigned int state; 2124 2125 state = READ_ONCE(p->__state); 2126 if (state & TASK_INTERRUPTIBLE) 2127 __schedstat_set(p->stats.sleep_start, 2128 rq_clock(rq_of_dl_rq(dl_rq))); 2129 2130 if (state & TASK_UNINTERRUPTIBLE) 2131 __schedstat_set(p->stats.block_start, 2132 rq_clock(rq_of_dl_rq(dl_rq))); 2133 } 2134 } 2135 2136 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se) 2137 { 2138 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 2139 2140 WARN_ON_ONCE(!RB_EMPTY_NODE(&dl_se->rb_node)); 2141 2142 rb_add_cached(&dl_se->rb_node, &dl_rq->root, __dl_less); 2143 2144 inc_dl_tasks(dl_se, dl_rq); 2145 } 2146 2147 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se) 2148 { 2149 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 2150 2151 if (RB_EMPTY_NODE(&dl_se->rb_node)) 2152 return; 2153 2154 rb_erase_cached(&dl_se->rb_node, &dl_rq->root); 2155 2156 RB_CLEAR_NODE(&dl_se->rb_node); 2157 2158 dec_dl_tasks(dl_se, dl_rq); 2159 } 2160 2161 static void 2162 enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags) 2163 { 2164 WARN_ON_ONCE(on_dl_rq(dl_se)); 2165 2166 update_stats_enqueue_dl(dl_rq_of_se(dl_se), dl_se, flags); 2167 2168 /* 2169 * Check if a constrained deadline task was activated 2170 * after the deadline but before the next period. 2171 * If that is the case, the task will be throttled and 2172 * the replenishment timer will be set to the next period. 2173 */ 2174 if (!dl_se->dl_throttled && !dl_is_implicit(dl_se)) 2175 dl_check_constrained_dl(dl_se); 2176 2177 if (flags & (ENQUEUE_RESTORE|ENQUEUE_MIGRATING)) { 2178 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 2179 2180 add_rq_bw(dl_se, dl_rq); 2181 add_running_bw(dl_se, dl_rq); 2182 } 2183 2184 /* 2185 * If p is throttled, we do not enqueue it. In fact, if it exhausted 2186 * its budget it needs a replenishment and, since it now is on 2187 * its rq, the bandwidth timer callback (which clearly has not 2188 * run yet) will take care of this. 2189 * However, the active utilization does not depend on the fact 2190 * that the task is on the runqueue or not (but depends on the 2191 * task's state - in GRUB parlance, "inactive" vs "active contending"). 2192 * In other words, even if a task is throttled its utilization must 2193 * be counted in the active utilization; hence, we need to call 2194 * add_running_bw(). 2195 */ 2196 if (!dl_se->dl_defer && dl_se->dl_throttled && !(flags & ENQUEUE_REPLENISH)) { 2197 if (flags & ENQUEUE_WAKEUP) 2198 task_contending(dl_se, flags); 2199 2200 return; 2201 } 2202 2203 /* 2204 * If this is a wakeup or a new instance, the scheduling 2205 * parameters of the task might need updating. Otherwise, 2206 * we want a replenishment of its runtime. 2207 */ 2208 if (flags & ENQUEUE_WAKEUP) { 2209 task_contending(dl_se, flags); 2210 update_dl_entity(dl_se); 2211 } else if (flags & ENQUEUE_REPLENISH) { 2212 replenish_dl_entity(dl_se); 2213 } else if ((flags & ENQUEUE_RESTORE) && 2214 !is_dl_boosted(dl_se) && 2215 dl_time_before(dl_se->deadline, rq_clock(rq_of_dl_se(dl_se)))) { 2216 setup_new_dl_entity(dl_se); 2217 } 2218 2219 /* 2220 * If the reservation is still throttled, e.g., it got replenished but is a 2221 * deferred task and still got to wait, don't enqueue. 2222 */ 2223 if (dl_se->dl_throttled && start_dl_timer(dl_se)) 2224 return; 2225 2226 /* 2227 * We're about to enqueue, make sure we're not ->dl_throttled! 2228 * In case the timer was not started, say because the defer time 2229 * has passed, mark as not throttled and mark unarmed. 2230 * Also cancel earlier timers, since letting those run is pointless. 2231 */ 2232 if (dl_se->dl_throttled) { 2233 hrtimer_try_to_cancel(&dl_se->dl_timer); 2234 dl_se->dl_defer_armed = 0; 2235 dl_se->dl_throttled = 0; 2236 } 2237 2238 __enqueue_dl_entity(dl_se); 2239 } 2240 2241 static void dequeue_dl_entity(struct sched_dl_entity *dl_se, int flags) 2242 { 2243 __dequeue_dl_entity(dl_se); 2244 2245 if (flags & (DEQUEUE_SAVE|DEQUEUE_MIGRATING)) { 2246 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 2247 2248 sub_running_bw(dl_se, dl_rq); 2249 sub_rq_bw(dl_se, dl_rq); 2250 } 2251 2252 /* 2253 * This check allows to start the inactive timer (or to immediately 2254 * decrease the active utilization, if needed) in two cases: 2255 * when the task blocks and when it is terminating 2256 * (p->state == TASK_DEAD). We can handle the two cases in the same 2257 * way, because from GRUB's point of view the same thing is happening 2258 * (the task moves from "active contending" to "active non contending" 2259 * or "inactive") 2260 */ 2261 if (flags & DEQUEUE_SLEEP) 2262 task_non_contending(dl_se, true); 2263 } 2264 2265 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags) 2266 { 2267 if (is_dl_boosted(&p->dl)) { 2268 /* 2269 * Because of delays in the detection of the overrun of a 2270 * thread's runtime, it might be the case that a thread 2271 * goes to sleep in a rt mutex with negative runtime. As 2272 * a consequence, the thread will be throttled. 2273 * 2274 * While waiting for the mutex, this thread can also be 2275 * boosted via PI, resulting in a thread that is throttled 2276 * and boosted at the same time. 2277 * 2278 * In this case, the boost overrides the throttle. 2279 */ 2280 if (p->dl.dl_throttled) { 2281 /* 2282 * The replenish timer needs to be canceled. No 2283 * problem if it fires concurrently: boosted threads 2284 * are ignored in dl_task_timer(). 2285 */ 2286 cancel_replenish_timer(&p->dl); 2287 p->dl.dl_throttled = 0; 2288 } 2289 } else if (!dl_prio(p->normal_prio)) { 2290 /* 2291 * Special case in which we have a !SCHED_DEADLINE task that is going 2292 * to be deboosted, but exceeds its runtime while doing so. No point in 2293 * replenishing it, as it's going to return back to its original 2294 * scheduling class after this. If it has been throttled, we need to 2295 * clear the flag, otherwise the task may wake up as throttled after 2296 * being boosted again with no means to replenish the runtime and clear 2297 * the throttle. 2298 */ 2299 p->dl.dl_throttled = 0; 2300 if (!(flags & ENQUEUE_REPLENISH)) 2301 printk_deferred_once("sched: DL de-boosted task PID %d: REPLENISH flag missing\n", 2302 task_pid_nr(p)); 2303 2304 return; 2305 } 2306 2307 check_schedstat_required(); 2308 update_stats_wait_start_dl(dl_rq_of_se(&p->dl), &p->dl); 2309 2310 if (p->on_rq == TASK_ON_RQ_MIGRATING) 2311 flags |= ENQUEUE_MIGRATING; 2312 2313 enqueue_dl_entity(&p->dl, flags); 2314 2315 if (dl_server(&p->dl)) 2316 return; 2317 2318 if (task_is_blocked(p)) 2319 return; 2320 2321 if (!task_current(rq, p) && !p->dl.dl_throttled && p->nr_cpus_allowed > 1) 2322 enqueue_pushable_dl_task(rq, p); 2323 } 2324 2325 static bool dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) 2326 { 2327 update_curr_dl(rq); 2328 2329 if (p->on_rq == TASK_ON_RQ_MIGRATING) 2330 flags |= DEQUEUE_MIGRATING; 2331 2332 dequeue_dl_entity(&p->dl, flags); 2333 if (!p->dl.dl_throttled && !dl_server(&p->dl)) 2334 dequeue_pushable_dl_task(rq, p); 2335 2336 return true; 2337 } 2338 2339 /* 2340 * Yield task semantic for -deadline tasks is: 2341 * 2342 * get off from the CPU until our next instance, with 2343 * a new runtime. This is of little use now, since we 2344 * don't have a bandwidth reclaiming mechanism. Anyway, 2345 * bandwidth reclaiming is planned for the future, and 2346 * yield_task_dl will indicate that some spare budget 2347 * is available for other task instances to use it. 2348 */ 2349 static void yield_task_dl(struct rq *rq) 2350 { 2351 /* 2352 * We make the task go to sleep until its current deadline by 2353 * forcing its runtime to zero. This way, update_curr_dl() stops 2354 * it and the bandwidth timer will wake it up and will give it 2355 * new scheduling parameters (thanks to dl_yielded=1). 2356 */ 2357 rq->donor->dl.dl_yielded = 1; 2358 2359 update_rq_clock(rq); 2360 update_curr_dl(rq); 2361 /* 2362 * Tell update_rq_clock() that we've just updated, 2363 * so we don't do microscopic update in schedule() 2364 * and double the fastpath cost. 2365 */ 2366 rq_clock_skip_update(rq); 2367 } 2368 2369 static inline bool dl_task_is_earliest_deadline(struct task_struct *p, 2370 struct rq *rq) 2371 { 2372 return (!rq->dl.dl_nr_running || 2373 dl_time_before(p->dl.deadline, 2374 rq->dl.earliest_dl.curr)); 2375 } 2376 2377 static int find_later_rq(struct task_struct *task); 2378 2379 static int 2380 select_task_rq_dl(struct task_struct *p, int cpu, int flags) 2381 { 2382 struct task_struct *curr, *donor; 2383 bool select_rq; 2384 struct rq *rq; 2385 2386 if (!(flags & WF_TTWU)) 2387 return cpu; 2388 2389 rq = cpu_rq(cpu); 2390 2391 rcu_read_lock(); 2392 curr = READ_ONCE(rq->curr); /* unlocked access */ 2393 donor = READ_ONCE(rq->donor); 2394 2395 /* 2396 * If we are dealing with a -deadline task, we must 2397 * decide where to wake it up. 2398 * If it has a later deadline and the current task 2399 * on this rq can't move (provided the waking task 2400 * can!) we prefer to send it somewhere else. On the 2401 * other hand, if it has a shorter deadline, we 2402 * try to make it stay here, it might be important. 2403 */ 2404 select_rq = unlikely(dl_task(donor)) && 2405 (curr->nr_cpus_allowed < 2 || 2406 !dl_entity_preempt(&p->dl, &donor->dl)) && 2407 p->nr_cpus_allowed > 1; 2408 2409 /* 2410 * Take the capacity of the CPU into account to 2411 * ensure it fits the requirement of the task. 2412 */ 2413 if (sched_asym_cpucap_active()) 2414 select_rq |= !dl_task_fits_capacity(p, cpu); 2415 2416 if (select_rq) { 2417 int target = find_later_rq(p); 2418 2419 if (target != -1 && 2420 dl_task_is_earliest_deadline(p, cpu_rq(target))) 2421 cpu = target; 2422 } 2423 rcu_read_unlock(); 2424 2425 return cpu; 2426 } 2427 2428 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused) 2429 { 2430 struct rq_flags rf; 2431 struct rq *rq; 2432 2433 if (READ_ONCE(p->__state) != TASK_WAKING) 2434 return; 2435 2436 rq = task_rq(p); 2437 /* 2438 * Since p->state == TASK_WAKING, set_task_cpu() has been called 2439 * from try_to_wake_up(). Hence, p->pi_lock is locked, but 2440 * rq->lock is not... So, lock it 2441 */ 2442 rq_lock(rq, &rf); 2443 if (p->dl.dl_non_contending) { 2444 update_rq_clock(rq); 2445 sub_running_bw(&p->dl, &rq->dl); 2446 p->dl.dl_non_contending = 0; 2447 /* 2448 * If the timer handler is currently running and the 2449 * timer cannot be canceled, inactive_task_timer() 2450 * will see that dl_not_contending is not set, and 2451 * will not touch the rq's active utilization, 2452 * so we are still safe. 2453 */ 2454 cancel_inactive_timer(&p->dl); 2455 } 2456 sub_rq_bw(&p->dl, &rq->dl); 2457 rq_unlock(rq, &rf); 2458 } 2459 2460 static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p) 2461 { 2462 /* 2463 * Current can't be migrated, useless to reschedule, 2464 * let's hope p can move out. 2465 */ 2466 if (rq->curr->nr_cpus_allowed == 1 || 2467 !cpudl_find(&rq->rd->cpudl, rq->donor, NULL)) 2468 return; 2469 2470 /* 2471 * p is migratable, so let's not schedule it and 2472 * see if it is pushed or pulled somewhere else. 2473 */ 2474 if (p->nr_cpus_allowed != 1 && 2475 cpudl_find(&rq->rd->cpudl, p, NULL)) 2476 return; 2477 2478 resched_curr(rq); 2479 } 2480 2481 static int balance_dl(struct rq *rq, struct task_struct *p, struct rq_flags *rf) 2482 { 2483 if (!on_dl_rq(&p->dl) && need_pull_dl_task(rq, p)) { 2484 /* 2485 * This is OK, because current is on_cpu, which avoids it being 2486 * picked for load-balance and preemption/IRQs are still 2487 * disabled avoiding further scheduler activity on it and we've 2488 * not yet started the picking loop. 2489 */ 2490 rq_unpin_lock(rq, rf); 2491 pull_dl_task(rq); 2492 rq_repin_lock(rq, rf); 2493 } 2494 2495 return sched_stop_runnable(rq) || sched_dl_runnable(rq); 2496 } 2497 2498 /* 2499 * Only called when both the current and waking task are -deadline 2500 * tasks. 2501 */ 2502 static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p, 2503 int flags) 2504 { 2505 if (dl_entity_preempt(&p->dl, &rq->donor->dl)) { 2506 resched_curr(rq); 2507 return; 2508 } 2509 2510 /* 2511 * In the unlikely case current and p have the same deadline 2512 * let us try to decide what's the best thing to do... 2513 */ 2514 if ((p->dl.deadline == rq->donor->dl.deadline) && 2515 !test_tsk_need_resched(rq->curr)) 2516 check_preempt_equal_dl(rq, p); 2517 } 2518 2519 #ifdef CONFIG_SCHED_HRTICK 2520 static void start_hrtick_dl(struct rq *rq, struct sched_dl_entity *dl_se) 2521 { 2522 hrtick_start(rq, dl_se->runtime); 2523 } 2524 #else /* !CONFIG_SCHED_HRTICK: */ 2525 static void start_hrtick_dl(struct rq *rq, struct sched_dl_entity *dl_se) 2526 { 2527 } 2528 #endif /* !CONFIG_SCHED_HRTICK */ 2529 2530 static void set_next_task_dl(struct rq *rq, struct task_struct *p, bool first) 2531 { 2532 struct sched_dl_entity *dl_se = &p->dl; 2533 struct dl_rq *dl_rq = &rq->dl; 2534 2535 p->se.exec_start = rq_clock_task(rq); 2536 if (on_dl_rq(&p->dl)) 2537 update_stats_wait_end_dl(dl_rq, dl_se); 2538 2539 /* You can't push away the running task */ 2540 dequeue_pushable_dl_task(rq, p); 2541 2542 if (!first) 2543 return; 2544 2545 if (rq->donor->sched_class != &dl_sched_class) 2546 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0); 2547 2548 deadline_queue_push_tasks(rq); 2549 2550 if (hrtick_enabled_dl(rq)) 2551 start_hrtick_dl(rq, &p->dl); 2552 } 2553 2554 static struct sched_dl_entity *pick_next_dl_entity(struct dl_rq *dl_rq) 2555 { 2556 struct rb_node *left = rb_first_cached(&dl_rq->root); 2557 2558 if (!left) 2559 return NULL; 2560 2561 return __node_2_dle(left); 2562 } 2563 2564 /* 2565 * __pick_next_task_dl - Helper to pick the next -deadline task to run. 2566 * @rq: The runqueue to pick the next task from. 2567 */ 2568 static struct task_struct *__pick_task_dl(struct rq *rq, struct rq_flags *rf) 2569 { 2570 struct sched_dl_entity *dl_se; 2571 struct dl_rq *dl_rq = &rq->dl; 2572 struct task_struct *p; 2573 2574 again: 2575 if (!sched_dl_runnable(rq)) 2576 return NULL; 2577 2578 dl_se = pick_next_dl_entity(dl_rq); 2579 WARN_ON_ONCE(!dl_se); 2580 2581 if (dl_server(dl_se)) { 2582 p = dl_se->server_pick_task(dl_se, rf); 2583 if (!p) { 2584 dl_server_stop(dl_se); 2585 goto again; 2586 } 2587 rq->dl_server = dl_se; 2588 } else { 2589 p = dl_task_of(dl_se); 2590 } 2591 2592 return p; 2593 } 2594 2595 static struct task_struct *pick_task_dl(struct rq *rq, struct rq_flags *rf) 2596 { 2597 return __pick_task_dl(rq, rf); 2598 } 2599 2600 static void put_prev_task_dl(struct rq *rq, struct task_struct *p, struct task_struct *next) 2601 { 2602 struct sched_dl_entity *dl_se = &p->dl; 2603 struct dl_rq *dl_rq = &rq->dl; 2604 2605 if (on_dl_rq(&p->dl)) 2606 update_stats_wait_start_dl(dl_rq, dl_se); 2607 2608 update_curr_dl(rq); 2609 2610 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1); 2611 2612 if (task_is_blocked(p)) 2613 return; 2614 2615 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1) 2616 enqueue_pushable_dl_task(rq, p); 2617 } 2618 2619 /* 2620 * scheduler tick hitting a task of our scheduling class. 2621 * 2622 * NOTE: This function can be called remotely by the tick offload that 2623 * goes along full dynticks. Therefore no local assumption can be made 2624 * and everything must be accessed through the @rq and @curr passed in 2625 * parameters. 2626 */ 2627 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued) 2628 { 2629 update_curr_dl(rq); 2630 2631 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1); 2632 /* 2633 * Even when we have runtime, update_curr_dl() might have resulted in us 2634 * not being the leftmost task anymore. In that case NEED_RESCHED will 2635 * be set and schedule() will start a new hrtick for the next task. 2636 */ 2637 if (hrtick_enabled_dl(rq) && queued && p->dl.runtime > 0 && 2638 is_leftmost(&p->dl, &rq->dl)) 2639 start_hrtick_dl(rq, &p->dl); 2640 } 2641 2642 static void task_fork_dl(struct task_struct *p) 2643 { 2644 /* 2645 * SCHED_DEADLINE tasks cannot fork and this is achieved through 2646 * sched_fork() 2647 */ 2648 } 2649 2650 /* Only try algorithms three times */ 2651 #define DL_MAX_TRIES 3 2652 2653 /* 2654 * Return the earliest pushable rq's task, which is suitable to be executed 2655 * on the CPU, NULL otherwise: 2656 */ 2657 static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu) 2658 { 2659 struct task_struct *p = NULL; 2660 struct rb_node *next_node; 2661 2662 if (!has_pushable_dl_tasks(rq)) 2663 return NULL; 2664 2665 next_node = rb_first_cached(&rq->dl.pushable_dl_tasks_root); 2666 while (next_node) { 2667 p = __node_2_pdl(next_node); 2668 2669 if (task_is_pushable(rq, p, cpu)) 2670 return p; 2671 2672 next_node = rb_next(next_node); 2673 } 2674 2675 return NULL; 2676 } 2677 2678 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl); 2679 2680 static int find_later_rq(struct task_struct *task) 2681 { 2682 struct sched_domain *sd; 2683 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl); 2684 int this_cpu = smp_processor_id(); 2685 int cpu = task_cpu(task); 2686 2687 /* Make sure the mask is initialized first */ 2688 if (unlikely(!later_mask)) 2689 return -1; 2690 2691 if (task->nr_cpus_allowed == 1) 2692 return -1; 2693 2694 /* 2695 * We have to consider system topology and task affinity 2696 * first, then we can look for a suitable CPU. 2697 */ 2698 if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask)) 2699 return -1; 2700 2701 /* 2702 * If we are here, some targets have been found, including 2703 * the most suitable which is, among the runqueues where the 2704 * current tasks have later deadlines than the task's one, the 2705 * rq with the latest possible one. 2706 * 2707 * Now we check how well this matches with task's 2708 * affinity and system topology. 2709 * 2710 * The last CPU where the task run is our first 2711 * guess, since it is most likely cache-hot there. 2712 */ 2713 if (cpumask_test_cpu(cpu, later_mask)) 2714 return cpu; 2715 /* 2716 * Check if this_cpu is to be skipped (i.e., it is 2717 * not in the mask) or not. 2718 */ 2719 if (!cpumask_test_cpu(this_cpu, later_mask)) 2720 this_cpu = -1; 2721 2722 rcu_read_lock(); 2723 for_each_domain(cpu, sd) { 2724 if (sd->flags & SD_WAKE_AFFINE) { 2725 int best_cpu; 2726 2727 /* 2728 * If possible, preempting this_cpu is 2729 * cheaper than migrating. 2730 */ 2731 if (this_cpu != -1 && 2732 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { 2733 rcu_read_unlock(); 2734 return this_cpu; 2735 } 2736 2737 best_cpu = cpumask_any_and_distribute(later_mask, 2738 sched_domain_span(sd)); 2739 /* 2740 * Last chance: if a CPU being in both later_mask 2741 * and current sd span is valid, that becomes our 2742 * choice. Of course, the latest possible CPU is 2743 * already under consideration through later_mask. 2744 */ 2745 if (best_cpu < nr_cpu_ids) { 2746 rcu_read_unlock(); 2747 return best_cpu; 2748 } 2749 } 2750 } 2751 rcu_read_unlock(); 2752 2753 /* 2754 * At this point, all our guesses failed, we just return 2755 * 'something', and let the caller sort the things out. 2756 */ 2757 if (this_cpu != -1) 2758 return this_cpu; 2759 2760 cpu = cpumask_any_distribute(later_mask); 2761 if (cpu < nr_cpu_ids) 2762 return cpu; 2763 2764 return -1; 2765 } 2766 2767 static struct task_struct *pick_next_pushable_dl_task(struct rq *rq) 2768 { 2769 struct task_struct *p; 2770 2771 if (!has_pushable_dl_tasks(rq)) 2772 return NULL; 2773 2774 p = __node_2_pdl(rb_first_cached(&rq->dl.pushable_dl_tasks_root)); 2775 2776 WARN_ON_ONCE(rq->cpu != task_cpu(p)); 2777 WARN_ON_ONCE(task_current(rq, p)); 2778 WARN_ON_ONCE(p->nr_cpus_allowed <= 1); 2779 2780 WARN_ON_ONCE(!task_on_rq_queued(p)); 2781 WARN_ON_ONCE(!dl_task(p)); 2782 2783 return p; 2784 } 2785 2786 /* Locks the rq it finds */ 2787 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq) 2788 { 2789 struct rq *later_rq = NULL; 2790 int tries; 2791 int cpu; 2792 2793 for (tries = 0; tries < DL_MAX_TRIES; tries++) { 2794 cpu = find_later_rq(task); 2795 2796 if ((cpu == -1) || (cpu == rq->cpu)) 2797 break; 2798 2799 later_rq = cpu_rq(cpu); 2800 2801 if (!dl_task_is_earliest_deadline(task, later_rq)) { 2802 /* 2803 * Target rq has tasks of equal or earlier deadline, 2804 * retrying does not release any lock and is unlikely 2805 * to yield a different result. 2806 */ 2807 later_rq = NULL; 2808 break; 2809 } 2810 2811 /* Retry if something changed. */ 2812 if (double_lock_balance(rq, later_rq)) { 2813 /* 2814 * double_lock_balance had to release rq->lock, in the 2815 * meantime, task may no longer be fit to be migrated. 2816 * Check the following to ensure that the task is 2817 * still suitable for migration: 2818 * 1. It is possible the task was scheduled, 2819 * migrate_disabled was set and then got preempted, 2820 * so we must check the task migration disable 2821 * flag. 2822 * 2. The CPU picked is in the task's affinity. 2823 * 3. For throttled task (dl_task_offline_migration), 2824 * check the following: 2825 * - the task is not on the rq anymore (it was 2826 * migrated) 2827 * - the task is not on CPU anymore 2828 * - the task is still a dl task 2829 * - the task is not queued on the rq anymore 2830 * 4. For the non-throttled task (push_dl_task), the 2831 * check to ensure that this task is still at the 2832 * head of the pushable tasks list is enough. 2833 */ 2834 if (unlikely(is_migration_disabled(task) || 2835 !cpumask_test_cpu(later_rq->cpu, &task->cpus_mask) || 2836 (task->dl.dl_throttled && 2837 (task_rq(task) != rq || 2838 task_on_cpu(rq, task) || 2839 !dl_task(task) || 2840 !task_on_rq_queued(task))) || 2841 (!task->dl.dl_throttled && 2842 task != pick_next_pushable_dl_task(rq)))) { 2843 2844 double_unlock_balance(rq, later_rq); 2845 later_rq = NULL; 2846 break; 2847 } 2848 } 2849 2850 /* 2851 * If the rq we found has no -deadline task, or 2852 * its earliest one has a later deadline than our 2853 * task, the rq is a good one. 2854 */ 2855 if (dl_task_is_earliest_deadline(task, later_rq)) 2856 break; 2857 2858 /* Otherwise we try again. */ 2859 double_unlock_balance(rq, later_rq); 2860 later_rq = NULL; 2861 } 2862 2863 return later_rq; 2864 } 2865 2866 /* 2867 * See if the non running -deadline tasks on this rq 2868 * can be sent to some other CPU where they can preempt 2869 * and start executing. 2870 */ 2871 static int push_dl_task(struct rq *rq) 2872 { 2873 struct task_struct *next_task; 2874 struct rq *later_rq; 2875 int ret = 0; 2876 2877 next_task = pick_next_pushable_dl_task(rq); 2878 if (!next_task) 2879 return 0; 2880 2881 retry: 2882 /* 2883 * If next_task preempts rq->curr, and rq->curr 2884 * can move away, it makes sense to just reschedule 2885 * without going further in pushing next_task. 2886 */ 2887 if (dl_task(rq->donor) && 2888 dl_time_before(next_task->dl.deadline, rq->donor->dl.deadline) && 2889 rq->curr->nr_cpus_allowed > 1) { 2890 resched_curr(rq); 2891 return 0; 2892 } 2893 2894 if (is_migration_disabled(next_task)) 2895 return 0; 2896 2897 if (WARN_ON(next_task == rq->curr)) 2898 return 0; 2899 2900 /* We might release rq lock */ 2901 get_task_struct(next_task); 2902 2903 /* Will lock the rq it'll find */ 2904 later_rq = find_lock_later_rq(next_task, rq); 2905 if (!later_rq) { 2906 struct task_struct *task; 2907 2908 /* 2909 * We must check all this again, since 2910 * find_lock_later_rq releases rq->lock and it is 2911 * then possible that next_task has migrated. 2912 */ 2913 task = pick_next_pushable_dl_task(rq); 2914 if (task == next_task) { 2915 /* 2916 * The task is still there. We don't try 2917 * again, some other CPU will pull it when ready. 2918 */ 2919 goto out; 2920 } 2921 2922 if (!task) 2923 /* No more tasks */ 2924 goto out; 2925 2926 put_task_struct(next_task); 2927 next_task = task; 2928 goto retry; 2929 } 2930 2931 move_queued_task_locked(rq, later_rq, next_task); 2932 ret = 1; 2933 2934 resched_curr(later_rq); 2935 2936 double_unlock_balance(rq, later_rq); 2937 2938 out: 2939 put_task_struct(next_task); 2940 2941 return ret; 2942 } 2943 2944 static void push_dl_tasks(struct rq *rq) 2945 { 2946 /* push_dl_task() will return true if it moved a -deadline task */ 2947 while (push_dl_task(rq)) 2948 ; 2949 } 2950 2951 static void pull_dl_task(struct rq *this_rq) 2952 { 2953 int this_cpu = this_rq->cpu, cpu; 2954 struct task_struct *p, *push_task; 2955 bool resched = false; 2956 struct rq *src_rq; 2957 u64 dmin = LONG_MAX; 2958 2959 if (likely(!dl_overloaded(this_rq))) 2960 return; 2961 2962 /* 2963 * Match the barrier from dl_set_overloaded; this guarantees that if we 2964 * see overloaded we must also see the dlo_mask bit. 2965 */ 2966 smp_rmb(); 2967 2968 for_each_cpu(cpu, this_rq->rd->dlo_mask) { 2969 if (this_cpu == cpu) 2970 continue; 2971 2972 src_rq = cpu_rq(cpu); 2973 2974 /* 2975 * It looks racy, and it is! However, as in sched_rt.c, 2976 * we are fine with this. 2977 */ 2978 if (this_rq->dl.dl_nr_running && 2979 dl_time_before(this_rq->dl.earliest_dl.curr, 2980 src_rq->dl.earliest_dl.next)) 2981 continue; 2982 2983 /* Might drop this_rq->lock */ 2984 push_task = NULL; 2985 double_lock_balance(this_rq, src_rq); 2986 2987 /* 2988 * If there are no more pullable tasks on the 2989 * rq, we're done with it. 2990 */ 2991 if (src_rq->dl.dl_nr_running <= 1) 2992 goto skip; 2993 2994 p = pick_earliest_pushable_dl_task(src_rq, this_cpu); 2995 2996 /* 2997 * We found a task to be pulled if: 2998 * - it preempts our current (if there's one), 2999 * - it will preempt the last one we pulled (if any). 3000 */ 3001 if (p && dl_time_before(p->dl.deadline, dmin) && 3002 dl_task_is_earliest_deadline(p, this_rq)) { 3003 WARN_ON(p == src_rq->curr); 3004 WARN_ON(!task_on_rq_queued(p)); 3005 3006 /* 3007 * Then we pull iff p has actually an earlier 3008 * deadline than the current task of its runqueue. 3009 */ 3010 if (dl_time_before(p->dl.deadline, 3011 src_rq->donor->dl.deadline)) 3012 goto skip; 3013 3014 if (is_migration_disabled(p)) { 3015 push_task = get_push_task(src_rq); 3016 } else { 3017 move_queued_task_locked(src_rq, this_rq, p); 3018 dmin = p->dl.deadline; 3019 resched = true; 3020 } 3021 3022 /* Is there any other task even earlier? */ 3023 } 3024 skip: 3025 double_unlock_balance(this_rq, src_rq); 3026 3027 if (push_task) { 3028 preempt_disable(); 3029 raw_spin_rq_unlock(this_rq); 3030 stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop, 3031 push_task, &src_rq->push_work); 3032 preempt_enable(); 3033 raw_spin_rq_lock(this_rq); 3034 } 3035 } 3036 3037 if (resched) 3038 resched_curr(this_rq); 3039 } 3040 3041 /* 3042 * Since the task is not running and a reschedule is not going to happen 3043 * anytime soon on its runqueue, we try pushing it away now. 3044 */ 3045 static void task_woken_dl(struct rq *rq, struct task_struct *p) 3046 { 3047 if (!task_on_cpu(rq, p) && 3048 !test_tsk_need_resched(rq->curr) && 3049 p->nr_cpus_allowed > 1 && 3050 dl_task(rq->donor) && 3051 (rq->curr->nr_cpus_allowed < 2 || 3052 !dl_entity_preempt(&p->dl, &rq->donor->dl))) { 3053 push_dl_tasks(rq); 3054 } 3055 } 3056 3057 static void set_cpus_allowed_dl(struct task_struct *p, 3058 struct affinity_context *ctx) 3059 { 3060 struct root_domain *src_rd; 3061 struct rq *rq; 3062 3063 WARN_ON_ONCE(!dl_task(p)); 3064 3065 rq = task_rq(p); 3066 src_rd = rq->rd; 3067 /* 3068 * Migrating a SCHED_DEADLINE task between exclusive 3069 * cpusets (different root_domains) entails a bandwidth 3070 * update. We already made space for us in the destination 3071 * domain (see cpuset_can_attach()). 3072 */ 3073 if (!cpumask_intersects(src_rd->span, ctx->new_mask)) { 3074 struct dl_bw *src_dl_b; 3075 3076 src_dl_b = dl_bw_of(cpu_of(rq)); 3077 /* 3078 * We now free resources of the root_domain we are migrating 3079 * off. In the worst case, sched_setattr() may temporary fail 3080 * until we complete the update. 3081 */ 3082 raw_spin_lock(&src_dl_b->lock); 3083 __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); 3084 raw_spin_unlock(&src_dl_b->lock); 3085 } 3086 3087 set_cpus_allowed_common(p, ctx); 3088 } 3089 3090 /* Assumes rq->lock is held */ 3091 static void rq_online_dl(struct rq *rq) 3092 { 3093 if (rq->dl.overloaded) 3094 dl_set_overload(rq); 3095 3096 if (rq->dl.dl_nr_running > 0) 3097 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr); 3098 else 3099 cpudl_clear(&rq->rd->cpudl, rq->cpu, true); 3100 } 3101 3102 /* Assumes rq->lock is held */ 3103 static void rq_offline_dl(struct rq *rq) 3104 { 3105 if (rq->dl.overloaded) 3106 dl_clear_overload(rq); 3107 3108 cpudl_clear(&rq->rd->cpudl, rq->cpu, false); 3109 } 3110 3111 void __init init_sched_dl_class(void) 3112 { 3113 unsigned int i; 3114 3115 for_each_possible_cpu(i) 3116 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i), 3117 GFP_KERNEL, cpu_to_node(i)); 3118 } 3119 3120 void dl_add_task_root_domain(struct task_struct *p) 3121 { 3122 struct rq_flags rf; 3123 struct rq *rq; 3124 struct dl_bw *dl_b; 3125 3126 raw_spin_lock_irqsave(&p->pi_lock, rf.flags); 3127 if (!dl_task(p) || dl_entity_is_special(&p->dl)) { 3128 raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags); 3129 return; 3130 } 3131 3132 rq = __task_rq_lock(p, &rf); 3133 3134 dl_b = &rq->rd->dl_bw; 3135 raw_spin_lock(&dl_b->lock); 3136 3137 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span)); 3138 3139 raw_spin_unlock(&dl_b->lock); 3140 3141 task_rq_unlock(rq, p, &rf); 3142 } 3143 3144 void dl_clear_root_domain(struct root_domain *rd) 3145 { 3146 int i; 3147 3148 guard(raw_spinlock_irqsave)(&rd->dl_bw.lock); 3149 3150 /* 3151 * Reset total_bw to zero and extra_bw to max_bw so that next 3152 * loop will add dl-servers contributions back properly, 3153 */ 3154 rd->dl_bw.total_bw = 0; 3155 for_each_cpu(i, rd->span) 3156 cpu_rq(i)->dl.extra_bw = cpu_rq(i)->dl.max_bw; 3157 3158 /* 3159 * dl_servers are not tasks. Since dl_add_task_root_domain ignores 3160 * them, we need to account for them here explicitly. 3161 */ 3162 for_each_cpu(i, rd->span) { 3163 struct sched_dl_entity *dl_se = &cpu_rq(i)->fair_server; 3164 3165 if (dl_server(dl_se) && cpu_active(i)) 3166 __dl_add(&rd->dl_bw, dl_se->dl_bw, dl_bw_cpus(i)); 3167 } 3168 } 3169 3170 void dl_clear_root_domain_cpu(int cpu) 3171 { 3172 dl_clear_root_domain(cpu_rq(cpu)->rd); 3173 } 3174 3175 static void switched_from_dl(struct rq *rq, struct task_struct *p) 3176 { 3177 /* 3178 * task_non_contending() can start the "inactive timer" (if the 0-lag 3179 * time is in the future). If the task switches back to dl before 3180 * the "inactive timer" fires, it can continue to consume its current 3181 * runtime using its current deadline. If it stays outside of 3182 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer() 3183 * will reset the task parameters. 3184 */ 3185 if (task_on_rq_queued(p) && p->dl.dl_runtime) 3186 task_non_contending(&p->dl, false); 3187 3188 /* 3189 * In case a task is setscheduled out from SCHED_DEADLINE we need to 3190 * keep track of that on its cpuset (for correct bandwidth tracking). 3191 */ 3192 dec_dl_tasks_cs(p); 3193 3194 if (!task_on_rq_queued(p)) { 3195 /* 3196 * Inactive timer is armed. However, p is leaving DEADLINE and 3197 * might migrate away from this rq while continuing to run on 3198 * some other class. We need to remove its contribution from 3199 * this rq running_bw now, or sub_rq_bw (below) will complain. 3200 */ 3201 if (p->dl.dl_non_contending) 3202 sub_running_bw(&p->dl, &rq->dl); 3203 sub_rq_bw(&p->dl, &rq->dl); 3204 } 3205 3206 /* 3207 * We cannot use inactive_task_timer() to invoke sub_running_bw() 3208 * at the 0-lag time, because the task could have been migrated 3209 * while SCHED_OTHER in the meanwhile. 3210 */ 3211 if (p->dl.dl_non_contending) 3212 p->dl.dl_non_contending = 0; 3213 3214 /* 3215 * Since this might be the only -deadline task on the rq, 3216 * this is the right place to try to pull some other one 3217 * from an overloaded CPU, if any. 3218 */ 3219 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running) 3220 return; 3221 3222 deadline_queue_pull_task(rq); 3223 } 3224 3225 /* 3226 * When switching to -deadline, we may overload the rq, then 3227 * we try to push someone off, if possible. 3228 */ 3229 static void switched_to_dl(struct rq *rq, struct task_struct *p) 3230 { 3231 cancel_inactive_timer(&p->dl); 3232 3233 /* 3234 * In case a task is setscheduled to SCHED_DEADLINE we need to keep 3235 * track of that on its cpuset (for correct bandwidth tracking). 3236 */ 3237 inc_dl_tasks_cs(p); 3238 3239 /* If p is not queued we will update its parameters at next wakeup. */ 3240 if (!task_on_rq_queued(p)) { 3241 add_rq_bw(&p->dl, &rq->dl); 3242 3243 return; 3244 } 3245 3246 if (rq->donor != p) { 3247 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded) 3248 deadline_queue_push_tasks(rq); 3249 if (dl_task(rq->donor)) 3250 wakeup_preempt_dl(rq, p, 0); 3251 else 3252 resched_curr(rq); 3253 } else { 3254 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0); 3255 } 3256 } 3257 3258 static u64 get_prio_dl(struct rq *rq, struct task_struct *p) 3259 { 3260 return p->dl.deadline; 3261 } 3262 3263 /* 3264 * If the scheduling parameters of a -deadline task changed, 3265 * a push or pull operation might be needed. 3266 */ 3267 static void prio_changed_dl(struct rq *rq, struct task_struct *p, u64 old_deadline) 3268 { 3269 if (!task_on_rq_queued(p)) 3270 return; 3271 3272 if (p->dl.deadline == old_deadline) 3273 return; 3274 3275 if (dl_time_before(old_deadline, p->dl.deadline)) 3276 deadline_queue_pull_task(rq); 3277 3278 if (task_current_donor(rq, p)) { 3279 /* 3280 * If we now have a earlier deadline task than p, 3281 * then reschedule, provided p is still on this 3282 * runqueue. 3283 */ 3284 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline)) 3285 resched_curr(rq); 3286 } else { 3287 /* 3288 * Current may not be deadline in case p was throttled but we 3289 * have just replenished it (e.g. rt_mutex_setprio()). 3290 * 3291 * Otherwise, if p was given an earlier deadline, reschedule. 3292 */ 3293 if (!dl_task(rq->curr) || 3294 dl_time_before(p->dl.deadline, rq->curr->dl.deadline)) 3295 resched_curr(rq); 3296 } 3297 } 3298 3299 #ifdef CONFIG_SCHED_CORE 3300 static int task_is_throttled_dl(struct task_struct *p, int cpu) 3301 { 3302 return p->dl.dl_throttled; 3303 } 3304 #endif 3305 3306 DEFINE_SCHED_CLASS(dl) = { 3307 3308 .queue_mask = 8, 3309 3310 .enqueue_task = enqueue_task_dl, 3311 .dequeue_task = dequeue_task_dl, 3312 .yield_task = yield_task_dl, 3313 3314 .wakeup_preempt = wakeup_preempt_dl, 3315 3316 .pick_task = pick_task_dl, 3317 .put_prev_task = put_prev_task_dl, 3318 .set_next_task = set_next_task_dl, 3319 3320 .balance = balance_dl, 3321 .select_task_rq = select_task_rq_dl, 3322 .migrate_task_rq = migrate_task_rq_dl, 3323 .set_cpus_allowed = set_cpus_allowed_dl, 3324 .rq_online = rq_online_dl, 3325 .rq_offline = rq_offline_dl, 3326 .task_woken = task_woken_dl, 3327 .find_lock_rq = find_lock_later_rq, 3328 3329 .task_tick = task_tick_dl, 3330 .task_fork = task_fork_dl, 3331 3332 .get_prio = get_prio_dl, 3333 .prio_changed = prio_changed_dl, 3334 .switched_from = switched_from_dl, 3335 .switched_to = switched_to_dl, 3336 3337 .update_curr = update_curr_dl, 3338 #ifdef CONFIG_SCHED_CORE 3339 .task_is_throttled = task_is_throttled_dl, 3340 #endif 3341 }; 3342 3343 /* 3344 * Used for dl_bw check and update, used under sched_rt_handler()::mutex and 3345 * sched_domains_mutex. 3346 */ 3347 u64 dl_cookie; 3348 3349 int sched_dl_global_validate(void) 3350 { 3351 u64 runtime = global_rt_runtime(); 3352 u64 period = global_rt_period(); 3353 u64 new_bw = to_ratio(period, runtime); 3354 u64 cookie = ++dl_cookie; 3355 struct dl_bw *dl_b; 3356 int cpu, cpus, ret = 0; 3357 unsigned long flags; 3358 3359 /* 3360 * Here we want to check the bandwidth not being set to some 3361 * value smaller than the currently allocated bandwidth in 3362 * any of the root_domains. 3363 */ 3364 for_each_online_cpu(cpu) { 3365 rcu_read_lock_sched(); 3366 3367 if (dl_bw_visited(cpu, cookie)) 3368 goto next; 3369 3370 dl_b = dl_bw_of(cpu); 3371 cpus = dl_bw_cpus(cpu); 3372 3373 raw_spin_lock_irqsave(&dl_b->lock, flags); 3374 if (new_bw * cpus < dl_b->total_bw) 3375 ret = -EBUSY; 3376 raw_spin_unlock_irqrestore(&dl_b->lock, flags); 3377 3378 next: 3379 rcu_read_unlock_sched(); 3380 3381 if (ret) 3382 break; 3383 } 3384 3385 return ret; 3386 } 3387 3388 static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq) 3389 { 3390 if (global_rt_runtime() == RUNTIME_INF) { 3391 dl_rq->bw_ratio = 1 << RATIO_SHIFT; 3392 dl_rq->max_bw = dl_rq->extra_bw = 1 << BW_SHIFT; 3393 } else { 3394 dl_rq->bw_ratio = to_ratio(global_rt_runtime(), 3395 global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT); 3396 dl_rq->max_bw = dl_rq->extra_bw = 3397 to_ratio(global_rt_period(), global_rt_runtime()); 3398 } 3399 } 3400 3401 void sched_dl_do_global(void) 3402 { 3403 u64 new_bw = -1; 3404 u64 cookie = ++dl_cookie; 3405 struct dl_bw *dl_b; 3406 int cpu; 3407 unsigned long flags; 3408 3409 if (global_rt_runtime() != RUNTIME_INF) 3410 new_bw = to_ratio(global_rt_period(), global_rt_runtime()); 3411 3412 for_each_possible_cpu(cpu) 3413 init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl); 3414 3415 for_each_possible_cpu(cpu) { 3416 rcu_read_lock_sched(); 3417 3418 if (dl_bw_visited(cpu, cookie)) { 3419 rcu_read_unlock_sched(); 3420 continue; 3421 } 3422 3423 dl_b = dl_bw_of(cpu); 3424 3425 raw_spin_lock_irqsave(&dl_b->lock, flags); 3426 dl_b->bw = new_bw; 3427 raw_spin_unlock_irqrestore(&dl_b->lock, flags); 3428 3429 rcu_read_unlock_sched(); 3430 } 3431 } 3432 3433 /* 3434 * We must be sure that accepting a new task (or allowing changing the 3435 * parameters of an existing one) is consistent with the bandwidth 3436 * constraints. If yes, this function also accordingly updates the currently 3437 * allocated bandwidth to reflect the new situation. 3438 * 3439 * This function is called while holding p's rq->lock. 3440 */ 3441 int sched_dl_overflow(struct task_struct *p, int policy, 3442 const struct sched_attr *attr) 3443 { 3444 u64 period = attr->sched_period ?: attr->sched_deadline; 3445 u64 runtime = attr->sched_runtime; 3446 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0; 3447 int cpus, err = -1, cpu = task_cpu(p); 3448 struct dl_bw *dl_b = dl_bw_of(cpu); 3449 unsigned long cap; 3450 3451 if (attr->sched_flags & SCHED_FLAG_SUGOV) 3452 return 0; 3453 3454 /* !deadline task may carry old deadline bandwidth */ 3455 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p)) 3456 return 0; 3457 3458 /* 3459 * Either if a task, enters, leave, or stays -deadline but changes 3460 * its parameters, we may need to update accordingly the total 3461 * allocated bandwidth of the container. 3462 */ 3463 raw_spin_lock(&dl_b->lock); 3464 cpus = dl_bw_cpus(cpu); 3465 cap = dl_bw_capacity(cpu); 3466 3467 if (dl_policy(policy) && !task_has_dl_policy(p) && 3468 !__dl_overflow(dl_b, cap, 0, new_bw)) { 3469 if (hrtimer_active(&p->dl.inactive_timer)) 3470 __dl_sub(dl_b, p->dl.dl_bw, cpus); 3471 __dl_add(dl_b, new_bw, cpus); 3472 err = 0; 3473 } else if (dl_policy(policy) && task_has_dl_policy(p) && 3474 !__dl_overflow(dl_b, cap, p->dl.dl_bw, new_bw)) { 3475 /* 3476 * XXX this is slightly incorrect: when the task 3477 * utilization decreases, we should delay the total 3478 * utilization change until the task's 0-lag point. 3479 * But this would require to set the task's "inactive 3480 * timer" when the task is not inactive. 3481 */ 3482 __dl_sub(dl_b, p->dl.dl_bw, cpus); 3483 __dl_add(dl_b, new_bw, cpus); 3484 dl_change_utilization(p, new_bw); 3485 err = 0; 3486 } else if (!dl_policy(policy) && task_has_dl_policy(p)) { 3487 /* 3488 * Do not decrease the total deadline utilization here, 3489 * switched_from_dl() will take care to do it at the correct 3490 * (0-lag) time. 3491 */ 3492 err = 0; 3493 } 3494 raw_spin_unlock(&dl_b->lock); 3495 3496 return err; 3497 } 3498 3499 /* 3500 * This function initializes the sched_dl_entity of a newly becoming 3501 * SCHED_DEADLINE task. 3502 * 3503 * Only the static values are considered here, the actual runtime and the 3504 * absolute deadline will be properly calculated when the task is enqueued 3505 * for the first time with its new policy. 3506 */ 3507 void __setparam_dl(struct task_struct *p, const struct sched_attr *attr) 3508 { 3509 struct sched_dl_entity *dl_se = &p->dl; 3510 3511 dl_se->dl_runtime = attr->sched_runtime; 3512 dl_se->dl_deadline = attr->sched_deadline; 3513 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline; 3514 dl_se->flags = attr->sched_flags & SCHED_DL_FLAGS; 3515 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime); 3516 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime); 3517 } 3518 3519 void __getparam_dl(struct task_struct *p, struct sched_attr *attr) 3520 { 3521 struct sched_dl_entity *dl_se = &p->dl; 3522 3523 attr->sched_priority = p->rt_priority; 3524 attr->sched_runtime = dl_se->dl_runtime; 3525 attr->sched_deadline = dl_se->dl_deadline; 3526 attr->sched_period = dl_se->dl_period; 3527 attr->sched_flags &= ~SCHED_DL_FLAGS; 3528 attr->sched_flags |= dl_se->flags; 3529 } 3530 3531 /* 3532 * This function validates the new parameters of a -deadline task. 3533 * We ask for the deadline not being zero, and greater or equal 3534 * than the runtime, as well as the period of being zero or 3535 * greater than deadline. Furthermore, we have to be sure that 3536 * user parameters are above the internal resolution of 1us (we 3537 * check sched_runtime only since it is always the smaller one) and 3538 * below 2^63 ns (we have to check both sched_deadline and 3539 * sched_period, as the latter can be zero). 3540 */ 3541 bool __checkparam_dl(const struct sched_attr *attr) 3542 { 3543 u64 period, max, min; 3544 3545 /* special dl tasks don't actually use any parameter */ 3546 if (attr->sched_flags & SCHED_FLAG_SUGOV) 3547 return true; 3548 3549 /* deadline != 0 */ 3550 if (attr->sched_deadline == 0) 3551 return false; 3552 3553 /* 3554 * Since we truncate DL_SCALE bits, make sure we're at least 3555 * that big. 3556 */ 3557 if (attr->sched_runtime < (1ULL << DL_SCALE)) 3558 return false; 3559 3560 /* 3561 * Since we use the MSB for wrap-around and sign issues, make 3562 * sure it's not set (mind that period can be equal to zero). 3563 */ 3564 if (attr->sched_deadline & (1ULL << 63) || 3565 attr->sched_period & (1ULL << 63)) 3566 return false; 3567 3568 period = attr->sched_period; 3569 if (!period) 3570 period = attr->sched_deadline; 3571 3572 /* runtime <= deadline <= period (if period != 0) */ 3573 if (period < attr->sched_deadline || 3574 attr->sched_deadline < attr->sched_runtime) 3575 return false; 3576 3577 max = (u64)READ_ONCE(sysctl_sched_dl_period_max) * NSEC_PER_USEC; 3578 min = (u64)READ_ONCE(sysctl_sched_dl_period_min) * NSEC_PER_USEC; 3579 3580 if (period < min || period > max) 3581 return false; 3582 3583 return true; 3584 } 3585 3586 /* 3587 * This function clears the sched_dl_entity static params. 3588 */ 3589 static void __dl_clear_params(struct sched_dl_entity *dl_se) 3590 { 3591 dl_se->dl_runtime = 0; 3592 dl_se->dl_deadline = 0; 3593 dl_se->dl_period = 0; 3594 dl_se->flags = 0; 3595 dl_se->dl_bw = 0; 3596 dl_se->dl_density = 0; 3597 3598 dl_se->dl_throttled = 0; 3599 dl_se->dl_yielded = 0; 3600 dl_se->dl_non_contending = 0; 3601 dl_se->dl_overrun = 0; 3602 dl_se->dl_server = 0; 3603 3604 #ifdef CONFIG_RT_MUTEXES 3605 dl_se->pi_se = dl_se; 3606 #endif 3607 } 3608 3609 void init_dl_entity(struct sched_dl_entity *dl_se) 3610 { 3611 RB_CLEAR_NODE(&dl_se->rb_node); 3612 init_dl_task_timer(dl_se); 3613 init_dl_inactive_task_timer(dl_se); 3614 __dl_clear_params(dl_se); 3615 } 3616 3617 bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr) 3618 { 3619 struct sched_dl_entity *dl_se = &p->dl; 3620 3621 if (dl_se->dl_runtime != attr->sched_runtime || 3622 dl_se->dl_deadline != attr->sched_deadline || 3623 dl_se->dl_period != attr->sched_period || 3624 dl_se->flags != (attr->sched_flags & SCHED_DL_FLAGS)) 3625 return true; 3626 3627 return false; 3628 } 3629 3630 int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, 3631 const struct cpumask *trial) 3632 { 3633 unsigned long flags, cap; 3634 struct dl_bw *cur_dl_b; 3635 int ret = 1; 3636 3637 rcu_read_lock_sched(); 3638 cur_dl_b = dl_bw_of(cpumask_any(cur)); 3639 cap = __dl_bw_capacity(trial); 3640 raw_spin_lock_irqsave(&cur_dl_b->lock, flags); 3641 if (__dl_overflow(cur_dl_b, cap, 0, 0)) 3642 ret = 0; 3643 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags); 3644 rcu_read_unlock_sched(); 3645 3646 return ret; 3647 } 3648 3649 enum dl_bw_request { 3650 dl_bw_req_deactivate = 0, 3651 dl_bw_req_alloc, 3652 dl_bw_req_free 3653 }; 3654 3655 static int dl_bw_manage(enum dl_bw_request req, int cpu, u64 dl_bw) 3656 { 3657 unsigned long flags, cap; 3658 struct dl_bw *dl_b; 3659 bool overflow = 0; 3660 u64 fair_server_bw = 0; 3661 3662 rcu_read_lock_sched(); 3663 dl_b = dl_bw_of(cpu); 3664 raw_spin_lock_irqsave(&dl_b->lock, flags); 3665 3666 cap = dl_bw_capacity(cpu); 3667 switch (req) { 3668 case dl_bw_req_free: 3669 __dl_sub(dl_b, dl_bw, dl_bw_cpus(cpu)); 3670 break; 3671 case dl_bw_req_alloc: 3672 overflow = __dl_overflow(dl_b, cap, 0, dl_bw); 3673 3674 if (!overflow) { 3675 /* 3676 * We reserve space in the destination 3677 * root_domain, as we can't fail after this point. 3678 * We will free resources in the source root_domain 3679 * later on (see set_cpus_allowed_dl()). 3680 */ 3681 __dl_add(dl_b, dl_bw, dl_bw_cpus(cpu)); 3682 } 3683 break; 3684 case dl_bw_req_deactivate: 3685 /* 3686 * cpu is not off yet, but we need to do the math by 3687 * considering it off already (i.e., what would happen if we 3688 * turn cpu off?). 3689 */ 3690 cap -= arch_scale_cpu_capacity(cpu); 3691 3692 /* 3693 * cpu is going offline and NORMAL tasks will be moved away 3694 * from it. We can thus discount dl_server bandwidth 3695 * contribution as it won't need to be servicing tasks after 3696 * the cpu is off. 3697 */ 3698 if (cpu_rq(cpu)->fair_server.dl_server) 3699 fair_server_bw = cpu_rq(cpu)->fair_server.dl_bw; 3700 3701 /* 3702 * Not much to check if no DEADLINE bandwidth is present. 3703 * dl_servers we can discount, as tasks will be moved out the 3704 * offlined CPUs anyway. 3705 */ 3706 if (dl_b->total_bw - fair_server_bw > 0) { 3707 /* 3708 * Leaving at least one CPU for DEADLINE tasks seems a 3709 * wise thing to do. As said above, cpu is not offline 3710 * yet, so account for that. 3711 */ 3712 if (dl_bw_cpus(cpu) - 1) 3713 overflow = __dl_overflow(dl_b, cap, fair_server_bw, 0); 3714 else 3715 overflow = 1; 3716 } 3717 3718 break; 3719 } 3720 3721 raw_spin_unlock_irqrestore(&dl_b->lock, flags); 3722 rcu_read_unlock_sched(); 3723 3724 return overflow ? -EBUSY : 0; 3725 } 3726 3727 int dl_bw_deactivate(int cpu) 3728 { 3729 return dl_bw_manage(dl_bw_req_deactivate, cpu, 0); 3730 } 3731 3732 int dl_bw_alloc(int cpu, u64 dl_bw) 3733 { 3734 return dl_bw_manage(dl_bw_req_alloc, cpu, dl_bw); 3735 } 3736 3737 void dl_bw_free(int cpu, u64 dl_bw) 3738 { 3739 dl_bw_manage(dl_bw_req_free, cpu, dl_bw); 3740 } 3741 3742 void print_dl_stats(struct seq_file *m, int cpu) 3743 { 3744 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl); 3745 } 3746