1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Scheduler internal types and methods:
5 #ifndef _KERNEL_SCHED_SCHED_H
6 #define _KERNEL_SCHED_SCHED_H
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>
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>
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>
73 #include <trace/events/power.h>
74 #include <trace/events/sched.h>
76 #include "../workqueue_internal.h"
84 #ifdef CONFIG_PARAVIRT
85 # include <asm/paravirt.h>
86 # include <asm/paravirt_api_clock.h>
89 #include <asm/barrier.h>
92 #include "cpudeadline.h"
94 /* task_struct::on_rq states: */
95 #define TASK_ON_RQ_QUEUED 1
96 #define TASK_ON_RQ_MIGRATING 2
98 extern __read_mostly int scheduler_running;
100 extern unsigned long calc_load_update;
101 extern atomic_long_t calc_load_tasks;
103 extern void calc_global_load_tick(struct rq *this_rq);
104 extern long calc_load_fold_active(struct rq *this_rq, long adjust);
106 extern void call_trace_sched_update_nr_running(struct rq *rq, int count);
108 extern int sysctl_sched_rt_period;
109 extern int sysctl_sched_rt_runtime;
110 extern int sched_rr_timeslice;
113 * Asymmetric CPU capacity bits
115 struct asym_cap_data {
116 struct list_head link;
118 unsigned long capacity;
119 unsigned long cpus[];
122 extern struct list_head asym_cap_list;
124 #define cpu_capacity_span(asym_data) to_cpumask((asym_data)->cpus)
127 * Helpers for converting nanosecond timing to jiffy resolution
129 #define NS_TO_JIFFIES(time) ((unsigned long)(time) / (NSEC_PER_SEC/HZ))
132 * Increase resolution of nice-level calculations for 64-bit architectures.
133 * The extra resolution improves shares distribution and load balancing of
134 * low-weight task groups (eg. nice +19 on an autogroup), deeper task-group
135 * hierarchies, especially on larger systems. This is not a user-visible change
136 * and does not change the user-interface for setting shares/weights.
138 * We increase resolution only if we have enough bits to allow this increased
139 * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
140 * are pretty high and the returns do not justify the increased costs.
142 * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to
143 * increase coverage and consistency always enable it on 64-bit platforms.
146 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
147 # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
148 # define scale_load_down(w) \
150 unsigned long __w = (w); \
153 __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \
157 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
158 # define scale_load(w) (w)
159 # define scale_load_down(w) (w)
163 * Task weight (visible to users) and its load (invisible to users) have
164 * independent resolution, but they should be well calibrated. We use
165 * scale_load() and scale_load_down(w) to convert between them. The
166 * following must be true:
168 * scale_load(sched_prio_to_weight[NICE_TO_PRIO(0)-MAX_RT_PRIO]) == NICE_0_LOAD
171 #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
174 * Single value that decides SCHED_DEADLINE internal math precision.
175 * 10 -> just above 1us
176 * 9 -> just above 0.5us
181 * Single value that denotes runtime == period, ie unlimited time.
183 #define RUNTIME_INF ((u64)~0ULL)
185 static inline int idle_policy(int policy)
187 return policy == SCHED_IDLE;
190 static inline int normal_policy(int policy)
192 #ifdef CONFIG_SCHED_CLASS_EXT
193 if (policy == SCHED_EXT)
196 return policy == SCHED_NORMAL;
199 static inline int fair_policy(int policy)
201 return normal_policy(policy) || policy == SCHED_BATCH;
204 static inline int rt_policy(int policy)
206 return policy == SCHED_FIFO || policy == SCHED_RR;
209 static inline int dl_policy(int policy)
211 return policy == SCHED_DEADLINE;
214 static inline bool valid_policy(int policy)
216 return idle_policy(policy) || fair_policy(policy) ||
217 rt_policy(policy) || dl_policy(policy);
220 static inline int task_has_idle_policy(struct task_struct *p)
222 return idle_policy(p->policy);
225 static inline int task_has_rt_policy(struct task_struct *p)
227 return rt_policy(p->policy);
230 static inline int task_has_dl_policy(struct task_struct *p)
232 return dl_policy(p->policy);
235 #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
237 static inline void update_avg(u64 *avg, u64 sample)
239 s64 diff = sample - *avg;
245 * Shifting a value by an exponent greater *or equal* to the size of said value
246 * is UB; cap at size-1.
248 #define shr_bound(val, shift) \
249 (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1))
252 * cgroup weight knobs should use the common MIN, DFL and MAX values which are
253 * 1, 100 and 10000 respectively. While it loses a bit of range on both ends, it
254 * maps pretty well onto the shares value used by scheduler and the round-trip
255 * conversions preserve the original value over the entire range.
257 static inline unsigned long sched_weight_from_cgroup(unsigned long cgrp_weight)
259 return DIV_ROUND_CLOSEST_ULL(cgrp_weight * 1024, CGROUP_WEIGHT_DFL);
262 static inline unsigned long sched_weight_to_cgroup(unsigned long weight)
264 return clamp_t(unsigned long,
265 DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024),
266 CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
270 * !! For sched_setattr_nocheck() (kernel) only !!
272 * This is actually gross. :(
274 * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE
275 * tasks, but still be able to sleep. We need this on platforms that cannot
276 * atomically change clock frequency. Remove once fast switching will be
277 * available on such platforms.
279 * SUGOV stands for SchedUtil GOVernor.
281 #define SCHED_FLAG_SUGOV 0x10000000
283 #define SCHED_DL_FLAGS (SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV)
285 static inline bool dl_entity_is_special(const struct sched_dl_entity *dl_se)
287 #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
288 return unlikely(dl_se->flags & SCHED_FLAG_SUGOV);
295 * Tells if entity @a should preempt entity @b.
297 static inline bool dl_entity_preempt(const struct sched_dl_entity *a,
298 const struct sched_dl_entity *b)
300 return dl_entity_is_special(a) ||
301 dl_time_before(a->deadline, b->deadline);
305 * This is the priority-queue data structure of the RT scheduling class:
307 struct rt_prio_array {
308 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
309 struct list_head queue[MAX_RT_PRIO];
312 struct rt_bandwidth {
313 /* nests inside the rq lock: */
314 raw_spinlock_t rt_runtime_lock;
317 struct hrtimer rt_period_timer;
318 unsigned int rt_period_active;
321 static inline int dl_bandwidth_enabled(void)
323 return sysctl_sched_rt_runtime >= 0;
327 * To keep the bandwidth of -deadline tasks under control
328 * we need some place where:
329 * - store the maximum -deadline bandwidth of each cpu;
330 * - cache the fraction of bandwidth that is currently allocated in
333 * This is all done in the data structure below. It is similar to the
334 * one used for RT-throttling (rt_bandwidth), with the main difference
335 * that, since here we are only interested in admission control, we
336 * do not decrease any runtime while the group "executes", neither we
337 * need a timer to replenish it.
339 * With respect to SMP, bandwidth is given on a per root domain basis,
341 * - bw (< 100%) is the deadline bandwidth of each CPU;
342 * - total_bw is the currently allocated bandwidth in each root domain;
350 extern void init_dl_bw(struct dl_bw *dl_b);
351 extern int sched_dl_global_validate(void);
352 extern void sched_dl_do_global(void);
353 extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr);
354 extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
355 extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
356 extern bool __checkparam_dl(const struct sched_attr *attr);
357 extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
358 extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
359 extern int dl_bw_deactivate(int cpu);
360 extern s64 dl_scaled_delta_exec(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec);
362 * SCHED_DEADLINE supports servers (nested scheduling) with the following
365 * dl_se::rq -- runqueue we belong to.
367 * dl_se::server_has_tasks() -- used on bandwidth enforcement; we 'stop' the
368 * server when it runs out of tasks to run.
370 * dl_se::server_pick() -- nested pick_next_task(); we yield the period if this
373 * dl_server_update() -- called from update_curr_common(), propagates runtime
377 * dl_server_stop() -- start/stop the server when it has (no) tasks.
379 * dl_server_init() -- initializes the server.
381 extern void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec);
382 extern void dl_server_start(struct sched_dl_entity *dl_se);
383 extern void dl_server_stop(struct sched_dl_entity *dl_se);
384 extern void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq,
385 dl_server_has_tasks_f has_tasks,
386 dl_server_pick_f pick_task);
388 extern void dl_server_update_idle_time(struct rq *rq,
389 struct task_struct *p);
390 extern void fair_server_init(struct rq *rq);
391 extern void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq);
392 extern int dl_server_apply_params(struct sched_dl_entity *dl_se,
393 u64 runtime, u64 period, bool init);
395 static inline bool dl_server_active(struct sched_dl_entity *dl_se)
397 return dl_se->dl_server_active;
400 #ifdef CONFIG_CGROUP_SCHED
402 extern struct list_head task_groups;
404 struct cfs_bandwidth {
405 #ifdef CONFIG_CFS_BANDWIDTH
412 s64 hierarchical_quota;
417 struct hrtimer period_timer;
418 struct hrtimer slack_timer;
419 struct list_head throttled_cfs_rq;
430 /* Task group related information */
432 struct cgroup_subsys_state css;
434 #ifdef CONFIG_GROUP_SCHED_WEIGHT
435 /* A positive value indicates that this is a SCHED_IDLE group. */
439 #ifdef CONFIG_FAIR_GROUP_SCHED
440 /* schedulable entities of this group on each CPU */
441 struct sched_entity **se;
442 /* runqueue "owned" by this group on each CPU */
443 struct cfs_rq **cfs_rq;
444 unsigned long shares;
447 * load_avg can be heavily contended at clock tick time, so put
448 * it in its own cache-line separated from the fields above which
449 * will also be accessed at each tick.
451 atomic_long_t load_avg ____cacheline_aligned;
455 #ifdef CONFIG_RT_GROUP_SCHED
456 struct sched_rt_entity **rt_se;
457 struct rt_rq **rt_rq;
459 struct rt_bandwidth rt_bandwidth;
462 #ifdef CONFIG_EXT_GROUP_SCHED
463 u32 scx_flags; /* SCX_TG_* */
468 struct list_head list;
470 struct task_group *parent;
471 struct list_head siblings;
472 struct list_head children;
474 #ifdef CONFIG_SCHED_AUTOGROUP
475 struct autogroup *autogroup;
478 struct cfs_bandwidth cfs_bandwidth;
480 #ifdef CONFIG_UCLAMP_TASK_GROUP
481 /* The two decimal precision [%] value requested from user-space */
482 unsigned int uclamp_pct[UCLAMP_CNT];
483 /* Clamp values requested for a task group */
484 struct uclamp_se uclamp_req[UCLAMP_CNT];
485 /* Effective clamp values used for a task group */
486 struct uclamp_se uclamp[UCLAMP_CNT];
491 #ifdef CONFIG_GROUP_SCHED_WEIGHT
492 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
495 * A weight of 0 or 1 can cause arithmetics problems.
496 * A weight of a cfs_rq is the sum of weights of which entities
497 * are queued on this cfs_rq, so a weight of a entity should not be
498 * too large, so as the shares value of a task group.
499 * (The default weight is 1024 - so there's no practical
500 * limitation from this.)
502 #define MIN_SHARES (1UL << 1)
503 #define MAX_SHARES (1UL << 18)
506 typedef int (*tg_visitor)(struct task_group *, void *);
508 extern int walk_tg_tree_from(struct task_group *from,
509 tg_visitor down, tg_visitor up, void *data);
512 * Iterate the full tree, calling @down when first entering a node and @up when
513 * leaving it for the final time.
515 * Caller must hold rcu_lock or sufficient equivalent.
517 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
519 return walk_tg_tree_from(&root_task_group, down, up, data);
522 static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
524 return css ? container_of(css, struct task_group, css) : NULL;
527 extern int tg_nop(struct task_group *tg, void *data);
529 #ifdef CONFIG_FAIR_GROUP_SCHED
530 extern void free_fair_sched_group(struct task_group *tg);
531 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
532 extern void online_fair_sched_group(struct task_group *tg);
533 extern void unregister_fair_sched_group(struct task_group *tg);
535 static inline void free_fair_sched_group(struct task_group *tg) { }
536 static inline int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
540 static inline void online_fair_sched_group(struct task_group *tg) { }
541 static inline void unregister_fair_sched_group(struct task_group *tg) { }
544 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
545 struct sched_entity *se, int cpu,
546 struct sched_entity *parent);
547 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent);
549 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
550 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
551 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
552 extern bool cfs_task_bw_constrained(struct task_struct *p);
554 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
555 struct sched_rt_entity *rt_se, int cpu,
556 struct sched_rt_entity *parent);
557 extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
558 extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
559 extern long sched_group_rt_runtime(struct task_group *tg);
560 extern long sched_group_rt_period(struct task_group *tg);
561 extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
563 extern struct task_group *sched_create_group(struct task_group *parent);
564 extern void sched_online_group(struct task_group *tg,
565 struct task_group *parent);
566 extern void sched_destroy_group(struct task_group *tg);
567 extern void sched_release_group(struct task_group *tg);
569 extern void sched_move_task(struct task_struct *tsk, bool for_autogroup);
571 #ifdef CONFIG_FAIR_GROUP_SCHED
572 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
574 extern int sched_group_set_idle(struct task_group *tg, long idle);
577 extern void set_task_rq_fair(struct sched_entity *se,
578 struct cfs_rq *prev, struct cfs_rq *next);
579 #else /* !CONFIG_SMP */
580 static inline void set_task_rq_fair(struct sched_entity *se,
581 struct cfs_rq *prev, struct cfs_rq *next) { }
582 #endif /* CONFIG_SMP */
583 #else /* !CONFIG_FAIR_GROUP_SCHED */
584 static inline int sched_group_set_shares(struct task_group *tg, unsigned long shares) { return 0; }
585 static inline int sched_group_set_idle(struct task_group *tg, long idle) { return 0; }
586 #endif /* CONFIG_FAIR_GROUP_SCHED */
588 #else /* CONFIG_CGROUP_SCHED */
590 struct cfs_bandwidth { };
592 static inline bool cfs_task_bw_constrained(struct task_struct *p) { return false; }
594 #endif /* CONFIG_CGROUP_SCHED */
596 extern void unregister_rt_sched_group(struct task_group *tg);
597 extern void free_rt_sched_group(struct task_group *tg);
598 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
601 * u64_u32_load/u64_u32_store
603 * Use a copy of a u64 value to protect against data race. This is only
604 * applicable for 32-bits architectures.
607 # define u64_u32_load_copy(var, copy) var
608 # define u64_u32_store_copy(var, copy, val) (var = val)
610 # define u64_u32_load_copy(var, copy) \
612 u64 __val, __val_copy; \
616 * paired with u64_u32_store_copy(), ordering access \
621 } while (__val != __val_copy); \
624 # define u64_u32_store_copy(var, copy, val) \
626 typeof(val) __val = (val); \
629 * paired with u64_u32_load_copy(), ordering access to var and \
636 # define u64_u32_load(var) u64_u32_load_copy(var, var##_copy)
637 # define u64_u32_store(var, val) u64_u32_store_copy(var, var##_copy, val)
639 struct balance_callback {
640 struct balance_callback *next;
641 void (*func)(struct rq *rq);
644 /* CFS-related fields in a runqueue */
646 struct load_weight load;
647 unsigned int nr_queued;
648 unsigned int h_nr_queued; /* SCHED_{NORMAL,BATCH,IDLE} */
649 unsigned int h_nr_runnable; /* SCHED_{NORMAL,BATCH,IDLE} */
650 unsigned int h_nr_idle; /* SCHED_IDLE */
656 #ifdef CONFIG_SCHED_CORE
657 unsigned int forceidle_seq;
661 struct rb_root_cached tasks_timeline;
664 * 'curr' points to currently running entity on this cfs_rq.
665 * It is set to NULL otherwise (i.e when none are currently running).
667 struct sched_entity *curr;
668 struct sched_entity *next;
674 struct sched_avg avg;
676 u64 last_update_time_copy;
679 raw_spinlock_t lock ____cacheline_aligned;
681 unsigned long load_avg;
682 unsigned long util_avg;
683 unsigned long runnable_avg;
686 #ifdef CONFIG_FAIR_GROUP_SCHED
687 u64 last_update_tg_load_avg;
688 unsigned long tg_load_avg_contrib;
690 long prop_runnable_sum;
693 * h_load = weight * f(tg)
695 * Where f(tg) is the recursive weight fraction assigned to
698 unsigned long h_load;
699 u64 last_h_load_update;
700 struct sched_entity *h_load_next;
701 #endif /* CONFIG_FAIR_GROUP_SCHED */
702 #endif /* CONFIG_SMP */
704 #ifdef CONFIG_FAIR_GROUP_SCHED
705 struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */
708 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
709 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
710 * (like users, containers etc.)
712 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
713 * This list is used during load balance.
716 struct list_head leaf_cfs_rq_list;
717 struct task_group *tg; /* group that "owns" this runqueue */
719 /* Locally cached copy of our task_group's idle value */
722 #ifdef CONFIG_CFS_BANDWIDTH
724 s64 runtime_remaining;
726 u64 throttled_pelt_idle;
728 u64 throttled_pelt_idle_copy;
731 u64 throttled_clock_pelt;
732 u64 throttled_clock_pelt_time;
733 u64 throttled_clock_self;
734 u64 throttled_clock_self_time;
737 struct list_head throttled_list;
738 struct list_head throttled_csd_list;
739 #endif /* CONFIG_CFS_BANDWIDTH */
740 #endif /* CONFIG_FAIR_GROUP_SCHED */
743 #ifdef CONFIG_SCHED_CLASS_EXT
744 /* scx_rq->flags, protected by the rq lock */
747 * A hotplugged CPU starts scheduling before rq_online_scx(). Track
748 * ops.cpu_on/offline() state so that ops.enqueue/dispatch() are called
749 * only while the BPF scheduler considers the CPU to be online.
751 SCX_RQ_ONLINE = 1 << 0,
752 SCX_RQ_CAN_STOP_TICK = 1 << 1,
753 SCX_RQ_BAL_PENDING = 1 << 2, /* balance hasn't run yet */
754 SCX_RQ_BAL_KEEP = 1 << 3, /* balance decided to keep current */
755 SCX_RQ_BYPASSING = 1 << 4,
756 SCX_RQ_CLK_VALID = 1 << 5, /* RQ clock is fresh and valid */
758 SCX_RQ_IN_WAKEUP = 1 << 16,
759 SCX_RQ_IN_BALANCE = 1 << 17,
763 struct scx_dispatch_q local_dsq;
764 struct list_head runnable_list; /* runnable tasks on this rq */
765 struct list_head ddsp_deferred_locals; /* deferred ddsps from enq */
766 unsigned long ops_qseq;
767 u64 extra_enq_flags; /* see move_task_to_local_dsq() */
769 u32 cpuperf_target; /* [0, SCHED_CAPACITY_SCALE] */
772 u64 clock; /* current per-rq clock -- see scx_bpf_now() */
773 cpumask_var_t cpus_to_kick;
774 cpumask_var_t cpus_to_kick_if_idle;
775 cpumask_var_t cpus_to_preempt;
776 cpumask_var_t cpus_to_wait;
777 unsigned long pnt_seq;
778 struct balance_callback deferred_bal_cb;
779 struct irq_work deferred_irq_work;
780 struct irq_work kick_cpus_irq_work;
782 #endif /* CONFIG_SCHED_CLASS_EXT */
784 static inline int rt_bandwidth_enabled(void)
786 return sysctl_sched_rt_runtime >= 0;
789 /* RT IPI pull logic requires IRQ_WORK */
790 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
791 # define HAVE_RT_PUSH_IPI
794 /* Real-Time classes' related field in a runqueue: */
796 struct rt_prio_array active;
797 unsigned int rt_nr_running;
798 unsigned int rr_nr_running;
799 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
801 int curr; /* highest queued rt task prio */
803 int next; /* next highest */
809 struct plist_head pushable_tasks;
811 #endif /* CONFIG_SMP */
814 #ifdef CONFIG_RT_GROUP_SCHED
816 u64 rt_time; /* consumed RT time, goes up in update_curr_rt */
817 u64 rt_runtime; /* allotted RT time, "slice" from rt_bandwidth, RT sharing/balancing */
818 /* Nests inside the rq lock: */
819 raw_spinlock_t rt_runtime_lock;
821 unsigned int rt_nr_boosted;
823 struct rq *rq; /* this is always top-level rq, cache? */
825 #ifdef CONFIG_CGROUP_SCHED
826 struct task_group *tg; /* this tg has "this" rt_rq on given CPU for runnable entities */
830 static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
832 return rt_rq->rt_queued && rt_rq->rt_nr_running;
835 /* Deadline class' related fields in a runqueue */
837 /* runqueue is an rbtree, ordered by deadline */
838 struct rb_root_cached root;
840 unsigned int dl_nr_running;
844 * Deadline values of the currently executing and the
845 * earliest ready task on this rq. Caching these facilitates
846 * the decision whether or not a ready but not running task
847 * should migrate somewhere else.
857 * Tasks on this rq that can be pushed away. They are kept in
858 * an rb-tree, ordered by tasks' deadlines, with caching
859 * of the leftmost (earliest deadline) element.
861 struct rb_root_cached pushable_dl_tasks_root;
866 * "Active utilization" for this runqueue: increased when a
867 * task wakes up (becomes TASK_RUNNING) and decreased when a
873 * Utilization of the tasks "assigned" to this runqueue (including
874 * the tasks that are in runqueue and the tasks that executed on this
875 * CPU and blocked). Increased when a task moves to this runqueue, and
876 * decreased when the task moves away (migrates, changes scheduling
877 * policy, or terminates).
878 * This is needed to compute the "inactive utilization" for the
879 * runqueue (inactive utilization = this_bw - running_bw).
885 * Maximum available bandwidth for reclaiming by SCHED_FLAG_RECLAIM
886 * tasks of this rq. Used in calculation of reclaimable bandwidth(GRUB).
891 * Inverse of the fraction of CPU utilization that can be reclaimed
892 * by the GRUB algorithm.
897 #ifdef CONFIG_FAIR_GROUP_SCHED
899 /* An entity is a task if it doesn't "own" a runqueue */
900 #define entity_is_task(se) (!se->my_q)
902 static inline void se_update_runnable(struct sched_entity *se)
904 if (!entity_is_task(se))
905 se->runnable_weight = se->my_q->h_nr_runnable;
908 static inline long se_runnable(struct sched_entity *se)
910 if (se->sched_delayed)
913 if (entity_is_task(se))
916 return se->runnable_weight;
919 #else /* !CONFIG_FAIR_GROUP_SCHED: */
921 #define entity_is_task(se) 1
923 static inline void se_update_runnable(struct sched_entity *se) { }
925 static inline long se_runnable(struct sched_entity *se)
927 if (se->sched_delayed)
933 #endif /* !CONFIG_FAIR_GROUP_SCHED */
937 * XXX we want to get rid of these helpers and use the full load resolution.
939 static inline long se_weight(struct sched_entity *se)
941 return scale_load_down(se->load.weight);
945 static inline bool sched_asym_prefer(int a, int b)
947 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
951 struct em_perf_domain *em_pd;
952 struct perf_domain *next;
957 * We add the notion of a root-domain which will be used to define per-domain
958 * variables. Each exclusive cpuset essentially defines an island domain by
959 * fully partitioning the member CPUs from any other cpuset. Whenever a new
960 * exclusive cpuset is created, we also create and attach a new root-domain
969 cpumask_var_t online;
972 * Indicate pullable load on at least one CPU, e.g:
973 * - More than one runnable task
974 * - Running task is misfit
978 /* Indicate one or more CPUs over-utilized (tipping point) */
982 * The bit corresponding to a CPU gets set here if such CPU has more
983 * than one runnable -deadline task (as it is below for RT tasks).
985 cpumask_var_t dlo_mask;
991 * Indicate whether a root_domain's dl_bw has been checked or
992 * updated. It's monotonously increasing value.
994 * Also, some corner cases, like 'wrap around' is dangerous, but given
995 * that u64 is 'big enough'. So that shouldn't be a concern.
999 #ifdef HAVE_RT_PUSH_IPI
1001 * For IPI pull requests, loop across the rto_mask.
1003 struct irq_work rto_push_work;
1004 raw_spinlock_t rto_lock;
1005 /* These are only updated and read within rto_lock */
1008 /* These atomics are updated outside of a lock */
1009 atomic_t rto_loop_next;
1010 atomic_t rto_loop_start;
1013 * The "RT overload" flag: it gets set if a CPU has more than
1014 * one runnable RT task.
1016 cpumask_var_t rto_mask;
1017 struct cpupri cpupri;
1020 * NULL-terminated list of performance domains intersecting with the
1021 * CPUs of the rd. Protected by RCU.
1023 struct perf_domain __rcu *pd;
1026 extern void init_defrootdomain(void);
1027 extern int sched_init_domains(const struct cpumask *cpu_map);
1028 extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
1029 extern void sched_get_rd(struct root_domain *rd);
1030 extern void sched_put_rd(struct root_domain *rd);
1032 static inline int get_rd_overloaded(struct root_domain *rd)
1034 return READ_ONCE(rd->overloaded);
1037 static inline void set_rd_overloaded(struct root_domain *rd, int status)
1039 if (get_rd_overloaded(rd) != status)
1040 WRITE_ONCE(rd->overloaded, status);
1043 #ifdef HAVE_RT_PUSH_IPI
1044 extern void rto_push_irq_work_func(struct irq_work *work);
1046 #endif /* CONFIG_SMP */
1048 #ifdef CONFIG_UCLAMP_TASK
1050 * struct uclamp_bucket - Utilization clamp bucket
1051 * @value: utilization clamp value for tasks on this clamp bucket
1052 * @tasks: number of RUNNABLE tasks on this clamp bucket
1054 * Keep track of how many tasks are RUNNABLE for a given utilization
1057 struct uclamp_bucket {
1058 unsigned long value : bits_per(SCHED_CAPACITY_SCALE);
1059 unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE);
1063 * struct uclamp_rq - rq's utilization clamp
1064 * @value: currently active clamp values for a rq
1065 * @bucket: utilization clamp buckets affecting a rq
1067 * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values.
1068 * A clamp value is affecting a rq when there is at least one task RUNNABLE
1069 * (or actually running) with that value.
1071 * There are up to UCLAMP_CNT possible different clamp values, currently there
1072 * are only two: minimum utilization and maximum utilization.
1074 * All utilization clamping values are MAX aggregated, since:
1075 * - for util_min: we want to run the CPU at least at the max of the minimum
1076 * utilization required by its currently RUNNABLE tasks.
1077 * - for util_max: we want to allow the CPU to run up to the max of the
1078 * maximum utilization allowed by its currently RUNNABLE tasks.
1080 * Since on each system we expect only a limited number of different
1081 * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track
1082 * the metrics required to compute all the per-rq utilization clamp values.
1086 struct uclamp_bucket bucket[UCLAMP_BUCKETS];
1089 DECLARE_STATIC_KEY_FALSE(sched_uclamp_used);
1090 #endif /* CONFIG_UCLAMP_TASK */
1093 * This is the main, per-CPU runqueue data structure.
1095 * Locking rule: those places that want to lock multiple runqueues
1096 * (such as the load balancing or the thread migration code), lock
1097 * acquire operations must be ordered by ascending &runqueue.
1100 /* runqueue lock: */
1101 raw_spinlock_t __lock;
1103 unsigned int nr_running;
1104 #ifdef CONFIG_NUMA_BALANCING
1105 unsigned int nr_numa_running;
1106 unsigned int nr_preferred_running;
1107 unsigned int numa_migrate_on;
1109 #ifdef CONFIG_NO_HZ_COMMON
1111 unsigned long last_blocked_load_update_tick;
1112 unsigned int has_blocked_load;
1113 call_single_data_t nohz_csd;
1114 #endif /* CONFIG_SMP */
1115 unsigned int nohz_tick_stopped;
1116 atomic_t nohz_flags;
1117 #endif /* CONFIG_NO_HZ_COMMON */
1120 unsigned int ttwu_pending;
1124 #ifdef CONFIG_UCLAMP_TASK
1125 /* Utilization clamp values based on CPU's RUNNABLE tasks */
1126 struct uclamp_rq uclamp[UCLAMP_CNT] ____cacheline_aligned;
1127 unsigned int uclamp_flags;
1128 #define UCLAMP_FLAG_IDLE 0x01
1134 #ifdef CONFIG_SCHED_CLASS_EXT
1138 struct sched_dl_entity fair_server;
1140 #ifdef CONFIG_FAIR_GROUP_SCHED
1141 /* list of leaf cfs_rq on this CPU: */
1142 struct list_head leaf_cfs_rq_list;
1143 struct list_head *tmp_alone_branch;
1144 #endif /* CONFIG_FAIR_GROUP_SCHED */
1147 * This is part of a global counter where only the total sum
1148 * over all CPUs matters. A task can increase this counter on
1149 * one CPU and if it got migrated afterwards it may decrease
1150 * it on another CPU. Always updated under the runqueue lock:
1152 unsigned int nr_uninterruptible;
1155 struct task_struct __rcu *donor; /* Scheduler context */
1156 struct task_struct __rcu *curr; /* Execution context */
1158 struct sched_dl_entity *dl_server;
1159 struct task_struct *idle;
1160 struct task_struct *stop;
1161 unsigned long next_balance;
1162 struct mm_struct *prev_mm;
1164 unsigned int clock_update_flags;
1166 /* Ensure that all clocks are in the same cache line */
1167 u64 clock_task ____cacheline_aligned;
1169 unsigned long lost_idle_time;
1170 u64 clock_pelt_idle;
1172 #ifndef CONFIG_64BIT
1173 u64 clock_pelt_idle_copy;
1174 u64 clock_idle_copy;
1179 u64 last_seen_need_resched_ns;
1180 int ticks_without_resched;
1182 #ifdef CONFIG_MEMBARRIER
1183 int membarrier_state;
1187 struct root_domain *rd;
1188 struct sched_domain __rcu *sd;
1190 unsigned long cpu_capacity;
1192 struct balance_callback *balance_callback;
1194 unsigned char nohz_idle_balance;
1195 unsigned char idle_balance;
1197 unsigned long misfit_task_load;
1199 /* For active balancing */
1202 struct cpu_stop_work active_balance_work;
1204 /* CPU of this runqueue: */
1208 struct list_head cfs_tasks;
1210 struct sched_avg avg_rt;
1211 struct sched_avg avg_dl;
1212 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
1213 struct sched_avg avg_irq;
1215 #ifdef CONFIG_SCHED_HW_PRESSURE
1216 struct sched_avg avg_hw;
1221 /* This is used to determine avg_idle's max value */
1222 u64 max_idle_balance_cost;
1224 #ifdef CONFIG_HOTPLUG_CPU
1225 struct rcuwait hotplug_wait;
1227 #endif /* CONFIG_SMP */
1229 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1233 #ifdef CONFIG_PARAVIRT
1234 u64 prev_steal_time;
1236 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
1237 u64 prev_steal_time_rq;
1240 /* calc_load related fields */
1241 unsigned long calc_load_update;
1242 long calc_load_active;
1244 #ifdef CONFIG_SCHED_HRTICK
1246 call_single_data_t hrtick_csd;
1248 struct hrtimer hrtick_timer;
1249 ktime_t hrtick_time;
1252 #ifdef CONFIG_SCHEDSTATS
1254 struct sched_info rq_sched_info;
1255 unsigned long long rq_cpu_time;
1257 /* sys_sched_yield() stats */
1258 unsigned int yld_count;
1260 /* schedule() stats */
1261 unsigned int sched_count;
1262 unsigned int sched_goidle;
1264 /* try_to_wake_up() stats */
1265 unsigned int ttwu_count;
1266 unsigned int ttwu_local;
1269 #ifdef CONFIG_CPU_IDLE
1270 /* Must be inspected within a RCU lock section */
1271 struct cpuidle_state *idle_state;
1275 unsigned int nr_pinned;
1277 unsigned int push_busy;
1278 struct cpu_stop_work push_work;
1280 #ifdef CONFIG_SCHED_CORE
1283 struct task_struct *core_pick;
1284 struct sched_dl_entity *core_dl_server;
1285 unsigned int core_enabled;
1286 unsigned int core_sched_seq;
1287 struct rb_root core_tree;
1289 /* shared state -- careful with sched_core_cpu_deactivate() */
1290 unsigned int core_task_seq;
1291 unsigned int core_pick_seq;
1292 unsigned long core_cookie;
1293 unsigned int core_forceidle_count;
1294 unsigned int core_forceidle_seq;
1295 unsigned int core_forceidle_occupation;
1296 u64 core_forceidle_start;
1299 /* Scratch cpumask to be temporarily used under rq_lock */
1300 cpumask_var_t scratch_mask;
1302 #if defined(CONFIG_CFS_BANDWIDTH) && defined(CONFIG_SMP)
1303 call_single_data_t cfsb_csd;
1304 struct list_head cfsb_csd_list;
1308 #ifdef CONFIG_FAIR_GROUP_SCHED
1310 /* CPU runqueue to which this cfs_rq is attached */
1311 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1318 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1320 return container_of(cfs_rq, struct rq, cfs);
1324 static inline int cpu_of(struct rq *rq)
1333 #define MDF_PUSH 0x01
1335 static inline bool is_migration_disabled(struct task_struct *p)
1338 return p->migration_disabled;
1344 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1346 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
1347 #define this_rq() this_cpu_ptr(&runqueues)
1348 #define task_rq(p) cpu_rq(task_cpu(p))
1349 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
1350 #define raw_rq() raw_cpu_ptr(&runqueues)
1352 static inline void rq_set_donor(struct rq *rq, struct task_struct *t)
1357 #ifdef CONFIG_SCHED_CORE
1358 static inline struct cpumask *sched_group_span(struct sched_group *sg);
1360 DECLARE_STATIC_KEY_FALSE(__sched_core_enabled);
1362 static inline bool sched_core_enabled(struct rq *rq)
1364 return static_branch_unlikely(&__sched_core_enabled) && rq->core_enabled;
1367 static inline bool sched_core_disabled(void)
1369 return !static_branch_unlikely(&__sched_core_enabled);
1373 * Be careful with this function; not for general use. The return value isn't
1374 * stable unless you actually hold a relevant rq->__lock.
1376 static inline raw_spinlock_t *rq_lockp(struct rq *rq)
1378 if (sched_core_enabled(rq))
1379 return &rq->core->__lock;
1384 static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
1386 if (rq->core_enabled)
1387 return &rq->core->__lock;
1393 cfs_prio_less(const struct task_struct *a, const struct task_struct *b, bool fi);
1395 extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi);
1398 * Helpers to check if the CPU's core cookie matches with the task's cookie
1399 * when core scheduling is enabled.
1400 * A special case is that the task's cookie always matches with CPU's core
1401 * cookie if the CPU is in an idle core.
1403 static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
1405 /* Ignore cookie match if core scheduler is not enabled on the CPU. */
1406 if (!sched_core_enabled(rq))
1409 return rq->core->core_cookie == p->core_cookie;
1412 static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
1414 bool idle_core = true;
1417 /* Ignore cookie match if core scheduler is not enabled on the CPU. */
1418 if (!sched_core_enabled(rq))
1421 for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
1422 if (!available_idle_cpu(cpu)) {
1429 * A CPU in an idle core is always the best choice for tasks with
1432 return idle_core || rq->core->core_cookie == p->core_cookie;
1435 static inline bool sched_group_cookie_match(struct rq *rq,
1436 struct task_struct *p,
1437 struct sched_group *group)
1441 /* Ignore cookie match if core scheduler is not enabled on the CPU. */
1442 if (!sched_core_enabled(rq))
1445 for_each_cpu_and(cpu, sched_group_span(group), p->cpus_ptr) {
1446 if (sched_core_cookie_match(cpu_rq(cpu), p))
1452 static inline bool sched_core_enqueued(struct task_struct *p)
1454 return !RB_EMPTY_NODE(&p->core_node);
1457 extern void sched_core_enqueue(struct rq *rq, struct task_struct *p);
1458 extern void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags);
1460 extern void sched_core_get(void);
1461 extern void sched_core_put(void);
1463 #else /* !CONFIG_SCHED_CORE: */
1465 static inline bool sched_core_enabled(struct rq *rq)
1470 static inline bool sched_core_disabled(void)
1475 static inline raw_spinlock_t *rq_lockp(struct rq *rq)
1480 static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
1485 static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
1490 static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
1495 static inline bool sched_group_cookie_match(struct rq *rq,
1496 struct task_struct *p,
1497 struct sched_group *group)
1502 #endif /* !CONFIG_SCHED_CORE */
1503 #ifdef CONFIG_RT_GROUP_SCHED
1504 # ifdef CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED
1505 DECLARE_STATIC_KEY_FALSE(rt_group_sched);
1506 static inline bool rt_group_sched_enabled(void)
1508 return static_branch_unlikely(&rt_group_sched);
1511 DECLARE_STATIC_KEY_TRUE(rt_group_sched);
1512 static inline bool rt_group_sched_enabled(void)
1514 return static_branch_likely(&rt_group_sched);
1516 # endif /* CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED */
1518 # define rt_group_sched_enabled() false
1519 #endif /* CONFIG_RT_GROUP_SCHED */
1521 static inline void lockdep_assert_rq_held(struct rq *rq)
1523 lockdep_assert_held(__rq_lockp(rq));
1526 extern void raw_spin_rq_lock_nested(struct rq *rq, int subclass);
1527 extern bool raw_spin_rq_trylock(struct rq *rq);
1528 extern void raw_spin_rq_unlock(struct rq *rq);
1530 static inline void raw_spin_rq_lock(struct rq *rq)
1532 raw_spin_rq_lock_nested(rq, 0);
1535 static inline void raw_spin_rq_lock_irq(struct rq *rq)
1537 local_irq_disable();
1538 raw_spin_rq_lock(rq);
1541 static inline void raw_spin_rq_unlock_irq(struct rq *rq)
1543 raw_spin_rq_unlock(rq);
1547 static inline unsigned long _raw_spin_rq_lock_irqsave(struct rq *rq)
1549 unsigned long flags;
1551 local_irq_save(flags);
1552 raw_spin_rq_lock(rq);
1557 static inline void raw_spin_rq_unlock_irqrestore(struct rq *rq, unsigned long flags)
1559 raw_spin_rq_unlock(rq);
1560 local_irq_restore(flags);
1563 #define raw_spin_rq_lock_irqsave(rq, flags) \
1565 flags = _raw_spin_rq_lock_irqsave(rq); \
1568 #ifdef CONFIG_SCHED_SMT
1569 extern void __update_idle_core(struct rq *rq);
1571 static inline void update_idle_core(struct rq *rq)
1573 if (static_branch_unlikely(&sched_smt_present))
1574 __update_idle_core(rq);
1578 static inline void update_idle_core(struct rq *rq) { }
1581 #ifdef CONFIG_FAIR_GROUP_SCHED
1583 static inline struct task_struct *task_of(struct sched_entity *se)
1585 WARN_ON_ONCE(!entity_is_task(se));
1586 return container_of(se, struct task_struct, se);
1589 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
1591 return p->se.cfs_rq;
1594 /* runqueue on which this entity is (to be) queued */
1595 static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
1600 /* runqueue "owned" by this group */
1601 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
1606 #else /* !CONFIG_FAIR_GROUP_SCHED: */
1608 #define task_of(_se) container_of(_se, struct task_struct, se)
1610 static inline struct cfs_rq *task_cfs_rq(const struct task_struct *p)
1612 return &task_rq(p)->cfs;
1615 static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
1617 const struct task_struct *p = task_of(se);
1618 struct rq *rq = task_rq(p);
1623 /* runqueue "owned" by this group */
1624 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
1629 #endif /* !CONFIG_FAIR_GROUP_SCHED */
1631 extern void update_rq_clock(struct rq *rq);
1634 * rq::clock_update_flags bits
1636 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
1637 * call to __schedule(). This is an optimisation to avoid
1638 * neighbouring rq clock updates.
1640 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
1641 * in effect and calls to update_rq_clock() are being ignored.
1643 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
1644 * made to update_rq_clock() since the last time rq::lock was pinned.
1646 * If inside of __schedule(), clock_update_flags will have been
1647 * shifted left (a left shift is a cheap operation for the fast path
1648 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
1650 * if (rq-clock_update_flags >= RQCF_UPDATED)
1652 * to check if %RQCF_UPDATED is set. It'll never be shifted more than
1653 * one position though, because the next rq_unpin_lock() will shift it
1656 #define RQCF_REQ_SKIP 0x01
1657 #define RQCF_ACT_SKIP 0x02
1658 #define RQCF_UPDATED 0x04
1660 static inline void assert_clock_updated(struct rq *rq)
1663 * The only reason for not seeing a clock update since the
1664 * last rq_pin_lock() is if we're currently skipping updates.
1666 WARN_ON_ONCE(rq->clock_update_flags < RQCF_ACT_SKIP);
1669 static inline u64 rq_clock(struct rq *rq)
1671 lockdep_assert_rq_held(rq);
1672 assert_clock_updated(rq);
1677 static inline u64 rq_clock_task(struct rq *rq)
1679 lockdep_assert_rq_held(rq);
1680 assert_clock_updated(rq);
1682 return rq->clock_task;
1685 static inline void rq_clock_skip_update(struct rq *rq)
1687 lockdep_assert_rq_held(rq);
1688 rq->clock_update_flags |= RQCF_REQ_SKIP;
1692 * See rt task throttling, which is the only time a skip
1693 * request is canceled.
1695 static inline void rq_clock_cancel_skipupdate(struct rq *rq)
1697 lockdep_assert_rq_held(rq);
1698 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
1702 * During cpu offlining and rq wide unthrottling, we can trigger
1703 * an update_rq_clock() for several cfs and rt runqueues (Typically
1704 * when using list_for_each_entry_*)
1705 * rq_clock_start_loop_update() can be called after updating the clock
1706 * once and before iterating over the list to prevent multiple update.
1707 * After the iterative traversal, we need to call rq_clock_stop_loop_update()
1708 * to clear RQCF_ACT_SKIP of rq->clock_update_flags.
1710 static inline void rq_clock_start_loop_update(struct rq *rq)
1712 lockdep_assert_rq_held(rq);
1713 WARN_ON_ONCE(rq->clock_update_flags & RQCF_ACT_SKIP);
1714 rq->clock_update_flags |= RQCF_ACT_SKIP;
1717 static inline void rq_clock_stop_loop_update(struct rq *rq)
1719 lockdep_assert_rq_held(rq);
1720 rq->clock_update_flags &= ~RQCF_ACT_SKIP;
1724 unsigned long flags;
1725 struct pin_cookie cookie;
1727 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
1728 * current pin context is stashed here in case it needs to be
1729 * restored in rq_repin_lock().
1731 unsigned int clock_update_flags;
1734 extern struct balance_callback balance_push_callback;
1736 #ifdef CONFIG_SCHED_CLASS_EXT
1737 extern const struct sched_class ext_sched_class;
1739 DECLARE_STATIC_KEY_FALSE(__scx_enabled); /* SCX BPF scheduler loaded */
1740 DECLARE_STATIC_KEY_FALSE(__scx_switched_all); /* all fair class tasks on SCX */
1742 #define scx_enabled() static_branch_unlikely(&__scx_enabled)
1743 #define scx_switched_all() static_branch_unlikely(&__scx_switched_all)
1745 static inline void scx_rq_clock_update(struct rq *rq, u64 clock)
1749 WRITE_ONCE(rq->scx.clock, clock);
1750 smp_store_release(&rq->scx.flags, rq->scx.flags | SCX_RQ_CLK_VALID);
1753 static inline void scx_rq_clock_invalidate(struct rq *rq)
1757 WRITE_ONCE(rq->scx.flags, rq->scx.flags & ~SCX_RQ_CLK_VALID);
1760 #else /* !CONFIG_SCHED_CLASS_EXT */
1761 #define scx_enabled() false
1762 #define scx_switched_all() false
1764 static inline void scx_rq_clock_update(struct rq *rq, u64 clock) {}
1765 static inline void scx_rq_clock_invalidate(struct rq *rq) {}
1766 #endif /* !CONFIG_SCHED_CLASS_EXT */
1769 * Lockdep annotation that avoids accidental unlocks; it's like a
1770 * sticky/continuous lockdep_assert_held().
1772 * This avoids code that has access to 'struct rq *rq' (basically everything in
1773 * the scheduler) from accidentally unlocking the rq if they do not also have a
1774 * copy of the (on-stack) 'struct rq_flags rf'.
1776 * Also see Documentation/locking/lockdep-design.rst.
1778 static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
1780 rf->cookie = lockdep_pin_lock(__rq_lockp(rq));
1782 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
1783 rf->clock_update_flags = 0;
1785 WARN_ON_ONCE(rq->balance_callback && rq->balance_callback != &balance_push_callback);
1789 static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
1791 if (rq->clock_update_flags > RQCF_ACT_SKIP)
1792 rf->clock_update_flags = RQCF_UPDATED;
1794 scx_rq_clock_invalidate(rq);
1795 lockdep_unpin_lock(__rq_lockp(rq), rf->cookie);
1798 static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
1800 lockdep_repin_lock(__rq_lockp(rq), rf->cookie);
1803 * Restore the value we stashed in @rf for this pin context.
1805 rq->clock_update_flags |= rf->clock_update_flags;
1809 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1810 __acquires(rq->lock);
1813 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1814 __acquires(p->pi_lock)
1815 __acquires(rq->lock);
1817 static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
1818 __releases(rq->lock)
1820 rq_unpin_lock(rq, rf);
1821 raw_spin_rq_unlock(rq);
1825 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1826 __releases(rq->lock)
1827 __releases(p->pi_lock)
1829 rq_unpin_lock(rq, rf);
1830 raw_spin_rq_unlock(rq);
1831 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
1834 DEFINE_LOCK_GUARD_1(task_rq_lock, struct task_struct,
1835 _T->rq = task_rq_lock(_T->lock, &_T->rf),
1836 task_rq_unlock(_T->rq, _T->lock, &_T->rf),
1837 struct rq *rq; struct rq_flags rf)
1839 static inline void rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1840 __acquires(rq->lock)
1842 raw_spin_rq_lock_irqsave(rq, rf->flags);
1843 rq_pin_lock(rq, rf);
1846 static inline void rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1847 __acquires(rq->lock)
1849 raw_spin_rq_lock_irq(rq);
1850 rq_pin_lock(rq, rf);
1853 static inline void rq_lock(struct rq *rq, struct rq_flags *rf)
1854 __acquires(rq->lock)
1856 raw_spin_rq_lock(rq);
1857 rq_pin_lock(rq, rf);
1860 static inline void rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1861 __releases(rq->lock)
1863 rq_unpin_lock(rq, rf);
1864 raw_spin_rq_unlock_irqrestore(rq, rf->flags);
1867 static inline void rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1868 __releases(rq->lock)
1870 rq_unpin_lock(rq, rf);
1871 raw_spin_rq_unlock_irq(rq);
1874 static inline void rq_unlock(struct rq *rq, struct rq_flags *rf)
1875 __releases(rq->lock)
1877 rq_unpin_lock(rq, rf);
1878 raw_spin_rq_unlock(rq);
1881 DEFINE_LOCK_GUARD_1(rq_lock, struct rq,
1882 rq_lock(_T->lock, &_T->rf),
1883 rq_unlock(_T->lock, &_T->rf),
1886 DEFINE_LOCK_GUARD_1(rq_lock_irq, struct rq,
1887 rq_lock_irq(_T->lock, &_T->rf),
1888 rq_unlock_irq(_T->lock, &_T->rf),
1891 DEFINE_LOCK_GUARD_1(rq_lock_irqsave, struct rq,
1892 rq_lock_irqsave(_T->lock, &_T->rf),
1893 rq_unlock_irqrestore(_T->lock, &_T->rf),
1896 static inline struct rq *this_rq_lock_irq(struct rq_flags *rf)
1897 __acquires(rq->lock)
1901 local_irq_disable();
1910 enum numa_topology_type {
1916 extern enum numa_topology_type sched_numa_topology_type;
1917 extern int sched_max_numa_distance;
1918 extern bool find_numa_distance(int distance);
1919 extern void sched_init_numa(int offline_node);
1920 extern void sched_update_numa(int cpu, bool online);
1921 extern void sched_domains_numa_masks_set(unsigned int cpu);
1922 extern void sched_domains_numa_masks_clear(unsigned int cpu);
1923 extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu);
1925 #else /* !CONFIG_NUMA: */
1927 static inline void sched_init_numa(int offline_node) { }
1928 static inline void sched_update_numa(int cpu, bool online) { }
1929 static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
1930 static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
1932 static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1937 #endif /* !CONFIG_NUMA */
1939 #ifdef CONFIG_NUMA_BALANCING
1941 /* The regions in numa_faults array from task_struct */
1942 enum numa_faults_stats {
1949 extern void sched_setnuma(struct task_struct *p, int node);
1950 extern int migrate_task_to(struct task_struct *p, int cpu);
1951 extern int migrate_swap(struct task_struct *p, struct task_struct *t,
1953 extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p);
1955 #else /* !CONFIG_NUMA_BALANCING: */
1958 init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
1962 #endif /* !CONFIG_NUMA_BALANCING */
1967 queue_balance_callback(struct rq *rq,
1968 struct balance_callback *head,
1969 void (*func)(struct rq *rq))
1971 lockdep_assert_rq_held(rq);
1974 * Don't (re)queue an already queued item; nor queue anything when
1975 * balance_push() is active, see the comment with
1976 * balance_push_callback.
1978 if (unlikely(head->next || rq->balance_callback == &balance_push_callback))
1982 head->next = rq->balance_callback;
1983 rq->balance_callback = head;
1986 #define rcu_dereference_check_sched_domain(p) \
1987 rcu_dereference_check((p), lockdep_is_held(&sched_domains_mutex))
1990 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1991 * See destroy_sched_domains: call_rcu for details.
1993 * The domain tree of any CPU may only be accessed from within
1994 * preempt-disabled sections.
1996 #define for_each_domain(cpu, __sd) \
1997 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1998 __sd; __sd = __sd->parent)
2000 /* A mask of all the SD flags that have the SDF_SHARED_CHILD metaflag */
2001 #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_SHARED_CHILD)) |
2002 static const unsigned int SD_SHARED_CHILD_MASK =
2003 #include <linux/sched/sd_flags.h>
2008 * highest_flag_domain - Return highest sched_domain containing flag.
2009 * @cpu: The CPU whose highest level of sched domain is to
2011 * @flag: The flag to check for the highest sched_domain
2012 * for the given CPU.
2014 * Returns the highest sched_domain of a CPU which contains @flag. If @flag has
2015 * the SDF_SHARED_CHILD metaflag, all the children domains also have @flag.
2017 static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
2019 struct sched_domain *sd, *hsd = NULL;
2021 for_each_domain(cpu, sd) {
2022 if (sd->flags & flag) {
2028 * Stop the search if @flag is known to be shared at lower
2029 * levels. It will not be found further up.
2031 if (flag & SD_SHARED_CHILD_MASK)
2038 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
2040 struct sched_domain *sd;
2042 for_each_domain(cpu, sd) {
2043 if (sd->flags & flag)
2050 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
2051 DECLARE_PER_CPU(int, sd_llc_size);
2052 DECLARE_PER_CPU(int, sd_llc_id);
2053 DECLARE_PER_CPU(int, sd_share_id);
2054 DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
2055 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
2056 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
2057 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
2059 extern struct static_key_false sched_asym_cpucapacity;
2060 extern struct static_key_false sched_cluster_active;
2062 static __always_inline bool sched_asym_cpucap_active(void)
2064 return static_branch_unlikely(&sched_asym_cpucapacity);
2067 struct sched_group_capacity {
2070 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
2073 unsigned long capacity;
2074 unsigned long min_capacity; /* Min per-CPU capacity in group */
2075 unsigned long max_capacity; /* Max per-CPU capacity in group */
2076 unsigned long next_update;
2077 int imbalance; /* XXX unrelated to capacity but shared group state */
2081 unsigned long cpumask[]; /* Balance mask */
2084 struct sched_group {
2085 struct sched_group *next; /* Must be a circular list */
2088 unsigned int group_weight;
2090 struct sched_group_capacity *sgc;
2091 int asym_prefer_cpu; /* CPU of highest priority in group */
2095 * The CPUs this group covers.
2097 * NOTE: this field is variable length. (Allocated dynamically
2098 * by attaching extra space to the end of the structure,
2099 * depending on how many CPUs the kernel has booted up with)
2101 unsigned long cpumask[];
2104 static inline struct cpumask *sched_group_span(struct sched_group *sg)
2106 return to_cpumask(sg->cpumask);
2110 * See build_balance_mask().
2112 static inline struct cpumask *group_balance_mask(struct sched_group *sg)
2114 return to_cpumask(sg->sgc->cpumask);
2117 extern int group_balance_cpu(struct sched_group *sg);
2119 extern void update_sched_domain_debugfs(void);
2120 extern void dirty_sched_domain_sysctl(int cpu);
2122 extern int sched_update_scaling(void);
2124 static inline const struct cpumask *task_user_cpus(struct task_struct *p)
2126 if (!p->user_cpus_ptr)
2127 return cpu_possible_mask; /* &init_task.cpus_mask */
2128 return p->user_cpus_ptr;
2131 #endif /* CONFIG_SMP */
2133 #ifdef CONFIG_CGROUP_SCHED
2136 * Return the group to which this tasks belongs.
2138 * We cannot use task_css() and friends because the cgroup subsystem
2139 * changes that value before the cgroup_subsys::attach() method is called,
2140 * therefore we cannot pin it and might observe the wrong value.
2142 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
2143 * core changes this before calling sched_move_task().
2145 * Instead we use a 'copy' which is updated from sched_move_task() while
2146 * holding both task_struct::pi_lock and rq::lock.
2148 static inline struct task_group *task_group(struct task_struct *p)
2150 return p->sched_task_group;
2153 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
2154 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
2156 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
2157 struct task_group *tg = task_group(p);
2160 #ifdef CONFIG_FAIR_GROUP_SCHED
2161 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
2162 p->se.cfs_rq = tg->cfs_rq[cpu];
2163 p->se.parent = tg->se[cpu];
2164 p->se.depth = tg->se[cpu] ? tg->se[cpu]->depth + 1 : 0;
2167 #ifdef CONFIG_RT_GROUP_SCHED
2169 * p->rt.rt_rq is NULL initially and it is easier to assign
2170 * root_task_group's rt_rq than switching in rt_rq_of_se()
2173 if (!rt_group_sched_enabled())
2174 tg = &root_task_group;
2175 p->rt.rt_rq = tg->rt_rq[cpu];
2176 p->rt.parent = tg->rt_se[cpu];
2180 #else /* !CONFIG_CGROUP_SCHED: */
2182 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
2184 static inline struct task_group *task_group(struct task_struct *p)
2189 #endif /* !CONFIG_CGROUP_SCHED */
2191 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2193 set_task_rq(p, cpu);
2196 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2197 * successfully executed on another CPU. We must ensure that updates of
2198 * per-task data have been completed by this moment.
2201 WRITE_ONCE(task_thread_info(p)->cpu, cpu);
2210 #define SCHED_FEAT(name, enabled) \
2211 __SCHED_FEAT_##name ,
2214 #include "features.h"
2221 * To support run-time toggling of sched features, all the translation units
2222 * (but core.c) reference the sysctl_sched_features defined in core.c.
2224 extern __read_mostly unsigned int sysctl_sched_features;
2226 #ifdef CONFIG_JUMP_LABEL
2228 #define SCHED_FEAT(name, enabled) \
2229 static __always_inline bool static_branch_##name(struct static_key *key) \
2231 return static_key_##enabled(key); \
2234 #include "features.h"
2237 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
2238 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
2240 #else /* !CONFIG_JUMP_LABEL: */
2242 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
2244 #endif /* !CONFIG_JUMP_LABEL */
2246 extern struct static_key_false sched_numa_balancing;
2247 extern struct static_key_false sched_schedstats;
2249 static inline u64 global_rt_period(void)
2251 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
2254 static inline u64 global_rt_runtime(void)
2256 if (sysctl_sched_rt_runtime < 0)
2259 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
2263 * Is p the current execution context?
2265 static inline int task_current(struct rq *rq, struct task_struct *p)
2267 return rq->curr == p;
2271 * Is p the current scheduling context?
2273 * Note that it might be the current execution context at the same time if
2274 * rq->curr == rq->donor == p.
2276 static inline int task_current_donor(struct rq *rq, struct task_struct *p)
2278 return rq->donor == p;
2281 static inline int task_on_cpu(struct rq *rq, struct task_struct *p)
2286 return task_current(rq, p);
2290 static inline int task_on_rq_queued(struct task_struct *p)
2292 return READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED;
2295 static inline int task_on_rq_migrating(struct task_struct *p)
2297 return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
2300 /* Wake flags. The first three directly map to some SD flag value */
2301 #define WF_EXEC 0x02 /* Wakeup after exec; maps to SD_BALANCE_EXEC */
2302 #define WF_FORK 0x04 /* Wakeup after fork; maps to SD_BALANCE_FORK */
2303 #define WF_TTWU 0x08 /* Wakeup; maps to SD_BALANCE_WAKE */
2305 #define WF_SYNC 0x10 /* Waker goes to sleep after wakeup */
2306 #define WF_MIGRATED 0x20 /* Internal use, task got migrated */
2307 #define WF_CURRENT_CPU 0x40 /* Prefer to move the wakee to the current CPU. */
2308 #define WF_RQ_SELECTED 0x80 /* ->select_task_rq() was called */
2311 static_assert(WF_EXEC == SD_BALANCE_EXEC);
2312 static_assert(WF_FORK == SD_BALANCE_FORK);
2313 static_assert(WF_TTWU == SD_BALANCE_WAKE);
2317 * To aid in avoiding the subversion of "niceness" due to uneven distribution
2318 * of tasks with abnormal "nice" values across CPUs the contribution that
2319 * each task makes to its run queue's load is weighted according to its
2320 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
2321 * scaled version of the new time slice allocation that they receive on time
2325 #define WEIGHT_IDLEPRIO 3
2326 #define WMULT_IDLEPRIO 1431655765
2328 extern const int sched_prio_to_weight[40];
2329 extern const u32 sched_prio_to_wmult[40];
2332 * {de,en}queue flags:
2334 * DEQUEUE_SLEEP - task is no longer runnable
2335 * ENQUEUE_WAKEUP - task just became runnable
2337 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
2338 * are in a known state which allows modification. Such pairs
2339 * should preserve as much state as possible.
2341 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
2344 * NOCLOCK - skip the update_rq_clock() (avoids double updates)
2346 * MIGRATION - p->on_rq == TASK_ON_RQ_MIGRATING (used for DEADLINE)
2348 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
2349 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
2350 * ENQUEUE_MIGRATED - the task was migrated during wakeup
2351 * ENQUEUE_RQ_SELECTED - ->select_task_rq() was called
2355 #define DEQUEUE_SLEEP 0x01 /* Matches ENQUEUE_WAKEUP */
2356 #define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */
2357 #define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */
2358 #define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */
2359 #define DEQUEUE_SPECIAL 0x10
2360 #define DEQUEUE_MIGRATING 0x100 /* Matches ENQUEUE_MIGRATING */
2361 #define DEQUEUE_DELAYED 0x200 /* Matches ENQUEUE_DELAYED */
2363 #define ENQUEUE_WAKEUP 0x01
2364 #define ENQUEUE_RESTORE 0x02
2365 #define ENQUEUE_MOVE 0x04
2366 #define ENQUEUE_NOCLOCK 0x08
2368 #define ENQUEUE_HEAD 0x10
2369 #define ENQUEUE_REPLENISH 0x20
2371 #define ENQUEUE_MIGRATED 0x40
2373 #define ENQUEUE_MIGRATED 0x00
2375 #define ENQUEUE_INITIAL 0x80
2376 #define ENQUEUE_MIGRATING 0x100
2377 #define ENQUEUE_DELAYED 0x200
2378 #define ENQUEUE_RQ_SELECTED 0x400
2380 #define RETRY_TASK ((void *)-1UL)
2382 struct affinity_context {
2383 const struct cpumask *new_mask;
2384 struct cpumask *user_mask;
2388 extern s64 update_curr_common(struct rq *rq);
2390 struct sched_class {
2392 #ifdef CONFIG_UCLAMP_TASK
2396 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
2397 bool (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
2398 void (*yield_task) (struct rq *rq);
2399 bool (*yield_to_task)(struct rq *rq, struct task_struct *p);
2401 void (*wakeup_preempt)(struct rq *rq, struct task_struct *p, int flags);
2403 int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
2404 struct task_struct *(*pick_task)(struct rq *rq);
2406 * Optional! When implemented pick_next_task() should be equivalent to:
2408 * next = pick_task();
2410 * put_prev_task(prev);
2411 * set_next_task_first(next);
2414 struct task_struct *(*pick_next_task)(struct rq *rq, struct task_struct *prev);
2416 void (*put_prev_task)(struct rq *rq, struct task_struct *p, struct task_struct *next);
2417 void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first);
2420 int (*select_task_rq)(struct task_struct *p, int task_cpu, int flags);
2422 void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
2424 void (*task_woken)(struct rq *this_rq, struct task_struct *task);
2426 void (*set_cpus_allowed)(struct task_struct *p, struct affinity_context *ctx);
2428 void (*rq_online)(struct rq *rq);
2429 void (*rq_offline)(struct rq *rq);
2431 struct rq *(*find_lock_rq)(struct task_struct *p, struct rq *rq);
2434 void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
2435 void (*task_fork)(struct task_struct *p);
2436 void (*task_dead)(struct task_struct *p);
2439 * The switched_from() call is allowed to drop rq->lock, therefore we
2440 * cannot assume the switched_from/switched_to pair is serialized by
2441 * rq->lock. They are however serialized by p->pi_lock.
2443 void (*switching_to) (struct rq *this_rq, struct task_struct *task);
2444 void (*switched_from)(struct rq *this_rq, struct task_struct *task);
2445 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
2446 void (*reweight_task)(struct rq *this_rq, struct task_struct *task,
2447 const struct load_weight *lw);
2448 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
2451 unsigned int (*get_rr_interval)(struct rq *rq,
2452 struct task_struct *task);
2454 void (*update_curr)(struct rq *rq);
2456 #ifdef CONFIG_FAIR_GROUP_SCHED
2457 void (*task_change_group)(struct task_struct *p);
2460 #ifdef CONFIG_SCHED_CORE
2461 int (*task_is_throttled)(struct task_struct *p, int cpu);
2465 static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
2467 WARN_ON_ONCE(rq->donor != prev);
2468 prev->sched_class->put_prev_task(rq, prev, NULL);
2471 static inline void set_next_task(struct rq *rq, struct task_struct *next)
2473 next->sched_class->set_next_task(rq, next, false);
2477 __put_prev_set_next_dl_server(struct rq *rq,
2478 struct task_struct *prev,
2479 struct task_struct *next)
2481 prev->dl_server = NULL;
2482 next->dl_server = rq->dl_server;
2483 rq->dl_server = NULL;
2486 static inline void put_prev_set_next_task(struct rq *rq,
2487 struct task_struct *prev,
2488 struct task_struct *next)
2490 WARN_ON_ONCE(rq->curr != prev);
2492 __put_prev_set_next_dl_server(rq, prev, next);
2497 prev->sched_class->put_prev_task(rq, prev, next);
2498 next->sched_class->set_next_task(rq, next, true);
2502 * Helper to define a sched_class instance; each one is placed in a separate
2503 * section which is ordered by the linker script:
2505 * include/asm-generic/vmlinux.lds.h
2507 * *CAREFUL* they are laid out in *REVERSE* order!!!
2509 * Also enforce alignment on the instance, not the type, to guarantee layout.
2511 #define DEFINE_SCHED_CLASS(name) \
2512 const struct sched_class name##_sched_class \
2513 __aligned(__alignof__(struct sched_class)) \
2514 __section("__" #name "_sched_class")
2516 /* Defined in include/asm-generic/vmlinux.lds.h */
2517 extern struct sched_class __sched_class_highest[];
2518 extern struct sched_class __sched_class_lowest[];
2520 extern const struct sched_class stop_sched_class;
2521 extern const struct sched_class dl_sched_class;
2522 extern const struct sched_class rt_sched_class;
2523 extern const struct sched_class fair_sched_class;
2524 extern const struct sched_class idle_sched_class;
2527 * Iterate only active classes. SCX can take over all fair tasks or be
2528 * completely disabled. If the former, skip fair. If the latter, skip SCX.
2530 static inline const struct sched_class *next_active_class(const struct sched_class *class)
2533 #ifdef CONFIG_SCHED_CLASS_EXT
2534 if (scx_switched_all() && class == &fair_sched_class)
2536 if (!scx_enabled() && class == &ext_sched_class)
2542 #define for_class_range(class, _from, _to) \
2543 for (class = (_from); class < (_to); class++)
2545 #define for_each_class(class) \
2546 for_class_range(class, __sched_class_highest, __sched_class_lowest)
2548 #define for_active_class_range(class, _from, _to) \
2549 for (class = (_from); class != (_to); class = next_active_class(class))
2551 #define for_each_active_class(class) \
2552 for_active_class_range(class, __sched_class_highest, __sched_class_lowest)
2554 #define sched_class_above(_a, _b) ((_a) < (_b))
2556 static inline bool sched_stop_runnable(struct rq *rq)
2558 return rq->stop && task_on_rq_queued(rq->stop);
2561 static inline bool sched_dl_runnable(struct rq *rq)
2563 return rq->dl.dl_nr_running > 0;
2566 static inline bool sched_rt_runnable(struct rq *rq)
2568 return rq->rt.rt_queued > 0;
2571 static inline bool sched_fair_runnable(struct rq *rq)
2573 return rq->cfs.nr_queued > 0;
2576 extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
2577 extern struct task_struct *pick_task_idle(struct rq *rq);
2579 #define SCA_CHECK 0x01
2580 #define SCA_MIGRATE_DISABLE 0x02
2581 #define SCA_MIGRATE_ENABLE 0x04
2582 #define SCA_USER 0x08
2586 extern void update_group_capacity(struct sched_domain *sd, int cpu);
2588 extern void sched_balance_trigger(struct rq *rq);
2590 extern int __set_cpus_allowed_ptr(struct task_struct *p, struct affinity_context *ctx);
2591 extern void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx);
2593 static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
2595 /* When not in the task's cpumask, no point in looking further. */
2596 if (!cpumask_test_cpu(cpu, p->cpus_ptr))
2599 /* Can @cpu run a user thread? */
2600 if (!(p->flags & PF_KTHREAD) && !task_cpu_possible(cpu, p))
2606 static inline cpumask_t *alloc_user_cpus_ptr(int node)
2609 * See do_set_cpus_allowed() above for the rcu_head usage.
2611 int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));
2613 return kmalloc_node(size, GFP_KERNEL, node);
2616 static inline struct task_struct *get_push_task(struct rq *rq)
2618 struct task_struct *p = rq->donor;
2620 lockdep_assert_rq_held(rq);
2625 if (p->nr_cpus_allowed == 1)
2628 if (p->migration_disabled)
2631 rq->push_busy = true;
2632 return get_task_struct(p);
2635 extern int push_cpu_stop(void *arg);
2637 #else /* !CONFIG_SMP: */
2639 static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
2644 static inline int __set_cpus_allowed_ptr(struct task_struct *p,
2645 struct affinity_context *ctx)
2647 return set_cpus_allowed_ptr(p, ctx->new_mask);
2650 static inline cpumask_t *alloc_user_cpus_ptr(int node)
2655 #endif /* !CONFIG_SMP */
2657 #ifdef CONFIG_CPU_IDLE
2659 static inline void idle_set_state(struct rq *rq,
2660 struct cpuidle_state *idle_state)
2662 rq->idle_state = idle_state;
2665 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
2667 WARN_ON_ONCE(!rcu_read_lock_held());
2669 return rq->idle_state;
2672 #else /* !CONFIG_CPU_IDLE: */
2674 static inline void idle_set_state(struct rq *rq,
2675 struct cpuidle_state *idle_state)
2679 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
2684 #endif /* !CONFIG_CPU_IDLE */
2686 extern void schedule_idle(void);
2687 asmlinkage void schedule_user(void);
2689 extern void sysrq_sched_debug_show(void);
2690 extern void sched_init_granularity(void);
2691 extern void update_max_interval(void);
2693 extern void init_sched_dl_class(void);
2694 extern void init_sched_rt_class(void);
2695 extern void init_sched_fair_class(void);
2697 extern void resched_curr(struct rq *rq);
2698 extern void resched_curr_lazy(struct rq *rq);
2699 extern void resched_cpu(int cpu);
2701 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
2702 extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
2704 extern void init_dl_entity(struct sched_dl_entity *dl_se);
2707 #define BW_UNIT (1 << BW_SHIFT)
2708 #define RATIO_SHIFT 8
2709 #define MAX_BW_BITS (64 - BW_SHIFT)
2710 #define MAX_BW ((1ULL << MAX_BW_BITS) - 1)
2712 extern unsigned long to_ratio(u64 period, u64 runtime);
2714 extern void init_entity_runnable_average(struct sched_entity *se);
2715 extern void post_init_entity_util_avg(struct task_struct *p);
2717 #ifdef CONFIG_NO_HZ_FULL
2718 extern bool sched_can_stop_tick(struct rq *rq);
2719 extern int __init sched_tick_offload_init(void);
2722 * Tick may be needed by tasks in the runqueue depending on their policy and
2723 * requirements. If tick is needed, lets send the target an IPI to kick it out of
2724 * nohz mode if necessary.
2726 static inline void sched_update_tick_dependency(struct rq *rq)
2728 int cpu = cpu_of(rq);
2730 if (!tick_nohz_full_cpu(cpu))
2733 if (sched_can_stop_tick(rq))
2734 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
2736 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
2738 #else /* !CONFIG_NO_HZ_FULL: */
2739 static inline int sched_tick_offload_init(void) { return 0; }
2740 static inline void sched_update_tick_dependency(struct rq *rq) { }
2741 #endif /* !CONFIG_NO_HZ_FULL */
2743 static inline void add_nr_running(struct rq *rq, unsigned count)
2745 unsigned prev_nr = rq->nr_running;
2747 rq->nr_running = prev_nr + count;
2748 if (trace_sched_update_nr_running_tp_enabled()) {
2749 call_trace_sched_update_nr_running(rq, count);
2753 if (prev_nr < 2 && rq->nr_running >= 2)
2754 set_rd_overloaded(rq->rd, 1);
2757 sched_update_tick_dependency(rq);
2760 static inline void sub_nr_running(struct rq *rq, unsigned count)
2762 rq->nr_running -= count;
2763 if (trace_sched_update_nr_running_tp_enabled()) {
2764 call_trace_sched_update_nr_running(rq, -count);
2767 /* Check if we still need preemption */
2768 sched_update_tick_dependency(rq);
2771 static inline void __block_task(struct rq *rq, struct task_struct *p)
2773 if (p->sched_contributes_to_load)
2774 rq->nr_uninterruptible++;
2777 atomic_inc(&rq->nr_iowait);
2778 delayacct_blkio_start();
2781 ASSERT_EXCLUSIVE_WRITER(p->on_rq);
2784 * The moment this write goes through, ttwu() can swoop in and migrate
2785 * this task, rendering our rq->__lock ineffective.
2787 * __schedule() try_to_wake_up()
2788 * LOCK rq->__lock LOCK p->pi_lock
2790 * pick_next_task_fair()
2791 * pick_next_entity()
2792 * dequeue_entities()
2794 * RELEASE p->on_rq = 0 if (p->on_rq && ...)
2797 * ACQUIRE (after ctrl-dep)
2799 * cpu = select_task_rq();
2800 * set_task_cpu(p, cpu);
2802 * ttwu_do_activate()
2805 * STORE p->on_rq = 1
2808 * Callers must ensure to not reference @p after this -- we no longer
2811 smp_store_release(&p->on_rq, 0);
2814 extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
2815 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
2817 extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags);
2819 #ifdef CONFIG_PREEMPT_RT
2820 # define SCHED_NR_MIGRATE_BREAK 8
2822 # define SCHED_NR_MIGRATE_BREAK 32
2825 extern __read_mostly unsigned int sysctl_sched_nr_migrate;
2826 extern __read_mostly unsigned int sysctl_sched_migration_cost;
2828 extern unsigned int sysctl_sched_base_slice;
2830 extern int sysctl_resched_latency_warn_ms;
2831 extern int sysctl_resched_latency_warn_once;
2833 extern unsigned int sysctl_sched_tunable_scaling;
2835 extern unsigned int sysctl_numa_balancing_scan_delay;
2836 extern unsigned int sysctl_numa_balancing_scan_period_min;
2837 extern unsigned int sysctl_numa_balancing_scan_period_max;
2838 extern unsigned int sysctl_numa_balancing_scan_size;
2839 extern unsigned int sysctl_numa_balancing_hot_threshold;
2841 #ifdef CONFIG_SCHED_HRTICK
2845 * - enabled by features
2846 * - hrtimer is actually high res
2848 static inline int hrtick_enabled(struct rq *rq)
2850 if (!cpu_active(cpu_of(rq)))
2852 return hrtimer_is_hres_active(&rq->hrtick_timer);
2855 static inline int hrtick_enabled_fair(struct rq *rq)
2857 if (!sched_feat(HRTICK))
2859 return hrtick_enabled(rq);
2862 static inline int hrtick_enabled_dl(struct rq *rq)
2864 if (!sched_feat(HRTICK_DL))
2866 return hrtick_enabled(rq);
2869 extern void hrtick_start(struct rq *rq, u64 delay);
2871 #else /* !CONFIG_SCHED_HRTICK: */
2873 static inline int hrtick_enabled_fair(struct rq *rq)
2878 static inline int hrtick_enabled_dl(struct rq *rq)
2883 static inline int hrtick_enabled(struct rq *rq)
2888 #endif /* !CONFIG_SCHED_HRTICK */
2890 #ifndef arch_scale_freq_tick
2891 static __always_inline void arch_scale_freq_tick(void) { }
2894 #ifndef arch_scale_freq_capacity
2896 * arch_scale_freq_capacity - get the frequency scale factor of a given CPU.
2897 * @cpu: the CPU in question.
2899 * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
2902 * ------ * SCHED_CAPACITY_SCALE
2905 static __always_inline
2906 unsigned long arch_scale_freq_capacity(int cpu)
2908 return SCHED_CAPACITY_SCALE;
2913 * In double_lock_balance()/double_rq_lock(), we use raw_spin_rq_lock() to
2914 * acquire rq lock instead of rq_lock(). So at the end of these two functions
2915 * we need to call double_rq_clock_clear_update() to clear RQCF_UPDATED of
2916 * rq->clock_update_flags to avoid the WARN_DOUBLE_CLOCK warning.
2918 static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2)
2920 rq1->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
2921 /* rq1 == rq2 for !CONFIG_SMP, so just clear RQCF_UPDATED once. */
2923 rq2->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
2927 #define DEFINE_LOCK_GUARD_2(name, type, _lock, _unlock, ...) \
2928 __DEFINE_UNLOCK_GUARD(name, type, _unlock, type *lock2; __VA_ARGS__) \
2929 static inline class_##name##_t class_##name##_constructor(type *lock, type *lock2) \
2930 { class_##name##_t _t = { .lock = lock, .lock2 = lock2 }, *_T = &_t; \
2935 static inline bool rq_order_less(struct rq *rq1, struct rq *rq2)
2937 #ifdef CONFIG_SCHED_CORE
2939 * In order to not have {0,2},{1,3} turn into into an AB-BA,
2940 * order by core-id first and cpu-id second.
2944 * double_rq_lock(0,3); will take core-0, core-1 lock
2945 * double_rq_lock(1,2); will take core-1, core-0 lock
2947 * when only cpu-id is considered.
2949 if (rq1->core->cpu < rq2->core->cpu)
2951 if (rq1->core->cpu > rq2->core->cpu)
2955 * __sched_core_flip() relies on SMT having cpu-id lock order.
2958 return rq1->cpu < rq2->cpu;
2961 extern void double_rq_lock(struct rq *rq1, struct rq *rq2);
2963 #ifdef CONFIG_PREEMPTION
2966 * fair double_lock_balance: Safely acquires both rq->locks in a fair
2967 * way at the expense of forcing extra atomic operations in all
2968 * invocations. This assures that the double_lock is acquired using the
2969 * same underlying policy as the spinlock_t on this architecture, which
2970 * reduces latency compared to the unfair variant below. However, it
2971 * also adds more overhead and therefore may reduce throughput.
2973 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2974 __releases(this_rq->lock)
2975 __acquires(busiest->lock)
2976 __acquires(this_rq->lock)
2978 raw_spin_rq_unlock(this_rq);
2979 double_rq_lock(this_rq, busiest);
2984 #else /* !CONFIG_PREEMPTION: */
2986 * Unfair double_lock_balance: Optimizes throughput at the expense of
2987 * latency by eliminating extra atomic operations when the locks are
2988 * already in proper order on entry. This favors lower CPU-ids and will
2989 * grant the double lock to lower CPUs over higher ids under contention,
2990 * regardless of entry order into the function.
2992 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2993 __releases(this_rq->lock)
2994 __acquires(busiest->lock)
2995 __acquires(this_rq->lock)
2997 if (__rq_lockp(this_rq) == __rq_lockp(busiest) ||
2998 likely(raw_spin_rq_trylock(busiest))) {
2999 double_rq_clock_clear_update(this_rq, busiest);
3003 if (rq_order_less(this_rq, busiest)) {
3004 raw_spin_rq_lock_nested(busiest, SINGLE_DEPTH_NESTING);
3005 double_rq_clock_clear_update(this_rq, busiest);
3009 raw_spin_rq_unlock(this_rq);
3010 double_rq_lock(this_rq, busiest);
3015 #endif /* !CONFIG_PREEMPTION */
3018 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3020 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
3022 lockdep_assert_irqs_disabled();
3024 return _double_lock_balance(this_rq, busiest);
3027 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
3028 __releases(busiest->lock)
3030 if (__rq_lockp(this_rq) != __rq_lockp(busiest))
3031 raw_spin_rq_unlock(busiest);
3032 lock_set_subclass(&__rq_lockp(this_rq)->dep_map, 0, _RET_IP_);
3035 static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
3041 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3044 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
3050 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3053 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
3059 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3062 static inline void double_raw_unlock(raw_spinlock_t *l1, raw_spinlock_t *l2)
3064 raw_spin_unlock(l1);
3065 raw_spin_unlock(l2);
3068 DEFINE_LOCK_GUARD_2(double_raw_spinlock, raw_spinlock_t,
3069 double_raw_lock(_T->lock, _T->lock2),
3070 double_raw_unlock(_T->lock, _T->lock2))
3073 * double_rq_unlock - safely unlock two runqueues
3075 * Note this does not restore interrupts like task_rq_unlock,
3076 * you need to do so manually after calling.
3078 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
3079 __releases(rq1->lock)
3080 __releases(rq2->lock)
3082 if (__rq_lockp(rq1) != __rq_lockp(rq2))
3083 raw_spin_rq_unlock(rq2);
3085 __release(rq2->lock);
3086 raw_spin_rq_unlock(rq1);
3089 extern void set_rq_online (struct rq *rq);
3090 extern void set_rq_offline(struct rq *rq);
3092 extern bool sched_smp_initialized;
3094 #else /* !CONFIG_SMP: */
3097 * double_rq_lock - safely lock two runqueues
3099 * Note this does not disable interrupts like task_rq_lock,
3100 * you need to do so manually before calling.
3102 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
3103 __acquires(rq1->lock)
3104 __acquires(rq2->lock)
3106 WARN_ON_ONCE(!irqs_disabled());
3107 WARN_ON_ONCE(rq1 != rq2);
3108 raw_spin_rq_lock(rq1);
3109 __acquire(rq2->lock); /* Fake it out ;) */
3110 double_rq_clock_clear_update(rq1, rq2);
3114 * double_rq_unlock - safely unlock two runqueues
3116 * Note this does not restore interrupts like task_rq_unlock,
3117 * you need to do so manually after calling.
3119 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
3120 __releases(rq1->lock)
3121 __releases(rq2->lock)
3123 WARN_ON_ONCE(rq1 != rq2);
3124 raw_spin_rq_unlock(rq1);
3125 __release(rq2->lock);
3128 #endif /* !CONFIG_SMP */
3130 DEFINE_LOCK_GUARD_2(double_rq_lock, struct rq,
3131 double_rq_lock(_T->lock, _T->lock2),
3132 double_rq_unlock(_T->lock, _T->lock2))
3134 extern struct sched_entity *__pick_root_entity(struct cfs_rq *cfs_rq);
3135 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
3136 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
3138 extern bool sched_debug_verbose;
3140 extern void print_cfs_stats(struct seq_file *m, int cpu);
3141 extern void print_rt_stats(struct seq_file *m, int cpu);
3142 extern void print_dl_stats(struct seq_file *m, int cpu);
3143 extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
3144 extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
3145 extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
3147 extern void resched_latency_warn(int cpu, u64 latency);
3148 #ifdef CONFIG_NUMA_BALANCING
3149 extern void show_numa_stats(struct task_struct *p, struct seq_file *m);
3151 print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
3152 unsigned long tpf, unsigned long gsf, unsigned long gpf);
3153 #endif /* CONFIG_NUMA_BALANCING */
3155 extern void init_cfs_rq(struct cfs_rq *cfs_rq);
3156 extern void init_rt_rq(struct rt_rq *rt_rq);
3157 extern void init_dl_rq(struct dl_rq *dl_rq);
3159 extern void cfs_bandwidth_usage_inc(void);
3160 extern void cfs_bandwidth_usage_dec(void);
3162 #ifdef CONFIG_NO_HZ_COMMON
3164 #define NOHZ_BALANCE_KICK_BIT 0
3165 #define NOHZ_STATS_KICK_BIT 1
3166 #define NOHZ_NEWILB_KICK_BIT 2
3167 #define NOHZ_NEXT_KICK_BIT 3
3169 /* Run sched_balance_domains() */
3170 #define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT)
3171 /* Update blocked load */
3172 #define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT)
3173 /* Update blocked load when entering idle */
3174 #define NOHZ_NEWILB_KICK BIT(NOHZ_NEWILB_KICK_BIT)
3175 /* Update nohz.next_balance */
3176 #define NOHZ_NEXT_KICK BIT(NOHZ_NEXT_KICK_BIT)
3178 #define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK | NOHZ_NEXT_KICK)
3180 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
3182 extern void nohz_balance_exit_idle(struct rq *rq);
3183 #else /* !CONFIG_NO_HZ_COMMON: */
3184 static inline void nohz_balance_exit_idle(struct rq *rq) { }
3185 #endif /* !CONFIG_NO_HZ_COMMON */
3187 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
3188 extern void nohz_run_idle_balance(int cpu);
3190 static inline void nohz_run_idle_balance(int cpu) { }
3195 #if defined(CONFIG_SCHED_CORE) && defined(CONFIG_SCHEDSTATS)
3197 extern void __sched_core_account_forceidle(struct rq *rq);
3199 static inline void sched_core_account_forceidle(struct rq *rq)
3201 if (schedstat_enabled())
3202 __sched_core_account_forceidle(rq);
3205 extern void __sched_core_tick(struct rq *rq);
3207 static inline void sched_core_tick(struct rq *rq)
3209 if (sched_core_enabled(rq) && schedstat_enabled())
3210 __sched_core_tick(rq);
3213 #else /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS): */
3215 static inline void sched_core_account_forceidle(struct rq *rq) { }
3217 static inline void sched_core_tick(struct rq *rq) { }
3219 #endif /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS) */
3221 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
3227 struct u64_stats_sync sync;
3230 DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
3231 extern int sched_clock_irqtime;
3233 static inline int irqtime_enabled(void)
3235 return sched_clock_irqtime;
3239 * Returns the irqtime minus the softirq time computed by ksoftirqd.
3240 * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime
3241 * and never move forward.
3243 static inline u64 irq_time_read(int cpu)
3245 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
3250 seq = __u64_stats_fetch_begin(&irqtime->sync);
3251 total = irqtime->total;
3252 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
3259 static inline int irqtime_enabled(void)
3264 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
3266 #ifdef CONFIG_CPU_FREQ
3268 DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
3271 * cpufreq_update_util - Take a note about CPU utilization changes.
3272 * @rq: Runqueue to carry out the update for.
3273 * @flags: Update reason flags.
3275 * This function is called by the scheduler on the CPU whose utilization is
3278 * It can only be called from RCU-sched read-side critical sections.
3280 * The way cpufreq is currently arranged requires it to evaluate the CPU
3281 * performance state (frequency/voltage) on a regular basis to prevent it from
3282 * being stuck in a completely inadequate performance level for too long.
3283 * That is not guaranteed to happen if the updates are only triggered from CFS
3284 * and DL, though, because they may not be coming in if only RT tasks are
3285 * active all the time (or there are RT tasks only).
3287 * As a workaround for that issue, this function is called periodically by the
3288 * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
3289 * but that really is a band-aid. Going forward it should be replaced with
3290 * solutions targeted more specifically at RT tasks.
3292 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
3294 struct update_util_data *data;
3296 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
3299 data->func(data, rq_clock(rq), flags);
3301 #else /* !CONFIG_CPU_FREQ: */
3302 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) { }
3303 #endif /* !CONFIG_CPU_FREQ */
3305 #ifdef arch_scale_freq_capacity
3306 # ifndef arch_scale_freq_invariant
3307 # define arch_scale_freq_invariant() true
3310 # define arch_scale_freq_invariant() false
3315 unsigned long effective_cpu_util(int cpu, unsigned long util_cfs,
3317 unsigned long *max);
3319 unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual,
3325 * Verify the fitness of task @p to run on @cpu taking into account the
3326 * CPU original capacity and the runtime/deadline ratio of the task.
3328 * The function will return true if the original capacity of @cpu is
3329 * greater than or equal to task's deadline density right shifted by
3330 * (BW_SHIFT - SCHED_CAPACITY_SHIFT) and false otherwise.
3332 static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu)
3334 unsigned long cap = arch_scale_cpu_capacity(cpu);
3336 return cap >= p->dl.dl_density >> (BW_SHIFT - SCHED_CAPACITY_SHIFT);
3339 static inline unsigned long cpu_bw_dl(struct rq *rq)
3341 return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
3344 static inline unsigned long cpu_util_dl(struct rq *rq)
3346 return READ_ONCE(rq->avg_dl.util_avg);
3350 extern unsigned long cpu_util_cfs(int cpu);
3351 extern unsigned long cpu_util_cfs_boost(int cpu);
3353 static inline unsigned long cpu_util_rt(struct rq *rq)
3355 return READ_ONCE(rq->avg_rt.util_avg);
3358 #else /* !CONFIG_SMP */
3359 static inline bool update_other_load_avgs(struct rq *rq) { return false; }
3360 #endif /* CONFIG_SMP */
3362 #ifdef CONFIG_UCLAMP_TASK
3364 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
3367 * When uclamp is compiled in, the aggregation at rq level is 'turned off'
3368 * by default in the fast path and only gets turned on once userspace performs
3369 * an operation that requires it.
3371 * Returns true if userspace opted-in to use uclamp and aggregation at rq level
3374 static inline bool uclamp_is_used(void)
3376 return static_branch_likely(&sched_uclamp_used);
3380 * Enabling static branches would get the cpus_read_lock(),
3381 * check whether uclamp_is_used before enable it to avoid always
3382 * calling cpus_read_lock(). Because we never disable this
3383 * static key once enable it.
3385 static inline void sched_uclamp_enable(void)
3387 if (!uclamp_is_used())
3388 static_branch_enable(&sched_uclamp_used);
3391 static inline unsigned long uclamp_rq_get(struct rq *rq,
3392 enum uclamp_id clamp_id)
3394 return READ_ONCE(rq->uclamp[clamp_id].value);
3397 static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id,
3400 WRITE_ONCE(rq->uclamp[clamp_id].value, value);
3403 static inline bool uclamp_rq_is_idle(struct rq *rq)
3405 return rq->uclamp_flags & UCLAMP_FLAG_IDLE;
3408 /* Is the rq being capped/throttled by uclamp_max? */
3409 static inline bool uclamp_rq_is_capped(struct rq *rq)
3411 unsigned long rq_util;
3412 unsigned long max_util;
3414 if (!uclamp_is_used())
3417 rq_util = cpu_util_cfs(cpu_of(rq)) + cpu_util_rt(rq);
3418 max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
3420 return max_util != SCHED_CAPACITY_SCALE && rq_util >= max_util;
3423 #define for_each_clamp_id(clamp_id) \
3424 for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++)
3426 extern unsigned int sysctl_sched_uclamp_util_min_rt_default;
3429 static inline unsigned int uclamp_none(enum uclamp_id clamp_id)
3431 if (clamp_id == UCLAMP_MIN)
3433 return SCHED_CAPACITY_SCALE;
3436 /* Integer rounded range for each bucket */
3437 #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS)
3439 static inline unsigned int uclamp_bucket_id(unsigned int clamp_value)
3441 return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1);
3445 uclamp_se_set(struct uclamp_se *uc_se, unsigned int value, bool user_defined)
3447 uc_se->value = value;
3448 uc_se->bucket_id = uclamp_bucket_id(value);
3449 uc_se->user_defined = user_defined;
3452 #else /* !CONFIG_UCLAMP_TASK: */
3454 static inline unsigned long
3455 uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
3457 if (clamp_id == UCLAMP_MIN)
3460 return SCHED_CAPACITY_SCALE;
3463 static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; }
3465 static inline bool uclamp_is_used(void)
3470 static inline void sched_uclamp_enable(void) {}
3472 static inline unsigned long
3473 uclamp_rq_get(struct rq *rq, enum uclamp_id clamp_id)
3475 if (clamp_id == UCLAMP_MIN)
3478 return SCHED_CAPACITY_SCALE;
3482 uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, unsigned int value)
3486 static inline bool uclamp_rq_is_idle(struct rq *rq)
3491 #endif /* !CONFIG_UCLAMP_TASK */
3493 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
3495 static inline unsigned long cpu_util_irq(struct rq *rq)
3497 return READ_ONCE(rq->avg_irq.util_avg);
3501 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
3503 util *= (max - irq);
3510 #else /* !CONFIG_HAVE_SCHED_AVG_IRQ: */
3512 static inline unsigned long cpu_util_irq(struct rq *rq)
3518 unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
3523 #endif /* !CONFIG_HAVE_SCHED_AVG_IRQ */
3525 extern void __setparam_fair(struct task_struct *p, const struct sched_attr *attr);
3527 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
3529 #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus)))
3531 DECLARE_STATIC_KEY_FALSE(sched_energy_present);
3533 static inline bool sched_energy_enabled(void)
3535 return static_branch_unlikely(&sched_energy_present);
3538 #else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */
3540 #define perf_domain_span(pd) NULL
3542 static inline bool sched_energy_enabled(void) { return false; }
3544 #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
3546 #ifdef CONFIG_MEMBARRIER
3549 * The scheduler provides memory barriers required by membarrier between:
3550 * - prior user-space memory accesses and store to rq->membarrier_state,
3551 * - store to rq->membarrier_state and following user-space memory accesses.
3552 * In the same way it provides those guarantees around store to rq->curr.
3554 static inline void membarrier_switch_mm(struct rq *rq,
3555 struct mm_struct *prev_mm,
3556 struct mm_struct *next_mm)
3558 int membarrier_state;
3560 if (prev_mm == next_mm)
3563 membarrier_state = atomic_read(&next_mm->membarrier_state);
3564 if (READ_ONCE(rq->membarrier_state) == membarrier_state)
3567 WRITE_ONCE(rq->membarrier_state, membarrier_state);
3570 #else /* !CONFIG_MEMBARRIER :*/
3572 static inline void membarrier_switch_mm(struct rq *rq,
3573 struct mm_struct *prev_mm,
3574 struct mm_struct *next_mm)
3578 #endif /* !CONFIG_MEMBARRIER */
3581 static inline bool is_per_cpu_kthread(struct task_struct *p)
3583 if (!(p->flags & PF_KTHREAD))
3586 if (p->nr_cpus_allowed != 1)
3593 extern void swake_up_all_locked(struct swait_queue_head *q);
3594 extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
3596 extern int try_to_wake_up(struct task_struct *tsk, unsigned int state, int wake_flags);
3598 #ifdef CONFIG_PREEMPT_DYNAMIC
3599 extern int preempt_dynamic_mode;
3600 extern int sched_dynamic_mode(const char *str);
3601 extern void sched_dynamic_update(int mode);
3603 extern const char *preempt_modes[];
3605 #ifdef CONFIG_SCHED_MM_CID
3607 #define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */
3608 #define MM_CID_SCAN_DELAY 100 /* 100ms */
3610 extern raw_spinlock_t cid_lock;
3611 extern int use_cid_lock;
3613 extern void sched_mm_cid_migrate_from(struct task_struct *t);
3614 extern void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t);
3615 extern void task_tick_mm_cid(struct rq *rq, struct task_struct *curr);
3616 extern void init_sched_mm_cid(struct task_struct *t);
3618 static inline void __mm_cid_put(struct mm_struct *mm, int cid)
3622 cpumask_clear_cpu(cid, mm_cidmask(mm));
3626 * The per-mm/cpu cid can have the MM_CID_LAZY_PUT flag set or transition to
3627 * the MM_CID_UNSET state without holding the rq lock, but the rq lock needs to
3628 * be held to transition to other states.
3630 * State transitions synchronized with cmpxchg or try_cmpxchg need to be
3631 * consistent across CPUs, which prevents use of this_cpu_cmpxchg.
3633 static inline void mm_cid_put_lazy(struct task_struct *t)
3635 struct mm_struct *mm = t->mm;
3636 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
3639 lockdep_assert_irqs_disabled();
3640 cid = __this_cpu_read(pcpu_cid->cid);
3641 if (!mm_cid_is_lazy_put(cid) ||
3642 !try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET))
3644 __mm_cid_put(mm, mm_cid_clear_lazy_put(cid));
3647 static inline int mm_cid_pcpu_unset(struct mm_struct *mm)
3649 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
3652 lockdep_assert_irqs_disabled();
3653 cid = __this_cpu_read(pcpu_cid->cid);
3655 if (mm_cid_is_unset(cid))
3656 return MM_CID_UNSET;
3658 * Attempt transition from valid or lazy-put to unset.
3660 res = cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, cid, MM_CID_UNSET);
3668 static inline void mm_cid_put(struct mm_struct *mm)
3672 lockdep_assert_irqs_disabled();
3673 cid = mm_cid_pcpu_unset(mm);
3674 if (cid == MM_CID_UNSET)
3676 __mm_cid_put(mm, mm_cid_clear_lazy_put(cid));
3679 static inline int __mm_cid_try_get(struct task_struct *t, struct mm_struct *mm)
3681 struct cpumask *cidmask = mm_cidmask(mm);
3682 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
3683 int cid, max_nr_cid, allowed_max_nr_cid;
3686 * After shrinking the number of threads or reducing the number
3687 * of allowed cpus, reduce the value of max_nr_cid so expansion
3688 * of cid allocation will preserve cache locality if the number
3689 * of threads or allowed cpus increase again.
3691 max_nr_cid = atomic_read(&mm->max_nr_cid);
3692 while ((allowed_max_nr_cid = min_t(int, READ_ONCE(mm->nr_cpus_allowed),
3693 atomic_read(&mm->mm_users))),
3694 max_nr_cid > allowed_max_nr_cid) {
3695 /* atomic_try_cmpxchg loads previous mm->max_nr_cid into max_nr_cid. */
3696 if (atomic_try_cmpxchg(&mm->max_nr_cid, &max_nr_cid, allowed_max_nr_cid)) {
3697 max_nr_cid = allowed_max_nr_cid;
3701 /* Try to re-use recent cid. This improves cache locality. */
3702 cid = __this_cpu_read(pcpu_cid->recent_cid);
3703 if (!mm_cid_is_unset(cid) && cid < max_nr_cid &&
3704 !cpumask_test_and_set_cpu(cid, cidmask))
3707 * Expand cid allocation if the maximum number of concurrency
3708 * IDs allocated (max_nr_cid) is below the number cpus allowed
3709 * and number of threads. Expanding cid allocation as much as
3710 * possible improves cache locality.
3713 while (cid < READ_ONCE(mm->nr_cpus_allowed) && cid < atomic_read(&mm->mm_users)) {
3714 /* atomic_try_cmpxchg loads previous mm->max_nr_cid into cid. */
3715 if (!atomic_try_cmpxchg(&mm->max_nr_cid, &cid, cid + 1))
3717 if (!cpumask_test_and_set_cpu(cid, cidmask))
3721 * Find the first available concurrency id.
3722 * Retry finding first zero bit if the mask is temporarily
3723 * filled. This only happens during concurrent remote-clear
3724 * which owns a cid without holding a rq lock.
3727 cid = cpumask_first_zero(cidmask);
3728 if (cid < READ_ONCE(mm->nr_cpus_allowed))
3732 if (cpumask_test_and_set_cpu(cid, cidmask))
3739 * Save a snapshot of the current runqueue time of this cpu
3740 * with the per-cpu cid value, allowing to estimate how recently it was used.
3742 static inline void mm_cid_snapshot_time(struct rq *rq, struct mm_struct *mm)
3744 struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu_of(rq));
3746 lockdep_assert_rq_held(rq);
3747 WRITE_ONCE(pcpu_cid->time, rq->clock);
3750 static inline int __mm_cid_get(struct rq *rq, struct task_struct *t,
3751 struct mm_struct *mm)
3756 * All allocations (even those using the cid_lock) are lock-free. If
3757 * use_cid_lock is set, hold the cid_lock to perform cid allocation to
3758 * guarantee forward progress.
3760 if (!READ_ONCE(use_cid_lock)) {
3761 cid = __mm_cid_try_get(t, mm);
3764 raw_spin_lock(&cid_lock);
3766 raw_spin_lock(&cid_lock);
3767 cid = __mm_cid_try_get(t, mm);
3773 * cid concurrently allocated. Retry while forcing following
3774 * allocations to use the cid_lock to ensure forward progress.
3776 WRITE_ONCE(use_cid_lock, 1);
3778 * Set use_cid_lock before allocation. Only care about program order
3779 * because this is only required for forward progress.
3783 * Retry until it succeeds. It is guaranteed to eventually succeed once
3784 * all newcoming allocations observe the use_cid_lock flag set.
3787 cid = __mm_cid_try_get(t, mm);
3791 * Allocate before clearing use_cid_lock. Only care about
3792 * program order because this is for forward progress.
3795 WRITE_ONCE(use_cid_lock, 0);
3797 raw_spin_unlock(&cid_lock);
3799 mm_cid_snapshot_time(rq, mm);
3804 static inline int mm_cid_get(struct rq *rq, struct task_struct *t,
3805 struct mm_struct *mm)
3807 struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
3808 struct cpumask *cpumask;
3811 lockdep_assert_rq_held(rq);
3812 cpumask = mm_cidmask(mm);
3813 cid = __this_cpu_read(pcpu_cid->cid);
3814 if (mm_cid_is_valid(cid)) {
3815 mm_cid_snapshot_time(rq, mm);
3818 if (mm_cid_is_lazy_put(cid)) {
3819 if (try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET))
3820 __mm_cid_put(mm, mm_cid_clear_lazy_put(cid));
3822 cid = __mm_cid_get(rq, t, mm);
3823 __this_cpu_write(pcpu_cid->cid, cid);
3824 __this_cpu_write(pcpu_cid->recent_cid, cid);
3829 static inline void switch_mm_cid(struct rq *rq,
3830 struct task_struct *prev,
3831 struct task_struct *next)
3834 * Provide a memory barrier between rq->curr store and load of
3835 * {prev,next}->mm->pcpu_cid[cpu] on rq->curr->mm transition.
3837 * Should be adapted if context_switch() is modified.
3839 if (!next->mm) { // to kernel
3841 * user -> kernel transition does not guarantee a barrier, but
3842 * we can use the fact that it performs an atomic operation in
3845 if (prev->mm) // from user
3846 smp_mb__after_mmgrab();
3848 * kernel -> kernel transition does not change rq->curr->mm
3849 * state. It stays NULL.
3853 * kernel -> user transition does not provide a barrier
3854 * between rq->curr store and load of {prev,next}->mm->pcpu_cid[cpu].
3857 if (!prev->mm) { // from kernel
3859 } else { // from user
3861 * user->user transition relies on an implicit
3862 * memory barrier in switch_mm() when
3863 * current->mm changes. If the architecture
3864 * switch_mm() does not have an implicit memory
3865 * barrier, it is emitted here. If current->mm
3866 * is unchanged, no barrier is needed.
3868 smp_mb__after_switch_mm();
3871 if (prev->mm_cid_active) {
3872 mm_cid_snapshot_time(rq, prev->mm);
3873 mm_cid_put_lazy(prev);
3876 if (next->mm_cid_active)
3877 next->last_mm_cid = next->mm_cid = mm_cid_get(rq, next, next->mm);
3880 #else /* !CONFIG_SCHED_MM_CID: */
3881 static inline void switch_mm_cid(struct rq *rq, struct task_struct *prev, struct task_struct *next) { }
3882 static inline void sched_mm_cid_migrate_from(struct task_struct *t) { }
3883 static inline void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t) { }
3884 static inline void task_tick_mm_cid(struct rq *rq, struct task_struct *curr) { }
3885 static inline void init_sched_mm_cid(struct task_struct *t) { }
3886 #endif /* !CONFIG_SCHED_MM_CID */
3888 extern u64 avg_vruntime(struct cfs_rq *cfs_rq);
3889 extern int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se);
3892 void move_queued_task_locked(struct rq *src_rq, struct rq *dst_rq, struct task_struct *task)
3894 lockdep_assert_rq_held(src_rq);
3895 lockdep_assert_rq_held(dst_rq);
3897 deactivate_task(src_rq, task, 0);
3898 set_task_cpu(task, dst_rq->cpu);
3899 activate_task(dst_rq, task, 0);
3903 bool task_is_pushable(struct rq *rq, struct task_struct *p, int cpu)
3905 if (!task_on_cpu(rq, p) &&
3906 cpumask_test_cpu(cpu, &p->cpus_mask))
3913 #ifdef CONFIG_RT_MUTEXES
3915 static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
3918 prio = min(prio, pi_task->prio);
3923 static inline int rt_effective_prio(struct task_struct *p, int prio)
3925 struct task_struct *pi_task = rt_mutex_get_top_task(p);
3927 return __rt_effective_prio(pi_task, prio);
3930 #else /* !CONFIG_RT_MUTEXES: */
3932 static inline int rt_effective_prio(struct task_struct *p, int prio)
3937 #endif /* !CONFIG_RT_MUTEXES */
3939 extern int __sched_setscheduler(struct task_struct *p, const struct sched_attr *attr, bool user, bool pi);
3940 extern int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx);
3941 extern const struct sched_class *__setscheduler_class(int policy, int prio);
3942 extern void set_load_weight(struct task_struct *p, bool update_load);
3943 extern void enqueue_task(struct rq *rq, struct task_struct *p, int flags);
3944 extern bool dequeue_task(struct rq *rq, struct task_struct *p, int flags);
3946 extern void check_class_changing(struct rq *rq, struct task_struct *p,
3947 const struct sched_class *prev_class);
3948 extern void check_class_changed(struct rq *rq, struct task_struct *p,
3949 const struct sched_class *prev_class,
3953 extern struct balance_callback *splice_balance_callbacks(struct rq *rq);
3954 extern void balance_callbacks(struct rq *rq, struct balance_callback *head);
3957 static inline struct balance_callback *splice_balance_callbacks(struct rq *rq)
3962 static inline void balance_callbacks(struct rq *rq, struct balance_callback *head)
3968 #ifdef CONFIG_SCHED_CLASS_EXT
3970 * Used by SCX in the enable/disable paths to move tasks between sched_classes
3971 * and establish invariants.
3973 struct sched_enq_and_set_ctx {
3974 struct task_struct *p;
3980 void sched_deq_and_put_task(struct task_struct *p, int queue_flags,
3981 struct sched_enq_and_set_ctx *ctx);
3982 void sched_enq_and_set_task(struct sched_enq_and_set_ctx *ctx);
3984 #endif /* CONFIG_SCHED_CLASS_EXT */
3988 #endif /* _KERNEL_SCHED_SCHED_H */