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