]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/sched_rt.c
sched: clean up pick_next_highest_task_rt()
[net-next-2.6.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 */
17b3279b
GH
179 if (unlikely(rt_task(rq->curr)) &&
180 (p->nr_cpus_allowed > 1)) {
318e0893
GH
181 int cpu = find_lowest_rq(p);
182
183 return (cpu == -1) ? task_cpu(p) : cpu;
184 }
185
186 /*
187 * Otherwise, just let it ride on the affined RQ and the
188 * post-schedule router will push the preempted task away
189 */
e7693a36
GH
190 return task_cpu(p);
191}
192#endif /* CONFIG_SMP */
193
bb44e5d1
IM
194/*
195 * Preempt the current task with a newly woken task if needed:
196 */
197static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
198{
199 if (p->prio < rq->curr->prio)
200 resched_task(rq->curr);
201}
202
fb8d4724 203static struct task_struct *pick_next_task_rt(struct rq *rq)
bb44e5d1
IM
204{
205 struct rt_prio_array *array = &rq->rt.active;
206 struct task_struct *next;
207 struct list_head *queue;
208 int idx;
209
210 idx = sched_find_first_bit(array->bitmap);
211 if (idx >= MAX_RT_PRIO)
212 return NULL;
213
214 queue = array->queue + idx;
215 next = list_entry(queue->next, struct task_struct, run_list);
216
d281918d 217 next->se.exec_start = rq->clock;
bb44e5d1
IM
218
219 return next;
220}
221
31ee529c 222static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
bb44e5d1 223{
f1e14ef6 224 update_curr_rt(rq);
bb44e5d1
IM
225 p->se.exec_start = 0;
226}
227
681f3e68 228#ifdef CONFIG_SMP
e8fa1362
SR
229/* Only try algorithms three times */
230#define RT_MAX_TRIES 3
231
232static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
233static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
234
f65eda4f
SR
235static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
236{
237 if (!task_running(rq, p) &&
73fe6aae
GH
238 (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
239 (p->nr_cpus_allowed > 1))
f65eda4f
SR
240 return 1;
241 return 0;
242}
243
e8fa1362 244/* Return the second highest RT task, NULL otherwise */
79064fbf 245static struct task_struct *pick_next_highest_task_rt(struct rq *rq, 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 */
79064fbf
IM
272 next = list_entry(queue->next->next, struct task_struct,
273 run_list);
f65eda4f
SR
274 if (pick_rt_task(rq, next, cpu))
275 goto out;
e8fa1362
SR
276 }
277
f65eda4f 278 retry:
e8fa1362
SR
279 /* slower, but more flexible */
280 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
f65eda4f 281 if (unlikely(idx >= MAX_RT_PRIO))
e8fa1362 282 return NULL;
e8fa1362
SR
283
284 queue = array->queue + idx;
f65eda4f
SR
285 BUG_ON(list_empty(queue));
286
287 list_for_each_entry(next, queue, run_list) {
288 if (pick_rt_task(rq, next, cpu))
289 goto out;
290 }
291
292 goto retry;
e8fa1362 293
f65eda4f 294 out:
e8fa1362
SR
295 return next;
296}
297
298static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
299
6e1254d2 300static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
e8fa1362 301{
6e1254d2 302 int lowest_prio = -1;
610bf056 303 int lowest_cpu = -1;
06f90dbd 304 int count = 0;
610bf056 305 int cpu;
e8fa1362 306
610bf056 307 cpus_and(*lowest_mask, cpu_online_map, task->cpus_allowed);
e8fa1362 308
07b4032c
GH
309 /*
310 * Scan each rq for the lowest prio.
311 */
610bf056 312 for_each_cpu_mask(cpu, *lowest_mask) {
07b4032c 313 struct rq *rq = cpu_rq(cpu);
e8fa1362 314
07b4032c
GH
315 /* We look for lowest RT prio or non-rt CPU */
316 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
610bf056
SR
317 /*
318 * if we already found a low RT queue
319 * and now we found this non-rt queue
320 * clear the mask and set our bit.
321 * Otherwise just return the queue as is
322 * and the count==1 will cause the algorithm
323 * to use the first bit found.
324 */
325 if (lowest_cpu != -1) {
6e1254d2 326 cpus_clear(*lowest_mask);
610bf056
SR
327 cpu_set(rq->cpu, *lowest_mask);
328 }
6e1254d2 329 return 1;
07b4032c
GH
330 }
331
332 /* no locking for now */
6e1254d2
GH
333 if ((rq->rt.highest_prio > task->prio)
334 && (rq->rt.highest_prio >= lowest_prio)) {
335 if (rq->rt.highest_prio > lowest_prio) {
336 /* new low - clear old data */
337 lowest_prio = rq->rt.highest_prio;
610bf056
SR
338 lowest_cpu = cpu;
339 count = 0;
6e1254d2 340 }
06f90dbd 341 count++;
610bf056
SR
342 } else
343 cpu_clear(cpu, *lowest_mask);
344 }
345
346 /*
347 * Clear out all the set bits that represent
348 * runqueues that were of higher prio than
349 * the lowest_prio.
350 */
351 if (lowest_cpu > 0) {
352 /*
353 * Perhaps we could add another cpumask op to
354 * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
355 * Then that could be optimized to use memset and such.
356 */
357 for_each_cpu_mask(cpu, *lowest_mask) {
358 if (cpu >= lowest_cpu)
359 break;
360 cpu_clear(cpu, *lowest_mask);
e8fa1362 361 }
07b4032c
GH
362 }
363
06f90dbd 364 return count;
6e1254d2
GH
365}
366
367static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
368{
369 int first;
370
371 /* "this_cpu" is cheaper to preempt than a remote processor */
372 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
373 return this_cpu;
374
375 first = first_cpu(*mask);
376 if (first != NR_CPUS)
377 return first;
378
379 return -1;
380}
381
382static int find_lowest_rq(struct task_struct *task)
383{
384 struct sched_domain *sd;
385 cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
386 int this_cpu = smp_processor_id();
387 int cpu = task_cpu(task);
06f90dbd
GH
388 int count = find_lowest_cpus(task, lowest_mask);
389
390 if (!count)
391 return -1; /* No targets found */
6e1254d2 392
06f90dbd
GH
393 /*
394 * There is no sense in performing an optimal search if only one
395 * target is found.
396 */
397 if (count == 1)
398 return first_cpu(*lowest_mask);
6e1254d2
GH
399
400 /*
401 * At this point we have built a mask of cpus representing the
402 * lowest priority tasks in the system. Now we want to elect
403 * the best one based on our affinity and topology.
404 *
405 * We prioritize the last cpu that the task executed on since
406 * it is most likely cache-hot in that location.
407 */
408 if (cpu_isset(cpu, *lowest_mask))
409 return cpu;
410
411 /*
412 * Otherwise, we consult the sched_domains span maps to figure
413 * out which cpu is logically closest to our hot cache data.
414 */
415 if (this_cpu == cpu)
416 this_cpu = -1; /* Skip this_cpu opt if the same */
417
418 for_each_domain(cpu, sd) {
419 if (sd->flags & SD_WAKE_AFFINE) {
420 cpumask_t domain_mask;
421 int best_cpu;
422
423 cpus_and(domain_mask, sd->span, *lowest_mask);
424
425 best_cpu = pick_optimal_cpu(this_cpu,
426 &domain_mask);
427 if (best_cpu != -1)
428 return best_cpu;
429 }
430 }
431
432 /*
433 * And finally, if there were no matches within the domains
434 * just give the caller *something* to work with from the compatible
435 * locations.
436 */
437 return pick_optimal_cpu(this_cpu, lowest_mask);
07b4032c
GH
438}
439
440/* Will lock the rq it finds */
441static struct rq *find_lock_lowest_rq(struct task_struct *task,
442 struct rq *rq)
443{
444 struct rq *lowest_rq = NULL;
445 int cpu;
446 int tries;
e8fa1362 447
07b4032c
GH
448 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
449 cpu = find_lowest_rq(task);
450
2de0b463 451 if ((cpu == -1) || (cpu == rq->cpu))
e8fa1362
SR
452 break;
453
07b4032c
GH
454 lowest_rq = cpu_rq(cpu);
455
e8fa1362 456 /* if the prio of this runqueue changed, try again */
07b4032c 457 if (double_lock_balance(rq, lowest_rq)) {
e8fa1362
SR
458 /*
459 * We had to unlock the run queue. In
460 * the mean time, task could have
461 * migrated already or had its affinity changed.
462 * Also make sure that it wasn't scheduled on its rq.
463 */
07b4032c 464 if (unlikely(task_rq(task) != rq ||
e8fa1362 465 !cpu_isset(lowest_rq->cpu, task->cpus_allowed) ||
07b4032c 466 task_running(rq, task) ||
e8fa1362
SR
467 !task->se.on_rq)) {
468 spin_unlock(&lowest_rq->lock);
469 lowest_rq = NULL;
470 break;
471 }
472 }
473
474 /* If this rq is still suitable use it. */
475 if (lowest_rq->rt.highest_prio > task->prio)
476 break;
477
478 /* try again */
479 spin_unlock(&lowest_rq->lock);
480 lowest_rq = NULL;
481 }
482
483 return lowest_rq;
484}
485
486/*
487 * If the current CPU has more than one RT task, see if the non
488 * running task can migrate over to a CPU that is running a task
489 * of lesser priority.
490 */
697f0a48 491static int push_rt_task(struct rq *rq)
e8fa1362
SR
492{
493 struct task_struct *next_task;
494 struct rq *lowest_rq;
495 int ret = 0;
496 int paranoid = RT_MAX_TRIES;
497
697f0a48 498 assert_spin_locked(&rq->lock);
e8fa1362 499
a22d7fc1
GH
500 if (!rq->rt.overloaded)
501 return 0;
502
697f0a48 503 next_task = pick_next_highest_task_rt(rq, -1);
e8fa1362
SR
504 if (!next_task)
505 return 0;
506
507 retry:
697f0a48 508 if (unlikely(next_task == rq->curr)) {
f65eda4f 509 WARN_ON(1);
e8fa1362 510 return 0;
f65eda4f 511 }
e8fa1362
SR
512
513 /*
514 * It's possible that the next_task slipped in of
515 * higher priority than current. If that's the case
516 * just reschedule current.
517 */
697f0a48
GH
518 if (unlikely(next_task->prio < rq->curr->prio)) {
519 resched_task(rq->curr);
e8fa1362
SR
520 return 0;
521 }
522
697f0a48 523 /* We might release rq lock */
e8fa1362
SR
524 get_task_struct(next_task);
525
526 /* find_lock_lowest_rq locks the rq if found */
697f0a48 527 lowest_rq = find_lock_lowest_rq(next_task, rq);
e8fa1362
SR
528 if (!lowest_rq) {
529 struct task_struct *task;
530 /*
697f0a48 531 * find lock_lowest_rq releases rq->lock
e8fa1362
SR
532 * so it is possible that next_task has changed.
533 * If it has, then try again.
534 */
697f0a48 535 task = pick_next_highest_task_rt(rq, -1);
e8fa1362
SR
536 if (unlikely(task != next_task) && task && paranoid--) {
537 put_task_struct(next_task);
538 next_task = task;
539 goto retry;
540 }
541 goto out;
542 }
543
544 assert_spin_locked(&lowest_rq->lock);
545
697f0a48 546 deactivate_task(rq, next_task, 0);
e8fa1362
SR
547 set_task_cpu(next_task, lowest_rq->cpu);
548 activate_task(lowest_rq, next_task, 0);
549
550 resched_task(lowest_rq->curr);
551
552 spin_unlock(&lowest_rq->lock);
553
554 ret = 1;
555out:
556 put_task_struct(next_task);
557
558 return ret;
559}
560
561/*
562 * TODO: Currently we just use the second highest prio task on
563 * the queue, and stop when it can't migrate (or there's
564 * no more RT tasks). There may be a case where a lower
565 * priority RT task has a different affinity than the
566 * higher RT task. In this case the lower RT task could
567 * possibly be able to migrate where as the higher priority
568 * RT task could not. We currently ignore this issue.
569 * Enhancements are welcome!
570 */
571static void push_rt_tasks(struct rq *rq)
572{
573 /* push_rt_task will return true if it moved an RT */
574 while (push_rt_task(rq))
575 ;
576}
577
f65eda4f
SR
578static int pull_rt_task(struct rq *this_rq)
579{
580 struct task_struct *next;
581 struct task_struct *p;
582 struct rq *src_rq;
583 cpumask_t *rto_cpumask;
584 int this_cpu = this_rq->cpu;
585 int cpu;
586 int ret = 0;
587
588 assert_spin_locked(&this_rq->lock);
589
590 /*
591 * If cpusets are used, and we have overlapping
592 * run queue cpusets, then this algorithm may not catch all.
593 * This is just the price you pay on trying to keep
594 * dirtying caches down on large SMP machines.
595 */
596 if (likely(!rt_overloaded()))
597 return 0;
598
599 next = pick_next_task_rt(this_rq);
600
601 rto_cpumask = rt_overload();
602
603 for_each_cpu_mask(cpu, *rto_cpumask) {
604 if (this_cpu == cpu)
605 continue;
606
607 src_rq = cpu_rq(cpu);
608 if (unlikely(src_rq->rt.rt_nr_running <= 1)) {
609 /*
610 * It is possible that overlapping cpusets
611 * will miss clearing a non overloaded runqueue.
612 * Clear it now.
613 */
614 if (double_lock_balance(this_rq, src_rq)) {
615 /* unlocked our runqueue lock */
616 struct task_struct *old_next = next;
617 next = pick_next_task_rt(this_rq);
618 if (next != old_next)
619 ret = 1;
620 }
621 if (likely(src_rq->rt.rt_nr_running <= 1))
622 /*
623 * Small chance that this_rq->curr changed
624 * but it's really harmless here.
625 */
626 rt_clear_overload(this_rq);
627 else
628 /*
629 * Heh, the src_rq is now overloaded, since
630 * we already have the src_rq lock, go straight
631 * to pulling tasks from it.
632 */
633 goto try_pulling;
634 spin_unlock(&src_rq->lock);
635 continue;
636 }
637
638 /*
639 * We can potentially drop this_rq's lock in
640 * double_lock_balance, and another CPU could
641 * steal our next task - hence we must cause
642 * the caller to recalculate the next task
643 * in that case:
644 */
645 if (double_lock_balance(this_rq, src_rq)) {
646 struct task_struct *old_next = next;
647 next = pick_next_task_rt(this_rq);
648 if (next != old_next)
649 ret = 1;
650 }
651
652 /*
653 * Are there still pullable RT tasks?
654 */
655 if (src_rq->rt.rt_nr_running <= 1) {
656 spin_unlock(&src_rq->lock);
657 continue;
658 }
659
660 try_pulling:
661 p = pick_next_highest_task_rt(src_rq, this_cpu);
662
663 /*
664 * Do we have an RT task that preempts
665 * the to-be-scheduled task?
666 */
667 if (p && (!next || (p->prio < next->prio))) {
668 WARN_ON(p == src_rq->curr);
669 WARN_ON(!p->se.on_rq);
670
671 /*
672 * There's a chance that p is higher in priority
673 * than what's currently running on its cpu.
674 * This is just that p is wakeing up and hasn't
675 * had a chance to schedule. We only pull
676 * p if it is lower in priority than the
677 * current task on the run queue or
678 * this_rq next task is lower in prio than
679 * the current task on that rq.
680 */
681 if (p->prio < src_rq->curr->prio ||
682 (next && next->prio < src_rq->curr->prio))
683 goto bail;
684
685 ret = 1;
686
687 deactivate_task(src_rq, p, 0);
688 set_task_cpu(p, this_cpu);
689 activate_task(this_rq, p, 0);
690 /*
691 * We continue with the search, just in
692 * case there's an even higher prio task
693 * in another runqueue. (low likelyhood
694 * but possible)
695 */
696
697 /*
698 * Update next so that we won't pick a task
699 * on another cpu with a priority lower (or equal)
700 * than the one we just picked.
701 */
702 next = p;
703
704 }
705 bail:
706 spin_unlock(&src_rq->lock);
707 }
708
709 return ret;
710}
711
712static void schedule_balance_rt(struct rq *rq,
713 struct task_struct *prev)
714{
715 /* Try to pull RT tasks here if we lower this rq's prio */
716 if (unlikely(rt_task(prev)) &&
717 rq->rt.highest_prio > prev->prio)
718 pull_rt_task(rq);
719}
720
e8fa1362
SR
721static void schedule_tail_balance_rt(struct rq *rq)
722{
723 /*
724 * If we have more than one rt_task queued, then
725 * see if we can push the other rt_tasks off to other CPUS.
726 * Note we may release the rq lock, and since
727 * the lock was owned by prev, we need to release it
728 * first via finish_lock_switch and then reaquire it here.
729 */
a22d7fc1 730 if (unlikely(rq->rt.overloaded)) {
e8fa1362
SR
731 spin_lock_irq(&rq->lock);
732 push_rt_tasks(rq);
733 spin_unlock_irq(&rq->lock);
734 }
735}
736
4642dafd
SR
737
738static void wakeup_balance_rt(struct rq *rq, struct task_struct *p)
739{
740 if (unlikely(rt_task(p)) &&
741 !task_running(rq, p) &&
a22d7fc1
GH
742 (p->prio >= rq->rt.highest_prio) &&
743 rq->rt.overloaded)
4642dafd
SR
744 push_rt_tasks(rq);
745}
746
43010659 747static unsigned long
bb44e5d1 748load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
e1d1484f
PW
749 unsigned long max_load_move,
750 struct sched_domain *sd, enum cpu_idle_type idle,
751 int *all_pinned, int *this_best_prio)
bb44e5d1 752{
c7a1e46a
SR
753 /* don't touch RT tasks */
754 return 0;
e1d1484f
PW
755}
756
757static int
758move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
759 struct sched_domain *sd, enum cpu_idle_type idle)
760{
c7a1e46a
SR
761 /* don't touch RT tasks */
762 return 0;
bb44e5d1 763}
73fe6aae
GH
764static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
765{
766 int weight = cpus_weight(*new_mask);
767
768 BUG_ON(!rt_task(p));
769
770 /*
771 * Update the migration status of the RQ if we have an RT task
772 * which is running AND changing its weight value.
773 */
774 if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
775 struct rq *rq = task_rq(p);
776
777 if ((p->nr_cpus_allowed <= 1) && (weight > 1))
778 rq->rt.rt_nr_migratory++;
779 else if((p->nr_cpus_allowed > 1) && (weight <= 1)) {
780 BUG_ON(!rq->rt.rt_nr_migratory);
781 rq->rt.rt_nr_migratory--;
782 }
783
784 update_rt_migration(rq);
785 }
786
787 p->cpus_allowed = *new_mask;
788 p->nr_cpus_allowed = weight;
789}
e8fa1362
SR
790#else /* CONFIG_SMP */
791# define schedule_tail_balance_rt(rq) do { } while (0)
f65eda4f 792# define schedule_balance_rt(rq, prev) do { } while (0)
4642dafd 793# define wakeup_balance_rt(rq, p) do { } while (0)
e8fa1362 794#endif /* CONFIG_SMP */
bb44e5d1
IM
795
796static void task_tick_rt(struct rq *rq, struct task_struct *p)
797{
67e2be02
PZ
798 update_curr_rt(rq);
799
bb44e5d1
IM
800 /*
801 * RR tasks need a special form of timeslice management.
802 * FIFO tasks have no timeslices.
803 */
804 if (p->policy != SCHED_RR)
805 return;
806
807 if (--p->time_slice)
808 return;
809
a4ec24b4 810 p->time_slice = DEF_TIMESLICE;
bb44e5d1 811
98fbc798
DA
812 /*
813 * Requeue to the end of queue if we are not the only element
814 * on the queue:
815 */
816 if (p->run_list.prev != p->run_list.next) {
817 requeue_task_rt(rq, p);
818 set_tsk_need_resched(p);
819 }
bb44e5d1
IM
820}
821
83b699ed
SV
822static void set_curr_task_rt(struct rq *rq)
823{
824 struct task_struct *p = rq->curr;
825
826 p->se.exec_start = rq->clock;
827}
828
5522d5d5
IM
829const struct sched_class rt_sched_class = {
830 .next = &fair_sched_class,
bb44e5d1
IM
831 .enqueue_task = enqueue_task_rt,
832 .dequeue_task = dequeue_task_rt,
833 .yield_task = yield_task_rt,
e7693a36
GH
834#ifdef CONFIG_SMP
835 .select_task_rq = select_task_rq_rt,
836#endif /* CONFIG_SMP */
bb44e5d1
IM
837
838 .check_preempt_curr = check_preempt_curr_rt,
839
840 .pick_next_task = pick_next_task_rt,
841 .put_prev_task = put_prev_task_rt,
842
681f3e68 843#ifdef CONFIG_SMP
bb44e5d1 844 .load_balance = load_balance_rt,
e1d1484f 845 .move_one_task = move_one_task_rt,
73fe6aae 846 .set_cpus_allowed = set_cpus_allowed_rt,
681f3e68 847#endif
bb44e5d1 848
83b699ed 849 .set_curr_task = set_curr_task_rt,
bb44e5d1 850 .task_tick = task_tick_rt,
bb44e5d1 851};