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