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