Commit | Line | Data |
---|---|---|
bb44e5d1 IM |
1 | /* |
2 | * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR | |
3 | * policies) | |
4 | */ | |
5 | ||
029632fb PZ |
6 | #include "sched.h" |
7 | ||
8 | #include <linux/slab.h> | |
b6366f04 | 9 | #include <linux/irq_work.h> |
029632fb | 10 | |
ce0dbbbb CW |
11 | int sched_rr_timeslice = RR_TIMESLICE; |
12 | ||
029632fb PZ |
13 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); |
14 | ||
15 | struct rt_bandwidth def_rt_bandwidth; | |
16 | ||
17 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) | |
18 | { | |
19 | struct rt_bandwidth *rt_b = | |
20 | container_of(timer, struct rt_bandwidth, rt_period_timer); | |
029632fb | 21 | int idle = 0; |
77a4d1a1 | 22 | int overrun; |
029632fb | 23 | |
77a4d1a1 | 24 | raw_spin_lock(&rt_b->rt_runtime_lock); |
029632fb | 25 | for (;;) { |
77a4d1a1 | 26 | overrun = hrtimer_forward_now(timer, rt_b->rt_period); |
029632fb PZ |
27 | if (!overrun) |
28 | break; | |
29 | ||
77a4d1a1 | 30 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
029632fb | 31 | idle = do_sched_rt_period_timer(rt_b, overrun); |
77a4d1a1 | 32 | raw_spin_lock(&rt_b->rt_runtime_lock); |
029632fb | 33 | } |
4cfafd30 PZ |
34 | if (idle) |
35 | rt_b->rt_period_active = 0; | |
77a4d1a1 | 36 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
029632fb PZ |
37 | |
38 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
39 | } | |
40 | ||
41 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) | |
42 | { | |
43 | rt_b->rt_period = ns_to_ktime(period); | |
44 | rt_b->rt_runtime = runtime; | |
45 | ||
46 | raw_spin_lock_init(&rt_b->rt_runtime_lock); | |
47 | ||
48 | hrtimer_init(&rt_b->rt_period_timer, | |
49 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
50 | rt_b->rt_period_timer.function = sched_rt_period_timer; | |
51 | } | |
52 | ||
53 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) | |
54 | { | |
55 | if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) | |
56 | return; | |
57 | ||
029632fb | 58 | raw_spin_lock(&rt_b->rt_runtime_lock); |
4cfafd30 PZ |
59 | if (!rt_b->rt_period_active) { |
60 | rt_b->rt_period_active = 1; | |
61 | hrtimer_forward_now(&rt_b->rt_period_timer, rt_b->rt_period); | |
62 | hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED); | |
63 | } | |
029632fb PZ |
64 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
65 | } | |
66 | ||
b6366f04 SR |
67 | #ifdef CONFIG_SMP |
68 | static void push_irq_work_func(struct irq_work *work); | |
69 | #endif | |
70 | ||
07c54f7a | 71 | void init_rt_rq(struct rt_rq *rt_rq) |
029632fb PZ |
72 | { |
73 | struct rt_prio_array *array; | |
74 | int i; | |
75 | ||
76 | array = &rt_rq->active; | |
77 | for (i = 0; i < MAX_RT_PRIO; i++) { | |
78 | INIT_LIST_HEAD(array->queue + i); | |
79 | __clear_bit(i, array->bitmap); | |
80 | } | |
81 | /* delimiter for bitsearch: */ | |
82 | __set_bit(MAX_RT_PRIO, array->bitmap); | |
83 | ||
84 | #if defined CONFIG_SMP | |
85 | rt_rq->highest_prio.curr = MAX_RT_PRIO; | |
86 | rt_rq->highest_prio.next = MAX_RT_PRIO; | |
87 | rt_rq->rt_nr_migratory = 0; | |
88 | rt_rq->overloaded = 0; | |
89 | plist_head_init(&rt_rq->pushable_tasks); | |
b6366f04 SR |
90 | |
91 | #ifdef HAVE_RT_PUSH_IPI | |
92 | rt_rq->push_flags = 0; | |
93 | rt_rq->push_cpu = nr_cpu_ids; | |
94 | raw_spin_lock_init(&rt_rq->push_lock); | |
95 | init_irq_work(&rt_rq->push_work, push_irq_work_func); | |
029632fb | 96 | #endif |
b6366f04 | 97 | #endif /* CONFIG_SMP */ |
f4ebcbc0 KT |
98 | /* We start is dequeued state, because no RT tasks are queued */ |
99 | rt_rq->rt_queued = 0; | |
029632fb PZ |
100 | |
101 | rt_rq->rt_time = 0; | |
102 | rt_rq->rt_throttled = 0; | |
103 | rt_rq->rt_runtime = 0; | |
104 | raw_spin_lock_init(&rt_rq->rt_runtime_lock); | |
105 | } | |
106 | ||
8f48894f | 107 | #ifdef CONFIG_RT_GROUP_SCHED |
029632fb PZ |
108 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) |
109 | { | |
110 | hrtimer_cancel(&rt_b->rt_period_timer); | |
111 | } | |
8f48894f PZ |
112 | |
113 | #define rt_entity_is_task(rt_se) (!(rt_se)->my_q) | |
114 | ||
398a153b GH |
115 | static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) |
116 | { | |
8f48894f PZ |
117 | #ifdef CONFIG_SCHED_DEBUG |
118 | WARN_ON_ONCE(!rt_entity_is_task(rt_se)); | |
119 | #endif | |
398a153b GH |
120 | return container_of(rt_se, struct task_struct, rt); |
121 | } | |
122 | ||
398a153b GH |
123 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) |
124 | { | |
125 | return rt_rq->rq; | |
126 | } | |
127 | ||
128 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) | |
129 | { | |
130 | return rt_se->rt_rq; | |
131 | } | |
132 | ||
653d07a6 KT |
133 | static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se) |
134 | { | |
135 | struct rt_rq *rt_rq = rt_se->rt_rq; | |
136 | ||
137 | return rt_rq->rq; | |
138 | } | |
139 | ||
029632fb PZ |
140 | void free_rt_sched_group(struct task_group *tg) |
141 | { | |
142 | int i; | |
143 | ||
144 | if (tg->rt_se) | |
145 | destroy_rt_bandwidth(&tg->rt_bandwidth); | |
146 | ||
147 | for_each_possible_cpu(i) { | |
148 | if (tg->rt_rq) | |
149 | kfree(tg->rt_rq[i]); | |
150 | if (tg->rt_se) | |
151 | kfree(tg->rt_se[i]); | |
152 | } | |
153 | ||
154 | kfree(tg->rt_rq); | |
155 | kfree(tg->rt_se); | |
156 | } | |
157 | ||
158 | void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, | |
159 | struct sched_rt_entity *rt_se, int cpu, | |
160 | struct sched_rt_entity *parent) | |
161 | { | |
162 | struct rq *rq = cpu_rq(cpu); | |
163 | ||
164 | rt_rq->highest_prio.curr = MAX_RT_PRIO; | |
165 | rt_rq->rt_nr_boosted = 0; | |
166 | rt_rq->rq = rq; | |
167 | rt_rq->tg = tg; | |
168 | ||
169 | tg->rt_rq[cpu] = rt_rq; | |
170 | tg->rt_se[cpu] = rt_se; | |
171 | ||
172 | if (!rt_se) | |
173 | return; | |
174 | ||
175 | if (!parent) | |
176 | rt_se->rt_rq = &rq->rt; | |
177 | else | |
178 | rt_se->rt_rq = parent->my_q; | |
179 | ||
180 | rt_se->my_q = rt_rq; | |
181 | rt_se->parent = parent; | |
182 | INIT_LIST_HEAD(&rt_se->run_list); | |
183 | } | |
184 | ||
185 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
186 | { | |
187 | struct rt_rq *rt_rq; | |
188 | struct sched_rt_entity *rt_se; | |
189 | int i; | |
190 | ||
191 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); | |
192 | if (!tg->rt_rq) | |
193 | goto err; | |
194 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); | |
195 | if (!tg->rt_se) | |
196 | goto err; | |
197 | ||
198 | init_rt_bandwidth(&tg->rt_bandwidth, | |
199 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); | |
200 | ||
201 | for_each_possible_cpu(i) { | |
202 | rt_rq = kzalloc_node(sizeof(struct rt_rq), | |
203 | GFP_KERNEL, cpu_to_node(i)); | |
204 | if (!rt_rq) | |
205 | goto err; | |
206 | ||
207 | rt_se = kzalloc_node(sizeof(struct sched_rt_entity), | |
208 | GFP_KERNEL, cpu_to_node(i)); | |
209 | if (!rt_se) | |
210 | goto err_free_rq; | |
211 | ||
07c54f7a | 212 | init_rt_rq(rt_rq); |
029632fb PZ |
213 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; |
214 | init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]); | |
215 | } | |
216 | ||
217 | return 1; | |
218 | ||
219 | err_free_rq: | |
220 | kfree(rt_rq); | |
221 | err: | |
222 | return 0; | |
223 | } | |
224 | ||
398a153b GH |
225 | #else /* CONFIG_RT_GROUP_SCHED */ |
226 | ||
a1ba4d8b PZ |
227 | #define rt_entity_is_task(rt_se) (1) |
228 | ||
8f48894f PZ |
229 | static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) |
230 | { | |
231 | return container_of(rt_se, struct task_struct, rt); | |
232 | } | |
233 | ||
398a153b GH |
234 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) |
235 | { | |
236 | return container_of(rt_rq, struct rq, rt); | |
237 | } | |
238 | ||
653d07a6 | 239 | static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se) |
398a153b GH |
240 | { |
241 | struct task_struct *p = rt_task_of(rt_se); | |
653d07a6 KT |
242 | |
243 | return task_rq(p); | |
244 | } | |
245 | ||
246 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) | |
247 | { | |
248 | struct rq *rq = rq_of_rt_se(rt_se); | |
398a153b GH |
249 | |
250 | return &rq->rt; | |
251 | } | |
252 | ||
029632fb PZ |
253 | void free_rt_sched_group(struct task_group *tg) { } |
254 | ||
255 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
256 | { | |
257 | return 1; | |
258 | } | |
398a153b GH |
259 | #endif /* CONFIG_RT_GROUP_SCHED */ |
260 | ||
4fd29176 | 261 | #ifdef CONFIG_SMP |
84de4274 | 262 | |
38033c37 PZ |
263 | static int pull_rt_task(struct rq *this_rq); |
264 | ||
dc877341 PZ |
265 | static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev) |
266 | { | |
267 | /* Try to pull RT tasks here if we lower this rq's prio */ | |
268 | return rq->rt.highest_prio.curr > prev->prio; | |
269 | } | |
270 | ||
637f5085 | 271 | static inline int rt_overloaded(struct rq *rq) |
4fd29176 | 272 | { |
637f5085 | 273 | return atomic_read(&rq->rd->rto_count); |
4fd29176 | 274 | } |
84de4274 | 275 | |
4fd29176 SR |
276 | static inline void rt_set_overload(struct rq *rq) |
277 | { | |
1f11eb6a GH |
278 | if (!rq->online) |
279 | return; | |
280 | ||
c6c4927b | 281 | cpumask_set_cpu(rq->cpu, rq->rd->rto_mask); |
4fd29176 SR |
282 | /* |
283 | * Make sure the mask is visible before we set | |
284 | * the overload count. That is checked to determine | |
285 | * if we should look at the mask. It would be a shame | |
286 | * if we looked at the mask, but the mask was not | |
287 | * updated yet. | |
7c3f2ab7 PZ |
288 | * |
289 | * Matched by the barrier in pull_rt_task(). | |
4fd29176 | 290 | */ |
7c3f2ab7 | 291 | smp_wmb(); |
637f5085 | 292 | atomic_inc(&rq->rd->rto_count); |
4fd29176 | 293 | } |
84de4274 | 294 | |
4fd29176 SR |
295 | static inline void rt_clear_overload(struct rq *rq) |
296 | { | |
1f11eb6a GH |
297 | if (!rq->online) |
298 | return; | |
299 | ||
4fd29176 | 300 | /* the order here really doesn't matter */ |
637f5085 | 301 | atomic_dec(&rq->rd->rto_count); |
c6c4927b | 302 | cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask); |
4fd29176 | 303 | } |
73fe6aae | 304 | |
398a153b | 305 | static void update_rt_migration(struct rt_rq *rt_rq) |
73fe6aae | 306 | { |
a1ba4d8b | 307 | if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) { |
398a153b GH |
308 | if (!rt_rq->overloaded) { |
309 | rt_set_overload(rq_of_rt_rq(rt_rq)); | |
310 | rt_rq->overloaded = 1; | |
cdc8eb98 | 311 | } |
398a153b GH |
312 | } else if (rt_rq->overloaded) { |
313 | rt_clear_overload(rq_of_rt_rq(rt_rq)); | |
314 | rt_rq->overloaded = 0; | |
637f5085 | 315 | } |
73fe6aae | 316 | } |
4fd29176 | 317 | |
398a153b GH |
318 | static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
319 | { | |
29baa747 PZ |
320 | struct task_struct *p; |
321 | ||
a1ba4d8b PZ |
322 | if (!rt_entity_is_task(rt_se)) |
323 | return; | |
324 | ||
29baa747 | 325 | p = rt_task_of(rt_se); |
a1ba4d8b PZ |
326 | rt_rq = &rq_of_rt_rq(rt_rq)->rt; |
327 | ||
328 | rt_rq->rt_nr_total++; | |
29baa747 | 329 | if (p->nr_cpus_allowed > 1) |
398a153b GH |
330 | rt_rq->rt_nr_migratory++; |
331 | ||
332 | update_rt_migration(rt_rq); | |
333 | } | |
334 | ||
335 | static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
336 | { | |
29baa747 PZ |
337 | struct task_struct *p; |
338 | ||
a1ba4d8b PZ |
339 | if (!rt_entity_is_task(rt_se)) |
340 | return; | |
341 | ||
29baa747 | 342 | p = rt_task_of(rt_se); |
a1ba4d8b PZ |
343 | rt_rq = &rq_of_rt_rq(rt_rq)->rt; |
344 | ||
345 | rt_rq->rt_nr_total--; | |
29baa747 | 346 | if (p->nr_cpus_allowed > 1) |
398a153b GH |
347 | rt_rq->rt_nr_migratory--; |
348 | ||
349 | update_rt_migration(rt_rq); | |
350 | } | |
351 | ||
5181f4a4 SR |
352 | static inline int has_pushable_tasks(struct rq *rq) |
353 | { | |
354 | return !plist_head_empty(&rq->rt.pushable_tasks); | |
355 | } | |
356 | ||
e3fca9e7 PZ |
357 | static DEFINE_PER_CPU(struct callback_head, rt_balance_head); |
358 | ||
359 | static void push_rt_tasks(struct rq *); | |
360 | ||
361 | static inline void queue_push_tasks(struct rq *rq) | |
dc877341 | 362 | { |
e3fca9e7 PZ |
363 | if (!has_pushable_tasks(rq)) |
364 | return; | |
365 | ||
366 | queue_balance_callback(rq, &per_cpu(rt_balance_head, rq->cpu), push_rt_tasks); | |
dc877341 PZ |
367 | } |
368 | ||
917b627d GH |
369 | static void enqueue_pushable_task(struct rq *rq, struct task_struct *p) |
370 | { | |
371 | plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); | |
372 | plist_node_init(&p->pushable_tasks, p->prio); | |
373 | plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks); | |
5181f4a4 SR |
374 | |
375 | /* Update the highest prio pushable task */ | |
376 | if (p->prio < rq->rt.highest_prio.next) | |
377 | rq->rt.highest_prio.next = p->prio; | |
917b627d GH |
378 | } |
379 | ||
380 | static void dequeue_pushable_task(struct rq *rq, struct task_struct *p) | |
381 | { | |
382 | plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); | |
917b627d | 383 | |
5181f4a4 SR |
384 | /* Update the new highest prio pushable task */ |
385 | if (has_pushable_tasks(rq)) { | |
386 | p = plist_first_entry(&rq->rt.pushable_tasks, | |
387 | struct task_struct, pushable_tasks); | |
388 | rq->rt.highest_prio.next = p->prio; | |
389 | } else | |
390 | rq->rt.highest_prio.next = MAX_RT_PRIO; | |
bcf08df3 IM |
391 | } |
392 | ||
917b627d GH |
393 | #else |
394 | ||
ceacc2c1 | 395 | static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p) |
fa85ae24 | 396 | { |
6f505b16 PZ |
397 | } |
398 | ||
ceacc2c1 PZ |
399 | static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p) |
400 | { | |
401 | } | |
402 | ||
b07430ac | 403 | static inline |
ceacc2c1 PZ |
404 | void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
405 | { | |
406 | } | |
407 | ||
398a153b | 408 | static inline |
ceacc2c1 PZ |
409 | void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
410 | { | |
411 | } | |
917b627d | 412 | |
dc877341 PZ |
413 | static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev) |
414 | { | |
415 | return false; | |
416 | } | |
417 | ||
418 | static inline int pull_rt_task(struct rq *this_rq) | |
419 | { | |
420 | return 0; | |
421 | } | |
422 | ||
e3fca9e7 | 423 | static inline void queue_push_tasks(struct rq *rq) |
dc877341 PZ |
424 | { |
425 | } | |
4fd29176 SR |
426 | #endif /* CONFIG_SMP */ |
427 | ||
f4ebcbc0 KT |
428 | static void enqueue_top_rt_rq(struct rt_rq *rt_rq); |
429 | static void dequeue_top_rt_rq(struct rt_rq *rt_rq); | |
430 | ||
6f505b16 PZ |
431 | static inline int on_rt_rq(struct sched_rt_entity *rt_se) |
432 | { | |
433 | return !list_empty(&rt_se->run_list); | |
434 | } | |
435 | ||
052f1dc7 | 436 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 437 | |
9f0c1e56 | 438 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) |
6f505b16 PZ |
439 | { |
440 | if (!rt_rq->tg) | |
9f0c1e56 | 441 | return RUNTIME_INF; |
6f505b16 | 442 | |
ac086bc2 PZ |
443 | return rt_rq->rt_runtime; |
444 | } | |
445 | ||
446 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) | |
447 | { | |
448 | return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period); | |
6f505b16 PZ |
449 | } |
450 | ||
ec514c48 CX |
451 | typedef struct task_group *rt_rq_iter_t; |
452 | ||
1c09ab0d YZ |
453 | static inline struct task_group *next_task_group(struct task_group *tg) |
454 | { | |
455 | do { | |
456 | tg = list_entry_rcu(tg->list.next, | |
457 | typeof(struct task_group), list); | |
458 | } while (&tg->list != &task_groups && task_group_is_autogroup(tg)); | |
459 | ||
460 | if (&tg->list == &task_groups) | |
461 | tg = NULL; | |
462 | ||
463 | return tg; | |
464 | } | |
465 | ||
466 | #define for_each_rt_rq(rt_rq, iter, rq) \ | |
467 | for (iter = container_of(&task_groups, typeof(*iter), list); \ | |
468 | (iter = next_task_group(iter)) && \ | |
469 | (rt_rq = iter->rt_rq[cpu_of(rq)]);) | |
ec514c48 | 470 | |
6f505b16 PZ |
471 | #define for_each_sched_rt_entity(rt_se) \ |
472 | for (; rt_se; rt_se = rt_se->parent) | |
473 | ||
474 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) | |
475 | { | |
476 | return rt_se->my_q; | |
477 | } | |
478 | ||
37dad3fc | 479 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head); |
6f505b16 PZ |
480 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se); |
481 | ||
9f0c1e56 | 482 | static void sched_rt_rq_enqueue(struct rt_rq *rt_rq) |
6f505b16 | 483 | { |
f6121f4f | 484 | struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr; |
8875125e | 485 | struct rq *rq = rq_of_rt_rq(rt_rq); |
74b7eb58 YZ |
486 | struct sched_rt_entity *rt_se; |
487 | ||
8875125e | 488 | int cpu = cpu_of(rq); |
0c3b9168 BS |
489 | |
490 | rt_se = rt_rq->tg->rt_se[cpu]; | |
6f505b16 | 491 | |
f6121f4f | 492 | if (rt_rq->rt_nr_running) { |
f4ebcbc0 KT |
493 | if (!rt_se) |
494 | enqueue_top_rt_rq(rt_rq); | |
495 | else if (!on_rt_rq(rt_se)) | |
37dad3fc | 496 | enqueue_rt_entity(rt_se, false); |
f4ebcbc0 | 497 | |
e864c499 | 498 | if (rt_rq->highest_prio.curr < curr->prio) |
8875125e | 499 | resched_curr(rq); |
6f505b16 PZ |
500 | } |
501 | } | |
502 | ||
9f0c1e56 | 503 | static void sched_rt_rq_dequeue(struct rt_rq *rt_rq) |
6f505b16 | 504 | { |
74b7eb58 | 505 | struct sched_rt_entity *rt_se; |
0c3b9168 | 506 | int cpu = cpu_of(rq_of_rt_rq(rt_rq)); |
74b7eb58 | 507 | |
0c3b9168 | 508 | rt_se = rt_rq->tg->rt_se[cpu]; |
6f505b16 | 509 | |
f4ebcbc0 KT |
510 | if (!rt_se) |
511 | dequeue_top_rt_rq(rt_rq); | |
512 | else if (on_rt_rq(rt_se)) | |
6f505b16 PZ |
513 | dequeue_rt_entity(rt_se); |
514 | } | |
515 | ||
46383648 KT |
516 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) |
517 | { | |
518 | return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted; | |
519 | } | |
520 | ||
23b0fdfc PZ |
521 | static int rt_se_boosted(struct sched_rt_entity *rt_se) |
522 | { | |
523 | struct rt_rq *rt_rq = group_rt_rq(rt_se); | |
524 | struct task_struct *p; | |
525 | ||
526 | if (rt_rq) | |
527 | return !!rt_rq->rt_nr_boosted; | |
528 | ||
529 | p = rt_task_of(rt_se); | |
530 | return p->prio != p->normal_prio; | |
531 | } | |
532 | ||
d0b27fa7 | 533 | #ifdef CONFIG_SMP |
c6c4927b | 534 | static inline const struct cpumask *sched_rt_period_mask(void) |
d0b27fa7 | 535 | { |
424c93fe | 536 | return this_rq()->rd->span; |
d0b27fa7 | 537 | } |
6f505b16 | 538 | #else |
c6c4927b | 539 | static inline const struct cpumask *sched_rt_period_mask(void) |
d0b27fa7 | 540 | { |
c6c4927b | 541 | return cpu_online_mask; |
d0b27fa7 PZ |
542 | } |
543 | #endif | |
6f505b16 | 544 | |
d0b27fa7 PZ |
545 | static inline |
546 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) | |
6f505b16 | 547 | { |
d0b27fa7 PZ |
548 | return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu]; |
549 | } | |
9f0c1e56 | 550 | |
ac086bc2 PZ |
551 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) |
552 | { | |
553 | return &rt_rq->tg->rt_bandwidth; | |
554 | } | |
555 | ||
55e12e5e | 556 | #else /* !CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 PZ |
557 | |
558 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) | |
559 | { | |
ac086bc2 PZ |
560 | return rt_rq->rt_runtime; |
561 | } | |
562 | ||
563 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) | |
564 | { | |
565 | return ktime_to_ns(def_rt_bandwidth.rt_period); | |
6f505b16 PZ |
566 | } |
567 | ||
ec514c48 CX |
568 | typedef struct rt_rq *rt_rq_iter_t; |
569 | ||
570 | #define for_each_rt_rq(rt_rq, iter, rq) \ | |
571 | for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL) | |
572 | ||
6f505b16 PZ |
573 | #define for_each_sched_rt_entity(rt_se) \ |
574 | for (; rt_se; rt_se = NULL) | |
575 | ||
576 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) | |
577 | { | |
578 | return NULL; | |
579 | } | |
580 | ||
9f0c1e56 | 581 | static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq) |
6f505b16 | 582 | { |
f4ebcbc0 KT |
583 | struct rq *rq = rq_of_rt_rq(rt_rq); |
584 | ||
585 | if (!rt_rq->rt_nr_running) | |
586 | return; | |
587 | ||
588 | enqueue_top_rt_rq(rt_rq); | |
8875125e | 589 | resched_curr(rq); |
6f505b16 PZ |
590 | } |
591 | ||
9f0c1e56 | 592 | static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq) |
6f505b16 | 593 | { |
f4ebcbc0 | 594 | dequeue_top_rt_rq(rt_rq); |
6f505b16 PZ |
595 | } |
596 | ||
46383648 KT |
597 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) |
598 | { | |
599 | return rt_rq->rt_throttled; | |
600 | } | |
601 | ||
c6c4927b | 602 | static inline const struct cpumask *sched_rt_period_mask(void) |
d0b27fa7 | 603 | { |
c6c4927b | 604 | return cpu_online_mask; |
d0b27fa7 PZ |
605 | } |
606 | ||
607 | static inline | |
608 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) | |
609 | { | |
610 | return &cpu_rq(cpu)->rt; | |
611 | } | |
612 | ||
ac086bc2 PZ |
613 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) |
614 | { | |
615 | return &def_rt_bandwidth; | |
616 | } | |
617 | ||
55e12e5e | 618 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 | 619 | |
faa59937 JL |
620 | bool sched_rt_bandwidth_account(struct rt_rq *rt_rq) |
621 | { | |
622 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); | |
623 | ||
624 | return (hrtimer_active(&rt_b->rt_period_timer) || | |
625 | rt_rq->rt_time < rt_b->rt_runtime); | |
626 | } | |
627 | ||
ac086bc2 | 628 | #ifdef CONFIG_SMP |
78333cdd PZ |
629 | /* |
630 | * We ran out of runtime, see if we can borrow some from our neighbours. | |
631 | */ | |
b79f3833 | 632 | static int do_balance_runtime(struct rt_rq *rt_rq) |
ac086bc2 PZ |
633 | { |
634 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); | |
aa7f6730 | 635 | struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd; |
ac086bc2 PZ |
636 | int i, weight, more = 0; |
637 | u64 rt_period; | |
638 | ||
c6c4927b | 639 | weight = cpumask_weight(rd->span); |
ac086bc2 | 640 | |
0986b11b | 641 | raw_spin_lock(&rt_b->rt_runtime_lock); |
ac086bc2 | 642 | rt_period = ktime_to_ns(rt_b->rt_period); |
c6c4927b | 643 | for_each_cpu(i, rd->span) { |
ac086bc2 PZ |
644 | struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); |
645 | s64 diff; | |
646 | ||
647 | if (iter == rt_rq) | |
648 | continue; | |
649 | ||
0986b11b | 650 | raw_spin_lock(&iter->rt_runtime_lock); |
78333cdd PZ |
651 | /* |
652 | * Either all rqs have inf runtime and there's nothing to steal | |
653 | * or __disable_runtime() below sets a specific rq to inf to | |
654 | * indicate its been disabled and disalow stealing. | |
655 | */ | |
7def2be1 PZ |
656 | if (iter->rt_runtime == RUNTIME_INF) |
657 | goto next; | |
658 | ||
78333cdd PZ |
659 | /* |
660 | * From runqueues with spare time, take 1/n part of their | |
661 | * spare time, but no more than our period. | |
662 | */ | |
ac086bc2 PZ |
663 | diff = iter->rt_runtime - iter->rt_time; |
664 | if (diff > 0) { | |
58838cf3 | 665 | diff = div_u64((u64)diff, weight); |
ac086bc2 PZ |
666 | if (rt_rq->rt_runtime + diff > rt_period) |
667 | diff = rt_period - rt_rq->rt_runtime; | |
668 | iter->rt_runtime -= diff; | |
669 | rt_rq->rt_runtime += diff; | |
670 | more = 1; | |
671 | if (rt_rq->rt_runtime == rt_period) { | |
0986b11b | 672 | raw_spin_unlock(&iter->rt_runtime_lock); |
ac086bc2 PZ |
673 | break; |
674 | } | |
675 | } | |
7def2be1 | 676 | next: |
0986b11b | 677 | raw_spin_unlock(&iter->rt_runtime_lock); |
ac086bc2 | 678 | } |
0986b11b | 679 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
ac086bc2 PZ |
680 | |
681 | return more; | |
682 | } | |
7def2be1 | 683 | |
78333cdd PZ |
684 | /* |
685 | * Ensure this RQ takes back all the runtime it lend to its neighbours. | |
686 | */ | |
7def2be1 PZ |
687 | static void __disable_runtime(struct rq *rq) |
688 | { | |
689 | struct root_domain *rd = rq->rd; | |
ec514c48 | 690 | rt_rq_iter_t iter; |
7def2be1 PZ |
691 | struct rt_rq *rt_rq; |
692 | ||
693 | if (unlikely(!scheduler_running)) | |
694 | return; | |
695 | ||
ec514c48 | 696 | for_each_rt_rq(rt_rq, iter, rq) { |
7def2be1 PZ |
697 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
698 | s64 want; | |
699 | int i; | |
700 | ||
0986b11b TG |
701 | raw_spin_lock(&rt_b->rt_runtime_lock); |
702 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
78333cdd PZ |
703 | /* |
704 | * Either we're all inf and nobody needs to borrow, or we're | |
705 | * already disabled and thus have nothing to do, or we have | |
706 | * exactly the right amount of runtime to take out. | |
707 | */ | |
7def2be1 PZ |
708 | if (rt_rq->rt_runtime == RUNTIME_INF || |
709 | rt_rq->rt_runtime == rt_b->rt_runtime) | |
710 | goto balanced; | |
0986b11b | 711 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
7def2be1 | 712 | |
78333cdd PZ |
713 | /* |
714 | * Calculate the difference between what we started out with | |
715 | * and what we current have, that's the amount of runtime | |
716 | * we lend and now have to reclaim. | |
717 | */ | |
7def2be1 PZ |
718 | want = rt_b->rt_runtime - rt_rq->rt_runtime; |
719 | ||
78333cdd PZ |
720 | /* |
721 | * Greedy reclaim, take back as much as we can. | |
722 | */ | |
c6c4927b | 723 | for_each_cpu(i, rd->span) { |
7def2be1 PZ |
724 | struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); |
725 | s64 diff; | |
726 | ||
78333cdd PZ |
727 | /* |
728 | * Can't reclaim from ourselves or disabled runqueues. | |
729 | */ | |
f1679d08 | 730 | if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF) |
7def2be1 PZ |
731 | continue; |
732 | ||
0986b11b | 733 | raw_spin_lock(&iter->rt_runtime_lock); |
7def2be1 PZ |
734 | if (want > 0) { |
735 | diff = min_t(s64, iter->rt_runtime, want); | |
736 | iter->rt_runtime -= diff; | |
737 | want -= diff; | |
738 | } else { | |
739 | iter->rt_runtime -= want; | |
740 | want -= want; | |
741 | } | |
0986b11b | 742 | raw_spin_unlock(&iter->rt_runtime_lock); |
7def2be1 PZ |
743 | |
744 | if (!want) | |
745 | break; | |
746 | } | |
747 | ||
0986b11b | 748 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
78333cdd PZ |
749 | /* |
750 | * We cannot be left wanting - that would mean some runtime | |
751 | * leaked out of the system. | |
752 | */ | |
7def2be1 PZ |
753 | BUG_ON(want); |
754 | balanced: | |
78333cdd PZ |
755 | /* |
756 | * Disable all the borrow logic by pretending we have inf | |
757 | * runtime - in which case borrowing doesn't make sense. | |
758 | */ | |
7def2be1 | 759 | rt_rq->rt_runtime = RUNTIME_INF; |
a4c96ae3 | 760 | rt_rq->rt_throttled = 0; |
0986b11b TG |
761 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
762 | raw_spin_unlock(&rt_b->rt_runtime_lock); | |
99b62567 KT |
763 | |
764 | /* Make rt_rq available for pick_next_task() */ | |
765 | sched_rt_rq_enqueue(rt_rq); | |
7def2be1 PZ |
766 | } |
767 | } | |
768 | ||
7def2be1 PZ |
769 | static void __enable_runtime(struct rq *rq) |
770 | { | |
ec514c48 | 771 | rt_rq_iter_t iter; |
7def2be1 PZ |
772 | struct rt_rq *rt_rq; |
773 | ||
774 | if (unlikely(!scheduler_running)) | |
775 | return; | |
776 | ||
78333cdd PZ |
777 | /* |
778 | * Reset each runqueue's bandwidth settings | |
779 | */ | |
ec514c48 | 780 | for_each_rt_rq(rt_rq, iter, rq) { |
7def2be1 PZ |
781 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
782 | ||
0986b11b TG |
783 | raw_spin_lock(&rt_b->rt_runtime_lock); |
784 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
7def2be1 PZ |
785 | rt_rq->rt_runtime = rt_b->rt_runtime; |
786 | rt_rq->rt_time = 0; | |
baf25731 | 787 | rt_rq->rt_throttled = 0; |
0986b11b TG |
788 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
789 | raw_spin_unlock(&rt_b->rt_runtime_lock); | |
7def2be1 PZ |
790 | } |
791 | } | |
792 | ||
eff6549b PZ |
793 | static int balance_runtime(struct rt_rq *rt_rq) |
794 | { | |
795 | int more = 0; | |
796 | ||
4a6184ce PZ |
797 | if (!sched_feat(RT_RUNTIME_SHARE)) |
798 | return more; | |
799 | ||
eff6549b | 800 | if (rt_rq->rt_time > rt_rq->rt_runtime) { |
0986b11b | 801 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
eff6549b | 802 | more = do_balance_runtime(rt_rq); |
0986b11b | 803 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
eff6549b PZ |
804 | } |
805 | ||
806 | return more; | |
807 | } | |
55e12e5e | 808 | #else /* !CONFIG_SMP */ |
eff6549b PZ |
809 | static inline int balance_runtime(struct rt_rq *rt_rq) |
810 | { | |
811 | return 0; | |
812 | } | |
55e12e5e | 813 | #endif /* CONFIG_SMP */ |
ac086bc2 | 814 | |
eff6549b PZ |
815 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun) |
816 | { | |
42c62a58 | 817 | int i, idle = 1, throttled = 0; |
c6c4927b | 818 | const struct cpumask *span; |
eff6549b | 819 | |
eff6549b | 820 | span = sched_rt_period_mask(); |
e221d028 MG |
821 | #ifdef CONFIG_RT_GROUP_SCHED |
822 | /* | |
823 | * FIXME: isolated CPUs should really leave the root task group, | |
824 | * whether they are isolcpus or were isolated via cpusets, lest | |
825 | * the timer run on a CPU which does not service all runqueues, | |
826 | * potentially leaving other CPUs indefinitely throttled. If | |
827 | * isolation is really required, the user will turn the throttle | |
828 | * off to kill the perturbations it causes anyway. Meanwhile, | |
829 | * this maintains functionality for boot and/or troubleshooting. | |
830 | */ | |
831 | if (rt_b == &root_task_group.rt_bandwidth) | |
832 | span = cpu_online_mask; | |
833 | #endif | |
c6c4927b | 834 | for_each_cpu(i, span) { |
eff6549b PZ |
835 | int enqueue = 0; |
836 | struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i); | |
837 | struct rq *rq = rq_of_rt_rq(rt_rq); | |
838 | ||
05fa785c | 839 | raw_spin_lock(&rq->lock); |
eff6549b PZ |
840 | if (rt_rq->rt_time) { |
841 | u64 runtime; | |
842 | ||
0986b11b | 843 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
eff6549b PZ |
844 | if (rt_rq->rt_throttled) |
845 | balance_runtime(rt_rq); | |
846 | runtime = rt_rq->rt_runtime; | |
847 | rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime); | |
848 | if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) { | |
849 | rt_rq->rt_throttled = 0; | |
850 | enqueue = 1; | |
61eadef6 MG |
851 | |
852 | /* | |
9edfbfed PZ |
853 | * When we're idle and a woken (rt) task is |
854 | * throttled check_preempt_curr() will set | |
855 | * skip_update and the time between the wakeup | |
856 | * and this unthrottle will get accounted as | |
857 | * 'runtime'. | |
61eadef6 MG |
858 | */ |
859 | if (rt_rq->rt_nr_running && rq->curr == rq->idle) | |
9edfbfed | 860 | rq_clock_skip_update(rq, false); |
eff6549b PZ |
861 | } |
862 | if (rt_rq->rt_time || rt_rq->rt_nr_running) | |
863 | idle = 0; | |
0986b11b | 864 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
0c3b9168 | 865 | } else if (rt_rq->rt_nr_running) { |
6c3df255 | 866 | idle = 0; |
0c3b9168 BS |
867 | if (!rt_rq_throttled(rt_rq)) |
868 | enqueue = 1; | |
869 | } | |
42c62a58 PZ |
870 | if (rt_rq->rt_throttled) |
871 | throttled = 1; | |
eff6549b PZ |
872 | |
873 | if (enqueue) | |
874 | sched_rt_rq_enqueue(rt_rq); | |
05fa785c | 875 | raw_spin_unlock(&rq->lock); |
eff6549b PZ |
876 | } |
877 | ||
42c62a58 PZ |
878 | if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)) |
879 | return 1; | |
880 | ||
eff6549b PZ |
881 | return idle; |
882 | } | |
ac086bc2 | 883 | |
6f505b16 PZ |
884 | static inline int rt_se_prio(struct sched_rt_entity *rt_se) |
885 | { | |
052f1dc7 | 886 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 PZ |
887 | struct rt_rq *rt_rq = group_rt_rq(rt_se); |
888 | ||
889 | if (rt_rq) | |
e864c499 | 890 | return rt_rq->highest_prio.curr; |
6f505b16 PZ |
891 | #endif |
892 | ||
893 | return rt_task_of(rt_se)->prio; | |
894 | } | |
895 | ||
9f0c1e56 | 896 | static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq) |
6f505b16 | 897 | { |
9f0c1e56 | 898 | u64 runtime = sched_rt_runtime(rt_rq); |
fa85ae24 | 899 | |
fa85ae24 | 900 | if (rt_rq->rt_throttled) |
23b0fdfc | 901 | return rt_rq_throttled(rt_rq); |
fa85ae24 | 902 | |
5b680fd6 | 903 | if (runtime >= sched_rt_period(rt_rq)) |
ac086bc2 PZ |
904 | return 0; |
905 | ||
b79f3833 PZ |
906 | balance_runtime(rt_rq); |
907 | runtime = sched_rt_runtime(rt_rq); | |
908 | if (runtime == RUNTIME_INF) | |
909 | return 0; | |
ac086bc2 | 910 | |
9f0c1e56 | 911 | if (rt_rq->rt_time > runtime) { |
7abc63b1 PZ |
912 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
913 | ||
914 | /* | |
915 | * Don't actually throttle groups that have no runtime assigned | |
916 | * but accrue some time due to boosting. | |
917 | */ | |
918 | if (likely(rt_b->rt_runtime)) { | |
919 | rt_rq->rt_throttled = 1; | |
c224815d | 920 | printk_deferred_once("sched: RT throttling activated\n"); |
7abc63b1 PZ |
921 | } else { |
922 | /* | |
923 | * In case we did anyway, make it go away, | |
924 | * replenishment is a joke, since it will replenish us | |
925 | * with exactly 0 ns. | |
926 | */ | |
927 | rt_rq->rt_time = 0; | |
928 | } | |
929 | ||
23b0fdfc | 930 | if (rt_rq_throttled(rt_rq)) { |
9f0c1e56 | 931 | sched_rt_rq_dequeue(rt_rq); |
23b0fdfc PZ |
932 | return 1; |
933 | } | |
fa85ae24 PZ |
934 | } |
935 | ||
936 | return 0; | |
937 | } | |
938 | ||
bb44e5d1 IM |
939 | /* |
940 | * Update the current task's runtime statistics. Skip current tasks that | |
941 | * are not in our scheduling class. | |
942 | */ | |
a9957449 | 943 | static void update_curr_rt(struct rq *rq) |
bb44e5d1 IM |
944 | { |
945 | struct task_struct *curr = rq->curr; | |
6f505b16 | 946 | struct sched_rt_entity *rt_se = &curr->rt; |
bb44e5d1 IM |
947 | u64 delta_exec; |
948 | ||
06c3bc65 | 949 | if (curr->sched_class != &rt_sched_class) |
bb44e5d1 IM |
950 | return; |
951 | ||
78becc27 | 952 | delta_exec = rq_clock_task(rq) - curr->se.exec_start; |
fc79e240 KT |
953 | if (unlikely((s64)delta_exec <= 0)) |
954 | return; | |
6cfb0d5d | 955 | |
42c62a58 PZ |
956 | schedstat_set(curr->se.statistics.exec_max, |
957 | max(curr->se.statistics.exec_max, delta_exec)); | |
bb44e5d1 IM |
958 | |
959 | curr->se.sum_exec_runtime += delta_exec; | |
f06febc9 FM |
960 | account_group_exec_runtime(curr, delta_exec); |
961 | ||
78becc27 | 962 | curr->se.exec_start = rq_clock_task(rq); |
d842de87 | 963 | cpuacct_charge(curr, delta_exec); |
fa85ae24 | 964 | |
e9e9250b PZ |
965 | sched_rt_avg_update(rq, delta_exec); |
966 | ||
0b148fa0 PZ |
967 | if (!rt_bandwidth_enabled()) |
968 | return; | |
969 | ||
354d60c2 | 970 | for_each_sched_rt_entity(rt_se) { |
0b07939c | 971 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
354d60c2 | 972 | |
cc2991cf | 973 | if (sched_rt_runtime(rt_rq) != RUNTIME_INF) { |
0986b11b | 974 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
cc2991cf PZ |
975 | rt_rq->rt_time += delta_exec; |
976 | if (sched_rt_runtime_exceeded(rt_rq)) | |
8875125e | 977 | resched_curr(rq); |
0986b11b | 978 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
cc2991cf | 979 | } |
354d60c2 | 980 | } |
bb44e5d1 IM |
981 | } |
982 | ||
f4ebcbc0 KT |
983 | static void |
984 | dequeue_top_rt_rq(struct rt_rq *rt_rq) | |
985 | { | |
986 | struct rq *rq = rq_of_rt_rq(rt_rq); | |
987 | ||
988 | BUG_ON(&rq->rt != rt_rq); | |
989 | ||
990 | if (!rt_rq->rt_queued) | |
991 | return; | |
992 | ||
993 | BUG_ON(!rq->nr_running); | |
994 | ||
72465447 | 995 | sub_nr_running(rq, rt_rq->rt_nr_running); |
f4ebcbc0 KT |
996 | rt_rq->rt_queued = 0; |
997 | } | |
998 | ||
999 | static void | |
1000 | enqueue_top_rt_rq(struct rt_rq *rt_rq) | |
1001 | { | |
1002 | struct rq *rq = rq_of_rt_rq(rt_rq); | |
1003 | ||
1004 | BUG_ON(&rq->rt != rt_rq); | |
1005 | ||
1006 | if (rt_rq->rt_queued) | |
1007 | return; | |
1008 | if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running) | |
1009 | return; | |
1010 | ||
72465447 | 1011 | add_nr_running(rq, rt_rq->rt_nr_running); |
f4ebcbc0 KT |
1012 | rt_rq->rt_queued = 1; |
1013 | } | |
1014 | ||
398a153b | 1015 | #if defined CONFIG_SMP |
e864c499 | 1016 | |
398a153b GH |
1017 | static void |
1018 | inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) | |
63489e45 | 1019 | { |
4d984277 | 1020 | struct rq *rq = rq_of_rt_rq(rt_rq); |
1f11eb6a | 1021 | |
757dfcaa KT |
1022 | #ifdef CONFIG_RT_GROUP_SCHED |
1023 | /* | |
1024 | * Change rq's cpupri only if rt_rq is the top queue. | |
1025 | */ | |
1026 | if (&rq->rt != rt_rq) | |
1027 | return; | |
1028 | #endif | |
5181f4a4 SR |
1029 | if (rq->online && prio < prev_prio) |
1030 | cpupri_set(&rq->rd->cpupri, rq->cpu, prio); | |
398a153b | 1031 | } |
73fe6aae | 1032 | |
398a153b GH |
1033 | static void |
1034 | dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) | |
1035 | { | |
1036 | struct rq *rq = rq_of_rt_rq(rt_rq); | |
d0b27fa7 | 1037 | |
757dfcaa KT |
1038 | #ifdef CONFIG_RT_GROUP_SCHED |
1039 | /* | |
1040 | * Change rq's cpupri only if rt_rq is the top queue. | |
1041 | */ | |
1042 | if (&rq->rt != rt_rq) | |
1043 | return; | |
1044 | #endif | |
398a153b GH |
1045 | if (rq->online && rt_rq->highest_prio.curr != prev_prio) |
1046 | cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr); | |
63489e45 SR |
1047 | } |
1048 | ||
398a153b GH |
1049 | #else /* CONFIG_SMP */ |
1050 | ||
6f505b16 | 1051 | static inline |
398a153b GH |
1052 | void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} |
1053 | static inline | |
1054 | void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} | |
1055 | ||
1056 | #endif /* CONFIG_SMP */ | |
6e0534f2 | 1057 | |
052f1dc7 | 1058 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
398a153b GH |
1059 | static void |
1060 | inc_rt_prio(struct rt_rq *rt_rq, int prio) | |
1061 | { | |
1062 | int prev_prio = rt_rq->highest_prio.curr; | |
1063 | ||
1064 | if (prio < prev_prio) | |
1065 | rt_rq->highest_prio.curr = prio; | |
1066 | ||
1067 | inc_rt_prio_smp(rt_rq, prio, prev_prio); | |
1068 | } | |
1069 | ||
1070 | static void | |
1071 | dec_rt_prio(struct rt_rq *rt_rq, int prio) | |
1072 | { | |
1073 | int prev_prio = rt_rq->highest_prio.curr; | |
1074 | ||
6f505b16 | 1075 | if (rt_rq->rt_nr_running) { |
764a9d6f | 1076 | |
398a153b | 1077 | WARN_ON(prio < prev_prio); |
764a9d6f | 1078 | |
e864c499 | 1079 | /* |
398a153b GH |
1080 | * This may have been our highest task, and therefore |
1081 | * we may have some recomputation to do | |
e864c499 | 1082 | */ |
398a153b | 1083 | if (prio == prev_prio) { |
e864c499 GH |
1084 | struct rt_prio_array *array = &rt_rq->active; |
1085 | ||
1086 | rt_rq->highest_prio.curr = | |
764a9d6f | 1087 | sched_find_first_bit(array->bitmap); |
e864c499 GH |
1088 | } |
1089 | ||
764a9d6f | 1090 | } else |
e864c499 | 1091 | rt_rq->highest_prio.curr = MAX_RT_PRIO; |
73fe6aae | 1092 | |
398a153b GH |
1093 | dec_rt_prio_smp(rt_rq, prio, prev_prio); |
1094 | } | |
1f11eb6a | 1095 | |
398a153b GH |
1096 | #else |
1097 | ||
1098 | static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {} | |
1099 | static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {} | |
1100 | ||
1101 | #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */ | |
6e0534f2 | 1102 | |
052f1dc7 | 1103 | #ifdef CONFIG_RT_GROUP_SCHED |
398a153b GH |
1104 | |
1105 | static void | |
1106 | inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1107 | { | |
1108 | if (rt_se_boosted(rt_se)) | |
1109 | rt_rq->rt_nr_boosted++; | |
1110 | ||
1111 | if (rt_rq->tg) | |
1112 | start_rt_bandwidth(&rt_rq->tg->rt_bandwidth); | |
1113 | } | |
1114 | ||
1115 | static void | |
1116 | dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1117 | { | |
23b0fdfc PZ |
1118 | if (rt_se_boosted(rt_se)) |
1119 | rt_rq->rt_nr_boosted--; | |
1120 | ||
1121 | WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted); | |
398a153b GH |
1122 | } |
1123 | ||
1124 | #else /* CONFIG_RT_GROUP_SCHED */ | |
1125 | ||
1126 | static void | |
1127 | inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1128 | { | |
1129 | start_rt_bandwidth(&def_rt_bandwidth); | |
1130 | } | |
1131 | ||
1132 | static inline | |
1133 | void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {} | |
1134 | ||
1135 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
1136 | ||
22abdef3 KT |
1137 | static inline |
1138 | unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se) | |
1139 | { | |
1140 | struct rt_rq *group_rq = group_rt_rq(rt_se); | |
1141 | ||
1142 | if (group_rq) | |
1143 | return group_rq->rt_nr_running; | |
1144 | else | |
1145 | return 1; | |
1146 | } | |
1147 | ||
398a153b GH |
1148 | static inline |
1149 | void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1150 | { | |
1151 | int prio = rt_se_prio(rt_se); | |
1152 | ||
1153 | WARN_ON(!rt_prio(prio)); | |
22abdef3 | 1154 | rt_rq->rt_nr_running += rt_se_nr_running(rt_se); |
398a153b GH |
1155 | |
1156 | inc_rt_prio(rt_rq, prio); | |
1157 | inc_rt_migration(rt_se, rt_rq); | |
1158 | inc_rt_group(rt_se, rt_rq); | |
1159 | } | |
1160 | ||
1161 | static inline | |
1162 | void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1163 | { | |
1164 | WARN_ON(!rt_prio(rt_se_prio(rt_se))); | |
1165 | WARN_ON(!rt_rq->rt_nr_running); | |
22abdef3 | 1166 | rt_rq->rt_nr_running -= rt_se_nr_running(rt_se); |
398a153b GH |
1167 | |
1168 | dec_rt_prio(rt_rq, rt_se_prio(rt_se)); | |
1169 | dec_rt_migration(rt_se, rt_rq); | |
1170 | dec_rt_group(rt_se, rt_rq); | |
63489e45 SR |
1171 | } |
1172 | ||
37dad3fc | 1173 | static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head) |
bb44e5d1 | 1174 | { |
6f505b16 PZ |
1175 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
1176 | struct rt_prio_array *array = &rt_rq->active; | |
1177 | struct rt_rq *group_rq = group_rt_rq(rt_se); | |
20b6331b | 1178 | struct list_head *queue = array->queue + rt_se_prio(rt_se); |
bb44e5d1 | 1179 | |
ad2a3f13 PZ |
1180 | /* |
1181 | * Don't enqueue the group if its throttled, or when empty. | |
1182 | * The latter is a consequence of the former when a child group | |
1183 | * get throttled and the current group doesn't have any other | |
1184 | * active members. | |
1185 | */ | |
1186 | if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) | |
6f505b16 | 1187 | return; |
63489e45 | 1188 | |
37dad3fc TG |
1189 | if (head) |
1190 | list_add(&rt_se->run_list, queue); | |
1191 | else | |
1192 | list_add_tail(&rt_se->run_list, queue); | |
6f505b16 | 1193 | __set_bit(rt_se_prio(rt_se), array->bitmap); |
78f2c7db | 1194 | |
6f505b16 PZ |
1195 | inc_rt_tasks(rt_se, rt_rq); |
1196 | } | |
1197 | ||
ad2a3f13 | 1198 | static void __dequeue_rt_entity(struct sched_rt_entity *rt_se) |
6f505b16 PZ |
1199 | { |
1200 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); | |
1201 | struct rt_prio_array *array = &rt_rq->active; | |
1202 | ||
1203 | list_del_init(&rt_se->run_list); | |
1204 | if (list_empty(array->queue + rt_se_prio(rt_se))) | |
1205 | __clear_bit(rt_se_prio(rt_se), array->bitmap); | |
1206 | ||
1207 | dec_rt_tasks(rt_se, rt_rq); | |
1208 | } | |
1209 | ||
1210 | /* | |
1211 | * Because the prio of an upper entry depends on the lower | |
1212 | * entries, we must remove entries top - down. | |
6f505b16 | 1213 | */ |
ad2a3f13 | 1214 | static void dequeue_rt_stack(struct sched_rt_entity *rt_se) |
6f505b16 | 1215 | { |
ad2a3f13 | 1216 | struct sched_rt_entity *back = NULL; |
6f505b16 | 1217 | |
58d6c2d7 PZ |
1218 | for_each_sched_rt_entity(rt_se) { |
1219 | rt_se->back = back; | |
1220 | back = rt_se; | |
1221 | } | |
1222 | ||
f4ebcbc0 KT |
1223 | dequeue_top_rt_rq(rt_rq_of_se(back)); |
1224 | ||
58d6c2d7 PZ |
1225 | for (rt_se = back; rt_se; rt_se = rt_se->back) { |
1226 | if (on_rt_rq(rt_se)) | |
ad2a3f13 PZ |
1227 | __dequeue_rt_entity(rt_se); |
1228 | } | |
1229 | } | |
1230 | ||
37dad3fc | 1231 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head) |
ad2a3f13 | 1232 | { |
f4ebcbc0 KT |
1233 | struct rq *rq = rq_of_rt_se(rt_se); |
1234 | ||
ad2a3f13 PZ |
1235 | dequeue_rt_stack(rt_se); |
1236 | for_each_sched_rt_entity(rt_se) | |
37dad3fc | 1237 | __enqueue_rt_entity(rt_se, head); |
f4ebcbc0 | 1238 | enqueue_top_rt_rq(&rq->rt); |
ad2a3f13 PZ |
1239 | } |
1240 | ||
1241 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se) | |
1242 | { | |
f4ebcbc0 KT |
1243 | struct rq *rq = rq_of_rt_se(rt_se); |
1244 | ||
ad2a3f13 PZ |
1245 | dequeue_rt_stack(rt_se); |
1246 | ||
1247 | for_each_sched_rt_entity(rt_se) { | |
1248 | struct rt_rq *rt_rq = group_rt_rq(rt_se); | |
1249 | ||
1250 | if (rt_rq && rt_rq->rt_nr_running) | |
37dad3fc | 1251 | __enqueue_rt_entity(rt_se, false); |
58d6c2d7 | 1252 | } |
f4ebcbc0 | 1253 | enqueue_top_rt_rq(&rq->rt); |
bb44e5d1 IM |
1254 | } |
1255 | ||
1256 | /* | |
1257 | * Adding/removing a task to/from a priority array: | |
1258 | */ | |
ea87bb78 | 1259 | static void |
371fd7e7 | 1260 | enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags) |
6f505b16 PZ |
1261 | { |
1262 | struct sched_rt_entity *rt_se = &p->rt; | |
1263 | ||
371fd7e7 | 1264 | if (flags & ENQUEUE_WAKEUP) |
6f505b16 PZ |
1265 | rt_se->timeout = 0; |
1266 | ||
371fd7e7 | 1267 | enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD); |
c09595f6 | 1268 | |
29baa747 | 1269 | if (!task_current(rq, p) && p->nr_cpus_allowed > 1) |
917b627d | 1270 | enqueue_pushable_task(rq, p); |
6f505b16 PZ |
1271 | } |
1272 | ||
371fd7e7 | 1273 | static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags) |
bb44e5d1 | 1274 | { |
6f505b16 | 1275 | struct sched_rt_entity *rt_se = &p->rt; |
bb44e5d1 | 1276 | |
f1e14ef6 | 1277 | update_curr_rt(rq); |
ad2a3f13 | 1278 | dequeue_rt_entity(rt_se); |
c09595f6 | 1279 | |
917b627d | 1280 | dequeue_pushable_task(rq, p); |
bb44e5d1 IM |
1281 | } |
1282 | ||
1283 | /* | |
60686317 RW |
1284 | * Put task to the head or the end of the run list without the overhead of |
1285 | * dequeue followed by enqueue. | |
bb44e5d1 | 1286 | */ |
7ebefa8c DA |
1287 | static void |
1288 | requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head) | |
6f505b16 | 1289 | { |
1cdad715 | 1290 | if (on_rt_rq(rt_se)) { |
7ebefa8c DA |
1291 | struct rt_prio_array *array = &rt_rq->active; |
1292 | struct list_head *queue = array->queue + rt_se_prio(rt_se); | |
1293 | ||
1294 | if (head) | |
1295 | list_move(&rt_se->run_list, queue); | |
1296 | else | |
1297 | list_move_tail(&rt_se->run_list, queue); | |
1cdad715 | 1298 | } |
6f505b16 PZ |
1299 | } |
1300 | ||
7ebefa8c | 1301 | static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head) |
bb44e5d1 | 1302 | { |
6f505b16 PZ |
1303 | struct sched_rt_entity *rt_se = &p->rt; |
1304 | struct rt_rq *rt_rq; | |
bb44e5d1 | 1305 | |
6f505b16 PZ |
1306 | for_each_sched_rt_entity(rt_se) { |
1307 | rt_rq = rt_rq_of_se(rt_se); | |
7ebefa8c | 1308 | requeue_rt_entity(rt_rq, rt_se, head); |
6f505b16 | 1309 | } |
bb44e5d1 IM |
1310 | } |
1311 | ||
6f505b16 | 1312 | static void yield_task_rt(struct rq *rq) |
bb44e5d1 | 1313 | { |
7ebefa8c | 1314 | requeue_task_rt(rq, rq->curr, 0); |
bb44e5d1 IM |
1315 | } |
1316 | ||
e7693a36 | 1317 | #ifdef CONFIG_SMP |
318e0893 GH |
1318 | static int find_lowest_rq(struct task_struct *task); |
1319 | ||
0017d735 | 1320 | static int |
ac66f547 | 1321 | select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags) |
e7693a36 | 1322 | { |
7608dec2 PZ |
1323 | struct task_struct *curr; |
1324 | struct rq *rq; | |
c37495fd SR |
1325 | |
1326 | /* For anything but wake ups, just return the task_cpu */ | |
1327 | if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK) | |
1328 | goto out; | |
1329 | ||
7608dec2 PZ |
1330 | rq = cpu_rq(cpu); |
1331 | ||
1332 | rcu_read_lock(); | |
316c1608 | 1333 | curr = READ_ONCE(rq->curr); /* unlocked access */ |
7608dec2 | 1334 | |
318e0893 | 1335 | /* |
7608dec2 | 1336 | * If the current task on @p's runqueue is an RT task, then |
e1f47d89 SR |
1337 | * try to see if we can wake this RT task up on another |
1338 | * runqueue. Otherwise simply start this RT task | |
1339 | * on its current runqueue. | |
1340 | * | |
43fa5460 SR |
1341 | * We want to avoid overloading runqueues. If the woken |
1342 | * task is a higher priority, then it will stay on this CPU | |
1343 | * and the lower prio task should be moved to another CPU. | |
1344 | * Even though this will probably make the lower prio task | |
1345 | * lose its cache, we do not want to bounce a higher task | |
1346 | * around just because it gave up its CPU, perhaps for a | |
1347 | * lock? | |
1348 | * | |
1349 | * For equal prio tasks, we just let the scheduler sort it out. | |
7608dec2 PZ |
1350 | * |
1351 | * Otherwise, just let it ride on the affined RQ and the | |
1352 | * post-schedule router will push the preempted task away | |
1353 | * | |
1354 | * This test is optimistic, if we get it wrong the load-balancer | |
1355 | * will have to sort it out. | |
318e0893 | 1356 | */ |
7608dec2 | 1357 | if (curr && unlikely(rt_task(curr)) && |
29baa747 | 1358 | (curr->nr_cpus_allowed < 2 || |
6bfa687c | 1359 | curr->prio <= p->prio)) { |
7608dec2 | 1360 | int target = find_lowest_rq(p); |
318e0893 | 1361 | |
80e3d87b TC |
1362 | /* |
1363 | * Don't bother moving it if the destination CPU is | |
1364 | * not running a lower priority task. | |
1365 | */ | |
1366 | if (target != -1 && | |
1367 | p->prio < cpu_rq(target)->rt.highest_prio.curr) | |
7608dec2 | 1368 | cpu = target; |
318e0893 | 1369 | } |
7608dec2 | 1370 | rcu_read_unlock(); |
318e0893 | 1371 | |
c37495fd | 1372 | out: |
7608dec2 | 1373 | return cpu; |
e7693a36 | 1374 | } |
7ebefa8c DA |
1375 | |
1376 | static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p) | |
1377 | { | |
308a623a WL |
1378 | /* |
1379 | * Current can't be migrated, useless to reschedule, | |
1380 | * let's hope p can move out. | |
1381 | */ | |
1382 | if (rq->curr->nr_cpus_allowed == 1 || | |
1383 | !cpupri_find(&rq->rd->cpupri, rq->curr, NULL)) | |
7ebefa8c DA |
1384 | return; |
1385 | ||
308a623a WL |
1386 | /* |
1387 | * p is migratable, so let's not schedule it and | |
1388 | * see if it is pushed or pulled somewhere else. | |
1389 | */ | |
29baa747 | 1390 | if (p->nr_cpus_allowed != 1 |
13b8bd0a RR |
1391 | && cpupri_find(&rq->rd->cpupri, p, NULL)) |
1392 | return; | |
24600ce8 | 1393 | |
7ebefa8c DA |
1394 | /* |
1395 | * There appears to be other cpus that can accept | |
1396 | * current and none to run 'p', so lets reschedule | |
1397 | * to try and push current away: | |
1398 | */ | |
1399 | requeue_task_rt(rq, p, 1); | |
8875125e | 1400 | resched_curr(rq); |
7ebefa8c DA |
1401 | } |
1402 | ||
e7693a36 GH |
1403 | #endif /* CONFIG_SMP */ |
1404 | ||
bb44e5d1 IM |
1405 | /* |
1406 | * Preempt the current task with a newly woken task if needed: | |
1407 | */ | |
7d478721 | 1408 | static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags) |
bb44e5d1 | 1409 | { |
45c01e82 | 1410 | if (p->prio < rq->curr->prio) { |
8875125e | 1411 | resched_curr(rq); |
45c01e82 GH |
1412 | return; |
1413 | } | |
1414 | ||
1415 | #ifdef CONFIG_SMP | |
1416 | /* | |
1417 | * If: | |
1418 | * | |
1419 | * - the newly woken task is of equal priority to the current task | |
1420 | * - the newly woken task is non-migratable while current is migratable | |
1421 | * - current will be preempted on the next reschedule | |
1422 | * | |
1423 | * we should check to see if current can readily move to a different | |
1424 | * cpu. If so, we will reschedule to allow the push logic to try | |
1425 | * to move current somewhere else, making room for our non-migratable | |
1426 | * task. | |
1427 | */ | |
8dd0de8b | 1428 | if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr)) |
7ebefa8c | 1429 | check_preempt_equal_prio(rq, p); |
45c01e82 | 1430 | #endif |
bb44e5d1 IM |
1431 | } |
1432 | ||
6f505b16 PZ |
1433 | static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq, |
1434 | struct rt_rq *rt_rq) | |
bb44e5d1 | 1435 | { |
6f505b16 PZ |
1436 | struct rt_prio_array *array = &rt_rq->active; |
1437 | struct sched_rt_entity *next = NULL; | |
bb44e5d1 IM |
1438 | struct list_head *queue; |
1439 | int idx; | |
1440 | ||
1441 | idx = sched_find_first_bit(array->bitmap); | |
6f505b16 | 1442 | BUG_ON(idx >= MAX_RT_PRIO); |
bb44e5d1 IM |
1443 | |
1444 | queue = array->queue + idx; | |
6f505b16 | 1445 | next = list_entry(queue->next, struct sched_rt_entity, run_list); |
326587b8 | 1446 | |
6f505b16 PZ |
1447 | return next; |
1448 | } | |
bb44e5d1 | 1449 | |
917b627d | 1450 | static struct task_struct *_pick_next_task_rt(struct rq *rq) |
6f505b16 PZ |
1451 | { |
1452 | struct sched_rt_entity *rt_se; | |
1453 | struct task_struct *p; | |
606dba2e | 1454 | struct rt_rq *rt_rq = &rq->rt; |
6f505b16 PZ |
1455 | |
1456 | do { | |
1457 | rt_se = pick_next_rt_entity(rq, rt_rq); | |
326587b8 | 1458 | BUG_ON(!rt_se); |
6f505b16 PZ |
1459 | rt_rq = group_rt_rq(rt_se); |
1460 | } while (rt_rq); | |
1461 | ||
1462 | p = rt_task_of(rt_se); | |
78becc27 | 1463 | p->se.exec_start = rq_clock_task(rq); |
917b627d GH |
1464 | |
1465 | return p; | |
1466 | } | |
1467 | ||
606dba2e PZ |
1468 | static struct task_struct * |
1469 | pick_next_task_rt(struct rq *rq, struct task_struct *prev) | |
917b627d | 1470 | { |
606dba2e PZ |
1471 | struct task_struct *p; |
1472 | struct rt_rq *rt_rq = &rq->rt; | |
1473 | ||
37e117c0 | 1474 | if (need_pull_rt_task(rq, prev)) { |
38033c37 | 1475 | pull_rt_task(rq); |
37e117c0 PZ |
1476 | /* |
1477 | * pull_rt_task() can drop (and re-acquire) rq->lock; this | |
a1d9a323 KT |
1478 | * means a dl or stop task can slip in, in which case we need |
1479 | * to re-start task selection. | |
37e117c0 | 1480 | */ |
da0c1e65 | 1481 | if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) || |
a1d9a323 | 1482 | rq->dl.dl_nr_running)) |
37e117c0 PZ |
1483 | return RETRY_TASK; |
1484 | } | |
38033c37 | 1485 | |
734ff2a7 KT |
1486 | /* |
1487 | * We may dequeue prev's rt_rq in put_prev_task(). | |
1488 | * So, we update time before rt_nr_running check. | |
1489 | */ | |
1490 | if (prev->sched_class == &rt_sched_class) | |
1491 | update_curr_rt(rq); | |
1492 | ||
f4ebcbc0 | 1493 | if (!rt_rq->rt_queued) |
606dba2e PZ |
1494 | return NULL; |
1495 | ||
3f1d2a31 | 1496 | put_prev_task(rq, prev); |
606dba2e PZ |
1497 | |
1498 | p = _pick_next_task_rt(rq); | |
917b627d GH |
1499 | |
1500 | /* The running task is never eligible for pushing */ | |
f3f1768f | 1501 | dequeue_pushable_task(rq, p); |
917b627d | 1502 | |
e3fca9e7 | 1503 | queue_push_tasks(rq); |
3f029d3c | 1504 | |
6f505b16 | 1505 | return p; |
bb44e5d1 IM |
1506 | } |
1507 | ||
31ee529c | 1508 | static void put_prev_task_rt(struct rq *rq, struct task_struct *p) |
bb44e5d1 | 1509 | { |
f1e14ef6 | 1510 | update_curr_rt(rq); |
917b627d GH |
1511 | |
1512 | /* | |
1513 | * The previous task needs to be made eligible for pushing | |
1514 | * if it is still active | |
1515 | */ | |
29baa747 | 1516 | if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1) |
917b627d | 1517 | enqueue_pushable_task(rq, p); |
bb44e5d1 IM |
1518 | } |
1519 | ||
681f3e68 | 1520 | #ifdef CONFIG_SMP |
6f505b16 | 1521 | |
e8fa1362 SR |
1522 | /* Only try algorithms three times */ |
1523 | #define RT_MAX_TRIES 3 | |
1524 | ||
f65eda4f SR |
1525 | static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu) |
1526 | { | |
1527 | if (!task_running(rq, p) && | |
60334caf | 1528 | cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) |
f65eda4f SR |
1529 | return 1; |
1530 | return 0; | |
1531 | } | |
1532 | ||
e23ee747 KT |
1533 | /* |
1534 | * Return the highest pushable rq's task, which is suitable to be executed | |
1535 | * on the cpu, NULL otherwise | |
1536 | */ | |
1537 | static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu) | |
e8fa1362 | 1538 | { |
e23ee747 KT |
1539 | struct plist_head *head = &rq->rt.pushable_tasks; |
1540 | struct task_struct *p; | |
3d07467b | 1541 | |
e23ee747 KT |
1542 | if (!has_pushable_tasks(rq)) |
1543 | return NULL; | |
3d07467b | 1544 | |
e23ee747 KT |
1545 | plist_for_each_entry(p, head, pushable_tasks) { |
1546 | if (pick_rt_task(rq, p, cpu)) | |
1547 | return p; | |
f65eda4f SR |
1548 | } |
1549 | ||
e23ee747 | 1550 | return NULL; |
e8fa1362 SR |
1551 | } |
1552 | ||
0e3900e6 | 1553 | static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask); |
e8fa1362 | 1554 | |
6e1254d2 GH |
1555 | static int find_lowest_rq(struct task_struct *task) |
1556 | { | |
1557 | struct sched_domain *sd; | |
4ba29684 | 1558 | struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask); |
6e1254d2 GH |
1559 | int this_cpu = smp_processor_id(); |
1560 | int cpu = task_cpu(task); | |
06f90dbd | 1561 | |
0da938c4 SR |
1562 | /* Make sure the mask is initialized first */ |
1563 | if (unlikely(!lowest_mask)) | |
1564 | return -1; | |
1565 | ||
29baa747 | 1566 | if (task->nr_cpus_allowed == 1) |
6e0534f2 | 1567 | return -1; /* No other targets possible */ |
6e1254d2 | 1568 | |
6e0534f2 GH |
1569 | if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask)) |
1570 | return -1; /* No targets found */ | |
6e1254d2 GH |
1571 | |
1572 | /* | |
1573 | * At this point we have built a mask of cpus representing the | |
1574 | * lowest priority tasks in the system. Now we want to elect | |
1575 | * the best one based on our affinity and topology. | |
1576 | * | |
1577 | * We prioritize the last cpu that the task executed on since | |
1578 | * it is most likely cache-hot in that location. | |
1579 | */ | |
96f874e2 | 1580 | if (cpumask_test_cpu(cpu, lowest_mask)) |
6e1254d2 GH |
1581 | return cpu; |
1582 | ||
1583 | /* | |
1584 | * Otherwise, we consult the sched_domains span maps to figure | |
1585 | * out which cpu is logically closest to our hot cache data. | |
1586 | */ | |
e2c88063 RR |
1587 | if (!cpumask_test_cpu(this_cpu, lowest_mask)) |
1588 | this_cpu = -1; /* Skip this_cpu opt if not among lowest */ | |
6e1254d2 | 1589 | |
cd4ae6ad | 1590 | rcu_read_lock(); |
e2c88063 RR |
1591 | for_each_domain(cpu, sd) { |
1592 | if (sd->flags & SD_WAKE_AFFINE) { | |
1593 | int best_cpu; | |
6e1254d2 | 1594 | |
e2c88063 RR |
1595 | /* |
1596 | * "this_cpu" is cheaper to preempt than a | |
1597 | * remote processor. | |
1598 | */ | |
1599 | if (this_cpu != -1 && | |
cd4ae6ad XF |
1600 | cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { |
1601 | rcu_read_unlock(); | |
e2c88063 | 1602 | return this_cpu; |
cd4ae6ad | 1603 | } |
e2c88063 RR |
1604 | |
1605 | best_cpu = cpumask_first_and(lowest_mask, | |
1606 | sched_domain_span(sd)); | |
cd4ae6ad XF |
1607 | if (best_cpu < nr_cpu_ids) { |
1608 | rcu_read_unlock(); | |
e2c88063 | 1609 | return best_cpu; |
cd4ae6ad | 1610 | } |
6e1254d2 GH |
1611 | } |
1612 | } | |
cd4ae6ad | 1613 | rcu_read_unlock(); |
6e1254d2 GH |
1614 | |
1615 | /* | |
1616 | * And finally, if there were no matches within the domains | |
1617 | * just give the caller *something* to work with from the compatible | |
1618 | * locations. | |
1619 | */ | |
e2c88063 RR |
1620 | if (this_cpu != -1) |
1621 | return this_cpu; | |
1622 | ||
1623 | cpu = cpumask_any(lowest_mask); | |
1624 | if (cpu < nr_cpu_ids) | |
1625 | return cpu; | |
1626 | return -1; | |
07b4032c GH |
1627 | } |
1628 | ||
1629 | /* Will lock the rq it finds */ | |
4df64c0b | 1630 | static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) |
07b4032c GH |
1631 | { |
1632 | struct rq *lowest_rq = NULL; | |
07b4032c | 1633 | int tries; |
4df64c0b | 1634 | int cpu; |
e8fa1362 | 1635 | |
07b4032c GH |
1636 | for (tries = 0; tries < RT_MAX_TRIES; tries++) { |
1637 | cpu = find_lowest_rq(task); | |
1638 | ||
2de0b463 | 1639 | if ((cpu == -1) || (cpu == rq->cpu)) |
e8fa1362 SR |
1640 | break; |
1641 | ||
07b4032c GH |
1642 | lowest_rq = cpu_rq(cpu); |
1643 | ||
80e3d87b TC |
1644 | if (lowest_rq->rt.highest_prio.curr <= task->prio) { |
1645 | /* | |
1646 | * Target rq has tasks of equal or higher priority, | |
1647 | * retrying does not release any lock and is unlikely | |
1648 | * to yield a different result. | |
1649 | */ | |
1650 | lowest_rq = NULL; | |
1651 | break; | |
1652 | } | |
1653 | ||
e8fa1362 | 1654 | /* if the prio of this runqueue changed, try again */ |
07b4032c | 1655 | if (double_lock_balance(rq, lowest_rq)) { |
e8fa1362 SR |
1656 | /* |
1657 | * We had to unlock the run queue. In | |
1658 | * the mean time, task could have | |
1659 | * migrated already or had its affinity changed. | |
1660 | * Also make sure that it wasn't scheduled on its rq. | |
1661 | */ | |
07b4032c | 1662 | if (unlikely(task_rq(task) != rq || |
96f874e2 | 1663 | !cpumask_test_cpu(lowest_rq->cpu, |
fa17b507 | 1664 | tsk_cpus_allowed(task)) || |
07b4032c | 1665 | task_running(rq, task) || |
da0c1e65 | 1666 | !task_on_rq_queued(task))) { |
4df64c0b | 1667 | |
7f1b4393 | 1668 | double_unlock_balance(rq, lowest_rq); |
e8fa1362 SR |
1669 | lowest_rq = NULL; |
1670 | break; | |
1671 | } | |
1672 | } | |
1673 | ||
1674 | /* If this rq is still suitable use it. */ | |
e864c499 | 1675 | if (lowest_rq->rt.highest_prio.curr > task->prio) |
e8fa1362 SR |
1676 | break; |
1677 | ||
1678 | /* try again */ | |
1b12bbc7 | 1679 | double_unlock_balance(rq, lowest_rq); |
e8fa1362 SR |
1680 | lowest_rq = NULL; |
1681 | } | |
1682 | ||
1683 | return lowest_rq; | |
1684 | } | |
1685 | ||
917b627d GH |
1686 | static struct task_struct *pick_next_pushable_task(struct rq *rq) |
1687 | { | |
1688 | struct task_struct *p; | |
1689 | ||
1690 | if (!has_pushable_tasks(rq)) | |
1691 | return NULL; | |
1692 | ||
1693 | p = plist_first_entry(&rq->rt.pushable_tasks, | |
1694 | struct task_struct, pushable_tasks); | |
1695 | ||
1696 | BUG_ON(rq->cpu != task_cpu(p)); | |
1697 | BUG_ON(task_current(rq, p)); | |
29baa747 | 1698 | BUG_ON(p->nr_cpus_allowed <= 1); |
917b627d | 1699 | |
da0c1e65 | 1700 | BUG_ON(!task_on_rq_queued(p)); |
917b627d GH |
1701 | BUG_ON(!rt_task(p)); |
1702 | ||
1703 | return p; | |
1704 | } | |
1705 | ||
e8fa1362 SR |
1706 | /* |
1707 | * If the current CPU has more than one RT task, see if the non | |
1708 | * running task can migrate over to a CPU that is running a task | |
1709 | * of lesser priority. | |
1710 | */ | |
697f0a48 | 1711 | static int push_rt_task(struct rq *rq) |
e8fa1362 SR |
1712 | { |
1713 | struct task_struct *next_task; | |
1714 | struct rq *lowest_rq; | |
311e800e | 1715 | int ret = 0; |
e8fa1362 | 1716 | |
a22d7fc1 GH |
1717 | if (!rq->rt.overloaded) |
1718 | return 0; | |
1719 | ||
917b627d | 1720 | next_task = pick_next_pushable_task(rq); |
e8fa1362 SR |
1721 | if (!next_task) |
1722 | return 0; | |
1723 | ||
49246274 | 1724 | retry: |
697f0a48 | 1725 | if (unlikely(next_task == rq->curr)) { |
f65eda4f | 1726 | WARN_ON(1); |
e8fa1362 | 1727 | return 0; |
f65eda4f | 1728 | } |
e8fa1362 SR |
1729 | |
1730 | /* | |
1731 | * It's possible that the next_task slipped in of | |
1732 | * higher priority than current. If that's the case | |
1733 | * just reschedule current. | |
1734 | */ | |
697f0a48 | 1735 | if (unlikely(next_task->prio < rq->curr->prio)) { |
8875125e | 1736 | resched_curr(rq); |
e8fa1362 SR |
1737 | return 0; |
1738 | } | |
1739 | ||
697f0a48 | 1740 | /* We might release rq lock */ |
e8fa1362 SR |
1741 | get_task_struct(next_task); |
1742 | ||
1743 | /* find_lock_lowest_rq locks the rq if found */ | |
697f0a48 | 1744 | lowest_rq = find_lock_lowest_rq(next_task, rq); |
e8fa1362 SR |
1745 | if (!lowest_rq) { |
1746 | struct task_struct *task; | |
1747 | /* | |
311e800e | 1748 | * find_lock_lowest_rq releases rq->lock |
1563513d GH |
1749 | * so it is possible that next_task has migrated. |
1750 | * | |
1751 | * We need to make sure that the task is still on the same | |
1752 | * run-queue and is also still the next task eligible for | |
1753 | * pushing. | |
e8fa1362 | 1754 | */ |
917b627d | 1755 | task = pick_next_pushable_task(rq); |
1563513d GH |
1756 | if (task_cpu(next_task) == rq->cpu && task == next_task) { |
1757 | /* | |
311e800e HD |
1758 | * The task hasn't migrated, and is still the next |
1759 | * eligible task, but we failed to find a run-queue | |
1760 | * to push it to. Do not retry in this case, since | |
1761 | * other cpus will pull from us when ready. | |
1563513d | 1762 | */ |
1563513d | 1763 | goto out; |
e8fa1362 | 1764 | } |
917b627d | 1765 | |
1563513d GH |
1766 | if (!task) |
1767 | /* No more tasks, just exit */ | |
1768 | goto out; | |
1769 | ||
917b627d | 1770 | /* |
1563513d | 1771 | * Something has shifted, try again. |
917b627d | 1772 | */ |
1563513d GH |
1773 | put_task_struct(next_task); |
1774 | next_task = task; | |
1775 | goto retry; | |
e8fa1362 SR |
1776 | } |
1777 | ||
697f0a48 | 1778 | deactivate_task(rq, next_task, 0); |
e8fa1362 SR |
1779 | set_task_cpu(next_task, lowest_rq->cpu); |
1780 | activate_task(lowest_rq, next_task, 0); | |
311e800e | 1781 | ret = 1; |
e8fa1362 | 1782 | |
8875125e | 1783 | resched_curr(lowest_rq); |
e8fa1362 | 1784 | |
1b12bbc7 | 1785 | double_unlock_balance(rq, lowest_rq); |
e8fa1362 | 1786 | |
e8fa1362 SR |
1787 | out: |
1788 | put_task_struct(next_task); | |
1789 | ||
311e800e | 1790 | return ret; |
e8fa1362 SR |
1791 | } |
1792 | ||
e8fa1362 SR |
1793 | static void push_rt_tasks(struct rq *rq) |
1794 | { | |
1795 | /* push_rt_task will return true if it moved an RT */ | |
1796 | while (push_rt_task(rq)) | |
1797 | ; | |
1798 | } | |
1799 | ||
b6366f04 SR |
1800 | #ifdef HAVE_RT_PUSH_IPI |
1801 | /* | |
1802 | * The search for the next cpu always starts at rq->cpu and ends | |
1803 | * when we reach rq->cpu again. It will never return rq->cpu. | |
1804 | * This returns the next cpu to check, or nr_cpu_ids if the loop | |
1805 | * is complete. | |
1806 | * | |
1807 | * rq->rt.push_cpu holds the last cpu returned by this function, | |
1808 | * or if this is the first instance, it must hold rq->cpu. | |
1809 | */ | |
1810 | static int rto_next_cpu(struct rq *rq) | |
1811 | { | |
1812 | int prev_cpu = rq->rt.push_cpu; | |
1813 | int cpu; | |
1814 | ||
1815 | cpu = cpumask_next(prev_cpu, rq->rd->rto_mask); | |
1816 | ||
1817 | /* | |
1818 | * If the previous cpu is less than the rq's CPU, then it already | |
1819 | * passed the end of the mask, and has started from the beginning. | |
1820 | * We end if the next CPU is greater or equal to rq's CPU. | |
1821 | */ | |
1822 | if (prev_cpu < rq->cpu) { | |
1823 | if (cpu >= rq->cpu) | |
1824 | return nr_cpu_ids; | |
1825 | ||
1826 | } else if (cpu >= nr_cpu_ids) { | |
1827 | /* | |
1828 | * We passed the end of the mask, start at the beginning. | |
1829 | * If the result is greater or equal to the rq's CPU, then | |
1830 | * the loop is finished. | |
1831 | */ | |
1832 | cpu = cpumask_first(rq->rd->rto_mask); | |
1833 | if (cpu >= rq->cpu) | |
1834 | return nr_cpu_ids; | |
1835 | } | |
1836 | rq->rt.push_cpu = cpu; | |
1837 | ||
1838 | /* Return cpu to let the caller know if the loop is finished or not */ | |
1839 | return cpu; | |
1840 | } | |
1841 | ||
1842 | static int find_next_push_cpu(struct rq *rq) | |
1843 | { | |
1844 | struct rq *next_rq; | |
1845 | int cpu; | |
1846 | ||
1847 | while (1) { | |
1848 | cpu = rto_next_cpu(rq); | |
1849 | if (cpu >= nr_cpu_ids) | |
1850 | break; | |
1851 | next_rq = cpu_rq(cpu); | |
1852 | ||
1853 | /* Make sure the next rq can push to this rq */ | |
1854 | if (next_rq->rt.highest_prio.next < rq->rt.highest_prio.curr) | |
1855 | break; | |
1856 | } | |
1857 | ||
1858 | return cpu; | |
1859 | } | |
1860 | ||
1861 | #define RT_PUSH_IPI_EXECUTING 1 | |
1862 | #define RT_PUSH_IPI_RESTART 2 | |
1863 | ||
1864 | static void tell_cpu_to_push(struct rq *rq) | |
1865 | { | |
1866 | int cpu; | |
1867 | ||
1868 | if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) { | |
1869 | raw_spin_lock(&rq->rt.push_lock); | |
1870 | /* Make sure it's still executing */ | |
1871 | if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) { | |
1872 | /* | |
1873 | * Tell the IPI to restart the loop as things have | |
1874 | * changed since it started. | |
1875 | */ | |
1876 | rq->rt.push_flags |= RT_PUSH_IPI_RESTART; | |
1877 | raw_spin_unlock(&rq->rt.push_lock); | |
1878 | return; | |
1879 | } | |
1880 | raw_spin_unlock(&rq->rt.push_lock); | |
1881 | } | |
1882 | ||
1883 | /* When here, there's no IPI going around */ | |
1884 | ||
1885 | rq->rt.push_cpu = rq->cpu; | |
1886 | cpu = find_next_push_cpu(rq); | |
1887 | if (cpu >= nr_cpu_ids) | |
1888 | return; | |
1889 | ||
1890 | rq->rt.push_flags = RT_PUSH_IPI_EXECUTING; | |
1891 | ||
1892 | irq_work_queue_on(&rq->rt.push_work, cpu); | |
1893 | } | |
1894 | ||
1895 | /* Called from hardirq context */ | |
1896 | static void try_to_push_tasks(void *arg) | |
1897 | { | |
1898 | struct rt_rq *rt_rq = arg; | |
1899 | struct rq *rq, *src_rq; | |
1900 | int this_cpu; | |
1901 | int cpu; | |
1902 | ||
1903 | this_cpu = rt_rq->push_cpu; | |
1904 | ||
1905 | /* Paranoid check */ | |
1906 | BUG_ON(this_cpu != smp_processor_id()); | |
1907 | ||
1908 | rq = cpu_rq(this_cpu); | |
1909 | src_rq = rq_of_rt_rq(rt_rq); | |
1910 | ||
1911 | again: | |
1912 | if (has_pushable_tasks(rq)) { | |
1913 | raw_spin_lock(&rq->lock); | |
1914 | push_rt_task(rq); | |
1915 | raw_spin_unlock(&rq->lock); | |
1916 | } | |
1917 | ||
1918 | /* Pass the IPI to the next rt overloaded queue */ | |
1919 | raw_spin_lock(&rt_rq->push_lock); | |
1920 | /* | |
1921 | * If the source queue changed since the IPI went out, | |
1922 | * we need to restart the search from that CPU again. | |
1923 | */ | |
1924 | if (rt_rq->push_flags & RT_PUSH_IPI_RESTART) { | |
1925 | rt_rq->push_flags &= ~RT_PUSH_IPI_RESTART; | |
1926 | rt_rq->push_cpu = src_rq->cpu; | |
1927 | } | |
1928 | ||
1929 | cpu = find_next_push_cpu(src_rq); | |
1930 | ||
1931 | if (cpu >= nr_cpu_ids) | |
1932 | rt_rq->push_flags &= ~RT_PUSH_IPI_EXECUTING; | |
1933 | raw_spin_unlock(&rt_rq->push_lock); | |
1934 | ||
1935 | if (cpu >= nr_cpu_ids) | |
1936 | return; | |
1937 | ||
1938 | /* | |
1939 | * It is possible that a restart caused this CPU to be | |
1940 | * chosen again. Don't bother with an IPI, just see if we | |
1941 | * have more to push. | |
1942 | */ | |
1943 | if (unlikely(cpu == rq->cpu)) | |
1944 | goto again; | |
1945 | ||
1946 | /* Try the next RT overloaded CPU */ | |
1947 | irq_work_queue_on(&rt_rq->push_work, cpu); | |
1948 | } | |
1949 | ||
1950 | static void push_irq_work_func(struct irq_work *work) | |
1951 | { | |
1952 | struct rt_rq *rt_rq = container_of(work, struct rt_rq, push_work); | |
1953 | ||
1954 | try_to_push_tasks(rt_rq); | |
1955 | } | |
1956 | #endif /* HAVE_RT_PUSH_IPI */ | |
1957 | ||
f65eda4f SR |
1958 | static int pull_rt_task(struct rq *this_rq) |
1959 | { | |
80bf3171 | 1960 | int this_cpu = this_rq->cpu, ret = 0, cpu; |
a8728944 | 1961 | struct task_struct *p; |
f65eda4f | 1962 | struct rq *src_rq; |
f65eda4f | 1963 | |
637f5085 | 1964 | if (likely(!rt_overloaded(this_rq))) |
f65eda4f SR |
1965 | return 0; |
1966 | ||
7c3f2ab7 PZ |
1967 | /* |
1968 | * Match the barrier from rt_set_overloaded; this guarantees that if we | |
1969 | * see overloaded we must also see the rto_mask bit. | |
1970 | */ | |
1971 | smp_rmb(); | |
1972 | ||
b6366f04 SR |
1973 | #ifdef HAVE_RT_PUSH_IPI |
1974 | if (sched_feat(RT_PUSH_IPI)) { | |
1975 | tell_cpu_to_push(this_rq); | |
1976 | return 0; | |
1977 | } | |
1978 | #endif | |
1979 | ||
c6c4927b | 1980 | for_each_cpu(cpu, this_rq->rd->rto_mask) { |
f65eda4f SR |
1981 | if (this_cpu == cpu) |
1982 | continue; | |
1983 | ||
1984 | src_rq = cpu_rq(cpu); | |
74ab8e4f GH |
1985 | |
1986 | /* | |
1987 | * Don't bother taking the src_rq->lock if the next highest | |
1988 | * task is known to be lower-priority than our current task. | |
1989 | * This may look racy, but if this value is about to go | |
1990 | * logically higher, the src_rq will push this task away. | |
1991 | * And if its going logically lower, we do not care | |
1992 | */ | |
1993 | if (src_rq->rt.highest_prio.next >= | |
1994 | this_rq->rt.highest_prio.curr) | |
1995 | continue; | |
1996 | ||
f65eda4f SR |
1997 | /* |
1998 | * We can potentially drop this_rq's lock in | |
1999 | * double_lock_balance, and another CPU could | |
a8728944 | 2000 | * alter this_rq |
f65eda4f | 2001 | */ |
a8728944 | 2002 | double_lock_balance(this_rq, src_rq); |
f65eda4f SR |
2003 | |
2004 | /* | |
e23ee747 KT |
2005 | * We can pull only a task, which is pushable |
2006 | * on its rq, and no others. | |
f65eda4f | 2007 | */ |
e23ee747 | 2008 | p = pick_highest_pushable_task(src_rq, this_cpu); |
f65eda4f SR |
2009 | |
2010 | /* | |
2011 | * Do we have an RT task that preempts | |
2012 | * the to-be-scheduled task? | |
2013 | */ | |
a8728944 | 2014 | if (p && (p->prio < this_rq->rt.highest_prio.curr)) { |
f65eda4f | 2015 | WARN_ON(p == src_rq->curr); |
da0c1e65 | 2016 | WARN_ON(!task_on_rq_queued(p)); |
f65eda4f SR |
2017 | |
2018 | /* | |
2019 | * There's a chance that p is higher in priority | |
2020 | * than what's currently running on its cpu. | |
2021 | * This is just that p is wakeing up and hasn't | |
2022 | * had a chance to schedule. We only pull | |
2023 | * p if it is lower in priority than the | |
a8728944 | 2024 | * current task on the run queue |
f65eda4f | 2025 | */ |
a8728944 | 2026 | if (p->prio < src_rq->curr->prio) |
614ee1f6 | 2027 | goto skip; |
f65eda4f SR |
2028 | |
2029 | ret = 1; | |
2030 | ||
2031 | deactivate_task(src_rq, p, 0); | |
2032 | set_task_cpu(p, this_cpu); | |
2033 | activate_task(this_rq, p, 0); | |
2034 | /* | |
2035 | * We continue with the search, just in | |
2036 | * case there's an even higher prio task | |
25985edc | 2037 | * in another runqueue. (low likelihood |
f65eda4f | 2038 | * but possible) |
f65eda4f | 2039 | */ |
f65eda4f | 2040 | } |
49246274 | 2041 | skip: |
1b12bbc7 | 2042 | double_unlock_balance(this_rq, src_rq); |
f65eda4f SR |
2043 | } |
2044 | ||
2045 | return ret; | |
2046 | } | |
2047 | ||
8ae121ac GH |
2048 | /* |
2049 | * If we are not running and we are not going to reschedule soon, we should | |
2050 | * try to push tasks away now | |
2051 | */ | |
efbbd05a | 2052 | static void task_woken_rt(struct rq *rq, struct task_struct *p) |
4642dafd | 2053 | { |
9a897c5a | 2054 | if (!task_running(rq, p) && |
8ae121ac | 2055 | !test_tsk_need_resched(rq->curr) && |
917b627d | 2056 | has_pushable_tasks(rq) && |
29baa747 | 2057 | p->nr_cpus_allowed > 1 && |
1baca4ce | 2058 | (dl_task(rq->curr) || rt_task(rq->curr)) && |
29baa747 | 2059 | (rq->curr->nr_cpus_allowed < 2 || |
3be209a8 | 2060 | rq->curr->prio <= p->prio)) |
4642dafd SR |
2061 | push_rt_tasks(rq); |
2062 | } | |
2063 | ||
cd8ba7cd | 2064 | static void set_cpus_allowed_rt(struct task_struct *p, |
96f874e2 | 2065 | const struct cpumask *new_mask) |
73fe6aae | 2066 | { |
8d3d5ada KT |
2067 | struct rq *rq; |
2068 | int weight; | |
73fe6aae GH |
2069 | |
2070 | BUG_ON(!rt_task(p)); | |
2071 | ||
da0c1e65 | 2072 | if (!task_on_rq_queued(p)) |
8d3d5ada | 2073 | return; |
917b627d | 2074 | |
8d3d5ada | 2075 | weight = cpumask_weight(new_mask); |
917b627d | 2076 | |
8d3d5ada KT |
2077 | /* |
2078 | * Only update if the process changes its state from whether it | |
2079 | * can migrate or not. | |
2080 | */ | |
29baa747 | 2081 | if ((p->nr_cpus_allowed > 1) == (weight > 1)) |
8d3d5ada | 2082 | return; |
917b627d | 2083 | |
8d3d5ada | 2084 | rq = task_rq(p); |
73fe6aae | 2085 | |
8d3d5ada KT |
2086 | /* |
2087 | * The process used to be able to migrate OR it can now migrate | |
2088 | */ | |
2089 | if (weight <= 1) { | |
2090 | if (!task_current(rq, p)) | |
2091 | dequeue_pushable_task(rq, p); | |
2092 | BUG_ON(!rq->rt.rt_nr_migratory); | |
2093 | rq->rt.rt_nr_migratory--; | |
2094 | } else { | |
2095 | if (!task_current(rq, p)) | |
2096 | enqueue_pushable_task(rq, p); | |
2097 | rq->rt.rt_nr_migratory++; | |
73fe6aae | 2098 | } |
8d3d5ada KT |
2099 | |
2100 | update_rt_migration(&rq->rt); | |
73fe6aae | 2101 | } |
deeeccd4 | 2102 | |
bdd7c81b | 2103 | /* Assumes rq->lock is held */ |
1f11eb6a | 2104 | static void rq_online_rt(struct rq *rq) |
bdd7c81b IM |
2105 | { |
2106 | if (rq->rt.overloaded) | |
2107 | rt_set_overload(rq); | |
6e0534f2 | 2108 | |
7def2be1 PZ |
2109 | __enable_runtime(rq); |
2110 | ||
e864c499 | 2111 | cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr); |
bdd7c81b IM |
2112 | } |
2113 | ||
2114 | /* Assumes rq->lock is held */ | |
1f11eb6a | 2115 | static void rq_offline_rt(struct rq *rq) |
bdd7c81b IM |
2116 | { |
2117 | if (rq->rt.overloaded) | |
2118 | rt_clear_overload(rq); | |
6e0534f2 | 2119 | |
7def2be1 PZ |
2120 | __disable_runtime(rq); |
2121 | ||
6e0534f2 | 2122 | cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID); |
bdd7c81b | 2123 | } |
cb469845 SR |
2124 | |
2125 | /* | |
2126 | * When switch from the rt queue, we bring ourselves to a position | |
2127 | * that we might want to pull RT tasks from other runqueues. | |
2128 | */ | |
da7a735e | 2129 | static void switched_from_rt(struct rq *rq, struct task_struct *p) |
cb469845 SR |
2130 | { |
2131 | /* | |
2132 | * If there are other RT tasks then we will reschedule | |
2133 | * and the scheduling of the other RT tasks will handle | |
2134 | * the balancing. But if we are the last RT task | |
2135 | * we may need to handle the pulling of RT tasks | |
2136 | * now. | |
2137 | */ | |
da0c1e65 | 2138 | if (!task_on_rq_queued(p) || rq->rt.rt_nr_running) |
1158ddb5 KT |
2139 | return; |
2140 | ||
2141 | if (pull_rt_task(rq)) | |
8875125e | 2142 | resched_curr(rq); |
cb469845 | 2143 | } |
3d8cbdf8 | 2144 | |
11c785b7 | 2145 | void __init init_sched_rt_class(void) |
3d8cbdf8 RR |
2146 | { |
2147 | unsigned int i; | |
2148 | ||
029632fb | 2149 | for_each_possible_cpu(i) { |
eaa95840 | 2150 | zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i), |
6ca09dfc | 2151 | GFP_KERNEL, cpu_to_node(i)); |
029632fb | 2152 | } |
3d8cbdf8 | 2153 | } |
cb469845 SR |
2154 | #endif /* CONFIG_SMP */ |
2155 | ||
2156 | /* | |
2157 | * When switching a task to RT, we may overload the runqueue | |
2158 | * with RT tasks. In this case we try to push them off to | |
2159 | * other runqueues. | |
2160 | */ | |
da7a735e | 2161 | static void switched_to_rt(struct rq *rq, struct task_struct *p) |
cb469845 SR |
2162 | { |
2163 | int check_resched = 1; | |
2164 | ||
2165 | /* | |
2166 | * If we are already running, then there's nothing | |
2167 | * that needs to be done. But if we are not running | |
2168 | * we may need to preempt the current running task. | |
2169 | * If that current running task is also an RT task | |
2170 | * then see if we can move to another run queue. | |
2171 | */ | |
da0c1e65 | 2172 | if (task_on_rq_queued(p) && rq->curr != p) { |
cb469845 | 2173 | #ifdef CONFIG_SMP |
10447917 | 2174 | if (p->nr_cpus_allowed > 1 && rq->rt.overloaded && |
cb469845 | 2175 | /* Don't resched if we changed runqueues */ |
10447917 | 2176 | push_rt_task(rq) && rq != task_rq(p)) |
cb469845 SR |
2177 | check_resched = 0; |
2178 | #endif /* CONFIG_SMP */ | |
2179 | if (check_resched && p->prio < rq->curr->prio) | |
8875125e | 2180 | resched_curr(rq); |
cb469845 SR |
2181 | } |
2182 | } | |
2183 | ||
2184 | /* | |
2185 | * Priority of the task has changed. This may cause | |
2186 | * us to initiate a push or pull. | |
2187 | */ | |
da7a735e PZ |
2188 | static void |
2189 | prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 2190 | { |
da0c1e65 | 2191 | if (!task_on_rq_queued(p)) |
da7a735e PZ |
2192 | return; |
2193 | ||
2194 | if (rq->curr == p) { | |
cb469845 SR |
2195 | #ifdef CONFIG_SMP |
2196 | /* | |
2197 | * If our priority decreases while running, we | |
2198 | * may need to pull tasks to this runqueue. | |
2199 | */ | |
2200 | if (oldprio < p->prio) | |
2201 | pull_rt_task(rq); | |
2202 | /* | |
2203 | * If there's a higher priority task waiting to run | |
6fa46fa5 SR |
2204 | * then reschedule. Note, the above pull_rt_task |
2205 | * can release the rq lock and p could migrate. | |
2206 | * Only reschedule if p is still on the same runqueue. | |
cb469845 | 2207 | */ |
e864c499 | 2208 | if (p->prio > rq->rt.highest_prio.curr && rq->curr == p) |
8875125e | 2209 | resched_curr(rq); |
cb469845 SR |
2210 | #else |
2211 | /* For UP simply resched on drop of prio */ | |
2212 | if (oldprio < p->prio) | |
8875125e | 2213 | resched_curr(rq); |
e8fa1362 | 2214 | #endif /* CONFIG_SMP */ |
cb469845 SR |
2215 | } else { |
2216 | /* | |
2217 | * This task is not running, but if it is | |
2218 | * greater than the current running task | |
2219 | * then reschedule. | |
2220 | */ | |
2221 | if (p->prio < rq->curr->prio) | |
8875125e | 2222 | resched_curr(rq); |
cb469845 SR |
2223 | } |
2224 | } | |
2225 | ||
78f2c7db PZ |
2226 | static void watchdog(struct rq *rq, struct task_struct *p) |
2227 | { | |
2228 | unsigned long soft, hard; | |
2229 | ||
78d7d407 JS |
2230 | /* max may change after cur was read, this will be fixed next tick */ |
2231 | soft = task_rlimit(p, RLIMIT_RTTIME); | |
2232 | hard = task_rlimit_max(p, RLIMIT_RTTIME); | |
78f2c7db PZ |
2233 | |
2234 | if (soft != RLIM_INFINITY) { | |
2235 | unsigned long next; | |
2236 | ||
57d2aa00 YX |
2237 | if (p->rt.watchdog_stamp != jiffies) { |
2238 | p->rt.timeout++; | |
2239 | p->rt.watchdog_stamp = jiffies; | |
2240 | } | |
2241 | ||
78f2c7db | 2242 | next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ); |
5a52dd50 | 2243 | if (p->rt.timeout > next) |
f06febc9 | 2244 | p->cputime_expires.sched_exp = p->se.sum_exec_runtime; |
78f2c7db PZ |
2245 | } |
2246 | } | |
bb44e5d1 | 2247 | |
8f4d37ec | 2248 | static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued) |
bb44e5d1 | 2249 | { |
454c7999 CC |
2250 | struct sched_rt_entity *rt_se = &p->rt; |
2251 | ||
67e2be02 PZ |
2252 | update_curr_rt(rq); |
2253 | ||
78f2c7db PZ |
2254 | watchdog(rq, p); |
2255 | ||
bb44e5d1 IM |
2256 | /* |
2257 | * RR tasks need a special form of timeslice management. | |
2258 | * FIFO tasks have no timeslices. | |
2259 | */ | |
2260 | if (p->policy != SCHED_RR) | |
2261 | return; | |
2262 | ||
fa717060 | 2263 | if (--p->rt.time_slice) |
bb44e5d1 IM |
2264 | return; |
2265 | ||
ce0dbbbb | 2266 | p->rt.time_slice = sched_rr_timeslice; |
bb44e5d1 | 2267 | |
98fbc798 | 2268 | /* |
e9aa39bb LB |
2269 | * Requeue to the end of queue if we (and all of our ancestors) are not |
2270 | * the only element on the queue | |
98fbc798 | 2271 | */ |
454c7999 CC |
2272 | for_each_sched_rt_entity(rt_se) { |
2273 | if (rt_se->run_list.prev != rt_se->run_list.next) { | |
2274 | requeue_task_rt(rq, p, 0); | |
8aa6f0eb | 2275 | resched_curr(rq); |
454c7999 CC |
2276 | return; |
2277 | } | |
98fbc798 | 2278 | } |
bb44e5d1 IM |
2279 | } |
2280 | ||
83b699ed SV |
2281 | static void set_curr_task_rt(struct rq *rq) |
2282 | { | |
2283 | struct task_struct *p = rq->curr; | |
2284 | ||
78becc27 | 2285 | p->se.exec_start = rq_clock_task(rq); |
917b627d GH |
2286 | |
2287 | /* The running task is never eligible for pushing */ | |
2288 | dequeue_pushable_task(rq, p); | |
83b699ed SV |
2289 | } |
2290 | ||
6d686f45 | 2291 | static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task) |
0d721cea PW |
2292 | { |
2293 | /* | |
2294 | * Time slice is 0 for SCHED_FIFO tasks | |
2295 | */ | |
2296 | if (task->policy == SCHED_RR) | |
ce0dbbbb | 2297 | return sched_rr_timeslice; |
0d721cea PW |
2298 | else |
2299 | return 0; | |
2300 | } | |
2301 | ||
029632fb | 2302 | const struct sched_class rt_sched_class = { |
5522d5d5 | 2303 | .next = &fair_sched_class, |
bb44e5d1 IM |
2304 | .enqueue_task = enqueue_task_rt, |
2305 | .dequeue_task = dequeue_task_rt, | |
2306 | .yield_task = yield_task_rt, | |
2307 | ||
2308 | .check_preempt_curr = check_preempt_curr_rt, | |
2309 | ||
2310 | .pick_next_task = pick_next_task_rt, | |
2311 | .put_prev_task = put_prev_task_rt, | |
2312 | ||
681f3e68 | 2313 | #ifdef CONFIG_SMP |
4ce72a2c LZ |
2314 | .select_task_rq = select_task_rq_rt, |
2315 | ||
73fe6aae | 2316 | .set_cpus_allowed = set_cpus_allowed_rt, |
1f11eb6a GH |
2317 | .rq_online = rq_online_rt, |
2318 | .rq_offline = rq_offline_rt, | |
efbbd05a | 2319 | .task_woken = task_woken_rt, |
cb469845 | 2320 | .switched_from = switched_from_rt, |
681f3e68 | 2321 | #endif |
bb44e5d1 | 2322 | |
83b699ed | 2323 | .set_curr_task = set_curr_task_rt, |
bb44e5d1 | 2324 | .task_tick = task_tick_rt, |
cb469845 | 2325 | |
0d721cea PW |
2326 | .get_rr_interval = get_rr_interval_rt, |
2327 | ||
cb469845 SR |
2328 | .prio_changed = prio_changed_rt, |
2329 | .switched_to = switched_to_rt, | |
6e998916 SG |
2330 | |
2331 | .update_curr = update_curr_rt, | |
bb44e5d1 | 2332 | }; |
ada18de2 PZ |
2333 | |
2334 | #ifdef CONFIG_SCHED_DEBUG | |
2335 | extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq); | |
2336 | ||
029632fb | 2337 | void print_rt_stats(struct seq_file *m, int cpu) |
ada18de2 | 2338 | { |
ec514c48 | 2339 | rt_rq_iter_t iter; |
ada18de2 PZ |
2340 | struct rt_rq *rt_rq; |
2341 | ||
2342 | rcu_read_lock(); | |
ec514c48 | 2343 | for_each_rt_rq(rt_rq, iter, cpu_rq(cpu)) |
ada18de2 PZ |
2344 | print_rt_rq(m, cpu, rt_rq); |
2345 | rcu_read_unlock(); | |
2346 | } | |
55e12e5e | 2347 | #endif /* CONFIG_SCHED_DEBUG */ |