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