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