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