1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Scheduler internal types and methods:
4 */
5 #ifndef _KERNEL_SCHED_SCHED_H
6 #define _KERNEL_SCHED_SCHED_H
7
8 #include <linux/sched/affinity.h>
9 #include <linux/sched/autogroup.h>
10 #include <linux/sched/cpufreq.h>
11 #include <linux/sched/deadline.h>
12 #include <linux/sched.h>
13 #include <linux/sched/loadavg.h>
14 #include <linux/sched/mm.h>
15 #include <linux/sched/rseq_api.h>
16 #include <linux/sched/signal.h>
17 #include <linux/sched/smt.h>
18 #include <linux/sched/stat.h>
19 #include <linux/sched/sysctl.h>
20 #include <linux/sched/task_flags.h>
21 #include <linux/sched/task.h>
22 #include <linux/sched/topology.h>
23
24 #include <linux/atomic.h>
25 #include <linux/bitmap.h>
26 #include <linux/bug.h>
27 #include <linux/capability.h>
28 #include <linux/cgroup_api.h>
29 #include <linux/cgroup.h>
30 #include <linux/context_tracking.h>
31 #include <linux/cpufreq.h>
32 #include <linux/cpumask_api.h>
33 #include <linux/ctype.h>
34 #include <linux/file.h>
35 #include <linux/fs_api.h>
36 #include <linux/hrtimer_api.h>
37 #include <linux/interrupt.h>
38 #include <linux/irq_work.h>
39 #include <linux/jiffies.h>
40 #include <linux/kref_api.h>
41 #include <linux/kthread.h>
42 #include <linux/ktime_api.h>
43 #include <linux/lockdep_api.h>
44 #include <linux/lockdep.h>
45 #include <linux/minmax.h>
46 #include <linux/mm.h>
47 #include <linux/mmu_context.h>
48 #include <linux/module.h>
49 #include <linux/mutex_api.h>
50 #include <linux/plist.h>
51 #include <linux/poll.h>
52 #include <linux/proc_fs.h>
53 #include <linux/profile.h>
54 #include <linux/psi.h>
55 #include <linux/rcupdate.h>
56 #include <linux/seq_file.h>
57 #include <linux/seqlock.h>
58 #include <linux/softirq.h>
59 #include <linux/spinlock_api.h>
60 #include <linux/static_key.h>
61 #include <linux/stop_machine.h>
62 #include <linux/syscalls_api.h>
63 #include <linux/syscalls.h>
64 #include <linux/tick.h>
65 #include <linux/topology.h>
66 #include <linux/types.h>
67 #include <linux/u64_stats_sync_api.h>
68 #include <linux/uaccess.h>
69 #include <linux/wait_api.h>
70 #include <linux/wait_bit.h>
71 #include <linux/workqueue_api.h>
72 #include <linux/delayacct.h>
73 #include <linux/android_vendor.h>
74 #include <linux/android_kabi.h>
75
76 #include <trace/events/power.h>
77 #include <trace/events/sched.h>
78
79 #include "../workqueue_internal.h"
80
81 struct rq;
82 struct cfs_rq;
83 struct rt_rq;
84 struct sched_group;
85 struct cpuidle_state;
86
87 #ifdef CONFIG_PARAVIRT
88 # include <asm/paravirt.h>
89 # include <asm/paravirt_api_clock.h>
90 #endif
91
92 #include <asm/barrier.h>
93
94 #include "cpupri.h"
95 #include "cpudeadline.h"
96
97 #ifdef CONFIG_SCHED_DEBUG
98 # define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
99 #else
100 # define SCHED_WARN_ON(x) ({ (void)(x), 0; })
101 #endif
102
103 /* task_struct::on_rq states: */
104 #define TASK_ON_RQ_QUEUED 1
105 #define TASK_ON_RQ_MIGRATING 2
106
107 extern __read_mostly int scheduler_running;
108
109 extern unsigned long calc_load_update;
110 extern atomic_long_t calc_load_tasks;
111
112 extern unsigned int sysctl_sched_child_runs_first;
113
114 extern void calc_global_load_tick(struct rq *this_rq);
115 extern long calc_load_fold_active(struct rq *this_rq, long adjust);
116
117 extern void call_trace_sched_update_nr_running(struct rq *rq, int count);
118
119 extern int sysctl_sched_rt_period;
120 extern int sysctl_sched_rt_runtime;
121 extern int sched_rr_timeslice;
122
123 /*
124 * Asymmetric CPU capacity bits
125 */
126 struct asym_cap_data {
127 struct list_head link;
128 struct rcu_head rcu;
129 unsigned long capacity;
130 unsigned long cpus[];
131 };
132
133 extern struct list_head asym_cap_list;
134
135 #define cpu_capacity_span(asym_data) to_cpumask((asym_data)->cpus)
136
137 /*
138 * Helpers for converting nanosecond timing to jiffy resolution
139 */
140 #define NS_TO_JIFFIES(time) ((unsigned long)(time) / (NSEC_PER_SEC/HZ))
141
142 /*
143 * Increase resolution of nice-level calculations for 64-bit architectures.
144 * The extra resolution improves shares distribution and load balancing of
145 * low-weight task groups (eg. nice +19 on an autogroup), deeper task-group
146 * hierarchies, especially on larger systems. This is not a user-visible change
147 * and does not change the user-interface for setting shares/weights.
148 *
149 * We increase resolution only if we have enough bits to allow this increased
150 * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
151 * are pretty high and the returns do not justify the increased costs.
152 *
153 * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to
154 * increase coverage and consistency always enable it on 64-bit platforms.
155 */
156 #ifdef CONFIG_64BIT
157 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
158 # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
159 # define scale_load_down(w) \
160 ({ \
161 unsigned long __w = (w); \
162 \
163 if (__w) \
164 __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \
165 __w; \
166 })
167 #else
168 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
169 # define scale_load(w) (w)
170 # define scale_load_down(w) (w)
171 #endif
172
173 /*
174 * Task weight (visible to users) and its load (invisible to users) have
175 * independent resolution, but they should be well calibrated. We use
176 * scale_load() and scale_load_down(w) to convert between them. The
177 * following must be true:
178 *
179 * scale_load(sched_prio_to_weight[NICE_TO_PRIO(0)-MAX_RT_PRIO]) == NICE_0_LOAD
180 *
181 */
182 #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
183
184 /*
185 * Single value that decides SCHED_DEADLINE internal math precision.
186 * 10 -> just above 1us
187 * 9 -> just above 0.5us
188 */
189 #define DL_SCALE 10
190
191 /*
192 * Single value that denotes runtime == period, ie unlimited time.
193 */
194 #define RUNTIME_INF ((u64)~0ULL)
195
idle_policy(int policy)196 static inline int idle_policy(int policy)
197 {
198 return policy == SCHED_IDLE;
199 }
200
normal_policy(int policy)201 static inline int normal_policy(int policy)
202 {
203 #ifdef CONFIG_SCHED_CLASS_EXT
204 if (policy == SCHED_EXT)
205 return true;
206 #endif
207 return policy == SCHED_NORMAL;
208 }
209
fair_policy(int policy)210 static inline int fair_policy(int policy)
211 {
212 return normal_policy(policy) || policy == SCHED_BATCH;
213 }
214
rt_policy(int policy)215 static inline int rt_policy(int policy)
216 {
217 return policy == SCHED_FIFO || policy == SCHED_RR;
218 }
219
dl_policy(int policy)220 static inline int dl_policy(int policy)
221 {
222 return policy == SCHED_DEADLINE;
223 }
224
valid_policy(int policy)225 static inline bool valid_policy(int policy)
226 {
227 return idle_policy(policy) || fair_policy(policy) ||
228 rt_policy(policy) || dl_policy(policy);
229 }
230
task_has_idle_policy(struct task_struct * p)231 static inline int task_has_idle_policy(struct task_struct *p)
232 {
233 return idle_policy(p->policy);
234 }
235
task_has_rt_policy(struct task_struct * p)236 static inline int task_has_rt_policy(struct task_struct *p)
237 {
238 return rt_policy(p->policy);
239 }
240
task_has_dl_policy(struct task_struct * p)241 static inline int task_has_dl_policy(struct task_struct *p)
242 {
243 return dl_policy(p->policy);
244 }
245
246 #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
247
update_avg(u64 * avg,u64 sample)248 static inline void update_avg(u64 *avg, u64 sample)
249 {
250 s64 diff = sample - *avg;
251
252 *avg += diff / 8;
253 }
254
255 /*
256 * Shifting a value by an exponent greater *or equal* to the size of said value
257 * is UB; cap at size-1.
258 */
259 #define shr_bound(val, shift) \
260 (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1))
261
262 /*
263 * cgroup weight knobs should use the common MIN, DFL and MAX values which are
264 * 1, 100 and 10000 respectively. While it loses a bit of range on both ends, it
265 * maps pretty well onto the shares value used by scheduler and the round-trip
266 * conversions preserve the original value over the entire range.
267 */
sched_weight_from_cgroup(unsigned long cgrp_weight)268 static inline unsigned long sched_weight_from_cgroup(unsigned long cgrp_weight)
269 {
270 return DIV_ROUND_CLOSEST_ULL(cgrp_weight * 1024, CGROUP_WEIGHT_DFL);
271 }
272
sched_weight_to_cgroup(unsigned long weight)273 static inline unsigned long sched_weight_to_cgroup(unsigned long weight)
274 {
275 return clamp_t(unsigned long,
276 DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024),
277 CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
278 }
279
280 /*
281 * !! For sched_setattr_nocheck() (kernel) only !!
282 *
283 * This is actually gross. :(
284 *
285 * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE
286 * tasks, but still be able to sleep. We need this on platforms that cannot
287 * atomically change clock frequency. Remove once fast switching will be
288 * available on such platforms.
289 *
290 * SUGOV stands for SchedUtil GOVernor.
291 */
292 #define SCHED_FLAG_SUGOV 0x10000000
293
294 #define SCHED_DL_FLAGS (SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV)
295
dl_entity_is_special(const struct sched_dl_entity * dl_se)296 static inline bool dl_entity_is_special(const struct sched_dl_entity *dl_se)
297 {
298 #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
299 return unlikely(dl_se->flags & SCHED_FLAG_SUGOV);
300 #else
301 return false;
302 #endif
303 }
304
305 /*
306 * Tells if entity @a should preempt entity @b.
307 */
dl_entity_preempt(const struct sched_dl_entity * a,const struct sched_dl_entity * b)308 static inline bool dl_entity_preempt(const struct sched_dl_entity *a,
309 const struct sched_dl_entity *b)
310 {
311 return dl_entity_is_special(a) ||
312 dl_time_before(a->deadline, b->deadline);
313 }
314
315 /*
316 * This is the priority-queue data structure of the RT scheduling class:
317 */
318 struct rt_prio_array {
319 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
320 struct list_head queue[MAX_RT_PRIO];
321 };
322
323 struct rt_bandwidth {
324 /* nests inside the rq lock: */
325 raw_spinlock_t rt_runtime_lock;
326 ktime_t rt_period;
327 u64 rt_runtime;
328 struct hrtimer rt_period_timer;
329 unsigned int rt_period_active;
330 };
331
dl_bandwidth_enabled(void)332 static inline int dl_bandwidth_enabled(void)
333 {
334 return sysctl_sched_rt_runtime >= 0;
335 }
336
337 /*
338 * To keep the bandwidth of -deadline tasks under control
339 * we need some place where:
340 * - store the maximum -deadline bandwidth of each cpu;
341 * - cache the fraction of bandwidth that is currently allocated in
342 * each root domain;
343 *
344 * This is all done in the data structure below. It is similar to the
345 * one used for RT-throttling (rt_bandwidth), with the main difference
346 * that, since here we are only interested in admission control, we
347 * do not decrease any runtime while the group "executes", neither we
348 * need a timer to replenish it.
349 *
350 * With respect to SMP, bandwidth is given on a per root domain basis,
351 * meaning that:
352 * - bw (< 100%) is the deadline bandwidth of each CPU;
353 * - total_bw is the currently allocated bandwidth in each root domain;
354 */
355 struct dl_bw {
356 raw_spinlock_t lock;
357 u64 bw;
358 u64 total_bw;
359 };
360
361 extern void init_dl_bw(struct dl_bw *dl_b);
362 extern int sched_dl_global_validate(void);
363 extern void sched_dl_do_global(void);
364 extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr);
365 extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
366 extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
367 extern bool __checkparam_dl(const struct sched_attr *attr);
368 extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
369 extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
370 extern int dl_bw_check_overflow(int cpu);
371 extern s64 dl_scaled_delta_exec(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec);
372 /*
373 * SCHED_DEADLINE supports servers (nested scheduling) with the following
374 * interface:
375 *
376 * dl_se::rq -- runqueue we belong to.
377 *
378 * dl_se::server_pick() -- nested pick_next_task(); we yield the period if this
379 * returns NULL.
380 *
381 * dl_server_update() -- called from update_curr_common(), propagates runtime
382 * to the server.
383 *
384 * dl_server_start() -- start the server when it has tasks; it will stop
385 * automatically when there are no more tasks, per
386 * dl_se::server_pick() returning NULL.
387 *
388 * dl_server_stop() -- (force) stop the server; use when updating
389 * parameters.
390 *
391 * dl_server_init() -- initializes the server.
392 *
393 * When started the dl_server will (per dl_defer) schedule a timer for its
394 * zero-laxity point -- that is, unlike regular EDF tasks which run ASAP, a
395 * server will run at the very end of its period.
396 *
397 * This is done such that any runtime from the target class can be accounted
398 * against the server -- through dl_server_update() above -- such that when it
399 * becomes time to run, it might already be out of runtime and get deferred
400 * until the next period. In this case dl_server_timer() will alternate
401 * between defer and replenish but never actually enqueue the server.
402 *
403 * Only when the target class does not manage to exhaust the server's runtime
404 * (there's actualy starvation in the given period), will the dl_server get on
405 * the runqueue. Once queued it will pick tasks from the target class and run
406 * them until either its runtime is exhaused, at which point its back to
407 * dl_server_timer, or until there are no more tasks to run, at which point
408 * the dl_server stops itself.
409 *
410 * By stopping at this point the dl_server retains bandwidth, which, if a new
411 * task wakes up imminently (starting the server again), can be used --
412 * subject to CBS wakeup rules -- without having to wait for the next period.
413 *
414 * Additionally, because of the dl_defer behaviour the start/stop behaviour is
415 * naturally thottled to once per period, avoiding high context switch
416 * workloads from spamming the hrtimer program/cancel paths.
417 */
418 extern void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec);
419 extern void dl_server_start(struct sched_dl_entity *dl_se);
420 extern void dl_server_stop(struct sched_dl_entity *dl_se);
421 extern void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq,
422 dl_server_pick_f pick_task);
423
424 extern void dl_server_update_idle_time(struct rq *rq,
425 struct task_struct *p);
426 extern void fair_server_init(struct rq *rq);
427 extern void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq);
428 extern int dl_server_apply_params(struct sched_dl_entity *dl_se,
429 u64 runtime, u64 period, bool init);
430
dl_server_active(struct sched_dl_entity * dl_se)431 static inline bool dl_server_active(struct sched_dl_entity *dl_se)
432 {
433 return dl_se->dl_server_active;
434 }
435
436 #ifdef CONFIG_CGROUP_SCHED
437
438 extern struct list_head task_groups;
439
440 struct cfs_bandwidth {
441 #ifdef CONFIG_CFS_BANDWIDTH
442 raw_spinlock_t lock;
443 ktime_t period;
444 u64 quota;
445 u64 runtime;
446 u64 burst;
447 u64 runtime_snap;
448 s64 hierarchical_quota;
449
450 u8 idle;
451 u8 period_active;
452 u8 slack_started;
453 struct hrtimer period_timer;
454 struct hrtimer slack_timer;
455 struct list_head throttled_cfs_rq;
456
457 /* Statistics: */
458 int nr_periods;
459 int nr_throttled;
460 int nr_burst;
461 u64 throttled_time;
462 u64 burst_time;
463 #endif
464 };
465
466 /* Task group related information */
467 struct task_group {
468 struct cgroup_subsys_state css;
469
470 #ifdef CONFIG_GROUP_SCHED_WEIGHT
471 /* A positive value indicates that this is a SCHED_IDLE group. */
472 int idle;
473 #endif
474
475 #ifdef CONFIG_FAIR_GROUP_SCHED
476 /* schedulable entities of this group on each CPU */
477 struct sched_entity **se;
478 /* runqueue "owned" by this group on each CPU */
479 struct cfs_rq **cfs_rq;
480 unsigned long shares;
481 #ifdef CONFIG_SMP
482 /*
483 * load_avg can be heavily contended at clock tick time, so put
484 * it in its own cache-line separated from the fields above which
485 * will also be accessed at each tick.
486 */
487 atomic_long_t load_avg ____cacheline_aligned;
488 #endif
489 #endif
490
491 #ifdef CONFIG_RT_GROUP_SCHED
492 struct sched_rt_entity **rt_se;
493 struct rt_rq **rt_rq;
494
495 struct rt_bandwidth rt_bandwidth;
496 #endif
497
498 #ifdef CONFIG_EXT_GROUP_SCHED
499 u32 scx_flags; /* SCX_TG_* */
500 u32 scx_weight;
501 #endif
502
503 struct rcu_head rcu;
504 struct list_head list;
505
506 struct task_group *parent;
507 struct list_head siblings;
508 struct list_head children;
509
510 #ifdef CONFIG_SCHED_AUTOGROUP
511 struct autogroup *autogroup;
512 #endif
513
514 struct cfs_bandwidth cfs_bandwidth;
515
516 #ifdef CONFIG_UCLAMP_TASK_GROUP
517 /* The two decimal precision [%] value requested from user-space */
518 unsigned int uclamp_pct[UCLAMP_CNT];
519 /* Clamp values requested for a task group */
520 struct uclamp_se uclamp_req[UCLAMP_CNT];
521 /* Effective clamp values used for a task group */
522 struct uclamp_se uclamp[UCLAMP_CNT];
523 /* Latency-sensitive flag used for a task group */
524 unsigned int latency_sensitive;
525
526 ANDROID_VENDOR_DATA_ARRAY(1, 4);
527 #endif
528
529 ANDROID_KABI_RESERVE(1);
530 ANDROID_KABI_RESERVE(2);
531 ANDROID_KABI_RESERVE(3);
532 ANDROID_KABI_RESERVE(4);
533 };
534
535 #ifdef CONFIG_GROUP_SCHED_WEIGHT
536 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
537
538 /*
539 * A weight of 0 or 1 can cause arithmetics problems.
540 * A weight of a cfs_rq is the sum of weights of which entities
541 * are queued on this cfs_rq, so a weight of a entity should not be
542 * too large, so as the shares value of a task group.
543 * (The default weight is 1024 - so there's no practical
544 * limitation from this.)
545 */
546 #define MIN_SHARES (1UL << 1)
547 #define MAX_SHARES (1UL << 18)
548 #endif
549
550 typedef int (*tg_visitor)(struct task_group *, void *);
551
552 extern int walk_tg_tree_from(struct task_group *from,
553 tg_visitor down, tg_visitor up, void *data);
554
555 /*
556 * Iterate the full tree, calling @down when first entering a node and @up when
557 * leaving it for the final time.
558 *
559 * Caller must hold rcu_lock or sufficient equivalent.
560 */
walk_tg_tree(tg_visitor down,tg_visitor up,void * data)561 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
562 {
563 return walk_tg_tree_from(&root_task_group, down, up, data);
564 }
565
css_tg(struct cgroup_subsys_state * css)566 static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
567 {
568 return css ? container_of(css, struct task_group, css) : NULL;
569 }
570
571 extern int tg_nop(struct task_group *tg, void *data);
572
573 #ifdef CONFIG_FAIR_GROUP_SCHED
574 extern void free_fair_sched_group(struct task_group *tg);
575 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
576 extern void online_fair_sched_group(struct task_group *tg);
577 extern void unregister_fair_sched_group(struct task_group *tg);
578 #else
free_fair_sched_group(struct task_group * tg)579 static inline void free_fair_sched_group(struct task_group *tg) { }
alloc_fair_sched_group(struct task_group * tg,struct task_group * parent)580 static inline int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
581 {
582 return 1;
583 }
online_fair_sched_group(struct task_group * tg)584 static inline void online_fair_sched_group(struct task_group *tg) { }
unregister_fair_sched_group(struct task_group * tg)585 static inline void unregister_fair_sched_group(struct task_group *tg) { }
586 #endif
587
588 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
589 struct sched_entity *se, int cpu,
590 struct sched_entity *parent);
591 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent);
592
593 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
594 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
595 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
596 extern bool cfs_task_bw_constrained(struct task_struct *p);
597
598 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
599 struct sched_rt_entity *rt_se, int cpu,
600 struct sched_rt_entity *parent);
601 extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
602 extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
603 extern long sched_group_rt_runtime(struct task_group *tg);
604 extern long sched_group_rt_period(struct task_group *tg);
605 extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
606
607 extern struct task_group *sched_create_group(struct task_group *parent);
608 extern void sched_online_group(struct task_group *tg,
609 struct task_group *parent);
610 extern void sched_destroy_group(struct task_group *tg);
611 extern void sched_release_group(struct task_group *tg);
612
613 extern void sched_move_task(struct task_struct *tsk, bool for_autogroup);
614
615 #ifdef CONFIG_FAIR_GROUP_SCHED
616 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
617
618 extern int sched_group_set_idle(struct task_group *tg, long idle);
619
620 #ifdef CONFIG_SMP
621 extern void set_task_rq_fair(struct sched_entity *se,
622 struct cfs_rq *prev, struct cfs_rq *next);
623 #else /* !CONFIG_SMP */
set_task_rq_fair(struct sched_entity * se,struct cfs_rq * prev,struct cfs_rq * next)624 static inline void set_task_rq_fair(struct sched_entity *se,
625 struct cfs_rq *prev, struct cfs_rq *next) { }
626 #endif /* CONFIG_SMP */
627 #else /* !CONFIG_FAIR_GROUP_SCHED */
sched_group_set_shares(struct task_group * tg,unsigned long shares)628 static inline int sched_group_set_shares(struct task_group *tg, unsigned long shares) { return 0; }
sched_group_set_idle(struct task_group * tg,long idle)629 static inline int sched_group_set_idle(struct task_group *tg, long idle) { return 0; }
630 #endif /* CONFIG_FAIR_GROUP_SCHED */
631
632 #else /* CONFIG_CGROUP_SCHED */
633
634 struct cfs_bandwidth { };
635
cfs_task_bw_constrained(struct task_struct * p)636 static inline bool cfs_task_bw_constrained(struct task_struct *p) { return false; }
637
638 #endif /* CONFIG_CGROUP_SCHED */
639
640 extern void unregister_rt_sched_group(struct task_group *tg);
641 extern void free_rt_sched_group(struct task_group *tg);
642 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
643
644 /*
645 * u64_u32_load/u64_u32_store
646 *
647 * Use a copy of a u64 value to protect against data race. This is only
648 * applicable for 32-bits architectures.
649 */
650 #ifdef CONFIG_64BIT
651 # define u64_u32_load_copy(var, copy) var
652 # define u64_u32_store_copy(var, copy, val) (var = val)
653 #else
654 # define u64_u32_load_copy(var, copy) \
655 ({ \
656 u64 __val, __val_copy; \
657 do { \
658 __val_copy = copy; \
659 /* \
660 * paired with u64_u32_store_copy(), ordering access \
661 * to var and copy. \
662 */ \
663 smp_rmb(); \
664 __val = var; \
665 } while (__val != __val_copy); \
666 __val; \
667 })
668 # define u64_u32_store_copy(var, copy, val) \
669 do { \
670 typeof(val) __val = (val); \
671 var = __val; \
672 /* \
673 * paired with u64_u32_load_copy(), ordering access to var and \
674 * copy. \
675 */ \
676 smp_wmb(); \
677 copy = __val; \
678 } while (0)
679 #endif
680 # define u64_u32_load(var) u64_u32_load_copy(var, var##_copy)
681 # define u64_u32_store(var, val) u64_u32_store_copy(var, var##_copy, val)
682
683 struct balance_callback {
684 struct balance_callback *next;
685 void (*func)(struct rq *rq);
686 };
687
688 /* CFS-related fields in a runqueue */
689 struct cfs_rq {
690 struct load_weight load;
691 unsigned int nr_running;
692 unsigned int h_nr_running; /* SCHED_{NORMAL,BATCH,IDLE} */
693 unsigned int idle_nr_running; /* SCHED_IDLE */
694 unsigned int idle_h_nr_running; /* SCHED_IDLE */
695 unsigned int h_nr_delayed;
696
697 s64 avg_vruntime;
698 u64 avg_load;
699
700 u64 min_vruntime;
701 #ifdef CONFIG_SCHED_CORE
702 unsigned int forceidle_seq;
703 u64 min_vruntime_fi;
704 #endif
705
706 struct rb_root_cached tasks_timeline;
707
708 /*
709 * 'curr' points to currently running entity on this cfs_rq.
710 * It is set to NULL otherwise (i.e when none are currently running).
711 */
712 struct sched_entity *curr;
713 struct sched_entity *next;
714
715 #ifdef CONFIG_SMP
716 /*
717 * CFS load tracking
718 */
719 struct sched_avg avg;
720 #ifndef CONFIG_64BIT
721 u64 last_update_time_copy;
722 #endif
723 struct {
724 raw_spinlock_t lock ____cacheline_aligned;
725 int nr;
726 unsigned long load_avg;
727 unsigned long util_avg;
728 unsigned long runnable_avg;
729 } removed;
730
731 #ifdef CONFIG_FAIR_GROUP_SCHED
732 u64 last_update_tg_load_avg;
733 unsigned long tg_load_avg_contrib;
734 long propagate;
735 long prop_runnable_sum;
736
737 /*
738 * h_load = weight * f(tg)
739 *
740 * Where f(tg) is the recursive weight fraction assigned to
741 * this group.
742 */
743 unsigned long h_load;
744 u64 last_h_load_update;
745 struct sched_entity *h_load_next;
746 #endif /* CONFIG_FAIR_GROUP_SCHED */
747 #endif /* CONFIG_SMP */
748
749 #ifdef CONFIG_FAIR_GROUP_SCHED
750 struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */
751
752 /*
753 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
754 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
755 * (like users, containers etc.)
756 *
757 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
758 * This list is used during load balance.
759 */
760 int on_list;
761 struct list_head leaf_cfs_rq_list;
762 struct task_group *tg; /* group that "owns" this runqueue */
763
764 /* Locally cached copy of our task_group's idle value */
765 int idle;
766
767 #ifdef CONFIG_CFS_BANDWIDTH
768 int runtime_enabled;
769 s64 runtime_remaining;
770
771 u64 throttled_pelt_idle;
772 #ifndef CONFIG_64BIT
773 u64 throttled_pelt_idle_copy;
774 #endif
775 u64 throttled_clock;
776 u64 throttled_clock_pelt;
777 u64 throttled_clock_pelt_time;
778 u64 throttled_clock_self;
779 u64 throttled_clock_self_time;
780 int throttled;
781 int throttle_count;
782 struct list_head throttled_list;
783 struct list_head throttled_csd_list;
784 #endif /* CONFIG_CFS_BANDWIDTH */
785 #endif /* CONFIG_FAIR_GROUP_SCHED */
786 };
787
788 #ifdef CONFIG_SCHED_CLASS_EXT
789 /* scx_rq->flags, protected by the rq lock */
790 enum scx_rq_flags {
791 /*
792 * A hotplugged CPU starts scheduling before rq_online_scx(). Track
793 * ops.cpu_on/offline() state so that ops.enqueue/dispatch() are called
794 * only while the BPF scheduler considers the CPU to be online.
795 */
796 SCX_RQ_ONLINE = 1 << 0,
797 SCX_RQ_CAN_STOP_TICK = 1 << 1,
798 SCX_RQ_BAL_PENDING = 1 << 2, /* balance hasn't run yet */
799 SCX_RQ_BAL_KEEP = 1 << 3, /* balance decided to keep current */
800 SCX_RQ_BYPASSING = 1 << 4,
801
802 SCX_RQ_IN_WAKEUP = 1 << 16,
803 SCX_RQ_IN_BALANCE = 1 << 17,
804 };
805
806 struct scx_rq {
807 struct scx_dispatch_q local_dsq;
808 struct list_head runnable_list; /* runnable tasks on this rq */
809 struct list_head ddsp_deferred_locals; /* deferred ddsps from enq */
810 unsigned long ops_qseq;
811 u64 extra_enq_flags; /* see move_task_to_local_dsq() */
812 u32 nr_running;
813 u32 flags;
814 u32 cpuperf_target; /* [0, SCHED_CAPACITY_SCALE] */
815 bool cpu_released;
816 cpumask_var_t cpus_to_kick;
817 cpumask_var_t cpus_to_kick_if_idle;
818 cpumask_var_t cpus_to_preempt;
819 cpumask_var_t cpus_to_wait;
820 unsigned long pnt_seq;
821 struct balance_callback deferred_bal_cb;
822 struct irq_work deferred_irq_work;
823 struct irq_work kick_cpus_irq_work;
824 };
825 #endif /* CONFIG_SCHED_CLASS_EXT */
826
rt_bandwidth_enabled(void)827 static inline int rt_bandwidth_enabled(void)
828 {
829 return sysctl_sched_rt_runtime >= 0;
830 }
831
832 /* RT IPI pull logic requires IRQ_WORK */
833 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
834 # define HAVE_RT_PUSH_IPI
835 #endif
836
837 /* Real-Time classes' related field in a runqueue: */
838 struct rt_rq {
839 struct rt_prio_array active;
840 unsigned int rt_nr_running;
841 unsigned int rr_nr_running;
842 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
843 struct {
844 int curr; /* highest queued rt task prio */
845 #ifdef CONFIG_SMP
846 int next; /* next highest */
847 #endif
848 } highest_prio;
849 #endif
850 #ifdef CONFIG_SMP
851 bool overloaded;
852 struct plist_head pushable_tasks;
853
854 #endif /* CONFIG_SMP */
855 int rt_queued;
856
857 #ifdef CONFIG_RT_GROUP_SCHED
858 int rt_throttled;
859 u64 rt_time;
860 u64 rt_runtime;
861 /* Nests inside the rq lock: */
862 raw_spinlock_t rt_runtime_lock;
863
864 unsigned int rt_nr_boosted;
865
866 struct rq *rq;
867 struct task_group *tg;
868 #endif
869 };
870
rt_rq_is_runnable(struct rt_rq * rt_rq)871 static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
872 {
873 return rt_rq->rt_queued && rt_rq->rt_nr_running;
874 }
875
876 /* Deadline class' related fields in a runqueue */
877 struct dl_rq {
878 /* runqueue is an rbtree, ordered by deadline */
879 struct rb_root_cached root;
880
881 unsigned int dl_nr_running;
882
883 #ifdef CONFIG_SMP
884 /*
885 * Deadline values of the currently executing and the
886 * earliest ready task on this rq. Caching these facilitates
887 * the decision whether or not a ready but not running task
888 * should migrate somewhere else.
889 */
890 struct {
891 u64 curr;
892 u64 next;
893 } earliest_dl;
894
895 bool overloaded;
896
897 /*
898 * Tasks on this rq that can be pushed away. They are kept in
899 * an rb-tree, ordered by tasks' deadlines, with caching
900 * of the leftmost (earliest deadline) element.
901 */
902 struct rb_root_cached pushable_dl_tasks_root;
903 #else
904 struct dl_bw dl_bw;
905 #endif
906 /*
907 * "Active utilization" for this runqueue: increased when a
908 * task wakes up (becomes TASK_RUNNING) and decreased when a
909 * task blocks
910 */
911 u64 running_bw;
912
913 /*
914 * Utilization of the tasks "assigned" to this runqueue (including
915 * the tasks that are in runqueue and the tasks that executed on this
916 * CPU and blocked). Increased when a task moves to this runqueue, and
917 * decreased when the task moves away (migrates, changes scheduling
918 * policy, or terminates).
919 * This is needed to compute the "inactive utilization" for the
920 * runqueue (inactive utilization = this_bw - running_bw).
921 */
922 u64 this_bw;
923 u64 extra_bw;
924
925 /*
926 * Maximum available bandwidth for reclaiming by SCHED_FLAG_RECLAIM
927 * tasks of this rq. Used in calculation of reclaimable bandwidth(GRUB).
928 */
929 u64 max_bw;
930
931 /*
932 * Inverse of the fraction of CPU utilization that can be reclaimed
933 * by the GRUB algorithm.
934 */
935 u64 bw_ratio;
936 };
937
938 #ifdef CONFIG_FAIR_GROUP_SCHED
939
940 /* An entity is a task if it doesn't "own" a runqueue */
941 #define entity_is_task(se) (!se->my_q)
942
se_update_runnable(struct sched_entity * se)943 static inline void se_update_runnable(struct sched_entity *se)
944 {
945 if (!entity_is_task(se)) {
946 struct cfs_rq *cfs_rq = se->my_q;
947
948 se->runnable_weight = cfs_rq->h_nr_running - cfs_rq->h_nr_delayed;
949 }
950 }
951
se_runnable(struct sched_entity * se)952 static inline long se_runnable(struct sched_entity *se)
953 {
954 if (se->sched_delayed)
955 return false;
956
957 if (entity_is_task(se))
958 return !!se->on_rq;
959 else
960 return se->runnable_weight;
961 }
962
963 #else /* !CONFIG_FAIR_GROUP_SCHED: */
964
965 #define entity_is_task(se) 1
966
se_update_runnable(struct sched_entity * se)967 static inline void se_update_runnable(struct sched_entity *se) { }
968
se_runnable(struct sched_entity * se)969 static inline long se_runnable(struct sched_entity *se)
970 {
971 if (se->sched_delayed)
972 return false;
973
974 return !!se->on_rq;
975 }
976
977 #endif /* !CONFIG_FAIR_GROUP_SCHED */
978
979 #ifdef CONFIG_SMP
980 /*
981 * XXX we want to get rid of these helpers and use the full load resolution.
982 */
se_weight(struct sched_entity * se)983 static inline long se_weight(struct sched_entity *se)
984 {
985 return scale_load_down(se->load.weight);
986 }
987
988
sched_asym_prefer(int a,int b)989 static inline bool sched_asym_prefer(int a, int b)
990 {
991 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
992 }
993
994 struct perf_domain {
995 struct em_perf_domain *em_pd;
996 struct perf_domain *next;
997 struct rcu_head rcu;
998 };
999
1000 /*
1001 * We add the notion of a root-domain which will be used to define per-domain
1002 * variables. Each exclusive cpuset essentially defines an island domain by
1003 * fully partitioning the member CPUs from any other cpuset. Whenever a new
1004 * exclusive cpuset is created, we also create and attach a new root-domain
1005 * object.
1006 *
1007 */
1008 struct root_domain {
1009 atomic_t refcount;
1010 atomic_t rto_count;
1011 struct rcu_head rcu;
1012 cpumask_var_t span;
1013 cpumask_var_t online;
1014
1015 /*
1016 * Indicate pullable load on at least one CPU, e.g:
1017 * - More than one runnable task
1018 * - Running task is misfit
1019 */
1020 bool overloaded;
1021
1022 /* Indicate one or more CPUs over-utilized (tipping point) */
1023 bool overutilized;
1024
1025 /*
1026 * The bit corresponding to a CPU gets set here if such CPU has more
1027 * than one runnable -deadline task (as it is below for RT tasks).
1028 */
1029 cpumask_var_t dlo_mask;
1030 atomic_t dlo_count;
1031 struct dl_bw dl_bw;
1032 struct cpudl cpudl;
1033
1034 /*
1035 * Indicate whether a root_domain's dl_bw has been checked or
1036 * updated. It's monotonously increasing value.
1037 *
1038 * Also, some corner cases, like 'wrap around' is dangerous, but given
1039 * that u64 is 'big enough'. So that shouldn't be a concern.
1040 */
1041 u64 visit_gen;
1042
1043 #ifdef HAVE_RT_PUSH_IPI
1044 /*
1045 * For IPI pull requests, loop across the rto_mask.
1046 */
1047 struct irq_work rto_push_work;
1048 raw_spinlock_t rto_lock;
1049 /* These are only updated and read within rto_lock */
1050 int rto_loop;
1051 int rto_cpu;
1052 /* These atomics are updated outside of a lock */
1053 atomic_t rto_loop_next;
1054 atomic_t rto_loop_start;
1055 #endif
1056 /*
1057 * The "RT overload" flag: it gets set if a CPU has more than
1058 * one runnable RT task.
1059 */
1060 cpumask_var_t rto_mask;
1061 struct cpupri cpupri;
1062
1063 /*
1064 * NULL-terminated list of performance domains intersecting with the
1065 * CPUs of the rd. Protected by RCU.
1066 */
1067 struct perf_domain __rcu *pd;
1068
1069 ANDROID_VENDOR_DATA(1);
1070
1071 ANDROID_KABI_RESERVE(1);
1072 ANDROID_KABI_RESERVE(2);
1073 ANDROID_KABI_RESERVE(3);
1074 ANDROID_KABI_RESERVE(4);
1075 };
1076
1077 extern void init_defrootdomain(void);
1078 extern int sched_init_domains(const struct cpumask *cpu_map);
1079 extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
1080 extern void sched_get_rd(struct root_domain *rd);
1081 extern void sched_put_rd(struct root_domain *rd);
1082
get_rd_overloaded(struct root_domain * rd)1083 static inline int get_rd_overloaded(struct root_domain *rd)
1084 {
1085 return READ_ONCE(rd->overloaded);
1086 }
1087
set_rd_overloaded(struct root_domain * rd,int status)1088 static inline void set_rd_overloaded(struct root_domain *rd, int status)
1089 {
1090 if (get_rd_overloaded(rd) != status)
1091 WRITE_ONCE(rd->overloaded, status);
1092 }
1093
1094 #ifdef HAVE_RT_PUSH_IPI
1095 extern void rto_push_irq_work_func(struct irq_work *work);
1096 #endif
1097 extern struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu);
1098 #endif /* CONFIG_SMP */
1099
1100 #ifdef CONFIG_UCLAMP_TASK
1101 /*
1102 * struct uclamp_bucket - Utilization clamp bucket
1103 * @value: utilization clamp value for tasks on this clamp bucket
1104 * @tasks: number of RUNNABLE tasks on this clamp bucket
1105 *
1106 * Keep track of how many tasks are RUNNABLE for a given utilization
1107 * clamp value.
1108 */
1109 struct uclamp_bucket {
1110 unsigned long value : bits_per(SCHED_CAPACITY_SCALE);
1111 unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE);
1112 };
1113
1114 /*
1115 * struct uclamp_rq - rq's utilization clamp
1116 * @value: currently active clamp values for a rq
1117 * @bucket: utilization clamp buckets affecting a rq
1118 *
1119 * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values.
1120 * A clamp value is affecting a rq when there is at least one task RUNNABLE
1121 * (or actually running) with that value.
1122 *
1123 * There are up to UCLAMP_CNT possible different clamp values, currently there
1124 * are only two: minimum utilization and maximum utilization.
1125 *
1126 * All utilization clamping values are MAX aggregated, since:
1127 * - for util_min: we want to run the CPU at least at the max of the minimum
1128 * utilization required by its currently RUNNABLE tasks.
1129 * - for util_max: we want to allow the CPU to run up to the max of the
1130 * maximum utilization allowed by its currently RUNNABLE tasks.
1131 *
1132 * Since on each system we expect only a limited number of different
1133 * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track
1134 * the metrics required to compute all the per-rq utilization clamp values.
1135 */
1136 struct uclamp_rq {
1137 unsigned int value;
1138 struct uclamp_bucket bucket[UCLAMP_BUCKETS];
1139 };
1140
1141 DECLARE_STATIC_KEY_FALSE(sched_uclamp_used);
1142 #endif /* CONFIG_UCLAMP_TASK */
1143
1144 typedef enum misfit_reason {
1145 MISFIT_PERF, /* Requires moving to a more performant CPU */
1146 } misfit_reason_t;
1147
1148 /*
1149 * This is the main, per-CPU runqueue data structure.
1150 *
1151 * Locking rule: those places that want to lock multiple runqueues
1152 * (such as the load balancing or the thread migration code), lock
1153 * acquire operations must be ordered by ascending &runqueue.
1154 */
1155 struct rq {
1156 /* runqueue lock: */
1157 raw_spinlock_t __lock;
1158
1159 unsigned int nr_running;
1160 #ifdef CONFIG_NUMA_BALANCING
1161 unsigned int nr_numa_running;
1162 unsigned int nr_preferred_running;
1163 unsigned int numa_migrate_on;
1164 #endif
1165 #ifdef CONFIG_NO_HZ_COMMON
1166 #ifdef CONFIG_SMP
1167 unsigned long last_blocked_load_update_tick;
1168 unsigned int has_blocked_load;
1169 call_single_data_t nohz_csd;
1170 #endif /* CONFIG_SMP */
1171 unsigned int nohz_tick_stopped;
1172 atomic_t nohz_flags;
1173 #endif /* CONFIG_NO_HZ_COMMON */
1174
1175 #ifdef CONFIG_SMP
1176 unsigned int ttwu_pending;
1177 #endif
1178 u64 nr_switches;
1179
1180 #ifdef CONFIG_UCLAMP_TASK
1181 /* Utilization clamp values based on CPU's RUNNABLE tasks */
1182 struct uclamp_rq uclamp[UCLAMP_CNT] ____cacheline_aligned;
1183 unsigned int uclamp_flags;
1184 #define UCLAMP_FLAG_IDLE 0x01
1185 #endif
1186
1187 struct cfs_rq cfs;
1188 struct rt_rq rt;
1189 struct dl_rq dl;
1190 #ifdef CONFIG_SCHED_CLASS_EXT
1191 struct scx_rq scx;
1192 #endif
1193
1194 struct sched_dl_entity fair_server;
1195
1196 #ifdef CONFIG_FAIR_GROUP_SCHED
1197 /* list of leaf cfs_rq on this CPU: */
1198 struct list_head leaf_cfs_rq_list;
1199 struct list_head *tmp_alone_branch;
1200 #endif /* CONFIG_FAIR_GROUP_SCHED */
1201
1202 /*
1203 * This is part of a global counter where only the total sum
1204 * over all CPUs matters. A task can increase this counter on
1205 * one CPU and if it got migrated afterwards it may decrease
1206 * it on another CPU. Always updated under the runqueue lock:
1207 */
1208 unsigned int nr_uninterruptible;
1209
1210 #ifdef CONFIG_SCHED_PROXY_EXEC
1211 struct task_struct __rcu *donor; /* Scheduling context */
1212 struct task_struct __rcu *curr; /* Execution context */
1213 #else
1214 union {
1215 struct task_struct __rcu *donor; /* Scheduler context */
1216 struct task_struct __rcu *curr; /* Execution context */
1217 };
1218 #endif
1219 struct sched_dl_entity *dl_server;
1220 struct task_struct *idle;
1221 struct task_struct *stop;
1222 unsigned long next_balance;
1223 struct mm_struct *prev_mm;
1224
1225 unsigned int clock_update_flags;
1226 u64 clock;
1227 /* Ensure that all clocks are in the same cache line */
1228 u64 clock_task ____cacheline_aligned;
1229 u64 clock_task_mult;
1230 u64 clock_pelt;
1231 unsigned long lost_idle_time;
1232 u64 clock_pelt_idle;
1233 u64 clock_idle;
1234 #ifndef CONFIG_64BIT
1235 u64 clock_pelt_idle_copy;
1236 u64 clock_idle_copy;
1237 #endif
1238
1239 atomic_t nr_iowait;
1240
1241 #ifdef CONFIG_SCHED_DEBUG
1242 u64 last_seen_need_resched_ns;
1243 int ticks_without_resched;
1244 #endif
1245
1246 #ifdef CONFIG_MEMBARRIER
1247 int membarrier_state;
1248 #endif
1249
1250 #ifdef CONFIG_SMP
1251 struct root_domain *rd;
1252 struct sched_domain __rcu *sd;
1253
1254 unsigned long cpu_capacity;
1255
1256 struct balance_callback *balance_callback;
1257
1258 unsigned char nohz_idle_balance;
1259 unsigned char idle_balance;
1260
1261 unsigned long misfit_task_load;
1262
1263 /* For active balancing */
1264 int active_balance;
1265 int push_cpu;
1266 struct cpu_stop_work active_balance_work;
1267
1268 /* CPU of this runqueue: */
1269 int cpu;
1270 int online;
1271
1272 struct list_head cfs_tasks;
1273
1274 struct sched_avg avg_rt;
1275 struct sched_avg avg_dl;
1276 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
1277 struct sched_avg avg_irq;
1278 #endif
1279 #ifdef CONFIG_SCHED_HW_PRESSURE
1280 struct sched_avg avg_hw;
1281 #endif
1282 u64 idle_stamp;
1283 u64 avg_idle;
1284
1285 /* This is used to determine avg_idle's max value */
1286 u64 max_idle_balance_cost;
1287
1288 #ifdef CONFIG_HOTPLUG_CPU
1289 struct rcuwait hotplug_wait;
1290 #endif
1291 #endif /* CONFIG_SMP */
1292
1293 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1294 u64 prev_irq_time;
1295 u64 psi_irq_time;
1296 #endif
1297 #ifdef CONFIG_PARAVIRT
1298 u64 prev_steal_time;
1299 #endif
1300 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
1301 u64 prev_steal_time_rq;
1302 #endif
1303
1304 /* calc_load related fields */
1305 unsigned long calc_load_update;
1306 long calc_load_active;
1307
1308 #ifdef CONFIG_SCHED_HRTICK
1309 #ifdef CONFIG_SMP
1310 call_single_data_t hrtick_csd;
1311 #endif
1312 struct hrtimer hrtick_timer;
1313 ktime_t hrtick_time;
1314 #endif
1315
1316 #ifdef CONFIG_SCHEDSTATS
1317 /* latency stats */
1318 struct sched_info rq_sched_info;
1319 unsigned long long rq_cpu_time;
1320
1321 /* sys_sched_yield() stats */
1322 unsigned int yld_count;
1323
1324 /* schedule() stats */
1325 unsigned int sched_count;
1326 unsigned int sched_goidle;
1327
1328 /* try_to_wake_up() stats */
1329 unsigned int ttwu_count;
1330 unsigned int ttwu_local;
1331 #endif
1332
1333 #ifdef CONFIG_CPU_IDLE
1334 /* Must be inspected within a RCU lock section */
1335 struct cpuidle_state *idle_state;
1336 #endif
1337
1338 #ifdef CONFIG_SMP
1339 unsigned int nr_pinned;
1340 #endif
1341 unsigned int push_busy;
1342 struct cpu_stop_work push_work;
1343
1344 #ifdef CONFIG_SCHED_CORE
1345 /* per rq */
1346 struct rq *core;
1347 struct task_struct *core_pick;
1348 struct sched_dl_entity *core_dl_server;
1349 unsigned int core_enabled;
1350 unsigned int core_sched_seq;
1351 struct rb_root core_tree;
1352
1353 /* shared state -- careful with sched_core_cpu_deactivate() */
1354 unsigned int core_task_seq;
1355 unsigned int core_pick_seq;
1356 unsigned long core_cookie;
1357 unsigned int core_forceidle_count;
1358 unsigned int core_forceidle_seq;
1359 unsigned int core_forceidle_occupation;
1360 u64 core_forceidle_start;
1361 #endif
1362
1363 /* Scratch cpumask to be temporarily used under rq_lock */
1364 cpumask_var_t scratch_mask;
1365
1366 #if defined(CONFIG_CFS_BANDWIDTH) && defined(CONFIG_SMP)
1367 call_single_data_t cfsb_csd;
1368 struct list_head cfsb_csd_list;
1369 #endif
1370
1371 #ifdef CONFIG_SMP
1372 misfit_reason_t misfit_reason;
1373 #endif
1374
1375 ANDROID_KABI_RESERVE(1);
1376 ANDROID_KABI_RESERVE(2);
1377 ANDROID_KABI_RESERVE(3);
1378 ANDROID_KABI_RESERVE(4);
1379 ANDROID_OEM_DATA_ARRAY(1, 16);
1380 };
1381
1382 #ifdef CONFIG_FAIR_GROUP_SCHED
1383
1384 /* CPU runqueue to which this cfs_rq is attached */
rq_of(struct cfs_rq * cfs_rq)1385 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1386 {
1387 return cfs_rq->rq;
1388 }
1389
1390 #else
1391
rq_of(struct cfs_rq * cfs_rq)1392 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1393 {
1394 return container_of(cfs_rq, struct rq, cfs);
1395 }
1396 #endif
1397
cpu_of(struct rq * rq)1398 static inline int cpu_of(struct rq *rq)
1399 {
1400 #ifdef CONFIG_SMP
1401 return rq->cpu;
1402 #else
1403 return 0;
1404 #endif
1405 }
1406
1407 #define MDF_PUSH 0x01
1408
is_migration_disabled(struct task_struct * p)1409 static inline bool is_migration_disabled(struct task_struct *p)
1410 {
1411 #ifdef CONFIG_SMP
1412 return p->migration_disabled;
1413 #else
1414 return false;
1415 #endif
1416 }
1417
1418 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1419
1420 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
1421 #define this_rq() this_cpu_ptr(&runqueues)
1422 #define task_rq(p) cpu_rq(task_cpu(p))
1423 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
1424 #define raw_rq() raw_cpu_ptr(&runqueues)
1425
1426 #ifdef CONFIG_SCHED_PROXY_EXEC
rq_set_donor(struct rq * rq,struct task_struct * t)1427 static inline void rq_set_donor(struct rq *rq, struct task_struct *t)
1428 {
1429 rcu_assign_pointer(rq->donor, t);
1430 }
1431 #else
rq_set_donor(struct rq * rq,struct task_struct * t)1432 static inline void rq_set_donor(struct rq *rq, struct task_struct *t)
1433 {
1434 /* Do nothing */
1435 }
1436 #endif
1437
1438 #ifdef CONFIG_SCHED_CORE
1439 static inline struct cpumask *sched_group_span(struct sched_group *sg);
1440
1441 DECLARE_STATIC_KEY_FALSE(__sched_core_enabled);
1442
sched_core_enabled(struct rq * rq)1443 static inline bool sched_core_enabled(struct rq *rq)
1444 {
1445 return static_branch_unlikely(&__sched_core_enabled) && rq->core_enabled;
1446 }
1447
sched_core_disabled(void)1448 static inline bool sched_core_disabled(void)
1449 {
1450 return !static_branch_unlikely(&__sched_core_enabled);
1451 }
1452
1453 /*
1454 * Be careful with this function; not for general use. The return value isn't
1455 * stable unless you actually hold a relevant rq->__lock.
1456 */
rq_lockp(struct rq * rq)1457 static inline raw_spinlock_t *rq_lockp(struct rq *rq)
1458 {
1459 if (sched_core_enabled(rq))
1460 return &rq->core->__lock;
1461
1462 return &rq->__lock;
1463 }
1464
__rq_lockp(struct rq * rq)1465 static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
1466 {
1467 if (rq->core_enabled)
1468 return &rq->core->__lock;
1469
1470 return &rq->__lock;
1471 }
1472
1473 extern bool
1474 cfs_prio_less(const struct task_struct *a, const struct task_struct *b, bool fi);
1475
1476 extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi);
1477
1478 /*
1479 * Helpers to check if the CPU's core cookie matches with the task's cookie
1480 * when core scheduling is enabled.
1481 * A special case is that the task's cookie always matches with CPU's core
1482 * cookie if the CPU is in an idle core.
1483 */
sched_cpu_cookie_match(struct rq * rq,struct task_struct * p)1484 static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
1485 {
1486 /* Ignore cookie match if core scheduler is not enabled on the CPU. */
1487 if (!sched_core_enabled(rq))
1488 return true;
1489
1490 return rq->core->core_cookie == p->core_cookie;
1491 }
1492
sched_core_cookie_match(struct rq * rq,struct task_struct * p)1493 static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
1494 {
1495 bool idle_core = true;
1496 int cpu;
1497
1498 /* Ignore cookie match if core scheduler is not enabled on the CPU. */
1499 if (!sched_core_enabled(rq))
1500 return true;
1501
1502 for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
1503 if (!available_idle_cpu(cpu)) {
1504 idle_core = false;
1505 break;
1506 }
1507 }
1508
1509 /*
1510 * A CPU in an idle core is always the best choice for tasks with
1511 * cookies.
1512 */
1513 return idle_core || rq->core->core_cookie == p->core_cookie;
1514 }
1515
sched_group_cookie_match(struct rq * rq,struct task_struct * p,struct sched_group * group)1516 static inline bool sched_group_cookie_match(struct rq *rq,
1517 struct task_struct *p,
1518 struct sched_group *group)
1519 {
1520 int cpu;
1521
1522 /* Ignore cookie match if core scheduler is not enabled on the CPU. */
1523 if (!sched_core_enabled(rq))
1524 return true;
1525
1526 for_each_cpu_and(cpu, sched_group_span(group), p->cpus_ptr) {
1527 if (sched_core_cookie_match(cpu_rq(cpu), p))
1528 return true;
1529 }
1530 return false;
1531 }
1532
sched_core_enqueued(struct task_struct * p)1533 static inline bool sched_core_enqueued(struct task_struct *p)
1534 {
1535 return !RB_EMPTY_NODE(&p->core_node);
1536 }
1537
1538 extern void sched_core_enqueue(struct rq *rq, struct task_struct *p);
1539 extern void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags);
1540
1541 extern void sched_core_get(void);
1542 extern void sched_core_put(void);
1543
1544 #else /* !CONFIG_SCHED_CORE: */
1545
sched_core_enabled(struct rq * rq)1546 static inline bool sched_core_enabled(struct rq *rq)
1547 {
1548 return false;
1549 }
1550
sched_core_disabled(void)1551 static inline bool sched_core_disabled(void)
1552 {
1553 return true;
1554 }
1555
rq_lockp(struct rq * rq)1556 static inline raw_spinlock_t *rq_lockp(struct rq *rq)
1557 {
1558 return &rq->__lock;
1559 }
1560
__rq_lockp(struct rq * rq)1561 static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
1562 {
1563 return &rq->__lock;
1564 }
1565
sched_cpu_cookie_match(struct rq * rq,struct task_struct * p)1566 static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
1567 {
1568 return true;
1569 }
1570
sched_core_cookie_match(struct rq * rq,struct task_struct * p)1571 static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
1572 {
1573 return true;
1574 }
1575
sched_group_cookie_match(struct rq * rq,struct task_struct * p,struct sched_group * group)1576 static inline bool sched_group_cookie_match(struct rq *rq,
1577 struct task_struct *p,
1578 struct sched_group *group)
1579 {
1580 return true;
1581 }
1582
1583 #endif /* !CONFIG_SCHED_CORE */
1584
lockdep_assert_rq_held(struct rq * rq)1585 static inline void lockdep_assert_rq_held(struct rq *rq)
1586 {
1587 lockdep_assert_held(__rq_lockp(rq));
1588 }
1589
1590 extern void raw_spin_rq_lock_nested(struct rq *rq, int subclass);
1591 extern bool raw_spin_rq_trylock(struct rq *rq);
1592 extern void raw_spin_rq_unlock(struct rq *rq);
1593
raw_spin_rq_lock(struct rq * rq)1594 static inline void raw_spin_rq_lock(struct rq *rq)
1595 {
1596 raw_spin_rq_lock_nested(rq, 0);
1597 }
1598
raw_spin_rq_lock_irq(struct rq * rq)1599 static inline void raw_spin_rq_lock_irq(struct rq *rq)
1600 {
1601 local_irq_disable();
1602 raw_spin_rq_lock(rq);
1603 }
1604
raw_spin_rq_unlock_irq(struct rq * rq)1605 static inline void raw_spin_rq_unlock_irq(struct rq *rq)
1606 {
1607 raw_spin_rq_unlock(rq);
1608 local_irq_enable();
1609 }
1610
_raw_spin_rq_lock_irqsave(struct rq * rq)1611 static inline unsigned long _raw_spin_rq_lock_irqsave(struct rq *rq)
1612 {
1613 unsigned long flags;
1614
1615 local_irq_save(flags);
1616 raw_spin_rq_lock(rq);
1617
1618 return flags;
1619 }
1620
raw_spin_rq_unlock_irqrestore(struct rq * rq,unsigned long flags)1621 static inline void raw_spin_rq_unlock_irqrestore(struct rq *rq, unsigned long flags)
1622 {
1623 raw_spin_rq_unlock(rq);
1624 local_irq_restore(flags);
1625 }
1626
1627 #define raw_spin_rq_lock_irqsave(rq, flags) \
1628 do { \
1629 flags = _raw_spin_rq_lock_irqsave(rq); \
1630 } while (0)
1631
1632 #ifdef CONFIG_SCHED_SMT
1633 extern void __update_idle_core(struct rq *rq);
1634
update_idle_core(struct rq * rq)1635 static inline void update_idle_core(struct rq *rq)
1636 {
1637 if (static_branch_unlikely(&sched_smt_present))
1638 __update_idle_core(rq);
1639 }
1640
1641 #else
update_idle_core(struct rq * rq)1642 static inline void update_idle_core(struct rq *rq) { }
1643 #endif
1644
1645 #ifdef CONFIG_FAIR_GROUP_SCHED
1646
task_of(struct sched_entity * se)1647 static inline struct task_struct *task_of(struct sched_entity *se)
1648 {
1649 SCHED_WARN_ON(!entity_is_task(se));
1650 return container_of(se, struct task_struct, se);
1651 }
1652
task_cfs_rq(struct task_struct * p)1653 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
1654 {
1655 return p->se.cfs_rq;
1656 }
1657
1658 /* runqueue on which this entity is (to be) queued */
cfs_rq_of(const struct sched_entity * se)1659 static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
1660 {
1661 return se->cfs_rq;
1662 }
1663
1664 /* runqueue "owned" by this group */
group_cfs_rq(struct sched_entity * grp)1665 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
1666 {
1667 return grp->my_q;
1668 }
1669
1670 #else /* !CONFIG_FAIR_GROUP_SCHED: */
1671
1672 #define task_of(_se) container_of(_se, struct task_struct, se)
1673
task_cfs_rq(const struct task_struct * p)1674 static inline struct cfs_rq *task_cfs_rq(const struct task_struct *p)
1675 {
1676 return &task_rq(p)->cfs;
1677 }
1678
cfs_rq_of(const struct sched_entity * se)1679 static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
1680 {
1681 const struct task_struct *p = task_of(se);
1682 struct rq *rq = task_rq(p);
1683
1684 return &rq->cfs;
1685 }
1686
1687 /* runqueue "owned" by this group */
group_cfs_rq(struct sched_entity * grp)1688 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
1689 {
1690 return NULL;
1691 }
1692
1693 #endif /* !CONFIG_FAIR_GROUP_SCHED */
1694
1695 extern void update_rq_clock(struct rq *rq);
1696
1697 /*
1698 * rq::clock_update_flags bits
1699 *
1700 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
1701 * call to __schedule(). This is an optimisation to avoid
1702 * neighbouring rq clock updates.
1703 *
1704 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
1705 * in effect and calls to update_rq_clock() are being ignored.
1706 *
1707 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
1708 * made to update_rq_clock() since the last time rq::lock was pinned.
1709 *
1710 * If inside of __schedule(), clock_update_flags will have been
1711 * shifted left (a left shift is a cheap operation for the fast path
1712 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
1713 *
1714 * if (rq-clock_update_flags >= RQCF_UPDATED)
1715 *
1716 * to check if %RQCF_UPDATED is set. It'll never be shifted more than
1717 * one position though, because the next rq_unpin_lock() will shift it
1718 * back.
1719 */
1720 #define RQCF_REQ_SKIP 0x01
1721 #define RQCF_ACT_SKIP 0x02
1722 #define RQCF_UPDATED 0x04
1723
assert_clock_updated(struct rq * rq)1724 static inline void assert_clock_updated(struct rq *rq)
1725 {
1726 /*
1727 * The only reason for not seeing a clock update since the
1728 * last rq_pin_lock() is if we're currently skipping updates.
1729 */
1730 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
1731 }
1732
rq_clock(struct rq * rq)1733 static inline u64 rq_clock(struct rq *rq)
1734 {
1735 lockdep_assert_rq_held(rq);
1736 assert_clock_updated(rq);
1737
1738 return rq->clock;
1739 }
1740
rq_clock_task(struct rq * rq)1741 static inline u64 rq_clock_task(struct rq *rq)
1742 {
1743 lockdep_assert_rq_held(rq);
1744 assert_clock_updated(rq);
1745
1746 return rq->clock_task;
1747 }
1748
rq_clock_skip_update(struct rq * rq)1749 static inline void rq_clock_skip_update(struct rq *rq)
1750 {
1751 lockdep_assert_rq_held(rq);
1752 rq->clock_update_flags |= RQCF_REQ_SKIP;
1753 }
1754
1755 /*
1756 * See rt task throttling, which is the only time a skip
1757 * request is canceled.
1758 */
rq_clock_cancel_skipupdate(struct rq * rq)1759 static inline void rq_clock_cancel_skipupdate(struct rq *rq)
1760 {
1761 lockdep_assert_rq_held(rq);
1762 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
1763 }
1764
1765 /*
1766 * During cpu offlining and rq wide unthrottling, we can trigger
1767 * an update_rq_clock() for several cfs and rt runqueues (Typically
1768 * when using list_for_each_entry_*)
1769 * rq_clock_start_loop_update() can be called after updating the clock
1770 * once and before iterating over the list to prevent multiple update.
1771 * After the iterative traversal, we need to call rq_clock_stop_loop_update()
1772 * to clear RQCF_ACT_SKIP of rq->clock_update_flags.
1773 */
rq_clock_start_loop_update(struct rq * rq)1774 static inline void rq_clock_start_loop_update(struct rq *rq)
1775 {
1776 lockdep_assert_rq_held(rq);
1777 SCHED_WARN_ON(rq->clock_update_flags & RQCF_ACT_SKIP);
1778 rq->clock_update_flags |= RQCF_ACT_SKIP;
1779 }
1780
rq_clock_stop_loop_update(struct rq * rq)1781 static inline void rq_clock_stop_loop_update(struct rq *rq)
1782 {
1783 lockdep_assert_rq_held(rq);
1784 rq->clock_update_flags &= ~RQCF_ACT_SKIP;
1785 }
1786
1787 struct rq_flags {
1788 unsigned long flags;
1789 struct pin_cookie cookie;
1790 #ifdef CONFIG_SCHED_DEBUG
1791 /*
1792 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
1793 * current pin context is stashed here in case it needs to be
1794 * restored in rq_repin_lock().
1795 */
1796 unsigned int clock_update_flags;
1797 #endif
1798 };
1799
1800 #ifdef CONFIG_SMP
1801 extern struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf,
1802 struct task_struct *p, int dest_cpu);
1803 #endif
1804
1805 extern struct balance_callback balance_push_callback;
1806
1807 /*
1808 * Lockdep annotation that avoids accidental unlocks; it's like a
1809 * sticky/continuous lockdep_assert_held().
1810 *
1811 * This avoids code that has access to 'struct rq *rq' (basically everything in
1812 * the scheduler) from accidentally unlocking the rq if they do not also have a
1813 * copy of the (on-stack) 'struct rq_flags rf'.
1814 *
1815 * Also see Documentation/locking/lockdep-design.rst.
1816 */
rq_pin_lock(struct rq * rq,struct rq_flags * rf)1817 static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
1818 {
1819 rf->cookie = lockdep_pin_lock(__rq_lockp(rq));
1820
1821 #ifdef CONFIG_SCHED_DEBUG
1822 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
1823 rf->clock_update_flags = 0;
1824 # ifdef CONFIG_SMP
1825 SCHED_WARN_ON(rq->balance_callback && rq->balance_callback != &balance_push_callback);
1826 # endif
1827 #endif
1828 }
1829
rq_unpin_lock(struct rq * rq,struct rq_flags * rf)1830 static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
1831 {
1832 #ifdef CONFIG_SCHED_DEBUG
1833 if (rq->clock_update_flags > RQCF_ACT_SKIP)
1834 rf->clock_update_flags = RQCF_UPDATED;
1835 #endif
1836
1837 lockdep_unpin_lock(__rq_lockp(rq), rf->cookie);
1838 }
1839
rq_repin_lock(struct rq * rq,struct rq_flags * rf)1840 static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
1841 {
1842 lockdep_repin_lock(__rq_lockp(rq), rf->cookie);
1843
1844 #ifdef CONFIG_SCHED_DEBUG
1845 /*
1846 * Restore the value we stashed in @rf for this pin context.
1847 */
1848 rq->clock_update_flags |= rf->clock_update_flags;
1849 #endif
1850 }
1851
1852 extern
1853 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1854 __acquires(rq->lock);
1855
1856 extern
1857 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1858 __acquires(p->pi_lock)
1859 __acquires(rq->lock);
1860
__task_rq_unlock(struct rq * rq,struct rq_flags * rf)1861 static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
1862 __releases(rq->lock)
1863 {
1864 rq_unpin_lock(rq, rf);
1865 raw_spin_rq_unlock(rq);
1866 }
1867
1868 static inline void
task_rq_unlock(struct rq * rq,struct task_struct * p,struct rq_flags * rf)1869 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1870 __releases(rq->lock)
1871 __releases(p->pi_lock)
1872 {
1873 rq_unpin_lock(rq, rf);
1874 raw_spin_rq_unlock(rq);
1875 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
1876 }
1877
1878 DEFINE_LOCK_GUARD_1(task_rq_lock, struct task_struct,
1879 _T->rq = task_rq_lock(_T->lock, &_T->rf),
1880 task_rq_unlock(_T->rq, _T->lock, &_T->rf),
1881 struct rq *rq; struct rq_flags rf)
1882
rq_lock_irqsave(struct rq * rq,struct rq_flags * rf)1883 static inline void rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1884 __acquires(rq->lock)
1885 {
1886 raw_spin_rq_lock_irqsave(rq, rf->flags);
1887 rq_pin_lock(rq, rf);
1888 }
1889
rq_lock_irq(struct rq * rq,struct rq_flags * rf)1890 static inline void rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1891 __acquires(rq->lock)
1892 {
1893 raw_spin_rq_lock_irq(rq);
1894 rq_pin_lock(rq, rf);
1895 }
1896
rq_lock(struct rq * rq,struct rq_flags * rf)1897 static inline void rq_lock(struct rq *rq, struct rq_flags *rf)
1898 __acquires(rq->lock)
1899 {
1900 raw_spin_rq_lock(rq);
1901 rq_pin_lock(rq, rf);
1902 }
1903
rq_unlock_irqrestore(struct rq * rq,struct rq_flags * rf)1904 static inline void rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1905 __releases(rq->lock)
1906 {
1907 rq_unpin_lock(rq, rf);
1908 raw_spin_rq_unlock_irqrestore(rq, rf->flags);
1909 }
1910
rq_unlock_irq(struct rq * rq,struct rq_flags * rf)1911 static inline void rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1912 __releases(rq->lock)
1913 {
1914 rq_unpin_lock(rq, rf);
1915 raw_spin_rq_unlock_irq(rq);
1916 }
1917
rq_unlock(struct rq * rq,struct rq_flags * rf)1918 static inline void rq_unlock(struct rq *rq, struct rq_flags *rf)
1919 __releases(rq->lock)
1920 {
1921 rq_unpin_lock(rq, rf);
1922 raw_spin_rq_unlock(rq);
1923 }
1924
1925 DEFINE_LOCK_GUARD_1(rq_lock, struct rq,
1926 rq_lock(_T->lock, &_T->rf),
1927 rq_unlock(_T->lock, &_T->rf),
1928 struct rq_flags rf)
1929
1930 DEFINE_LOCK_GUARD_1(rq_lock_irq, struct rq,
1931 rq_lock_irq(_T->lock, &_T->rf),
1932 rq_unlock_irq(_T->lock, &_T->rf),
1933 struct rq_flags rf)
1934
1935 DEFINE_LOCK_GUARD_1(rq_lock_irqsave, struct rq,
1936 rq_lock_irqsave(_T->lock, &_T->rf),
1937 rq_unlock_irqrestore(_T->lock, &_T->rf),
1938 struct rq_flags rf)
1939
this_rq_lock_irq(struct rq_flags * rf)1940 static inline struct rq *this_rq_lock_irq(struct rq_flags *rf)
1941 __acquires(rq->lock)
1942 {
1943 struct rq *rq;
1944
1945 local_irq_disable();
1946 rq = this_rq();
1947 rq_lock(rq, rf);
1948
1949 return rq;
1950 }
1951
1952 #ifdef CONFIG_NUMA
1953
1954 enum numa_topology_type {
1955 NUMA_DIRECT,
1956 NUMA_GLUELESS_MESH,
1957 NUMA_BACKPLANE,
1958 };
1959
1960 extern enum numa_topology_type sched_numa_topology_type;
1961 extern int sched_max_numa_distance;
1962 extern bool find_numa_distance(int distance);
1963 extern void sched_init_numa(int offline_node);
1964 extern void sched_update_numa(int cpu, bool online);
1965 extern void sched_domains_numa_masks_set(unsigned int cpu);
1966 extern void sched_domains_numa_masks_clear(unsigned int cpu);
1967 extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu);
1968
1969 #else /* !CONFIG_NUMA: */
1970
sched_init_numa(int offline_node)1971 static inline void sched_init_numa(int offline_node) { }
sched_update_numa(int cpu,bool online)1972 static inline void sched_update_numa(int cpu, bool online) { }
sched_domains_numa_masks_set(unsigned int cpu)1973 static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
sched_domains_numa_masks_clear(unsigned int cpu)1974 static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
1975
sched_numa_find_closest(const struct cpumask * cpus,int cpu)1976 static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1977 {
1978 return nr_cpu_ids;
1979 }
1980
1981 #endif /* !CONFIG_NUMA */
1982
1983 #ifdef CONFIG_NUMA_BALANCING
1984
1985 /* The regions in numa_faults array from task_struct */
1986 enum numa_faults_stats {
1987 NUMA_MEM = 0,
1988 NUMA_CPU,
1989 NUMA_MEMBUF,
1990 NUMA_CPUBUF
1991 };
1992
1993 extern void sched_setnuma(struct task_struct *p, int node);
1994 extern int migrate_task_to(struct task_struct *p, int cpu);
1995 extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p);
1996
1997 #else /* !CONFIG_NUMA_BALANCING: */
1998
1999 static inline void
init_numa_balancing(unsigned long clone_flags,struct task_struct * p)2000 init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
2001 {
2002 }
2003
2004 #endif /* !CONFIG_NUMA_BALANCING */
2005
2006 #ifdef CONFIG_SMP
2007
2008 extern int migrate_swap(struct task_struct *p, struct task_struct *t,
2009 int cpu, int scpu);
2010
2011 static inline void
queue_balance_callback(struct rq * rq,struct balance_callback * head,void (* func)(struct rq * rq))2012 queue_balance_callback(struct rq *rq,
2013 struct balance_callback *head,
2014 void (*func)(struct rq *rq))
2015 {
2016 lockdep_assert_rq_held(rq);
2017
2018 /*
2019 * Don't (re)queue an already queued item; nor queue anything when
2020 * balance_push() is active, see the comment with
2021 * balance_push_callback.
2022 */
2023 if (unlikely(head->next || rq->balance_callback == &balance_push_callback))
2024 return;
2025
2026 head->func = func;
2027 head->next = rq->balance_callback;
2028 rq->balance_callback = head;
2029 }
2030
2031 #define rcu_dereference_check_sched_domain(p) \
2032 rcu_dereference_check((p), lockdep_is_held(&sched_domains_mutex))
2033
2034 /*
2035 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
2036 * See destroy_sched_domains: call_rcu for details.
2037 *
2038 * The domain tree of any CPU may only be accessed from within
2039 * preempt-disabled sections.
2040 */
2041 #define for_each_domain(cpu, __sd) \
2042 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
2043 __sd; __sd = __sd->parent)
2044
2045 /* A mask of all the SD flags that have the SDF_SHARED_CHILD metaflag */
2046 #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_SHARED_CHILD)) |
2047 static const unsigned int SD_SHARED_CHILD_MASK =
2048 #include <linux/sched/sd_flags.h>
2049 0;
2050 #undef SD_FLAG
2051
2052 /**
2053 * highest_flag_domain - Return highest sched_domain containing flag.
2054 * @cpu: The CPU whose highest level of sched domain is to
2055 * be returned.
2056 * @flag: The flag to check for the highest sched_domain
2057 * for the given CPU.
2058 *
2059 * Returns the highest sched_domain of a CPU which contains @flag. If @flag has
2060 * the SDF_SHARED_CHILD metaflag, all the children domains also have @flag.
2061 */
highest_flag_domain(int cpu,int flag)2062 static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
2063 {
2064 struct sched_domain *sd, *hsd = NULL;
2065
2066 for_each_domain(cpu, sd) {
2067 if (sd->flags & flag) {
2068 hsd = sd;
2069 continue;
2070 }
2071
2072 /*
2073 * Stop the search if @flag is known to be shared at lower
2074 * levels. It will not be found further up.
2075 */
2076 if (flag & SD_SHARED_CHILD_MASK)
2077 break;
2078 }
2079
2080 return hsd;
2081 }
2082
lowest_flag_domain(int cpu,int flag)2083 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
2084 {
2085 struct sched_domain *sd;
2086
2087 for_each_domain(cpu, sd) {
2088 if (sd->flags & flag)
2089 break;
2090 }
2091
2092 return sd;
2093 }
2094
2095 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
2096 DECLARE_PER_CPU(int, sd_llc_size);
2097 DECLARE_PER_CPU(int, sd_llc_id);
2098 DECLARE_PER_CPU(int, sd_share_id);
2099 DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
2100 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
2101 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
2102 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
2103
2104 extern struct static_key_false sched_asym_cpucapacity;
2105 extern struct static_key_false sched_cluster_active;
2106
sched_asym_cpucap_active(void)2107 static __always_inline bool sched_asym_cpucap_active(void)
2108 {
2109 return static_branch_unlikely(&sched_asym_cpucapacity);
2110 }
2111
2112 struct sched_group_capacity {
2113 atomic_t ref;
2114 /*
2115 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
2116 * for a single CPU.
2117 */
2118 unsigned long capacity;
2119 unsigned long min_capacity; /* Min per-CPU capacity in group */
2120 unsigned long max_capacity; /* Max per-CPU capacity in group */
2121 unsigned long next_update;
2122 int imbalance; /* XXX unrelated to capacity but shared group state */
2123
2124 #ifdef CONFIG_SCHED_DEBUG
2125 int id;
2126 #endif
2127
2128 unsigned long cpumask[]; /* Balance mask */
2129 };
2130
2131 struct sched_group {
2132 struct sched_group *next; /* Must be a circular list */
2133 atomic_t ref;
2134
2135 unsigned int group_weight;
2136 unsigned int cores;
2137 struct sched_group_capacity *sgc;
2138 int asym_prefer_cpu; /* CPU of highest priority in group */
2139 int flags;
2140
2141 /*
2142 * The CPUs this group covers.
2143 *
2144 * NOTE: this field is variable length. (Allocated dynamically
2145 * by attaching extra space to the end of the structure,
2146 * depending on how many CPUs the kernel has booted up with)
2147 */
2148 unsigned long cpumask[];
2149 };
2150
sched_group_span(struct sched_group * sg)2151 static inline struct cpumask *sched_group_span(struct sched_group *sg)
2152 {
2153 return to_cpumask(sg->cpumask);
2154 }
2155
2156 /*
2157 * See build_balance_mask().
2158 */
group_balance_mask(struct sched_group * sg)2159 static inline struct cpumask *group_balance_mask(struct sched_group *sg)
2160 {
2161 return to_cpumask(sg->sgc->cpumask);
2162 }
2163
2164 extern int group_balance_cpu(struct sched_group *sg);
2165
2166 #ifdef CONFIG_SCHED_DEBUG
2167 extern void update_sched_domain_debugfs(void);
2168 extern void dirty_sched_domain_sysctl(int cpu);
2169 #else
update_sched_domain_debugfs(void)2170 static inline void update_sched_domain_debugfs(void) { }
dirty_sched_domain_sysctl(int cpu)2171 static inline void dirty_sched_domain_sysctl(int cpu) { }
2172 #endif
2173
2174 extern int sched_update_scaling(void);
2175
task_user_cpus(struct task_struct * p)2176 static inline const struct cpumask *task_user_cpus(struct task_struct *p)
2177 {
2178 if (!p->user_cpus_ptr)
2179 return cpu_possible_mask; /* &init_task.cpus_mask */
2180 return p->user_cpus_ptr;
2181 }
2182
2183 #endif /* CONFIG_SMP */
2184
2185 #ifdef CONFIG_CGROUP_SCHED
2186
2187 /*
2188 * Return the group to which this tasks belongs.
2189 *
2190 * We cannot use task_css() and friends because the cgroup subsystem
2191 * changes that value before the cgroup_subsys::attach() method is called,
2192 * therefore we cannot pin it and might observe the wrong value.
2193 *
2194 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
2195 * core changes this before calling sched_move_task().
2196 *
2197 * Instead we use a 'copy' which is updated from sched_move_task() while
2198 * holding both task_struct::pi_lock and rq::lock.
2199 */
task_group(struct task_struct * p)2200 static inline struct task_group *task_group(struct task_struct *p)
2201 {
2202 return p->sched_task_group;
2203 }
2204
2205 /* 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)2206 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
2207 {
2208 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
2209 struct task_group *tg = task_group(p);
2210 #endif
2211
2212 #ifdef CONFIG_FAIR_GROUP_SCHED
2213 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
2214 p->se.cfs_rq = tg->cfs_rq[cpu];
2215 p->se.parent = tg->se[cpu];
2216 p->se.depth = tg->se[cpu] ? tg->se[cpu]->depth + 1 : 0;
2217 #endif
2218
2219 #ifdef CONFIG_RT_GROUP_SCHED
2220 p->rt.rt_rq = tg->rt_rq[cpu];
2221 p->rt.parent = tg->rt_se[cpu];
2222 #endif
2223 }
2224
2225 #else /* !CONFIG_CGROUP_SCHED: */
2226
set_task_rq(struct task_struct * p,unsigned int cpu)2227 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
2228
task_group(struct task_struct * p)2229 static inline struct task_group *task_group(struct task_struct *p)
2230 {
2231 return NULL;
2232 }
2233
2234 #endif /* !CONFIG_CGROUP_SCHED */
2235
__set_task_cpu(struct task_struct * p,unsigned int cpu)2236 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2237 {
2238 set_task_rq(p, cpu);
2239 #ifdef CONFIG_SMP
2240 /*
2241 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2242 * successfully executed on another CPU. We must ensure that updates of
2243 * per-task data have been completed by this moment.
2244 */
2245 smp_wmb();
2246 WRITE_ONCE(task_thread_info(p)->cpu, cpu);
2247 p->wake_cpu = cpu;
2248 #endif
2249 }
2250
2251 /*
2252 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
2253 */
2254 #ifdef CONFIG_SCHED_DEBUG
2255 # define const_debug __read_mostly
2256 #else
2257 # define const_debug const
2258 #endif
2259
2260 #define SCHED_FEAT(name, enabled) \
2261 __SCHED_FEAT_##name ,
2262
2263 enum {
2264 #include "features.h"
2265 __SCHED_FEAT_NR,
2266 };
2267
2268 #undef SCHED_FEAT
2269
2270 #ifdef CONFIG_SCHED_DEBUG
2271
2272 /*
2273 * To support run-time toggling of sched features, all the translation units
2274 * (but core.c) reference the sysctl_sched_features defined in core.c.
2275 */
2276 extern const_debug unsigned int sysctl_sched_features;
2277
2278 #ifdef CONFIG_JUMP_LABEL
2279
2280 #define SCHED_FEAT(name, enabled) \
2281 static __always_inline bool static_branch_##name(struct static_key *key) \
2282 { \
2283 return static_key_##enabled(key); \
2284 }
2285
2286 #include "features.h"
2287 #undef SCHED_FEAT
2288
2289 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
2290 extern const char * const sched_feat_names[__SCHED_FEAT_NR];
2291
2292 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
2293
2294 #else /* !CONFIG_JUMP_LABEL: */
2295
2296 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
2297
2298 #endif /* !CONFIG_JUMP_LABEL */
2299
2300 #else /* !SCHED_DEBUG: */
2301
2302 /*
2303 * Each translation unit has its own copy of sysctl_sched_features to allow
2304 * constants propagation at compile time and compiler optimization based on
2305 * features default.
2306 */
2307 #define SCHED_FEAT(name, enabled) \
2308 (1UL << __SCHED_FEAT_##name) * enabled |
2309 static const_debug __maybe_unused unsigned int sysctl_sched_features =
2310 #include "features.h"
2311 0;
2312 #undef SCHED_FEAT
2313
2314 #define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
2315
2316 #endif /* !SCHED_DEBUG */
2317
2318 extern struct static_key_false sched_numa_balancing;
2319 extern struct static_key_false sched_schedstats;
2320
global_rt_period(void)2321 static inline u64 global_rt_period(void)
2322 {
2323 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
2324 }
2325
global_rt_runtime(void)2326 static inline u64 global_rt_runtime(void)
2327 {
2328 if (sysctl_sched_rt_runtime < 0)
2329 return RUNTIME_INF;
2330
2331 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
2332 }
2333
2334 /*
2335 * Is p the current execution context?
2336 */
task_current(struct rq * rq,struct task_struct * p)2337 static inline int task_current(struct rq *rq, struct task_struct *p)
2338 {
2339 return rq->curr == p;
2340 }
2341
2342 /*
2343 * Is p the current scheduling context?
2344 *
2345 * Note that it might be the current execution context at the same time if
2346 * rq->curr == rq->donor == p.
2347 */
task_current_donor(struct rq * rq,struct task_struct * p)2348 static inline int task_current_donor(struct rq *rq, struct task_struct *p)
2349 {
2350 return rq->donor == p;
2351 }
2352
task_is_blocked(struct task_struct * p)2353 static inline bool task_is_blocked(struct task_struct *p)
2354 {
2355 if (!sched_proxy_exec())
2356 return false;
2357
2358 return !!p->blocked_on && p->blocked_on_state != BO_RUNNABLE;
2359 }
2360
task_on_cpu(struct rq * rq,struct task_struct * p)2361 static inline int task_on_cpu(struct rq *rq, struct task_struct *p)
2362 {
2363 #ifdef CONFIG_SMP
2364 return p->on_cpu;
2365 #else
2366 return task_current(rq, p);
2367 #endif
2368 }
2369
task_on_rq_queued(struct task_struct * p)2370 static inline int task_on_rq_queued(struct task_struct *p)
2371 {
2372 return READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED;
2373 }
2374
task_on_rq_migrating(struct task_struct * p)2375 static inline int task_on_rq_migrating(struct task_struct *p)
2376 {
2377 return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
2378 }
2379
2380 /* Wake flags. The first three directly map to some SD flag value */
2381 #define WF_EXEC 0x02 /* Wakeup after exec; maps to SD_BALANCE_EXEC */
2382 #define WF_FORK 0x04 /* Wakeup after fork; maps to SD_BALANCE_FORK */
2383 #define WF_TTWU 0x08 /* Wakeup; maps to SD_BALANCE_WAKE */
2384
2385 #define WF_SYNC 0x10 /* Waker goes to sleep after wakeup */
2386 #define WF_MIGRATED 0x20 /* Internal use, task got migrated */
2387 #define WF_CURRENT_CPU 0x40 /* Prefer to move the wakee to the current CPU. */
2388 #define WF_RQ_SELECTED 0x80 /* ->select_task_rq() was called */
2389
2390 #define WF_ANDROID_VENDOR 0x1000 /* Vendor specific for Android */
2391
2392 #ifdef CONFIG_SMP
2393 static_assert(WF_EXEC == SD_BALANCE_EXEC);
2394 static_assert(WF_FORK == SD_BALANCE_FORK);
2395 static_assert(WF_TTWU == SD_BALANCE_WAKE);
2396 #endif
2397
2398 /*
2399 * To aid in avoiding the subversion of "niceness" due to uneven distribution
2400 * of tasks with abnormal "nice" values across CPUs the contribution that
2401 * each task makes to its run queue's load is weighted according to its
2402 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
2403 * scaled version of the new time slice allocation that they receive on time
2404 * slice expiry etc.
2405 */
2406
2407 #define WEIGHT_IDLEPRIO 3
2408 #define WMULT_IDLEPRIO 1431655765
2409
2410 extern const int sched_prio_to_weight[40];
2411 extern const u32 sched_prio_to_wmult[40];
2412
2413 /*
2414 * {de,en}queue flags:
2415 *
2416 * DEQUEUE_SLEEP - task is no longer runnable
2417 * ENQUEUE_WAKEUP - task just became runnable
2418 *
2419 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
2420 * are in a known state which allows modification. Such pairs
2421 * should preserve as much state as possible.
2422 *
2423 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
2424 * in the runqueue.
2425 *
2426 * NOCLOCK - skip the update_rq_clock() (avoids double updates)
2427 *
2428 * MIGRATION - p->on_rq == TASK_ON_RQ_MIGRATING (used for DEADLINE)
2429 *
2430 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
2431 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
2432 * ENQUEUE_MIGRATED - the task was migrated during wakeup
2433 * ENQUEUE_RQ_SELECTED - ->select_task_rq() was called
2434 *
2435 */
2436
2437 #define DEQUEUE_SLEEP 0x01 /* Matches ENQUEUE_WAKEUP */
2438 #define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */
2439 #define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */
2440 #define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */
2441 #define DEQUEUE_SPECIAL 0x10
2442 #define DEQUEUE_MIGRATING 0x100 /* Matches ENQUEUE_MIGRATING */
2443 #define DEQUEUE_DELAYED 0x200 /* Matches ENQUEUE_DELAYED */
2444
2445 #define ENQUEUE_WAKEUP 0x01
2446 #define ENQUEUE_RESTORE 0x02
2447 #define ENQUEUE_MOVE 0x04
2448 #define ENQUEUE_NOCLOCK 0x08
2449
2450 #define ENQUEUE_HEAD 0x10
2451 #define ENQUEUE_REPLENISH 0x20
2452 #ifdef CONFIG_SMP
2453 #define ENQUEUE_MIGRATED 0x40
2454 #else
2455 #define ENQUEUE_MIGRATED 0x00
2456 #endif
2457 #define ENQUEUE_INITIAL 0x80
2458 #define ENQUEUE_MIGRATING 0x100
2459 #define ENQUEUE_DELAYED 0x200
2460 #define ENQUEUE_RQ_SELECTED 0x400
2461
2462 #define ENQUEUE_WAKEUP_SYNC 0x80
2463
2464 #define RETRY_TASK ((void *)-1UL)
2465
2466 struct affinity_context {
2467 const struct cpumask *new_mask;
2468 struct cpumask *user_mask;
2469 unsigned int flags;
2470 };
2471
2472 extern s64 update_curr_common(struct rq *rq);
2473
2474 struct sched_class {
2475
2476 #ifdef CONFIG_UCLAMP_TASK
2477 int uclamp_enabled;
2478 #endif
2479
2480 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
2481 bool (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
2482 void (*yield_task) (struct rq *rq);
2483 bool (*yield_to_task)(struct rq *rq, struct task_struct *p);
2484
2485 void (*wakeup_preempt)(struct rq *rq, struct task_struct *p, int flags);
2486
2487 int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
2488 struct task_struct *(*pick_task)(struct rq *rq);
2489 /*
2490 * Optional! When implemented pick_next_task() should be equivalent to:
2491 *
2492 * next = pick_task();
2493 * if (next) {
2494 * put_prev_task(prev);
2495 * set_next_task_first(next);
2496 * }
2497 */
2498 struct task_struct *(*pick_next_task)(struct rq *rq, struct task_struct *prev);
2499
2500 void (*put_prev_task)(struct rq *rq, struct task_struct *p, struct task_struct *next);
2501 void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first);
2502
2503 #ifdef CONFIG_SMP
2504 int (*select_task_rq)(struct task_struct *p, int task_cpu, int flags);
2505
2506 void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
2507
2508 void (*task_woken)(struct rq *this_rq, struct task_struct *task);
2509
2510 void (*set_cpus_allowed)(struct task_struct *p, struct affinity_context *ctx);
2511
2512 void (*rq_online)(struct rq *rq);
2513 void (*rq_offline)(struct rq *rq);
2514
2515 struct rq *(*find_lock_rq)(struct task_struct *p, struct rq *rq);
2516 #endif
2517
2518 void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
2519 void (*task_fork)(struct task_struct *p);
2520 void (*task_dead)(struct task_struct *p);
2521
2522 /*
2523 * The switched_from() call is allowed to drop rq->lock, therefore we
2524 * cannot assume the switched_from/switched_to pair is serialized by
2525 * rq->lock. They are however serialized by p->pi_lock.
2526 */
2527 void (*switching_to) (struct rq *this_rq, struct task_struct *task);
2528 void (*switched_from)(struct rq *this_rq, struct task_struct *task);
2529 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
2530 void (*reweight_task)(struct rq *this_rq, struct task_struct *task,
2531 const struct load_weight *lw);
2532 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
2533 int oldprio);
2534
2535 unsigned int (*get_rr_interval)(struct rq *rq,
2536 struct task_struct *task);
2537
2538 void (*update_curr)(struct rq *rq);
2539
2540 #ifdef CONFIG_FAIR_GROUP_SCHED
2541 void (*task_change_group)(struct task_struct *p);
2542 #endif
2543
2544 #ifdef CONFIG_SCHED_CORE
2545 int (*task_is_throttled)(struct task_struct *p, int cpu);
2546 #endif
2547 };
2548
put_prev_task(struct rq * rq,struct task_struct * prev)2549 static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
2550 {
2551 WARN_ON_ONCE(rq->donor != prev);
2552 prev->sched_class->put_prev_task(rq, prev, NULL);
2553 }
2554
set_next_task(struct rq * rq,struct task_struct * next)2555 static inline void set_next_task(struct rq *rq, struct task_struct *next)
2556 {
2557 next->sched_class->set_next_task(rq, next, false);
2558 }
2559
2560 static inline void
__put_prev_set_next_dl_server(struct rq * rq,struct task_struct * prev,struct task_struct * next)2561 __put_prev_set_next_dl_server(struct rq *rq,
2562 struct task_struct *prev,
2563 struct task_struct *next)
2564 {
2565 prev->dl_server = NULL;
2566 next->dl_server = rq->dl_server;
2567 rq->dl_server = NULL;
2568 }
2569
put_prev_set_next_task(struct rq * rq,struct task_struct * prev,struct task_struct * next)2570 static inline void put_prev_set_next_task(struct rq *rq,
2571 struct task_struct *prev,
2572 struct task_struct *next)
2573 {
2574 WARN_ON_ONCE(rq->donor != prev);
2575
2576 __put_prev_set_next_dl_server(rq, prev, next);
2577
2578 if (next == prev)
2579 return;
2580
2581 prev->sched_class->put_prev_task(rq, prev, next);
2582 next->sched_class->set_next_task(rq, next, true);
2583 }
2584
2585 /*
2586 * Helper to define a sched_class instance; each one is placed in a separate
2587 * section which is ordered by the linker script:
2588 *
2589 * include/asm-generic/vmlinux.lds.h
2590 *
2591 * *CAREFUL* they are laid out in *REVERSE* order!!!
2592 *
2593 * Also enforce alignment on the instance, not the type, to guarantee layout.
2594 */
2595 #define DEFINE_SCHED_CLASS(name) \
2596 const struct sched_class name##_sched_class \
2597 __aligned(__alignof__(struct sched_class)) \
2598 __section("__" #name "_sched_class")
2599
2600 /* Defined in include/asm-generic/vmlinux.lds.h */
2601 extern struct sched_class __sched_class_highest[];
2602 extern struct sched_class __sched_class_lowest[];
2603
2604 extern const struct sched_class stop_sched_class;
2605 extern const struct sched_class dl_sched_class;
2606 extern const struct sched_class rt_sched_class;
2607 extern const struct sched_class fair_sched_class;
2608 extern const struct sched_class idle_sched_class;
2609
2610 #ifdef CONFIG_SCHED_CLASS_EXT
2611 extern const struct sched_class ext_sched_class;
2612
2613 DECLARE_STATIC_KEY_FALSE(__scx_ops_enabled); /* SCX BPF scheduler loaded */
2614 DECLARE_STATIC_KEY_FALSE(__scx_switched_all); /* all fair class tasks on SCX */
2615
2616 #define scx_enabled() static_branch_unlikely(&__scx_ops_enabled)
2617 #define scx_switched_all() static_branch_unlikely(&__scx_switched_all)
2618 #else /* !CONFIG_SCHED_CLASS_EXT */
2619 #define scx_enabled() false
2620 #define scx_switched_all() false
2621 #endif /* !CONFIG_SCHED_CLASS_EXT */
2622
2623 /*
2624 * Iterate only active classes. SCX can take over all fair tasks or be
2625 * completely disabled. If the former, skip fair. If the latter, skip SCX.
2626 */
next_active_class(const struct sched_class * class)2627 static inline const struct sched_class *next_active_class(const struct sched_class *class)
2628 {
2629 class++;
2630 #ifdef CONFIG_SCHED_CLASS_EXT
2631 if (scx_switched_all() && class == &fair_sched_class)
2632 class++;
2633 if (!scx_enabled() && class == &ext_sched_class)
2634 class++;
2635 #endif
2636 return class;
2637 }
2638
2639 #define for_class_range(class, _from, _to) \
2640 for (class = (_from); class < (_to); class++)
2641
2642 #define for_each_class(class) \
2643 for_class_range(class, __sched_class_highest, __sched_class_lowest)
2644
2645 #define for_active_class_range(class, _from, _to) \
2646 for (class = (_from); class != (_to); class = next_active_class(class))
2647
2648 #define for_each_active_class(class) \
2649 for_active_class_range(class, __sched_class_highest, __sched_class_lowest)
2650
2651 #define sched_class_above(_a, _b) ((_a) < (_b))
2652
sched_stop_runnable(struct rq * rq)2653 static inline bool sched_stop_runnable(struct rq *rq)
2654 {
2655 return rq->stop && task_on_rq_queued(rq->stop);
2656 }
2657
sched_dl_runnable(struct rq * rq)2658 static inline bool sched_dl_runnable(struct rq *rq)
2659 {
2660 return rq->dl.dl_nr_running > 0;
2661 }
2662
sched_rt_runnable(struct rq * rq)2663 static inline bool sched_rt_runnable(struct rq *rq)
2664 {
2665 return rq->rt.rt_queued > 0;
2666 }
2667
sched_fair_runnable(struct rq * rq)2668 static inline bool sched_fair_runnable(struct rq *rq)
2669 {
2670 return rq->cfs.nr_running > 0;
2671 }
2672
2673 extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
2674 extern struct task_struct *pick_task_idle(struct rq *rq);
2675
2676 #define SCA_CHECK 0x01
2677 #define SCA_MIGRATE_DISABLE 0x02
2678 #define SCA_MIGRATE_ENABLE 0x04
2679 #define SCA_USER 0x08
2680
2681 #ifdef CONFIG_SMP
2682
2683 extern void update_group_capacity(struct sched_domain *sd, int cpu);
2684
2685 extern void sched_balance_trigger(struct rq *rq);
2686
2687 extern int __set_cpus_allowed_ptr(struct task_struct *p, struct affinity_context *ctx);
2688 extern void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx);
2689
task_allowed_on_cpu(struct task_struct * p,int cpu)2690 static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
2691 {
2692 /* When not in the task's cpumask, no point in looking further. */
2693 if (!cpumask_test_cpu(cpu, p->cpus_ptr))
2694 return false;
2695
2696 /* Can @cpu run a user thread? */
2697 if (!(p->flags & PF_KTHREAD) && !task_cpu_possible(cpu, p))
2698 return false;
2699
2700 return true;
2701 }
2702
alloc_user_cpus_ptr(int node)2703 static inline cpumask_t *alloc_user_cpus_ptr(int node)
2704 {
2705 /*
2706 * See do_set_cpus_allowed() above for the rcu_head usage.
2707 */
2708 int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));
2709
2710 return kmalloc_node(size, GFP_KERNEL, node);
2711 }
2712
get_push_task(struct rq * rq)2713 static inline struct task_struct *get_push_task(struct rq *rq)
2714 {
2715 struct task_struct *p = rq->donor;
2716
2717 lockdep_assert_rq_held(rq);
2718
2719 if (rq->push_busy)
2720 return NULL;
2721
2722 if (p->nr_cpus_allowed == 1)
2723 return NULL;
2724
2725 if (p->migration_disabled)
2726 return NULL;
2727
2728 rq->push_busy = true;
2729 return get_task_struct(p);
2730 }
2731
2732 extern int push_cpu_stop(void *arg);
2733
2734 extern unsigned long __read_mostly max_load_balance_interval;
2735 #else /* !CONFIG_SMP: */
2736
task_allowed_on_cpu(struct task_struct * p,int cpu)2737 static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
2738 {
2739 return true;
2740 }
2741
__set_cpus_allowed_ptr(struct task_struct * p,struct affinity_context * ctx)2742 static inline int __set_cpus_allowed_ptr(struct task_struct *p,
2743 struct affinity_context *ctx)
2744 {
2745 return set_cpus_allowed_ptr(p, ctx->new_mask);
2746 }
2747
alloc_user_cpus_ptr(int node)2748 static inline cpumask_t *alloc_user_cpus_ptr(int node)
2749 {
2750 return NULL;
2751 }
2752
2753 #endif /* !CONFIG_SMP */
2754
2755 #ifdef CONFIG_CPU_IDLE
2756
idle_set_state(struct rq * rq,struct cpuidle_state * idle_state)2757 static inline void idle_set_state(struct rq *rq,
2758 struct cpuidle_state *idle_state)
2759 {
2760 rq->idle_state = idle_state;
2761 }
2762
idle_get_state(struct rq * rq)2763 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
2764 {
2765 SCHED_WARN_ON(!rcu_read_lock_held());
2766
2767 return rq->idle_state;
2768 }
2769
2770 #else /* !CONFIG_CPU_IDLE: */
2771
idle_set_state(struct rq * rq,struct cpuidle_state * idle_state)2772 static inline void idle_set_state(struct rq *rq,
2773 struct cpuidle_state *idle_state)
2774 {
2775 }
2776
idle_get_state(struct rq * rq)2777 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
2778 {
2779 return NULL;
2780 }
2781
2782 #endif /* !CONFIG_CPU_IDLE */
2783
2784 extern void schedule_idle(void);
2785 asmlinkage void schedule_user(void);
2786
2787 extern void sysrq_sched_debug_show(void);
2788 extern void sched_init_granularity(void);
2789 extern void update_max_interval(void);
2790
2791 extern void init_sched_dl_class(void);
2792 extern void init_sched_rt_class(void);
2793 extern void init_sched_fair_class(void);
2794
2795 extern void resched_curr(struct rq *rq);
2796 extern void resched_cpu(int cpu);
2797
2798 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
2799 extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
2800
2801 extern void init_dl_entity(struct sched_dl_entity *dl_se);
2802
2803 #define BW_SHIFT 20
2804 #define BW_UNIT (1 << BW_SHIFT)
2805 #define RATIO_SHIFT 8
2806 #define MAX_BW_BITS (64 - BW_SHIFT)
2807 #define MAX_BW ((1ULL << MAX_BW_BITS) - 1)
2808
2809 extern unsigned long to_ratio(u64 period, u64 runtime);
2810
2811 extern void init_entity_runnable_average(struct sched_entity *se);
2812 extern void post_init_entity_util_avg(struct task_struct *p);
2813
2814 #ifdef CONFIG_NO_HZ_FULL
2815 extern bool sched_can_stop_tick(struct rq *rq);
2816 extern int __init sched_tick_offload_init(void);
2817
2818 /*
2819 * Tick may be needed by tasks in the runqueue depending on their policy and
2820 * requirements. If tick is needed, lets send the target an IPI to kick it out of
2821 * nohz mode if necessary.
2822 */
sched_update_tick_dependency(struct rq * rq)2823 static inline void sched_update_tick_dependency(struct rq *rq)
2824 {
2825 int cpu = cpu_of(rq);
2826
2827 if (!tick_nohz_full_cpu(cpu))
2828 return;
2829
2830 if (sched_can_stop_tick(rq))
2831 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
2832 else
2833 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
2834 }
2835 #else /* !CONFIG_NO_HZ_FULL: */
sched_tick_offload_init(void)2836 static inline int sched_tick_offload_init(void) { return 0; }
sched_update_tick_dependency(struct rq * rq)2837 static inline void sched_update_tick_dependency(struct rq *rq) { }
2838 #endif /* !CONFIG_NO_HZ_FULL */
2839
add_nr_running(struct rq * rq,unsigned count)2840 static inline void add_nr_running(struct rq *rq, unsigned count)
2841 {
2842 unsigned prev_nr = rq->nr_running;
2843
2844 rq->nr_running = prev_nr + count;
2845 if (trace_sched_update_nr_running_tp_enabled()) {
2846 call_trace_sched_update_nr_running(rq, count);
2847 }
2848
2849 #ifdef CONFIG_SMP
2850 if (prev_nr < 2 && rq->nr_running >= 2)
2851 set_rd_overloaded(rq->rd, 1);
2852 #endif
2853
2854 sched_update_tick_dependency(rq);
2855 }
2856
sub_nr_running(struct rq * rq,unsigned count)2857 static inline void sub_nr_running(struct rq *rq, unsigned count)
2858 {
2859 rq->nr_running -= count;
2860 if (trace_sched_update_nr_running_tp_enabled()) {
2861 call_trace_sched_update_nr_running(rq, -count);
2862 }
2863
2864 /* Check if we still need preemption */
2865 sched_update_tick_dependency(rq);
2866 }
2867
__block_task(struct rq * rq,struct task_struct * p)2868 static inline void __block_task(struct rq *rq, struct task_struct *p)
2869 {
2870 if (p->sched_contributes_to_load)
2871 rq->nr_uninterruptible++;
2872
2873 if (p->in_iowait) {
2874 atomic_inc(&rq->nr_iowait);
2875 delayacct_blkio_start();
2876 }
2877
2878 ASSERT_EXCLUSIVE_WRITER(p->on_rq);
2879
2880 /*
2881 * The moment this write goes through, ttwu() can swoop in and migrate
2882 * this task, rendering our rq->__lock ineffective.
2883 *
2884 * __schedule() try_to_wake_up()
2885 * LOCK rq->__lock LOCK p->pi_lock
2886 * pick_next_task()
2887 * pick_next_task_fair()
2888 * pick_next_entity()
2889 * dequeue_entities()
2890 * __block_task()
2891 * RELEASE p->on_rq = 0 if (p->on_rq && ...)
2892 * break;
2893 *
2894 * ACQUIRE (after ctrl-dep)
2895 *
2896 * cpu = select_task_rq();
2897 * set_task_cpu(p, cpu);
2898 * ttwu_queue()
2899 * ttwu_do_activate()
2900 * LOCK rq->__lock
2901 * activate_task()
2902 * STORE p->on_rq = 1
2903 * UNLOCK rq->__lock
2904 *
2905 * Callers must ensure to not reference @p after this -- we no longer
2906 * own it.
2907 */
2908 smp_store_release(&p->on_rq, 0);
2909 }
2910
2911 extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
2912 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
2913
2914 extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags);
2915
2916 #ifdef CONFIG_PREEMPT_RT
2917 # define SCHED_NR_MIGRATE_BREAK 8
2918 #else
2919 # define SCHED_NR_MIGRATE_BREAK 32
2920 #endif
2921
2922 extern const_debug unsigned int sysctl_sched_nr_migrate;
2923 extern const_debug unsigned int sysctl_sched_migration_cost;
2924
2925 extern unsigned int sysctl_sched_base_slice;
2926
2927 #ifdef CONFIG_SCHED_DEBUG
2928 extern int sysctl_resched_latency_warn_ms;
2929 extern int sysctl_resched_latency_warn_once;
2930
2931 extern unsigned int sysctl_sched_tunable_scaling;
2932
2933 extern unsigned int sysctl_numa_balancing_scan_delay;
2934 extern unsigned int sysctl_numa_balancing_scan_period_min;
2935 extern unsigned int sysctl_numa_balancing_scan_period_max;
2936 extern unsigned int sysctl_numa_balancing_scan_size;
2937 extern unsigned int sysctl_numa_balancing_hot_threshold;
2938 #endif
2939
2940 #ifdef CONFIG_SCHED_HRTICK
2941
2942 /*
2943 * Use hrtick when:
2944 * - enabled by features
2945 * - hrtimer is actually high res
2946 */
hrtick_enabled(struct rq * rq)2947 static inline int hrtick_enabled(struct rq *rq)
2948 {
2949 if (!cpu_active(cpu_of(rq)))
2950 return 0;
2951 return hrtimer_is_hres_active(&rq->hrtick_timer);
2952 }
2953
hrtick_enabled_fair(struct rq * rq)2954 static inline int hrtick_enabled_fair(struct rq *rq)
2955 {
2956 if (!sched_feat(HRTICK))
2957 return 0;
2958 return hrtick_enabled(rq);
2959 }
2960
hrtick_enabled_dl(struct rq * rq)2961 static inline int hrtick_enabled_dl(struct rq *rq)
2962 {
2963 if (!sched_feat(HRTICK_DL))
2964 return 0;
2965 return hrtick_enabled(rq);
2966 }
2967
2968 extern void hrtick_start(struct rq *rq, u64 delay);
2969
2970 #else /* !CONFIG_SCHED_HRTICK: */
2971
hrtick_enabled_fair(struct rq * rq)2972 static inline int hrtick_enabled_fair(struct rq *rq)
2973 {
2974 return 0;
2975 }
2976
hrtick_enabled_dl(struct rq * rq)2977 static inline int hrtick_enabled_dl(struct rq *rq)
2978 {
2979 return 0;
2980 }
2981
hrtick_enabled(struct rq * rq)2982 static inline int hrtick_enabled(struct rq *rq)
2983 {
2984 return 0;
2985 }
2986
2987 #endif /* !CONFIG_SCHED_HRTICK */
2988
2989 #ifndef arch_scale_freq_tick
arch_scale_freq_tick(void)2990 static __always_inline void arch_scale_freq_tick(void) { }
2991 #endif
2992
2993 #ifndef arch_scale_freq_capacity
2994 /**
2995 * arch_scale_freq_capacity - get the frequency scale factor of a given CPU.
2996 * @cpu: the CPU in question.
2997 *
2998 * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
2999 *
3000 * f_curr
3001 * ------ * SCHED_CAPACITY_SCALE
3002 * f_max
3003 */
3004 static __always_inline
arch_scale_freq_capacity(int cpu)3005 unsigned long arch_scale_freq_capacity(int cpu)
3006 {
3007 return SCHED_CAPACITY_SCALE;
3008 }
3009 #endif
3010
3011 #ifdef CONFIG_SCHED_DEBUG
3012 /*
3013 * In double_lock_balance()/double_rq_lock(), we use raw_spin_rq_lock() to
3014 * acquire rq lock instead of rq_lock(). So at the end of these two functions
3015 * we need to call double_rq_clock_clear_update() to clear RQCF_UPDATED of
3016 * rq->clock_update_flags to avoid the WARN_DOUBLE_CLOCK warning.
3017 */
double_rq_clock_clear_update(struct rq * rq1,struct rq * rq2)3018 static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2)
3019 {
3020 rq1->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
3021 /* rq1 == rq2 for !CONFIG_SMP, so just clear RQCF_UPDATED once. */
3022 #ifdef CONFIG_SMP
3023 rq2->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
3024 #endif
3025 }
3026 #else
double_rq_clock_clear_update(struct rq * rq1,struct rq * rq2)3027 static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2) { }
3028 #endif
3029
3030 #define DEFINE_LOCK_GUARD_2(name, type, _lock, _unlock, ...) \
3031 __DEFINE_UNLOCK_GUARD(name, type, _unlock, type *lock2; __VA_ARGS__) \
3032 static inline class_##name##_t class_##name##_constructor(type *lock, type *lock2) \
3033 { class_##name##_t _t = { .lock = lock, .lock2 = lock2 }, *_T = &_t; \
3034 _lock; return _t; }
3035
3036 #ifdef CONFIG_SMP
3037
rq_order_less(struct rq * rq1,struct rq * rq2)3038 static inline bool rq_order_less(struct rq *rq1, struct rq *rq2)
3039 {
3040 #ifdef CONFIG_SCHED_CORE
3041 /*
3042 * In order to not have {0,2},{1,3} turn into into an AB-BA,
3043 * order by core-id first and cpu-id second.
3044 *
3045 * Notably:
3046 *
3047 * double_rq_lock(0,3); will take core-0, core-1 lock
3048 * double_rq_lock(1,2); will take core-1, core-0 lock
3049 *
3050 * when only cpu-id is considered.
3051 */
3052 if (rq1->core->cpu < rq2->core->cpu)
3053 return true;
3054 if (rq1->core->cpu > rq2->core->cpu)
3055 return false;
3056
3057 /*
3058 * __sched_core_flip() relies on SMT having cpu-id lock order.
3059 */
3060 #endif
3061 return rq1->cpu < rq2->cpu;
3062 }
3063
3064 extern void double_rq_lock(struct rq *rq1, struct rq *rq2);
3065
3066 #ifdef CONFIG_PREEMPTION
3067
3068 /*
3069 * fair double_lock_balance: Safely acquires both rq->locks in a fair
3070 * way at the expense of forcing extra atomic operations in all
3071 * invocations. This assures that the double_lock is acquired using the
3072 * same underlying policy as the spinlock_t on this architecture, which
3073 * reduces latency compared to the unfair variant below. However, it
3074 * also adds more overhead and therefore may reduce throughput.
3075 */
_double_lock_balance(struct rq * this_rq,struct rq * busiest)3076 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
3077 __releases(this_rq->lock)
3078 __acquires(busiest->lock)
3079 __acquires(this_rq->lock)
3080 {
3081 raw_spin_rq_unlock(this_rq);
3082 double_rq_lock(this_rq, busiest);
3083
3084 return 1;
3085 }
3086
3087 #else /* !CONFIG_PREEMPTION: */
3088 /*
3089 * Unfair double_lock_balance: Optimizes throughput at the expense of
3090 * latency by eliminating extra atomic operations when the locks are
3091 * already in proper order on entry. This favors lower CPU-ids and will
3092 * grant the double lock to lower CPUs over higher ids under contention,
3093 * regardless of entry order into the function.
3094 */
_double_lock_balance(struct rq * this_rq,struct rq * busiest)3095 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
3096 __releases(this_rq->lock)
3097 __acquires(busiest->lock)
3098 __acquires(this_rq->lock)
3099 {
3100 if (__rq_lockp(this_rq) == __rq_lockp(busiest) ||
3101 likely(raw_spin_rq_trylock(busiest))) {
3102 double_rq_clock_clear_update(this_rq, busiest);
3103 return 0;
3104 }
3105
3106 if (rq_order_less(this_rq, busiest)) {
3107 raw_spin_rq_lock_nested(busiest, SINGLE_DEPTH_NESTING);
3108 double_rq_clock_clear_update(this_rq, busiest);
3109 return 0;
3110 }
3111
3112 raw_spin_rq_unlock(this_rq);
3113 double_rq_lock(this_rq, busiest);
3114
3115 return 1;
3116 }
3117
3118 #endif /* !CONFIG_PREEMPTION */
3119
3120 /*
3121 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3122 */
double_lock_balance(struct rq * this_rq,struct rq * busiest)3123 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
3124 {
3125 lockdep_assert_irqs_disabled();
3126
3127 return _double_lock_balance(this_rq, busiest);
3128 }
3129
double_unlock_balance(struct rq * this_rq,struct rq * busiest)3130 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
3131 __releases(busiest->lock)
3132 {
3133 if (__rq_lockp(this_rq) != __rq_lockp(busiest))
3134 raw_spin_rq_unlock(busiest);
3135 lock_set_subclass(&__rq_lockp(this_rq)->dep_map, 0, _RET_IP_);
3136 }
3137
double_lock(spinlock_t * l1,spinlock_t * l2)3138 static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
3139 {
3140 if (l1 > l2)
3141 swap(l1, l2);
3142
3143 spin_lock(l1);
3144 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3145 }
3146
double_lock_irq(spinlock_t * l1,spinlock_t * l2)3147 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
3148 {
3149 if (l1 > l2)
3150 swap(l1, l2);
3151
3152 spin_lock_irq(l1);
3153 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3154 }
3155
double_raw_lock(raw_spinlock_t * l1,raw_spinlock_t * l2)3156 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
3157 {
3158 if (l1 > l2)
3159 swap(l1, l2);
3160
3161 raw_spin_lock(l1);
3162 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3163 }
3164
double_raw_unlock(raw_spinlock_t * l1,raw_spinlock_t * l2)3165 static inline void double_raw_unlock(raw_spinlock_t *l1, raw_spinlock_t *l2)
3166 {
3167 raw_spin_unlock(l1);
3168 raw_spin_unlock(l2);
3169 }
3170
3171 DEFINE_LOCK_GUARD_2(double_raw_spinlock, raw_spinlock_t,
3172 double_raw_lock(_T->lock, _T->lock2),
3173 double_raw_unlock(_T->lock, _T->lock2))
3174
3175 /*
3176 * double_rq_unlock - safely unlock two runqueues
3177 *
3178 * Note this does not restore interrupts like task_rq_unlock,
3179 * you need to do so manually after calling.
3180 */
double_rq_unlock(struct rq * rq1,struct rq * rq2)3181 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
3182 __releases(rq1->lock)
3183 __releases(rq2->lock)
3184 {
3185 if (__rq_lockp(rq1) != __rq_lockp(rq2))
3186 raw_spin_rq_unlock(rq2);
3187 else
3188 __release(rq2->lock);
3189 raw_spin_rq_unlock(rq1);
3190 }
3191
3192 extern void set_rq_online (struct rq *rq);
3193 extern void set_rq_offline(struct rq *rq);
3194
3195 extern bool sched_smp_initialized;
3196
__revalidate_rq_state(struct task_struct * task,struct rq * rq,struct rq * lowest)3197 static inline bool __revalidate_rq_state(struct task_struct *task, struct rq *rq,
3198 struct rq *lowest)
3199 {
3200 /*
3201 * We had to unlock the run queue. In the mean time, task could have
3202 * migrated already or had its affinity changed. Also make sure that it
3203 * wasn't scheduled on its rq. It is possible the task was scheduled,
3204 * set "migrate_disabled" and then got preempted, so we must check the
3205 * task migration disable flag here too.
3206 */
3207 if (task_rq(task) != rq)
3208 return false;
3209
3210 if (!cpumask_test_cpu(lowest->cpu, &task->cpus_mask))
3211 return false;
3212
3213 if (task_on_cpu(rq, task))
3214 return false;
3215
3216 if (is_migration_disabled(task))
3217 return false;
3218
3219 if (!task_on_rq_queued(task))
3220 return false;
3221
3222 return true;
3223 }
3224
3225 #else /* !CONFIG_SMP: */
3226
3227 /*
3228 * double_rq_lock - safely lock two runqueues
3229 *
3230 * Note this does not disable interrupts like task_rq_lock,
3231 * you need to do so manually before calling.
3232 */
double_rq_lock(struct rq * rq1,struct rq * rq2)3233 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
3234 __acquires(rq1->lock)
3235 __acquires(rq2->lock)
3236 {
3237 WARN_ON_ONCE(!irqs_disabled());
3238 WARN_ON_ONCE(rq1 != rq2);
3239 raw_spin_rq_lock(rq1);
3240 __acquire(rq2->lock); /* Fake it out ;) */
3241 double_rq_clock_clear_update(rq1, rq2);
3242 }
3243
3244 /*
3245 * double_rq_unlock - safely unlock two runqueues
3246 *
3247 * Note this does not restore interrupts like task_rq_unlock,
3248 * you need to do so manually after calling.
3249 */
double_rq_unlock(struct rq * rq1,struct rq * rq2)3250 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
3251 __releases(rq1->lock)
3252 __releases(rq2->lock)
3253 {
3254 WARN_ON_ONCE(rq1 != rq2);
3255 raw_spin_rq_unlock(rq1);
3256 __release(rq2->lock);
3257 }
3258
3259 #endif /* !CONFIG_SMP */
3260
3261 DEFINE_LOCK_GUARD_2(double_rq_lock, struct rq,
3262 double_rq_lock(_T->lock, _T->lock2),
3263 double_rq_unlock(_T->lock, _T->lock2))
3264
3265 extern struct sched_entity *__pick_root_entity(struct cfs_rq *cfs_rq);
3266 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
3267 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
3268
3269 #ifdef CONFIG_SCHED_DEBUG
3270 extern bool sched_debug_verbose;
3271
3272 extern void print_cfs_stats(struct seq_file *m, int cpu);
3273 extern void print_rt_stats(struct seq_file *m, int cpu);
3274 extern void print_dl_stats(struct seq_file *m, int cpu);
3275 extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
3276 extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
3277 extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
3278
3279 extern void resched_latency_warn(int cpu, u64 latency);
3280 # ifdef CONFIG_NUMA_BALANCING
3281 extern void show_numa_stats(struct task_struct *p, struct seq_file *m);
3282 extern void
3283 print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
3284 unsigned long tpf, unsigned long gsf, unsigned long gpf);
3285 # endif /* CONFIG_NUMA_BALANCING */
3286 #else /* !CONFIG_SCHED_DEBUG: */
resched_latency_warn(int cpu,u64 latency)3287 static inline void resched_latency_warn(int cpu, u64 latency) { }
3288 #endif /* !CONFIG_SCHED_DEBUG */
3289
3290 extern void init_cfs_rq(struct cfs_rq *cfs_rq);
3291 extern void init_rt_rq(struct rt_rq *rt_rq);
3292 extern void init_dl_rq(struct dl_rq *dl_rq);
3293
3294 extern void cfs_bandwidth_usage_inc(void);
3295 extern void cfs_bandwidth_usage_dec(void);
3296
3297 #ifdef CONFIG_NO_HZ_COMMON
3298
3299 #define NOHZ_BALANCE_KICK_BIT 0
3300 #define NOHZ_STATS_KICK_BIT 1
3301 #define NOHZ_NEWILB_KICK_BIT 2
3302 #define NOHZ_NEXT_KICK_BIT 3
3303
3304 /* Run sched_balance_domains() */
3305 #define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT)
3306 /* Update blocked load */
3307 #define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT)
3308 /* Update blocked load when entering idle */
3309 #define NOHZ_NEWILB_KICK BIT(NOHZ_NEWILB_KICK_BIT)
3310 /* Update nohz.next_balance */
3311 #define NOHZ_NEXT_KICK BIT(NOHZ_NEXT_KICK_BIT)
3312
3313 #define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK | NOHZ_NEXT_KICK)
3314
3315 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
3316
3317 extern void nohz_balance_exit_idle(struct rq *rq);
3318 #else /* !CONFIG_NO_HZ_COMMON: */
nohz_balance_exit_idle(struct rq * rq)3319 static inline void nohz_balance_exit_idle(struct rq *rq) { }
3320 #endif /* !CONFIG_NO_HZ_COMMON */
3321
3322 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
3323 extern void nohz_run_idle_balance(int cpu);
3324 #else
nohz_run_idle_balance(int cpu)3325 static inline void nohz_run_idle_balance(int cpu) { }
3326 #endif
3327
3328 #include "stats.h"
3329
3330 #if defined(CONFIG_SCHED_CORE) && defined(CONFIG_SCHEDSTATS)
3331
3332 extern void __sched_core_account_forceidle(struct rq *rq);
3333
sched_core_account_forceidle(struct rq * rq)3334 static inline void sched_core_account_forceidle(struct rq *rq)
3335 {
3336 if (schedstat_enabled())
3337 __sched_core_account_forceidle(rq);
3338 }
3339
3340 extern void __sched_core_tick(struct rq *rq);
3341
sched_core_tick(struct rq * rq)3342 static inline void sched_core_tick(struct rq *rq)
3343 {
3344 if (sched_core_enabled(rq) && schedstat_enabled())
3345 __sched_core_tick(rq);
3346 }
3347
3348 #else /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS): */
3349
sched_core_account_forceidle(struct rq * rq)3350 static inline void sched_core_account_forceidle(struct rq *rq) { }
3351
sched_core_tick(struct rq * rq)3352 static inline void sched_core_tick(struct rq *rq) { }
3353
3354 #endif /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS) */
3355
3356 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
3357
3358 struct irqtime {
3359 u64 total;
3360 u64 tick_delta;
3361 u64 irq_start_time;
3362 struct u64_stats_sync sync;
3363 };
3364
3365 DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
3366
3367 /*
3368 * Returns the irqtime minus the softirq time computed by ksoftirqd.
3369 * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime
3370 * and never move forward.
3371 */
irq_time_read(int cpu)3372 static inline u64 irq_time_read(int cpu)
3373 {
3374 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
3375 unsigned int seq;
3376 u64 total;
3377
3378 do {
3379 seq = __u64_stats_fetch_begin(&irqtime->sync);
3380 total = irqtime->total;
3381 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
3382
3383 return total;
3384 }
3385
3386 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
3387
3388 #ifdef CONFIG_CPU_FREQ
3389
3390 DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
3391
3392 /**
3393 * cpufreq_update_util - Take a note about CPU utilization changes.
3394 * @rq: Runqueue to carry out the update for.
3395 * @flags: Update reason flags.
3396 *
3397 * This function is called by the scheduler on the CPU whose utilization is
3398 * being updated.
3399 *
3400 * It can only be called from RCU-sched read-side critical sections.
3401 *
3402 * The way cpufreq is currently arranged requires it to evaluate the CPU
3403 * performance state (frequency/voltage) on a regular basis to prevent it from
3404 * being stuck in a completely inadequate performance level for too long.
3405 * That is not guaranteed to happen if the updates are only triggered from CFS
3406 * and DL, though, because they may not be coming in if only RT tasks are
3407 * active all the time (or there are RT tasks only).
3408 *
3409 * As a workaround for that issue, this function is called periodically by the
3410 * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
3411 * but that really is a band-aid. Going forward it should be replaced with
3412 * solutions targeted more specifically at RT tasks.
3413 */
cpufreq_update_util(struct rq * rq,unsigned int flags)3414 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
3415 {
3416 struct update_util_data *data;
3417
3418 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
3419 cpu_of(rq)));
3420 if (data)
3421 data->func(data, rq_clock(rq), flags);
3422 }
3423 #else /* !CONFIG_CPU_FREQ: */
cpufreq_update_util(struct rq * rq,unsigned int flags)3424 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) { }
3425 #endif /* !CONFIG_CPU_FREQ */
3426
3427 #ifdef arch_scale_freq_capacity
3428 # ifndef arch_scale_freq_invariant
3429 # define arch_scale_freq_invariant() true
3430 # endif
3431 #else
3432 # define arch_scale_freq_invariant() false
3433 #endif
3434
3435 #ifdef CONFIG_SMP
3436
3437 unsigned long effective_cpu_util(int cpu, unsigned long util_cfs,
3438 unsigned long *min,
3439 unsigned long *max);
3440
3441 unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual,
3442 unsigned long min,
3443 unsigned long max);
3444
3445
3446 /*
3447 * Verify the fitness of task @p to run on @cpu taking into account the
3448 * CPU original capacity and the runtime/deadline ratio of the task.
3449 *
3450 * The function will return true if the original capacity of @cpu is
3451 * greater than or equal to task's deadline density right shifted by
3452 * (BW_SHIFT - SCHED_CAPACITY_SHIFT) and false otherwise.
3453 */
dl_task_fits_capacity(struct task_struct * p,int cpu)3454 static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu)
3455 {
3456 unsigned long cap = arch_scale_cpu_capacity(cpu);
3457
3458 return cap >= p->dl.dl_density >> (BW_SHIFT - SCHED_CAPACITY_SHIFT);
3459 }
3460
cpu_bw_dl(struct rq * rq)3461 static inline unsigned long cpu_bw_dl(struct rq *rq)
3462 {
3463 return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
3464 }
3465
cpu_util_dl(struct rq * rq)3466 static inline unsigned long cpu_util_dl(struct rq *rq)
3467 {
3468 return READ_ONCE(rq->avg_dl.util_avg);
3469 }
3470
3471
3472 extern unsigned long cpu_util_cfs(int cpu);
3473 extern unsigned long cpu_util_cfs_boost(int cpu);
3474
cpu_util_rt(struct rq * rq)3475 static inline unsigned long cpu_util_rt(struct rq *rq)
3476 {
3477 return READ_ONCE(rq->avg_rt.util_avg);
3478 }
3479
3480 #else /* !CONFIG_SMP */
update_other_load_avgs(struct rq * rq)3481 static inline bool update_other_load_avgs(struct rq *rq) { return false; }
3482 #endif /* CONFIG_SMP */
3483
3484 #ifdef CONFIG_UCLAMP_TASK
3485
3486 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
3487
3488 /*
3489 * When uclamp is compiled in, the aggregation at rq level is 'turned off'
3490 * by default in the fast path and only gets turned on once userspace performs
3491 * an operation that requires it.
3492 *
3493 * Returns true if userspace opted-in to use uclamp and aggregation at rq level
3494 * hence is active.
3495 */
uclamp_is_used(void)3496 static inline bool uclamp_is_used(void)
3497 {
3498 return static_branch_likely(&sched_uclamp_used);
3499 }
3500
uclamp_rq_get(struct rq * rq,enum uclamp_id clamp_id)3501 static inline unsigned long uclamp_rq_get(struct rq *rq,
3502 enum uclamp_id clamp_id)
3503 {
3504 return READ_ONCE(rq->uclamp[clamp_id].value);
3505 }
3506
uclamp_rq_set(struct rq * rq,enum uclamp_id clamp_id,unsigned int value)3507 static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id,
3508 unsigned int value)
3509 {
3510 WRITE_ONCE(rq->uclamp[clamp_id].value, value);
3511 }
3512
uclamp_rq_is_idle(struct rq * rq)3513 static inline bool uclamp_rq_is_idle(struct rq *rq)
3514 {
3515 return rq->uclamp_flags & UCLAMP_FLAG_IDLE;
3516 }
3517
3518 /* Is the rq being capped/throttled by uclamp_max? */
uclamp_rq_is_capped(struct rq * rq)3519 static inline bool uclamp_rq_is_capped(struct rq *rq)
3520 {
3521 unsigned long rq_util;
3522 unsigned long max_util;
3523
3524 if (!uclamp_is_used())
3525 return false;
3526
3527 rq_util = cpu_util_cfs(cpu_of(rq)) + cpu_util_rt(rq);
3528 max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
3529
3530 return max_util != SCHED_CAPACITY_SCALE && rq_util >= max_util;
3531 }
3532
3533 #define for_each_clamp_id(clamp_id) \
3534 for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++)
3535
3536 extern unsigned int sysctl_sched_uclamp_util_min_rt_default;
3537
3538
uclamp_none(enum uclamp_id clamp_id)3539 static inline unsigned int uclamp_none(enum uclamp_id clamp_id)
3540 {
3541 if (clamp_id == UCLAMP_MIN)
3542 return 0;
3543 return SCHED_CAPACITY_SCALE;
3544 }
3545
3546 /* Integer rounded range for each bucket */
3547 #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS)
3548
uclamp_bucket_id(unsigned int clamp_value)3549 static inline unsigned int uclamp_bucket_id(unsigned int clamp_value)
3550 {
3551 return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1);
3552 }
3553
3554 static inline void
uclamp_se_set(struct uclamp_se * uc_se,unsigned int value,bool user_defined)3555 uclamp_se_set(struct uclamp_se *uc_se, unsigned int value, bool user_defined)
3556 {
3557 uc_se->value = value;
3558 uc_se->bucket_id = uclamp_bucket_id(value);
3559 uc_se->user_defined = user_defined;
3560 }
3561
3562 #else /* !CONFIG_UCLAMP_TASK: */
3563
3564 static inline unsigned long
uclamp_eff_value(struct task_struct * p,enum uclamp_id clamp_id)3565 uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
3566 {
3567 if (clamp_id == UCLAMP_MIN)
3568 return 0;
3569
3570 return SCHED_CAPACITY_SCALE;
3571 }
3572
uclamp_rq_is_capped(struct rq * rq)3573 static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; }
3574
uclamp_is_used(void)3575 static inline bool uclamp_is_used(void)
3576 {
3577 return false;
3578 }
3579
3580 static inline unsigned long
uclamp_rq_get(struct rq * rq,enum uclamp_id clamp_id)3581 uclamp_rq_get(struct rq *rq, enum uclamp_id clamp_id)
3582 {
3583 if (clamp_id == UCLAMP_MIN)
3584 return 0;
3585
3586 return SCHED_CAPACITY_SCALE;
3587 }
3588
3589 static inline void
uclamp_rq_set(struct rq * rq,enum uclamp_id clamp_id,unsigned int value)3590 uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, unsigned int value)
3591 {
3592 }
3593
uclamp_rq_is_idle(struct rq * rq)3594 static inline bool uclamp_rq_is_idle(struct rq *rq)
3595 {
3596 return false;
3597 }
3598
3599 #endif /* !CONFIG_UCLAMP_TASK */
3600
3601 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
3602
cpu_util_irq(struct rq * rq)3603 static inline unsigned long cpu_util_irq(struct rq *rq)
3604 {
3605 return READ_ONCE(rq->avg_irq.util_avg);
3606 }
3607
3608 static inline
scale_irq_capacity(unsigned long util,unsigned long irq,unsigned long max)3609 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
3610 {
3611 util *= (max - irq);
3612 util /= max;
3613
3614 return util;
3615
3616 }
3617
3618 #else /* !CONFIG_HAVE_SCHED_AVG_IRQ: */
3619
cpu_util_irq(struct rq * rq)3620 static inline unsigned long cpu_util_irq(struct rq *rq)
3621 {
3622 return 0;
3623 }
3624
3625 static inline
scale_irq_capacity(unsigned long util,unsigned long irq,unsigned long max)3626 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
3627 {
3628 return util;
3629 }
3630
3631 #endif /* !CONFIG_HAVE_SCHED_AVG_IRQ */
3632
3633 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
3634
3635 #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus)))
3636
3637 DECLARE_STATIC_KEY_FALSE(sched_energy_present);
3638
sched_energy_enabled(void)3639 static inline bool sched_energy_enabled(void)
3640 {
3641 return static_branch_unlikely(&sched_energy_present);
3642 }
3643
3644 extern struct cpufreq_governor schedutil_gov;
3645
3646 #else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */
3647
3648 #define perf_domain_span(pd) NULL
3649
sched_energy_enabled(void)3650 static inline bool sched_energy_enabled(void) { return false; }
3651
3652 #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
3653
3654 #ifdef CONFIG_MEMBARRIER
3655
3656 /*
3657 * The scheduler provides memory barriers required by membarrier between:
3658 * - prior user-space memory accesses and store to rq->membarrier_state,
3659 * - store to rq->membarrier_state and following user-space memory accesses.
3660 * In the same way it provides those guarantees around store to rq->curr.
3661 */
membarrier_switch_mm(struct rq * rq,struct mm_struct * prev_mm,struct mm_struct * next_mm)3662 static inline void membarrier_switch_mm(struct rq *rq,
3663 struct mm_struct *prev_mm,
3664 struct mm_struct *next_mm)
3665 {
3666 int membarrier_state;
3667
3668 if (prev_mm == next_mm)
3669 return;
3670
3671 membarrier_state = atomic_read(&next_mm->membarrier_state);
3672 if (READ_ONCE(rq->membarrier_state) == membarrier_state)
3673 return;
3674
3675 WRITE_ONCE(rq->membarrier_state, membarrier_state);
3676 }
3677
3678 #else /* !CONFIG_MEMBARRIER :*/
3679
membarrier_switch_mm(struct rq * rq,struct mm_struct * prev_mm,struct mm_struct * next_mm)3680 static inline void membarrier_switch_mm(struct rq *rq,
3681 struct mm_struct *prev_mm,
3682 struct mm_struct *next_mm)
3683 {
3684 }
3685
3686 #endif /* !CONFIG_MEMBARRIER */
3687
3688 #ifdef CONFIG_SMP
is_per_cpu_kthread(struct task_struct * p)3689 static inline bool is_per_cpu_kthread(struct task_struct *p)
3690 {
3691 if (!(p->flags & PF_KTHREAD))
3692 return false;
3693
3694 if (p->nr_cpus_allowed != 1)
3695 return false;
3696
3697 return true;
3698 }
3699 #endif
3700
3701 extern void swake_up_all_locked(struct swait_queue_head *q);
3702 extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
3703
3704 extern int try_to_wake_up(struct task_struct *tsk, unsigned int state, int wake_flags);
3705
3706 #ifdef CONFIG_PREEMPT_DYNAMIC
3707 extern int preempt_dynamic_mode;
3708 extern int sched_dynamic_mode(const char *str);
3709 extern void sched_dynamic_update(int mode);
3710 #endif
3711
3712 #ifdef CONFIG_SCHED_MM_CID
3713
3714 #define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */
3715 #define MM_CID_SCAN_DELAY 100 /* 100ms */
3716
3717 extern raw_spinlock_t cid_lock;
3718 extern int use_cid_lock;
3719
3720 extern void sched_mm_cid_migrate_from(struct task_struct *t);
3721 extern void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t);
3722 extern void task_tick_mm_cid(struct rq *rq, struct task_struct *curr);
3723 extern void init_sched_mm_cid(struct task_struct *t);
3724
__mm_cid_put(struct mm_struct * mm,int cid)3725 static inline void __mm_cid_put(struct mm_struct *mm, int cid)
3726 {
3727 if (cid < 0)
3728 return;
3729 cpumask_clear_cpu(cid, mm_cidmask(mm));
3730 }
3731
3732 /*
3733 * The per-mm/cpu cid can have the MM_CID_LAZY_PUT flag set or transition to
3734 * the MM_CID_UNSET state without holding the rq lock, but the rq lock needs to
3735 * be held to transition to other states.
3736 *
3737 * State transitions synchronized with cmpxchg or try_cmpxchg need to be
3738 * consistent across CPUs, which prevents use of this_cpu_cmpxchg.
3739 */
mm_cid_put_lazy(struct task_struct * t)3740 static inline void mm_cid_put_lazy(struct task_struct *t)
3741 {
3742 struct mm_struct *mm = t->mm;
3743 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
3744 int cid;
3745
3746 lockdep_assert_irqs_disabled();
3747 cid = __this_cpu_read(pcpu_cid->cid);
3748 if (!mm_cid_is_lazy_put(cid) ||
3749 !try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET))
3750 return;
3751 __mm_cid_put(mm, mm_cid_clear_lazy_put(cid));
3752 }
3753
mm_cid_pcpu_unset(struct mm_struct * mm)3754 static inline int mm_cid_pcpu_unset(struct mm_struct *mm)
3755 {
3756 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
3757 int cid, res;
3758
3759 lockdep_assert_irqs_disabled();
3760 cid = __this_cpu_read(pcpu_cid->cid);
3761 for (;;) {
3762 if (mm_cid_is_unset(cid))
3763 return MM_CID_UNSET;
3764 /*
3765 * Attempt transition from valid or lazy-put to unset.
3766 */
3767 res = cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, cid, MM_CID_UNSET);
3768 if (res == cid)
3769 break;
3770 cid = res;
3771 }
3772 return cid;
3773 }
3774
mm_cid_put(struct mm_struct * mm)3775 static inline void mm_cid_put(struct mm_struct *mm)
3776 {
3777 int cid;
3778
3779 lockdep_assert_irqs_disabled();
3780 cid = mm_cid_pcpu_unset(mm);
3781 if (cid == MM_CID_UNSET)
3782 return;
3783 __mm_cid_put(mm, mm_cid_clear_lazy_put(cid));
3784 }
3785
__mm_cid_try_get(struct mm_struct * mm)3786 static inline int __mm_cid_try_get(struct mm_struct *mm)
3787 {
3788 struct cpumask *cpumask;
3789 int cid;
3790
3791 cpumask = mm_cidmask(mm);
3792 /*
3793 * Retry finding first zero bit if the mask is temporarily
3794 * filled. This only happens during concurrent remote-clear
3795 * which owns a cid without holding a rq lock.
3796 */
3797 for (;;) {
3798 cid = cpumask_first_zero(cpumask);
3799 if (cid < nr_cpu_ids)
3800 break;
3801 cpu_relax();
3802 }
3803 if (cpumask_test_and_set_cpu(cid, cpumask))
3804 return -1;
3805
3806 return cid;
3807 }
3808
3809 /*
3810 * Save a snapshot of the current runqueue time of this cpu
3811 * with the per-cpu cid value, allowing to estimate how recently it was used.
3812 */
mm_cid_snapshot_time(struct rq * rq,struct mm_struct * mm)3813 static inline void mm_cid_snapshot_time(struct rq *rq, struct mm_struct *mm)
3814 {
3815 struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu_of(rq));
3816
3817 lockdep_assert_rq_held(rq);
3818 WRITE_ONCE(pcpu_cid->time, rq->clock);
3819 }
3820
__mm_cid_get(struct rq * rq,struct mm_struct * mm)3821 static inline int __mm_cid_get(struct rq *rq, struct mm_struct *mm)
3822 {
3823 int cid;
3824
3825 /*
3826 * All allocations (even those using the cid_lock) are lock-free. If
3827 * use_cid_lock is set, hold the cid_lock to perform cid allocation to
3828 * guarantee forward progress.
3829 */
3830 if (!READ_ONCE(use_cid_lock)) {
3831 cid = __mm_cid_try_get(mm);
3832 if (cid >= 0)
3833 goto end;
3834 raw_spin_lock(&cid_lock);
3835 } else {
3836 raw_spin_lock(&cid_lock);
3837 cid = __mm_cid_try_get(mm);
3838 if (cid >= 0)
3839 goto unlock;
3840 }
3841
3842 /*
3843 * cid concurrently allocated. Retry while forcing following
3844 * allocations to use the cid_lock to ensure forward progress.
3845 */
3846 WRITE_ONCE(use_cid_lock, 1);
3847 /*
3848 * Set use_cid_lock before allocation. Only care about program order
3849 * because this is only required for forward progress.
3850 */
3851 barrier();
3852 /*
3853 * Retry until it succeeds. It is guaranteed to eventually succeed once
3854 * all newcoming allocations observe the use_cid_lock flag set.
3855 */
3856 do {
3857 cid = __mm_cid_try_get(mm);
3858 cpu_relax();
3859 } while (cid < 0);
3860 /*
3861 * Allocate before clearing use_cid_lock. Only care about
3862 * program order because this is for forward progress.
3863 */
3864 barrier();
3865 WRITE_ONCE(use_cid_lock, 0);
3866 unlock:
3867 raw_spin_unlock(&cid_lock);
3868 end:
3869 mm_cid_snapshot_time(rq, mm);
3870
3871 return cid;
3872 }
3873
mm_cid_get(struct rq * rq,struct mm_struct * mm)3874 static inline int mm_cid_get(struct rq *rq, struct mm_struct *mm)
3875 {
3876 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
3877 struct cpumask *cpumask;
3878 int cid;
3879
3880 lockdep_assert_rq_held(rq);
3881 cpumask = mm_cidmask(mm);
3882 cid = __this_cpu_read(pcpu_cid->cid);
3883 if (mm_cid_is_valid(cid)) {
3884 mm_cid_snapshot_time(rq, mm);
3885 return cid;
3886 }
3887 if (mm_cid_is_lazy_put(cid)) {
3888 if (try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET))
3889 __mm_cid_put(mm, mm_cid_clear_lazy_put(cid));
3890 }
3891 cid = __mm_cid_get(rq, mm);
3892 __this_cpu_write(pcpu_cid->cid, cid);
3893
3894 return cid;
3895 }
3896
switch_mm_cid(struct rq * rq,struct task_struct * prev,struct task_struct * next)3897 static inline void switch_mm_cid(struct rq *rq,
3898 struct task_struct *prev,
3899 struct task_struct *next)
3900 {
3901 /*
3902 * Provide a memory barrier between rq->curr store and load of
3903 * {prev,next}->mm->pcpu_cid[cpu] on rq->curr->mm transition.
3904 *
3905 * Should be adapted if context_switch() is modified.
3906 */
3907 if (!next->mm) { // to kernel
3908 /*
3909 * user -> kernel transition does not guarantee a barrier, but
3910 * we can use the fact that it performs an atomic operation in
3911 * mmgrab().
3912 */
3913 if (prev->mm) // from user
3914 smp_mb__after_mmgrab();
3915 /*
3916 * kernel -> kernel transition does not change rq->curr->mm
3917 * state. It stays NULL.
3918 */
3919 } else { // to user
3920 /*
3921 * kernel -> user transition does not provide a barrier
3922 * between rq->curr store and load of {prev,next}->mm->pcpu_cid[cpu].
3923 * Provide it here.
3924 */
3925 if (!prev->mm) { // from kernel
3926 smp_mb();
3927 } else { // from user
3928 /*
3929 * user->user transition relies on an implicit
3930 * memory barrier in switch_mm() when
3931 * current->mm changes. If the architecture
3932 * switch_mm() does not have an implicit memory
3933 * barrier, it is emitted here. If current->mm
3934 * is unchanged, no barrier is needed.
3935 */
3936 smp_mb__after_switch_mm();
3937 }
3938 }
3939 if (prev->mm_cid_active) {
3940 mm_cid_snapshot_time(rq, prev->mm);
3941 mm_cid_put_lazy(prev);
3942 prev->mm_cid = -1;
3943 }
3944 if (next->mm_cid_active)
3945 next->last_mm_cid = next->mm_cid = mm_cid_get(rq, next->mm);
3946 }
3947
3948 #else /* !CONFIG_SCHED_MM_CID: */
switch_mm_cid(struct rq * rq,struct task_struct * prev,struct task_struct * next)3949 static inline void switch_mm_cid(struct rq *rq, struct task_struct *prev, struct task_struct *next) { }
sched_mm_cid_migrate_from(struct task_struct * t)3950 static inline void sched_mm_cid_migrate_from(struct task_struct *t) { }
sched_mm_cid_migrate_to(struct rq * dst_rq,struct task_struct * t)3951 static inline void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t) { }
task_tick_mm_cid(struct rq * rq,struct task_struct * curr)3952 static inline void task_tick_mm_cid(struct rq *rq, struct task_struct *curr) { }
init_sched_mm_cid(struct task_struct * t)3953 static inline void init_sched_mm_cid(struct task_struct *t) { }
3954 #endif /* !CONFIG_SCHED_MM_CID */
3955
3956 extern u64 avg_vruntime(struct cfs_rq *cfs_rq);
3957 extern int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se);
3958 #ifdef CONFIG_SMP
3959 static inline
__move_queued_task_locked(struct rq * src_rq,struct rq * dst_rq,struct task_struct * task)3960 void __move_queued_task_locked(struct rq *src_rq, struct rq *dst_rq, struct task_struct *task)
3961 {
3962 lockdep_assert_rq_held(src_rq);
3963 lockdep_assert_rq_held(dst_rq);
3964
3965 deactivate_task(src_rq, task, 0);
3966 set_task_cpu(task, dst_rq->cpu);
3967 activate_task(dst_rq, task, 0);
3968 }
3969
3970 static inline
__task_is_pushable(struct rq * rq,struct task_struct * p,int cpu)3971 int __task_is_pushable(struct rq *rq, struct task_struct *p, int cpu)
3972 {
3973 if (!task_on_cpu(rq, p))
3974 return cpumask_test_cpu(cpu, &p->cpus_mask) ? 1 : -1;
3975
3976 return 0;
3977 }
3978 #endif /* CONFIG_SMP */
3979
3980 #ifdef CONFIG_SCHED_PROXY_EXEC
3981 void move_queued_task_locked(struct rq *rq, struct rq *dst_rq, struct task_struct *task);
3982 int task_is_pushable(struct rq *rq, struct task_struct *p, int cpu);
3983 struct task_struct *find_exec_ctx(struct rq *rq, struct task_struct *p);
3984 #else /* !CONFIG_SCHED_PROXY_EXEC */
3985 #ifdef CONFIG_SMP
3986 static inline
move_queued_task_locked(struct rq * rq,struct rq * dst_rq,struct task_struct * task)3987 void move_queued_task_locked(struct rq *rq, struct rq *dst_rq, struct task_struct *task)
3988 {
3989 __move_queued_task_locked(rq, dst_rq, task);
3990 }
3991
3992 static inline
task_is_pushable(struct rq * rq,struct task_struct * p,int cpu)3993 int task_is_pushable(struct rq *rq, struct task_struct *p, int cpu)
3994 {
3995 return __task_is_pushable(rq, p, cpu);
3996 }
3997 #endif
3998 static inline
find_exec_ctx(struct rq * rq,struct task_struct * p)3999 struct task_struct *find_exec_ctx(struct rq *rq, struct task_struct *p)
4000 {
4001 return p;
4002 }
4003 #endif /* CONFIG_SCHED_PROXY_EXEC */
4004
4005 #ifdef CONFIG_RT_MUTEXES
4006
__rt_effective_prio(struct task_struct * pi_task,int prio)4007 static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
4008 {
4009 if (pi_task)
4010 prio = min(prio, pi_task->prio);
4011
4012 return prio;
4013 }
4014
rt_effective_prio(struct task_struct * p,int prio)4015 static inline int rt_effective_prio(struct task_struct *p, int prio)
4016 {
4017 struct task_struct *pi_task = rt_mutex_get_top_task(p);
4018
4019 return __rt_effective_prio(pi_task, prio);
4020 }
4021
4022 #else /* !CONFIG_RT_MUTEXES: */
4023
rt_effective_prio(struct task_struct * p,int prio)4024 static inline int rt_effective_prio(struct task_struct *p, int prio)
4025 {
4026 return prio;
4027 }
4028
4029 #endif /* !CONFIG_RT_MUTEXES */
4030
4031 extern int __sched_setscheduler(struct task_struct *p, const struct sched_attr *attr, bool user, bool pi);
4032 extern int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx);
4033 extern const struct sched_class *__setscheduler_class(int policy, int prio);
4034 extern void set_load_weight(struct task_struct *p, bool update_load);
4035 extern void enqueue_task(struct rq *rq, struct task_struct *p, int flags);
4036 extern bool dequeue_task(struct rq *rq, struct task_struct *p, int flags);
4037
4038 extern void check_class_changing(struct rq *rq, struct task_struct *p,
4039 const struct sched_class *prev_class);
4040 extern void check_class_changed(struct rq *rq, struct task_struct *p,
4041 const struct sched_class *prev_class,
4042 int oldprio);
4043
4044 #ifdef CONFIG_SMP
4045 extern struct balance_callback *splice_balance_callbacks(struct rq *rq);
4046 extern void balance_callbacks(struct rq *rq, struct balance_callback *head);
4047 #else
4048
splice_balance_callbacks(struct rq * rq)4049 static inline struct balance_callback *splice_balance_callbacks(struct rq *rq)
4050 {
4051 return NULL;
4052 }
4053
balance_callbacks(struct rq * rq,struct balance_callback * head)4054 static inline void balance_callbacks(struct rq *rq, struct balance_callback *head)
4055 {
4056 }
4057
4058 #endif
4059
4060 #ifdef CONFIG_SCHED_CLASS_EXT
4061 /*
4062 * Used by SCX in the enable/disable paths to move tasks between sched_classes
4063 * and establish invariants.
4064 */
4065 struct sched_enq_and_set_ctx {
4066 struct task_struct *p;
4067 int queue_flags;
4068 bool queued;
4069 bool running;
4070 };
4071
4072 void sched_deq_and_put_task(struct task_struct *p, int queue_flags,
4073 struct sched_enq_and_set_ctx *ctx);
4074 void sched_enq_and_set_task(struct sched_enq_and_set_ctx *ctx);
4075
4076 #endif /* CONFIG_SCHED_CLASS_EXT */
4077
4078 #include "ext.h"
4079
4080
4081 #ifdef CONFIG_RT_SOFTIRQ_AWARE_SCHED
4082 extern bool cpu_busy_with_softirqs(int cpu);
4083 #else
cpu_busy_with_softirqs(int cpu)4084 static inline bool cpu_busy_with_softirqs(int cpu)
4085 {
4086 return false;
4087 }
4088 #endif /* CONFIG_RT_SOFTIRQ_AWARE_SCHED */
4089
4090 #endif /* _KERNEL_SCHED_SCHED_H */
4091