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