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