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