blob: deff0c77d7059909f1744629e98980e5662e2768 [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
Ingo Molnar84de4272008-01-25 21:08:15 +01007
8/*
9 * The "RT overload" flag: it gets set if a CPU has more than
10 * one runnable RT task.
11 */
Steven Rostedt4fd29172008-01-25 21:08:06 +010012static cpumask_t rt_overload_mask;
13static atomic_t rto_count;
Ingo Molnar84de4272008-01-25 21:08:15 +010014
Steven Rostedt4fd29172008-01-25 21:08:06 +010015static inline int rt_overloaded(void)
16{
17 return atomic_read(&rto_count);
18}
Ingo Molnar84de4272008-01-25 21:08:15 +010019
Steven Rostedt4fd29172008-01-25 21:08:06 +010020static inline void rt_set_overload(struct rq *rq)
21{
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +010022 rq->rt.overloaded = 1;
Steven Rostedt4fd29172008-01-25 21:08:06 +010023 cpu_set(rq->cpu, rt_overload_mask);
24 /*
25 * Make sure the mask is visible before we set
26 * the overload count. That is checked to determine
27 * if we should look at the mask. It would be a shame
28 * if we looked at the mask, but the mask was not
29 * updated yet.
30 */
31 wmb();
32 atomic_inc(&rto_count);
33}
Ingo Molnar84de4272008-01-25 21:08:15 +010034
Steven Rostedt4fd29172008-01-25 21:08:06 +010035static inline void rt_clear_overload(struct rq *rq)
36{
37 /* the order here really doesn't matter */
38 atomic_dec(&rto_count);
39 cpu_clear(rq->cpu, rt_overload_mask);
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +010040 rq->rt.overloaded = 0;
Steven Rostedt4fd29172008-01-25 21:08:06 +010041}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010042
43static void update_rt_migration(struct rq *rq)
44{
45 if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1))
46 rt_set_overload(rq);
47 else
48 rt_clear_overload(rq);
49}
Steven Rostedt4fd29172008-01-25 21:08:06 +010050#endif /* CONFIG_SMP */
51
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020052/*
53 * Update the current task's runtime statistics. Skip current tasks that
54 * are not in our scheduling class.
55 */
Alexey Dobriyana9957442007-10-15 17:00:13 +020056static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020057{
58 struct task_struct *curr = rq->curr;
59 u64 delta_exec;
60
61 if (!task_has_rt_policy(curr))
62 return;
63
Ingo Molnard2819182007-08-09 11:16:47 +020064 delta_exec = rq->clock - curr->se.exec_start;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020065 if (unlikely((s64)delta_exec < 0))
66 delta_exec = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +020067
68 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020069
70 curr->se.sum_exec_runtime += delta_exec;
Ingo Molnard2819182007-08-09 11:16:47 +020071 curr->se.exec_start = rq->clock;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010072 cpuacct_charge(curr, delta_exec);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020073}
74
Steven Rostedt63489e42008-01-25 21:08:03 +010075static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq)
76{
77 WARN_ON(!rt_task(p));
78 rq->rt.rt_nr_running++;
Steven Rostedt764a9d62008-01-25 21:08:04 +010079#ifdef CONFIG_SMP
80 if (p->prio < rq->rt.highest_prio)
81 rq->rt.highest_prio = p->prio;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010082 if (p->nr_cpus_allowed > 1)
83 rq->rt.rt_nr_migratory++;
84
85 update_rt_migration(rq);
Steven Rostedt764a9d62008-01-25 21:08:04 +010086#endif /* CONFIG_SMP */
Steven Rostedt63489e42008-01-25 21:08:03 +010087}
88
89static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq)
90{
91 WARN_ON(!rt_task(p));
92 WARN_ON(!rq->rt.rt_nr_running);
93 rq->rt.rt_nr_running--;
Steven Rostedt764a9d62008-01-25 21:08:04 +010094#ifdef CONFIG_SMP
95 if (rq->rt.rt_nr_running) {
96 struct rt_prio_array *array;
97
98 WARN_ON(p->prio < rq->rt.highest_prio);
99 if (p->prio == rq->rt.highest_prio) {
100 /* recalculate */
101 array = &rq->rt.active;
102 rq->rt.highest_prio =
103 sched_find_first_bit(array->bitmap);
104 } /* otherwise leave rq->highest prio alone */
105 } else
106 rq->rt.highest_prio = MAX_RT_PRIO;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100107 if (p->nr_cpus_allowed > 1)
108 rq->rt.rt_nr_migratory--;
109
110 update_rt_migration(rq);
Steven Rostedt764a9d62008-01-25 21:08:04 +0100111#endif /* CONFIG_SMP */
Steven Rostedt63489e42008-01-25 21:08:03 +0100112}
113
Ingo Molnarfd390f62007-08-09 11:16:48 +0200114static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200115{
116 struct rt_prio_array *array = &rq->rt.active;
117
118 list_add_tail(&p->run_list, array->queue + p->prio);
119 __set_bit(p->prio, array->bitmap);
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +0100120 inc_cpu_load(rq, p->se.load.weight);
Steven Rostedt63489e42008-01-25 21:08:03 +0100121
122 inc_rt_tasks(p, rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200123}
124
125/*
126 * Adding/removing a task to/from a priority array:
127 */
Ingo Molnarf02231e2007-08-09 11:16:48 +0200128static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200129{
130 struct rt_prio_array *array = &rq->rt.active;
131
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200132 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200133
134 list_del(&p->run_list);
135 if (list_empty(array->queue + p->prio))
136 __clear_bit(p->prio, array->bitmap);
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +0100137 dec_cpu_load(rq, p->se.load.weight);
Steven Rostedt63489e42008-01-25 21:08:03 +0100138
139 dec_rt_tasks(p, rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200140}
141
142/*
143 * Put task to the end of the run list without the overhead of dequeue
144 * followed by enqueue.
145 */
146static void requeue_task_rt(struct rq *rq, struct task_struct *p)
147{
148 struct rt_prio_array *array = &rq->rt.active;
149
150 list_move_tail(&p->run_list, array->queue + p->prio);
151}
152
153static void
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200154yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200155{
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200156 requeue_task_rt(rq, rq->curr);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200157}
158
Gregory Haskinse7693a32008-01-25 21:08:09 +0100159#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +0100160static int find_lowest_rq(struct task_struct *task);
161
Gregory Haskinse7693a32008-01-25 21:08:09 +0100162static int select_task_rq_rt(struct task_struct *p, int sync)
163{
Gregory Haskins318e0892008-01-25 21:08:10 +0100164 struct rq *rq = task_rq(p);
165
166 /*
Steven Rostedte1f47d82008-01-25 21:08:12 +0100167 * If the current task is an RT task, then
168 * try to see if we can wake this RT task up on another
169 * runqueue. Otherwise simply start this RT task
170 * on its current runqueue.
171 *
172 * We want to avoid overloading runqueues. Even if
173 * the RT task is of higher priority than the current RT task.
174 * RT tasks behave differently than other tasks. If
175 * one gets preempted, we try to push it off to another queue.
176 * So trying to keep a preempting RT task on the same
177 * cache hot CPU will force the running RT task to
178 * a cold CPU. So we waste all the cache for the lower
179 * RT task in hopes of saving some of a RT task
180 * that is just being woken and probably will have
181 * cold cache anyway.
Gregory Haskins318e0892008-01-25 21:08:10 +0100182 */
Gregory Haskins17b32792008-01-25 21:08:13 +0100183 if (unlikely(rt_task(rq->curr)) &&
184 (p->nr_cpus_allowed > 1)) {
Gregory Haskins318e0892008-01-25 21:08:10 +0100185 int cpu = find_lowest_rq(p);
186
187 return (cpu == -1) ? task_cpu(p) : cpu;
188 }
189
190 /*
191 * Otherwise, just let it ride on the affined RQ and the
192 * post-schedule router will push the preempted task away
193 */
Gregory Haskinse7693a32008-01-25 21:08:09 +0100194 return task_cpu(p);
195}
196#endif /* CONFIG_SMP */
197
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200198/*
199 * Preempt the current task with a newly woken task if needed:
200 */
201static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
202{
203 if (p->prio < rq->curr->prio)
204 resched_task(rq->curr);
205}
206
Ingo Molnarfb8d4722007-08-09 11:16:48 +0200207static struct task_struct *pick_next_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200208{
209 struct rt_prio_array *array = &rq->rt.active;
210 struct task_struct *next;
211 struct list_head *queue;
212 int idx;
213
214 idx = sched_find_first_bit(array->bitmap);
215 if (idx >= MAX_RT_PRIO)
216 return NULL;
217
218 queue = array->queue + idx;
219 next = list_entry(queue->next, struct task_struct, run_list);
220
Ingo Molnard2819182007-08-09 11:16:47 +0200221 next->se.exec_start = rq->clock;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200222
223 return next;
224}
225
Ingo Molnar31ee5292007-08-09 11:16:49 +0200226static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200227{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200228 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200229 p->se.exec_start = 0;
230}
231
Peter Williams681f3e62007-10-24 18:23:51 +0200232#ifdef CONFIG_SMP
Steven Rostedte8fa1362008-01-25 21:08:05 +0100233/* Only try algorithms three times */
234#define RT_MAX_TRIES 3
235
236static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
237static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
238
Steven Rostedtf65eda42008-01-25 21:08:07 +0100239static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
240{
241 if (!task_running(rq, p) &&
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100242 (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
243 (p->nr_cpus_allowed > 1))
Steven Rostedtf65eda42008-01-25 21:08:07 +0100244 return 1;
245 return 0;
246}
247
Steven Rostedte8fa1362008-01-25 21:08:05 +0100248/* Return the second highest RT task, NULL otherwise */
Ingo Molnar79064fb2008-01-25 21:08:14 +0100249static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100250{
251 struct rt_prio_array *array = &rq->rt.active;
252 struct task_struct *next;
253 struct list_head *queue;
254 int idx;
255
256 assert_spin_locked(&rq->lock);
257
258 if (likely(rq->rt.rt_nr_running < 2))
259 return NULL;
260
261 idx = sched_find_first_bit(array->bitmap);
262 if (unlikely(idx >= MAX_RT_PRIO)) {
263 WARN_ON(1); /* rt_nr_running is bad */
264 return NULL;
265 }
266
267 queue = array->queue + idx;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100268 BUG_ON(list_empty(queue));
269
Steven Rostedte8fa1362008-01-25 21:08:05 +0100270 next = list_entry(queue->next, struct task_struct, run_list);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100271 if (unlikely(pick_rt_task(rq, next, cpu)))
272 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100273
274 if (queue->next->next != queue) {
275 /* same prio task */
Ingo Molnar79064fb2008-01-25 21:08:14 +0100276 next = list_entry(queue->next->next, struct task_struct,
277 run_list);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100278 if (pick_rt_task(rq, next, cpu))
279 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100280 }
281
Steven Rostedtf65eda42008-01-25 21:08:07 +0100282 retry:
Steven Rostedte8fa1362008-01-25 21:08:05 +0100283 /* slower, but more flexible */
284 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100285 if (unlikely(idx >= MAX_RT_PRIO))
Steven Rostedte8fa1362008-01-25 21:08:05 +0100286 return NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100287
288 queue = array->queue + idx;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100289 BUG_ON(list_empty(queue));
Steven Rostedte8fa1362008-01-25 21:08:05 +0100290
Steven Rostedtf65eda42008-01-25 21:08:07 +0100291 list_for_each_entry(next, queue, run_list) {
292 if (pick_rt_task(rq, next, cpu))
293 goto out;
294 }
295
296 goto retry;
297
298 out:
Steven Rostedte8fa1362008-01-25 21:08:05 +0100299 return next;
300}
301
302static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
303
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100304static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
Gregory Haskins07b40322008-01-25 21:08:10 +0100305{
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100306 int lowest_prio = -1;
Steven Rostedt610bf052008-01-25 21:08:13 +0100307 int lowest_cpu = -1;
Gregory Haskins06f90db2008-01-25 21:08:13 +0100308 int count = 0;
Steven Rostedt610bf052008-01-25 21:08:13 +0100309 int cpu;
Gregory Haskins07b40322008-01-25 21:08:10 +0100310
Steven Rostedt610bf052008-01-25 21:08:13 +0100311 cpus_and(*lowest_mask, cpu_online_map, task->cpus_allowed);
Gregory Haskins07b40322008-01-25 21:08:10 +0100312
313 /*
314 * Scan each rq for the lowest prio.
315 */
Steven Rostedt610bf052008-01-25 21:08:13 +0100316 for_each_cpu_mask(cpu, *lowest_mask) {
Gregory Haskins07b40322008-01-25 21:08:10 +0100317 struct rq *rq = cpu_rq(cpu);
318
Gregory Haskins07b40322008-01-25 21:08:10 +0100319 /* We look for lowest RT prio or non-rt CPU */
320 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
Steven Rostedt610bf052008-01-25 21:08:13 +0100321 /*
322 * if we already found a low RT queue
323 * and now we found this non-rt queue
324 * clear the mask and set our bit.
325 * Otherwise just return the queue as is
326 * and the count==1 will cause the algorithm
327 * to use the first bit found.
328 */
329 if (lowest_cpu != -1) {
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100330 cpus_clear(*lowest_mask);
Steven Rostedt610bf052008-01-25 21:08:13 +0100331 cpu_set(rq->cpu, *lowest_mask);
332 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100333 return 1;
Gregory Haskins07b40322008-01-25 21:08:10 +0100334 }
335
336 /* no locking for now */
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100337 if ((rq->rt.highest_prio > task->prio)
338 && (rq->rt.highest_prio >= lowest_prio)) {
339 if (rq->rt.highest_prio > lowest_prio) {
340 /* new low - clear old data */
341 lowest_prio = rq->rt.highest_prio;
Steven Rostedt610bf052008-01-25 21:08:13 +0100342 lowest_cpu = cpu;
343 count = 0;
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100344 }
Gregory Haskins06f90db2008-01-25 21:08:13 +0100345 count++;
Steven Rostedt610bf052008-01-25 21:08:13 +0100346 } else
347 cpu_clear(cpu, *lowest_mask);
348 }
349
350 /*
351 * Clear out all the set bits that represent
352 * runqueues that were of higher prio than
353 * the lowest_prio.
354 */
355 if (lowest_cpu > 0) {
356 /*
357 * Perhaps we could add another cpumask op to
358 * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
359 * Then that could be optimized to use memset and such.
360 */
361 for_each_cpu_mask(cpu, *lowest_mask) {
362 if (cpu >= lowest_cpu)
363 break;
364 cpu_clear(cpu, *lowest_mask);
Gregory Haskins07b40322008-01-25 21:08:10 +0100365 }
366 }
367
Gregory Haskins06f90db2008-01-25 21:08:13 +0100368 return count;
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100369}
370
371static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
372{
373 int first;
374
375 /* "this_cpu" is cheaper to preempt than a remote processor */
376 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
377 return this_cpu;
378
379 first = first_cpu(*mask);
380 if (first != NR_CPUS)
381 return first;
382
383 return -1;
384}
385
386static int find_lowest_rq(struct task_struct *task)
387{
388 struct sched_domain *sd;
389 cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
390 int this_cpu = smp_processor_id();
391 int cpu = task_cpu(task);
Gregory Haskins06f90db2008-01-25 21:08:13 +0100392 int count = find_lowest_cpus(task, lowest_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100393
Gregory Haskins06f90db2008-01-25 21:08:13 +0100394 if (!count)
395 return -1; /* No targets found */
396
397 /*
398 * There is no sense in performing an optimal search if only one
399 * target is found.
400 */
401 if (count == 1)
402 return first_cpu(*lowest_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100403
404 /*
405 * At this point we have built a mask of cpus representing the
406 * lowest priority tasks in the system. Now we want to elect
407 * the best one based on our affinity and topology.
408 *
409 * We prioritize the last cpu that the task executed on since
410 * it is most likely cache-hot in that location.
411 */
412 if (cpu_isset(cpu, *lowest_mask))
413 return cpu;
414
415 /*
416 * Otherwise, we consult the sched_domains span maps to figure
417 * out which cpu is logically closest to our hot cache data.
418 */
419 if (this_cpu == cpu)
420 this_cpu = -1; /* Skip this_cpu opt if the same */
421
422 for_each_domain(cpu, sd) {
423 if (sd->flags & SD_WAKE_AFFINE) {
424 cpumask_t domain_mask;
425 int best_cpu;
426
427 cpus_and(domain_mask, sd->span, *lowest_mask);
428
429 best_cpu = pick_optimal_cpu(this_cpu,
430 &domain_mask);
431 if (best_cpu != -1)
432 return best_cpu;
433 }
434 }
435
436 /*
437 * And finally, if there were no matches within the domains
438 * just give the caller *something* to work with from the compatible
439 * locations.
440 */
441 return pick_optimal_cpu(this_cpu, lowest_mask);
Gregory Haskins07b40322008-01-25 21:08:10 +0100442}
443
Steven Rostedte8fa1362008-01-25 21:08:05 +0100444/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +0100445static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100446{
447 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100448 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +0100449 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100450
451 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +0100452 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100453
Gregory Haskins2de0b462008-01-25 21:08:10 +0100454 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +0100455 break;
456
Gregory Haskins07b40322008-01-25 21:08:10 +0100457 lowest_rq = cpu_rq(cpu);
458
Steven Rostedte8fa1362008-01-25 21:08:05 +0100459 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +0100460 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +0100461 /*
462 * We had to unlock the run queue. In
463 * the mean time, task could have
464 * migrated already or had its affinity changed.
465 * Also make sure that it wasn't scheduled on its rq.
466 */
Gregory Haskins07b40322008-01-25 21:08:10 +0100467 if (unlikely(task_rq(task) != rq ||
Ingo Molnar4df64c02008-01-25 21:08:15 +0100468 !cpu_isset(lowest_rq->cpu,
469 task->cpus_allowed) ||
Gregory Haskins07b40322008-01-25 21:08:10 +0100470 task_running(rq, task) ||
Steven Rostedte8fa1362008-01-25 21:08:05 +0100471 !task->se.on_rq)) {
Ingo Molnar4df64c02008-01-25 21:08:15 +0100472
Steven Rostedte8fa1362008-01-25 21:08:05 +0100473 spin_unlock(&lowest_rq->lock);
474 lowest_rq = NULL;
475 break;
476 }
477 }
478
479 /* If this rq is still suitable use it. */
480 if (lowest_rq->rt.highest_prio > task->prio)
481 break;
482
483 /* try again */
484 spin_unlock(&lowest_rq->lock);
485 lowest_rq = NULL;
486 }
487
488 return lowest_rq;
489}
490
491/*
492 * If the current CPU has more than one RT task, see if the non
493 * running task can migrate over to a CPU that is running a task
494 * of lesser priority.
495 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100496static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100497{
498 struct task_struct *next_task;
499 struct rq *lowest_rq;
500 int ret = 0;
501 int paranoid = RT_MAX_TRIES;
502
Gregory Haskins697f0a42008-01-25 21:08:09 +0100503 assert_spin_locked(&rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100504
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100505 if (!rq->rt.overloaded)
506 return 0;
507
Gregory Haskins697f0a42008-01-25 21:08:09 +0100508 next_task = pick_next_highest_task_rt(rq, -1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100509 if (!next_task)
510 return 0;
511
512 retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +0100513 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +0100514 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100515 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100516 }
Steven Rostedte8fa1362008-01-25 21:08:05 +0100517
518 /*
519 * It's possible that the next_task slipped in of
520 * higher priority than current. If that's the case
521 * just reschedule current.
522 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100523 if (unlikely(next_task->prio < rq->curr->prio)) {
524 resched_task(rq->curr);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100525 return 0;
526 }
527
Gregory Haskins697f0a42008-01-25 21:08:09 +0100528 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +0100529 get_task_struct(next_task);
530
531 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100532 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100533 if (!lowest_rq) {
534 struct task_struct *task;
535 /*
Gregory Haskins697f0a42008-01-25 21:08:09 +0100536 * find lock_lowest_rq releases rq->lock
Steven Rostedte8fa1362008-01-25 21:08:05 +0100537 * so it is possible that next_task has changed.
538 * If it has, then try again.
539 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100540 task = pick_next_highest_task_rt(rq, -1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100541 if (unlikely(task != next_task) && task && paranoid--) {
542 put_task_struct(next_task);
543 next_task = task;
544 goto retry;
545 }
546 goto out;
547 }
548
549 assert_spin_locked(&lowest_rq->lock);
550
Gregory Haskins697f0a42008-01-25 21:08:09 +0100551 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100552 set_task_cpu(next_task, lowest_rq->cpu);
553 activate_task(lowest_rq, next_task, 0);
554
555 resched_task(lowest_rq->curr);
556
557 spin_unlock(&lowest_rq->lock);
558
559 ret = 1;
560out:
561 put_task_struct(next_task);
562
563 return ret;
564}
565
566/*
567 * TODO: Currently we just use the second highest prio task on
568 * the queue, and stop when it can't migrate (or there's
569 * no more RT tasks). There may be a case where a lower
570 * priority RT task has a different affinity than the
571 * higher RT task. In this case the lower RT task could
572 * possibly be able to migrate where as the higher priority
573 * RT task could not. We currently ignore this issue.
574 * Enhancements are welcome!
575 */
576static void push_rt_tasks(struct rq *rq)
577{
578 /* push_rt_task will return true if it moved an RT */
579 while (push_rt_task(rq))
580 ;
581}
582
Steven Rostedtf65eda42008-01-25 21:08:07 +0100583static int pull_rt_task(struct rq *this_rq)
584{
585 struct task_struct *next;
586 struct task_struct *p;
587 struct rq *src_rq;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100588 int this_cpu = this_rq->cpu;
589 int cpu;
590 int ret = 0;
591
592 assert_spin_locked(&this_rq->lock);
593
594 /*
595 * If cpusets are used, and we have overlapping
596 * run queue cpusets, then this algorithm may not catch all.
597 * This is just the price you pay on trying to keep
598 * dirtying caches down on large SMP machines.
599 */
600 if (likely(!rt_overloaded()))
601 return 0;
602
603 next = pick_next_task_rt(this_rq);
604
Ingo Molnar6e1938d2008-01-25 21:08:16 +0100605 for_each_cpu_mask(cpu, rt_overload_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +0100606 if (this_cpu == cpu)
607 continue;
608
609 src_rq = cpu_rq(cpu);
610 if (unlikely(src_rq->rt.rt_nr_running <= 1)) {
611 /*
612 * It is possible that overlapping cpusets
613 * will miss clearing a non overloaded runqueue.
614 * Clear it now.
615 */
616 if (double_lock_balance(this_rq, src_rq)) {
617 /* unlocked our runqueue lock */
618 struct task_struct *old_next = next;
619 next = pick_next_task_rt(this_rq);
620 if (next != old_next)
621 ret = 1;
622 }
623 if (likely(src_rq->rt.rt_nr_running <= 1))
624 /*
625 * Small chance that this_rq->curr changed
626 * but it's really harmless here.
627 */
628 rt_clear_overload(this_rq);
629 else
630 /*
631 * Heh, the src_rq is now overloaded, since
632 * we already have the src_rq lock, go straight
633 * to pulling tasks from it.
634 */
635 goto try_pulling;
636 spin_unlock(&src_rq->lock);
637 continue;
638 }
639
640 /*
641 * We can potentially drop this_rq's lock in
642 * double_lock_balance, and another CPU could
643 * steal our next task - hence we must cause
644 * the caller to recalculate the next task
645 * in that case:
646 */
647 if (double_lock_balance(this_rq, src_rq)) {
648 struct task_struct *old_next = next;
649 next = pick_next_task_rt(this_rq);
650 if (next != old_next)
651 ret = 1;
652 }
653
654 /*
655 * Are there still pullable RT tasks?
656 */
657 if (src_rq->rt.rt_nr_running <= 1) {
658 spin_unlock(&src_rq->lock);
659 continue;
660 }
661
662 try_pulling:
663 p = pick_next_highest_task_rt(src_rq, this_cpu);
664
665 /*
666 * Do we have an RT task that preempts
667 * the to-be-scheduled task?
668 */
669 if (p && (!next || (p->prio < next->prio))) {
670 WARN_ON(p == src_rq->curr);
671 WARN_ON(!p->se.on_rq);
672
673 /*
674 * There's a chance that p is higher in priority
675 * than what's currently running on its cpu.
676 * This is just that p is wakeing up and hasn't
677 * had a chance to schedule. We only pull
678 * p if it is lower in priority than the
679 * current task on the run queue or
680 * this_rq next task is lower in prio than
681 * the current task on that rq.
682 */
683 if (p->prio < src_rq->curr->prio ||
684 (next && next->prio < src_rq->curr->prio))
685 goto bail;
686
687 ret = 1;
688
689 deactivate_task(src_rq, p, 0);
690 set_task_cpu(p, this_cpu);
691 activate_task(this_rq, p, 0);
692 /*
693 * We continue with the search, just in
694 * case there's an even higher prio task
695 * in another runqueue. (low likelyhood
696 * but possible)
697 */
698
699 /*
700 * Update next so that we won't pick a task
701 * on another cpu with a priority lower (or equal)
702 * than the one we just picked.
703 */
704 next = p;
705
706 }
707 bail:
708 spin_unlock(&src_rq->lock);
709 }
710
711 return ret;
712}
713
714static void schedule_balance_rt(struct rq *rq,
715 struct task_struct *prev)
716{
717 /* Try to pull RT tasks here if we lower this rq's prio */
718 if (unlikely(rt_task(prev)) &&
719 rq->rt.highest_prio > prev->prio)
720 pull_rt_task(rq);
721}
722
Steven Rostedte8fa1362008-01-25 21:08:05 +0100723static void schedule_tail_balance_rt(struct rq *rq)
724{
725 /*
726 * If we have more than one rt_task queued, then
727 * see if we can push the other rt_tasks off to other CPUS.
728 * Note we may release the rq lock, and since
729 * the lock was owned by prev, we need to release it
730 * first via finish_lock_switch and then reaquire it here.
731 */
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100732 if (unlikely(rq->rt.overloaded)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +0100733 spin_lock_irq(&rq->lock);
734 push_rt_tasks(rq);
735 spin_unlock_irq(&rq->lock);
736 }
737}
738
Steven Rostedt4642daf2008-01-25 21:08:07 +0100739
740static void wakeup_balance_rt(struct rq *rq, struct task_struct *p)
741{
742 if (unlikely(rt_task(p)) &&
743 !task_running(rq, p) &&
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100744 (p->prio >= rq->rt.highest_prio) &&
745 rq->rt.overloaded)
Steven Rostedt4642daf2008-01-25 21:08:07 +0100746 push_rt_tasks(rq);
747}
748
Peter Williams43010652007-08-09 11:16:46 +0200749static unsigned long
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200750load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamse1d14842007-10-24 18:23:51 +0200751 unsigned long max_load_move,
752 struct sched_domain *sd, enum cpu_idle_type idle,
753 int *all_pinned, int *this_best_prio)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200754{
Steven Rostedtc7a1e462008-01-25 21:08:07 +0100755 /* don't touch RT tasks */
756 return 0;
Peter Williamse1d14842007-10-24 18:23:51 +0200757}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200758
Peter Williamse1d14842007-10-24 18:23:51 +0200759static int
760move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
761 struct sched_domain *sd, enum cpu_idle_type idle)
762{
Steven Rostedtc7a1e462008-01-25 21:08:07 +0100763 /* don't touch RT tasks */
764 return 0;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200765}
Ingo Molnardeeeccd2008-01-25 21:08:15 +0100766
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100767static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
768{
769 int weight = cpus_weight(*new_mask);
770
771 BUG_ON(!rt_task(p));
772
773 /*
774 * Update the migration status of the RQ if we have an RT task
775 * which is running AND changing its weight value.
776 */
777 if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
778 struct rq *rq = task_rq(p);
779
Ingo Molnardeeeccd2008-01-25 21:08:15 +0100780 if ((p->nr_cpus_allowed <= 1) && (weight > 1)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100781 rq->rt.rt_nr_migratory++;
Ingo Molnardeeeccd2008-01-25 21:08:15 +0100782 } else if ((p->nr_cpus_allowed > 1) && (weight <= 1)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100783 BUG_ON(!rq->rt.rt_nr_migratory);
784 rq->rt.rt_nr_migratory--;
785 }
786
787 update_rt_migration(rq);
788 }
789
790 p->cpus_allowed = *new_mask;
791 p->nr_cpus_allowed = weight;
792}
Ingo Molnardeeeccd2008-01-25 21:08:15 +0100793
Steven Rostedte8fa1362008-01-25 21:08:05 +0100794#else /* CONFIG_SMP */
795# define schedule_tail_balance_rt(rq) do { } while (0)
Steven Rostedtf65eda42008-01-25 21:08:07 +0100796# define schedule_balance_rt(rq, prev) do { } while (0)
Steven Rostedt4642daf2008-01-25 21:08:07 +0100797# define wakeup_balance_rt(rq, p) do { } while (0)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100798#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200799
800static void task_tick_rt(struct rq *rq, struct task_struct *p)
801{
Peter Zijlstra67e2be02007-12-20 15:01:17 +0100802 update_curr_rt(rq);
803
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200804 /*
805 * RR tasks need a special form of timeslice management.
806 * FIFO tasks have no timeslices.
807 */
808 if (p->policy != SCHED_RR)
809 return;
810
811 if (--p->time_slice)
812 return;
813
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200814 p->time_slice = DEF_TIMESLICE;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200815
Dmitry Adamushko98fbc792007-08-24 20:39:10 +0200816 /*
817 * Requeue to the end of queue if we are not the only element
818 * on the queue:
819 */
820 if (p->run_list.prev != p->run_list.next) {
821 requeue_task_rt(rq, p);
822 set_tsk_need_resched(p);
823 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200824}
825
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200826static void set_curr_task_rt(struct rq *rq)
827{
828 struct task_struct *p = rq->curr;
829
830 p->se.exec_start = rq->clock;
831}
832
Ingo Molnar5522d5d2007-10-15 17:00:12 +0200833const struct sched_class rt_sched_class = {
834 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200835 .enqueue_task = enqueue_task_rt,
836 .dequeue_task = dequeue_task_rt,
837 .yield_task = yield_task_rt,
Gregory Haskinse7693a32008-01-25 21:08:09 +0100838#ifdef CONFIG_SMP
839 .select_task_rq = select_task_rq_rt,
840#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200841
842 .check_preempt_curr = check_preempt_curr_rt,
843
844 .pick_next_task = pick_next_task_rt,
845 .put_prev_task = put_prev_task_rt,
846
Peter Williams681f3e62007-10-24 18:23:51 +0200847#ifdef CONFIG_SMP
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200848 .load_balance = load_balance_rt,
Peter Williamse1d14842007-10-24 18:23:51 +0200849 .move_one_task = move_one_task_rt,
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100850 .set_cpus_allowed = set_cpus_allowed_rt,
Peter Williams681f3e62007-10-24 18:23:51 +0200851#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200852
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200853 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200854 .task_tick = task_tick_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200855};