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