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