1 /* 2 * Deadline Scheduling Class (SCHED_DEADLINE) 3 * 4 * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS). 5 * 6 * Tasks that periodically executes their instances for less than their 7 * runtime won't miss any of their deadlines. 8 * Tasks that are not periodic or sporadic or that tries to execute more 9 * than their reserved bandwidth will be slowed down (and may potentially 10 * miss some of their deadlines), and won't affect any other task. 11 * 12 * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>, 13 * Juri Lelli <juri.lelli@gmail.com>, 14 * Michael Trimarchi <michael@amarulasolutions.com>, 15 * Fabio Checconi <fchecconi@gmail.com> 16 */ 17 #include "sched.h" 18 19 #include <linux/slab.h> 20 21 struct dl_bandwidth def_dl_bandwidth; 22 23 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se) 24 { 25 return container_of(dl_se, struct task_struct, dl); 26 } 27 28 static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq) 29 { 30 return container_of(dl_rq, struct rq, dl); 31 } 32 33 static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se) 34 { 35 struct task_struct *p = dl_task_of(dl_se); 36 struct rq *rq = task_rq(p); 37 38 return &rq->dl; 39 } 40 41 static inline int on_dl_rq(struct sched_dl_entity *dl_se) 42 { 43 return !RB_EMPTY_NODE(&dl_se->rb_node); 44 } 45 46 static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) 47 { 48 struct sched_dl_entity *dl_se = &p->dl; 49 50 return dl_rq->rb_leftmost == &dl_se->rb_node; 51 } 52 53 void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime) 54 { 55 raw_spin_lock_init(&dl_b->dl_runtime_lock); 56 dl_b->dl_period = period; 57 dl_b->dl_runtime = runtime; 58 } 59 60 extern unsigned long to_ratio(u64 period, u64 runtime); 61 62 void init_dl_bw(struct dl_bw *dl_b) 63 { 64 raw_spin_lock_init(&dl_b->lock); 65 raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock); 66 if (global_rt_runtime() == RUNTIME_INF) 67 dl_b->bw = -1; 68 else 69 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime()); 70 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock); 71 dl_b->total_bw = 0; 72 } 73 74 void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq) 75 { 76 dl_rq->rb_root = RB_ROOT; 77 78 #ifdef CONFIG_SMP 79 /* zero means no -deadline tasks */ 80 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0; 81 82 dl_rq->dl_nr_migratory = 0; 83 dl_rq->overloaded = 0; 84 dl_rq->pushable_dl_tasks_root = RB_ROOT; 85 #else 86 init_dl_bw(&dl_rq->dl_bw); 87 #endif 88 } 89 90 #ifdef CONFIG_SMP 91 92 static inline int dl_overloaded(struct rq *rq) 93 { 94 return atomic_read(&rq->rd->dlo_count); 95 } 96 97 static inline void dl_set_overload(struct rq *rq) 98 { 99 if (!rq->online) 100 return; 101 102 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask); 103 /* 104 * Must be visible before the overload count is 105 * set (as in sched_rt.c). 106 * 107 * Matched by the barrier in pull_dl_task(). 108 */ 109 smp_wmb(); 110 atomic_inc(&rq->rd->dlo_count); 111 } 112 113 static inline void dl_clear_overload(struct rq *rq) 114 { 115 if (!rq->online) 116 return; 117 118 atomic_dec(&rq->rd->dlo_count); 119 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask); 120 } 121 122 static void update_dl_migration(struct dl_rq *dl_rq) 123 { 124 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_total > 1) { 125 if (!dl_rq->overloaded) { 126 dl_set_overload(rq_of_dl_rq(dl_rq)); 127 dl_rq->overloaded = 1; 128 } 129 } else if (dl_rq->overloaded) { 130 dl_clear_overload(rq_of_dl_rq(dl_rq)); 131 dl_rq->overloaded = 0; 132 } 133 } 134 135 static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 136 { 137 struct task_struct *p = dl_task_of(dl_se); 138 dl_rq = &rq_of_dl_rq(dl_rq)->dl; 139 140 dl_rq->dl_nr_total++; 141 if (p->nr_cpus_allowed > 1) 142 dl_rq->dl_nr_migratory++; 143 144 update_dl_migration(dl_rq); 145 } 146 147 static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 148 { 149 struct task_struct *p = dl_task_of(dl_se); 150 dl_rq = &rq_of_dl_rq(dl_rq)->dl; 151 152 dl_rq->dl_nr_total--; 153 if (p->nr_cpus_allowed > 1) 154 dl_rq->dl_nr_migratory--; 155 156 update_dl_migration(dl_rq); 157 } 158 159 /* 160 * The list of pushable -deadline task is not a plist, like in 161 * sched_rt.c, it is an rb-tree with tasks ordered by deadline. 162 */ 163 static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) 164 { 165 struct dl_rq *dl_rq = &rq->dl; 166 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node; 167 struct rb_node *parent = NULL; 168 struct task_struct *entry; 169 int leftmost = 1; 170 171 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks)); 172 173 while (*link) { 174 parent = *link; 175 entry = rb_entry(parent, struct task_struct, 176 pushable_dl_tasks); 177 if (dl_entity_preempt(&p->dl, &entry->dl)) 178 link = &parent->rb_left; 179 else { 180 link = &parent->rb_right; 181 leftmost = 0; 182 } 183 } 184 185 if (leftmost) 186 dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks; 187 188 rb_link_node(&p->pushable_dl_tasks, parent, link); 189 rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root); 190 } 191 192 static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p) 193 { 194 struct dl_rq *dl_rq = &rq->dl; 195 196 if (RB_EMPTY_NODE(&p->pushable_dl_tasks)) 197 return; 198 199 if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) { 200 struct rb_node *next_node; 201 202 next_node = rb_next(&p->pushable_dl_tasks); 203 dl_rq->pushable_dl_tasks_leftmost = next_node; 204 } 205 206 rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root); 207 RB_CLEAR_NODE(&p->pushable_dl_tasks); 208 } 209 210 static inline int has_pushable_dl_tasks(struct rq *rq) 211 { 212 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root); 213 } 214 215 static int push_dl_task(struct rq *rq); 216 217 #else 218 219 static inline 220 void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) 221 { 222 } 223 224 static inline 225 void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p) 226 { 227 } 228 229 static inline 230 void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 231 { 232 } 233 234 static inline 235 void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 236 { 237 } 238 239 #endif /* CONFIG_SMP */ 240 241 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags); 242 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags); 243 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, 244 int flags); 245 246 /* 247 * We are being explicitly informed that a new instance is starting, 248 * and this means that: 249 * - the absolute deadline of the entity has to be placed at 250 * current time + relative deadline; 251 * - the runtime of the entity has to be set to the maximum value. 252 * 253 * The capability of specifying such event is useful whenever a -deadline 254 * entity wants to (try to!) synchronize its behaviour with the scheduler's 255 * one, and to (try to!) reconcile itself with its own scheduling 256 * parameters. 257 */ 258 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se, 259 struct sched_dl_entity *pi_se) 260 { 261 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 262 struct rq *rq = rq_of_dl_rq(dl_rq); 263 264 WARN_ON(!dl_se->dl_new || dl_se->dl_throttled); 265 266 /* 267 * We use the regular wall clock time to set deadlines in the 268 * future; in fact, we must consider execution overheads (time 269 * spent on hardirq context, etc.). 270 */ 271 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; 272 dl_se->runtime = pi_se->dl_runtime; 273 dl_se->dl_new = 0; 274 } 275 276 /* 277 * Pure Earliest Deadline First (EDF) scheduling does not deal with the 278 * possibility of a entity lasting more than what it declared, and thus 279 * exhausting its runtime. 280 * 281 * Here we are interested in making runtime overrun possible, but we do 282 * not want a entity which is misbehaving to affect the scheduling of all 283 * other entities. 284 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS) 285 * is used, in order to confine each entity within its own bandwidth. 286 * 287 * This function deals exactly with that, and ensures that when the runtime 288 * of a entity is replenished, its deadline is also postponed. That ensures 289 * the overrunning entity can't interfere with other entity in the system and 290 * can't make them miss their deadlines. Reasons why this kind of overruns 291 * could happen are, typically, a entity voluntarily trying to overcome its 292 * runtime, or it just underestimated it during sched_setscheduler_ex(). 293 */ 294 static void replenish_dl_entity(struct sched_dl_entity *dl_se, 295 struct sched_dl_entity *pi_se) 296 { 297 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 298 struct rq *rq = rq_of_dl_rq(dl_rq); 299 300 BUG_ON(pi_se->dl_runtime <= 0); 301 302 /* 303 * This could be the case for a !-dl task that is boosted. 304 * Just go with full inherited parameters. 305 */ 306 if (dl_se->dl_deadline == 0) { 307 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; 308 dl_se->runtime = pi_se->dl_runtime; 309 } 310 311 /* 312 * We keep moving the deadline away until we get some 313 * available runtime for the entity. This ensures correct 314 * handling of situations where the runtime overrun is 315 * arbitrary large. 316 */ 317 while (dl_se->runtime <= 0) { 318 dl_se->deadline += pi_se->dl_period; 319 dl_se->runtime += pi_se->dl_runtime; 320 } 321 322 /* 323 * At this point, the deadline really should be "in 324 * the future" with respect to rq->clock. If it's 325 * not, we are, for some reason, lagging too much! 326 * Anyway, after having warn userspace abut that, 327 * we still try to keep the things running by 328 * resetting the deadline and the budget of the 329 * entity. 330 */ 331 if (dl_time_before(dl_se->deadline, rq_clock(rq))) { 332 static bool lag_once = false; 333 334 if (!lag_once) { 335 lag_once = true; 336 printk_sched("sched: DL replenish lagged to much\n"); 337 } 338 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; 339 dl_se->runtime = pi_se->dl_runtime; 340 } 341 } 342 343 /* 344 * Here we check if --at time t-- an entity (which is probably being 345 * [re]activated or, in general, enqueued) can use its remaining runtime 346 * and its current deadline _without_ exceeding the bandwidth it is 347 * assigned (function returns true if it can't). We are in fact applying 348 * one of the CBS rules: when a task wakes up, if the residual runtime 349 * over residual deadline fits within the allocated bandwidth, then we 350 * can keep the current (absolute) deadline and residual budget without 351 * disrupting the schedulability of the system. Otherwise, we should 352 * refill the runtime and set the deadline a period in the future, 353 * because keeping the current (absolute) deadline of the task would 354 * result in breaking guarantees promised to other tasks (refer to 355 * Documentation/scheduler/sched-deadline.txt for more informations). 356 * 357 * This function returns true if: 358 * 359 * runtime / (deadline - t) > dl_runtime / dl_period , 360 * 361 * IOW we can't recycle current parameters. 362 * 363 * Notice that the bandwidth check is done against the period. For 364 * task with deadline equal to period this is the same of using 365 * dl_deadline instead of dl_period in the equation above. 366 */ 367 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, 368 struct sched_dl_entity *pi_se, u64 t) 369 { 370 u64 left, right; 371 372 /* 373 * left and right are the two sides of the equation above, 374 * after a bit of shuffling to use multiplications instead 375 * of divisions. 376 * 377 * Note that none of the time values involved in the two 378 * multiplications are absolute: dl_deadline and dl_runtime 379 * are the relative deadline and the maximum runtime of each 380 * instance, runtime is the runtime left for the last instance 381 * and (deadline - t), since t is rq->clock, is the time left 382 * to the (absolute) deadline. Even if overflowing the u64 type 383 * is very unlikely to occur in both cases, here we scale down 384 * as we want to avoid that risk at all. Scaling down by 10 385 * means that we reduce granularity to 1us. We are fine with it, 386 * since this is only a true/false check and, anyway, thinking 387 * of anything below microseconds resolution is actually fiction 388 * (but still we want to give the user that illusion >;). 389 */ 390 left = (pi_se->dl_period >> DL_SCALE) * (dl_se->runtime >> DL_SCALE); 391 right = ((dl_se->deadline - t) >> DL_SCALE) * 392 (pi_se->dl_runtime >> DL_SCALE); 393 394 return dl_time_before(right, left); 395 } 396 397 /* 398 * When a -deadline entity is queued back on the runqueue, its runtime and 399 * deadline might need updating. 400 * 401 * The policy here is that we update the deadline of the entity only if: 402 * - the current deadline is in the past, 403 * - using the remaining runtime with the current deadline would make 404 * the entity exceed its bandwidth. 405 */ 406 static void update_dl_entity(struct sched_dl_entity *dl_se, 407 struct sched_dl_entity *pi_se) 408 { 409 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 410 struct rq *rq = rq_of_dl_rq(dl_rq); 411 412 /* 413 * The arrival of a new instance needs special treatment, i.e., 414 * the actual scheduling parameters have to be "renewed". 415 */ 416 if (dl_se->dl_new) { 417 setup_new_dl_entity(dl_se, pi_se); 418 return; 419 } 420 421 if (dl_time_before(dl_se->deadline, rq_clock(rq)) || 422 dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) { 423 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; 424 dl_se->runtime = pi_se->dl_runtime; 425 } 426 } 427 428 /* 429 * If the entity depleted all its runtime, and if we want it to sleep 430 * while waiting for some new execution time to become available, we 431 * set the bandwidth enforcement timer to the replenishment instant 432 * and try to activate it. 433 * 434 * Notice that it is important for the caller to know if the timer 435 * actually started or not (i.e., the replenishment instant is in 436 * the future or in the past). 437 */ 438 static int start_dl_timer(struct sched_dl_entity *dl_se, bool boosted) 439 { 440 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 441 struct rq *rq = rq_of_dl_rq(dl_rq); 442 ktime_t now, act; 443 ktime_t soft, hard; 444 unsigned long range; 445 s64 delta; 446 447 if (boosted) 448 return 0; 449 /* 450 * We want the timer to fire at the deadline, but considering 451 * that it is actually coming from rq->clock and not from 452 * hrtimer's time base reading. 453 */ 454 act = ns_to_ktime(dl_se->deadline); 455 now = hrtimer_cb_get_time(&dl_se->dl_timer); 456 delta = ktime_to_ns(now) - rq_clock(rq); 457 act = ktime_add_ns(act, delta); 458 459 /* 460 * If the expiry time already passed, e.g., because the value 461 * chosen as the deadline is too small, don't even try to 462 * start the timer in the past! 463 */ 464 if (ktime_us_delta(act, now) < 0) 465 return 0; 466 467 hrtimer_set_expires(&dl_se->dl_timer, act); 468 469 soft = hrtimer_get_softexpires(&dl_se->dl_timer); 470 hard = hrtimer_get_expires(&dl_se->dl_timer); 471 range = ktime_to_ns(ktime_sub(hard, soft)); 472 __hrtimer_start_range_ns(&dl_se->dl_timer, soft, 473 range, HRTIMER_MODE_ABS, 0); 474 475 return hrtimer_active(&dl_se->dl_timer); 476 } 477 478 /* 479 * This is the bandwidth enforcement timer callback. If here, we know 480 * a task is not on its dl_rq, since the fact that the timer was running 481 * means the task is throttled and needs a runtime replenishment. 482 * 483 * However, what we actually do depends on the fact the task is active, 484 * (it is on its rq) or has been removed from there by a call to 485 * dequeue_task_dl(). In the former case we must issue the runtime 486 * replenishment and add the task back to the dl_rq; in the latter, we just 487 * do nothing but clearing dl_throttled, so that runtime and deadline 488 * updating (and the queueing back to dl_rq) will be done by the 489 * next call to enqueue_task_dl(). 490 */ 491 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) 492 { 493 struct sched_dl_entity *dl_se = container_of(timer, 494 struct sched_dl_entity, 495 dl_timer); 496 struct task_struct *p = dl_task_of(dl_se); 497 struct rq *rq = task_rq(p); 498 raw_spin_lock(&rq->lock); 499 500 /* 501 * We need to take care of a possible races here. In fact, the 502 * task might have changed its scheduling policy to something 503 * different from SCHED_DEADLINE or changed its reservation 504 * parameters (through sched_setscheduler()). 505 */ 506 if (!dl_task(p) || dl_se->dl_new) 507 goto unlock; 508 509 sched_clock_tick(); 510 update_rq_clock(rq); 511 dl_se->dl_throttled = 0; 512 if (p->on_rq) { 513 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH); 514 if (task_has_dl_policy(rq->curr)) 515 check_preempt_curr_dl(rq, p, 0); 516 else 517 resched_task(rq->curr); 518 #ifdef CONFIG_SMP 519 /* 520 * Queueing this task back might have overloaded rq, 521 * check if we need to kick someone away. 522 */ 523 if (has_pushable_dl_tasks(rq)) 524 push_dl_task(rq); 525 #endif 526 } 527 unlock: 528 raw_spin_unlock(&rq->lock); 529 530 return HRTIMER_NORESTART; 531 } 532 533 void init_dl_task_timer(struct sched_dl_entity *dl_se) 534 { 535 struct hrtimer *timer = &dl_se->dl_timer; 536 537 if (hrtimer_active(timer)) { 538 hrtimer_try_to_cancel(timer); 539 return; 540 } 541 542 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 543 timer->function = dl_task_timer; 544 } 545 546 static 547 int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se) 548 { 549 int dmiss = dl_time_before(dl_se->deadline, rq_clock(rq)); 550 int rorun = dl_se->runtime <= 0; 551 552 if (!rorun && !dmiss) 553 return 0; 554 555 /* 556 * If we are beyond our current deadline and we are still 557 * executing, then we have already used some of the runtime of 558 * the next instance. Thus, if we do not account that, we are 559 * stealing bandwidth from the system at each deadline miss! 560 */ 561 if (dmiss) { 562 dl_se->runtime = rorun ? dl_se->runtime : 0; 563 dl_se->runtime -= rq_clock(rq) - dl_se->deadline; 564 } 565 566 return 1; 567 } 568 569 /* 570 * Update the current task's runtime statistics (provided it is still 571 * a -deadline task and has not been removed from the dl_rq). 572 */ 573 static void update_curr_dl(struct rq *rq) 574 { 575 struct task_struct *curr = rq->curr; 576 struct sched_dl_entity *dl_se = &curr->dl; 577 u64 delta_exec; 578 579 if (!dl_task(curr) || !on_dl_rq(dl_se)) 580 return; 581 582 /* 583 * Consumed budget is computed considering the time as 584 * observed by schedulable tasks (excluding time spent 585 * in hardirq context, etc.). Deadlines are instead 586 * computed using hard walltime. This seems to be the more 587 * natural solution, but the full ramifications of this 588 * approach need further study. 589 */ 590 delta_exec = rq_clock_task(rq) - curr->se.exec_start; 591 if (unlikely((s64)delta_exec < 0)) 592 delta_exec = 0; 593 594 schedstat_set(curr->se.statistics.exec_max, 595 max(curr->se.statistics.exec_max, delta_exec)); 596 597 curr->se.sum_exec_runtime += delta_exec; 598 account_group_exec_runtime(curr, delta_exec); 599 600 curr->se.exec_start = rq_clock_task(rq); 601 cpuacct_charge(curr, delta_exec); 602 603 sched_rt_avg_update(rq, delta_exec); 604 605 dl_se->runtime -= delta_exec; 606 if (dl_runtime_exceeded(rq, dl_se)) { 607 __dequeue_task_dl(rq, curr, 0); 608 if (likely(start_dl_timer(dl_se, curr->dl.dl_boosted))) 609 dl_se->dl_throttled = 1; 610 else 611 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH); 612 613 if (!is_leftmost(curr, &rq->dl)) 614 resched_task(curr); 615 } 616 617 /* 618 * Because -- for now -- we share the rt bandwidth, we need to 619 * account our runtime there too, otherwise actual rt tasks 620 * would be able to exceed the shared quota. 621 * 622 * Account to the root rt group for now. 623 * 624 * The solution we're working towards is having the RT groups scheduled 625 * using deadline servers -- however there's a few nasties to figure 626 * out before that can happen. 627 */ 628 if (rt_bandwidth_enabled()) { 629 struct rt_rq *rt_rq = &rq->rt; 630 631 raw_spin_lock(&rt_rq->rt_runtime_lock); 632 rt_rq->rt_time += delta_exec; 633 /* 634 * We'll let actual RT tasks worry about the overflow here, we 635 * have our own CBS to keep us inline -- see above. 636 */ 637 raw_spin_unlock(&rt_rq->rt_runtime_lock); 638 } 639 } 640 641 #ifdef CONFIG_SMP 642 643 static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu); 644 645 static inline u64 next_deadline(struct rq *rq) 646 { 647 struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu); 648 649 if (next && dl_prio(next->prio)) 650 return next->dl.deadline; 651 else 652 return 0; 653 } 654 655 static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) 656 { 657 struct rq *rq = rq_of_dl_rq(dl_rq); 658 659 if (dl_rq->earliest_dl.curr == 0 || 660 dl_time_before(deadline, dl_rq->earliest_dl.curr)) { 661 /* 662 * If the dl_rq had no -deadline tasks, or if the new task 663 * has shorter deadline than the current one on dl_rq, we 664 * know that the previous earliest becomes our next earliest, 665 * as the new task becomes the earliest itself. 666 */ 667 dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr; 668 dl_rq->earliest_dl.curr = deadline; 669 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline, 1); 670 } else if (dl_rq->earliest_dl.next == 0 || 671 dl_time_before(deadline, dl_rq->earliest_dl.next)) { 672 /* 673 * On the other hand, if the new -deadline task has a 674 * a later deadline than the earliest one on dl_rq, but 675 * it is earlier than the next (if any), we must 676 * recompute the next-earliest. 677 */ 678 dl_rq->earliest_dl.next = next_deadline(rq); 679 } 680 } 681 682 static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) 683 { 684 struct rq *rq = rq_of_dl_rq(dl_rq); 685 686 /* 687 * Since we may have removed our earliest (and/or next earliest) 688 * task we must recompute them. 689 */ 690 if (!dl_rq->dl_nr_running) { 691 dl_rq->earliest_dl.curr = 0; 692 dl_rq->earliest_dl.next = 0; 693 cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0); 694 } else { 695 struct rb_node *leftmost = dl_rq->rb_leftmost; 696 struct sched_dl_entity *entry; 697 698 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node); 699 dl_rq->earliest_dl.curr = entry->deadline; 700 dl_rq->earliest_dl.next = next_deadline(rq); 701 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline, 1); 702 } 703 } 704 705 #else 706 707 static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} 708 static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} 709 710 #endif /* CONFIG_SMP */ 711 712 static inline 713 void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 714 { 715 int prio = dl_task_of(dl_se)->prio; 716 u64 deadline = dl_se->deadline; 717 718 WARN_ON(!dl_prio(prio)); 719 dl_rq->dl_nr_running++; 720 721 inc_dl_deadline(dl_rq, deadline); 722 inc_dl_migration(dl_se, dl_rq); 723 } 724 725 static inline 726 void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) 727 { 728 int prio = dl_task_of(dl_se)->prio; 729 730 WARN_ON(!dl_prio(prio)); 731 WARN_ON(!dl_rq->dl_nr_running); 732 dl_rq->dl_nr_running--; 733 734 dec_dl_deadline(dl_rq, dl_se->deadline); 735 dec_dl_migration(dl_se, dl_rq); 736 } 737 738 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se) 739 { 740 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 741 struct rb_node **link = &dl_rq->rb_root.rb_node; 742 struct rb_node *parent = NULL; 743 struct sched_dl_entity *entry; 744 int leftmost = 1; 745 746 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node)); 747 748 while (*link) { 749 parent = *link; 750 entry = rb_entry(parent, struct sched_dl_entity, rb_node); 751 if (dl_time_before(dl_se->deadline, entry->deadline)) 752 link = &parent->rb_left; 753 else { 754 link = &parent->rb_right; 755 leftmost = 0; 756 } 757 } 758 759 if (leftmost) 760 dl_rq->rb_leftmost = &dl_se->rb_node; 761 762 rb_link_node(&dl_se->rb_node, parent, link); 763 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root); 764 765 inc_dl_tasks(dl_se, dl_rq); 766 } 767 768 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se) 769 { 770 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 771 772 if (RB_EMPTY_NODE(&dl_se->rb_node)) 773 return; 774 775 if (dl_rq->rb_leftmost == &dl_se->rb_node) { 776 struct rb_node *next_node; 777 778 next_node = rb_next(&dl_se->rb_node); 779 dl_rq->rb_leftmost = next_node; 780 } 781 782 rb_erase(&dl_se->rb_node, &dl_rq->rb_root); 783 RB_CLEAR_NODE(&dl_se->rb_node); 784 785 dec_dl_tasks(dl_se, dl_rq); 786 } 787 788 static void 789 enqueue_dl_entity(struct sched_dl_entity *dl_se, 790 struct sched_dl_entity *pi_se, int flags) 791 { 792 BUG_ON(on_dl_rq(dl_se)); 793 794 /* 795 * If this is a wakeup or a new instance, the scheduling 796 * parameters of the task might need updating. Otherwise, 797 * we want a replenishment of its runtime. 798 */ 799 if (!dl_se->dl_new && flags & ENQUEUE_REPLENISH) 800 replenish_dl_entity(dl_se, pi_se); 801 else 802 update_dl_entity(dl_se, pi_se); 803 804 __enqueue_dl_entity(dl_se); 805 } 806 807 static void dequeue_dl_entity(struct sched_dl_entity *dl_se) 808 { 809 __dequeue_dl_entity(dl_se); 810 } 811 812 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags) 813 { 814 struct task_struct *pi_task = rt_mutex_get_top_task(p); 815 struct sched_dl_entity *pi_se = &p->dl; 816 817 /* 818 * Use the scheduling parameters of the top pi-waiter 819 * task if we have one and its (relative) deadline is 820 * smaller than our one... OTW we keep our runtime and 821 * deadline. 822 */ 823 if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) 824 pi_se = &pi_task->dl; 825 826 /* 827 * If p is throttled, we do nothing. In fact, if it exhausted 828 * its budget it needs a replenishment and, since it now is on 829 * its rq, the bandwidth timer callback (which clearly has not 830 * run yet) will take care of this. 831 */ 832 if (p->dl.dl_throttled) 833 return; 834 835 enqueue_dl_entity(&p->dl, pi_se, flags); 836 837 if (!task_current(rq, p) && p->nr_cpus_allowed > 1) 838 enqueue_pushable_dl_task(rq, p); 839 840 inc_nr_running(rq); 841 } 842 843 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) 844 { 845 dequeue_dl_entity(&p->dl); 846 dequeue_pushable_dl_task(rq, p); 847 } 848 849 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) 850 { 851 update_curr_dl(rq); 852 __dequeue_task_dl(rq, p, flags); 853 854 dec_nr_running(rq); 855 } 856 857 /* 858 * Yield task semantic for -deadline tasks is: 859 * 860 * get off from the CPU until our next instance, with 861 * a new runtime. This is of little use now, since we 862 * don't have a bandwidth reclaiming mechanism. Anyway, 863 * bandwidth reclaiming is planned for the future, and 864 * yield_task_dl will indicate that some spare budget 865 * is available for other task instances to use it. 866 */ 867 static void yield_task_dl(struct rq *rq) 868 { 869 struct task_struct *p = rq->curr; 870 871 /* 872 * We make the task go to sleep until its current deadline by 873 * forcing its runtime to zero. This way, update_curr_dl() stops 874 * it and the bandwidth timer will wake it up and will give it 875 * new scheduling parameters (thanks to dl_new=1). 876 */ 877 if (p->dl.runtime > 0) { 878 rq->curr->dl.dl_new = 1; 879 p->dl.runtime = 0; 880 } 881 update_curr_dl(rq); 882 } 883 884 #ifdef CONFIG_SMP 885 886 static int find_later_rq(struct task_struct *task); 887 888 static int 889 select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags) 890 { 891 struct task_struct *curr; 892 struct rq *rq; 893 894 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK) 895 goto out; 896 897 rq = cpu_rq(cpu); 898 899 rcu_read_lock(); 900 curr = ACCESS_ONCE(rq->curr); /* unlocked access */ 901 902 /* 903 * If we are dealing with a -deadline task, we must 904 * decide where to wake it up. 905 * If it has a later deadline and the current task 906 * on this rq can't move (provided the waking task 907 * can!) we prefer to send it somewhere else. On the 908 * other hand, if it has a shorter deadline, we 909 * try to make it stay here, it might be important. 910 */ 911 if (unlikely(dl_task(curr)) && 912 (curr->nr_cpus_allowed < 2 || 913 !dl_entity_preempt(&p->dl, &curr->dl)) && 914 (p->nr_cpus_allowed > 1)) { 915 int target = find_later_rq(p); 916 917 if (target != -1) 918 cpu = target; 919 } 920 rcu_read_unlock(); 921 922 out: 923 return cpu; 924 } 925 926 static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p) 927 { 928 /* 929 * Current can't be migrated, useless to reschedule, 930 * let's hope p can move out. 931 */ 932 if (rq->curr->nr_cpus_allowed == 1 || 933 cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1) 934 return; 935 936 /* 937 * p is migratable, so let's not schedule it and 938 * see if it is pushed or pulled somewhere else. 939 */ 940 if (p->nr_cpus_allowed != 1 && 941 cpudl_find(&rq->rd->cpudl, p, NULL) != -1) 942 return; 943 944 resched_task(rq->curr); 945 } 946 947 #endif /* CONFIG_SMP */ 948 949 /* 950 * Only called when both the current and waking task are -deadline 951 * tasks. 952 */ 953 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, 954 int flags) 955 { 956 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) { 957 resched_task(rq->curr); 958 return; 959 } 960 961 #ifdef CONFIG_SMP 962 /* 963 * In the unlikely case current and p have the same deadline 964 * let us try to decide what's the best thing to do... 965 */ 966 if ((p->dl.deadline == rq->curr->dl.deadline) && 967 !test_tsk_need_resched(rq->curr)) 968 check_preempt_equal_dl(rq, p); 969 #endif /* CONFIG_SMP */ 970 } 971 972 #ifdef CONFIG_SCHED_HRTICK 973 static void start_hrtick_dl(struct rq *rq, struct task_struct *p) 974 { 975 s64 delta = p->dl.dl_runtime - p->dl.runtime; 976 977 if (delta > 10000) 978 hrtick_start(rq, p->dl.runtime); 979 } 980 #endif 981 982 static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq, 983 struct dl_rq *dl_rq) 984 { 985 struct rb_node *left = dl_rq->rb_leftmost; 986 987 if (!left) 988 return NULL; 989 990 return rb_entry(left, struct sched_dl_entity, rb_node); 991 } 992 993 struct task_struct *pick_next_task_dl(struct rq *rq) 994 { 995 struct sched_dl_entity *dl_se; 996 struct task_struct *p; 997 struct dl_rq *dl_rq; 998 999 dl_rq = &rq->dl; 1000 1001 if (unlikely(!dl_rq->dl_nr_running)) 1002 return NULL; 1003 1004 dl_se = pick_next_dl_entity(rq, dl_rq); 1005 BUG_ON(!dl_se); 1006 1007 p = dl_task_of(dl_se); 1008 p->se.exec_start = rq_clock_task(rq); 1009 1010 /* Running task will never be pushed. */ 1011 dequeue_pushable_dl_task(rq, p); 1012 1013 #ifdef CONFIG_SCHED_HRTICK 1014 if (hrtick_enabled(rq)) 1015 start_hrtick_dl(rq, p); 1016 #endif 1017 1018 #ifdef CONFIG_SMP 1019 rq->post_schedule = has_pushable_dl_tasks(rq); 1020 #endif /* CONFIG_SMP */ 1021 1022 return p; 1023 } 1024 1025 static void put_prev_task_dl(struct rq *rq, struct task_struct *p) 1026 { 1027 update_curr_dl(rq); 1028 1029 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1) 1030 enqueue_pushable_dl_task(rq, p); 1031 } 1032 1033 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued) 1034 { 1035 update_curr_dl(rq); 1036 1037 #ifdef CONFIG_SCHED_HRTICK 1038 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0) 1039 start_hrtick_dl(rq, p); 1040 #endif 1041 } 1042 1043 static void task_fork_dl(struct task_struct *p) 1044 { 1045 /* 1046 * SCHED_DEADLINE tasks cannot fork and this is achieved through 1047 * sched_fork() 1048 */ 1049 } 1050 1051 static void task_dead_dl(struct task_struct *p) 1052 { 1053 struct hrtimer *timer = &p->dl.dl_timer; 1054 struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); 1055 1056 /* 1057 * Since we are TASK_DEAD we won't slip out of the domain! 1058 */ 1059 raw_spin_lock_irq(&dl_b->lock); 1060 dl_b->total_bw -= p->dl.dl_bw; 1061 raw_spin_unlock_irq(&dl_b->lock); 1062 1063 hrtimer_cancel(timer); 1064 } 1065 1066 static void set_curr_task_dl(struct rq *rq) 1067 { 1068 struct task_struct *p = rq->curr; 1069 1070 p->se.exec_start = rq_clock_task(rq); 1071 1072 /* You can't push away the running task */ 1073 dequeue_pushable_dl_task(rq, p); 1074 } 1075 1076 #ifdef CONFIG_SMP 1077 1078 /* Only try algorithms three times */ 1079 #define DL_MAX_TRIES 3 1080 1081 static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu) 1082 { 1083 if (!task_running(rq, p) && 1084 (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) && 1085 (p->nr_cpus_allowed > 1)) 1086 return 1; 1087 1088 return 0; 1089 } 1090 1091 /* Returns the second earliest -deadline task, NULL otherwise */ 1092 static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu) 1093 { 1094 struct rb_node *next_node = rq->dl.rb_leftmost; 1095 struct sched_dl_entity *dl_se; 1096 struct task_struct *p = NULL; 1097 1098 next_node: 1099 next_node = rb_next(next_node); 1100 if (next_node) { 1101 dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node); 1102 p = dl_task_of(dl_se); 1103 1104 if (pick_dl_task(rq, p, cpu)) 1105 return p; 1106 1107 goto next_node; 1108 } 1109 1110 return NULL; 1111 } 1112 1113 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl); 1114 1115 static int find_later_rq(struct task_struct *task) 1116 { 1117 struct sched_domain *sd; 1118 struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl); 1119 int this_cpu = smp_processor_id(); 1120 int best_cpu, cpu = task_cpu(task); 1121 1122 /* Make sure the mask is initialized first */ 1123 if (unlikely(!later_mask)) 1124 return -1; 1125 1126 if (task->nr_cpus_allowed == 1) 1127 return -1; 1128 1129 best_cpu = cpudl_find(&task_rq(task)->rd->cpudl, 1130 task, later_mask); 1131 if (best_cpu == -1) 1132 return -1; 1133 1134 /* 1135 * If we are here, some target has been found, 1136 * the most suitable of which is cached in best_cpu. 1137 * This is, among the runqueues where the current tasks 1138 * have later deadlines than the task's one, the rq 1139 * with the latest possible one. 1140 * 1141 * Now we check how well this matches with task's 1142 * affinity and system topology. 1143 * 1144 * The last cpu where the task run is our first 1145 * guess, since it is most likely cache-hot there. 1146 */ 1147 if (cpumask_test_cpu(cpu, later_mask)) 1148 return cpu; 1149 /* 1150 * Check if this_cpu is to be skipped (i.e., it is 1151 * not in the mask) or not. 1152 */ 1153 if (!cpumask_test_cpu(this_cpu, later_mask)) 1154 this_cpu = -1; 1155 1156 rcu_read_lock(); 1157 for_each_domain(cpu, sd) { 1158 if (sd->flags & SD_WAKE_AFFINE) { 1159 1160 /* 1161 * If possible, preempting this_cpu is 1162 * cheaper than migrating. 1163 */ 1164 if (this_cpu != -1 && 1165 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { 1166 rcu_read_unlock(); 1167 return this_cpu; 1168 } 1169 1170 /* 1171 * Last chance: if best_cpu is valid and is 1172 * in the mask, that becomes our choice. 1173 */ 1174 if (best_cpu < nr_cpu_ids && 1175 cpumask_test_cpu(best_cpu, sched_domain_span(sd))) { 1176 rcu_read_unlock(); 1177 return best_cpu; 1178 } 1179 } 1180 } 1181 rcu_read_unlock(); 1182 1183 /* 1184 * At this point, all our guesses failed, we just return 1185 * 'something', and let the caller sort the things out. 1186 */ 1187 if (this_cpu != -1) 1188 return this_cpu; 1189 1190 cpu = cpumask_any(later_mask); 1191 if (cpu < nr_cpu_ids) 1192 return cpu; 1193 1194 return -1; 1195 } 1196 1197 /* Locks the rq it finds */ 1198 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq) 1199 { 1200 struct rq *later_rq = NULL; 1201 int tries; 1202 int cpu; 1203 1204 for (tries = 0; tries < DL_MAX_TRIES; tries++) { 1205 cpu = find_later_rq(task); 1206 1207 if ((cpu == -1) || (cpu == rq->cpu)) 1208 break; 1209 1210 later_rq = cpu_rq(cpu); 1211 1212 /* Retry if something changed. */ 1213 if (double_lock_balance(rq, later_rq)) { 1214 if (unlikely(task_rq(task) != rq || 1215 !cpumask_test_cpu(later_rq->cpu, 1216 &task->cpus_allowed) || 1217 task_running(rq, task) || !task->on_rq)) { 1218 double_unlock_balance(rq, later_rq); 1219 later_rq = NULL; 1220 break; 1221 } 1222 } 1223 1224 /* 1225 * If the rq we found has no -deadline task, or 1226 * its earliest one has a later deadline than our 1227 * task, the rq is a good one. 1228 */ 1229 if (!later_rq->dl.dl_nr_running || 1230 dl_time_before(task->dl.deadline, 1231 later_rq->dl.earliest_dl.curr)) 1232 break; 1233 1234 /* Otherwise we try again. */ 1235 double_unlock_balance(rq, later_rq); 1236 later_rq = NULL; 1237 } 1238 1239 return later_rq; 1240 } 1241 1242 static struct task_struct *pick_next_pushable_dl_task(struct rq *rq) 1243 { 1244 struct task_struct *p; 1245 1246 if (!has_pushable_dl_tasks(rq)) 1247 return NULL; 1248 1249 p = rb_entry(rq->dl.pushable_dl_tasks_leftmost, 1250 struct task_struct, pushable_dl_tasks); 1251 1252 BUG_ON(rq->cpu != task_cpu(p)); 1253 BUG_ON(task_current(rq, p)); 1254 BUG_ON(p->nr_cpus_allowed <= 1); 1255 1256 BUG_ON(!p->on_rq); 1257 BUG_ON(!dl_task(p)); 1258 1259 return p; 1260 } 1261 1262 /* 1263 * See if the non running -deadline tasks on this rq 1264 * can be sent to some other CPU where they can preempt 1265 * and start executing. 1266 */ 1267 static int push_dl_task(struct rq *rq) 1268 { 1269 struct task_struct *next_task; 1270 struct rq *later_rq; 1271 1272 if (!rq->dl.overloaded) 1273 return 0; 1274 1275 next_task = pick_next_pushable_dl_task(rq); 1276 if (!next_task) 1277 return 0; 1278 1279 retry: 1280 if (unlikely(next_task == rq->curr)) { 1281 WARN_ON(1); 1282 return 0; 1283 } 1284 1285 /* 1286 * If next_task preempts rq->curr, and rq->curr 1287 * can move away, it makes sense to just reschedule 1288 * without going further in pushing next_task. 1289 */ 1290 if (dl_task(rq->curr) && 1291 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) && 1292 rq->curr->nr_cpus_allowed > 1) { 1293 resched_task(rq->curr); 1294 return 0; 1295 } 1296 1297 /* We might release rq lock */ 1298 get_task_struct(next_task); 1299 1300 /* Will lock the rq it'll find */ 1301 later_rq = find_lock_later_rq(next_task, rq); 1302 if (!later_rq) { 1303 struct task_struct *task; 1304 1305 /* 1306 * We must check all this again, since 1307 * find_lock_later_rq releases rq->lock and it is 1308 * then possible that next_task has migrated. 1309 */ 1310 task = pick_next_pushable_dl_task(rq); 1311 if (task_cpu(next_task) == rq->cpu && task == next_task) { 1312 /* 1313 * The task is still there. We don't try 1314 * again, some other cpu will pull it when ready. 1315 */ 1316 dequeue_pushable_dl_task(rq, next_task); 1317 goto out; 1318 } 1319 1320 if (!task) 1321 /* No more tasks */ 1322 goto out; 1323 1324 put_task_struct(next_task); 1325 next_task = task; 1326 goto retry; 1327 } 1328 1329 deactivate_task(rq, next_task, 0); 1330 set_task_cpu(next_task, later_rq->cpu); 1331 activate_task(later_rq, next_task, 0); 1332 1333 resched_task(later_rq->curr); 1334 1335 double_unlock_balance(rq, later_rq); 1336 1337 out: 1338 put_task_struct(next_task); 1339 1340 return 1; 1341 } 1342 1343 static void push_dl_tasks(struct rq *rq) 1344 { 1345 /* Terminates as it moves a -deadline task */ 1346 while (push_dl_task(rq)) 1347 ; 1348 } 1349 1350 static int pull_dl_task(struct rq *this_rq) 1351 { 1352 int this_cpu = this_rq->cpu, ret = 0, cpu; 1353 struct task_struct *p; 1354 struct rq *src_rq; 1355 u64 dmin = LONG_MAX; 1356 1357 if (likely(!dl_overloaded(this_rq))) 1358 return 0; 1359 1360 /* 1361 * Match the barrier from dl_set_overloaded; this guarantees that if we 1362 * see overloaded we must also see the dlo_mask bit. 1363 */ 1364 smp_rmb(); 1365 1366 for_each_cpu(cpu, this_rq->rd->dlo_mask) { 1367 if (this_cpu == cpu) 1368 continue; 1369 1370 src_rq = cpu_rq(cpu); 1371 1372 /* 1373 * It looks racy, abd it is! However, as in sched_rt.c, 1374 * we are fine with this. 1375 */ 1376 if (this_rq->dl.dl_nr_running && 1377 dl_time_before(this_rq->dl.earliest_dl.curr, 1378 src_rq->dl.earliest_dl.next)) 1379 continue; 1380 1381 /* Might drop this_rq->lock */ 1382 double_lock_balance(this_rq, src_rq); 1383 1384 /* 1385 * If there are no more pullable tasks on the 1386 * rq, we're done with it. 1387 */ 1388 if (src_rq->dl.dl_nr_running <= 1) 1389 goto skip; 1390 1391 p = pick_next_earliest_dl_task(src_rq, this_cpu); 1392 1393 /* 1394 * We found a task to be pulled if: 1395 * - it preempts our current (if there's one), 1396 * - it will preempt the last one we pulled (if any). 1397 */ 1398 if (p && dl_time_before(p->dl.deadline, dmin) && 1399 (!this_rq->dl.dl_nr_running || 1400 dl_time_before(p->dl.deadline, 1401 this_rq->dl.earliest_dl.curr))) { 1402 WARN_ON(p == src_rq->curr); 1403 WARN_ON(!p->on_rq); 1404 1405 /* 1406 * Then we pull iff p has actually an earlier 1407 * deadline than the current task of its runqueue. 1408 */ 1409 if (dl_time_before(p->dl.deadline, 1410 src_rq->curr->dl.deadline)) 1411 goto skip; 1412 1413 ret = 1; 1414 1415 deactivate_task(src_rq, p, 0); 1416 set_task_cpu(p, this_cpu); 1417 activate_task(this_rq, p, 0); 1418 dmin = p->dl.deadline; 1419 1420 /* Is there any other task even earlier? */ 1421 } 1422 skip: 1423 double_unlock_balance(this_rq, src_rq); 1424 } 1425 1426 return ret; 1427 } 1428 1429 static void pre_schedule_dl(struct rq *rq, struct task_struct *prev) 1430 { 1431 /* Try to pull other tasks here */ 1432 if (dl_task(prev)) 1433 pull_dl_task(rq); 1434 } 1435 1436 static void post_schedule_dl(struct rq *rq) 1437 { 1438 push_dl_tasks(rq); 1439 } 1440 1441 /* 1442 * Since the task is not running and a reschedule is not going to happen 1443 * anytime soon on its runqueue, we try pushing it away now. 1444 */ 1445 static void task_woken_dl(struct rq *rq, struct task_struct *p) 1446 { 1447 if (!task_running(rq, p) && 1448 !test_tsk_need_resched(rq->curr) && 1449 has_pushable_dl_tasks(rq) && 1450 p->nr_cpus_allowed > 1 && 1451 dl_task(rq->curr) && 1452 (rq->curr->nr_cpus_allowed < 2 || 1453 dl_entity_preempt(&rq->curr->dl, &p->dl))) { 1454 push_dl_tasks(rq); 1455 } 1456 } 1457 1458 static void set_cpus_allowed_dl(struct task_struct *p, 1459 const struct cpumask *new_mask) 1460 { 1461 struct rq *rq; 1462 int weight; 1463 1464 BUG_ON(!dl_task(p)); 1465 1466 /* 1467 * Update only if the task is actually running (i.e., 1468 * it is on the rq AND it is not throttled). 1469 */ 1470 if (!on_dl_rq(&p->dl)) 1471 return; 1472 1473 weight = cpumask_weight(new_mask); 1474 1475 /* 1476 * Only update if the process changes its state from whether it 1477 * can migrate or not. 1478 */ 1479 if ((p->nr_cpus_allowed > 1) == (weight > 1)) 1480 return; 1481 1482 rq = task_rq(p); 1483 1484 /* 1485 * The process used to be able to migrate OR it can now migrate 1486 */ 1487 if (weight <= 1) { 1488 if (!task_current(rq, p)) 1489 dequeue_pushable_dl_task(rq, p); 1490 BUG_ON(!rq->dl.dl_nr_migratory); 1491 rq->dl.dl_nr_migratory--; 1492 } else { 1493 if (!task_current(rq, p)) 1494 enqueue_pushable_dl_task(rq, p); 1495 rq->dl.dl_nr_migratory++; 1496 } 1497 1498 update_dl_migration(&rq->dl); 1499 } 1500 1501 /* Assumes rq->lock is held */ 1502 static void rq_online_dl(struct rq *rq) 1503 { 1504 if (rq->dl.overloaded) 1505 dl_set_overload(rq); 1506 1507 if (rq->dl.dl_nr_running > 0) 1508 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr, 1); 1509 } 1510 1511 /* Assumes rq->lock is held */ 1512 static void rq_offline_dl(struct rq *rq) 1513 { 1514 if (rq->dl.overloaded) 1515 dl_clear_overload(rq); 1516 1517 cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0); 1518 } 1519 1520 void init_sched_dl_class(void) 1521 { 1522 unsigned int i; 1523 1524 for_each_possible_cpu(i) 1525 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i), 1526 GFP_KERNEL, cpu_to_node(i)); 1527 } 1528 1529 #endif /* CONFIG_SMP */ 1530 1531 static void switched_from_dl(struct rq *rq, struct task_struct *p) 1532 { 1533 if (hrtimer_active(&p->dl.dl_timer) && !dl_policy(p->policy)) 1534 hrtimer_try_to_cancel(&p->dl.dl_timer); 1535 1536 #ifdef CONFIG_SMP 1537 /* 1538 * Since this might be the only -deadline task on the rq, 1539 * this is the right place to try to pull some other one 1540 * from an overloaded cpu, if any. 1541 */ 1542 if (!rq->dl.dl_nr_running) 1543 pull_dl_task(rq); 1544 #endif 1545 } 1546 1547 /* 1548 * When switching to -deadline, we may overload the rq, then 1549 * we try to push someone off, if possible. 1550 */ 1551 static void switched_to_dl(struct rq *rq, struct task_struct *p) 1552 { 1553 int check_resched = 1; 1554 1555 /* 1556 * If p is throttled, don't consider the possibility 1557 * of preempting rq->curr, the check will be done right 1558 * after its runtime will get replenished. 1559 */ 1560 if (unlikely(p->dl.dl_throttled)) 1561 return; 1562 1563 if (p->on_rq || rq->curr != p) { 1564 #ifdef CONFIG_SMP 1565 if (rq->dl.overloaded && push_dl_task(rq) && rq != task_rq(p)) 1566 /* Only reschedule if pushing failed */ 1567 check_resched = 0; 1568 #endif /* CONFIG_SMP */ 1569 if (check_resched && task_has_dl_policy(rq->curr)) 1570 check_preempt_curr_dl(rq, p, 0); 1571 } 1572 } 1573 1574 /* 1575 * If the scheduling parameters of a -deadline task changed, 1576 * a push or pull operation might be needed. 1577 */ 1578 static void prio_changed_dl(struct rq *rq, struct task_struct *p, 1579 int oldprio) 1580 { 1581 if (p->on_rq || rq->curr == p) { 1582 #ifdef CONFIG_SMP 1583 /* 1584 * This might be too much, but unfortunately 1585 * we don't have the old deadline value, and 1586 * we can't argue if the task is increasing 1587 * or lowering its prio, so... 1588 */ 1589 if (!rq->dl.overloaded) 1590 pull_dl_task(rq); 1591 1592 /* 1593 * If we now have a earlier deadline task than p, 1594 * then reschedule, provided p is still on this 1595 * runqueue. 1596 */ 1597 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) && 1598 rq->curr == p) 1599 resched_task(p); 1600 #else 1601 /* 1602 * Again, we don't know if p has a earlier 1603 * or later deadline, so let's blindly set a 1604 * (maybe not needed) rescheduling point. 1605 */ 1606 resched_task(p); 1607 #endif /* CONFIG_SMP */ 1608 } else 1609 switched_to_dl(rq, p); 1610 } 1611 1612 const struct sched_class dl_sched_class = { 1613 .next = &rt_sched_class, 1614 .enqueue_task = enqueue_task_dl, 1615 .dequeue_task = dequeue_task_dl, 1616 .yield_task = yield_task_dl, 1617 1618 .check_preempt_curr = check_preempt_curr_dl, 1619 1620 .pick_next_task = pick_next_task_dl, 1621 .put_prev_task = put_prev_task_dl, 1622 1623 #ifdef CONFIG_SMP 1624 .select_task_rq = select_task_rq_dl, 1625 .set_cpus_allowed = set_cpus_allowed_dl, 1626 .rq_online = rq_online_dl, 1627 .rq_offline = rq_offline_dl, 1628 .pre_schedule = pre_schedule_dl, 1629 .post_schedule = post_schedule_dl, 1630 .task_woken = task_woken_dl, 1631 #endif 1632 1633 .set_curr_task = set_curr_task_dl, 1634 .task_tick = task_tick_dl, 1635 .task_fork = task_fork_dl, 1636 .task_dead = task_dead_dl, 1637 1638 .prio_changed = prio_changed_dl, 1639 .switched_from = switched_from_dl, 1640 .switched_to = switched_to_dl, 1641 }; 1642