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/blkdev.h>
40 #include <linux/compat.h>
41 #include <linux/context_tracking.h>
42 #include <linux/cpufreq.h>
43 #include <linux/cpuidle.h>
44 #include <linux/cpuset.h>
45 #include <linux/ctype.h>
46 #include <linux/debugfs.h>
47 #include <linux/delayacct.h>
48 #include <linux/energy_model.h>
49 #include <linux/init_task.h>
50 #include <linux/kprobes.h>
51 #include <linux/kthread.h>
52 #include <linux/membarrier.h>
53 #include <linux/migrate.h>
54 #include <linux/mmu_context.h>
55 #include <linux/nmi.h>
56 #include <linux/proc_fs.h>
57 #include <linux/prefetch.h>
58 #include <linux/profile.h>
59 #include <linux/psi.h>
60 #include <linux/rcupdate_wait.h>
61 #include <linux/security.h>
62 #include <linux/stop_machine.h>
63 #include <linux/suspend.h>
64 #include <linux/swait.h>
65 #include <linux/syscalls.h>
66 #include <linux/task_work.h>
67 #include <linux/tsacct_kern.h>
68 #include <linux/android_vendor.h>
69 #include <linux/android_kabi.h>
70
71 #include <asm/tlb.h>
72 #include <asm-generic/vmlinux.lds.h>
73 #include <soc/rockchip/rockchip_performance.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[USER_PRIO(NICE_TO_PRIO(0))]) == 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_cpu_busy(int cpu, struct task_struct *p);
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 s64 hierarchical_quota;
373
374 u8 idle;
375 u8 period_active;
376 u8 slack_started;
377 struct hrtimer period_timer;
378 struct hrtimer slack_timer;
379 struct list_head throttled_cfs_rq;
380
381 /* Statistics: */
382 int nr_periods;
383 int nr_throttled;
384 u64 throttled_time;
385 #endif
386 };
387
388 /* Task group related information */
389 struct task_group {
390 struct cgroup_subsys_state css;
391
392 #ifdef CONFIG_FAIR_GROUP_SCHED
393 /* schedulable entities of this group on each CPU */
394 struct sched_entity **se;
395 /* runqueue "owned" by this group on each CPU */
396 struct cfs_rq **cfs_rq;
397 unsigned long shares;
398
399 #ifdef CONFIG_SMP
400 /*
401 * load_avg can be heavily contended at clock tick time, so put
402 * it in its own cacheline separated from the fields above which
403 * will also be accessed at each tick.
404 */
405 atomic_long_t load_avg ____cacheline_aligned;
406 #endif
407 #endif
408
409 #ifdef CONFIG_RT_GROUP_SCHED
410 struct sched_rt_entity **rt_se;
411 struct rt_rq **rt_rq;
412
413 struct rt_bandwidth rt_bandwidth;
414 #endif
415
416 struct rcu_head rcu;
417 struct list_head list;
418
419 struct task_group *parent;
420 struct list_head siblings;
421 struct list_head children;
422
423 #ifdef CONFIG_SCHED_AUTOGROUP
424 struct autogroup *autogroup;
425 #endif
426
427 struct cfs_bandwidth cfs_bandwidth;
428
429 #ifdef CONFIG_UCLAMP_TASK_GROUP
430 /* The two decimal precision [%] value requested from user-space */
431 unsigned int uclamp_pct[UCLAMP_CNT];
432 /* Clamp values requested for a task group */
433 struct uclamp_se uclamp_req[UCLAMP_CNT];
434 /* Effective clamp values used for a task group */
435 struct uclamp_se uclamp[UCLAMP_CNT];
436 /* Latency-sensitive flag used for a task group */
437 unsigned int latency_sensitive;
438
439 ANDROID_VENDOR_DATA_ARRAY(1, 4);
440 #endif
441
442 ANDROID_KABI_RESERVE(1);
443 ANDROID_KABI_RESERVE(2);
444 ANDROID_KABI_RESERVE(3);
445 ANDROID_KABI_RESERVE(4);
446 };
447
448 #ifdef CONFIG_FAIR_GROUP_SCHED
449 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
450
451 /*
452 * A weight of 0 or 1 can cause arithmetics problems.
453 * A weight of a cfs_rq is the sum of weights of which entities
454 * are queued on this cfs_rq, so a weight of a entity should not be
455 * too large, so as the shares value of a task group.
456 * (The default weight is 1024 - so there's no practical
457 * limitation from this.)
458 */
459 #define MIN_SHARES (1UL << 1)
460 #define MAX_SHARES (1UL << 18)
461 #endif
462
463 typedef int (*tg_visitor)(struct task_group *, void *);
464
465 extern int walk_tg_tree_from(struct task_group *from,
466 tg_visitor down, tg_visitor up, void *data);
467
468 /*
469 * Iterate the full tree, calling @down when first entering a node and @up when
470 * leaving it for the final time.
471 *
472 * Caller must hold rcu_lock or sufficient equivalent.
473 */
walk_tg_tree(tg_visitor down,tg_visitor up,void * data)474 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
475 {
476 return walk_tg_tree_from(&root_task_group, down, up, data);
477 }
478
479 extern int tg_nop(struct task_group *tg, void *data);
480
481 extern void free_fair_sched_group(struct task_group *tg);
482 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
483 extern void online_fair_sched_group(struct task_group *tg);
484 extern void unregister_fair_sched_group(struct task_group *tg);
485 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
486 struct sched_entity *se, int cpu,
487 struct sched_entity *parent);
488 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
489
490 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
491 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
492 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
493
494 extern void free_rt_sched_group(struct task_group *tg);
495 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
496 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
497 struct sched_rt_entity *rt_se, int cpu,
498 struct sched_rt_entity *parent);
499 extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
500 extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
501 extern long sched_group_rt_runtime(struct task_group *tg);
502 extern long sched_group_rt_period(struct task_group *tg);
503 extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
504
505 extern struct task_group *sched_create_group(struct task_group *parent);
506 extern void sched_online_group(struct task_group *tg,
507 struct task_group *parent);
508 extern void sched_destroy_group(struct task_group *tg);
509 extern void sched_offline_group(struct task_group *tg);
510
511 extern void sched_move_task(struct task_struct *tsk);
512
513 #ifdef CONFIG_FAIR_GROUP_SCHED
514 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
515
516 #ifdef CONFIG_SMP
517 extern void set_task_rq_fair(struct sched_entity *se,
518 struct cfs_rq *prev, struct cfs_rq *next);
519 #else /* !CONFIG_SMP */
set_task_rq_fair(struct sched_entity * se,struct cfs_rq * prev,struct cfs_rq * next)520 static inline void set_task_rq_fair(struct sched_entity *se,
521 struct cfs_rq *prev, struct cfs_rq *next) { }
522 #endif /* CONFIG_SMP */
523 #endif /* CONFIG_FAIR_GROUP_SCHED */
524
525 #else /* CONFIG_CGROUP_SCHED */
526
527 struct cfs_bandwidth { };
528
529 #endif /* CONFIG_CGROUP_SCHED */
530
531 /* CFS-related fields in a runqueue */
532 struct cfs_rq {
533 struct load_weight load;
534 unsigned int nr_running;
535 unsigned int h_nr_running; /* SCHED_{NORMAL,BATCH,IDLE} */
536 unsigned int idle_h_nr_running; /* SCHED_IDLE */
537
538 u64 exec_clock;
539 u64 min_vruntime;
540 #ifndef CONFIG_64BIT
541 u64 min_vruntime_copy;
542 #endif
543
544 struct rb_root_cached tasks_timeline;
545
546 /*
547 * 'curr' points to currently running entity on this cfs_rq.
548 * It is set to NULL otherwise (i.e when none are currently running).
549 */
550 struct sched_entity *curr;
551 struct sched_entity *next;
552 struct sched_entity *last;
553 struct sched_entity *skip;
554
555 #ifdef CONFIG_SCHED_DEBUG
556 unsigned int nr_spread_over;
557 #endif
558
559 #ifdef CONFIG_SMP
560 /*
561 * CFS load tracking
562 */
563 struct sched_avg avg;
564 #ifndef CONFIG_64BIT
565 u64 load_last_update_time_copy;
566 #endif
567 struct {
568 raw_spinlock_t lock ____cacheline_aligned;
569 int nr;
570 unsigned long load_avg;
571 unsigned long util_avg;
572 unsigned long runnable_avg;
573 } removed;
574
575 #ifdef CONFIG_FAIR_GROUP_SCHED
576 unsigned long tg_load_avg_contrib;
577 long propagate;
578 long prop_runnable_sum;
579
580 /*
581 * h_load = weight * f(tg)
582 *
583 * Where f(tg) is the recursive weight fraction assigned to
584 * this group.
585 */
586 unsigned long h_load;
587 u64 last_h_load_update;
588 struct sched_entity *h_load_next;
589 #endif /* CONFIG_FAIR_GROUP_SCHED */
590 #endif /* CONFIG_SMP */
591
592 #ifdef CONFIG_FAIR_GROUP_SCHED
593 struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */
594
595 /*
596 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
597 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
598 * (like users, containers etc.)
599 *
600 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
601 * This list is used during load balance.
602 */
603 int on_list;
604 struct list_head leaf_cfs_rq_list;
605 struct task_group *tg; /* group that "owns" this runqueue */
606
607 #ifdef CONFIG_CFS_BANDWIDTH
608 int runtime_enabled;
609 s64 runtime_remaining;
610
611 u64 throttled_clock;
612 u64 throttled_clock_pelt;
613 u64 throttled_clock_pelt_time;
614 int throttled;
615 int throttle_count;
616 struct list_head throttled_list;
617 #endif /* CONFIG_CFS_BANDWIDTH */
618
619 ANDROID_VENDOR_DATA_ARRAY(1, 16);
620 #endif /* CONFIG_FAIR_GROUP_SCHED */
621 };
622
rt_bandwidth_enabled(void)623 static inline int rt_bandwidth_enabled(void)
624 {
625 return sysctl_sched_rt_runtime >= 0;
626 }
627
628 /* RT IPI pull logic requires IRQ_WORK */
629 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
630 # define HAVE_RT_PUSH_IPI
631 #endif
632
633 /* Real-Time classes' related field in a runqueue: */
634 struct rt_rq {
635 struct rt_prio_array active;
636 unsigned int rt_nr_running;
637 unsigned int rr_nr_running;
638 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
639 struct {
640 int curr; /* highest queued rt task prio */
641 #ifdef CONFIG_SMP
642 int next; /* next highest */
643 #endif
644 } highest_prio;
645 #endif
646 #ifdef CONFIG_SMP
647 unsigned long rt_nr_migratory;
648 unsigned long rt_nr_total;
649 int overloaded;
650 struct plist_head pushable_tasks;
651
652 #endif /* CONFIG_SMP */
653 int rt_queued;
654
655 int rt_throttled;
656 u64 rt_time;
657 u64 rt_runtime;
658 /* Nests inside the rq lock: */
659 raw_spinlock_t rt_runtime_lock;
660
661 #ifdef CONFIG_RT_GROUP_SCHED
662 unsigned long rt_nr_boosted;
663
664 struct rq *rq;
665 struct task_group *tg;
666 #endif
667 };
668
rt_rq_is_runnable(struct rt_rq * rt_rq)669 static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
670 {
671 return rt_rq->rt_queued && rt_rq->rt_nr_running;
672 }
673
674 /* Deadline class' related fields in a runqueue */
675 struct dl_rq {
676 /* runqueue is an rbtree, ordered by deadline */
677 struct rb_root_cached root;
678
679 unsigned long dl_nr_running;
680
681 #ifdef CONFIG_SMP
682 /*
683 * Deadline values of the currently executing and the
684 * earliest ready task on this rq. Caching these facilitates
685 * the decision whether or not a ready but not running task
686 * should migrate somewhere else.
687 */
688 struct {
689 u64 curr;
690 u64 next;
691 } earliest_dl;
692
693 unsigned long dl_nr_migratory;
694 int overloaded;
695
696 /*
697 * Tasks on this rq that can be pushed away. They are kept in
698 * an rb-tree, ordered by tasks' deadlines, with caching
699 * of the leftmost (earliest deadline) element.
700 */
701 struct rb_root_cached pushable_dl_tasks_root;
702 #else
703 struct dl_bw dl_bw;
704 #endif
705 /*
706 * "Active utilization" for this runqueue: increased when a
707 * task wakes up (becomes TASK_RUNNING) and decreased when a
708 * task blocks
709 */
710 u64 running_bw;
711
712 /*
713 * Utilization of the tasks "assigned" to this runqueue (including
714 * the tasks that are in runqueue and the tasks that executed on this
715 * CPU and blocked). Increased when a task moves to this runqueue, and
716 * decreased when the task moves away (migrates, changes scheduling
717 * policy, or terminates).
718 * This is needed to compute the "inactive utilization" for the
719 * runqueue (inactive utilization = this_bw - running_bw).
720 */
721 u64 this_bw;
722 u64 extra_bw;
723
724 /*
725 * Inverse of the fraction of CPU utilization that can be reclaimed
726 * by the GRUB algorithm.
727 */
728 u64 bw_ratio;
729 };
730
731 #ifdef CONFIG_FAIR_GROUP_SCHED
732 /* An entity is a task if it doesn't "own" a runqueue */
733 #define entity_is_task(se) (!se->my_q)
734
se_update_runnable(struct sched_entity * se)735 static inline void se_update_runnable(struct sched_entity *se)
736 {
737 if (!entity_is_task(se))
738 se->runnable_weight = se->my_q->h_nr_running;
739 }
740
se_runnable(struct sched_entity * se)741 static inline long se_runnable(struct sched_entity *se)
742 {
743 if (entity_is_task(se))
744 return !!se->on_rq;
745 else
746 return se->runnable_weight;
747 }
748
749 #else
750 #define entity_is_task(se) 1
751
se_update_runnable(struct sched_entity * se)752 static inline void se_update_runnable(struct sched_entity *se) {}
753
se_runnable(struct sched_entity * se)754 static inline long se_runnable(struct sched_entity *se)
755 {
756 return !!se->on_rq;
757 }
758 #endif
759
760 #ifdef CONFIG_SMP
761 /*
762 * XXX we want to get rid of these helpers and use the full load resolution.
763 */
se_weight(struct sched_entity * se)764 static inline long se_weight(struct sched_entity *se)
765 {
766 return scale_load_down(se->load.weight);
767 }
768
769
sched_asym_prefer(int a,int b)770 static inline bool sched_asym_prefer(int a, int b)
771 {
772 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
773 }
774
775 struct perf_domain {
776 struct em_perf_domain *em_pd;
777 struct perf_domain *next;
778 struct rcu_head rcu;
779 };
780
781 /* Scheduling group status flags */
782 #define SG_OVERLOAD 0x1 /* More than one runnable task on a CPU. */
783 #define SG_OVERUTILIZED 0x2 /* One or more CPUs are over-utilized. */
784
785 /*
786 * We add the notion of a root-domain which will be used to define per-domain
787 * variables. Each exclusive cpuset essentially defines an island domain by
788 * fully partitioning the member CPUs from any other cpuset. Whenever a new
789 * exclusive cpuset is created, we also create and attach a new root-domain
790 * object.
791 *
792 */
793 struct root_domain {
794 atomic_t refcount;
795 atomic_t rto_count;
796 struct rcu_head rcu;
797 cpumask_var_t span;
798 cpumask_var_t online;
799
800 /*
801 * Indicate pullable load on at least one CPU, e.g:
802 * - More than one runnable task
803 * - Running task is misfit
804 */
805 int overload;
806
807 /* Indicate one or more cpus over-utilized (tipping point) */
808 int overutilized;
809
810 /*
811 * The bit corresponding to a CPU gets set here if such CPU has more
812 * than one runnable -deadline task (as it is below for RT tasks).
813 */
814 cpumask_var_t dlo_mask;
815 atomic_t dlo_count;
816 struct dl_bw dl_bw;
817 struct cpudl cpudl;
818
819 #ifdef HAVE_RT_PUSH_IPI
820 /*
821 * For IPI pull requests, loop across the rto_mask.
822 */
823 struct irq_work rto_push_work;
824 raw_spinlock_t rto_lock;
825 /* These are only updated and read within rto_lock */
826 int rto_loop;
827 int rto_cpu;
828 /* These atomics are updated outside of a lock */
829 atomic_t rto_loop_next;
830 atomic_t rto_loop_start;
831 #endif
832 /*
833 * The "RT overload" flag: it gets set if a CPU has more than
834 * one runnable RT task.
835 */
836 cpumask_var_t rto_mask;
837 struct cpupri cpupri;
838
839 unsigned long max_cpu_capacity;
840
841 /*
842 * NULL-terminated list of performance domains intersecting with the
843 * CPUs of the rd. Protected by RCU.
844 */
845 struct perf_domain __rcu *pd;
846
847 ANDROID_VENDOR_DATA_ARRAY(1, 4);
848
849 ANDROID_KABI_RESERVE(1);
850 ANDROID_KABI_RESERVE(2);
851 ANDROID_KABI_RESERVE(3);
852 ANDROID_KABI_RESERVE(4);
853 };
854
855 extern void init_defrootdomain(void);
856 extern int sched_init_domains(const struct cpumask *cpu_map);
857 extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
858 extern void sched_get_rd(struct root_domain *rd);
859 extern void sched_put_rd(struct root_domain *rd);
860
861 #ifdef HAVE_RT_PUSH_IPI
862 extern void rto_push_irq_work_func(struct irq_work *work);
863 #endif
864 extern struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu);
865 #endif /* CONFIG_SMP */
866
867 #ifdef CONFIG_UCLAMP_TASK
868 /*
869 * struct uclamp_bucket - Utilization clamp bucket
870 * @value: utilization clamp value for tasks on this clamp bucket
871 * @tasks: number of RUNNABLE tasks on this clamp bucket
872 *
873 * Keep track of how many tasks are RUNNABLE for a given utilization
874 * clamp value.
875 */
876 struct uclamp_bucket {
877 unsigned long value : bits_per(SCHED_CAPACITY_SCALE);
878 unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE);
879 };
880
881 /*
882 * struct uclamp_rq - rq's utilization clamp
883 * @value: currently active clamp values for a rq
884 * @bucket: utilization clamp buckets affecting a rq
885 *
886 * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values.
887 * A clamp value is affecting a rq when there is at least one task RUNNABLE
888 * (or actually running) with that value.
889 *
890 * There are up to UCLAMP_CNT possible different clamp values, currently there
891 * are only two: minimum utilization and maximum utilization.
892 *
893 * All utilization clamping values are MAX aggregated, since:
894 * - for util_min: we want to run the CPU at least at the max of the minimum
895 * utilization required by its currently RUNNABLE tasks.
896 * - for util_max: we want to allow the CPU to run up to the max of the
897 * maximum utilization allowed by its currently RUNNABLE tasks.
898 *
899 * Since on each system we expect only a limited number of different
900 * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track
901 * the metrics required to compute all the per-rq utilization clamp values.
902 */
903 struct uclamp_rq {
904 unsigned int value;
905 struct uclamp_bucket bucket[UCLAMP_BUCKETS];
906 };
907
908 DECLARE_STATIC_KEY_FALSE(sched_uclamp_used);
909 #endif /* CONFIG_UCLAMP_TASK */
910
911 /*
912 * This is the main, per-CPU runqueue data structure.
913 *
914 * Locking rule: those places that want to lock multiple runqueues
915 * (such as the load balancing or the thread migration code), lock
916 * acquire operations must be ordered by ascending &runqueue.
917 */
918 struct rq {
919 /* runqueue lock: */
920 raw_spinlock_t lock;
921
922 /*
923 * nr_running and cpu_load should be in the same cacheline because
924 * remote CPUs use both these fields when doing load calculation.
925 */
926 unsigned int nr_running;
927 #ifdef CONFIG_NUMA_BALANCING
928 unsigned int nr_numa_running;
929 unsigned int nr_preferred_running;
930 unsigned int numa_migrate_on;
931 #endif
932 #ifdef CONFIG_NO_HZ_COMMON
933 #ifdef CONFIG_SMP
934 unsigned long last_blocked_load_update_tick;
935 unsigned int has_blocked_load;
936 call_single_data_t nohz_csd;
937 #endif /* CONFIG_SMP */
938 unsigned int nohz_tick_stopped;
939 atomic_t nohz_flags;
940 #endif /* CONFIG_NO_HZ_COMMON */
941
942 #ifdef CONFIG_SMP
943 unsigned int ttwu_pending;
944 #endif
945 u64 nr_switches;
946
947 #ifdef CONFIG_UCLAMP_TASK
948 /* Utilization clamp values based on CPU's RUNNABLE tasks */
949 struct uclamp_rq uclamp[UCLAMP_CNT] ____cacheline_aligned;
950 unsigned int uclamp_flags;
951 #define UCLAMP_FLAG_IDLE 0x01
952 #endif
953
954 struct cfs_rq cfs;
955 struct rt_rq rt;
956 struct dl_rq dl;
957
958 #ifdef CONFIG_FAIR_GROUP_SCHED
959 /* list of leaf cfs_rq on this CPU: */
960 struct list_head leaf_cfs_rq_list;
961 struct list_head *tmp_alone_branch;
962 #endif /* CONFIG_FAIR_GROUP_SCHED */
963
964 /*
965 * This is part of a global counter where only the total sum
966 * over all CPUs matters. A task can increase this counter on
967 * one CPU and if it got migrated afterwards it may decrease
968 * it on another CPU. Always updated under the runqueue lock:
969 */
970 unsigned long nr_uninterruptible;
971
972 struct task_struct __rcu *curr;
973 struct task_struct *idle;
974 struct task_struct *stop;
975 unsigned long next_balance;
976 struct mm_struct *prev_mm;
977
978 unsigned int clock_update_flags;
979 u64 clock;
980 /* Ensure that all clocks are in the same cache line */
981 u64 clock_task ____cacheline_aligned;
982 u64 clock_pelt;
983 unsigned long lost_idle_time;
984
985 atomic_t nr_iowait;
986
987 #ifdef CONFIG_MEMBARRIER
988 int membarrier_state;
989 #endif
990
991 #ifdef CONFIG_SMP
992 struct root_domain *rd;
993 struct sched_domain __rcu *sd;
994
995 unsigned long cpu_capacity;
996 unsigned long cpu_capacity_orig;
997
998 struct callback_head *balance_callback;
999
1000 unsigned char nohz_idle_balance;
1001 unsigned char idle_balance;
1002
1003 unsigned long misfit_task_load;
1004
1005 /* For active balancing */
1006 int active_balance;
1007 int push_cpu;
1008 struct cpu_stop_work active_balance_work;
1009
1010 /* CPU of this runqueue: */
1011 int cpu;
1012 int online;
1013
1014 struct list_head cfs_tasks;
1015
1016 struct sched_avg avg_rt;
1017 struct sched_avg avg_dl;
1018 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
1019 struct sched_avg avg_irq;
1020 #endif
1021 #ifdef CONFIG_SCHED_THERMAL_PRESSURE
1022 struct sched_avg avg_thermal;
1023 #endif
1024 u64 idle_stamp;
1025 u64 avg_idle;
1026
1027 /* This is used to determine avg_idle's max value */
1028 u64 max_idle_balance_cost;
1029 #endif /* CONFIG_SMP */
1030
1031 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1032 u64 prev_irq_time;
1033 #endif
1034 #ifdef CONFIG_PARAVIRT
1035 u64 prev_steal_time;
1036 #endif
1037 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
1038 u64 prev_steal_time_rq;
1039 #endif
1040
1041 /* calc_load related fields */
1042 unsigned long calc_load_update;
1043 long calc_load_active;
1044
1045 #ifdef CONFIG_SCHED_HRTICK
1046 #ifdef CONFIG_SMP
1047 call_single_data_t hrtick_csd;
1048 #endif
1049 struct hrtimer hrtick_timer;
1050 ktime_t hrtick_time;
1051 #endif
1052
1053 #ifdef CONFIG_SCHEDSTATS
1054 /* latency stats */
1055 struct sched_info rq_sched_info;
1056 unsigned long long rq_cpu_time;
1057 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
1058
1059 /* sys_sched_yield() stats */
1060 unsigned int yld_count;
1061
1062 /* schedule() stats */
1063 unsigned int sched_count;
1064 unsigned int sched_goidle;
1065
1066 /* try_to_wake_up() stats */
1067 unsigned int ttwu_count;
1068 unsigned int ttwu_local;
1069 #endif
1070
1071 #ifdef CONFIG_HOTPLUG_CPU
1072 struct cpu_stop_work drain;
1073 struct cpu_stop_done drain_done;
1074 #endif
1075
1076 #ifdef CONFIG_CPU_IDLE
1077 /* Must be inspected within a rcu lock section */
1078 struct cpuidle_state *idle_state;
1079 #endif
1080
1081 ANDROID_VENDOR_DATA_ARRAY(1, 96);
1082 ANDROID_OEM_DATA_ARRAY(1, 16);
1083
1084 ANDROID_KABI_RESERVE(1);
1085 ANDROID_KABI_RESERVE(2);
1086 ANDROID_KABI_RESERVE(3);
1087 ANDROID_KABI_RESERVE(4);
1088 };
1089
1090 #ifdef CONFIG_FAIR_GROUP_SCHED
1091
1092 /* CPU runqueue to which this cfs_rq is attached */
rq_of(struct cfs_rq * cfs_rq)1093 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1094 {
1095 return cfs_rq->rq;
1096 }
1097
1098 #else
1099
rq_of(struct cfs_rq * cfs_rq)1100 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1101 {
1102 return container_of(cfs_rq, struct rq, cfs);
1103 }
1104 #endif
1105
cpu_of(struct rq * rq)1106 static inline int cpu_of(struct rq *rq)
1107 {
1108 #ifdef CONFIG_SMP
1109 return rq->cpu;
1110 #else
1111 return 0;
1112 #endif
1113 }
1114
1115
1116 #ifdef CONFIG_SCHED_SMT
1117 extern void __update_idle_core(struct rq *rq);
1118
update_idle_core(struct rq * rq)1119 static inline void update_idle_core(struct rq *rq)
1120 {
1121 if (static_branch_unlikely(&sched_smt_present))
1122 __update_idle_core(rq);
1123 }
1124
1125 #else
update_idle_core(struct rq * rq)1126 static inline void update_idle_core(struct rq *rq) { }
1127 #endif
1128
1129 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1130
1131 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
1132 #define this_rq() this_cpu_ptr(&runqueues)
1133 #define task_rq(p) cpu_rq(task_cpu(p))
1134 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
1135 #define raw_rq() raw_cpu_ptr(&runqueues)
1136
1137 extern void update_rq_clock(struct rq *rq);
1138
__rq_clock_broken(struct rq * rq)1139 static inline u64 __rq_clock_broken(struct rq *rq)
1140 {
1141 return READ_ONCE(rq->clock);
1142 }
1143
1144 /*
1145 * rq::clock_update_flags bits
1146 *
1147 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
1148 * call to __schedule(). This is an optimisation to avoid
1149 * neighbouring rq clock updates.
1150 *
1151 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
1152 * in effect and calls to update_rq_clock() are being ignored.
1153 *
1154 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
1155 * made to update_rq_clock() since the last time rq::lock was pinned.
1156 *
1157 * If inside of __schedule(), clock_update_flags will have been
1158 * shifted left (a left shift is a cheap operation for the fast path
1159 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
1160 *
1161 * if (rq-clock_update_flags >= RQCF_UPDATED)
1162 *
1163 * to check if %RQCF_UPADTED is set. It'll never be shifted more than
1164 * one position though, because the next rq_unpin_lock() will shift it
1165 * back.
1166 */
1167 #define RQCF_REQ_SKIP 0x01
1168 #define RQCF_ACT_SKIP 0x02
1169 #define RQCF_UPDATED 0x04
1170
assert_clock_updated(struct rq * rq)1171 static inline void assert_clock_updated(struct rq *rq)
1172 {
1173 /*
1174 * The only reason for not seeing a clock update since the
1175 * last rq_pin_lock() is if we're currently skipping updates.
1176 */
1177 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
1178 }
1179
rq_clock(struct rq * rq)1180 static inline u64 rq_clock(struct rq *rq)
1181 {
1182 lockdep_assert_held(&rq->lock);
1183 assert_clock_updated(rq);
1184
1185 return rq->clock;
1186 }
1187
rq_clock_task(struct rq * rq)1188 static inline u64 rq_clock_task(struct rq *rq)
1189 {
1190 lockdep_assert_held(&rq->lock);
1191 assert_clock_updated(rq);
1192
1193 return rq->clock_task;
1194 }
1195
1196 #ifdef CONFIG_SMP
1197 DECLARE_PER_CPU(u64, clock_task_mult);
1198
rq_clock_task_mult(struct rq * rq)1199 static inline u64 rq_clock_task_mult(struct rq *rq)
1200 {
1201 lockdep_assert_held(&rq->lock);
1202 assert_clock_updated(rq);
1203
1204 return per_cpu(clock_task_mult, cpu_of(rq));
1205 }
1206 #else
rq_clock_task_mult(struct rq * rq)1207 static inline u64 rq_clock_task_mult(struct rq *rq)
1208 {
1209 return rq_clock_task(rq);
1210 }
1211 #endif
1212
1213 /**
1214 * By default the decay is the default pelt decay period.
1215 * The decay shift can change the decay period in
1216 * multiples of 32.
1217 * Decay shift Decay period(ms)
1218 * 0 32
1219 * 1 64
1220 * 2 128
1221 * 3 256
1222 * 4 512
1223 */
1224 extern int sched_thermal_decay_shift;
1225
rq_clock_thermal(struct rq * rq)1226 static inline u64 rq_clock_thermal(struct rq *rq)
1227 {
1228 return rq_clock_task(rq) >> sched_thermal_decay_shift;
1229 }
1230
rq_clock_skip_update(struct rq * rq)1231 static inline void rq_clock_skip_update(struct rq *rq)
1232 {
1233 lockdep_assert_held(&rq->lock);
1234 rq->clock_update_flags |= RQCF_REQ_SKIP;
1235 }
1236
1237 /*
1238 * See rt task throttling, which is the only time a skip
1239 * request is cancelled.
1240 */
rq_clock_cancel_skipupdate(struct rq * rq)1241 static inline void rq_clock_cancel_skipupdate(struct rq *rq)
1242 {
1243 lockdep_assert_held(&rq->lock);
1244 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
1245 }
1246
1247 struct rq_flags {
1248 unsigned long flags;
1249 struct pin_cookie cookie;
1250 #ifdef CONFIG_SCHED_DEBUG
1251 /*
1252 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
1253 * current pin context is stashed here in case it needs to be
1254 * restored in rq_repin_lock().
1255 */
1256 unsigned int clock_update_flags;
1257 #endif
1258 };
1259
1260 /*
1261 * Lockdep annotation that avoids accidental unlocks; it's like a
1262 * sticky/continuous lockdep_assert_held().
1263 *
1264 * This avoids code that has access to 'struct rq *rq' (basically everything in
1265 * the scheduler) from accidentally unlocking the rq if they do not also have a
1266 * copy of the (on-stack) 'struct rq_flags rf'.
1267 *
1268 * Also see Documentation/locking/lockdep-design.rst.
1269 */
rq_pin_lock(struct rq * rq,struct rq_flags * rf)1270 static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
1271 {
1272 rf->cookie = lockdep_pin_lock(&rq->lock);
1273
1274 #ifdef CONFIG_SCHED_DEBUG
1275 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
1276 rf->clock_update_flags = 0;
1277 #endif
1278 }
1279
rq_unpin_lock(struct rq * rq,struct rq_flags * rf)1280 static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
1281 {
1282 #ifdef CONFIG_SCHED_DEBUG
1283 if (rq->clock_update_flags > RQCF_ACT_SKIP)
1284 rf->clock_update_flags = RQCF_UPDATED;
1285 #endif
1286
1287 lockdep_unpin_lock(&rq->lock, rf->cookie);
1288 }
1289
rq_repin_lock(struct rq * rq,struct rq_flags * rf)1290 static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
1291 {
1292 lockdep_repin_lock(&rq->lock, rf->cookie);
1293
1294 #ifdef CONFIG_SCHED_DEBUG
1295 /*
1296 * Restore the value we stashed in @rf for this pin context.
1297 */
1298 rq->clock_update_flags |= rf->clock_update_flags;
1299 #endif
1300 }
1301
1302 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1303 __acquires(rq->lock);
1304
1305 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1306 __acquires(p->pi_lock)
1307 __acquires(rq->lock);
1308
__task_rq_unlock(struct rq * rq,struct rq_flags * rf)1309 static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
1310 __releases(rq->lock)
1311 {
1312 rq_unpin_lock(rq, rf);
1313 raw_spin_unlock(&rq->lock);
1314 }
1315
1316 static inline void
task_rq_unlock(struct rq * rq,struct task_struct * p,struct rq_flags * rf)1317 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1318 __releases(rq->lock)
1319 __releases(p->pi_lock)
1320 {
1321 rq_unpin_lock(rq, rf);
1322 raw_spin_unlock(&rq->lock);
1323 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
1324 }
1325
1326 static inline void
rq_lock_irqsave(struct rq * rq,struct rq_flags * rf)1327 rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1328 __acquires(rq->lock)
1329 {
1330 raw_spin_lock_irqsave(&rq->lock, rf->flags);
1331 rq_pin_lock(rq, rf);
1332 }
1333
1334 static inline void
rq_lock_irq(struct rq * rq,struct rq_flags * rf)1335 rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1336 __acquires(rq->lock)
1337 {
1338 raw_spin_lock_irq(&rq->lock);
1339 rq_pin_lock(rq, rf);
1340 }
1341
1342 static inline void
rq_lock(struct rq * rq,struct rq_flags * rf)1343 rq_lock(struct rq *rq, struct rq_flags *rf)
1344 __acquires(rq->lock)
1345 {
1346 raw_spin_lock(&rq->lock);
1347 rq_pin_lock(rq, rf);
1348 }
1349
1350 static inline void
rq_relock(struct rq * rq,struct rq_flags * rf)1351 rq_relock(struct rq *rq, struct rq_flags *rf)
1352 __acquires(rq->lock)
1353 {
1354 raw_spin_lock(&rq->lock);
1355 rq_repin_lock(rq, rf);
1356 }
1357
1358 static inline void
rq_unlock_irqrestore(struct rq * rq,struct rq_flags * rf)1359 rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1360 __releases(rq->lock)
1361 {
1362 rq_unpin_lock(rq, rf);
1363 raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
1364 }
1365
1366 static inline void
rq_unlock_irq(struct rq * rq,struct rq_flags * rf)1367 rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1368 __releases(rq->lock)
1369 {
1370 rq_unpin_lock(rq, rf);
1371 raw_spin_unlock_irq(&rq->lock);
1372 }
1373
1374 static inline void
rq_unlock(struct rq * rq,struct rq_flags * rf)1375 rq_unlock(struct rq *rq, struct rq_flags *rf)
1376 __releases(rq->lock)
1377 {
1378 rq_unpin_lock(rq, rf);
1379 raw_spin_unlock(&rq->lock);
1380 }
1381
1382 static inline struct rq *
this_rq_lock_irq(struct rq_flags * rf)1383 this_rq_lock_irq(struct rq_flags *rf)
1384 __acquires(rq->lock)
1385 {
1386 struct rq *rq;
1387
1388 local_irq_disable();
1389 rq = this_rq();
1390 rq_lock(rq, rf);
1391 return rq;
1392 }
1393
1394 #ifdef CONFIG_NUMA
1395 enum numa_topology_type {
1396 NUMA_DIRECT,
1397 NUMA_GLUELESS_MESH,
1398 NUMA_BACKPLANE,
1399 };
1400 extern enum numa_topology_type sched_numa_topology_type;
1401 extern int sched_max_numa_distance;
1402 extern bool find_numa_distance(int distance);
1403 extern void sched_init_numa(void);
1404 extern void sched_domains_numa_masks_set(unsigned int cpu);
1405 extern void sched_domains_numa_masks_clear(unsigned int cpu);
1406 extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu);
1407 #else
sched_init_numa(void)1408 static inline void sched_init_numa(void) { }
sched_domains_numa_masks_set(unsigned int cpu)1409 static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
sched_domains_numa_masks_clear(unsigned int cpu)1410 static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
sched_numa_find_closest(const struct cpumask * cpus,int cpu)1411 static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1412 {
1413 return nr_cpu_ids;
1414 }
1415 #endif
1416
1417 #ifdef CONFIG_NUMA_BALANCING
1418 /* The regions in numa_faults array from task_struct */
1419 enum numa_faults_stats {
1420 NUMA_MEM = 0,
1421 NUMA_CPU,
1422 NUMA_MEMBUF,
1423 NUMA_CPUBUF
1424 };
1425 extern void sched_setnuma(struct task_struct *p, int node);
1426 extern int migrate_task_to(struct task_struct *p, int cpu);
1427 extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p);
1428 #else
1429 static inline void
init_numa_balancing(unsigned long clone_flags,struct task_struct * p)1430 init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
1431 {
1432 }
1433 #endif /* CONFIG_NUMA_BALANCING */
1434
1435 #ifdef CONFIG_SMP
1436
1437 extern int migrate_swap(struct task_struct *p, struct task_struct *t,
1438 int cpu, int scpu);
1439 static inline void
queue_balance_callback(struct rq * rq,struct callback_head * head,void (* func)(struct rq * rq))1440 queue_balance_callback(struct rq *rq,
1441 struct callback_head *head,
1442 void (*func)(struct rq *rq))
1443 {
1444 lockdep_assert_held(&rq->lock);
1445
1446 if (unlikely(head->next))
1447 return;
1448
1449 head->func = (void (*)(struct callback_head *))func;
1450 head->next = rq->balance_callback;
1451 rq->balance_callback = head;
1452 }
1453
1454 #define rcu_dereference_check_sched_domain(p) \
1455 rcu_dereference_check((p), \
1456 lockdep_is_held(&sched_domains_mutex))
1457
1458 /*
1459 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1460 * See destroy_sched_domains: call_rcu for details.
1461 *
1462 * The domain tree of any CPU may only be accessed from within
1463 * preempt-disabled sections.
1464 */
1465 #define for_each_domain(cpu, __sd) \
1466 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1467 __sd; __sd = __sd->parent)
1468
1469 /**
1470 * highest_flag_domain - Return highest sched_domain containing flag.
1471 * @cpu: The CPU whose highest level of sched domain is to
1472 * be returned.
1473 * @flag: The flag to check for the highest sched_domain
1474 * for the given CPU.
1475 *
1476 * Returns the highest sched_domain of a CPU which contains the given flag.
1477 */
highest_flag_domain(int cpu,int flag)1478 static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1479 {
1480 struct sched_domain *sd, *hsd = NULL;
1481
1482 for_each_domain(cpu, sd) {
1483 if (!(sd->flags & flag))
1484 break;
1485 hsd = sd;
1486 }
1487
1488 return hsd;
1489 }
1490
lowest_flag_domain(int cpu,int flag)1491 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1492 {
1493 struct sched_domain *sd;
1494
1495 for_each_domain(cpu, sd) {
1496 if (sd->flags & flag)
1497 break;
1498 }
1499
1500 return sd;
1501 }
1502
1503 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
1504 DECLARE_PER_CPU(int, sd_llc_size);
1505 DECLARE_PER_CPU(int, sd_llc_id);
1506 DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
1507 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
1508 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
1509 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
1510 extern struct static_key_false sched_asym_cpucapacity;
1511
1512 struct sched_group_capacity {
1513 atomic_t ref;
1514 /*
1515 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
1516 * for a single CPU.
1517 */
1518 unsigned long capacity;
1519 unsigned long min_capacity; /* Min per-CPU capacity in group */
1520 unsigned long max_capacity; /* Max per-CPU capacity in group */
1521 unsigned long next_update;
1522 int imbalance; /* XXX unrelated to capacity but shared group state */
1523
1524 #ifdef CONFIG_SCHED_DEBUG
1525 int id;
1526 #endif
1527
1528 unsigned long cpumask[]; /* Balance mask */
1529 };
1530
1531 struct sched_group {
1532 struct sched_group *next; /* Must be a circular list */
1533 atomic_t ref;
1534
1535 unsigned int group_weight;
1536 struct sched_group_capacity *sgc;
1537 int asym_prefer_cpu; /* CPU of highest priority in group */
1538
1539 /*
1540 * The CPUs this group covers.
1541 *
1542 * NOTE: this field is variable length. (Allocated dynamically
1543 * by attaching extra space to the end of the structure,
1544 * depending on how many CPUs the kernel has booted up with)
1545 */
1546 unsigned long cpumask[];
1547 };
1548
sched_group_span(struct sched_group * sg)1549 static inline struct cpumask *sched_group_span(struct sched_group *sg)
1550 {
1551 return to_cpumask(sg->cpumask);
1552 }
1553
1554 /*
1555 * See build_balance_mask().
1556 */
group_balance_mask(struct sched_group * sg)1557 static inline struct cpumask *group_balance_mask(struct sched_group *sg)
1558 {
1559 return to_cpumask(sg->sgc->cpumask);
1560 }
1561
1562 /**
1563 * group_first_cpu - Returns the first CPU in the cpumask of a sched_group.
1564 * @group: The group whose first CPU is to be returned.
1565 */
group_first_cpu(struct sched_group * group)1566 static inline unsigned int group_first_cpu(struct sched_group *group)
1567 {
1568 return cpumask_first(sched_group_span(group));
1569 }
1570
1571 extern int group_balance_cpu(struct sched_group *sg);
1572
1573 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1574 void register_sched_domain_sysctl(void);
1575 void dirty_sched_domain_sysctl(int cpu);
1576 void unregister_sched_domain_sysctl(void);
1577 #else
register_sched_domain_sysctl(void)1578 static inline void register_sched_domain_sysctl(void)
1579 {
1580 }
dirty_sched_domain_sysctl(int cpu)1581 static inline void dirty_sched_domain_sysctl(int cpu)
1582 {
1583 }
unregister_sched_domain_sysctl(void)1584 static inline void unregister_sched_domain_sysctl(void)
1585 {
1586 }
1587 #endif
1588
1589 extern void flush_smp_call_function_from_idle(void);
1590
1591 #else /* !CONFIG_SMP: */
flush_smp_call_function_from_idle(void)1592 static inline void flush_smp_call_function_from_idle(void) { }
1593 #endif
1594
1595 #include "stats.h"
1596 #include "autogroup.h"
1597
1598 #ifdef CONFIG_CGROUP_SCHED
1599
1600 /*
1601 * Return the group to which this tasks belongs.
1602 *
1603 * We cannot use task_css() and friends because the cgroup subsystem
1604 * changes that value before the cgroup_subsys::attach() method is called,
1605 * therefore we cannot pin it and might observe the wrong value.
1606 *
1607 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1608 * core changes this before calling sched_move_task().
1609 *
1610 * Instead we use a 'copy' which is updated from sched_move_task() while
1611 * holding both task_struct::pi_lock and rq::lock.
1612 */
task_group(struct task_struct * p)1613 static inline struct task_group *task_group(struct task_struct *p)
1614 {
1615 return p->sched_task_group;
1616 }
1617
1618 /* 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)1619 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1620 {
1621 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1622 struct task_group *tg = task_group(p);
1623 #endif
1624
1625 #ifdef CONFIG_FAIR_GROUP_SCHED
1626 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
1627 p->se.cfs_rq = tg->cfs_rq[cpu];
1628 p->se.parent = tg->se[cpu];
1629 #endif
1630
1631 #ifdef CONFIG_RT_GROUP_SCHED
1632 p->rt.rt_rq = tg->rt_rq[cpu];
1633 p->rt.parent = tg->rt_se[cpu];
1634 #endif
1635 }
1636
1637 #else /* CONFIG_CGROUP_SCHED */
1638
set_task_rq(struct task_struct * p,unsigned int cpu)1639 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
task_group(struct task_struct * p)1640 static inline struct task_group *task_group(struct task_struct *p)
1641 {
1642 return NULL;
1643 }
1644
1645 #endif /* CONFIG_CGROUP_SCHED */
1646
__set_task_cpu(struct task_struct * p,unsigned int cpu)1647 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1648 {
1649 set_task_rq(p, cpu);
1650 #ifdef CONFIG_SMP
1651 /*
1652 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1653 * successfully executed on another CPU. We must ensure that updates of
1654 * per-task data have been completed by this moment.
1655 */
1656 smp_wmb();
1657 #ifdef CONFIG_THREAD_INFO_IN_TASK
1658 WRITE_ONCE(p->cpu, cpu);
1659 #else
1660 WRITE_ONCE(task_thread_info(p)->cpu, cpu);
1661 #endif
1662 p->wake_cpu = cpu;
1663 #endif
1664 }
1665
1666 /*
1667 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1668 */
1669 #ifdef CONFIG_SCHED_DEBUG
1670 # include <linux/static_key.h>
1671 # define const_debug __read_mostly
1672 #else
1673 # define const_debug const
1674 #endif
1675
1676 #define SCHED_FEAT(name, enabled) \
1677 __SCHED_FEAT_##name ,
1678
1679 enum {
1680 #include "features.h"
1681 __SCHED_FEAT_NR,
1682 };
1683
1684 #undef SCHED_FEAT
1685
1686 #ifdef CONFIG_SCHED_DEBUG
1687
1688 /*
1689 * To support run-time toggling of sched features, all the translation units
1690 * (but core.c) reference the sysctl_sched_features defined in core.c.
1691 */
1692 extern const_debug unsigned int sysctl_sched_features;
1693
1694 #ifdef CONFIG_JUMP_LABEL
1695 #define SCHED_FEAT(name, enabled) \
1696 static __always_inline bool static_branch_##name(struct static_key *key) \
1697 { \
1698 return static_key_##enabled(key); \
1699 }
1700
1701 #include "features.h"
1702 #undef SCHED_FEAT
1703
1704 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
1705 extern const char * const sched_feat_names[__SCHED_FEAT_NR];
1706
1707 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1708
1709 #else /* !CONFIG_JUMP_LABEL */
1710
1711 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1712
1713 #endif /* CONFIG_JUMP_LABEL */
1714
1715 #else /* !SCHED_DEBUG */
1716
1717 /*
1718 * Each translation unit has its own copy of sysctl_sched_features to allow
1719 * constants propagation at compile time and compiler optimization based on
1720 * features default.
1721 */
1722 #define SCHED_FEAT(name, enabled) \
1723 (1UL << __SCHED_FEAT_##name) * enabled |
1724 static const_debug __maybe_unused unsigned int sysctl_sched_features =
1725 #include "features.h"
1726 0;
1727 #undef SCHED_FEAT
1728
1729 #define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1730
1731 #endif /* SCHED_DEBUG */
1732
1733 extern struct static_key_false sched_numa_balancing;
1734 extern struct static_key_false sched_schedstats;
1735
global_rt_period(void)1736 static inline u64 global_rt_period(void)
1737 {
1738 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1739 }
1740
global_rt_runtime(void)1741 static inline u64 global_rt_runtime(void)
1742 {
1743 if (sysctl_sched_rt_runtime < 0)
1744 return RUNTIME_INF;
1745
1746 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1747 }
1748
task_current(struct rq * rq,struct task_struct * p)1749 static inline int task_current(struct rq *rq, struct task_struct *p)
1750 {
1751 return rq->curr == p;
1752 }
1753
task_running(struct rq * rq,struct task_struct * p)1754 static inline int task_running(struct rq *rq, struct task_struct *p)
1755 {
1756 #ifdef CONFIG_SMP
1757 return p->on_cpu;
1758 #else
1759 return task_current(rq, p);
1760 #endif
1761 }
1762
task_on_rq_queued(struct task_struct * p)1763 static inline int task_on_rq_queued(struct task_struct *p)
1764 {
1765 return p->on_rq == TASK_ON_RQ_QUEUED;
1766 }
1767
task_on_rq_migrating(struct task_struct * p)1768 static inline int task_on_rq_migrating(struct task_struct *p)
1769 {
1770 return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
1771 }
1772
1773 /*
1774 * wake flags
1775 */
1776 #define WF_SYNC 0x01 /* Waker goes to sleep after wakeup */
1777 #define WF_FORK 0x02 /* Child wakeup after fork */
1778 #define WF_MIGRATED 0x04 /* Internal use, task got migrated */
1779 #define WF_ON_CPU 0x08 /* Wakee is on_cpu */
1780 #define WF_ANDROID_VENDOR 0x1000 /* Vendor specific for Android */
1781
1782 /*
1783 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1784 * of tasks with abnormal "nice" values across CPUs the contribution that
1785 * each task makes to its run queue's load is weighted according to its
1786 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1787 * scaled version of the new time slice allocation that they receive on time
1788 * slice expiry etc.
1789 */
1790
1791 #define WEIGHT_IDLEPRIO 3
1792 #define WMULT_IDLEPRIO 1431655765
1793
1794 extern const int sched_prio_to_weight[40];
1795 extern const u32 sched_prio_to_wmult[40];
1796
1797 /*
1798 * {de,en}queue flags:
1799 *
1800 * DEQUEUE_SLEEP - task is no longer runnable
1801 * ENQUEUE_WAKEUP - task just became runnable
1802 *
1803 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1804 * are in a known state which allows modification. Such pairs
1805 * should preserve as much state as possible.
1806 *
1807 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1808 * in the runqueue.
1809 *
1810 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1811 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
1812 * ENQUEUE_MIGRATED - the task was migrated during wakeup
1813 *
1814 */
1815
1816 #define DEQUEUE_SLEEP 0x01
1817 #define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */
1818 #define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */
1819 #define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */
1820
1821 #define ENQUEUE_WAKEUP 0x01
1822 #define ENQUEUE_RESTORE 0x02
1823 #define ENQUEUE_MOVE 0x04
1824 #define ENQUEUE_NOCLOCK 0x08
1825
1826 #define ENQUEUE_HEAD 0x10
1827 #define ENQUEUE_REPLENISH 0x20
1828 #ifdef CONFIG_SMP
1829 #define ENQUEUE_MIGRATED 0x40
1830 #else
1831 #define ENQUEUE_MIGRATED 0x00
1832 #endif
1833
1834 #define ENQUEUE_WAKEUP_SYNC 0x80
1835
1836 #define RETRY_TASK ((void *)-1UL)
1837
1838 struct sched_class {
1839
1840 #ifdef CONFIG_UCLAMP_TASK
1841 int uclamp_enabled;
1842 #endif
1843
1844 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1845 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1846 void (*yield_task) (struct rq *rq);
1847 bool (*yield_to_task)(struct rq *rq, struct task_struct *p);
1848
1849 void (*check_preempt_curr)(struct rq *rq, struct task_struct *p, int flags);
1850
1851 struct task_struct *(*pick_next_task)(struct rq *rq);
1852
1853 void (*put_prev_task)(struct rq *rq, struct task_struct *p);
1854 void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first);
1855
1856 #ifdef CONFIG_SMP
1857 int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
1858 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
1859 void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
1860
1861 void (*task_woken)(struct rq *this_rq, struct task_struct *task);
1862
1863 void (*set_cpus_allowed)(struct task_struct *p,
1864 const struct cpumask *newmask);
1865
1866 void (*rq_online)(struct rq *rq);
1867 void (*rq_offline)(struct rq *rq);
1868 #endif
1869
1870 void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
1871 void (*task_fork)(struct task_struct *p);
1872 void (*task_dead)(struct task_struct *p);
1873
1874 /*
1875 * The switched_from() call is allowed to drop rq->lock, therefore we
1876 * cannot assume the switched_from/switched_to pair is serliazed by
1877 * rq->lock. They are however serialized by p->pi_lock.
1878 */
1879 void (*switched_from)(struct rq *this_rq, struct task_struct *task);
1880 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1881 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1882 int oldprio);
1883
1884 unsigned int (*get_rr_interval)(struct rq *rq,
1885 struct task_struct *task);
1886
1887 void (*update_curr)(struct rq *rq);
1888
1889 #define TASK_SET_GROUP 0
1890 #define TASK_MOVE_GROUP 1
1891
1892 #ifdef CONFIG_FAIR_GROUP_SCHED
1893 void (*task_change_group)(struct task_struct *p, int type);
1894 #endif
1895 } __aligned(STRUCT_ALIGNMENT); /* STRUCT_ALIGN(), vmlinux.lds.h */
1896
put_prev_task(struct rq * rq,struct task_struct * prev)1897 static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1898 {
1899 WARN_ON_ONCE(rq->curr != prev);
1900 prev->sched_class->put_prev_task(rq, prev);
1901 }
1902
set_next_task(struct rq * rq,struct task_struct * next)1903 static inline void set_next_task(struct rq *rq, struct task_struct *next)
1904 {
1905 WARN_ON_ONCE(rq->curr != next);
1906 next->sched_class->set_next_task(rq, next, false);
1907 }
1908
1909 /* Defined in include/asm-generic/vmlinux.lds.h */
1910 extern struct sched_class __begin_sched_classes[];
1911 extern struct sched_class __end_sched_classes[];
1912
1913 #define sched_class_highest (__end_sched_classes - 1)
1914 #define sched_class_lowest (__begin_sched_classes - 1)
1915
1916 #define for_class_range(class, _from, _to) \
1917 for (class = (_from); class != (_to); class--)
1918
1919 #define for_each_class(class) \
1920 for_class_range(class, sched_class_highest, sched_class_lowest)
1921
1922 extern const struct sched_class stop_sched_class;
1923 extern const struct sched_class dl_sched_class;
1924 extern const struct sched_class rt_sched_class;
1925 extern const struct sched_class fair_sched_class;
1926 extern const struct sched_class idle_sched_class;
1927
sched_stop_runnable(struct rq * rq)1928 static inline bool sched_stop_runnable(struct rq *rq)
1929 {
1930 return rq->stop && task_on_rq_queued(rq->stop);
1931 }
1932
sched_dl_runnable(struct rq * rq)1933 static inline bool sched_dl_runnable(struct rq *rq)
1934 {
1935 return rq->dl.dl_nr_running > 0;
1936 }
1937
sched_rt_runnable(struct rq * rq)1938 static inline bool sched_rt_runnable(struct rq *rq)
1939 {
1940 return rq->rt.rt_queued > 0;
1941 }
1942
sched_fair_runnable(struct rq * rq)1943 static inline bool sched_fair_runnable(struct rq *rq)
1944 {
1945 return rq->cfs.nr_running > 0;
1946 }
1947
1948 extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
1949 extern struct task_struct *pick_next_task_idle(struct rq *rq);
1950
1951 #ifdef CONFIG_SMP
1952
1953 extern void update_group_capacity(struct sched_domain *sd, int cpu);
1954
1955 extern void trigger_load_balance(struct rq *rq);
1956
1957 extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1958
1959 extern unsigned long __read_mostly max_load_balance_interval;
1960 #endif
1961
1962 #ifdef CONFIG_CPU_IDLE
idle_set_state(struct rq * rq,struct cpuidle_state * idle_state)1963 static inline void idle_set_state(struct rq *rq,
1964 struct cpuidle_state *idle_state)
1965 {
1966 rq->idle_state = idle_state;
1967 }
1968
idle_get_state(struct rq * rq)1969 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1970 {
1971 SCHED_WARN_ON(!rcu_read_lock_held());
1972
1973 return rq->idle_state;
1974 }
1975 #else
idle_set_state(struct rq * rq,struct cpuidle_state * idle_state)1976 static inline void idle_set_state(struct rq *rq,
1977 struct cpuidle_state *idle_state)
1978 {
1979 }
1980
idle_get_state(struct rq * rq)1981 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1982 {
1983 return NULL;
1984 }
1985 #endif
1986
1987 extern void schedule_idle(void);
1988
1989 extern void sysrq_sched_debug_show(void);
1990 extern void sched_init_granularity(void);
1991 extern void update_max_interval(void);
1992
1993 extern void init_sched_dl_class(void);
1994 extern void init_sched_rt_class(void);
1995 extern void init_sched_fair_class(void);
1996
1997 extern void reweight_task(struct task_struct *p, int prio);
1998
1999 extern void resched_curr(struct rq *rq);
2000 extern void resched_cpu(int cpu);
2001
2002 extern struct rt_bandwidth def_rt_bandwidth;
2003 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
2004
2005 extern struct dl_bandwidth def_dl_bandwidth;
2006 extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
2007 extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
2008 extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
2009
2010 #define BW_SHIFT 20
2011 #define BW_UNIT (1 << BW_SHIFT)
2012 #define RATIO_SHIFT 8
2013 #define MAX_BW_BITS (64 - BW_SHIFT)
2014 #define MAX_BW ((1ULL << MAX_BW_BITS) - 1)
2015 unsigned long to_ratio(u64 period, u64 runtime);
2016
2017 extern void init_entity_runnable_average(struct sched_entity *se);
2018 extern void post_init_entity_util_avg(struct task_struct *p);
2019
2020 #ifdef CONFIG_NO_HZ_FULL
2021 extern bool sched_can_stop_tick(struct rq *rq);
2022 extern int __init sched_tick_offload_init(void);
2023
2024 /*
2025 * Tick may be needed by tasks in the runqueue depending on their policy and
2026 * requirements. If tick is needed, lets send the target an IPI to kick it out of
2027 * nohz mode if necessary.
2028 */
sched_update_tick_dependency(struct rq * rq)2029 static inline void sched_update_tick_dependency(struct rq *rq)
2030 {
2031 int cpu = cpu_of(rq);
2032
2033 if (!tick_nohz_full_cpu(cpu))
2034 return;
2035
2036 if (sched_can_stop_tick(rq))
2037 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
2038 else
2039 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
2040 }
2041 #else
sched_tick_offload_init(void)2042 static inline int sched_tick_offload_init(void) { return 0; }
sched_update_tick_dependency(struct rq * rq)2043 static inline void sched_update_tick_dependency(struct rq *rq) { }
2044 #endif
2045
add_nr_running(struct rq * rq,unsigned count)2046 static inline void add_nr_running(struct rq *rq, unsigned count)
2047 {
2048 unsigned prev_nr = rq->nr_running;
2049
2050 rq->nr_running = prev_nr + count;
2051 if (trace_sched_update_nr_running_tp_enabled()) {
2052 call_trace_sched_update_nr_running(rq, count);
2053 }
2054
2055 #ifdef CONFIG_SMP
2056 if (prev_nr < 2 && rq->nr_running >= 2) {
2057 if (!READ_ONCE(rq->rd->overload))
2058 WRITE_ONCE(rq->rd->overload, 1);
2059 }
2060 #endif
2061
2062 sched_update_tick_dependency(rq);
2063 }
2064
sub_nr_running(struct rq * rq,unsigned count)2065 static inline void sub_nr_running(struct rq *rq, unsigned count)
2066 {
2067 rq->nr_running -= count;
2068 if (trace_sched_update_nr_running_tp_enabled()) {
2069 call_trace_sched_update_nr_running(rq, -count);
2070 }
2071
2072 /* Check if we still need preemption */
2073 sched_update_tick_dependency(rq);
2074 }
2075
2076 extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
2077 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
2078
2079 extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
2080
2081 extern const_debug unsigned int sysctl_sched_nr_migrate;
2082 extern const_debug unsigned int sysctl_sched_migration_cost;
2083
2084 #ifdef CONFIG_SCHED_HRTICK
2085
2086 /*
2087 * Use hrtick when:
2088 * - enabled by features
2089 * - hrtimer is actually high res
2090 */
hrtick_enabled(struct rq * rq)2091 static inline int hrtick_enabled(struct rq *rq)
2092 {
2093 if (!sched_feat(HRTICK))
2094 return 0;
2095 if (!cpu_active(cpu_of(rq)))
2096 return 0;
2097 return hrtimer_is_hres_active(&rq->hrtick_timer);
2098 }
2099
2100 void hrtick_start(struct rq *rq, u64 delay);
2101
2102 #else
2103
hrtick_enabled(struct rq * rq)2104 static inline int hrtick_enabled(struct rq *rq)
2105 {
2106 return 0;
2107 }
2108
2109 #endif /* CONFIG_SCHED_HRTICK */
2110
2111 #ifndef arch_scale_freq_tick
2112 static __always_inline
arch_scale_freq_tick(void)2113 void arch_scale_freq_tick(void)
2114 {
2115 }
2116 #endif
2117
2118 #ifndef arch_scale_freq_capacity
2119 /**
2120 * arch_scale_freq_capacity - get the frequency scale factor of a given CPU.
2121 * @cpu: the CPU in question.
2122 *
2123 * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
2124 *
2125 * f_curr
2126 * ------ * SCHED_CAPACITY_SCALE
2127 * f_max
2128 */
2129 static __always_inline
arch_scale_freq_capacity(int cpu)2130 unsigned long arch_scale_freq_capacity(int cpu)
2131 {
2132 return SCHED_CAPACITY_SCALE;
2133 }
2134 #endif
2135
2136 #ifdef CONFIG_SMP
2137 #ifdef CONFIG_PREEMPTION
2138
2139 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
2140
2141 /*
2142 * fair double_lock_balance: Safely acquires both rq->locks in a fair
2143 * way at the expense of forcing extra atomic operations in all
2144 * invocations. This assures that the double_lock is acquired using the
2145 * same underlying policy as the spinlock_t on this architecture, which
2146 * reduces latency compared to the unfair variant below. However, it
2147 * also adds more overhead and therefore may reduce throughput.
2148 */
_double_lock_balance(struct rq * this_rq,struct rq * busiest)2149 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2150 __releases(this_rq->lock)
2151 __acquires(busiest->lock)
2152 __acquires(this_rq->lock)
2153 {
2154 raw_spin_unlock(&this_rq->lock);
2155 double_rq_lock(this_rq, busiest);
2156
2157 return 1;
2158 }
2159
2160 #else
2161 /*
2162 * Unfair double_lock_balance: Optimizes throughput at the expense of
2163 * latency by eliminating extra atomic operations when the locks are
2164 * already in proper order on entry. This favors lower CPU-ids and will
2165 * grant the double lock to lower CPUs over higher ids under contention,
2166 * regardless of entry order into the function.
2167 */
_double_lock_balance(struct rq * this_rq,struct rq * busiest)2168 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2169 __releases(this_rq->lock)
2170 __acquires(busiest->lock)
2171 __acquires(this_rq->lock)
2172 {
2173 int ret = 0;
2174
2175 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
2176 if (busiest < this_rq) {
2177 raw_spin_unlock(&this_rq->lock);
2178 raw_spin_lock(&busiest->lock);
2179 raw_spin_lock_nested(&this_rq->lock,
2180 SINGLE_DEPTH_NESTING);
2181 ret = 1;
2182 } else
2183 raw_spin_lock_nested(&busiest->lock,
2184 SINGLE_DEPTH_NESTING);
2185 }
2186 return ret;
2187 }
2188
2189 #endif /* CONFIG_PREEMPTION */
2190
2191 /*
2192 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2193 */
double_lock_balance(struct rq * this_rq,struct rq * busiest)2194 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
2195 {
2196 if (unlikely(!irqs_disabled())) {
2197 /* printk() doesn't work well under rq->lock */
2198 raw_spin_unlock(&this_rq->lock);
2199 BUG_ON(1);
2200 }
2201
2202 return _double_lock_balance(this_rq, busiest);
2203 }
2204
double_unlock_balance(struct rq * this_rq,struct rq * busiest)2205 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
2206 __releases(busiest->lock)
2207 {
2208 raw_spin_unlock(&busiest->lock);
2209 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
2210 }
2211
double_lock(spinlock_t * l1,spinlock_t * l2)2212 static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
2213 {
2214 if (l1 > l2)
2215 swap(l1, l2);
2216
2217 spin_lock(l1);
2218 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2219 }
2220
double_lock_irq(spinlock_t * l1,spinlock_t * l2)2221 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
2222 {
2223 if (l1 > l2)
2224 swap(l1, l2);
2225
2226 spin_lock_irq(l1);
2227 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2228 }
2229
double_raw_lock(raw_spinlock_t * l1,raw_spinlock_t * l2)2230 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
2231 {
2232 if (l1 > l2)
2233 swap(l1, l2);
2234
2235 raw_spin_lock(l1);
2236 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2237 }
2238
2239 /*
2240 * double_rq_lock - safely lock two runqueues
2241 *
2242 * Note this does not disable interrupts like task_rq_lock,
2243 * you need to do so manually before calling.
2244 */
double_rq_lock(struct rq * rq1,struct rq * rq2)2245 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2246 __acquires(rq1->lock)
2247 __acquires(rq2->lock)
2248 {
2249 BUG_ON(!irqs_disabled());
2250 if (rq1 == rq2) {
2251 raw_spin_lock(&rq1->lock);
2252 __acquire(rq2->lock); /* Fake it out ;) */
2253 } else {
2254 if (rq1 < rq2) {
2255 raw_spin_lock(&rq1->lock);
2256 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
2257 } else {
2258 raw_spin_lock(&rq2->lock);
2259 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
2260 }
2261 }
2262 }
2263
2264 /*
2265 * double_rq_unlock - safely unlock two runqueues
2266 *
2267 * Note this does not restore interrupts like task_rq_unlock,
2268 * you need to do so manually after calling.
2269 */
double_rq_unlock(struct rq * rq1,struct rq * rq2)2270 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2271 __releases(rq1->lock)
2272 __releases(rq2->lock)
2273 {
2274 raw_spin_unlock(&rq1->lock);
2275 if (rq1 != rq2)
2276 raw_spin_unlock(&rq2->lock);
2277 else
2278 __release(rq2->lock);
2279 }
2280
2281 extern void set_rq_online (struct rq *rq);
2282 extern void set_rq_offline(struct rq *rq);
2283 extern bool sched_smp_initialized;
2284
2285 #else /* CONFIG_SMP */
2286
2287 /*
2288 * double_rq_lock - safely lock two runqueues
2289 *
2290 * Note this does not disable interrupts like task_rq_lock,
2291 * you need to do so manually before calling.
2292 */
double_rq_lock(struct rq * rq1,struct rq * rq2)2293 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2294 __acquires(rq1->lock)
2295 __acquires(rq2->lock)
2296 {
2297 BUG_ON(!irqs_disabled());
2298 BUG_ON(rq1 != rq2);
2299 raw_spin_lock(&rq1->lock);
2300 __acquire(rq2->lock); /* Fake it out ;) */
2301 }
2302
2303 /*
2304 * double_rq_unlock - safely unlock two runqueues
2305 *
2306 * Note this does not restore interrupts like task_rq_unlock,
2307 * you need to do so manually after calling.
2308 */
double_rq_unlock(struct rq * rq1,struct rq * rq2)2309 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2310 __releases(rq1->lock)
2311 __releases(rq2->lock)
2312 {
2313 BUG_ON(rq1 != rq2);
2314 raw_spin_unlock(&rq1->lock);
2315 __release(rq2->lock);
2316 }
2317
2318 #endif
2319
2320 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
2321 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
2322
2323 #ifdef CONFIG_SCHED_DEBUG
2324 extern bool sched_debug_enabled;
2325
2326 extern void print_cfs_stats(struct seq_file *m, int cpu);
2327 extern void print_rt_stats(struct seq_file *m, int cpu);
2328 extern void print_dl_stats(struct seq_file *m, int cpu);
2329 extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
2330 extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2331 extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
2332 #ifdef CONFIG_NUMA_BALANCING
2333 extern void
2334 show_numa_stats(struct task_struct *p, struct seq_file *m);
2335 extern void
2336 print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
2337 unsigned long tpf, unsigned long gsf, unsigned long gpf);
2338 #endif /* CONFIG_NUMA_BALANCING */
2339 #endif /* CONFIG_SCHED_DEBUG */
2340
2341 extern void init_cfs_rq(struct cfs_rq *cfs_rq);
2342 extern void init_rt_rq(struct rt_rq *rt_rq);
2343 extern void init_dl_rq(struct dl_rq *dl_rq);
2344
2345 extern void cfs_bandwidth_usage_inc(void);
2346 extern void cfs_bandwidth_usage_dec(void);
2347
2348 #ifdef CONFIG_NO_HZ_COMMON
2349 #define NOHZ_BALANCE_KICK_BIT 0
2350 #define NOHZ_STATS_KICK_BIT 1
2351
2352 #define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT)
2353 #define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT)
2354
2355 #define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK)
2356
2357 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
2358
2359 extern void nohz_balance_exit_idle(struct rq *rq);
2360 #else
nohz_balance_exit_idle(struct rq * rq)2361 static inline void nohz_balance_exit_idle(struct rq *rq) { }
2362 #endif
2363
2364
2365 #ifdef CONFIG_SMP
2366 static inline
__dl_update(struct dl_bw * dl_b,s64 bw)2367 void __dl_update(struct dl_bw *dl_b, s64 bw)
2368 {
2369 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
2370 int i;
2371
2372 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2373 "sched RCU must be held");
2374 for_each_cpu_and(i, rd->span, cpu_active_mask) {
2375 struct rq *rq = cpu_rq(i);
2376
2377 rq->dl.extra_bw += bw;
2378 }
2379 }
2380 #else
2381 static inline
__dl_update(struct dl_bw * dl_b,s64 bw)2382 void __dl_update(struct dl_bw *dl_b, s64 bw)
2383 {
2384 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
2385
2386 dl->extra_bw += bw;
2387 }
2388 #endif
2389
2390
2391 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
2392 struct irqtime {
2393 u64 total;
2394 u64 tick_delta;
2395 u64 irq_start_time;
2396 struct u64_stats_sync sync;
2397 };
2398
2399 DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
2400
2401 /*
2402 * Returns the irqtime minus the softirq time computed by ksoftirqd.
2403 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
2404 * and never move forward.
2405 */
irq_time_read(int cpu)2406 static inline u64 irq_time_read(int cpu)
2407 {
2408 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2409 unsigned int seq;
2410 u64 total;
2411
2412 do {
2413 seq = __u64_stats_fetch_begin(&irqtime->sync);
2414 total = irqtime->total;
2415 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
2416
2417 return total;
2418 }
2419 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
2420
2421 #ifdef CONFIG_CPU_FREQ
2422 DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
2423
2424 /**
2425 * cpufreq_update_util - Take a note about CPU utilization changes.
2426 * @rq: Runqueue to carry out the update for.
2427 * @flags: Update reason flags.
2428 *
2429 * This function is called by the scheduler on the CPU whose utilization is
2430 * being updated.
2431 *
2432 * It can only be called from RCU-sched read-side critical sections.
2433 *
2434 * The way cpufreq is currently arranged requires it to evaluate the CPU
2435 * performance state (frequency/voltage) on a regular basis to prevent it from
2436 * being stuck in a completely inadequate performance level for too long.
2437 * That is not guaranteed to happen if the updates are only triggered from CFS
2438 * and DL, though, because they may not be coming in if only RT tasks are
2439 * active all the time (or there are RT tasks only).
2440 *
2441 * As a workaround for that issue, this function is called periodically by the
2442 * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
2443 * but that really is a band-aid. Going forward it should be replaced with
2444 * solutions targeted more specifically at RT tasks.
2445 */
cpufreq_update_util(struct rq * rq,unsigned int flags)2446 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
2447 {
2448 struct update_util_data *data;
2449
2450 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2451 cpu_of(rq)));
2452 if (data)
2453 data->func(data, rq_clock(rq), flags);
2454 }
2455 #else
cpufreq_update_util(struct rq * rq,unsigned int flags)2456 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
2457 #endif /* CONFIG_CPU_FREQ */
2458
2459 #ifdef CONFIG_UCLAMP_TASK
2460 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
2461
2462 /**
2463 * uclamp_rq_util_with - clamp @util with @rq and @p effective uclamp values.
2464 * @rq: The rq to clamp against. Must not be NULL.
2465 * @util: The util value to clamp.
2466 * @p: The task to clamp against. Can be NULL if you want to clamp
2467 * against @rq only.
2468 *
2469 * Clamps the passed @util to the max(@rq, @p) effective uclamp values.
2470 *
2471 * If sched_uclamp_used static key is disabled, then just return the util
2472 * without any clamping since uclamp aggregation at the rq level in the fast
2473 * path is disabled, rendering this operation a NOP.
2474 *
2475 * Use uclamp_eff_value() if you don't care about uclamp values at rq level. It
2476 * will return the correct effective uclamp value of the task even if the
2477 * static key is disabled.
2478 */
2479 static __always_inline
uclamp_rq_util_with(struct rq * rq,unsigned long util,struct task_struct * p)2480 unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
2481 struct task_struct *p)
2482 {
2483 unsigned long min_util = 0;
2484 unsigned long max_util = 0;
2485
2486 if (!static_branch_likely(&sched_uclamp_used))
2487 return util;
2488
2489 if (p) {
2490 min_util = uclamp_eff_value(p, UCLAMP_MIN);
2491 max_util = uclamp_eff_value(p, UCLAMP_MAX);
2492
2493 /*
2494 * Ignore last runnable task's max clamp, as this task will
2495 * reset it. Similarly, no need to read the rq's min clamp.
2496 */
2497 if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
2498 goto out;
2499 }
2500
2501 min_util = max_t(unsigned long, min_util, READ_ONCE(rq->uclamp[UCLAMP_MIN].value));
2502 max_util = max_t(unsigned long, max_util, READ_ONCE(rq->uclamp[UCLAMP_MAX].value));
2503 out:
2504 /*
2505 * Since CPU's {min,max}_util clamps are MAX aggregated considering
2506 * RUNNABLE tasks with _different_ clamps, we can end up with an
2507 * inversion. Fix it now when the clamps are applied.
2508 */
2509 if (unlikely(min_util >= max_util))
2510 return min_util;
2511
2512 return clamp(util, min_util, max_util);
2513 }
2514
uclamp_boosted(struct task_struct * p)2515 static inline bool uclamp_boosted(struct task_struct *p)
2516 {
2517 return uclamp_eff_value(p, UCLAMP_MIN) > 0;
2518 }
2519
2520 /*
2521 * When uclamp is compiled in, the aggregation at rq level is 'turned off'
2522 * by default in the fast path and only gets turned on once userspace performs
2523 * an operation that requires it.
2524 *
2525 * Returns true if userspace opted-in to use uclamp and aggregation at rq level
2526 * hence is active.
2527 */
uclamp_is_used(void)2528 static inline bool uclamp_is_used(void)
2529 {
2530 return static_branch_likely(&sched_uclamp_used);
2531 }
2532 #else /* CONFIG_UCLAMP_TASK */
2533 static inline
uclamp_rq_util_with(struct rq * rq,unsigned long util,struct task_struct * p)2534 unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
2535 struct task_struct *p)
2536 {
2537 return util;
2538 }
2539
uclamp_boosted(struct task_struct * p)2540 static inline bool uclamp_boosted(struct task_struct *p)
2541 {
2542 return false;
2543 }
2544
uclamp_is_used(void)2545 static inline bool uclamp_is_used(void)
2546 {
2547 return false;
2548 }
2549 #endif /* CONFIG_UCLAMP_TASK */
2550
2551 #ifdef CONFIG_UCLAMP_TASK_GROUP
uclamp_latency_sensitive(struct task_struct * p)2552 static inline bool uclamp_latency_sensitive(struct task_struct *p)
2553 {
2554 struct cgroup_subsys_state *css = task_css(p, cpu_cgrp_id);
2555 struct task_group *tg;
2556
2557 if (!css)
2558 return false;
2559 tg = container_of(css, struct task_group, css);
2560
2561 return tg->latency_sensitive;
2562 }
2563 #else
uclamp_latency_sensitive(struct task_struct * p)2564 static inline bool uclamp_latency_sensitive(struct task_struct *p)
2565 {
2566 return false;
2567 }
2568 #endif /* CONFIG_UCLAMP_TASK_GROUP */
2569
2570 #ifdef arch_scale_freq_capacity
2571 # ifndef arch_scale_freq_invariant
2572 # define arch_scale_freq_invariant() true
2573 # endif
2574 #else
2575 # define arch_scale_freq_invariant() false
2576 #endif
2577
2578 #ifdef CONFIG_SMP
capacity_orig_of(int cpu)2579 static inline unsigned long capacity_orig_of(int cpu)
2580 {
2581 return cpu_rq(cpu)->cpu_capacity_orig;
2582 }
2583 #endif
2584
2585 /**
2586 * enum schedutil_type - CPU utilization type
2587 * @FREQUENCY_UTIL: Utilization used to select frequency
2588 * @ENERGY_UTIL: Utilization used during energy calculation
2589 *
2590 * The utilization signals of all scheduling classes (CFS/RT/DL) and IRQ time
2591 * need to be aggregated differently depending on the usage made of them. This
2592 * enum is used within schedutil_freq_util() to differentiate the types of
2593 * utilization expected by the callers, and adjust the aggregation accordingly.
2594 */
2595 enum schedutil_type {
2596 FREQUENCY_UTIL,
2597 ENERGY_UTIL,
2598 };
2599
2600 #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
2601
2602 unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
2603 unsigned long max, enum schedutil_type type,
2604 struct task_struct *p);
2605
cpu_bw_dl(struct rq * rq)2606 static inline unsigned long cpu_bw_dl(struct rq *rq)
2607 {
2608 return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
2609 }
2610
cpu_util_dl(struct rq * rq)2611 static inline unsigned long cpu_util_dl(struct rq *rq)
2612 {
2613 return READ_ONCE(rq->avg_dl.util_avg);
2614 }
2615
cpu_util_cfs(struct rq * rq)2616 static inline unsigned long cpu_util_cfs(struct rq *rq)
2617 {
2618 unsigned long util = READ_ONCE(rq->cfs.avg.util_avg);
2619
2620 if (sched_feat(UTIL_EST)) {
2621 util = max_t(unsigned long, util,
2622 READ_ONCE(rq->cfs.avg.util_est.enqueued));
2623 }
2624
2625 return util;
2626 }
2627
cpu_util_rt(struct rq * rq)2628 static inline unsigned long cpu_util_rt(struct rq *rq)
2629 {
2630 return READ_ONCE(rq->avg_rt.util_avg);
2631 }
2632 #else /* CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
schedutil_cpu_util(int cpu,unsigned long util_cfs,unsigned long max,enum schedutil_type type,struct task_struct * p)2633 static inline unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
2634 unsigned long max, enum schedutil_type type,
2635 struct task_struct *p)
2636 {
2637 return 0;
2638 }
2639 #endif /* CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
2640
2641 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
cpu_util_irq(struct rq * rq)2642 static inline unsigned long cpu_util_irq(struct rq *rq)
2643 {
2644 return rq->avg_irq.util_avg;
2645 }
2646
2647 static inline
scale_irq_capacity(unsigned long util,unsigned long irq,unsigned long max)2648 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
2649 {
2650 util *= (max - irq);
2651 util /= max;
2652
2653 return util;
2654
2655 }
2656 #else
cpu_util_irq(struct rq * rq)2657 static inline unsigned long cpu_util_irq(struct rq *rq)
2658 {
2659 return 0;
2660 }
2661
2662 static inline
scale_irq_capacity(unsigned long util,unsigned long irq,unsigned long max)2663 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
2664 {
2665 return util;
2666 }
2667 #endif
2668
2669 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
2670
2671 #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus)))
2672
2673 DECLARE_STATIC_KEY_FALSE(sched_energy_present);
2674
sched_energy_enabled(void)2675 static inline bool sched_energy_enabled(void)
2676 {
2677 return static_branch_unlikely(&sched_energy_present);
2678 }
2679
2680 #else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */
2681
2682 #define perf_domain_span(pd) NULL
sched_energy_enabled(void)2683 static inline bool sched_energy_enabled(void) { return false; }
2684
2685 #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
2686
2687 #ifdef CONFIG_MEMBARRIER
2688 /*
2689 * The scheduler provides memory barriers required by membarrier between:
2690 * - prior user-space memory accesses and store to rq->membarrier_state,
2691 * - store to rq->membarrier_state and following user-space memory accesses.
2692 * In the same way it provides those guarantees around store to rq->curr.
2693 */
membarrier_switch_mm(struct rq * rq,struct mm_struct * prev_mm,struct mm_struct * next_mm)2694 static inline void membarrier_switch_mm(struct rq *rq,
2695 struct mm_struct *prev_mm,
2696 struct mm_struct *next_mm)
2697 {
2698 int membarrier_state;
2699
2700 if (prev_mm == next_mm)
2701 return;
2702
2703 membarrier_state = atomic_read(&next_mm->membarrier_state);
2704 if (READ_ONCE(rq->membarrier_state) == membarrier_state)
2705 return;
2706
2707 WRITE_ONCE(rq->membarrier_state, membarrier_state);
2708 }
2709 #else
membarrier_switch_mm(struct rq * rq,struct mm_struct * prev_mm,struct mm_struct * next_mm)2710 static inline void membarrier_switch_mm(struct rq *rq,
2711 struct mm_struct *prev_mm,
2712 struct mm_struct *next_mm)
2713 {
2714 }
2715 #endif
2716
2717 #ifdef CONFIG_SMP
is_per_cpu_kthread(struct task_struct * p)2718 static inline bool is_per_cpu_kthread(struct task_struct *p)
2719 {
2720 if (!(p->flags & PF_KTHREAD))
2721 return false;
2722
2723 if (p->nr_cpus_allowed != 1)
2724 return false;
2725
2726 return true;
2727 }
2728 #endif
2729
2730 void swake_up_all_locked(struct swait_queue_head *q);
2731 void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
2732
2733 /*
2734 * task_may_not_preempt - check whether a task may not be preemptible soon
2735 */
2736 #ifdef CONFIG_RT_SOFTINT_OPTIMIZATION
2737 extern bool task_may_not_preempt(struct task_struct *task, int cpu);
2738 #else
task_may_not_preempt(struct task_struct * task,int cpu)2739 static inline bool task_may_not_preempt(struct task_struct *task, int cpu)
2740 {
2741 return false;
2742 }
2743 #endif /* CONFIG_RT_SOFTINT_OPTIMIZATION */
2744