]>
Commit | Line | Data |
---|---|---|
029632fb PZ |
1 | |
2 | #include <linux/sched.h> | |
cf4aebc2 | 3 | #include <linux/sched/sysctl.h> |
105ab3d8 | 4 | #include <linux/sched/topology.h> |
8bd75c77 | 5 | #include <linux/sched/rt.h> |
ef8bd77f | 6 | #include <linux/sched/deadline.h> |
e6017571 | 7 | #include <linux/sched/clock.h> |
84f001e1 | 8 | #include <linux/sched/wake_q.h> |
3f07c014 | 9 | #include <linux/sched/signal.h> |
6a3827d7 | 10 | #include <linux/sched/numa_balancing.h> |
6e84f315 | 11 | #include <linux/sched/mm.h> |
55687da1 | 12 | #include <linux/sched/cpufreq.h> |
03441a34 | 13 | #include <linux/sched/stat.h> |
370c9135 | 14 | #include <linux/sched/nohz.h> |
b17b0153 | 15 | #include <linux/sched/debug.h> |
ef8bd77f | 16 | #include <linux/sched/hotplug.h> |
29930025 | 17 | #include <linux/sched/task.h> |
ef8bd77f | 18 | |
19d23dbf | 19 | #include <linux/u64_stats_sync.h> |
a499a5a1 | 20 | #include <linux/kernel_stat.h> |
3866e845 | 21 | #include <linux/binfmts.h> |
029632fb PZ |
22 | #include <linux/mutex.h> |
23 | #include <linux/spinlock.h> | |
24 | #include <linux/stop_machine.h> | |
b6366f04 | 25 | #include <linux/irq_work.h> |
9f3660c2 | 26 | #include <linux/tick.h> |
f809ca9a | 27 | #include <linux/slab.h> |
029632fb | 28 | |
7fce777c IM |
29 | #ifdef CONFIG_PARAVIRT |
30 | #include <asm/paravirt.h> | |
31 | #endif | |
32 | ||
391e43da | 33 | #include "cpupri.h" |
6bfd6d72 | 34 | #include "cpudeadline.h" |
60fed789 | 35 | #include "cpuacct.h" |
029632fb | 36 | |
9148a3a1 PZ |
37 | #ifdef CONFIG_SCHED_DEBUG |
38 | #define SCHED_WARN_ON(x) WARN_ONCE(x, #x) | |
39 | #else | |
40 | #define SCHED_WARN_ON(x) ((void)(x)) | |
41 | #endif | |
42 | ||
45ceebf7 | 43 | struct rq; |
442bf3aa | 44 | struct cpuidle_state; |
45ceebf7 | 45 | |
da0c1e65 KT |
46 | /* task_struct::on_rq states: */ |
47 | #define TASK_ON_RQ_QUEUED 1 | |
cca26e80 | 48 | #define TASK_ON_RQ_MIGRATING 2 |
da0c1e65 | 49 | |
029632fb PZ |
50 | extern __read_mostly int scheduler_running; |
51 | ||
45ceebf7 PG |
52 | extern unsigned long calc_load_update; |
53 | extern atomic_long_t calc_load_tasks; | |
54 | ||
3289bdb4 | 55 | extern void calc_global_load_tick(struct rq *this_rq); |
d60585c5 | 56 | extern long calc_load_fold_active(struct rq *this_rq, long adjust); |
3289bdb4 PZ |
57 | |
58 | #ifdef CONFIG_SMP | |
cee1afce | 59 | extern void cpu_load_update_active(struct rq *this_rq); |
3289bdb4 | 60 | #else |
cee1afce | 61 | static inline void cpu_load_update_active(struct rq *this_rq) { } |
3289bdb4 | 62 | #endif |
45ceebf7 | 63 | |
029632fb PZ |
64 | /* |
65 | * Helpers for converting nanosecond timing to jiffy resolution | |
66 | */ | |
67 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) | |
68 | ||
cc1f4b1f LZ |
69 | /* |
70 | * Increase resolution of nice-level calculations for 64-bit architectures. | |
71 | * The extra resolution improves shares distribution and load balancing of | |
72 | * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup | |
73 | * hierarchies, especially on larger systems. This is not a user-visible change | |
74 | * and does not change the user-interface for setting shares/weights. | |
75 | * | |
76 | * We increase resolution only if we have enough bits to allow this increased | |
2159197d PZ |
77 | * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are |
78 | * pretty high and the returns do not justify the increased costs. | |
79 | * | |
80 | * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to | |
81 | * increase coverage and consistency always enable it on 64bit platforms. | |
cc1f4b1f | 82 | */ |
2159197d | 83 | #ifdef CONFIG_64BIT |
172895e6 | 84 | # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT) |
6ecdd749 YD |
85 | # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT) |
86 | # define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT) | |
cc1f4b1f | 87 | #else |
172895e6 | 88 | # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT) |
cc1f4b1f LZ |
89 | # define scale_load(w) (w) |
90 | # define scale_load_down(w) (w) | |
91 | #endif | |
92 | ||
6ecdd749 | 93 | /* |
172895e6 YD |
94 | * Task weight (visible to users) and its load (invisible to users) have |
95 | * independent resolution, but they should be well calibrated. We use | |
96 | * scale_load() and scale_load_down(w) to convert between them. The | |
97 | * following must be true: | |
98 | * | |
99 | * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD | |
100 | * | |
6ecdd749 | 101 | */ |
172895e6 | 102 | #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT) |
029632fb | 103 | |
332ac17e DF |
104 | /* |
105 | * Single value that decides SCHED_DEADLINE internal math precision. | |
106 | * 10 -> just above 1us | |
107 | * 9 -> just above 0.5us | |
108 | */ | |
109 | #define DL_SCALE (10) | |
110 | ||
029632fb PZ |
111 | /* |
112 | * These are the 'tuning knobs' of the scheduler: | |
029632fb | 113 | */ |
029632fb PZ |
114 | |
115 | /* | |
116 | * single value that denotes runtime == period, ie unlimited time. | |
117 | */ | |
118 | #define RUNTIME_INF ((u64)~0ULL) | |
119 | ||
20f9cd2a HA |
120 | static inline int idle_policy(int policy) |
121 | { | |
122 | return policy == SCHED_IDLE; | |
123 | } | |
d50dde5a DF |
124 | static inline int fair_policy(int policy) |
125 | { | |
126 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; | |
127 | } | |
128 | ||
029632fb PZ |
129 | static inline int rt_policy(int policy) |
130 | { | |
d50dde5a | 131 | return policy == SCHED_FIFO || policy == SCHED_RR; |
029632fb PZ |
132 | } |
133 | ||
aab03e05 DF |
134 | static inline int dl_policy(int policy) |
135 | { | |
136 | return policy == SCHED_DEADLINE; | |
137 | } | |
20f9cd2a HA |
138 | static inline bool valid_policy(int policy) |
139 | { | |
140 | return idle_policy(policy) || fair_policy(policy) || | |
141 | rt_policy(policy) || dl_policy(policy); | |
142 | } | |
aab03e05 | 143 | |
029632fb PZ |
144 | static inline int task_has_rt_policy(struct task_struct *p) |
145 | { | |
146 | return rt_policy(p->policy); | |
147 | } | |
148 | ||
aab03e05 DF |
149 | static inline int task_has_dl_policy(struct task_struct *p) |
150 | { | |
151 | return dl_policy(p->policy); | |
152 | } | |
153 | ||
2d3d891d DF |
154 | /* |
155 | * Tells if entity @a should preempt entity @b. | |
156 | */ | |
332ac17e DF |
157 | static inline bool |
158 | dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b) | |
2d3d891d DF |
159 | { |
160 | return dl_time_before(a->deadline, b->deadline); | |
161 | } | |
162 | ||
029632fb PZ |
163 | /* |
164 | * This is the priority-queue data structure of the RT scheduling class: | |
165 | */ | |
166 | struct rt_prio_array { | |
167 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ | |
168 | struct list_head queue[MAX_RT_PRIO]; | |
169 | }; | |
170 | ||
171 | struct rt_bandwidth { | |
172 | /* nests inside the rq lock: */ | |
173 | raw_spinlock_t rt_runtime_lock; | |
174 | ktime_t rt_period; | |
175 | u64 rt_runtime; | |
176 | struct hrtimer rt_period_timer; | |
4cfafd30 | 177 | unsigned int rt_period_active; |
029632fb | 178 | }; |
a5e7be3b JL |
179 | |
180 | void __dl_clear_params(struct task_struct *p); | |
181 | ||
332ac17e DF |
182 | /* |
183 | * To keep the bandwidth of -deadline tasks and groups under control | |
184 | * we need some place where: | |
185 | * - store the maximum -deadline bandwidth of the system (the group); | |
186 | * - cache the fraction of that bandwidth that is currently allocated. | |
187 | * | |
188 | * This is all done in the data structure below. It is similar to the | |
189 | * one used for RT-throttling (rt_bandwidth), with the main difference | |
190 | * that, since here we are only interested in admission control, we | |
191 | * do not decrease any runtime while the group "executes", neither we | |
192 | * need a timer to replenish it. | |
193 | * | |
194 | * With respect to SMP, the bandwidth is given on a per-CPU basis, | |
195 | * meaning that: | |
196 | * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU; | |
197 | * - dl_total_bw array contains, in the i-eth element, the currently | |
198 | * allocated bandwidth on the i-eth CPU. | |
199 | * Moreover, groups consume bandwidth on each CPU, while tasks only | |
200 | * consume bandwidth on the CPU they're running on. | |
201 | * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw | |
202 | * that will be shown the next time the proc or cgroup controls will | |
203 | * be red. It on its turn can be changed by writing on its own | |
204 | * control. | |
205 | */ | |
206 | struct dl_bandwidth { | |
207 | raw_spinlock_t dl_runtime_lock; | |
208 | u64 dl_runtime; | |
209 | u64 dl_period; | |
210 | }; | |
211 | ||
212 | static inline int dl_bandwidth_enabled(void) | |
213 | { | |
1724813d | 214 | return sysctl_sched_rt_runtime >= 0; |
332ac17e DF |
215 | } |
216 | ||
217 | extern struct dl_bw *dl_bw_of(int i); | |
218 | ||
219 | struct dl_bw { | |
220 | raw_spinlock_t lock; | |
221 | u64 bw, total_bw; | |
222 | }; | |
223 | ||
7f51412a JL |
224 | static inline |
225 | void __dl_clear(struct dl_bw *dl_b, u64 tsk_bw) | |
226 | { | |
227 | dl_b->total_bw -= tsk_bw; | |
228 | } | |
229 | ||
230 | static inline | |
231 | void __dl_add(struct dl_bw *dl_b, u64 tsk_bw) | |
232 | { | |
233 | dl_b->total_bw += tsk_bw; | |
234 | } | |
235 | ||
236 | static inline | |
237 | bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw) | |
238 | { | |
239 | return dl_b->bw != -1 && | |
240 | dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw; | |
241 | } | |
242 | ||
f2cb1360 | 243 | extern void init_dl_bw(struct dl_bw *dl_b); |
029632fb PZ |
244 | |
245 | #ifdef CONFIG_CGROUP_SCHED | |
246 | ||
247 | #include <linux/cgroup.h> | |
248 | ||
249 | struct cfs_rq; | |
250 | struct rt_rq; | |
251 | ||
35cf4e50 | 252 | extern struct list_head task_groups; |
029632fb PZ |
253 | |
254 | struct cfs_bandwidth { | |
255 | #ifdef CONFIG_CFS_BANDWIDTH | |
256 | raw_spinlock_t lock; | |
257 | ktime_t period; | |
258 | u64 quota, runtime; | |
9c58c79a | 259 | s64 hierarchical_quota; |
029632fb PZ |
260 | u64 runtime_expires; |
261 | ||
4cfafd30 | 262 | int idle, period_active; |
029632fb PZ |
263 | struct hrtimer period_timer, slack_timer; |
264 | struct list_head throttled_cfs_rq; | |
265 | ||
266 | /* statistics */ | |
267 | int nr_periods, nr_throttled; | |
268 | u64 throttled_time; | |
269 | #endif | |
270 | }; | |
271 | ||
272 | /* task group related information */ | |
273 | struct task_group { | |
274 | struct cgroup_subsys_state css; | |
275 | ||
276 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
277 | /* schedulable entities of this group on each cpu */ | |
278 | struct sched_entity **se; | |
279 | /* runqueue "owned" by this group on each cpu */ | |
280 | struct cfs_rq **cfs_rq; | |
281 | unsigned long shares; | |
282 | ||
fa6bddeb | 283 | #ifdef CONFIG_SMP |
b0367629 WL |
284 | /* |
285 | * load_avg can be heavily contended at clock tick time, so put | |
286 | * it in its own cacheline separated from the fields above which | |
287 | * will also be accessed at each tick. | |
288 | */ | |
289 | atomic_long_t load_avg ____cacheline_aligned; | |
029632fb | 290 | #endif |
fa6bddeb | 291 | #endif |
029632fb PZ |
292 | |
293 | #ifdef CONFIG_RT_GROUP_SCHED | |
294 | struct sched_rt_entity **rt_se; | |
295 | struct rt_rq **rt_rq; | |
296 | ||
297 | struct rt_bandwidth rt_bandwidth; | |
298 | #endif | |
299 | ||
300 | struct rcu_head rcu; | |
301 | struct list_head list; | |
302 | ||
303 | struct task_group *parent; | |
304 | struct list_head siblings; | |
305 | struct list_head children; | |
306 | ||
307 | #ifdef CONFIG_SCHED_AUTOGROUP | |
308 | struct autogroup *autogroup; | |
309 | #endif | |
310 | ||
311 | struct cfs_bandwidth cfs_bandwidth; | |
312 | }; | |
313 | ||
314 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
315 | #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD | |
316 | ||
317 | /* | |
318 | * A weight of 0 or 1 can cause arithmetics problems. | |
319 | * A weight of a cfs_rq is the sum of weights of which entities | |
320 | * are queued on this cfs_rq, so a weight of a entity should not be | |
321 | * too large, so as the shares value of a task group. | |
322 | * (The default weight is 1024 - so there's no practical | |
323 | * limitation from this.) | |
324 | */ | |
325 | #define MIN_SHARES (1UL << 1) | |
326 | #define MAX_SHARES (1UL << 18) | |
327 | #endif | |
328 | ||
029632fb PZ |
329 | typedef int (*tg_visitor)(struct task_group *, void *); |
330 | ||
331 | extern int walk_tg_tree_from(struct task_group *from, | |
332 | tg_visitor down, tg_visitor up, void *data); | |
333 | ||
334 | /* | |
335 | * Iterate the full tree, calling @down when first entering a node and @up when | |
336 | * leaving it for the final time. | |
337 | * | |
338 | * Caller must hold rcu_lock or sufficient equivalent. | |
339 | */ | |
340 | static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) | |
341 | { | |
342 | return walk_tg_tree_from(&root_task_group, down, up, data); | |
343 | } | |
344 | ||
345 | extern int tg_nop(struct task_group *tg, void *data); | |
346 | ||
347 | extern void free_fair_sched_group(struct task_group *tg); | |
348 | extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent); | |
8663e24d | 349 | extern void online_fair_sched_group(struct task_group *tg); |
6fe1f348 | 350 | extern void unregister_fair_sched_group(struct task_group *tg); |
029632fb PZ |
351 | extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
352 | struct sched_entity *se, int cpu, | |
353 | struct sched_entity *parent); | |
354 | extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b); | |
029632fb PZ |
355 | |
356 | extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b); | |
77a4d1a1 | 357 | extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b); |
029632fb PZ |
358 | extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq); |
359 | ||
360 | extern void free_rt_sched_group(struct task_group *tg); | |
361 | extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent); | |
362 | extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, | |
363 | struct sched_rt_entity *rt_se, int cpu, | |
364 | struct sched_rt_entity *parent); | |
365 | ||
25cc7da7 LZ |
366 | extern struct task_group *sched_create_group(struct task_group *parent); |
367 | extern void sched_online_group(struct task_group *tg, | |
368 | struct task_group *parent); | |
369 | extern void sched_destroy_group(struct task_group *tg); | |
370 | extern void sched_offline_group(struct task_group *tg); | |
371 | ||
372 | extern void sched_move_task(struct task_struct *tsk); | |
373 | ||
374 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
375 | extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); | |
ad936d86 BP |
376 | |
377 | #ifdef CONFIG_SMP | |
378 | extern void set_task_rq_fair(struct sched_entity *se, | |
379 | struct cfs_rq *prev, struct cfs_rq *next); | |
380 | #else /* !CONFIG_SMP */ | |
381 | static inline void set_task_rq_fair(struct sched_entity *se, | |
382 | struct cfs_rq *prev, struct cfs_rq *next) { } | |
383 | #endif /* CONFIG_SMP */ | |
384 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
25cc7da7 | 385 | |
029632fb PZ |
386 | #else /* CONFIG_CGROUP_SCHED */ |
387 | ||
388 | struct cfs_bandwidth { }; | |
389 | ||
390 | #endif /* CONFIG_CGROUP_SCHED */ | |
391 | ||
392 | /* CFS-related fields in a runqueue */ | |
393 | struct cfs_rq { | |
394 | struct load_weight load; | |
c82513e5 | 395 | unsigned int nr_running, h_nr_running; |
029632fb PZ |
396 | |
397 | u64 exec_clock; | |
398 | u64 min_vruntime; | |
399 | #ifndef CONFIG_64BIT | |
400 | u64 min_vruntime_copy; | |
401 | #endif | |
402 | ||
403 | struct rb_root tasks_timeline; | |
404 | struct rb_node *rb_leftmost; | |
405 | ||
029632fb PZ |
406 | /* |
407 | * 'curr' points to currently running entity on this cfs_rq. | |
408 | * It is set to NULL otherwise (i.e when none are currently running). | |
409 | */ | |
410 | struct sched_entity *curr, *next, *last, *skip; | |
411 | ||
412 | #ifdef CONFIG_SCHED_DEBUG | |
413 | unsigned int nr_spread_over; | |
414 | #endif | |
415 | ||
2dac754e PT |
416 | #ifdef CONFIG_SMP |
417 | /* | |
9d89c257 | 418 | * CFS load tracking |
2dac754e | 419 | */ |
9d89c257 | 420 | struct sched_avg avg; |
13962234 YD |
421 | u64 runnable_load_sum; |
422 | unsigned long runnable_load_avg; | |
c566e8e9 | 423 | #ifdef CONFIG_FAIR_GROUP_SCHED |
9d89c257 | 424 | unsigned long tg_load_avg_contrib; |
09a43ace | 425 | unsigned long propagate_avg; |
9d89c257 YD |
426 | #endif |
427 | atomic_long_t removed_load_avg, removed_util_avg; | |
428 | #ifndef CONFIG_64BIT | |
429 | u64 load_last_update_time_copy; | |
430 | #endif | |
82958366 | 431 | |
9d89c257 | 432 | #ifdef CONFIG_FAIR_GROUP_SCHED |
82958366 PT |
433 | /* |
434 | * h_load = weight * f(tg) | |
435 | * | |
436 | * Where f(tg) is the recursive weight fraction assigned to | |
437 | * this group. | |
438 | */ | |
439 | unsigned long h_load; | |
68520796 VD |
440 | u64 last_h_load_update; |
441 | struct sched_entity *h_load_next; | |
442 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
82958366 PT |
443 | #endif /* CONFIG_SMP */ |
444 | ||
029632fb PZ |
445 | #ifdef CONFIG_FAIR_GROUP_SCHED |
446 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ | |
447 | ||
448 | /* | |
449 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in | |
450 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities | |
451 | * (like users, containers etc.) | |
452 | * | |
453 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This | |
454 | * list is used during load balance. | |
455 | */ | |
456 | int on_list; | |
457 | struct list_head leaf_cfs_rq_list; | |
458 | struct task_group *tg; /* group that "owns" this runqueue */ | |
459 | ||
029632fb PZ |
460 | #ifdef CONFIG_CFS_BANDWIDTH |
461 | int runtime_enabled; | |
462 | u64 runtime_expires; | |
463 | s64 runtime_remaining; | |
464 | ||
f1b17280 PT |
465 | u64 throttled_clock, throttled_clock_task; |
466 | u64 throttled_clock_task_time; | |
55e16d30 | 467 | int throttled, throttle_count; |
029632fb PZ |
468 | struct list_head throttled_list; |
469 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
470 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
471 | }; | |
472 | ||
473 | static inline int rt_bandwidth_enabled(void) | |
474 | { | |
475 | return sysctl_sched_rt_runtime >= 0; | |
476 | } | |
477 | ||
b6366f04 SR |
478 | /* RT IPI pull logic requires IRQ_WORK */ |
479 | #ifdef CONFIG_IRQ_WORK | |
480 | # define HAVE_RT_PUSH_IPI | |
481 | #endif | |
482 | ||
029632fb PZ |
483 | /* Real-Time classes' related field in a runqueue: */ |
484 | struct rt_rq { | |
485 | struct rt_prio_array active; | |
c82513e5 | 486 | unsigned int rt_nr_running; |
01d36d0a | 487 | unsigned int rr_nr_running; |
029632fb PZ |
488 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
489 | struct { | |
490 | int curr; /* highest queued rt task prio */ | |
491 | #ifdef CONFIG_SMP | |
492 | int next; /* next highest */ | |
493 | #endif | |
494 | } highest_prio; | |
495 | #endif | |
496 | #ifdef CONFIG_SMP | |
497 | unsigned long rt_nr_migratory; | |
498 | unsigned long rt_nr_total; | |
499 | int overloaded; | |
500 | struct plist_head pushable_tasks; | |
b6366f04 SR |
501 | #ifdef HAVE_RT_PUSH_IPI |
502 | int push_flags; | |
503 | int push_cpu; | |
504 | struct irq_work push_work; | |
505 | raw_spinlock_t push_lock; | |
029632fb | 506 | #endif |
b6366f04 | 507 | #endif /* CONFIG_SMP */ |
f4ebcbc0 KT |
508 | int rt_queued; |
509 | ||
029632fb PZ |
510 | int rt_throttled; |
511 | u64 rt_time; | |
512 | u64 rt_runtime; | |
513 | /* Nests inside the rq lock: */ | |
514 | raw_spinlock_t rt_runtime_lock; | |
515 | ||
516 | #ifdef CONFIG_RT_GROUP_SCHED | |
517 | unsigned long rt_nr_boosted; | |
518 | ||
519 | struct rq *rq; | |
029632fb PZ |
520 | struct task_group *tg; |
521 | #endif | |
522 | }; | |
523 | ||
aab03e05 DF |
524 | /* Deadline class' related fields in a runqueue */ |
525 | struct dl_rq { | |
526 | /* runqueue is an rbtree, ordered by deadline */ | |
527 | struct rb_root rb_root; | |
528 | struct rb_node *rb_leftmost; | |
529 | ||
530 | unsigned long dl_nr_running; | |
1baca4ce JL |
531 | |
532 | #ifdef CONFIG_SMP | |
533 | /* | |
534 | * Deadline values of the currently executing and the | |
535 | * earliest ready task on this rq. Caching these facilitates | |
536 | * the decision wether or not a ready but not running task | |
537 | * should migrate somewhere else. | |
538 | */ | |
539 | struct { | |
540 | u64 curr; | |
541 | u64 next; | |
542 | } earliest_dl; | |
543 | ||
544 | unsigned long dl_nr_migratory; | |
1baca4ce JL |
545 | int overloaded; |
546 | ||
547 | /* | |
548 | * Tasks on this rq that can be pushed away. They are kept in | |
549 | * an rb-tree, ordered by tasks' deadlines, with caching | |
550 | * of the leftmost (earliest deadline) element. | |
551 | */ | |
552 | struct rb_root pushable_dl_tasks_root; | |
553 | struct rb_node *pushable_dl_tasks_leftmost; | |
332ac17e DF |
554 | #else |
555 | struct dl_bw dl_bw; | |
1baca4ce | 556 | #endif |
aab03e05 DF |
557 | }; |
558 | ||
029632fb PZ |
559 | #ifdef CONFIG_SMP |
560 | ||
afe06efd TC |
561 | static inline bool sched_asym_prefer(int a, int b) |
562 | { | |
563 | return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b); | |
564 | } | |
565 | ||
029632fb PZ |
566 | /* |
567 | * We add the notion of a root-domain which will be used to define per-domain | |
568 | * variables. Each exclusive cpuset essentially defines an island domain by | |
569 | * fully partitioning the member cpus from any other cpuset. Whenever a new | |
570 | * exclusive cpuset is created, we also create and attach a new root-domain | |
571 | * object. | |
572 | * | |
573 | */ | |
574 | struct root_domain { | |
575 | atomic_t refcount; | |
576 | atomic_t rto_count; | |
577 | struct rcu_head rcu; | |
578 | cpumask_var_t span; | |
579 | cpumask_var_t online; | |
580 | ||
4486edd1 TC |
581 | /* Indicate more than one runnable task for any CPU */ |
582 | bool overload; | |
583 | ||
1baca4ce JL |
584 | /* |
585 | * The bit corresponding to a CPU gets set here if such CPU has more | |
586 | * than one runnable -deadline task (as it is below for RT tasks). | |
587 | */ | |
588 | cpumask_var_t dlo_mask; | |
589 | atomic_t dlo_count; | |
332ac17e | 590 | struct dl_bw dl_bw; |
6bfd6d72 | 591 | struct cpudl cpudl; |
1baca4ce | 592 | |
029632fb PZ |
593 | /* |
594 | * The "RT overload" flag: it gets set if a CPU has more than | |
595 | * one runnable RT task. | |
596 | */ | |
597 | cpumask_var_t rto_mask; | |
598 | struct cpupri cpupri; | |
cd92bfd3 DE |
599 | |
600 | unsigned long max_cpu_capacity; | |
029632fb PZ |
601 | }; |
602 | ||
603 | extern struct root_domain def_root_domain; | |
f2cb1360 IM |
604 | extern struct mutex sched_domains_mutex; |
605 | extern cpumask_var_t fallback_doms; | |
606 | extern cpumask_var_t sched_domains_tmpmask; | |
607 | ||
608 | extern void init_defrootdomain(void); | |
609 | extern int init_sched_domains(const struct cpumask *cpu_map); | |
610 | extern void rq_attach_root(struct rq *rq, struct root_domain *rd); | |
029632fb PZ |
611 | |
612 | #endif /* CONFIG_SMP */ | |
613 | ||
614 | /* | |
615 | * This is the main, per-CPU runqueue data structure. | |
616 | * | |
617 | * Locking rule: those places that want to lock multiple runqueues | |
618 | * (such as the load balancing or the thread migration code), lock | |
619 | * acquire operations must be ordered by ascending &runqueue. | |
620 | */ | |
621 | struct rq { | |
622 | /* runqueue lock: */ | |
623 | raw_spinlock_t lock; | |
624 | ||
625 | /* | |
626 | * nr_running and cpu_load should be in the same cacheline because | |
627 | * remote CPUs use both these fields when doing load calculation. | |
628 | */ | |
c82513e5 | 629 | unsigned int nr_running; |
0ec8aa00 PZ |
630 | #ifdef CONFIG_NUMA_BALANCING |
631 | unsigned int nr_numa_running; | |
632 | unsigned int nr_preferred_running; | |
633 | #endif | |
029632fb PZ |
634 | #define CPU_LOAD_IDX_MAX 5 |
635 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; | |
3451d024 | 636 | #ifdef CONFIG_NO_HZ_COMMON |
9fd81dd5 FW |
637 | #ifdef CONFIG_SMP |
638 | unsigned long last_load_update_tick; | |
639 | #endif /* CONFIG_SMP */ | |
1c792db7 | 640 | unsigned long nohz_flags; |
9fd81dd5 | 641 | #endif /* CONFIG_NO_HZ_COMMON */ |
265f22a9 FW |
642 | #ifdef CONFIG_NO_HZ_FULL |
643 | unsigned long last_sched_tick; | |
029632fb | 644 | #endif |
029632fb PZ |
645 | /* capture load from *all* tasks on this cpu: */ |
646 | struct load_weight load; | |
647 | unsigned long nr_load_updates; | |
648 | u64 nr_switches; | |
649 | ||
650 | struct cfs_rq cfs; | |
651 | struct rt_rq rt; | |
aab03e05 | 652 | struct dl_rq dl; |
029632fb PZ |
653 | |
654 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
655 | /* list of leaf cfs_rq on this cpu: */ | |
656 | struct list_head leaf_cfs_rq_list; | |
9c2791f9 | 657 | struct list_head *tmp_alone_branch; |
a35b6466 PZ |
658 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
659 | ||
029632fb PZ |
660 | /* |
661 | * This is part of a global counter where only the total sum | |
662 | * over all CPUs matters. A task can increase this counter on | |
663 | * one CPU and if it got migrated afterwards it may decrease | |
664 | * it on another CPU. Always updated under the runqueue lock: | |
665 | */ | |
666 | unsigned long nr_uninterruptible; | |
667 | ||
668 | struct task_struct *curr, *idle, *stop; | |
669 | unsigned long next_balance; | |
670 | struct mm_struct *prev_mm; | |
671 | ||
cb42c9a3 | 672 | unsigned int clock_update_flags; |
029632fb PZ |
673 | u64 clock; |
674 | u64 clock_task; | |
675 | ||
676 | atomic_t nr_iowait; | |
677 | ||
678 | #ifdef CONFIG_SMP | |
679 | struct root_domain *rd; | |
680 | struct sched_domain *sd; | |
681 | ||
ced549fa | 682 | unsigned long cpu_capacity; |
ca6d75e6 | 683 | unsigned long cpu_capacity_orig; |
029632fb | 684 | |
e3fca9e7 PZ |
685 | struct callback_head *balance_callback; |
686 | ||
029632fb PZ |
687 | unsigned char idle_balance; |
688 | /* For active balancing */ | |
029632fb PZ |
689 | int active_balance; |
690 | int push_cpu; | |
691 | struct cpu_stop_work active_balance_work; | |
692 | /* cpu of this runqueue: */ | |
693 | int cpu; | |
694 | int online; | |
695 | ||
367456c7 PZ |
696 | struct list_head cfs_tasks; |
697 | ||
029632fb PZ |
698 | u64 rt_avg; |
699 | u64 age_stamp; | |
700 | u64 idle_stamp; | |
701 | u64 avg_idle; | |
9bd721c5 JL |
702 | |
703 | /* This is used to determine avg_idle's max value */ | |
704 | u64 max_idle_balance_cost; | |
029632fb PZ |
705 | #endif |
706 | ||
707 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | |
708 | u64 prev_irq_time; | |
709 | #endif | |
710 | #ifdef CONFIG_PARAVIRT | |
711 | u64 prev_steal_time; | |
712 | #endif | |
713 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | |
714 | u64 prev_steal_time_rq; | |
715 | #endif | |
716 | ||
717 | /* calc_load related fields */ | |
718 | unsigned long calc_load_update; | |
719 | long calc_load_active; | |
720 | ||
721 | #ifdef CONFIG_SCHED_HRTICK | |
722 | #ifdef CONFIG_SMP | |
723 | int hrtick_csd_pending; | |
724 | struct call_single_data hrtick_csd; | |
725 | #endif | |
726 | struct hrtimer hrtick_timer; | |
727 | #endif | |
728 | ||
729 | #ifdef CONFIG_SCHEDSTATS | |
730 | /* latency stats */ | |
731 | struct sched_info rq_sched_info; | |
732 | unsigned long long rq_cpu_time; | |
733 | /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ | |
734 | ||
735 | /* sys_sched_yield() stats */ | |
736 | unsigned int yld_count; | |
737 | ||
738 | /* schedule() stats */ | |
029632fb PZ |
739 | unsigned int sched_count; |
740 | unsigned int sched_goidle; | |
741 | ||
742 | /* try_to_wake_up() stats */ | |
743 | unsigned int ttwu_count; | |
744 | unsigned int ttwu_local; | |
745 | #endif | |
746 | ||
747 | #ifdef CONFIG_SMP | |
748 | struct llist_head wake_list; | |
749 | #endif | |
442bf3aa DL |
750 | |
751 | #ifdef CONFIG_CPU_IDLE | |
752 | /* Must be inspected within a rcu lock section */ | |
753 | struct cpuidle_state *idle_state; | |
754 | #endif | |
029632fb PZ |
755 | }; |
756 | ||
757 | static inline int cpu_of(struct rq *rq) | |
758 | { | |
759 | #ifdef CONFIG_SMP | |
760 | return rq->cpu; | |
761 | #else | |
762 | return 0; | |
763 | #endif | |
764 | } | |
765 | ||
1b568f0a PZ |
766 | |
767 | #ifdef CONFIG_SCHED_SMT | |
768 | ||
769 | extern struct static_key_false sched_smt_present; | |
770 | ||
771 | extern void __update_idle_core(struct rq *rq); | |
772 | ||
773 | static inline void update_idle_core(struct rq *rq) | |
774 | { | |
775 | if (static_branch_unlikely(&sched_smt_present)) | |
776 | __update_idle_core(rq); | |
777 | } | |
778 | ||
779 | #else | |
780 | static inline void update_idle_core(struct rq *rq) { } | |
781 | #endif | |
782 | ||
8b06c55b | 783 | DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
029632fb | 784 | |
518cd623 | 785 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
4a32fea9 | 786 | #define this_rq() this_cpu_ptr(&runqueues) |
518cd623 PZ |
787 | #define task_rq(p) cpu_rq(task_cpu(p)) |
788 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) | |
4a32fea9 | 789 | #define raw_rq() raw_cpu_ptr(&runqueues) |
518cd623 | 790 | |
cebde6d6 PZ |
791 | static inline u64 __rq_clock_broken(struct rq *rq) |
792 | { | |
316c1608 | 793 | return READ_ONCE(rq->clock); |
cebde6d6 PZ |
794 | } |
795 | ||
cb42c9a3 MF |
796 | /* |
797 | * rq::clock_update_flags bits | |
798 | * | |
799 | * %RQCF_REQ_SKIP - will request skipping of clock update on the next | |
800 | * call to __schedule(). This is an optimisation to avoid | |
801 | * neighbouring rq clock updates. | |
802 | * | |
803 | * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is | |
804 | * in effect and calls to update_rq_clock() are being ignored. | |
805 | * | |
806 | * %RQCF_UPDATED - is a debug flag that indicates whether a call has been | |
807 | * made to update_rq_clock() since the last time rq::lock was pinned. | |
808 | * | |
809 | * If inside of __schedule(), clock_update_flags will have been | |
810 | * shifted left (a left shift is a cheap operation for the fast path | |
811 | * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use, | |
812 | * | |
813 | * if (rq-clock_update_flags >= RQCF_UPDATED) | |
814 | * | |
815 | * to check if %RQCF_UPADTED is set. It'll never be shifted more than | |
816 | * one position though, because the next rq_unpin_lock() will shift it | |
817 | * back. | |
818 | */ | |
819 | #define RQCF_REQ_SKIP 0x01 | |
820 | #define RQCF_ACT_SKIP 0x02 | |
821 | #define RQCF_UPDATED 0x04 | |
822 | ||
823 | static inline void assert_clock_updated(struct rq *rq) | |
824 | { | |
825 | /* | |
826 | * The only reason for not seeing a clock update since the | |
827 | * last rq_pin_lock() is if we're currently skipping updates. | |
828 | */ | |
829 | SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP); | |
830 | } | |
831 | ||
78becc27 FW |
832 | static inline u64 rq_clock(struct rq *rq) |
833 | { | |
cebde6d6 | 834 | lockdep_assert_held(&rq->lock); |
cb42c9a3 MF |
835 | assert_clock_updated(rq); |
836 | ||
78becc27 FW |
837 | return rq->clock; |
838 | } | |
839 | ||
840 | static inline u64 rq_clock_task(struct rq *rq) | |
841 | { | |
cebde6d6 | 842 | lockdep_assert_held(&rq->lock); |
cb42c9a3 MF |
843 | assert_clock_updated(rq); |
844 | ||
78becc27 FW |
845 | return rq->clock_task; |
846 | } | |
847 | ||
9edfbfed PZ |
848 | static inline void rq_clock_skip_update(struct rq *rq, bool skip) |
849 | { | |
850 | lockdep_assert_held(&rq->lock); | |
851 | if (skip) | |
cb42c9a3 | 852 | rq->clock_update_flags |= RQCF_REQ_SKIP; |
9edfbfed | 853 | else |
cb42c9a3 | 854 | rq->clock_update_flags &= ~RQCF_REQ_SKIP; |
9edfbfed PZ |
855 | } |
856 | ||
d8ac8971 MF |
857 | struct rq_flags { |
858 | unsigned long flags; | |
859 | struct pin_cookie cookie; | |
cb42c9a3 MF |
860 | #ifdef CONFIG_SCHED_DEBUG |
861 | /* | |
862 | * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the | |
863 | * current pin context is stashed here in case it needs to be | |
864 | * restored in rq_repin_lock(). | |
865 | */ | |
866 | unsigned int clock_update_flags; | |
867 | #endif | |
d8ac8971 MF |
868 | }; |
869 | ||
870 | static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf) | |
871 | { | |
872 | rf->cookie = lockdep_pin_lock(&rq->lock); | |
cb42c9a3 MF |
873 | |
874 | #ifdef CONFIG_SCHED_DEBUG | |
875 | rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); | |
876 | rf->clock_update_flags = 0; | |
877 | #endif | |
d8ac8971 MF |
878 | } |
879 | ||
880 | static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf) | |
881 | { | |
cb42c9a3 MF |
882 | #ifdef CONFIG_SCHED_DEBUG |
883 | if (rq->clock_update_flags > RQCF_ACT_SKIP) | |
884 | rf->clock_update_flags = RQCF_UPDATED; | |
885 | #endif | |
886 | ||
d8ac8971 MF |
887 | lockdep_unpin_lock(&rq->lock, rf->cookie); |
888 | } | |
889 | ||
890 | static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf) | |
891 | { | |
892 | lockdep_repin_lock(&rq->lock, rf->cookie); | |
cb42c9a3 MF |
893 | |
894 | #ifdef CONFIG_SCHED_DEBUG | |
895 | /* | |
896 | * Restore the value we stashed in @rf for this pin context. | |
897 | */ | |
898 | rq->clock_update_flags |= rf->clock_update_flags; | |
899 | #endif | |
d8ac8971 MF |
900 | } |
901 | ||
9942f79b | 902 | #ifdef CONFIG_NUMA |
e3fe70b1 RR |
903 | enum numa_topology_type { |
904 | NUMA_DIRECT, | |
905 | NUMA_GLUELESS_MESH, | |
906 | NUMA_BACKPLANE, | |
907 | }; | |
908 | extern enum numa_topology_type sched_numa_topology_type; | |
9942f79b RR |
909 | extern int sched_max_numa_distance; |
910 | extern bool find_numa_distance(int distance); | |
911 | #endif | |
912 | ||
f2cb1360 IM |
913 | #ifdef CONFIG_NUMA |
914 | extern void sched_init_numa(void); | |
915 | extern void sched_domains_numa_masks_set(unsigned int cpu); | |
916 | extern void sched_domains_numa_masks_clear(unsigned int cpu); | |
917 | #else | |
918 | static inline void sched_init_numa(void) { } | |
919 | static inline void sched_domains_numa_masks_set(unsigned int cpu) { } | |
920 | static inline void sched_domains_numa_masks_clear(unsigned int cpu) { } | |
921 | #endif | |
922 | ||
f809ca9a | 923 | #ifdef CONFIG_NUMA_BALANCING |
44dba3d5 IM |
924 | /* The regions in numa_faults array from task_struct */ |
925 | enum numa_faults_stats { | |
926 | NUMA_MEM = 0, | |
927 | NUMA_CPU, | |
928 | NUMA_MEMBUF, | |
929 | NUMA_CPUBUF | |
930 | }; | |
0ec8aa00 | 931 | extern void sched_setnuma(struct task_struct *p, int node); |
e6628d5b | 932 | extern int migrate_task_to(struct task_struct *p, int cpu); |
ac66f547 | 933 | extern int migrate_swap(struct task_struct *, struct task_struct *); |
f809ca9a MG |
934 | #endif /* CONFIG_NUMA_BALANCING */ |
935 | ||
518cd623 PZ |
936 | #ifdef CONFIG_SMP |
937 | ||
e3fca9e7 PZ |
938 | static inline void |
939 | queue_balance_callback(struct rq *rq, | |
940 | struct callback_head *head, | |
941 | void (*func)(struct rq *rq)) | |
942 | { | |
943 | lockdep_assert_held(&rq->lock); | |
944 | ||
945 | if (unlikely(head->next)) | |
946 | return; | |
947 | ||
948 | head->func = (void (*)(struct callback_head *))func; | |
949 | head->next = rq->balance_callback; | |
950 | rq->balance_callback = head; | |
951 | } | |
952 | ||
e3baac47 PZ |
953 | extern void sched_ttwu_pending(void); |
954 | ||
029632fb PZ |
955 | #define rcu_dereference_check_sched_domain(p) \ |
956 | rcu_dereference_check((p), \ | |
957 | lockdep_is_held(&sched_domains_mutex)) | |
958 | ||
959 | /* | |
960 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. | |
961 | * See detach_destroy_domains: synchronize_sched for details. | |
962 | * | |
963 | * The domain tree of any CPU may only be accessed from within | |
964 | * preempt-disabled sections. | |
965 | */ | |
966 | #define for_each_domain(cpu, __sd) \ | |
518cd623 PZ |
967 | for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \ |
968 | __sd; __sd = __sd->parent) | |
029632fb | 969 | |
77e81365 SS |
970 | #define for_each_lower_domain(sd) for (; sd; sd = sd->child) |
971 | ||
518cd623 PZ |
972 | /** |
973 | * highest_flag_domain - Return highest sched_domain containing flag. | |
974 | * @cpu: The cpu whose highest level of sched domain is to | |
975 | * be returned. | |
976 | * @flag: The flag to check for the highest sched_domain | |
977 | * for the given cpu. | |
978 | * | |
979 | * Returns the highest sched_domain of a cpu which contains the given flag. | |
980 | */ | |
981 | static inline struct sched_domain *highest_flag_domain(int cpu, int flag) | |
982 | { | |
983 | struct sched_domain *sd, *hsd = NULL; | |
984 | ||
985 | for_each_domain(cpu, sd) { | |
986 | if (!(sd->flags & flag)) | |
987 | break; | |
988 | hsd = sd; | |
989 | } | |
990 | ||
991 | return hsd; | |
992 | } | |
993 | ||
fb13c7ee MG |
994 | static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) |
995 | { | |
996 | struct sched_domain *sd; | |
997 | ||
998 | for_each_domain(cpu, sd) { | |
999 | if (sd->flags & flag) | |
1000 | break; | |
1001 | } | |
1002 | ||
1003 | return sd; | |
1004 | } | |
1005 | ||
518cd623 | 1006 | DECLARE_PER_CPU(struct sched_domain *, sd_llc); |
7d9ffa89 | 1007 | DECLARE_PER_CPU(int, sd_llc_size); |
518cd623 | 1008 | DECLARE_PER_CPU(int, sd_llc_id); |
0e369d75 | 1009 | DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared); |
fb13c7ee | 1010 | DECLARE_PER_CPU(struct sched_domain *, sd_numa); |
37dc6b50 | 1011 | DECLARE_PER_CPU(struct sched_domain *, sd_asym); |
518cd623 | 1012 | |
63b2ca30 | 1013 | struct sched_group_capacity { |
5e6521ea LZ |
1014 | atomic_t ref; |
1015 | /* | |
172895e6 | 1016 | * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity |
63b2ca30 | 1017 | * for a single CPU. |
5e6521ea | 1018 | */ |
bf475ce0 MR |
1019 | unsigned long capacity; |
1020 | unsigned long min_capacity; /* Min per-CPU capacity in group */ | |
5e6521ea | 1021 | unsigned long next_update; |
63b2ca30 | 1022 | int imbalance; /* XXX unrelated to capacity but shared group state */ |
5e6521ea LZ |
1023 | |
1024 | unsigned long cpumask[0]; /* iteration mask */ | |
1025 | }; | |
1026 | ||
1027 | struct sched_group { | |
1028 | struct sched_group *next; /* Must be a circular list */ | |
1029 | atomic_t ref; | |
1030 | ||
1031 | unsigned int group_weight; | |
63b2ca30 | 1032 | struct sched_group_capacity *sgc; |
afe06efd | 1033 | int asym_prefer_cpu; /* cpu of highest priority in group */ |
5e6521ea LZ |
1034 | |
1035 | /* | |
1036 | * The CPUs this group covers. | |
1037 | * | |
1038 | * NOTE: this field is variable length. (Allocated dynamically | |
1039 | * by attaching extra space to the end of the structure, | |
1040 | * depending on how many CPUs the kernel has booted up with) | |
1041 | */ | |
1042 | unsigned long cpumask[0]; | |
1043 | }; | |
1044 | ||
1045 | static inline struct cpumask *sched_group_cpus(struct sched_group *sg) | |
1046 | { | |
1047 | return to_cpumask(sg->cpumask); | |
1048 | } | |
1049 | ||
1050 | /* | |
1051 | * cpumask masking which cpus in the group are allowed to iterate up the domain | |
1052 | * tree. | |
1053 | */ | |
1054 | static inline struct cpumask *sched_group_mask(struct sched_group *sg) | |
1055 | { | |
63b2ca30 | 1056 | return to_cpumask(sg->sgc->cpumask); |
5e6521ea LZ |
1057 | } |
1058 | ||
1059 | /** | |
1060 | * group_first_cpu - Returns the first cpu in the cpumask of a sched_group. | |
1061 | * @group: The group whose first cpu is to be returned. | |
1062 | */ | |
1063 | static inline unsigned int group_first_cpu(struct sched_group *group) | |
1064 | { | |
1065 | return cpumask_first(sched_group_cpus(group)); | |
1066 | } | |
1067 | ||
c1174876 PZ |
1068 | extern int group_balance_cpu(struct sched_group *sg); |
1069 | ||
3866e845 SRRH |
1070 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
1071 | void register_sched_domain_sysctl(void); | |
1072 | void unregister_sched_domain_sysctl(void); | |
1073 | #else | |
1074 | static inline void register_sched_domain_sysctl(void) | |
1075 | { | |
1076 | } | |
1077 | static inline void unregister_sched_domain_sysctl(void) | |
1078 | { | |
1079 | } | |
1080 | #endif | |
1081 | ||
e3baac47 PZ |
1082 | #else |
1083 | ||
1084 | static inline void sched_ttwu_pending(void) { } | |
1085 | ||
518cd623 | 1086 | #endif /* CONFIG_SMP */ |
029632fb | 1087 | |
391e43da | 1088 | #include "stats.h" |
1051408f | 1089 | #include "autogroup.h" |
029632fb PZ |
1090 | |
1091 | #ifdef CONFIG_CGROUP_SCHED | |
1092 | ||
1093 | /* | |
1094 | * Return the group to which this tasks belongs. | |
1095 | * | |
8af01f56 TH |
1096 | * We cannot use task_css() and friends because the cgroup subsystem |
1097 | * changes that value before the cgroup_subsys::attach() method is called, | |
1098 | * therefore we cannot pin it and might observe the wrong value. | |
8323f26c PZ |
1099 | * |
1100 | * The same is true for autogroup's p->signal->autogroup->tg, the autogroup | |
1101 | * core changes this before calling sched_move_task(). | |
1102 | * | |
1103 | * Instead we use a 'copy' which is updated from sched_move_task() while | |
1104 | * holding both task_struct::pi_lock and rq::lock. | |
029632fb PZ |
1105 | */ |
1106 | static inline struct task_group *task_group(struct task_struct *p) | |
1107 | { | |
8323f26c | 1108 | return p->sched_task_group; |
029632fb PZ |
1109 | } |
1110 | ||
1111 | /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ | |
1112 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) | |
1113 | { | |
1114 | #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED) | |
1115 | struct task_group *tg = task_group(p); | |
1116 | #endif | |
1117 | ||
1118 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
ad936d86 | 1119 | set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]); |
029632fb PZ |
1120 | p->se.cfs_rq = tg->cfs_rq[cpu]; |
1121 | p->se.parent = tg->se[cpu]; | |
1122 | #endif | |
1123 | ||
1124 | #ifdef CONFIG_RT_GROUP_SCHED | |
1125 | p->rt.rt_rq = tg->rt_rq[cpu]; | |
1126 | p->rt.parent = tg->rt_se[cpu]; | |
1127 | #endif | |
1128 | } | |
1129 | ||
1130 | #else /* CONFIG_CGROUP_SCHED */ | |
1131 | ||
1132 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } | |
1133 | static inline struct task_group *task_group(struct task_struct *p) | |
1134 | { | |
1135 | return NULL; | |
1136 | } | |
1137 | ||
1138 | #endif /* CONFIG_CGROUP_SCHED */ | |
1139 | ||
1140 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) | |
1141 | { | |
1142 | set_task_rq(p, cpu); | |
1143 | #ifdef CONFIG_SMP | |
1144 | /* | |
1145 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be | |
1146 | * successfuly executed on another CPU. We must ensure that updates of | |
1147 | * per-task data have been completed by this moment. | |
1148 | */ | |
1149 | smp_wmb(); | |
c65eacbe AL |
1150 | #ifdef CONFIG_THREAD_INFO_IN_TASK |
1151 | p->cpu = cpu; | |
1152 | #else | |
029632fb | 1153 | task_thread_info(p)->cpu = cpu; |
c65eacbe | 1154 | #endif |
ac66f547 | 1155 | p->wake_cpu = cpu; |
029632fb PZ |
1156 | #endif |
1157 | } | |
1158 | ||
1159 | /* | |
1160 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: | |
1161 | */ | |
1162 | #ifdef CONFIG_SCHED_DEBUG | |
c5905afb | 1163 | # include <linux/static_key.h> |
029632fb PZ |
1164 | # define const_debug __read_mostly |
1165 | #else | |
1166 | # define const_debug const | |
1167 | #endif | |
1168 | ||
1169 | extern const_debug unsigned int sysctl_sched_features; | |
1170 | ||
1171 | #define SCHED_FEAT(name, enabled) \ | |
1172 | __SCHED_FEAT_##name , | |
1173 | ||
1174 | enum { | |
391e43da | 1175 | #include "features.h" |
f8b6d1cc | 1176 | __SCHED_FEAT_NR, |
029632fb PZ |
1177 | }; |
1178 | ||
1179 | #undef SCHED_FEAT | |
1180 | ||
f8b6d1cc | 1181 | #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL) |
f8b6d1cc | 1182 | #define SCHED_FEAT(name, enabled) \ |
c5905afb | 1183 | static __always_inline bool static_branch_##name(struct static_key *key) \ |
f8b6d1cc | 1184 | { \ |
6e76ea8a | 1185 | return static_key_##enabled(key); \ |
f8b6d1cc PZ |
1186 | } |
1187 | ||
1188 | #include "features.h" | |
1189 | ||
1190 | #undef SCHED_FEAT | |
1191 | ||
c5905afb | 1192 | extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; |
f8b6d1cc PZ |
1193 | #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x])) |
1194 | #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */ | |
029632fb | 1195 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) |
f8b6d1cc | 1196 | #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */ |
029632fb | 1197 | |
2a595721 | 1198 | extern struct static_key_false sched_numa_balancing; |
cb251765 | 1199 | extern struct static_key_false sched_schedstats; |
cbee9f88 | 1200 | |
029632fb PZ |
1201 | static inline u64 global_rt_period(void) |
1202 | { | |
1203 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; | |
1204 | } | |
1205 | ||
1206 | static inline u64 global_rt_runtime(void) | |
1207 | { | |
1208 | if (sysctl_sched_rt_runtime < 0) | |
1209 | return RUNTIME_INF; | |
1210 | ||
1211 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; | |
1212 | } | |
1213 | ||
029632fb PZ |
1214 | static inline int task_current(struct rq *rq, struct task_struct *p) |
1215 | { | |
1216 | return rq->curr == p; | |
1217 | } | |
1218 | ||
1219 | static inline int task_running(struct rq *rq, struct task_struct *p) | |
1220 | { | |
1221 | #ifdef CONFIG_SMP | |
1222 | return p->on_cpu; | |
1223 | #else | |
1224 | return task_current(rq, p); | |
1225 | #endif | |
1226 | } | |
1227 | ||
da0c1e65 KT |
1228 | static inline int task_on_rq_queued(struct task_struct *p) |
1229 | { | |
1230 | return p->on_rq == TASK_ON_RQ_QUEUED; | |
1231 | } | |
029632fb | 1232 | |
cca26e80 KT |
1233 | static inline int task_on_rq_migrating(struct task_struct *p) |
1234 | { | |
1235 | return p->on_rq == TASK_ON_RQ_MIGRATING; | |
1236 | } | |
1237 | ||
029632fb PZ |
1238 | #ifndef prepare_arch_switch |
1239 | # define prepare_arch_switch(next) do { } while (0) | |
1240 | #endif | |
01f23e16 CM |
1241 | #ifndef finish_arch_post_lock_switch |
1242 | # define finish_arch_post_lock_switch() do { } while (0) | |
1243 | #endif | |
029632fb | 1244 | |
029632fb PZ |
1245 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
1246 | { | |
1247 | #ifdef CONFIG_SMP | |
1248 | /* | |
1249 | * We can optimise this out completely for !SMP, because the | |
1250 | * SMP rebalancing from interrupt is the only thing that cares | |
1251 | * here. | |
1252 | */ | |
1253 | next->on_cpu = 1; | |
1254 | #endif | |
1255 | } | |
1256 | ||
1257 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) | |
1258 | { | |
1259 | #ifdef CONFIG_SMP | |
1260 | /* | |
1261 | * After ->on_cpu is cleared, the task can be moved to a different CPU. | |
1262 | * We must ensure this doesn't happen until the switch is completely | |
1263 | * finished. | |
95913d97 | 1264 | * |
b75a2253 PZ |
1265 | * In particular, the load of prev->state in finish_task_switch() must |
1266 | * happen before this. | |
1267 | * | |
1f03e8d2 | 1268 | * Pairs with the smp_cond_load_acquire() in try_to_wake_up(). |
029632fb | 1269 | */ |
95913d97 | 1270 | smp_store_release(&prev->on_cpu, 0); |
029632fb PZ |
1271 | #endif |
1272 | #ifdef CONFIG_DEBUG_SPINLOCK | |
1273 | /* this is a valid case when another task releases the spinlock */ | |
1274 | rq->lock.owner = current; | |
1275 | #endif | |
1276 | /* | |
1277 | * If we are tracking spinlock dependencies then we have to | |
1278 | * fix up the runqueue lock - which gets 'carried over' from | |
1279 | * prev into current: | |
1280 | */ | |
1281 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); | |
1282 | ||
1283 | raw_spin_unlock_irq(&rq->lock); | |
1284 | } | |
1285 | ||
b13095f0 LZ |
1286 | /* |
1287 | * wake flags | |
1288 | */ | |
1289 | #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */ | |
1290 | #define WF_FORK 0x02 /* child wakeup after fork */ | |
1291 | #define WF_MIGRATED 0x4 /* internal use, task got migrated */ | |
1292 | ||
029632fb PZ |
1293 | /* |
1294 | * To aid in avoiding the subversion of "niceness" due to uneven distribution | |
1295 | * of tasks with abnormal "nice" values across CPUs the contribution that | |
1296 | * each task makes to its run queue's load is weighted according to its | |
1297 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a | |
1298 | * scaled version of the new time slice allocation that they receive on time | |
1299 | * slice expiry etc. | |
1300 | */ | |
1301 | ||
1302 | #define WEIGHT_IDLEPRIO 3 | |
1303 | #define WMULT_IDLEPRIO 1431655765 | |
1304 | ||
ed82b8a1 AK |
1305 | extern const int sched_prio_to_weight[40]; |
1306 | extern const u32 sched_prio_to_wmult[40]; | |
029632fb | 1307 | |
ff77e468 PZ |
1308 | /* |
1309 | * {de,en}queue flags: | |
1310 | * | |
1311 | * DEQUEUE_SLEEP - task is no longer runnable | |
1312 | * ENQUEUE_WAKEUP - task just became runnable | |
1313 | * | |
1314 | * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks | |
1315 | * are in a known state which allows modification. Such pairs | |
1316 | * should preserve as much state as possible. | |
1317 | * | |
1318 | * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location | |
1319 | * in the runqueue. | |
1320 | * | |
1321 | * ENQUEUE_HEAD - place at front of runqueue (tail if not specified) | |
1322 | * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline) | |
59efa0ba | 1323 | * ENQUEUE_MIGRATED - the task was migrated during wakeup |
ff77e468 PZ |
1324 | * |
1325 | */ | |
1326 | ||
1327 | #define DEQUEUE_SLEEP 0x01 | |
1328 | #define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */ | |
1329 | #define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */ | |
1330 | ||
1de64443 | 1331 | #define ENQUEUE_WAKEUP 0x01 |
ff77e468 PZ |
1332 | #define ENQUEUE_RESTORE 0x02 |
1333 | #define ENQUEUE_MOVE 0x04 | |
1334 | ||
1335 | #define ENQUEUE_HEAD 0x08 | |
1336 | #define ENQUEUE_REPLENISH 0x10 | |
c82ba9fa | 1337 | #ifdef CONFIG_SMP |
59efa0ba | 1338 | #define ENQUEUE_MIGRATED 0x20 |
c82ba9fa | 1339 | #else |
59efa0ba | 1340 | #define ENQUEUE_MIGRATED 0x00 |
c82ba9fa | 1341 | #endif |
c82ba9fa | 1342 | |
37e117c0 PZ |
1343 | #define RETRY_TASK ((void *)-1UL) |
1344 | ||
c82ba9fa LZ |
1345 | struct sched_class { |
1346 | const struct sched_class *next; | |
1347 | ||
1348 | void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags); | |
1349 | void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags); | |
1350 | void (*yield_task) (struct rq *rq); | |
1351 | bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt); | |
1352 | ||
1353 | void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags); | |
1354 | ||
606dba2e PZ |
1355 | /* |
1356 | * It is the responsibility of the pick_next_task() method that will | |
1357 | * return the next task to call put_prev_task() on the @prev task or | |
1358 | * something equivalent. | |
37e117c0 PZ |
1359 | * |
1360 | * May return RETRY_TASK when it finds a higher prio class has runnable | |
1361 | * tasks. | |
606dba2e PZ |
1362 | */ |
1363 | struct task_struct * (*pick_next_task) (struct rq *rq, | |
e7904a28 | 1364 | struct task_struct *prev, |
d8ac8971 | 1365 | struct rq_flags *rf); |
c82ba9fa LZ |
1366 | void (*put_prev_task) (struct rq *rq, struct task_struct *p); |
1367 | ||
1368 | #ifdef CONFIG_SMP | |
ac66f547 | 1369 | int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags); |
5a4fd036 | 1370 | void (*migrate_task_rq)(struct task_struct *p); |
c82ba9fa | 1371 | |
c82ba9fa LZ |
1372 | void (*task_woken) (struct rq *this_rq, struct task_struct *task); |
1373 | ||
1374 | void (*set_cpus_allowed)(struct task_struct *p, | |
1375 | const struct cpumask *newmask); | |
1376 | ||
1377 | void (*rq_online)(struct rq *rq); | |
1378 | void (*rq_offline)(struct rq *rq); | |
1379 | #endif | |
1380 | ||
1381 | void (*set_curr_task) (struct rq *rq); | |
1382 | void (*task_tick) (struct rq *rq, struct task_struct *p, int queued); | |
1383 | void (*task_fork) (struct task_struct *p); | |
e6c390f2 | 1384 | void (*task_dead) (struct task_struct *p); |
c82ba9fa | 1385 | |
67dfa1b7 KT |
1386 | /* |
1387 | * The switched_from() call is allowed to drop rq->lock, therefore we | |
1388 | * cannot assume the switched_from/switched_to pair is serliazed by | |
1389 | * rq->lock. They are however serialized by p->pi_lock. | |
1390 | */ | |
c82ba9fa LZ |
1391 | void (*switched_from) (struct rq *this_rq, struct task_struct *task); |
1392 | void (*switched_to) (struct rq *this_rq, struct task_struct *task); | |
1393 | void (*prio_changed) (struct rq *this_rq, struct task_struct *task, | |
1394 | int oldprio); | |
1395 | ||
1396 | unsigned int (*get_rr_interval) (struct rq *rq, | |
1397 | struct task_struct *task); | |
1398 | ||
6e998916 SG |
1399 | void (*update_curr) (struct rq *rq); |
1400 | ||
ea86cb4b VG |
1401 | #define TASK_SET_GROUP 0 |
1402 | #define TASK_MOVE_GROUP 1 | |
1403 | ||
c82ba9fa | 1404 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b | 1405 | void (*task_change_group) (struct task_struct *p, int type); |
c82ba9fa LZ |
1406 | #endif |
1407 | }; | |
029632fb | 1408 | |
3f1d2a31 PZ |
1409 | static inline void put_prev_task(struct rq *rq, struct task_struct *prev) |
1410 | { | |
1411 | prev->sched_class->put_prev_task(rq, prev); | |
1412 | } | |
1413 | ||
b2bf6c31 PZ |
1414 | static inline void set_curr_task(struct rq *rq, struct task_struct *curr) |
1415 | { | |
1416 | curr->sched_class->set_curr_task(rq); | |
1417 | } | |
1418 | ||
029632fb PZ |
1419 | #define sched_class_highest (&stop_sched_class) |
1420 | #define for_each_class(class) \ | |
1421 | for (class = sched_class_highest; class; class = class->next) | |
1422 | ||
1423 | extern const struct sched_class stop_sched_class; | |
aab03e05 | 1424 | extern const struct sched_class dl_sched_class; |
029632fb PZ |
1425 | extern const struct sched_class rt_sched_class; |
1426 | extern const struct sched_class fair_sched_class; | |
1427 | extern const struct sched_class idle_sched_class; | |
1428 | ||
1429 | ||
1430 | #ifdef CONFIG_SMP | |
1431 | ||
63b2ca30 | 1432 | extern void update_group_capacity(struct sched_domain *sd, int cpu); |
b719203b | 1433 | |
7caff66f | 1434 | extern void trigger_load_balance(struct rq *rq); |
029632fb | 1435 | |
c5b28038 PZ |
1436 | extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask); |
1437 | ||
029632fb PZ |
1438 | #endif |
1439 | ||
442bf3aa DL |
1440 | #ifdef CONFIG_CPU_IDLE |
1441 | static inline void idle_set_state(struct rq *rq, | |
1442 | struct cpuidle_state *idle_state) | |
1443 | { | |
1444 | rq->idle_state = idle_state; | |
1445 | } | |
1446 | ||
1447 | static inline struct cpuidle_state *idle_get_state(struct rq *rq) | |
1448 | { | |
9148a3a1 | 1449 | SCHED_WARN_ON(!rcu_read_lock_held()); |
442bf3aa DL |
1450 | return rq->idle_state; |
1451 | } | |
1452 | #else | |
1453 | static inline void idle_set_state(struct rq *rq, | |
1454 | struct cpuidle_state *idle_state) | |
1455 | { | |
1456 | } | |
1457 | ||
1458 | static inline struct cpuidle_state *idle_get_state(struct rq *rq) | |
1459 | { | |
1460 | return NULL; | |
1461 | } | |
1462 | #endif | |
1463 | ||
029632fb PZ |
1464 | extern void sysrq_sched_debug_show(void); |
1465 | extern void sched_init_granularity(void); | |
1466 | extern void update_max_interval(void); | |
1baca4ce JL |
1467 | |
1468 | extern void init_sched_dl_class(void); | |
029632fb PZ |
1469 | extern void init_sched_rt_class(void); |
1470 | extern void init_sched_fair_class(void); | |
1471 | ||
8875125e | 1472 | extern void resched_curr(struct rq *rq); |
029632fb PZ |
1473 | extern void resched_cpu(int cpu); |
1474 | ||
1475 | extern struct rt_bandwidth def_rt_bandwidth; | |
1476 | extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime); | |
1477 | ||
332ac17e DF |
1478 | extern struct dl_bandwidth def_dl_bandwidth; |
1479 | extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime); | |
aab03e05 DF |
1480 | extern void init_dl_task_timer(struct sched_dl_entity *dl_se); |
1481 | ||
332ac17e DF |
1482 | unsigned long to_ratio(u64 period, u64 runtime); |
1483 | ||
540247fb | 1484 | extern void init_entity_runnable_average(struct sched_entity *se); |
2b8c41da | 1485 | extern void post_init_entity_util_avg(struct sched_entity *se); |
a75cdaa9 | 1486 | |
76d92ac3 FW |
1487 | #ifdef CONFIG_NO_HZ_FULL |
1488 | extern bool sched_can_stop_tick(struct rq *rq); | |
1489 | ||
1490 | /* | |
1491 | * Tick may be needed by tasks in the runqueue depending on their policy and | |
1492 | * requirements. If tick is needed, lets send the target an IPI to kick it out of | |
1493 | * nohz mode if necessary. | |
1494 | */ | |
1495 | static inline void sched_update_tick_dependency(struct rq *rq) | |
1496 | { | |
1497 | int cpu; | |
1498 | ||
1499 | if (!tick_nohz_full_enabled()) | |
1500 | return; | |
1501 | ||
1502 | cpu = cpu_of(rq); | |
1503 | ||
1504 | if (!tick_nohz_full_cpu(cpu)) | |
1505 | return; | |
1506 | ||
1507 | if (sched_can_stop_tick(rq)) | |
1508 | tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED); | |
1509 | else | |
1510 | tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); | |
1511 | } | |
1512 | #else | |
1513 | static inline void sched_update_tick_dependency(struct rq *rq) { } | |
1514 | #endif | |
1515 | ||
72465447 | 1516 | static inline void add_nr_running(struct rq *rq, unsigned count) |
029632fb | 1517 | { |
72465447 KT |
1518 | unsigned prev_nr = rq->nr_running; |
1519 | ||
1520 | rq->nr_running = prev_nr + count; | |
9f3660c2 | 1521 | |
72465447 | 1522 | if (prev_nr < 2 && rq->nr_running >= 2) { |
4486edd1 TC |
1523 | #ifdef CONFIG_SMP |
1524 | if (!rq->rd->overload) | |
1525 | rq->rd->overload = true; | |
1526 | #endif | |
4486edd1 | 1527 | } |
76d92ac3 FW |
1528 | |
1529 | sched_update_tick_dependency(rq); | |
029632fb PZ |
1530 | } |
1531 | ||
72465447 | 1532 | static inline void sub_nr_running(struct rq *rq, unsigned count) |
029632fb | 1533 | { |
72465447 | 1534 | rq->nr_running -= count; |
76d92ac3 FW |
1535 | /* Check if we still need preemption */ |
1536 | sched_update_tick_dependency(rq); | |
029632fb PZ |
1537 | } |
1538 | ||
265f22a9 FW |
1539 | static inline void rq_last_tick_reset(struct rq *rq) |
1540 | { | |
1541 | #ifdef CONFIG_NO_HZ_FULL | |
1542 | rq->last_sched_tick = jiffies; | |
1543 | #endif | |
1544 | } | |
1545 | ||
029632fb PZ |
1546 | extern void update_rq_clock(struct rq *rq); |
1547 | ||
1548 | extern void activate_task(struct rq *rq, struct task_struct *p, int flags); | |
1549 | extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags); | |
1550 | ||
1551 | extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); | |
1552 | ||
1553 | extern const_debug unsigned int sysctl_sched_time_avg; | |
1554 | extern const_debug unsigned int sysctl_sched_nr_migrate; | |
1555 | extern const_debug unsigned int sysctl_sched_migration_cost; | |
1556 | ||
1557 | static inline u64 sched_avg_period(void) | |
1558 | { | |
1559 | return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2; | |
1560 | } | |
1561 | ||
029632fb PZ |
1562 | #ifdef CONFIG_SCHED_HRTICK |
1563 | ||
1564 | /* | |
1565 | * Use hrtick when: | |
1566 | * - enabled by features | |
1567 | * - hrtimer is actually high res | |
1568 | */ | |
1569 | static inline int hrtick_enabled(struct rq *rq) | |
1570 | { | |
1571 | if (!sched_feat(HRTICK)) | |
1572 | return 0; | |
1573 | if (!cpu_active(cpu_of(rq))) | |
1574 | return 0; | |
1575 | return hrtimer_is_hres_active(&rq->hrtick_timer); | |
1576 | } | |
1577 | ||
1578 | void hrtick_start(struct rq *rq, u64 delay); | |
1579 | ||
b39e66ea MG |
1580 | #else |
1581 | ||
1582 | static inline int hrtick_enabled(struct rq *rq) | |
1583 | { | |
1584 | return 0; | |
1585 | } | |
1586 | ||
029632fb PZ |
1587 | #endif /* CONFIG_SCHED_HRTICK */ |
1588 | ||
1589 | #ifdef CONFIG_SMP | |
1590 | extern void sched_avg_update(struct rq *rq); | |
dfbca41f PZ |
1591 | |
1592 | #ifndef arch_scale_freq_capacity | |
1593 | static __always_inline | |
1594 | unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu) | |
1595 | { | |
1596 | return SCHED_CAPACITY_SCALE; | |
1597 | } | |
1598 | #endif | |
b5b4860d | 1599 | |
8cd5601c MR |
1600 | #ifndef arch_scale_cpu_capacity |
1601 | static __always_inline | |
1602 | unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu) | |
1603 | { | |
e3279a2e | 1604 | if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1)) |
8cd5601c MR |
1605 | return sd->smt_gain / sd->span_weight; |
1606 | ||
1607 | return SCHED_CAPACITY_SCALE; | |
1608 | } | |
1609 | #endif | |
1610 | ||
029632fb PZ |
1611 | static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) |
1612 | { | |
b5b4860d | 1613 | rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq)); |
029632fb PZ |
1614 | sched_avg_update(rq); |
1615 | } | |
1616 | #else | |
1617 | static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { } | |
1618 | static inline void sched_avg_update(struct rq *rq) { } | |
1619 | #endif | |
1620 | ||
eb580751 | 1621 | struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
3e71a462 | 1622 | __acquires(rq->lock); |
eb580751 | 1623 | struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
3960c8c0 | 1624 | __acquires(p->pi_lock) |
3e71a462 | 1625 | __acquires(rq->lock); |
3960c8c0 | 1626 | |
eb580751 | 1627 | static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf) |
3960c8c0 PZ |
1628 | __releases(rq->lock) |
1629 | { | |
d8ac8971 | 1630 | rq_unpin_lock(rq, rf); |
3960c8c0 PZ |
1631 | raw_spin_unlock(&rq->lock); |
1632 | } | |
1633 | ||
1634 | static inline void | |
eb580751 | 1635 | task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf) |
3960c8c0 PZ |
1636 | __releases(rq->lock) |
1637 | __releases(p->pi_lock) | |
1638 | { | |
d8ac8971 | 1639 | rq_unpin_lock(rq, rf); |
3960c8c0 | 1640 | raw_spin_unlock(&rq->lock); |
eb580751 | 1641 | raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); |
3960c8c0 PZ |
1642 | } |
1643 | ||
029632fb PZ |
1644 | #ifdef CONFIG_SMP |
1645 | #ifdef CONFIG_PREEMPT | |
1646 | ||
1647 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2); | |
1648 | ||
1649 | /* | |
1650 | * fair double_lock_balance: Safely acquires both rq->locks in a fair | |
1651 | * way at the expense of forcing extra atomic operations in all | |
1652 | * invocations. This assures that the double_lock is acquired using the | |
1653 | * same underlying policy as the spinlock_t on this architecture, which | |
1654 | * reduces latency compared to the unfair variant below. However, it | |
1655 | * also adds more overhead and therefore may reduce throughput. | |
1656 | */ | |
1657 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1658 | __releases(this_rq->lock) | |
1659 | __acquires(busiest->lock) | |
1660 | __acquires(this_rq->lock) | |
1661 | { | |
1662 | raw_spin_unlock(&this_rq->lock); | |
1663 | double_rq_lock(this_rq, busiest); | |
1664 | ||
1665 | return 1; | |
1666 | } | |
1667 | ||
1668 | #else | |
1669 | /* | |
1670 | * Unfair double_lock_balance: Optimizes throughput at the expense of | |
1671 | * latency by eliminating extra atomic operations when the locks are | |
1672 | * already in proper order on entry. This favors lower cpu-ids and will | |
1673 | * grant the double lock to lower cpus over higher ids under contention, | |
1674 | * regardless of entry order into the function. | |
1675 | */ | |
1676 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1677 | __releases(this_rq->lock) | |
1678 | __acquires(busiest->lock) | |
1679 | __acquires(this_rq->lock) | |
1680 | { | |
1681 | int ret = 0; | |
1682 | ||
1683 | if (unlikely(!raw_spin_trylock(&busiest->lock))) { | |
1684 | if (busiest < this_rq) { | |
1685 | raw_spin_unlock(&this_rq->lock); | |
1686 | raw_spin_lock(&busiest->lock); | |
1687 | raw_spin_lock_nested(&this_rq->lock, | |
1688 | SINGLE_DEPTH_NESTING); | |
1689 | ret = 1; | |
1690 | } else | |
1691 | raw_spin_lock_nested(&busiest->lock, | |
1692 | SINGLE_DEPTH_NESTING); | |
1693 | } | |
1694 | return ret; | |
1695 | } | |
1696 | ||
1697 | #endif /* CONFIG_PREEMPT */ | |
1698 | ||
1699 | /* | |
1700 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. | |
1701 | */ | |
1702 | static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1703 | { | |
1704 | if (unlikely(!irqs_disabled())) { | |
1705 | /* printk() doesn't work good under rq->lock */ | |
1706 | raw_spin_unlock(&this_rq->lock); | |
1707 | BUG_ON(1); | |
1708 | } | |
1709 | ||
1710 | return _double_lock_balance(this_rq, busiest); | |
1711 | } | |
1712 | ||
1713 | static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) | |
1714 | __releases(busiest->lock) | |
1715 | { | |
1716 | raw_spin_unlock(&busiest->lock); | |
1717 | lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_); | |
1718 | } | |
1719 | ||
74602315 PZ |
1720 | static inline void double_lock(spinlock_t *l1, spinlock_t *l2) |
1721 | { | |
1722 | if (l1 > l2) | |
1723 | swap(l1, l2); | |
1724 | ||
1725 | spin_lock(l1); | |
1726 | spin_lock_nested(l2, SINGLE_DEPTH_NESTING); | |
1727 | } | |
1728 | ||
60e69eed MG |
1729 | static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2) |
1730 | { | |
1731 | if (l1 > l2) | |
1732 | swap(l1, l2); | |
1733 | ||
1734 | spin_lock_irq(l1); | |
1735 | spin_lock_nested(l2, SINGLE_DEPTH_NESTING); | |
1736 | } | |
1737 | ||
74602315 PZ |
1738 | static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2) |
1739 | { | |
1740 | if (l1 > l2) | |
1741 | swap(l1, l2); | |
1742 | ||
1743 | raw_spin_lock(l1); | |
1744 | raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING); | |
1745 | } | |
1746 | ||
029632fb PZ |
1747 | /* |
1748 | * double_rq_lock - safely lock two runqueues | |
1749 | * | |
1750 | * Note this does not disable interrupts like task_rq_lock, | |
1751 | * you need to do so manually before calling. | |
1752 | */ | |
1753 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
1754 | __acquires(rq1->lock) | |
1755 | __acquires(rq2->lock) | |
1756 | { | |
1757 | BUG_ON(!irqs_disabled()); | |
1758 | if (rq1 == rq2) { | |
1759 | raw_spin_lock(&rq1->lock); | |
1760 | __acquire(rq2->lock); /* Fake it out ;) */ | |
1761 | } else { | |
1762 | if (rq1 < rq2) { | |
1763 | raw_spin_lock(&rq1->lock); | |
1764 | raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING); | |
1765 | } else { | |
1766 | raw_spin_lock(&rq2->lock); | |
1767 | raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); | |
1768 | } | |
1769 | } | |
1770 | } | |
1771 | ||
1772 | /* | |
1773 | * double_rq_unlock - safely unlock two runqueues | |
1774 | * | |
1775 | * Note this does not restore interrupts like task_rq_unlock, | |
1776 | * you need to do so manually after calling. | |
1777 | */ | |
1778 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) | |
1779 | __releases(rq1->lock) | |
1780 | __releases(rq2->lock) | |
1781 | { | |
1782 | raw_spin_unlock(&rq1->lock); | |
1783 | if (rq1 != rq2) | |
1784 | raw_spin_unlock(&rq2->lock); | |
1785 | else | |
1786 | __release(rq2->lock); | |
1787 | } | |
1788 | ||
f2cb1360 IM |
1789 | extern void set_rq_online (struct rq *rq); |
1790 | extern void set_rq_offline(struct rq *rq); | |
1791 | extern bool sched_smp_initialized; | |
1792 | ||
029632fb PZ |
1793 | #else /* CONFIG_SMP */ |
1794 | ||
1795 | /* | |
1796 | * double_rq_lock - safely lock two runqueues | |
1797 | * | |
1798 | * Note this does not disable interrupts like task_rq_lock, | |
1799 | * you need to do so manually before calling. | |
1800 | */ | |
1801 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
1802 | __acquires(rq1->lock) | |
1803 | __acquires(rq2->lock) | |
1804 | { | |
1805 | BUG_ON(!irqs_disabled()); | |
1806 | BUG_ON(rq1 != rq2); | |
1807 | raw_spin_lock(&rq1->lock); | |
1808 | __acquire(rq2->lock); /* Fake it out ;) */ | |
1809 | } | |
1810 | ||
1811 | /* | |
1812 | * double_rq_unlock - safely unlock two runqueues | |
1813 | * | |
1814 | * Note this does not restore interrupts like task_rq_unlock, | |
1815 | * you need to do so manually after calling. | |
1816 | */ | |
1817 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) | |
1818 | __releases(rq1->lock) | |
1819 | __releases(rq2->lock) | |
1820 | { | |
1821 | BUG_ON(rq1 != rq2); | |
1822 | raw_spin_unlock(&rq1->lock); | |
1823 | __release(rq2->lock); | |
1824 | } | |
1825 | ||
1826 | #endif | |
1827 | ||
1828 | extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq); | |
1829 | extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq); | |
6b55c965 SD |
1830 | |
1831 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb PZ |
1832 | extern void print_cfs_stats(struct seq_file *m, int cpu); |
1833 | extern void print_rt_stats(struct seq_file *m, int cpu); | |
acb32132 | 1834 | extern void print_dl_stats(struct seq_file *m, int cpu); |
6b55c965 SD |
1835 | extern void |
1836 | print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq); | |
397f2378 SD |
1837 | #ifdef CONFIG_NUMA_BALANCING |
1838 | extern void | |
1839 | show_numa_stats(struct task_struct *p, struct seq_file *m); | |
1840 | extern void | |
1841 | print_numa_stats(struct seq_file *m, int node, unsigned long tsf, | |
1842 | unsigned long tpf, unsigned long gsf, unsigned long gpf); | |
1843 | #endif /* CONFIG_NUMA_BALANCING */ | |
1844 | #endif /* CONFIG_SCHED_DEBUG */ | |
029632fb PZ |
1845 | |
1846 | extern void init_cfs_rq(struct cfs_rq *cfs_rq); | |
07c54f7a AV |
1847 | extern void init_rt_rq(struct rt_rq *rt_rq); |
1848 | extern void init_dl_rq(struct dl_rq *dl_rq); | |
029632fb | 1849 | |
1ee14e6c BS |
1850 | extern void cfs_bandwidth_usage_inc(void); |
1851 | extern void cfs_bandwidth_usage_dec(void); | |
1c792db7 | 1852 | |
3451d024 | 1853 | #ifdef CONFIG_NO_HZ_COMMON |
1c792db7 SS |
1854 | enum rq_nohz_flag_bits { |
1855 | NOHZ_TICK_STOPPED, | |
1856 | NOHZ_BALANCE_KICK, | |
1857 | }; | |
1858 | ||
1859 | #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags) | |
20a5c8cc TG |
1860 | |
1861 | extern void nohz_balance_exit_idle(unsigned int cpu); | |
1862 | #else | |
1863 | static inline void nohz_balance_exit_idle(unsigned int cpu) { } | |
1c792db7 | 1864 | #endif |
73fbec60 FW |
1865 | |
1866 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | |
19d23dbf | 1867 | struct irqtime { |
a499a5a1 | 1868 | u64 tick_delta; |
19d23dbf FW |
1869 | u64 irq_start_time; |
1870 | struct u64_stats_sync sync; | |
1871 | }; | |
73fbec60 | 1872 | |
19d23dbf | 1873 | DECLARE_PER_CPU(struct irqtime, cpu_irqtime); |
73fbec60 FW |
1874 | |
1875 | static inline u64 irq_time_read(int cpu) | |
1876 | { | |
19d23dbf | 1877 | struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu); |
a499a5a1 | 1878 | u64 *cpustat = kcpustat_cpu(cpu).cpustat; |
19d23dbf FW |
1879 | unsigned int seq; |
1880 | u64 total; | |
73fbec60 FW |
1881 | |
1882 | do { | |
19d23dbf | 1883 | seq = __u64_stats_fetch_begin(&irqtime->sync); |
a499a5a1 | 1884 | total = cpustat[CPUTIME_SOFTIRQ] + cpustat[CPUTIME_IRQ]; |
19d23dbf | 1885 | } while (__u64_stats_fetch_retry(&irqtime->sync, seq)); |
73fbec60 | 1886 | |
19d23dbf | 1887 | return total; |
73fbec60 | 1888 | } |
73fbec60 | 1889 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ |
adaf9fcd RW |
1890 | |
1891 | #ifdef CONFIG_CPU_FREQ | |
1892 | DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data); | |
1893 | ||
1894 | /** | |
1895 | * cpufreq_update_util - Take a note about CPU utilization changes. | |
12bde33d | 1896 | * @rq: Runqueue to carry out the update for. |
58919e83 | 1897 | * @flags: Update reason flags. |
adaf9fcd | 1898 | * |
58919e83 RW |
1899 | * This function is called by the scheduler on the CPU whose utilization is |
1900 | * being updated. | |
adaf9fcd RW |
1901 | * |
1902 | * It can only be called from RCU-sched read-side critical sections. | |
adaf9fcd RW |
1903 | * |
1904 | * The way cpufreq is currently arranged requires it to evaluate the CPU | |
1905 | * performance state (frequency/voltage) on a regular basis to prevent it from | |
1906 | * being stuck in a completely inadequate performance level for too long. | |
1907 | * That is not guaranteed to happen if the updates are only triggered from CFS, | |
1908 | * though, because they may not be coming in if RT or deadline tasks are active | |
1909 | * all the time (or there are RT and DL tasks only). | |
1910 | * | |
1911 | * As a workaround for that issue, this function is called by the RT and DL | |
1912 | * sched classes to trigger extra cpufreq updates to prevent it from stalling, | |
1913 | * but that really is a band-aid. Going forward it should be replaced with | |
1914 | * solutions targeted more specifically at RT and DL tasks. | |
1915 | */ | |
12bde33d | 1916 | static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) |
adaf9fcd | 1917 | { |
58919e83 RW |
1918 | struct update_util_data *data; |
1919 | ||
1920 | data = rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data)); | |
1921 | if (data) | |
12bde33d RW |
1922 | data->func(data, rq_clock(rq), flags); |
1923 | } | |
1924 | ||
1925 | static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags) | |
1926 | { | |
1927 | if (cpu_of(rq) == smp_processor_id()) | |
1928 | cpufreq_update_util(rq, flags); | |
adaf9fcd RW |
1929 | } |
1930 | #else | |
12bde33d RW |
1931 | static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {} |
1932 | static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags) {} | |
adaf9fcd | 1933 | #endif /* CONFIG_CPU_FREQ */ |
be53f58f | 1934 | |
9bdcb44e RW |
1935 | #ifdef arch_scale_freq_capacity |
1936 | #ifndef arch_scale_freq_invariant | |
1937 | #define arch_scale_freq_invariant() (true) | |
1938 | #endif | |
1939 | #else /* arch_scale_freq_capacity */ | |
1940 | #define arch_scale_freq_invariant() (false) | |
1941 | #endif |