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