Commit | Line | Data |
---|---|---|
029632fb PZ |
1 | |
2 | #include <linux/sched.h> | |
cf4aebc2 | 3 | #include <linux/sched/sysctl.h> |
8bd75c77 | 4 | #include <linux/sched/rt.h> |
029632fb PZ |
5 | #include <linux/mutex.h> |
6 | #include <linux/spinlock.h> | |
7 | #include <linux/stop_machine.h> | |
9f3660c2 | 8 | #include <linux/tick.h> |
f809ca9a | 9 | #include <linux/slab.h> |
029632fb | 10 | |
391e43da | 11 | #include "cpupri.h" |
60fed789 | 12 | #include "cpuacct.h" |
029632fb | 13 | |
45ceebf7 PG |
14 | struct rq; |
15 | ||
029632fb PZ |
16 | extern __read_mostly int scheduler_running; |
17 | ||
45ceebf7 PG |
18 | extern unsigned long calc_load_update; |
19 | extern atomic_long_t calc_load_tasks; | |
20 | ||
21 | extern long calc_load_fold_active(struct rq *this_rq); | |
22 | extern void update_cpu_load_active(struct rq *this_rq); | |
23 | ||
029632fb PZ |
24 | /* |
25 | * Convert user-nice values [ -20 ... 0 ... 19 ] | |
26 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], | |
27 | * and back. | |
28 | */ | |
29 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) | |
30 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) | |
31 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) | |
32 | ||
33 | /* | |
34 | * 'User priority' is the nice value converted to something we | |
35 | * can work with better when scaling various scheduler parameters, | |
36 | * it's a [ 0 ... 39 ] range. | |
37 | */ | |
38 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) | |
39 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) | |
40 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) | |
41 | ||
42 | /* | |
43 | * Helpers for converting nanosecond timing to jiffy resolution | |
44 | */ | |
45 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) | |
46 | ||
cc1f4b1f LZ |
47 | /* |
48 | * Increase resolution of nice-level calculations for 64-bit architectures. | |
49 | * The extra resolution improves shares distribution and load balancing of | |
50 | * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup | |
51 | * hierarchies, especially on larger systems. This is not a user-visible change | |
52 | * and does not change the user-interface for setting shares/weights. | |
53 | * | |
54 | * We increase resolution only if we have enough bits to allow this increased | |
55 | * resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution | |
56 | * when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the | |
57 | * increased costs. | |
58 | */ | |
59 | #if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */ | |
60 | # define SCHED_LOAD_RESOLUTION 10 | |
61 | # define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION) | |
62 | # define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION) | |
63 | #else | |
64 | # define SCHED_LOAD_RESOLUTION 0 | |
65 | # define scale_load(w) (w) | |
66 | # define scale_load_down(w) (w) | |
67 | #endif | |
68 | ||
69 | #define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION) | |
70 | #define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT) | |
71 | ||
029632fb PZ |
72 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
73 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT | |
74 | ||
75 | /* | |
76 | * These are the 'tuning knobs' of the scheduler: | |
029632fb | 77 | */ |
029632fb PZ |
78 | |
79 | /* | |
80 | * single value that denotes runtime == period, ie unlimited time. | |
81 | */ | |
82 | #define RUNTIME_INF ((u64)~0ULL) | |
83 | ||
d50dde5a DF |
84 | static inline int fair_policy(int policy) |
85 | { | |
86 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; | |
87 | } | |
88 | ||
029632fb PZ |
89 | static inline int rt_policy(int policy) |
90 | { | |
d50dde5a | 91 | return policy == SCHED_FIFO || policy == SCHED_RR; |
029632fb PZ |
92 | } |
93 | ||
94 | static inline int task_has_rt_policy(struct task_struct *p) | |
95 | { | |
96 | return rt_policy(p->policy); | |
97 | } | |
98 | ||
99 | /* | |
100 | * This is the priority-queue data structure of the RT scheduling class: | |
101 | */ | |
102 | struct rt_prio_array { | |
103 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ | |
104 | struct list_head queue[MAX_RT_PRIO]; | |
105 | }; | |
106 | ||
107 | struct rt_bandwidth { | |
108 | /* nests inside the rq lock: */ | |
109 | raw_spinlock_t rt_runtime_lock; | |
110 | ktime_t rt_period; | |
111 | u64 rt_runtime; | |
112 | struct hrtimer rt_period_timer; | |
113 | }; | |
114 | ||
115 | extern struct mutex sched_domains_mutex; | |
116 | ||
117 | #ifdef CONFIG_CGROUP_SCHED | |
118 | ||
119 | #include <linux/cgroup.h> | |
120 | ||
121 | struct cfs_rq; | |
122 | struct rt_rq; | |
123 | ||
35cf4e50 | 124 | extern struct list_head task_groups; |
029632fb PZ |
125 | |
126 | struct cfs_bandwidth { | |
127 | #ifdef CONFIG_CFS_BANDWIDTH | |
128 | raw_spinlock_t lock; | |
129 | ktime_t period; | |
130 | u64 quota, runtime; | |
131 | s64 hierarchal_quota; | |
132 | u64 runtime_expires; | |
133 | ||
134 | int idle, timer_active; | |
135 | struct hrtimer period_timer, slack_timer; | |
136 | struct list_head throttled_cfs_rq; | |
137 | ||
138 | /* statistics */ | |
139 | int nr_periods, nr_throttled; | |
140 | u64 throttled_time; | |
141 | #endif | |
142 | }; | |
143 | ||
144 | /* task group related information */ | |
145 | struct task_group { | |
146 | struct cgroup_subsys_state css; | |
147 | ||
148 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
149 | /* schedulable entities of this group on each cpu */ | |
150 | struct sched_entity **se; | |
151 | /* runqueue "owned" by this group on each cpu */ | |
152 | struct cfs_rq **cfs_rq; | |
153 | unsigned long shares; | |
154 | ||
fa6bddeb | 155 | #ifdef CONFIG_SMP |
bf5b986e | 156 | atomic_long_t load_avg; |
bb17f655 | 157 | atomic_t runnable_avg; |
029632fb | 158 | #endif |
fa6bddeb | 159 | #endif |
029632fb PZ |
160 | |
161 | #ifdef CONFIG_RT_GROUP_SCHED | |
162 | struct sched_rt_entity **rt_se; | |
163 | struct rt_rq **rt_rq; | |
164 | ||
165 | struct rt_bandwidth rt_bandwidth; | |
166 | #endif | |
167 | ||
168 | struct rcu_head rcu; | |
169 | struct list_head list; | |
170 | ||
171 | struct task_group *parent; | |
172 | struct list_head siblings; | |
173 | struct list_head children; | |
174 | ||
175 | #ifdef CONFIG_SCHED_AUTOGROUP | |
176 | struct autogroup *autogroup; | |
177 | #endif | |
178 | ||
179 | struct cfs_bandwidth cfs_bandwidth; | |
180 | }; | |
181 | ||
182 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
183 | #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD | |
184 | ||
185 | /* | |
186 | * A weight of 0 or 1 can cause arithmetics problems. | |
187 | * A weight of a cfs_rq is the sum of weights of which entities | |
188 | * are queued on this cfs_rq, so a weight of a entity should not be | |
189 | * too large, so as the shares value of a task group. | |
190 | * (The default weight is 1024 - so there's no practical | |
191 | * limitation from this.) | |
192 | */ | |
193 | #define MIN_SHARES (1UL << 1) | |
194 | #define MAX_SHARES (1UL << 18) | |
195 | #endif | |
196 | ||
029632fb PZ |
197 | typedef int (*tg_visitor)(struct task_group *, void *); |
198 | ||
199 | extern int walk_tg_tree_from(struct task_group *from, | |
200 | tg_visitor down, tg_visitor up, void *data); | |
201 | ||
202 | /* | |
203 | * Iterate the full tree, calling @down when first entering a node and @up when | |
204 | * leaving it for the final time. | |
205 | * | |
206 | * Caller must hold rcu_lock or sufficient equivalent. | |
207 | */ | |
208 | static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) | |
209 | { | |
210 | return walk_tg_tree_from(&root_task_group, down, up, data); | |
211 | } | |
212 | ||
213 | extern int tg_nop(struct task_group *tg, void *data); | |
214 | ||
215 | extern void free_fair_sched_group(struct task_group *tg); | |
216 | extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent); | |
217 | extern void unregister_fair_sched_group(struct task_group *tg, int cpu); | |
218 | extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
219 | struct sched_entity *se, int cpu, | |
220 | struct sched_entity *parent); | |
221 | extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b); | |
222 | extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); | |
223 | ||
224 | extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b); | |
225 | extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b); | |
226 | extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq); | |
227 | ||
228 | extern void free_rt_sched_group(struct task_group *tg); | |
229 | extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent); | |
230 | extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, | |
231 | struct sched_rt_entity *rt_se, int cpu, | |
232 | struct sched_rt_entity *parent); | |
233 | ||
25cc7da7 LZ |
234 | extern struct task_group *sched_create_group(struct task_group *parent); |
235 | extern void sched_online_group(struct task_group *tg, | |
236 | struct task_group *parent); | |
237 | extern void sched_destroy_group(struct task_group *tg); | |
238 | extern void sched_offline_group(struct task_group *tg); | |
239 | ||
240 | extern void sched_move_task(struct task_struct *tsk); | |
241 | ||
242 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
243 | extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); | |
244 | #endif | |
245 | ||
029632fb PZ |
246 | #else /* CONFIG_CGROUP_SCHED */ |
247 | ||
248 | struct cfs_bandwidth { }; | |
249 | ||
250 | #endif /* CONFIG_CGROUP_SCHED */ | |
251 | ||
252 | /* CFS-related fields in a runqueue */ | |
253 | struct cfs_rq { | |
254 | struct load_weight load; | |
c82513e5 | 255 | unsigned int nr_running, h_nr_running; |
029632fb PZ |
256 | |
257 | u64 exec_clock; | |
258 | u64 min_vruntime; | |
259 | #ifndef CONFIG_64BIT | |
260 | u64 min_vruntime_copy; | |
261 | #endif | |
262 | ||
263 | struct rb_root tasks_timeline; | |
264 | struct rb_node *rb_leftmost; | |
265 | ||
029632fb PZ |
266 | /* |
267 | * 'curr' points to currently running entity on this cfs_rq. | |
268 | * It is set to NULL otherwise (i.e when none are currently running). | |
269 | */ | |
270 | struct sched_entity *curr, *next, *last, *skip; | |
271 | ||
272 | #ifdef CONFIG_SCHED_DEBUG | |
273 | unsigned int nr_spread_over; | |
274 | #endif | |
275 | ||
2dac754e PT |
276 | #ifdef CONFIG_SMP |
277 | /* | |
278 | * CFS Load tracking | |
279 | * Under CFS, load is tracked on a per-entity basis and aggregated up. | |
280 | * This allows for the description of both thread and group usage (in | |
281 | * the FAIR_GROUP_SCHED case). | |
282 | */ | |
72a4cf20 | 283 | unsigned long runnable_load_avg, blocked_load_avg; |
2509940f | 284 | atomic64_t decay_counter; |
9ee474f5 | 285 | u64 last_decay; |
2509940f | 286 | atomic_long_t removed_load; |
141965c7 | 287 | |
c566e8e9 | 288 | #ifdef CONFIG_FAIR_GROUP_SCHED |
141965c7 | 289 | /* Required to track per-cpu representation of a task_group */ |
bb17f655 | 290 | u32 tg_runnable_contrib; |
bf5b986e | 291 | unsigned long tg_load_contrib; |
82958366 PT |
292 | |
293 | /* | |
294 | * h_load = weight * f(tg) | |
295 | * | |
296 | * Where f(tg) is the recursive weight fraction assigned to | |
297 | * this group. | |
298 | */ | |
299 | unsigned long h_load; | |
68520796 VD |
300 | u64 last_h_load_update; |
301 | struct sched_entity *h_load_next; | |
302 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
82958366 PT |
303 | #endif /* CONFIG_SMP */ |
304 | ||
029632fb PZ |
305 | #ifdef CONFIG_FAIR_GROUP_SCHED |
306 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ | |
307 | ||
308 | /* | |
309 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in | |
310 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities | |
311 | * (like users, containers etc.) | |
312 | * | |
313 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This | |
314 | * list is used during load balance. | |
315 | */ | |
316 | int on_list; | |
317 | struct list_head leaf_cfs_rq_list; | |
318 | struct task_group *tg; /* group that "owns" this runqueue */ | |
319 | ||
029632fb PZ |
320 | #ifdef CONFIG_CFS_BANDWIDTH |
321 | int runtime_enabled; | |
322 | u64 runtime_expires; | |
323 | s64 runtime_remaining; | |
324 | ||
f1b17280 PT |
325 | u64 throttled_clock, throttled_clock_task; |
326 | u64 throttled_clock_task_time; | |
029632fb PZ |
327 | int throttled, throttle_count; |
328 | struct list_head throttled_list; | |
329 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
330 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
331 | }; | |
332 | ||
333 | static inline int rt_bandwidth_enabled(void) | |
334 | { | |
335 | return sysctl_sched_rt_runtime >= 0; | |
336 | } | |
337 | ||
338 | /* Real-Time classes' related field in a runqueue: */ | |
339 | struct rt_rq { | |
340 | struct rt_prio_array active; | |
c82513e5 | 341 | unsigned int rt_nr_running; |
029632fb PZ |
342 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
343 | struct { | |
344 | int curr; /* highest queued rt task prio */ | |
345 | #ifdef CONFIG_SMP | |
346 | int next; /* next highest */ | |
347 | #endif | |
348 | } highest_prio; | |
349 | #endif | |
350 | #ifdef CONFIG_SMP | |
351 | unsigned long rt_nr_migratory; | |
352 | unsigned long rt_nr_total; | |
353 | int overloaded; | |
354 | struct plist_head pushable_tasks; | |
355 | #endif | |
356 | int rt_throttled; | |
357 | u64 rt_time; | |
358 | u64 rt_runtime; | |
359 | /* Nests inside the rq lock: */ | |
360 | raw_spinlock_t rt_runtime_lock; | |
361 | ||
362 | #ifdef CONFIG_RT_GROUP_SCHED | |
363 | unsigned long rt_nr_boosted; | |
364 | ||
365 | struct rq *rq; | |
029632fb PZ |
366 | struct task_group *tg; |
367 | #endif | |
368 | }; | |
369 | ||
370 | #ifdef CONFIG_SMP | |
371 | ||
372 | /* | |
373 | * We add the notion of a root-domain which will be used to define per-domain | |
374 | * variables. Each exclusive cpuset essentially defines an island domain by | |
375 | * fully partitioning the member cpus from any other cpuset. Whenever a new | |
376 | * exclusive cpuset is created, we also create and attach a new root-domain | |
377 | * object. | |
378 | * | |
379 | */ | |
380 | struct root_domain { | |
381 | atomic_t refcount; | |
382 | atomic_t rto_count; | |
383 | struct rcu_head rcu; | |
384 | cpumask_var_t span; | |
385 | cpumask_var_t online; | |
386 | ||
387 | /* | |
388 | * The "RT overload" flag: it gets set if a CPU has more than | |
389 | * one runnable RT task. | |
390 | */ | |
391 | cpumask_var_t rto_mask; | |
392 | struct cpupri cpupri; | |
393 | }; | |
394 | ||
395 | extern struct root_domain def_root_domain; | |
396 | ||
397 | #endif /* CONFIG_SMP */ | |
398 | ||
399 | /* | |
400 | * This is the main, per-CPU runqueue data structure. | |
401 | * | |
402 | * Locking rule: those places that want to lock multiple runqueues | |
403 | * (such as the load balancing or the thread migration code), lock | |
404 | * acquire operations must be ordered by ascending &runqueue. | |
405 | */ | |
406 | struct rq { | |
407 | /* runqueue lock: */ | |
408 | raw_spinlock_t lock; | |
409 | ||
410 | /* | |
411 | * nr_running and cpu_load should be in the same cacheline because | |
412 | * remote CPUs use both these fields when doing load calculation. | |
413 | */ | |
c82513e5 | 414 | unsigned int nr_running; |
0ec8aa00 PZ |
415 | #ifdef CONFIG_NUMA_BALANCING |
416 | unsigned int nr_numa_running; | |
417 | unsigned int nr_preferred_running; | |
418 | #endif | |
029632fb PZ |
419 | #define CPU_LOAD_IDX_MAX 5 |
420 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; | |
421 | unsigned long last_load_update_tick; | |
3451d024 | 422 | #ifdef CONFIG_NO_HZ_COMMON |
029632fb | 423 | u64 nohz_stamp; |
1c792db7 | 424 | unsigned long nohz_flags; |
265f22a9 FW |
425 | #endif |
426 | #ifdef CONFIG_NO_HZ_FULL | |
427 | unsigned long last_sched_tick; | |
029632fb PZ |
428 | #endif |
429 | int skip_clock_update; | |
430 | ||
431 | /* capture load from *all* tasks on this cpu: */ | |
432 | struct load_weight load; | |
433 | unsigned long nr_load_updates; | |
434 | u64 nr_switches; | |
435 | ||
436 | struct cfs_rq cfs; | |
437 | struct rt_rq rt; | |
438 | ||
439 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
440 | /* list of leaf cfs_rq on this cpu: */ | |
441 | struct list_head leaf_cfs_rq_list; | |
a35b6466 PZ |
442 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
443 | ||
029632fb PZ |
444 | #ifdef CONFIG_RT_GROUP_SCHED |
445 | struct list_head leaf_rt_rq_list; | |
446 | #endif | |
447 | ||
448 | /* | |
449 | * This is part of a global counter where only the total sum | |
450 | * over all CPUs matters. A task can increase this counter on | |
451 | * one CPU and if it got migrated afterwards it may decrease | |
452 | * it on another CPU. Always updated under the runqueue lock: | |
453 | */ | |
454 | unsigned long nr_uninterruptible; | |
455 | ||
456 | struct task_struct *curr, *idle, *stop; | |
457 | unsigned long next_balance; | |
458 | struct mm_struct *prev_mm; | |
459 | ||
460 | u64 clock; | |
461 | u64 clock_task; | |
462 | ||
463 | atomic_t nr_iowait; | |
464 | ||
465 | #ifdef CONFIG_SMP | |
466 | struct root_domain *rd; | |
467 | struct sched_domain *sd; | |
468 | ||
469 | unsigned long cpu_power; | |
470 | ||
471 | unsigned char idle_balance; | |
472 | /* For active balancing */ | |
473 | int post_schedule; | |
474 | int active_balance; | |
475 | int push_cpu; | |
476 | struct cpu_stop_work active_balance_work; | |
477 | /* cpu of this runqueue: */ | |
478 | int cpu; | |
479 | int online; | |
480 | ||
367456c7 PZ |
481 | struct list_head cfs_tasks; |
482 | ||
029632fb PZ |
483 | u64 rt_avg; |
484 | u64 age_stamp; | |
485 | u64 idle_stamp; | |
486 | u64 avg_idle; | |
9bd721c5 JL |
487 | |
488 | /* This is used to determine avg_idle's max value */ | |
489 | u64 max_idle_balance_cost; | |
029632fb PZ |
490 | #endif |
491 | ||
492 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | |
493 | u64 prev_irq_time; | |
494 | #endif | |
495 | #ifdef CONFIG_PARAVIRT | |
496 | u64 prev_steal_time; | |
497 | #endif | |
498 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | |
499 | u64 prev_steal_time_rq; | |
500 | #endif | |
501 | ||
502 | /* calc_load related fields */ | |
503 | unsigned long calc_load_update; | |
504 | long calc_load_active; | |
505 | ||
506 | #ifdef CONFIG_SCHED_HRTICK | |
507 | #ifdef CONFIG_SMP | |
508 | int hrtick_csd_pending; | |
509 | struct call_single_data hrtick_csd; | |
510 | #endif | |
511 | struct hrtimer hrtick_timer; | |
512 | #endif | |
513 | ||
514 | #ifdef CONFIG_SCHEDSTATS | |
515 | /* latency stats */ | |
516 | struct sched_info rq_sched_info; | |
517 | unsigned long long rq_cpu_time; | |
518 | /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ | |
519 | ||
520 | /* sys_sched_yield() stats */ | |
521 | unsigned int yld_count; | |
522 | ||
523 | /* schedule() stats */ | |
029632fb PZ |
524 | unsigned int sched_count; |
525 | unsigned int sched_goidle; | |
526 | ||
527 | /* try_to_wake_up() stats */ | |
528 | unsigned int ttwu_count; | |
529 | unsigned int ttwu_local; | |
530 | #endif | |
531 | ||
532 | #ifdef CONFIG_SMP | |
533 | struct llist_head wake_list; | |
534 | #endif | |
18bf2805 BS |
535 | |
536 | struct sched_avg avg; | |
029632fb PZ |
537 | }; |
538 | ||
539 | static inline int cpu_of(struct rq *rq) | |
540 | { | |
541 | #ifdef CONFIG_SMP | |
542 | return rq->cpu; | |
543 | #else | |
544 | return 0; | |
545 | #endif | |
546 | } | |
547 | ||
548 | DECLARE_PER_CPU(struct rq, runqueues); | |
549 | ||
518cd623 PZ |
550 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
551 | #define this_rq() (&__get_cpu_var(runqueues)) | |
552 | #define task_rq(p) cpu_rq(task_cpu(p)) | |
553 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) | |
554 | #define raw_rq() (&__raw_get_cpu_var(runqueues)) | |
555 | ||
78becc27 FW |
556 | static inline u64 rq_clock(struct rq *rq) |
557 | { | |
558 | return rq->clock; | |
559 | } | |
560 | ||
561 | static inline u64 rq_clock_task(struct rq *rq) | |
562 | { | |
563 | return rq->clock_task; | |
564 | } | |
565 | ||
f809ca9a | 566 | #ifdef CONFIG_NUMA_BALANCING |
0ec8aa00 | 567 | extern void sched_setnuma(struct task_struct *p, int node); |
e6628d5b | 568 | extern int migrate_task_to(struct task_struct *p, int cpu); |
ac66f547 | 569 | extern int migrate_swap(struct task_struct *, struct task_struct *); |
f809ca9a MG |
570 | #endif /* CONFIG_NUMA_BALANCING */ |
571 | ||
518cd623 PZ |
572 | #ifdef CONFIG_SMP |
573 | ||
029632fb PZ |
574 | #define rcu_dereference_check_sched_domain(p) \ |
575 | rcu_dereference_check((p), \ | |
576 | lockdep_is_held(&sched_domains_mutex)) | |
577 | ||
578 | /* | |
579 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. | |
580 | * See detach_destroy_domains: synchronize_sched for details. | |
581 | * | |
582 | * The domain tree of any CPU may only be accessed from within | |
583 | * preempt-disabled sections. | |
584 | */ | |
585 | #define for_each_domain(cpu, __sd) \ | |
518cd623 PZ |
586 | for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \ |
587 | __sd; __sd = __sd->parent) | |
029632fb | 588 | |
77e81365 SS |
589 | #define for_each_lower_domain(sd) for (; sd; sd = sd->child) |
590 | ||
518cd623 PZ |
591 | /** |
592 | * highest_flag_domain - Return highest sched_domain containing flag. | |
593 | * @cpu: The cpu whose highest level of sched domain is to | |
594 | * be returned. | |
595 | * @flag: The flag to check for the highest sched_domain | |
596 | * for the given cpu. | |
597 | * | |
598 | * Returns the highest sched_domain of a cpu which contains the given flag. | |
599 | */ | |
600 | static inline struct sched_domain *highest_flag_domain(int cpu, int flag) | |
601 | { | |
602 | struct sched_domain *sd, *hsd = NULL; | |
603 | ||
604 | for_each_domain(cpu, sd) { | |
605 | if (!(sd->flags & flag)) | |
606 | break; | |
607 | hsd = sd; | |
608 | } | |
609 | ||
610 | return hsd; | |
611 | } | |
612 | ||
fb13c7ee MG |
613 | static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) |
614 | { | |
615 | struct sched_domain *sd; | |
616 | ||
617 | for_each_domain(cpu, sd) { | |
618 | if (sd->flags & flag) | |
619 | break; | |
620 | } | |
621 | ||
622 | return sd; | |
623 | } | |
624 | ||
518cd623 | 625 | DECLARE_PER_CPU(struct sched_domain *, sd_llc); |
7d9ffa89 | 626 | DECLARE_PER_CPU(int, sd_llc_size); |
518cd623 | 627 | DECLARE_PER_CPU(int, sd_llc_id); |
fb13c7ee | 628 | DECLARE_PER_CPU(struct sched_domain *, sd_numa); |
37dc6b50 PM |
629 | DECLARE_PER_CPU(struct sched_domain *, sd_busy); |
630 | DECLARE_PER_CPU(struct sched_domain *, sd_asym); | |
518cd623 | 631 | |
5e6521ea LZ |
632 | struct sched_group_power { |
633 | atomic_t ref; | |
634 | /* | |
635 | * CPU power of this group, SCHED_LOAD_SCALE being max power for a | |
636 | * single CPU. | |
637 | */ | |
638 | unsigned int power, power_orig; | |
639 | unsigned long next_update; | |
6263322c | 640 | int imbalance; /* XXX unrelated to power but shared group state */ |
5e6521ea LZ |
641 | /* |
642 | * Number of busy cpus in this group. | |
643 | */ | |
644 | atomic_t nr_busy_cpus; | |
645 | ||
646 | unsigned long cpumask[0]; /* iteration mask */ | |
647 | }; | |
648 | ||
649 | struct sched_group { | |
650 | struct sched_group *next; /* Must be a circular list */ | |
651 | atomic_t ref; | |
652 | ||
653 | unsigned int group_weight; | |
654 | struct sched_group_power *sgp; | |
655 | ||
656 | /* | |
657 | * The CPUs this group covers. | |
658 | * | |
659 | * NOTE: this field is variable length. (Allocated dynamically | |
660 | * by attaching extra space to the end of the structure, | |
661 | * depending on how many CPUs the kernel has booted up with) | |
662 | */ | |
663 | unsigned long cpumask[0]; | |
664 | }; | |
665 | ||
666 | static inline struct cpumask *sched_group_cpus(struct sched_group *sg) | |
667 | { | |
668 | return to_cpumask(sg->cpumask); | |
669 | } | |
670 | ||
671 | /* | |
672 | * cpumask masking which cpus in the group are allowed to iterate up the domain | |
673 | * tree. | |
674 | */ | |
675 | static inline struct cpumask *sched_group_mask(struct sched_group *sg) | |
676 | { | |
677 | return to_cpumask(sg->sgp->cpumask); | |
678 | } | |
679 | ||
680 | /** | |
681 | * group_first_cpu - Returns the first cpu in the cpumask of a sched_group. | |
682 | * @group: The group whose first cpu is to be returned. | |
683 | */ | |
684 | static inline unsigned int group_first_cpu(struct sched_group *group) | |
685 | { | |
686 | return cpumask_first(sched_group_cpus(group)); | |
687 | } | |
688 | ||
c1174876 PZ |
689 | extern int group_balance_cpu(struct sched_group *sg); |
690 | ||
518cd623 | 691 | #endif /* CONFIG_SMP */ |
029632fb | 692 | |
391e43da PZ |
693 | #include "stats.h" |
694 | #include "auto_group.h" | |
029632fb PZ |
695 | |
696 | #ifdef CONFIG_CGROUP_SCHED | |
697 | ||
698 | /* | |
699 | * Return the group to which this tasks belongs. | |
700 | * | |
8af01f56 TH |
701 | * We cannot use task_css() and friends because the cgroup subsystem |
702 | * changes that value before the cgroup_subsys::attach() method is called, | |
703 | * therefore we cannot pin it and might observe the wrong value. | |
8323f26c PZ |
704 | * |
705 | * The same is true for autogroup's p->signal->autogroup->tg, the autogroup | |
706 | * core changes this before calling sched_move_task(). | |
707 | * | |
708 | * Instead we use a 'copy' which is updated from sched_move_task() while | |
709 | * holding both task_struct::pi_lock and rq::lock. | |
029632fb PZ |
710 | */ |
711 | static inline struct task_group *task_group(struct task_struct *p) | |
712 | { | |
8323f26c | 713 | return p->sched_task_group; |
029632fb PZ |
714 | } |
715 | ||
716 | /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ | |
717 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) | |
718 | { | |
719 | #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED) | |
720 | struct task_group *tg = task_group(p); | |
721 | #endif | |
722 | ||
723 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
724 | p->se.cfs_rq = tg->cfs_rq[cpu]; | |
725 | p->se.parent = tg->se[cpu]; | |
726 | #endif | |
727 | ||
728 | #ifdef CONFIG_RT_GROUP_SCHED | |
729 | p->rt.rt_rq = tg->rt_rq[cpu]; | |
730 | p->rt.parent = tg->rt_se[cpu]; | |
731 | #endif | |
732 | } | |
733 | ||
734 | #else /* CONFIG_CGROUP_SCHED */ | |
735 | ||
736 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } | |
737 | static inline struct task_group *task_group(struct task_struct *p) | |
738 | { | |
739 | return NULL; | |
740 | } | |
741 | ||
742 | #endif /* CONFIG_CGROUP_SCHED */ | |
743 | ||
744 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) | |
745 | { | |
746 | set_task_rq(p, cpu); | |
747 | #ifdef CONFIG_SMP | |
748 | /* | |
749 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be | |
750 | * successfuly executed on another CPU. We must ensure that updates of | |
751 | * per-task data have been completed by this moment. | |
752 | */ | |
753 | smp_wmb(); | |
754 | task_thread_info(p)->cpu = cpu; | |
ac66f547 | 755 | p->wake_cpu = cpu; |
029632fb PZ |
756 | #endif |
757 | } | |
758 | ||
759 | /* | |
760 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: | |
761 | */ | |
762 | #ifdef CONFIG_SCHED_DEBUG | |
c5905afb | 763 | # include <linux/static_key.h> |
029632fb PZ |
764 | # define const_debug __read_mostly |
765 | #else | |
766 | # define const_debug const | |
767 | #endif | |
768 | ||
769 | extern const_debug unsigned int sysctl_sched_features; | |
770 | ||
771 | #define SCHED_FEAT(name, enabled) \ | |
772 | __SCHED_FEAT_##name , | |
773 | ||
774 | enum { | |
391e43da | 775 | #include "features.h" |
f8b6d1cc | 776 | __SCHED_FEAT_NR, |
029632fb PZ |
777 | }; |
778 | ||
779 | #undef SCHED_FEAT | |
780 | ||
f8b6d1cc | 781 | #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL) |
c5905afb | 782 | static __always_inline bool static_branch__true(struct static_key *key) |
f8b6d1cc | 783 | { |
c5905afb | 784 | return static_key_true(key); /* Not out of line branch. */ |
f8b6d1cc PZ |
785 | } |
786 | ||
c5905afb | 787 | static __always_inline bool static_branch__false(struct static_key *key) |
f8b6d1cc | 788 | { |
c5905afb | 789 | return static_key_false(key); /* Out of line branch. */ |
f8b6d1cc PZ |
790 | } |
791 | ||
792 | #define SCHED_FEAT(name, enabled) \ | |
c5905afb | 793 | static __always_inline bool static_branch_##name(struct static_key *key) \ |
f8b6d1cc PZ |
794 | { \ |
795 | return static_branch__##enabled(key); \ | |
796 | } | |
797 | ||
798 | #include "features.h" | |
799 | ||
800 | #undef SCHED_FEAT | |
801 | ||
c5905afb | 802 | extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; |
f8b6d1cc PZ |
803 | #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x])) |
804 | #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */ | |
029632fb | 805 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) |
f8b6d1cc | 806 | #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */ |
029632fb | 807 | |
cbee9f88 PZ |
808 | #ifdef CONFIG_NUMA_BALANCING |
809 | #define sched_feat_numa(x) sched_feat(x) | |
3105b86a MG |
810 | #ifdef CONFIG_SCHED_DEBUG |
811 | #define numabalancing_enabled sched_feat_numa(NUMA) | |
812 | #else | |
813 | extern bool numabalancing_enabled; | |
814 | #endif /* CONFIG_SCHED_DEBUG */ | |
cbee9f88 PZ |
815 | #else |
816 | #define sched_feat_numa(x) (0) | |
3105b86a MG |
817 | #define numabalancing_enabled (0) |
818 | #endif /* CONFIG_NUMA_BALANCING */ | |
cbee9f88 | 819 | |
029632fb PZ |
820 | static inline u64 global_rt_period(void) |
821 | { | |
822 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; | |
823 | } | |
824 | ||
825 | static inline u64 global_rt_runtime(void) | |
826 | { | |
827 | if (sysctl_sched_rt_runtime < 0) | |
828 | return RUNTIME_INF; | |
829 | ||
830 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; | |
831 | } | |
832 | ||
833 | ||
834 | ||
835 | static inline int task_current(struct rq *rq, struct task_struct *p) | |
836 | { | |
837 | return rq->curr == p; | |
838 | } | |
839 | ||
840 | static inline int task_running(struct rq *rq, struct task_struct *p) | |
841 | { | |
842 | #ifdef CONFIG_SMP | |
843 | return p->on_cpu; | |
844 | #else | |
845 | return task_current(rq, p); | |
846 | #endif | |
847 | } | |
848 | ||
849 | ||
850 | #ifndef prepare_arch_switch | |
851 | # define prepare_arch_switch(next) do { } while (0) | |
852 | #endif | |
853 | #ifndef finish_arch_switch | |
854 | # define finish_arch_switch(prev) do { } while (0) | |
855 | #endif | |
01f23e16 CM |
856 | #ifndef finish_arch_post_lock_switch |
857 | # define finish_arch_post_lock_switch() do { } while (0) | |
858 | #endif | |
029632fb PZ |
859 | |
860 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | |
861 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) | |
862 | { | |
863 | #ifdef CONFIG_SMP | |
864 | /* | |
865 | * We can optimise this out completely for !SMP, because the | |
866 | * SMP rebalancing from interrupt is the only thing that cares | |
867 | * here. | |
868 | */ | |
869 | next->on_cpu = 1; | |
870 | #endif | |
871 | } | |
872 | ||
873 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) | |
874 | { | |
875 | #ifdef CONFIG_SMP | |
876 | /* | |
877 | * After ->on_cpu is cleared, the task can be moved to a different CPU. | |
878 | * We must ensure this doesn't happen until the switch is completely | |
879 | * finished. | |
880 | */ | |
881 | smp_wmb(); | |
882 | prev->on_cpu = 0; | |
883 | #endif | |
884 | #ifdef CONFIG_DEBUG_SPINLOCK | |
885 | /* this is a valid case when another task releases the spinlock */ | |
886 | rq->lock.owner = current; | |
887 | #endif | |
888 | /* | |
889 | * If we are tracking spinlock dependencies then we have to | |
890 | * fix up the runqueue lock - which gets 'carried over' from | |
891 | * prev into current: | |
892 | */ | |
893 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); | |
894 | ||
895 | raw_spin_unlock_irq(&rq->lock); | |
896 | } | |
897 | ||
898 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
899 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) | |
900 | { | |
901 | #ifdef CONFIG_SMP | |
902 | /* | |
903 | * We can optimise this out completely for !SMP, because the | |
904 | * SMP rebalancing from interrupt is the only thing that cares | |
905 | * here. | |
906 | */ | |
907 | next->on_cpu = 1; | |
908 | #endif | |
029632fb | 909 | raw_spin_unlock(&rq->lock); |
029632fb PZ |
910 | } |
911 | ||
912 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) | |
913 | { | |
914 | #ifdef CONFIG_SMP | |
915 | /* | |
916 | * After ->on_cpu is cleared, the task can be moved to a different CPU. | |
917 | * We must ensure this doesn't happen until the switch is completely | |
918 | * finished. | |
919 | */ | |
920 | smp_wmb(); | |
921 | prev->on_cpu = 0; | |
922 | #endif | |
029632fb | 923 | local_irq_enable(); |
029632fb PZ |
924 | } |
925 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
926 | ||
b13095f0 LZ |
927 | /* |
928 | * wake flags | |
929 | */ | |
930 | #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */ | |
931 | #define WF_FORK 0x02 /* child wakeup after fork */ | |
932 | #define WF_MIGRATED 0x4 /* internal use, task got migrated */ | |
933 | ||
029632fb PZ |
934 | /* |
935 | * To aid in avoiding the subversion of "niceness" due to uneven distribution | |
936 | * of tasks with abnormal "nice" values across CPUs the contribution that | |
937 | * each task makes to its run queue's load is weighted according to its | |
938 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a | |
939 | * scaled version of the new time slice allocation that they receive on time | |
940 | * slice expiry etc. | |
941 | */ | |
942 | ||
943 | #define WEIGHT_IDLEPRIO 3 | |
944 | #define WMULT_IDLEPRIO 1431655765 | |
945 | ||
946 | /* | |
947 | * Nice levels are multiplicative, with a gentle 10% change for every | |
948 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to | |
949 | * nice 1, it will get ~10% less CPU time than another CPU-bound task | |
950 | * that remained on nice 0. | |
951 | * | |
952 | * The "10% effect" is relative and cumulative: from _any_ nice level, | |
953 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level | |
954 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. | |
955 | * If a task goes up by ~10% and another task goes down by ~10% then | |
956 | * the relative distance between them is ~25%.) | |
957 | */ | |
958 | static const int prio_to_weight[40] = { | |
959 | /* -20 */ 88761, 71755, 56483, 46273, 36291, | |
960 | /* -15 */ 29154, 23254, 18705, 14949, 11916, | |
961 | /* -10 */ 9548, 7620, 6100, 4904, 3906, | |
962 | /* -5 */ 3121, 2501, 1991, 1586, 1277, | |
963 | /* 0 */ 1024, 820, 655, 526, 423, | |
964 | /* 5 */ 335, 272, 215, 172, 137, | |
965 | /* 10 */ 110, 87, 70, 56, 45, | |
966 | /* 15 */ 36, 29, 23, 18, 15, | |
967 | }; | |
968 | ||
969 | /* | |
970 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. | |
971 | * | |
972 | * In cases where the weight does not change often, we can use the | |
973 | * precalculated inverse to speed up arithmetics by turning divisions | |
974 | * into multiplications: | |
975 | */ | |
976 | static const u32 prio_to_wmult[40] = { | |
977 | /* -20 */ 48388, 59856, 76040, 92818, 118348, | |
978 | /* -15 */ 147320, 184698, 229616, 287308, 360437, | |
979 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, | |
980 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, | |
981 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, | |
982 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, | |
983 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, | |
984 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, | |
985 | }; | |
986 | ||
c82ba9fa LZ |
987 | #define ENQUEUE_WAKEUP 1 |
988 | #define ENQUEUE_HEAD 2 | |
989 | #ifdef CONFIG_SMP | |
990 | #define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */ | |
991 | #else | |
992 | #define ENQUEUE_WAKING 0 | |
993 | #endif | |
994 | ||
995 | #define DEQUEUE_SLEEP 1 | |
996 | ||
997 | struct sched_class { | |
998 | const struct sched_class *next; | |
999 | ||
1000 | void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags); | |
1001 | void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags); | |
1002 | void (*yield_task) (struct rq *rq); | |
1003 | bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt); | |
1004 | ||
1005 | void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags); | |
1006 | ||
1007 | struct task_struct * (*pick_next_task) (struct rq *rq); | |
1008 | void (*put_prev_task) (struct rq *rq, struct task_struct *p); | |
1009 | ||
1010 | #ifdef CONFIG_SMP | |
ac66f547 | 1011 | int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags); |
c82ba9fa LZ |
1012 | void (*migrate_task_rq)(struct task_struct *p, int next_cpu); |
1013 | ||
1014 | void (*pre_schedule) (struct rq *this_rq, struct task_struct *task); | |
1015 | void (*post_schedule) (struct rq *this_rq); | |
1016 | void (*task_waking) (struct task_struct *task); | |
1017 | void (*task_woken) (struct rq *this_rq, struct task_struct *task); | |
1018 | ||
1019 | void (*set_cpus_allowed)(struct task_struct *p, | |
1020 | const struct cpumask *newmask); | |
1021 | ||
1022 | void (*rq_online)(struct rq *rq); | |
1023 | void (*rq_offline)(struct rq *rq); | |
1024 | #endif | |
1025 | ||
1026 | void (*set_curr_task) (struct rq *rq); | |
1027 | void (*task_tick) (struct rq *rq, struct task_struct *p, int queued); | |
1028 | void (*task_fork) (struct task_struct *p); | |
e6c390f2 | 1029 | void (*task_dead) (struct task_struct *p); |
c82ba9fa LZ |
1030 | |
1031 | void (*switched_from) (struct rq *this_rq, struct task_struct *task); | |
1032 | void (*switched_to) (struct rq *this_rq, struct task_struct *task); | |
1033 | void (*prio_changed) (struct rq *this_rq, struct task_struct *task, | |
1034 | int oldprio); | |
1035 | ||
1036 | unsigned int (*get_rr_interval) (struct rq *rq, | |
1037 | struct task_struct *task); | |
1038 | ||
1039 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
1040 | void (*task_move_group) (struct task_struct *p, int on_rq); | |
1041 | #endif | |
1042 | }; | |
029632fb PZ |
1043 | |
1044 | #define sched_class_highest (&stop_sched_class) | |
1045 | #define for_each_class(class) \ | |
1046 | for (class = sched_class_highest; class; class = class->next) | |
1047 | ||
1048 | extern const struct sched_class stop_sched_class; | |
1049 | extern const struct sched_class rt_sched_class; | |
1050 | extern const struct sched_class fair_sched_class; | |
1051 | extern const struct sched_class idle_sched_class; | |
1052 | ||
1053 | ||
1054 | #ifdef CONFIG_SMP | |
1055 | ||
b719203b LZ |
1056 | extern void update_group_power(struct sched_domain *sd, int cpu); |
1057 | ||
029632fb PZ |
1058 | extern void trigger_load_balance(struct rq *rq, int cpu); |
1059 | extern void idle_balance(int this_cpu, struct rq *this_rq); | |
1060 | ||
642dbc39 VG |
1061 | extern void idle_enter_fair(struct rq *this_rq); |
1062 | extern void idle_exit_fair(struct rq *this_rq); | |
642dbc39 | 1063 | |
029632fb PZ |
1064 | #else /* CONFIG_SMP */ |
1065 | ||
1066 | static inline void idle_balance(int cpu, struct rq *rq) | |
1067 | { | |
1068 | } | |
1069 | ||
1070 | #endif | |
1071 | ||
1072 | extern void sysrq_sched_debug_show(void); | |
1073 | extern void sched_init_granularity(void); | |
1074 | extern void update_max_interval(void); | |
029632fb PZ |
1075 | extern void init_sched_rt_class(void); |
1076 | extern void init_sched_fair_class(void); | |
1077 | ||
1078 | extern void resched_task(struct task_struct *p); | |
1079 | extern void resched_cpu(int cpu); | |
1080 | ||
1081 | extern struct rt_bandwidth def_rt_bandwidth; | |
1082 | extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime); | |
1083 | ||
556061b0 | 1084 | extern void update_idle_cpu_load(struct rq *this_rq); |
029632fb | 1085 | |
a75cdaa9 AS |
1086 | extern void init_task_runnable_average(struct task_struct *p); |
1087 | ||
73fbec60 FW |
1088 | #ifdef CONFIG_PARAVIRT |
1089 | static inline u64 steal_ticks(u64 steal) | |
1090 | { | |
1091 | if (unlikely(steal > NSEC_PER_SEC)) | |
1092 | return div_u64(steal, TICK_NSEC); | |
1093 | ||
1094 | return __iter_div_u64_rem(steal, TICK_NSEC, &steal); | |
1095 | } | |
1096 | #endif | |
1097 | ||
029632fb PZ |
1098 | static inline void inc_nr_running(struct rq *rq) |
1099 | { | |
1100 | rq->nr_running++; | |
9f3660c2 FW |
1101 | |
1102 | #ifdef CONFIG_NO_HZ_FULL | |
1103 | if (rq->nr_running == 2) { | |
1104 | if (tick_nohz_full_cpu(rq->cpu)) { | |
1105 | /* Order rq->nr_running write against the IPI */ | |
1106 | smp_wmb(); | |
1107 | smp_send_reschedule(rq->cpu); | |
1108 | } | |
1109 | } | |
1110 | #endif | |
029632fb PZ |
1111 | } |
1112 | ||
1113 | static inline void dec_nr_running(struct rq *rq) | |
1114 | { | |
1115 | rq->nr_running--; | |
1116 | } | |
1117 | ||
265f22a9 FW |
1118 | static inline void rq_last_tick_reset(struct rq *rq) |
1119 | { | |
1120 | #ifdef CONFIG_NO_HZ_FULL | |
1121 | rq->last_sched_tick = jiffies; | |
1122 | #endif | |
1123 | } | |
1124 | ||
029632fb PZ |
1125 | extern void update_rq_clock(struct rq *rq); |
1126 | ||
1127 | extern void activate_task(struct rq *rq, struct task_struct *p, int flags); | |
1128 | extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags); | |
1129 | ||
1130 | extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); | |
1131 | ||
1132 | extern const_debug unsigned int sysctl_sched_time_avg; | |
1133 | extern const_debug unsigned int sysctl_sched_nr_migrate; | |
1134 | extern const_debug unsigned int sysctl_sched_migration_cost; | |
1135 | ||
1136 | static inline u64 sched_avg_period(void) | |
1137 | { | |
1138 | return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2; | |
1139 | } | |
1140 | ||
029632fb PZ |
1141 | #ifdef CONFIG_SCHED_HRTICK |
1142 | ||
1143 | /* | |
1144 | * Use hrtick when: | |
1145 | * - enabled by features | |
1146 | * - hrtimer is actually high res | |
1147 | */ | |
1148 | static inline int hrtick_enabled(struct rq *rq) | |
1149 | { | |
1150 | if (!sched_feat(HRTICK)) | |
1151 | return 0; | |
1152 | if (!cpu_active(cpu_of(rq))) | |
1153 | return 0; | |
1154 | return hrtimer_is_hres_active(&rq->hrtick_timer); | |
1155 | } | |
1156 | ||
1157 | void hrtick_start(struct rq *rq, u64 delay); | |
1158 | ||
b39e66ea MG |
1159 | #else |
1160 | ||
1161 | static inline int hrtick_enabled(struct rq *rq) | |
1162 | { | |
1163 | return 0; | |
1164 | } | |
1165 | ||
029632fb PZ |
1166 | #endif /* CONFIG_SCHED_HRTICK */ |
1167 | ||
1168 | #ifdef CONFIG_SMP | |
1169 | extern void sched_avg_update(struct rq *rq); | |
1170 | static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) | |
1171 | { | |
1172 | rq->rt_avg += rt_delta; | |
1173 | sched_avg_update(rq); | |
1174 | } | |
1175 | #else | |
1176 | static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { } | |
1177 | static inline void sched_avg_update(struct rq *rq) { } | |
1178 | #endif | |
1179 | ||
1180 | extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period); | |
1181 | ||
1182 | #ifdef CONFIG_SMP | |
1183 | #ifdef CONFIG_PREEMPT | |
1184 | ||
1185 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2); | |
1186 | ||
1187 | /* | |
1188 | * fair double_lock_balance: Safely acquires both rq->locks in a fair | |
1189 | * way at the expense of forcing extra atomic operations in all | |
1190 | * invocations. This assures that the double_lock is acquired using the | |
1191 | * same underlying policy as the spinlock_t on this architecture, which | |
1192 | * reduces latency compared to the unfair variant below. However, it | |
1193 | * also adds more overhead and therefore may reduce throughput. | |
1194 | */ | |
1195 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1196 | __releases(this_rq->lock) | |
1197 | __acquires(busiest->lock) | |
1198 | __acquires(this_rq->lock) | |
1199 | { | |
1200 | raw_spin_unlock(&this_rq->lock); | |
1201 | double_rq_lock(this_rq, busiest); | |
1202 | ||
1203 | return 1; | |
1204 | } | |
1205 | ||
1206 | #else | |
1207 | /* | |
1208 | * Unfair double_lock_balance: Optimizes throughput at the expense of | |
1209 | * latency by eliminating extra atomic operations when the locks are | |
1210 | * already in proper order on entry. This favors lower cpu-ids and will | |
1211 | * grant the double lock to lower cpus over higher ids under contention, | |
1212 | * regardless of entry order into the function. | |
1213 | */ | |
1214 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1215 | __releases(this_rq->lock) | |
1216 | __acquires(busiest->lock) | |
1217 | __acquires(this_rq->lock) | |
1218 | { | |
1219 | int ret = 0; | |
1220 | ||
1221 | if (unlikely(!raw_spin_trylock(&busiest->lock))) { | |
1222 | if (busiest < this_rq) { | |
1223 | raw_spin_unlock(&this_rq->lock); | |
1224 | raw_spin_lock(&busiest->lock); | |
1225 | raw_spin_lock_nested(&this_rq->lock, | |
1226 | SINGLE_DEPTH_NESTING); | |
1227 | ret = 1; | |
1228 | } else | |
1229 | raw_spin_lock_nested(&busiest->lock, | |
1230 | SINGLE_DEPTH_NESTING); | |
1231 | } | |
1232 | return ret; | |
1233 | } | |
1234 | ||
1235 | #endif /* CONFIG_PREEMPT */ | |
1236 | ||
1237 | /* | |
1238 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. | |
1239 | */ | |
1240 | static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1241 | { | |
1242 | if (unlikely(!irqs_disabled())) { | |
1243 | /* printk() doesn't work good under rq->lock */ | |
1244 | raw_spin_unlock(&this_rq->lock); | |
1245 | BUG_ON(1); | |
1246 | } | |
1247 | ||
1248 | return _double_lock_balance(this_rq, busiest); | |
1249 | } | |
1250 | ||
1251 | static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) | |
1252 | __releases(busiest->lock) | |
1253 | { | |
1254 | raw_spin_unlock(&busiest->lock); | |
1255 | lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_); | |
1256 | } | |
1257 | ||
74602315 PZ |
1258 | static inline void double_lock(spinlock_t *l1, spinlock_t *l2) |
1259 | { | |
1260 | if (l1 > l2) | |
1261 | swap(l1, l2); | |
1262 | ||
1263 | spin_lock(l1); | |
1264 | spin_lock_nested(l2, SINGLE_DEPTH_NESTING); | |
1265 | } | |
1266 | ||
1267 | static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2) | |
1268 | { | |
1269 | if (l1 > l2) | |
1270 | swap(l1, l2); | |
1271 | ||
1272 | raw_spin_lock(l1); | |
1273 | raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING); | |
1274 | } | |
1275 | ||
029632fb PZ |
1276 | /* |
1277 | * double_rq_lock - safely lock two runqueues | |
1278 | * | |
1279 | * Note this does not disable interrupts like task_rq_lock, | |
1280 | * you need to do so manually before calling. | |
1281 | */ | |
1282 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
1283 | __acquires(rq1->lock) | |
1284 | __acquires(rq2->lock) | |
1285 | { | |
1286 | BUG_ON(!irqs_disabled()); | |
1287 | if (rq1 == rq2) { | |
1288 | raw_spin_lock(&rq1->lock); | |
1289 | __acquire(rq2->lock); /* Fake it out ;) */ | |
1290 | } else { | |
1291 | if (rq1 < rq2) { | |
1292 | raw_spin_lock(&rq1->lock); | |
1293 | raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING); | |
1294 | } else { | |
1295 | raw_spin_lock(&rq2->lock); | |
1296 | raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); | |
1297 | } | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | /* | |
1302 | * double_rq_unlock - safely unlock two runqueues | |
1303 | * | |
1304 | * Note this does not restore interrupts like task_rq_unlock, | |
1305 | * you need to do so manually after calling. | |
1306 | */ | |
1307 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) | |
1308 | __releases(rq1->lock) | |
1309 | __releases(rq2->lock) | |
1310 | { | |
1311 | raw_spin_unlock(&rq1->lock); | |
1312 | if (rq1 != rq2) | |
1313 | raw_spin_unlock(&rq2->lock); | |
1314 | else | |
1315 | __release(rq2->lock); | |
1316 | } | |
1317 | ||
1318 | #else /* CONFIG_SMP */ | |
1319 | ||
1320 | /* | |
1321 | * double_rq_lock - safely lock two runqueues | |
1322 | * | |
1323 | * Note this does not disable interrupts like task_rq_lock, | |
1324 | * you need to do so manually before calling. | |
1325 | */ | |
1326 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
1327 | __acquires(rq1->lock) | |
1328 | __acquires(rq2->lock) | |
1329 | { | |
1330 | BUG_ON(!irqs_disabled()); | |
1331 | BUG_ON(rq1 != rq2); | |
1332 | raw_spin_lock(&rq1->lock); | |
1333 | __acquire(rq2->lock); /* Fake it out ;) */ | |
1334 | } | |
1335 | ||
1336 | /* | |
1337 | * double_rq_unlock - safely unlock two runqueues | |
1338 | * | |
1339 | * Note this does not restore interrupts like task_rq_unlock, | |
1340 | * you need to do so manually after calling. | |
1341 | */ | |
1342 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) | |
1343 | __releases(rq1->lock) | |
1344 | __releases(rq2->lock) | |
1345 | { | |
1346 | BUG_ON(rq1 != rq2); | |
1347 | raw_spin_unlock(&rq1->lock); | |
1348 | __release(rq2->lock); | |
1349 | } | |
1350 | ||
1351 | #endif | |
1352 | ||
1353 | extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq); | |
1354 | extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq); | |
1355 | extern void print_cfs_stats(struct seq_file *m, int cpu); | |
1356 | extern void print_rt_stats(struct seq_file *m, int cpu); | |
1357 | ||
1358 | extern void init_cfs_rq(struct cfs_rq *cfs_rq); | |
1359 | extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq); | |
029632fb | 1360 | |
1ee14e6c BS |
1361 | extern void cfs_bandwidth_usage_inc(void); |
1362 | extern void cfs_bandwidth_usage_dec(void); | |
1c792db7 | 1363 | |
3451d024 | 1364 | #ifdef CONFIG_NO_HZ_COMMON |
1c792db7 SS |
1365 | enum rq_nohz_flag_bits { |
1366 | NOHZ_TICK_STOPPED, | |
1367 | NOHZ_BALANCE_KICK, | |
1368 | }; | |
1369 | ||
1370 | #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags) | |
1371 | #endif | |
73fbec60 FW |
1372 | |
1373 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | |
1374 | ||
1375 | DECLARE_PER_CPU(u64, cpu_hardirq_time); | |
1376 | DECLARE_PER_CPU(u64, cpu_softirq_time); | |
1377 | ||
1378 | #ifndef CONFIG_64BIT | |
1379 | DECLARE_PER_CPU(seqcount_t, irq_time_seq); | |
1380 | ||
1381 | static inline void irq_time_write_begin(void) | |
1382 | { | |
1383 | __this_cpu_inc(irq_time_seq.sequence); | |
1384 | smp_wmb(); | |
1385 | } | |
1386 | ||
1387 | static inline void irq_time_write_end(void) | |
1388 | { | |
1389 | smp_wmb(); | |
1390 | __this_cpu_inc(irq_time_seq.sequence); | |
1391 | } | |
1392 | ||
1393 | static inline u64 irq_time_read(int cpu) | |
1394 | { | |
1395 | u64 irq_time; | |
1396 | unsigned seq; | |
1397 | ||
1398 | do { | |
1399 | seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu)); | |
1400 | irq_time = per_cpu(cpu_softirq_time, cpu) + | |
1401 | per_cpu(cpu_hardirq_time, cpu); | |
1402 | } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq)); | |
1403 | ||
1404 | return irq_time; | |
1405 | } | |
1406 | #else /* CONFIG_64BIT */ | |
1407 | static inline void irq_time_write_begin(void) | |
1408 | { | |
1409 | } | |
1410 | ||
1411 | static inline void irq_time_write_end(void) | |
1412 | { | |
1413 | } | |
1414 | ||
1415 | static inline u64 irq_time_read(int cpu) | |
1416 | { | |
1417 | return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu); | |
1418 | } | |
1419 | #endif /* CONFIG_64BIT */ | |
1420 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ |