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