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