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