1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Scheduler internal types and methods: 4 */ 5 #ifndef _KERNEL_SCHED_SCHED_H 6 #define _KERNEL_SCHED_SCHED_H 7 8 #include <linux/sched/affinity.h> 9 #include <linux/sched/autogroup.h> 10 #include <linux/sched/cpufreq.h> 11 #include <linux/sched/deadline.h> 12 #include <linux/sched.h> 13 #include <linux/sched/loadavg.h> 14 #include <linux/sched/mm.h> 15 #include <linux/sched/rseq_api.h> 16 #include <linux/sched/signal.h> 17 #include <linux/sched/smt.h> 18 #include <linux/sched/stat.h> 19 #include <linux/sched/sysctl.h> 20 #include <linux/sched/task_flags.h> 21 #include <linux/sched/task.h> 22 #include <linux/sched/topology.h> 23 24 #include <linux/atomic.h> 25 #include <linux/bitmap.h> 26 #include <linux/bug.h> 27 #include <linux/capability.h> 28 #include <linux/cgroup_api.h> 29 #include <linux/cgroup.h> 30 #include <linux/context_tracking.h> 31 #include <linux/cpufreq.h> 32 #include <linux/cpumask_api.h> 33 #include <linux/ctype.h> 34 #include <linux/file.h> 35 #include <linux/fs_api.h> 36 #include <linux/hrtimer_api.h> 37 #include <linux/interrupt.h> 38 #include <linux/irq_work.h> 39 #include <linux/jiffies.h> 40 #include <linux/kref_api.h> 41 #include <linux/kthread.h> 42 #include <linux/ktime_api.h> 43 #include <linux/lockdep_api.h> 44 #include <linux/lockdep.h> 45 #include <linux/minmax.h> 46 #include <linux/mm.h> 47 #include <linux/module.h> 48 #include <linux/mutex_api.h> 49 #include <linux/plist.h> 50 #include <linux/poll.h> 51 #include <linux/proc_fs.h> 52 #include <linux/profile.h> 53 #include <linux/psi.h> 54 #include <linux/rcupdate.h> 55 #include <linux/seq_file.h> 56 #include <linux/seqlock.h> 57 #include <linux/softirq.h> 58 #include <linux/spinlock_api.h> 59 #include <linux/static_key.h> 60 #include <linux/stop_machine.h> 61 #include <linux/syscalls_api.h> 62 #include <linux/syscalls.h> 63 #include <linux/tick.h> 64 #include <linux/topology.h> 65 #include <linux/types.h> 66 #include <linux/u64_stats_sync_api.h> 67 #include <linux/uaccess.h> 68 #include <linux/wait_api.h> 69 #include <linux/wait_bit.h> 70 #include <linux/workqueue_api.h> 71 72 #include <trace/events/power.h> 73 #include <trace/events/sched.h> 74 75 #include "../workqueue_internal.h" 76 77 struct rq; 78 struct cfs_rq; 79 struct rt_rq; 80 struct sched_group; 81 struct cpuidle_state; 82 83 #ifdef CONFIG_PARAVIRT 84 # include <asm/paravirt.h> 85 # include <asm/paravirt_api_clock.h> 86 #endif 87 88 #include <asm/barrier.h> 89 90 #include "cpupri.h" 91 #include "cpudeadline.h" 92 93 #ifdef CONFIG_SCHED_DEBUG 94 # define SCHED_WARN_ON(x) WARN_ONCE(x, #x) 95 #else 96 # define SCHED_WARN_ON(x) ({ (void)(x), 0; }) 97 #endif 98 99 /* task_struct::on_rq states: */ 100 #define TASK_ON_RQ_QUEUED 1 101 #define TASK_ON_RQ_MIGRATING 2 102 103 extern __read_mostly int scheduler_running; 104 105 extern unsigned long calc_load_update; 106 extern atomic_long_t calc_load_tasks; 107 108 extern void calc_global_load_tick(struct rq *this_rq); 109 extern long calc_load_fold_active(struct rq *this_rq, long adjust); 110 111 extern void call_trace_sched_update_nr_running(struct rq *rq, int count); 112 113 extern int sysctl_sched_rt_period; 114 extern int sysctl_sched_rt_runtime; 115 extern int sched_rr_timeslice; 116 117 /* 118 * Asymmetric CPU capacity bits 119 */ 120 struct asym_cap_data { 121 struct list_head link; 122 struct rcu_head rcu; 123 unsigned long capacity; 124 unsigned long cpus[]; 125 }; 126 127 extern struct list_head asym_cap_list; 128 129 #define cpu_capacity_span(asym_data) to_cpumask((asym_data)->cpus) 130 131 /* 132 * Helpers for converting nanosecond timing to jiffy resolution 133 */ 134 #define NS_TO_JIFFIES(time) ((unsigned long)(time) / (NSEC_PER_SEC/HZ)) 135 136 /* 137 * Increase resolution of nice-level calculations for 64-bit architectures. 138 * The extra resolution improves shares distribution and load balancing of 139 * low-weight task groups (eg. nice +19 on an autogroup), deeper task-group 140 * hierarchies, especially on larger systems. This is not a user-visible change 141 * and does not change the user-interface for setting shares/weights. 142 * 143 * We increase resolution only if we have enough bits to allow this increased 144 * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit 145 * are pretty high and the returns do not justify the increased costs. 146 * 147 * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to 148 * increase coverage and consistency always enable it on 64-bit platforms. 149 */ 150 #ifdef CONFIG_64BIT 151 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT) 152 # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT) 153 # define scale_load_down(w) \ 154 ({ \ 155 unsigned long __w = (w); \ 156 \ 157 if (__w) \ 158 __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \ 159 __w; \ 160 }) 161 #else 162 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT) 163 # define scale_load(w) (w) 164 # define scale_load_down(w) (w) 165 #endif 166 167 /* 168 * Task weight (visible to users) and its load (invisible to users) have 169 * independent resolution, but they should be well calibrated. We use 170 * scale_load() and scale_load_down(w) to convert between them. The 171 * following must be true: 172 * 173 * scale_load(sched_prio_to_weight[NICE_TO_PRIO(0)-MAX_RT_PRIO]) == NICE_0_LOAD 174 * 175 */ 176 #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT) 177 178 /* 179 * Single value that decides SCHED_DEADLINE internal math precision. 180 * 10 -> just above 1us 181 * 9 -> just above 0.5us 182 */ 183 #define DL_SCALE 10 184 185 /* 186 * Single value that denotes runtime == period, ie unlimited time. 187 */ 188 #define RUNTIME_INF ((u64)~0ULL) 189 190 static inline int idle_policy(int policy) 191 { 192 return policy == SCHED_IDLE; 193 } 194 195 static inline int normal_policy(int policy) 196 { 197 #ifdef CONFIG_SCHED_CLASS_EXT 198 if (policy == SCHED_EXT) 199 return true; 200 #endif 201 return policy == SCHED_NORMAL; 202 } 203 204 static inline int fair_policy(int policy) 205 { 206 return normal_policy(policy) || policy == SCHED_BATCH; 207 } 208 209 static inline int rt_policy(int policy) 210 { 211 return policy == SCHED_FIFO || policy == SCHED_RR; 212 } 213 214 static inline int dl_policy(int policy) 215 { 216 return policy == SCHED_DEADLINE; 217 } 218 219 static inline bool valid_policy(int policy) 220 { 221 return idle_policy(policy) || fair_policy(policy) || 222 rt_policy(policy) || dl_policy(policy); 223 } 224 225 static inline int task_has_idle_policy(struct task_struct *p) 226 { 227 return idle_policy(p->policy); 228 } 229 230 static inline int task_has_rt_policy(struct task_struct *p) 231 { 232 return rt_policy(p->policy); 233 } 234 235 static inline int task_has_dl_policy(struct task_struct *p) 236 { 237 return dl_policy(p->policy); 238 } 239 240 #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) 241 242 static inline void update_avg(u64 *avg, u64 sample) 243 { 244 s64 diff = sample - *avg; 245 246 *avg += diff / 8; 247 } 248 249 /* 250 * Shifting a value by an exponent greater *or equal* to the size of said value 251 * is UB; cap at size-1. 252 */ 253 #define shr_bound(val, shift) \ 254 (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1)) 255 256 /* 257 * cgroup weight knobs should use the common MIN, DFL and MAX values which are 258 * 1, 100 and 10000 respectively. While it loses a bit of range on both ends, it 259 * maps pretty well onto the shares value used by scheduler and the round-trip 260 * conversions preserve the original value over the entire range. 261 */ 262 static inline unsigned long sched_weight_from_cgroup(unsigned long cgrp_weight) 263 { 264 return DIV_ROUND_CLOSEST_ULL(cgrp_weight * 1024, CGROUP_WEIGHT_DFL); 265 } 266 267 static inline unsigned long sched_weight_to_cgroup(unsigned long weight) 268 { 269 return clamp_t(unsigned long, 270 DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024), 271 CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX); 272 } 273 274 /* 275 * !! For sched_setattr_nocheck() (kernel) only !! 276 * 277 * This is actually gross. :( 278 * 279 * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE 280 * tasks, but still be able to sleep. We need this on platforms that cannot 281 * atomically change clock frequency. Remove once fast switching will be 282 * available on such platforms. 283 * 284 * SUGOV stands for SchedUtil GOVernor. 285 */ 286 #define SCHED_FLAG_SUGOV 0x10000000 287 288 #define SCHED_DL_FLAGS (SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV) 289 290 static inline bool dl_entity_is_special(const struct sched_dl_entity *dl_se) 291 { 292 #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL 293 return unlikely(dl_se->flags & SCHED_FLAG_SUGOV); 294 #else 295 return false; 296 #endif 297 } 298 299 /* 300 * Tells if entity @a should preempt entity @b. 301 */ 302 static inline bool dl_entity_preempt(const struct sched_dl_entity *a, 303 const struct sched_dl_entity *b) 304 { 305 return dl_entity_is_special(a) || 306 dl_time_before(a->deadline, b->deadline); 307 } 308 309 /* 310 * This is the priority-queue data structure of the RT scheduling class: 311 */ 312 struct rt_prio_array { 313 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ 314 struct list_head queue[MAX_RT_PRIO]; 315 }; 316 317 struct rt_bandwidth { 318 /* nests inside the rq lock: */ 319 raw_spinlock_t rt_runtime_lock; 320 ktime_t rt_period; 321 u64 rt_runtime; 322 struct hrtimer rt_period_timer; 323 unsigned int rt_period_active; 324 }; 325 326 static inline int dl_bandwidth_enabled(void) 327 { 328 return sysctl_sched_rt_runtime >= 0; 329 } 330 331 /* 332 * To keep the bandwidth of -deadline tasks under control 333 * we need some place where: 334 * - store the maximum -deadline bandwidth of each cpu; 335 * - cache the fraction of bandwidth that is currently allocated in 336 * each root domain; 337 * 338 * This is all done in the data structure below. It is similar to the 339 * one used for RT-throttling (rt_bandwidth), with the main difference 340 * that, since here we are only interested in admission control, we 341 * do not decrease any runtime while the group "executes", neither we 342 * need a timer to replenish it. 343 * 344 * With respect to SMP, bandwidth is given on a per root domain basis, 345 * meaning that: 346 * - bw (< 100%) is the deadline bandwidth of each CPU; 347 * - total_bw is the currently allocated bandwidth in each root domain; 348 */ 349 struct dl_bw { 350 raw_spinlock_t lock; 351 u64 bw; 352 u64 total_bw; 353 }; 354 355 extern void init_dl_bw(struct dl_bw *dl_b); 356 extern int sched_dl_global_validate(void); 357 extern void sched_dl_do_global(void); 358 extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr); 359 extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr); 360 extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr); 361 extern bool __checkparam_dl(const struct sched_attr *attr); 362 extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr); 363 extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial); 364 extern int dl_bw_check_overflow(int cpu); 365 366 /* 367 * SCHED_DEADLINE supports servers (nested scheduling) with the following 368 * interface: 369 * 370 * dl_se::rq -- runqueue we belong to. 371 * 372 * dl_se::server_has_tasks() -- used on bandwidth enforcement; we 'stop' the 373 * server when it runs out of tasks to run. 374 * 375 * dl_se::server_pick() -- nested pick_next_task(); we yield the period if this 376 * returns NULL. 377 * 378 * dl_server_update() -- called from update_curr_common(), propagates runtime 379 * to the server. 380 * 381 * dl_server_start() 382 * dl_server_stop() -- start/stop the server when it has (no) tasks. 383 * 384 * dl_server_init() -- initializes the server. 385 */ 386 extern void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec); 387 extern void dl_server_start(struct sched_dl_entity *dl_se); 388 extern void dl_server_stop(struct sched_dl_entity *dl_se); 389 extern void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq, 390 dl_server_has_tasks_f has_tasks, 391 dl_server_pick_f pick); 392 393 #ifdef CONFIG_CGROUP_SCHED 394 395 extern struct list_head task_groups; 396 397 struct cfs_bandwidth { 398 #ifdef CONFIG_CFS_BANDWIDTH 399 raw_spinlock_t lock; 400 ktime_t period; 401 u64 quota; 402 u64 runtime; 403 u64 burst; 404 u64 runtime_snap; 405 s64 hierarchical_quota; 406 407 u8 idle; 408 u8 period_active; 409 u8 slack_started; 410 struct hrtimer period_timer; 411 struct hrtimer slack_timer; 412 struct list_head throttled_cfs_rq; 413 414 /* Statistics: */ 415 int nr_periods; 416 int nr_throttled; 417 int nr_burst; 418 u64 throttled_time; 419 u64 burst_time; 420 #endif 421 }; 422 423 /* Task group related information */ 424 struct task_group { 425 struct cgroup_subsys_state css; 426 427 #ifdef CONFIG_FAIR_GROUP_SCHED 428 /* schedulable entities of this group on each CPU */ 429 struct sched_entity **se; 430 /* runqueue "owned" by this group on each CPU */ 431 struct cfs_rq **cfs_rq; 432 unsigned long shares; 433 434 /* A positive value indicates that this is a SCHED_IDLE group. */ 435 int idle; 436 437 #ifdef CONFIG_SMP 438 /* 439 * load_avg can be heavily contended at clock tick time, so put 440 * it in its own cache-line separated from the fields above which 441 * will also be accessed at each tick. 442 */ 443 atomic_long_t load_avg ____cacheline_aligned; 444 #endif 445 #endif 446 447 #ifdef CONFIG_RT_GROUP_SCHED 448 struct sched_rt_entity **rt_se; 449 struct rt_rq **rt_rq; 450 451 struct rt_bandwidth rt_bandwidth; 452 #endif 453 454 struct rcu_head rcu; 455 struct list_head list; 456 457 struct task_group *parent; 458 struct list_head siblings; 459 struct list_head children; 460 461 #ifdef CONFIG_SCHED_AUTOGROUP 462 struct autogroup *autogroup; 463 #endif 464 465 struct cfs_bandwidth cfs_bandwidth; 466 467 #ifdef CONFIG_UCLAMP_TASK_GROUP 468 /* The two decimal precision [%] value requested from user-space */ 469 unsigned int uclamp_pct[UCLAMP_CNT]; 470 /* Clamp values requested for a task group */ 471 struct uclamp_se uclamp_req[UCLAMP_CNT]; 472 /* Effective clamp values used for a task group */ 473 struct uclamp_se uclamp[UCLAMP_CNT]; 474 #endif 475 476 }; 477 478 #ifdef CONFIG_FAIR_GROUP_SCHED 479 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD 480 481 /* 482 * A weight of 0 or 1 can cause arithmetics problems. 483 * A weight of a cfs_rq is the sum of weights of which entities 484 * are queued on this cfs_rq, so a weight of a entity should not be 485 * too large, so as the shares value of a task group. 486 * (The default weight is 1024 - so there's no practical 487 * limitation from this.) 488 */ 489 #define MIN_SHARES (1UL << 1) 490 #define MAX_SHARES (1UL << 18) 491 #endif 492 493 typedef int (*tg_visitor)(struct task_group *, void *); 494 495 extern int walk_tg_tree_from(struct task_group *from, 496 tg_visitor down, tg_visitor up, void *data); 497 498 /* 499 * Iterate the full tree, calling @down when first entering a node and @up when 500 * leaving it for the final time. 501 * 502 * Caller must hold rcu_lock or sufficient equivalent. 503 */ 504 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) 505 { 506 return walk_tg_tree_from(&root_task_group, down, up, data); 507 } 508 509 extern int tg_nop(struct task_group *tg, void *data); 510 511 #ifdef CONFIG_FAIR_GROUP_SCHED 512 extern void free_fair_sched_group(struct task_group *tg); 513 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent); 514 extern void online_fair_sched_group(struct task_group *tg); 515 extern void unregister_fair_sched_group(struct task_group *tg); 516 #else 517 static inline void free_fair_sched_group(struct task_group *tg) { } 518 static inline int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) 519 { 520 return 1; 521 } 522 static inline void online_fair_sched_group(struct task_group *tg) { } 523 static inline void unregister_fair_sched_group(struct task_group *tg) { } 524 #endif 525 526 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, 527 struct sched_entity *se, int cpu, 528 struct sched_entity *parent); 529 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent); 530 531 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b); 532 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b); 533 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq); 534 extern bool cfs_task_bw_constrained(struct task_struct *p); 535 536 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, 537 struct sched_rt_entity *rt_se, int cpu, 538 struct sched_rt_entity *parent); 539 extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us); 540 extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us); 541 extern long sched_group_rt_runtime(struct task_group *tg); 542 extern long sched_group_rt_period(struct task_group *tg); 543 extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk); 544 545 extern struct task_group *sched_create_group(struct task_group *parent); 546 extern void sched_online_group(struct task_group *tg, 547 struct task_group *parent); 548 extern void sched_destroy_group(struct task_group *tg); 549 extern void sched_release_group(struct task_group *tg); 550 551 extern void sched_move_task(struct task_struct *tsk); 552 553 #ifdef CONFIG_FAIR_GROUP_SCHED 554 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); 555 556 extern int sched_group_set_idle(struct task_group *tg, long idle); 557 558 #ifdef CONFIG_SMP 559 extern void set_task_rq_fair(struct sched_entity *se, 560 struct cfs_rq *prev, struct cfs_rq *next); 561 #else /* !CONFIG_SMP */ 562 static inline void set_task_rq_fair(struct sched_entity *se, 563 struct cfs_rq *prev, struct cfs_rq *next) { } 564 #endif /* CONFIG_SMP */ 565 #endif /* CONFIG_FAIR_GROUP_SCHED */ 566 567 #else /* CONFIG_CGROUP_SCHED */ 568 569 struct cfs_bandwidth { }; 570 571 static inline bool cfs_task_bw_constrained(struct task_struct *p) { return false; } 572 573 #endif /* CONFIG_CGROUP_SCHED */ 574 575 extern void unregister_rt_sched_group(struct task_group *tg); 576 extern void free_rt_sched_group(struct task_group *tg); 577 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent); 578 579 /* 580 * u64_u32_load/u64_u32_store 581 * 582 * Use a copy of a u64 value to protect against data race. This is only 583 * applicable for 32-bits architectures. 584 */ 585 #ifdef CONFIG_64BIT 586 # define u64_u32_load_copy(var, copy) var 587 # define u64_u32_store_copy(var, copy, val) (var = val) 588 #else 589 # define u64_u32_load_copy(var, copy) \ 590 ({ \ 591 u64 __val, __val_copy; \ 592 do { \ 593 __val_copy = copy; \ 594 /* \ 595 * paired with u64_u32_store_copy(), ordering access \ 596 * to var and copy. \ 597 */ \ 598 smp_rmb(); \ 599 __val = var; \ 600 } while (__val != __val_copy); \ 601 __val; \ 602 }) 603 # define u64_u32_store_copy(var, copy, val) \ 604 do { \ 605 typeof(val) __val = (val); \ 606 var = __val; \ 607 /* \ 608 * paired with u64_u32_load_copy(), ordering access to var and \ 609 * copy. \ 610 */ \ 611 smp_wmb(); \ 612 copy = __val; \ 613 } while (0) 614 #endif 615 # define u64_u32_load(var) u64_u32_load_copy(var, var##_copy) 616 # define u64_u32_store(var, val) u64_u32_store_copy(var, var##_copy, val) 617 618 /* CFS-related fields in a runqueue */ 619 struct cfs_rq { 620 struct load_weight load; 621 unsigned int nr_running; 622 unsigned int h_nr_running; /* SCHED_{NORMAL,BATCH,IDLE} */ 623 unsigned int idle_nr_running; /* SCHED_IDLE */ 624 unsigned int idle_h_nr_running; /* SCHED_IDLE */ 625 626 s64 avg_vruntime; 627 u64 avg_load; 628 629 u64 exec_clock; 630 u64 min_vruntime; 631 #ifdef CONFIG_SCHED_CORE 632 unsigned int forceidle_seq; 633 u64 min_vruntime_fi; 634 #endif 635 636 #ifndef CONFIG_64BIT 637 u64 min_vruntime_copy; 638 #endif 639 640 struct rb_root_cached tasks_timeline; 641 642 /* 643 * 'curr' points to currently running entity on this cfs_rq. 644 * It is set to NULL otherwise (i.e when none are currently running). 645 */ 646 struct sched_entity *curr; 647 struct sched_entity *next; 648 649 #ifdef CONFIG_SCHED_DEBUG 650 unsigned int nr_spread_over; 651 #endif 652 653 #ifdef CONFIG_SMP 654 /* 655 * CFS load tracking 656 */ 657 struct sched_avg avg; 658 #ifndef CONFIG_64BIT 659 u64 last_update_time_copy; 660 #endif 661 struct { 662 raw_spinlock_t lock ____cacheline_aligned; 663 int nr; 664 unsigned long load_avg; 665 unsigned long util_avg; 666 unsigned long runnable_avg; 667 } removed; 668 669 #ifdef CONFIG_FAIR_GROUP_SCHED 670 u64 last_update_tg_load_avg; 671 unsigned long tg_load_avg_contrib; 672 long propagate; 673 long prop_runnable_sum; 674 675 /* 676 * h_load = weight * f(tg) 677 * 678 * Where f(tg) is the recursive weight fraction assigned to 679 * this group. 680 */ 681 unsigned long h_load; 682 u64 last_h_load_update; 683 struct sched_entity *h_load_next; 684 #endif /* CONFIG_FAIR_GROUP_SCHED */ 685 #endif /* CONFIG_SMP */ 686 687 #ifdef CONFIG_FAIR_GROUP_SCHED 688 struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */ 689 690 /* 691 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in 692 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities 693 * (like users, containers etc.) 694 * 695 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU. 696 * This list is used during load balance. 697 */ 698 int on_list; 699 struct list_head leaf_cfs_rq_list; 700 struct task_group *tg; /* group that "owns" this runqueue */ 701 702 /* Locally cached copy of our task_group's idle value */ 703 int idle; 704 705 #ifdef CONFIG_CFS_BANDWIDTH 706 int runtime_enabled; 707 s64 runtime_remaining; 708 709 u64 throttled_pelt_idle; 710 #ifndef CONFIG_64BIT 711 u64 throttled_pelt_idle_copy; 712 #endif 713 u64 throttled_clock; 714 u64 throttled_clock_pelt; 715 u64 throttled_clock_pelt_time; 716 u64 throttled_clock_self; 717 u64 throttled_clock_self_time; 718 int throttled; 719 int throttle_count; 720 struct list_head throttled_list; 721 struct list_head throttled_csd_list; 722 #endif /* CONFIG_CFS_BANDWIDTH */ 723 #endif /* CONFIG_FAIR_GROUP_SCHED */ 724 }; 725 726 #ifdef CONFIG_SCHED_CLASS_EXT 727 struct scx_rq { 728 struct scx_dispatch_q local_dsq; 729 struct list_head runnable_list; /* runnable tasks on this rq */ 730 unsigned long ops_qseq; 731 u64 extra_enq_flags; /* see move_task_to_local_dsq() */ 732 u32 nr_running; 733 }; 734 #endif /* CONFIG_SCHED_CLASS_EXT */ 735 736 static inline int rt_bandwidth_enabled(void) 737 { 738 return sysctl_sched_rt_runtime >= 0; 739 } 740 741 /* RT IPI pull logic requires IRQ_WORK */ 742 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP) 743 # define HAVE_RT_PUSH_IPI 744 #endif 745 746 /* Real-Time classes' related field in a runqueue: */ 747 struct rt_rq { 748 struct rt_prio_array active; 749 unsigned int rt_nr_running; 750 unsigned int rr_nr_running; 751 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED 752 struct { 753 int curr; /* highest queued rt task prio */ 754 #ifdef CONFIG_SMP 755 int next; /* next highest */ 756 #endif 757 } highest_prio; 758 #endif 759 #ifdef CONFIG_SMP 760 bool overloaded; 761 struct plist_head pushable_tasks; 762 763 #endif /* CONFIG_SMP */ 764 int rt_queued; 765 766 int rt_throttled; 767 u64 rt_time; 768 u64 rt_runtime; 769 /* Nests inside the rq lock: */ 770 raw_spinlock_t rt_runtime_lock; 771 772 #ifdef CONFIG_RT_GROUP_SCHED 773 unsigned int rt_nr_boosted; 774 775 struct rq *rq; 776 struct task_group *tg; 777 #endif 778 }; 779 780 static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq) 781 { 782 return rt_rq->rt_queued && rt_rq->rt_nr_running; 783 } 784 785 /* Deadline class' related fields in a runqueue */ 786 struct dl_rq { 787 /* runqueue is an rbtree, ordered by deadline */ 788 struct rb_root_cached root; 789 790 unsigned int dl_nr_running; 791 792 #ifdef CONFIG_SMP 793 /* 794 * Deadline values of the currently executing and the 795 * earliest ready task on this rq. Caching these facilitates 796 * the decision whether or not a ready but not running task 797 * should migrate somewhere else. 798 */ 799 struct { 800 u64 curr; 801 u64 next; 802 } earliest_dl; 803 804 bool overloaded; 805 806 /* 807 * Tasks on this rq that can be pushed away. They are kept in 808 * an rb-tree, ordered by tasks' deadlines, with caching 809 * of the leftmost (earliest deadline) element. 810 */ 811 struct rb_root_cached pushable_dl_tasks_root; 812 #else 813 struct dl_bw dl_bw; 814 #endif 815 /* 816 * "Active utilization" for this runqueue: increased when a 817 * task wakes up (becomes TASK_RUNNING) and decreased when a 818 * task blocks 819 */ 820 u64 running_bw; 821 822 /* 823 * Utilization of the tasks "assigned" to this runqueue (including 824 * the tasks that are in runqueue and the tasks that executed on this 825 * CPU and blocked). Increased when a task moves to this runqueue, and 826 * decreased when the task moves away (migrates, changes scheduling 827 * policy, or terminates). 828 * This is needed to compute the "inactive utilization" for the 829 * runqueue (inactive utilization = this_bw - running_bw). 830 */ 831 u64 this_bw; 832 u64 extra_bw; 833 834 /* 835 * Maximum available bandwidth for reclaiming by SCHED_FLAG_RECLAIM 836 * tasks of this rq. Used in calculation of reclaimable bandwidth(GRUB). 837 */ 838 u64 max_bw; 839 840 /* 841 * Inverse of the fraction of CPU utilization that can be reclaimed 842 * by the GRUB algorithm. 843 */ 844 u64 bw_ratio; 845 }; 846 847 #ifdef CONFIG_FAIR_GROUP_SCHED 848 849 /* An entity is a task if it doesn't "own" a runqueue */ 850 #define entity_is_task(se) (!se->my_q) 851 852 static inline void se_update_runnable(struct sched_entity *se) 853 { 854 if (!entity_is_task(se)) 855 se->runnable_weight = se->my_q->h_nr_running; 856 } 857 858 static inline long se_runnable(struct sched_entity *se) 859 { 860 if (entity_is_task(se)) 861 return !!se->on_rq; 862 else 863 return se->runnable_weight; 864 } 865 866 #else /* !CONFIG_FAIR_GROUP_SCHED: */ 867 868 #define entity_is_task(se) 1 869 870 static inline void se_update_runnable(struct sched_entity *se) { } 871 872 static inline long se_runnable(struct sched_entity *se) 873 { 874 return !!se->on_rq; 875 } 876 877 #endif /* !CONFIG_FAIR_GROUP_SCHED */ 878 879 #ifdef CONFIG_SMP 880 /* 881 * XXX we want to get rid of these helpers and use the full load resolution. 882 */ 883 static inline long se_weight(struct sched_entity *se) 884 { 885 return scale_load_down(se->load.weight); 886 } 887 888 889 static inline bool sched_asym_prefer(int a, int b) 890 { 891 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b); 892 } 893 894 struct perf_domain { 895 struct em_perf_domain *em_pd; 896 struct perf_domain *next; 897 struct rcu_head rcu; 898 }; 899 900 /* 901 * We add the notion of a root-domain which will be used to define per-domain 902 * variables. Each exclusive cpuset essentially defines an island domain by 903 * fully partitioning the member CPUs from any other cpuset. Whenever a new 904 * exclusive cpuset is created, we also create and attach a new root-domain 905 * object. 906 * 907 */ 908 struct root_domain { 909 atomic_t refcount; 910 atomic_t rto_count; 911 struct rcu_head rcu; 912 cpumask_var_t span; 913 cpumask_var_t online; 914 915 /* 916 * Indicate pullable load on at least one CPU, e.g: 917 * - More than one runnable task 918 * - Running task is misfit 919 */ 920 bool overloaded; 921 922 /* Indicate one or more CPUs over-utilized (tipping point) */ 923 bool overutilized; 924 925 /* 926 * The bit corresponding to a CPU gets set here if such CPU has more 927 * than one runnable -deadline task (as it is below for RT tasks). 928 */ 929 cpumask_var_t dlo_mask; 930 atomic_t dlo_count; 931 struct dl_bw dl_bw; 932 struct cpudl cpudl; 933 934 /* 935 * Indicate whether a root_domain's dl_bw has been checked or 936 * updated. It's monotonously increasing value. 937 * 938 * Also, some corner cases, like 'wrap around' is dangerous, but given 939 * that u64 is 'big enough'. So that shouldn't be a concern. 940 */ 941 u64 visit_gen; 942 943 #ifdef HAVE_RT_PUSH_IPI 944 /* 945 * For IPI pull requests, loop across the rto_mask. 946 */ 947 struct irq_work rto_push_work; 948 raw_spinlock_t rto_lock; 949 /* These are only updated and read within rto_lock */ 950 int rto_loop; 951 int rto_cpu; 952 /* These atomics are updated outside of a lock */ 953 atomic_t rto_loop_next; 954 atomic_t rto_loop_start; 955 #endif 956 /* 957 * The "RT overload" flag: it gets set if a CPU has more than 958 * one runnable RT task. 959 */ 960 cpumask_var_t rto_mask; 961 struct cpupri cpupri; 962 963 /* 964 * NULL-terminated list of performance domains intersecting with the 965 * CPUs of the rd. Protected by RCU. 966 */ 967 struct perf_domain __rcu *pd; 968 }; 969 970 extern void init_defrootdomain(void); 971 extern int sched_init_domains(const struct cpumask *cpu_map); 972 extern void rq_attach_root(struct rq *rq, struct root_domain *rd); 973 extern void sched_get_rd(struct root_domain *rd); 974 extern void sched_put_rd(struct root_domain *rd); 975 976 static inline int get_rd_overloaded(struct root_domain *rd) 977 { 978 return READ_ONCE(rd->overloaded); 979 } 980 981 static inline void set_rd_overloaded(struct root_domain *rd, int status) 982 { 983 if (get_rd_overloaded(rd) != status) 984 WRITE_ONCE(rd->overloaded, status); 985 } 986 987 #ifdef HAVE_RT_PUSH_IPI 988 extern void rto_push_irq_work_func(struct irq_work *work); 989 #endif 990 #endif /* CONFIG_SMP */ 991 992 #ifdef CONFIG_UCLAMP_TASK 993 /* 994 * struct uclamp_bucket - Utilization clamp bucket 995 * @value: utilization clamp value for tasks on this clamp bucket 996 * @tasks: number of RUNNABLE tasks on this clamp bucket 997 * 998 * Keep track of how many tasks are RUNNABLE for a given utilization 999 * clamp value. 1000 */ 1001 struct uclamp_bucket { 1002 unsigned long value : bits_per(SCHED_CAPACITY_SCALE); 1003 unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE); 1004 }; 1005 1006 /* 1007 * struct uclamp_rq - rq's utilization clamp 1008 * @value: currently active clamp values for a rq 1009 * @bucket: utilization clamp buckets affecting a rq 1010 * 1011 * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values. 1012 * A clamp value is affecting a rq when there is at least one task RUNNABLE 1013 * (or actually running) with that value. 1014 * 1015 * There are up to UCLAMP_CNT possible different clamp values, currently there 1016 * are only two: minimum utilization and maximum utilization. 1017 * 1018 * All utilization clamping values are MAX aggregated, since: 1019 * - for util_min: we want to run the CPU at least at the max of the minimum 1020 * utilization required by its currently RUNNABLE tasks. 1021 * - for util_max: we want to allow the CPU to run up to the max of the 1022 * maximum utilization allowed by its currently RUNNABLE tasks. 1023 * 1024 * Since on each system we expect only a limited number of different 1025 * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track 1026 * the metrics required to compute all the per-rq utilization clamp values. 1027 */ 1028 struct uclamp_rq { 1029 unsigned int value; 1030 struct uclamp_bucket bucket[UCLAMP_BUCKETS]; 1031 }; 1032 1033 DECLARE_STATIC_KEY_FALSE(sched_uclamp_used); 1034 #endif /* CONFIG_UCLAMP_TASK */ 1035 1036 struct balance_callback { 1037 struct balance_callback *next; 1038 void (*func)(struct rq *rq); 1039 }; 1040 1041 /* 1042 * This is the main, per-CPU runqueue data structure. 1043 * 1044 * Locking rule: those places that want to lock multiple runqueues 1045 * (such as the load balancing or the thread migration code), lock 1046 * acquire operations must be ordered by ascending &runqueue. 1047 */ 1048 struct rq { 1049 /* runqueue lock: */ 1050 raw_spinlock_t __lock; 1051 1052 unsigned int nr_running; 1053 #ifdef CONFIG_NUMA_BALANCING 1054 unsigned int nr_numa_running; 1055 unsigned int nr_preferred_running; 1056 unsigned int numa_migrate_on; 1057 #endif 1058 #ifdef CONFIG_NO_HZ_COMMON 1059 #ifdef CONFIG_SMP 1060 unsigned long last_blocked_load_update_tick; 1061 unsigned int has_blocked_load; 1062 call_single_data_t nohz_csd; 1063 #endif /* CONFIG_SMP */ 1064 unsigned int nohz_tick_stopped; 1065 atomic_t nohz_flags; 1066 #endif /* CONFIG_NO_HZ_COMMON */ 1067 1068 #ifdef CONFIG_SMP 1069 unsigned int ttwu_pending; 1070 #endif 1071 u64 nr_switches; 1072 1073 #ifdef CONFIG_UCLAMP_TASK 1074 /* Utilization clamp values based on CPU's RUNNABLE tasks */ 1075 struct uclamp_rq uclamp[UCLAMP_CNT] ____cacheline_aligned; 1076 unsigned int uclamp_flags; 1077 #define UCLAMP_FLAG_IDLE 0x01 1078 #endif 1079 1080 struct cfs_rq cfs; 1081 struct rt_rq rt; 1082 struct dl_rq dl; 1083 #ifdef CONFIG_SCHED_CLASS_EXT 1084 struct scx_rq scx; 1085 #endif 1086 1087 #ifdef CONFIG_FAIR_GROUP_SCHED 1088 /* list of leaf cfs_rq on this CPU: */ 1089 struct list_head leaf_cfs_rq_list; 1090 struct list_head *tmp_alone_branch; 1091 #endif /* CONFIG_FAIR_GROUP_SCHED */ 1092 1093 /* 1094 * This is part of a global counter where only the total sum 1095 * over all CPUs matters. A task can increase this counter on 1096 * one CPU and if it got migrated afterwards it may decrease 1097 * it on another CPU. Always updated under the runqueue lock: 1098 */ 1099 unsigned int nr_uninterruptible; 1100 1101 struct task_struct __rcu *curr; 1102 struct task_struct *idle; 1103 struct task_struct *stop; 1104 unsigned long next_balance; 1105 struct mm_struct *prev_mm; 1106 1107 unsigned int clock_update_flags; 1108 u64 clock; 1109 /* Ensure that all clocks are in the same cache line */ 1110 u64 clock_task ____cacheline_aligned; 1111 u64 clock_pelt; 1112 unsigned long lost_idle_time; 1113 u64 clock_pelt_idle; 1114 u64 clock_idle; 1115 #ifndef CONFIG_64BIT 1116 u64 clock_pelt_idle_copy; 1117 u64 clock_idle_copy; 1118 #endif 1119 1120 atomic_t nr_iowait; 1121 1122 #ifdef CONFIG_SCHED_DEBUG 1123 u64 last_seen_need_resched_ns; 1124 int ticks_without_resched; 1125 #endif 1126 1127 #ifdef CONFIG_MEMBARRIER 1128 int membarrier_state; 1129 #endif 1130 1131 #ifdef CONFIG_SMP 1132 struct root_domain *rd; 1133 struct sched_domain __rcu *sd; 1134 1135 unsigned long cpu_capacity; 1136 1137 struct balance_callback *balance_callback; 1138 1139 unsigned char nohz_idle_balance; 1140 unsigned char idle_balance; 1141 1142 unsigned long misfit_task_load; 1143 1144 /* For active balancing */ 1145 int active_balance; 1146 int push_cpu; 1147 struct cpu_stop_work active_balance_work; 1148 1149 /* CPU of this runqueue: */ 1150 int cpu; 1151 int online; 1152 1153 struct list_head cfs_tasks; 1154 1155 struct sched_avg avg_rt; 1156 struct sched_avg avg_dl; 1157 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ 1158 struct sched_avg avg_irq; 1159 #endif 1160 #ifdef CONFIG_SCHED_HW_PRESSURE 1161 struct sched_avg avg_hw; 1162 #endif 1163 u64 idle_stamp; 1164 u64 avg_idle; 1165 1166 /* This is used to determine avg_idle's max value */ 1167 u64 max_idle_balance_cost; 1168 1169 #ifdef CONFIG_HOTPLUG_CPU 1170 struct rcuwait hotplug_wait; 1171 #endif 1172 #endif /* CONFIG_SMP */ 1173 1174 #ifdef CONFIG_IRQ_TIME_ACCOUNTING 1175 u64 prev_irq_time; 1176 #endif 1177 #ifdef CONFIG_PARAVIRT 1178 u64 prev_steal_time; 1179 #endif 1180 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING 1181 u64 prev_steal_time_rq; 1182 #endif 1183 1184 /* calc_load related fields */ 1185 unsigned long calc_load_update; 1186 long calc_load_active; 1187 1188 #ifdef CONFIG_SCHED_HRTICK 1189 #ifdef CONFIG_SMP 1190 call_single_data_t hrtick_csd; 1191 #endif 1192 struct hrtimer hrtick_timer; 1193 ktime_t hrtick_time; 1194 #endif 1195 1196 #ifdef CONFIG_SCHEDSTATS 1197 /* latency stats */ 1198 struct sched_info rq_sched_info; 1199 unsigned long long rq_cpu_time; 1200 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ 1201 1202 /* sys_sched_yield() stats */ 1203 unsigned int yld_count; 1204 1205 /* schedule() stats */ 1206 unsigned int sched_count; 1207 unsigned int sched_goidle; 1208 1209 /* try_to_wake_up() stats */ 1210 unsigned int ttwu_count; 1211 unsigned int ttwu_local; 1212 #endif 1213 1214 #ifdef CONFIG_CPU_IDLE 1215 /* Must be inspected within a RCU lock section */ 1216 struct cpuidle_state *idle_state; 1217 #endif 1218 1219 #ifdef CONFIG_SMP 1220 unsigned int nr_pinned; 1221 #endif 1222 unsigned int push_busy; 1223 struct cpu_stop_work push_work; 1224 1225 #ifdef CONFIG_SCHED_CORE 1226 /* per rq */ 1227 struct rq *core; 1228 struct task_struct *core_pick; 1229 unsigned int core_enabled; 1230 unsigned int core_sched_seq; 1231 struct rb_root core_tree; 1232 1233 /* shared state -- careful with sched_core_cpu_deactivate() */ 1234 unsigned int core_task_seq; 1235 unsigned int core_pick_seq; 1236 unsigned long core_cookie; 1237 unsigned int core_forceidle_count; 1238 unsigned int core_forceidle_seq; 1239 unsigned int core_forceidle_occupation; 1240 u64 core_forceidle_start; 1241 #endif 1242 1243 /* Scratch cpumask to be temporarily used under rq_lock */ 1244 cpumask_var_t scratch_mask; 1245 1246 #if defined(CONFIG_CFS_BANDWIDTH) && defined(CONFIG_SMP) 1247 call_single_data_t cfsb_csd; 1248 struct list_head cfsb_csd_list; 1249 #endif 1250 }; 1251 1252 #ifdef CONFIG_FAIR_GROUP_SCHED 1253 1254 /* CPU runqueue to which this cfs_rq is attached */ 1255 static inline struct rq *rq_of(struct cfs_rq *cfs_rq) 1256 { 1257 return cfs_rq->rq; 1258 } 1259 1260 #else 1261 1262 static inline struct rq *rq_of(struct cfs_rq *cfs_rq) 1263 { 1264 return container_of(cfs_rq, struct rq, cfs); 1265 } 1266 #endif 1267 1268 static inline int cpu_of(struct rq *rq) 1269 { 1270 #ifdef CONFIG_SMP 1271 return rq->cpu; 1272 #else 1273 return 0; 1274 #endif 1275 } 1276 1277 #define MDF_PUSH 0x01 1278 1279 static inline bool is_migration_disabled(struct task_struct *p) 1280 { 1281 #ifdef CONFIG_SMP 1282 return p->migration_disabled; 1283 #else 1284 return false; 1285 #endif 1286 } 1287 1288 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); 1289 1290 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) 1291 #define this_rq() this_cpu_ptr(&runqueues) 1292 #define task_rq(p) cpu_rq(task_cpu(p)) 1293 #define cpu_curr(cpu) (cpu_rq(cpu)->curr) 1294 #define raw_rq() raw_cpu_ptr(&runqueues) 1295 1296 #ifdef CONFIG_SCHED_CORE 1297 static inline struct cpumask *sched_group_span(struct sched_group *sg); 1298 1299 DECLARE_STATIC_KEY_FALSE(__sched_core_enabled); 1300 1301 static inline bool sched_core_enabled(struct rq *rq) 1302 { 1303 return static_branch_unlikely(&__sched_core_enabled) && rq->core_enabled; 1304 } 1305 1306 static inline bool sched_core_disabled(void) 1307 { 1308 return !static_branch_unlikely(&__sched_core_enabled); 1309 } 1310 1311 /* 1312 * Be careful with this function; not for general use. The return value isn't 1313 * stable unless you actually hold a relevant rq->__lock. 1314 */ 1315 static inline raw_spinlock_t *rq_lockp(struct rq *rq) 1316 { 1317 if (sched_core_enabled(rq)) 1318 return &rq->core->__lock; 1319 1320 return &rq->__lock; 1321 } 1322 1323 static inline raw_spinlock_t *__rq_lockp(struct rq *rq) 1324 { 1325 if (rq->core_enabled) 1326 return &rq->core->__lock; 1327 1328 return &rq->__lock; 1329 } 1330 1331 extern bool 1332 cfs_prio_less(const struct task_struct *a, const struct task_struct *b, bool fi); 1333 1334 extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi); 1335 1336 /* 1337 * Helpers to check if the CPU's core cookie matches with the task's cookie 1338 * when core scheduling is enabled. 1339 * A special case is that the task's cookie always matches with CPU's core 1340 * cookie if the CPU is in an idle core. 1341 */ 1342 static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p) 1343 { 1344 /* Ignore cookie match if core scheduler is not enabled on the CPU. */ 1345 if (!sched_core_enabled(rq)) 1346 return true; 1347 1348 return rq->core->core_cookie == p->core_cookie; 1349 } 1350 1351 static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) 1352 { 1353 bool idle_core = true; 1354 int cpu; 1355 1356 /* Ignore cookie match if core scheduler is not enabled on the CPU. */ 1357 if (!sched_core_enabled(rq)) 1358 return true; 1359 1360 for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) { 1361 if (!available_idle_cpu(cpu)) { 1362 idle_core = false; 1363 break; 1364 } 1365 } 1366 1367 /* 1368 * A CPU in an idle core is always the best choice for tasks with 1369 * cookies. 1370 */ 1371 return idle_core || rq->core->core_cookie == p->core_cookie; 1372 } 1373 1374 static inline bool sched_group_cookie_match(struct rq *rq, 1375 struct task_struct *p, 1376 struct sched_group *group) 1377 { 1378 int cpu; 1379 1380 /* Ignore cookie match if core scheduler is not enabled on the CPU. */ 1381 if (!sched_core_enabled(rq)) 1382 return true; 1383 1384 for_each_cpu_and(cpu, sched_group_span(group), p->cpus_ptr) { 1385 if (sched_core_cookie_match(cpu_rq(cpu), p)) 1386 return true; 1387 } 1388 return false; 1389 } 1390 1391 static inline bool sched_core_enqueued(struct task_struct *p) 1392 { 1393 return !RB_EMPTY_NODE(&p->core_node); 1394 } 1395 1396 extern void sched_core_enqueue(struct rq *rq, struct task_struct *p); 1397 extern void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags); 1398 1399 extern void sched_core_get(void); 1400 extern void sched_core_put(void); 1401 1402 #else /* !CONFIG_SCHED_CORE: */ 1403 1404 static inline bool sched_core_enabled(struct rq *rq) 1405 { 1406 return false; 1407 } 1408 1409 static inline bool sched_core_disabled(void) 1410 { 1411 return true; 1412 } 1413 1414 static inline raw_spinlock_t *rq_lockp(struct rq *rq) 1415 { 1416 return &rq->__lock; 1417 } 1418 1419 static inline raw_spinlock_t *__rq_lockp(struct rq *rq) 1420 { 1421 return &rq->__lock; 1422 } 1423 1424 static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p) 1425 { 1426 return true; 1427 } 1428 1429 static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) 1430 { 1431 return true; 1432 } 1433 1434 static inline bool sched_group_cookie_match(struct rq *rq, 1435 struct task_struct *p, 1436 struct sched_group *group) 1437 { 1438 return true; 1439 } 1440 1441 #endif /* !CONFIG_SCHED_CORE */ 1442 1443 static inline void lockdep_assert_rq_held(struct rq *rq) 1444 { 1445 lockdep_assert_held(__rq_lockp(rq)); 1446 } 1447 1448 extern void raw_spin_rq_lock_nested(struct rq *rq, int subclass); 1449 extern bool raw_spin_rq_trylock(struct rq *rq); 1450 extern void raw_spin_rq_unlock(struct rq *rq); 1451 1452 static inline void raw_spin_rq_lock(struct rq *rq) 1453 { 1454 raw_spin_rq_lock_nested(rq, 0); 1455 } 1456 1457 static inline void raw_spin_rq_lock_irq(struct rq *rq) 1458 { 1459 local_irq_disable(); 1460 raw_spin_rq_lock(rq); 1461 } 1462 1463 static inline void raw_spin_rq_unlock_irq(struct rq *rq) 1464 { 1465 raw_spin_rq_unlock(rq); 1466 local_irq_enable(); 1467 } 1468 1469 static inline unsigned long _raw_spin_rq_lock_irqsave(struct rq *rq) 1470 { 1471 unsigned long flags; 1472 1473 local_irq_save(flags); 1474 raw_spin_rq_lock(rq); 1475 1476 return flags; 1477 } 1478 1479 static inline void raw_spin_rq_unlock_irqrestore(struct rq *rq, unsigned long flags) 1480 { 1481 raw_spin_rq_unlock(rq); 1482 local_irq_restore(flags); 1483 } 1484 1485 #define raw_spin_rq_lock_irqsave(rq, flags) \ 1486 do { \ 1487 flags = _raw_spin_rq_lock_irqsave(rq); \ 1488 } while (0) 1489 1490 #ifdef CONFIG_SCHED_SMT 1491 extern void __update_idle_core(struct rq *rq); 1492 1493 static inline void update_idle_core(struct rq *rq) 1494 { 1495 if (static_branch_unlikely(&sched_smt_present)) 1496 __update_idle_core(rq); 1497 } 1498 1499 #else 1500 static inline void update_idle_core(struct rq *rq) { } 1501 #endif 1502 1503 #ifdef CONFIG_FAIR_GROUP_SCHED 1504 1505 static inline struct task_struct *task_of(struct sched_entity *se) 1506 { 1507 SCHED_WARN_ON(!entity_is_task(se)); 1508 return container_of(se, struct task_struct, se); 1509 } 1510 1511 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) 1512 { 1513 return p->se.cfs_rq; 1514 } 1515 1516 /* runqueue on which this entity is (to be) queued */ 1517 static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se) 1518 { 1519 return se->cfs_rq; 1520 } 1521 1522 /* runqueue "owned" by this group */ 1523 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) 1524 { 1525 return grp->my_q; 1526 } 1527 1528 #else /* !CONFIG_FAIR_GROUP_SCHED: */ 1529 1530 #define task_of(_se) container_of(_se, struct task_struct, se) 1531 1532 static inline struct cfs_rq *task_cfs_rq(const struct task_struct *p) 1533 { 1534 return &task_rq(p)->cfs; 1535 } 1536 1537 static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se) 1538 { 1539 const struct task_struct *p = task_of(se); 1540 struct rq *rq = task_rq(p); 1541 1542 return &rq->cfs; 1543 } 1544 1545 /* runqueue "owned" by this group */ 1546 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) 1547 { 1548 return NULL; 1549 } 1550 1551 #endif /* !CONFIG_FAIR_GROUP_SCHED */ 1552 1553 extern void update_rq_clock(struct rq *rq); 1554 1555 /* 1556 * rq::clock_update_flags bits 1557 * 1558 * %RQCF_REQ_SKIP - will request skipping of clock update on the next 1559 * call to __schedule(). This is an optimisation to avoid 1560 * neighbouring rq clock updates. 1561 * 1562 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is 1563 * in effect and calls to update_rq_clock() are being ignored. 1564 * 1565 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been 1566 * made to update_rq_clock() since the last time rq::lock was pinned. 1567 * 1568 * If inside of __schedule(), clock_update_flags will have been 1569 * shifted left (a left shift is a cheap operation for the fast path 1570 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use, 1571 * 1572 * if (rq-clock_update_flags >= RQCF_UPDATED) 1573 * 1574 * to check if %RQCF_UPDATED is set. It'll never be shifted more than 1575 * one position though, because the next rq_unpin_lock() will shift it 1576 * back. 1577 */ 1578 #define RQCF_REQ_SKIP 0x01 1579 #define RQCF_ACT_SKIP 0x02 1580 #define RQCF_UPDATED 0x04 1581 1582 static inline void assert_clock_updated(struct rq *rq) 1583 { 1584 /* 1585 * The only reason for not seeing a clock update since the 1586 * last rq_pin_lock() is if we're currently skipping updates. 1587 */ 1588 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP); 1589 } 1590 1591 static inline u64 rq_clock(struct rq *rq) 1592 { 1593 lockdep_assert_rq_held(rq); 1594 assert_clock_updated(rq); 1595 1596 return rq->clock; 1597 } 1598 1599 static inline u64 rq_clock_task(struct rq *rq) 1600 { 1601 lockdep_assert_rq_held(rq); 1602 assert_clock_updated(rq); 1603 1604 return rq->clock_task; 1605 } 1606 1607 static inline void rq_clock_skip_update(struct rq *rq) 1608 { 1609 lockdep_assert_rq_held(rq); 1610 rq->clock_update_flags |= RQCF_REQ_SKIP; 1611 } 1612 1613 /* 1614 * See rt task throttling, which is the only time a skip 1615 * request is canceled. 1616 */ 1617 static inline void rq_clock_cancel_skipupdate(struct rq *rq) 1618 { 1619 lockdep_assert_rq_held(rq); 1620 rq->clock_update_flags &= ~RQCF_REQ_SKIP; 1621 } 1622 1623 /* 1624 * During cpu offlining and rq wide unthrottling, we can trigger 1625 * an update_rq_clock() for several cfs and rt runqueues (Typically 1626 * when using list_for_each_entry_*) 1627 * rq_clock_start_loop_update() can be called after updating the clock 1628 * once and before iterating over the list to prevent multiple update. 1629 * After the iterative traversal, we need to call rq_clock_stop_loop_update() 1630 * to clear RQCF_ACT_SKIP of rq->clock_update_flags. 1631 */ 1632 static inline void rq_clock_start_loop_update(struct rq *rq) 1633 { 1634 lockdep_assert_rq_held(rq); 1635 SCHED_WARN_ON(rq->clock_update_flags & RQCF_ACT_SKIP); 1636 rq->clock_update_flags |= RQCF_ACT_SKIP; 1637 } 1638 1639 static inline void rq_clock_stop_loop_update(struct rq *rq) 1640 { 1641 lockdep_assert_rq_held(rq); 1642 rq->clock_update_flags &= ~RQCF_ACT_SKIP; 1643 } 1644 1645 struct rq_flags { 1646 unsigned long flags; 1647 struct pin_cookie cookie; 1648 #ifdef CONFIG_SCHED_DEBUG 1649 /* 1650 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the 1651 * current pin context is stashed here in case it needs to be 1652 * restored in rq_repin_lock(). 1653 */ 1654 unsigned int clock_update_flags; 1655 #endif 1656 }; 1657 1658 extern struct balance_callback balance_push_callback; 1659 1660 /* 1661 * Lockdep annotation that avoids accidental unlocks; it's like a 1662 * sticky/continuous lockdep_assert_held(). 1663 * 1664 * This avoids code that has access to 'struct rq *rq' (basically everything in 1665 * the scheduler) from accidentally unlocking the rq if they do not also have a 1666 * copy of the (on-stack) 'struct rq_flags rf'. 1667 * 1668 * Also see Documentation/locking/lockdep-design.rst. 1669 */ 1670 static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf) 1671 { 1672 rf->cookie = lockdep_pin_lock(__rq_lockp(rq)); 1673 1674 #ifdef CONFIG_SCHED_DEBUG 1675 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); 1676 rf->clock_update_flags = 0; 1677 # ifdef CONFIG_SMP 1678 SCHED_WARN_ON(rq->balance_callback && rq->balance_callback != &balance_push_callback); 1679 # endif 1680 #endif 1681 } 1682 1683 static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf) 1684 { 1685 #ifdef CONFIG_SCHED_DEBUG 1686 if (rq->clock_update_flags > RQCF_ACT_SKIP) 1687 rf->clock_update_flags = RQCF_UPDATED; 1688 #endif 1689 1690 lockdep_unpin_lock(__rq_lockp(rq), rf->cookie); 1691 } 1692 1693 static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf) 1694 { 1695 lockdep_repin_lock(__rq_lockp(rq), rf->cookie); 1696 1697 #ifdef CONFIG_SCHED_DEBUG 1698 /* 1699 * Restore the value we stashed in @rf for this pin context. 1700 */ 1701 rq->clock_update_flags |= rf->clock_update_flags; 1702 #endif 1703 } 1704 1705 extern 1706 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) 1707 __acquires(rq->lock); 1708 1709 extern 1710 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) 1711 __acquires(p->pi_lock) 1712 __acquires(rq->lock); 1713 1714 static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf) 1715 __releases(rq->lock) 1716 { 1717 rq_unpin_lock(rq, rf); 1718 raw_spin_rq_unlock(rq); 1719 } 1720 1721 static inline void 1722 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf) 1723 __releases(rq->lock) 1724 __releases(p->pi_lock) 1725 { 1726 rq_unpin_lock(rq, rf); 1727 raw_spin_rq_unlock(rq); 1728 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); 1729 } 1730 1731 DEFINE_LOCK_GUARD_1(task_rq_lock, struct task_struct, 1732 _T->rq = task_rq_lock(_T->lock, &_T->rf), 1733 task_rq_unlock(_T->rq, _T->lock, &_T->rf), 1734 struct rq *rq; struct rq_flags rf) 1735 1736 static inline void rq_lock_irqsave(struct rq *rq, struct rq_flags *rf) 1737 __acquires(rq->lock) 1738 { 1739 raw_spin_rq_lock_irqsave(rq, rf->flags); 1740 rq_pin_lock(rq, rf); 1741 } 1742 1743 static inline void rq_lock_irq(struct rq *rq, struct rq_flags *rf) 1744 __acquires(rq->lock) 1745 { 1746 raw_spin_rq_lock_irq(rq); 1747 rq_pin_lock(rq, rf); 1748 } 1749 1750 static inline void rq_lock(struct rq *rq, struct rq_flags *rf) 1751 __acquires(rq->lock) 1752 { 1753 raw_spin_rq_lock(rq); 1754 rq_pin_lock(rq, rf); 1755 } 1756 1757 static inline void rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf) 1758 __releases(rq->lock) 1759 { 1760 rq_unpin_lock(rq, rf); 1761 raw_spin_rq_unlock_irqrestore(rq, rf->flags); 1762 } 1763 1764 static inline void rq_unlock_irq(struct rq *rq, struct rq_flags *rf) 1765 __releases(rq->lock) 1766 { 1767 rq_unpin_lock(rq, rf); 1768 raw_spin_rq_unlock_irq(rq); 1769 } 1770 1771 static inline void rq_unlock(struct rq *rq, struct rq_flags *rf) 1772 __releases(rq->lock) 1773 { 1774 rq_unpin_lock(rq, rf); 1775 raw_spin_rq_unlock(rq); 1776 } 1777 1778 DEFINE_LOCK_GUARD_1(rq_lock, struct rq, 1779 rq_lock(_T->lock, &_T->rf), 1780 rq_unlock(_T->lock, &_T->rf), 1781 struct rq_flags rf) 1782 1783 DEFINE_LOCK_GUARD_1(rq_lock_irq, struct rq, 1784 rq_lock_irq(_T->lock, &_T->rf), 1785 rq_unlock_irq(_T->lock, &_T->rf), 1786 struct rq_flags rf) 1787 1788 DEFINE_LOCK_GUARD_1(rq_lock_irqsave, struct rq, 1789 rq_lock_irqsave(_T->lock, &_T->rf), 1790 rq_unlock_irqrestore(_T->lock, &_T->rf), 1791 struct rq_flags rf) 1792 1793 static inline struct rq *this_rq_lock_irq(struct rq_flags *rf) 1794 __acquires(rq->lock) 1795 { 1796 struct rq *rq; 1797 1798 local_irq_disable(); 1799 rq = this_rq(); 1800 rq_lock(rq, rf); 1801 1802 return rq; 1803 } 1804 1805 #ifdef CONFIG_NUMA 1806 1807 enum numa_topology_type { 1808 NUMA_DIRECT, 1809 NUMA_GLUELESS_MESH, 1810 NUMA_BACKPLANE, 1811 }; 1812 1813 extern enum numa_topology_type sched_numa_topology_type; 1814 extern int sched_max_numa_distance; 1815 extern bool find_numa_distance(int distance); 1816 extern void sched_init_numa(int offline_node); 1817 extern void sched_update_numa(int cpu, bool online); 1818 extern void sched_domains_numa_masks_set(unsigned int cpu); 1819 extern void sched_domains_numa_masks_clear(unsigned int cpu); 1820 extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu); 1821 1822 #else /* !CONFIG_NUMA: */ 1823 1824 static inline void sched_init_numa(int offline_node) { } 1825 static inline void sched_update_numa(int cpu, bool online) { } 1826 static inline void sched_domains_numa_masks_set(unsigned int cpu) { } 1827 static inline void sched_domains_numa_masks_clear(unsigned int cpu) { } 1828 1829 static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu) 1830 { 1831 return nr_cpu_ids; 1832 } 1833 1834 #endif /* !CONFIG_NUMA */ 1835 1836 #ifdef CONFIG_NUMA_BALANCING 1837 1838 /* The regions in numa_faults array from task_struct */ 1839 enum numa_faults_stats { 1840 NUMA_MEM = 0, 1841 NUMA_CPU, 1842 NUMA_MEMBUF, 1843 NUMA_CPUBUF 1844 }; 1845 1846 extern void sched_setnuma(struct task_struct *p, int node); 1847 extern int migrate_task_to(struct task_struct *p, int cpu); 1848 extern int migrate_swap(struct task_struct *p, struct task_struct *t, 1849 int cpu, int scpu); 1850 extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p); 1851 1852 #else /* !CONFIG_NUMA_BALANCING: */ 1853 1854 static inline void 1855 init_numa_balancing(unsigned long clone_flags, struct task_struct *p) 1856 { 1857 } 1858 1859 #endif /* !CONFIG_NUMA_BALANCING */ 1860 1861 #ifdef CONFIG_SMP 1862 1863 static inline void 1864 queue_balance_callback(struct rq *rq, 1865 struct balance_callback *head, 1866 void (*func)(struct rq *rq)) 1867 { 1868 lockdep_assert_rq_held(rq); 1869 1870 /* 1871 * Don't (re)queue an already queued item; nor queue anything when 1872 * balance_push() is active, see the comment with 1873 * balance_push_callback. 1874 */ 1875 if (unlikely(head->next || rq->balance_callback == &balance_push_callback)) 1876 return; 1877 1878 head->func = func; 1879 head->next = rq->balance_callback; 1880 rq->balance_callback = head; 1881 } 1882 1883 #define rcu_dereference_check_sched_domain(p) \ 1884 rcu_dereference_check((p), lockdep_is_held(&sched_domains_mutex)) 1885 1886 /* 1887 * The domain tree (rq->sd) is protected by RCU's quiescent state transition. 1888 * See destroy_sched_domains: call_rcu for details. 1889 * 1890 * The domain tree of any CPU may only be accessed from within 1891 * preempt-disabled sections. 1892 */ 1893 #define for_each_domain(cpu, __sd) \ 1894 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \ 1895 __sd; __sd = __sd->parent) 1896 1897 /* A mask of all the SD flags that have the SDF_SHARED_CHILD metaflag */ 1898 #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_SHARED_CHILD)) | 1899 static const unsigned int SD_SHARED_CHILD_MASK = 1900 #include <linux/sched/sd_flags.h> 1901 0; 1902 #undef SD_FLAG 1903 1904 /** 1905 * highest_flag_domain - Return highest sched_domain containing flag. 1906 * @cpu: The CPU whose highest level of sched domain is to 1907 * be returned. 1908 * @flag: The flag to check for the highest sched_domain 1909 * for the given CPU. 1910 * 1911 * Returns the highest sched_domain of a CPU which contains @flag. If @flag has 1912 * the SDF_SHARED_CHILD metaflag, all the children domains also have @flag. 1913 */ 1914 static inline struct sched_domain *highest_flag_domain(int cpu, int flag) 1915 { 1916 struct sched_domain *sd, *hsd = NULL; 1917 1918 for_each_domain(cpu, sd) { 1919 if (sd->flags & flag) { 1920 hsd = sd; 1921 continue; 1922 } 1923 1924 /* 1925 * Stop the search if @flag is known to be shared at lower 1926 * levels. It will not be found further up. 1927 */ 1928 if (flag & SD_SHARED_CHILD_MASK) 1929 break; 1930 } 1931 1932 return hsd; 1933 } 1934 1935 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) 1936 { 1937 struct sched_domain *sd; 1938 1939 for_each_domain(cpu, sd) { 1940 if (sd->flags & flag) 1941 break; 1942 } 1943 1944 return sd; 1945 } 1946 1947 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc); 1948 DECLARE_PER_CPU(int, sd_llc_size); 1949 DECLARE_PER_CPU(int, sd_llc_id); 1950 DECLARE_PER_CPU(int, sd_share_id); 1951 DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared); 1952 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa); 1953 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing); 1954 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity); 1955 1956 extern struct static_key_false sched_asym_cpucapacity; 1957 extern struct static_key_false sched_cluster_active; 1958 1959 static __always_inline bool sched_asym_cpucap_active(void) 1960 { 1961 return static_branch_unlikely(&sched_asym_cpucapacity); 1962 } 1963 1964 struct sched_group_capacity { 1965 atomic_t ref; 1966 /* 1967 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity 1968 * for a single CPU. 1969 */ 1970 unsigned long capacity; 1971 unsigned long min_capacity; /* Min per-CPU capacity in group */ 1972 unsigned long max_capacity; /* Max per-CPU capacity in group */ 1973 unsigned long next_update; 1974 int imbalance; /* XXX unrelated to capacity but shared group state */ 1975 1976 #ifdef CONFIG_SCHED_DEBUG 1977 int id; 1978 #endif 1979 1980 unsigned long cpumask[]; /* Balance mask */ 1981 }; 1982 1983 struct sched_group { 1984 struct sched_group *next; /* Must be a circular list */ 1985 atomic_t ref; 1986 1987 unsigned int group_weight; 1988 unsigned int cores; 1989 struct sched_group_capacity *sgc; 1990 int asym_prefer_cpu; /* CPU of highest priority in group */ 1991 int flags; 1992 1993 /* 1994 * The CPUs this group covers. 1995 * 1996 * NOTE: this field is variable length. (Allocated dynamically 1997 * by attaching extra space to the end of the structure, 1998 * depending on how many CPUs the kernel has booted up with) 1999 */ 2000 unsigned long cpumask[]; 2001 }; 2002 2003 static inline struct cpumask *sched_group_span(struct sched_group *sg) 2004 { 2005 return to_cpumask(sg->cpumask); 2006 } 2007 2008 /* 2009 * See build_balance_mask(). 2010 */ 2011 static inline struct cpumask *group_balance_mask(struct sched_group *sg) 2012 { 2013 return to_cpumask(sg->sgc->cpumask); 2014 } 2015 2016 extern int group_balance_cpu(struct sched_group *sg); 2017 2018 #ifdef CONFIG_SCHED_DEBUG 2019 extern void update_sched_domain_debugfs(void); 2020 extern void dirty_sched_domain_sysctl(int cpu); 2021 #else 2022 static inline void update_sched_domain_debugfs(void) { } 2023 static inline void dirty_sched_domain_sysctl(int cpu) { } 2024 #endif 2025 2026 extern int sched_update_scaling(void); 2027 2028 static inline const struct cpumask *task_user_cpus(struct task_struct *p) 2029 { 2030 if (!p->user_cpus_ptr) 2031 return cpu_possible_mask; /* &init_task.cpus_mask */ 2032 return p->user_cpus_ptr; 2033 } 2034 2035 #endif /* CONFIG_SMP */ 2036 2037 #include "stats.h" 2038 2039 #if defined(CONFIG_SCHED_CORE) && defined(CONFIG_SCHEDSTATS) 2040 2041 extern void __sched_core_account_forceidle(struct rq *rq); 2042 2043 static inline void sched_core_account_forceidle(struct rq *rq) 2044 { 2045 if (schedstat_enabled()) 2046 __sched_core_account_forceidle(rq); 2047 } 2048 2049 extern void __sched_core_tick(struct rq *rq); 2050 2051 static inline void sched_core_tick(struct rq *rq) 2052 { 2053 if (sched_core_enabled(rq) && schedstat_enabled()) 2054 __sched_core_tick(rq); 2055 } 2056 2057 #else /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS): */ 2058 2059 static inline void sched_core_account_forceidle(struct rq *rq) { } 2060 2061 static inline void sched_core_tick(struct rq *rq) { } 2062 2063 #endif /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS) */ 2064 2065 #ifdef CONFIG_CGROUP_SCHED 2066 2067 /* 2068 * Return the group to which this tasks belongs. 2069 * 2070 * We cannot use task_css() and friends because the cgroup subsystem 2071 * changes that value before the cgroup_subsys::attach() method is called, 2072 * therefore we cannot pin it and might observe the wrong value. 2073 * 2074 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup 2075 * core changes this before calling sched_move_task(). 2076 * 2077 * Instead we use a 'copy' which is updated from sched_move_task() while 2078 * holding both task_struct::pi_lock and rq::lock. 2079 */ 2080 static inline struct task_group *task_group(struct task_struct *p) 2081 { 2082 return p->sched_task_group; 2083 } 2084 2085 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ 2086 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) 2087 { 2088 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED) 2089 struct task_group *tg = task_group(p); 2090 #endif 2091 2092 #ifdef CONFIG_FAIR_GROUP_SCHED 2093 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]); 2094 p->se.cfs_rq = tg->cfs_rq[cpu]; 2095 p->se.parent = tg->se[cpu]; 2096 p->se.depth = tg->se[cpu] ? tg->se[cpu]->depth + 1 : 0; 2097 #endif 2098 2099 #ifdef CONFIG_RT_GROUP_SCHED 2100 p->rt.rt_rq = tg->rt_rq[cpu]; 2101 p->rt.parent = tg->rt_se[cpu]; 2102 #endif 2103 } 2104 2105 #else /* !CONFIG_CGROUP_SCHED: */ 2106 2107 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } 2108 2109 static inline struct task_group *task_group(struct task_struct *p) 2110 { 2111 return NULL; 2112 } 2113 2114 #endif /* !CONFIG_CGROUP_SCHED */ 2115 2116 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) 2117 { 2118 set_task_rq(p, cpu); 2119 #ifdef CONFIG_SMP 2120 /* 2121 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be 2122 * successfully executed on another CPU. We must ensure that updates of 2123 * per-task data have been completed by this moment. 2124 */ 2125 smp_wmb(); 2126 WRITE_ONCE(task_thread_info(p)->cpu, cpu); 2127 p->wake_cpu = cpu; 2128 #endif 2129 } 2130 2131 /* 2132 * Tunables that become constants when CONFIG_SCHED_DEBUG is off: 2133 */ 2134 #ifdef CONFIG_SCHED_DEBUG 2135 # define const_debug __read_mostly 2136 #else 2137 # define const_debug const 2138 #endif 2139 2140 #define SCHED_FEAT(name, enabled) \ 2141 __SCHED_FEAT_##name , 2142 2143 enum { 2144 #include "features.h" 2145 __SCHED_FEAT_NR, 2146 }; 2147 2148 #undef SCHED_FEAT 2149 2150 #ifdef CONFIG_SCHED_DEBUG 2151 2152 /* 2153 * To support run-time toggling of sched features, all the translation units 2154 * (but core.c) reference the sysctl_sched_features defined in core.c. 2155 */ 2156 extern const_debug unsigned int sysctl_sched_features; 2157 2158 #ifdef CONFIG_JUMP_LABEL 2159 2160 #define SCHED_FEAT(name, enabled) \ 2161 static __always_inline bool static_branch_##name(struct static_key *key) \ 2162 { \ 2163 return static_key_##enabled(key); \ 2164 } 2165 2166 #include "features.h" 2167 #undef SCHED_FEAT 2168 2169 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; 2170 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x])) 2171 2172 #else /* !CONFIG_JUMP_LABEL: */ 2173 2174 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) 2175 2176 #endif /* !CONFIG_JUMP_LABEL */ 2177 2178 #else /* !SCHED_DEBUG: */ 2179 2180 /* 2181 * Each translation unit has its own copy of sysctl_sched_features to allow 2182 * constants propagation at compile time and compiler optimization based on 2183 * features default. 2184 */ 2185 #define SCHED_FEAT(name, enabled) \ 2186 (1UL << __SCHED_FEAT_##name) * enabled | 2187 static const_debug __maybe_unused unsigned int sysctl_sched_features = 2188 #include "features.h" 2189 0; 2190 #undef SCHED_FEAT 2191 2192 #define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) 2193 2194 #endif /* !SCHED_DEBUG */ 2195 2196 extern struct static_key_false sched_numa_balancing; 2197 extern struct static_key_false sched_schedstats; 2198 2199 static inline u64 global_rt_period(void) 2200 { 2201 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; 2202 } 2203 2204 static inline u64 global_rt_runtime(void) 2205 { 2206 if (sysctl_sched_rt_runtime < 0) 2207 return RUNTIME_INF; 2208 2209 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; 2210 } 2211 2212 static inline int task_current(struct rq *rq, struct task_struct *p) 2213 { 2214 return rq->curr == p; 2215 } 2216 2217 static inline int task_on_cpu(struct rq *rq, struct task_struct *p) 2218 { 2219 #ifdef CONFIG_SMP 2220 return p->on_cpu; 2221 #else 2222 return task_current(rq, p); 2223 #endif 2224 } 2225 2226 static inline int task_on_rq_queued(struct task_struct *p) 2227 { 2228 return p->on_rq == TASK_ON_RQ_QUEUED; 2229 } 2230 2231 static inline int task_on_rq_migrating(struct task_struct *p) 2232 { 2233 return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING; 2234 } 2235 2236 /* Wake flags. The first three directly map to some SD flag value */ 2237 #define WF_EXEC 0x02 /* Wakeup after exec; maps to SD_BALANCE_EXEC */ 2238 #define WF_FORK 0x04 /* Wakeup after fork; maps to SD_BALANCE_FORK */ 2239 #define WF_TTWU 0x08 /* Wakeup; maps to SD_BALANCE_WAKE */ 2240 2241 #define WF_SYNC 0x10 /* Waker goes to sleep after wakeup */ 2242 #define WF_MIGRATED 0x20 /* Internal use, task got migrated */ 2243 #define WF_CURRENT_CPU 0x40 /* Prefer to move the wakee to the current CPU. */ 2244 2245 #ifdef CONFIG_SMP 2246 static_assert(WF_EXEC == SD_BALANCE_EXEC); 2247 static_assert(WF_FORK == SD_BALANCE_FORK); 2248 static_assert(WF_TTWU == SD_BALANCE_WAKE); 2249 #endif 2250 2251 /* 2252 * To aid in avoiding the subversion of "niceness" due to uneven distribution 2253 * of tasks with abnormal "nice" values across CPUs the contribution that 2254 * each task makes to its run queue's load is weighted according to its 2255 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a 2256 * scaled version of the new time slice allocation that they receive on time 2257 * slice expiry etc. 2258 */ 2259 2260 #define WEIGHT_IDLEPRIO 3 2261 #define WMULT_IDLEPRIO 1431655765 2262 2263 extern const int sched_prio_to_weight[40]; 2264 extern const u32 sched_prio_to_wmult[40]; 2265 2266 /* 2267 * {de,en}queue flags: 2268 * 2269 * DEQUEUE_SLEEP - task is no longer runnable 2270 * ENQUEUE_WAKEUP - task just became runnable 2271 * 2272 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks 2273 * are in a known state which allows modification. Such pairs 2274 * should preserve as much state as possible. 2275 * 2276 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location 2277 * in the runqueue. 2278 * 2279 * NOCLOCK - skip the update_rq_clock() (avoids double updates) 2280 * 2281 * MIGRATION - p->on_rq == TASK_ON_RQ_MIGRATING (used for DEADLINE) 2282 * 2283 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified) 2284 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline) 2285 * ENQUEUE_MIGRATED - the task was migrated during wakeup 2286 * 2287 */ 2288 2289 #define DEQUEUE_SLEEP 0x01 2290 #define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */ 2291 #define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */ 2292 #define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */ 2293 #define DEQUEUE_MIGRATING 0x100 /* Matches ENQUEUE_MIGRATING */ 2294 2295 #define ENQUEUE_WAKEUP 0x01 2296 #define ENQUEUE_RESTORE 0x02 2297 #define ENQUEUE_MOVE 0x04 2298 #define ENQUEUE_NOCLOCK 0x08 2299 2300 #define ENQUEUE_HEAD 0x10 2301 #define ENQUEUE_REPLENISH 0x20 2302 #ifdef CONFIG_SMP 2303 #define ENQUEUE_MIGRATED 0x40 2304 #else 2305 #define ENQUEUE_MIGRATED 0x00 2306 #endif 2307 #define ENQUEUE_INITIAL 0x80 2308 #define ENQUEUE_MIGRATING 0x100 2309 2310 #define RETRY_TASK ((void *)-1UL) 2311 2312 struct affinity_context { 2313 const struct cpumask *new_mask; 2314 struct cpumask *user_mask; 2315 unsigned int flags; 2316 }; 2317 2318 extern s64 update_curr_common(struct rq *rq); 2319 2320 struct sched_class { 2321 2322 #ifdef CONFIG_UCLAMP_TASK 2323 int uclamp_enabled; 2324 #endif 2325 2326 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags); 2327 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags); 2328 void (*yield_task) (struct rq *rq); 2329 bool (*yield_to_task)(struct rq *rq, struct task_struct *p); 2330 2331 void (*wakeup_preempt)(struct rq *rq, struct task_struct *p, int flags); 2332 2333 struct task_struct *(*pick_next_task)(struct rq *rq); 2334 2335 void (*put_prev_task)(struct rq *rq, struct task_struct *p); 2336 void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first); 2337 2338 #ifdef CONFIG_SMP 2339 int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf); 2340 int (*select_task_rq)(struct task_struct *p, int task_cpu, int flags); 2341 2342 struct task_struct * (*pick_task)(struct rq *rq); 2343 2344 void (*migrate_task_rq)(struct task_struct *p, int new_cpu); 2345 2346 void (*task_woken)(struct rq *this_rq, struct task_struct *task); 2347 2348 void (*set_cpus_allowed)(struct task_struct *p, struct affinity_context *ctx); 2349 2350 void (*rq_online)(struct rq *rq); 2351 void (*rq_offline)(struct rq *rq); 2352 2353 struct rq *(*find_lock_rq)(struct task_struct *p, struct rq *rq); 2354 #endif 2355 2356 void (*task_tick)(struct rq *rq, struct task_struct *p, int queued); 2357 void (*task_fork)(struct task_struct *p); 2358 void (*task_dead)(struct task_struct *p); 2359 2360 /* 2361 * The switched_from() call is allowed to drop rq->lock, therefore we 2362 * cannot assume the switched_from/switched_to pair is serialized by 2363 * rq->lock. They are however serialized by p->pi_lock. 2364 */ 2365 void (*switching_to) (struct rq *this_rq, struct task_struct *task); 2366 void (*switched_from)(struct rq *this_rq, struct task_struct *task); 2367 void (*switched_to) (struct rq *this_rq, struct task_struct *task); 2368 void (*reweight_task)(struct rq *this_rq, struct task_struct *task, 2369 int newprio); 2370 void (*prio_changed) (struct rq *this_rq, struct task_struct *task, 2371 int oldprio); 2372 2373 unsigned int (*get_rr_interval)(struct rq *rq, 2374 struct task_struct *task); 2375 2376 void (*update_curr)(struct rq *rq); 2377 2378 #ifdef CONFIG_FAIR_GROUP_SCHED 2379 void (*task_change_group)(struct task_struct *p); 2380 #endif 2381 2382 #ifdef CONFIG_SCHED_CORE 2383 int (*task_is_throttled)(struct task_struct *p, int cpu); 2384 #endif 2385 }; 2386 2387 static inline void put_prev_task(struct rq *rq, struct task_struct *prev) 2388 { 2389 WARN_ON_ONCE(rq->curr != prev); 2390 prev->sched_class->put_prev_task(rq, prev); 2391 } 2392 2393 static inline void set_next_task(struct rq *rq, struct task_struct *next) 2394 { 2395 next->sched_class->set_next_task(rq, next, false); 2396 } 2397 2398 2399 /* 2400 * Helper to define a sched_class instance; each one is placed in a separate 2401 * section which is ordered by the linker script: 2402 * 2403 * include/asm-generic/vmlinux.lds.h 2404 * 2405 * *CAREFUL* they are laid out in *REVERSE* order!!! 2406 * 2407 * Also enforce alignment on the instance, not the type, to guarantee layout. 2408 */ 2409 #define DEFINE_SCHED_CLASS(name) \ 2410 const struct sched_class name##_sched_class \ 2411 __aligned(__alignof__(struct sched_class)) \ 2412 __section("__" #name "_sched_class") 2413 2414 /* Defined in include/asm-generic/vmlinux.lds.h */ 2415 extern struct sched_class __sched_class_highest[]; 2416 extern struct sched_class __sched_class_lowest[]; 2417 2418 #define for_class_range(class, _from, _to) \ 2419 for (class = (_from); class < (_to); class++) 2420 2421 #define for_each_class(class) \ 2422 for_class_range(class, __sched_class_highest, __sched_class_lowest) 2423 2424 #define sched_class_above(_a, _b) ((_a) < (_b)) 2425 2426 extern const struct sched_class stop_sched_class; 2427 extern const struct sched_class dl_sched_class; 2428 extern const struct sched_class rt_sched_class; 2429 extern const struct sched_class fair_sched_class; 2430 extern const struct sched_class idle_sched_class; 2431 2432 static inline bool sched_stop_runnable(struct rq *rq) 2433 { 2434 return rq->stop && task_on_rq_queued(rq->stop); 2435 } 2436 2437 static inline bool sched_dl_runnable(struct rq *rq) 2438 { 2439 return rq->dl.dl_nr_running > 0; 2440 } 2441 2442 static inline bool sched_rt_runnable(struct rq *rq) 2443 { 2444 return rq->rt.rt_queued > 0; 2445 } 2446 2447 static inline bool sched_fair_runnable(struct rq *rq) 2448 { 2449 return rq->cfs.nr_running > 0; 2450 } 2451 2452 extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf); 2453 extern struct task_struct *pick_next_task_idle(struct rq *rq); 2454 2455 #define SCA_CHECK 0x01 2456 #define SCA_MIGRATE_DISABLE 0x02 2457 #define SCA_MIGRATE_ENABLE 0x04 2458 #define SCA_USER 0x08 2459 2460 #ifdef CONFIG_SMP 2461 2462 extern void update_group_capacity(struct sched_domain *sd, int cpu); 2463 2464 extern void sched_balance_trigger(struct rq *rq); 2465 2466 extern int __set_cpus_allowed_ptr(struct task_struct *p, struct affinity_context *ctx); 2467 extern void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx); 2468 2469 static inline cpumask_t *alloc_user_cpus_ptr(int node) 2470 { 2471 /* 2472 * See do_set_cpus_allowed() above for the rcu_head usage. 2473 */ 2474 int size = max_t(int, cpumask_size(), sizeof(struct rcu_head)); 2475 2476 return kmalloc_node(size, GFP_KERNEL, node); 2477 } 2478 2479 static inline struct task_struct *get_push_task(struct rq *rq) 2480 { 2481 struct task_struct *p = rq->curr; 2482 2483 lockdep_assert_rq_held(rq); 2484 2485 if (rq->push_busy) 2486 return NULL; 2487 2488 if (p->nr_cpus_allowed == 1) 2489 return NULL; 2490 2491 if (p->migration_disabled) 2492 return NULL; 2493 2494 rq->push_busy = true; 2495 return get_task_struct(p); 2496 } 2497 2498 extern int push_cpu_stop(void *arg); 2499 2500 #else /* !CONFIG_SMP: */ 2501 2502 static inline int __set_cpus_allowed_ptr(struct task_struct *p, 2503 struct affinity_context *ctx) 2504 { 2505 return set_cpus_allowed_ptr(p, ctx->new_mask); 2506 } 2507 2508 static inline cpumask_t *alloc_user_cpus_ptr(int node) 2509 { 2510 return NULL; 2511 } 2512 2513 #endif /* !CONFIG_SMP */ 2514 2515 #ifdef CONFIG_CPU_IDLE 2516 2517 static inline void idle_set_state(struct rq *rq, 2518 struct cpuidle_state *idle_state) 2519 { 2520 rq->idle_state = idle_state; 2521 } 2522 2523 static inline struct cpuidle_state *idle_get_state(struct rq *rq) 2524 { 2525 SCHED_WARN_ON(!rcu_read_lock_held()); 2526 2527 return rq->idle_state; 2528 } 2529 2530 #else /* !CONFIG_CPU_IDLE: */ 2531 2532 static inline void idle_set_state(struct rq *rq, 2533 struct cpuidle_state *idle_state) 2534 { 2535 } 2536 2537 static inline struct cpuidle_state *idle_get_state(struct rq *rq) 2538 { 2539 return NULL; 2540 } 2541 2542 #endif /* !CONFIG_CPU_IDLE */ 2543 2544 extern void schedule_idle(void); 2545 asmlinkage void schedule_user(void); 2546 2547 extern void sysrq_sched_debug_show(void); 2548 extern void sched_init_granularity(void); 2549 extern void update_max_interval(void); 2550 2551 extern void init_sched_dl_class(void); 2552 extern void init_sched_rt_class(void); 2553 extern void init_sched_fair_class(void); 2554 2555 extern void resched_curr(struct rq *rq); 2556 extern void resched_cpu(int cpu); 2557 2558 extern struct rt_bandwidth def_rt_bandwidth; 2559 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime); 2560 extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq); 2561 2562 extern void init_dl_entity(struct sched_dl_entity *dl_se); 2563 2564 #define BW_SHIFT 20 2565 #define BW_UNIT (1 << BW_SHIFT) 2566 #define RATIO_SHIFT 8 2567 #define MAX_BW_BITS (64 - BW_SHIFT) 2568 #define MAX_BW ((1ULL << MAX_BW_BITS) - 1) 2569 2570 extern unsigned long to_ratio(u64 period, u64 runtime); 2571 2572 extern void init_entity_runnable_average(struct sched_entity *se); 2573 extern void post_init_entity_util_avg(struct task_struct *p); 2574 2575 #ifdef CONFIG_NO_HZ_FULL 2576 extern bool sched_can_stop_tick(struct rq *rq); 2577 extern int __init sched_tick_offload_init(void); 2578 2579 /* 2580 * Tick may be needed by tasks in the runqueue depending on their policy and 2581 * requirements. If tick is needed, lets send the target an IPI to kick it out of 2582 * nohz mode if necessary. 2583 */ 2584 static inline void sched_update_tick_dependency(struct rq *rq) 2585 { 2586 int cpu = cpu_of(rq); 2587 2588 if (!tick_nohz_full_cpu(cpu)) 2589 return; 2590 2591 if (sched_can_stop_tick(rq)) 2592 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED); 2593 else 2594 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); 2595 } 2596 #else /* !CONFIG_NO_HZ_FULL: */ 2597 static inline int sched_tick_offload_init(void) { return 0; } 2598 static inline void sched_update_tick_dependency(struct rq *rq) { } 2599 #endif /* !CONFIG_NO_HZ_FULL */ 2600 2601 static inline void add_nr_running(struct rq *rq, unsigned count) 2602 { 2603 unsigned prev_nr = rq->nr_running; 2604 2605 rq->nr_running = prev_nr + count; 2606 if (trace_sched_update_nr_running_tp_enabled()) { 2607 call_trace_sched_update_nr_running(rq, count); 2608 } 2609 2610 #ifdef CONFIG_SMP 2611 if (prev_nr < 2 && rq->nr_running >= 2) 2612 set_rd_overloaded(rq->rd, 1); 2613 #endif 2614 2615 sched_update_tick_dependency(rq); 2616 } 2617 2618 static inline void sub_nr_running(struct rq *rq, unsigned count) 2619 { 2620 rq->nr_running -= count; 2621 if (trace_sched_update_nr_running_tp_enabled()) { 2622 call_trace_sched_update_nr_running(rq, -count); 2623 } 2624 2625 /* Check if we still need preemption */ 2626 sched_update_tick_dependency(rq); 2627 } 2628 2629 extern void activate_task(struct rq *rq, struct task_struct *p, int flags); 2630 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags); 2631 2632 extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags); 2633 2634 #ifdef CONFIG_PREEMPT_RT 2635 # define SCHED_NR_MIGRATE_BREAK 8 2636 #else 2637 # define SCHED_NR_MIGRATE_BREAK 32 2638 #endif 2639 2640 extern const_debug unsigned int sysctl_sched_nr_migrate; 2641 extern const_debug unsigned int sysctl_sched_migration_cost; 2642 2643 extern unsigned int sysctl_sched_base_slice; 2644 2645 #ifdef CONFIG_SCHED_DEBUG 2646 extern int sysctl_resched_latency_warn_ms; 2647 extern int sysctl_resched_latency_warn_once; 2648 2649 extern unsigned int sysctl_sched_tunable_scaling; 2650 2651 extern unsigned int sysctl_numa_balancing_scan_delay; 2652 extern unsigned int sysctl_numa_balancing_scan_period_min; 2653 extern unsigned int sysctl_numa_balancing_scan_period_max; 2654 extern unsigned int sysctl_numa_balancing_scan_size; 2655 extern unsigned int sysctl_numa_balancing_hot_threshold; 2656 #endif 2657 2658 #ifdef CONFIG_SCHED_HRTICK 2659 2660 /* 2661 * Use hrtick when: 2662 * - enabled by features 2663 * - hrtimer is actually high res 2664 */ 2665 static inline int hrtick_enabled(struct rq *rq) 2666 { 2667 if (!cpu_active(cpu_of(rq))) 2668 return 0; 2669 return hrtimer_is_hres_active(&rq->hrtick_timer); 2670 } 2671 2672 static inline int hrtick_enabled_fair(struct rq *rq) 2673 { 2674 if (!sched_feat(HRTICK)) 2675 return 0; 2676 return hrtick_enabled(rq); 2677 } 2678 2679 static inline int hrtick_enabled_dl(struct rq *rq) 2680 { 2681 if (!sched_feat(HRTICK_DL)) 2682 return 0; 2683 return hrtick_enabled(rq); 2684 } 2685 2686 extern void hrtick_start(struct rq *rq, u64 delay); 2687 2688 #else /* !CONFIG_SCHED_HRTICK: */ 2689 2690 static inline int hrtick_enabled_fair(struct rq *rq) 2691 { 2692 return 0; 2693 } 2694 2695 static inline int hrtick_enabled_dl(struct rq *rq) 2696 { 2697 return 0; 2698 } 2699 2700 static inline int hrtick_enabled(struct rq *rq) 2701 { 2702 return 0; 2703 } 2704 2705 #endif /* !CONFIG_SCHED_HRTICK */ 2706 2707 #ifndef arch_scale_freq_tick 2708 static __always_inline void arch_scale_freq_tick(void) { } 2709 #endif 2710 2711 #ifndef arch_scale_freq_capacity 2712 /** 2713 * arch_scale_freq_capacity - get the frequency scale factor of a given CPU. 2714 * @cpu: the CPU in question. 2715 * 2716 * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e. 2717 * 2718 * f_curr 2719 * ------ * SCHED_CAPACITY_SCALE 2720 * f_max 2721 */ 2722 static __always_inline 2723 unsigned long arch_scale_freq_capacity(int cpu) 2724 { 2725 return SCHED_CAPACITY_SCALE; 2726 } 2727 #endif 2728 2729 #ifdef CONFIG_SCHED_DEBUG 2730 /* 2731 * In double_lock_balance()/double_rq_lock(), we use raw_spin_rq_lock() to 2732 * acquire rq lock instead of rq_lock(). So at the end of these two functions 2733 * we need to call double_rq_clock_clear_update() to clear RQCF_UPDATED of 2734 * rq->clock_update_flags to avoid the WARN_DOUBLE_CLOCK warning. 2735 */ 2736 static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2) 2737 { 2738 rq1->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); 2739 /* rq1 == rq2 for !CONFIG_SMP, so just clear RQCF_UPDATED once. */ 2740 #ifdef CONFIG_SMP 2741 rq2->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); 2742 #endif 2743 } 2744 #else 2745 static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2) { } 2746 #endif 2747 2748 #define DEFINE_LOCK_GUARD_2(name, type, _lock, _unlock, ...) \ 2749 __DEFINE_UNLOCK_GUARD(name, type, _unlock, type *lock2; __VA_ARGS__) \ 2750 static inline class_##name##_t class_##name##_constructor(type *lock, type *lock2) \ 2751 { class_##name##_t _t = { .lock = lock, .lock2 = lock2 }, *_T = &_t; \ 2752 _lock; return _t; } 2753 2754 #ifdef CONFIG_SMP 2755 2756 static inline bool rq_order_less(struct rq *rq1, struct rq *rq2) 2757 { 2758 #ifdef CONFIG_SCHED_CORE 2759 /* 2760 * In order to not have {0,2},{1,3} turn into into an AB-BA, 2761 * order by core-id first and cpu-id second. 2762 * 2763 * Notably: 2764 * 2765 * double_rq_lock(0,3); will take core-0, core-1 lock 2766 * double_rq_lock(1,2); will take core-1, core-0 lock 2767 * 2768 * when only cpu-id is considered. 2769 */ 2770 if (rq1->core->cpu < rq2->core->cpu) 2771 return true; 2772 if (rq1->core->cpu > rq2->core->cpu) 2773 return false; 2774 2775 /* 2776 * __sched_core_flip() relies on SMT having cpu-id lock order. 2777 */ 2778 #endif 2779 return rq1->cpu < rq2->cpu; 2780 } 2781 2782 extern void double_rq_lock(struct rq *rq1, struct rq *rq2); 2783 2784 #ifdef CONFIG_PREEMPTION 2785 2786 /* 2787 * fair double_lock_balance: Safely acquires both rq->locks in a fair 2788 * way at the expense of forcing extra atomic operations in all 2789 * invocations. This assures that the double_lock is acquired using the 2790 * same underlying policy as the spinlock_t on this architecture, which 2791 * reduces latency compared to the unfair variant below. However, it 2792 * also adds more overhead and therefore may reduce throughput. 2793 */ 2794 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) 2795 __releases(this_rq->lock) 2796 __acquires(busiest->lock) 2797 __acquires(this_rq->lock) 2798 { 2799 raw_spin_rq_unlock(this_rq); 2800 double_rq_lock(this_rq, busiest); 2801 2802 return 1; 2803 } 2804 2805 #else /* !CONFIG_PREEMPTION: */ 2806 /* 2807 * Unfair double_lock_balance: Optimizes throughput at the expense of 2808 * latency by eliminating extra atomic operations when the locks are 2809 * already in proper order on entry. This favors lower CPU-ids and will 2810 * grant the double lock to lower CPUs over higher ids under contention, 2811 * regardless of entry order into the function. 2812 */ 2813 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) 2814 __releases(this_rq->lock) 2815 __acquires(busiest->lock) 2816 __acquires(this_rq->lock) 2817 { 2818 if (__rq_lockp(this_rq) == __rq_lockp(busiest) || 2819 likely(raw_spin_rq_trylock(busiest))) { 2820 double_rq_clock_clear_update(this_rq, busiest); 2821 return 0; 2822 } 2823 2824 if (rq_order_less(this_rq, busiest)) { 2825 raw_spin_rq_lock_nested(busiest, SINGLE_DEPTH_NESTING); 2826 double_rq_clock_clear_update(this_rq, busiest); 2827 return 0; 2828 } 2829 2830 raw_spin_rq_unlock(this_rq); 2831 double_rq_lock(this_rq, busiest); 2832 2833 return 1; 2834 } 2835 2836 #endif /* !CONFIG_PREEMPTION */ 2837 2838 /* 2839 * double_lock_balance - lock the busiest runqueue, this_rq is locked already. 2840 */ 2841 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest) 2842 { 2843 lockdep_assert_irqs_disabled(); 2844 2845 return _double_lock_balance(this_rq, busiest); 2846 } 2847 2848 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) 2849 __releases(busiest->lock) 2850 { 2851 if (__rq_lockp(this_rq) != __rq_lockp(busiest)) 2852 raw_spin_rq_unlock(busiest); 2853 lock_set_subclass(&__rq_lockp(this_rq)->dep_map, 0, _RET_IP_); 2854 } 2855 2856 static inline void double_lock(spinlock_t *l1, spinlock_t *l2) 2857 { 2858 if (l1 > l2) 2859 swap(l1, l2); 2860 2861 spin_lock(l1); 2862 spin_lock_nested(l2, SINGLE_DEPTH_NESTING); 2863 } 2864 2865 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2) 2866 { 2867 if (l1 > l2) 2868 swap(l1, l2); 2869 2870 spin_lock_irq(l1); 2871 spin_lock_nested(l2, SINGLE_DEPTH_NESTING); 2872 } 2873 2874 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2) 2875 { 2876 if (l1 > l2) 2877 swap(l1, l2); 2878 2879 raw_spin_lock(l1); 2880 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING); 2881 } 2882 2883 static inline void double_raw_unlock(raw_spinlock_t *l1, raw_spinlock_t *l2) 2884 { 2885 raw_spin_unlock(l1); 2886 raw_spin_unlock(l2); 2887 } 2888 2889 DEFINE_LOCK_GUARD_2(double_raw_spinlock, raw_spinlock_t, 2890 double_raw_lock(_T->lock, _T->lock2), 2891 double_raw_unlock(_T->lock, _T->lock2)) 2892 2893 /* 2894 * double_rq_unlock - safely unlock two runqueues 2895 * 2896 * Note this does not restore interrupts like task_rq_unlock, 2897 * you need to do so manually after calling. 2898 */ 2899 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) 2900 __releases(rq1->lock) 2901 __releases(rq2->lock) 2902 { 2903 if (__rq_lockp(rq1) != __rq_lockp(rq2)) 2904 raw_spin_rq_unlock(rq2); 2905 else 2906 __release(rq2->lock); 2907 raw_spin_rq_unlock(rq1); 2908 } 2909 2910 extern void set_rq_online (struct rq *rq); 2911 extern void set_rq_offline(struct rq *rq); 2912 2913 extern bool sched_smp_initialized; 2914 2915 #else /* !CONFIG_SMP: */ 2916 2917 /* 2918 * double_rq_lock - safely lock two runqueues 2919 * 2920 * Note this does not disable interrupts like task_rq_lock, 2921 * you need to do so manually before calling. 2922 */ 2923 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) 2924 __acquires(rq1->lock) 2925 __acquires(rq2->lock) 2926 { 2927 WARN_ON_ONCE(!irqs_disabled()); 2928 WARN_ON_ONCE(rq1 != rq2); 2929 raw_spin_rq_lock(rq1); 2930 __acquire(rq2->lock); /* Fake it out ;) */ 2931 double_rq_clock_clear_update(rq1, rq2); 2932 } 2933 2934 /* 2935 * double_rq_unlock - safely unlock two runqueues 2936 * 2937 * Note this does not restore interrupts like task_rq_unlock, 2938 * you need to do so manually after calling. 2939 */ 2940 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) 2941 __releases(rq1->lock) 2942 __releases(rq2->lock) 2943 { 2944 WARN_ON_ONCE(rq1 != rq2); 2945 raw_spin_rq_unlock(rq1); 2946 __release(rq2->lock); 2947 } 2948 2949 #endif /* !CONFIG_SMP */ 2950 2951 DEFINE_LOCK_GUARD_2(double_rq_lock, struct rq, 2952 double_rq_lock(_T->lock, _T->lock2), 2953 double_rq_unlock(_T->lock, _T->lock2)) 2954 2955 extern struct sched_entity *__pick_root_entity(struct cfs_rq *cfs_rq); 2956 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq); 2957 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq); 2958 2959 #ifdef CONFIG_SCHED_DEBUG 2960 extern bool sched_debug_verbose; 2961 2962 extern void print_cfs_stats(struct seq_file *m, int cpu); 2963 extern void print_rt_stats(struct seq_file *m, int cpu); 2964 extern void print_dl_stats(struct seq_file *m, int cpu); 2965 extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq); 2966 extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq); 2967 extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq); 2968 2969 extern void resched_latency_warn(int cpu, u64 latency); 2970 # ifdef CONFIG_NUMA_BALANCING 2971 extern void show_numa_stats(struct task_struct *p, struct seq_file *m); 2972 extern void 2973 print_numa_stats(struct seq_file *m, int node, unsigned long tsf, 2974 unsigned long tpf, unsigned long gsf, unsigned long gpf); 2975 # endif /* CONFIG_NUMA_BALANCING */ 2976 #else /* !CONFIG_SCHED_DEBUG: */ 2977 static inline void resched_latency_warn(int cpu, u64 latency) { } 2978 #endif /* !CONFIG_SCHED_DEBUG */ 2979 2980 extern void init_cfs_rq(struct cfs_rq *cfs_rq); 2981 extern void init_rt_rq(struct rt_rq *rt_rq); 2982 extern void init_dl_rq(struct dl_rq *dl_rq); 2983 2984 extern void cfs_bandwidth_usage_inc(void); 2985 extern void cfs_bandwidth_usage_dec(void); 2986 2987 #ifdef CONFIG_NO_HZ_COMMON 2988 2989 #define NOHZ_BALANCE_KICK_BIT 0 2990 #define NOHZ_STATS_KICK_BIT 1 2991 #define NOHZ_NEWILB_KICK_BIT 2 2992 #define NOHZ_NEXT_KICK_BIT 3 2993 2994 /* Run sched_balance_domains() */ 2995 #define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT) 2996 /* Update blocked load */ 2997 #define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT) 2998 /* Update blocked load when entering idle */ 2999 #define NOHZ_NEWILB_KICK BIT(NOHZ_NEWILB_KICK_BIT) 3000 /* Update nohz.next_balance */ 3001 #define NOHZ_NEXT_KICK BIT(NOHZ_NEXT_KICK_BIT) 3002 3003 #define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK | NOHZ_NEXT_KICK) 3004 3005 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags) 3006 3007 extern void nohz_balance_exit_idle(struct rq *rq); 3008 #else /* !CONFIG_NO_HZ_COMMON: */ 3009 static inline void nohz_balance_exit_idle(struct rq *rq) { } 3010 #endif /* !CONFIG_NO_HZ_COMMON */ 3011 3012 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON) 3013 extern void nohz_run_idle_balance(int cpu); 3014 #else 3015 static inline void nohz_run_idle_balance(int cpu) { } 3016 #endif 3017 3018 #ifdef CONFIG_IRQ_TIME_ACCOUNTING 3019 3020 struct irqtime { 3021 u64 total; 3022 u64 tick_delta; 3023 u64 irq_start_time; 3024 struct u64_stats_sync sync; 3025 }; 3026 3027 DECLARE_PER_CPU(struct irqtime, cpu_irqtime); 3028 3029 /* 3030 * Returns the irqtime minus the softirq time computed by ksoftirqd. 3031 * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime 3032 * and never move forward. 3033 */ 3034 static inline u64 irq_time_read(int cpu) 3035 { 3036 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu); 3037 unsigned int seq; 3038 u64 total; 3039 3040 do { 3041 seq = __u64_stats_fetch_begin(&irqtime->sync); 3042 total = irqtime->total; 3043 } while (__u64_stats_fetch_retry(&irqtime->sync, seq)); 3044 3045 return total; 3046 } 3047 3048 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ 3049 3050 #ifdef CONFIG_CPU_FREQ 3051 3052 DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data); 3053 3054 /** 3055 * cpufreq_update_util - Take a note about CPU utilization changes. 3056 * @rq: Runqueue to carry out the update for. 3057 * @flags: Update reason flags. 3058 * 3059 * This function is called by the scheduler on the CPU whose utilization is 3060 * being updated. 3061 * 3062 * It can only be called from RCU-sched read-side critical sections. 3063 * 3064 * The way cpufreq is currently arranged requires it to evaluate the CPU 3065 * performance state (frequency/voltage) on a regular basis to prevent it from 3066 * being stuck in a completely inadequate performance level for too long. 3067 * That is not guaranteed to happen if the updates are only triggered from CFS 3068 * and DL, though, because they may not be coming in if only RT tasks are 3069 * active all the time (or there are RT tasks only). 3070 * 3071 * As a workaround for that issue, this function is called periodically by the 3072 * RT sched class to trigger extra cpufreq updates to prevent it from stalling, 3073 * but that really is a band-aid. Going forward it should be replaced with 3074 * solutions targeted more specifically at RT tasks. 3075 */ 3076 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) 3077 { 3078 struct update_util_data *data; 3079 3080 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data, 3081 cpu_of(rq))); 3082 if (data) 3083 data->func(data, rq_clock(rq), flags); 3084 } 3085 #else /* !CONFIG_CPU_FREQ: */ 3086 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) { } 3087 #endif /* !CONFIG_CPU_FREQ */ 3088 3089 #ifdef arch_scale_freq_capacity 3090 # ifndef arch_scale_freq_invariant 3091 # define arch_scale_freq_invariant() true 3092 # endif 3093 #else 3094 # define arch_scale_freq_invariant() false 3095 #endif 3096 3097 #ifdef CONFIG_SMP 3098 3099 bool update_other_load_avgs(struct rq *rq); 3100 3101 unsigned long effective_cpu_util(int cpu, unsigned long util_cfs, 3102 unsigned long *min, 3103 unsigned long *max); 3104 3105 unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual, 3106 unsigned long min, 3107 unsigned long max); 3108 3109 3110 /* 3111 * Verify the fitness of task @p to run on @cpu taking into account the 3112 * CPU original capacity and the runtime/deadline ratio of the task. 3113 * 3114 * The function will return true if the original capacity of @cpu is 3115 * greater than or equal to task's deadline density right shifted by 3116 * (BW_SHIFT - SCHED_CAPACITY_SHIFT) and false otherwise. 3117 */ 3118 static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu) 3119 { 3120 unsigned long cap = arch_scale_cpu_capacity(cpu); 3121 3122 return cap >= p->dl.dl_density >> (BW_SHIFT - SCHED_CAPACITY_SHIFT); 3123 } 3124 3125 static inline unsigned long cpu_bw_dl(struct rq *rq) 3126 { 3127 return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT; 3128 } 3129 3130 static inline unsigned long cpu_util_dl(struct rq *rq) 3131 { 3132 return READ_ONCE(rq->avg_dl.util_avg); 3133 } 3134 3135 3136 extern unsigned long cpu_util_cfs(int cpu); 3137 extern unsigned long cpu_util_cfs_boost(int cpu); 3138 3139 static inline unsigned long cpu_util_rt(struct rq *rq) 3140 { 3141 return READ_ONCE(rq->avg_rt.util_avg); 3142 } 3143 3144 #else /* !CONFIG_SMP */ 3145 static inline bool update_other_load_avgs(struct rq *rq) { return false; } 3146 #endif /* CONFIG_SMP */ 3147 3148 #ifdef CONFIG_UCLAMP_TASK 3149 3150 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id); 3151 3152 static inline unsigned long uclamp_rq_get(struct rq *rq, 3153 enum uclamp_id clamp_id) 3154 { 3155 return READ_ONCE(rq->uclamp[clamp_id].value); 3156 } 3157 3158 static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, 3159 unsigned int value) 3160 { 3161 WRITE_ONCE(rq->uclamp[clamp_id].value, value); 3162 } 3163 3164 static inline bool uclamp_rq_is_idle(struct rq *rq) 3165 { 3166 return rq->uclamp_flags & UCLAMP_FLAG_IDLE; 3167 } 3168 3169 /* Is the rq being capped/throttled by uclamp_max? */ 3170 static inline bool uclamp_rq_is_capped(struct rq *rq) 3171 { 3172 unsigned long rq_util; 3173 unsigned long max_util; 3174 3175 if (!static_branch_likely(&sched_uclamp_used)) 3176 return false; 3177 3178 rq_util = cpu_util_cfs(cpu_of(rq)) + cpu_util_rt(rq); 3179 max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value); 3180 3181 return max_util != SCHED_CAPACITY_SCALE && rq_util >= max_util; 3182 } 3183 3184 /* 3185 * When uclamp is compiled in, the aggregation at rq level is 'turned off' 3186 * by default in the fast path and only gets turned on once userspace performs 3187 * an operation that requires it. 3188 * 3189 * Returns true if userspace opted-in to use uclamp and aggregation at rq level 3190 * hence is active. 3191 */ 3192 static inline bool uclamp_is_used(void) 3193 { 3194 return static_branch_likely(&sched_uclamp_used); 3195 } 3196 3197 #define for_each_clamp_id(clamp_id) \ 3198 for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++) 3199 3200 extern unsigned int sysctl_sched_uclamp_util_min_rt_default; 3201 3202 3203 static inline unsigned int uclamp_none(enum uclamp_id clamp_id) 3204 { 3205 if (clamp_id == UCLAMP_MIN) 3206 return 0; 3207 return SCHED_CAPACITY_SCALE; 3208 } 3209 3210 /* Integer rounded range for each bucket */ 3211 #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS) 3212 3213 static inline unsigned int uclamp_bucket_id(unsigned int clamp_value) 3214 { 3215 return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1); 3216 } 3217 3218 static inline void 3219 uclamp_se_set(struct uclamp_se *uc_se, unsigned int value, bool user_defined) 3220 { 3221 uc_se->value = value; 3222 uc_se->bucket_id = uclamp_bucket_id(value); 3223 uc_se->user_defined = user_defined; 3224 } 3225 3226 #else /* !CONFIG_UCLAMP_TASK: */ 3227 3228 static inline unsigned long 3229 uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id) 3230 { 3231 if (clamp_id == UCLAMP_MIN) 3232 return 0; 3233 3234 return SCHED_CAPACITY_SCALE; 3235 } 3236 3237 static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; } 3238 3239 static inline bool uclamp_is_used(void) 3240 { 3241 return false; 3242 } 3243 3244 static inline unsigned long 3245 uclamp_rq_get(struct rq *rq, enum uclamp_id clamp_id) 3246 { 3247 if (clamp_id == UCLAMP_MIN) 3248 return 0; 3249 3250 return SCHED_CAPACITY_SCALE; 3251 } 3252 3253 static inline void 3254 uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, unsigned int value) 3255 { 3256 } 3257 3258 static inline bool uclamp_rq_is_idle(struct rq *rq) 3259 { 3260 return false; 3261 } 3262 3263 #endif /* !CONFIG_UCLAMP_TASK */ 3264 3265 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ 3266 3267 static inline unsigned long cpu_util_irq(struct rq *rq) 3268 { 3269 return READ_ONCE(rq->avg_irq.util_avg); 3270 } 3271 3272 static inline 3273 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max) 3274 { 3275 util *= (max - irq); 3276 util /= max; 3277 3278 return util; 3279 3280 } 3281 3282 #else /* !CONFIG_HAVE_SCHED_AVG_IRQ: */ 3283 3284 static inline unsigned long cpu_util_irq(struct rq *rq) 3285 { 3286 return 0; 3287 } 3288 3289 static inline 3290 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max) 3291 { 3292 return util; 3293 } 3294 3295 #endif /* !CONFIG_HAVE_SCHED_AVG_IRQ */ 3296 3297 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) 3298 3299 #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus))) 3300 3301 DECLARE_STATIC_KEY_FALSE(sched_energy_present); 3302 3303 static inline bool sched_energy_enabled(void) 3304 { 3305 return static_branch_unlikely(&sched_energy_present); 3306 } 3307 3308 extern struct cpufreq_governor schedutil_gov; 3309 3310 #else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */ 3311 3312 #define perf_domain_span(pd) NULL 3313 3314 static inline bool sched_energy_enabled(void) { return false; } 3315 3316 #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */ 3317 3318 #ifdef CONFIG_MEMBARRIER 3319 3320 /* 3321 * The scheduler provides memory barriers required by membarrier between: 3322 * - prior user-space memory accesses and store to rq->membarrier_state, 3323 * - store to rq->membarrier_state and following user-space memory accesses. 3324 * In the same way it provides those guarantees around store to rq->curr. 3325 */ 3326 static inline void membarrier_switch_mm(struct rq *rq, 3327 struct mm_struct *prev_mm, 3328 struct mm_struct *next_mm) 3329 { 3330 int membarrier_state; 3331 3332 if (prev_mm == next_mm) 3333 return; 3334 3335 membarrier_state = atomic_read(&next_mm->membarrier_state); 3336 if (READ_ONCE(rq->membarrier_state) == membarrier_state) 3337 return; 3338 3339 WRITE_ONCE(rq->membarrier_state, membarrier_state); 3340 } 3341 3342 #else /* !CONFIG_MEMBARRIER :*/ 3343 3344 static inline void membarrier_switch_mm(struct rq *rq, 3345 struct mm_struct *prev_mm, 3346 struct mm_struct *next_mm) 3347 { 3348 } 3349 3350 #endif /* !CONFIG_MEMBARRIER */ 3351 3352 #ifdef CONFIG_SMP 3353 static inline bool is_per_cpu_kthread(struct task_struct *p) 3354 { 3355 if (!(p->flags & PF_KTHREAD)) 3356 return false; 3357 3358 if (p->nr_cpus_allowed != 1) 3359 return false; 3360 3361 return true; 3362 } 3363 #endif 3364 3365 extern void swake_up_all_locked(struct swait_queue_head *q); 3366 extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait); 3367 3368 extern int try_to_wake_up(struct task_struct *tsk, unsigned int state, int wake_flags); 3369 3370 #ifdef CONFIG_PREEMPT_DYNAMIC 3371 extern int preempt_dynamic_mode; 3372 extern int sched_dynamic_mode(const char *str); 3373 extern void sched_dynamic_update(int mode); 3374 #endif 3375 3376 #ifdef CONFIG_SCHED_MM_CID 3377 3378 #define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */ 3379 #define MM_CID_SCAN_DELAY 100 /* 100ms */ 3380 3381 extern raw_spinlock_t cid_lock; 3382 extern int use_cid_lock; 3383 3384 extern void sched_mm_cid_migrate_from(struct task_struct *t); 3385 extern void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t); 3386 extern void task_tick_mm_cid(struct rq *rq, struct task_struct *curr); 3387 extern void init_sched_mm_cid(struct task_struct *t); 3388 3389 static inline void __mm_cid_put(struct mm_struct *mm, int cid) 3390 { 3391 if (cid < 0) 3392 return; 3393 cpumask_clear_cpu(cid, mm_cidmask(mm)); 3394 } 3395 3396 /* 3397 * The per-mm/cpu cid can have the MM_CID_LAZY_PUT flag set or transition to 3398 * the MM_CID_UNSET state without holding the rq lock, but the rq lock needs to 3399 * be held to transition to other states. 3400 * 3401 * State transitions synchronized with cmpxchg or try_cmpxchg need to be 3402 * consistent across CPUs, which prevents use of this_cpu_cmpxchg. 3403 */ 3404 static inline void mm_cid_put_lazy(struct task_struct *t) 3405 { 3406 struct mm_struct *mm = t->mm; 3407 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; 3408 int cid; 3409 3410 lockdep_assert_irqs_disabled(); 3411 cid = __this_cpu_read(pcpu_cid->cid); 3412 if (!mm_cid_is_lazy_put(cid) || 3413 !try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET)) 3414 return; 3415 __mm_cid_put(mm, mm_cid_clear_lazy_put(cid)); 3416 } 3417 3418 static inline int mm_cid_pcpu_unset(struct mm_struct *mm) 3419 { 3420 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; 3421 int cid, res; 3422 3423 lockdep_assert_irqs_disabled(); 3424 cid = __this_cpu_read(pcpu_cid->cid); 3425 for (;;) { 3426 if (mm_cid_is_unset(cid)) 3427 return MM_CID_UNSET; 3428 /* 3429 * Attempt transition from valid or lazy-put to unset. 3430 */ 3431 res = cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, cid, MM_CID_UNSET); 3432 if (res == cid) 3433 break; 3434 cid = res; 3435 } 3436 return cid; 3437 } 3438 3439 static inline void mm_cid_put(struct mm_struct *mm) 3440 { 3441 int cid; 3442 3443 lockdep_assert_irqs_disabled(); 3444 cid = mm_cid_pcpu_unset(mm); 3445 if (cid == MM_CID_UNSET) 3446 return; 3447 __mm_cid_put(mm, mm_cid_clear_lazy_put(cid)); 3448 } 3449 3450 static inline int __mm_cid_try_get(struct mm_struct *mm) 3451 { 3452 struct cpumask *cpumask; 3453 int cid; 3454 3455 cpumask = mm_cidmask(mm); 3456 /* 3457 * Retry finding first zero bit if the mask is temporarily 3458 * filled. This only happens during concurrent remote-clear 3459 * which owns a cid without holding a rq lock. 3460 */ 3461 for (;;) { 3462 cid = cpumask_first_zero(cpumask); 3463 if (cid < nr_cpu_ids) 3464 break; 3465 cpu_relax(); 3466 } 3467 if (cpumask_test_and_set_cpu(cid, cpumask)) 3468 return -1; 3469 3470 return cid; 3471 } 3472 3473 /* 3474 * Save a snapshot of the current runqueue time of this cpu 3475 * with the per-cpu cid value, allowing to estimate how recently it was used. 3476 */ 3477 static inline void mm_cid_snapshot_time(struct rq *rq, struct mm_struct *mm) 3478 { 3479 struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu_of(rq)); 3480 3481 lockdep_assert_rq_held(rq); 3482 WRITE_ONCE(pcpu_cid->time, rq->clock); 3483 } 3484 3485 static inline int __mm_cid_get(struct rq *rq, struct mm_struct *mm) 3486 { 3487 int cid; 3488 3489 /* 3490 * All allocations (even those using the cid_lock) are lock-free. If 3491 * use_cid_lock is set, hold the cid_lock to perform cid allocation to 3492 * guarantee forward progress. 3493 */ 3494 if (!READ_ONCE(use_cid_lock)) { 3495 cid = __mm_cid_try_get(mm); 3496 if (cid >= 0) 3497 goto end; 3498 raw_spin_lock(&cid_lock); 3499 } else { 3500 raw_spin_lock(&cid_lock); 3501 cid = __mm_cid_try_get(mm); 3502 if (cid >= 0) 3503 goto unlock; 3504 } 3505 3506 /* 3507 * cid concurrently allocated. Retry while forcing following 3508 * allocations to use the cid_lock to ensure forward progress. 3509 */ 3510 WRITE_ONCE(use_cid_lock, 1); 3511 /* 3512 * Set use_cid_lock before allocation. Only care about program order 3513 * because this is only required for forward progress. 3514 */ 3515 barrier(); 3516 /* 3517 * Retry until it succeeds. It is guaranteed to eventually succeed once 3518 * all newcoming allocations observe the use_cid_lock flag set. 3519 */ 3520 do { 3521 cid = __mm_cid_try_get(mm); 3522 cpu_relax(); 3523 } while (cid < 0); 3524 /* 3525 * Allocate before clearing use_cid_lock. Only care about 3526 * program order because this is for forward progress. 3527 */ 3528 barrier(); 3529 WRITE_ONCE(use_cid_lock, 0); 3530 unlock: 3531 raw_spin_unlock(&cid_lock); 3532 end: 3533 mm_cid_snapshot_time(rq, mm); 3534 3535 return cid; 3536 } 3537 3538 static inline int mm_cid_get(struct rq *rq, struct mm_struct *mm) 3539 { 3540 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; 3541 struct cpumask *cpumask; 3542 int cid; 3543 3544 lockdep_assert_rq_held(rq); 3545 cpumask = mm_cidmask(mm); 3546 cid = __this_cpu_read(pcpu_cid->cid); 3547 if (mm_cid_is_valid(cid)) { 3548 mm_cid_snapshot_time(rq, mm); 3549 return cid; 3550 } 3551 if (mm_cid_is_lazy_put(cid)) { 3552 if (try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET)) 3553 __mm_cid_put(mm, mm_cid_clear_lazy_put(cid)); 3554 } 3555 cid = __mm_cid_get(rq, mm); 3556 __this_cpu_write(pcpu_cid->cid, cid); 3557 3558 return cid; 3559 } 3560 3561 static inline void switch_mm_cid(struct rq *rq, 3562 struct task_struct *prev, 3563 struct task_struct *next) 3564 { 3565 /* 3566 * Provide a memory barrier between rq->curr store and load of 3567 * {prev,next}->mm->pcpu_cid[cpu] on rq->curr->mm transition. 3568 * 3569 * Should be adapted if context_switch() is modified. 3570 */ 3571 if (!next->mm) { // to kernel 3572 /* 3573 * user -> kernel transition does not guarantee a barrier, but 3574 * we can use the fact that it performs an atomic operation in 3575 * mmgrab(). 3576 */ 3577 if (prev->mm) // from user 3578 smp_mb__after_mmgrab(); 3579 /* 3580 * kernel -> kernel transition does not change rq->curr->mm 3581 * state. It stays NULL. 3582 */ 3583 } else { // to user 3584 /* 3585 * kernel -> user transition does not provide a barrier 3586 * between rq->curr store and load of {prev,next}->mm->pcpu_cid[cpu]. 3587 * Provide it here. 3588 */ 3589 if (!prev->mm) { // from kernel 3590 smp_mb(); 3591 } else { // from user 3592 /* 3593 * user->user transition relies on an implicit 3594 * memory barrier in switch_mm() when 3595 * current->mm changes. If the architecture 3596 * switch_mm() does not have an implicit memory 3597 * barrier, it is emitted here. If current->mm 3598 * is unchanged, no barrier is needed. 3599 */ 3600 smp_mb__after_switch_mm(); 3601 } 3602 } 3603 if (prev->mm_cid_active) { 3604 mm_cid_snapshot_time(rq, prev->mm); 3605 mm_cid_put_lazy(prev); 3606 prev->mm_cid = -1; 3607 } 3608 if (next->mm_cid_active) 3609 next->last_mm_cid = next->mm_cid = mm_cid_get(rq, next->mm); 3610 } 3611 3612 #else /* !CONFIG_SCHED_MM_CID: */ 3613 static inline void switch_mm_cid(struct rq *rq, struct task_struct *prev, struct task_struct *next) { } 3614 static inline void sched_mm_cid_migrate_from(struct task_struct *t) { } 3615 static inline void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t) { } 3616 static inline void task_tick_mm_cid(struct rq *rq, struct task_struct *curr) { } 3617 static inline void init_sched_mm_cid(struct task_struct *t) { } 3618 #endif /* !CONFIG_SCHED_MM_CID */ 3619 3620 extern u64 avg_vruntime(struct cfs_rq *cfs_rq); 3621 extern int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se); 3622 3623 #ifdef CONFIG_RT_MUTEXES 3624 3625 static inline int __rt_effective_prio(struct task_struct *pi_task, int prio) 3626 { 3627 if (pi_task) 3628 prio = min(prio, pi_task->prio); 3629 3630 return prio; 3631 } 3632 3633 static inline int rt_effective_prio(struct task_struct *p, int prio) 3634 { 3635 struct task_struct *pi_task = rt_mutex_get_top_task(p); 3636 3637 return __rt_effective_prio(pi_task, prio); 3638 } 3639 3640 #else /* !CONFIG_RT_MUTEXES: */ 3641 3642 static inline int rt_effective_prio(struct task_struct *p, int prio) 3643 { 3644 return prio; 3645 } 3646 3647 #endif /* !CONFIG_RT_MUTEXES */ 3648 3649 extern int __sched_setscheduler(struct task_struct *p, const struct sched_attr *attr, bool user, bool pi); 3650 extern int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx); 3651 extern void __setscheduler_prio(struct task_struct *p, int prio); 3652 extern void set_load_weight(struct task_struct *p, bool update_load); 3653 extern void enqueue_task(struct rq *rq, struct task_struct *p, int flags); 3654 extern void dequeue_task(struct rq *rq, struct task_struct *p, int flags); 3655 3656 extern void check_class_changing(struct rq *rq, struct task_struct *p, 3657 const struct sched_class *prev_class); 3658 extern void check_class_changed(struct rq *rq, struct task_struct *p, 3659 const struct sched_class *prev_class, 3660 int oldprio); 3661 3662 #ifdef CONFIG_SMP 3663 extern struct balance_callback *splice_balance_callbacks(struct rq *rq); 3664 extern void balance_callbacks(struct rq *rq, struct balance_callback *head); 3665 #else 3666 3667 static inline struct balance_callback *splice_balance_callbacks(struct rq *rq) 3668 { 3669 return NULL; 3670 } 3671 3672 static inline void balance_callbacks(struct rq *rq, struct balance_callback *head) 3673 { 3674 } 3675 3676 #endif 3677 3678 #include "ext.h" 3679 3680 #endif /* _KERNEL_SCHED_SCHED_H */ 3681