1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 297fb7a0aSIngo Molnar /* 397fb7a0aSIngo Molnar * Scheduler internal types and methods: 497fb7a0aSIngo Molnar */ 595458477SIngo Molnar #ifndef _KERNEL_SCHED_SCHED_H 695458477SIngo Molnar #define _KERNEL_SCHED_SCHED_H 7325ea10cSIngo Molnar 8801c1419SIngo Molnar #include <linux/sched/affinity.h> 9dfc3401aSIngo Molnar #include <linux/sched/autogroup.h> 1055687da1SIngo Molnar #include <linux/sched/cpufreq.h> 11325ea10cSIngo Molnar #include <linux/sched/deadline.h> 124ff8f2caSIngo Molnar #include <linux/sched.h> 13325ea10cSIngo Molnar #include <linux/sched/loadavg.h> 14325ea10cSIngo Molnar #include <linux/sched/mm.h> 15801c1419SIngo Molnar #include <linux/sched/rseq_api.h> 16325ea10cSIngo Molnar #include <linux/sched/signal.h> 17321a874aSThomas Gleixner #include <linux/sched/smt.h> 18325ea10cSIngo Molnar #include <linux/sched/stat.h> 19325ea10cSIngo Molnar #include <linux/sched/sysctl.h> 204ff8f2caSIngo Molnar #include <linux/sched/task_flags.h> 2129930025SIngo Molnar #include <linux/sched/task.h> 22325ea10cSIngo Molnar #include <linux/sched/topology.h> 23ef8bd77fSIngo Molnar 244ff8f2caSIngo Molnar #include <linux/atomic.h> 254ff8f2caSIngo Molnar #include <linux/bitmap.h> 264ff8f2caSIngo Molnar #include <linux/bug.h> 274ff8f2caSIngo Molnar #include <linux/capability.h> 284ff8f2caSIngo Molnar #include <linux/cgroup_api.h> 294ff8f2caSIngo Molnar #include <linux/cgroup.h> 30e67198ccSFrederic Weisbecker #include <linux/context_tracking.h> 31325ea10cSIngo Molnar #include <linux/cpufreq.h> 324ff8f2caSIngo Molnar #include <linux/cpumask_api.h> 33325ea10cSIngo Molnar #include <linux/ctype.h> 344ff8f2caSIngo Molnar #include <linux/file.h> 354ff8f2caSIngo Molnar #include <linux/fs_api.h> 364ff8f2caSIngo Molnar #include <linux/hrtimer_api.h> 374ff8f2caSIngo Molnar #include <linux/interrupt.h> 384ff8f2caSIngo Molnar #include <linux/irq_work.h> 394ff8f2caSIngo Molnar #include <linux/jiffies.h> 404ff8f2caSIngo Molnar #include <linux/kref_api.h> 41325ea10cSIngo Molnar #include <linux/kthread.h> 424ff8f2caSIngo Molnar #include <linux/ktime_api.h> 434ff8f2caSIngo Molnar #include <linux/lockdep_api.h> 444ff8f2caSIngo Molnar #include <linux/lockdep.h> 454ff8f2caSIngo Molnar #include <linux/minmax.h> 464ff8f2caSIngo Molnar #include <linux/mm.h> 474ff8f2caSIngo Molnar #include <linux/module.h> 484ff8f2caSIngo Molnar #include <linux/mutex_api.h> 494ff8f2caSIngo Molnar #include <linux/plist.h> 504ff8f2caSIngo Molnar #include <linux/poll.h> 51325ea10cSIngo Molnar #include <linux/proc_fs.h> 52325ea10cSIngo Molnar #include <linux/profile.h> 53eb414681SJohannes Weiner #include <linux/psi.h> 544ff8f2caSIngo Molnar #include <linux/rcupdate.h> 554ff8f2caSIngo Molnar #include <linux/seq_file.h> 564ff8f2caSIngo Molnar #include <linux/seqlock.h> 574ff8f2caSIngo Molnar #include <linux/softirq.h> 584ff8f2caSIngo Molnar #include <linux/spinlock_api.h> 594ff8f2caSIngo Molnar #include <linux/static_key.h> 60391e43daSPeter Zijlstra #include <linux/stop_machine.h> 614ff8f2caSIngo Molnar #include <linux/syscalls_api.h> 62325ea10cSIngo Molnar #include <linux/syscalls.h> 634ff8f2caSIngo Molnar #include <linux/tick.h> 644ff8f2caSIngo Molnar #include <linux/topology.h> 654ff8f2caSIngo Molnar #include <linux/types.h> 664ff8f2caSIngo Molnar #include <linux/u64_stats_sync_api.h> 674ff8f2caSIngo Molnar #include <linux/uaccess.h> 684ff8f2caSIngo Molnar #include <linux/wait_api.h> 694ff8f2caSIngo Molnar #include <linux/wait_bit.h> 704ff8f2caSIngo Molnar #include <linux/workqueue_api.h> 71325ea10cSIngo Molnar 724ff8f2caSIngo Molnar #include <trace/events/power.h> 73b9e9c6caSIngo Molnar #include <trace/events/sched.h> 74b9e9c6caSIngo Molnar 754ff8f2caSIngo Molnar #include "../workqueue_internal.h" 764ff8f2caSIngo Molnar 77b9e9c6caSIngo Molnar #ifdef CONFIG_CGROUP_SCHED 78b9e9c6caSIngo Molnar #include <linux/cgroup.h> 79b9e9c6caSIngo Molnar #include <linux/psi.h> 80b9e9c6caSIngo Molnar #endif 81b9e9c6caSIngo Molnar 82b9e9c6caSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 83b9e9c6caSIngo Molnar # include <linux/static_key.h> 84b9e9c6caSIngo Molnar #endif 85391e43daSPeter Zijlstra 867fce777cSIngo Molnar #ifdef CONFIG_PARAVIRT 877fce777cSIngo Molnar # include <asm/paravirt.h> 884ff8f2caSIngo Molnar # include <asm/paravirt_api_clock.h> 897fce777cSIngo Molnar #endif 907fce777cSIngo Molnar 91391e43daSPeter Zijlstra #include "cpupri.h" 926bfd6d72SJuri Lelli #include "cpudeadline.h" 93391e43daSPeter Zijlstra 949148a3a1SPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 959148a3a1SPeter Zijlstra # define SCHED_WARN_ON(x) WARN_ONCE(x, #x) 969148a3a1SPeter Zijlstra #else 976d3aed3dSIngo Molnar # define SCHED_WARN_ON(x) ({ (void)(x), 0; }) 989148a3a1SPeter Zijlstra #endif 999148a3a1SPeter Zijlstra 10045ceebf7SPaul Gortmaker struct rq; 101442bf3aaSDaniel Lezcano struct cpuidle_state; 10245ceebf7SPaul Gortmaker 103da0c1e65SKirill Tkhai /* task_struct::on_rq states: */ 104da0c1e65SKirill Tkhai #define TASK_ON_RQ_QUEUED 1 105cca26e80SKirill Tkhai #define TASK_ON_RQ_MIGRATING 2 106da0c1e65SKirill Tkhai 107391e43daSPeter Zijlstra extern __read_mostly int scheduler_running; 108391e43daSPeter Zijlstra 10945ceebf7SPaul Gortmaker extern unsigned long calc_load_update; 11045ceebf7SPaul Gortmaker extern atomic_long_t calc_load_tasks; 11145ceebf7SPaul Gortmaker 112a60707d7SZhen Ni extern unsigned int sysctl_sched_child_runs_first; 113a60707d7SZhen Ni 1143289bdb4SPeter Zijlstra extern void calc_global_load_tick(struct rq *this_rq); 115d60585c5SThomas Gleixner extern long calc_load_fold_active(struct rq *this_rq, long adjust); 1163289bdb4SPeter Zijlstra 1179d246053SPhil Auld extern void call_trace_sched_update_nr_running(struct rq *rq, int count); 118d9ab0e63SZhen Ni 119d9ab0e63SZhen Ni extern unsigned int sysctl_sched_rt_period; 120d9ab0e63SZhen Ni extern int sysctl_sched_rt_runtime; 121dafd7a9dSZhen Ni extern int sched_rr_timeslice; 122d9ab0e63SZhen Ni 123391e43daSPeter Zijlstra /* 124391e43daSPeter Zijlstra * Helpers for converting nanosecond timing to jiffy resolution 125391e43daSPeter Zijlstra */ 126391e43daSPeter Zijlstra #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) 127391e43daSPeter Zijlstra 128cc1f4b1fSLi Zefan /* 129cc1f4b1fSLi Zefan * Increase resolution of nice-level calculations for 64-bit architectures. 130cc1f4b1fSLi Zefan * The extra resolution improves shares distribution and load balancing of 131cc1f4b1fSLi Zefan * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup 132cc1f4b1fSLi Zefan * hierarchies, especially on larger systems. This is not a user-visible change 133cc1f4b1fSLi Zefan * and does not change the user-interface for setting shares/weights. 134cc1f4b1fSLi Zefan * 135cc1f4b1fSLi Zefan * We increase resolution only if we have enough bits to allow this increased 13697fb7a0aSIngo Molnar * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit 13797fb7a0aSIngo Molnar * are pretty high and the returns do not justify the increased costs. 1382159197dSPeter Zijlstra * 13997fb7a0aSIngo Molnar * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to 14097fb7a0aSIngo Molnar * increase coverage and consistency always enable it on 64-bit platforms. 141cc1f4b1fSLi Zefan */ 1422159197dSPeter Zijlstra #ifdef CONFIG_64BIT 143172895e6SYuyang Du # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT) 1446ecdd749SYuyang Du # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT) 14526cf5222SMichael Wang # define scale_load_down(w) \ 14626cf5222SMichael Wang ({ \ 14726cf5222SMichael Wang unsigned long __w = (w); \ 14826cf5222SMichael Wang if (__w) \ 14926cf5222SMichael Wang __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \ 15026cf5222SMichael Wang __w; \ 15126cf5222SMichael Wang }) 152cc1f4b1fSLi Zefan #else 153172895e6SYuyang Du # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT) 154cc1f4b1fSLi Zefan # define scale_load(w) (w) 155cc1f4b1fSLi Zefan # define scale_load_down(w) (w) 156cc1f4b1fSLi Zefan #endif 157cc1f4b1fSLi Zefan 1586ecdd749SYuyang Du /* 159172895e6SYuyang Du * Task weight (visible to users) and its load (invisible to users) have 160172895e6SYuyang Du * independent resolution, but they should be well calibrated. We use 161172895e6SYuyang Du * scale_load() and scale_load_down(w) to convert between them. The 162172895e6SYuyang Du * following must be true: 163172895e6SYuyang Du * 1649d061ba6SDietmar Eggemann * scale_load(sched_prio_to_weight[NICE_TO_PRIO(0)-MAX_RT_PRIO]) == NICE_0_LOAD 165172895e6SYuyang Du * 1666ecdd749SYuyang Du */ 167172895e6SYuyang Du #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT) 168391e43daSPeter Zijlstra 169391e43daSPeter Zijlstra /* 170332ac17eSDario Faggioli * Single value that decides SCHED_DEADLINE internal math precision. 171332ac17eSDario Faggioli * 10 -> just above 1us 172332ac17eSDario Faggioli * 9 -> just above 0.5us 173332ac17eSDario Faggioli */ 17497fb7a0aSIngo Molnar #define DL_SCALE 10 175332ac17eSDario Faggioli 176332ac17eSDario Faggioli /* 17797fb7a0aSIngo Molnar * Single value that denotes runtime == period, ie unlimited time. 178391e43daSPeter Zijlstra */ 179391e43daSPeter Zijlstra #define RUNTIME_INF ((u64)~0ULL) 180391e43daSPeter Zijlstra 18120f9cd2aSHenrik Austad static inline int idle_policy(int policy) 18220f9cd2aSHenrik Austad { 18320f9cd2aSHenrik Austad return policy == SCHED_IDLE; 18420f9cd2aSHenrik Austad } 185d50dde5aSDario Faggioli static inline int fair_policy(int policy) 186d50dde5aSDario Faggioli { 187d50dde5aSDario Faggioli return policy == SCHED_NORMAL || policy == SCHED_BATCH; 188d50dde5aSDario Faggioli } 189d50dde5aSDario Faggioli 190391e43daSPeter Zijlstra static inline int rt_policy(int policy) 191391e43daSPeter Zijlstra { 192d50dde5aSDario Faggioli return policy == SCHED_FIFO || policy == SCHED_RR; 193391e43daSPeter Zijlstra } 194391e43daSPeter Zijlstra 195aab03e05SDario Faggioli static inline int dl_policy(int policy) 196aab03e05SDario Faggioli { 197aab03e05SDario Faggioli return policy == SCHED_DEADLINE; 198aab03e05SDario Faggioli } 19920f9cd2aSHenrik Austad static inline bool valid_policy(int policy) 20020f9cd2aSHenrik Austad { 20120f9cd2aSHenrik Austad return idle_policy(policy) || fair_policy(policy) || 20220f9cd2aSHenrik Austad rt_policy(policy) || dl_policy(policy); 20320f9cd2aSHenrik Austad } 204aab03e05SDario Faggioli 2051da1843fSViresh Kumar static inline int task_has_idle_policy(struct task_struct *p) 2061da1843fSViresh Kumar { 2071da1843fSViresh Kumar return idle_policy(p->policy); 2081da1843fSViresh Kumar } 2091da1843fSViresh Kumar 210391e43daSPeter Zijlstra static inline int task_has_rt_policy(struct task_struct *p) 211391e43daSPeter Zijlstra { 212391e43daSPeter Zijlstra return rt_policy(p->policy); 213391e43daSPeter Zijlstra } 214391e43daSPeter Zijlstra 215aab03e05SDario Faggioli static inline int task_has_dl_policy(struct task_struct *p) 216aab03e05SDario Faggioli { 217aab03e05SDario Faggioli return dl_policy(p->policy); 218aab03e05SDario Faggioli } 219aab03e05SDario Faggioli 22007881166SJuri Lelli #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) 22107881166SJuri Lelli 222d76343c6SValentin Schneider static inline void update_avg(u64 *avg, u64 sample) 223d76343c6SValentin Schneider { 224d76343c6SValentin Schneider s64 diff = sample - *avg; 225d76343c6SValentin Schneider *avg += diff / 8; 226d76343c6SValentin Schneider } 227d76343c6SValentin Schneider 2282d3d891dSDario Faggioli /* 22939a2a6ebSValentin Schneider * Shifting a value by an exponent greater *or equal* to the size of said value 23039a2a6ebSValentin Schneider * is UB; cap at size-1. 23139a2a6ebSValentin Schneider */ 23239a2a6ebSValentin Schneider #define shr_bound(val, shift) \ 23339a2a6ebSValentin Schneider (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1)) 23439a2a6ebSValentin Schneider 23539a2a6ebSValentin Schneider /* 236794a56ebSJuri Lelli * !! For sched_setattr_nocheck() (kernel) only !! 237794a56ebSJuri Lelli * 238794a56ebSJuri Lelli * This is actually gross. :( 239794a56ebSJuri Lelli * 240794a56ebSJuri Lelli * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE 241794a56ebSJuri Lelli * tasks, but still be able to sleep. We need this on platforms that cannot 242794a56ebSJuri Lelli * atomically change clock frequency. Remove once fast switching will be 243794a56ebSJuri Lelli * available on such platforms. 244794a56ebSJuri Lelli * 245794a56ebSJuri Lelli * SUGOV stands for SchedUtil GOVernor. 246794a56ebSJuri Lelli */ 247794a56ebSJuri Lelli #define SCHED_FLAG_SUGOV 0x10000000 248794a56ebSJuri Lelli 249f9509153SQuentin Perret #define SCHED_DL_FLAGS (SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV) 250f9509153SQuentin Perret 251904cbab7SMatthew Wilcox (Oracle) static inline bool dl_entity_is_special(const struct sched_dl_entity *dl_se) 252794a56ebSJuri Lelli { 253794a56ebSJuri Lelli #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL 254794a56ebSJuri Lelli return unlikely(dl_se->flags & SCHED_FLAG_SUGOV); 255794a56ebSJuri Lelli #else 256794a56ebSJuri Lelli return false; 257794a56ebSJuri Lelli #endif 258794a56ebSJuri Lelli } 259794a56ebSJuri Lelli 260794a56ebSJuri Lelli /* 2612d3d891dSDario Faggioli * Tells if entity @a should preempt entity @b. 2622d3d891dSDario Faggioli */ 263904cbab7SMatthew Wilcox (Oracle) static inline bool dl_entity_preempt(const struct sched_dl_entity *a, 264904cbab7SMatthew Wilcox (Oracle) const struct sched_dl_entity *b) 2652d3d891dSDario Faggioli { 266794a56ebSJuri Lelli return dl_entity_is_special(a) || 267794a56ebSJuri Lelli dl_time_before(a->deadline, b->deadline); 2682d3d891dSDario Faggioli } 2692d3d891dSDario Faggioli 270391e43daSPeter Zijlstra /* 271391e43daSPeter Zijlstra * This is the priority-queue data structure of the RT scheduling class: 272391e43daSPeter Zijlstra */ 273391e43daSPeter Zijlstra struct rt_prio_array { 274391e43daSPeter Zijlstra DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ 275391e43daSPeter Zijlstra struct list_head queue[MAX_RT_PRIO]; 276391e43daSPeter Zijlstra }; 277391e43daSPeter Zijlstra 278391e43daSPeter Zijlstra struct rt_bandwidth { 279391e43daSPeter Zijlstra /* nests inside the rq lock: */ 280391e43daSPeter Zijlstra raw_spinlock_t rt_runtime_lock; 281391e43daSPeter Zijlstra ktime_t rt_period; 282391e43daSPeter Zijlstra u64 rt_runtime; 283391e43daSPeter Zijlstra struct hrtimer rt_period_timer; 2844cfafd30SPeter Zijlstra unsigned int rt_period_active; 285391e43daSPeter Zijlstra }; 286a5e7be3bSJuri Lelli 287a5e7be3bSJuri Lelli void __dl_clear_params(struct task_struct *p); 288a5e7be3bSJuri Lelli 289332ac17eSDario Faggioli static inline int dl_bandwidth_enabled(void) 290332ac17eSDario Faggioli { 2911724813dSPeter Zijlstra return sysctl_sched_rt_runtime >= 0; 292332ac17eSDario Faggioli } 293332ac17eSDario Faggioli 294a57415f5SPeng Liu /* 295a57415f5SPeng Liu * To keep the bandwidth of -deadline tasks under control 296a57415f5SPeng Liu * we need some place where: 297a57415f5SPeng Liu * - store the maximum -deadline bandwidth of each cpu; 298a57415f5SPeng Liu * - cache the fraction of bandwidth that is currently allocated in 299a57415f5SPeng Liu * each root domain; 300a57415f5SPeng Liu * 301a57415f5SPeng Liu * This is all done in the data structure below. It is similar to the 302a57415f5SPeng Liu * one used for RT-throttling (rt_bandwidth), with the main difference 303a57415f5SPeng Liu * that, since here we are only interested in admission control, we 304a57415f5SPeng Liu * do not decrease any runtime while the group "executes", neither we 305a57415f5SPeng Liu * need a timer to replenish it. 306a57415f5SPeng Liu * 307a57415f5SPeng Liu * With respect to SMP, bandwidth is given on a per root domain basis, 308a57415f5SPeng Liu * meaning that: 309a57415f5SPeng Liu * - bw (< 100%) is the deadline bandwidth of each CPU; 310a57415f5SPeng Liu * - total_bw is the currently allocated bandwidth in each root domain; 311a57415f5SPeng Liu */ 312332ac17eSDario Faggioli struct dl_bw { 313332ac17eSDario Faggioli raw_spinlock_t lock; 31497fb7a0aSIngo Molnar u64 bw; 31597fb7a0aSIngo Molnar u64 total_bw; 316332ac17eSDario Faggioli }; 317332ac17eSDario Faggioli 318f2cb1360SIngo Molnar extern void init_dl_bw(struct dl_bw *dl_b); 31906a76fe0SNicolas Pitre extern int sched_dl_global_validate(void); 32006a76fe0SNicolas Pitre extern void sched_dl_do_global(void); 32197fb7a0aSIngo Molnar extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr); 32206a76fe0SNicolas Pitre extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr); 32306a76fe0SNicolas Pitre extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr); 32406a76fe0SNicolas Pitre extern bool __checkparam_dl(const struct sched_attr *attr); 32506a76fe0SNicolas Pitre extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr); 32697fb7a0aSIngo Molnar extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial); 32785989106SDietmar Eggemann extern int dl_bw_check_overflow(int cpu); 328391e43daSPeter Zijlstra 329391e43daSPeter Zijlstra #ifdef CONFIG_CGROUP_SCHED 330391e43daSPeter Zijlstra 331391e43daSPeter Zijlstra struct cfs_rq; 332391e43daSPeter Zijlstra struct rt_rq; 333391e43daSPeter Zijlstra 33435cf4e50SMike Galbraith extern struct list_head task_groups; 335391e43daSPeter Zijlstra 336391e43daSPeter Zijlstra struct cfs_bandwidth { 337391e43daSPeter Zijlstra #ifdef CONFIG_CFS_BANDWIDTH 338391e43daSPeter Zijlstra raw_spinlock_t lock; 339391e43daSPeter Zijlstra ktime_t period; 34097fb7a0aSIngo Molnar u64 quota; 34197fb7a0aSIngo Molnar u64 runtime; 342f4183717SHuaixin Chang u64 burst; 343bcb1704aSHuaixin Chang u64 runtime_snap; 3449c58c79aSZhihui Zhang s64 hierarchical_quota; 345391e43daSPeter Zijlstra 34666567fcbSbsegall@google.com u8 idle; 34766567fcbSbsegall@google.com u8 period_active; 34866567fcbSbsegall@google.com u8 slack_started; 34997fb7a0aSIngo Molnar struct hrtimer period_timer; 35097fb7a0aSIngo Molnar struct hrtimer slack_timer; 351391e43daSPeter Zijlstra struct list_head throttled_cfs_rq; 352391e43daSPeter Zijlstra 35397fb7a0aSIngo Molnar /* Statistics: */ 35497fb7a0aSIngo Molnar int nr_periods; 35597fb7a0aSIngo Molnar int nr_throttled; 356bcb1704aSHuaixin Chang int nr_burst; 357391e43daSPeter Zijlstra u64 throttled_time; 358bcb1704aSHuaixin Chang u64 burst_time; 359391e43daSPeter Zijlstra #endif 360391e43daSPeter Zijlstra }; 361391e43daSPeter Zijlstra 36297fb7a0aSIngo Molnar /* Task group related information */ 363391e43daSPeter Zijlstra struct task_group { 364391e43daSPeter Zijlstra struct cgroup_subsys_state css; 365391e43daSPeter Zijlstra 366391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED 36797fb7a0aSIngo Molnar /* schedulable entities of this group on each CPU */ 368391e43daSPeter Zijlstra struct sched_entity **se; 36997fb7a0aSIngo Molnar /* runqueue "owned" by this group on each CPU */ 370391e43daSPeter Zijlstra struct cfs_rq **cfs_rq; 371391e43daSPeter Zijlstra unsigned long shares; 372391e43daSPeter Zijlstra 37330400039SJosh Don /* A positive value indicates that this is a SCHED_IDLE group. */ 37430400039SJosh Don int idle; 37530400039SJosh Don 376fa6bddebSAlex Shi #ifdef CONFIG_SMP 377b0367629SWaiman Long /* 378b0367629SWaiman Long * load_avg can be heavily contended at clock tick time, so put 379b0367629SWaiman Long * it in its own cacheline separated from the fields above which 380b0367629SWaiman Long * will also be accessed at each tick. 381b0367629SWaiman Long */ 382b0367629SWaiman Long atomic_long_t load_avg ____cacheline_aligned; 383391e43daSPeter Zijlstra #endif 384fa6bddebSAlex Shi #endif 385391e43daSPeter Zijlstra 386391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 387391e43daSPeter Zijlstra struct sched_rt_entity **rt_se; 388391e43daSPeter Zijlstra struct rt_rq **rt_rq; 389391e43daSPeter Zijlstra 390391e43daSPeter Zijlstra struct rt_bandwidth rt_bandwidth; 391391e43daSPeter Zijlstra #endif 392391e43daSPeter Zijlstra 393391e43daSPeter Zijlstra struct rcu_head rcu; 394391e43daSPeter Zijlstra struct list_head list; 395391e43daSPeter Zijlstra 396391e43daSPeter Zijlstra struct task_group *parent; 397391e43daSPeter Zijlstra struct list_head siblings; 398391e43daSPeter Zijlstra struct list_head children; 399391e43daSPeter Zijlstra 400391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_AUTOGROUP 401391e43daSPeter Zijlstra struct autogroup *autogroup; 402391e43daSPeter Zijlstra #endif 403391e43daSPeter Zijlstra 404391e43daSPeter Zijlstra struct cfs_bandwidth cfs_bandwidth; 4052480c093SPatrick Bellasi 4062480c093SPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK_GROUP 4072480c093SPatrick Bellasi /* The two decimal precision [%] value requested from user-space */ 4082480c093SPatrick Bellasi unsigned int uclamp_pct[UCLAMP_CNT]; 4092480c093SPatrick Bellasi /* Clamp values requested for a task group */ 4102480c093SPatrick Bellasi struct uclamp_se uclamp_req[UCLAMP_CNT]; 4110b60ba2dSPatrick Bellasi /* Effective clamp values used for a task group */ 4120b60ba2dSPatrick Bellasi struct uclamp_se uclamp[UCLAMP_CNT]; 4132480c093SPatrick Bellasi #endif 4142480c093SPatrick Bellasi 415391e43daSPeter Zijlstra }; 416391e43daSPeter Zijlstra 417391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED 418391e43daSPeter Zijlstra #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD 419391e43daSPeter Zijlstra 420391e43daSPeter Zijlstra /* 421391e43daSPeter Zijlstra * A weight of 0 or 1 can cause arithmetics problems. 422391e43daSPeter Zijlstra * A weight of a cfs_rq is the sum of weights of which entities 423391e43daSPeter Zijlstra * are queued on this cfs_rq, so a weight of a entity should not be 424391e43daSPeter Zijlstra * too large, so as the shares value of a task group. 425391e43daSPeter Zijlstra * (The default weight is 1024 - so there's no practical 426391e43daSPeter Zijlstra * limitation from this.) 427391e43daSPeter Zijlstra */ 428391e43daSPeter Zijlstra #define MIN_SHARES (1UL << 1) 429391e43daSPeter Zijlstra #define MAX_SHARES (1UL << 18) 430391e43daSPeter Zijlstra #endif 431391e43daSPeter Zijlstra 432391e43daSPeter Zijlstra typedef int (*tg_visitor)(struct task_group *, void *); 433391e43daSPeter Zijlstra 434391e43daSPeter Zijlstra extern int walk_tg_tree_from(struct task_group *from, 435391e43daSPeter Zijlstra tg_visitor down, tg_visitor up, void *data); 436391e43daSPeter Zijlstra 437391e43daSPeter Zijlstra /* 438391e43daSPeter Zijlstra * Iterate the full tree, calling @down when first entering a node and @up when 439391e43daSPeter Zijlstra * leaving it for the final time. 440391e43daSPeter Zijlstra * 441391e43daSPeter Zijlstra * Caller must hold rcu_lock or sufficient equivalent. 442391e43daSPeter Zijlstra */ 443391e43daSPeter Zijlstra static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) 444391e43daSPeter Zijlstra { 445391e43daSPeter Zijlstra return walk_tg_tree_from(&root_task_group, down, up, data); 446391e43daSPeter Zijlstra } 447391e43daSPeter Zijlstra 448391e43daSPeter Zijlstra extern int tg_nop(struct task_group *tg, void *data); 449391e43daSPeter Zijlstra 450391e43daSPeter Zijlstra extern void free_fair_sched_group(struct task_group *tg); 451391e43daSPeter Zijlstra extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent); 4528663e24dSPeter Zijlstra extern void online_fair_sched_group(struct task_group *tg); 4536fe1f348SPeter Zijlstra extern void unregister_fair_sched_group(struct task_group *tg); 454391e43daSPeter Zijlstra extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, 455391e43daSPeter Zijlstra struct sched_entity *se, int cpu, 456391e43daSPeter Zijlstra struct sched_entity *parent); 457391e43daSPeter Zijlstra extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b); 458391e43daSPeter Zijlstra 459391e43daSPeter Zijlstra extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b); 46077a4d1a1SPeter Zijlstra extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b); 461391e43daSPeter Zijlstra extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq); 462391e43daSPeter Zijlstra 463391e43daSPeter Zijlstra extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, 464391e43daSPeter Zijlstra struct sched_rt_entity *rt_se, int cpu, 465391e43daSPeter Zijlstra struct sched_rt_entity *parent); 4668887cd99SNicolas Pitre extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us); 4678887cd99SNicolas Pitre extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us); 4688887cd99SNicolas Pitre extern long sched_group_rt_runtime(struct task_group *tg); 4698887cd99SNicolas Pitre extern long sched_group_rt_period(struct task_group *tg); 4708887cd99SNicolas Pitre extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk); 471391e43daSPeter Zijlstra 47225cc7da7SLi Zefan extern struct task_group *sched_create_group(struct task_group *parent); 47325cc7da7SLi Zefan extern void sched_online_group(struct task_group *tg, 47425cc7da7SLi Zefan struct task_group *parent); 47525cc7da7SLi Zefan extern void sched_destroy_group(struct task_group *tg); 476b027789eSMathias Krause extern void sched_release_group(struct task_group *tg); 47725cc7da7SLi Zefan 47825cc7da7SLi Zefan extern void sched_move_task(struct task_struct *tsk); 47925cc7da7SLi Zefan 48025cc7da7SLi Zefan #ifdef CONFIG_FAIR_GROUP_SCHED 48125cc7da7SLi Zefan extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); 482ad936d86SByungchul Park 48330400039SJosh Don extern int sched_group_set_idle(struct task_group *tg, long idle); 48430400039SJosh Don 485ad936d86SByungchul Park #ifdef CONFIG_SMP 486ad936d86SByungchul Park extern void set_task_rq_fair(struct sched_entity *se, 487ad936d86SByungchul Park struct cfs_rq *prev, struct cfs_rq *next); 488ad936d86SByungchul Park #else /* !CONFIG_SMP */ 489ad936d86SByungchul Park static inline void set_task_rq_fair(struct sched_entity *se, 490ad936d86SByungchul Park struct cfs_rq *prev, struct cfs_rq *next) { } 491ad936d86SByungchul Park #endif /* CONFIG_SMP */ 492ad936d86SByungchul Park #endif /* CONFIG_FAIR_GROUP_SCHED */ 49325cc7da7SLi Zefan 494391e43daSPeter Zijlstra #else /* CONFIG_CGROUP_SCHED */ 495391e43daSPeter Zijlstra 496391e43daSPeter Zijlstra struct cfs_bandwidth { }; 497391e43daSPeter Zijlstra 498391e43daSPeter Zijlstra #endif /* CONFIG_CGROUP_SCHED */ 499391e43daSPeter Zijlstra 50087514b2cSBen Dooks extern void unregister_rt_sched_group(struct task_group *tg); 50187514b2cSBen Dooks extern void free_rt_sched_group(struct task_group *tg); 50287514b2cSBen Dooks extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent); 50387514b2cSBen Dooks 504d05b4305SVincent Donnefort /* 505d05b4305SVincent Donnefort * u64_u32_load/u64_u32_store 506d05b4305SVincent Donnefort * 507d05b4305SVincent Donnefort * Use a copy of a u64 value to protect against data race. This is only 508d05b4305SVincent Donnefort * applicable for 32-bits architectures. 509d05b4305SVincent Donnefort */ 510d05b4305SVincent Donnefort #ifdef CONFIG_64BIT 511d05b4305SVincent Donnefort # define u64_u32_load_copy(var, copy) var 512d05b4305SVincent Donnefort # define u64_u32_store_copy(var, copy, val) (var = val) 513d05b4305SVincent Donnefort #else 514d05b4305SVincent Donnefort # define u64_u32_load_copy(var, copy) \ 515d05b4305SVincent Donnefort ({ \ 516d05b4305SVincent Donnefort u64 __val, __val_copy; \ 517d05b4305SVincent Donnefort do { \ 518d05b4305SVincent Donnefort __val_copy = copy; \ 519d05b4305SVincent Donnefort /* \ 520d05b4305SVincent Donnefort * paired with u64_u32_store_copy(), ordering access \ 521d05b4305SVincent Donnefort * to var and copy. \ 522d05b4305SVincent Donnefort */ \ 523d05b4305SVincent Donnefort smp_rmb(); \ 524d05b4305SVincent Donnefort __val = var; \ 525d05b4305SVincent Donnefort } while (__val != __val_copy); \ 526d05b4305SVincent Donnefort __val; \ 527d05b4305SVincent Donnefort }) 528d05b4305SVincent Donnefort # define u64_u32_store_copy(var, copy, val) \ 529d05b4305SVincent Donnefort do { \ 530d05b4305SVincent Donnefort typeof(val) __val = (val); \ 531d05b4305SVincent Donnefort var = __val; \ 532d05b4305SVincent Donnefort /* \ 533d05b4305SVincent Donnefort * paired with u64_u32_load_copy(), ordering access to var and \ 534d05b4305SVincent Donnefort * copy. \ 535d05b4305SVincent Donnefort */ \ 536d05b4305SVincent Donnefort smp_wmb(); \ 537d05b4305SVincent Donnefort copy = __val; \ 538d05b4305SVincent Donnefort } while (0) 539d05b4305SVincent Donnefort #endif 540d05b4305SVincent Donnefort # define u64_u32_load(var) u64_u32_load_copy(var, var##_copy) 541d05b4305SVincent Donnefort # define u64_u32_store(var, val) u64_u32_store_copy(var, var##_copy, val) 542d05b4305SVincent Donnefort 543391e43daSPeter Zijlstra /* CFS-related fields in a runqueue */ 544391e43daSPeter Zijlstra struct cfs_rq { 545391e43daSPeter Zijlstra struct load_weight load; 54697fb7a0aSIngo Molnar unsigned int nr_running; 54743e9f7f2SViresh Kumar unsigned int h_nr_running; /* SCHED_{NORMAL,BATCH,IDLE} */ 548a480addeSJosh Don unsigned int idle_nr_running; /* SCHED_IDLE */ 54943e9f7f2SViresh Kumar unsigned int idle_h_nr_running; /* SCHED_IDLE */ 550391e43daSPeter Zijlstra 551391e43daSPeter Zijlstra u64 exec_clock; 552391e43daSPeter Zijlstra u64 min_vruntime; 553c6047c2eSJoel Fernandes (Google) #ifdef CONFIG_SCHED_CORE 554c6047c2eSJoel Fernandes (Google) unsigned int forceidle_seq; 555c6047c2eSJoel Fernandes (Google) u64 min_vruntime_fi; 556c6047c2eSJoel Fernandes (Google) #endif 557c6047c2eSJoel Fernandes (Google) 558391e43daSPeter Zijlstra #ifndef CONFIG_64BIT 559391e43daSPeter Zijlstra u64 min_vruntime_copy; 560391e43daSPeter Zijlstra #endif 561391e43daSPeter Zijlstra 562bfb06889SDavidlohr Bueso struct rb_root_cached tasks_timeline; 563391e43daSPeter Zijlstra 564391e43daSPeter Zijlstra /* 565391e43daSPeter Zijlstra * 'curr' points to currently running entity on this cfs_rq. 566391e43daSPeter Zijlstra * It is set to NULL otherwise (i.e when none are currently running). 567391e43daSPeter Zijlstra */ 56897fb7a0aSIngo Molnar struct sched_entity *curr; 56997fb7a0aSIngo Molnar struct sched_entity *next; 57097fb7a0aSIngo Molnar struct sched_entity *last; 57197fb7a0aSIngo Molnar struct sched_entity *skip; 572391e43daSPeter Zijlstra 573391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 574391e43daSPeter Zijlstra unsigned int nr_spread_over; 575391e43daSPeter Zijlstra #endif 576391e43daSPeter Zijlstra 5772dac754eSPaul Turner #ifdef CONFIG_SMP 5782dac754eSPaul Turner /* 5799d89c257SYuyang Du * CFS load tracking 5802dac754eSPaul Turner */ 5819d89c257SYuyang Du struct sched_avg avg; 5822a2f5d4eSPeter Zijlstra #ifndef CONFIG_64BIT 583d05b4305SVincent Donnefort u64 last_update_time_copy; 5842a2f5d4eSPeter Zijlstra #endif 5852a2f5d4eSPeter Zijlstra struct { 5862a2f5d4eSPeter Zijlstra raw_spinlock_t lock ____cacheline_aligned; 5872a2f5d4eSPeter Zijlstra int nr; 5882a2f5d4eSPeter Zijlstra unsigned long load_avg; 5892a2f5d4eSPeter Zijlstra unsigned long util_avg; 5909f683953SVincent Guittot unsigned long runnable_avg; 5912a2f5d4eSPeter Zijlstra } removed; 592141965c7SAlex Shi 593c566e8e9SPaul Turner #ifdef CONFIG_FAIR_GROUP_SCHED 5940e2d2aaaSPeter Zijlstra unsigned long tg_load_avg_contrib; 5950e2d2aaaSPeter Zijlstra long propagate; 5960e2d2aaaSPeter Zijlstra long prop_runnable_sum; 5970e2d2aaaSPeter Zijlstra 59882958366SPaul Turner /* 59982958366SPaul Turner * h_load = weight * f(tg) 60082958366SPaul Turner * 60182958366SPaul Turner * Where f(tg) is the recursive weight fraction assigned to 60282958366SPaul Turner * this group. 60382958366SPaul Turner */ 60482958366SPaul Turner unsigned long h_load; 60568520796SVladimir Davydov u64 last_h_load_update; 60668520796SVladimir Davydov struct sched_entity *h_load_next; 60768520796SVladimir Davydov #endif /* CONFIG_FAIR_GROUP_SCHED */ 60882958366SPaul Turner #endif /* CONFIG_SMP */ 60982958366SPaul Turner 610391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED 61197fb7a0aSIngo Molnar struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */ 612391e43daSPeter Zijlstra 613391e43daSPeter Zijlstra /* 614391e43daSPeter Zijlstra * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in 615391e43daSPeter Zijlstra * a hierarchy). Non-leaf lrqs hold other higher schedulable entities 616391e43daSPeter Zijlstra * (like users, containers etc.) 617391e43daSPeter Zijlstra * 61897fb7a0aSIngo Molnar * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU. 61997fb7a0aSIngo Molnar * This list is used during load balance. 620391e43daSPeter Zijlstra */ 621391e43daSPeter Zijlstra int on_list; 622391e43daSPeter Zijlstra struct list_head leaf_cfs_rq_list; 623391e43daSPeter Zijlstra struct task_group *tg; /* group that "owns" this runqueue */ 624391e43daSPeter Zijlstra 62530400039SJosh Don /* Locally cached copy of our task_group's idle value */ 62630400039SJosh Don int idle; 62730400039SJosh Don 628391e43daSPeter Zijlstra #ifdef CONFIG_CFS_BANDWIDTH 629391e43daSPeter Zijlstra int runtime_enabled; 630391e43daSPeter Zijlstra s64 runtime_remaining; 631391e43daSPeter Zijlstra 632e2f3e35fSVincent Donnefort u64 throttled_pelt_idle; 633e2f3e35fSVincent Donnefort #ifndef CONFIG_64BIT 634e2f3e35fSVincent Donnefort u64 throttled_pelt_idle_copy; 635e2f3e35fSVincent Donnefort #endif 63697fb7a0aSIngo Molnar u64 throttled_clock; 63764eaf507SChengming Zhou u64 throttled_clock_pelt; 63864eaf507SChengming Zhou u64 throttled_clock_pelt_time; 639677ea015SJosh Don u64 throttled_clock_self; 640677ea015SJosh Don u64 throttled_clock_self_time; 64197fb7a0aSIngo Molnar int throttled; 64297fb7a0aSIngo Molnar int throttle_count; 643391e43daSPeter Zijlstra struct list_head throttled_list; 6448ad075c2SJosh Don #ifdef CONFIG_SMP 6458ad075c2SJosh Don struct list_head throttled_csd_list; 6468ad075c2SJosh Don #endif 647391e43daSPeter Zijlstra #endif /* CONFIG_CFS_BANDWIDTH */ 648391e43daSPeter Zijlstra #endif /* CONFIG_FAIR_GROUP_SCHED */ 649391e43daSPeter Zijlstra }; 650391e43daSPeter Zijlstra 651391e43daSPeter Zijlstra static inline int rt_bandwidth_enabled(void) 652391e43daSPeter Zijlstra { 653391e43daSPeter Zijlstra return sysctl_sched_rt_runtime >= 0; 654391e43daSPeter Zijlstra } 655391e43daSPeter Zijlstra 656b6366f04SSteven Rostedt /* RT IPI pull logic requires IRQ_WORK */ 6574bdced5cSSteven Rostedt (Red Hat) #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP) 658b6366f04SSteven Rostedt # define HAVE_RT_PUSH_IPI 659b6366f04SSteven Rostedt #endif 660b6366f04SSteven Rostedt 661391e43daSPeter Zijlstra /* Real-Time classes' related field in a runqueue: */ 662391e43daSPeter Zijlstra struct rt_rq { 663391e43daSPeter Zijlstra struct rt_prio_array active; 664c82513e5SPeter Zijlstra unsigned int rt_nr_running; 66501d36d0aSFrederic Weisbecker unsigned int rr_nr_running; 666391e43daSPeter Zijlstra #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED 667391e43daSPeter Zijlstra struct { 668391e43daSPeter Zijlstra int curr; /* highest queued rt task prio */ 669391e43daSPeter Zijlstra #ifdef CONFIG_SMP 670391e43daSPeter Zijlstra int next; /* next highest */ 671391e43daSPeter Zijlstra #endif 672391e43daSPeter Zijlstra } highest_prio; 673391e43daSPeter Zijlstra #endif 674391e43daSPeter Zijlstra #ifdef CONFIG_SMP 675e6fe3f42SAlexey Dobriyan unsigned int rt_nr_migratory; 676e6fe3f42SAlexey Dobriyan unsigned int rt_nr_total; 677391e43daSPeter Zijlstra int overloaded; 678391e43daSPeter Zijlstra struct plist_head pushable_tasks; 679371bf427SVincent Guittot 680b6366f04SSteven Rostedt #endif /* CONFIG_SMP */ 681f4ebcbc0SKirill Tkhai int rt_queued; 682f4ebcbc0SKirill Tkhai 683391e43daSPeter Zijlstra int rt_throttled; 684391e43daSPeter Zijlstra u64 rt_time; 685391e43daSPeter Zijlstra u64 rt_runtime; 686391e43daSPeter Zijlstra /* Nests inside the rq lock: */ 687391e43daSPeter Zijlstra raw_spinlock_t rt_runtime_lock; 688391e43daSPeter Zijlstra 689391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 690e6fe3f42SAlexey Dobriyan unsigned int rt_nr_boosted; 691391e43daSPeter Zijlstra 692391e43daSPeter Zijlstra struct rq *rq; 693391e43daSPeter Zijlstra struct task_group *tg; 694391e43daSPeter Zijlstra #endif 695391e43daSPeter Zijlstra }; 696391e43daSPeter Zijlstra 697296b2ffeSVincent Guittot static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq) 698296b2ffeSVincent Guittot { 699296b2ffeSVincent Guittot return rt_rq->rt_queued && rt_rq->rt_nr_running; 700296b2ffeSVincent Guittot } 701296b2ffeSVincent Guittot 702aab03e05SDario Faggioli /* Deadline class' related fields in a runqueue */ 703aab03e05SDario Faggioli struct dl_rq { 704aab03e05SDario Faggioli /* runqueue is an rbtree, ordered by deadline */ 7052161573eSDavidlohr Bueso struct rb_root_cached root; 706aab03e05SDario Faggioli 707e6fe3f42SAlexey Dobriyan unsigned int dl_nr_running; 7081baca4ceSJuri Lelli 7091baca4ceSJuri Lelli #ifdef CONFIG_SMP 7101baca4ceSJuri Lelli /* 7111baca4ceSJuri Lelli * Deadline values of the currently executing and the 7121baca4ceSJuri Lelli * earliest ready task on this rq. Caching these facilitates 713dfcb245eSIngo Molnar * the decision whether or not a ready but not running task 7141baca4ceSJuri Lelli * should migrate somewhere else. 7151baca4ceSJuri Lelli */ 7161baca4ceSJuri Lelli struct { 7171baca4ceSJuri Lelli u64 curr; 7181baca4ceSJuri Lelli u64 next; 7191baca4ceSJuri Lelli } earliest_dl; 7201baca4ceSJuri Lelli 721e6fe3f42SAlexey Dobriyan unsigned int dl_nr_migratory; 7221baca4ceSJuri Lelli int overloaded; 7231baca4ceSJuri Lelli 7241baca4ceSJuri Lelli /* 7251baca4ceSJuri Lelli * Tasks on this rq that can be pushed away. They are kept in 7261baca4ceSJuri Lelli * an rb-tree, ordered by tasks' deadlines, with caching 7271baca4ceSJuri Lelli * of the leftmost (earliest deadline) element. 7281baca4ceSJuri Lelli */ 7292161573eSDavidlohr Bueso struct rb_root_cached pushable_dl_tasks_root; 730332ac17eSDario Faggioli #else 731332ac17eSDario Faggioli struct dl_bw dl_bw; 7321baca4ceSJuri Lelli #endif 733e36d8677SLuca Abeni /* 734e36d8677SLuca Abeni * "Active utilization" for this runqueue: increased when a 735e36d8677SLuca Abeni * task wakes up (becomes TASK_RUNNING) and decreased when a 736e36d8677SLuca Abeni * task blocks 737e36d8677SLuca Abeni */ 738e36d8677SLuca Abeni u64 running_bw; 7394da3abceSLuca Abeni 7404da3abceSLuca Abeni /* 7418fd27231SLuca Abeni * Utilization of the tasks "assigned" to this runqueue (including 7428fd27231SLuca Abeni * the tasks that are in runqueue and the tasks that executed on this 7438fd27231SLuca Abeni * CPU and blocked). Increased when a task moves to this runqueue, and 7448fd27231SLuca Abeni * decreased when the task moves away (migrates, changes scheduling 7458fd27231SLuca Abeni * policy, or terminates). 7468fd27231SLuca Abeni * This is needed to compute the "inactive utilization" for the 7478fd27231SLuca Abeni * runqueue (inactive utilization = this_bw - running_bw). 7488fd27231SLuca Abeni */ 7498fd27231SLuca Abeni u64 this_bw; 750daec5798SLuca Abeni u64 extra_bw; 7518fd27231SLuca Abeni 7528fd27231SLuca Abeni /* 7536a9d623aSVineeth Pillai * Maximum available bandwidth for reclaiming by SCHED_FLAG_RECLAIM 7546a9d623aSVineeth Pillai * tasks of this rq. Used in calculation of reclaimable bandwidth(GRUB). 7556a9d623aSVineeth Pillai */ 7566a9d623aSVineeth Pillai u64 max_bw; 7576a9d623aSVineeth Pillai 7586a9d623aSVineeth Pillai /* 7594da3abceSLuca Abeni * Inverse of the fraction of CPU utilization that can be reclaimed 7604da3abceSLuca Abeni * by the GRUB algorithm. 7614da3abceSLuca Abeni */ 7624da3abceSLuca Abeni u64 bw_ratio; 763aab03e05SDario Faggioli }; 764aab03e05SDario Faggioli 765c0796298SVincent Guittot #ifdef CONFIG_FAIR_GROUP_SCHED 766c0796298SVincent Guittot /* An entity is a task if it doesn't "own" a runqueue */ 767c0796298SVincent Guittot #define entity_is_task(se) (!se->my_q) 7680dacee1bSVincent Guittot 7699f683953SVincent Guittot static inline void se_update_runnable(struct sched_entity *se) 7709f683953SVincent Guittot { 7719f683953SVincent Guittot if (!entity_is_task(se)) 7729f683953SVincent Guittot se->runnable_weight = se->my_q->h_nr_running; 7739f683953SVincent Guittot } 7749f683953SVincent Guittot 7759f683953SVincent Guittot static inline long se_runnable(struct sched_entity *se) 7769f683953SVincent Guittot { 7779f683953SVincent Guittot if (entity_is_task(se)) 7789f683953SVincent Guittot return !!se->on_rq; 7799f683953SVincent Guittot else 7809f683953SVincent Guittot return se->runnable_weight; 7819f683953SVincent Guittot } 7829f683953SVincent Guittot 783c0796298SVincent Guittot #else 784c0796298SVincent Guittot #define entity_is_task(se) 1 7850dacee1bSVincent Guittot 7869f683953SVincent Guittot static inline void se_update_runnable(struct sched_entity *se) {} 7879f683953SVincent Guittot 7889f683953SVincent Guittot static inline long se_runnable(struct sched_entity *se) 7899f683953SVincent Guittot { 7909f683953SVincent Guittot return !!se->on_rq; 7919f683953SVincent Guittot } 792c0796298SVincent Guittot #endif 793c0796298SVincent Guittot 794391e43daSPeter Zijlstra #ifdef CONFIG_SMP 795c0796298SVincent Guittot /* 796c0796298SVincent Guittot * XXX we want to get rid of these helpers and use the full load resolution. 797c0796298SVincent Guittot */ 798c0796298SVincent Guittot static inline long se_weight(struct sched_entity *se) 799c0796298SVincent Guittot { 800c0796298SVincent Guittot return scale_load_down(se->load.weight); 801c0796298SVincent Guittot } 802c0796298SVincent Guittot 803391e43daSPeter Zijlstra 804afe06efdSTim Chen static inline bool sched_asym_prefer(int a, int b) 805afe06efdSTim Chen { 806afe06efdSTim Chen return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b); 807afe06efdSTim Chen } 808afe06efdSTim Chen 8096aa140faSQuentin Perret struct perf_domain { 8106aa140faSQuentin Perret struct em_perf_domain *em_pd; 8116aa140faSQuentin Perret struct perf_domain *next; 8126aa140faSQuentin Perret struct rcu_head rcu; 8136aa140faSQuentin Perret }; 8146aa140faSQuentin Perret 815630246a0SQuentin Perret /* Scheduling group status flags */ 816630246a0SQuentin Perret #define SG_OVERLOAD 0x1 /* More than one runnable task on a CPU. */ 8172802bf3cSMorten Rasmussen #define SG_OVERUTILIZED 0x2 /* One or more CPUs are over-utilized. */ 818630246a0SQuentin Perret 819391e43daSPeter Zijlstra /* 820391e43daSPeter Zijlstra * We add the notion of a root-domain which will be used to define per-domain 821391e43daSPeter Zijlstra * variables. Each exclusive cpuset essentially defines an island domain by 82297fb7a0aSIngo Molnar * fully partitioning the member CPUs from any other cpuset. Whenever a new 823391e43daSPeter Zijlstra * exclusive cpuset is created, we also create and attach a new root-domain 824391e43daSPeter Zijlstra * object. 825391e43daSPeter Zijlstra * 826391e43daSPeter Zijlstra */ 827391e43daSPeter Zijlstra struct root_domain { 828391e43daSPeter Zijlstra atomic_t refcount; 829391e43daSPeter Zijlstra atomic_t rto_count; 830391e43daSPeter Zijlstra struct rcu_head rcu; 831391e43daSPeter Zijlstra cpumask_var_t span; 832391e43daSPeter Zijlstra cpumask_var_t online; 833391e43daSPeter Zijlstra 834757ffdd7SValentin Schneider /* 835757ffdd7SValentin Schneider * Indicate pullable load on at least one CPU, e.g: 836757ffdd7SValentin Schneider * - More than one runnable task 837757ffdd7SValentin Schneider * - Running task is misfit 838757ffdd7SValentin Schneider */ 839575638d1SValentin Schneider int overload; 8404486edd1STim Chen 8412802bf3cSMorten Rasmussen /* Indicate one or more cpus over-utilized (tipping point) */ 8422802bf3cSMorten Rasmussen int overutilized; 8432802bf3cSMorten Rasmussen 844391e43daSPeter Zijlstra /* 8451baca4ceSJuri Lelli * The bit corresponding to a CPU gets set here if such CPU has more 8461baca4ceSJuri Lelli * than one runnable -deadline task (as it is below for RT tasks). 8471baca4ceSJuri Lelli */ 8481baca4ceSJuri Lelli cpumask_var_t dlo_mask; 8491baca4ceSJuri Lelli atomic_t dlo_count; 850332ac17eSDario Faggioli struct dl_bw dl_bw; 8516bfd6d72SJuri Lelli struct cpudl cpudl; 8521baca4ceSJuri Lelli 85326762423SPeng Liu /* 85426762423SPeng Liu * Indicate whether a root_domain's dl_bw has been checked or 85526762423SPeng Liu * updated. It's monotonously increasing value. 85626762423SPeng Liu * 85726762423SPeng Liu * Also, some corner cases, like 'wrap around' is dangerous, but given 85826762423SPeng Liu * that u64 is 'big enough'. So that shouldn't be a concern. 85926762423SPeng Liu */ 86026762423SPeng Liu u64 visit_gen; 86126762423SPeng Liu 8624bdced5cSSteven Rostedt (Red Hat) #ifdef HAVE_RT_PUSH_IPI 8634bdced5cSSteven Rostedt (Red Hat) /* 8644bdced5cSSteven Rostedt (Red Hat) * For IPI pull requests, loop across the rto_mask. 8654bdced5cSSteven Rostedt (Red Hat) */ 8664bdced5cSSteven Rostedt (Red Hat) struct irq_work rto_push_work; 8674bdced5cSSteven Rostedt (Red Hat) raw_spinlock_t rto_lock; 8684bdced5cSSteven Rostedt (Red Hat) /* These are only updated and read within rto_lock */ 8694bdced5cSSteven Rostedt (Red Hat) int rto_loop; 8704bdced5cSSteven Rostedt (Red Hat) int rto_cpu; 8714bdced5cSSteven Rostedt (Red Hat) /* These atomics are updated outside of a lock */ 8724bdced5cSSteven Rostedt (Red Hat) atomic_t rto_loop_next; 8734bdced5cSSteven Rostedt (Red Hat) atomic_t rto_loop_start; 8744bdced5cSSteven Rostedt (Red Hat) #endif 8751baca4ceSJuri Lelli /* 876391e43daSPeter Zijlstra * The "RT overload" flag: it gets set if a CPU has more than 877391e43daSPeter Zijlstra * one runnable RT task. 878391e43daSPeter Zijlstra */ 879391e43daSPeter Zijlstra cpumask_var_t rto_mask; 880391e43daSPeter Zijlstra struct cpupri cpupri; 881cd92bfd3SDietmar Eggemann 882cd92bfd3SDietmar Eggemann unsigned long max_cpu_capacity; 8836aa140faSQuentin Perret 8846aa140faSQuentin Perret /* 8856aa140faSQuentin Perret * NULL-terminated list of performance domains intersecting with the 8866aa140faSQuentin Perret * CPUs of the rd. Protected by RCU. 8876aa140faSQuentin Perret */ 8887ba7319fSJoel Fernandes (Google) struct perf_domain __rcu *pd; 889391e43daSPeter Zijlstra }; 890391e43daSPeter Zijlstra 891f2cb1360SIngo Molnar extern void init_defrootdomain(void); 8928d5dc512SPeter Zijlstra extern int sched_init_domains(const struct cpumask *cpu_map); 893f2cb1360SIngo Molnar extern void rq_attach_root(struct rq *rq, struct root_domain *rd); 894364f5665SSteven Rostedt (VMware) extern void sched_get_rd(struct root_domain *rd); 895364f5665SSteven Rostedt (VMware) extern void sched_put_rd(struct root_domain *rd); 896391e43daSPeter Zijlstra 8974bdced5cSSteven Rostedt (Red Hat) #ifdef HAVE_RT_PUSH_IPI 8984bdced5cSSteven Rostedt (Red Hat) extern void rto_push_irq_work_func(struct irq_work *work); 8994bdced5cSSteven Rostedt (Red Hat) #endif 900391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 901391e43daSPeter Zijlstra 90269842cbaSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK 90369842cbaSPatrick Bellasi /* 90469842cbaSPatrick Bellasi * struct uclamp_bucket - Utilization clamp bucket 90569842cbaSPatrick Bellasi * @value: utilization clamp value for tasks on this clamp bucket 90669842cbaSPatrick Bellasi * @tasks: number of RUNNABLE tasks on this clamp bucket 90769842cbaSPatrick Bellasi * 90869842cbaSPatrick Bellasi * Keep track of how many tasks are RUNNABLE for a given utilization 90969842cbaSPatrick Bellasi * clamp value. 91069842cbaSPatrick Bellasi */ 91169842cbaSPatrick Bellasi struct uclamp_bucket { 91269842cbaSPatrick Bellasi unsigned long value : bits_per(SCHED_CAPACITY_SCALE); 91369842cbaSPatrick Bellasi unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE); 91469842cbaSPatrick Bellasi }; 91569842cbaSPatrick Bellasi 91669842cbaSPatrick Bellasi /* 91769842cbaSPatrick Bellasi * struct uclamp_rq - rq's utilization clamp 91869842cbaSPatrick Bellasi * @value: currently active clamp values for a rq 91969842cbaSPatrick Bellasi * @bucket: utilization clamp buckets affecting a rq 92069842cbaSPatrick Bellasi * 92169842cbaSPatrick Bellasi * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values. 92269842cbaSPatrick Bellasi * A clamp value is affecting a rq when there is at least one task RUNNABLE 92369842cbaSPatrick Bellasi * (or actually running) with that value. 92469842cbaSPatrick Bellasi * 92569842cbaSPatrick Bellasi * There are up to UCLAMP_CNT possible different clamp values, currently there 92669842cbaSPatrick Bellasi * are only two: minimum utilization and maximum utilization. 92769842cbaSPatrick Bellasi * 92869842cbaSPatrick Bellasi * All utilization clamping values are MAX aggregated, since: 92969842cbaSPatrick Bellasi * - for util_min: we want to run the CPU at least at the max of the minimum 93069842cbaSPatrick Bellasi * utilization required by its currently RUNNABLE tasks. 93169842cbaSPatrick Bellasi * - for util_max: we want to allow the CPU to run up to the max of the 93269842cbaSPatrick Bellasi * maximum utilization allowed by its currently RUNNABLE tasks. 93369842cbaSPatrick Bellasi * 93469842cbaSPatrick Bellasi * Since on each system we expect only a limited number of different 93569842cbaSPatrick Bellasi * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track 93669842cbaSPatrick Bellasi * the metrics required to compute all the per-rq utilization clamp values. 93769842cbaSPatrick Bellasi */ 93869842cbaSPatrick Bellasi struct uclamp_rq { 93969842cbaSPatrick Bellasi unsigned int value; 94069842cbaSPatrick Bellasi struct uclamp_bucket bucket[UCLAMP_BUCKETS]; 94169842cbaSPatrick Bellasi }; 94246609ce2SQais Yousef 94346609ce2SQais Yousef DECLARE_STATIC_KEY_FALSE(sched_uclamp_used); 94469842cbaSPatrick Bellasi #endif /* CONFIG_UCLAMP_TASK */ 94569842cbaSPatrick Bellasi 9468e5bad7dSKees Cook struct rq; 9478e5bad7dSKees Cook struct balance_callback { 9488e5bad7dSKees Cook struct balance_callback *next; 9498e5bad7dSKees Cook void (*func)(struct rq *rq); 9508e5bad7dSKees Cook }; 9518e5bad7dSKees Cook 952391e43daSPeter Zijlstra /* 953391e43daSPeter Zijlstra * This is the main, per-CPU runqueue data structure. 954391e43daSPeter Zijlstra * 955391e43daSPeter Zijlstra * Locking rule: those places that want to lock multiple runqueues 956391e43daSPeter Zijlstra * (such as the load balancing or the thread migration code), lock 957391e43daSPeter Zijlstra * acquire operations must be ordered by ascending &runqueue. 958391e43daSPeter Zijlstra */ 959391e43daSPeter Zijlstra struct rq { 960391e43daSPeter Zijlstra /* runqueue lock: */ 9615cb9eaa3SPeter Zijlstra raw_spinlock_t __lock; 962391e43daSPeter Zijlstra 963391e43daSPeter Zijlstra /* 964391e43daSPeter Zijlstra * nr_running and cpu_load should be in the same cacheline because 965391e43daSPeter Zijlstra * remote CPUs use both these fields when doing load calculation. 966391e43daSPeter Zijlstra */ 967c82513e5SPeter Zijlstra unsigned int nr_running; 9680ec8aa00SPeter Zijlstra #ifdef CONFIG_NUMA_BALANCING 9690ec8aa00SPeter Zijlstra unsigned int nr_numa_running; 9700ec8aa00SPeter Zijlstra unsigned int nr_preferred_running; 971a4739ecaSSrikar Dronamraju unsigned int numa_migrate_on; 9720ec8aa00SPeter Zijlstra #endif 9733451d024SFrederic Weisbecker #ifdef CONFIG_NO_HZ_COMMON 9749fd81dd5SFrederic Weisbecker #ifdef CONFIG_SMP 975e022e0d3SPeter Zijlstra unsigned long last_blocked_load_update_tick; 976f643ea22SVincent Guittot unsigned int has_blocked_load; 97790b5363aSPeter Zijlstra (Intel) call_single_data_t nohz_csd; 9789fd81dd5SFrederic Weisbecker #endif /* CONFIG_SMP */ 97900357f5eSPeter Zijlstra unsigned int nohz_tick_stopped; 980a22e47a4SPeter Zijlstra atomic_t nohz_flags; 9819fd81dd5SFrederic Weisbecker #endif /* CONFIG_NO_HZ_COMMON */ 982dcdedb24SFrederic Weisbecker 983126c2092SPeter Zijlstra #ifdef CONFIG_SMP 984126c2092SPeter Zijlstra unsigned int ttwu_pending; 985126c2092SPeter Zijlstra #endif 986391e43daSPeter Zijlstra u64 nr_switches; 987391e43daSPeter Zijlstra 98869842cbaSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK 98969842cbaSPatrick Bellasi /* Utilization clamp values based on CPU's RUNNABLE tasks */ 99069842cbaSPatrick Bellasi struct uclamp_rq uclamp[UCLAMP_CNT] ____cacheline_aligned; 991e496187dSPatrick Bellasi unsigned int uclamp_flags; 992e496187dSPatrick Bellasi #define UCLAMP_FLAG_IDLE 0x01 99369842cbaSPatrick Bellasi #endif 99469842cbaSPatrick Bellasi 995391e43daSPeter Zijlstra struct cfs_rq cfs; 996391e43daSPeter Zijlstra struct rt_rq rt; 997aab03e05SDario Faggioli struct dl_rq dl; 998391e43daSPeter Zijlstra 999391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED 100097fb7a0aSIngo Molnar /* list of leaf cfs_rq on this CPU: */ 1001391e43daSPeter Zijlstra struct list_head leaf_cfs_rq_list; 10029c2791f9SVincent Guittot struct list_head *tmp_alone_branch; 1003a35b6466SPeter Zijlstra #endif /* CONFIG_FAIR_GROUP_SCHED */ 1004a35b6466SPeter Zijlstra 1005391e43daSPeter Zijlstra /* 1006391e43daSPeter Zijlstra * This is part of a global counter where only the total sum 1007391e43daSPeter Zijlstra * over all CPUs matters. A task can increase this counter on 1008391e43daSPeter Zijlstra * one CPU and if it got migrated afterwards it may decrease 1009391e43daSPeter Zijlstra * it on another CPU. Always updated under the runqueue lock: 1010391e43daSPeter Zijlstra */ 1011e6fe3f42SAlexey Dobriyan unsigned int nr_uninterruptible; 1012391e43daSPeter Zijlstra 10134104a562SMadhuparna Bhowmik struct task_struct __rcu *curr; 101497fb7a0aSIngo Molnar struct task_struct *idle; 101597fb7a0aSIngo Molnar struct task_struct *stop; 1016391e43daSPeter Zijlstra unsigned long next_balance; 1017391e43daSPeter Zijlstra struct mm_struct *prev_mm; 1018391e43daSPeter Zijlstra 1019cb42c9a3SMatt Fleming unsigned int clock_update_flags; 1020391e43daSPeter Zijlstra u64 clock; 102123127296SVincent Guittot /* Ensure that all clocks are in the same cache line */ 102223127296SVincent Guittot u64 clock_task ____cacheline_aligned; 102323127296SVincent Guittot u64 clock_pelt; 102423127296SVincent Guittot unsigned long lost_idle_time; 1025e2f3e35fSVincent Donnefort u64 clock_pelt_idle; 1026e2f3e35fSVincent Donnefort u64 clock_idle; 1027e2f3e35fSVincent Donnefort #ifndef CONFIG_64BIT 1028e2f3e35fSVincent Donnefort u64 clock_pelt_idle_copy; 1029e2f3e35fSVincent Donnefort u64 clock_idle_copy; 1030e2f3e35fSVincent Donnefort #endif 1031391e43daSPeter Zijlstra 1032391e43daSPeter Zijlstra atomic_t nr_iowait; 1033391e43daSPeter Zijlstra 1034c006fac5SPaul Turner #ifdef CONFIG_SCHED_DEBUG 1035c006fac5SPaul Turner u64 last_seen_need_resched_ns; 1036c006fac5SPaul Turner int ticks_without_resched; 1037c006fac5SPaul Turner #endif 1038c006fac5SPaul Turner 1039227a4aadSMathieu Desnoyers #ifdef CONFIG_MEMBARRIER 1040227a4aadSMathieu Desnoyers int membarrier_state; 1041227a4aadSMathieu Desnoyers #endif 1042227a4aadSMathieu Desnoyers 1043391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1044391e43daSPeter Zijlstra struct root_domain *rd; 1045994aeb7aSJoel Fernandes (Google) struct sched_domain __rcu *sd; 1046391e43daSPeter Zijlstra 1047ced549faSNicolas Pitre unsigned long cpu_capacity; 1048ca6d75e6SVincent Guittot unsigned long cpu_capacity_orig; 1049391e43daSPeter Zijlstra 10508e5bad7dSKees Cook struct balance_callback *balance_callback; 1051e3fca9e7SPeter Zijlstra 105219a1f5ecSPeter Zijlstra unsigned char nohz_idle_balance; 1053391e43daSPeter Zijlstra unsigned char idle_balance; 105497fb7a0aSIngo Molnar 10553b1baa64SMorten Rasmussen unsigned long misfit_task_load; 10563b1baa64SMorten Rasmussen 1057391e43daSPeter Zijlstra /* For active balancing */ 1058391e43daSPeter Zijlstra int active_balance; 1059391e43daSPeter Zijlstra int push_cpu; 1060391e43daSPeter Zijlstra struct cpu_stop_work active_balance_work; 106197fb7a0aSIngo Molnar 106297fb7a0aSIngo Molnar /* CPU of this runqueue: */ 1063391e43daSPeter Zijlstra int cpu; 1064391e43daSPeter Zijlstra int online; 1065391e43daSPeter Zijlstra 1066367456c7SPeter Zijlstra struct list_head cfs_tasks; 1067367456c7SPeter Zijlstra 1068371bf427SVincent Guittot struct sched_avg avg_rt; 10693727e0e1SVincent Guittot struct sched_avg avg_dl; 107011d4afd4SVincent Guittot #ifdef CONFIG_HAVE_SCHED_AVG_IRQ 107191c27493SVincent Guittot struct sched_avg avg_irq; 107291c27493SVincent Guittot #endif 107376504793SThara Gopinath #ifdef CONFIG_SCHED_THERMAL_PRESSURE 107476504793SThara Gopinath struct sched_avg avg_thermal; 107576504793SThara Gopinath #endif 1076391e43daSPeter Zijlstra u64 idle_stamp; 1077391e43daSPeter Zijlstra u64 avg_idle; 10789bd721c5SJason Low 107994aafc3eSPeter Zijlstra unsigned long wake_stamp; 108094aafc3eSPeter Zijlstra u64 wake_avg_idle; 108194aafc3eSPeter Zijlstra 10829bd721c5SJason Low /* This is used to determine avg_idle's max value */ 10839bd721c5SJason Low u64 max_idle_balance_cost; 1084f2469a1fSThomas Gleixner 1085f2469a1fSThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 1086f2469a1fSThomas Gleixner struct rcuwait hotplug_wait; 1087f2469a1fSThomas Gleixner #endif 108890b5363aSPeter Zijlstra (Intel) #endif /* CONFIG_SMP */ 1089391e43daSPeter Zijlstra 1090391e43daSPeter Zijlstra #ifdef CONFIG_IRQ_TIME_ACCOUNTING 1091391e43daSPeter Zijlstra u64 prev_irq_time; 1092391e43daSPeter Zijlstra #endif 1093391e43daSPeter Zijlstra #ifdef CONFIG_PARAVIRT 1094391e43daSPeter Zijlstra u64 prev_steal_time; 1095391e43daSPeter Zijlstra #endif 1096391e43daSPeter Zijlstra #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING 1097391e43daSPeter Zijlstra u64 prev_steal_time_rq; 1098391e43daSPeter Zijlstra #endif 1099391e43daSPeter Zijlstra 1100391e43daSPeter Zijlstra /* calc_load related fields */ 1101391e43daSPeter Zijlstra unsigned long calc_load_update; 1102391e43daSPeter Zijlstra long calc_load_active; 1103391e43daSPeter Zijlstra 1104391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_HRTICK 1105391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1106966a9671SYing Huang call_single_data_t hrtick_csd; 1107391e43daSPeter Zijlstra #endif 1108391e43daSPeter Zijlstra struct hrtimer hrtick_timer; 1109156ec6f4SJuri Lelli ktime_t hrtick_time; 1110391e43daSPeter Zijlstra #endif 1111391e43daSPeter Zijlstra 1112391e43daSPeter Zijlstra #ifdef CONFIG_SCHEDSTATS 1113391e43daSPeter Zijlstra /* latency stats */ 1114391e43daSPeter Zijlstra struct sched_info rq_sched_info; 1115391e43daSPeter Zijlstra unsigned long long rq_cpu_time; 1116391e43daSPeter Zijlstra /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ 1117391e43daSPeter Zijlstra 1118391e43daSPeter Zijlstra /* sys_sched_yield() stats */ 1119391e43daSPeter Zijlstra unsigned int yld_count; 1120391e43daSPeter Zijlstra 1121391e43daSPeter Zijlstra /* schedule() stats */ 1122391e43daSPeter Zijlstra unsigned int sched_count; 1123391e43daSPeter Zijlstra unsigned int sched_goidle; 1124391e43daSPeter Zijlstra 1125391e43daSPeter Zijlstra /* try_to_wake_up() stats */ 1126391e43daSPeter Zijlstra unsigned int ttwu_count; 1127391e43daSPeter Zijlstra unsigned int ttwu_local; 1128391e43daSPeter Zijlstra #endif 1129391e43daSPeter Zijlstra 1130442bf3aaSDaniel Lezcano #ifdef CONFIG_CPU_IDLE 1131442bf3aaSDaniel Lezcano /* Must be inspected within a rcu lock section */ 1132442bf3aaSDaniel Lezcano struct cpuidle_state *idle_state; 1133442bf3aaSDaniel Lezcano #endif 11343015ef4bSThomas Gleixner 113574d862b6SThomas Gleixner #ifdef CONFIG_SMP 11363015ef4bSThomas Gleixner unsigned int nr_pinned; 11373015ef4bSThomas Gleixner #endif 1138a7c81556SPeter Zijlstra unsigned int push_busy; 1139a7c81556SPeter Zijlstra struct cpu_stop_work push_work; 11409edeaea1SPeter Zijlstra 11419edeaea1SPeter Zijlstra #ifdef CONFIG_SCHED_CORE 11429edeaea1SPeter Zijlstra /* per rq */ 11439edeaea1SPeter Zijlstra struct rq *core; 1144539f6512SPeter Zijlstra struct task_struct *core_pick; 11459edeaea1SPeter Zijlstra unsigned int core_enabled; 1146539f6512SPeter Zijlstra unsigned int core_sched_seq; 11478a311c74SPeter Zijlstra struct rb_root core_tree; 11488a311c74SPeter Zijlstra 11493c474b32SPeter Zijlstra /* shared state -- careful with sched_core_cpu_deactivate() */ 11508a311c74SPeter Zijlstra unsigned int core_task_seq; 1151539f6512SPeter Zijlstra unsigned int core_pick_seq; 1152539f6512SPeter Zijlstra unsigned long core_cookie; 11534feee7d1SJosh Don unsigned int core_forceidle_count; 1154c6047c2eSJoel Fernandes (Google) unsigned int core_forceidle_seq; 11554feee7d1SJosh Don unsigned int core_forceidle_occupation; 11564feee7d1SJosh Don u64 core_forceidle_start; 11579edeaea1SPeter Zijlstra #endif 1158da019032SWaiman Long 1159da019032SWaiman Long /* Scratch cpumask to be temporarily used under rq_lock */ 1160da019032SWaiman Long cpumask_var_t scratch_mask; 11618ad075c2SJosh Don 11628ad075c2SJosh Don #if defined(CONFIG_CFS_BANDWIDTH) && defined(CONFIG_SMP) 11638ad075c2SJosh Don call_single_data_t cfsb_csd; 11648ad075c2SJosh Don struct list_head cfsb_csd_list; 11658ad075c2SJosh Don #endif 1166391e43daSPeter Zijlstra }; 1167391e43daSPeter Zijlstra 116862478d99SVincent Guittot #ifdef CONFIG_FAIR_GROUP_SCHED 116962478d99SVincent Guittot 117062478d99SVincent Guittot /* CPU runqueue to which this cfs_rq is attached */ 117162478d99SVincent Guittot static inline struct rq *rq_of(struct cfs_rq *cfs_rq) 117262478d99SVincent Guittot { 117362478d99SVincent Guittot return cfs_rq->rq; 117462478d99SVincent Guittot } 117562478d99SVincent Guittot 117662478d99SVincent Guittot #else 117762478d99SVincent Guittot 117862478d99SVincent Guittot static inline struct rq *rq_of(struct cfs_rq *cfs_rq) 117962478d99SVincent Guittot { 118062478d99SVincent Guittot return container_of(cfs_rq, struct rq, cfs); 118162478d99SVincent Guittot } 118262478d99SVincent Guittot #endif 118362478d99SVincent Guittot 1184391e43daSPeter Zijlstra static inline int cpu_of(struct rq *rq) 1185391e43daSPeter Zijlstra { 1186391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1187391e43daSPeter Zijlstra return rq->cpu; 1188391e43daSPeter Zijlstra #else 1189391e43daSPeter Zijlstra return 0; 1190391e43daSPeter Zijlstra #endif 1191391e43daSPeter Zijlstra } 1192391e43daSPeter Zijlstra 1193a7c81556SPeter Zijlstra #define MDF_PUSH 0x01 1194a7c81556SPeter Zijlstra 1195a7c81556SPeter Zijlstra static inline bool is_migration_disabled(struct task_struct *p) 1196a7c81556SPeter Zijlstra { 119774d862b6SThomas Gleixner #ifdef CONFIG_SMP 1198a7c81556SPeter Zijlstra return p->migration_disabled; 1199a7c81556SPeter Zijlstra #else 1200a7c81556SPeter Zijlstra return false; 1201a7c81556SPeter Zijlstra #endif 1202a7c81556SPeter Zijlstra } 12031b568f0aSPeter Zijlstra 1204e705968dSLin Shengwang DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); 1205e705968dSLin Shengwang 1206e705968dSLin Shengwang #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) 1207e705968dSLin Shengwang #define this_rq() this_cpu_ptr(&runqueues) 1208e705968dSLin Shengwang #define task_rq(p) cpu_rq(task_cpu(p)) 1209e705968dSLin Shengwang #define cpu_curr(cpu) (cpu_rq(cpu)->curr) 1210e705968dSLin Shengwang #define raw_rq() raw_cpu_ptr(&runqueues) 1211e705968dSLin Shengwang 121297886d9dSAubrey Li struct sched_group; 12139edeaea1SPeter Zijlstra #ifdef CONFIG_SCHED_CORE 121497886d9dSAubrey Li static inline struct cpumask *sched_group_span(struct sched_group *sg); 12159edeaea1SPeter Zijlstra 12169edeaea1SPeter Zijlstra DECLARE_STATIC_KEY_FALSE(__sched_core_enabled); 12179edeaea1SPeter Zijlstra 12189edeaea1SPeter Zijlstra static inline bool sched_core_enabled(struct rq *rq) 12199edeaea1SPeter Zijlstra { 12209edeaea1SPeter Zijlstra return static_branch_unlikely(&__sched_core_enabled) && rq->core_enabled; 12219edeaea1SPeter Zijlstra } 12229edeaea1SPeter Zijlstra 12239edeaea1SPeter Zijlstra static inline bool sched_core_disabled(void) 12249edeaea1SPeter Zijlstra { 12259edeaea1SPeter Zijlstra return !static_branch_unlikely(&__sched_core_enabled); 12269edeaea1SPeter Zijlstra } 12279edeaea1SPeter Zijlstra 12289ef7e7e3SPeter Zijlstra /* 12299ef7e7e3SPeter Zijlstra * Be careful with this function; not for general use. The return value isn't 12309ef7e7e3SPeter Zijlstra * stable unless you actually hold a relevant rq->__lock. 12319ef7e7e3SPeter Zijlstra */ 12329edeaea1SPeter Zijlstra static inline raw_spinlock_t *rq_lockp(struct rq *rq) 12339edeaea1SPeter Zijlstra { 12349edeaea1SPeter Zijlstra if (sched_core_enabled(rq)) 12359edeaea1SPeter Zijlstra return &rq->core->__lock; 12369edeaea1SPeter Zijlstra 12379edeaea1SPeter Zijlstra return &rq->__lock; 12389edeaea1SPeter Zijlstra } 12399edeaea1SPeter Zijlstra 12409ef7e7e3SPeter Zijlstra static inline raw_spinlock_t *__rq_lockp(struct rq *rq) 12419ef7e7e3SPeter Zijlstra { 12429ef7e7e3SPeter Zijlstra if (rq->core_enabled) 12439ef7e7e3SPeter Zijlstra return &rq->core->__lock; 12449ef7e7e3SPeter Zijlstra 12459ef7e7e3SPeter Zijlstra return &rq->__lock; 12469ef7e7e3SPeter Zijlstra } 12479ef7e7e3SPeter Zijlstra 1248904cbab7SMatthew Wilcox (Oracle) bool cfs_prio_less(const struct task_struct *a, const struct task_struct *b, 1249904cbab7SMatthew Wilcox (Oracle) bool fi); 1250c6047c2eSJoel Fernandes (Google) 125197886d9dSAubrey Li /* 125297886d9dSAubrey Li * Helpers to check if the CPU's core cookie matches with the task's cookie 125397886d9dSAubrey Li * when core scheduling is enabled. 125497886d9dSAubrey Li * A special case is that the task's cookie always matches with CPU's core 125597886d9dSAubrey Li * cookie if the CPU is in an idle core. 125697886d9dSAubrey Li */ 125797886d9dSAubrey Li static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p) 125897886d9dSAubrey Li { 125997886d9dSAubrey Li /* Ignore cookie match if core scheduler is not enabled on the CPU. */ 126097886d9dSAubrey Li if (!sched_core_enabled(rq)) 126197886d9dSAubrey Li return true; 126297886d9dSAubrey Li 126397886d9dSAubrey Li return rq->core->core_cookie == p->core_cookie; 126497886d9dSAubrey Li } 126597886d9dSAubrey Li 126697886d9dSAubrey Li static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) 126797886d9dSAubrey Li { 126897886d9dSAubrey Li bool idle_core = true; 126997886d9dSAubrey Li int cpu; 127097886d9dSAubrey Li 127197886d9dSAubrey Li /* Ignore cookie match if core scheduler is not enabled on the CPU. */ 127297886d9dSAubrey Li if (!sched_core_enabled(rq)) 127397886d9dSAubrey Li return true; 127497886d9dSAubrey Li 127597886d9dSAubrey Li for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) { 127697886d9dSAubrey Li if (!available_idle_cpu(cpu)) { 127797886d9dSAubrey Li idle_core = false; 127897886d9dSAubrey Li break; 127997886d9dSAubrey Li } 128097886d9dSAubrey Li } 128197886d9dSAubrey Li 128297886d9dSAubrey Li /* 128397886d9dSAubrey Li * A CPU in an idle core is always the best choice for tasks with 128497886d9dSAubrey Li * cookies. 128597886d9dSAubrey Li */ 128697886d9dSAubrey Li return idle_core || rq->core->core_cookie == p->core_cookie; 128797886d9dSAubrey Li } 128897886d9dSAubrey Li 128997886d9dSAubrey Li static inline bool sched_group_cookie_match(struct rq *rq, 129097886d9dSAubrey Li struct task_struct *p, 129197886d9dSAubrey Li struct sched_group *group) 129297886d9dSAubrey Li { 129397886d9dSAubrey Li int cpu; 129497886d9dSAubrey Li 129597886d9dSAubrey Li /* Ignore cookie match if core scheduler is not enabled on the CPU. */ 129697886d9dSAubrey Li if (!sched_core_enabled(rq)) 129797886d9dSAubrey Li return true; 129897886d9dSAubrey Li 129997886d9dSAubrey Li for_each_cpu_and(cpu, sched_group_span(group), p->cpus_ptr) { 1300e705968dSLin Shengwang if (sched_core_cookie_match(cpu_rq(cpu), p)) 130197886d9dSAubrey Li return true; 130297886d9dSAubrey Li } 130397886d9dSAubrey Li return false; 130497886d9dSAubrey Li } 130597886d9dSAubrey Li 13066e33cad0SPeter Zijlstra static inline bool sched_core_enqueued(struct task_struct *p) 13076e33cad0SPeter Zijlstra { 13086e33cad0SPeter Zijlstra return !RB_EMPTY_NODE(&p->core_node); 13096e33cad0SPeter Zijlstra } 13106e33cad0SPeter Zijlstra 13116e33cad0SPeter Zijlstra extern void sched_core_enqueue(struct rq *rq, struct task_struct *p); 13124feee7d1SJosh Don extern void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags); 13136e33cad0SPeter Zijlstra 13146e33cad0SPeter Zijlstra extern void sched_core_get(void); 13156e33cad0SPeter Zijlstra extern void sched_core_put(void); 13166e33cad0SPeter Zijlstra 13179edeaea1SPeter Zijlstra #else /* !CONFIG_SCHED_CORE */ 13189edeaea1SPeter Zijlstra 13199edeaea1SPeter Zijlstra static inline bool sched_core_enabled(struct rq *rq) 13209edeaea1SPeter Zijlstra { 13219edeaea1SPeter Zijlstra return false; 13229edeaea1SPeter Zijlstra } 13239edeaea1SPeter Zijlstra 1324d66f1b06SPeter Zijlstra static inline bool sched_core_disabled(void) 1325d66f1b06SPeter Zijlstra { 1326d66f1b06SPeter Zijlstra return true; 1327d66f1b06SPeter Zijlstra } 1328d66f1b06SPeter Zijlstra 132939d371b7SPeter Zijlstra static inline raw_spinlock_t *rq_lockp(struct rq *rq) 133039d371b7SPeter Zijlstra { 13315cb9eaa3SPeter Zijlstra return &rq->__lock; 133239d371b7SPeter Zijlstra } 133339d371b7SPeter Zijlstra 13349ef7e7e3SPeter Zijlstra static inline raw_spinlock_t *__rq_lockp(struct rq *rq) 13359ef7e7e3SPeter Zijlstra { 13369ef7e7e3SPeter Zijlstra return &rq->__lock; 13379ef7e7e3SPeter Zijlstra } 13389ef7e7e3SPeter Zijlstra 133997886d9dSAubrey Li static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p) 134097886d9dSAubrey Li { 134197886d9dSAubrey Li return true; 134297886d9dSAubrey Li } 134397886d9dSAubrey Li 134497886d9dSAubrey Li static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) 134597886d9dSAubrey Li { 134697886d9dSAubrey Li return true; 134797886d9dSAubrey Li } 134897886d9dSAubrey Li 134997886d9dSAubrey Li static inline bool sched_group_cookie_match(struct rq *rq, 135097886d9dSAubrey Li struct task_struct *p, 135197886d9dSAubrey Li struct sched_group *group) 135297886d9dSAubrey Li { 135397886d9dSAubrey Li return true; 135497886d9dSAubrey Li } 13559edeaea1SPeter Zijlstra #endif /* CONFIG_SCHED_CORE */ 13569edeaea1SPeter Zijlstra 135739d371b7SPeter Zijlstra static inline void lockdep_assert_rq_held(struct rq *rq) 135839d371b7SPeter Zijlstra { 13599ef7e7e3SPeter Zijlstra lockdep_assert_held(__rq_lockp(rq)); 136039d371b7SPeter Zijlstra } 136139d371b7SPeter Zijlstra 136239d371b7SPeter Zijlstra extern void raw_spin_rq_lock_nested(struct rq *rq, int subclass); 136339d371b7SPeter Zijlstra extern bool raw_spin_rq_trylock(struct rq *rq); 136439d371b7SPeter Zijlstra extern void raw_spin_rq_unlock(struct rq *rq); 136539d371b7SPeter Zijlstra 136639d371b7SPeter Zijlstra static inline void raw_spin_rq_lock(struct rq *rq) 136739d371b7SPeter Zijlstra { 136839d371b7SPeter Zijlstra raw_spin_rq_lock_nested(rq, 0); 136939d371b7SPeter Zijlstra } 137039d371b7SPeter Zijlstra 137139d371b7SPeter Zijlstra static inline void raw_spin_rq_lock_irq(struct rq *rq) 137239d371b7SPeter Zijlstra { 137339d371b7SPeter Zijlstra local_irq_disable(); 137439d371b7SPeter Zijlstra raw_spin_rq_lock(rq); 137539d371b7SPeter Zijlstra } 137639d371b7SPeter Zijlstra 137739d371b7SPeter Zijlstra static inline void raw_spin_rq_unlock_irq(struct rq *rq) 137839d371b7SPeter Zijlstra { 137939d371b7SPeter Zijlstra raw_spin_rq_unlock(rq); 138039d371b7SPeter Zijlstra local_irq_enable(); 138139d371b7SPeter Zijlstra } 138239d371b7SPeter Zijlstra 138339d371b7SPeter Zijlstra static inline unsigned long _raw_spin_rq_lock_irqsave(struct rq *rq) 138439d371b7SPeter Zijlstra { 138539d371b7SPeter Zijlstra unsigned long flags; 138639d371b7SPeter Zijlstra local_irq_save(flags); 138739d371b7SPeter Zijlstra raw_spin_rq_lock(rq); 138839d371b7SPeter Zijlstra return flags; 138939d371b7SPeter Zijlstra } 139039d371b7SPeter Zijlstra 139139d371b7SPeter Zijlstra static inline void raw_spin_rq_unlock_irqrestore(struct rq *rq, unsigned long flags) 139239d371b7SPeter Zijlstra { 139339d371b7SPeter Zijlstra raw_spin_rq_unlock(rq); 139439d371b7SPeter Zijlstra local_irq_restore(flags); 139539d371b7SPeter Zijlstra } 139639d371b7SPeter Zijlstra 139739d371b7SPeter Zijlstra #define raw_spin_rq_lock_irqsave(rq, flags) \ 139839d371b7SPeter Zijlstra do { \ 139939d371b7SPeter Zijlstra flags = _raw_spin_rq_lock_irqsave(rq); \ 140039d371b7SPeter Zijlstra } while (0) 140139d371b7SPeter Zijlstra 14021b568f0aSPeter Zijlstra #ifdef CONFIG_SCHED_SMT 14031b568f0aSPeter Zijlstra extern void __update_idle_core(struct rq *rq); 14041b568f0aSPeter Zijlstra 14051b568f0aSPeter Zijlstra static inline void update_idle_core(struct rq *rq) 14061b568f0aSPeter Zijlstra { 14071b568f0aSPeter Zijlstra if (static_branch_unlikely(&sched_smt_present)) 14081b568f0aSPeter Zijlstra __update_idle_core(rq); 14091b568f0aSPeter Zijlstra } 14101b568f0aSPeter Zijlstra 14111b568f0aSPeter Zijlstra #else 14121b568f0aSPeter Zijlstra static inline void update_idle_core(struct rq *rq) { } 14131b568f0aSPeter Zijlstra #endif 14141b568f0aSPeter Zijlstra 14158a311c74SPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED 14168a311c74SPeter Zijlstra static inline struct task_struct *task_of(struct sched_entity *se) 14178a311c74SPeter Zijlstra { 14188a311c74SPeter Zijlstra SCHED_WARN_ON(!entity_is_task(se)); 14198a311c74SPeter Zijlstra return container_of(se, struct task_struct, se); 14208a311c74SPeter Zijlstra } 14218a311c74SPeter Zijlstra 14228a311c74SPeter Zijlstra static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) 14238a311c74SPeter Zijlstra { 14248a311c74SPeter Zijlstra return p->se.cfs_rq; 14258a311c74SPeter Zijlstra } 14268a311c74SPeter Zijlstra 14278a311c74SPeter Zijlstra /* runqueue on which this entity is (to be) queued */ 1428904cbab7SMatthew Wilcox (Oracle) static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se) 14298a311c74SPeter Zijlstra { 14308a311c74SPeter Zijlstra return se->cfs_rq; 14318a311c74SPeter Zijlstra } 14328a311c74SPeter Zijlstra 14338a311c74SPeter Zijlstra /* runqueue "owned" by this group */ 14348a311c74SPeter Zijlstra static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) 14358a311c74SPeter Zijlstra { 14368a311c74SPeter Zijlstra return grp->my_q; 14378a311c74SPeter Zijlstra } 14388a311c74SPeter Zijlstra 14398a311c74SPeter Zijlstra #else 14408a311c74SPeter Zijlstra 1441904cbab7SMatthew Wilcox (Oracle) #define task_of(_se) container_of(_se, struct task_struct, se) 14428a311c74SPeter Zijlstra 1443904cbab7SMatthew Wilcox (Oracle) static inline struct cfs_rq *task_cfs_rq(const struct task_struct *p) 14448a311c74SPeter Zijlstra { 14458a311c74SPeter Zijlstra return &task_rq(p)->cfs; 14468a311c74SPeter Zijlstra } 14478a311c74SPeter Zijlstra 1448904cbab7SMatthew Wilcox (Oracle) static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se) 14498a311c74SPeter Zijlstra { 1450904cbab7SMatthew Wilcox (Oracle) const struct task_struct *p = task_of(se); 14518a311c74SPeter Zijlstra struct rq *rq = task_rq(p); 14528a311c74SPeter Zijlstra 14538a311c74SPeter Zijlstra return &rq->cfs; 14548a311c74SPeter Zijlstra } 14558a311c74SPeter Zijlstra 14568a311c74SPeter Zijlstra /* runqueue "owned" by this group */ 14578a311c74SPeter Zijlstra static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) 14588a311c74SPeter Zijlstra { 14598a311c74SPeter Zijlstra return NULL; 14608a311c74SPeter Zijlstra } 14618a311c74SPeter Zijlstra #endif 14628a311c74SPeter Zijlstra 14631f351d7fSJohannes Weiner extern void update_rq_clock(struct rq *rq); 14641f351d7fSJohannes Weiner 1465cb42c9a3SMatt Fleming /* 1466cb42c9a3SMatt Fleming * rq::clock_update_flags bits 1467cb42c9a3SMatt Fleming * 1468cb42c9a3SMatt Fleming * %RQCF_REQ_SKIP - will request skipping of clock update on the next 1469cb42c9a3SMatt Fleming * call to __schedule(). This is an optimisation to avoid 1470cb42c9a3SMatt Fleming * neighbouring rq clock updates. 1471cb42c9a3SMatt Fleming * 1472cb42c9a3SMatt Fleming * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is 1473cb42c9a3SMatt Fleming * in effect and calls to update_rq_clock() are being ignored. 1474cb42c9a3SMatt Fleming * 1475cb42c9a3SMatt Fleming * %RQCF_UPDATED - is a debug flag that indicates whether a call has been 1476cb42c9a3SMatt Fleming * made to update_rq_clock() since the last time rq::lock was pinned. 1477cb42c9a3SMatt Fleming * 1478cb42c9a3SMatt Fleming * If inside of __schedule(), clock_update_flags will have been 1479cb42c9a3SMatt Fleming * shifted left (a left shift is a cheap operation for the fast path 1480cb42c9a3SMatt Fleming * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use, 1481cb42c9a3SMatt Fleming * 1482cb42c9a3SMatt Fleming * if (rq-clock_update_flags >= RQCF_UPDATED) 1483cb42c9a3SMatt Fleming * 14843b03706fSIngo Molnar * to check if %RQCF_UPDATED is set. It'll never be shifted more than 1485cb42c9a3SMatt Fleming * one position though, because the next rq_unpin_lock() will shift it 1486cb42c9a3SMatt Fleming * back. 1487cb42c9a3SMatt Fleming */ 1488cb42c9a3SMatt Fleming #define RQCF_REQ_SKIP 0x01 1489cb42c9a3SMatt Fleming #define RQCF_ACT_SKIP 0x02 1490cb42c9a3SMatt Fleming #define RQCF_UPDATED 0x04 1491cb42c9a3SMatt Fleming 1492cb42c9a3SMatt Fleming static inline void assert_clock_updated(struct rq *rq) 1493cb42c9a3SMatt Fleming { 1494cb42c9a3SMatt Fleming /* 1495cb42c9a3SMatt Fleming * The only reason for not seeing a clock update since the 1496cb42c9a3SMatt Fleming * last rq_pin_lock() is if we're currently skipping updates. 1497cb42c9a3SMatt Fleming */ 1498cb42c9a3SMatt Fleming SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP); 1499cb42c9a3SMatt Fleming } 1500cb42c9a3SMatt Fleming 150178becc27SFrederic Weisbecker static inline u64 rq_clock(struct rq *rq) 150278becc27SFrederic Weisbecker { 15035cb9eaa3SPeter Zijlstra lockdep_assert_rq_held(rq); 1504cb42c9a3SMatt Fleming assert_clock_updated(rq); 1505cb42c9a3SMatt Fleming 150678becc27SFrederic Weisbecker return rq->clock; 150778becc27SFrederic Weisbecker } 150878becc27SFrederic Weisbecker 150978becc27SFrederic Weisbecker static inline u64 rq_clock_task(struct rq *rq) 151078becc27SFrederic Weisbecker { 15115cb9eaa3SPeter Zijlstra lockdep_assert_rq_held(rq); 1512cb42c9a3SMatt Fleming assert_clock_updated(rq); 1513cb42c9a3SMatt Fleming 151478becc27SFrederic Weisbecker return rq->clock_task; 151578becc27SFrederic Weisbecker } 151678becc27SFrederic Weisbecker 151705289b90SThara Gopinath /** 151805289b90SThara Gopinath * By default the decay is the default pelt decay period. 151905289b90SThara Gopinath * The decay shift can change the decay period in 152005289b90SThara Gopinath * multiples of 32. 152105289b90SThara Gopinath * Decay shift Decay period(ms) 152205289b90SThara Gopinath * 0 32 152305289b90SThara Gopinath * 1 64 152405289b90SThara Gopinath * 2 128 152505289b90SThara Gopinath * 3 256 152605289b90SThara Gopinath * 4 512 152705289b90SThara Gopinath */ 152805289b90SThara Gopinath extern int sched_thermal_decay_shift; 152905289b90SThara Gopinath 153005289b90SThara Gopinath static inline u64 rq_clock_thermal(struct rq *rq) 153105289b90SThara Gopinath { 153205289b90SThara Gopinath return rq_clock_task(rq) >> sched_thermal_decay_shift; 153305289b90SThara Gopinath } 153405289b90SThara Gopinath 1535adcc8da8SDavidlohr Bueso static inline void rq_clock_skip_update(struct rq *rq) 15369edfbfedSPeter Zijlstra { 15375cb9eaa3SPeter Zijlstra lockdep_assert_rq_held(rq); 1538cb42c9a3SMatt Fleming rq->clock_update_flags |= RQCF_REQ_SKIP; 1539adcc8da8SDavidlohr Bueso } 1540adcc8da8SDavidlohr Bueso 1541adcc8da8SDavidlohr Bueso /* 1542595058b6SDavidlohr Bueso * See rt task throttling, which is the only time a skip 15433b03706fSIngo Molnar * request is canceled. 1544adcc8da8SDavidlohr Bueso */ 1545adcc8da8SDavidlohr Bueso static inline void rq_clock_cancel_skipupdate(struct rq *rq) 1546adcc8da8SDavidlohr Bueso { 15475cb9eaa3SPeter Zijlstra lockdep_assert_rq_held(rq); 1548cb42c9a3SMatt Fleming rq->clock_update_flags &= ~RQCF_REQ_SKIP; 15499edfbfedSPeter Zijlstra } 15509edfbfedSPeter Zijlstra 1551ebb83d84SHao Jia /* 1552ebb83d84SHao Jia * During cpu offlining and rq wide unthrottling, we can trigger 1553ebb83d84SHao Jia * an update_rq_clock() for several cfs and rt runqueues (Typically 1554ebb83d84SHao Jia * when using list_for_each_entry_*) 1555ebb83d84SHao Jia * rq_clock_start_loop_update() can be called after updating the clock 1556ebb83d84SHao Jia * once and before iterating over the list to prevent multiple update. 1557ebb83d84SHao Jia * After the iterative traversal, we need to call rq_clock_stop_loop_update() 1558ebb83d84SHao Jia * to clear RQCF_ACT_SKIP of rq->clock_update_flags. 1559ebb83d84SHao Jia */ 1560ebb83d84SHao Jia static inline void rq_clock_start_loop_update(struct rq *rq) 1561ebb83d84SHao Jia { 1562ebb83d84SHao Jia lockdep_assert_rq_held(rq); 1563ebb83d84SHao Jia SCHED_WARN_ON(rq->clock_update_flags & RQCF_ACT_SKIP); 1564ebb83d84SHao Jia rq->clock_update_flags |= RQCF_ACT_SKIP; 1565ebb83d84SHao Jia } 1566ebb83d84SHao Jia 1567ebb83d84SHao Jia static inline void rq_clock_stop_loop_update(struct rq *rq) 1568ebb83d84SHao Jia { 1569ebb83d84SHao Jia lockdep_assert_rq_held(rq); 1570ebb83d84SHao Jia rq->clock_update_flags &= ~RQCF_ACT_SKIP; 1571ebb83d84SHao Jia } 1572ebb83d84SHao Jia 1573d8ac8971SMatt Fleming struct rq_flags { 1574d8ac8971SMatt Fleming unsigned long flags; 1575d8ac8971SMatt Fleming struct pin_cookie cookie; 1576cb42c9a3SMatt Fleming #ifdef CONFIG_SCHED_DEBUG 1577cb42c9a3SMatt Fleming /* 1578cb42c9a3SMatt Fleming * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the 1579cb42c9a3SMatt Fleming * current pin context is stashed here in case it needs to be 1580cb42c9a3SMatt Fleming * restored in rq_repin_lock(). 1581cb42c9a3SMatt Fleming */ 1582cb42c9a3SMatt Fleming unsigned int clock_update_flags; 1583cb42c9a3SMatt Fleming #endif 1584d8ac8971SMatt Fleming }; 1585d8ac8971SMatt Fleming 15868e5bad7dSKees Cook extern struct balance_callback balance_push_callback; 1587ae792702SPeter Zijlstra 158858877d34SPeter Zijlstra /* 158958877d34SPeter Zijlstra * Lockdep annotation that avoids accidental unlocks; it's like a 159058877d34SPeter Zijlstra * sticky/continuous lockdep_assert_held(). 159158877d34SPeter Zijlstra * 159258877d34SPeter Zijlstra * This avoids code that has access to 'struct rq *rq' (basically everything in 159358877d34SPeter Zijlstra * the scheduler) from accidentally unlocking the rq if they do not also have a 159458877d34SPeter Zijlstra * copy of the (on-stack) 'struct rq_flags rf'. 159558877d34SPeter Zijlstra * 159658877d34SPeter Zijlstra * Also see Documentation/locking/lockdep-design.rst. 159758877d34SPeter Zijlstra */ 1598d8ac8971SMatt Fleming static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf) 1599d8ac8971SMatt Fleming { 16009ef7e7e3SPeter Zijlstra rf->cookie = lockdep_pin_lock(__rq_lockp(rq)); 1601cb42c9a3SMatt Fleming 1602cb42c9a3SMatt Fleming #ifdef CONFIG_SCHED_DEBUG 1603cb42c9a3SMatt Fleming rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); 1604cb42c9a3SMatt Fleming rf->clock_update_flags = 0; 1605565790d2SPeter Zijlstra #ifdef CONFIG_SMP 1606ae792702SPeter Zijlstra SCHED_WARN_ON(rq->balance_callback && rq->balance_callback != &balance_push_callback); 1607ae792702SPeter Zijlstra #endif 1608565790d2SPeter Zijlstra #endif 1609d8ac8971SMatt Fleming } 1610d8ac8971SMatt Fleming 1611d8ac8971SMatt Fleming static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf) 1612d8ac8971SMatt Fleming { 1613cb42c9a3SMatt Fleming #ifdef CONFIG_SCHED_DEBUG 1614cb42c9a3SMatt Fleming if (rq->clock_update_flags > RQCF_ACT_SKIP) 1615cb42c9a3SMatt Fleming rf->clock_update_flags = RQCF_UPDATED; 1616cb42c9a3SMatt Fleming #endif 1617cb42c9a3SMatt Fleming 16189ef7e7e3SPeter Zijlstra lockdep_unpin_lock(__rq_lockp(rq), rf->cookie); 1619d8ac8971SMatt Fleming } 1620d8ac8971SMatt Fleming 1621d8ac8971SMatt Fleming static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf) 1622d8ac8971SMatt Fleming { 16239ef7e7e3SPeter Zijlstra lockdep_repin_lock(__rq_lockp(rq), rf->cookie); 1624cb42c9a3SMatt Fleming 1625cb42c9a3SMatt Fleming #ifdef CONFIG_SCHED_DEBUG 1626cb42c9a3SMatt Fleming /* 1627cb42c9a3SMatt Fleming * Restore the value we stashed in @rf for this pin context. 1628cb42c9a3SMatt Fleming */ 1629cb42c9a3SMatt Fleming rq->clock_update_flags |= rf->clock_update_flags; 1630cb42c9a3SMatt Fleming #endif 1631d8ac8971SMatt Fleming } 1632d8ac8971SMatt Fleming 16331f351d7fSJohannes Weiner struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) 16341f351d7fSJohannes Weiner __acquires(rq->lock); 16351f351d7fSJohannes Weiner 16361f351d7fSJohannes Weiner struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) 16371f351d7fSJohannes Weiner __acquires(p->pi_lock) 16381f351d7fSJohannes Weiner __acquires(rq->lock); 16391f351d7fSJohannes Weiner 16401f351d7fSJohannes Weiner static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf) 16411f351d7fSJohannes Weiner __releases(rq->lock) 16421f351d7fSJohannes Weiner { 16431f351d7fSJohannes Weiner rq_unpin_lock(rq, rf); 16445cb9eaa3SPeter Zijlstra raw_spin_rq_unlock(rq); 16451f351d7fSJohannes Weiner } 16461f351d7fSJohannes Weiner 16471f351d7fSJohannes Weiner static inline void 16481f351d7fSJohannes Weiner task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf) 16491f351d7fSJohannes Weiner __releases(rq->lock) 16501f351d7fSJohannes Weiner __releases(p->pi_lock) 16511f351d7fSJohannes Weiner { 16521f351d7fSJohannes Weiner rq_unpin_lock(rq, rf); 16535cb9eaa3SPeter Zijlstra raw_spin_rq_unlock(rq); 16541f351d7fSJohannes Weiner raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); 16551f351d7fSJohannes Weiner } 16561f351d7fSJohannes Weiner 16571f351d7fSJohannes Weiner static inline void 16581f351d7fSJohannes Weiner rq_lock_irqsave(struct rq *rq, struct rq_flags *rf) 16591f351d7fSJohannes Weiner __acquires(rq->lock) 16601f351d7fSJohannes Weiner { 16615cb9eaa3SPeter Zijlstra raw_spin_rq_lock_irqsave(rq, rf->flags); 16621f351d7fSJohannes Weiner rq_pin_lock(rq, rf); 16631f351d7fSJohannes Weiner } 16641f351d7fSJohannes Weiner 16651f351d7fSJohannes Weiner static inline void 16661f351d7fSJohannes Weiner rq_lock_irq(struct rq *rq, struct rq_flags *rf) 16671f351d7fSJohannes Weiner __acquires(rq->lock) 16681f351d7fSJohannes Weiner { 16695cb9eaa3SPeter Zijlstra raw_spin_rq_lock_irq(rq); 16701f351d7fSJohannes Weiner rq_pin_lock(rq, rf); 16711f351d7fSJohannes Weiner } 16721f351d7fSJohannes Weiner 16731f351d7fSJohannes Weiner static inline void 16741f351d7fSJohannes Weiner rq_lock(struct rq *rq, struct rq_flags *rf) 16751f351d7fSJohannes Weiner __acquires(rq->lock) 16761f351d7fSJohannes Weiner { 16775cb9eaa3SPeter Zijlstra raw_spin_rq_lock(rq); 16781f351d7fSJohannes Weiner rq_pin_lock(rq, rf); 16791f351d7fSJohannes Weiner } 16801f351d7fSJohannes Weiner 16811f351d7fSJohannes Weiner static inline void 16821f351d7fSJohannes Weiner rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf) 16831f351d7fSJohannes Weiner __releases(rq->lock) 16841f351d7fSJohannes Weiner { 16851f351d7fSJohannes Weiner rq_unpin_lock(rq, rf); 16865cb9eaa3SPeter Zijlstra raw_spin_rq_unlock_irqrestore(rq, rf->flags); 16871f351d7fSJohannes Weiner } 16881f351d7fSJohannes Weiner 16891f351d7fSJohannes Weiner static inline void 16901f351d7fSJohannes Weiner rq_unlock_irq(struct rq *rq, struct rq_flags *rf) 16911f351d7fSJohannes Weiner __releases(rq->lock) 16921f351d7fSJohannes Weiner { 16931f351d7fSJohannes Weiner rq_unpin_lock(rq, rf); 16945cb9eaa3SPeter Zijlstra raw_spin_rq_unlock_irq(rq); 16951f351d7fSJohannes Weiner } 16961f351d7fSJohannes Weiner 16971f351d7fSJohannes Weiner static inline void 16981f351d7fSJohannes Weiner rq_unlock(struct rq *rq, struct rq_flags *rf) 16991f351d7fSJohannes Weiner __releases(rq->lock) 17001f351d7fSJohannes Weiner { 17011f351d7fSJohannes Weiner rq_unpin_lock(rq, rf); 17025cb9eaa3SPeter Zijlstra raw_spin_rq_unlock(rq); 17031f351d7fSJohannes Weiner } 17041f351d7fSJohannes Weiner 1705246b3b33SJohannes Weiner static inline struct rq * 1706246b3b33SJohannes Weiner this_rq_lock_irq(struct rq_flags *rf) 1707246b3b33SJohannes Weiner __acquires(rq->lock) 1708246b3b33SJohannes Weiner { 1709246b3b33SJohannes Weiner struct rq *rq; 1710246b3b33SJohannes Weiner 1711246b3b33SJohannes Weiner local_irq_disable(); 1712246b3b33SJohannes Weiner rq = this_rq(); 1713246b3b33SJohannes Weiner rq_lock(rq, rf); 1714246b3b33SJohannes Weiner return rq; 1715246b3b33SJohannes Weiner } 1716246b3b33SJohannes Weiner 17179942f79bSRik van Riel #ifdef CONFIG_NUMA 1718e3fe70b1SRik van Riel enum numa_topology_type { 1719e3fe70b1SRik van Riel NUMA_DIRECT, 1720e3fe70b1SRik van Riel NUMA_GLUELESS_MESH, 1721e3fe70b1SRik van Riel NUMA_BACKPLANE, 1722e3fe70b1SRik van Riel }; 1723e3fe70b1SRik van Riel extern enum numa_topology_type sched_numa_topology_type; 17249942f79bSRik van Riel extern int sched_max_numa_distance; 17259942f79bSRik van Riel extern bool find_numa_distance(int distance); 17260fb3978bSHuang Ying extern void sched_init_numa(int offline_node); 17270fb3978bSHuang Ying extern void sched_update_numa(int cpu, bool online); 1728f2cb1360SIngo Molnar extern void sched_domains_numa_masks_set(unsigned int cpu); 1729f2cb1360SIngo Molnar extern void sched_domains_numa_masks_clear(unsigned int cpu); 1730e0e8d491SWanpeng Li extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu); 1731f2cb1360SIngo Molnar #else 17320fb3978bSHuang Ying static inline void sched_init_numa(int offline_node) { } 17330fb3978bSHuang Ying static inline void sched_update_numa(int cpu, bool online) { } 1734f2cb1360SIngo Molnar static inline void sched_domains_numa_masks_set(unsigned int cpu) { } 1735f2cb1360SIngo Molnar static inline void sched_domains_numa_masks_clear(unsigned int cpu) { } 1736e0e8d491SWanpeng Li static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu) 1737e0e8d491SWanpeng Li { 1738e0e8d491SWanpeng Li return nr_cpu_ids; 1739e0e8d491SWanpeng Li } 1740f2cb1360SIngo Molnar #endif 1741f2cb1360SIngo Molnar 1742f809ca9aSMel Gorman #ifdef CONFIG_NUMA_BALANCING 174344dba3d5SIulia Manda /* The regions in numa_faults array from task_struct */ 174444dba3d5SIulia Manda enum numa_faults_stats { 174544dba3d5SIulia Manda NUMA_MEM = 0, 174644dba3d5SIulia Manda NUMA_CPU, 174744dba3d5SIulia Manda NUMA_MEMBUF, 174844dba3d5SIulia Manda NUMA_CPUBUF 174944dba3d5SIulia Manda }; 17500ec8aa00SPeter Zijlstra extern void sched_setnuma(struct task_struct *p, int node); 1751e6628d5bSMel Gorman extern int migrate_task_to(struct task_struct *p, int cpu); 17520ad4e3dfSSrikar Dronamraju extern int migrate_swap(struct task_struct *p, struct task_struct *t, 17530ad4e3dfSSrikar Dronamraju int cpu, int scpu); 175413784475SMel Gorman extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p); 175513784475SMel Gorman #else 175613784475SMel Gorman static inline void 175713784475SMel Gorman init_numa_balancing(unsigned long clone_flags, struct task_struct *p) 175813784475SMel Gorman { 175913784475SMel Gorman } 1760f809ca9aSMel Gorman #endif /* CONFIG_NUMA_BALANCING */ 1761f809ca9aSMel Gorman 1762518cd623SPeter Zijlstra #ifdef CONFIG_SMP 1763518cd623SPeter Zijlstra 1764e3fca9e7SPeter Zijlstra static inline void 1765e3fca9e7SPeter Zijlstra queue_balance_callback(struct rq *rq, 17668e5bad7dSKees Cook struct balance_callback *head, 1767e3fca9e7SPeter Zijlstra void (*func)(struct rq *rq)) 1768e3fca9e7SPeter Zijlstra { 17695cb9eaa3SPeter Zijlstra lockdep_assert_rq_held(rq); 1770e3fca9e7SPeter Zijlstra 177104193d59SPeter Zijlstra /* 177204193d59SPeter Zijlstra * Don't (re)queue an already queued item; nor queue anything when 177304193d59SPeter Zijlstra * balance_push() is active, see the comment with 177404193d59SPeter Zijlstra * balance_push_callback. 177504193d59SPeter Zijlstra */ 1776ae792702SPeter Zijlstra if (unlikely(head->next || rq->balance_callback == &balance_push_callback)) 1777e3fca9e7SPeter Zijlstra return; 1778e3fca9e7SPeter Zijlstra 17798e5bad7dSKees Cook head->func = func; 1780e3fca9e7SPeter Zijlstra head->next = rq->balance_callback; 1781e3fca9e7SPeter Zijlstra rq->balance_callback = head; 1782e3fca9e7SPeter Zijlstra } 1783e3fca9e7SPeter Zijlstra 1784391e43daSPeter Zijlstra #define rcu_dereference_check_sched_domain(p) \ 1785391e43daSPeter Zijlstra rcu_dereference_check((p), \ 1786391e43daSPeter Zijlstra lockdep_is_held(&sched_domains_mutex)) 1787391e43daSPeter Zijlstra 1788391e43daSPeter Zijlstra /* 1789391e43daSPeter Zijlstra * The domain tree (rq->sd) is protected by RCU's quiescent state transition. 1790337e9b07SPaul E. McKenney * See destroy_sched_domains: call_rcu for details. 1791391e43daSPeter Zijlstra * 1792391e43daSPeter Zijlstra * The domain tree of any CPU may only be accessed from within 1793391e43daSPeter Zijlstra * preempt-disabled sections. 1794391e43daSPeter Zijlstra */ 1795391e43daSPeter Zijlstra #define for_each_domain(cpu, __sd) \ 1796518cd623SPeter Zijlstra for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \ 1797518cd623SPeter Zijlstra __sd; __sd = __sd->parent) 1798391e43daSPeter Zijlstra 179940b4d3dcSRicardo Neri /* A mask of all the SD flags that have the SDF_SHARED_CHILD metaflag */ 180040b4d3dcSRicardo Neri #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_SHARED_CHILD)) | 180140b4d3dcSRicardo Neri static const unsigned int SD_SHARED_CHILD_MASK = 180240b4d3dcSRicardo Neri #include <linux/sched/sd_flags.h> 180340b4d3dcSRicardo Neri 0; 180440b4d3dcSRicardo Neri #undef SD_FLAG 180540b4d3dcSRicardo Neri 1806518cd623SPeter Zijlstra /** 1807518cd623SPeter Zijlstra * highest_flag_domain - Return highest sched_domain containing flag. 180897fb7a0aSIngo Molnar * @cpu: The CPU whose highest level of sched domain is to 1809518cd623SPeter Zijlstra * be returned. 1810518cd623SPeter Zijlstra * @flag: The flag to check for the highest sched_domain 181197fb7a0aSIngo Molnar * for the given CPU. 1812518cd623SPeter Zijlstra * 181340b4d3dcSRicardo Neri * Returns the highest sched_domain of a CPU which contains @flag. If @flag has 181440b4d3dcSRicardo Neri * the SDF_SHARED_CHILD metaflag, all the children domains also have @flag. 1815518cd623SPeter Zijlstra */ 1816518cd623SPeter Zijlstra static inline struct sched_domain *highest_flag_domain(int cpu, int flag) 1817518cd623SPeter Zijlstra { 1818518cd623SPeter Zijlstra struct sched_domain *sd, *hsd = NULL; 1819518cd623SPeter Zijlstra 1820518cd623SPeter Zijlstra for_each_domain(cpu, sd) { 182140b4d3dcSRicardo Neri if (sd->flags & flag) { 1822518cd623SPeter Zijlstra hsd = sd; 182340b4d3dcSRicardo Neri continue; 182440b4d3dcSRicardo Neri } 182540b4d3dcSRicardo Neri 182640b4d3dcSRicardo Neri /* 182740b4d3dcSRicardo Neri * Stop the search if @flag is known to be shared at lower 182840b4d3dcSRicardo Neri * levels. It will not be found further up. 182940b4d3dcSRicardo Neri */ 183040b4d3dcSRicardo Neri if (flag & SD_SHARED_CHILD_MASK) 183140b4d3dcSRicardo Neri break; 1832518cd623SPeter Zijlstra } 1833518cd623SPeter Zijlstra 1834518cd623SPeter Zijlstra return hsd; 1835518cd623SPeter Zijlstra } 1836518cd623SPeter Zijlstra 1837fb13c7eeSMel Gorman static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) 1838fb13c7eeSMel Gorman { 1839fb13c7eeSMel Gorman struct sched_domain *sd; 1840fb13c7eeSMel Gorman 1841fb13c7eeSMel Gorman for_each_domain(cpu, sd) { 1842fb13c7eeSMel Gorman if (sd->flags & flag) 1843fb13c7eeSMel Gorman break; 1844fb13c7eeSMel Gorman } 1845fb13c7eeSMel Gorman 1846fb13c7eeSMel Gorman return sd; 1847fb13c7eeSMel Gorman } 1848fb13c7eeSMel Gorman 1849994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc); 18507d9ffa89SPeter Zijlstra DECLARE_PER_CPU(int, sd_llc_size); 1851518cd623SPeter Zijlstra DECLARE_PER_CPU(int, sd_llc_id); 1852994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared); 1853994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa); 1854994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing); 1855994aeb7aSJoel Fernandes (Google) DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity); 1856df054e84SMorten Rasmussen extern struct static_key_false sched_asym_cpucapacity; 1857518cd623SPeter Zijlstra 1858740cf8a7SDietmar Eggemann static __always_inline bool sched_asym_cpucap_active(void) 1859740cf8a7SDietmar Eggemann { 1860740cf8a7SDietmar Eggemann return static_branch_unlikely(&sched_asym_cpucapacity); 1861740cf8a7SDietmar Eggemann } 1862740cf8a7SDietmar Eggemann 186363b2ca30SNicolas Pitre struct sched_group_capacity { 18645e6521eaSLi Zefan atomic_t ref; 18655e6521eaSLi Zefan /* 1866172895e6SYuyang Du * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity 186763b2ca30SNicolas Pitre * for a single CPU. 18685e6521eaSLi Zefan */ 1869bf475ce0SMorten Rasmussen unsigned long capacity; 1870bf475ce0SMorten Rasmussen unsigned long min_capacity; /* Min per-CPU capacity in group */ 1871e3d6d0cbSMorten Rasmussen unsigned long max_capacity; /* Max per-CPU capacity in group */ 18725e6521eaSLi Zefan unsigned long next_update; 187363b2ca30SNicolas Pitre int imbalance; /* XXX unrelated to capacity but shared group state */ 18745e6521eaSLi Zefan 1875005f874dSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 1876005f874dSPeter Zijlstra int id; 1877005f874dSPeter Zijlstra #endif 1878005f874dSPeter Zijlstra 1879eba9f082Szhuguangqing unsigned long cpumask[]; /* Balance mask */ 18805e6521eaSLi Zefan }; 18815e6521eaSLi Zefan 18825e6521eaSLi Zefan struct sched_group { 18835e6521eaSLi Zefan struct sched_group *next; /* Must be a circular list */ 18845e6521eaSLi Zefan atomic_t ref; 18855e6521eaSLi Zefan 18865e6521eaSLi Zefan unsigned int group_weight; 1887*d24cb0d9STim C Chen unsigned int cores; 188863b2ca30SNicolas Pitre struct sched_group_capacity *sgc; 188997fb7a0aSIngo Molnar int asym_prefer_cpu; /* CPU of highest priority in group */ 189016d364baSRicardo Neri int flags; 18915e6521eaSLi Zefan 18925e6521eaSLi Zefan /* 18935e6521eaSLi Zefan * The CPUs this group covers. 18945e6521eaSLi Zefan * 18955e6521eaSLi Zefan * NOTE: this field is variable length. (Allocated dynamically 18965e6521eaSLi Zefan * by attaching extra space to the end of the structure, 18975e6521eaSLi Zefan * depending on how many CPUs the kernel has booted up with) 18985e6521eaSLi Zefan */ 189904f5c362SGustavo A. R. Silva unsigned long cpumask[]; 19005e6521eaSLi Zefan }; 19015e6521eaSLi Zefan 1902ae4df9d6SPeter Zijlstra static inline struct cpumask *sched_group_span(struct sched_group *sg) 19035e6521eaSLi Zefan { 19045e6521eaSLi Zefan return to_cpumask(sg->cpumask); 19055e6521eaSLi Zefan } 19065e6521eaSLi Zefan 19075e6521eaSLi Zefan /* 1908e5c14b1fSPeter Zijlstra * See build_balance_mask(). 19095e6521eaSLi Zefan */ 1910e5c14b1fSPeter Zijlstra static inline struct cpumask *group_balance_mask(struct sched_group *sg) 19115e6521eaSLi Zefan { 191263b2ca30SNicolas Pitre return to_cpumask(sg->sgc->cpumask); 19135e6521eaSLi Zefan } 19145e6521eaSLi Zefan 1915c1174876SPeter Zijlstra extern int group_balance_cpu(struct sched_group *sg); 1916c1174876SPeter Zijlstra 19173b87f136SPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 19183b87f136SPeter Zijlstra void update_sched_domain_debugfs(void); 1919bbdacdfeSPeter Zijlstra void dirty_sched_domain_sysctl(int cpu); 19203866e845SSteven Rostedt (Red Hat) #else 19213b87f136SPeter Zijlstra static inline void update_sched_domain_debugfs(void) 19223866e845SSteven Rostedt (Red Hat) { 19233866e845SSteven Rostedt (Red Hat) } 1924bbdacdfeSPeter Zijlstra static inline void dirty_sched_domain_sysctl(int cpu) 1925bbdacdfeSPeter Zijlstra { 1926bbdacdfeSPeter Zijlstra } 19273866e845SSteven Rostedt (Red Hat) #endif 19283866e845SSteven Rostedt (Red Hat) 19298a99b683SPeter Zijlstra extern int sched_update_scaling(void); 19308f9ea86fSWaiman Long 19318f9ea86fSWaiman Long static inline const struct cpumask *task_user_cpus(struct task_struct *p) 19328f9ea86fSWaiman Long { 19338f9ea86fSWaiman Long if (!p->user_cpus_ptr) 19348f9ea86fSWaiman Long return cpu_possible_mask; /* &init_task.cpus_mask */ 19358f9ea86fSWaiman Long return p->user_cpus_ptr; 19368f9ea86fSWaiman Long } 1937d664e399SThomas Gleixner #endif /* CONFIG_SMP */ 1938391e43daSPeter Zijlstra 1939391e43daSPeter Zijlstra #include "stats.h" 1940391e43daSPeter Zijlstra 19414feee7d1SJosh Don #if defined(CONFIG_SCHED_CORE) && defined(CONFIG_SCHEDSTATS) 19424feee7d1SJosh Don 19434feee7d1SJosh Don extern void __sched_core_account_forceidle(struct rq *rq); 19444feee7d1SJosh Don 19454feee7d1SJosh Don static inline void sched_core_account_forceidle(struct rq *rq) 19464feee7d1SJosh Don { 19474feee7d1SJosh Don if (schedstat_enabled()) 19484feee7d1SJosh Don __sched_core_account_forceidle(rq); 19494feee7d1SJosh Don } 19504feee7d1SJosh Don 19514feee7d1SJosh Don extern void __sched_core_tick(struct rq *rq); 19524feee7d1SJosh Don 19534feee7d1SJosh Don static inline void sched_core_tick(struct rq *rq) 19544feee7d1SJosh Don { 19554feee7d1SJosh Don if (sched_core_enabled(rq) && schedstat_enabled()) 19564feee7d1SJosh Don __sched_core_tick(rq); 19574feee7d1SJosh Don } 19584feee7d1SJosh Don 19594feee7d1SJosh Don #else 19604feee7d1SJosh Don 19614feee7d1SJosh Don static inline void sched_core_account_forceidle(struct rq *rq) {} 19624feee7d1SJosh Don 19634feee7d1SJosh Don static inline void sched_core_tick(struct rq *rq) {} 19644feee7d1SJosh Don 19654feee7d1SJosh Don #endif /* CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS */ 19664feee7d1SJosh Don 1967391e43daSPeter Zijlstra #ifdef CONFIG_CGROUP_SCHED 1968391e43daSPeter Zijlstra 1969391e43daSPeter Zijlstra /* 1970391e43daSPeter Zijlstra * Return the group to which this tasks belongs. 1971391e43daSPeter Zijlstra * 19728af01f56STejun Heo * We cannot use task_css() and friends because the cgroup subsystem 19738af01f56STejun Heo * changes that value before the cgroup_subsys::attach() method is called, 19748af01f56STejun Heo * therefore we cannot pin it and might observe the wrong value. 19758323f26cSPeter Zijlstra * 19768323f26cSPeter Zijlstra * The same is true for autogroup's p->signal->autogroup->tg, the autogroup 19778323f26cSPeter Zijlstra * core changes this before calling sched_move_task(). 19788323f26cSPeter Zijlstra * 19798323f26cSPeter Zijlstra * Instead we use a 'copy' which is updated from sched_move_task() while 19808323f26cSPeter Zijlstra * holding both task_struct::pi_lock and rq::lock. 1981391e43daSPeter Zijlstra */ 1982391e43daSPeter Zijlstra static inline struct task_group *task_group(struct task_struct *p) 1983391e43daSPeter Zijlstra { 19848323f26cSPeter Zijlstra return p->sched_task_group; 1985391e43daSPeter Zijlstra } 1986391e43daSPeter Zijlstra 1987391e43daSPeter Zijlstra /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ 1988391e43daSPeter Zijlstra static inline void set_task_rq(struct task_struct *p, unsigned int cpu) 1989391e43daSPeter Zijlstra { 1990391e43daSPeter Zijlstra #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED) 1991391e43daSPeter Zijlstra struct task_group *tg = task_group(p); 1992391e43daSPeter Zijlstra #endif 1993391e43daSPeter Zijlstra 1994391e43daSPeter Zijlstra #ifdef CONFIG_FAIR_GROUP_SCHED 1995ad936d86SByungchul Park set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]); 1996391e43daSPeter Zijlstra p->se.cfs_rq = tg->cfs_rq[cpu]; 1997391e43daSPeter Zijlstra p->se.parent = tg->se[cpu]; 199878b6b157SChengming Zhou p->se.depth = tg->se[cpu] ? tg->se[cpu]->depth + 1 : 0; 1999391e43daSPeter Zijlstra #endif 2000391e43daSPeter Zijlstra 2001391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 2002391e43daSPeter Zijlstra p->rt.rt_rq = tg->rt_rq[cpu]; 2003391e43daSPeter Zijlstra p->rt.parent = tg->rt_se[cpu]; 2004391e43daSPeter Zijlstra #endif 2005391e43daSPeter Zijlstra } 2006391e43daSPeter Zijlstra 2007391e43daSPeter Zijlstra #else /* CONFIG_CGROUP_SCHED */ 2008391e43daSPeter Zijlstra 2009391e43daSPeter Zijlstra static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } 2010391e43daSPeter Zijlstra static inline struct task_group *task_group(struct task_struct *p) 2011391e43daSPeter Zijlstra { 2012391e43daSPeter Zijlstra return NULL; 2013391e43daSPeter Zijlstra } 2014391e43daSPeter Zijlstra 2015391e43daSPeter Zijlstra #endif /* CONFIG_CGROUP_SCHED */ 2016391e43daSPeter Zijlstra 2017391e43daSPeter Zijlstra static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) 2018391e43daSPeter Zijlstra { 2019391e43daSPeter Zijlstra set_task_rq(p, cpu); 2020391e43daSPeter Zijlstra #ifdef CONFIG_SMP 2021391e43daSPeter Zijlstra /* 2022391e43daSPeter Zijlstra * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be 2023dfcb245eSIngo Molnar * successfully executed on another CPU. We must ensure that updates of 2024391e43daSPeter Zijlstra * per-task data have been completed by this moment. 2025391e43daSPeter Zijlstra */ 2026391e43daSPeter Zijlstra smp_wmb(); 2027c546951dSAndrea Parri WRITE_ONCE(task_thread_info(p)->cpu, cpu); 2028ac66f547SPeter Zijlstra p->wake_cpu = cpu; 2029391e43daSPeter Zijlstra #endif 2030391e43daSPeter Zijlstra } 2031391e43daSPeter Zijlstra 2032391e43daSPeter Zijlstra /* 2033391e43daSPeter Zijlstra * Tunables that become constants when CONFIG_SCHED_DEBUG is off: 2034391e43daSPeter Zijlstra */ 2035391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 2036391e43daSPeter Zijlstra # define const_debug __read_mostly 2037391e43daSPeter Zijlstra #else 2038391e43daSPeter Zijlstra # define const_debug const 2039391e43daSPeter Zijlstra #endif 2040391e43daSPeter Zijlstra 2041391e43daSPeter Zijlstra #define SCHED_FEAT(name, enabled) \ 2042391e43daSPeter Zijlstra __SCHED_FEAT_##name , 2043391e43daSPeter Zijlstra 2044391e43daSPeter Zijlstra enum { 2045391e43daSPeter Zijlstra #include "features.h" 2046f8b6d1ccSPeter Zijlstra __SCHED_FEAT_NR, 2047391e43daSPeter Zijlstra }; 2048391e43daSPeter Zijlstra 2049391e43daSPeter Zijlstra #undef SCHED_FEAT 2050391e43daSPeter Zijlstra 2051a73f863aSJuri Lelli #ifdef CONFIG_SCHED_DEBUG 2052765cc3a4SPatrick Bellasi 2053765cc3a4SPatrick Bellasi /* 2054765cc3a4SPatrick Bellasi * To support run-time toggling of sched features, all the translation units 2055765cc3a4SPatrick Bellasi * (but core.c) reference the sysctl_sched_features defined in core.c. 2056765cc3a4SPatrick Bellasi */ 2057765cc3a4SPatrick Bellasi extern const_debug unsigned int sysctl_sched_features; 2058765cc3a4SPatrick Bellasi 2059a73f863aSJuri Lelli #ifdef CONFIG_JUMP_LABEL 2060f8b6d1ccSPeter Zijlstra #define SCHED_FEAT(name, enabled) \ 2061c5905afbSIngo Molnar static __always_inline bool static_branch_##name(struct static_key *key) \ 2062f8b6d1ccSPeter Zijlstra { \ 20636e76ea8aSJason Baron return static_key_##enabled(key); \ 2064f8b6d1ccSPeter Zijlstra } 2065f8b6d1ccSPeter Zijlstra 2066f8b6d1ccSPeter Zijlstra #include "features.h" 2067f8b6d1ccSPeter Zijlstra #undef SCHED_FEAT 2068f8b6d1ccSPeter Zijlstra 2069c5905afbSIngo Molnar extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; 2070f8b6d1ccSPeter Zijlstra #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x])) 2071765cc3a4SPatrick Bellasi 2072a73f863aSJuri Lelli #else /* !CONFIG_JUMP_LABEL */ 2073a73f863aSJuri Lelli 2074a73f863aSJuri Lelli #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) 2075a73f863aSJuri Lelli 2076a73f863aSJuri Lelli #endif /* CONFIG_JUMP_LABEL */ 2077a73f863aSJuri Lelli 2078a73f863aSJuri Lelli #else /* !SCHED_DEBUG */ 2079765cc3a4SPatrick Bellasi 2080765cc3a4SPatrick Bellasi /* 2081765cc3a4SPatrick Bellasi * Each translation unit has its own copy of sysctl_sched_features to allow 2082765cc3a4SPatrick Bellasi * constants propagation at compile time and compiler optimization based on 2083765cc3a4SPatrick Bellasi * features default. 2084765cc3a4SPatrick Bellasi */ 2085765cc3a4SPatrick Bellasi #define SCHED_FEAT(name, enabled) \ 2086765cc3a4SPatrick Bellasi (1UL << __SCHED_FEAT_##name) * enabled | 2087765cc3a4SPatrick Bellasi static const_debug __maybe_unused unsigned int sysctl_sched_features = 2088765cc3a4SPatrick Bellasi #include "features.h" 2089765cc3a4SPatrick Bellasi 0; 2090765cc3a4SPatrick Bellasi #undef SCHED_FEAT 2091765cc3a4SPatrick Bellasi 20927e6f4c5dSPeter Zijlstra #define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) 2093765cc3a4SPatrick Bellasi 2094a73f863aSJuri Lelli #endif /* SCHED_DEBUG */ 2095391e43daSPeter Zijlstra 20962a595721SSrikar Dronamraju extern struct static_key_false sched_numa_balancing; 2097cb251765SMel Gorman extern struct static_key_false sched_schedstats; 2098cbee9f88SPeter Zijlstra 2099391e43daSPeter Zijlstra static inline u64 global_rt_period(void) 2100391e43daSPeter Zijlstra { 2101391e43daSPeter Zijlstra return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; 2102391e43daSPeter Zijlstra } 2103391e43daSPeter Zijlstra 2104391e43daSPeter Zijlstra static inline u64 global_rt_runtime(void) 2105391e43daSPeter Zijlstra { 2106391e43daSPeter Zijlstra if (sysctl_sched_rt_runtime < 0) 2107391e43daSPeter Zijlstra return RUNTIME_INF; 2108391e43daSPeter Zijlstra 2109391e43daSPeter Zijlstra return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; 2110391e43daSPeter Zijlstra } 2111391e43daSPeter Zijlstra 2112391e43daSPeter Zijlstra static inline int task_current(struct rq *rq, struct task_struct *p) 2113391e43daSPeter Zijlstra { 2114391e43daSPeter Zijlstra return rq->curr == p; 2115391e43daSPeter Zijlstra } 2116391e43daSPeter Zijlstra 21170b9d46fcSPeter Zijlstra static inline int task_on_cpu(struct rq *rq, struct task_struct *p) 2118391e43daSPeter Zijlstra { 2119391e43daSPeter Zijlstra #ifdef CONFIG_SMP 2120391e43daSPeter Zijlstra return p->on_cpu; 2121391e43daSPeter Zijlstra #else 2122391e43daSPeter Zijlstra return task_current(rq, p); 2123391e43daSPeter Zijlstra #endif 2124391e43daSPeter Zijlstra } 2125391e43daSPeter Zijlstra 2126da0c1e65SKirill Tkhai static inline int task_on_rq_queued(struct task_struct *p) 2127da0c1e65SKirill Tkhai { 2128da0c1e65SKirill Tkhai return p->on_rq == TASK_ON_RQ_QUEUED; 2129da0c1e65SKirill Tkhai } 2130391e43daSPeter Zijlstra 2131cca26e80SKirill Tkhai static inline int task_on_rq_migrating(struct task_struct *p) 2132cca26e80SKirill Tkhai { 2133c546951dSAndrea Parri return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING; 2134cca26e80SKirill Tkhai } 2135cca26e80SKirill Tkhai 213617770579SValentin Schneider /* Wake flags. The first three directly map to some SD flag value */ 213717770579SValentin Schneider #define WF_EXEC 0x02 /* Wakeup after exec; maps to SD_BALANCE_EXEC */ 213817770579SValentin Schneider #define WF_FORK 0x04 /* Wakeup after fork; maps to SD_BALANCE_FORK */ 213917770579SValentin Schneider #define WF_TTWU 0x08 /* Wakeup; maps to SD_BALANCE_WAKE */ 214017770579SValentin Schneider 214117770579SValentin Schneider #define WF_SYNC 0x10 /* Waker goes to sleep after wakeup */ 214217770579SValentin Schneider #define WF_MIGRATED 0x20 /* Internal use, task got migrated */ 214317770579SValentin Schneider 214417770579SValentin Schneider #ifdef CONFIG_SMP 214517770579SValentin Schneider static_assert(WF_EXEC == SD_BALANCE_EXEC); 214617770579SValentin Schneider static_assert(WF_FORK == SD_BALANCE_FORK); 214717770579SValentin Schneider static_assert(WF_TTWU == SD_BALANCE_WAKE); 214817770579SValentin Schneider #endif 2149b13095f0SLi Zefan 2150391e43daSPeter Zijlstra /* 2151391e43daSPeter Zijlstra * To aid in avoiding the subversion of "niceness" due to uneven distribution 2152391e43daSPeter Zijlstra * of tasks with abnormal "nice" values across CPUs the contribution that 2153391e43daSPeter Zijlstra * each task makes to its run queue's load is weighted according to its 2154391e43daSPeter Zijlstra * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a 2155391e43daSPeter Zijlstra * scaled version of the new time slice allocation that they receive on time 2156391e43daSPeter Zijlstra * slice expiry etc. 2157391e43daSPeter Zijlstra */ 2158391e43daSPeter Zijlstra 2159391e43daSPeter Zijlstra #define WEIGHT_IDLEPRIO 3 2160391e43daSPeter Zijlstra #define WMULT_IDLEPRIO 1431655765 2161391e43daSPeter Zijlstra 2162ed82b8a1SAndi Kleen extern const int sched_prio_to_weight[40]; 2163ed82b8a1SAndi Kleen extern const u32 sched_prio_to_wmult[40]; 2164391e43daSPeter Zijlstra 2165ff77e468SPeter Zijlstra /* 2166ff77e468SPeter Zijlstra * {de,en}queue flags: 2167ff77e468SPeter Zijlstra * 2168ff77e468SPeter Zijlstra * DEQUEUE_SLEEP - task is no longer runnable 2169ff77e468SPeter Zijlstra * ENQUEUE_WAKEUP - task just became runnable 2170ff77e468SPeter Zijlstra * 2171ff77e468SPeter Zijlstra * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks 2172ff77e468SPeter Zijlstra * are in a known state which allows modification. Such pairs 2173ff77e468SPeter Zijlstra * should preserve as much state as possible. 2174ff77e468SPeter Zijlstra * 2175ff77e468SPeter Zijlstra * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location 2176ff77e468SPeter Zijlstra * in the runqueue. 2177ff77e468SPeter Zijlstra * 2178ff77e468SPeter Zijlstra * ENQUEUE_HEAD - place at front of runqueue (tail if not specified) 2179ff77e468SPeter Zijlstra * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline) 218059efa0baSPeter Zijlstra * ENQUEUE_MIGRATED - the task was migrated during wakeup 2181ff77e468SPeter Zijlstra * 2182ff77e468SPeter Zijlstra */ 2183ff77e468SPeter Zijlstra 2184ff77e468SPeter Zijlstra #define DEQUEUE_SLEEP 0x01 218597fb7a0aSIngo Molnar #define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */ 218697fb7a0aSIngo Molnar #define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */ 218797fb7a0aSIngo Molnar #define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */ 2188ff77e468SPeter Zijlstra 21891de64443SPeter Zijlstra #define ENQUEUE_WAKEUP 0x01 2190ff77e468SPeter Zijlstra #define ENQUEUE_RESTORE 0x02 2191ff77e468SPeter Zijlstra #define ENQUEUE_MOVE 0x04 21920a67d1eeSPeter Zijlstra #define ENQUEUE_NOCLOCK 0x08 2193ff77e468SPeter Zijlstra 21940a67d1eeSPeter Zijlstra #define ENQUEUE_HEAD 0x10 21950a67d1eeSPeter Zijlstra #define ENQUEUE_REPLENISH 0x20 2196c82ba9faSLi Zefan #ifdef CONFIG_SMP 21970a67d1eeSPeter Zijlstra #define ENQUEUE_MIGRATED 0x40 2198c82ba9faSLi Zefan #else 219959efa0baSPeter Zijlstra #define ENQUEUE_MIGRATED 0x00 2200c82ba9faSLi Zefan #endif 2201c82ba9faSLi Zefan 220237e117c0SPeter Zijlstra #define RETRY_TASK ((void *)-1UL) 220337e117c0SPeter Zijlstra 2204713a2e21SWaiman Long struct affinity_context { 2205713a2e21SWaiman Long const struct cpumask *new_mask; 22068f9ea86fSWaiman Long struct cpumask *user_mask; 2207713a2e21SWaiman Long unsigned int flags; 2208713a2e21SWaiman Long }; 2209713a2e21SWaiman Long 2210c82ba9faSLi Zefan struct sched_class { 2211c82ba9faSLi Zefan 221269842cbaSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK 221369842cbaSPatrick Bellasi int uclamp_enabled; 221469842cbaSPatrick Bellasi #endif 221569842cbaSPatrick Bellasi 2216c82ba9faSLi Zefan void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags); 2217c82ba9faSLi Zefan void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags); 2218c82ba9faSLi Zefan void (*yield_task) (struct rq *rq); 22190900acf2SDietmar Eggemann bool (*yield_to_task)(struct rq *rq, struct task_struct *p); 2220c82ba9faSLi Zefan 2221c82ba9faSLi Zefan void (*check_preempt_curr)(struct rq *rq, struct task_struct *p, int flags); 2222c82ba9faSLi Zefan 222398c2f700SPeter Zijlstra struct task_struct *(*pick_next_task)(struct rq *rq); 222498c2f700SPeter Zijlstra 22256e2df058SPeter Zijlstra void (*put_prev_task)(struct rq *rq, struct task_struct *p); 2226a0e813f2SPeter Zijlstra void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first); 2227c82ba9faSLi Zefan 2228c82ba9faSLi Zefan #ifdef CONFIG_SMP 22296e2df058SPeter Zijlstra int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf); 22303aef1551SValentin Schneider int (*select_task_rq)(struct task_struct *p, int task_cpu, int flags); 223121f56ffeSPeter Zijlstra 223221f56ffeSPeter Zijlstra struct task_struct * (*pick_task)(struct rq *rq); 223321f56ffeSPeter Zijlstra 22341327237aSSrikar Dronamraju void (*migrate_task_rq)(struct task_struct *p, int new_cpu); 2235c82ba9faSLi Zefan 2236c82ba9faSLi Zefan void (*task_woken)(struct rq *this_rq, struct task_struct *task); 2237c82ba9faSLi Zefan 2238713a2e21SWaiman Long void (*set_cpus_allowed)(struct task_struct *p, struct affinity_context *ctx); 2239c82ba9faSLi Zefan 2240c82ba9faSLi Zefan void (*rq_online)(struct rq *rq); 2241c82ba9faSLi Zefan void (*rq_offline)(struct rq *rq); 2242a7c81556SPeter Zijlstra 2243a7c81556SPeter Zijlstra struct rq *(*find_lock_rq)(struct task_struct *p, struct rq *rq); 2244c82ba9faSLi Zefan #endif 2245c82ba9faSLi Zefan 2246c82ba9faSLi Zefan void (*task_tick)(struct rq *rq, struct task_struct *p, int queued); 2247c82ba9faSLi Zefan void (*task_fork)(struct task_struct *p); 2248e6c390f2SDario Faggioli void (*task_dead)(struct task_struct *p); 2249c82ba9faSLi Zefan 225067dfa1b7SKirill Tkhai /* 225167dfa1b7SKirill Tkhai * The switched_from() call is allowed to drop rq->lock, therefore we 22523b03706fSIngo Molnar * cannot assume the switched_from/switched_to pair is serialized by 225367dfa1b7SKirill Tkhai * rq->lock. They are however serialized by p->pi_lock. 225467dfa1b7SKirill Tkhai */ 2255c82ba9faSLi Zefan void (*switched_from)(struct rq *this_rq, struct task_struct *task); 2256c82ba9faSLi Zefan void (*switched_to) (struct rq *this_rq, struct task_struct *task); 2257c82ba9faSLi Zefan void (*prio_changed) (struct rq *this_rq, struct task_struct *task, 2258c82ba9faSLi Zefan int oldprio); 2259c82ba9faSLi Zefan 2260c82ba9faSLi Zefan unsigned int (*get_rr_interval)(struct rq *rq, 2261c82ba9faSLi Zefan struct task_struct *task); 2262c82ba9faSLi Zefan 22636e998916SStanislaw Gruszka void (*update_curr)(struct rq *rq); 22646e998916SStanislaw Gruszka 2265c82ba9faSLi Zefan #ifdef CONFIG_FAIR_GROUP_SCHED 226639c42611SChengming Zhou void (*task_change_group)(struct task_struct *p); 2267c82ba9faSLi Zefan #endif 2268530bfad1SHao Jia 2269530bfad1SHao Jia #ifdef CONFIG_SCHED_CORE 2270530bfad1SHao Jia int (*task_is_throttled)(struct task_struct *p, int cpu); 2271530bfad1SHao Jia #endif 227243c31ac0SPeter Zijlstra }; 2273391e43daSPeter Zijlstra 22743f1d2a31SPeter Zijlstra static inline void put_prev_task(struct rq *rq, struct task_struct *prev) 22753f1d2a31SPeter Zijlstra { 227610e7071bSPeter Zijlstra WARN_ON_ONCE(rq->curr != prev); 22776e2df058SPeter Zijlstra prev->sched_class->put_prev_task(rq, prev); 22783f1d2a31SPeter Zijlstra } 22793f1d2a31SPeter Zijlstra 228003b7fad1SPeter Zijlstra static inline void set_next_task(struct rq *rq, struct task_struct *next) 2281b2bf6c31SPeter Zijlstra { 2282a0e813f2SPeter Zijlstra next->sched_class->set_next_task(rq, next, false); 2283b2bf6c31SPeter Zijlstra } 2284b2bf6c31SPeter Zijlstra 228543c31ac0SPeter Zijlstra 228643c31ac0SPeter Zijlstra /* 228743c31ac0SPeter Zijlstra * Helper to define a sched_class instance; each one is placed in a separate 228843c31ac0SPeter Zijlstra * section which is ordered by the linker script: 228943c31ac0SPeter Zijlstra * 229043c31ac0SPeter Zijlstra * include/asm-generic/vmlinux.lds.h 229143c31ac0SPeter Zijlstra * 2292546a3feeSPeter Zijlstra * *CAREFUL* they are laid out in *REVERSE* order!!! 2293546a3feeSPeter Zijlstra * 229443c31ac0SPeter Zijlstra * Also enforce alignment on the instance, not the type, to guarantee layout. 229543c31ac0SPeter Zijlstra */ 229643c31ac0SPeter Zijlstra #define DEFINE_SCHED_CLASS(name) \ 229743c31ac0SPeter Zijlstra const struct sched_class name##_sched_class \ 229843c31ac0SPeter Zijlstra __aligned(__alignof__(struct sched_class)) \ 229943c31ac0SPeter Zijlstra __section("__" #name "_sched_class") 230043c31ac0SPeter Zijlstra 2301c3a340f7SSteven Rostedt (VMware) /* Defined in include/asm-generic/vmlinux.lds.h */ 2302546a3feeSPeter Zijlstra extern struct sched_class __sched_class_highest[]; 2303546a3feeSPeter Zijlstra extern struct sched_class __sched_class_lowest[]; 23046e2df058SPeter Zijlstra 23056e2df058SPeter Zijlstra #define for_class_range(class, _from, _to) \ 2306546a3feeSPeter Zijlstra for (class = (_from); class < (_to); class++) 23076e2df058SPeter Zijlstra 2308391e43daSPeter Zijlstra #define for_each_class(class) \ 2309546a3feeSPeter Zijlstra for_class_range(class, __sched_class_highest, __sched_class_lowest) 2310546a3feeSPeter Zijlstra 2311546a3feeSPeter Zijlstra #define sched_class_above(_a, _b) ((_a) < (_b)) 2312391e43daSPeter Zijlstra 2313391e43daSPeter Zijlstra extern const struct sched_class stop_sched_class; 2314aab03e05SDario Faggioli extern const struct sched_class dl_sched_class; 2315391e43daSPeter Zijlstra extern const struct sched_class rt_sched_class; 2316391e43daSPeter Zijlstra extern const struct sched_class fair_sched_class; 2317391e43daSPeter Zijlstra extern const struct sched_class idle_sched_class; 2318391e43daSPeter Zijlstra 23196e2df058SPeter Zijlstra static inline bool sched_stop_runnable(struct rq *rq) 23206e2df058SPeter Zijlstra { 23216e2df058SPeter Zijlstra return rq->stop && task_on_rq_queued(rq->stop); 23226e2df058SPeter Zijlstra } 23236e2df058SPeter Zijlstra 23246e2df058SPeter Zijlstra static inline bool sched_dl_runnable(struct rq *rq) 23256e2df058SPeter Zijlstra { 23266e2df058SPeter Zijlstra return rq->dl.dl_nr_running > 0; 23276e2df058SPeter Zijlstra } 23286e2df058SPeter Zijlstra 23296e2df058SPeter Zijlstra static inline bool sched_rt_runnable(struct rq *rq) 23306e2df058SPeter Zijlstra { 23316e2df058SPeter Zijlstra return rq->rt.rt_queued > 0; 23326e2df058SPeter Zijlstra } 23336e2df058SPeter Zijlstra 23346e2df058SPeter Zijlstra static inline bool sched_fair_runnable(struct rq *rq) 23356e2df058SPeter Zijlstra { 23366e2df058SPeter Zijlstra return rq->cfs.nr_running > 0; 23376e2df058SPeter Zijlstra } 2338391e43daSPeter Zijlstra 23395d7d6056SPeter Zijlstra extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf); 234098c2f700SPeter Zijlstra extern struct task_struct *pick_next_task_idle(struct rq *rq); 23415d7d6056SPeter Zijlstra 2342af449901SPeter Zijlstra #define SCA_CHECK 0x01 2343af449901SPeter Zijlstra #define SCA_MIGRATE_DISABLE 0x02 2344af449901SPeter Zijlstra #define SCA_MIGRATE_ENABLE 0x04 234507ec77a1SWill Deacon #define SCA_USER 0x08 2346af449901SPeter Zijlstra 2347391e43daSPeter Zijlstra #ifdef CONFIG_SMP 2348391e43daSPeter Zijlstra 234963b2ca30SNicolas Pitre extern void update_group_capacity(struct sched_domain *sd, int cpu); 2350b719203bSLi Zefan 23517caff66fSDaniel Lezcano extern void trigger_load_balance(struct rq *rq); 2352391e43daSPeter Zijlstra 2353713a2e21SWaiman Long extern void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx); 2354c5b28038SPeter Zijlstra 2355a7c81556SPeter Zijlstra static inline struct task_struct *get_push_task(struct rq *rq) 2356a7c81556SPeter Zijlstra { 2357a7c81556SPeter Zijlstra struct task_struct *p = rq->curr; 2358a7c81556SPeter Zijlstra 23595cb9eaa3SPeter Zijlstra lockdep_assert_rq_held(rq); 2360a7c81556SPeter Zijlstra 2361a7c81556SPeter Zijlstra if (rq->push_busy) 2362a7c81556SPeter Zijlstra return NULL; 2363a7c81556SPeter Zijlstra 2364a7c81556SPeter Zijlstra if (p->nr_cpus_allowed == 1) 2365a7c81556SPeter Zijlstra return NULL; 2366a7c81556SPeter Zijlstra 2367e681dcbaSSebastian Andrzej Siewior if (p->migration_disabled) 2368e681dcbaSSebastian Andrzej Siewior return NULL; 2369e681dcbaSSebastian Andrzej Siewior 2370a7c81556SPeter Zijlstra rq->push_busy = true; 2371a7c81556SPeter Zijlstra return get_task_struct(p); 2372a7c81556SPeter Zijlstra } 2373a7c81556SPeter Zijlstra 2374a7c81556SPeter Zijlstra extern int push_cpu_stop(void *arg); 2375dc877341SPeter Zijlstra 2376391e43daSPeter Zijlstra #endif 2377391e43daSPeter Zijlstra 2378442bf3aaSDaniel Lezcano #ifdef CONFIG_CPU_IDLE 2379442bf3aaSDaniel Lezcano static inline void idle_set_state(struct rq *rq, 2380442bf3aaSDaniel Lezcano struct cpuidle_state *idle_state) 2381442bf3aaSDaniel Lezcano { 2382442bf3aaSDaniel Lezcano rq->idle_state = idle_state; 2383442bf3aaSDaniel Lezcano } 2384442bf3aaSDaniel Lezcano 2385442bf3aaSDaniel Lezcano static inline struct cpuidle_state *idle_get_state(struct rq *rq) 2386442bf3aaSDaniel Lezcano { 23879148a3a1SPeter Zijlstra SCHED_WARN_ON(!rcu_read_lock_held()); 238897fb7a0aSIngo Molnar 2389442bf3aaSDaniel Lezcano return rq->idle_state; 2390442bf3aaSDaniel Lezcano } 2391442bf3aaSDaniel Lezcano #else 2392442bf3aaSDaniel Lezcano static inline void idle_set_state(struct rq *rq, 2393442bf3aaSDaniel Lezcano struct cpuidle_state *idle_state) 2394442bf3aaSDaniel Lezcano { 2395442bf3aaSDaniel Lezcano } 2396442bf3aaSDaniel Lezcano 2397442bf3aaSDaniel Lezcano static inline struct cpuidle_state *idle_get_state(struct rq *rq) 2398442bf3aaSDaniel Lezcano { 2399442bf3aaSDaniel Lezcano return NULL; 2400442bf3aaSDaniel Lezcano } 2401442bf3aaSDaniel Lezcano #endif 2402442bf3aaSDaniel Lezcano 24038663effbSSteven Rostedt (VMware) extern void schedule_idle(void); 24048663effbSSteven Rostedt (VMware) 2405391e43daSPeter Zijlstra extern void sysrq_sched_debug_show(void); 2406391e43daSPeter Zijlstra extern void sched_init_granularity(void); 2407391e43daSPeter Zijlstra extern void update_max_interval(void); 24081baca4ceSJuri Lelli 24091baca4ceSJuri Lelli extern void init_sched_dl_class(void); 2410391e43daSPeter Zijlstra extern void init_sched_rt_class(void); 2411391e43daSPeter Zijlstra extern void init_sched_fair_class(void); 2412391e43daSPeter Zijlstra 24139059393eSVincent Guittot extern void reweight_task(struct task_struct *p, int prio); 24149059393eSVincent Guittot 24158875125eSKirill Tkhai extern void resched_curr(struct rq *rq); 2416391e43daSPeter Zijlstra extern void resched_cpu(int cpu); 2417391e43daSPeter Zijlstra 2418391e43daSPeter Zijlstra extern struct rt_bandwidth def_rt_bandwidth; 2419391e43daSPeter Zijlstra extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime); 2420d664e399SThomas Gleixner extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq); 2421391e43daSPeter Zijlstra 2422aab03e05SDario Faggioli extern void init_dl_task_timer(struct sched_dl_entity *dl_se); 2423209a0cbdSLuca Abeni extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se); 2424aab03e05SDario Faggioli 2425c52f14d3SLuca Abeni #define BW_SHIFT 20 2426c52f14d3SLuca Abeni #define BW_UNIT (1 << BW_SHIFT) 24274da3abceSLuca Abeni #define RATIO_SHIFT 8 2428d505b8afSHuaixin Chang #define MAX_BW_BITS (64 - BW_SHIFT) 2429d505b8afSHuaixin Chang #define MAX_BW ((1ULL << MAX_BW_BITS) - 1) 2430332ac17eSDario Faggioli unsigned long to_ratio(u64 period, u64 runtime); 2431332ac17eSDario Faggioli 2432540247fbSYuyang Du extern void init_entity_runnable_average(struct sched_entity *se); 2433d0fe0b9cSDietmar Eggemann extern void post_init_entity_util_avg(struct task_struct *p); 2434a75cdaa9SAlex Shi 243576d92ac3SFrederic Weisbecker #ifdef CONFIG_NO_HZ_FULL 243676d92ac3SFrederic Weisbecker extern bool sched_can_stop_tick(struct rq *rq); 2437d84b3131SFrederic Weisbecker extern int __init sched_tick_offload_init(void); 243876d92ac3SFrederic Weisbecker 243976d92ac3SFrederic Weisbecker /* 244076d92ac3SFrederic Weisbecker * Tick may be needed by tasks in the runqueue depending on their policy and 244176d92ac3SFrederic Weisbecker * requirements. If tick is needed, lets send the target an IPI to kick it out of 244276d92ac3SFrederic Weisbecker * nohz mode if necessary. 244376d92ac3SFrederic Weisbecker */ 244476d92ac3SFrederic Weisbecker static inline void sched_update_tick_dependency(struct rq *rq) 244576d92ac3SFrederic Weisbecker { 244621a6ee14SMiaohe Lin int cpu = cpu_of(rq); 244776d92ac3SFrederic Weisbecker 244876d92ac3SFrederic Weisbecker if (!tick_nohz_full_cpu(cpu)) 244976d92ac3SFrederic Weisbecker return; 245076d92ac3SFrederic Weisbecker 245176d92ac3SFrederic Weisbecker if (sched_can_stop_tick(rq)) 245276d92ac3SFrederic Weisbecker tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED); 245376d92ac3SFrederic Weisbecker else 245476d92ac3SFrederic Weisbecker tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); 245576d92ac3SFrederic Weisbecker } 245676d92ac3SFrederic Weisbecker #else 2457d84b3131SFrederic Weisbecker static inline int sched_tick_offload_init(void) { return 0; } 245876d92ac3SFrederic Weisbecker static inline void sched_update_tick_dependency(struct rq *rq) { } 245976d92ac3SFrederic Weisbecker #endif 246076d92ac3SFrederic Weisbecker 246172465447SKirill Tkhai static inline void add_nr_running(struct rq *rq, unsigned count) 2462391e43daSPeter Zijlstra { 246372465447SKirill Tkhai unsigned prev_nr = rq->nr_running; 246472465447SKirill Tkhai 246572465447SKirill Tkhai rq->nr_running = prev_nr + count; 24669d246053SPhil Auld if (trace_sched_update_nr_running_tp_enabled()) { 24679d246053SPhil Auld call_trace_sched_update_nr_running(rq, count); 24689d246053SPhil Auld } 24699f3660c2SFrederic Weisbecker 24704486edd1STim Chen #ifdef CONFIG_SMP 24713e184501SViresh Kumar if (prev_nr < 2 && rq->nr_running >= 2) { 2472e90c8fe1SValentin Schneider if (!READ_ONCE(rq->rd->overload)) 2473e90c8fe1SValentin Schneider WRITE_ONCE(rq->rd->overload, 1); 247476d92ac3SFrederic Weisbecker } 24753e184501SViresh Kumar #endif 24764486edd1STim Chen 247776d92ac3SFrederic Weisbecker sched_update_tick_dependency(rq); 24784486edd1STim Chen } 2479391e43daSPeter Zijlstra 248072465447SKirill Tkhai static inline void sub_nr_running(struct rq *rq, unsigned count) 2481391e43daSPeter Zijlstra { 248272465447SKirill Tkhai rq->nr_running -= count; 24839d246053SPhil Auld if (trace_sched_update_nr_running_tp_enabled()) { 2484a1bd0685SPhil Auld call_trace_sched_update_nr_running(rq, -count); 24859d246053SPhil Auld } 24869d246053SPhil Auld 248776d92ac3SFrederic Weisbecker /* Check if we still need preemption */ 248876d92ac3SFrederic Weisbecker sched_update_tick_dependency(rq); 2489391e43daSPeter Zijlstra } 2490391e43daSPeter Zijlstra 2491391e43daSPeter Zijlstra extern void activate_task(struct rq *rq, struct task_struct *p, int flags); 2492391e43daSPeter Zijlstra extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags); 2493391e43daSPeter Zijlstra 2494391e43daSPeter Zijlstra extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); 2495391e43daSPeter Zijlstra 2496c59862f8SVincent Guittot #ifdef CONFIG_PREEMPT_RT 2497c59862f8SVincent Guittot #define SCHED_NR_MIGRATE_BREAK 8 2498c59862f8SVincent Guittot #else 2499c59862f8SVincent Guittot #define SCHED_NR_MIGRATE_BREAK 32 2500c59862f8SVincent Guittot #endif 2501c59862f8SVincent Guittot 2502391e43daSPeter Zijlstra extern const_debug unsigned int sysctl_sched_nr_migrate; 2503391e43daSPeter Zijlstra extern const_debug unsigned int sysctl_sched_migration_cost; 2504391e43daSPeter Zijlstra 250518765447SHailong Liu #ifdef CONFIG_SCHED_DEBUG 250618765447SHailong Liu extern unsigned int sysctl_sched_latency; 250718765447SHailong Liu extern unsigned int sysctl_sched_min_granularity; 250851ce83edSJosh Don extern unsigned int sysctl_sched_idle_min_granularity; 250918765447SHailong Liu extern unsigned int sysctl_sched_wakeup_granularity; 251018765447SHailong Liu extern int sysctl_resched_latency_warn_ms; 251118765447SHailong Liu extern int sysctl_resched_latency_warn_once; 251218765447SHailong Liu 251318765447SHailong Liu extern unsigned int sysctl_sched_tunable_scaling; 251418765447SHailong Liu 251518765447SHailong Liu extern unsigned int sysctl_numa_balancing_scan_delay; 251618765447SHailong Liu extern unsigned int sysctl_numa_balancing_scan_period_min; 251718765447SHailong Liu extern unsigned int sysctl_numa_balancing_scan_period_max; 251818765447SHailong Liu extern unsigned int sysctl_numa_balancing_scan_size; 251933024536SHuang Ying extern unsigned int sysctl_numa_balancing_hot_threshold; 252018765447SHailong Liu #endif 252118765447SHailong Liu 2522391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_HRTICK 2523391e43daSPeter Zijlstra 2524391e43daSPeter Zijlstra /* 2525391e43daSPeter Zijlstra * Use hrtick when: 2526391e43daSPeter Zijlstra * - enabled by features 2527391e43daSPeter Zijlstra * - hrtimer is actually high res 2528391e43daSPeter Zijlstra */ 2529391e43daSPeter Zijlstra static inline int hrtick_enabled(struct rq *rq) 2530391e43daSPeter Zijlstra { 2531391e43daSPeter Zijlstra if (!cpu_active(cpu_of(rq))) 2532391e43daSPeter Zijlstra return 0; 2533391e43daSPeter Zijlstra return hrtimer_is_hres_active(&rq->hrtick_timer); 2534391e43daSPeter Zijlstra } 2535391e43daSPeter Zijlstra 2536e0ee463cSJuri Lelli static inline int hrtick_enabled_fair(struct rq *rq) 2537e0ee463cSJuri Lelli { 2538e0ee463cSJuri Lelli if (!sched_feat(HRTICK)) 2539e0ee463cSJuri Lelli return 0; 2540e0ee463cSJuri Lelli return hrtick_enabled(rq); 2541e0ee463cSJuri Lelli } 2542e0ee463cSJuri Lelli 2543e0ee463cSJuri Lelli static inline int hrtick_enabled_dl(struct rq *rq) 2544e0ee463cSJuri Lelli { 2545e0ee463cSJuri Lelli if (!sched_feat(HRTICK_DL)) 2546e0ee463cSJuri Lelli return 0; 2547e0ee463cSJuri Lelli return hrtick_enabled(rq); 2548e0ee463cSJuri Lelli } 2549e0ee463cSJuri Lelli 2550391e43daSPeter Zijlstra void hrtick_start(struct rq *rq, u64 delay); 2551391e43daSPeter Zijlstra 2552b39e66eaSMike Galbraith #else 2553b39e66eaSMike Galbraith 2554e0ee463cSJuri Lelli static inline int hrtick_enabled_fair(struct rq *rq) 2555e0ee463cSJuri Lelli { 2556e0ee463cSJuri Lelli return 0; 2557e0ee463cSJuri Lelli } 2558e0ee463cSJuri Lelli 2559e0ee463cSJuri Lelli static inline int hrtick_enabled_dl(struct rq *rq) 2560e0ee463cSJuri Lelli { 2561e0ee463cSJuri Lelli return 0; 2562e0ee463cSJuri Lelli } 2563e0ee463cSJuri Lelli 2564b39e66eaSMike Galbraith static inline int hrtick_enabled(struct rq *rq) 2565b39e66eaSMike Galbraith { 2566b39e66eaSMike Galbraith return 0; 2567b39e66eaSMike Galbraith } 2568b39e66eaSMike Galbraith 2569391e43daSPeter Zijlstra #endif /* CONFIG_SCHED_HRTICK */ 2570391e43daSPeter Zijlstra 25711567c3e3SGiovanni Gherdovich #ifndef arch_scale_freq_tick 25721567c3e3SGiovanni Gherdovich static __always_inline 25731567c3e3SGiovanni Gherdovich void arch_scale_freq_tick(void) 25741567c3e3SGiovanni Gherdovich { 25751567c3e3SGiovanni Gherdovich } 25761567c3e3SGiovanni Gherdovich #endif 25771567c3e3SGiovanni Gherdovich 2578dfbca41fSPeter Zijlstra #ifndef arch_scale_freq_capacity 2579f4470cdfSValentin Schneider /** 2580f4470cdfSValentin Schneider * arch_scale_freq_capacity - get the frequency scale factor of a given CPU. 2581f4470cdfSValentin Schneider * @cpu: the CPU in question. 2582f4470cdfSValentin Schneider * 2583f4470cdfSValentin Schneider * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e. 2584f4470cdfSValentin Schneider * 2585f4470cdfSValentin Schneider * f_curr 2586f4470cdfSValentin Schneider * ------ * SCHED_CAPACITY_SCALE 2587f4470cdfSValentin Schneider * f_max 2588f4470cdfSValentin Schneider */ 2589dfbca41fSPeter Zijlstra static __always_inline 25907673c8a4SJuri Lelli unsigned long arch_scale_freq_capacity(int cpu) 2591dfbca41fSPeter Zijlstra { 2592dfbca41fSPeter Zijlstra return SCHED_CAPACITY_SCALE; 2593dfbca41fSPeter Zijlstra } 2594dfbca41fSPeter Zijlstra #endif 2595b5b4860dSVincent Guittot 25962679a837SHao Jia #ifdef CONFIG_SCHED_DEBUG 25972679a837SHao Jia /* 25982679a837SHao Jia * In double_lock_balance()/double_rq_lock(), we use raw_spin_rq_lock() to 25992679a837SHao Jia * acquire rq lock instead of rq_lock(). So at the end of these two functions 26002679a837SHao Jia * we need to call double_rq_clock_clear_update() to clear RQCF_UPDATED of 26012679a837SHao Jia * rq->clock_update_flags to avoid the WARN_DOUBLE_CLOCK warning. 26022679a837SHao Jia */ 26032679a837SHao Jia static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2) 26042679a837SHao Jia { 26052679a837SHao Jia rq1->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); 26062679a837SHao Jia /* rq1 == rq2 for !CONFIG_SMP, so just clear RQCF_UPDATED once. */ 26072679a837SHao Jia #ifdef CONFIG_SMP 26082679a837SHao Jia rq2->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); 26092679a837SHao Jia #endif 26102679a837SHao Jia } 26112679a837SHao Jia #else 26122679a837SHao Jia static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2) {} 26132679a837SHao Jia #endif 2614391e43daSPeter Zijlstra 2615d66f1b06SPeter Zijlstra #ifdef CONFIG_SMP 2616d66f1b06SPeter Zijlstra 2617d66f1b06SPeter Zijlstra static inline bool rq_order_less(struct rq *rq1, struct rq *rq2) 2618d66f1b06SPeter Zijlstra { 26199edeaea1SPeter Zijlstra #ifdef CONFIG_SCHED_CORE 26209edeaea1SPeter Zijlstra /* 26219edeaea1SPeter Zijlstra * In order to not have {0,2},{1,3} turn into into an AB-BA, 26229edeaea1SPeter Zijlstra * order by core-id first and cpu-id second. 26239edeaea1SPeter Zijlstra * 26249edeaea1SPeter Zijlstra * Notably: 26259edeaea1SPeter Zijlstra * 26269edeaea1SPeter Zijlstra * double_rq_lock(0,3); will take core-0, core-1 lock 26279edeaea1SPeter Zijlstra * double_rq_lock(1,2); will take core-1, core-0 lock 26289edeaea1SPeter Zijlstra * 26299edeaea1SPeter Zijlstra * when only cpu-id is considered. 26309edeaea1SPeter Zijlstra */ 26319edeaea1SPeter Zijlstra if (rq1->core->cpu < rq2->core->cpu) 26329edeaea1SPeter Zijlstra return true; 26339edeaea1SPeter Zijlstra if (rq1->core->cpu > rq2->core->cpu) 26349edeaea1SPeter Zijlstra return false; 26359edeaea1SPeter Zijlstra 26369edeaea1SPeter Zijlstra /* 26379edeaea1SPeter Zijlstra * __sched_core_flip() relies on SMT having cpu-id lock order. 26389edeaea1SPeter Zijlstra */ 26399edeaea1SPeter Zijlstra #endif 2640d66f1b06SPeter Zijlstra return rq1->cpu < rq2->cpu; 2641d66f1b06SPeter Zijlstra } 2642d66f1b06SPeter Zijlstra 2643d66f1b06SPeter Zijlstra extern void double_rq_lock(struct rq *rq1, struct rq *rq2); 2644d66f1b06SPeter Zijlstra 2645d66f1b06SPeter Zijlstra #ifdef CONFIG_PREEMPTION 2646391e43daSPeter Zijlstra 2647391e43daSPeter Zijlstra /* 2648391e43daSPeter Zijlstra * fair double_lock_balance: Safely acquires both rq->locks in a fair 2649391e43daSPeter Zijlstra * way at the expense of forcing extra atomic operations in all 2650391e43daSPeter Zijlstra * invocations. This assures that the double_lock is acquired using the 2651391e43daSPeter Zijlstra * same underlying policy as the spinlock_t on this architecture, which 2652391e43daSPeter Zijlstra * reduces latency compared to the unfair variant below. However, it 2653391e43daSPeter Zijlstra * also adds more overhead and therefore may reduce throughput. 2654391e43daSPeter Zijlstra */ 2655391e43daSPeter Zijlstra static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) 2656391e43daSPeter Zijlstra __releases(this_rq->lock) 2657391e43daSPeter Zijlstra __acquires(busiest->lock) 2658391e43daSPeter Zijlstra __acquires(this_rq->lock) 2659391e43daSPeter Zijlstra { 26605cb9eaa3SPeter Zijlstra raw_spin_rq_unlock(this_rq); 2661391e43daSPeter Zijlstra double_rq_lock(this_rq, busiest); 2662391e43daSPeter Zijlstra 2663391e43daSPeter Zijlstra return 1; 2664391e43daSPeter Zijlstra } 2665391e43daSPeter Zijlstra 2666391e43daSPeter Zijlstra #else 2667391e43daSPeter Zijlstra /* 2668391e43daSPeter Zijlstra * Unfair double_lock_balance: Optimizes throughput at the expense of 2669391e43daSPeter Zijlstra * latency by eliminating extra atomic operations when the locks are 267097fb7a0aSIngo Molnar * already in proper order on entry. This favors lower CPU-ids and will 267197fb7a0aSIngo Molnar * grant the double lock to lower CPUs over higher ids under contention, 2672391e43daSPeter Zijlstra * regardless of entry order into the function. 2673391e43daSPeter Zijlstra */ 2674391e43daSPeter Zijlstra static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) 2675391e43daSPeter Zijlstra __releases(this_rq->lock) 2676391e43daSPeter Zijlstra __acquires(busiest->lock) 2677391e43daSPeter Zijlstra __acquires(this_rq->lock) 2678391e43daSPeter Zijlstra { 26792679a837SHao Jia if (__rq_lockp(this_rq) == __rq_lockp(busiest) || 26802679a837SHao Jia likely(raw_spin_rq_trylock(busiest))) { 26812679a837SHao Jia double_rq_clock_clear_update(this_rq, busiest); 26825cb9eaa3SPeter Zijlstra return 0; 26832679a837SHao Jia } 26845cb9eaa3SPeter Zijlstra 2685d66f1b06SPeter Zijlstra if (rq_order_less(this_rq, busiest)) { 26865cb9eaa3SPeter Zijlstra raw_spin_rq_lock_nested(busiest, SINGLE_DEPTH_NESTING); 26872679a837SHao Jia double_rq_clock_clear_update(this_rq, busiest); 26885cb9eaa3SPeter Zijlstra return 0; 2689391e43daSPeter Zijlstra } 26905cb9eaa3SPeter Zijlstra 26915cb9eaa3SPeter Zijlstra raw_spin_rq_unlock(this_rq); 2692d66f1b06SPeter Zijlstra double_rq_lock(this_rq, busiest); 26935cb9eaa3SPeter Zijlstra 26945cb9eaa3SPeter Zijlstra return 1; 2695391e43daSPeter Zijlstra } 2696391e43daSPeter Zijlstra 2697c1a280b6SThomas Gleixner #endif /* CONFIG_PREEMPTION */ 2698391e43daSPeter Zijlstra 2699391e43daSPeter Zijlstra /* 2700391e43daSPeter Zijlstra * double_lock_balance - lock the busiest runqueue, this_rq is locked already. 2701391e43daSPeter Zijlstra */ 2702391e43daSPeter Zijlstra static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest) 2703391e43daSPeter Zijlstra { 27045cb9eaa3SPeter Zijlstra lockdep_assert_irqs_disabled(); 2705391e43daSPeter Zijlstra 2706391e43daSPeter Zijlstra return _double_lock_balance(this_rq, busiest); 2707391e43daSPeter Zijlstra } 2708391e43daSPeter Zijlstra 2709391e43daSPeter Zijlstra static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) 2710391e43daSPeter Zijlstra __releases(busiest->lock) 2711391e43daSPeter Zijlstra { 27129ef7e7e3SPeter Zijlstra if (__rq_lockp(this_rq) != __rq_lockp(busiest)) 27135cb9eaa3SPeter Zijlstra raw_spin_rq_unlock(busiest); 27149ef7e7e3SPeter Zijlstra lock_set_subclass(&__rq_lockp(this_rq)->dep_map, 0, _RET_IP_); 2715391e43daSPeter Zijlstra } 2716391e43daSPeter Zijlstra 271774602315SPeter Zijlstra static inline void double_lock(spinlock_t *l1, spinlock_t *l2) 271874602315SPeter Zijlstra { 271974602315SPeter Zijlstra if (l1 > l2) 272074602315SPeter Zijlstra swap(l1, l2); 272174602315SPeter Zijlstra 272274602315SPeter Zijlstra spin_lock(l1); 272374602315SPeter Zijlstra spin_lock_nested(l2, SINGLE_DEPTH_NESTING); 272474602315SPeter Zijlstra } 272574602315SPeter Zijlstra 272660e69eedSMike Galbraith static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2) 272760e69eedSMike Galbraith { 272860e69eedSMike Galbraith if (l1 > l2) 272960e69eedSMike Galbraith swap(l1, l2); 273060e69eedSMike Galbraith 273160e69eedSMike Galbraith spin_lock_irq(l1); 273260e69eedSMike Galbraith spin_lock_nested(l2, SINGLE_DEPTH_NESTING); 273360e69eedSMike Galbraith } 273460e69eedSMike Galbraith 273574602315SPeter Zijlstra static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2) 273674602315SPeter Zijlstra { 273774602315SPeter Zijlstra if (l1 > l2) 273874602315SPeter Zijlstra swap(l1, l2); 273974602315SPeter Zijlstra 274074602315SPeter Zijlstra raw_spin_lock(l1); 274174602315SPeter Zijlstra raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING); 274274602315SPeter Zijlstra } 274374602315SPeter Zijlstra 2744391e43daSPeter Zijlstra /* 2745391e43daSPeter Zijlstra * double_rq_unlock - safely unlock two runqueues 2746391e43daSPeter Zijlstra * 2747391e43daSPeter Zijlstra * Note this does not restore interrupts like task_rq_unlock, 2748391e43daSPeter Zijlstra * you need to do so manually after calling. 2749391e43daSPeter Zijlstra */ 2750391e43daSPeter Zijlstra static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) 2751391e43daSPeter Zijlstra __releases(rq1->lock) 2752391e43daSPeter Zijlstra __releases(rq2->lock) 2753391e43daSPeter Zijlstra { 27549ef7e7e3SPeter Zijlstra if (__rq_lockp(rq1) != __rq_lockp(rq2)) 27555cb9eaa3SPeter Zijlstra raw_spin_rq_unlock(rq2); 2756391e43daSPeter Zijlstra else 2757391e43daSPeter Zijlstra __release(rq2->lock); 2758d66f1b06SPeter Zijlstra raw_spin_rq_unlock(rq1); 2759391e43daSPeter Zijlstra } 2760391e43daSPeter Zijlstra 2761f2cb1360SIngo Molnar extern void set_rq_online (struct rq *rq); 2762f2cb1360SIngo Molnar extern void set_rq_offline(struct rq *rq); 2763f2cb1360SIngo Molnar extern bool sched_smp_initialized; 2764f2cb1360SIngo Molnar 2765391e43daSPeter Zijlstra #else /* CONFIG_SMP */ 2766391e43daSPeter Zijlstra 2767391e43daSPeter Zijlstra /* 2768391e43daSPeter Zijlstra * double_rq_lock - safely lock two runqueues 2769391e43daSPeter Zijlstra * 2770391e43daSPeter Zijlstra * Note this does not disable interrupts like task_rq_lock, 2771391e43daSPeter Zijlstra * you need to do so manually before calling. 2772391e43daSPeter Zijlstra */ 2773391e43daSPeter Zijlstra static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) 2774391e43daSPeter Zijlstra __acquires(rq1->lock) 2775391e43daSPeter Zijlstra __acquires(rq2->lock) 2776391e43daSPeter Zijlstra { 277709348d75SIngo Molnar WARN_ON_ONCE(!irqs_disabled()); 277809348d75SIngo Molnar WARN_ON_ONCE(rq1 != rq2); 27795cb9eaa3SPeter Zijlstra raw_spin_rq_lock(rq1); 2780391e43daSPeter Zijlstra __acquire(rq2->lock); /* Fake it out ;) */ 27812679a837SHao Jia double_rq_clock_clear_update(rq1, rq2); 2782391e43daSPeter Zijlstra } 2783391e43daSPeter Zijlstra 2784391e43daSPeter Zijlstra /* 2785391e43daSPeter Zijlstra * double_rq_unlock - safely unlock two runqueues 2786391e43daSPeter Zijlstra * 2787391e43daSPeter Zijlstra * Note this does not restore interrupts like task_rq_unlock, 2788391e43daSPeter Zijlstra * you need to do so manually after calling. 2789391e43daSPeter Zijlstra */ 2790391e43daSPeter Zijlstra static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) 2791391e43daSPeter Zijlstra __releases(rq1->lock) 2792391e43daSPeter Zijlstra __releases(rq2->lock) 2793391e43daSPeter Zijlstra { 279409348d75SIngo Molnar WARN_ON_ONCE(rq1 != rq2); 27955cb9eaa3SPeter Zijlstra raw_spin_rq_unlock(rq1); 2796391e43daSPeter Zijlstra __release(rq2->lock); 2797391e43daSPeter Zijlstra } 2798391e43daSPeter Zijlstra 2799391e43daSPeter Zijlstra #endif 2800391e43daSPeter Zijlstra 2801391e43daSPeter Zijlstra extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq); 2802391e43daSPeter Zijlstra extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq); 28036b55c965SSrikar Dronamraju 28046b55c965SSrikar Dronamraju #ifdef CONFIG_SCHED_DEBUG 28059406415fSPeter Zijlstra extern bool sched_debug_verbose; 28069469eb01SPeter Zijlstra 2807391e43daSPeter Zijlstra extern void print_cfs_stats(struct seq_file *m, int cpu); 2808391e43daSPeter Zijlstra extern void print_rt_stats(struct seq_file *m, int cpu); 2809acb32132SWanpeng Li extern void print_dl_stats(struct seq_file *m, int cpu); 2810f6a34630SMathieu Malaterre extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq); 2811f6a34630SMathieu Malaterre extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq); 2812f6a34630SMathieu Malaterre extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq); 2813c006fac5SPaul Turner 2814c006fac5SPaul Turner extern void resched_latency_warn(int cpu, u64 latency); 2815397f2378SSrikar Dronamraju #ifdef CONFIG_NUMA_BALANCING 2816397f2378SSrikar Dronamraju extern void 2817397f2378SSrikar Dronamraju show_numa_stats(struct task_struct *p, struct seq_file *m); 2818397f2378SSrikar Dronamraju extern void 2819397f2378SSrikar Dronamraju print_numa_stats(struct seq_file *m, int node, unsigned long tsf, 2820397f2378SSrikar Dronamraju unsigned long tpf, unsigned long gsf, unsigned long gpf); 2821397f2378SSrikar Dronamraju #endif /* CONFIG_NUMA_BALANCING */ 2822c006fac5SPaul Turner #else 2823c006fac5SPaul Turner static inline void resched_latency_warn(int cpu, u64 latency) {} 2824397f2378SSrikar Dronamraju #endif /* CONFIG_SCHED_DEBUG */ 2825391e43daSPeter Zijlstra 2826391e43daSPeter Zijlstra extern void init_cfs_rq(struct cfs_rq *cfs_rq); 282707c54f7aSAbel Vesa extern void init_rt_rq(struct rt_rq *rt_rq); 282807c54f7aSAbel Vesa extern void init_dl_rq(struct dl_rq *dl_rq); 2829391e43daSPeter Zijlstra 28301ee14e6cSBen Segall extern void cfs_bandwidth_usage_inc(void); 28311ee14e6cSBen Segall extern void cfs_bandwidth_usage_dec(void); 28321c792db7SSuresh Siddha 28333451d024SFrederic Weisbecker #ifdef CONFIG_NO_HZ_COMMON 283400357f5eSPeter Zijlstra #define NOHZ_BALANCE_KICK_BIT 0 283500357f5eSPeter Zijlstra #define NOHZ_STATS_KICK_BIT 1 2836c6f88654SVincent Guittot #define NOHZ_NEWILB_KICK_BIT 2 2837efd984c4SValentin Schneider #define NOHZ_NEXT_KICK_BIT 3 2838a22e47a4SPeter Zijlstra 2839efd984c4SValentin Schneider /* Run rebalance_domains() */ 2840a22e47a4SPeter Zijlstra #define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT) 2841efd984c4SValentin Schneider /* Update blocked load */ 2842b7031a02SPeter Zijlstra #define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT) 2843efd984c4SValentin Schneider /* Update blocked load when entering idle */ 2844c6f88654SVincent Guittot #define NOHZ_NEWILB_KICK BIT(NOHZ_NEWILB_KICK_BIT) 2845efd984c4SValentin Schneider /* Update nohz.next_balance */ 2846efd984c4SValentin Schneider #define NOHZ_NEXT_KICK BIT(NOHZ_NEXT_KICK_BIT) 2847b7031a02SPeter Zijlstra 2848efd984c4SValentin Schneider #define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK | NOHZ_NEXT_KICK) 28491c792db7SSuresh Siddha 28501c792db7SSuresh Siddha #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags) 285120a5c8ccSThomas Gleixner 285200357f5eSPeter Zijlstra extern void nohz_balance_exit_idle(struct rq *rq); 285320a5c8ccSThomas Gleixner #else 285400357f5eSPeter Zijlstra static inline void nohz_balance_exit_idle(struct rq *rq) { } 28551c792db7SSuresh Siddha #endif 285673fbec60SFrederic Weisbecker 2857c6f88654SVincent Guittot #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON) 2858c6f88654SVincent Guittot extern void nohz_run_idle_balance(int cpu); 2859c6f88654SVincent Guittot #else 2860c6f88654SVincent Guittot static inline void nohz_run_idle_balance(int cpu) { } 2861c6f88654SVincent Guittot #endif 2862daec5798SLuca Abeni 286373fbec60SFrederic Weisbecker #ifdef CONFIG_IRQ_TIME_ACCOUNTING 286419d23dbfSFrederic Weisbecker struct irqtime { 286525e2d8c1SFrederic Weisbecker u64 total; 2866a499a5a1SFrederic Weisbecker u64 tick_delta; 286719d23dbfSFrederic Weisbecker u64 irq_start_time; 286819d23dbfSFrederic Weisbecker struct u64_stats_sync sync; 286919d23dbfSFrederic Weisbecker }; 287073fbec60SFrederic Weisbecker 287119d23dbfSFrederic Weisbecker DECLARE_PER_CPU(struct irqtime, cpu_irqtime); 287273fbec60SFrederic Weisbecker 287325e2d8c1SFrederic Weisbecker /* 287425e2d8c1SFrederic Weisbecker * Returns the irqtime minus the softirq time computed by ksoftirqd. 28753b03706fSIngo Molnar * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime 287625e2d8c1SFrederic Weisbecker * and never move forward. 287725e2d8c1SFrederic Weisbecker */ 287873fbec60SFrederic Weisbecker static inline u64 irq_time_read(int cpu) 287973fbec60SFrederic Weisbecker { 288019d23dbfSFrederic Weisbecker struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu); 288119d23dbfSFrederic Weisbecker unsigned int seq; 288219d23dbfSFrederic Weisbecker u64 total; 288373fbec60SFrederic Weisbecker 288473fbec60SFrederic Weisbecker do { 288519d23dbfSFrederic Weisbecker seq = __u64_stats_fetch_begin(&irqtime->sync); 288625e2d8c1SFrederic Weisbecker total = irqtime->total; 288719d23dbfSFrederic Weisbecker } while (__u64_stats_fetch_retry(&irqtime->sync, seq)); 288873fbec60SFrederic Weisbecker 288919d23dbfSFrederic Weisbecker return total; 289073fbec60SFrederic Weisbecker } 289173fbec60SFrederic Weisbecker #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ 2892adaf9fcdSRafael J. Wysocki 2893adaf9fcdSRafael J. Wysocki #ifdef CONFIG_CPU_FREQ 2894b10abd0aSJoel Fernandes (Google) DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data); 2895adaf9fcdSRafael J. Wysocki 2896adaf9fcdSRafael J. Wysocki /** 2897adaf9fcdSRafael J. Wysocki * cpufreq_update_util - Take a note about CPU utilization changes. 289812bde33dSRafael J. Wysocki * @rq: Runqueue to carry out the update for. 289958919e83SRafael J. Wysocki * @flags: Update reason flags. 2900adaf9fcdSRafael J. Wysocki * 290158919e83SRafael J. Wysocki * This function is called by the scheduler on the CPU whose utilization is 290258919e83SRafael J. Wysocki * being updated. 2903adaf9fcdSRafael J. Wysocki * 2904adaf9fcdSRafael J. Wysocki * It can only be called from RCU-sched read-side critical sections. 2905adaf9fcdSRafael J. Wysocki * 2906adaf9fcdSRafael J. Wysocki * The way cpufreq is currently arranged requires it to evaluate the CPU 2907adaf9fcdSRafael J. Wysocki * performance state (frequency/voltage) on a regular basis to prevent it from 2908adaf9fcdSRafael J. Wysocki * being stuck in a completely inadequate performance level for too long. 2909e0367b12SJuri Lelli * That is not guaranteed to happen if the updates are only triggered from CFS 2910e0367b12SJuri Lelli * and DL, though, because they may not be coming in if only RT tasks are 2911e0367b12SJuri Lelli * active all the time (or there are RT tasks only). 2912adaf9fcdSRafael J. Wysocki * 2913e0367b12SJuri Lelli * As a workaround for that issue, this function is called periodically by the 2914e0367b12SJuri Lelli * RT sched class to trigger extra cpufreq updates to prevent it from stalling, 2915adaf9fcdSRafael J. Wysocki * but that really is a band-aid. Going forward it should be replaced with 2916e0367b12SJuri Lelli * solutions targeted more specifically at RT tasks. 2917adaf9fcdSRafael J. Wysocki */ 291812bde33dSRafael J. Wysocki static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) 2919adaf9fcdSRafael J. Wysocki { 292058919e83SRafael J. Wysocki struct update_util_data *data; 292158919e83SRafael J. Wysocki 2922674e7541SViresh Kumar data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data, 2923674e7541SViresh Kumar cpu_of(rq))); 292458919e83SRafael J. Wysocki if (data) 292512bde33dSRafael J. Wysocki data->func(data, rq_clock(rq), flags); 292612bde33dSRafael J. Wysocki } 2927adaf9fcdSRafael J. Wysocki #else 292812bde33dSRafael J. Wysocki static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {} 2929adaf9fcdSRafael J. Wysocki #endif /* CONFIG_CPU_FREQ */ 2930be53f58fSLinus Torvalds 29319bdcb44eSRafael J. Wysocki #ifdef arch_scale_freq_capacity 29329bdcb44eSRafael J. Wysocki # ifndef arch_scale_freq_invariant 293397fb7a0aSIngo Molnar # define arch_scale_freq_invariant() true 29349bdcb44eSRafael J. Wysocki # endif 293597fb7a0aSIngo Molnar #else 293697fb7a0aSIngo Molnar # define arch_scale_freq_invariant() false 29379bdcb44eSRafael J. Wysocki #endif 2938d4edd662SJuri Lelli 293910a35e68SVincent Guittot #ifdef CONFIG_SMP 294010a35e68SVincent Guittot static inline unsigned long capacity_orig_of(int cpu) 294110a35e68SVincent Guittot { 294210a35e68SVincent Guittot return cpu_rq(cpu)->cpu_capacity_orig; 294310a35e68SVincent Guittot } 294410a35e68SVincent Guittot 2945938e5e4bSQuentin Perret /** 2946a5418be9SViresh Kumar * enum cpu_util_type - CPU utilization type 2947938e5e4bSQuentin Perret * @FREQUENCY_UTIL: Utilization used to select frequency 2948938e5e4bSQuentin Perret * @ENERGY_UTIL: Utilization used during energy calculation 2949938e5e4bSQuentin Perret * 2950938e5e4bSQuentin Perret * The utilization signals of all scheduling classes (CFS/RT/DL) and IRQ time 2951938e5e4bSQuentin Perret * need to be aggregated differently depending on the usage made of them. This 2952a5418be9SViresh Kumar * enum is used within effective_cpu_util() to differentiate the types of 2953938e5e4bSQuentin Perret * utilization expected by the callers, and adjust the aggregation accordingly. 2954938e5e4bSQuentin Perret */ 2955a5418be9SViresh Kumar enum cpu_util_type { 2956938e5e4bSQuentin Perret FREQUENCY_UTIL, 2957938e5e4bSQuentin Perret ENERGY_UTIL, 2958938e5e4bSQuentin Perret }; 2959938e5e4bSQuentin Perret 2960a5418be9SViresh Kumar unsigned long effective_cpu_util(int cpu, unsigned long util_cfs, 2961bb447999SDietmar Eggemann enum cpu_util_type type, 2962af24bde8SPatrick Bellasi struct task_struct *p); 2963938e5e4bSQuentin Perret 2964b3f53daaSDietmar Eggemann /* 2965b3f53daaSDietmar Eggemann * Verify the fitness of task @p to run on @cpu taking into account the 2966b3f53daaSDietmar Eggemann * CPU original capacity and the runtime/deadline ratio of the task. 2967b3f53daaSDietmar Eggemann * 2968b3f53daaSDietmar Eggemann * The function will return true if the original capacity of @cpu is 2969b3f53daaSDietmar Eggemann * greater than or equal to task's deadline density right shifted by 2970b3f53daaSDietmar Eggemann * (BW_SHIFT - SCHED_CAPACITY_SHIFT) and false otherwise. 2971b3f53daaSDietmar Eggemann */ 2972b3f53daaSDietmar Eggemann static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu) 2973b3f53daaSDietmar Eggemann { 2974b3f53daaSDietmar Eggemann unsigned long cap = arch_scale_cpu_capacity(cpu); 2975b3f53daaSDietmar Eggemann 2976b3f53daaSDietmar Eggemann return cap >= p->dl.dl_density >> (BW_SHIFT - SCHED_CAPACITY_SHIFT); 2977b3f53daaSDietmar Eggemann } 2978b3f53daaSDietmar Eggemann 29798cc90515SVincent Guittot static inline unsigned long cpu_bw_dl(struct rq *rq) 2980d4edd662SJuri Lelli { 2981d4edd662SJuri Lelli return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT; 2982d4edd662SJuri Lelli } 2983d4edd662SJuri Lelli 29848cc90515SVincent Guittot static inline unsigned long cpu_util_dl(struct rq *rq) 29858cc90515SVincent Guittot { 29868cc90515SVincent Guittot return READ_ONCE(rq->avg_dl.util_avg); 29878cc90515SVincent Guittot } 29888cc90515SVincent Guittot 298982762d2aSDietmar Eggemann 29903eb6d6ecSDietmar Eggemann extern unsigned long cpu_util_cfs(int cpu); 29917d0583cfSDietmar Eggemann extern unsigned long cpu_util_cfs_boost(int cpu); 2992371bf427SVincent Guittot 2993371bf427SVincent Guittot static inline unsigned long cpu_util_rt(struct rq *rq) 2994371bf427SVincent Guittot { 2995dfa444dcSVincent Guittot return READ_ONCE(rq->avg_rt.util_avg); 2996371bf427SVincent Guittot } 29977d6a905fSViresh Kumar #endif 29989033ea11SVincent Guittot 29997a17e1dbSQais Yousef #ifdef CONFIG_UCLAMP_TASK 30007a17e1dbSQais Yousef unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id); 30017a17e1dbSQais Yousef 300224422603SQais Yousef static inline unsigned long uclamp_rq_get(struct rq *rq, 300324422603SQais Yousef enum uclamp_id clamp_id) 300424422603SQais Yousef { 300524422603SQais Yousef return READ_ONCE(rq->uclamp[clamp_id].value); 300624422603SQais Yousef } 300724422603SQais Yousef 300824422603SQais Yousef static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, 300924422603SQais Yousef unsigned int value) 301024422603SQais Yousef { 301124422603SQais Yousef WRITE_ONCE(rq->uclamp[clamp_id].value, value); 301224422603SQais Yousef } 301324422603SQais Yousef 301424422603SQais Yousef static inline bool uclamp_rq_is_idle(struct rq *rq) 301524422603SQais Yousef { 301624422603SQais Yousef return rq->uclamp_flags & UCLAMP_FLAG_IDLE; 301724422603SQais Yousef } 301824422603SQais Yousef 30197a17e1dbSQais Yousef /** 30207a17e1dbSQais Yousef * uclamp_rq_util_with - clamp @util with @rq and @p effective uclamp values. 30217a17e1dbSQais Yousef * @rq: The rq to clamp against. Must not be NULL. 30227a17e1dbSQais Yousef * @util: The util value to clamp. 30237a17e1dbSQais Yousef * @p: The task to clamp against. Can be NULL if you want to clamp 30247a17e1dbSQais Yousef * against @rq only. 30257a17e1dbSQais Yousef * 30267a17e1dbSQais Yousef * Clamps the passed @util to the max(@rq, @p) effective uclamp values. 30277a17e1dbSQais Yousef * 30287a17e1dbSQais Yousef * If sched_uclamp_used static key is disabled, then just return the util 30297a17e1dbSQais Yousef * without any clamping since uclamp aggregation at the rq level in the fast 30307a17e1dbSQais Yousef * path is disabled, rendering this operation a NOP. 30317a17e1dbSQais Yousef * 30327a17e1dbSQais Yousef * Use uclamp_eff_value() if you don't care about uclamp values at rq level. It 30337a17e1dbSQais Yousef * will return the correct effective uclamp value of the task even if the 30347a17e1dbSQais Yousef * static key is disabled. 30357a17e1dbSQais Yousef */ 30367a17e1dbSQais Yousef static __always_inline 30377a17e1dbSQais Yousef unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util, 30387a17e1dbSQais Yousef struct task_struct *p) 30397a17e1dbSQais Yousef { 30407a17e1dbSQais Yousef unsigned long min_util = 0; 30417a17e1dbSQais Yousef unsigned long max_util = 0; 30427a17e1dbSQais Yousef 30437a17e1dbSQais Yousef if (!static_branch_likely(&sched_uclamp_used)) 30447a17e1dbSQais Yousef return util; 30457a17e1dbSQais Yousef 30467a17e1dbSQais Yousef if (p) { 30477a17e1dbSQais Yousef min_util = uclamp_eff_value(p, UCLAMP_MIN); 30487a17e1dbSQais Yousef max_util = uclamp_eff_value(p, UCLAMP_MAX); 30497a17e1dbSQais Yousef 30507a17e1dbSQais Yousef /* 30517a17e1dbSQais Yousef * Ignore last runnable task's max clamp, as this task will 30527a17e1dbSQais Yousef * reset it. Similarly, no need to read the rq's min clamp. 30537a17e1dbSQais Yousef */ 305424422603SQais Yousef if (uclamp_rq_is_idle(rq)) 30557a17e1dbSQais Yousef goto out; 30567a17e1dbSQais Yousef } 30577a17e1dbSQais Yousef 305824422603SQais Yousef min_util = max_t(unsigned long, min_util, uclamp_rq_get(rq, UCLAMP_MIN)); 305924422603SQais Yousef max_util = max_t(unsigned long, max_util, uclamp_rq_get(rq, UCLAMP_MAX)); 30607a17e1dbSQais Yousef out: 30617a17e1dbSQais Yousef /* 30627a17e1dbSQais Yousef * Since CPU's {min,max}_util clamps are MAX aggregated considering 30637a17e1dbSQais Yousef * RUNNABLE tasks with _different_ clamps, we can end up with an 30647a17e1dbSQais Yousef * inversion. Fix it now when the clamps are applied. 30657a17e1dbSQais Yousef */ 30667a17e1dbSQais Yousef if (unlikely(min_util >= max_util)) 30677a17e1dbSQais Yousef return min_util; 30687a17e1dbSQais Yousef 30697a17e1dbSQais Yousef return clamp(util, min_util, max_util); 30707a17e1dbSQais Yousef } 30717a17e1dbSQais Yousef 30727a17e1dbSQais Yousef /* Is the rq being capped/throttled by uclamp_max? */ 30737a17e1dbSQais Yousef static inline bool uclamp_rq_is_capped(struct rq *rq) 30747a17e1dbSQais Yousef { 30757a17e1dbSQais Yousef unsigned long rq_util; 30767a17e1dbSQais Yousef unsigned long max_util; 30777a17e1dbSQais Yousef 30787a17e1dbSQais Yousef if (!static_branch_likely(&sched_uclamp_used)) 30797a17e1dbSQais Yousef return false; 30807a17e1dbSQais Yousef 30817a17e1dbSQais Yousef rq_util = cpu_util_cfs(cpu_of(rq)) + cpu_util_rt(rq); 30827a17e1dbSQais Yousef max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value); 30837a17e1dbSQais Yousef 30847a17e1dbSQais Yousef return max_util != SCHED_CAPACITY_SCALE && rq_util >= max_util; 30857a17e1dbSQais Yousef } 30867a17e1dbSQais Yousef 30877a17e1dbSQais Yousef /* 30887a17e1dbSQais Yousef * When uclamp is compiled in, the aggregation at rq level is 'turned off' 30897a17e1dbSQais Yousef * by default in the fast path and only gets turned on once userspace performs 30907a17e1dbSQais Yousef * an operation that requires it. 30917a17e1dbSQais Yousef * 30927a17e1dbSQais Yousef * Returns true if userspace opted-in to use uclamp and aggregation at rq level 30937a17e1dbSQais Yousef * hence is active. 30947a17e1dbSQais Yousef */ 30957a17e1dbSQais Yousef static inline bool uclamp_is_used(void) 30967a17e1dbSQais Yousef { 30977a17e1dbSQais Yousef return static_branch_likely(&sched_uclamp_used); 30987a17e1dbSQais Yousef } 30997a17e1dbSQais Yousef #else /* CONFIG_UCLAMP_TASK */ 3100b48e16a6SQais Yousef static inline unsigned long uclamp_eff_value(struct task_struct *p, 3101b48e16a6SQais Yousef enum uclamp_id clamp_id) 3102b48e16a6SQais Yousef { 3103b48e16a6SQais Yousef if (clamp_id == UCLAMP_MIN) 3104b48e16a6SQais Yousef return 0; 3105b48e16a6SQais Yousef 3106b48e16a6SQais Yousef return SCHED_CAPACITY_SCALE; 3107b48e16a6SQais Yousef } 3108b48e16a6SQais Yousef 31097a17e1dbSQais Yousef static inline 31107a17e1dbSQais Yousef unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util, 31117a17e1dbSQais Yousef struct task_struct *p) 31127a17e1dbSQais Yousef { 31137a17e1dbSQais Yousef return util; 31147a17e1dbSQais Yousef } 31157a17e1dbSQais Yousef 31167a17e1dbSQais Yousef static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; } 31177a17e1dbSQais Yousef 31187a17e1dbSQais Yousef static inline bool uclamp_is_used(void) 31197a17e1dbSQais Yousef { 31207a17e1dbSQais Yousef return false; 31217a17e1dbSQais Yousef } 312224422603SQais Yousef 312324422603SQais Yousef static inline unsigned long uclamp_rq_get(struct rq *rq, 312424422603SQais Yousef enum uclamp_id clamp_id) 312524422603SQais Yousef { 312624422603SQais Yousef if (clamp_id == UCLAMP_MIN) 312724422603SQais Yousef return 0; 312824422603SQais Yousef 312924422603SQais Yousef return SCHED_CAPACITY_SCALE; 313024422603SQais Yousef } 313124422603SQais Yousef 313224422603SQais Yousef static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, 313324422603SQais Yousef unsigned int value) 313424422603SQais Yousef { 313524422603SQais Yousef } 313624422603SQais Yousef 313724422603SQais Yousef static inline bool uclamp_rq_is_idle(struct rq *rq) 313824422603SQais Yousef { 313924422603SQais Yousef return false; 314024422603SQais Yousef } 31417a17e1dbSQais Yousef #endif /* CONFIG_UCLAMP_TASK */ 31427a17e1dbSQais Yousef 314311d4afd4SVincent Guittot #ifdef CONFIG_HAVE_SCHED_AVG_IRQ 31449033ea11SVincent Guittot static inline unsigned long cpu_util_irq(struct rq *rq) 31459033ea11SVincent Guittot { 31469033ea11SVincent Guittot return rq->avg_irq.util_avg; 31479033ea11SVincent Guittot } 31482e62c474SVincent Guittot 31492e62c474SVincent Guittot static inline 31502e62c474SVincent Guittot unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max) 31512e62c474SVincent Guittot { 31522e62c474SVincent Guittot util *= (max - irq); 31532e62c474SVincent Guittot util /= max; 31542e62c474SVincent Guittot 31552e62c474SVincent Guittot return util; 31562e62c474SVincent Guittot 31572e62c474SVincent Guittot } 31589033ea11SVincent Guittot #else 31599033ea11SVincent Guittot static inline unsigned long cpu_util_irq(struct rq *rq) 31609033ea11SVincent Guittot { 31619033ea11SVincent Guittot return 0; 31629033ea11SVincent Guittot } 31639033ea11SVincent Guittot 31642e62c474SVincent Guittot static inline 31652e62c474SVincent Guittot unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max) 31662e62c474SVincent Guittot { 31672e62c474SVincent Guittot return util; 31682e62c474SVincent Guittot } 3169794a56ebSJuri Lelli #endif 31706aa140faSQuentin Perret 3171531b5c9fSQuentin Perret #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) 31721f74de87SQuentin Perret 3173f8a696f2SPeter Zijlstra #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus))) 3174f8a696f2SPeter Zijlstra 3175f8a696f2SPeter Zijlstra DECLARE_STATIC_KEY_FALSE(sched_energy_present); 3176f8a696f2SPeter Zijlstra 3177f8a696f2SPeter Zijlstra static inline bool sched_energy_enabled(void) 3178f8a696f2SPeter Zijlstra { 3179f8a696f2SPeter Zijlstra return static_branch_unlikely(&sched_energy_present); 3180f8a696f2SPeter Zijlstra } 3181f8a696f2SPeter Zijlstra 3182f8a696f2SPeter Zijlstra #else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */ 3183f8a696f2SPeter Zijlstra 3184f8a696f2SPeter Zijlstra #define perf_domain_span(pd) NULL 3185f8a696f2SPeter Zijlstra static inline bool sched_energy_enabled(void) { return false; } 3186f8a696f2SPeter Zijlstra 3187f8a696f2SPeter Zijlstra #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */ 3188227a4aadSMathieu Desnoyers 3189227a4aadSMathieu Desnoyers #ifdef CONFIG_MEMBARRIER 3190227a4aadSMathieu Desnoyers /* 3191227a4aadSMathieu Desnoyers * The scheduler provides memory barriers required by membarrier between: 3192227a4aadSMathieu Desnoyers * - prior user-space memory accesses and store to rq->membarrier_state, 3193227a4aadSMathieu Desnoyers * - store to rq->membarrier_state and following user-space memory accesses. 3194227a4aadSMathieu Desnoyers * In the same way it provides those guarantees around store to rq->curr. 3195227a4aadSMathieu Desnoyers */ 3196227a4aadSMathieu Desnoyers static inline void membarrier_switch_mm(struct rq *rq, 3197227a4aadSMathieu Desnoyers struct mm_struct *prev_mm, 3198227a4aadSMathieu Desnoyers struct mm_struct *next_mm) 3199227a4aadSMathieu Desnoyers { 3200227a4aadSMathieu Desnoyers int membarrier_state; 3201227a4aadSMathieu Desnoyers 3202227a4aadSMathieu Desnoyers if (prev_mm == next_mm) 3203227a4aadSMathieu Desnoyers return; 3204227a4aadSMathieu Desnoyers 3205227a4aadSMathieu Desnoyers membarrier_state = atomic_read(&next_mm->membarrier_state); 3206227a4aadSMathieu Desnoyers if (READ_ONCE(rq->membarrier_state) == membarrier_state) 3207227a4aadSMathieu Desnoyers return; 3208227a4aadSMathieu Desnoyers 3209227a4aadSMathieu Desnoyers WRITE_ONCE(rq->membarrier_state, membarrier_state); 3210227a4aadSMathieu Desnoyers } 3211227a4aadSMathieu Desnoyers #else 3212227a4aadSMathieu Desnoyers static inline void membarrier_switch_mm(struct rq *rq, 3213227a4aadSMathieu Desnoyers struct mm_struct *prev_mm, 3214227a4aadSMathieu Desnoyers struct mm_struct *next_mm) 3215227a4aadSMathieu Desnoyers { 3216227a4aadSMathieu Desnoyers } 3217227a4aadSMathieu Desnoyers #endif 321852262ee5SMel Gorman 321952262ee5SMel Gorman #ifdef CONFIG_SMP 322052262ee5SMel Gorman static inline bool is_per_cpu_kthread(struct task_struct *p) 322152262ee5SMel Gorman { 322252262ee5SMel Gorman if (!(p->flags & PF_KTHREAD)) 322352262ee5SMel Gorman return false; 322452262ee5SMel Gorman 322552262ee5SMel Gorman if (p->nr_cpus_allowed != 1) 322652262ee5SMel Gorman return false; 322752262ee5SMel Gorman 322852262ee5SMel Gorman return true; 322952262ee5SMel Gorman } 323052262ee5SMel Gorman #endif 3231b3212fe2SThomas Gleixner 32321011dcceSPeter Zijlstra extern void swake_up_all_locked(struct swait_queue_head *q); 32331011dcceSPeter Zijlstra extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait); 32341011dcceSPeter Zijlstra 32351011dcceSPeter Zijlstra #ifdef CONFIG_PREEMPT_DYNAMIC 32361011dcceSPeter Zijlstra extern int preempt_dynamic_mode; 32371011dcceSPeter Zijlstra extern int sched_dynamic_mode(const char *str); 32381011dcceSPeter Zijlstra extern void sched_dynamic_update(int mode); 32391011dcceSPeter Zijlstra #endif 32401011dcceSPeter Zijlstra 32415531ecffSShang XiaoJing static inline void update_current_exec_runtime(struct task_struct *curr, 32425531ecffSShang XiaoJing u64 now, u64 delta_exec) 32435531ecffSShang XiaoJing { 32445531ecffSShang XiaoJing curr->se.sum_exec_runtime += delta_exec; 32455531ecffSShang XiaoJing account_group_exec_runtime(curr, delta_exec); 32465531ecffSShang XiaoJing 32475531ecffSShang XiaoJing curr->se.exec_start = now; 32485531ecffSShang XiaoJing cgroup_account_cputime(curr, delta_exec); 32495531ecffSShang XiaoJing } 32505531ecffSShang XiaoJing 3251af7f588dSMathieu Desnoyers #ifdef CONFIG_SCHED_MM_CID 3252223baf9dSMathieu Desnoyers 3253223baf9dSMathieu Desnoyers #define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */ 3254223baf9dSMathieu Desnoyers #define MM_CID_SCAN_DELAY 100 /* 100ms */ 3255223baf9dSMathieu Desnoyers 3256223baf9dSMathieu Desnoyers extern raw_spinlock_t cid_lock; 3257223baf9dSMathieu Desnoyers extern int use_cid_lock; 3258223baf9dSMathieu Desnoyers 3259223baf9dSMathieu Desnoyers extern void sched_mm_cid_migrate_from(struct task_struct *t); 3260223baf9dSMathieu Desnoyers extern void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t); 3261223baf9dSMathieu Desnoyers extern void task_tick_mm_cid(struct rq *rq, struct task_struct *curr); 3262223baf9dSMathieu Desnoyers extern void init_sched_mm_cid(struct task_struct *t); 3263223baf9dSMathieu Desnoyers 3264223baf9dSMathieu Desnoyers static inline void __mm_cid_put(struct mm_struct *mm, int cid) 3265223baf9dSMathieu Desnoyers { 3266223baf9dSMathieu Desnoyers if (cid < 0) 3267223baf9dSMathieu Desnoyers return; 3268223baf9dSMathieu Desnoyers cpumask_clear_cpu(cid, mm_cidmask(mm)); 3269223baf9dSMathieu Desnoyers } 3270223baf9dSMathieu Desnoyers 3271223baf9dSMathieu Desnoyers /* 3272223baf9dSMathieu Desnoyers * The per-mm/cpu cid can have the MM_CID_LAZY_PUT flag set or transition to 3273223baf9dSMathieu Desnoyers * the MM_CID_UNSET state without holding the rq lock, but the rq lock needs to 3274223baf9dSMathieu Desnoyers * be held to transition to other states. 3275223baf9dSMathieu Desnoyers * 3276223baf9dSMathieu Desnoyers * State transitions synchronized with cmpxchg or try_cmpxchg need to be 3277223baf9dSMathieu Desnoyers * consistent across cpus, which prevents use of this_cpu_cmpxchg. 3278223baf9dSMathieu Desnoyers */ 3279223baf9dSMathieu Desnoyers static inline void mm_cid_put_lazy(struct task_struct *t) 3280223baf9dSMathieu Desnoyers { 3281223baf9dSMathieu Desnoyers struct mm_struct *mm = t->mm; 3282223baf9dSMathieu Desnoyers struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; 3283223baf9dSMathieu Desnoyers int cid; 3284223baf9dSMathieu Desnoyers 3285223baf9dSMathieu Desnoyers lockdep_assert_irqs_disabled(); 3286223baf9dSMathieu Desnoyers cid = __this_cpu_read(pcpu_cid->cid); 3287223baf9dSMathieu Desnoyers if (!mm_cid_is_lazy_put(cid) || 3288223baf9dSMathieu Desnoyers !try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET)) 3289223baf9dSMathieu Desnoyers return; 3290223baf9dSMathieu Desnoyers __mm_cid_put(mm, mm_cid_clear_lazy_put(cid)); 3291223baf9dSMathieu Desnoyers } 3292223baf9dSMathieu Desnoyers 3293223baf9dSMathieu Desnoyers static inline int mm_cid_pcpu_unset(struct mm_struct *mm) 3294223baf9dSMathieu Desnoyers { 3295223baf9dSMathieu Desnoyers struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; 3296223baf9dSMathieu Desnoyers int cid, res; 3297223baf9dSMathieu Desnoyers 3298223baf9dSMathieu Desnoyers lockdep_assert_irqs_disabled(); 3299223baf9dSMathieu Desnoyers cid = __this_cpu_read(pcpu_cid->cid); 3300223baf9dSMathieu Desnoyers for (;;) { 3301223baf9dSMathieu Desnoyers if (mm_cid_is_unset(cid)) 3302223baf9dSMathieu Desnoyers return MM_CID_UNSET; 3303223baf9dSMathieu Desnoyers /* 3304223baf9dSMathieu Desnoyers * Attempt transition from valid or lazy-put to unset. 3305223baf9dSMathieu Desnoyers */ 3306223baf9dSMathieu Desnoyers res = cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, cid, MM_CID_UNSET); 3307223baf9dSMathieu Desnoyers if (res == cid) 3308223baf9dSMathieu Desnoyers break; 3309223baf9dSMathieu Desnoyers cid = res; 3310223baf9dSMathieu Desnoyers } 3311223baf9dSMathieu Desnoyers return cid; 3312223baf9dSMathieu Desnoyers } 3313223baf9dSMathieu Desnoyers 3314223baf9dSMathieu Desnoyers static inline void mm_cid_put(struct mm_struct *mm) 3315223baf9dSMathieu Desnoyers { 3316223baf9dSMathieu Desnoyers int cid; 3317223baf9dSMathieu Desnoyers 3318223baf9dSMathieu Desnoyers lockdep_assert_irqs_disabled(); 3319223baf9dSMathieu Desnoyers cid = mm_cid_pcpu_unset(mm); 3320223baf9dSMathieu Desnoyers if (cid == MM_CID_UNSET) 3321223baf9dSMathieu Desnoyers return; 3322223baf9dSMathieu Desnoyers __mm_cid_put(mm, mm_cid_clear_lazy_put(cid)); 3323223baf9dSMathieu Desnoyers } 3324223baf9dSMathieu Desnoyers 3325223baf9dSMathieu Desnoyers static inline int __mm_cid_try_get(struct mm_struct *mm) 3326af7f588dSMathieu Desnoyers { 3327af7f588dSMathieu Desnoyers struct cpumask *cpumask; 3328af7f588dSMathieu Desnoyers int cid; 3329af7f588dSMathieu Desnoyers 3330af7f588dSMathieu Desnoyers cpumask = mm_cidmask(mm); 3331223baf9dSMathieu Desnoyers /* 3332223baf9dSMathieu Desnoyers * Retry finding first zero bit if the mask is temporarily 3333223baf9dSMathieu Desnoyers * filled. This only happens during concurrent remote-clear 3334223baf9dSMathieu Desnoyers * which owns a cid without holding a rq lock. 3335223baf9dSMathieu Desnoyers */ 3336223baf9dSMathieu Desnoyers for (;;) { 3337af7f588dSMathieu Desnoyers cid = cpumask_first_zero(cpumask); 3338223baf9dSMathieu Desnoyers if (cid < nr_cpu_ids) 3339223baf9dSMathieu Desnoyers break; 3340223baf9dSMathieu Desnoyers cpu_relax(); 3341223baf9dSMathieu Desnoyers } 3342223baf9dSMathieu Desnoyers if (cpumask_test_and_set_cpu(cid, cpumask)) 3343af7f588dSMathieu Desnoyers return -1; 3344af7f588dSMathieu Desnoyers return cid; 3345af7f588dSMathieu Desnoyers } 3346af7f588dSMathieu Desnoyers 3347af7f588dSMathieu Desnoyers /* 3348223baf9dSMathieu Desnoyers * Save a snapshot of the current runqueue time of this cpu 3349223baf9dSMathieu Desnoyers * with the per-cpu cid value, allowing to estimate how recently it was used. 3350af7f588dSMathieu Desnoyers */ 3351223baf9dSMathieu Desnoyers static inline void mm_cid_snapshot_time(struct rq *rq, struct mm_struct *mm) 3352223baf9dSMathieu Desnoyers { 3353223baf9dSMathieu Desnoyers struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu_of(rq)); 3354223baf9dSMathieu Desnoyers 3355223baf9dSMathieu Desnoyers lockdep_assert_rq_held(rq); 3356223baf9dSMathieu Desnoyers WRITE_ONCE(pcpu_cid->time, rq->clock); 3357af7f588dSMathieu Desnoyers } 3358223baf9dSMathieu Desnoyers 3359223baf9dSMathieu Desnoyers static inline int __mm_cid_get(struct rq *rq, struct mm_struct *mm) 3360223baf9dSMathieu Desnoyers { 3361223baf9dSMathieu Desnoyers int cid; 3362223baf9dSMathieu Desnoyers 3363223baf9dSMathieu Desnoyers /* 3364223baf9dSMathieu Desnoyers * All allocations (even those using the cid_lock) are lock-free. If 3365223baf9dSMathieu Desnoyers * use_cid_lock is set, hold the cid_lock to perform cid allocation to 3366223baf9dSMathieu Desnoyers * guarantee forward progress. 3367223baf9dSMathieu Desnoyers */ 3368223baf9dSMathieu Desnoyers if (!READ_ONCE(use_cid_lock)) { 3369223baf9dSMathieu Desnoyers cid = __mm_cid_try_get(mm); 3370223baf9dSMathieu Desnoyers if (cid >= 0) 3371223baf9dSMathieu Desnoyers goto end; 3372223baf9dSMathieu Desnoyers raw_spin_lock(&cid_lock); 3373223baf9dSMathieu Desnoyers } else { 3374223baf9dSMathieu Desnoyers raw_spin_lock(&cid_lock); 3375223baf9dSMathieu Desnoyers cid = __mm_cid_try_get(mm); 3376223baf9dSMathieu Desnoyers if (cid >= 0) 3377223baf9dSMathieu Desnoyers goto unlock; 3378223baf9dSMathieu Desnoyers } 3379223baf9dSMathieu Desnoyers 3380223baf9dSMathieu Desnoyers /* 3381223baf9dSMathieu Desnoyers * cid concurrently allocated. Retry while forcing following 3382223baf9dSMathieu Desnoyers * allocations to use the cid_lock to ensure forward progress. 3383223baf9dSMathieu Desnoyers */ 3384223baf9dSMathieu Desnoyers WRITE_ONCE(use_cid_lock, 1); 3385223baf9dSMathieu Desnoyers /* 3386223baf9dSMathieu Desnoyers * Set use_cid_lock before allocation. Only care about program order 3387223baf9dSMathieu Desnoyers * because this is only required for forward progress. 3388223baf9dSMathieu Desnoyers */ 3389223baf9dSMathieu Desnoyers barrier(); 3390223baf9dSMathieu Desnoyers /* 3391223baf9dSMathieu Desnoyers * Retry until it succeeds. It is guaranteed to eventually succeed once 3392223baf9dSMathieu Desnoyers * all newcoming allocations observe the use_cid_lock flag set. 3393223baf9dSMathieu Desnoyers */ 3394223baf9dSMathieu Desnoyers do { 3395223baf9dSMathieu Desnoyers cid = __mm_cid_try_get(mm); 3396223baf9dSMathieu Desnoyers cpu_relax(); 3397223baf9dSMathieu Desnoyers } while (cid < 0); 3398223baf9dSMathieu Desnoyers /* 3399223baf9dSMathieu Desnoyers * Allocate before clearing use_cid_lock. Only care about 3400223baf9dSMathieu Desnoyers * program order because this is for forward progress. 3401223baf9dSMathieu Desnoyers */ 3402223baf9dSMathieu Desnoyers barrier(); 3403223baf9dSMathieu Desnoyers WRITE_ONCE(use_cid_lock, 0); 3404223baf9dSMathieu Desnoyers unlock: 3405223baf9dSMathieu Desnoyers raw_spin_unlock(&cid_lock); 3406223baf9dSMathieu Desnoyers end: 3407223baf9dSMathieu Desnoyers mm_cid_snapshot_time(rq, mm); 3408223baf9dSMathieu Desnoyers return cid; 3409223baf9dSMathieu Desnoyers } 3410223baf9dSMathieu Desnoyers 3411223baf9dSMathieu Desnoyers static inline int mm_cid_get(struct rq *rq, struct mm_struct *mm) 3412223baf9dSMathieu Desnoyers { 3413223baf9dSMathieu Desnoyers struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; 3414223baf9dSMathieu Desnoyers struct cpumask *cpumask; 3415223baf9dSMathieu Desnoyers int cid; 3416223baf9dSMathieu Desnoyers 3417223baf9dSMathieu Desnoyers lockdep_assert_rq_held(rq); 3418223baf9dSMathieu Desnoyers cpumask = mm_cidmask(mm); 3419223baf9dSMathieu Desnoyers cid = __this_cpu_read(pcpu_cid->cid); 3420223baf9dSMathieu Desnoyers if (mm_cid_is_valid(cid)) { 3421223baf9dSMathieu Desnoyers mm_cid_snapshot_time(rq, mm); 3422223baf9dSMathieu Desnoyers return cid; 3423223baf9dSMathieu Desnoyers } 3424223baf9dSMathieu Desnoyers if (mm_cid_is_lazy_put(cid)) { 3425223baf9dSMathieu Desnoyers if (try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET)) 3426223baf9dSMathieu Desnoyers __mm_cid_put(mm, mm_cid_clear_lazy_put(cid)); 3427223baf9dSMathieu Desnoyers } 3428223baf9dSMathieu Desnoyers cid = __mm_cid_get(rq, mm); 3429223baf9dSMathieu Desnoyers __this_cpu_write(pcpu_cid->cid, cid); 3430223baf9dSMathieu Desnoyers return cid; 3431223baf9dSMathieu Desnoyers } 3432223baf9dSMathieu Desnoyers 3433223baf9dSMathieu Desnoyers static inline void switch_mm_cid(struct rq *rq, 3434223baf9dSMathieu Desnoyers struct task_struct *prev, 3435223baf9dSMathieu Desnoyers struct task_struct *next) 3436223baf9dSMathieu Desnoyers { 3437223baf9dSMathieu Desnoyers /* 3438223baf9dSMathieu Desnoyers * Provide a memory barrier between rq->curr store and load of 3439223baf9dSMathieu Desnoyers * {prev,next}->mm->pcpu_cid[cpu] on rq->curr->mm transition. 3440223baf9dSMathieu Desnoyers * 3441223baf9dSMathieu Desnoyers * Should be adapted if context_switch() is modified. 3442223baf9dSMathieu Desnoyers */ 3443223baf9dSMathieu Desnoyers if (!next->mm) { // to kernel 3444223baf9dSMathieu Desnoyers /* 3445223baf9dSMathieu Desnoyers * user -> kernel transition does not guarantee a barrier, but 3446223baf9dSMathieu Desnoyers * we can use the fact that it performs an atomic operation in 3447223baf9dSMathieu Desnoyers * mmgrab(). 3448223baf9dSMathieu Desnoyers */ 3449223baf9dSMathieu Desnoyers if (prev->mm) // from user 3450223baf9dSMathieu Desnoyers smp_mb__after_mmgrab(); 3451223baf9dSMathieu Desnoyers /* 3452223baf9dSMathieu Desnoyers * kernel -> kernel transition does not change rq->curr->mm 3453223baf9dSMathieu Desnoyers * state. It stays NULL. 3454223baf9dSMathieu Desnoyers */ 3455223baf9dSMathieu Desnoyers } else { // to user 3456223baf9dSMathieu Desnoyers /* 3457223baf9dSMathieu Desnoyers * kernel -> user transition does not provide a barrier 3458223baf9dSMathieu Desnoyers * between rq->curr store and load of {prev,next}->mm->pcpu_cid[cpu]. 3459223baf9dSMathieu Desnoyers * Provide it here. 3460223baf9dSMathieu Desnoyers */ 3461223baf9dSMathieu Desnoyers if (!prev->mm) // from kernel 3462223baf9dSMathieu Desnoyers smp_mb(); 3463223baf9dSMathieu Desnoyers /* 3464223baf9dSMathieu Desnoyers * user -> user transition guarantees a memory barrier through 3465223baf9dSMathieu Desnoyers * switch_mm() when current->mm changes. If current->mm is 3466223baf9dSMathieu Desnoyers * unchanged, no barrier is needed. 3467223baf9dSMathieu Desnoyers */ 3468223baf9dSMathieu Desnoyers } 3469223baf9dSMathieu Desnoyers if (prev->mm_cid_active) { 3470223baf9dSMathieu Desnoyers mm_cid_snapshot_time(rq, prev->mm); 3471223baf9dSMathieu Desnoyers mm_cid_put_lazy(prev); 3472af7f588dSMathieu Desnoyers prev->mm_cid = -1; 3473af7f588dSMathieu Desnoyers } 3474af7f588dSMathieu Desnoyers if (next->mm_cid_active) 3475223baf9dSMathieu Desnoyers next->last_mm_cid = next->mm_cid = mm_cid_get(rq, next->mm); 3476af7f588dSMathieu Desnoyers } 3477af7f588dSMathieu Desnoyers 3478af7f588dSMathieu Desnoyers #else 3479223baf9dSMathieu Desnoyers static inline void switch_mm_cid(struct rq *rq, struct task_struct *prev, struct task_struct *next) { } 3480223baf9dSMathieu Desnoyers static inline void sched_mm_cid_migrate_from(struct task_struct *t) { } 3481223baf9dSMathieu Desnoyers static inline void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t) { } 3482223baf9dSMathieu Desnoyers static inline void task_tick_mm_cid(struct rq *rq, struct task_struct *curr) { } 3483223baf9dSMathieu Desnoyers static inline void init_sched_mm_cid(struct task_struct *t) { } 3484af7f588dSMathieu Desnoyers #endif 3485af7f588dSMathieu Desnoyers 348695458477SIngo Molnar #endif /* _KERNEL_SCHED_SCHED_H */ 3487