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