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