Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
97fb7a0a IM |
2 | /* |
3 | * Scheduler internal types and methods: | |
4 | */ | |
95458477 IM |
5 | #ifndef _KERNEL_SCHED_SCHED_H |
6 | #define _KERNEL_SCHED_SCHED_H | |
325ea10c | 7 | |
801c1419 | 8 | #include <linux/sched/affinity.h> |
dfc3401a | 9 | #include <linux/sched/autogroup.h> |
55687da1 | 10 | #include <linux/sched/cpufreq.h> |
325ea10c | 11 | #include <linux/sched/deadline.h> |
4ff8f2ca | 12 | #include <linux/sched.h> |
325ea10c IM |
13 | #include <linux/sched/loadavg.h> |
14 | #include <linux/sched/mm.h> | |
801c1419 | 15 | #include <linux/sched/rseq_api.h> |
325ea10c | 16 | #include <linux/sched/signal.h> |
321a874a | 17 | #include <linux/sched/smt.h> |
325ea10c IM |
18 | #include <linux/sched/stat.h> |
19 | #include <linux/sched/sysctl.h> | |
4ff8f2ca | 20 | #include <linux/sched/task_flags.h> |
29930025 | 21 | #include <linux/sched/task.h> |
325ea10c | 22 | #include <linux/sched/topology.h> |
ef8bd77f | 23 | |
4ff8f2ca | 24 | #include <linux/atomic.h> |
801c1419 | 25 | #include <linux/bitmap.h> |
4ff8f2ca | 26 | #include <linux/bug.h> |
801c1419 | 27 | #include <linux/capability.h> |
4ff8f2ca | 28 | #include <linux/cgroup_api.h> |
801c1419 | 29 | #include <linux/cgroup.h> |
e67198cc | 30 | #include <linux/context_tracking.h> |
325ea10c | 31 | #include <linux/cpufreq.h> |
801c1419 | 32 | #include <linux/cpumask_api.h> |
325ea10c | 33 | #include <linux/ctype.h> |
801c1419 | 34 | #include <linux/file.h> |
4ff8f2ca | 35 | #include <linux/fs_api.h> |
f96eca43 IM |
36 | #include <linux/hrtimer_api.h> |
37 | #include <linux/interrupt.h> | |
4ff8f2ca | 38 | #include <linux/irq_work.h> |
801c1419 IM |
39 | #include <linux/jiffies.h> |
40 | #include <linux/kref_api.h> | |
325ea10c | 41 | #include <linux/kthread.h> |
f96eca43 | 42 | #include <linux/ktime_api.h> |
801c1419 | 43 | #include <linux/lockdep_api.h> |
4ff8f2ca IM |
44 | #include <linux/lockdep.h> |
45 | #include <linux/minmax.h> | |
46 | #include <linux/mm.h> | |
801c1419 IM |
47 | #include <linux/module.h> |
48 | #include <linux/mutex_api.h> | |
4ff8f2ca | 49 | #include <linux/plist.h> |
801c1419 | 50 | #include <linux/poll.h> |
325ea10c | 51 | #include <linux/proc_fs.h> |
325ea10c | 52 | #include <linux/profile.h> |
eb414681 | 53 | #include <linux/psi.h> |
4ff8f2ca | 54 | #include <linux/rcupdate.h> |
801c1419 IM |
55 | #include <linux/seq_file.h> |
56 | #include <linux/seqlock.h> | |
f96eca43 IM |
57 | #include <linux/softirq.h> |
58 | #include <linux/spinlock_api.h> | |
4ff8f2ca | 59 | #include <linux/static_key.h> |
029632fb | 60 | #include <linux/stop_machine.h> |
801c1419 | 61 | #include <linux/syscalls_api.h> |
325ea10c | 62 | #include <linux/syscalls.h> |
4ff8f2ca | 63 | #include <linux/tick.h> |
801c1419 IM |
64 | #include <linux/topology.h> |
65 | #include <linux/types.h> | |
f96eca43 | 66 | #include <linux/u64_stats_sync_api.h> |
801c1419 IM |
67 | #include <linux/uaccess.h> |
68 | #include <linux/wait_api.h> | |
4ff8f2ca | 69 | #include <linux/wait_bit.h> |
801c1419 | 70 | #include <linux/workqueue_api.h> |
e8901061 | 71 | #include <linux/delayacct.h> |
801c1419 IM |
72 | |
73 | #include <trace/events/power.h> | |
4ff8f2ca | 74 | #include <trace/events/sched.h> |
801c1419 IM |
75 | |
76 | #include "../workqueue_internal.h" | |
029632fb | 77 | |
3cd72719 IM |
78 | struct rq; |
79 | struct cfs_rq; | |
80 | struct rt_rq; | |
81 | struct sched_group; | |
82 | struct cpuidle_state; | |
83 | ||
7fce777c | 84 | #ifdef CONFIG_PARAVIRT |
325ea10c | 85 | # include <asm/paravirt.h> |
4ff8f2ca | 86 | # include <asm/paravirt_api_clock.h> |
7fce777c IM |
87 | #endif |
88 | ||
fe90f396 MD |
89 | #include <asm/barrier.h> |
90 | ||
391e43da | 91 | #include "cpupri.h" |
6bfd6d72 | 92 | #include "cpudeadline.h" |
029632fb | 93 | |
da0c1e65 KT |
94 | /* task_struct::on_rq states: */ |
95 | #define TASK_ON_RQ_QUEUED 1 | |
cca26e80 | 96 | #define TASK_ON_RQ_MIGRATING 2 |
da0c1e65 | 97 | |
029632fb PZ |
98 | extern __read_mostly int scheduler_running; |
99 | ||
45ceebf7 PG |
100 | extern unsigned long calc_load_update; |
101 | extern atomic_long_t calc_load_tasks; | |
102 | ||
3289bdb4 | 103 | extern void calc_global_load_tick(struct rq *this_rq); |
d60585c5 | 104 | extern long calc_load_fold_active(struct rq *this_rq, long adjust); |
3289bdb4 | 105 | |
9d246053 | 106 | extern void call_trace_sched_update_nr_running(struct rq *rq, int count); |
d9ab0e63 | 107 | |
089768df | 108 | extern int sysctl_sched_rt_period; |
d9ab0e63 | 109 | extern int sysctl_sched_rt_runtime; |
dafd7a9d | 110 | extern int sched_rr_timeslice; |
d9ab0e63 | 111 | |
77222b0d QY |
112 | /* |
113 | * Asymmetric CPU capacity bits | |
114 | */ | |
115 | struct asym_cap_data { | |
116 | struct list_head link; | |
117 | struct rcu_head rcu; | |
118 | unsigned long capacity; | |
119 | unsigned long cpus[]; | |
120 | }; | |
121 | ||
122 | extern struct list_head asym_cap_list; | |
123 | ||
124 | #define cpu_capacity_span(asym_data) to_cpumask((asym_data)->cpus) | |
125 | ||
029632fb PZ |
126 | /* |
127 | * Helpers for converting nanosecond timing to jiffy resolution | |
128 | */ | |
127f6bf1 | 129 | #define NS_TO_JIFFIES(time) ((unsigned long)(time) / (NSEC_PER_SEC/HZ)) |
029632fb | 130 | |
cc1f4b1f LZ |
131 | /* |
132 | * Increase resolution of nice-level calculations for 64-bit architectures. | |
133 | * The extra resolution improves shares distribution and load balancing of | |
402de7fc | 134 | * low-weight task groups (eg. nice +19 on an autogroup), deeper task-group |
cc1f4b1f LZ |
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. | |
137 | * | |
138 | * We increase resolution only if we have enough bits to allow this increased | |
97fb7a0a IM |
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. | |
2159197d | 141 | * |
97fb7a0a IM |
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. | |
cc1f4b1f | 144 | */ |
2159197d | 145 | #ifdef CONFIG_64BIT |
172895e6 | 146 | # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT) |
6ecdd749 | 147 | # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT) |
127f6bf1 IM |
148 | # define scale_load_down(w) \ |
149 | ({ \ | |
150 | unsigned long __w = (w); \ | |
151 | \ | |
152 | if (__w) \ | |
153 | __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \ | |
154 | __w; \ | |
26cf5222 | 155 | }) |
cc1f4b1f | 156 | #else |
172895e6 | 157 | # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT) |
cc1f4b1f LZ |
158 | # define scale_load(w) (w) |
159 | # define scale_load_down(w) (w) | |
160 | #endif | |
161 | ||
6ecdd749 | 162 | /* |
172895e6 YD |
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: | |
167 | * | |
9d061ba6 | 168 | * scale_load(sched_prio_to_weight[NICE_TO_PRIO(0)-MAX_RT_PRIO]) == NICE_0_LOAD |
172895e6 | 169 | * |
6ecdd749 | 170 | */ |
172895e6 | 171 | #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT) |
029632fb | 172 | |
332ac17e DF |
173 | /* |
174 | * Single value that decides SCHED_DEADLINE internal math precision. | |
175 | * 10 -> just above 1us | |
176 | * 9 -> just above 0.5us | |
177 | */ | |
97fb7a0a | 178 | #define DL_SCALE 10 |
029632fb PZ |
179 | |
180 | /* | |
97fb7a0a | 181 | * Single value that denotes runtime == period, ie unlimited time. |
029632fb | 182 | */ |
97fb7a0a | 183 | #define RUNTIME_INF ((u64)~0ULL) |
029632fb | 184 | |
20f9cd2a HA |
185 | static inline int idle_policy(int policy) |
186 | { | |
187 | return policy == SCHED_IDLE; | |
188 | } | |
127f6bf1 | 189 | |
2c8d046d TH |
190 | static inline int normal_policy(int policy) |
191 | { | |
f0e1a064 TH |
192 | #ifdef CONFIG_SCHED_CLASS_EXT |
193 | if (policy == SCHED_EXT) | |
194 | return true; | |
195 | #endif | |
2c8d046d TH |
196 | return policy == SCHED_NORMAL; |
197 | } | |
198 | ||
d50dde5a DF |
199 | static inline int fair_policy(int policy) |
200 | { | |
2c8d046d | 201 | return normal_policy(policy) || policy == SCHED_BATCH; |
d50dde5a DF |
202 | } |
203 | ||
029632fb PZ |
204 | static inline int rt_policy(int policy) |
205 | { | |
d50dde5a | 206 | return policy == SCHED_FIFO || policy == SCHED_RR; |
029632fb PZ |
207 | } |
208 | ||
aab03e05 DF |
209 | static inline int dl_policy(int policy) |
210 | { | |
211 | return policy == SCHED_DEADLINE; | |
212 | } | |
127f6bf1 | 213 | |
20f9cd2a HA |
214 | static inline bool valid_policy(int policy) |
215 | { | |
216 | return idle_policy(policy) || fair_policy(policy) || | |
217 | rt_policy(policy) || dl_policy(policy); | |
218 | } | |
aab03e05 | 219 | |
1da1843f VK |
220 | static inline int task_has_idle_policy(struct task_struct *p) |
221 | { | |
222 | return idle_policy(p->policy); | |
223 | } | |
224 | ||
029632fb PZ |
225 | static inline int task_has_rt_policy(struct task_struct *p) |
226 | { | |
227 | return rt_policy(p->policy); | |
228 | } | |
229 | ||
aab03e05 DF |
230 | static inline int task_has_dl_policy(struct task_struct *p) |
231 | { | |
232 | return dl_policy(p->policy); | |
233 | } | |
234 | ||
127f6bf1 | 235 | #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) |
07881166 | 236 | |
d76343c6 VS |
237 | static inline void update_avg(u64 *avg, u64 sample) |
238 | { | |
239 | s64 diff = sample - *avg; | |
127f6bf1 | 240 | |
d76343c6 VS |
241 | *avg += diff / 8; |
242 | } | |
243 | ||
39a2a6eb VS |
244 | /* |
245 | * Shifting a value by an exponent greater *or equal* to the size of said value | |
246 | * is UB; cap at size-1. | |
247 | */ | |
248 | #define shr_bound(val, shift) \ | |
249 | (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1)) | |
250 | ||
4f9c7ca8 TH |
251 | /* |
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. | |
256 | */ | |
257 | static inline unsigned long sched_weight_from_cgroup(unsigned long cgrp_weight) | |
258 | { | |
259 | return DIV_ROUND_CLOSEST_ULL(cgrp_weight * 1024, CGROUP_WEIGHT_DFL); | |
260 | } | |
261 | ||
262 | static inline unsigned long sched_weight_to_cgroup(unsigned long weight) | |
263 | { | |
264 | return clamp_t(unsigned long, | |
265 | DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024), | |
266 | CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX); | |
267 | } | |
268 | ||
794a56eb JL |
269 | /* |
270 | * !! For sched_setattr_nocheck() (kernel) only !! | |
271 | * | |
272 | * This is actually gross. :( | |
273 | * | |
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. | |
278 | * | |
279 | * SUGOV stands for SchedUtil GOVernor. | |
280 | */ | |
281 | #define SCHED_FLAG_SUGOV 0x10000000 | |
282 | ||
127f6bf1 | 283 | #define SCHED_DL_FLAGS (SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV) |
f9509153 | 284 | |
904cbab7 | 285 | static inline bool dl_entity_is_special(const struct sched_dl_entity *dl_se) |
794a56eb JL |
286 | { |
287 | #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL | |
288 | return unlikely(dl_se->flags & SCHED_FLAG_SUGOV); | |
289 | #else | |
290 | return false; | |
291 | #endif | |
292 | } | |
293 | ||
2d3d891d DF |
294 | /* |
295 | * Tells if entity @a should preempt entity @b. | |
296 | */ | |
904cbab7 MWO |
297 | static inline bool dl_entity_preempt(const struct sched_dl_entity *a, |
298 | const struct sched_dl_entity *b) | |
2d3d891d | 299 | { |
794a56eb JL |
300 | return dl_entity_is_special(a) || |
301 | dl_time_before(a->deadline, b->deadline); | |
2d3d891d DF |
302 | } |
303 | ||
029632fb PZ |
304 | /* |
305 | * This is the priority-queue data structure of the RT scheduling class: | |
306 | */ | |
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]; | |
310 | }; | |
311 | ||
312 | struct rt_bandwidth { | |
313 | /* nests inside the rq lock: */ | |
314 | raw_spinlock_t rt_runtime_lock; | |
315 | ktime_t rt_period; | |
316 | u64 rt_runtime; | |
317 | struct hrtimer rt_period_timer; | |
4cfafd30 | 318 | unsigned int rt_period_active; |
029632fb | 319 | }; |
a5e7be3b | 320 | |
332ac17e DF |
321 | static inline int dl_bandwidth_enabled(void) |
322 | { | |
1724813d | 323 | return sysctl_sched_rt_runtime >= 0; |
332ac17e DF |
324 | } |
325 | ||
a57415f5 PL |
326 | /* |
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 | |
331 | * each root domain; | |
332 | * | |
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. | |
338 | * | |
339 | * With respect to SMP, bandwidth is given on a per root domain basis, | |
340 | * meaning that: | |
341 | * - bw (< 100%) is the deadline bandwidth of each CPU; | |
342 | * - total_bw is the currently allocated bandwidth in each root domain; | |
343 | */ | |
332ac17e | 344 | struct dl_bw { |
97fb7a0a IM |
345 | raw_spinlock_t lock; |
346 | u64 bw; | |
347 | u64 total_bw; | |
332ac17e DF |
348 | }; |
349 | ||
f2cb1360 | 350 | extern void init_dl_bw(struct dl_bw *dl_b); |
97fb7a0a | 351 | extern int sched_dl_global_validate(void); |
06a76fe0 | 352 | extern void sched_dl_do_global(void); |
97fb7a0a | 353 | extern int sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr); |
06a76fe0 NP |
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); | |
06a76fe0 | 357 | extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr); |
97fb7a0a | 358 | extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial); |
d4742f6e | 359 | extern int dl_bw_deactivate(int cpu); |
a110a81c | 360 | extern s64 dl_scaled_delta_exec(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec); |
63ba8422 PZ |
361 | /* |
362 | * SCHED_DEADLINE supports servers (nested scheduling) with the following | |
363 | * interface: | |
364 | * | |
365 | * dl_se::rq -- runqueue we belong to. | |
366 | * | |
367 | * dl_se::server_has_tasks() -- used on bandwidth enforcement; we 'stop' the | |
368 | * server when it runs out of tasks to run. | |
369 | * | |
370 | * dl_se::server_pick() -- nested pick_next_task(); we yield the period if this | |
371 | * returns NULL. | |
372 | * | |
373 | * dl_server_update() -- called from update_curr_common(), propagates runtime | |
374 | * to the server. | |
375 | * | |
376 | * dl_server_start() | |
377 | * dl_server_stop() -- start/stop the server when it has (no) tasks. | |
378 | * | |
379 | * dl_server_init() -- initializes the server. | |
380 | */ | |
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, | |
c8a85394 | 386 | dl_server_pick_f pick_task); |
63ba8422 | 387 | |
a110a81c DBO |
388 | extern void dl_server_update_idle_time(struct rq *rq, |
389 | struct task_struct *p); | |
557a6bfc | 390 | extern void fair_server_init(struct rq *rq); |
d741f297 DBO |
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); | |
63ba8422 | 394 | |
b53127db VPG |
395 | static inline bool dl_server_active(struct sched_dl_entity *dl_se) |
396 | { | |
397 | return dl_se->dl_server_active; | |
398 | } | |
399 | ||
029632fb PZ |
400 | #ifdef CONFIG_CGROUP_SCHED |
401 | ||
35cf4e50 | 402 | extern struct list_head task_groups; |
029632fb PZ |
403 | |
404 | struct cfs_bandwidth { | |
405 | #ifdef CONFIG_CFS_BANDWIDTH | |
97fb7a0a IM |
406 | raw_spinlock_t lock; |
407 | ktime_t period; | |
408 | u64 quota; | |
409 | u64 runtime; | |
f4183717 | 410 | u64 burst; |
bcb1704a | 411 | u64 runtime_snap; |
97fb7a0a | 412 | s64 hierarchical_quota; |
97fb7a0a | 413 | |
66567fcb | 414 | u8 idle; |
415 | u8 period_active; | |
66567fcb | 416 | u8 slack_started; |
97fb7a0a IM |
417 | struct hrtimer period_timer; |
418 | struct hrtimer slack_timer; | |
419 | struct list_head throttled_cfs_rq; | |
420 | ||
421 | /* Statistics: */ | |
422 | int nr_periods; | |
423 | int nr_throttled; | |
bcb1704a | 424 | int nr_burst; |
97fb7a0a | 425 | u64 throttled_time; |
bcb1704a | 426 | u64 burst_time; |
029632fb PZ |
427 | #endif |
428 | }; | |
429 | ||
97fb7a0a | 430 | /* Task group related information */ |
029632fb PZ |
431 | struct task_group { |
432 | struct cgroup_subsys_state css; | |
433 | ||
7ebd84d6 YL |
434 | #ifdef CONFIG_GROUP_SCHED_WEIGHT |
435 | /* A positive value indicates that this is a SCHED_IDLE group. */ | |
436 | int idle; | |
437 | #endif | |
438 | ||
029632fb | 439 | #ifdef CONFIG_FAIR_GROUP_SCHED |
97fb7a0a IM |
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; | |
fa6bddeb | 445 | #ifdef CONFIG_SMP |
b0367629 WL |
446 | /* |
447 | * load_avg can be heavily contended at clock tick time, so put | |
402de7fc | 448 | * it in its own cache-line separated from the fields above which |
b0367629 WL |
449 | * will also be accessed at each tick. |
450 | */ | |
97fb7a0a | 451 | atomic_long_t load_avg ____cacheline_aligned; |
029632fb | 452 | #endif |
fa6bddeb | 453 | #endif |
029632fb PZ |
454 | |
455 | #ifdef CONFIG_RT_GROUP_SCHED | |
97fb7a0a IM |
456 | struct sched_rt_entity **rt_se; |
457 | struct rt_rq **rt_rq; | |
029632fb | 458 | |
97fb7a0a | 459 | struct rt_bandwidth rt_bandwidth; |
029632fb PZ |
460 | #endif |
461 | ||
81951366 TH |
462 | #ifdef CONFIG_EXT_GROUP_SCHED |
463 | u32 scx_flags; /* SCX_TG_* */ | |
464 | u32 scx_weight; | |
465 | #endif | |
466 | ||
97fb7a0a IM |
467 | struct rcu_head rcu; |
468 | struct list_head list; | |
029632fb | 469 | |
97fb7a0a IM |
470 | struct task_group *parent; |
471 | struct list_head siblings; | |
472 | struct list_head children; | |
029632fb PZ |
473 | |
474 | #ifdef CONFIG_SCHED_AUTOGROUP | |
97fb7a0a | 475 | struct autogroup *autogroup; |
029632fb PZ |
476 | #endif |
477 | ||
97fb7a0a | 478 | struct cfs_bandwidth cfs_bandwidth; |
2480c093 PB |
479 | |
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]; | |
0b60ba2d PB |
485 | /* Effective clamp values used for a task group */ |
486 | struct uclamp_se uclamp[UCLAMP_CNT]; | |
2480c093 PB |
487 | #endif |
488 | ||
029632fb PZ |
489 | }; |
490 | ||
e179e80c | 491 | #ifdef CONFIG_GROUP_SCHED_WEIGHT |
029632fb PZ |
492 | #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD |
493 | ||
494 | /* | |
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.) | |
501 | */ | |
97fb7a0a IM |
502 | #define MIN_SHARES (1UL << 1) |
503 | #define MAX_SHARES (1UL << 18) | |
029632fb PZ |
504 | #endif |
505 | ||
029632fb PZ |
506 | typedef int (*tg_visitor)(struct task_group *, void *); |
507 | ||
508 | extern int walk_tg_tree_from(struct task_group *from, | |
509 | tg_visitor down, tg_visitor up, void *data); | |
510 | ||
511 | /* | |
512 | * Iterate the full tree, calling @down when first entering a node and @up when | |
513 | * leaving it for the final time. | |
514 | * | |
515 | * Caller must hold rcu_lock or sufficient equivalent. | |
516 | */ | |
517 | static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) | |
518 | { | |
519 | return walk_tg_tree_from(&root_task_group, down, up, data); | |
520 | } | |
521 | ||
859dc4ec TH |
522 | static inline struct task_group *css_tg(struct cgroup_subsys_state *css) |
523 | { | |
524 | return css ? container_of(css, struct task_group, css) : NULL; | |
525 | } | |
526 | ||
029632fb PZ |
527 | extern int tg_nop(struct task_group *tg, void *data); |
528 | ||
b1c3efe0 | 529 | #ifdef CONFIG_FAIR_GROUP_SCHED |
029632fb PZ |
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); | |
8663e24d | 532 | extern void online_fair_sched_group(struct task_group *tg); |
6fe1f348 | 533 | extern void unregister_fair_sched_group(struct task_group *tg); |
b1c3efe0 AB |
534 | #else |
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) | |
537 | { | |
538 | return 1; | |
539 | } | |
540 | static inline void online_fair_sched_group(struct task_group *tg) { } | |
541 | static inline void unregister_fair_sched_group(struct task_group *tg) { } | |
542 | #endif | |
543 | ||
029632fb PZ |
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); | |
c98c1827 | 547 | extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent); |
029632fb PZ |
548 | |
549 | extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b); | |
77a4d1a1 | 550 | extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b); |
029632fb | 551 | extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq); |
88c56cfe | 552 | extern bool cfs_task_bw_constrained(struct task_struct *p); |
029632fb | 553 | |
029632fb PZ |
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); | |
8887cd99 NP |
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); | |
029632fb | 562 | |
25cc7da7 LZ |
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); | |
b027789e | 567 | extern void sched_release_group(struct task_group *tg); |
25cc7da7 | 568 | |
d6f3e7d5 | 569 | extern void sched_move_task(struct task_struct *tsk, bool for_autogroup); |
25cc7da7 LZ |
570 | |
571 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
572 | extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); | |
ad936d86 | 573 | |
30400039 JD |
574 | extern int sched_group_set_idle(struct task_group *tg, long idle); |
575 | ||
ad936d86 BP |
576 | #ifdef CONFIG_SMP |
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 */ | |
e179e80c TH |
583 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
584 | static inline int sched_group_set_shares(struct task_group *tg, unsigned long shares) { return 0; } | |
bdeb868c | 585 | static inline int sched_group_set_idle(struct task_group *tg, long idle) { return 0; } |
ad936d86 | 586 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
25cc7da7 | 587 | |
029632fb PZ |
588 | #else /* CONFIG_CGROUP_SCHED */ |
589 | ||
590 | struct cfs_bandwidth { }; | |
127f6bf1 | 591 | |
88c56cfe | 592 | static inline bool cfs_task_bw_constrained(struct task_struct *p) { return false; } |
029632fb PZ |
593 | |
594 | #endif /* CONFIG_CGROUP_SCHED */ | |
595 | ||
87514b2c BD |
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); | |
599 | ||
d05b4305 VD |
600 | /* |
601 | * u64_u32_load/u64_u32_store | |
602 | * | |
603 | * Use a copy of a u64 value to protect against data race. This is only | |
604 | * applicable for 32-bits architectures. | |
605 | */ | |
606 | #ifdef CONFIG_64BIT | |
127f6bf1 IM |
607 | # define u64_u32_load_copy(var, copy) var |
608 | # define u64_u32_store_copy(var, copy, val) (var = val) | |
d05b4305 VD |
609 | #else |
610 | # define u64_u32_load_copy(var, copy) \ | |
611 | ({ \ | |
612 | u64 __val, __val_copy; \ | |
613 | do { \ | |
614 | __val_copy = copy; \ | |
615 | /* \ | |
616 | * paired with u64_u32_store_copy(), ordering access \ | |
617 | * to var and copy. \ | |
618 | */ \ | |
619 | smp_rmb(); \ | |
620 | __val = var; \ | |
621 | } while (__val != __val_copy); \ | |
622 | __val; \ | |
623 | }) | |
624 | # define u64_u32_store_copy(var, copy, val) \ | |
625 | do { \ | |
626 | typeof(val) __val = (val); \ | |
627 | var = __val; \ | |
628 | /* \ | |
629 | * paired with u64_u32_load_copy(), ordering access to var and \ | |
630 | * copy. \ | |
631 | */ \ | |
632 | smp_wmb(); \ | |
633 | copy = __val; \ | |
634 | } while (0) | |
635 | #endif | |
127f6bf1 IM |
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) | |
d05b4305 | 638 | |
fc283116 TH |
639 | struct balance_callback { |
640 | struct balance_callback *next; | |
641 | void (*func)(struct rq *rq); | |
642 | }; | |
643 | ||
029632fb PZ |
644 | /* CFS-related fields in a runqueue */ |
645 | struct cfs_rq { | |
97fb7a0a | 646 | struct load_weight load; |
736c55a0 | 647 | unsigned int nr_queued; |
7b8a702d | 648 | unsigned int h_nr_queued; /* SCHED_{NORMAL,BATCH,IDLE} */ |
c2a295bf | 649 | unsigned int h_nr_runnable; /* SCHED_{NORMAL,BATCH,IDLE} */ |
31898e7b | 650 | unsigned int h_nr_idle; /* SCHED_IDLE */ |
029632fb | 651 | |
af4cf404 PZ |
652 | s64 avg_vruntime; |
653 | u64 avg_load; | |
654 | ||
97fb7a0a | 655 | u64 min_vruntime; |
c6047c2e JFG |
656 | #ifdef CONFIG_SCHED_CORE |
657 | unsigned int forceidle_seq; | |
658 | u64 min_vruntime_fi; | |
659 | #endif | |
660 | ||
97fb7a0a | 661 | struct rb_root_cached tasks_timeline; |
029632fb | 662 | |
029632fb PZ |
663 | /* |
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). | |
666 | */ | |
97fb7a0a IM |
667 | struct sched_entity *curr; |
668 | struct sched_entity *next; | |
029632fb | 669 | |
2dac754e PT |
670 | #ifdef CONFIG_SMP |
671 | /* | |
9d89c257 | 672 | * CFS load tracking |
2dac754e | 673 | */ |
97fb7a0a | 674 | struct sched_avg avg; |
2a2f5d4e | 675 | #ifndef CONFIG_64BIT |
d05b4305 | 676 | u64 last_update_time_copy; |
9d89c257 | 677 | #endif |
2a2f5d4e PZ |
678 | struct { |
679 | raw_spinlock_t lock ____cacheline_aligned; | |
680 | int nr; | |
681 | unsigned long load_avg; | |
682 | unsigned long util_avg; | |
9f683953 | 683 | unsigned long runnable_avg; |
2a2f5d4e | 684 | } removed; |
82958366 | 685 | |
9d89c257 | 686 | #ifdef CONFIG_FAIR_GROUP_SCHED |
1528c661 | 687 | u64 last_update_tg_load_avg; |
97fb7a0a IM |
688 | unsigned long tg_load_avg_contrib; |
689 | long propagate; | |
690 | long prop_runnable_sum; | |
0e2d2aaa | 691 | |
82958366 PT |
692 | /* |
693 | * h_load = weight * f(tg) | |
694 | * | |
695 | * Where f(tg) is the recursive weight fraction assigned to | |
696 | * this group. | |
697 | */ | |
97fb7a0a IM |
698 | unsigned long h_load; |
699 | u64 last_h_load_update; | |
700 | struct sched_entity *h_load_next; | |
68520796 | 701 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
82958366 PT |
702 | #endif /* CONFIG_SMP */ |
703 | ||
029632fb | 704 | #ifdef CONFIG_FAIR_GROUP_SCHED |
97fb7a0a | 705 | struct rq *rq; /* CPU runqueue to which this cfs_rq is attached */ |
029632fb PZ |
706 | |
707 | /* | |
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.) | |
711 | * | |
97fb7a0a IM |
712 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU. |
713 | * This list is used during load balance. | |
029632fb | 714 | */ |
97fb7a0a IM |
715 | int on_list; |
716 | struct list_head leaf_cfs_rq_list; | |
717 | struct task_group *tg; /* group that "owns" this runqueue */ | |
029632fb | 718 | |
30400039 JD |
719 | /* Locally cached copy of our task_group's idle value */ |
720 | int idle; | |
721 | ||
029632fb | 722 | #ifdef CONFIG_CFS_BANDWIDTH |
97fb7a0a | 723 | int runtime_enabled; |
97fb7a0a IM |
724 | s64 runtime_remaining; |
725 | ||
e2f3e35f VD |
726 | u64 throttled_pelt_idle; |
727 | #ifndef CONFIG_64BIT | |
728 | u64 throttled_pelt_idle_copy; | |
729 | #endif | |
97fb7a0a | 730 | u64 throttled_clock; |
64eaf507 CZ |
731 | u64 throttled_clock_pelt; |
732 | u64 throttled_clock_pelt_time; | |
677ea015 JD |
733 | u64 throttled_clock_self; |
734 | u64 throttled_clock_self_time; | |
97fb7a0a IM |
735 | int throttled; |
736 | int throttle_count; | |
737 | struct list_head throttled_list; | |
8ad075c2 | 738 | struct list_head throttled_csd_list; |
029632fb PZ |
739 | #endif /* CONFIG_CFS_BANDWIDTH */ |
740 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
741 | }; | |
742 | ||
f0e1a064 | 743 | #ifdef CONFIG_SCHED_CLASS_EXT |
81aae789 TH |
744 | /* scx_rq->flags, protected by the rq lock */ |
745 | enum scx_rq_flags { | |
60c27fb5 TH |
746 | /* |
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. | |
750 | */ | |
751 | SCX_RQ_ONLINE = 1 << 0, | |
f47a8189 | 752 | SCX_RQ_CAN_STOP_TICK = 1 << 1, |
a6250aa2 TH |
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, | |
3a9910b5 | 756 | SCX_RQ_CLK_VALID = 1 << 5, /* RQ clock is fresh and valid */ |
f47a8189 TH |
757 | |
758 | SCX_RQ_IN_WAKEUP = 1 << 16, | |
759 | SCX_RQ_IN_BALANCE = 1 << 17, | |
81aae789 TH |
760 | }; |
761 | ||
f0e1a064 TH |
762 | struct scx_rq { |
763 | struct scx_dispatch_q local_dsq; | |
764 | struct list_head runnable_list; /* runnable tasks on this rq */ | |
5b26f7b9 | 765 | struct list_head ddsp_deferred_locals; /* deferred ddsps from enq */ |
f0e1a064 TH |
766 | unsigned long ops_qseq; |
767 | u64 extra_enq_flags; /* see move_task_to_local_dsq() */ | |
768 | u32 nr_running; | |
d86adb4f | 769 | u32 cpuperf_target; /* [0, SCHED_CAPACITY_SCALE] */ |
245254f7 | 770 | bool cpu_released; |
3a9910b5 CM |
771 | u32 flags; |
772 | u64 clock; /* current per-rq clock -- see scx_bpf_now() */ | |
81aae789 TH |
773 | cpumask_var_t cpus_to_kick; |
774 | cpumask_var_t cpus_to_kick_if_idle; | |
775 | cpumask_var_t cpus_to_preempt; | |
90e55164 DV |
776 | cpumask_var_t cpus_to_wait; |
777 | unsigned long pnt_seq; | |
5b26f7b9 TH |
778 | struct balance_callback deferred_bal_cb; |
779 | struct irq_work deferred_irq_work; | |
81aae789 | 780 | struct irq_work kick_cpus_irq_work; |
f0e1a064 TH |
781 | }; |
782 | #endif /* CONFIG_SCHED_CLASS_EXT */ | |
783 | ||
029632fb PZ |
784 | static inline int rt_bandwidth_enabled(void) |
785 | { | |
786 | return sysctl_sched_rt_runtime >= 0; | |
787 | } | |
788 | ||
b6366f04 | 789 | /* RT IPI pull logic requires IRQ_WORK */ |
4bdced5c | 790 | #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP) |
b6366f04 SR |
791 | # define HAVE_RT_PUSH_IPI |
792 | #endif | |
793 | ||
029632fb PZ |
794 | /* Real-Time classes' related field in a runqueue: */ |
795 | struct rt_rq { | |
97fb7a0a IM |
796 | struct rt_prio_array active; |
797 | unsigned int rt_nr_running; | |
798 | unsigned int rr_nr_running; | |
029632fb PZ |
799 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
800 | struct { | |
97fb7a0a | 801 | int curr; /* highest queued rt task prio */ |
029632fb | 802 | #ifdef CONFIG_SMP |
97fb7a0a | 803 | int next; /* next highest */ |
029632fb PZ |
804 | #endif |
805 | } highest_prio; | |
806 | #endif | |
807 | #ifdef CONFIG_SMP | |
4475cd8b | 808 | bool overloaded; |
97fb7a0a | 809 | struct plist_head pushable_tasks; |
371bf427 | 810 | |
b6366f04 | 811 | #endif /* CONFIG_SMP */ |
97fb7a0a | 812 | int rt_queued; |
f4ebcbc0 | 813 | |
5f6bd380 | 814 | #ifdef CONFIG_RT_GROUP_SCHED |
97fb7a0a | 815 | int rt_throttled; |
0ab94c32 MK |
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 */ | |
029632fb | 818 | /* Nests inside the rq lock: */ |
97fb7a0a | 819 | raw_spinlock_t rt_runtime_lock; |
029632fb | 820 | |
e6fe3f42 | 821 | unsigned int rt_nr_boosted; |
029632fb | 822 | |
0ab94c32 | 823 | struct rq *rq; /* this is always top-level rq, cache? */ |
a5a25b32 MK |
824 | #endif |
825 | #ifdef CONFIG_CGROUP_SCHED | |
0ab94c32 | 826 | struct task_group *tg; /* this tg has "this" rt_rq on given CPU for runnable entities */ |
029632fb PZ |
827 | #endif |
828 | }; | |
829 | ||
296b2ffe VG |
830 | static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq) |
831 | { | |
832 | return rt_rq->rt_queued && rt_rq->rt_nr_running; | |
833 | } | |
834 | ||
aab03e05 DF |
835 | /* Deadline class' related fields in a runqueue */ |
836 | struct dl_rq { | |
837 | /* runqueue is an rbtree, ordered by deadline */ | |
97fb7a0a | 838 | struct rb_root_cached root; |
aab03e05 | 839 | |
e6fe3f42 | 840 | unsigned int dl_nr_running; |
1baca4ce JL |
841 | |
842 | #ifdef CONFIG_SMP | |
843 | /* | |
844 | * Deadline values of the currently executing and the | |
845 | * earliest ready task on this rq. Caching these facilitates | |
dfcb245e | 846 | * the decision whether or not a ready but not running task |
1baca4ce JL |
847 | * should migrate somewhere else. |
848 | */ | |
849 | struct { | |
97fb7a0a IM |
850 | u64 curr; |
851 | u64 next; | |
1baca4ce JL |
852 | } earliest_dl; |
853 | ||
4475cd8b | 854 | bool overloaded; |
1baca4ce JL |
855 | |
856 | /* | |
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. | |
860 | */ | |
97fb7a0a | 861 | struct rb_root_cached pushable_dl_tasks_root; |
332ac17e | 862 | #else |
97fb7a0a | 863 | struct dl_bw dl_bw; |
1baca4ce | 864 | #endif |
e36d8677 LA |
865 | /* |
866 | * "Active utilization" for this runqueue: increased when a | |
867 | * task wakes up (becomes TASK_RUNNING) and decreased when a | |
868 | * task blocks | |
869 | */ | |
97fb7a0a | 870 | u64 running_bw; |
4da3abce | 871 | |
8fd27231 LA |
872 | /* |
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). | |
880 | */ | |
97fb7a0a IM |
881 | u64 this_bw; |
882 | u64 extra_bw; | |
8fd27231 | 883 | |
6a9d623a VP |
884 | /* |
885 | * Maximum available bandwidth for reclaiming by SCHED_FLAG_RECLAIM | |
886 | * tasks of this rq. Used in calculation of reclaimable bandwidth(GRUB). | |
887 | */ | |
888 | u64 max_bw; | |
889 | ||
4da3abce LA |
890 | /* |
891 | * Inverse of the fraction of CPU utilization that can be reclaimed | |
892 | * by the GRUB algorithm. | |
893 | */ | |
97fb7a0a | 894 | u64 bw_ratio; |
aab03e05 DF |
895 | }; |
896 | ||
c0796298 | 897 | #ifdef CONFIG_FAIR_GROUP_SCHED |
127f6bf1 | 898 | |
c0796298 VG |
899 | /* An entity is a task if it doesn't "own" a runqueue */ |
900 | #define entity_is_task(se) (!se->my_q) | |
0dacee1b | 901 | |
9f683953 VG |
902 | static inline void se_update_runnable(struct sched_entity *se) |
903 | { | |
1a491044 VG |
904 | if (!entity_is_task(se)) |
905 | se->runnable_weight = se->my_q->h_nr_runnable; | |
9f683953 VG |
906 | } |
907 | ||
908 | static inline long se_runnable(struct sched_entity *se) | |
909 | { | |
fc1892be PZ |
910 | if (se->sched_delayed) |
911 | return false; | |
912 | ||
9f683953 VG |
913 | if (entity_is_task(se)) |
914 | return !!se->on_rq; | |
915 | else | |
916 | return se->runnable_weight; | |
917 | } | |
918 | ||
127f6bf1 IM |
919 | #else /* !CONFIG_FAIR_GROUP_SCHED: */ |
920 | ||
c0796298 | 921 | #define entity_is_task(se) 1 |
0dacee1b | 922 | |
127f6bf1 | 923 | static inline void se_update_runnable(struct sched_entity *se) { } |
9f683953 VG |
924 | |
925 | static inline long se_runnable(struct sched_entity *se) | |
926 | { | |
fc1892be PZ |
927 | if (se->sched_delayed) |
928 | return false; | |
929 | ||
9f683953 VG |
930 | return !!se->on_rq; |
931 | } | |
127f6bf1 IM |
932 | |
933 | #endif /* !CONFIG_FAIR_GROUP_SCHED */ | |
c0796298 | 934 | |
029632fb | 935 | #ifdef CONFIG_SMP |
c0796298 VG |
936 | /* |
937 | * XXX we want to get rid of these helpers and use the full load resolution. | |
938 | */ | |
939 | static inline long se_weight(struct sched_entity *se) | |
940 | { | |
941 | return scale_load_down(se->load.weight); | |
942 | } | |
943 | ||
029632fb | 944 | |
afe06efd TC |
945 | static inline bool sched_asym_prefer(int a, int b) |
946 | { | |
947 | return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b); | |
948 | } | |
949 | ||
6aa140fa QP |
950 | struct perf_domain { |
951 | struct em_perf_domain *em_pd; | |
952 | struct perf_domain *next; | |
953 | struct rcu_head rcu; | |
954 | }; | |
955 | ||
029632fb PZ |
956 | /* |
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 | |
97fb7a0a | 959 | * fully partitioning the member CPUs from any other cpuset. Whenever a new |
029632fb PZ |
960 | * exclusive cpuset is created, we also create and attach a new root-domain |
961 | * object. | |
962 | * | |
963 | */ | |
964 | struct root_domain { | |
97fb7a0a IM |
965 | atomic_t refcount; |
966 | atomic_t rto_count; | |
967 | struct rcu_head rcu; | |
968 | cpumask_var_t span; | |
969 | cpumask_var_t online; | |
029632fb | 970 | |
757ffdd7 VS |
971 | /* |
972 | * Indicate pullable load on at least one CPU, e.g: | |
973 | * - More than one runnable task | |
974 | * - Running task is misfit | |
975 | */ | |
4475cd8b | 976 | bool overloaded; |
4486edd1 | 977 | |
402de7fc | 978 | /* Indicate one or more CPUs over-utilized (tipping point) */ |
4475cd8b | 979 | bool overutilized; |
2802bf3c | 980 | |
1baca4ce JL |
981 | /* |
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). | |
984 | */ | |
97fb7a0a IM |
985 | cpumask_var_t dlo_mask; |
986 | atomic_t dlo_count; | |
987 | struct dl_bw dl_bw; | |
988 | struct cpudl cpudl; | |
1baca4ce | 989 | |
26762423 PL |
990 | /* |
991 | * Indicate whether a root_domain's dl_bw has been checked or | |
992 | * updated. It's monotonously increasing value. | |
993 | * | |
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. | |
996 | */ | |
45007c6f | 997 | u64 visit_cookie; |
26762423 | 998 | |
4bdced5c SRRH |
999 | #ifdef HAVE_RT_PUSH_IPI |
1000 | /* | |
1001 | * For IPI pull requests, loop across the rto_mask. | |
1002 | */ | |
97fb7a0a IM |
1003 | struct irq_work rto_push_work; |
1004 | raw_spinlock_t rto_lock; | |
4bdced5c | 1005 | /* These are only updated and read within rto_lock */ |
97fb7a0a IM |
1006 | int rto_loop; |
1007 | int rto_cpu; | |
4bdced5c | 1008 | /* These atomics are updated outside of a lock */ |
97fb7a0a IM |
1009 | atomic_t rto_loop_next; |
1010 | atomic_t rto_loop_start; | |
4bdced5c | 1011 | #endif |
029632fb PZ |
1012 | /* |
1013 | * The "RT overload" flag: it gets set if a CPU has more than | |
1014 | * one runnable RT task. | |
1015 | */ | |
97fb7a0a IM |
1016 | cpumask_var_t rto_mask; |
1017 | struct cpupri cpupri; | |
cd92bfd3 | 1018 | |
6aa140fa QP |
1019 | /* |
1020 | * NULL-terminated list of performance domains intersecting with the | |
1021 | * CPUs of the rd. Protected by RCU. | |
1022 | */ | |
7ba7319f | 1023 | struct perf_domain __rcu *pd; |
029632fb PZ |
1024 | }; |
1025 | ||
f2cb1360 | 1026 | extern void init_defrootdomain(void); |
8d5dc512 | 1027 | extern int sched_init_domains(const struct cpumask *cpu_map); |
f2cb1360 | 1028 | extern void rq_attach_root(struct rq *rq, struct root_domain *rd); |
364f5665 SRV |
1029 | extern void sched_get_rd(struct root_domain *rd); |
1030 | extern void sched_put_rd(struct root_domain *rd); | |
029632fb | 1031 | |
76cc4f91 | 1032 | static inline int get_rd_overloaded(struct root_domain *rd) |
caac6291 | 1033 | { |
dfb83ef7 | 1034 | return READ_ONCE(rd->overloaded); |
caac6291 SH |
1035 | } |
1036 | ||
76cc4f91 | 1037 | static inline void set_rd_overloaded(struct root_domain *rd, int status) |
caac6291 | 1038 | { |
76cc4f91 | 1039 | if (get_rd_overloaded(rd) != status) |
dfb83ef7 | 1040 | WRITE_ONCE(rd->overloaded, status); |
caac6291 SH |
1041 | } |
1042 | ||
4bdced5c SRRH |
1043 | #ifdef HAVE_RT_PUSH_IPI |
1044 | extern void rto_push_irq_work_func(struct irq_work *work); | |
1045 | #endif | |
029632fb PZ |
1046 | #endif /* CONFIG_SMP */ |
1047 | ||
69842cba PB |
1048 | #ifdef CONFIG_UCLAMP_TASK |
1049 | /* | |
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 | |
1053 | * | |
1054 | * Keep track of how many tasks are RUNNABLE for a given utilization | |
1055 | * clamp value. | |
1056 | */ | |
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); | |
1060 | }; | |
1061 | ||
1062 | /* | |
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 | |
1066 | * | |
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. | |
1070 | * | |
1071 | * There are up to UCLAMP_CNT possible different clamp values, currently there | |
1072 | * are only two: minimum utilization and maximum utilization. | |
1073 | * | |
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. | |
1079 | * | |
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. | |
1083 | */ | |
1084 | struct uclamp_rq { | |
1085 | unsigned int value; | |
1086 | struct uclamp_bucket bucket[UCLAMP_BUCKETS]; | |
1087 | }; | |
46609ce2 QY |
1088 | |
1089 | DECLARE_STATIC_KEY_FALSE(sched_uclamp_used); | |
69842cba PB |
1090 | #endif /* CONFIG_UCLAMP_TASK */ |
1091 | ||
029632fb PZ |
1092 | /* |
1093 | * This is the main, per-CPU runqueue data structure. | |
1094 | * | |
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. | |
1098 | */ | |
1099 | struct rq { | |
1100 | /* runqueue lock: */ | |
5cb9eaa3 | 1101 | raw_spinlock_t __lock; |
029632fb | 1102 | |
97fb7a0a | 1103 | unsigned int nr_running; |
0ec8aa00 | 1104 | #ifdef CONFIG_NUMA_BALANCING |
97fb7a0a IM |
1105 | unsigned int nr_numa_running; |
1106 | unsigned int nr_preferred_running; | |
a4739eca | 1107 | unsigned int numa_migrate_on; |
0ec8aa00 | 1108 | #endif |
3451d024 | 1109 | #ifdef CONFIG_NO_HZ_COMMON |
9fd81dd5 | 1110 | #ifdef CONFIG_SMP |
e022e0d3 | 1111 | unsigned long last_blocked_load_update_tick; |
f643ea22 | 1112 | unsigned int has_blocked_load; |
90b5363a | 1113 | call_single_data_t nohz_csd; |
9fd81dd5 | 1114 | #endif /* CONFIG_SMP */ |
00357f5e | 1115 | unsigned int nohz_tick_stopped; |
90b5363a | 1116 | atomic_t nohz_flags; |
9fd81dd5 | 1117 | #endif /* CONFIG_NO_HZ_COMMON */ |
dcdedb24 | 1118 | |
126c2092 PZ |
1119 | #ifdef CONFIG_SMP |
1120 | unsigned int ttwu_pending; | |
1121 | #endif | |
97fb7a0a | 1122 | u64 nr_switches; |
029632fb | 1123 | |
69842cba PB |
1124 | #ifdef CONFIG_UCLAMP_TASK |
1125 | /* Utilization clamp values based on CPU's RUNNABLE tasks */ | |
1126 | struct uclamp_rq uclamp[UCLAMP_CNT] ____cacheline_aligned; | |
e496187d PB |
1127 | unsigned int uclamp_flags; |
1128 | #define UCLAMP_FLAG_IDLE 0x01 | |
69842cba PB |
1129 | #endif |
1130 | ||
97fb7a0a IM |
1131 | struct cfs_rq cfs; |
1132 | struct rt_rq rt; | |
1133 | struct dl_rq dl; | |
f0e1a064 TH |
1134 | #ifdef CONFIG_SCHED_CLASS_EXT |
1135 | struct scx_rq scx; | |
1136 | #endif | |
029632fb | 1137 | |
557a6bfc PZ |
1138 | struct sched_dl_entity fair_server; |
1139 | ||
029632fb | 1140 | #ifdef CONFIG_FAIR_GROUP_SCHED |
97fb7a0a IM |
1141 | /* list of leaf cfs_rq on this CPU: */ |
1142 | struct list_head leaf_cfs_rq_list; | |
1143 | struct list_head *tmp_alone_branch; | |
a35b6466 PZ |
1144 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
1145 | ||
029632fb PZ |
1146 | /* |
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: | |
1151 | */ | |
e6fe3f42 | 1152 | unsigned int nr_uninterruptible; |
029632fb | 1153 | |
af0c8b2b PZ |
1154 | union { |
1155 | struct task_struct __rcu *donor; /* Scheduler context */ | |
1156 | struct task_struct __rcu *curr; /* Execution context */ | |
1157 | }; | |
bd9bbc96 | 1158 | struct sched_dl_entity *dl_server; |
97fb7a0a IM |
1159 | struct task_struct *idle; |
1160 | struct task_struct *stop; | |
1161 | unsigned long next_balance; | |
1162 | struct mm_struct *prev_mm; | |
029632fb | 1163 | |
97fb7a0a IM |
1164 | unsigned int clock_update_flags; |
1165 | u64 clock; | |
23127296 VG |
1166 | /* Ensure that all clocks are in the same cache line */ |
1167 | u64 clock_task ____cacheline_aligned; | |
1168 | u64 clock_pelt; | |
1169 | unsigned long lost_idle_time; | |
e2f3e35f VD |
1170 | u64 clock_pelt_idle; |
1171 | u64 clock_idle; | |
1172 | #ifndef CONFIG_64BIT | |
1173 | u64 clock_pelt_idle_copy; | |
1174 | u64 clock_idle_copy; | |
1175 | #endif | |
029632fb | 1176 | |
97fb7a0a | 1177 | atomic_t nr_iowait; |
029632fb | 1178 | |
c006fac5 PT |
1179 | u64 last_seen_need_resched_ns; |
1180 | int ticks_without_resched; | |
c006fac5 | 1181 | |
227a4aad MD |
1182 | #ifdef CONFIG_MEMBARRIER |
1183 | int membarrier_state; | |
1184 | #endif | |
1185 | ||
029632fb | 1186 | #ifdef CONFIG_SMP |
994aeb7a JFG |
1187 | struct root_domain *rd; |
1188 | struct sched_domain __rcu *sd; | |
97fb7a0a IM |
1189 | |
1190 | unsigned long cpu_capacity; | |
029632fb | 1191 | |
8e5bad7d | 1192 | struct balance_callback *balance_callback; |
029632fb | 1193 | |
19a1f5ec | 1194 | unsigned char nohz_idle_balance; |
97fb7a0a | 1195 | unsigned char idle_balance; |
e3fca9e7 | 1196 | |
3b1baa64 MR |
1197 | unsigned long misfit_task_load; |
1198 | ||
029632fb | 1199 | /* For active balancing */ |
97fb7a0a IM |
1200 | int active_balance; |
1201 | int push_cpu; | |
1202 | struct cpu_stop_work active_balance_work; | |
1203 | ||
1204 | /* CPU of this runqueue: */ | |
1205 | int cpu; | |
1206 | int online; | |
029632fb | 1207 | |
367456c7 PZ |
1208 | struct list_head cfs_tasks; |
1209 | ||
371bf427 | 1210 | struct sched_avg avg_rt; |
3727e0e1 | 1211 | struct sched_avg avg_dl; |
11d4afd4 | 1212 | #ifdef CONFIG_HAVE_SCHED_AVG_IRQ |
91c27493 | 1213 | struct sched_avg avg_irq; |
76504793 | 1214 | #endif |
d4dbc991 VG |
1215 | #ifdef CONFIG_SCHED_HW_PRESSURE |
1216 | struct sched_avg avg_hw; | |
91c27493 | 1217 | #endif |
97fb7a0a IM |
1218 | u64 idle_stamp; |
1219 | u64 avg_idle; | |
9bd721c5 JL |
1220 | |
1221 | /* This is used to determine avg_idle's max value */ | |
97fb7a0a | 1222 | u64 max_idle_balance_cost; |
f2469a1f TG |
1223 | |
1224 | #ifdef CONFIG_HOTPLUG_CPU | |
1225 | struct rcuwait hotplug_wait; | |
1226 | #endif | |
90b5363a | 1227 | #endif /* CONFIG_SMP */ |
029632fb PZ |
1228 | |
1229 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | |
97fb7a0a | 1230 | u64 prev_irq_time; |
ddae0ca2 | 1231 | u64 psi_irq_time; |
029632fb PZ |
1232 | #endif |
1233 | #ifdef CONFIG_PARAVIRT | |
97fb7a0a | 1234 | u64 prev_steal_time; |
029632fb PZ |
1235 | #endif |
1236 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | |
97fb7a0a | 1237 | u64 prev_steal_time_rq; |
029632fb PZ |
1238 | #endif |
1239 | ||
1240 | /* calc_load related fields */ | |
97fb7a0a IM |
1241 | unsigned long calc_load_update; |
1242 | long calc_load_active; | |
029632fb PZ |
1243 | |
1244 | #ifdef CONFIG_SCHED_HRTICK | |
1245 | #ifdef CONFIG_SMP | |
97fb7a0a | 1246 | call_single_data_t hrtick_csd; |
029632fb | 1247 | #endif |
97fb7a0a | 1248 | struct hrtimer hrtick_timer; |
127f6bf1 | 1249 | ktime_t hrtick_time; |
029632fb PZ |
1250 | #endif |
1251 | ||
1252 | #ifdef CONFIG_SCHEDSTATS | |
1253 | /* latency stats */ | |
97fb7a0a IM |
1254 | struct sched_info rq_sched_info; |
1255 | unsigned long long rq_cpu_time; | |
029632fb PZ |
1256 | |
1257 | /* sys_sched_yield() stats */ | |
97fb7a0a | 1258 | unsigned int yld_count; |
029632fb PZ |
1259 | |
1260 | /* schedule() stats */ | |
97fb7a0a IM |
1261 | unsigned int sched_count; |
1262 | unsigned int sched_goidle; | |
029632fb PZ |
1263 | |
1264 | /* try_to_wake_up() stats */ | |
97fb7a0a IM |
1265 | unsigned int ttwu_count; |
1266 | unsigned int ttwu_local; | |
029632fb PZ |
1267 | #endif |
1268 | ||
442bf3aa | 1269 | #ifdef CONFIG_CPU_IDLE |
402de7fc | 1270 | /* Must be inspected within a RCU lock section */ |
97fb7a0a | 1271 | struct cpuidle_state *idle_state; |
442bf3aa | 1272 | #endif |
3015ef4b | 1273 | |
74d862b6 | 1274 | #ifdef CONFIG_SMP |
3015ef4b TG |
1275 | unsigned int nr_pinned; |
1276 | #endif | |
a7c81556 PZ |
1277 | unsigned int push_busy; |
1278 | struct cpu_stop_work push_work; | |
9edeaea1 PZ |
1279 | |
1280 | #ifdef CONFIG_SCHED_CORE | |
1281 | /* per rq */ | |
1282 | struct rq *core; | |
539f6512 | 1283 | struct task_struct *core_pick; |
bd9bbc96 | 1284 | struct sched_dl_entity *core_dl_server; |
9edeaea1 | 1285 | unsigned int core_enabled; |
539f6512 | 1286 | unsigned int core_sched_seq; |
8a311c74 PZ |
1287 | struct rb_root core_tree; |
1288 | ||
3c474b32 | 1289 | /* shared state -- careful with sched_core_cpu_deactivate() */ |
8a311c74 | 1290 | unsigned int core_task_seq; |
539f6512 PZ |
1291 | unsigned int core_pick_seq; |
1292 | unsigned long core_cookie; | |
4feee7d1 | 1293 | unsigned int core_forceidle_count; |
c6047c2e | 1294 | unsigned int core_forceidle_seq; |
4feee7d1 JD |
1295 | unsigned int core_forceidle_occupation; |
1296 | u64 core_forceidle_start; | |
9edeaea1 | 1297 | #endif |
da019032 WL |
1298 | |
1299 | /* Scratch cpumask to be temporarily used under rq_lock */ | |
1300 | cpumask_var_t scratch_mask; | |
8ad075c2 JD |
1301 | |
1302 | #if defined(CONFIG_CFS_BANDWIDTH) && defined(CONFIG_SMP) | |
1303 | call_single_data_t cfsb_csd; | |
1304 | struct list_head cfsb_csd_list; | |
1305 | #endif | |
029632fb PZ |
1306 | }; |
1307 | ||
62478d99 VG |
1308 | #ifdef CONFIG_FAIR_GROUP_SCHED |
1309 | ||
1310 | /* CPU runqueue to which this cfs_rq is attached */ | |
1311 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) | |
1312 | { | |
1313 | return cfs_rq->rq; | |
1314 | } | |
1315 | ||
1316 | #else | |
1317 | ||
1318 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) | |
1319 | { | |
1320 | return container_of(cfs_rq, struct rq, cfs); | |
1321 | } | |
1322 | #endif | |
1323 | ||
029632fb PZ |
1324 | static inline int cpu_of(struct rq *rq) |
1325 | { | |
1326 | #ifdef CONFIG_SMP | |
1327 | return rq->cpu; | |
1328 | #else | |
1329 | return 0; | |
1330 | #endif | |
1331 | } | |
1332 | ||
127f6bf1 | 1333 | #define MDF_PUSH 0x01 |
a7c81556 PZ |
1334 | |
1335 | static inline bool is_migration_disabled(struct task_struct *p) | |
1336 | { | |
74d862b6 | 1337 | #ifdef CONFIG_SMP |
a7c81556 PZ |
1338 | return p->migration_disabled; |
1339 | #else | |
1340 | return false; | |
1341 | #endif | |
1342 | } | |
1b568f0a | 1343 | |
e705968d LS |
1344 | DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
1345 | ||
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) | |
1351 | ||
af0c8b2b PZ |
1352 | static inline void rq_set_donor(struct rq *rq, struct task_struct *t) |
1353 | { | |
1354 | /* Do nothing */ | |
1355 | } | |
1356 | ||
9edeaea1 | 1357 | #ifdef CONFIG_SCHED_CORE |
97886d9d | 1358 | static inline struct cpumask *sched_group_span(struct sched_group *sg); |
9edeaea1 PZ |
1359 | |
1360 | DECLARE_STATIC_KEY_FALSE(__sched_core_enabled); | |
1361 | ||
1362 | static inline bool sched_core_enabled(struct rq *rq) | |
1363 | { | |
1364 | return static_branch_unlikely(&__sched_core_enabled) && rq->core_enabled; | |
1365 | } | |
1366 | ||
1367 | static inline bool sched_core_disabled(void) | |
1368 | { | |
1369 | return !static_branch_unlikely(&__sched_core_enabled); | |
1370 | } | |
1371 | ||
9ef7e7e3 PZ |
1372 | /* |
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. | |
1375 | */ | |
9edeaea1 PZ |
1376 | static inline raw_spinlock_t *rq_lockp(struct rq *rq) |
1377 | { | |
1378 | if (sched_core_enabled(rq)) | |
1379 | return &rq->core->__lock; | |
1380 | ||
1381 | return &rq->__lock; | |
1382 | } | |
1383 | ||
9ef7e7e3 PZ |
1384 | static inline raw_spinlock_t *__rq_lockp(struct rq *rq) |
1385 | { | |
1386 | if (rq->core_enabled) | |
1387 | return &rq->core->__lock; | |
1388 | ||
1389 | return &rq->__lock; | |
1390 | } | |
1391 | ||
127f6bf1 IM |
1392 | extern bool |
1393 | cfs_prio_less(const struct task_struct *a, const struct task_struct *b, bool fi); | |
1394 | ||
1395 | extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi); | |
c6047c2e | 1396 | |
97886d9d AL |
1397 | /* |
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. | |
1402 | */ | |
1403 | static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p) | |
1404 | { | |
1405 | /* Ignore cookie match if core scheduler is not enabled on the CPU. */ | |
1406 | if (!sched_core_enabled(rq)) | |
1407 | return true; | |
1408 | ||
1409 | return rq->core->core_cookie == p->core_cookie; | |
1410 | } | |
1411 | ||
1412 | static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) | |
1413 | { | |
1414 | bool idle_core = true; | |
1415 | int cpu; | |
1416 | ||
1417 | /* Ignore cookie match if core scheduler is not enabled on the CPU. */ | |
1418 | if (!sched_core_enabled(rq)) | |
1419 | return true; | |
1420 | ||
1421 | for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) { | |
1422 | if (!available_idle_cpu(cpu)) { | |
1423 | idle_core = false; | |
1424 | break; | |
1425 | } | |
1426 | } | |
1427 | ||
1428 | /* | |
1429 | * A CPU in an idle core is always the best choice for tasks with | |
1430 | * cookies. | |
1431 | */ | |
1432 | return idle_core || rq->core->core_cookie == p->core_cookie; | |
1433 | } | |
1434 | ||
1435 | static inline bool sched_group_cookie_match(struct rq *rq, | |
1436 | struct task_struct *p, | |
1437 | struct sched_group *group) | |
1438 | { | |
1439 | int cpu; | |
1440 | ||
1441 | /* Ignore cookie match if core scheduler is not enabled on the CPU. */ | |
1442 | if (!sched_core_enabled(rq)) | |
1443 | return true; | |
1444 | ||
1445 | for_each_cpu_and(cpu, sched_group_span(group), p->cpus_ptr) { | |
e705968d | 1446 | if (sched_core_cookie_match(cpu_rq(cpu), p)) |
97886d9d AL |
1447 | return true; |
1448 | } | |
1449 | return false; | |
1450 | } | |
1451 | ||
6e33cad0 PZ |
1452 | static inline bool sched_core_enqueued(struct task_struct *p) |
1453 | { | |
1454 | return !RB_EMPTY_NODE(&p->core_node); | |
1455 | } | |
1456 | ||
1457 | extern void sched_core_enqueue(struct rq *rq, struct task_struct *p); | |
4feee7d1 | 1458 | extern void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags); |
6e33cad0 PZ |
1459 | |
1460 | extern void sched_core_get(void); | |
1461 | extern void sched_core_put(void); | |
1462 | ||
127f6bf1 | 1463 | #else /* !CONFIG_SCHED_CORE: */ |
9edeaea1 PZ |
1464 | |
1465 | static inline bool sched_core_enabled(struct rq *rq) | |
1466 | { | |
1467 | return false; | |
1468 | } | |
1469 | ||
d66f1b06 PZ |
1470 | static inline bool sched_core_disabled(void) |
1471 | { | |
1472 | return true; | |
1473 | } | |
1474 | ||
39d371b7 PZ |
1475 | static inline raw_spinlock_t *rq_lockp(struct rq *rq) |
1476 | { | |
5cb9eaa3 | 1477 | return &rq->__lock; |
39d371b7 PZ |
1478 | } |
1479 | ||
9ef7e7e3 PZ |
1480 | static inline raw_spinlock_t *__rq_lockp(struct rq *rq) |
1481 | { | |
1482 | return &rq->__lock; | |
1483 | } | |
1484 | ||
97886d9d AL |
1485 | static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p) |
1486 | { | |
1487 | return true; | |
1488 | } | |
1489 | ||
1490 | static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) | |
1491 | { | |
1492 | return true; | |
1493 | } | |
1494 | ||
1495 | static inline bool sched_group_cookie_match(struct rq *rq, | |
1496 | struct task_struct *p, | |
1497 | struct sched_group *group) | |
1498 | { | |
1499 | return true; | |
1500 | } | |
127f6bf1 IM |
1501 | |
1502 | #endif /* !CONFIG_SCHED_CORE */ | |
e34e0131 MK |
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) | |
1507 | { | |
1508 | return static_branch_unlikely(&rt_group_sched); | |
1509 | } | |
1510 | # else | |
1511 | DECLARE_STATIC_KEY_TRUE(rt_group_sched); | |
1512 | static inline bool rt_group_sched_enabled(void) | |
1513 | { | |
1514 | return static_branch_likely(&rt_group_sched); | |
1515 | } | |
1516 | # endif /* CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED */ | |
1517 | #else | |
1518 | # define rt_group_sched_enabled() false | |
1519 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
9edeaea1 | 1520 | |
39d371b7 PZ |
1521 | static inline void lockdep_assert_rq_held(struct rq *rq) |
1522 | { | |
9ef7e7e3 | 1523 | lockdep_assert_held(__rq_lockp(rq)); |
39d371b7 PZ |
1524 | } |
1525 | ||
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); | |
1529 | ||
1530 | static inline void raw_spin_rq_lock(struct rq *rq) | |
1531 | { | |
1532 | raw_spin_rq_lock_nested(rq, 0); | |
1533 | } | |
1534 | ||
1535 | static inline void raw_spin_rq_lock_irq(struct rq *rq) | |
1536 | { | |
1537 | local_irq_disable(); | |
1538 | raw_spin_rq_lock(rq); | |
1539 | } | |
1540 | ||
1541 | static inline void raw_spin_rq_unlock_irq(struct rq *rq) | |
1542 | { | |
1543 | raw_spin_rq_unlock(rq); | |
1544 | local_irq_enable(); | |
1545 | } | |
1546 | ||
1547 | static inline unsigned long _raw_spin_rq_lock_irqsave(struct rq *rq) | |
1548 | { | |
1549 | unsigned long flags; | |
127f6bf1 | 1550 | |
39d371b7 PZ |
1551 | local_irq_save(flags); |
1552 | raw_spin_rq_lock(rq); | |
127f6bf1 | 1553 | |
39d371b7 PZ |
1554 | return flags; |
1555 | } | |
1556 | ||
1557 | static inline void raw_spin_rq_unlock_irqrestore(struct rq *rq, unsigned long flags) | |
1558 | { | |
1559 | raw_spin_rq_unlock(rq); | |
1560 | local_irq_restore(flags); | |
1561 | } | |
1562 | ||
1563 | #define raw_spin_rq_lock_irqsave(rq, flags) \ | |
1564 | do { \ | |
1565 | flags = _raw_spin_rq_lock_irqsave(rq); \ | |
1566 | } while (0) | |
1567 | ||
1b568f0a | 1568 | #ifdef CONFIG_SCHED_SMT |
1b568f0a PZ |
1569 | extern void __update_idle_core(struct rq *rq); |
1570 | ||
1571 | static inline void update_idle_core(struct rq *rq) | |
1572 | { | |
1573 | if (static_branch_unlikely(&sched_smt_present)) | |
1574 | __update_idle_core(rq); | |
1575 | } | |
1576 | ||
1577 | #else | |
1578 | static inline void update_idle_core(struct rq *rq) { } | |
1579 | #endif | |
1580 | ||
8a311c74 | 1581 | #ifdef CONFIG_FAIR_GROUP_SCHED |
127f6bf1 | 1582 | |
8a311c74 PZ |
1583 | static inline struct task_struct *task_of(struct sched_entity *se) |
1584 | { | |
f7d2728c | 1585 | WARN_ON_ONCE(!entity_is_task(se)); |
8a311c74 PZ |
1586 | return container_of(se, struct task_struct, se); |
1587 | } | |
1588 | ||
1589 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) | |
1590 | { | |
1591 | return p->se.cfs_rq; | |
1592 | } | |
1593 | ||
1594 | /* runqueue on which this entity is (to be) queued */ | |
904cbab7 | 1595 | static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se) |
8a311c74 PZ |
1596 | { |
1597 | return se->cfs_rq; | |
1598 | } | |
1599 | ||
1600 | /* runqueue "owned" by this group */ | |
1601 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
1602 | { | |
1603 | return grp->my_q; | |
1604 | } | |
1605 | ||
127f6bf1 | 1606 | #else /* !CONFIG_FAIR_GROUP_SCHED: */ |
8a311c74 | 1607 | |
127f6bf1 | 1608 | #define task_of(_se) container_of(_se, struct task_struct, se) |
8a311c74 | 1609 | |
904cbab7 | 1610 | static inline struct cfs_rq *task_cfs_rq(const struct task_struct *p) |
8a311c74 PZ |
1611 | { |
1612 | return &task_rq(p)->cfs; | |
1613 | } | |
1614 | ||
904cbab7 | 1615 | static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se) |
8a311c74 | 1616 | { |
904cbab7 | 1617 | const struct task_struct *p = task_of(se); |
8a311c74 PZ |
1618 | struct rq *rq = task_rq(p); |
1619 | ||
1620 | return &rq->cfs; | |
1621 | } | |
1622 | ||
1623 | /* runqueue "owned" by this group */ | |
1624 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
1625 | { | |
1626 | return NULL; | |
1627 | } | |
127f6bf1 IM |
1628 | |
1629 | #endif /* !CONFIG_FAIR_GROUP_SCHED */ | |
8a311c74 | 1630 | |
1f351d7f JW |
1631 | extern void update_rq_clock(struct rq *rq); |
1632 | ||
cb42c9a3 MF |
1633 | /* |
1634 | * rq::clock_update_flags bits | |
1635 | * | |
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. | |
1639 | * | |
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. | |
1642 | * | |
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. | |
1645 | * | |
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, | |
1649 | * | |
1650 | * if (rq-clock_update_flags >= RQCF_UPDATED) | |
1651 | * | |
3b03706f | 1652 | * to check if %RQCF_UPDATED is set. It'll never be shifted more than |
cb42c9a3 MF |
1653 | * one position though, because the next rq_unpin_lock() will shift it |
1654 | * back. | |
1655 | */ | |
97fb7a0a IM |
1656 | #define RQCF_REQ_SKIP 0x01 |
1657 | #define RQCF_ACT_SKIP 0x02 | |
1658 | #define RQCF_UPDATED 0x04 | |
cb42c9a3 MF |
1659 | |
1660 | static inline void assert_clock_updated(struct rq *rq) | |
1661 | { | |
1662 | /* | |
1663 | * The only reason for not seeing a clock update since the | |
1664 | * last rq_pin_lock() is if we're currently skipping updates. | |
1665 | */ | |
f7d2728c | 1666 | WARN_ON_ONCE(rq->clock_update_flags < RQCF_ACT_SKIP); |
cb42c9a3 MF |
1667 | } |
1668 | ||
78becc27 FW |
1669 | static inline u64 rq_clock(struct rq *rq) |
1670 | { | |
5cb9eaa3 | 1671 | lockdep_assert_rq_held(rq); |
cb42c9a3 MF |
1672 | assert_clock_updated(rq); |
1673 | ||
78becc27 FW |
1674 | return rq->clock; |
1675 | } | |
1676 | ||
1677 | static inline u64 rq_clock_task(struct rq *rq) | |
1678 | { | |
5cb9eaa3 | 1679 | lockdep_assert_rq_held(rq); |
cb42c9a3 MF |
1680 | assert_clock_updated(rq); |
1681 | ||
78becc27 FW |
1682 | return rq->clock_task; |
1683 | } | |
1684 | ||
adcc8da8 | 1685 | static inline void rq_clock_skip_update(struct rq *rq) |
9edfbfed | 1686 | { |
5cb9eaa3 | 1687 | lockdep_assert_rq_held(rq); |
adcc8da8 DB |
1688 | rq->clock_update_flags |= RQCF_REQ_SKIP; |
1689 | } | |
1690 | ||
1691 | /* | |
595058b6 | 1692 | * See rt task throttling, which is the only time a skip |
3b03706f | 1693 | * request is canceled. |
adcc8da8 DB |
1694 | */ |
1695 | static inline void rq_clock_cancel_skipupdate(struct rq *rq) | |
1696 | { | |
5cb9eaa3 | 1697 | lockdep_assert_rq_held(rq); |
adcc8da8 | 1698 | rq->clock_update_flags &= ~RQCF_REQ_SKIP; |
9edfbfed PZ |
1699 | } |
1700 | ||
ebb83d84 HJ |
1701 | /* |
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. | |
1709 | */ | |
1710 | static inline void rq_clock_start_loop_update(struct rq *rq) | |
1711 | { | |
1712 | lockdep_assert_rq_held(rq); | |
f7d2728c | 1713 | WARN_ON_ONCE(rq->clock_update_flags & RQCF_ACT_SKIP); |
ebb83d84 HJ |
1714 | rq->clock_update_flags |= RQCF_ACT_SKIP; |
1715 | } | |
1716 | ||
1717 | static inline void rq_clock_stop_loop_update(struct rq *rq) | |
1718 | { | |
1719 | lockdep_assert_rq_held(rq); | |
1720 | rq->clock_update_flags &= ~RQCF_ACT_SKIP; | |
1721 | } | |
1722 | ||
d8ac8971 MF |
1723 | struct rq_flags { |
1724 | unsigned long flags; | |
1725 | struct pin_cookie cookie; | |
cb42c9a3 MF |
1726 | /* |
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(). | |
1730 | */ | |
1731 | unsigned int clock_update_flags; | |
d8ac8971 MF |
1732 | }; |
1733 | ||
8e5bad7d | 1734 | extern struct balance_callback balance_push_callback; |
ae792702 | 1735 | |
ea9b2626 CM |
1736 | #ifdef CONFIG_SCHED_CLASS_EXT |
1737 | extern const struct sched_class ext_sched_class; | |
1738 | ||
a50c365f | 1739 | DECLARE_STATIC_KEY_FALSE(__scx_enabled); /* SCX BPF scheduler loaded */ |
ea9b2626 CM |
1740 | DECLARE_STATIC_KEY_FALSE(__scx_switched_all); /* all fair class tasks on SCX */ |
1741 | ||
a50c365f | 1742 | #define scx_enabled() static_branch_unlikely(&__scx_enabled) |
ea9b2626 | 1743 | #define scx_switched_all() static_branch_unlikely(&__scx_switched_all) |
3a9910b5 CM |
1744 | |
1745 | static inline void scx_rq_clock_update(struct rq *rq, u64 clock) | |
1746 | { | |
1747 | if (!scx_enabled()) | |
1748 | return; | |
1749 | WRITE_ONCE(rq->scx.clock, clock); | |
1750 | smp_store_release(&rq->scx.flags, rq->scx.flags | SCX_RQ_CLK_VALID); | |
1751 | } | |
1752 | ||
1753 | static inline void scx_rq_clock_invalidate(struct rq *rq) | |
1754 | { | |
1755 | if (!scx_enabled()) | |
1756 | return; | |
1757 | WRITE_ONCE(rq->scx.flags, rq->scx.flags & ~SCX_RQ_CLK_VALID); | |
1758 | } | |
1759 | ||
ea9b2626 CM |
1760 | #else /* !CONFIG_SCHED_CLASS_EXT */ |
1761 | #define scx_enabled() false | |
1762 | #define scx_switched_all() false | |
3a9910b5 CM |
1763 | |
1764 | static inline void scx_rq_clock_update(struct rq *rq, u64 clock) {} | |
1765 | static inline void scx_rq_clock_invalidate(struct rq *rq) {} | |
ea9b2626 CM |
1766 | #endif /* !CONFIG_SCHED_CLASS_EXT */ |
1767 | ||
58877d34 PZ |
1768 | /* |
1769 | * Lockdep annotation that avoids accidental unlocks; it's like a | |
1770 | * sticky/continuous lockdep_assert_held(). | |
1771 | * | |
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'. | |
1775 | * | |
1776 | * Also see Documentation/locking/lockdep-design.rst. | |
1777 | */ | |
d8ac8971 MF |
1778 | static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf) |
1779 | { | |
9ef7e7e3 | 1780 | rf->cookie = lockdep_pin_lock(__rq_lockp(rq)); |
cb42c9a3 | 1781 | |
cb42c9a3 MF |
1782 | rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); |
1783 | rf->clock_update_flags = 0; | |
dd5bdaf2 | 1784 | #ifdef CONFIG_SMP |
f7d2728c | 1785 | WARN_ON_ONCE(rq->balance_callback && rq->balance_callback != &balance_push_callback); |
565790d2 | 1786 | #endif |
d8ac8971 MF |
1787 | } |
1788 | ||
1789 | static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf) | |
1790 | { | |
cb42c9a3 MF |
1791 | if (rq->clock_update_flags > RQCF_ACT_SKIP) |
1792 | rf->clock_update_flags = RQCF_UPDATED; | |
dd5bdaf2 | 1793 | |
3a9910b5 | 1794 | scx_rq_clock_invalidate(rq); |
9ef7e7e3 | 1795 | lockdep_unpin_lock(__rq_lockp(rq), rf->cookie); |
d8ac8971 MF |
1796 | } |
1797 | ||
1798 | static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf) | |
1799 | { | |
9ef7e7e3 | 1800 | lockdep_repin_lock(__rq_lockp(rq), rf->cookie); |
cb42c9a3 | 1801 | |
cb42c9a3 MF |
1802 | /* |
1803 | * Restore the value we stashed in @rf for this pin context. | |
1804 | */ | |
1805 | rq->clock_update_flags |= rf->clock_update_flags; | |
d8ac8971 MF |
1806 | } |
1807 | ||
127f6bf1 | 1808 | extern |
1f351d7f JW |
1809 | struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
1810 | __acquires(rq->lock); | |
1811 | ||
127f6bf1 | 1812 | extern |
1f351d7f JW |
1813 | struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
1814 | __acquires(p->pi_lock) | |
1815 | __acquires(rq->lock); | |
1816 | ||
1817 | static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf) | |
1818 | __releases(rq->lock) | |
1819 | { | |
1820 | rq_unpin_lock(rq, rf); | |
5cb9eaa3 | 1821 | raw_spin_rq_unlock(rq); |
1f351d7f JW |
1822 | } |
1823 | ||
1824 | static inline void | |
1825 | task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf) | |
1826 | __releases(rq->lock) | |
1827 | __releases(p->pi_lock) | |
1828 | { | |
1829 | rq_unpin_lock(rq, rf); | |
5cb9eaa3 | 1830 | raw_spin_rq_unlock(rq); |
1f351d7f JW |
1831 | raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); |
1832 | } | |
1833 | ||
94b548a1 PZ |
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) | |
1838 | ||
127f6bf1 | 1839 | static inline void rq_lock_irqsave(struct rq *rq, struct rq_flags *rf) |
1f351d7f JW |
1840 | __acquires(rq->lock) |
1841 | { | |
5cb9eaa3 | 1842 | raw_spin_rq_lock_irqsave(rq, rf->flags); |
1f351d7f JW |
1843 | rq_pin_lock(rq, rf); |
1844 | } | |
1845 | ||
127f6bf1 | 1846 | static inline void rq_lock_irq(struct rq *rq, struct rq_flags *rf) |
1f351d7f JW |
1847 | __acquires(rq->lock) |
1848 | { | |
5cb9eaa3 | 1849 | raw_spin_rq_lock_irq(rq); |
1f351d7f JW |
1850 | rq_pin_lock(rq, rf); |
1851 | } | |
1852 | ||
127f6bf1 | 1853 | static inline void rq_lock(struct rq *rq, struct rq_flags *rf) |
1f351d7f JW |
1854 | __acquires(rq->lock) |
1855 | { | |
5cb9eaa3 | 1856 | raw_spin_rq_lock(rq); |
1f351d7f JW |
1857 | rq_pin_lock(rq, rf); |
1858 | } | |
1859 | ||
127f6bf1 | 1860 | static inline void rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf) |
1f351d7f JW |
1861 | __releases(rq->lock) |
1862 | { | |
1863 | rq_unpin_lock(rq, rf); | |
5cb9eaa3 | 1864 | raw_spin_rq_unlock_irqrestore(rq, rf->flags); |
1f351d7f JW |
1865 | } |
1866 | ||
127f6bf1 | 1867 | static inline void rq_unlock_irq(struct rq *rq, struct rq_flags *rf) |
1f351d7f JW |
1868 | __releases(rq->lock) |
1869 | { | |
1870 | rq_unpin_lock(rq, rf); | |
5cb9eaa3 | 1871 | raw_spin_rq_unlock_irq(rq); |
1f351d7f JW |
1872 | } |
1873 | ||
127f6bf1 | 1874 | static inline void rq_unlock(struct rq *rq, struct rq_flags *rf) |
1f351d7f JW |
1875 | __releases(rq->lock) |
1876 | { | |
1877 | rq_unpin_lock(rq, rf); | |
5cb9eaa3 | 1878 | raw_spin_rq_unlock(rq); |
1f351d7f JW |
1879 | } |
1880 | ||
4eb054f9 PZ |
1881 | DEFINE_LOCK_GUARD_1(rq_lock, struct rq, |
1882 | rq_lock(_T->lock, &_T->rf), | |
1883 | rq_unlock(_T->lock, &_T->rf), | |
1884 | struct rq_flags rf) | |
1885 | ||
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), | |
1889 | struct rq_flags rf) | |
1890 | ||
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), | |
1894 | struct rq_flags rf) | |
1895 | ||
127f6bf1 | 1896 | static inline struct rq *this_rq_lock_irq(struct rq_flags *rf) |
246b3b33 JW |
1897 | __acquires(rq->lock) |
1898 | { | |
1899 | struct rq *rq; | |
1900 | ||
1901 | local_irq_disable(); | |
1902 | rq = this_rq(); | |
1903 | rq_lock(rq, rf); | |
127f6bf1 | 1904 | |
246b3b33 JW |
1905 | return rq; |
1906 | } | |
1907 | ||
9942f79b | 1908 | #ifdef CONFIG_NUMA |
127f6bf1 | 1909 | |
e3fe70b1 RR |
1910 | enum numa_topology_type { |
1911 | NUMA_DIRECT, | |
1912 | NUMA_GLUELESS_MESH, | |
1913 | NUMA_BACKPLANE, | |
1914 | }; | |
127f6bf1 | 1915 | |
e3fe70b1 | 1916 | extern enum numa_topology_type sched_numa_topology_type; |
9942f79b RR |
1917 | extern int sched_max_numa_distance; |
1918 | extern bool find_numa_distance(int distance); | |
0fb3978b HY |
1919 | extern void sched_init_numa(int offline_node); |
1920 | extern void sched_update_numa(int cpu, bool online); | |
f2cb1360 IM |
1921 | extern void sched_domains_numa_masks_set(unsigned int cpu); |
1922 | extern void sched_domains_numa_masks_clear(unsigned int cpu); | |
e0e8d491 | 1923 | extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu); |
127f6bf1 IM |
1924 | |
1925 | #else /* !CONFIG_NUMA: */ | |
1926 | ||
0fb3978b HY |
1927 | static inline void sched_init_numa(int offline_node) { } |
1928 | static inline void sched_update_numa(int cpu, bool online) { } | |
f2cb1360 IM |
1929 | static inline void sched_domains_numa_masks_set(unsigned int cpu) { } |
1930 | static inline void sched_domains_numa_masks_clear(unsigned int cpu) { } | |
127f6bf1 | 1931 | |
e0e8d491 WL |
1932 | static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu) |
1933 | { | |
1934 | return nr_cpu_ids; | |
1935 | } | |
127f6bf1 IM |
1936 | |
1937 | #endif /* !CONFIG_NUMA */ | |
f2cb1360 | 1938 | |
f809ca9a | 1939 | #ifdef CONFIG_NUMA_BALANCING |
127f6bf1 | 1940 | |
44dba3d5 IM |
1941 | /* The regions in numa_faults array from task_struct */ |
1942 | enum numa_faults_stats { | |
1943 | NUMA_MEM = 0, | |
1944 | NUMA_CPU, | |
1945 | NUMA_MEMBUF, | |
1946 | NUMA_CPUBUF | |
1947 | }; | |
127f6bf1 | 1948 | |
0ec8aa00 | 1949 | extern void sched_setnuma(struct task_struct *p, int node); |
e6628d5b | 1950 | extern int migrate_task_to(struct task_struct *p, int cpu); |
0ad4e3df SD |
1951 | extern int migrate_swap(struct task_struct *p, struct task_struct *t, |
1952 | int cpu, int scpu); | |
13784475 | 1953 | extern void init_numa_balancing(unsigned long clone_flags, struct task_struct *p); |
127f6bf1 IM |
1954 | |
1955 | #else /* !CONFIG_NUMA_BALANCING: */ | |
1956 | ||
13784475 MG |
1957 | static inline void |
1958 | init_numa_balancing(unsigned long clone_flags, struct task_struct *p) | |
1959 | { | |
1960 | } | |
127f6bf1 IM |
1961 | |
1962 | #endif /* !CONFIG_NUMA_BALANCING */ | |
f809ca9a | 1963 | |
518cd623 PZ |
1964 | #ifdef CONFIG_SMP |
1965 | ||
e3fca9e7 PZ |
1966 | static inline void |
1967 | queue_balance_callback(struct rq *rq, | |
8e5bad7d | 1968 | struct balance_callback *head, |
e3fca9e7 PZ |
1969 | void (*func)(struct rq *rq)) |
1970 | { | |
5cb9eaa3 | 1971 | lockdep_assert_rq_held(rq); |
e3fca9e7 | 1972 | |
04193d59 PZ |
1973 | /* |
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. | |
1977 | */ | |
ae792702 | 1978 | if (unlikely(head->next || rq->balance_callback == &balance_push_callback)) |
e3fca9e7 PZ |
1979 | return; |
1980 | ||
8e5bad7d | 1981 | head->func = func; |
e3fca9e7 PZ |
1982 | head->next = rq->balance_callback; |
1983 | rq->balance_callback = head; | |
1984 | } | |
1985 | ||
029632fb | 1986 | #define rcu_dereference_check_sched_domain(p) \ |
127f6bf1 | 1987 | rcu_dereference_check((p), lockdep_is_held(&sched_domains_mutex)) |
029632fb PZ |
1988 | |
1989 | /* | |
1990 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. | |
337e9b07 | 1991 | * See destroy_sched_domains: call_rcu for details. |
029632fb PZ |
1992 | * |
1993 | * The domain tree of any CPU may only be accessed from within | |
1994 | * preempt-disabled sections. | |
1995 | */ | |
1996 | #define for_each_domain(cpu, __sd) \ | |
518cd623 PZ |
1997 | for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \ |
1998 | __sd; __sd = __sd->parent) | |
029632fb | 1999 | |
40b4d3dc RN |
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> | |
2004 | 0; | |
2005 | #undef SD_FLAG | |
2006 | ||
518cd623 PZ |
2007 | /** |
2008 | * highest_flag_domain - Return highest sched_domain containing flag. | |
97fb7a0a | 2009 | * @cpu: The CPU whose highest level of sched domain is to |
518cd623 PZ |
2010 | * be returned. |
2011 | * @flag: The flag to check for the highest sched_domain | |
97fb7a0a | 2012 | * for the given CPU. |
518cd623 | 2013 | * |
40b4d3dc RN |
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. | |
518cd623 PZ |
2016 | */ |
2017 | static inline struct sched_domain *highest_flag_domain(int cpu, int flag) | |
2018 | { | |
2019 | struct sched_domain *sd, *hsd = NULL; | |
2020 | ||
2021 | for_each_domain(cpu, sd) { | |
40b4d3dc RN |
2022 | if (sd->flags & flag) { |
2023 | hsd = sd; | |
2024 | continue; | |
2025 | } | |
2026 | ||
2027 | /* | |
2028 | * Stop the search if @flag is known to be shared at lower | |
2029 | * levels. It will not be found further up. | |
2030 | */ | |
2031 | if (flag & SD_SHARED_CHILD_MASK) | |
518cd623 | 2032 | break; |
518cd623 PZ |
2033 | } |
2034 | ||
2035 | return hsd; | |
2036 | } | |
2037 | ||
fb13c7ee MG |
2038 | static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) |
2039 | { | |
2040 | struct sched_domain *sd; | |
2041 | ||
2042 | for_each_domain(cpu, sd) { | |
2043 | if (sd->flags & flag) | |
2044 | break; | |
2045 | } | |
2046 | ||
2047 | return sd; | |
2048 | } | |
2049 | ||
994aeb7a | 2050 | DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc); |
7d9ffa89 | 2051 | DECLARE_PER_CPU(int, sd_llc_size); |
518cd623 | 2052 | DECLARE_PER_CPU(int, sd_llc_id); |
b95303e0 | 2053 | DECLARE_PER_CPU(int, sd_share_id); |
994aeb7a JFG |
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); | |
127f6bf1 | 2058 | |
df054e84 | 2059 | extern struct static_key_false sched_asym_cpucapacity; |
8881e163 | 2060 | extern struct static_key_false sched_cluster_active; |
518cd623 | 2061 | |
740cf8a7 DE |
2062 | static __always_inline bool sched_asym_cpucap_active(void) |
2063 | { | |
2064 | return static_branch_unlikely(&sched_asym_cpucapacity); | |
2065 | } | |
2066 | ||
63b2ca30 | 2067 | struct sched_group_capacity { |
97fb7a0a | 2068 | atomic_t ref; |
5e6521ea | 2069 | /* |
172895e6 | 2070 | * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity |
63b2ca30 | 2071 | * for a single CPU. |
5e6521ea | 2072 | */ |
97fb7a0a IM |
2073 | unsigned long capacity; |
2074 | unsigned long min_capacity; /* Min per-CPU capacity in group */ | |
e3d6d0cb | 2075 | unsigned long max_capacity; /* Max per-CPU capacity in group */ |
97fb7a0a IM |
2076 | unsigned long next_update; |
2077 | int imbalance; /* XXX unrelated to capacity but shared group state */ | |
5e6521ea | 2078 | |
97fb7a0a | 2079 | int id; |
005f874d | 2080 | |
eba9f082 | 2081 | unsigned long cpumask[]; /* Balance mask */ |
5e6521ea LZ |
2082 | }; |
2083 | ||
2084 | struct sched_group { | |
97fb7a0a IM |
2085 | struct sched_group *next; /* Must be a circular list */ |
2086 | atomic_t ref; | |
5e6521ea | 2087 | |
97fb7a0a | 2088 | unsigned int group_weight; |
d24cb0d9 | 2089 | unsigned int cores; |
63b2ca30 | 2090 | struct sched_group_capacity *sgc; |
97fb7a0a | 2091 | int asym_prefer_cpu; /* CPU of highest priority in group */ |
16d364ba | 2092 | int flags; |
5e6521ea LZ |
2093 | |
2094 | /* | |
2095 | * The CPUs this group covers. | |
2096 | * | |
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) | |
2100 | */ | |
04f5c362 | 2101 | unsigned long cpumask[]; |
5e6521ea LZ |
2102 | }; |
2103 | ||
ae4df9d6 | 2104 | static inline struct cpumask *sched_group_span(struct sched_group *sg) |
5e6521ea LZ |
2105 | { |
2106 | return to_cpumask(sg->cpumask); | |
2107 | } | |
2108 | ||
2109 | /* | |
e5c14b1f | 2110 | * See build_balance_mask(). |
5e6521ea | 2111 | */ |
e5c14b1f | 2112 | static inline struct cpumask *group_balance_mask(struct sched_group *sg) |
5e6521ea | 2113 | { |
63b2ca30 | 2114 | return to_cpumask(sg->sgc->cpumask); |
5e6521ea LZ |
2115 | } |
2116 | ||
c1174876 PZ |
2117 | extern int group_balance_cpu(struct sched_group *sg); |
2118 | ||
127f6bf1 IM |
2119 | extern void update_sched_domain_debugfs(void); |
2120 | extern void dirty_sched_domain_sysctl(int cpu); | |
3866e845 | 2121 | |
8a99b683 | 2122 | extern int sched_update_scaling(void); |
8f9ea86f WL |
2123 | |
2124 | static inline const struct cpumask *task_user_cpus(struct task_struct *p) | |
2125 | { | |
2126 | if (!p->user_cpus_ptr) | |
2127 | return cpu_possible_mask; /* &init_task.cpus_mask */ | |
2128 | return p->user_cpus_ptr; | |
2129 | } | |
127f6bf1 | 2130 | |
d664e399 | 2131 | #endif /* CONFIG_SMP */ |
029632fb | 2132 | |
029632fb PZ |
2133 | #ifdef CONFIG_CGROUP_SCHED |
2134 | ||
2135 | /* | |
2136 | * Return the group to which this tasks belongs. | |
2137 | * | |
8af01f56 TH |
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. | |
8323f26c PZ |
2141 | * |
2142 | * The same is true for autogroup's p->signal->autogroup->tg, the autogroup | |
2143 | * core changes this before calling sched_move_task(). | |
2144 | * | |
2145 | * Instead we use a 'copy' which is updated from sched_move_task() while | |
2146 | * holding both task_struct::pi_lock and rq::lock. | |
029632fb PZ |
2147 | */ |
2148 | static inline struct task_group *task_group(struct task_struct *p) | |
2149 | { | |
8323f26c | 2150 | return p->sched_task_group; |
029632fb PZ |
2151 | } |
2152 | ||
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) | |
2155 | { | |
2156 | #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED) | |
2157 | struct task_group *tg = task_group(p); | |
2158 | #endif | |
2159 | ||
2160 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
ad936d86 | 2161 | set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]); |
029632fb PZ |
2162 | p->se.cfs_rq = tg->cfs_rq[cpu]; |
2163 | p->se.parent = tg->se[cpu]; | |
78b6b157 | 2164 | p->se.depth = tg->se[cpu] ? tg->se[cpu]->depth + 1 : 0; |
029632fb PZ |
2165 | #endif |
2166 | ||
2167 | #ifdef CONFIG_RT_GROUP_SCHED | |
61d3164f MK |
2168 | /* |
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() | |
2171 | * Clobbers tg(!) | |
2172 | */ | |
2173 | if (!rt_group_sched_enabled()) | |
2174 | tg = &root_task_group; | |
029632fb PZ |
2175 | p->rt.rt_rq = tg->rt_rq[cpu]; |
2176 | p->rt.parent = tg->rt_se[cpu]; | |
2177 | #endif | |
2178 | } | |
2179 | ||
127f6bf1 | 2180 | #else /* !CONFIG_CGROUP_SCHED: */ |
029632fb PZ |
2181 | |
2182 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } | |
127f6bf1 | 2183 | |
029632fb PZ |
2184 | static inline struct task_group *task_group(struct task_struct *p) |
2185 | { | |
2186 | return NULL; | |
2187 | } | |
2188 | ||
127f6bf1 | 2189 | #endif /* !CONFIG_CGROUP_SCHED */ |
029632fb PZ |
2190 | |
2191 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) | |
2192 | { | |
2193 | set_task_rq(p, cpu); | |
2194 | #ifdef CONFIG_SMP | |
2195 | /* | |
2196 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be | |
dfcb245e | 2197 | * successfully executed on another CPU. We must ensure that updates of |
029632fb PZ |
2198 | * per-task data have been completed by this moment. |
2199 | */ | |
2200 | smp_wmb(); | |
c546951d | 2201 | WRITE_ONCE(task_thread_info(p)->cpu, cpu); |
ac66f547 | 2202 | p->wake_cpu = cpu; |
029632fb PZ |
2203 | #endif |
2204 | } | |
2205 | ||
2206 | /* | |
57903f72 | 2207 | * Tunables: |
029632fb | 2208 | */ |
029632fb | 2209 | |
029632fb PZ |
2210 | #define SCHED_FEAT(name, enabled) \ |
2211 | __SCHED_FEAT_##name , | |
2212 | ||
2213 | enum { | |
391e43da | 2214 | #include "features.h" |
f8b6d1cc | 2215 | __SCHED_FEAT_NR, |
029632fb PZ |
2216 | }; |
2217 | ||
2218 | #undef SCHED_FEAT | |
2219 | ||
765cc3a4 PB |
2220 | /* |
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. | |
2223 | */ | |
57903f72 | 2224 | extern __read_mostly unsigned int sysctl_sched_features; |
765cc3a4 | 2225 | |
a73f863a | 2226 | #ifdef CONFIG_JUMP_LABEL |
127f6bf1 | 2227 | |
f8b6d1cc | 2228 | #define SCHED_FEAT(name, enabled) \ |
c5905afb | 2229 | static __always_inline bool static_branch_##name(struct static_key *key) \ |
f8b6d1cc | 2230 | { \ |
6e76ea8a | 2231 | return static_key_##enabled(key); \ |
f8b6d1cc PZ |
2232 | } |
2233 | ||
2234 | #include "features.h" | |
f8b6d1cc PZ |
2235 | #undef SCHED_FEAT |
2236 | ||
c5905afb | 2237 | extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; |
f8b6d1cc | 2238 | #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x])) |
765cc3a4 | 2239 | |
127f6bf1 | 2240 | #else /* !CONFIG_JUMP_LABEL: */ |
a73f863a JL |
2241 | |
2242 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) | |
2243 | ||
127f6bf1 | 2244 | #endif /* !CONFIG_JUMP_LABEL */ |
a73f863a | 2245 | |
2a595721 | 2246 | extern struct static_key_false sched_numa_balancing; |
cb251765 | 2247 | extern struct static_key_false sched_schedstats; |
cbee9f88 | 2248 | |
029632fb PZ |
2249 | static inline u64 global_rt_period(void) |
2250 | { | |
2251 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; | |
2252 | } | |
2253 | ||
2254 | static inline u64 global_rt_runtime(void) | |
2255 | { | |
2256 | if (sysctl_sched_rt_runtime < 0) | |
2257 | return RUNTIME_INF; | |
2258 | ||
2259 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; | |
2260 | } | |
2261 | ||
af0c8b2b PZ |
2262 | /* |
2263 | * Is p the current execution context? | |
2264 | */ | |
029632fb PZ |
2265 | static inline int task_current(struct rq *rq, struct task_struct *p) |
2266 | { | |
2267 | return rq->curr == p; | |
2268 | } | |
2269 | ||
af0c8b2b PZ |
2270 | /* |
2271 | * Is p the current scheduling context? | |
2272 | * | |
2273 | * Note that it might be the current execution context at the same time if | |
2274 | * rq->curr == rq->donor == p. | |
2275 | */ | |
2276 | static inline int task_current_donor(struct rq *rq, struct task_struct *p) | |
2277 | { | |
2278 | return rq->donor == p; | |
2279 | } | |
2280 | ||
0b9d46fc | 2281 | static inline int task_on_cpu(struct rq *rq, struct task_struct *p) |
029632fb PZ |
2282 | { |
2283 | #ifdef CONFIG_SMP | |
2284 | return p->on_cpu; | |
2285 | #else | |
2286 | return task_current(rq, p); | |
2287 | #endif | |
2288 | } | |
2289 | ||
da0c1e65 KT |
2290 | static inline int task_on_rq_queued(struct task_struct *p) |
2291 | { | |
59297e20 | 2292 | return READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED; |
da0c1e65 | 2293 | } |
029632fb | 2294 | |
cca26e80 KT |
2295 | static inline int task_on_rq_migrating(struct task_struct *p) |
2296 | { | |
c546951d | 2297 | return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING; |
cca26e80 KT |
2298 | } |
2299 | ||
17770579 | 2300 | /* Wake flags. The first three directly map to some SD flag value */ |
127f6bf1 IM |
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 */ | |
17770579 | 2304 | |
127f6bf1 IM |
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. */ | |
f207dc2d | 2308 | #define WF_RQ_SELECTED 0x80 /* ->select_task_rq() was called */ |
17770579 VS |
2309 | |
2310 | #ifdef CONFIG_SMP | |
2311 | static_assert(WF_EXEC == SD_BALANCE_EXEC); | |
2312 | static_assert(WF_FORK == SD_BALANCE_FORK); | |
2313 | static_assert(WF_TTWU == SD_BALANCE_WAKE); | |
2314 | #endif | |
b13095f0 | 2315 | |
029632fb PZ |
2316 | /* |
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 | |
2322 | * slice expiry etc. | |
2323 | */ | |
2324 | ||
97fb7a0a IM |
2325 | #define WEIGHT_IDLEPRIO 3 |
2326 | #define WMULT_IDLEPRIO 1431655765 | |
029632fb | 2327 | |
97fb7a0a IM |
2328 | extern const int sched_prio_to_weight[40]; |
2329 | extern const u32 sched_prio_to_wmult[40]; | |
029632fb | 2330 | |
ff77e468 PZ |
2331 | /* |
2332 | * {de,en}queue flags: | |
2333 | * | |
2334 | * DEQUEUE_SLEEP - task is no longer runnable | |
2335 | * ENQUEUE_WAKEUP - task just became runnable | |
2336 | * | |
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. | |
2340 | * | |
2341 | * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location | |
2342 | * in the runqueue. | |
2343 | * | |
2f7a0f58 PZ |
2344 | * NOCLOCK - skip the update_rq_clock() (avoids double updates) |
2345 | * | |
2346 | * MIGRATION - p->on_rq == TASK_ON_RQ_MIGRATING (used for DEADLINE) | |
2347 | * | |
ff77e468 PZ |
2348 | * ENQUEUE_HEAD - place at front of runqueue (tail if not specified) |
2349 | * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline) | |
59efa0ba | 2350 | * ENQUEUE_MIGRATED - the task was migrated during wakeup |
f207dc2d | 2351 | * ENQUEUE_RQ_SELECTED - ->select_task_rq() was called |
ff77e468 PZ |
2352 | * |
2353 | */ | |
2354 | ||
e1459a50 | 2355 | #define DEQUEUE_SLEEP 0x01 /* Matches ENQUEUE_WAKEUP */ |
97fb7a0a IM |
2356 | #define DEQUEUE_SAVE 0x02 /* Matches ENQUEUE_RESTORE */ |
2357 | #define DEQUEUE_MOVE 0x04 /* Matches ENQUEUE_MOVE */ | |
2358 | #define DEQUEUE_NOCLOCK 0x08 /* Matches ENQUEUE_NOCLOCK */ | |
e1459a50 | 2359 | #define DEQUEUE_SPECIAL 0x10 |
2f7a0f58 | 2360 | #define DEQUEUE_MIGRATING 0x100 /* Matches ENQUEUE_MIGRATING */ |
abc158c8 | 2361 | #define DEQUEUE_DELAYED 0x200 /* Matches ENQUEUE_DELAYED */ |
ff77e468 | 2362 | |
1de64443 | 2363 | #define ENQUEUE_WAKEUP 0x01 |
ff77e468 PZ |
2364 | #define ENQUEUE_RESTORE 0x02 |
2365 | #define ENQUEUE_MOVE 0x04 | |
0a67d1ee | 2366 | #define ENQUEUE_NOCLOCK 0x08 |
ff77e468 | 2367 | |
0a67d1ee PZ |
2368 | #define ENQUEUE_HEAD 0x10 |
2369 | #define ENQUEUE_REPLENISH 0x20 | |
c82ba9fa | 2370 | #ifdef CONFIG_SMP |
0a67d1ee | 2371 | #define ENQUEUE_MIGRATED 0x40 |
c82ba9fa | 2372 | #else |
59efa0ba | 2373 | #define ENQUEUE_MIGRATED 0x00 |
c82ba9fa | 2374 | #endif |
d07f09a1 | 2375 | #define ENQUEUE_INITIAL 0x80 |
2f7a0f58 | 2376 | #define ENQUEUE_MIGRATING 0x100 |
abc158c8 | 2377 | #define ENQUEUE_DELAYED 0x200 |
f207dc2d | 2378 | #define ENQUEUE_RQ_SELECTED 0x400 |
c82ba9fa | 2379 | |
37e117c0 PZ |
2380 | #define RETRY_TASK ((void *)-1UL) |
2381 | ||
713a2e21 | 2382 | struct affinity_context { |
127f6bf1 IM |
2383 | const struct cpumask *new_mask; |
2384 | struct cpumask *user_mask; | |
2385 | unsigned int flags; | |
713a2e21 WL |
2386 | }; |
2387 | ||
5d69eca5 PZ |
2388 | extern s64 update_curr_common(struct rq *rq); |
2389 | ||
c82ba9fa | 2390 | struct sched_class { |
c82ba9fa | 2391 | |
69842cba PB |
2392 | #ifdef CONFIG_UCLAMP_TASK |
2393 | int uclamp_enabled; | |
2394 | #endif | |
2395 | ||
c82ba9fa | 2396 | void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags); |
863ccdbb | 2397 | bool (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags); |
97fb7a0a | 2398 | void (*yield_task) (struct rq *rq); |
0900acf2 | 2399 | bool (*yield_to_task)(struct rq *rq, struct task_struct *p); |
c82ba9fa | 2400 | |
e23edc86 | 2401 | void (*wakeup_preempt)(struct rq *rq, struct task_struct *p, int flags); |
c82ba9fa | 2402 | |
a735d43c | 2403 | int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf); |
fd03c5b8 PZ |
2404 | struct task_struct *(*pick_task)(struct rq *rq); |
2405 | /* | |
2406 | * Optional! When implemented pick_next_task() should be equivalent to: | |
2407 | * | |
2408 | * next = pick_task(); | |
2409 | * if (next) { | |
2410 | * put_prev_task(prev); | |
2411 | * set_next_task_first(next); | |
2412 | * } | |
2413 | */ | |
2414 | struct task_struct *(*pick_next_task)(struct rq *rq, struct task_struct *prev); | |
98c2f700 | 2415 | |
b2d70222 | 2416 | void (*put_prev_task)(struct rq *rq, struct task_struct *p, struct task_struct *next); |
a0e813f2 | 2417 | void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first); |
c82ba9fa LZ |
2418 | |
2419 | #ifdef CONFIG_SMP | |
3aef1551 | 2420 | int (*select_task_rq)(struct task_struct *p, int task_cpu, int flags); |
21f56ffe | 2421 | |
1327237a | 2422 | void (*migrate_task_rq)(struct task_struct *p, int new_cpu); |
c82ba9fa | 2423 | |
97fb7a0a | 2424 | void (*task_woken)(struct rq *this_rq, struct task_struct *task); |
c82ba9fa | 2425 | |
713a2e21 | 2426 | void (*set_cpus_allowed)(struct task_struct *p, struct affinity_context *ctx); |
c82ba9fa LZ |
2427 | |
2428 | void (*rq_online)(struct rq *rq); | |
2429 | void (*rq_offline)(struct rq *rq); | |
a7c81556 PZ |
2430 | |
2431 | struct rq *(*find_lock_rq)(struct task_struct *p, struct rq *rq); | |
c82ba9fa LZ |
2432 | #endif |
2433 | ||
97fb7a0a IM |
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); | |
c82ba9fa | 2437 | |
67dfa1b7 KT |
2438 | /* |
2439 | * The switched_from() call is allowed to drop rq->lock, therefore we | |
3b03706f | 2440 | * cannot assume the switched_from/switched_to pair is serialized by |
67dfa1b7 KT |
2441 | * rq->lock. They are however serialized by p->pi_lock. |
2442 | */ | |
d8c7bc2e | 2443 | void (*switching_to) (struct rq *this_rq, struct task_struct *task); |
97fb7a0a IM |
2444 | void (*switched_from)(struct rq *this_rq, struct task_struct *task); |
2445 | void (*switched_to) (struct rq *this_rq, struct task_struct *task); | |
e83edbf8 | 2446 | void (*reweight_task)(struct rq *this_rq, struct task_struct *task, |
7b9f6c86 | 2447 | const struct load_weight *lw); |
c82ba9fa | 2448 | void (*prio_changed) (struct rq *this_rq, struct task_struct *task, |
97fb7a0a | 2449 | int oldprio); |
c82ba9fa | 2450 | |
97fb7a0a IM |
2451 | unsigned int (*get_rr_interval)(struct rq *rq, |
2452 | struct task_struct *task); | |
c82ba9fa | 2453 | |
97fb7a0a | 2454 | void (*update_curr)(struct rq *rq); |
6e998916 | 2455 | |
c82ba9fa | 2456 | #ifdef CONFIG_FAIR_GROUP_SCHED |
39c42611 | 2457 | void (*task_change_group)(struct task_struct *p); |
c82ba9fa | 2458 | #endif |
530bfad1 HJ |
2459 | |
2460 | #ifdef CONFIG_SCHED_CORE | |
2461 | int (*task_is_throttled)(struct task_struct *p, int cpu); | |
2462 | #endif | |
43c31ac0 | 2463 | }; |
029632fb | 2464 | |
3f1d2a31 PZ |
2465 | static inline void put_prev_task(struct rq *rq, struct task_struct *prev) |
2466 | { | |
af0c8b2b | 2467 | WARN_ON_ONCE(rq->donor != prev); |
b2d70222 | 2468 | prev->sched_class->put_prev_task(rq, prev, NULL); |
3f1d2a31 PZ |
2469 | } |
2470 | ||
03b7fad1 | 2471 | static inline void set_next_task(struct rq *rq, struct task_struct *next) |
b2bf6c31 | 2472 | { |
a0e813f2 | 2473 | next->sched_class->set_next_task(rq, next, false); |
b2bf6c31 PZ |
2474 | } |
2475 | ||
bd9bbc96 PZ |
2476 | static inline void |
2477 | __put_prev_set_next_dl_server(struct rq *rq, | |
2478 | struct task_struct *prev, | |
2479 | struct task_struct *next) | |
2480 | { | |
2481 | prev->dl_server = NULL; | |
2482 | next->dl_server = rq->dl_server; | |
2483 | rq->dl_server = NULL; | |
2484 | } | |
2485 | ||
436f3eed PZ |
2486 | static inline void put_prev_set_next_task(struct rq *rq, |
2487 | struct task_struct *prev, | |
2488 | struct task_struct *next) | |
7d2180d9 | 2489 | { |
436f3eed PZ |
2490 | WARN_ON_ONCE(rq->curr != prev); |
2491 | ||
bd9bbc96 PZ |
2492 | __put_prev_set_next_dl_server(rq, prev, next); |
2493 | ||
436f3eed PZ |
2494 | if (next == prev) |
2495 | return; | |
2496 | ||
b2d70222 | 2497 | prev->sched_class->put_prev_task(rq, prev, next); |
7d2180d9 PZ |
2498 | next->sched_class->set_next_task(rq, next, true); |
2499 | } | |
43c31ac0 PZ |
2500 | |
2501 | /* | |
2502 | * Helper to define a sched_class instance; each one is placed in a separate | |
2503 | * section which is ordered by the linker script: | |
2504 | * | |
2505 | * include/asm-generic/vmlinux.lds.h | |
2506 | * | |
546a3fee PZ |
2507 | * *CAREFUL* they are laid out in *REVERSE* order!!! |
2508 | * | |
43c31ac0 PZ |
2509 | * Also enforce alignment on the instance, not the type, to guarantee layout. |
2510 | */ | |
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") | |
2515 | ||
c3a340f7 | 2516 | /* Defined in include/asm-generic/vmlinux.lds.h */ |
546a3fee PZ |
2517 | extern struct sched_class __sched_class_highest[]; |
2518 | extern struct sched_class __sched_class_lowest[]; | |
6e2df058 | 2519 | |
e196c908 TH |
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; | |
2525 | ||
e196c908 TH |
2526 | /* |
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. | |
2529 | */ | |
2530 | static inline const struct sched_class *next_active_class(const struct sched_class *class) | |
2531 | { | |
2532 | class++; | |
2533 | #ifdef CONFIG_SCHED_CLASS_EXT | |
2534 | if (scx_switched_all() && class == &fair_sched_class) | |
2535 | class++; | |
2536 | if (!scx_enabled() && class == &ext_sched_class) | |
2537 | class++; | |
2538 | #endif | |
2539 | return class; | |
2540 | } | |
2541 | ||
6e2df058 | 2542 | #define for_class_range(class, _from, _to) \ |
546a3fee | 2543 | for (class = (_from); class < (_to); class++) |
6e2df058 | 2544 | |
029632fb | 2545 | #define for_each_class(class) \ |
546a3fee PZ |
2546 | for_class_range(class, __sched_class_highest, __sched_class_lowest) |
2547 | ||
e196c908 TH |
2548 | #define for_active_class_range(class, _from, _to) \ |
2549 | for (class = (_from); class != (_to); class = next_active_class(class)) | |
029632fb | 2550 | |
e196c908 TH |
2551 | #define for_each_active_class(class) \ |
2552 | for_active_class_range(class, __sched_class_highest, __sched_class_lowest) | |
2553 | ||
2554 | #define sched_class_above(_a, _b) ((_a) < (_b)) | |
029632fb | 2555 | |
6e2df058 PZ |
2556 | static inline bool sched_stop_runnable(struct rq *rq) |
2557 | { | |
2558 | return rq->stop && task_on_rq_queued(rq->stop); | |
2559 | } | |
2560 | ||
2561 | static inline bool sched_dl_runnable(struct rq *rq) | |
2562 | { | |
2563 | return rq->dl.dl_nr_running > 0; | |
2564 | } | |
2565 | ||
2566 | static inline bool sched_rt_runnable(struct rq *rq) | |
2567 | { | |
2568 | return rq->rt.rt_queued > 0; | |
2569 | } | |
2570 | ||
2571 | static inline bool sched_fair_runnable(struct rq *rq) | |
2572 | { | |
736c55a0 | 2573 | return rq->cfs.nr_queued > 0; |
6e2df058 | 2574 | } |
029632fb | 2575 | |
5d7d6056 | 2576 | extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf); |
fd03c5b8 | 2577 | extern struct task_struct *pick_task_idle(struct rq *rq); |
5d7d6056 | 2578 | |
af449901 PZ |
2579 | #define SCA_CHECK 0x01 |
2580 | #define SCA_MIGRATE_DISABLE 0x02 | |
2581 | #define SCA_MIGRATE_ENABLE 0x04 | |
07ec77a1 | 2582 | #define SCA_USER 0x08 |
af449901 | 2583 | |
029632fb PZ |
2584 | #ifdef CONFIG_SMP |
2585 | ||
63b2ca30 | 2586 | extern void update_group_capacity(struct sched_domain *sd, int cpu); |
b719203b | 2587 | |
983be062 | 2588 | extern void sched_balance_trigger(struct rq *rq); |
029632fb | 2589 | |
04746ed8 | 2590 | extern int __set_cpus_allowed_ptr(struct task_struct *p, struct affinity_context *ctx); |
713a2e21 | 2591 | extern void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx); |
c5b28038 | 2592 | |
2c390dda TH |
2593 | static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu) |
2594 | { | |
2595 | /* When not in the task's cpumask, no point in looking further. */ | |
2596 | if (!cpumask_test_cpu(cpu, p->cpus_ptr)) | |
2597 | return false; | |
2598 | ||
2599 | /* Can @cpu run a user thread? */ | |
2600 | if (!(p->flags & PF_KTHREAD) && !task_cpu_possible(cpu, p)) | |
2601 | return false; | |
2602 | ||
2603 | return true; | |
2604 | } | |
2605 | ||
04746ed8 IM |
2606 | static inline cpumask_t *alloc_user_cpus_ptr(int node) |
2607 | { | |
2608 | /* | |
2609 | * See do_set_cpus_allowed() above for the rcu_head usage. | |
2610 | */ | |
2611 | int size = max_t(int, cpumask_size(), sizeof(struct rcu_head)); | |
2612 | ||
2613 | return kmalloc_node(size, GFP_KERNEL, node); | |
2614 | } | |
2615 | ||
a7c81556 PZ |
2616 | static inline struct task_struct *get_push_task(struct rq *rq) |
2617 | { | |
af0c8b2b | 2618 | struct task_struct *p = rq->donor; |
a7c81556 | 2619 | |
5cb9eaa3 | 2620 | lockdep_assert_rq_held(rq); |
a7c81556 PZ |
2621 | |
2622 | if (rq->push_busy) | |
2623 | return NULL; | |
2624 | ||
2625 | if (p->nr_cpus_allowed == 1) | |
2626 | return NULL; | |
2627 | ||
e681dcba SAS |
2628 | if (p->migration_disabled) |
2629 | return NULL; | |
2630 | ||
a7c81556 PZ |
2631 | rq->push_busy = true; |
2632 | return get_task_struct(p); | |
2633 | } | |
2634 | ||
2635 | extern int push_cpu_stop(void *arg); | |
c5b28038 | 2636 | |
04746ed8 IM |
2637 | #else /* !CONFIG_SMP: */ |
2638 | ||
2c390dda TH |
2639 | static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu) |
2640 | { | |
2641 | return true; | |
2642 | } | |
2643 | ||
04746ed8 IM |
2644 | static inline int __set_cpus_allowed_ptr(struct task_struct *p, |
2645 | struct affinity_context *ctx) | |
2646 | { | |
2647 | return set_cpus_allowed_ptr(p, ctx->new_mask); | |
2648 | } | |
2649 | ||
2650 | static inline cpumask_t *alloc_user_cpus_ptr(int node) | |
2651 | { | |
2652 | return NULL; | |
2653 | } | |
2654 | ||
2655 | #endif /* !CONFIG_SMP */ | |
029632fb | 2656 | |
442bf3aa | 2657 | #ifdef CONFIG_CPU_IDLE |
127f6bf1 | 2658 | |
442bf3aa DL |
2659 | static inline void idle_set_state(struct rq *rq, |
2660 | struct cpuidle_state *idle_state) | |
2661 | { | |
2662 | rq->idle_state = idle_state; | |
2663 | } | |
2664 | ||
2665 | static inline struct cpuidle_state *idle_get_state(struct rq *rq) | |
2666 | { | |
f7d2728c | 2667 | WARN_ON_ONCE(!rcu_read_lock_held()); |
97fb7a0a | 2668 | |
442bf3aa DL |
2669 | return rq->idle_state; |
2670 | } | |
127f6bf1 IM |
2671 | |
2672 | #else /* !CONFIG_CPU_IDLE: */ | |
2673 | ||
442bf3aa DL |
2674 | static inline void idle_set_state(struct rq *rq, |
2675 | struct cpuidle_state *idle_state) | |
2676 | { | |
2677 | } | |
2678 | ||
2679 | static inline struct cpuidle_state *idle_get_state(struct rq *rq) | |
2680 | { | |
2681 | return NULL; | |
2682 | } | |
127f6bf1 IM |
2683 | |
2684 | #endif /* !CONFIG_CPU_IDLE */ | |
442bf3aa | 2685 | |
8663effb | 2686 | extern void schedule_idle(void); |
22dc02f8 | 2687 | asmlinkage void schedule_user(void); |
8663effb | 2688 | |
029632fb PZ |
2689 | extern void sysrq_sched_debug_show(void); |
2690 | extern void sched_init_granularity(void); | |
2691 | extern void update_max_interval(void); | |
1baca4ce JL |
2692 | |
2693 | extern void init_sched_dl_class(void); | |
029632fb PZ |
2694 | extern void init_sched_rt_class(void); |
2695 | extern void init_sched_fair_class(void); | |
2696 | ||
8875125e | 2697 | extern void resched_curr(struct rq *rq); |
7c70cb94 | 2698 | extern void resched_curr_lazy(struct rq *rq); |
029632fb PZ |
2699 | extern void resched_cpu(int cpu); |
2700 | ||
029632fb | 2701 | extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime); |
d664e399 | 2702 | extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq); |
029632fb | 2703 | |
9e07d45c | 2704 | extern void init_dl_entity(struct sched_dl_entity *dl_se); |
aab03e05 | 2705 | |
97fb7a0a IM |
2706 | #define BW_SHIFT 20 |
2707 | #define BW_UNIT (1 << BW_SHIFT) | |
2708 | #define RATIO_SHIFT 8 | |
d505b8af HC |
2709 | #define MAX_BW_BITS (64 - BW_SHIFT) |
2710 | #define MAX_BW ((1ULL << MAX_BW_BITS) - 1) | |
127f6bf1 IM |
2711 | |
2712 | extern unsigned long to_ratio(u64 period, u64 runtime); | |
332ac17e | 2713 | |
540247fb | 2714 | extern void init_entity_runnable_average(struct sched_entity *se); |
d0fe0b9c | 2715 | extern void post_init_entity_util_avg(struct task_struct *p); |
a75cdaa9 | 2716 | |
76d92ac3 FW |
2717 | #ifdef CONFIG_NO_HZ_FULL |
2718 | extern bool sched_can_stop_tick(struct rq *rq); | |
d84b3131 | 2719 | extern int __init sched_tick_offload_init(void); |
76d92ac3 FW |
2720 | |
2721 | /* | |
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. | |
2725 | */ | |
2726 | static inline void sched_update_tick_dependency(struct rq *rq) | |
2727 | { | |
21a6ee14 | 2728 | int cpu = cpu_of(rq); |
76d92ac3 FW |
2729 | |
2730 | if (!tick_nohz_full_cpu(cpu)) | |
2731 | return; | |
2732 | ||
2733 | if (sched_can_stop_tick(rq)) | |
2734 | tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED); | |
2735 | else | |
2736 | tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); | |
2737 | } | |
127f6bf1 | 2738 | #else /* !CONFIG_NO_HZ_FULL: */ |
d84b3131 | 2739 | static inline int sched_tick_offload_init(void) { return 0; } |
76d92ac3 | 2740 | static inline void sched_update_tick_dependency(struct rq *rq) { } |
127f6bf1 | 2741 | #endif /* !CONFIG_NO_HZ_FULL */ |
76d92ac3 | 2742 | |
72465447 | 2743 | static inline void add_nr_running(struct rq *rq, unsigned count) |
029632fb | 2744 | { |
72465447 KT |
2745 | unsigned prev_nr = rq->nr_running; |
2746 | ||
2747 | rq->nr_running = prev_nr + count; | |
9d246053 PA |
2748 | if (trace_sched_update_nr_running_tp_enabled()) { |
2749 | call_trace_sched_update_nr_running(rq, count); | |
2750 | } | |
9f3660c2 | 2751 | |
4486edd1 | 2752 | #ifdef CONFIG_SMP |
4475cd8b IM |
2753 | if (prev_nr < 2 && rq->nr_running >= 2) |
2754 | set_rd_overloaded(rq->rd, 1); | |
3e184501 | 2755 | #endif |
76d92ac3 FW |
2756 | |
2757 | sched_update_tick_dependency(rq); | |
029632fb PZ |
2758 | } |
2759 | ||
72465447 | 2760 | static inline void sub_nr_running(struct rq *rq, unsigned count) |
029632fb | 2761 | { |
72465447 | 2762 | rq->nr_running -= count; |
9d246053 | 2763 | if (trace_sched_update_nr_running_tp_enabled()) { |
a1bd0685 | 2764 | call_trace_sched_update_nr_running(rq, -count); |
9d246053 PA |
2765 | } |
2766 | ||
76d92ac3 FW |
2767 | /* Check if we still need preemption */ |
2768 | sched_update_tick_dependency(rq); | |
029632fb PZ |
2769 | } |
2770 | ||
e8901061 PZ |
2771 | static inline void __block_task(struct rq *rq, struct task_struct *p) |
2772 | { | |
e8901061 PZ |
2773 | if (p->sched_contributes_to_load) |
2774 | rq->nr_uninterruptible++; | |
2775 | ||
2776 | if (p->in_iowait) { | |
2777 | atomic_inc(&rq->nr_iowait); | |
2778 | delayacct_blkio_start(); | |
2779 | } | |
b55945c5 PZ |
2780 | |
2781 | ASSERT_EXCLUSIVE_WRITER(p->on_rq); | |
2782 | ||
2783 | /* | |
2784 | * The moment this write goes through, ttwu() can swoop in and migrate | |
2785 | * this task, rendering our rq->__lock ineffective. | |
2786 | * | |
2787 | * __schedule() try_to_wake_up() | |
2788 | * LOCK rq->__lock LOCK p->pi_lock | |
2789 | * pick_next_task() | |
2790 | * pick_next_task_fair() | |
2791 | * pick_next_entity() | |
2792 | * dequeue_entities() | |
2793 | * __block_task() | |
2794 | * RELEASE p->on_rq = 0 if (p->on_rq && ...) | |
2795 | * break; | |
2796 | * | |
2797 | * ACQUIRE (after ctrl-dep) | |
2798 | * | |
2799 | * cpu = select_task_rq(); | |
2800 | * set_task_cpu(p, cpu); | |
2801 | * ttwu_queue() | |
2802 | * ttwu_do_activate() | |
2803 | * LOCK rq->__lock | |
2804 | * activate_task() | |
2805 | * STORE p->on_rq = 1 | |
2806 | * UNLOCK rq->__lock | |
2807 | * | |
2808 | * Callers must ensure to not reference @p after this -- we no longer | |
2809 | * own it. | |
2810 | */ | |
2811 | smp_store_release(&p->on_rq, 0); | |
e8901061 PZ |
2812 | } |
2813 | ||
029632fb PZ |
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); | |
2816 | ||
e23edc86 | 2817 | extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags); |
029632fb | 2818 | |
c59862f8 | 2819 | #ifdef CONFIG_PREEMPT_RT |
127f6bf1 | 2820 | # define SCHED_NR_MIGRATE_BREAK 8 |
c59862f8 | 2821 | #else |
127f6bf1 | 2822 | # define SCHED_NR_MIGRATE_BREAK 32 |
c59862f8 VG |
2823 | #endif |
2824 | ||
57903f72 IM |
2825 | extern __read_mostly unsigned int sysctl_sched_nr_migrate; |
2826 | extern __read_mostly unsigned int sysctl_sched_migration_cost; | |
029632fb | 2827 | |
e4ec3318 | 2828 | extern unsigned int sysctl_sched_base_slice; |
147f3efa | 2829 | |
18765447 HL |
2830 | extern int sysctl_resched_latency_warn_ms; |
2831 | extern int sysctl_resched_latency_warn_once; | |
2832 | ||
2833 | extern unsigned int sysctl_sched_tunable_scaling; | |
2834 | ||
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; | |
33024536 | 2839 | extern unsigned int sysctl_numa_balancing_hot_threshold; |
18765447 | 2840 | |
029632fb PZ |
2841 | #ifdef CONFIG_SCHED_HRTICK |
2842 | ||
2843 | /* | |
2844 | * Use hrtick when: | |
2845 | * - enabled by features | |
2846 | * - hrtimer is actually high res | |
2847 | */ | |
2848 | static inline int hrtick_enabled(struct rq *rq) | |
2849 | { | |
029632fb PZ |
2850 | if (!cpu_active(cpu_of(rq))) |
2851 | return 0; | |
2852 | return hrtimer_is_hres_active(&rq->hrtick_timer); | |
2853 | } | |
2854 | ||
e0ee463c JL |
2855 | static inline int hrtick_enabled_fair(struct rq *rq) |
2856 | { | |
2857 | if (!sched_feat(HRTICK)) | |
2858 | return 0; | |
2859 | return hrtick_enabled(rq); | |
2860 | } | |
2861 | ||
2862 | static inline int hrtick_enabled_dl(struct rq *rq) | |
2863 | { | |
2864 | if (!sched_feat(HRTICK_DL)) | |
2865 | return 0; | |
2866 | return hrtick_enabled(rq); | |
2867 | } | |
2868 | ||
127f6bf1 | 2869 | extern void hrtick_start(struct rq *rq, u64 delay); |
029632fb | 2870 | |
127f6bf1 | 2871 | #else /* !CONFIG_SCHED_HRTICK: */ |
b39e66ea | 2872 | |
e0ee463c JL |
2873 | static inline int hrtick_enabled_fair(struct rq *rq) |
2874 | { | |
2875 | return 0; | |
2876 | } | |
2877 | ||
2878 | static inline int hrtick_enabled_dl(struct rq *rq) | |
2879 | { | |
2880 | return 0; | |
2881 | } | |
2882 | ||
b39e66ea MG |
2883 | static inline int hrtick_enabled(struct rq *rq) |
2884 | { | |
2885 | return 0; | |
2886 | } | |
2887 | ||
127f6bf1 | 2888 | #endif /* !CONFIG_SCHED_HRTICK */ |
029632fb | 2889 | |
1567c3e3 | 2890 | #ifndef arch_scale_freq_tick |
127f6bf1 | 2891 | static __always_inline void arch_scale_freq_tick(void) { } |
1567c3e3 GG |
2892 | #endif |
2893 | ||
dfbca41f | 2894 | #ifndef arch_scale_freq_capacity |
f4470cdf VS |
2895 | /** |
2896 | * arch_scale_freq_capacity - get the frequency scale factor of a given CPU. | |
2897 | * @cpu: the CPU in question. | |
2898 | * | |
2899 | * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e. | |
2900 | * | |
2901 | * f_curr | |
2902 | * ------ * SCHED_CAPACITY_SCALE | |
2903 | * f_max | |
2904 | */ | |
dfbca41f | 2905 | static __always_inline |
7673c8a4 | 2906 | unsigned long arch_scale_freq_capacity(int cpu) |
dfbca41f PZ |
2907 | { |
2908 | return SCHED_CAPACITY_SCALE; | |
2909 | } | |
2910 | #endif | |
b5b4860d | 2911 | |
2679a837 HJ |
2912 | /* |
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. | |
2917 | */ | |
2918 | static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2) | |
2919 | { | |
2920 | rq1->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); | |
2921 | /* rq1 == rq2 for !CONFIG_SMP, so just clear RQCF_UPDATED once. */ | |
2922 | #ifdef CONFIG_SMP | |
2923 | rq2->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); | |
2924 | #endif | |
2925 | } | |
d66f1b06 | 2926 | |
127f6bf1 IM |
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; \ | |
5bb76f1d PZ |
2931 | _lock; return _t; } |
2932 | ||
029632fb | 2933 | #ifdef CONFIG_SMP |
029632fb | 2934 | |
d66f1b06 PZ |
2935 | static inline bool rq_order_less(struct rq *rq1, struct rq *rq2) |
2936 | { | |
9edeaea1 PZ |
2937 | #ifdef CONFIG_SCHED_CORE |
2938 | /* | |
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. | |
2941 | * | |
2942 | * Notably: | |
2943 | * | |
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 | |
2946 | * | |
2947 | * when only cpu-id is considered. | |
2948 | */ | |
2949 | if (rq1->core->cpu < rq2->core->cpu) | |
2950 | return true; | |
2951 | if (rq1->core->cpu > rq2->core->cpu) | |
2952 | return false; | |
2953 | ||
2954 | /* | |
2955 | * __sched_core_flip() relies on SMT having cpu-id lock order. | |
2956 | */ | |
2957 | #endif | |
d66f1b06 PZ |
2958 | return rq1->cpu < rq2->cpu; |
2959 | } | |
2960 | ||
2961 | extern void double_rq_lock(struct rq *rq1, struct rq *rq2); | |
2962 | ||
2963 | #ifdef CONFIG_PREEMPTION | |
029632fb PZ |
2964 | |
2965 | /* | |
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. | |
2972 | */ | |
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) | |
2977 | { | |
5cb9eaa3 | 2978 | raw_spin_rq_unlock(this_rq); |
029632fb PZ |
2979 | double_rq_lock(this_rq, busiest); |
2980 | ||
2981 | return 1; | |
2982 | } | |
2983 | ||
127f6bf1 | 2984 | #else /* !CONFIG_PREEMPTION: */ |
029632fb PZ |
2985 | /* |
2986 | * Unfair double_lock_balance: Optimizes throughput at the expense of | |
2987 | * latency by eliminating extra atomic operations when the locks are | |
97fb7a0a IM |
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, | |
029632fb PZ |
2990 | * regardless of entry order into the function. |
2991 | */ | |
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) | |
2996 | { | |
2679a837 HJ |
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); | |
5cb9eaa3 | 3000 | return 0; |
2679a837 | 3001 | } |
5cb9eaa3 | 3002 | |
d66f1b06 | 3003 | if (rq_order_less(this_rq, busiest)) { |
5cb9eaa3 | 3004 | raw_spin_rq_lock_nested(busiest, SINGLE_DEPTH_NESTING); |
2679a837 | 3005 | double_rq_clock_clear_update(this_rq, busiest); |
5cb9eaa3 | 3006 | return 0; |
029632fb | 3007 | } |
5cb9eaa3 PZ |
3008 | |
3009 | raw_spin_rq_unlock(this_rq); | |
d66f1b06 | 3010 | double_rq_lock(this_rq, busiest); |
5cb9eaa3 PZ |
3011 | |
3012 | return 1; | |
029632fb PZ |
3013 | } |
3014 | ||
127f6bf1 | 3015 | #endif /* !CONFIG_PREEMPTION */ |
029632fb PZ |
3016 | |
3017 | /* | |
3018 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. | |
3019 | */ | |
3020 | static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
3021 | { | |
5cb9eaa3 | 3022 | lockdep_assert_irqs_disabled(); |
029632fb PZ |
3023 | |
3024 | return _double_lock_balance(this_rq, busiest); | |
3025 | } | |
3026 | ||
3027 | static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) | |
3028 | __releases(busiest->lock) | |
3029 | { | |
9ef7e7e3 | 3030 | if (__rq_lockp(this_rq) != __rq_lockp(busiest)) |
5cb9eaa3 | 3031 | raw_spin_rq_unlock(busiest); |
9ef7e7e3 | 3032 | lock_set_subclass(&__rq_lockp(this_rq)->dep_map, 0, _RET_IP_); |
029632fb PZ |
3033 | } |
3034 | ||
74602315 PZ |
3035 | static inline void double_lock(spinlock_t *l1, spinlock_t *l2) |
3036 | { | |
3037 | if (l1 > l2) | |
3038 | swap(l1, l2); | |
3039 | ||
3040 | spin_lock(l1); | |
3041 | spin_lock_nested(l2, SINGLE_DEPTH_NESTING); | |
3042 | } | |
3043 | ||
60e69eed MG |
3044 | static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2) |
3045 | { | |
3046 | if (l1 > l2) | |
3047 | swap(l1, l2); | |
3048 | ||
3049 | spin_lock_irq(l1); | |
3050 | spin_lock_nested(l2, SINGLE_DEPTH_NESTING); | |
3051 | } | |
3052 | ||
74602315 PZ |
3053 | static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2) |
3054 | { | |
3055 | if (l1 > l2) | |
3056 | swap(l1, l2); | |
3057 | ||
3058 | raw_spin_lock(l1); | |
3059 | raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING); | |
3060 | } | |
3061 | ||
5bb76f1d PZ |
3062 | static inline void double_raw_unlock(raw_spinlock_t *l1, raw_spinlock_t *l2) |
3063 | { | |
3064 | raw_spin_unlock(l1); | |
3065 | raw_spin_unlock(l2); | |
3066 | } | |
3067 | ||
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)) | |
3071 | ||
029632fb PZ |
3072 | /* |
3073 | * double_rq_unlock - safely unlock two runqueues | |
3074 | * | |
3075 | * Note this does not restore interrupts like task_rq_unlock, | |
3076 | * you need to do so manually after calling. | |
3077 | */ | |
3078 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) | |
3079 | __releases(rq1->lock) | |
3080 | __releases(rq2->lock) | |
3081 | { | |
9ef7e7e3 | 3082 | if (__rq_lockp(rq1) != __rq_lockp(rq2)) |
5cb9eaa3 | 3083 | raw_spin_rq_unlock(rq2); |
029632fb PZ |
3084 | else |
3085 | __release(rq2->lock); | |
d66f1b06 | 3086 | raw_spin_rq_unlock(rq1); |
029632fb PZ |
3087 | } |
3088 | ||
f2cb1360 IM |
3089 | extern void set_rq_online (struct rq *rq); |
3090 | extern void set_rq_offline(struct rq *rq); | |
127f6bf1 | 3091 | |
f2cb1360 IM |
3092 | extern bool sched_smp_initialized; |
3093 | ||
127f6bf1 | 3094 | #else /* !CONFIG_SMP: */ |
029632fb PZ |
3095 | |
3096 | /* | |
3097 | * double_rq_lock - safely lock two runqueues | |
3098 | * | |
3099 | * Note this does not disable interrupts like task_rq_lock, | |
3100 | * you need to do so manually before calling. | |
3101 | */ | |
3102 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
3103 | __acquires(rq1->lock) | |
3104 | __acquires(rq2->lock) | |
3105 | { | |
09348d75 IM |
3106 | WARN_ON_ONCE(!irqs_disabled()); |
3107 | WARN_ON_ONCE(rq1 != rq2); | |
5cb9eaa3 | 3108 | raw_spin_rq_lock(rq1); |
029632fb | 3109 | __acquire(rq2->lock); /* Fake it out ;) */ |
2679a837 | 3110 | double_rq_clock_clear_update(rq1, rq2); |
029632fb PZ |
3111 | } |
3112 | ||
3113 | /* | |
3114 | * double_rq_unlock - safely unlock two runqueues | |
3115 | * | |
3116 | * Note this does not restore interrupts like task_rq_unlock, | |
3117 | * you need to do so manually after calling. | |
3118 | */ | |
3119 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) | |
3120 | __releases(rq1->lock) | |
3121 | __releases(rq2->lock) | |
3122 | { | |
09348d75 | 3123 | WARN_ON_ONCE(rq1 != rq2); |
5cb9eaa3 | 3124 | raw_spin_rq_unlock(rq1); |
029632fb PZ |
3125 | __release(rq2->lock); |
3126 | } | |
3127 | ||
127f6bf1 | 3128 | #endif /* !CONFIG_SMP */ |
029632fb | 3129 | |
5bb76f1d PZ |
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)) | |
3133 | ||
2227a957 | 3134 | extern struct sched_entity *__pick_root_entity(struct cfs_rq *cfs_rq); |
029632fb PZ |
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); | |
6b55c965 | 3137 | |
9406415f | 3138 | extern bool sched_debug_verbose; |
9469eb01 | 3139 | |
029632fb PZ |
3140 | extern void print_cfs_stats(struct seq_file *m, int cpu); |
3141 | extern void print_rt_stats(struct seq_file *m, int cpu); | |
acb32132 | 3142 | extern void print_dl_stats(struct seq_file *m, int cpu); |
f6a34630 MM |
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); | |
c006fac5 PT |
3146 | |
3147 | extern void resched_latency_warn(int cpu, u64 latency); | |
dd5bdaf2 | 3148 | #ifdef CONFIG_NUMA_BALANCING |
127f6bf1 | 3149 | extern void show_numa_stats(struct task_struct *p, struct seq_file *m); |
397f2378 SD |
3150 | extern void |
3151 | print_numa_stats(struct seq_file *m, int node, unsigned long tsf, | |
127f6bf1 | 3152 | unsigned long tpf, unsigned long gsf, unsigned long gpf); |
dd5bdaf2 | 3153 | #endif /* CONFIG_NUMA_BALANCING */ |
029632fb PZ |
3154 | |
3155 | extern void init_cfs_rq(struct cfs_rq *cfs_rq); | |
07c54f7a AV |
3156 | extern void init_rt_rq(struct rt_rq *rt_rq); |
3157 | extern void init_dl_rq(struct dl_rq *dl_rq); | |
029632fb | 3158 | |
1ee14e6c BS |
3159 | extern void cfs_bandwidth_usage_inc(void); |
3160 | extern void cfs_bandwidth_usage_dec(void); | |
1c792db7 | 3161 | |
3451d024 | 3162 | #ifdef CONFIG_NO_HZ_COMMON |
127f6bf1 | 3163 | |
00357f5e PZ |
3164 | #define NOHZ_BALANCE_KICK_BIT 0 |
3165 | #define NOHZ_STATS_KICK_BIT 1 | |
c6f88654 | 3166 | #define NOHZ_NEWILB_KICK_BIT 2 |
efd984c4 | 3167 | #define NOHZ_NEXT_KICK_BIT 3 |
a22e47a4 | 3168 | |
14ff4dbd | 3169 | /* Run sched_balance_domains() */ |
a22e47a4 | 3170 | #define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT) |
efd984c4 | 3171 | /* Update blocked load */ |
b7031a02 | 3172 | #define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT) |
efd984c4 | 3173 | /* Update blocked load when entering idle */ |
c6f88654 | 3174 | #define NOHZ_NEWILB_KICK BIT(NOHZ_NEWILB_KICK_BIT) |
efd984c4 VS |
3175 | /* Update nohz.next_balance */ |
3176 | #define NOHZ_NEXT_KICK BIT(NOHZ_NEXT_KICK_BIT) | |
b7031a02 | 3177 | |
127f6bf1 | 3178 | #define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK | NOHZ_NEXT_KICK) |
1c792db7 | 3179 | |
127f6bf1 | 3180 | #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags) |
20a5c8cc | 3181 | |
00357f5e | 3182 | extern void nohz_balance_exit_idle(struct rq *rq); |
127f6bf1 | 3183 | #else /* !CONFIG_NO_HZ_COMMON: */ |
00357f5e | 3184 | static inline void nohz_balance_exit_idle(struct rq *rq) { } |
127f6bf1 | 3185 | #endif /* !CONFIG_NO_HZ_COMMON */ |
73fbec60 | 3186 | |
c6f88654 VG |
3187 | #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON) |
3188 | extern void nohz_run_idle_balance(int cpu); | |
3189 | #else | |
3190 | static inline void nohz_run_idle_balance(int cpu) { } | |
3191 | #endif | |
daec5798 | 3192 | |
1a615101 JW |
3193 | #include "stats.h" |
3194 | ||
3195 | #if defined(CONFIG_SCHED_CORE) && defined(CONFIG_SCHEDSTATS) | |
3196 | ||
3197 | extern void __sched_core_account_forceidle(struct rq *rq); | |
3198 | ||
3199 | static inline void sched_core_account_forceidle(struct rq *rq) | |
3200 | { | |
3201 | if (schedstat_enabled()) | |
3202 | __sched_core_account_forceidle(rq); | |
3203 | } | |
3204 | ||
3205 | extern void __sched_core_tick(struct rq *rq); | |
3206 | ||
3207 | static inline void sched_core_tick(struct rq *rq) | |
3208 | { | |
3209 | if (sched_core_enabled(rq) && schedstat_enabled()) | |
3210 | __sched_core_tick(rq); | |
3211 | } | |
3212 | ||
3213 | #else /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS): */ | |
3214 | ||
3215 | static inline void sched_core_account_forceidle(struct rq *rq) { } | |
3216 | ||
3217 | static inline void sched_core_tick(struct rq *rq) { } | |
3218 | ||
3219 | #endif /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS) */ | |
3220 | ||
73fbec60 | 3221 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
127f6bf1 | 3222 | |
19d23dbf | 3223 | struct irqtime { |
25e2d8c1 | 3224 | u64 total; |
a499a5a1 | 3225 | u64 tick_delta; |
19d23dbf FW |
3226 | u64 irq_start_time; |
3227 | struct u64_stats_sync sync; | |
3228 | }; | |
73fbec60 | 3229 | |
19d23dbf | 3230 | DECLARE_PER_CPU(struct irqtime, cpu_irqtime); |
b9f2b29b | 3231 | extern int sched_clock_irqtime; |
8722903c YS |
3232 | |
3233 | static inline int irqtime_enabled(void) | |
3234 | { | |
b9f2b29b | 3235 | return sched_clock_irqtime; |
8722903c | 3236 | } |
73fbec60 | 3237 | |
25e2d8c1 FW |
3238 | /* |
3239 | * Returns the irqtime minus the softirq time computed by ksoftirqd. | |
3b03706f | 3240 | * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime |
25e2d8c1 FW |
3241 | * and never move forward. |
3242 | */ | |
73fbec60 FW |
3243 | static inline u64 irq_time_read(int cpu) |
3244 | { | |
19d23dbf FW |
3245 | struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu); |
3246 | unsigned int seq; | |
3247 | u64 total; | |
73fbec60 FW |
3248 | |
3249 | do { | |
19d23dbf | 3250 | seq = __u64_stats_fetch_begin(&irqtime->sync); |
25e2d8c1 | 3251 | total = irqtime->total; |
19d23dbf | 3252 | } while (__u64_stats_fetch_retry(&irqtime->sync, seq)); |
73fbec60 | 3253 | |
19d23dbf | 3254 | return total; |
73fbec60 | 3255 | } |
127f6bf1 | 3256 | |
8722903c YS |
3257 | #else |
3258 | ||
3259 | static inline int irqtime_enabled(void) | |
3260 | { | |
3261 | return 0; | |
3262 | } | |
3263 | ||
73fbec60 | 3264 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ |
adaf9fcd RW |
3265 | |
3266 | #ifdef CONFIG_CPU_FREQ | |
127f6bf1 | 3267 | |
b10abd0a | 3268 | DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data); |
adaf9fcd RW |
3269 | |
3270 | /** | |
3271 | * cpufreq_update_util - Take a note about CPU utilization changes. | |
12bde33d | 3272 | * @rq: Runqueue to carry out the update for. |
58919e83 | 3273 | * @flags: Update reason flags. |
adaf9fcd | 3274 | * |
58919e83 RW |
3275 | * This function is called by the scheduler on the CPU whose utilization is |
3276 | * being updated. | |
adaf9fcd RW |
3277 | * |
3278 | * It can only be called from RCU-sched read-side critical sections. | |
adaf9fcd RW |
3279 | * |
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. | |
e0367b12 JL |
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). | |
adaf9fcd | 3286 | * |
e0367b12 JL |
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, | |
adaf9fcd | 3289 | * but that really is a band-aid. Going forward it should be replaced with |
e0367b12 | 3290 | * solutions targeted more specifically at RT tasks. |
adaf9fcd | 3291 | */ |
12bde33d | 3292 | static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) |
adaf9fcd | 3293 | { |
58919e83 RW |
3294 | struct update_util_data *data; |
3295 | ||
674e7541 VK |
3296 | data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data, |
3297 | cpu_of(rq))); | |
58919e83 | 3298 | if (data) |
12bde33d RW |
3299 | data->func(data, rq_clock(rq), flags); |
3300 | } | |
127f6bf1 IM |
3301 | #else /* !CONFIG_CPU_FREQ: */ |
3302 | static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) { } | |
3303 | #endif /* !CONFIG_CPU_FREQ */ | |
be53f58f | 3304 | |
9bdcb44e | 3305 | #ifdef arch_scale_freq_capacity |
97fb7a0a IM |
3306 | # ifndef arch_scale_freq_invariant |
3307 | # define arch_scale_freq_invariant() true | |
3308 | # endif | |
3309 | #else | |
3310 | # define arch_scale_freq_invariant() false | |
9bdcb44e | 3311 | #endif |
d4edd662 | 3312 | |
10a35e68 | 3313 | #ifdef CONFIG_SMP |
127f6bf1 | 3314 | |
a5418be9 | 3315 | unsigned long effective_cpu_util(int cpu, unsigned long util_cfs, |
9c0b4bb7 VG |
3316 | unsigned long *min, |
3317 | unsigned long *max); | |
3318 | ||
3319 | unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual, | |
3320 | unsigned long min, | |
3321 | unsigned long max); | |
3322 | ||
938e5e4b | 3323 | |
b3f53daa DE |
3324 | /* |
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. | |
3327 | * | |
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. | |
3331 | */ | |
3332 | static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu) | |
3333 | { | |
3334 | unsigned long cap = arch_scale_cpu_capacity(cpu); | |
3335 | ||
3336 | return cap >= p->dl.dl_density >> (BW_SHIFT - SCHED_CAPACITY_SHIFT); | |
3337 | } | |
3338 | ||
8cc90515 | 3339 | static inline unsigned long cpu_bw_dl(struct rq *rq) |
d4edd662 JL |
3340 | { |
3341 | return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT; | |
3342 | } | |
3343 | ||
8cc90515 VG |
3344 | static inline unsigned long cpu_util_dl(struct rq *rq) |
3345 | { | |
3346 | return READ_ONCE(rq->avg_dl.util_avg); | |
3347 | } | |
3348 | ||
a07630b8 | 3349 | |
3eb6d6ec | 3350 | extern unsigned long cpu_util_cfs(int cpu); |
7d0583cf | 3351 | extern unsigned long cpu_util_cfs_boost(int cpu); |
371bf427 VG |
3352 | |
3353 | static inline unsigned long cpu_util_rt(struct rq *rq) | |
3354 | { | |
dfa444dc | 3355 | return READ_ONCE(rq->avg_rt.util_avg); |
371bf427 | 3356 | } |
127f6bf1 | 3357 | |
96fd6c65 TH |
3358 | #else /* !CONFIG_SMP */ |
3359 | static inline bool update_other_load_avgs(struct rq *rq) { return false; } | |
127f6bf1 | 3360 | #endif /* CONFIG_SMP */ |
9033ea11 | 3361 | |
7a17e1db | 3362 | #ifdef CONFIG_UCLAMP_TASK |
127f6bf1 | 3363 | |
7a17e1db QY |
3364 | unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id); |
3365 | ||
5fca5a4c XY |
3366 | /* |
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. | |
3370 | * | |
3371 | * Returns true if userspace opted-in to use uclamp and aggregation at rq level | |
3372 | * hence is active. | |
3373 | */ | |
3374 | static inline bool uclamp_is_used(void) | |
3375 | { | |
3376 | return static_branch_likely(&sched_uclamp_used); | |
3377 | } | |
3378 | ||
4bc45824 XY |
3379 | /* |
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. | |
3384 | */ | |
3385 | static inline void sched_uclamp_enable(void) | |
3386 | { | |
3387 | if (!uclamp_is_used()) | |
3388 | static_branch_enable(&sched_uclamp_used); | |
3389 | } | |
3390 | ||
24422603 QY |
3391 | static inline unsigned long uclamp_rq_get(struct rq *rq, |
3392 | enum uclamp_id clamp_id) | |
3393 | { | |
3394 | return READ_ONCE(rq->uclamp[clamp_id].value); | |
3395 | } | |
3396 | ||
3397 | static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, | |
3398 | unsigned int value) | |
3399 | { | |
3400 | WRITE_ONCE(rq->uclamp[clamp_id].value, value); | |
3401 | } | |
3402 | ||
3403 | static inline bool uclamp_rq_is_idle(struct rq *rq) | |
3404 | { | |
3405 | return rq->uclamp_flags & UCLAMP_FLAG_IDLE; | |
3406 | } | |
3407 | ||
7a17e1db QY |
3408 | /* Is the rq being capped/throttled by uclamp_max? */ |
3409 | static inline bool uclamp_rq_is_capped(struct rq *rq) | |
3410 | { | |
3411 | unsigned long rq_util; | |
3412 | unsigned long max_util; | |
3413 | ||
5fca5a4c | 3414 | if (!uclamp_is_used()) |
7a17e1db QY |
3415 | return false; |
3416 | ||
3417 | rq_util = cpu_util_cfs(cpu_of(rq)) + cpu_util_rt(rq); | |
3418 | max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value); | |
3419 | ||
3420 | return max_util != SCHED_CAPACITY_SCALE && rq_util >= max_util; | |
3421 | } | |
3422 | ||
04746ed8 IM |
3423 | #define for_each_clamp_id(clamp_id) \ |
3424 | for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++) | |
3425 | ||
3426 | extern unsigned int sysctl_sched_uclamp_util_min_rt_default; | |
3427 | ||
3428 | ||
3429 | static inline unsigned int uclamp_none(enum uclamp_id clamp_id) | |
3430 | { | |
3431 | if (clamp_id == UCLAMP_MIN) | |
3432 | return 0; | |
3433 | return SCHED_CAPACITY_SCALE; | |
3434 | } | |
3435 | ||
3436 | /* Integer rounded range for each bucket */ | |
3437 | #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS) | |
3438 | ||
3439 | static inline unsigned int uclamp_bucket_id(unsigned int clamp_value) | |
3440 | { | |
3441 | return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1); | |
3442 | } | |
3443 | ||
127f6bf1 IM |
3444 | static inline void |
3445 | uclamp_se_set(struct uclamp_se *uc_se, unsigned int value, bool user_defined) | |
04746ed8 IM |
3446 | { |
3447 | uc_se->value = value; | |
3448 | uc_se->bucket_id = uclamp_bucket_id(value); | |
3449 | uc_se->user_defined = user_defined; | |
3450 | } | |
3451 | ||
127f6bf1 IM |
3452 | #else /* !CONFIG_UCLAMP_TASK: */ |
3453 | ||
3454 | static inline unsigned long | |
3455 | uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id) | |
b48e16a6 QY |
3456 | { |
3457 | if (clamp_id == UCLAMP_MIN) | |
3458 | return 0; | |
3459 | ||
3460 | return SCHED_CAPACITY_SCALE; | |
3461 | } | |
3462 | ||
7a17e1db QY |
3463 | static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; } |
3464 | ||
3465 | static inline bool uclamp_is_used(void) | |
3466 | { | |
3467 | return false; | |
3468 | } | |
24422603 | 3469 | |
4bc45824 XY |
3470 | static inline void sched_uclamp_enable(void) {} |
3471 | ||
127f6bf1 IM |
3472 | static inline unsigned long |
3473 | uclamp_rq_get(struct rq *rq, enum uclamp_id clamp_id) | |
24422603 QY |
3474 | { |
3475 | if (clamp_id == UCLAMP_MIN) | |
3476 | return 0; | |
3477 | ||
3478 | return SCHED_CAPACITY_SCALE; | |
3479 | } | |
3480 | ||
127f6bf1 IM |
3481 | static inline void |
3482 | uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, unsigned int value) | |
24422603 QY |
3483 | { |
3484 | } | |
3485 | ||
3486 | static inline bool uclamp_rq_is_idle(struct rq *rq) | |
3487 | { | |
3488 | return false; | |
3489 | } | |
04746ed8 | 3490 | |
127f6bf1 | 3491 | #endif /* !CONFIG_UCLAMP_TASK */ |
7a17e1db | 3492 | |
11d4afd4 | 3493 | #ifdef CONFIG_HAVE_SCHED_AVG_IRQ |
127f6bf1 | 3494 | |
9033ea11 VG |
3495 | static inline unsigned long cpu_util_irq(struct rq *rq) |
3496 | { | |
a6965b31 | 3497 | return READ_ONCE(rq->avg_irq.util_avg); |
9033ea11 | 3498 | } |
2e62c474 VG |
3499 | |
3500 | static inline | |
3501 | unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max) | |
3502 | { | |
3503 | util *= (max - irq); | |
3504 | util /= max; | |
3505 | ||
3506 | return util; | |
3507 | ||
3508 | } | |
127f6bf1 IM |
3509 | |
3510 | #else /* !CONFIG_HAVE_SCHED_AVG_IRQ: */ | |
3511 | ||
9033ea11 VG |
3512 | static inline unsigned long cpu_util_irq(struct rq *rq) |
3513 | { | |
3514 | return 0; | |
3515 | } | |
3516 | ||
2e62c474 VG |
3517 | static inline |
3518 | unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max) | |
3519 | { | |
3520 | return util; | |
3521 | } | |
127f6bf1 IM |
3522 | |
3523 | #endif /* !CONFIG_HAVE_SCHED_AVG_IRQ */ | |
6aa140fa | 3524 | |
2cf9ac40 VG |
3525 | extern void __setparam_fair(struct task_struct *p, const struct sched_attr *attr); |
3526 | ||
531b5c9f | 3527 | #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) |
f8a696f2 | 3528 | |
6aa140fa | 3529 | #define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus))) |
f8a696f2 PZ |
3530 | |
3531 | DECLARE_STATIC_KEY_FALSE(sched_energy_present); | |
3532 | ||
3533 | static inline bool sched_energy_enabled(void) | |
3534 | { | |
3535 | return static_branch_unlikely(&sched_energy_present); | |
3536 | } | |
3537 | ||
3538 | #else /* ! (CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */ | |
3539 | ||
6aa140fa | 3540 | #define perf_domain_span(pd) NULL |
127f6bf1 | 3541 | |
f8a696f2 | 3542 | static inline bool sched_energy_enabled(void) { return false; } |
1f74de87 | 3543 | |
f8a696f2 | 3544 | #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */ |
227a4aad MD |
3545 | |
3546 | #ifdef CONFIG_MEMBARRIER | |
127f6bf1 | 3547 | |
227a4aad MD |
3548 | /* |
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. | |
3553 | */ | |
3554 | static inline void membarrier_switch_mm(struct rq *rq, | |
3555 | struct mm_struct *prev_mm, | |
3556 | struct mm_struct *next_mm) | |
3557 | { | |
3558 | int membarrier_state; | |
3559 | ||
3560 | if (prev_mm == next_mm) | |
3561 | return; | |
3562 | ||
3563 | membarrier_state = atomic_read(&next_mm->membarrier_state); | |
3564 | if (READ_ONCE(rq->membarrier_state) == membarrier_state) | |
3565 | return; | |
3566 | ||
3567 | WRITE_ONCE(rq->membarrier_state, membarrier_state); | |
3568 | } | |
127f6bf1 IM |
3569 | |
3570 | #else /* !CONFIG_MEMBARRIER :*/ | |
3571 | ||
227a4aad MD |
3572 | static inline void membarrier_switch_mm(struct rq *rq, |
3573 | struct mm_struct *prev_mm, | |
3574 | struct mm_struct *next_mm) | |
3575 | { | |
3576 | } | |
127f6bf1 IM |
3577 | |
3578 | #endif /* !CONFIG_MEMBARRIER */ | |
52262ee5 MG |
3579 | |
3580 | #ifdef CONFIG_SMP | |
3581 | static inline bool is_per_cpu_kthread(struct task_struct *p) | |
3582 | { | |
3583 | if (!(p->flags & PF_KTHREAD)) | |
3584 | return false; | |
3585 | ||
3586 | if (p->nr_cpus_allowed != 1) | |
3587 | return false; | |
3588 | ||
3589 | return true; | |
3590 | } | |
3591 | #endif | |
b3212fe2 | 3592 | |
1011dcce PZ |
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); | |
3595 | ||
ab83f455 PO |
3596 | extern int try_to_wake_up(struct task_struct *tsk, unsigned int state, int wake_flags); |
3597 | ||
1011dcce PZ |
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); | |
3602 | #endif | |
8bdc5daa | 3603 | extern const char *preempt_modes[]; |
1011dcce | 3604 | |
af7f588d | 3605 | #ifdef CONFIG_SCHED_MM_CID |
223baf9d MD |
3606 | |
3607 | #define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */ | |
3608 | #define MM_CID_SCAN_DELAY 100 /* 100ms */ | |
3609 | ||
3610 | extern raw_spinlock_t cid_lock; | |
3611 | extern int use_cid_lock; | |
3612 | ||
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); | |
3617 | ||
3618 | static inline void __mm_cid_put(struct mm_struct *mm, int cid) | |
3619 | { | |
3620 | if (cid < 0) | |
3621 | return; | |
3622 | cpumask_clear_cpu(cid, mm_cidmask(mm)); | |
3623 | } | |
3624 | ||
3625 | /* | |
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. | |
3629 | * | |
3630 | * State transitions synchronized with cmpxchg or try_cmpxchg need to be | |
402de7fc | 3631 | * consistent across CPUs, which prevents use of this_cpu_cmpxchg. |
223baf9d MD |
3632 | */ |
3633 | static inline void mm_cid_put_lazy(struct task_struct *t) | |
3634 | { | |
3635 | struct mm_struct *mm = t->mm; | |
3636 | struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; | |
3637 | int cid; | |
3638 | ||
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)) | |
3643 | return; | |
3644 | __mm_cid_put(mm, mm_cid_clear_lazy_put(cid)); | |
3645 | } | |
3646 | ||
3647 | static inline int mm_cid_pcpu_unset(struct mm_struct *mm) | |
3648 | { | |
3649 | struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; | |
3650 | int cid, res; | |
3651 | ||
3652 | lockdep_assert_irqs_disabled(); | |
3653 | cid = __this_cpu_read(pcpu_cid->cid); | |
3654 | for (;;) { | |
3655 | if (mm_cid_is_unset(cid)) | |
3656 | return MM_CID_UNSET; | |
3657 | /* | |
3658 | * Attempt transition from valid or lazy-put to unset. | |
3659 | */ | |
3660 | res = cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, cid, MM_CID_UNSET); | |
3661 | if (res == cid) | |
3662 | break; | |
3663 | cid = res; | |
3664 | } | |
3665 | return cid; | |
3666 | } | |
3667 | ||
3668 | static inline void mm_cid_put(struct mm_struct *mm) | |
3669 | { | |
3670 | int cid; | |
3671 | ||
3672 | lockdep_assert_irqs_disabled(); | |
3673 | cid = mm_cid_pcpu_unset(mm); | |
3674 | if (cid == MM_CID_UNSET) | |
3675 | return; | |
3676 | __mm_cid_put(mm, mm_cid_clear_lazy_put(cid)); | |
3677 | } | |
3678 | ||
7e019dcc | 3679 | static inline int __mm_cid_try_get(struct task_struct *t, struct mm_struct *mm) |
af7f588d | 3680 | { |
7e019dcc MD |
3681 | struct cpumask *cidmask = mm_cidmask(mm); |
3682 | struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; | |
02d954c0 | 3683 | int cid, max_nr_cid, allowed_max_nr_cid; |
af7f588d | 3684 | |
02d954c0 MD |
3685 | /* |
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. | |
3690 | */ | |
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; | |
3698 | break; | |
3699 | } | |
3700 | } | |
7e019dcc | 3701 | /* Try to re-use recent cid. This improves cache locality. */ |
02d954c0 MD |
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)) | |
7e019dcc | 3705 | return cid; |
223baf9d | 3706 | /* |
7e019dcc MD |
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. | |
3711 | */ | |
02d954c0 | 3712 | cid = max_nr_cid; |
7e019dcc | 3713 | while (cid < READ_ONCE(mm->nr_cpus_allowed) && cid < atomic_read(&mm->mm_users)) { |
02d954c0 | 3714 | /* atomic_try_cmpxchg loads previous mm->max_nr_cid into cid. */ |
7e019dcc MD |
3715 | if (!atomic_try_cmpxchg(&mm->max_nr_cid, &cid, cid + 1)) |
3716 | continue; | |
3717 | if (!cpumask_test_and_set_cpu(cid, cidmask)) | |
3718 | return cid; | |
3719 | } | |
223baf9d | 3720 | /* |
7e019dcc | 3721 | * Find the first available concurrency id. |
223baf9d MD |
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. | |
3725 | */ | |
3726 | for (;;) { | |
7e019dcc MD |
3727 | cid = cpumask_first_zero(cidmask); |
3728 | if (cid < READ_ONCE(mm->nr_cpus_allowed)) | |
223baf9d MD |
3729 | break; |
3730 | cpu_relax(); | |
3731 | } | |
7e019dcc | 3732 | if (cpumask_test_and_set_cpu(cid, cidmask)) |
af7f588d | 3733 | return -1; |
127f6bf1 | 3734 | |
af7f588d MD |
3735 | return cid; |
3736 | } | |
3737 | ||
223baf9d MD |
3738 | /* |
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. | |
3741 | */ | |
3742 | static inline void mm_cid_snapshot_time(struct rq *rq, struct mm_struct *mm) | |
af7f588d | 3743 | { |
223baf9d MD |
3744 | struct mm_cid *pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu_of(rq)); |
3745 | ||
3746 | lockdep_assert_rq_held(rq); | |
3747 | WRITE_ONCE(pcpu_cid->time, rq->clock); | |
af7f588d MD |
3748 | } |
3749 | ||
7e019dcc MD |
3750 | static inline int __mm_cid_get(struct rq *rq, struct task_struct *t, |
3751 | struct mm_struct *mm) | |
af7f588d | 3752 | { |
223baf9d | 3753 | int cid; |
af7f588d | 3754 | |
223baf9d MD |
3755 | /* |
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. | |
3759 | */ | |
3760 | if (!READ_ONCE(use_cid_lock)) { | |
7e019dcc | 3761 | cid = __mm_cid_try_get(t, mm); |
223baf9d MD |
3762 | if (cid >= 0) |
3763 | goto end; | |
3764 | raw_spin_lock(&cid_lock); | |
3765 | } else { | |
3766 | raw_spin_lock(&cid_lock); | |
7e019dcc | 3767 | cid = __mm_cid_try_get(t, mm); |
223baf9d MD |
3768 | if (cid >= 0) |
3769 | goto unlock; | |
3770 | } | |
3771 | ||
3772 | /* | |
3773 | * cid concurrently allocated. Retry while forcing following | |
3774 | * allocations to use the cid_lock to ensure forward progress. | |
3775 | */ | |
3776 | WRITE_ONCE(use_cid_lock, 1); | |
3777 | /* | |
3778 | * Set use_cid_lock before allocation. Only care about program order | |
3779 | * because this is only required for forward progress. | |
3780 | */ | |
3781 | barrier(); | |
3782 | /* | |
3783 | * Retry until it succeeds. It is guaranteed to eventually succeed once | |
3784 | * all newcoming allocations observe the use_cid_lock flag set. | |
3785 | */ | |
3786 | do { | |
7e019dcc | 3787 | cid = __mm_cid_try_get(t, mm); |
223baf9d MD |
3788 | cpu_relax(); |
3789 | } while (cid < 0); | |
3790 | /* | |
3791 | * Allocate before clearing use_cid_lock. Only care about | |
3792 | * program order because this is for forward progress. | |
3793 | */ | |
3794 | barrier(); | |
3795 | WRITE_ONCE(use_cid_lock, 0); | |
3796 | unlock: | |
3797 | raw_spin_unlock(&cid_lock); | |
3798 | end: | |
3799 | mm_cid_snapshot_time(rq, mm); | |
127f6bf1 | 3800 | |
223baf9d | 3801 | return cid; |
af7f588d MD |
3802 | } |
3803 | ||
7e019dcc MD |
3804 | static inline int mm_cid_get(struct rq *rq, struct task_struct *t, |
3805 | struct mm_struct *mm) | |
af7f588d | 3806 | { |
223baf9d MD |
3807 | struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; |
3808 | struct cpumask *cpumask; | |
3809 | int cid; | |
3810 | ||
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); | |
3816 | return cid; | |
3817 | } | |
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)); | |
3821 | } | |
7e019dcc | 3822 | cid = __mm_cid_get(rq, t, mm); |
223baf9d | 3823 | __this_cpu_write(pcpu_cid->cid, cid); |
7e019dcc | 3824 | __this_cpu_write(pcpu_cid->recent_cid, cid); |
127f6bf1 | 3825 | |
223baf9d MD |
3826 | return cid; |
3827 | } | |
3828 | ||
3829 | static inline void switch_mm_cid(struct rq *rq, | |
3830 | struct task_struct *prev, | |
3831 | struct task_struct *next) | |
3832 | { | |
3833 | /* | |
3834 | * Provide a memory barrier between rq->curr store and load of | |
3835 | * {prev,next}->mm->pcpu_cid[cpu] on rq->curr->mm transition. | |
3836 | * | |
3837 | * Should be adapted if context_switch() is modified. | |
3838 | */ | |
3839 | if (!next->mm) { // to kernel | |
3840 | /* | |
3841 | * user -> kernel transition does not guarantee a barrier, but | |
3842 | * we can use the fact that it performs an atomic operation in | |
3843 | * mmgrab(). | |
3844 | */ | |
3845 | if (prev->mm) // from user | |
3846 | smp_mb__after_mmgrab(); | |
3847 | /* | |
3848 | * kernel -> kernel transition does not change rq->curr->mm | |
3849 | * state. It stays NULL. | |
3850 | */ | |
3851 | } else { // to user | |
3852 | /* | |
3853 | * kernel -> user transition does not provide a barrier | |
3854 | * between rq->curr store and load of {prev,next}->mm->pcpu_cid[cpu]. | |
3855 | * Provide it here. | |
3856 | */ | |
fe90f396 | 3857 | if (!prev->mm) { // from kernel |
223baf9d | 3858 | smp_mb(); |
fe90f396 MD |
3859 | } else { // from user |
3860 | /* | |
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. | |
3867 | */ | |
3868 | smp_mb__after_switch_mm(); | |
3869 | } | |
223baf9d | 3870 | } |
af7f588d | 3871 | if (prev->mm_cid_active) { |
223baf9d MD |
3872 | mm_cid_snapshot_time(rq, prev->mm); |
3873 | mm_cid_put_lazy(prev); | |
af7f588d MD |
3874 | prev->mm_cid = -1; |
3875 | } | |
3876 | if (next->mm_cid_active) | |
7e019dcc | 3877 | next->last_mm_cid = next->mm_cid = mm_cid_get(rq, next, next->mm); |
af7f588d MD |
3878 | } |
3879 | ||
127f6bf1 | 3880 | #else /* !CONFIG_SCHED_MM_CID: */ |
223baf9d MD |
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) { } | |
127f6bf1 | 3886 | #endif /* !CONFIG_SCHED_MM_CID */ |
af7f588d | 3887 | |
af4cf404 | 3888 | extern u64 avg_vruntime(struct cfs_rq *cfs_rq); |
147f3efa | 3889 | extern int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se); |
2b05a0b4 CB |
3890 | #ifdef CONFIG_SMP |
3891 | static inline | |
3892 | void move_queued_task_locked(struct rq *src_rq, struct rq *dst_rq, struct task_struct *task) | |
3893 | { | |
3894 | lockdep_assert_rq_held(src_rq); | |
3895 | lockdep_assert_rq_held(dst_rq); | |
3896 | ||
3897 | deactivate_task(src_rq, task, 0); | |
3898 | set_task_cpu(task, dst_rq->cpu); | |
3899 | activate_task(dst_rq, task, 0); | |
3900 | } | |
18adad1d CB |
3901 | |
3902 | static inline | |
3903 | bool task_is_pushable(struct rq *rq, struct task_struct *p, int cpu) | |
3904 | { | |
3905 | if (!task_on_cpu(rq, p) && | |
3906 | cpumask_test_cpu(cpu, &p->cpus_mask)) | |
3907 | return true; | |
3908 | ||
3909 | return false; | |
3910 | } | |
2b05a0b4 | 3911 | #endif |
af4cf404 | 3912 | |
04746ed8 | 3913 | #ifdef CONFIG_RT_MUTEXES |
127f6bf1 | 3914 | |
04746ed8 IM |
3915 | static inline int __rt_effective_prio(struct task_struct *pi_task, int prio) |
3916 | { | |
3917 | if (pi_task) | |
3918 | prio = min(prio, pi_task->prio); | |
3919 | ||
3920 | return prio; | |
3921 | } | |
3922 | ||
3923 | static inline int rt_effective_prio(struct task_struct *p, int prio) | |
3924 | { | |
3925 | struct task_struct *pi_task = rt_mutex_get_top_task(p); | |
3926 | ||
3927 | return __rt_effective_prio(pi_task, prio); | |
3928 | } | |
127f6bf1 IM |
3929 | |
3930 | #else /* !CONFIG_RT_MUTEXES: */ | |
3931 | ||
04746ed8 IM |
3932 | static inline int rt_effective_prio(struct task_struct *p, int prio) |
3933 | { | |
3934 | return prio; | |
3935 | } | |
127f6bf1 IM |
3936 | |
3937 | #endif /* !CONFIG_RT_MUTEXES */ | |
04746ed8 IM |
3938 | |
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); | |
5db91545 | 3941 | extern const struct sched_class *__setscheduler_class(int policy, int prio); |
04746ed8 IM |
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); | |
863ccdbb | 3944 | extern bool dequeue_task(struct rq *rq, struct task_struct *p, int flags); |
04746ed8 | 3945 | |
d8c7bc2e TH |
3946 | extern void check_class_changing(struct rq *rq, struct task_struct *p, |
3947 | const struct sched_class *prev_class); | |
04746ed8 IM |
3948 | extern void check_class_changed(struct rq *rq, struct task_struct *p, |
3949 | const struct sched_class *prev_class, | |
3950 | int oldprio); | |
3951 | ||
3952 | #ifdef CONFIG_SMP | |
3953 | extern struct balance_callback *splice_balance_callbacks(struct rq *rq); | |
3954 | extern void balance_callbacks(struct rq *rq, struct balance_callback *head); | |
3955 | #else | |
3956 | ||
3957 | static inline struct balance_callback *splice_balance_callbacks(struct rq *rq) | |
3958 | { | |
3959 | return NULL; | |
3960 | } | |
3961 | ||
3962 | static inline void balance_callbacks(struct rq *rq, struct balance_callback *head) | |
3963 | { | |
3964 | } | |
3965 | ||
3966 | #endif | |
3967 | ||
e196c908 TH |
3968 | #ifdef CONFIG_SCHED_CLASS_EXT |
3969 | /* | |
3970 | * Used by SCX in the enable/disable paths to move tasks between sched_classes | |
3971 | * and establish invariants. | |
3972 | */ | |
3973 | struct sched_enq_and_set_ctx { | |
3974 | struct task_struct *p; | |
3975 | int queue_flags; | |
3976 | bool queued; | |
3977 | bool running; | |
3978 | }; | |
3979 | ||
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); | |
3983 | ||
3984 | #endif /* CONFIG_SCHED_CLASS_EXT */ | |
3985 | ||
a7a9fc54 TH |
3986 | #include "ext.h" |
3987 | ||
95458477 | 3988 | #endif /* _KERNEL_SCHED_SCHED_H */ |