blob: 52d88f193afcc044a2e14c4bc27c4901c45aa3c6 [file] [log] [blame]
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001/*
2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
3 * policies)
4 */
5
Steven Rostedt4fd29172008-01-25 21:08:06 +01006#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{
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +010019 rq->rt.overloaded = 1;
Steven Rostedt4fd29172008-01-25 21:08:06 +010020 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);
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +010036 rq->rt.overloaded = 0;
Steven Rostedt4fd29172008-01-25 21:08:06 +010037}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010038
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}
Steven Rostedt4fd29172008-01-25 21:08:06 +010046#endif /* CONFIG_SMP */
47
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020048/*
49 * Update the current task's runtime statistics. Skip current tasks that
50 * are not in our scheduling class.
51 */
Alexey Dobriyana9957442007-10-15 17:00:13 +020052static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020053{
54 struct task_struct *curr = rq->curr;
55 u64 delta_exec;
56
57 if (!task_has_rt_policy(curr))
58 return;
59
Ingo Molnard2819182007-08-09 11:16:47 +020060 delta_exec = rq->clock - curr->se.exec_start;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020061 if (unlikely((s64)delta_exec < 0))
62 delta_exec = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +020063
64 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020065
66 curr->se.sum_exec_runtime += delta_exec;
Ingo Molnard2819182007-08-09 11:16:47 +020067 curr->se.exec_start = rq->clock;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010068 cpuacct_charge(curr, delta_exec);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020069}
70
Steven Rostedt63489e42008-01-25 21:08:03 +010071static 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++;
Steven Rostedt764a9d62008-01-25 21:08:04 +010075#ifdef CONFIG_SMP
76 if (p->prio < rq->rt.highest_prio)
77 rq->rt.highest_prio = p->prio;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010078 if (p->nr_cpus_allowed > 1)
79 rq->rt.rt_nr_migratory++;
80
81 update_rt_migration(rq);
Steven Rostedt764a9d62008-01-25 21:08:04 +010082#endif /* CONFIG_SMP */
Steven Rostedt63489e42008-01-25 21:08:03 +010083}
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--;
Steven Rostedt764a9d62008-01-25 21:08:04 +010090#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;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100103 if (p->nr_cpus_allowed > 1)
104 rq->rt.rt_nr_migratory--;
105
106 update_rt_migration(rq);
Steven Rostedt764a9d62008-01-25 21:08:04 +0100107#endif /* CONFIG_SMP */
Steven Rostedt63489e42008-01-25 21:08:03 +0100108}
109
Ingo Molnarfd390f62007-08-09 11:16:48 +0200110static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200111{
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);
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +0100116 inc_cpu_load(rq, p->se.load.weight);
Steven Rostedt63489e42008-01-25 21:08:03 +0100117
118 inc_rt_tasks(p, rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200119}
120
121/*
122 * Adding/removing a task to/from a priority array:
123 */
Ingo Molnarf02231e2007-08-09 11:16:48 +0200124static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200125{
126 struct rt_prio_array *array = &rq->rt.active;
127
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200128 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200129
130 list_del(&p->run_list);
131 if (list_empty(array->queue + p->prio))
132 __clear_bit(p->prio, array->bitmap);
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +0100133 dec_cpu_load(rq, p->se.load.weight);
Steven Rostedt63489e42008-01-25 21:08:03 +0100134
135 dec_rt_tasks(p, rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200136}
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
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200150yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200151{
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200152 requeue_task_rt(rq, rq->curr);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200153}
154
Gregory Haskinse7693a32008-01-25 21:08:09 +0100155#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +0100156static int find_lowest_rq(struct task_struct *task);
157
Gregory Haskinse7693a32008-01-25 21:08:09 +0100158static int select_task_rq_rt(struct task_struct *p, int sync)
159{
Gregory Haskins318e0892008-01-25 21:08:10 +0100160 struct rq *rq = task_rq(p);
161
162 /*
Steven Rostedte1f47d82008-01-25 21:08:12 +0100163 * 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.
Gregory Haskins318e0892008-01-25 21:08:10 +0100178 */
Gregory Haskins17b32792008-01-25 21:08:13 +0100179 if (unlikely(rt_task(rq->curr)) &&
180 (p->nr_cpus_allowed > 1)) {
Gregory Haskins318e0892008-01-25 21:08:10 +0100181 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 */
Gregory Haskinse7693a32008-01-25 21:08:09 +0100190 return task_cpu(p);
191}
192#endif /* CONFIG_SMP */
193
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200194/*
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
Ingo Molnarfb8d4722007-08-09 11:16:48 +0200203static struct task_struct *pick_next_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200204{
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
Ingo Molnard2819182007-08-09 11:16:47 +0200217 next->se.exec_start = rq->clock;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200218
219 return next;
220}
221
Ingo Molnar31ee5292007-08-09 11:16:49 +0200222static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200223{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200224 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200225 p->se.exec_start = 0;
226}
227
Peter Williams681f3e62007-10-24 18:23:51 +0200228#ifdef CONFIG_SMP
Steven Rostedte8fa1362008-01-25 21:08:05 +0100229/* 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
Steven Rostedtf65eda42008-01-25 21:08:07 +0100235static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
236{
237 if (!task_running(rq, p) &&
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100238 (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
239 (p->nr_cpus_allowed > 1))
Steven Rostedtf65eda42008-01-25 21:08:07 +0100240 return 1;
241 return 0;
242}
243
Steven Rostedte8fa1362008-01-25 21:08:05 +0100244/* Return the second highest RT task, NULL otherwise */
Steven Rostedtf65eda42008-01-25 21:08:07 +0100245static struct task_struct *pick_next_highest_task_rt(struct rq *rq,
246 int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100247{
248 struct rt_prio_array *array = &rq->rt.active;
249 struct task_struct *next;
250 struct list_head *queue;
251 int idx;
252
253 assert_spin_locked(&rq->lock);
254
255 if (likely(rq->rt.rt_nr_running < 2))
256 return NULL;
257
258 idx = sched_find_first_bit(array->bitmap);
259 if (unlikely(idx >= MAX_RT_PRIO)) {
260 WARN_ON(1); /* rt_nr_running is bad */
261 return NULL;
262 }
263
264 queue = array->queue + idx;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100265 BUG_ON(list_empty(queue));
266
Steven Rostedte8fa1362008-01-25 21:08:05 +0100267 next = list_entry(queue->next, struct task_struct, run_list);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100268 if (unlikely(pick_rt_task(rq, next, cpu)))
269 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100270
271 if (queue->next->next != queue) {
272 /* same prio task */
273 next = list_entry(queue->next->next, struct task_struct, run_list);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100274 if (pick_rt_task(rq, next, cpu))
275 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100276 }
277
Steven Rostedtf65eda42008-01-25 21:08:07 +0100278 retry:
Steven Rostedte8fa1362008-01-25 21:08:05 +0100279 /* slower, but more flexible */
280 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100281 if (unlikely(idx >= MAX_RT_PRIO))
Steven Rostedte8fa1362008-01-25 21:08:05 +0100282 return NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100283
284 queue = array->queue + idx;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100285 BUG_ON(list_empty(queue));
Steven Rostedte8fa1362008-01-25 21:08:05 +0100286
Steven Rostedtf65eda42008-01-25 21:08:07 +0100287 list_for_each_entry(next, queue, run_list) {
288 if (pick_rt_task(rq, next, cpu))
289 goto out;
290 }
291
292 goto retry;
293
294 out:
Steven Rostedte8fa1362008-01-25 21:08:05 +0100295 return next;
296}
297
298static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100299static DEFINE_PER_CPU(cpumask_t, valid_cpu_mask);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100300
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100301static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
Gregory Haskins07b40322008-01-25 21:08:10 +0100302{
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100303 int cpu;
304 cpumask_t *valid_mask = &__get_cpu_var(valid_cpu_mask);
305 int lowest_prio = -1;
Gregory Haskins06f90db2008-01-25 21:08:13 +0100306 int count = 0;
Gregory Haskins07b40322008-01-25 21:08:10 +0100307
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100308 cpus_clear(*lowest_mask);
309 cpus_and(*valid_mask, cpu_online_map, task->cpus_allowed);
Gregory Haskins07b40322008-01-25 21:08:10 +0100310
311 /*
312 * Scan each rq for the lowest prio.
313 */
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100314 for_each_cpu_mask(cpu, *valid_mask) {
Gregory Haskins07b40322008-01-25 21:08:10 +0100315 struct rq *rq = cpu_rq(cpu);
316
Gregory Haskins07b40322008-01-25 21:08:10 +0100317 /* We look for lowest RT prio or non-rt CPU */
318 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
Gregory Haskins06f90db2008-01-25 21:08:13 +0100319 if (count)
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100320 cpus_clear(*lowest_mask);
321 cpu_set(rq->cpu, *lowest_mask);
322 return 1;
Gregory Haskins07b40322008-01-25 21:08:10 +0100323 }
324
325 /* no locking for now */
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100326 if ((rq->rt.highest_prio > task->prio)
327 && (rq->rt.highest_prio >= lowest_prio)) {
328 if (rq->rt.highest_prio > lowest_prio) {
329 /* new low - clear old data */
330 lowest_prio = rq->rt.highest_prio;
Gregory Haskins06f90db2008-01-25 21:08:13 +0100331 if (count) {
332 cpus_clear(*lowest_mask);
333 count = 0;
334 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100335 }
336 cpu_set(rq->cpu, *lowest_mask);
Gregory Haskins06f90db2008-01-25 21:08:13 +0100337 count++;
Gregory Haskins07b40322008-01-25 21:08:10 +0100338 }
339 }
340
Gregory Haskins06f90db2008-01-25 21:08:13 +0100341 return count;
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100342}
343
344static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
345{
346 int first;
347
348 /* "this_cpu" is cheaper to preempt than a remote processor */
349 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
350 return this_cpu;
351
352 first = first_cpu(*mask);
353 if (first != NR_CPUS)
354 return first;
355
356 return -1;
357}
358
359static int find_lowest_rq(struct task_struct *task)
360{
361 struct sched_domain *sd;
362 cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
363 int this_cpu = smp_processor_id();
364 int cpu = task_cpu(task);
Gregory Haskins06f90db2008-01-25 21:08:13 +0100365 int count = find_lowest_cpus(task, lowest_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100366
Gregory Haskins06f90db2008-01-25 21:08:13 +0100367 if (!count)
368 return -1; /* No targets found */
369
370 /*
371 * There is no sense in performing an optimal search if only one
372 * target is found.
373 */
374 if (count == 1)
375 return first_cpu(*lowest_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100376
377 /*
378 * At this point we have built a mask of cpus representing the
379 * lowest priority tasks in the system. Now we want to elect
380 * the best one based on our affinity and topology.
381 *
382 * We prioritize the last cpu that the task executed on since
383 * it is most likely cache-hot in that location.
384 */
385 if (cpu_isset(cpu, *lowest_mask))
386 return cpu;
387
388 /*
389 * Otherwise, we consult the sched_domains span maps to figure
390 * out which cpu is logically closest to our hot cache data.
391 */
392 if (this_cpu == cpu)
393 this_cpu = -1; /* Skip this_cpu opt if the same */
394
395 for_each_domain(cpu, sd) {
396 if (sd->flags & SD_WAKE_AFFINE) {
397 cpumask_t domain_mask;
398 int best_cpu;
399
400 cpus_and(domain_mask, sd->span, *lowest_mask);
401
402 best_cpu = pick_optimal_cpu(this_cpu,
403 &domain_mask);
404 if (best_cpu != -1)
405 return best_cpu;
406 }
407 }
408
409 /*
410 * And finally, if there were no matches within the domains
411 * just give the caller *something* to work with from the compatible
412 * locations.
413 */
414 return pick_optimal_cpu(this_cpu, lowest_mask);
Gregory Haskins07b40322008-01-25 21:08:10 +0100415}
416
Steven Rostedte8fa1362008-01-25 21:08:05 +0100417/* Will lock the rq it finds */
418static struct rq *find_lock_lowest_rq(struct task_struct *task,
Gregory Haskins07b40322008-01-25 21:08:10 +0100419 struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100420{
421 struct rq *lowest_rq = NULL;
422 int cpu;
423 int tries;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100424
425 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +0100426 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100427
Gregory Haskins2de0b462008-01-25 21:08:10 +0100428 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +0100429 break;
430
Gregory Haskins07b40322008-01-25 21:08:10 +0100431 lowest_rq = cpu_rq(cpu);
432
Steven Rostedte8fa1362008-01-25 21:08:05 +0100433 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +0100434 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +0100435 /*
436 * We had to unlock the run queue. In
437 * the mean time, task could have
438 * migrated already or had its affinity changed.
439 * Also make sure that it wasn't scheduled on its rq.
440 */
Gregory Haskins07b40322008-01-25 21:08:10 +0100441 if (unlikely(task_rq(task) != rq ||
Steven Rostedte8fa1362008-01-25 21:08:05 +0100442 !cpu_isset(lowest_rq->cpu, task->cpus_allowed) ||
Gregory Haskins07b40322008-01-25 21:08:10 +0100443 task_running(rq, task) ||
Steven Rostedte8fa1362008-01-25 21:08:05 +0100444 !task->se.on_rq)) {
445 spin_unlock(&lowest_rq->lock);
446 lowest_rq = NULL;
447 break;
448 }
449 }
450
451 /* If this rq is still suitable use it. */
452 if (lowest_rq->rt.highest_prio > task->prio)
453 break;
454
455 /* try again */
456 spin_unlock(&lowest_rq->lock);
457 lowest_rq = NULL;
458 }
459
460 return lowest_rq;
461}
462
463/*
464 * If the current CPU has more than one RT task, see if the non
465 * running task can migrate over to a CPU that is running a task
466 * of lesser priority.
467 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100468static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100469{
470 struct task_struct *next_task;
471 struct rq *lowest_rq;
472 int ret = 0;
473 int paranoid = RT_MAX_TRIES;
474
Gregory Haskins697f0a42008-01-25 21:08:09 +0100475 assert_spin_locked(&rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100476
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100477 if (!rq->rt.overloaded)
478 return 0;
479
Gregory Haskins697f0a42008-01-25 21:08:09 +0100480 next_task = pick_next_highest_task_rt(rq, -1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100481 if (!next_task)
482 return 0;
483
484 retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +0100485 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +0100486 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100487 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100488 }
Steven Rostedte8fa1362008-01-25 21:08:05 +0100489
490 /*
491 * It's possible that the next_task slipped in of
492 * higher priority than current. If that's the case
493 * just reschedule current.
494 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100495 if (unlikely(next_task->prio < rq->curr->prio)) {
496 resched_task(rq->curr);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100497 return 0;
498 }
499
Gregory Haskins697f0a42008-01-25 21:08:09 +0100500 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +0100501 get_task_struct(next_task);
502
503 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100504 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100505 if (!lowest_rq) {
506 struct task_struct *task;
507 /*
Gregory Haskins697f0a42008-01-25 21:08:09 +0100508 * find lock_lowest_rq releases rq->lock
Steven Rostedte8fa1362008-01-25 21:08:05 +0100509 * so it is possible that next_task has changed.
510 * If it has, then try again.
511 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100512 task = pick_next_highest_task_rt(rq, -1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100513 if (unlikely(task != next_task) && task && paranoid--) {
514 put_task_struct(next_task);
515 next_task = task;
516 goto retry;
517 }
518 goto out;
519 }
520
521 assert_spin_locked(&lowest_rq->lock);
522
Gregory Haskins697f0a42008-01-25 21:08:09 +0100523 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100524 set_task_cpu(next_task, lowest_rq->cpu);
525 activate_task(lowest_rq, next_task, 0);
526
527 resched_task(lowest_rq->curr);
528
529 spin_unlock(&lowest_rq->lock);
530
531 ret = 1;
532out:
533 put_task_struct(next_task);
534
535 return ret;
536}
537
538/*
539 * TODO: Currently we just use the second highest prio task on
540 * the queue, and stop when it can't migrate (or there's
541 * no more RT tasks). There may be a case where a lower
542 * priority RT task has a different affinity than the
543 * higher RT task. In this case the lower RT task could
544 * possibly be able to migrate where as the higher priority
545 * RT task could not. We currently ignore this issue.
546 * Enhancements are welcome!
547 */
548static void push_rt_tasks(struct rq *rq)
549{
550 /* push_rt_task will return true if it moved an RT */
551 while (push_rt_task(rq))
552 ;
553}
554
Steven Rostedtf65eda42008-01-25 21:08:07 +0100555static int pull_rt_task(struct rq *this_rq)
556{
557 struct task_struct *next;
558 struct task_struct *p;
559 struct rq *src_rq;
560 cpumask_t *rto_cpumask;
561 int this_cpu = this_rq->cpu;
562 int cpu;
563 int ret = 0;
564
565 assert_spin_locked(&this_rq->lock);
566
567 /*
568 * If cpusets are used, and we have overlapping
569 * run queue cpusets, then this algorithm may not catch all.
570 * This is just the price you pay on trying to keep
571 * dirtying caches down on large SMP machines.
572 */
573 if (likely(!rt_overloaded()))
574 return 0;
575
576 next = pick_next_task_rt(this_rq);
577
578 rto_cpumask = rt_overload();
579
580 for_each_cpu_mask(cpu, *rto_cpumask) {
581 if (this_cpu == cpu)
582 continue;
583
584 src_rq = cpu_rq(cpu);
585 if (unlikely(src_rq->rt.rt_nr_running <= 1)) {
586 /*
587 * It is possible that overlapping cpusets
588 * will miss clearing a non overloaded runqueue.
589 * Clear it now.
590 */
591 if (double_lock_balance(this_rq, src_rq)) {
592 /* unlocked our runqueue lock */
593 struct task_struct *old_next = next;
594 next = pick_next_task_rt(this_rq);
595 if (next != old_next)
596 ret = 1;
597 }
598 if (likely(src_rq->rt.rt_nr_running <= 1))
599 /*
600 * Small chance that this_rq->curr changed
601 * but it's really harmless here.
602 */
603 rt_clear_overload(this_rq);
604 else
605 /*
606 * Heh, the src_rq is now overloaded, since
607 * we already have the src_rq lock, go straight
608 * to pulling tasks from it.
609 */
610 goto try_pulling;
611 spin_unlock(&src_rq->lock);
612 continue;
613 }
614
615 /*
616 * We can potentially drop this_rq's lock in
617 * double_lock_balance, and another CPU could
618 * steal our next task - hence we must cause
619 * the caller to recalculate the next task
620 * in that case:
621 */
622 if (double_lock_balance(this_rq, src_rq)) {
623 struct task_struct *old_next = next;
624 next = pick_next_task_rt(this_rq);
625 if (next != old_next)
626 ret = 1;
627 }
628
629 /*
630 * Are there still pullable RT tasks?
631 */
632 if (src_rq->rt.rt_nr_running <= 1) {
633 spin_unlock(&src_rq->lock);
634 continue;
635 }
636
637 try_pulling:
638 p = pick_next_highest_task_rt(src_rq, this_cpu);
639
640 /*
641 * Do we have an RT task that preempts
642 * the to-be-scheduled task?
643 */
644 if (p && (!next || (p->prio < next->prio))) {
645 WARN_ON(p == src_rq->curr);
646 WARN_ON(!p->se.on_rq);
647
648 /*
649 * There's a chance that p is higher in priority
650 * than what's currently running on its cpu.
651 * This is just that p is wakeing up and hasn't
652 * had a chance to schedule. We only pull
653 * p if it is lower in priority than the
654 * current task on the run queue or
655 * this_rq next task is lower in prio than
656 * the current task on that rq.
657 */
658 if (p->prio < src_rq->curr->prio ||
659 (next && next->prio < src_rq->curr->prio))
660 goto bail;
661
662 ret = 1;
663
664 deactivate_task(src_rq, p, 0);
665 set_task_cpu(p, this_cpu);
666 activate_task(this_rq, p, 0);
667 /*
668 * We continue with the search, just in
669 * case there's an even higher prio task
670 * in another runqueue. (low likelyhood
671 * but possible)
672 */
673
674 /*
675 * Update next so that we won't pick a task
676 * on another cpu with a priority lower (or equal)
677 * than the one we just picked.
678 */
679 next = p;
680
681 }
682 bail:
683 spin_unlock(&src_rq->lock);
684 }
685
686 return ret;
687}
688
689static void schedule_balance_rt(struct rq *rq,
690 struct task_struct *prev)
691{
692 /* Try to pull RT tasks here if we lower this rq's prio */
693 if (unlikely(rt_task(prev)) &&
694 rq->rt.highest_prio > prev->prio)
695 pull_rt_task(rq);
696}
697
Steven Rostedte8fa1362008-01-25 21:08:05 +0100698static void schedule_tail_balance_rt(struct rq *rq)
699{
700 /*
701 * If we have more than one rt_task queued, then
702 * see if we can push the other rt_tasks off to other CPUS.
703 * Note we may release the rq lock, and since
704 * the lock was owned by prev, we need to release it
705 * first via finish_lock_switch and then reaquire it here.
706 */
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100707 if (unlikely(rq->rt.overloaded)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +0100708 spin_lock_irq(&rq->lock);
709 push_rt_tasks(rq);
710 spin_unlock_irq(&rq->lock);
711 }
712}
713
Steven Rostedt4642daf2008-01-25 21:08:07 +0100714
715static void wakeup_balance_rt(struct rq *rq, struct task_struct *p)
716{
717 if (unlikely(rt_task(p)) &&
718 !task_running(rq, p) &&
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100719 (p->prio >= rq->rt.highest_prio) &&
720 rq->rt.overloaded)
Steven Rostedt4642daf2008-01-25 21:08:07 +0100721 push_rt_tasks(rq);
722}
723
Peter Williams43010652007-08-09 11:16:46 +0200724static unsigned long
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200725load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamse1d14842007-10-24 18:23:51 +0200726 unsigned long max_load_move,
727 struct sched_domain *sd, enum cpu_idle_type idle,
728 int *all_pinned, int *this_best_prio)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200729{
Steven Rostedtc7a1e462008-01-25 21:08:07 +0100730 /* don't touch RT tasks */
731 return 0;
Peter Williamse1d14842007-10-24 18:23:51 +0200732}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200733
Peter Williamse1d14842007-10-24 18:23:51 +0200734static int
735move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
736 struct sched_domain *sd, enum cpu_idle_type idle)
737{
Steven Rostedtc7a1e462008-01-25 21:08:07 +0100738 /* don't touch RT tasks */
739 return 0;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200740}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100741static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
742{
743 int weight = cpus_weight(*new_mask);
744
745 BUG_ON(!rt_task(p));
746
747 /*
748 * Update the migration status of the RQ if we have an RT task
749 * which is running AND changing its weight value.
750 */
751 if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
752 struct rq *rq = task_rq(p);
753
754 if ((p->nr_cpus_allowed <= 1) && (weight > 1))
755 rq->rt.rt_nr_migratory++;
756 else if((p->nr_cpus_allowed > 1) && (weight <= 1)) {
757 BUG_ON(!rq->rt.rt_nr_migratory);
758 rq->rt.rt_nr_migratory--;
759 }
760
761 update_rt_migration(rq);
762 }
763
764 p->cpus_allowed = *new_mask;
765 p->nr_cpus_allowed = weight;
766}
Steven Rostedte8fa1362008-01-25 21:08:05 +0100767#else /* CONFIG_SMP */
768# define schedule_tail_balance_rt(rq) do { } while (0)
Steven Rostedtf65eda42008-01-25 21:08:07 +0100769# define schedule_balance_rt(rq, prev) do { } while (0)
Steven Rostedt4642daf2008-01-25 21:08:07 +0100770# define wakeup_balance_rt(rq, p) do { } while (0)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100771#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200772
773static void task_tick_rt(struct rq *rq, struct task_struct *p)
774{
Peter Zijlstra67e2be02007-12-20 15:01:17 +0100775 update_curr_rt(rq);
776
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200777 /*
778 * RR tasks need a special form of timeslice management.
779 * FIFO tasks have no timeslices.
780 */
781 if (p->policy != SCHED_RR)
782 return;
783
784 if (--p->time_slice)
785 return;
786
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200787 p->time_slice = DEF_TIMESLICE;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200788
Dmitry Adamushko98fbc792007-08-24 20:39:10 +0200789 /*
790 * Requeue to the end of queue if we are not the only element
791 * on the queue:
792 */
793 if (p->run_list.prev != p->run_list.next) {
794 requeue_task_rt(rq, p);
795 set_tsk_need_resched(p);
796 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200797}
798
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200799static void set_curr_task_rt(struct rq *rq)
800{
801 struct task_struct *p = rq->curr;
802
803 p->se.exec_start = rq->clock;
804}
805
Ingo Molnar5522d5d2007-10-15 17:00:12 +0200806const struct sched_class rt_sched_class = {
807 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200808 .enqueue_task = enqueue_task_rt,
809 .dequeue_task = dequeue_task_rt,
810 .yield_task = yield_task_rt,
Gregory Haskinse7693a32008-01-25 21:08:09 +0100811#ifdef CONFIG_SMP
812 .select_task_rq = select_task_rq_rt,
813#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200814
815 .check_preempt_curr = check_preempt_curr_rt,
816
817 .pick_next_task = pick_next_task_rt,
818 .put_prev_task = put_prev_task_rt,
819
Peter Williams681f3e62007-10-24 18:23:51 +0200820#ifdef CONFIG_SMP
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200821 .load_balance = load_balance_rt,
Peter Williamse1d14842007-10-24 18:23:51 +0200822 .move_one_task = move_one_task_rt,
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100823 .set_cpus_allowed = set_cpus_allowed_rt,
Peter Williams681f3e62007-10-24 18:23:51 +0200824#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200825
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200826 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200827 .task_tick = task_tick_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200828};