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