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