]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/sched_rt.c
sched: RT-balance, avoid overloading
[mirror_ubuntu-bionic-kernel.git] / kernel / sched_rt.c
CommitLineData
bb44e5d1
IM
1/*
2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
3 * policies)
4 */
5
4fd29176
SR
6#ifdef CONFIG_SMP
7static cpumask_t rt_overload_mask;
8static atomic_t rto_count;
9static inline int rt_overloaded(void)
10{
11 return atomic_read(&rto_count);
12}
13static inline cpumask_t *rt_overload(void)
14{
15 return &rt_overload_mask;
16}
17static inline void rt_set_overload(struct rq *rq)
18{
a22d7fc1 19 rq->rt.overloaded = 1;
4fd29176
SR
20 cpu_set(rq->cpu, rt_overload_mask);
21 /*
22 * Make sure the mask is visible before we set
23 * the overload count. That is checked to determine
24 * if we should look at the mask. It would be a shame
25 * if we looked at the mask, but the mask was not
26 * updated yet.
27 */
28 wmb();
29 atomic_inc(&rto_count);
30}
31static inline void rt_clear_overload(struct rq *rq)
32{
33 /* the order here really doesn't matter */
34 atomic_dec(&rto_count);
35 cpu_clear(rq->cpu, rt_overload_mask);
a22d7fc1 36 rq->rt.overloaded = 0;
4fd29176 37}
73fe6aae
GH
38
39static void update_rt_migration(struct rq *rq)
40{
41 if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1))
42 rt_set_overload(rq);
43 else
44 rt_clear_overload(rq);
45}
4fd29176
SR
46#endif /* CONFIG_SMP */
47
bb44e5d1
IM
48/*
49 * Update the current task's runtime statistics. Skip current tasks that
50 * are not in our scheduling class.
51 */
a9957449 52static void update_curr_rt(struct rq *rq)
bb44e5d1
IM
53{
54 struct task_struct *curr = rq->curr;
55 u64 delta_exec;
56
57 if (!task_has_rt_policy(curr))
58 return;
59
d281918d 60 delta_exec = rq->clock - curr->se.exec_start;
bb44e5d1
IM
61 if (unlikely((s64)delta_exec < 0))
62 delta_exec = 0;
6cfb0d5d
IM
63
64 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
bb44e5d1
IM
65
66 curr->se.sum_exec_runtime += delta_exec;
d281918d 67 curr->se.exec_start = rq->clock;
d842de87 68 cpuacct_charge(curr, delta_exec);
bb44e5d1
IM
69}
70
63489e45
SR
71static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq)
72{
73 WARN_ON(!rt_task(p));
74 rq->rt.rt_nr_running++;
764a9d6f
SR
75#ifdef CONFIG_SMP
76 if (p->prio < rq->rt.highest_prio)
77 rq->rt.highest_prio = p->prio;
73fe6aae
GH
78 if (p->nr_cpus_allowed > 1)
79 rq->rt.rt_nr_migratory++;
80
81 update_rt_migration(rq);
764a9d6f 82#endif /* CONFIG_SMP */
63489e45
SR
83}
84
85static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq)
86{
87 WARN_ON(!rt_task(p));
88 WARN_ON(!rq->rt.rt_nr_running);
89 rq->rt.rt_nr_running--;
764a9d6f
SR
90#ifdef CONFIG_SMP
91 if (rq->rt.rt_nr_running) {
92 struct rt_prio_array *array;
93
94 WARN_ON(p->prio < rq->rt.highest_prio);
95 if (p->prio == rq->rt.highest_prio) {
96 /* recalculate */
97 array = &rq->rt.active;
98 rq->rt.highest_prio =
99 sched_find_first_bit(array->bitmap);
100 } /* otherwise leave rq->highest prio alone */
101 } else
102 rq->rt.highest_prio = MAX_RT_PRIO;
73fe6aae
GH
103 if (p->nr_cpus_allowed > 1)
104 rq->rt.rt_nr_migratory--;
105
106 update_rt_migration(rq);
764a9d6f 107#endif /* CONFIG_SMP */
63489e45
SR
108}
109
fd390f6a 110static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
bb44e5d1
IM
111{
112 struct rt_prio_array *array = &rq->rt.active;
113
114 list_add_tail(&p->run_list, array->queue + p->prio);
115 __set_bit(p->prio, array->bitmap);
58e2d4ca 116 inc_cpu_load(rq, p->se.load.weight);
63489e45
SR
117
118 inc_rt_tasks(p, rq);
bb44e5d1
IM
119}
120
121/*
122 * Adding/removing a task to/from a priority array:
123 */
f02231e5 124static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
bb44e5d1
IM
125{
126 struct rt_prio_array *array = &rq->rt.active;
127
f1e14ef6 128 update_curr_rt(rq);
bb44e5d1
IM
129
130 list_del(&p->run_list);
131 if (list_empty(array->queue + p->prio))
132 __clear_bit(p->prio, array->bitmap);
58e2d4ca 133 dec_cpu_load(rq, p->se.load.weight);
63489e45
SR
134
135 dec_rt_tasks(p, rq);
bb44e5d1
IM
136}
137
138/*
139 * Put task to the end of the run list without the overhead of dequeue
140 * followed by enqueue.
141 */
142static void requeue_task_rt(struct rq *rq, struct task_struct *p)
143{
144 struct rt_prio_array *array = &rq->rt.active;
145
146 list_move_tail(&p->run_list, array->queue + p->prio);
147}
148
149static void
4530d7ab 150yield_task_rt(struct rq *rq)
bb44e5d1 151{
4530d7ab 152 requeue_task_rt(rq, rq->curr);
bb44e5d1
IM
153}
154
e7693a36 155#ifdef CONFIG_SMP
318e0893
GH
156static int find_lowest_rq(struct task_struct *task);
157
e7693a36
GH
158static int select_task_rq_rt(struct task_struct *p, int sync)
159{
318e0893
GH
160 struct rq *rq = task_rq(p);
161
162 /*
e1f47d89
SR
163 * If the current task is an RT task, then
164 * try to see if we can wake this RT task up on another
165 * runqueue. Otherwise simply start this RT task
166 * on its current runqueue.
167 *
168 * We want to avoid overloading runqueues. Even if
169 * the RT task is of higher priority than the current RT task.
170 * RT tasks behave differently than other tasks. If
171 * one gets preempted, we try to push it off to another queue.
172 * So trying to keep a preempting RT task on the same
173 * cache hot CPU will force the running RT task to
174 * a cold CPU. So we waste all the cache for the lower
175 * RT task in hopes of saving some of a RT task
176 * that is just being woken and probably will have
177 * cold cache anyway.
318e0893 178 */
e1f47d89 179 if (unlikely(rt_task(rq->curr))) {
318e0893
GH
180 int cpu = find_lowest_rq(p);
181
182 return (cpu == -1) ? task_cpu(p) : cpu;
183 }
184
185 /*
186 * Otherwise, just let it ride on the affined RQ and the
187 * post-schedule router will push the preempted task away
188 */
e7693a36
GH
189 return task_cpu(p);
190}
191#endif /* CONFIG_SMP */
192
bb44e5d1
IM
193/*
194 * Preempt the current task with a newly woken task if needed:
195 */
196static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
197{
198 if (p->prio < rq->curr->prio)
199 resched_task(rq->curr);
200}
201
fb8d4724 202static struct task_struct *pick_next_task_rt(struct rq *rq)
bb44e5d1
IM
203{
204 struct rt_prio_array *array = &rq->rt.active;
205 struct task_struct *next;
206 struct list_head *queue;
207 int idx;
208
209 idx = sched_find_first_bit(array->bitmap);
210 if (idx >= MAX_RT_PRIO)
211 return NULL;
212
213 queue = array->queue + idx;
214 next = list_entry(queue->next, struct task_struct, run_list);
215
d281918d 216 next->se.exec_start = rq->clock;
bb44e5d1
IM
217
218 return next;
219}
220
31ee529c 221static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
bb44e5d1 222{
f1e14ef6 223 update_curr_rt(rq);
bb44e5d1
IM
224 p->se.exec_start = 0;
225}
226
681f3e68 227#ifdef CONFIG_SMP
e8fa1362
SR
228/* Only try algorithms three times */
229#define RT_MAX_TRIES 3
230
231static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
232static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
233
f65eda4f
SR
234static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
235{
236 if (!task_running(rq, p) &&
73fe6aae
GH
237 (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
238 (p->nr_cpus_allowed > 1))
f65eda4f
SR
239 return 1;
240 return 0;
241}
242
e8fa1362 243/* Return the second highest RT task, NULL otherwise */
f65eda4f
SR
244static struct task_struct *pick_next_highest_task_rt(struct rq *rq,
245 int cpu)
e8fa1362
SR
246{
247 struct rt_prio_array *array = &rq->rt.active;
248 struct task_struct *next;
249 struct list_head *queue;
250 int idx;
251
252 assert_spin_locked(&rq->lock);
253
254 if (likely(rq->rt.rt_nr_running < 2))
255 return NULL;
256
257 idx = sched_find_first_bit(array->bitmap);
258 if (unlikely(idx >= MAX_RT_PRIO)) {
259 WARN_ON(1); /* rt_nr_running is bad */
260 return NULL;
261 }
262
263 queue = array->queue + idx;
f65eda4f
SR
264 BUG_ON(list_empty(queue));
265
e8fa1362 266 next = list_entry(queue->next, struct task_struct, run_list);
f65eda4f
SR
267 if (unlikely(pick_rt_task(rq, next, cpu)))
268 goto out;
e8fa1362
SR
269
270 if (queue->next->next != queue) {
271 /* same prio task */
272 next = list_entry(queue->next->next, struct task_struct, run_list);
f65eda4f
SR
273 if (pick_rt_task(rq, next, cpu))
274 goto out;
e8fa1362
SR
275 }
276
f65eda4f 277 retry:
e8fa1362
SR
278 /* slower, but more flexible */
279 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
f65eda4f 280 if (unlikely(idx >= MAX_RT_PRIO))
e8fa1362 281 return NULL;
e8fa1362
SR
282
283 queue = array->queue + idx;
f65eda4f
SR
284 BUG_ON(list_empty(queue));
285
286 list_for_each_entry(next, queue, run_list) {
287 if (pick_rt_task(rq, next, cpu))
288 goto out;
289 }
290
291 goto retry;
e8fa1362 292
f65eda4f 293 out:
e8fa1362
SR
294 return next;
295}
296
297static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
6e1254d2 298static DEFINE_PER_CPU(cpumask_t, valid_cpu_mask);
e8fa1362 299
6e1254d2 300static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
e8fa1362 301{
6e1254d2
GH
302 int cpu;
303 cpumask_t *valid_mask = &__get_cpu_var(valid_cpu_mask);
304 int lowest_prio = -1;
305 int ret = 0;
e8fa1362 306
6e1254d2
GH
307 cpus_clear(*lowest_mask);
308 cpus_and(*valid_mask, cpu_online_map, task->cpus_allowed);
e8fa1362 309
07b4032c
GH
310 /*
311 * Scan each rq for the lowest prio.
312 */
6e1254d2 313 for_each_cpu_mask(cpu, *valid_mask) {
07b4032c 314 struct rq *rq = cpu_rq(cpu);
e8fa1362 315
07b4032c
GH
316 /* We look for lowest RT prio or non-rt CPU */
317 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
6e1254d2
GH
318 if (ret)
319 cpus_clear(*lowest_mask);
320 cpu_set(rq->cpu, *lowest_mask);
321 return 1;
07b4032c
GH
322 }
323
324 /* no locking for now */
6e1254d2
GH
325 if ((rq->rt.highest_prio > task->prio)
326 && (rq->rt.highest_prio >= lowest_prio)) {
327 if (rq->rt.highest_prio > lowest_prio) {
328 /* new low - clear old data */
329 lowest_prio = rq->rt.highest_prio;
330 cpus_clear(*lowest_mask);
331 }
332 cpu_set(rq->cpu, *lowest_mask);
333 ret = 1;
e8fa1362 334 }
07b4032c
GH
335 }
336
6e1254d2
GH
337 return ret;
338}
339
340static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
341{
342 int first;
343
344 /* "this_cpu" is cheaper to preempt than a remote processor */
345 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
346 return this_cpu;
347
348 first = first_cpu(*mask);
349 if (first != NR_CPUS)
350 return first;
351
352 return -1;
353}
354
355static int find_lowest_rq(struct task_struct *task)
356{
357 struct sched_domain *sd;
358 cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
359 int this_cpu = smp_processor_id();
360 int cpu = task_cpu(task);
361
362 if (!find_lowest_cpus(task, lowest_mask))
363 return -1;
364
365 /*
366 * At this point we have built a mask of cpus representing the
367 * lowest priority tasks in the system. Now we want to elect
368 * the best one based on our affinity and topology.
369 *
370 * We prioritize the last cpu that the task executed on since
371 * it is most likely cache-hot in that location.
372 */
373 if (cpu_isset(cpu, *lowest_mask))
374 return cpu;
375
376 /*
377 * Otherwise, we consult the sched_domains span maps to figure
378 * out which cpu is logically closest to our hot cache data.
379 */
380 if (this_cpu == cpu)
381 this_cpu = -1; /* Skip this_cpu opt if the same */
382
383 for_each_domain(cpu, sd) {
384 if (sd->flags & SD_WAKE_AFFINE) {
385 cpumask_t domain_mask;
386 int best_cpu;
387
388 cpus_and(domain_mask, sd->span, *lowest_mask);
389
390 best_cpu = pick_optimal_cpu(this_cpu,
391 &domain_mask);
392 if (best_cpu != -1)
393 return best_cpu;
394 }
395 }
396
397 /*
398 * And finally, if there were no matches within the domains
399 * just give the caller *something* to work with from the compatible
400 * locations.
401 */
402 return pick_optimal_cpu(this_cpu, lowest_mask);
07b4032c
GH
403}
404
405/* Will lock the rq it finds */
406static struct rq *find_lock_lowest_rq(struct task_struct *task,
407 struct rq *rq)
408{
409 struct rq *lowest_rq = NULL;
410 int cpu;
411 int tries;
e8fa1362 412
07b4032c
GH
413 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
414 cpu = find_lowest_rq(task);
415
2de0b463 416 if ((cpu == -1) || (cpu == rq->cpu))
e8fa1362
SR
417 break;
418
07b4032c
GH
419 lowest_rq = cpu_rq(cpu);
420
e8fa1362 421 /* if the prio of this runqueue changed, try again */
07b4032c 422 if (double_lock_balance(rq, lowest_rq)) {
e8fa1362
SR
423 /*
424 * We had to unlock the run queue. In
425 * the mean time, task could have
426 * migrated already or had its affinity changed.
427 * Also make sure that it wasn't scheduled on its rq.
428 */
07b4032c 429 if (unlikely(task_rq(task) != rq ||
e8fa1362 430 !cpu_isset(lowest_rq->cpu, task->cpus_allowed) ||
07b4032c 431 task_running(rq, task) ||
e8fa1362
SR
432 !task->se.on_rq)) {
433 spin_unlock(&lowest_rq->lock);
434 lowest_rq = NULL;
435 break;
436 }
437 }
438
439 /* If this rq is still suitable use it. */
440 if (lowest_rq->rt.highest_prio > task->prio)
441 break;
442
443 /* try again */
444 spin_unlock(&lowest_rq->lock);
445 lowest_rq = NULL;
446 }
447
448 return lowest_rq;
449}
450
451/*
452 * If the current CPU has more than one RT task, see if the non
453 * running task can migrate over to a CPU that is running a task
454 * of lesser priority.
455 */
697f0a48 456static int push_rt_task(struct rq *rq)
e8fa1362
SR
457{
458 struct task_struct *next_task;
459 struct rq *lowest_rq;
460 int ret = 0;
461 int paranoid = RT_MAX_TRIES;
462
697f0a48 463 assert_spin_locked(&rq->lock);
e8fa1362 464
a22d7fc1
GH
465 if (!rq->rt.overloaded)
466 return 0;
467
697f0a48 468 next_task = pick_next_highest_task_rt(rq, -1);
e8fa1362
SR
469 if (!next_task)
470 return 0;
471
472 retry:
697f0a48 473 if (unlikely(next_task == rq->curr)) {
f65eda4f 474 WARN_ON(1);
e8fa1362 475 return 0;
f65eda4f 476 }
e8fa1362
SR
477
478 /*
479 * It's possible that the next_task slipped in of
480 * higher priority than current. If that's the case
481 * just reschedule current.
482 */
697f0a48
GH
483 if (unlikely(next_task->prio < rq->curr->prio)) {
484 resched_task(rq->curr);
e8fa1362
SR
485 return 0;
486 }
487
697f0a48 488 /* We might release rq lock */
e8fa1362
SR
489 get_task_struct(next_task);
490
491 /* find_lock_lowest_rq locks the rq if found */
697f0a48 492 lowest_rq = find_lock_lowest_rq(next_task, rq);
e8fa1362
SR
493 if (!lowest_rq) {
494 struct task_struct *task;
495 /*
697f0a48 496 * find lock_lowest_rq releases rq->lock
e8fa1362
SR
497 * so it is possible that next_task has changed.
498 * If it has, then try again.
499 */
697f0a48 500 task = pick_next_highest_task_rt(rq, -1);
e8fa1362
SR
501 if (unlikely(task != next_task) && task && paranoid--) {
502 put_task_struct(next_task);
503 next_task = task;
504 goto retry;
505 }
506 goto out;
507 }
508
509 assert_spin_locked(&lowest_rq->lock);
510
697f0a48 511 deactivate_task(rq, next_task, 0);
e8fa1362
SR
512 set_task_cpu(next_task, lowest_rq->cpu);
513 activate_task(lowest_rq, next_task, 0);
514
515 resched_task(lowest_rq->curr);
516
517 spin_unlock(&lowest_rq->lock);
518
519 ret = 1;
520out:
521 put_task_struct(next_task);
522
523 return ret;
524}
525
526/*
527 * TODO: Currently we just use the second highest prio task on
528 * the queue, and stop when it can't migrate (or there's
529 * no more RT tasks). There may be a case where a lower
530 * priority RT task has a different affinity than the
531 * higher RT task. In this case the lower RT task could
532 * possibly be able to migrate where as the higher priority
533 * RT task could not. We currently ignore this issue.
534 * Enhancements are welcome!
535 */
536static void push_rt_tasks(struct rq *rq)
537{
538 /* push_rt_task will return true if it moved an RT */
539 while (push_rt_task(rq))
540 ;
541}
542
f65eda4f
SR
543static int pull_rt_task(struct rq *this_rq)
544{
545 struct task_struct *next;
546 struct task_struct *p;
547 struct rq *src_rq;
548 cpumask_t *rto_cpumask;
549 int this_cpu = this_rq->cpu;
550 int cpu;
551 int ret = 0;
552
553 assert_spin_locked(&this_rq->lock);
554
555 /*
556 * If cpusets are used, and we have overlapping
557 * run queue cpusets, then this algorithm may not catch all.
558 * This is just the price you pay on trying to keep
559 * dirtying caches down on large SMP machines.
560 */
561 if (likely(!rt_overloaded()))
562 return 0;
563
564 next = pick_next_task_rt(this_rq);
565
566 rto_cpumask = rt_overload();
567
568 for_each_cpu_mask(cpu, *rto_cpumask) {
569 if (this_cpu == cpu)
570 continue;
571
572 src_rq = cpu_rq(cpu);
573 if (unlikely(src_rq->rt.rt_nr_running <= 1)) {
574 /*
575 * It is possible that overlapping cpusets
576 * will miss clearing a non overloaded runqueue.
577 * Clear it now.
578 */
579 if (double_lock_balance(this_rq, src_rq)) {
580 /* unlocked our runqueue lock */
581 struct task_struct *old_next = next;
582 next = pick_next_task_rt(this_rq);
583 if (next != old_next)
584 ret = 1;
585 }
586 if (likely(src_rq->rt.rt_nr_running <= 1))
587 /*
588 * Small chance that this_rq->curr changed
589 * but it's really harmless here.
590 */
591 rt_clear_overload(this_rq);
592 else
593 /*
594 * Heh, the src_rq is now overloaded, since
595 * we already have the src_rq lock, go straight
596 * to pulling tasks from it.
597 */
598 goto try_pulling;
599 spin_unlock(&src_rq->lock);
600 continue;
601 }
602
603 /*
604 * We can potentially drop this_rq's lock in
605 * double_lock_balance, and another CPU could
606 * steal our next task - hence we must cause
607 * the caller to recalculate the next task
608 * in that case:
609 */
610 if (double_lock_balance(this_rq, src_rq)) {
611 struct task_struct *old_next = next;
612 next = pick_next_task_rt(this_rq);
613 if (next != old_next)
614 ret = 1;
615 }
616
617 /*
618 * Are there still pullable RT tasks?
619 */
620 if (src_rq->rt.rt_nr_running <= 1) {
621 spin_unlock(&src_rq->lock);
622 continue;
623 }
624
625 try_pulling:
626 p = pick_next_highest_task_rt(src_rq, this_cpu);
627
628 /*
629 * Do we have an RT task that preempts
630 * the to-be-scheduled task?
631 */
632 if (p && (!next || (p->prio < next->prio))) {
633 WARN_ON(p == src_rq->curr);
634 WARN_ON(!p->se.on_rq);
635
636 /*
637 * There's a chance that p is higher in priority
638 * than what's currently running on its cpu.
639 * This is just that p is wakeing up and hasn't
640 * had a chance to schedule. We only pull
641 * p if it is lower in priority than the
642 * current task on the run queue or
643 * this_rq next task is lower in prio than
644 * the current task on that rq.
645 */
646 if (p->prio < src_rq->curr->prio ||
647 (next && next->prio < src_rq->curr->prio))
648 goto bail;
649
650 ret = 1;
651
652 deactivate_task(src_rq, p, 0);
653 set_task_cpu(p, this_cpu);
654 activate_task(this_rq, p, 0);
655 /*
656 * We continue with the search, just in
657 * case there's an even higher prio task
658 * in another runqueue. (low likelyhood
659 * but possible)
660 */
661
662 /*
663 * Update next so that we won't pick a task
664 * on another cpu with a priority lower (or equal)
665 * than the one we just picked.
666 */
667 next = p;
668
669 }
670 bail:
671 spin_unlock(&src_rq->lock);
672 }
673
674 return ret;
675}
676
677static void schedule_balance_rt(struct rq *rq,
678 struct task_struct *prev)
679{
680 /* Try to pull RT tasks here if we lower this rq's prio */
681 if (unlikely(rt_task(prev)) &&
682 rq->rt.highest_prio > prev->prio)
683 pull_rt_task(rq);
684}
685
e8fa1362
SR
686static void schedule_tail_balance_rt(struct rq *rq)
687{
688 /*
689 * If we have more than one rt_task queued, then
690 * see if we can push the other rt_tasks off to other CPUS.
691 * Note we may release the rq lock, and since
692 * the lock was owned by prev, we need to release it
693 * first via finish_lock_switch and then reaquire it here.
694 */
a22d7fc1 695 if (unlikely(rq->rt.overloaded)) {
e8fa1362
SR
696 spin_lock_irq(&rq->lock);
697 push_rt_tasks(rq);
698 spin_unlock_irq(&rq->lock);
699 }
700}
701
4642dafd
SR
702
703static void wakeup_balance_rt(struct rq *rq, struct task_struct *p)
704{
705 if (unlikely(rt_task(p)) &&
706 !task_running(rq, p) &&
a22d7fc1
GH
707 (p->prio >= rq->rt.highest_prio) &&
708 rq->rt.overloaded)
4642dafd
SR
709 push_rt_tasks(rq);
710}
711
43010659 712static unsigned long
bb44e5d1 713load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
e1d1484f
PW
714 unsigned long max_load_move,
715 struct sched_domain *sd, enum cpu_idle_type idle,
716 int *all_pinned, int *this_best_prio)
bb44e5d1 717{
c7a1e46a
SR
718 /* don't touch RT tasks */
719 return 0;
e1d1484f
PW
720}
721
722static int
723move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
724 struct sched_domain *sd, enum cpu_idle_type idle)
725{
c7a1e46a
SR
726 /* don't touch RT tasks */
727 return 0;
bb44e5d1 728}
73fe6aae
GH
729static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
730{
731 int weight = cpus_weight(*new_mask);
732
733 BUG_ON(!rt_task(p));
734
735 /*
736 * Update the migration status of the RQ if we have an RT task
737 * which is running AND changing its weight value.
738 */
739 if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
740 struct rq *rq = task_rq(p);
741
742 if ((p->nr_cpus_allowed <= 1) && (weight > 1))
743 rq->rt.rt_nr_migratory++;
744 else if((p->nr_cpus_allowed > 1) && (weight <= 1)) {
745 BUG_ON(!rq->rt.rt_nr_migratory);
746 rq->rt.rt_nr_migratory--;
747 }
748
749 update_rt_migration(rq);
750 }
751
752 p->cpus_allowed = *new_mask;
753 p->nr_cpus_allowed = weight;
754}
e8fa1362
SR
755#else /* CONFIG_SMP */
756# define schedule_tail_balance_rt(rq) do { } while (0)
f65eda4f 757# define schedule_balance_rt(rq, prev) do { } while (0)
4642dafd 758# define wakeup_balance_rt(rq, p) do { } while (0)
e8fa1362 759#endif /* CONFIG_SMP */
bb44e5d1
IM
760
761static void task_tick_rt(struct rq *rq, struct task_struct *p)
762{
67e2be02
PZ
763 update_curr_rt(rq);
764
bb44e5d1
IM
765 /*
766 * RR tasks need a special form of timeslice management.
767 * FIFO tasks have no timeslices.
768 */
769 if (p->policy != SCHED_RR)
770 return;
771
772 if (--p->time_slice)
773 return;
774
a4ec24b4 775 p->time_slice = DEF_TIMESLICE;
bb44e5d1 776
98fbc798
DA
777 /*
778 * Requeue to the end of queue if we are not the only element
779 * on the queue:
780 */
781 if (p->run_list.prev != p->run_list.next) {
782 requeue_task_rt(rq, p);
783 set_tsk_need_resched(p);
784 }
bb44e5d1
IM
785}
786
83b699ed
SV
787static void set_curr_task_rt(struct rq *rq)
788{
789 struct task_struct *p = rq->curr;
790
791 p->se.exec_start = rq->clock;
792}
793
5522d5d5
IM
794const struct sched_class rt_sched_class = {
795 .next = &fair_sched_class,
bb44e5d1
IM
796 .enqueue_task = enqueue_task_rt,
797 .dequeue_task = dequeue_task_rt,
798 .yield_task = yield_task_rt,
e7693a36
GH
799#ifdef CONFIG_SMP
800 .select_task_rq = select_task_rq_rt,
801#endif /* CONFIG_SMP */
bb44e5d1
IM
802
803 .check_preempt_curr = check_preempt_curr_rt,
804
805 .pick_next_task = pick_next_task_rt,
806 .put_prev_task = put_prev_task_rt,
807
681f3e68 808#ifdef CONFIG_SMP
bb44e5d1 809 .load_balance = load_balance_rt,
e1d1484f 810 .move_one_task = move_one_task_rt,
73fe6aae 811 .set_cpus_allowed = set_cpus_allowed_rt,
681f3e68 812#endif
bb44e5d1 813
83b699ed 814 .set_curr_task = set_curr_task_rt,
bb44e5d1 815 .task_tick = task_tick_rt,
bb44e5d1 816};