1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 #include <linux/sched.h> 4 #include <linux/sched/autogroup.h> 5 #include <linux/sched/sysctl.h> 6 #include <linux/sched/topology.h> 7 #include <linux/sched/rt.h> 8 #include <linux/sched/deadline.h> 9 #include <linux/sched/clock.h> 10 #include <linux/sched/wake_q.h> 11 #include <linux/sched/signal.h> 12 #include <linux/sched/numa_balancing.h> 13 #include <linux/sched/mm.h> 14 #include <linux/sched/cpufreq.h> 15 #include <linux/sched/stat.h> 16 #include <linux/sched/nohz.h> 17 #include <linux/sched/debug.h> 18 #include <linux/sched/hotplug.h> 19 #include <linux/sched/task.h> 20 #include <linux/sched/task_stack.h> 21 #include <linux/sched/cputime.h> 22 #include <linux/sched/init.h> 23 24 #include <linux/u64_stats_sync.h> 25 #include <linux/kernel_stat.h> 26 #include <linux/binfmts.h> 27 #include <linux/mutex.h> 28 #include <linux/spinlock.h> 29 #include <linux/stop_machine.h> 30 #include <linux/irq_work.h> 31 #include <linux/tick.h> 32 #include <linux/slab.h> 33 #include <linux/cgroup.h> 34 35 #ifdef CONFIG_PARAVIRT 36 #include <asm/paravirt.h> 37 #endif 38 39 #include "cpupri.h" 40 #include "cpudeadline.h" 41 42 #ifdef CONFIG_SCHED_DEBUG 43 # define SCHED_WARN_ON(x) WARN_ONCE(x, #x) 44 #else 45 # define SCHED_WARN_ON(x) ({ (void)(x), 0; }) 46 #endif 47 48 struct rq; 49 struct cpuidle_state; 50 51 /* task_struct::on_rq states: */ 52 #define TASK_ON_RQ_QUEUED 1 53 #define TASK_ON_RQ_MIGRATING 2 54 55 extern __read_mostly int scheduler_running; 56 57 extern unsigned long calc_load_update; 58 extern atomic_long_t calc_load_tasks; 59 60 extern void calc_global_load_tick(struct rq *this_rq); 61 extern long calc_load_fold_active(struct rq *this_rq, long adjust); 62 63 #ifdef CONFIG_SMP 64 extern void cpu_load_update_active(struct rq *this_rq); 65 #else 66 static inline void cpu_load_update_active(struct rq *this_rq) { } 67 #endif 68 69 /* 70 * Helpers for converting nanosecond timing to jiffy resolution 71 */ 72 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) 73 74 /* 75 * Increase resolution of nice-level calculations for 64-bit architectures. 76 * The extra resolution improves shares distribution and load balancing of 77 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup 78 * hierarchies, especially on larger systems. This is not a user-visible change 79 * and does not change the user-interface for setting shares/weights. 80 * 81 * We increase resolution only if we have enough bits to allow this increased 82 * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are 83 * pretty high and the returns do not justify the increased costs. 84 * 85 * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to 86 * increase coverage and consistency always enable it on 64bit platforms. 87 */ 88 #ifdef CONFIG_64BIT 89 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT) 90 # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT) 91 # define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT) 92 #else 93 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT) 94 # define scale_load(w) (w) 95 # define scale_load_down(w) (w) 96 #endif 97 98 /* 99 * Task weight (visible to users) and its load (invisible to users) have 100 * independent resolution, but they should be well calibrated. We use 101 * scale_load() and scale_load_down(w) to convert between them. The 102 * following must be true: 103 * 104 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD 105 * 106 */ 107 #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT) 108 109 /* 110 * Single value that decides SCHED_DEADLINE internal math precision. 111 * 10 -> just above 1us 112 * 9 -> just above 0.5us 113 */ 114 #define DL_SCALE (10) 115 116 /* 117 * These are the 'tuning knobs' of the scheduler: 118 */ 119 120 /* 121 * single value that denotes runtime == period, ie unlimited time. 122 */ 123 #define RUNTIME_INF ((u64)~0ULL) 124 125 static inline int idle_policy(int policy) 126 { 127 return policy == SCHED_IDLE; 128 } 129 static inline int fair_policy(int policy) 130 { 131 return policy == SCHED_NORMAL || policy == SCHED_BATCH; 132 } 133 134 static inline int rt_policy(int policy) 135 { 136 return policy == SCHED_FIFO || policy == SCHED_RR; 137 } 138 139 static inline int dl_policy(int policy) 140 { 141 return policy == SCHED_DEADLINE; 142 } 143 static inline bool valid_policy(int policy) 144 { 145 return idle_policy(policy) || fair_policy(policy) || 146 rt_policy(policy) || dl_policy(policy); 147 } 148 149 static inline int task_has_rt_policy(struct task_struct *p) 150 { 151 return rt_policy(p->policy); 152 } 153 154 static inline int task_has_dl_policy(struct task_struct *p) 155 { 156 return dl_policy(p->policy); 157 } 158 159 #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) 160 161 /* 162 * !! For sched_setattr_nocheck() (kernel) only !! 163 * 164 * This is actually gross. :( 165 * 166 * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE 167 * tasks, but still be able to sleep. We need this on platforms that cannot 168 * atomically change clock frequency. Remove once fast switching will be 169 * available on such platforms. 170 * 171 * SUGOV stands for SchedUtil GOVernor. 172 */ 173 #define SCHED_FLAG_SUGOV 0x10000000 174 175 static inline bool dl_entity_is_special(struct sched_dl_entity *dl_se) 176 { 177 #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL 178 return unlikely(dl_se->flags & SCHED_FLAG_SUGOV); 179 #else 180 return false; 181 #endif 182 } 183 184 /* 185 * Tells if entity @a should preempt entity @b. 186 */ 187 static inline bool 188 dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b) 189 { 190 return dl_entity_is_special(a) || 191 dl_time_before(a->deadline, b->deadline); 192 } 193 194 /* 195 * This is the priority-queue data structure of the RT scheduling class: 196 */ 197 struct rt_prio_array { 198 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ 199 struct list_head queue[MAX_RT_PRIO]; 200 }; 201 202 struct rt_bandwidth { 203 /* nests inside the rq lock: */ 204 raw_spinlock_t rt_runtime_lock; 205 ktime_t rt_period; 206 u64 rt_runtime; 207 struct hrtimer rt_period_timer; 208 unsigned int rt_period_active; 209 }; 210 211 void __dl_clear_params(struct task_struct *p); 212 213 /* 214 * To keep the bandwidth of -deadline tasks and groups under control 215 * we need some place where: 216 * - store the maximum -deadline bandwidth of the system (the group); 217 * - cache the fraction of that bandwidth that is currently allocated. 218 * 219 * This is all done in the data structure below. It is similar to the 220 * one used for RT-throttling (rt_bandwidth), with the main difference 221 * that, since here we are only interested in admission control, we 222 * do not decrease any runtime while the group "executes", neither we 223 * need a timer to replenish it. 224 * 225 * With respect to SMP, the bandwidth is given on a per-CPU basis, 226 * meaning that: 227 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU; 228 * - dl_total_bw array contains, in the i-eth element, the currently 229 * allocated bandwidth on the i-eth CPU. 230 * Moreover, groups consume bandwidth on each CPU, while tasks only 231 * consume bandwidth on the CPU they're running on. 232 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw 233 * that will be shown the next time the proc or cgroup controls will 234 * be red. It on its turn can be changed by writing on its own 235 * control. 236 */ 237 struct dl_bandwidth { 238 raw_spinlock_t dl_runtime_lock; 239 u64 dl_runtime; 240 u64 dl_period; 241 }; 242 243 static inline int dl_bandwidth_enabled(void) 244 { 245 return sysctl_sched_rt_runtime >= 0; 246 } 247 248 struct dl_bw { 249 raw_spinlock_t lock; 250 u64 bw, total_bw; 251 }; 252 253 static inline void __dl_update(struct dl_bw *dl_b, s64 bw); 254 255 static inline 256 void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus) 257 { 258 dl_b->total_bw -= tsk_bw; 259 __dl_update(dl_b, (s32)tsk_bw / cpus); 260 } 261 262 static inline 263 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus) 264 { 265 dl_b->total_bw += tsk_bw; 266 __dl_update(dl_b, -((s32)tsk_bw / cpus)); 267 } 268 269 static inline 270 bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw) 271 { 272 return dl_b->bw != -1 && 273 dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw; 274 } 275 276 void dl_change_utilization(struct task_struct *p, u64 new_bw); 277 extern void init_dl_bw(struct dl_bw *dl_b); 278 extern int sched_dl_global_validate(void); 279 extern void sched_dl_do_global(void); 280 extern int sched_dl_overflow(struct task_struct *p, int policy, 281 const struct sched_attr *attr); 282 extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr); 283 extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr); 284 extern bool __checkparam_dl(const struct sched_attr *attr); 285 extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr); 286 extern int dl_task_can_attach(struct task_struct *p, 287 const struct cpumask *cs_cpus_allowed); 288 extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, 289 const struct cpumask *trial); 290 extern bool dl_cpu_busy(unsigned int cpu); 291 292 #ifdef CONFIG_CGROUP_SCHED 293 294 #include <linux/cgroup.h> 295 296 struct cfs_rq; 297 struct rt_rq; 298 299 extern struct list_head task_groups; 300 301 struct cfs_bandwidth { 302 #ifdef CONFIG_CFS_BANDWIDTH 303 raw_spinlock_t lock; 304 ktime_t period; 305 u64 quota, runtime; 306 s64 hierarchical_quota; 307 u64 runtime_expires; 308 309 int idle, period_active; 310 struct hrtimer period_timer, slack_timer; 311 struct list_head throttled_cfs_rq; 312 313 /* statistics */ 314 int nr_periods, nr_throttled; 315 u64 throttled_time; 316 #endif 317 }; 318 319 /* task group related information */ 320 struct task_group { 321 struct cgroup_subsys_state css; 322 323 #ifdef CONFIG_FAIR_GROUP_SCHED 324 /* schedulable entities of this group on each cpu */ 325 struct sched_entity **se; 326 /* runqueue "owned" by this group on each cpu */ 327 struct cfs_rq **cfs_rq; 328 unsigned long shares; 329 330 #ifdef CONFIG_SMP 331 /* 332 * load_avg can be heavily contended at clock tick time, so put 333 * it in its own cacheline separated from the fields above which 334 * will also be accessed at each tick. 335 */ 336 atomic_long_t load_avg ____cacheline_aligned; 337 #endif 338 #endif 339 340 #ifdef CONFIG_RT_GROUP_SCHED 341 struct sched_rt_entity **rt_se; 342 struct rt_rq **rt_rq; 343 344 struct rt_bandwidth rt_bandwidth; 345 #endif 346 347 struct rcu_head rcu; 348 struct list_head list; 349 350 struct task_group *parent; 351 struct list_head siblings; 352 struct list_head children; 353 354 #ifdef CONFIG_SCHED_AUTOGROUP 355 struct autogroup *autogroup; 356 #endif 357 358 struct cfs_bandwidth cfs_bandwidth; 359 }; 360 361 #ifdef CONFIG_FAIR_GROUP_SCHED 362 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD 363 364 /* 365 * A weight of 0 or 1 can cause arithmetics problems. 366 * A weight of a cfs_rq is the sum of weights of which entities 367 * are queued on this cfs_rq, so a weight of a entity should not be 368 * too large, so as the shares value of a task group. 369 * (The default weight is 1024 - so there's no practical 370 * limitation from this.) 371 */ 372 #define MIN_SHARES (1UL << 1) 373 #define MAX_SHARES (1UL << 18) 374 #endif 375 376 typedef int (*tg_visitor)(struct task_group *, void *); 377 378 extern int walk_tg_tree_from(struct task_group *from, 379 tg_visitor down, tg_visitor up, void *data); 380 381 /* 382 * Iterate the full tree, calling @down when first entering a node and @up when 383 * leaving it for the final time. 384 * 385 * Caller must hold rcu_lock or sufficient equivalent. 386 */ 387 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) 388 { 389 return walk_tg_tree_from(&root_task_group, down, up, data); 390 } 391 392 extern int tg_nop(struct task_group *tg, void *data); 393 394 extern void free_fair_sched_group(struct task_group *tg); 395 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent); 396 extern void online_fair_sched_group(struct task_group *tg); 397 extern void unregister_fair_sched_group(struct task_group *tg); 398 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, 399 struct sched_entity *se, int cpu, 400 struct sched_entity *parent); 401 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b); 402 403 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b); 404 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b); 405 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq); 406 407 extern void free_rt_sched_group(struct task_group *tg); 408 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent); 409 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, 410 struct sched_rt_entity *rt_se, int cpu, 411 struct sched_rt_entity *parent); 412 extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us); 413 extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us); 414 extern long sched_group_rt_runtime(struct task_group *tg); 415 extern long sched_group_rt_period(struct task_group *tg); 416 extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk); 417 418 extern struct task_group *sched_create_group(struct task_group *parent); 419 extern void sched_online_group(struct task_group *tg, 420 struct task_group *parent); 421 extern void sched_destroy_group(struct task_group *tg); 422 extern void sched_offline_group(struct task_group *tg); 423 424 extern void sched_move_task(struct task_struct *tsk); 425 426 #ifdef CONFIG_FAIR_GROUP_SCHED 427 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); 428 429 #ifdef CONFIG_SMP 430 extern void set_task_rq_fair(struct sched_entity *se, 431 struct cfs_rq *prev, struct cfs_rq *next); 432 #else /* !CONFIG_SMP */ 433 static inline void set_task_rq_fair(struct sched_entity *se, 434 struct cfs_rq *prev, struct cfs_rq *next) { } 435 #endif /* CONFIG_SMP */ 436 #endif /* CONFIG_FAIR_GROUP_SCHED */ 437 438 #else /* CONFIG_CGROUP_SCHED */ 439 440 struct cfs_bandwidth { }; 441 442 #endif /* CONFIG_CGROUP_SCHED */ 443 444 /* CFS-related fields in a runqueue */ 445 struct cfs_rq { 446 struct load_weight load; 447 unsigned long runnable_weight; 448 unsigned int nr_running, h_nr_running; 449 450 u64 exec_clock; 451 u64 min_vruntime; 452 #ifndef CONFIG_64BIT 453 u64 min_vruntime_copy; 454 #endif 455 456 struct rb_root_cached tasks_timeline; 457 458 /* 459 * 'curr' points to currently running entity on this cfs_rq. 460 * It is set to NULL otherwise (i.e when none are currently running). 461 */ 462 struct sched_entity *curr, *next, *last, *skip; 463 464 #ifdef CONFIG_SCHED_DEBUG 465 unsigned int nr_spread_over; 466 #endif 467 468 #ifdef CONFIG_SMP 469 /* 470 * CFS load tracking 471 */ 472 struct sched_avg avg; 473 #ifndef CONFIG_64BIT 474 u64 load_last_update_time_copy; 475 #endif 476 struct { 477 raw_spinlock_t lock ____cacheline_aligned; 478 int nr; 479 unsigned long load_avg; 480 unsigned long util_avg; 481 unsigned long runnable_sum; 482 } removed; 483 484 #ifdef CONFIG_FAIR_GROUP_SCHED 485 unsigned long tg_load_avg_contrib; 486 long propagate; 487 long prop_runnable_sum; 488 489 /* 490 * h_load = weight * f(tg) 491 * 492 * Where f(tg) is the recursive weight fraction assigned to 493 * this group. 494 */ 495 unsigned long h_load; 496 u64 last_h_load_update; 497 struct sched_entity *h_load_next; 498 #endif /* CONFIG_FAIR_GROUP_SCHED */ 499 #endif /* CONFIG_SMP */ 500 501 #ifdef CONFIG_FAIR_GROUP_SCHED 502 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ 503 504 /* 505 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in 506 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities 507 * (like users, containers etc.) 508 * 509 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This 510 * list is used during load balance. 511 */ 512 int on_list; 513 struct list_head leaf_cfs_rq_list; 514 struct task_group *tg; /* group that "owns" this runqueue */ 515 516 #ifdef CONFIG_CFS_BANDWIDTH 517 int runtime_enabled; 518 u64 runtime_expires; 519 s64 runtime_remaining; 520 521 u64 throttled_clock, throttled_clock_task; 522 u64 throttled_clock_task_time; 523 int throttled, throttle_count; 524 struct list_head throttled_list; 525 #endif /* CONFIG_CFS_BANDWIDTH */ 526 #endif /* CONFIG_FAIR_GROUP_SCHED */ 527 }; 528 529 static inline int rt_bandwidth_enabled(void) 530 { 531 return sysctl_sched_rt_runtime >= 0; 532 } 533 534 /* RT IPI pull logic requires IRQ_WORK */ 535 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP) 536 # define HAVE_RT_PUSH_IPI 537 #endif 538 539 /* Real-Time classes' related field in a runqueue: */ 540 struct rt_rq { 541 struct rt_prio_array active; 542 unsigned int rt_nr_running; 543 unsigned int rr_nr_running; 544 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED 545 struct { 546 int curr; /* highest queued rt task prio */ 547 #ifdef CONFIG_SMP 548 int next; /* next highest */ 549 #endif 550 } highest_prio; 551 #endif 552 #ifdef CONFIG_SMP 553 unsigned long rt_nr_migratory; 554 unsigned long rt_nr_total; 555 int overloaded; 556 struct plist_head pushable_tasks; 557 #endif /* CONFIG_SMP */ 558 int rt_queued; 559 560 int rt_throttled; 561 u64 rt_time; 562 u64 rt_runtime; 563 /* Nests inside the rq lock: */ 564 raw_spinlock_t rt_runtime_lock; 565 566 #ifdef CONFIG_RT_GROUP_SCHED 567 unsigned long rt_nr_boosted; 568 569 struct rq *rq; 570 struct task_group *tg; 571 #endif 572 }; 573 574 /* Deadline class' related fields in a runqueue */ 575 struct dl_rq { 576 /* runqueue is an rbtree, ordered by deadline */ 577 struct rb_root_cached root; 578 579 unsigned long dl_nr_running; 580 581 #ifdef CONFIG_SMP 582 /* 583 * Deadline values of the currently executing and the 584 * earliest ready task on this rq. Caching these facilitates 585 * the decision wether or not a ready but not running task 586 * should migrate somewhere else. 587 */ 588 struct { 589 u64 curr; 590 u64 next; 591 } earliest_dl; 592 593 unsigned long dl_nr_migratory; 594 int overloaded; 595 596 /* 597 * Tasks on this rq that can be pushed away. They are kept in 598 * an rb-tree, ordered by tasks' deadlines, with caching 599 * of the leftmost (earliest deadline) element. 600 */ 601 struct rb_root_cached pushable_dl_tasks_root; 602 #else 603 struct dl_bw dl_bw; 604 #endif 605 /* 606 * "Active utilization" for this runqueue: increased when a 607 * task wakes up (becomes TASK_RUNNING) and decreased when a 608 * task blocks 609 */ 610 u64 running_bw; 611 612 /* 613 * Utilization of the tasks "assigned" to this runqueue (including 614 * the tasks that are in runqueue and the tasks that executed on this 615 * CPU and blocked). Increased when a task moves to this runqueue, and 616 * decreased when the task moves away (migrates, changes scheduling 617 * policy, or terminates). 618 * This is needed to compute the "inactive utilization" for the 619 * runqueue (inactive utilization = this_bw - running_bw). 620 */ 621 u64 this_bw; 622 u64 extra_bw; 623 624 /* 625 * Inverse of the fraction of CPU utilization that can be reclaimed 626 * by the GRUB algorithm. 627 */ 628 u64 bw_ratio; 629 }; 630 631 #ifdef CONFIG_SMP 632 633 static inline bool sched_asym_prefer(int a, int b) 634 { 635 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b); 636 } 637 638 /* 639 * We add the notion of a root-domain which will be used to define per-domain 640 * variables. Each exclusive cpuset essentially defines an island domain by 641 * fully partitioning the member cpus from any other cpuset. Whenever a new 642 * exclusive cpuset is created, we also create and attach a new root-domain 643 * object. 644 * 645 */ 646 struct root_domain { 647 atomic_t refcount; 648 atomic_t rto_count; 649 struct rcu_head rcu; 650 cpumask_var_t span; 651 cpumask_var_t online; 652 653 /* Indicate more than one runnable task for any CPU */ 654 bool overload; 655 656 /* 657 * The bit corresponding to a CPU gets set here if such CPU has more 658 * than one runnable -deadline task (as it is below for RT tasks). 659 */ 660 cpumask_var_t dlo_mask; 661 atomic_t dlo_count; 662 struct dl_bw dl_bw; 663 struct cpudl cpudl; 664 665 #ifdef HAVE_RT_PUSH_IPI 666 /* 667 * For IPI pull requests, loop across the rto_mask. 668 */ 669 struct irq_work rto_push_work; 670 raw_spinlock_t rto_lock; 671 /* These are only updated and read within rto_lock */ 672 int rto_loop; 673 int rto_cpu; 674 /* These atomics are updated outside of a lock */ 675 atomic_t rto_loop_next; 676 atomic_t rto_loop_start; 677 #endif 678 /* 679 * The "RT overload" flag: it gets set if a CPU has more than 680 * one runnable RT task. 681 */ 682 cpumask_var_t rto_mask; 683 struct cpupri cpupri; 684 685 unsigned long max_cpu_capacity; 686 }; 687 688 extern struct root_domain def_root_domain; 689 extern struct mutex sched_domains_mutex; 690 691 extern void init_defrootdomain(void); 692 extern int sched_init_domains(const struct cpumask *cpu_map); 693 extern void rq_attach_root(struct rq *rq, struct root_domain *rd); 694 extern void sched_get_rd(struct root_domain *rd); 695 extern void sched_put_rd(struct root_domain *rd); 696 697 #ifdef HAVE_RT_PUSH_IPI 698 extern void rto_push_irq_work_func(struct irq_work *work); 699 #endif 700 #endif /* CONFIG_SMP */ 701 702 /* 703 * This is the main, per-CPU runqueue data structure. 704 * 705 * Locking rule: those places that want to lock multiple runqueues 706 * (such as the load balancing or the thread migration code), lock 707 * acquire operations must be ordered by ascending &runqueue. 708 */ 709 struct rq { 710 /* runqueue lock: */ 711 raw_spinlock_t lock; 712 713 /* 714 * nr_running and cpu_load should be in the same cacheline because 715 * remote CPUs use both these fields when doing load calculation. 716 */ 717 unsigned int nr_running; 718 #ifdef CONFIG_NUMA_BALANCING 719 unsigned int nr_numa_running; 720 unsigned int nr_preferred_running; 721 #endif 722 #define CPU_LOAD_IDX_MAX 5 723 unsigned long cpu_load[CPU_LOAD_IDX_MAX]; 724 #ifdef CONFIG_NO_HZ_COMMON 725 #ifdef CONFIG_SMP 726 unsigned long last_load_update_tick; 727 #endif /* CONFIG_SMP */ 728 unsigned long nohz_flags; 729 #endif /* CONFIG_NO_HZ_COMMON */ 730 #ifdef CONFIG_NO_HZ_FULL 731 unsigned long last_sched_tick; 732 #endif 733 /* capture load from *all* tasks on this cpu: */ 734 struct load_weight load; 735 unsigned long nr_load_updates; 736 u64 nr_switches; 737 738 struct cfs_rq cfs; 739 struct rt_rq rt; 740 struct dl_rq dl; 741 742 #ifdef CONFIG_FAIR_GROUP_SCHED 743 /* list of leaf cfs_rq on this cpu: */ 744 struct list_head leaf_cfs_rq_list; 745 struct list_head *tmp_alone_branch; 746 #endif /* CONFIG_FAIR_GROUP_SCHED */ 747 748 /* 749 * This is part of a global counter where only the total sum 750 * over all CPUs matters. A task can increase this counter on 751 * one CPU and if it got migrated afterwards it may decrease 752 * it on another CPU. Always updated under the runqueue lock: 753 */ 754 unsigned long nr_uninterruptible; 755 756 struct task_struct *curr, *idle, *stop; 757 unsigned long next_balance; 758 struct mm_struct *prev_mm; 759 760 unsigned int clock_update_flags; 761 u64 clock; 762 u64 clock_task; 763 764 atomic_t nr_iowait; 765 766 #ifdef CONFIG_SMP 767 struct root_domain *rd; 768 struct sched_domain *sd; 769 770 unsigned long cpu_capacity; 771 unsigned long cpu_capacity_orig; 772 773 struct callback_head *balance_callback; 774 775 unsigned char idle_balance; 776 /* For active balancing */ 777 int active_balance; 778 int push_cpu; 779 struct cpu_stop_work active_balance_work; 780 /* cpu of this runqueue: */ 781 int cpu; 782 int online; 783 784 struct list_head cfs_tasks; 785 786 u64 rt_avg; 787 u64 age_stamp; 788 u64 idle_stamp; 789 u64 avg_idle; 790 791 /* This is used to determine avg_idle's max value */ 792 u64 max_idle_balance_cost; 793 #endif 794 795 #ifdef CONFIG_IRQ_TIME_ACCOUNTING 796 u64 prev_irq_time; 797 #endif 798 #ifdef CONFIG_PARAVIRT 799 u64 prev_steal_time; 800 #endif 801 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING 802 u64 prev_steal_time_rq; 803 #endif 804 805 /* calc_load related fields */ 806 unsigned long calc_load_update; 807 long calc_load_active; 808 809 #ifdef CONFIG_SCHED_HRTICK 810 #ifdef CONFIG_SMP 811 int hrtick_csd_pending; 812 call_single_data_t hrtick_csd; 813 #endif 814 struct hrtimer hrtick_timer; 815 #endif 816 817 #ifdef CONFIG_SCHEDSTATS 818 /* latency stats */ 819 struct sched_info rq_sched_info; 820 unsigned long long rq_cpu_time; 821 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ 822 823 /* sys_sched_yield() stats */ 824 unsigned int yld_count; 825 826 /* schedule() stats */ 827 unsigned int sched_count; 828 unsigned int sched_goidle; 829 830 /* try_to_wake_up() stats */ 831 unsigned int ttwu_count; 832 unsigned int ttwu_local; 833 #endif 834 835 #ifdef CONFIG_SMP 836 struct llist_head wake_list; 837 #endif 838 839 #ifdef CONFIG_CPU_IDLE 840 /* Must be inspected within a rcu lock section */ 841 struct cpuidle_state *idle_state; 842 #endif 843 }; 844 845 static inline int cpu_of(struct rq *rq) 846 { 847 #ifdef CONFIG_SMP 848 return rq->cpu; 849 #else 850 return 0; 851 #endif 852 } 853 854 855 #ifdef CONFIG_SCHED_SMT 856 857 extern struct static_key_false sched_smt_present; 858 859 extern void __update_idle_core(struct rq *rq); 860 861 static inline void update_idle_core(struct rq *rq) 862 { 863 if (static_branch_unlikely(&sched_smt_present)) 864 __update_idle_core(rq); 865 } 866 867 #else 868 static inline void update_idle_core(struct rq *rq) { } 869 #endif 870 871 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); 872 873 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) 874 #define this_rq() this_cpu_ptr(&runqueues) 875 #define task_rq(p) cpu_rq(task_cpu(p)) 876 #define cpu_curr(cpu) (cpu_rq(cpu)->curr) 877 #define raw_rq() raw_cpu_ptr(&runqueues) 878 879 static inline u64 __rq_clock_broken(struct rq *rq) 880 { 881 return READ_ONCE(rq->clock); 882 } 883 884 /* 885 * rq::clock_update_flags bits 886 * 887 * %RQCF_REQ_SKIP - will request skipping of clock update on the next 888 * call to __schedule(). This is an optimisation to avoid 889 * neighbouring rq clock updates. 890 * 891 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is 892 * in effect and calls to update_rq_clock() are being ignored. 893 * 894 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been 895 * made to update_rq_clock() since the last time rq::lock was pinned. 896 * 897 * If inside of __schedule(), clock_update_flags will have been 898 * shifted left (a left shift is a cheap operation for the fast path 899 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use, 900 * 901 * if (rq-clock_update_flags >= RQCF_UPDATED) 902 * 903 * to check if %RQCF_UPADTED is set. It'll never be shifted more than 904 * one position though, because the next rq_unpin_lock() will shift it 905 * back. 906 */ 907 #define RQCF_REQ_SKIP 0x01 908 #define RQCF_ACT_SKIP 0x02 909 #define RQCF_UPDATED 0x04 910 911 static inline void assert_clock_updated(struct rq *rq) 912 { 913 /* 914 * The only reason for not seeing a clock update since the 915 * last rq_pin_lock() is if we're currently skipping updates. 916 */ 917 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP); 918 } 919 920 static inline u64 rq_clock(struct rq *rq) 921 { 922 lockdep_assert_held(&rq->lock); 923 assert_clock_updated(rq); 924 925 return rq->clock; 926 } 927 928 static inline u64 rq_clock_task(struct rq *rq) 929 { 930 lockdep_assert_held(&rq->lock); 931 assert_clock_updated(rq); 932 933 return rq->clock_task; 934 } 935 936 static inline void rq_clock_skip_update(struct rq *rq, bool skip) 937 { 938 lockdep_assert_held(&rq->lock); 939 if (skip) 940 rq->clock_update_flags |= RQCF_REQ_SKIP; 941 else 942 rq->clock_update_flags &= ~RQCF_REQ_SKIP; 943 } 944 945 struct rq_flags { 946 unsigned long flags; 947 struct pin_cookie cookie; 948 #ifdef CONFIG_SCHED_DEBUG 949 /* 950 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the 951 * current pin context is stashed here in case it needs to be 952 * restored in rq_repin_lock(). 953 */ 954 unsigned int clock_update_flags; 955 #endif 956 }; 957 958 static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf) 959 { 960 rf->cookie = lockdep_pin_lock(&rq->lock); 961 962 #ifdef CONFIG_SCHED_DEBUG 963 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); 964 rf->clock_update_flags = 0; 965 #endif 966 } 967 968 static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf) 969 { 970 #ifdef CONFIG_SCHED_DEBUG 971 if (rq->clock_update_flags > RQCF_ACT_SKIP) 972 rf->clock_update_flags = RQCF_UPDATED; 973 #endif 974 975 lockdep_unpin_lock(&rq->lock, rf->cookie); 976 } 977 978 static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf) 979 { 980 lockdep_repin_lock(&rq->lock, rf->cookie); 981 982 #ifdef CONFIG_SCHED_DEBUG 983 /* 984 * Restore the value we stashed in @rf for this pin context. 985 */ 986 rq->clock_update_flags |= rf->clock_update_flags; 987 #endif 988 } 989 990 #ifdef CONFIG_NUMA 991 enum numa_topology_type { 992 NUMA_DIRECT, 993 NUMA_GLUELESS_MESH, 994 NUMA_BACKPLANE, 995 }; 996 extern enum numa_topology_type sched_numa_topology_type; 997 extern int sched_max_numa_distance; 998 extern bool find_numa_distance(int distance); 999 #endif 1000 1001 #ifdef CONFIG_NUMA 1002 extern void sched_init_numa(void); 1003 extern void sched_domains_numa_masks_set(unsigned int cpu); 1004 extern void sched_domains_numa_masks_clear(unsigned int cpu); 1005 #else 1006 static inline void sched_init_numa(void) { } 1007 static inline void sched_domains_numa_masks_set(unsigned int cpu) { } 1008 static inline void sched_domains_numa_masks_clear(unsigned int cpu) { } 1009 #endif 1010 1011 #ifdef CONFIG_NUMA_BALANCING 1012 /* The regions in numa_faults array from task_struct */ 1013 enum numa_faults_stats { 1014 NUMA_MEM = 0, 1015 NUMA_CPU, 1016 NUMA_MEMBUF, 1017 NUMA_CPUBUF 1018 }; 1019 extern void sched_setnuma(struct task_struct *p, int node); 1020 extern int migrate_task_to(struct task_struct *p, int cpu); 1021 extern int migrate_swap(struct task_struct *, struct task_struct *); 1022 #endif /* CONFIG_NUMA_BALANCING */ 1023 1024 #ifdef CONFIG_SMP 1025 1026 static inline void 1027 queue_balance_callback(struct rq *rq, 1028 struct callback_head *head, 1029 void (*func)(struct rq *rq)) 1030 { 1031 lockdep_assert_held(&rq->lock); 1032 1033 if (unlikely(head->next)) 1034 return; 1035 1036 head->func = (void (*)(struct callback_head *))func; 1037 head->next = rq->balance_callback; 1038 rq->balance_callback = head; 1039 } 1040 1041 extern void sched_ttwu_pending(void); 1042 1043 #define rcu_dereference_check_sched_domain(p) \ 1044 rcu_dereference_check((p), \ 1045 lockdep_is_held(&sched_domains_mutex)) 1046 1047 /* 1048 * The domain tree (rq->sd) is protected by RCU's quiescent state transition. 1049 * See detach_destroy_domains: synchronize_sched for details. 1050 * 1051 * The domain tree of any CPU may only be accessed from within 1052 * preempt-disabled sections. 1053 */ 1054 #define for_each_domain(cpu, __sd) \ 1055 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \ 1056 __sd; __sd = __sd->parent) 1057 1058 #define for_each_lower_domain(sd) for (; sd; sd = sd->child) 1059 1060 /** 1061 * highest_flag_domain - Return highest sched_domain containing flag. 1062 * @cpu: The cpu whose highest level of sched domain is to 1063 * be returned. 1064 * @flag: The flag to check for the highest sched_domain 1065 * for the given cpu. 1066 * 1067 * Returns the highest sched_domain of a cpu which contains the given flag. 1068 */ 1069 static inline struct sched_domain *highest_flag_domain(int cpu, int flag) 1070 { 1071 struct sched_domain *sd, *hsd = NULL; 1072 1073 for_each_domain(cpu, sd) { 1074 if (!(sd->flags & flag)) 1075 break; 1076 hsd = sd; 1077 } 1078 1079 return hsd; 1080 } 1081 1082 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) 1083 { 1084 struct sched_domain *sd; 1085 1086 for_each_domain(cpu, sd) { 1087 if (sd->flags & flag) 1088 break; 1089 } 1090 1091 return sd; 1092 } 1093 1094 DECLARE_PER_CPU(struct sched_domain *, sd_llc); 1095 DECLARE_PER_CPU(int, sd_llc_size); 1096 DECLARE_PER_CPU(int, sd_llc_id); 1097 DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared); 1098 DECLARE_PER_CPU(struct sched_domain *, sd_numa); 1099 DECLARE_PER_CPU(struct sched_domain *, sd_asym); 1100 1101 struct sched_group_capacity { 1102 atomic_t ref; 1103 /* 1104 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity 1105 * for a single CPU. 1106 */ 1107 unsigned long capacity; 1108 unsigned long min_capacity; /* Min per-CPU capacity in group */ 1109 unsigned long next_update; 1110 int imbalance; /* XXX unrelated to capacity but shared group state */ 1111 1112 #ifdef CONFIG_SCHED_DEBUG 1113 int id; 1114 #endif 1115 1116 unsigned long cpumask[0]; /* balance mask */ 1117 }; 1118 1119 struct sched_group { 1120 struct sched_group *next; /* Must be a circular list */ 1121 atomic_t ref; 1122 1123 unsigned int group_weight; 1124 struct sched_group_capacity *sgc; 1125 int asym_prefer_cpu; /* cpu of highest priority in group */ 1126 1127 /* 1128 * The CPUs this group covers. 1129 * 1130 * NOTE: this field is variable length. (Allocated dynamically 1131 * by attaching extra space to the end of the structure, 1132 * depending on how many CPUs the kernel has booted up with) 1133 */ 1134 unsigned long cpumask[0]; 1135 }; 1136 1137 static inline struct cpumask *sched_group_span(struct sched_group *sg) 1138 { 1139 return to_cpumask(sg->cpumask); 1140 } 1141 1142 /* 1143 * See build_balance_mask(). 1144 */ 1145 static inline struct cpumask *group_balance_mask(struct sched_group *sg) 1146 { 1147 return to_cpumask(sg->sgc->cpumask); 1148 } 1149 1150 /** 1151 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group. 1152 * @group: The group whose first cpu is to be returned. 1153 */ 1154 static inline unsigned int group_first_cpu(struct sched_group *group) 1155 { 1156 return cpumask_first(sched_group_span(group)); 1157 } 1158 1159 extern int group_balance_cpu(struct sched_group *sg); 1160 1161 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) 1162 void register_sched_domain_sysctl(void); 1163 void dirty_sched_domain_sysctl(int cpu); 1164 void unregister_sched_domain_sysctl(void); 1165 #else 1166 static inline void register_sched_domain_sysctl(void) 1167 { 1168 } 1169 static inline void dirty_sched_domain_sysctl(int cpu) 1170 { 1171 } 1172 static inline void unregister_sched_domain_sysctl(void) 1173 { 1174 } 1175 #endif 1176 1177 #else 1178 1179 static inline void sched_ttwu_pending(void) { } 1180 1181 #endif /* CONFIG_SMP */ 1182 1183 #include "stats.h" 1184 #include "autogroup.h" 1185 1186 #ifdef CONFIG_CGROUP_SCHED 1187 1188 /* 1189 * Return the group to which this tasks belongs. 1190 * 1191 * We cannot use task_css() and friends because the cgroup subsystem 1192 * changes that value before the cgroup_subsys::attach() method is called, 1193 * therefore we cannot pin it and might observe the wrong value. 1194 * 1195 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup 1196 * core changes this before calling sched_move_task(). 1197 * 1198 * Instead we use a 'copy' which is updated from sched_move_task() while 1199 * holding both task_struct::pi_lock and rq::lock. 1200 */ 1201 static inline struct task_group *task_group(struct task_struct *p) 1202 { 1203 return p->sched_task_group; 1204 } 1205 1206 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ 1207 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) 1208 { 1209 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED) 1210 struct task_group *tg = task_group(p); 1211 #endif 1212 1213 #ifdef CONFIG_FAIR_GROUP_SCHED 1214 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]); 1215 p->se.cfs_rq = tg->cfs_rq[cpu]; 1216 p->se.parent = tg->se[cpu]; 1217 #endif 1218 1219 #ifdef CONFIG_RT_GROUP_SCHED 1220 p->rt.rt_rq = tg->rt_rq[cpu]; 1221 p->rt.parent = tg->rt_se[cpu]; 1222 #endif 1223 } 1224 1225 #else /* CONFIG_CGROUP_SCHED */ 1226 1227 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } 1228 static inline struct task_group *task_group(struct task_struct *p) 1229 { 1230 return NULL; 1231 } 1232 1233 #endif /* CONFIG_CGROUP_SCHED */ 1234 1235 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) 1236 { 1237 set_task_rq(p, cpu); 1238 #ifdef CONFIG_SMP 1239 /* 1240 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be 1241 * successfuly executed on another CPU. We must ensure that updates of 1242 * per-task data have been completed by this moment. 1243 */ 1244 smp_wmb(); 1245 #ifdef CONFIG_THREAD_INFO_IN_TASK 1246 p->cpu = cpu; 1247 #else 1248 task_thread_info(p)->cpu = cpu; 1249 #endif 1250 p->wake_cpu = cpu; 1251 #endif 1252 } 1253 1254 /* 1255 * Tunables that become constants when CONFIG_SCHED_DEBUG is off: 1256 */ 1257 #ifdef CONFIG_SCHED_DEBUG 1258 # include <linux/static_key.h> 1259 # define const_debug __read_mostly 1260 #else 1261 # define const_debug const 1262 #endif 1263 1264 #define SCHED_FEAT(name, enabled) \ 1265 __SCHED_FEAT_##name , 1266 1267 enum { 1268 #include "features.h" 1269 __SCHED_FEAT_NR, 1270 }; 1271 1272 #undef SCHED_FEAT 1273 1274 #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL) 1275 1276 /* 1277 * To support run-time toggling of sched features, all the translation units 1278 * (but core.c) reference the sysctl_sched_features defined in core.c. 1279 */ 1280 extern const_debug unsigned int sysctl_sched_features; 1281 1282 #define SCHED_FEAT(name, enabled) \ 1283 static __always_inline bool static_branch_##name(struct static_key *key) \ 1284 { \ 1285 return static_key_##enabled(key); \ 1286 } 1287 1288 #include "features.h" 1289 #undef SCHED_FEAT 1290 1291 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; 1292 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x])) 1293 1294 #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */ 1295 1296 /* 1297 * Each translation unit has its own copy of sysctl_sched_features to allow 1298 * constants propagation at compile time and compiler optimization based on 1299 * features default. 1300 */ 1301 #define SCHED_FEAT(name, enabled) \ 1302 (1UL << __SCHED_FEAT_##name) * enabled | 1303 static const_debug __maybe_unused unsigned int sysctl_sched_features = 1304 #include "features.h" 1305 0; 1306 #undef SCHED_FEAT 1307 1308 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) 1309 1310 #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */ 1311 1312 extern struct static_key_false sched_numa_balancing; 1313 extern struct static_key_false sched_schedstats; 1314 1315 static inline u64 global_rt_period(void) 1316 { 1317 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; 1318 } 1319 1320 static inline u64 global_rt_runtime(void) 1321 { 1322 if (sysctl_sched_rt_runtime < 0) 1323 return RUNTIME_INF; 1324 1325 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; 1326 } 1327 1328 static inline int task_current(struct rq *rq, struct task_struct *p) 1329 { 1330 return rq->curr == p; 1331 } 1332 1333 static inline int task_running(struct rq *rq, struct task_struct *p) 1334 { 1335 #ifdef CONFIG_SMP 1336 return p->on_cpu; 1337 #else 1338 return task_current(rq, p); 1339 #endif 1340 } 1341 1342 static inline int task_on_rq_queued(struct task_struct *p) 1343 { 1344 return p->on_rq == TASK_ON_RQ_QUEUED; 1345 } 1346 1347 static inline int task_on_rq_migrating(struct task_struct *p) 1348 { 1349 return p->on_rq == TASK_ON_RQ_MIGRATING; 1350 } 1351 1352 #ifndef prepare_arch_switch 1353 # define prepare_arch_switch(next) do { } while (0) 1354 #endif 1355 #ifndef finish_arch_post_lock_switch 1356 # define finish_arch_post_lock_switch() do { } while (0) 1357 #endif 1358 1359 /* 1360 * wake flags 1361 */ 1362 #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */ 1363 #define WF_FORK 0x02 /* child wakeup after fork */ 1364 #define WF_MIGRATED 0x4 /* internal use, task got migrated */ 1365 1366 /* 1367 * To aid in avoiding the subversion of "niceness" due to uneven distribution 1368 * of tasks with abnormal "nice" values across CPUs the contribution that 1369 * each task makes to its run queue's load is weighted according to its 1370 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a 1371 * scaled version of the new time slice allocation that they receive on time 1372 * slice expiry etc. 1373 */ 1374 1375 #define WEIGHT_IDLEPRIO 3 1376 #define WMULT_IDLEPRIO 1431655765 1377 1378 extern const int sched_prio_to_weight[40]; 1379 extern const u32 sched_prio_to_wmult[40]; 1380 1381 /* 1382 * {de,en}queue flags: 1383 * 1384 * DEQUEUE_SLEEP - task is no longer runnable 1385 * ENQUEUE_WAKEUP - task just became runnable 1386 * 1387 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks 1388 * are in a known state which allows modification. Such pairs 1389 * should preserve as much state as possible. 1390 * 1391 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location 1392 * in the runqueue. 1393 * 1394 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified) 1395 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline) 1396 * ENQUEUE_MIGRATED - the task was migrated during wakeup 1397 * 1398 */ 1399 1400 #define DEQUEUE_SLEEP 0x01 1401 #define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */ 1402 #define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */ 1403 #define DEQUEUE_NOCLOCK 0x08 /* matches ENQUEUE_NOCLOCK */ 1404 1405 #define ENQUEUE_WAKEUP 0x01 1406 #define ENQUEUE_RESTORE 0x02 1407 #define ENQUEUE_MOVE 0x04 1408 #define ENQUEUE_NOCLOCK 0x08 1409 1410 #define ENQUEUE_HEAD 0x10 1411 #define ENQUEUE_REPLENISH 0x20 1412 #ifdef CONFIG_SMP 1413 #define ENQUEUE_MIGRATED 0x40 1414 #else 1415 #define ENQUEUE_MIGRATED 0x00 1416 #endif 1417 1418 #define RETRY_TASK ((void *)-1UL) 1419 1420 struct sched_class { 1421 const struct sched_class *next; 1422 1423 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags); 1424 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags); 1425 void (*yield_task) (struct rq *rq); 1426 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt); 1427 1428 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags); 1429 1430 /* 1431 * It is the responsibility of the pick_next_task() method that will 1432 * return the next task to call put_prev_task() on the @prev task or 1433 * something equivalent. 1434 * 1435 * May return RETRY_TASK when it finds a higher prio class has runnable 1436 * tasks. 1437 */ 1438 struct task_struct * (*pick_next_task) (struct rq *rq, 1439 struct task_struct *prev, 1440 struct rq_flags *rf); 1441 void (*put_prev_task) (struct rq *rq, struct task_struct *p); 1442 1443 #ifdef CONFIG_SMP 1444 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags); 1445 void (*migrate_task_rq)(struct task_struct *p); 1446 1447 void (*task_woken) (struct rq *this_rq, struct task_struct *task); 1448 1449 void (*set_cpus_allowed)(struct task_struct *p, 1450 const struct cpumask *newmask); 1451 1452 void (*rq_online)(struct rq *rq); 1453 void (*rq_offline)(struct rq *rq); 1454 #endif 1455 1456 void (*set_curr_task) (struct rq *rq); 1457 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued); 1458 void (*task_fork) (struct task_struct *p); 1459 void (*task_dead) (struct task_struct *p); 1460 1461 /* 1462 * The switched_from() call is allowed to drop rq->lock, therefore we 1463 * cannot assume the switched_from/switched_to pair is serliazed by 1464 * rq->lock. They are however serialized by p->pi_lock. 1465 */ 1466 void (*switched_from) (struct rq *this_rq, struct task_struct *task); 1467 void (*switched_to) (struct rq *this_rq, struct task_struct *task); 1468 void (*prio_changed) (struct rq *this_rq, struct task_struct *task, 1469 int oldprio); 1470 1471 unsigned int (*get_rr_interval) (struct rq *rq, 1472 struct task_struct *task); 1473 1474 void (*update_curr) (struct rq *rq); 1475 1476 #define TASK_SET_GROUP 0 1477 #define TASK_MOVE_GROUP 1 1478 1479 #ifdef CONFIG_FAIR_GROUP_SCHED 1480 void (*task_change_group) (struct task_struct *p, int type); 1481 #endif 1482 }; 1483 1484 static inline void put_prev_task(struct rq *rq, struct task_struct *prev) 1485 { 1486 prev->sched_class->put_prev_task(rq, prev); 1487 } 1488 1489 static inline void set_curr_task(struct rq *rq, struct task_struct *curr) 1490 { 1491 curr->sched_class->set_curr_task(rq); 1492 } 1493 1494 #ifdef CONFIG_SMP 1495 #define sched_class_highest (&stop_sched_class) 1496 #else 1497 #define sched_class_highest (&dl_sched_class) 1498 #endif 1499 #define for_each_class(class) \ 1500 for (class = sched_class_highest; class; class = class->next) 1501 1502 extern const struct sched_class stop_sched_class; 1503 extern const struct sched_class dl_sched_class; 1504 extern const struct sched_class rt_sched_class; 1505 extern const struct sched_class fair_sched_class; 1506 extern const struct sched_class idle_sched_class; 1507 1508 1509 #ifdef CONFIG_SMP 1510 1511 extern void update_group_capacity(struct sched_domain *sd, int cpu); 1512 1513 extern void trigger_load_balance(struct rq *rq); 1514 1515 extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask); 1516 1517 #endif 1518 1519 #ifdef CONFIG_CPU_IDLE 1520 static inline void idle_set_state(struct rq *rq, 1521 struct cpuidle_state *idle_state) 1522 { 1523 rq->idle_state = idle_state; 1524 } 1525 1526 static inline struct cpuidle_state *idle_get_state(struct rq *rq) 1527 { 1528 SCHED_WARN_ON(!rcu_read_lock_held()); 1529 return rq->idle_state; 1530 } 1531 #else 1532 static inline void idle_set_state(struct rq *rq, 1533 struct cpuidle_state *idle_state) 1534 { 1535 } 1536 1537 static inline struct cpuidle_state *idle_get_state(struct rq *rq) 1538 { 1539 return NULL; 1540 } 1541 #endif 1542 1543 extern void schedule_idle(void); 1544 1545 extern void sysrq_sched_debug_show(void); 1546 extern void sched_init_granularity(void); 1547 extern void update_max_interval(void); 1548 1549 extern void init_sched_dl_class(void); 1550 extern void init_sched_rt_class(void); 1551 extern void init_sched_fair_class(void); 1552 1553 extern void reweight_task(struct task_struct *p, int prio); 1554 1555 extern void resched_curr(struct rq *rq); 1556 extern void resched_cpu(int cpu); 1557 1558 extern struct rt_bandwidth def_rt_bandwidth; 1559 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime); 1560 1561 extern struct dl_bandwidth def_dl_bandwidth; 1562 extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime); 1563 extern void init_dl_task_timer(struct sched_dl_entity *dl_se); 1564 extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se); 1565 extern void init_dl_rq_bw_ratio(struct dl_rq *dl_rq); 1566 1567 #define BW_SHIFT 20 1568 #define BW_UNIT (1 << BW_SHIFT) 1569 #define RATIO_SHIFT 8 1570 unsigned long to_ratio(u64 period, u64 runtime); 1571 1572 extern void init_entity_runnable_average(struct sched_entity *se); 1573 extern void post_init_entity_util_avg(struct sched_entity *se); 1574 1575 #ifdef CONFIG_NO_HZ_FULL 1576 extern bool sched_can_stop_tick(struct rq *rq); 1577 1578 /* 1579 * Tick may be needed by tasks in the runqueue depending on their policy and 1580 * requirements. If tick is needed, lets send the target an IPI to kick it out of 1581 * nohz mode if necessary. 1582 */ 1583 static inline void sched_update_tick_dependency(struct rq *rq) 1584 { 1585 int cpu; 1586 1587 if (!tick_nohz_full_enabled()) 1588 return; 1589 1590 cpu = cpu_of(rq); 1591 1592 if (!tick_nohz_full_cpu(cpu)) 1593 return; 1594 1595 if (sched_can_stop_tick(rq)) 1596 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED); 1597 else 1598 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); 1599 } 1600 #else 1601 static inline void sched_update_tick_dependency(struct rq *rq) { } 1602 #endif 1603 1604 static inline void add_nr_running(struct rq *rq, unsigned count) 1605 { 1606 unsigned prev_nr = rq->nr_running; 1607 1608 rq->nr_running = prev_nr + count; 1609 1610 if (prev_nr < 2 && rq->nr_running >= 2) { 1611 #ifdef CONFIG_SMP 1612 if (!rq->rd->overload) 1613 rq->rd->overload = true; 1614 #endif 1615 } 1616 1617 sched_update_tick_dependency(rq); 1618 } 1619 1620 static inline void sub_nr_running(struct rq *rq, unsigned count) 1621 { 1622 rq->nr_running -= count; 1623 /* Check if we still need preemption */ 1624 sched_update_tick_dependency(rq); 1625 } 1626 1627 static inline void rq_last_tick_reset(struct rq *rq) 1628 { 1629 #ifdef CONFIG_NO_HZ_FULL 1630 rq->last_sched_tick = jiffies; 1631 #endif 1632 } 1633 1634 extern void update_rq_clock(struct rq *rq); 1635 1636 extern void activate_task(struct rq *rq, struct task_struct *p, int flags); 1637 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags); 1638 1639 extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); 1640 1641 extern const_debug unsigned int sysctl_sched_time_avg; 1642 extern const_debug unsigned int sysctl_sched_nr_migrate; 1643 extern const_debug unsigned int sysctl_sched_migration_cost; 1644 1645 static inline u64 sched_avg_period(void) 1646 { 1647 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2; 1648 } 1649 1650 #ifdef CONFIG_SCHED_HRTICK 1651 1652 /* 1653 * Use hrtick when: 1654 * - enabled by features 1655 * - hrtimer is actually high res 1656 */ 1657 static inline int hrtick_enabled(struct rq *rq) 1658 { 1659 if (!sched_feat(HRTICK)) 1660 return 0; 1661 if (!cpu_active(cpu_of(rq))) 1662 return 0; 1663 return hrtimer_is_hres_active(&rq->hrtick_timer); 1664 } 1665 1666 void hrtick_start(struct rq *rq, u64 delay); 1667 1668 #else 1669 1670 static inline int hrtick_enabled(struct rq *rq) 1671 { 1672 return 0; 1673 } 1674 1675 #endif /* CONFIG_SCHED_HRTICK */ 1676 1677 #ifndef arch_scale_freq_capacity 1678 static __always_inline 1679 unsigned long arch_scale_freq_capacity(int cpu) 1680 { 1681 return SCHED_CAPACITY_SCALE; 1682 } 1683 #endif 1684 1685 #ifdef CONFIG_SMP 1686 extern void sched_avg_update(struct rq *rq); 1687 1688 #ifndef arch_scale_cpu_capacity 1689 static __always_inline 1690 unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu) 1691 { 1692 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1)) 1693 return sd->smt_gain / sd->span_weight; 1694 1695 return SCHED_CAPACITY_SCALE; 1696 } 1697 #endif 1698 1699 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) 1700 { 1701 rq->rt_avg += rt_delta * arch_scale_freq_capacity(cpu_of(rq)); 1702 sched_avg_update(rq); 1703 } 1704 #else 1705 #ifndef arch_scale_cpu_capacity 1706 static __always_inline 1707 unsigned long arch_scale_cpu_capacity(void __always_unused *sd, int cpu) 1708 { 1709 return SCHED_CAPACITY_SCALE; 1710 } 1711 #endif 1712 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { } 1713 static inline void sched_avg_update(struct rq *rq) { } 1714 #endif 1715 1716 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) 1717 __acquires(rq->lock); 1718 1719 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) 1720 __acquires(p->pi_lock) 1721 __acquires(rq->lock); 1722 1723 static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf) 1724 __releases(rq->lock) 1725 { 1726 rq_unpin_lock(rq, rf); 1727 raw_spin_unlock(&rq->lock); 1728 } 1729 1730 static inline void 1731 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf) 1732 __releases(rq->lock) 1733 __releases(p->pi_lock) 1734 { 1735 rq_unpin_lock(rq, rf); 1736 raw_spin_unlock(&rq->lock); 1737 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); 1738 } 1739 1740 static inline void 1741 rq_lock_irqsave(struct rq *rq, struct rq_flags *rf) 1742 __acquires(rq->lock) 1743 { 1744 raw_spin_lock_irqsave(&rq->lock, rf->flags); 1745 rq_pin_lock(rq, rf); 1746 } 1747 1748 static inline void 1749 rq_lock_irq(struct rq *rq, struct rq_flags *rf) 1750 __acquires(rq->lock) 1751 { 1752 raw_spin_lock_irq(&rq->lock); 1753 rq_pin_lock(rq, rf); 1754 } 1755 1756 static inline void 1757 rq_lock(struct rq *rq, struct rq_flags *rf) 1758 __acquires(rq->lock) 1759 { 1760 raw_spin_lock(&rq->lock); 1761 rq_pin_lock(rq, rf); 1762 } 1763 1764 static inline void 1765 rq_relock(struct rq *rq, struct rq_flags *rf) 1766 __acquires(rq->lock) 1767 { 1768 raw_spin_lock(&rq->lock); 1769 rq_repin_lock(rq, rf); 1770 } 1771 1772 static inline void 1773 rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf) 1774 __releases(rq->lock) 1775 { 1776 rq_unpin_lock(rq, rf); 1777 raw_spin_unlock_irqrestore(&rq->lock, rf->flags); 1778 } 1779 1780 static inline void 1781 rq_unlock_irq(struct rq *rq, struct rq_flags *rf) 1782 __releases(rq->lock) 1783 { 1784 rq_unpin_lock(rq, rf); 1785 raw_spin_unlock_irq(&rq->lock); 1786 } 1787 1788 static inline void 1789 rq_unlock(struct rq *rq, struct rq_flags *rf) 1790 __releases(rq->lock) 1791 { 1792 rq_unpin_lock(rq, rf); 1793 raw_spin_unlock(&rq->lock); 1794 } 1795 1796 #ifdef CONFIG_SMP 1797 #ifdef CONFIG_PREEMPT 1798 1799 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2); 1800 1801 /* 1802 * fair double_lock_balance: Safely acquires both rq->locks in a fair 1803 * way at the expense of forcing extra atomic operations in all 1804 * invocations. This assures that the double_lock is acquired using the 1805 * same underlying policy as the spinlock_t on this architecture, which 1806 * reduces latency compared to the unfair variant below. However, it 1807 * also adds more overhead and therefore may reduce throughput. 1808 */ 1809 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) 1810 __releases(this_rq->lock) 1811 __acquires(busiest->lock) 1812 __acquires(this_rq->lock) 1813 { 1814 raw_spin_unlock(&this_rq->lock); 1815 double_rq_lock(this_rq, busiest); 1816 1817 return 1; 1818 } 1819 1820 #else 1821 /* 1822 * Unfair double_lock_balance: Optimizes throughput at the expense of 1823 * latency by eliminating extra atomic operations when the locks are 1824 * already in proper order on entry. This favors lower cpu-ids and will 1825 * grant the double lock to lower cpus over higher ids under contention, 1826 * regardless of entry order into the function. 1827 */ 1828 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) 1829 __releases(this_rq->lock) 1830 __acquires(busiest->lock) 1831 __acquires(this_rq->lock) 1832 { 1833 int ret = 0; 1834 1835 if (unlikely(!raw_spin_trylock(&busiest->lock))) { 1836 if (busiest < this_rq) { 1837 raw_spin_unlock(&this_rq->lock); 1838 raw_spin_lock(&busiest->lock); 1839 raw_spin_lock_nested(&this_rq->lock, 1840 SINGLE_DEPTH_NESTING); 1841 ret = 1; 1842 } else 1843 raw_spin_lock_nested(&busiest->lock, 1844 SINGLE_DEPTH_NESTING); 1845 } 1846 return ret; 1847 } 1848 1849 #endif /* CONFIG_PREEMPT */ 1850 1851 /* 1852 * double_lock_balance - lock the busiest runqueue, this_rq is locked already. 1853 */ 1854 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest) 1855 { 1856 if (unlikely(!irqs_disabled())) { 1857 /* printk() doesn't work good under rq->lock */ 1858 raw_spin_unlock(&this_rq->lock); 1859 BUG_ON(1); 1860 } 1861 1862 return _double_lock_balance(this_rq, busiest); 1863 } 1864 1865 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) 1866 __releases(busiest->lock) 1867 { 1868 raw_spin_unlock(&busiest->lock); 1869 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_); 1870 } 1871 1872 static inline void double_lock(spinlock_t *l1, spinlock_t *l2) 1873 { 1874 if (l1 > l2) 1875 swap(l1, l2); 1876 1877 spin_lock(l1); 1878 spin_lock_nested(l2, SINGLE_DEPTH_NESTING); 1879 } 1880 1881 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2) 1882 { 1883 if (l1 > l2) 1884 swap(l1, l2); 1885 1886 spin_lock_irq(l1); 1887 spin_lock_nested(l2, SINGLE_DEPTH_NESTING); 1888 } 1889 1890 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2) 1891 { 1892 if (l1 > l2) 1893 swap(l1, l2); 1894 1895 raw_spin_lock(l1); 1896 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING); 1897 } 1898 1899 /* 1900 * double_rq_lock - safely lock two runqueues 1901 * 1902 * Note this does not disable interrupts like task_rq_lock, 1903 * you need to do so manually before calling. 1904 */ 1905 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) 1906 __acquires(rq1->lock) 1907 __acquires(rq2->lock) 1908 { 1909 BUG_ON(!irqs_disabled()); 1910 if (rq1 == rq2) { 1911 raw_spin_lock(&rq1->lock); 1912 __acquire(rq2->lock); /* Fake it out ;) */ 1913 } else { 1914 if (rq1 < rq2) { 1915 raw_spin_lock(&rq1->lock); 1916 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING); 1917 } else { 1918 raw_spin_lock(&rq2->lock); 1919 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); 1920 } 1921 } 1922 } 1923 1924 /* 1925 * double_rq_unlock - safely unlock two runqueues 1926 * 1927 * Note this does not restore interrupts like task_rq_unlock, 1928 * you need to do so manually after calling. 1929 */ 1930 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) 1931 __releases(rq1->lock) 1932 __releases(rq2->lock) 1933 { 1934 raw_spin_unlock(&rq1->lock); 1935 if (rq1 != rq2) 1936 raw_spin_unlock(&rq2->lock); 1937 else 1938 __release(rq2->lock); 1939 } 1940 1941 extern void set_rq_online (struct rq *rq); 1942 extern void set_rq_offline(struct rq *rq); 1943 extern bool sched_smp_initialized; 1944 1945 #else /* CONFIG_SMP */ 1946 1947 /* 1948 * double_rq_lock - safely lock two runqueues 1949 * 1950 * Note this does not disable interrupts like task_rq_lock, 1951 * you need to do so manually before calling. 1952 */ 1953 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) 1954 __acquires(rq1->lock) 1955 __acquires(rq2->lock) 1956 { 1957 BUG_ON(!irqs_disabled()); 1958 BUG_ON(rq1 != rq2); 1959 raw_spin_lock(&rq1->lock); 1960 __acquire(rq2->lock); /* Fake it out ;) */ 1961 } 1962 1963 /* 1964 * double_rq_unlock - safely unlock two runqueues 1965 * 1966 * Note this does not restore interrupts like task_rq_unlock, 1967 * you need to do so manually after calling. 1968 */ 1969 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) 1970 __releases(rq1->lock) 1971 __releases(rq2->lock) 1972 { 1973 BUG_ON(rq1 != rq2); 1974 raw_spin_unlock(&rq1->lock); 1975 __release(rq2->lock); 1976 } 1977 1978 #endif 1979 1980 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq); 1981 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq); 1982 1983 #ifdef CONFIG_SCHED_DEBUG 1984 extern bool sched_debug_enabled; 1985 1986 extern void print_cfs_stats(struct seq_file *m, int cpu); 1987 extern void print_rt_stats(struct seq_file *m, int cpu); 1988 extern void print_dl_stats(struct seq_file *m, int cpu); 1989 extern void 1990 print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq); 1991 #ifdef CONFIG_NUMA_BALANCING 1992 extern void 1993 show_numa_stats(struct task_struct *p, struct seq_file *m); 1994 extern void 1995 print_numa_stats(struct seq_file *m, int node, unsigned long tsf, 1996 unsigned long tpf, unsigned long gsf, unsigned long gpf); 1997 #endif /* CONFIG_NUMA_BALANCING */ 1998 #endif /* CONFIG_SCHED_DEBUG */ 1999 2000 extern void init_cfs_rq(struct cfs_rq *cfs_rq); 2001 extern void init_rt_rq(struct rt_rq *rt_rq); 2002 extern void init_dl_rq(struct dl_rq *dl_rq); 2003 2004 extern void cfs_bandwidth_usage_inc(void); 2005 extern void cfs_bandwidth_usage_dec(void); 2006 2007 #ifdef CONFIG_NO_HZ_COMMON 2008 enum rq_nohz_flag_bits { 2009 NOHZ_TICK_STOPPED, 2010 NOHZ_BALANCE_KICK, 2011 }; 2012 2013 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags) 2014 2015 extern void nohz_balance_exit_idle(unsigned int cpu); 2016 #else 2017 static inline void nohz_balance_exit_idle(unsigned int cpu) { } 2018 #endif 2019 2020 2021 #ifdef CONFIG_SMP 2022 static inline 2023 void __dl_update(struct dl_bw *dl_b, s64 bw) 2024 { 2025 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw); 2026 int i; 2027 2028 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(), 2029 "sched RCU must be held"); 2030 for_each_cpu_and(i, rd->span, cpu_active_mask) { 2031 struct rq *rq = cpu_rq(i); 2032 2033 rq->dl.extra_bw += bw; 2034 } 2035 } 2036 #else 2037 static inline 2038 void __dl_update(struct dl_bw *dl_b, s64 bw) 2039 { 2040 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw); 2041 2042 dl->extra_bw += bw; 2043 } 2044 #endif 2045 2046 2047 #ifdef CONFIG_IRQ_TIME_ACCOUNTING 2048 struct irqtime { 2049 u64 total; 2050 u64 tick_delta; 2051 u64 irq_start_time; 2052 struct u64_stats_sync sync; 2053 }; 2054 2055 DECLARE_PER_CPU(struct irqtime, cpu_irqtime); 2056 2057 /* 2058 * Returns the irqtime minus the softirq time computed by ksoftirqd. 2059 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime 2060 * and never move forward. 2061 */ 2062 static inline u64 irq_time_read(int cpu) 2063 { 2064 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu); 2065 unsigned int seq; 2066 u64 total; 2067 2068 do { 2069 seq = __u64_stats_fetch_begin(&irqtime->sync); 2070 total = irqtime->total; 2071 } while (__u64_stats_fetch_retry(&irqtime->sync, seq)); 2072 2073 return total; 2074 } 2075 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ 2076 2077 #ifdef CONFIG_CPU_FREQ 2078 DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data); 2079 2080 /** 2081 * cpufreq_update_util - Take a note about CPU utilization changes. 2082 * @rq: Runqueue to carry out the update for. 2083 * @flags: Update reason flags. 2084 * 2085 * This function is called by the scheduler on the CPU whose utilization is 2086 * being updated. 2087 * 2088 * It can only be called from RCU-sched read-side critical sections. 2089 * 2090 * The way cpufreq is currently arranged requires it to evaluate the CPU 2091 * performance state (frequency/voltage) on a regular basis to prevent it from 2092 * being stuck in a completely inadequate performance level for too long. 2093 * That is not guaranteed to happen if the updates are only triggered from CFS 2094 * and DL, though, because they may not be coming in if only RT tasks are 2095 * active all the time (or there are RT tasks only). 2096 * 2097 * As a workaround for that issue, this function is called periodically by the 2098 * RT sched class to trigger extra cpufreq updates to prevent it from stalling, 2099 * but that really is a band-aid. Going forward it should be replaced with 2100 * solutions targeted more specifically at RT tasks. 2101 */ 2102 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) 2103 { 2104 struct update_util_data *data; 2105 2106 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data, 2107 cpu_of(rq))); 2108 if (data) 2109 data->func(data, rq_clock(rq), flags); 2110 } 2111 #else 2112 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {} 2113 #endif /* CONFIG_CPU_FREQ */ 2114 2115 #ifdef arch_scale_freq_capacity 2116 #ifndef arch_scale_freq_invariant 2117 #define arch_scale_freq_invariant() (true) 2118 #endif 2119 #else /* arch_scale_freq_capacity */ 2120 #define arch_scale_freq_invariant() (false) 2121 #endif 2122 2123 #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL 2124 2125 static inline unsigned long cpu_util_dl(struct rq *rq) 2126 { 2127 return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT; 2128 } 2129 2130 static inline unsigned long cpu_util_cfs(struct rq *rq) 2131 { 2132 return rq->cfs.avg.util_avg; 2133 } 2134 2135 #endif 2136