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