blob: a5a45104603a442d2d58742a09089abd4339fa77 [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
Gregory Haskins637f5082008-01-25 21:08:18 +01008static inline int rt_overloaded(struct rq *rq)
Steven Rostedt4fd29172008-01-25 21:08:06 +01009{
Gregory Haskins637f5082008-01-25 21:08:18 +010010 return atomic_read(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +010011}
Ingo Molnar84de4272008-01-25 21:08:15 +010012
Steven Rostedt4fd29172008-01-25 21:08:06 +010013static inline void rt_set_overload(struct rq *rq)
14{
Gregory Haskins637f5082008-01-25 21:08:18 +010015 cpu_set(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +010016 /*
17 * Make sure the mask is visible before we set
18 * the overload count. That is checked to determine
19 * if we should look at the mask. It would be a shame
20 * if we looked at the mask, but the mask was not
21 * updated yet.
22 */
23 wmb();
Gregory Haskins637f5082008-01-25 21:08:18 +010024 atomic_inc(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +010025}
Ingo Molnar84de4272008-01-25 21:08:15 +010026
Steven Rostedt4fd29172008-01-25 21:08:06 +010027static inline void rt_clear_overload(struct rq *rq)
28{
29 /* the order here really doesn't matter */
Gregory Haskins637f5082008-01-25 21:08:18 +010030 atomic_dec(&rq->rd->rto_count);
31 cpu_clear(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +010032}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010033
34static void update_rt_migration(struct rq *rq)
35{
Gregory Haskins637f5082008-01-25 21:08:18 +010036 if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010037 rt_set_overload(rq);
Gregory Haskins637f5082008-01-25 21:08:18 +010038 rq->rt.overloaded = 1;
39 } else {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010040 rt_clear_overload(rq);
Gregory Haskins637f5082008-01-25 21:08:18 +010041 rq->rt.overloaded = 0;
42 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010043}
Steven Rostedt4fd29172008-01-25 21:08:06 +010044#endif /* CONFIG_SMP */
45
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020046/*
47 * Update the current task's runtime statistics. Skip current tasks that
48 * are not in our scheduling class.
49 */
Alexey Dobriyana9957442007-10-15 17:00:13 +020050static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020051{
52 struct task_struct *curr = rq->curr;
53 u64 delta_exec;
54
55 if (!task_has_rt_policy(curr))
56 return;
57
Ingo Molnard2819182007-08-09 11:16:47 +020058 delta_exec = rq->clock - curr->se.exec_start;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020059 if (unlikely((s64)delta_exec < 0))
60 delta_exec = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +020061
62 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020063
64 curr->se.sum_exec_runtime += delta_exec;
Ingo Molnard2819182007-08-09 11:16:47 +020065 curr->se.exec_start = rq->clock;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010066 cpuacct_charge(curr, delta_exec);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020067}
68
Steven Rostedt63489e42008-01-25 21:08:03 +010069static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq)
70{
71 WARN_ON(!rt_task(p));
72 rq->rt.rt_nr_running++;
Steven Rostedt764a9d62008-01-25 21:08:04 +010073#ifdef CONFIG_SMP
74 if (p->prio < rq->rt.highest_prio)
75 rq->rt.highest_prio = p->prio;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010076 if (p->nr_cpus_allowed > 1)
77 rq->rt.rt_nr_migratory++;
78
79 update_rt_migration(rq);
Steven Rostedt764a9d62008-01-25 21:08:04 +010080#endif /* CONFIG_SMP */
Steven Rostedt63489e42008-01-25 21:08:03 +010081}
82
83static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq)
84{
85 WARN_ON(!rt_task(p));
86 WARN_ON(!rq->rt.rt_nr_running);
87 rq->rt.rt_nr_running--;
Steven Rostedt764a9d62008-01-25 21:08:04 +010088#ifdef CONFIG_SMP
89 if (rq->rt.rt_nr_running) {
90 struct rt_prio_array *array;
91
92 WARN_ON(p->prio < rq->rt.highest_prio);
93 if (p->prio == rq->rt.highest_prio) {
94 /* recalculate */
95 array = &rq->rt.active;
96 rq->rt.highest_prio =
97 sched_find_first_bit(array->bitmap);
98 } /* otherwise leave rq->highest prio alone */
99 } else
100 rq->rt.highest_prio = MAX_RT_PRIO;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100101 if (p->nr_cpus_allowed > 1)
102 rq->rt.rt_nr_migratory--;
103
104 update_rt_migration(rq);
Steven Rostedt764a9d62008-01-25 21:08:04 +0100105#endif /* CONFIG_SMP */
Steven Rostedt63489e42008-01-25 21:08:03 +0100106}
107
Ingo Molnarfd390f62007-08-09 11:16:48 +0200108static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200109{
110 struct rt_prio_array *array = &rq->rt.active;
111
112 list_add_tail(&p->run_list, array->queue + p->prio);
113 __set_bit(p->prio, array->bitmap);
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +0100114 inc_cpu_load(rq, p->se.load.weight);
Steven Rostedt63489e42008-01-25 21:08:03 +0100115
116 inc_rt_tasks(p, rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200117}
118
119/*
120 * Adding/removing a task to/from a priority array:
121 */
Ingo Molnarf02231e2007-08-09 11:16:48 +0200122static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200123{
124 struct rt_prio_array *array = &rq->rt.active;
125
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200126 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200127
128 list_del(&p->run_list);
129 if (list_empty(array->queue + p->prio))
130 __clear_bit(p->prio, array->bitmap);
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +0100131 dec_cpu_load(rq, p->se.load.weight);
Steven Rostedt63489e42008-01-25 21:08:03 +0100132
133 dec_rt_tasks(p, rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200134}
135
136/*
137 * Put task to the end of the run list without the overhead of dequeue
138 * followed by enqueue.
139 */
140static void requeue_task_rt(struct rq *rq, struct task_struct *p)
141{
142 struct rt_prio_array *array = &rq->rt.active;
143
144 list_move_tail(&p->run_list, array->queue + p->prio);
145}
146
147static void
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200148yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200149{
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200150 requeue_task_rt(rq, rq->curr);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200151}
152
Gregory Haskinse7693a32008-01-25 21:08:09 +0100153#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +0100154static int find_lowest_rq(struct task_struct *task);
155
Gregory Haskinse7693a32008-01-25 21:08:09 +0100156static int select_task_rq_rt(struct task_struct *p, int sync)
157{
Gregory Haskins318e0892008-01-25 21:08:10 +0100158 struct rq *rq = task_rq(p);
159
160 /*
Steven Rostedte1f47d82008-01-25 21:08:12 +0100161 * If the current task is an RT task, then
162 * try to see if we can wake this RT task up on another
163 * runqueue. Otherwise simply start this RT task
164 * on its current runqueue.
165 *
166 * We want to avoid overloading runqueues. Even if
167 * the RT task is of higher priority than the current RT task.
168 * RT tasks behave differently than other tasks. If
169 * one gets preempted, we try to push it off to another queue.
170 * So trying to keep a preempting RT task on the same
171 * cache hot CPU will force the running RT task to
172 * a cold CPU. So we waste all the cache for the lower
173 * RT task in hopes of saving some of a RT task
174 * that is just being woken and probably will have
175 * cold cache anyway.
Gregory Haskins318e0892008-01-25 21:08:10 +0100176 */
Gregory Haskins17b32792008-01-25 21:08:13 +0100177 if (unlikely(rt_task(rq->curr)) &&
178 (p->nr_cpus_allowed > 1)) {
Gregory Haskins318e0892008-01-25 21:08:10 +0100179 int cpu = find_lowest_rq(p);
180
181 return (cpu == -1) ? task_cpu(p) : cpu;
182 }
183
184 /*
185 * Otherwise, just let it ride on the affined RQ and the
186 * post-schedule router will push the preempted task away
187 */
Gregory Haskinse7693a32008-01-25 21:08:09 +0100188 return task_cpu(p);
189}
190#endif /* CONFIG_SMP */
191
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200192/*
193 * Preempt the current task with a newly woken task if needed:
194 */
195static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
196{
197 if (p->prio < rq->curr->prio)
198 resched_task(rq->curr);
199}
200
Ingo Molnarfb8d4722007-08-09 11:16:48 +0200201static struct task_struct *pick_next_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200202{
203 struct rt_prio_array *array = &rq->rt.active;
204 struct task_struct *next;
205 struct list_head *queue;
206 int idx;
207
208 idx = sched_find_first_bit(array->bitmap);
209 if (idx >= MAX_RT_PRIO)
210 return NULL;
211
212 queue = array->queue + idx;
213 next = list_entry(queue->next, struct task_struct, run_list);
214
Ingo Molnard2819182007-08-09 11:16:47 +0200215 next->se.exec_start = rq->clock;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200216
217 return next;
218}
219
Ingo Molnar31ee5292007-08-09 11:16:49 +0200220static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200221{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200222 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200223 p->se.exec_start = 0;
224}
225
Peter Williams681f3e62007-10-24 18:23:51 +0200226#ifdef CONFIG_SMP
Steven Rostedte8fa1362008-01-25 21:08:05 +0100227/* Only try algorithms three times */
228#define RT_MAX_TRIES 3
229
230static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
231static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
232
Steven Rostedtf65eda42008-01-25 21:08:07 +0100233static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
234{
235 if (!task_running(rq, p) &&
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100236 (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
237 (p->nr_cpus_allowed > 1))
Steven Rostedtf65eda42008-01-25 21:08:07 +0100238 return 1;
239 return 0;
240}
241
Steven Rostedte8fa1362008-01-25 21:08:05 +0100242/* Return the second highest RT task, NULL otherwise */
Ingo Molnar79064fb2008-01-25 21:08:14 +0100243static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100244{
245 struct rt_prio_array *array = &rq->rt.active;
246 struct task_struct *next;
247 struct list_head *queue;
248 int idx;
249
Steven Rostedte8fa1362008-01-25 21:08:05 +0100250 if (likely(rq->rt.rt_nr_running < 2))
251 return NULL;
252
253 idx = sched_find_first_bit(array->bitmap);
254 if (unlikely(idx >= MAX_RT_PRIO)) {
255 WARN_ON(1); /* rt_nr_running is bad */
256 return NULL;
257 }
258
259 queue = array->queue + idx;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100260 BUG_ON(list_empty(queue));
261
Steven Rostedte8fa1362008-01-25 21:08:05 +0100262 next = list_entry(queue->next, struct task_struct, run_list);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100263 if (unlikely(pick_rt_task(rq, next, cpu)))
264 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100265
266 if (queue->next->next != queue) {
267 /* same prio task */
Ingo Molnar79064fb2008-01-25 21:08:14 +0100268 next = list_entry(queue->next->next, struct task_struct,
269 run_list);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100270 if (pick_rt_task(rq, next, cpu))
271 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100272 }
273
Steven Rostedtf65eda42008-01-25 21:08:07 +0100274 retry:
Steven Rostedte8fa1362008-01-25 21:08:05 +0100275 /* slower, but more flexible */
276 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100277 if (unlikely(idx >= MAX_RT_PRIO))
Steven Rostedte8fa1362008-01-25 21:08:05 +0100278 return NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100279
280 queue = array->queue + idx;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100281 BUG_ON(list_empty(queue));
Steven Rostedte8fa1362008-01-25 21:08:05 +0100282
Steven Rostedtf65eda42008-01-25 21:08:07 +0100283 list_for_each_entry(next, queue, run_list) {
284 if (pick_rt_task(rq, next, cpu))
285 goto out;
286 }
287
288 goto retry;
289
290 out:
Steven Rostedte8fa1362008-01-25 21:08:05 +0100291 return next;
292}
293
294static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
295
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100296static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
Gregory Haskins07b40322008-01-25 21:08:10 +0100297{
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100298 int lowest_prio = -1;
Steven Rostedt610bf052008-01-25 21:08:13 +0100299 int lowest_cpu = -1;
Gregory Haskins06f90db2008-01-25 21:08:13 +0100300 int count = 0;
Steven Rostedt610bf052008-01-25 21:08:13 +0100301 int cpu;
Gregory Haskins07b40322008-01-25 21:08:10 +0100302
Gregory Haskins637f5082008-01-25 21:08:18 +0100303 cpus_and(*lowest_mask, task_rq(task)->rd->online, task->cpus_allowed);
Gregory Haskins07b40322008-01-25 21:08:10 +0100304
305 /*
306 * Scan each rq for the lowest prio.
307 */
Steven Rostedt610bf052008-01-25 21:08:13 +0100308 for_each_cpu_mask(cpu, *lowest_mask) {
Gregory Haskins07b40322008-01-25 21:08:10 +0100309 struct rq *rq = cpu_rq(cpu);
310
Gregory Haskins07b40322008-01-25 21:08:10 +0100311 /* We look for lowest RT prio or non-rt CPU */
312 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
Steven Rostedt610bf052008-01-25 21:08:13 +0100313 /*
314 * if we already found a low RT queue
315 * and now we found this non-rt queue
316 * clear the mask and set our bit.
317 * Otherwise just return the queue as is
318 * and the count==1 will cause the algorithm
319 * to use the first bit found.
320 */
321 if (lowest_cpu != -1) {
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100322 cpus_clear(*lowest_mask);
Steven Rostedt610bf052008-01-25 21:08:13 +0100323 cpu_set(rq->cpu, *lowest_mask);
324 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100325 return 1;
Gregory Haskins07b40322008-01-25 21:08:10 +0100326 }
327
328 /* no locking for now */
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100329 if ((rq->rt.highest_prio > task->prio)
330 && (rq->rt.highest_prio >= lowest_prio)) {
331 if (rq->rt.highest_prio > lowest_prio) {
332 /* new low - clear old data */
333 lowest_prio = rq->rt.highest_prio;
Steven Rostedt610bf052008-01-25 21:08:13 +0100334 lowest_cpu = cpu;
335 count = 0;
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100336 }
Gregory Haskins06f90db2008-01-25 21:08:13 +0100337 count++;
Steven Rostedt610bf052008-01-25 21:08:13 +0100338 } else
339 cpu_clear(cpu, *lowest_mask);
340 }
341
342 /*
343 * Clear out all the set bits that represent
344 * runqueues that were of higher prio than
345 * the lowest_prio.
346 */
347 if (lowest_cpu > 0) {
348 /*
349 * Perhaps we could add another cpumask op to
350 * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
351 * Then that could be optimized to use memset and such.
352 */
353 for_each_cpu_mask(cpu, *lowest_mask) {
354 if (cpu >= lowest_cpu)
355 break;
356 cpu_clear(cpu, *lowest_mask);
Gregory Haskins07b40322008-01-25 21:08:10 +0100357 }
358 }
359
Gregory Haskins06f90db2008-01-25 21:08:13 +0100360 return count;
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100361}
362
363static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
364{
365 int first;
366
367 /* "this_cpu" is cheaper to preempt than a remote processor */
368 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
369 return this_cpu;
370
371 first = first_cpu(*mask);
372 if (first != NR_CPUS)
373 return first;
374
375 return -1;
376}
377
378static int find_lowest_rq(struct task_struct *task)
379{
380 struct sched_domain *sd;
381 cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
382 int this_cpu = smp_processor_id();
383 int cpu = task_cpu(task);
Gregory Haskins06f90db2008-01-25 21:08:13 +0100384 int count = find_lowest_cpus(task, lowest_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100385
Gregory Haskins06f90db2008-01-25 21:08:13 +0100386 if (!count)
387 return -1; /* No targets found */
388
389 /*
390 * There is no sense in performing an optimal search if only one
391 * target is found.
392 */
393 if (count == 1)
394 return first_cpu(*lowest_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100395
396 /*
397 * At this point we have built a mask of cpus representing the
398 * lowest priority tasks in the system. Now we want to elect
399 * the best one based on our affinity and topology.
400 *
401 * We prioritize the last cpu that the task executed on since
402 * it is most likely cache-hot in that location.
403 */
404 if (cpu_isset(cpu, *lowest_mask))
405 return cpu;
406
407 /*
408 * Otherwise, we consult the sched_domains span maps to figure
409 * out which cpu is logically closest to our hot cache data.
410 */
411 if (this_cpu == cpu)
412 this_cpu = -1; /* Skip this_cpu opt if the same */
413
414 for_each_domain(cpu, sd) {
415 if (sd->flags & SD_WAKE_AFFINE) {
416 cpumask_t domain_mask;
417 int best_cpu;
418
419 cpus_and(domain_mask, sd->span, *lowest_mask);
420
421 best_cpu = pick_optimal_cpu(this_cpu,
422 &domain_mask);
423 if (best_cpu != -1)
424 return best_cpu;
425 }
426 }
427
428 /*
429 * And finally, if there were no matches within the domains
430 * just give the caller *something* to work with from the compatible
431 * locations.
432 */
433 return pick_optimal_cpu(this_cpu, lowest_mask);
Gregory Haskins07b40322008-01-25 21:08:10 +0100434}
435
Steven Rostedte8fa1362008-01-25 21:08:05 +0100436/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +0100437static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100438{
439 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100440 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +0100441 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100442
443 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +0100444 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100445
Gregory Haskins2de0b462008-01-25 21:08:10 +0100446 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +0100447 break;
448
Gregory Haskins07b40322008-01-25 21:08:10 +0100449 lowest_rq = cpu_rq(cpu);
450
Steven Rostedte8fa1362008-01-25 21:08:05 +0100451 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +0100452 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +0100453 /*
454 * We had to unlock the run queue. In
455 * the mean time, task could have
456 * migrated already or had its affinity changed.
457 * Also make sure that it wasn't scheduled on its rq.
458 */
Gregory Haskins07b40322008-01-25 21:08:10 +0100459 if (unlikely(task_rq(task) != rq ||
Ingo Molnar4df64c02008-01-25 21:08:15 +0100460 !cpu_isset(lowest_rq->cpu,
461 task->cpus_allowed) ||
Gregory Haskins07b40322008-01-25 21:08:10 +0100462 task_running(rq, task) ||
Steven Rostedte8fa1362008-01-25 21:08:05 +0100463 !task->se.on_rq)) {
Ingo Molnar4df64c02008-01-25 21:08:15 +0100464
Steven Rostedte8fa1362008-01-25 21:08:05 +0100465 spin_unlock(&lowest_rq->lock);
466 lowest_rq = NULL;
467 break;
468 }
469 }
470
471 /* If this rq is still suitable use it. */
472 if (lowest_rq->rt.highest_prio > task->prio)
473 break;
474
475 /* try again */
476 spin_unlock(&lowest_rq->lock);
477 lowest_rq = NULL;
478 }
479
480 return lowest_rq;
481}
482
483/*
484 * If the current CPU has more than one RT task, see if the non
485 * running task can migrate over to a CPU that is running a task
486 * of lesser priority.
487 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100488static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100489{
490 struct task_struct *next_task;
491 struct rq *lowest_rq;
492 int ret = 0;
493 int paranoid = RT_MAX_TRIES;
494
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100495 if (!rq->rt.overloaded)
496 return 0;
497
Gregory Haskins697f0a42008-01-25 21:08:09 +0100498 next_task = pick_next_highest_task_rt(rq, -1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100499 if (!next_task)
500 return 0;
501
502 retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +0100503 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +0100504 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100505 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100506 }
Steven Rostedte8fa1362008-01-25 21:08:05 +0100507
508 /*
509 * It's possible that the next_task slipped in of
510 * higher priority than current. If that's the case
511 * just reschedule current.
512 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100513 if (unlikely(next_task->prio < rq->curr->prio)) {
514 resched_task(rq->curr);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100515 return 0;
516 }
517
Gregory Haskins697f0a42008-01-25 21:08:09 +0100518 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +0100519 get_task_struct(next_task);
520
521 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100522 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100523 if (!lowest_rq) {
524 struct task_struct *task;
525 /*
Gregory Haskins697f0a42008-01-25 21:08:09 +0100526 * find lock_lowest_rq releases rq->lock
Steven Rostedte8fa1362008-01-25 21:08:05 +0100527 * so it is possible that next_task has changed.
528 * If it has, then try again.
529 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100530 task = pick_next_highest_task_rt(rq, -1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100531 if (unlikely(task != next_task) && task && paranoid--) {
532 put_task_struct(next_task);
533 next_task = task;
534 goto retry;
535 }
536 goto out;
537 }
538
Gregory Haskins697f0a42008-01-25 21:08:09 +0100539 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100540 set_task_cpu(next_task, lowest_rq->cpu);
541 activate_task(lowest_rq, next_task, 0);
542
543 resched_task(lowest_rq->curr);
544
545 spin_unlock(&lowest_rq->lock);
546
547 ret = 1;
548out:
549 put_task_struct(next_task);
550
551 return ret;
552}
553
554/*
555 * TODO: Currently we just use the second highest prio task on
556 * the queue, and stop when it can't migrate (or there's
557 * no more RT tasks). There may be a case where a lower
558 * priority RT task has a different affinity than the
559 * higher RT task. In this case the lower RT task could
560 * possibly be able to migrate where as the higher priority
561 * RT task could not. We currently ignore this issue.
562 * Enhancements are welcome!
563 */
564static void push_rt_tasks(struct rq *rq)
565{
566 /* push_rt_task will return true if it moved an RT */
567 while (push_rt_task(rq))
568 ;
569}
570
Steven Rostedtf65eda42008-01-25 21:08:07 +0100571static int pull_rt_task(struct rq *this_rq)
572{
Ingo Molnar80bf3172008-01-25 21:08:17 +0100573 int this_cpu = this_rq->cpu, ret = 0, cpu;
574 struct task_struct *p, *next;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100575 struct rq *src_rq;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100576
Gregory Haskins637f5082008-01-25 21:08:18 +0100577 if (likely(!rt_overloaded(this_rq)))
Steven Rostedtf65eda42008-01-25 21:08:07 +0100578 return 0;
579
580 next = pick_next_task_rt(this_rq);
581
Gregory Haskins637f5082008-01-25 21:08:18 +0100582 for_each_cpu_mask(cpu, this_rq->rd->rto_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +0100583 if (this_cpu == cpu)
584 continue;
585
586 src_rq = cpu_rq(cpu);
587 if (unlikely(src_rq->rt.rt_nr_running <= 1)) {
588 /*
589 * It is possible that overlapping cpusets
590 * will miss clearing a non overloaded runqueue.
591 * Clear it now.
592 */
593 if (double_lock_balance(this_rq, src_rq)) {
594 /* unlocked our runqueue lock */
595 struct task_struct *old_next = next;
Ingo Molnar80bf3172008-01-25 21:08:17 +0100596
Steven Rostedtf65eda42008-01-25 21:08:07 +0100597 next = pick_next_task_rt(this_rq);
598 if (next != old_next)
599 ret = 1;
600 }
Ingo Molnar80bf3172008-01-25 21:08:17 +0100601 if (likely(src_rq->rt.rt_nr_running <= 1)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +0100602 /*
603 * Small chance that this_rq->curr changed
604 * but it's really harmless here.
605 */
606 rt_clear_overload(this_rq);
Ingo Molnar80bf3172008-01-25 21:08:17 +0100607 } else {
Steven Rostedtf65eda42008-01-25 21:08:07 +0100608 /*
609 * Heh, the src_rq is now overloaded, since
610 * we already have the src_rq lock, go straight
611 * to pulling tasks from it.
612 */
613 goto try_pulling;
Ingo Molnar80bf3172008-01-25 21:08:17 +0100614 }
Steven Rostedtf65eda42008-01-25 21:08:07 +0100615 spin_unlock(&src_rq->lock);
616 continue;
617 }
618
619 /*
620 * We can potentially drop this_rq's lock in
621 * double_lock_balance, and another CPU could
622 * steal our next task - hence we must cause
623 * the caller to recalculate the next task
624 * in that case:
625 */
626 if (double_lock_balance(this_rq, src_rq)) {
627 struct task_struct *old_next = next;
Ingo Molnar80bf3172008-01-25 21:08:17 +0100628
Steven Rostedtf65eda42008-01-25 21:08:07 +0100629 next = pick_next_task_rt(this_rq);
630 if (next != old_next)
631 ret = 1;
632 }
633
634 /*
635 * Are there still pullable RT tasks?
636 */
637 if (src_rq->rt.rt_nr_running <= 1) {
638 spin_unlock(&src_rq->lock);
639 continue;
640 }
641
642 try_pulling:
643 p = pick_next_highest_task_rt(src_rq, this_cpu);
644
645 /*
646 * Do we have an RT task that preempts
647 * the to-be-scheduled task?
648 */
649 if (p && (!next || (p->prio < next->prio))) {
650 WARN_ON(p == src_rq->curr);
651 WARN_ON(!p->se.on_rq);
652
653 /*
654 * There's a chance that p is higher in priority
655 * than what's currently running on its cpu.
656 * This is just that p is wakeing up and hasn't
657 * had a chance to schedule. We only pull
658 * p if it is lower in priority than the
659 * current task on the run queue or
660 * this_rq next task is lower in prio than
661 * the current task on that rq.
662 */
663 if (p->prio < src_rq->curr->prio ||
664 (next && next->prio < src_rq->curr->prio))
Ingo Molnar80bf3172008-01-25 21:08:17 +0100665 goto out;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100666
667 ret = 1;
668
669 deactivate_task(src_rq, p, 0);
670 set_task_cpu(p, this_cpu);
671 activate_task(this_rq, p, 0);
672 /*
673 * We continue with the search, just in
674 * case there's an even higher prio task
675 * in another runqueue. (low likelyhood
676 * but possible)
Ingo Molnar80bf3172008-01-25 21:08:17 +0100677 *
Steven Rostedtf65eda42008-01-25 21:08:07 +0100678 * Update next so that we won't pick a task
679 * on another cpu with a priority lower (or equal)
680 * than the one we just picked.
681 */
682 next = p;
683
684 }
Ingo Molnar80bf3172008-01-25 21:08:17 +0100685 out:
Steven Rostedtf65eda42008-01-25 21:08:07 +0100686 spin_unlock(&src_rq->lock);
687 }
688
689 return ret;
690}
691
Steven Rostedt9a897c52008-01-25 21:08:22 +0100692static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
Steven Rostedtf65eda42008-01-25 21:08:07 +0100693{
694 /* Try to pull RT tasks here if we lower this rq's prio */
Ingo Molnar7f51f292008-01-25 21:08:17 +0100695 if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
Steven Rostedtf65eda42008-01-25 21:08:07 +0100696 pull_rt_task(rq);
697}
698
Steven Rostedt9a897c52008-01-25 21:08:22 +0100699static void post_schedule_rt(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100700{
701 /*
702 * If we have more than one rt_task queued, then
703 * see if we can push the other rt_tasks off to other CPUS.
704 * Note we may release the rq lock, and since
705 * the lock was owned by prev, we need to release it
706 * first via finish_lock_switch and then reaquire it here.
707 */
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100708 if (unlikely(rq->rt.overloaded)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +0100709 spin_lock_irq(&rq->lock);
710 push_rt_tasks(rq);
711 spin_unlock_irq(&rq->lock);
712 }
713}
714
Steven Rostedt4642daf2008-01-25 21:08:07 +0100715
Steven Rostedt9a897c52008-01-25 21:08:22 +0100716static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
Steven Rostedt4642daf2008-01-25 21:08:07 +0100717{
Steven Rostedt9a897c52008-01-25 21:08:22 +0100718 if (!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}
Ingo Molnardeeeccd2008-01-25 21:08:15 +0100741
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100742static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
743{
744 int weight = cpus_weight(*new_mask);
745
746 BUG_ON(!rt_task(p));
747
748 /*
749 * Update the migration status of the RQ if we have an RT task
750 * which is running AND changing its weight value.
751 */
752 if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
753 struct rq *rq = task_rq(p);
754
Ingo Molnardeeeccd2008-01-25 21:08:15 +0100755 if ((p->nr_cpus_allowed <= 1) && (weight > 1)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100756 rq->rt.rt_nr_migratory++;
Ingo Molnardeeeccd2008-01-25 21:08:15 +0100757 } else if ((p->nr_cpus_allowed > 1) && (weight <= 1)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100758 BUG_ON(!rq->rt.rt_nr_migratory);
759 rq->rt.rt_nr_migratory--;
760 }
761
762 update_rt_migration(rq);
763 }
764
765 p->cpus_allowed = *new_mask;
766 p->nr_cpus_allowed = weight;
767}
Ingo Molnardeeeccd2008-01-25 21:08:15 +0100768
Ingo Molnarbdd7c812008-01-25 21:08:18 +0100769/* Assumes rq->lock is held */
770static void join_domain_rt(struct rq *rq)
771{
772 if (rq->rt.overloaded)
773 rt_set_overload(rq);
774}
775
776/* Assumes rq->lock is held */
777static void leave_domain_rt(struct rq *rq)
778{
779 if (rq->rt.overloaded)
780 rt_clear_overload(rq);
781}
Steven Rostedte8fa1362008-01-25 21:08:05 +0100782#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200783
784static void task_tick_rt(struct rq *rq, struct task_struct *p)
785{
Peter Zijlstra67e2be02007-12-20 15:01:17 +0100786 update_curr_rt(rq);
787
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200788 /*
789 * RR tasks need a special form of timeslice management.
790 * FIFO tasks have no timeslices.
791 */
792 if (p->policy != SCHED_RR)
793 return;
794
795 if (--p->time_slice)
796 return;
797
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200798 p->time_slice = DEF_TIMESLICE;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200799
Dmitry Adamushko98fbc792007-08-24 20:39:10 +0200800 /*
801 * Requeue to the end of queue if we are not the only element
802 * on the queue:
803 */
804 if (p->run_list.prev != p->run_list.next) {
805 requeue_task_rt(rq, p);
806 set_tsk_need_resched(p);
807 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200808}
809
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200810static void set_curr_task_rt(struct rq *rq)
811{
812 struct task_struct *p = rq->curr;
813
814 p->se.exec_start = rq->clock;
815}
816
Ingo Molnar5522d5d2007-10-15 17:00:12 +0200817const struct sched_class rt_sched_class = {
818 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200819 .enqueue_task = enqueue_task_rt,
820 .dequeue_task = dequeue_task_rt,
821 .yield_task = yield_task_rt,
Gregory Haskinse7693a32008-01-25 21:08:09 +0100822#ifdef CONFIG_SMP
823 .select_task_rq = select_task_rq_rt,
824#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200825
826 .check_preempt_curr = check_preempt_curr_rt,
827
828 .pick_next_task = pick_next_task_rt,
829 .put_prev_task = put_prev_task_rt,
830
Peter Williams681f3e62007-10-24 18:23:51 +0200831#ifdef CONFIG_SMP
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200832 .load_balance = load_balance_rt,
Peter Williamse1d14842007-10-24 18:23:51 +0200833 .move_one_task = move_one_task_rt,
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100834 .set_cpus_allowed = set_cpus_allowed_rt,
Ingo Molnarbdd7c812008-01-25 21:08:18 +0100835 .join_domain = join_domain_rt,
836 .leave_domain = leave_domain_rt,
Steven Rostedt9a897c52008-01-25 21:08:22 +0100837 .pre_schedule = pre_schedule_rt,
838 .post_schedule = post_schedule_rt,
839 .task_wake_up = task_wake_up_rt,
Peter Williams681f3e62007-10-24 18:23:51 +0200840#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200841
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200842 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200843 .task_tick = task_tick_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200844};