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