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