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