blob: 8d42693812390bfdc48a5e4aeda838aa122c8c64 [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 Haskinscdc8eb92008-01-25 21:08:23 +010037 if (!rq->rt.overloaded) {
38 rt_set_overload(rq);
39 rq->rt.overloaded = 1;
40 }
41 } else if (rq->rt.overloaded) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010042 rt_clear_overload(rq);
Gregory Haskins637f5082008-01-25 21:08:18 +010043 rq->rt.overloaded = 0;
44 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010045}
Steven Rostedt4fd29172008-01-25 21:08:06 +010046#endif /* CONFIG_SMP */
47
Peter Zijlstra6f505b12008-01-25 21:08:30 +010048static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +010049{
Peter Zijlstra6f505b12008-01-25 21:08:30 +010050 return container_of(rt_se, struct task_struct, rt);
51}
52
53static inline int on_rt_rq(struct sched_rt_entity *rt_se)
54{
55 return !list_empty(&rt_se->run_list);
56}
57
58#ifdef CONFIG_FAIR_GROUP_SCHED
59
60static inline unsigned int sched_rt_ratio(struct rt_rq *rt_rq)
61{
62 if (!rt_rq->tg)
63 return SCHED_RT_FRAC;
64
65 return rt_rq->tg->rt_ratio;
66}
67
68#define for_each_leaf_rt_rq(rt_rq, rq) \
69 list_for_each_entry(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
70
71static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
72{
73 return rt_rq->rq;
74}
75
76static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
77{
78 return rt_se->rt_rq;
79}
80
81#define for_each_sched_rt_entity(rt_se) \
82 for (; rt_se; rt_se = rt_se->parent)
83
84static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
85{
86 return rt_se->my_q;
87}
88
89static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
90static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
91
92static void sched_rt_ratio_enqueue(struct rt_rq *rt_rq)
93{
94 struct sched_rt_entity *rt_se = rt_rq->rt_se;
95
96 if (rt_se && !on_rt_rq(rt_se) && rt_rq->rt_nr_running) {
Peter Zijlstra10203872008-01-25 21:08:32 +010097 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
98
Peter Zijlstra6f505b12008-01-25 21:08:30 +010099 enqueue_rt_entity(rt_se);
Peter Zijlstra10203872008-01-25 21:08:32 +0100100 if (rt_rq->highest_prio < curr->prio)
101 resched_task(curr);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100102 }
103}
104
105static void sched_rt_ratio_dequeue(struct rt_rq *rt_rq)
106{
107 struct sched_rt_entity *rt_se = rt_rq->rt_se;
108
109 if (rt_se && on_rt_rq(rt_se))
110 dequeue_rt_entity(rt_se);
111}
112
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100113static inline int rt_rq_throttled(struct rt_rq *rt_rq)
114{
115 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
116}
117
118static int rt_se_boosted(struct sched_rt_entity *rt_se)
119{
120 struct rt_rq *rt_rq = group_rt_rq(rt_se);
121 struct task_struct *p;
122
123 if (rt_rq)
124 return !!rt_rq->rt_nr_boosted;
125
126 p = rt_task_of(rt_se);
127 return p->prio != p->normal_prio;
128}
129
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100130#else
131
132static inline unsigned int sched_rt_ratio(struct rt_rq *rt_rq)
133{
134 return sysctl_sched_rt_ratio;
135}
136
137#define for_each_leaf_rt_rq(rt_rq, rq) \
138 for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
139
140static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
141{
142 return container_of(rt_rq, struct rq, rt);
143}
144
145static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
146{
147 struct task_struct *p = rt_task_of(rt_se);
148 struct rq *rq = task_rq(p);
149
150 return &rq->rt;
151}
152
153#define for_each_sched_rt_entity(rt_se) \
154 for (; rt_se; rt_se = NULL)
155
156static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
157{
158 return NULL;
159}
160
161static inline void sched_rt_ratio_enqueue(struct rt_rq *rt_rq)
162{
163}
164
165static inline void sched_rt_ratio_dequeue(struct rt_rq *rt_rq)
166{
167}
168
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100169static inline int rt_rq_throttled(struct rt_rq *rt_rq)
170{
171 return rt_rq->rt_throttled;
172}
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100173#endif
174
175static inline int rt_se_prio(struct sched_rt_entity *rt_se)
176{
177#ifdef CONFIG_FAIR_GROUP_SCHED
178 struct rt_rq *rt_rq = group_rt_rq(rt_se);
179
180 if (rt_rq)
181 return rt_rq->highest_prio;
182#endif
183
184 return rt_task_of(rt_se)->prio;
185}
186
187static int sched_rt_ratio_exceeded(struct rt_rq *rt_rq)
188{
189 unsigned int rt_ratio = sched_rt_ratio(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100190 u64 period, ratio;
191
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100192 if (rt_ratio == SCHED_RT_FRAC)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100193 return 0;
194
195 if (rt_rq->rt_throttled)
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100196 return rt_rq_throttled(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100197
198 period = (u64)sysctl_sched_rt_period * NSEC_PER_MSEC;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100199 ratio = (period * rt_ratio) >> SCHED_RT_FRAC_SHIFT;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100200
201 if (rt_rq->rt_time > ratio) {
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100202 struct rq *rq = rq_of_rt_rq(rt_rq);
203
204 rq->rt_throttled = 1;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100205 rt_rq->rt_throttled = 1;
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100206
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100207 if (rt_rq_throttled(rt_rq)) {
208 sched_rt_ratio_dequeue(rt_rq);
209 return 1;
210 }
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100211 }
212
213 return 0;
214}
215
216static void update_sched_rt_period(struct rq *rq)
217{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100218 struct rt_rq *rt_rq;
219 u64 period;
220
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100221 while (rq->clock > rq->rt_period_expire) {
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100222 period = (u64)sysctl_sched_rt_period * NSEC_PER_MSEC;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100223 rq->rt_period_expire += period;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100224
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100225 for_each_leaf_rt_rq(rt_rq, rq) {
226 unsigned long rt_ratio = sched_rt_ratio(rt_rq);
227 u64 ratio = (period * rt_ratio) >> SCHED_RT_FRAC_SHIFT;
228
229 rt_rq->rt_time -= min(rt_rq->rt_time, ratio);
230 if (rt_rq->rt_throttled) {
231 rt_rq->rt_throttled = 0;
232 sched_rt_ratio_enqueue(rt_rq);
233 }
234 }
235
236 rq->rt_throttled = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100237 }
238}
239
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200240/*
241 * Update the current task's runtime statistics. Skip current tasks that
242 * are not in our scheduling class.
243 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200244static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200245{
246 struct task_struct *curr = rq->curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100247 struct sched_rt_entity *rt_se = &curr->rt;
248 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200249 u64 delta_exec;
250
251 if (!task_has_rt_policy(curr))
252 return;
253
Ingo Molnard2819182007-08-09 11:16:47 +0200254 delta_exec = rq->clock - curr->se.exec_start;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200255 if (unlikely((s64)delta_exec < 0))
256 delta_exec = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200257
258 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200259
260 curr->se.sum_exec_runtime += delta_exec;
Ingo Molnard2819182007-08-09 11:16:47 +0200261 curr->se.exec_start = rq->clock;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100262 cpuacct_charge(curr, delta_exec);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100263
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100264 rt_rq->rt_time += delta_exec;
265 /*
266 * might make it a tad more accurate:
267 *
268 * update_sched_rt_period(rq);
269 */
270 if (sched_rt_ratio_exceeded(rt_rq))
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100271 resched_task(curr);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200272}
273
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100274static inline
275void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Steven Rostedt63489e42008-01-25 21:08:03 +0100276{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100277 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
278 rt_rq->rt_nr_running++;
279#if defined CONFIG_SMP || defined CONFIG_FAIR_GROUP_SCHED
280 if (rt_se_prio(rt_se) < rt_rq->highest_prio)
281 rt_rq->highest_prio = rt_se_prio(rt_se);
282#endif
Steven Rostedt764a9d62008-01-25 21:08:04 +0100283#ifdef CONFIG_SMP
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100284 if (rt_se->nr_cpus_allowed > 1) {
285 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100286 rq->rt.rt_nr_migratory++;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100287 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100288
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100289 update_rt_migration(rq_of_rt_rq(rt_rq));
290#endif
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100291#ifdef CONFIG_FAIR_GROUP_SCHED
292 if (rt_se_boosted(rt_se))
293 rt_rq->rt_nr_boosted++;
294#endif
Steven Rostedt63489e42008-01-25 21:08:03 +0100295}
296
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100297static inline
298void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Steven Rostedt63489e42008-01-25 21:08:03 +0100299{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100300 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
301 WARN_ON(!rt_rq->rt_nr_running);
302 rt_rq->rt_nr_running--;
303#if defined CONFIG_SMP || defined CONFIG_FAIR_GROUP_SCHED
304 if (rt_rq->rt_nr_running) {
Steven Rostedt764a9d62008-01-25 21:08:04 +0100305 struct rt_prio_array *array;
306
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100307 WARN_ON(rt_se_prio(rt_se) < rt_rq->highest_prio);
308 if (rt_se_prio(rt_se) == rt_rq->highest_prio) {
Steven Rostedt764a9d62008-01-25 21:08:04 +0100309 /* recalculate */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100310 array = &rt_rq->active;
311 rt_rq->highest_prio =
Steven Rostedt764a9d62008-01-25 21:08:04 +0100312 sched_find_first_bit(array->bitmap);
313 } /* otherwise leave rq->highest prio alone */
314 } else
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100315 rt_rq->highest_prio = MAX_RT_PRIO;
316#endif
317#ifdef CONFIG_SMP
318 if (rt_se->nr_cpus_allowed > 1) {
319 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100320 rq->rt.rt_nr_migratory--;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100321 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100322
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100323 update_rt_migration(rq_of_rt_rq(rt_rq));
Steven Rostedt764a9d62008-01-25 21:08:04 +0100324#endif /* CONFIG_SMP */
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100325#ifdef CONFIG_FAIR_GROUP_SCHED
326 if (rt_se_boosted(rt_se))
327 rt_rq->rt_nr_boosted--;
328
329 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
330#endif
Steven Rostedt63489e42008-01-25 21:08:03 +0100331}
332
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100333static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200334{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100335 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
336 struct rt_prio_array *array = &rt_rq->active;
337 struct rt_rq *group_rq = group_rt_rq(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200338
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100339 if (group_rq && rt_rq_throttled(group_rq))
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100340 return;
Steven Rostedt63489e42008-01-25 21:08:03 +0100341
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100342 list_add_tail(&rt_se->run_list, array->queue + rt_se_prio(rt_se));
343 __set_bit(rt_se_prio(rt_se), array->bitmap);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100344
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100345 inc_rt_tasks(rt_se, rt_rq);
346}
347
348static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
349{
350 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
351 struct rt_prio_array *array = &rt_rq->active;
352
353 list_del_init(&rt_se->run_list);
354 if (list_empty(array->queue + rt_se_prio(rt_se)))
355 __clear_bit(rt_se_prio(rt_se), array->bitmap);
356
357 dec_rt_tasks(rt_se, rt_rq);
358}
359
360/*
361 * Because the prio of an upper entry depends on the lower
362 * entries, we must remove entries top - down.
363 *
364 * XXX: O(1/2 h^2) because we can only walk up, not down the chain.
365 * doesn't matter much for now, as h=2 for GROUP_SCHED.
366 */
367static void dequeue_rt_stack(struct task_struct *p)
368{
369 struct sched_rt_entity *rt_se, *top_se;
370
371 /*
372 * dequeue all, top - down.
373 */
374 do {
375 rt_se = &p->rt;
376 top_se = NULL;
377 for_each_sched_rt_entity(rt_se) {
378 if (on_rt_rq(rt_se))
379 top_se = rt_se;
380 }
381 if (top_se)
382 dequeue_rt_entity(top_se);
383 } while (top_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200384}
385
386/*
387 * Adding/removing a task to/from a priority array:
388 */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100389static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
390{
391 struct sched_rt_entity *rt_se = &p->rt;
392
393 if (wakeup)
394 rt_se->timeout = 0;
395
396 dequeue_rt_stack(p);
397
398 /*
399 * enqueue everybody, bottom - up.
400 */
401 for_each_sched_rt_entity(rt_se)
402 enqueue_rt_entity(rt_se);
403
404 inc_cpu_load(rq, p->se.load.weight);
405}
406
Ingo Molnarf02231e2007-08-09 11:16:48 +0200407static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200408{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100409 struct sched_rt_entity *rt_se = &p->rt;
410 struct rt_rq *rt_rq;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200411
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200412 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200413
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100414 dequeue_rt_stack(p);
Steven Rostedt63489e42008-01-25 21:08:03 +0100415
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100416 /*
417 * re-enqueue all non-empty rt_rq entities.
418 */
419 for_each_sched_rt_entity(rt_se) {
420 rt_rq = group_rt_rq(rt_se);
421 if (rt_rq && rt_rq->rt_nr_running)
422 enqueue_rt_entity(rt_se);
423 }
424
425 dec_cpu_load(rq, p->se.load.weight);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200426}
427
428/*
429 * Put task to the end of the run list without the overhead of dequeue
430 * followed by enqueue.
431 */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100432static
433void requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200434{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100435 struct rt_prio_array *array = &rt_rq->active;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200436
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100437 list_move_tail(&rt_se->run_list, array->queue + rt_se_prio(rt_se));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200438}
439
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100440static void requeue_task_rt(struct rq *rq, struct task_struct *p)
441{
442 struct sched_rt_entity *rt_se = &p->rt;
443 struct rt_rq *rt_rq;
444
445 for_each_sched_rt_entity(rt_se) {
446 rt_rq = rt_rq_of_se(rt_se);
447 requeue_rt_entity(rt_rq, rt_se);
448 }
449}
450
451static void yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200452{
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200453 requeue_task_rt(rq, rq->curr);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200454}
455
Gregory Haskinse7693a32008-01-25 21:08:09 +0100456#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +0100457static int find_lowest_rq(struct task_struct *task);
458
Gregory Haskinse7693a32008-01-25 21:08:09 +0100459static int select_task_rq_rt(struct task_struct *p, int sync)
460{
Gregory Haskins318e0892008-01-25 21:08:10 +0100461 struct rq *rq = task_rq(p);
462
463 /*
Steven Rostedte1f47d82008-01-25 21:08:12 +0100464 * If the current task is an RT task, then
465 * try to see if we can wake this RT task up on another
466 * runqueue. Otherwise simply start this RT task
467 * on its current runqueue.
468 *
469 * We want to avoid overloading runqueues. Even if
470 * the RT task is of higher priority than the current RT task.
471 * RT tasks behave differently than other tasks. If
472 * one gets preempted, we try to push it off to another queue.
473 * So trying to keep a preempting RT task on the same
474 * cache hot CPU will force the running RT task to
475 * a cold CPU. So we waste all the cache for the lower
476 * RT task in hopes of saving some of a RT task
477 * that is just being woken and probably will have
478 * cold cache anyway.
Gregory Haskins318e0892008-01-25 21:08:10 +0100479 */
Gregory Haskins17b32792008-01-25 21:08:13 +0100480 if (unlikely(rt_task(rq->curr)) &&
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100481 (p->rt.nr_cpus_allowed > 1)) {
Gregory Haskins318e0892008-01-25 21:08:10 +0100482 int cpu = find_lowest_rq(p);
483
484 return (cpu == -1) ? task_cpu(p) : cpu;
485 }
486
487 /*
488 * Otherwise, just let it ride on the affined RQ and the
489 * post-schedule router will push the preempted task away
490 */
Gregory Haskinse7693a32008-01-25 21:08:09 +0100491 return task_cpu(p);
492}
493#endif /* CONFIG_SMP */
494
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200495/*
496 * Preempt the current task with a newly woken task if needed:
497 */
498static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
499{
500 if (p->prio < rq->curr->prio)
501 resched_task(rq->curr);
502}
503
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100504static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
505 struct rt_rq *rt_rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200506{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100507 struct rt_prio_array *array = &rt_rq->active;
508 struct sched_rt_entity *next = NULL;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200509 struct list_head *queue;
510 int idx;
511
512 idx = sched_find_first_bit(array->bitmap);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100513 BUG_ON(idx >= MAX_RT_PRIO);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200514
515 queue = array->queue + idx;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100516 next = list_entry(queue->next, struct sched_rt_entity, run_list);
Dmitry Adamushko326587b2008-01-25 21:08:34 +0100517
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200518 return next;
519}
520
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100521static struct task_struct *pick_next_task_rt(struct rq *rq)
522{
523 struct sched_rt_entity *rt_se;
524 struct task_struct *p;
525 struct rt_rq *rt_rq;
526
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100527 rt_rq = &rq->rt;
528
529 if (unlikely(!rt_rq->rt_nr_running))
530 return NULL;
531
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100532 if (rt_rq_throttled(rt_rq))
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100533 return NULL;
534
535 do {
536 rt_se = pick_next_rt_entity(rq, rt_rq);
Dmitry Adamushko326587b2008-01-25 21:08:34 +0100537 BUG_ON(!rt_se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100538 rt_rq = group_rt_rq(rt_se);
539 } while (rt_rq);
540
541 p = rt_task_of(rt_se);
542 p->se.exec_start = rq->clock;
543 return p;
544}
545
Ingo Molnar31ee5292007-08-09 11:16:49 +0200546static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200547{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200548 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200549 p->se.exec_start = 0;
550}
551
Peter Williams681f3e62007-10-24 18:23:51 +0200552#ifdef CONFIG_SMP
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100553
Steven Rostedte8fa1362008-01-25 21:08:05 +0100554/* Only try algorithms three times */
555#define RT_MAX_TRIES 3
556
557static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
558static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
559
Steven Rostedtf65eda42008-01-25 21:08:07 +0100560static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
561{
562 if (!task_running(rq, p) &&
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100563 (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100564 (p->rt.nr_cpus_allowed > 1))
Steven Rostedtf65eda42008-01-25 21:08:07 +0100565 return 1;
566 return 0;
567}
568
Steven Rostedte8fa1362008-01-25 21:08:05 +0100569/* Return the second highest RT task, NULL otherwise */
Ingo Molnar79064fb2008-01-25 21:08:14 +0100570static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100571{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100572 struct task_struct *next = NULL;
573 struct sched_rt_entity *rt_se;
574 struct rt_prio_array *array;
575 struct rt_rq *rt_rq;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100576 int idx;
577
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100578 for_each_leaf_rt_rq(rt_rq, rq) {
579 array = &rt_rq->active;
580 idx = sched_find_first_bit(array->bitmap);
581 next_idx:
582 if (idx >= MAX_RT_PRIO)
583 continue;
584 if (next && next->prio < idx)
585 continue;
586 list_for_each_entry(rt_se, array->queue + idx, run_list) {
587 struct task_struct *p = rt_task_of(rt_se);
588 if (pick_rt_task(rq, p, cpu)) {
589 next = p;
590 break;
591 }
592 }
593 if (!next) {
594 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
595 goto next_idx;
596 }
Steven Rostedte8fa1362008-01-25 21:08:05 +0100597 }
598
Steven Rostedte8fa1362008-01-25 21:08:05 +0100599 return next;
600}
601
602static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
603
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100604static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
Gregory Haskins07b40322008-01-25 21:08:10 +0100605{
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100606 int lowest_prio = -1;
Steven Rostedt610bf052008-01-25 21:08:13 +0100607 int lowest_cpu = -1;
Gregory Haskins06f90db2008-01-25 21:08:13 +0100608 int count = 0;
Steven Rostedt610bf052008-01-25 21:08:13 +0100609 int cpu;
Gregory Haskins07b40322008-01-25 21:08:10 +0100610
Gregory Haskins637f5082008-01-25 21:08:18 +0100611 cpus_and(*lowest_mask, task_rq(task)->rd->online, task->cpus_allowed);
Gregory Haskins07b40322008-01-25 21:08:10 +0100612
613 /*
614 * Scan each rq for the lowest prio.
615 */
Steven Rostedt610bf052008-01-25 21:08:13 +0100616 for_each_cpu_mask(cpu, *lowest_mask) {
Gregory Haskins07b40322008-01-25 21:08:10 +0100617 struct rq *rq = cpu_rq(cpu);
618
Gregory Haskins07b40322008-01-25 21:08:10 +0100619 /* We look for lowest RT prio or non-rt CPU */
620 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
Steven Rostedt610bf052008-01-25 21:08:13 +0100621 /*
622 * if we already found a low RT queue
623 * and now we found this non-rt queue
624 * clear the mask and set our bit.
625 * Otherwise just return the queue as is
626 * and the count==1 will cause the algorithm
627 * to use the first bit found.
628 */
629 if (lowest_cpu != -1) {
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100630 cpus_clear(*lowest_mask);
Steven Rostedt610bf052008-01-25 21:08:13 +0100631 cpu_set(rq->cpu, *lowest_mask);
632 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100633 return 1;
Gregory Haskins07b40322008-01-25 21:08:10 +0100634 }
635
636 /* no locking for now */
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100637 if ((rq->rt.highest_prio > task->prio)
638 && (rq->rt.highest_prio >= lowest_prio)) {
639 if (rq->rt.highest_prio > lowest_prio) {
640 /* new low - clear old data */
641 lowest_prio = rq->rt.highest_prio;
Steven Rostedt610bf052008-01-25 21:08:13 +0100642 lowest_cpu = cpu;
643 count = 0;
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100644 }
Gregory Haskins06f90db2008-01-25 21:08:13 +0100645 count++;
Steven Rostedt610bf052008-01-25 21:08:13 +0100646 } else
647 cpu_clear(cpu, *lowest_mask);
648 }
649
650 /*
651 * Clear out all the set bits that represent
652 * runqueues that were of higher prio than
653 * the lowest_prio.
654 */
655 if (lowest_cpu > 0) {
656 /*
657 * Perhaps we could add another cpumask op to
658 * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
659 * Then that could be optimized to use memset and such.
660 */
661 for_each_cpu_mask(cpu, *lowest_mask) {
662 if (cpu >= lowest_cpu)
663 break;
664 cpu_clear(cpu, *lowest_mask);
Gregory Haskins07b40322008-01-25 21:08:10 +0100665 }
666 }
667
Gregory Haskins06f90db2008-01-25 21:08:13 +0100668 return count;
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100669}
670
671static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
672{
673 int first;
674
675 /* "this_cpu" is cheaper to preempt than a remote processor */
676 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
677 return this_cpu;
678
679 first = first_cpu(*mask);
680 if (first != NR_CPUS)
681 return first;
682
683 return -1;
684}
685
686static int find_lowest_rq(struct task_struct *task)
687{
688 struct sched_domain *sd;
689 cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
690 int this_cpu = smp_processor_id();
691 int cpu = task_cpu(task);
Gregory Haskins06f90db2008-01-25 21:08:13 +0100692 int count = find_lowest_cpus(task, lowest_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100693
Gregory Haskins06f90db2008-01-25 21:08:13 +0100694 if (!count)
695 return -1; /* No targets found */
696
697 /*
698 * There is no sense in performing an optimal search if only one
699 * target is found.
700 */
701 if (count == 1)
702 return first_cpu(*lowest_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +0100703
704 /*
705 * At this point we have built a mask of cpus representing the
706 * lowest priority tasks in the system. Now we want to elect
707 * the best one based on our affinity and topology.
708 *
709 * We prioritize the last cpu that the task executed on since
710 * it is most likely cache-hot in that location.
711 */
712 if (cpu_isset(cpu, *lowest_mask))
713 return cpu;
714
715 /*
716 * Otherwise, we consult the sched_domains span maps to figure
717 * out which cpu is logically closest to our hot cache data.
718 */
719 if (this_cpu == cpu)
720 this_cpu = -1; /* Skip this_cpu opt if the same */
721
722 for_each_domain(cpu, sd) {
723 if (sd->flags & SD_WAKE_AFFINE) {
724 cpumask_t domain_mask;
725 int best_cpu;
726
727 cpus_and(domain_mask, sd->span, *lowest_mask);
728
729 best_cpu = pick_optimal_cpu(this_cpu,
730 &domain_mask);
731 if (best_cpu != -1)
732 return best_cpu;
733 }
734 }
735
736 /*
737 * And finally, if there were no matches within the domains
738 * just give the caller *something* to work with from the compatible
739 * locations.
740 */
741 return pick_optimal_cpu(this_cpu, lowest_mask);
Gregory Haskins07b40322008-01-25 21:08:10 +0100742}
743
Steven Rostedte8fa1362008-01-25 21:08:05 +0100744/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +0100745static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100746{
747 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100748 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +0100749 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +0100750
751 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +0100752 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100753
Gregory Haskins2de0b462008-01-25 21:08:10 +0100754 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +0100755 break;
756
Gregory Haskins07b40322008-01-25 21:08:10 +0100757 lowest_rq = cpu_rq(cpu);
758
Steven Rostedte8fa1362008-01-25 21:08:05 +0100759 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +0100760 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +0100761 /*
762 * We had to unlock the run queue. In
763 * the mean time, task could have
764 * migrated already or had its affinity changed.
765 * Also make sure that it wasn't scheduled on its rq.
766 */
Gregory Haskins07b40322008-01-25 21:08:10 +0100767 if (unlikely(task_rq(task) != rq ||
Ingo Molnar4df64c02008-01-25 21:08:15 +0100768 !cpu_isset(lowest_rq->cpu,
769 task->cpus_allowed) ||
Gregory Haskins07b40322008-01-25 21:08:10 +0100770 task_running(rq, task) ||
Steven Rostedte8fa1362008-01-25 21:08:05 +0100771 !task->se.on_rq)) {
Ingo Molnar4df64c02008-01-25 21:08:15 +0100772
Steven Rostedte8fa1362008-01-25 21:08:05 +0100773 spin_unlock(&lowest_rq->lock);
774 lowest_rq = NULL;
775 break;
776 }
777 }
778
779 /* If this rq is still suitable use it. */
780 if (lowest_rq->rt.highest_prio > task->prio)
781 break;
782
783 /* try again */
784 spin_unlock(&lowest_rq->lock);
785 lowest_rq = NULL;
786 }
787
788 return lowest_rq;
789}
790
791/*
792 * If the current CPU has more than one RT task, see if the non
793 * running task can migrate over to a CPU that is running a task
794 * of lesser priority.
795 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100796static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100797{
798 struct task_struct *next_task;
799 struct rq *lowest_rq;
800 int ret = 0;
801 int paranoid = RT_MAX_TRIES;
802
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100803 if (!rq->rt.overloaded)
804 return 0;
805
Gregory Haskins697f0a42008-01-25 21:08:09 +0100806 next_task = pick_next_highest_task_rt(rq, -1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100807 if (!next_task)
808 return 0;
809
810 retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +0100811 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +0100812 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100813 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100814 }
Steven Rostedte8fa1362008-01-25 21:08:05 +0100815
816 /*
817 * It's possible that the next_task slipped in of
818 * higher priority than current. If that's the case
819 * just reschedule current.
820 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100821 if (unlikely(next_task->prio < rq->curr->prio)) {
822 resched_task(rq->curr);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100823 return 0;
824 }
825
Gregory Haskins697f0a42008-01-25 21:08:09 +0100826 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +0100827 get_task_struct(next_task);
828
829 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100830 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100831 if (!lowest_rq) {
832 struct task_struct *task;
833 /*
Gregory Haskins697f0a42008-01-25 21:08:09 +0100834 * find lock_lowest_rq releases rq->lock
Steven Rostedte8fa1362008-01-25 21:08:05 +0100835 * so it is possible that next_task has changed.
836 * If it has, then try again.
837 */
Gregory Haskins697f0a42008-01-25 21:08:09 +0100838 task = pick_next_highest_task_rt(rq, -1);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100839 if (unlikely(task != next_task) && task && paranoid--) {
840 put_task_struct(next_task);
841 next_task = task;
842 goto retry;
843 }
844 goto out;
845 }
846
Gregory Haskins697f0a42008-01-25 21:08:09 +0100847 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +0100848 set_task_cpu(next_task, lowest_rq->cpu);
849 activate_task(lowest_rq, next_task, 0);
850
851 resched_task(lowest_rq->curr);
852
853 spin_unlock(&lowest_rq->lock);
854
855 ret = 1;
856out:
857 put_task_struct(next_task);
858
859 return ret;
860}
861
862/*
863 * TODO: Currently we just use the second highest prio task on
864 * the queue, and stop when it can't migrate (or there's
865 * no more RT tasks). There may be a case where a lower
866 * priority RT task has a different affinity than the
867 * higher RT task. In this case the lower RT task could
868 * possibly be able to migrate where as the higher priority
869 * RT task could not. We currently ignore this issue.
870 * Enhancements are welcome!
871 */
872static void push_rt_tasks(struct rq *rq)
873{
874 /* push_rt_task will return true if it moved an RT */
875 while (push_rt_task(rq))
876 ;
877}
878
Steven Rostedtf65eda42008-01-25 21:08:07 +0100879static int pull_rt_task(struct rq *this_rq)
880{
Ingo Molnar80bf3172008-01-25 21:08:17 +0100881 int this_cpu = this_rq->cpu, ret = 0, cpu;
882 struct task_struct *p, *next;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100883 struct rq *src_rq;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100884
Gregory Haskins637f5082008-01-25 21:08:18 +0100885 if (likely(!rt_overloaded(this_rq)))
Steven Rostedtf65eda42008-01-25 21:08:07 +0100886 return 0;
887
888 next = pick_next_task_rt(this_rq);
889
Gregory Haskins637f5082008-01-25 21:08:18 +0100890 for_each_cpu_mask(cpu, this_rq->rd->rto_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +0100891 if (this_cpu == cpu)
892 continue;
893
894 src_rq = cpu_rq(cpu);
Steven Rostedtf65eda42008-01-25 21:08:07 +0100895 /*
896 * We can potentially drop this_rq's lock in
897 * double_lock_balance, and another CPU could
898 * steal our next task - hence we must cause
899 * the caller to recalculate the next task
900 * in that case:
901 */
902 if (double_lock_balance(this_rq, src_rq)) {
903 struct task_struct *old_next = next;
Ingo Molnar80bf3172008-01-25 21:08:17 +0100904
Steven Rostedtf65eda42008-01-25 21:08:07 +0100905 next = pick_next_task_rt(this_rq);
906 if (next != old_next)
907 ret = 1;
908 }
909
910 /*
911 * Are there still pullable RT tasks?
912 */
Mike Galbraith614ee1f2008-01-25 21:08:30 +0100913 if (src_rq->rt.rt_nr_running <= 1)
914 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100915
Steven Rostedtf65eda42008-01-25 21:08:07 +0100916 p = pick_next_highest_task_rt(src_rq, this_cpu);
917
918 /*
919 * Do we have an RT task that preempts
920 * the to-be-scheduled task?
921 */
922 if (p && (!next || (p->prio < next->prio))) {
923 WARN_ON(p == src_rq->curr);
924 WARN_ON(!p->se.on_rq);
925
926 /*
927 * There's a chance that p is higher in priority
928 * than what's currently running on its cpu.
929 * This is just that p is wakeing up and hasn't
930 * had a chance to schedule. We only pull
931 * p if it is lower in priority than the
932 * current task on the run queue or
933 * this_rq next task is lower in prio than
934 * the current task on that rq.
935 */
936 if (p->prio < src_rq->curr->prio ||
937 (next && next->prio < src_rq->curr->prio))
Mike Galbraith614ee1f2008-01-25 21:08:30 +0100938 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +0100939
940 ret = 1;
941
942 deactivate_task(src_rq, p, 0);
943 set_task_cpu(p, this_cpu);
944 activate_task(this_rq, p, 0);
945 /*
946 * We continue with the search, just in
947 * case there's an even higher prio task
948 * in another runqueue. (low likelyhood
949 * but possible)
Ingo Molnar80bf3172008-01-25 21:08:17 +0100950 *
Steven Rostedtf65eda42008-01-25 21:08:07 +0100951 * Update next so that we won't pick a task
952 * on another cpu with a priority lower (or equal)
953 * than the one we just picked.
954 */
955 next = p;
956
957 }
Mike Galbraith614ee1f2008-01-25 21:08:30 +0100958 skip:
Steven Rostedtf65eda42008-01-25 21:08:07 +0100959 spin_unlock(&src_rq->lock);
960 }
961
962 return ret;
963}
964
Steven Rostedt9a897c52008-01-25 21:08:22 +0100965static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
Steven Rostedtf65eda42008-01-25 21:08:07 +0100966{
967 /* Try to pull RT tasks here if we lower this rq's prio */
Ingo Molnar7f51f292008-01-25 21:08:17 +0100968 if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
Steven Rostedtf65eda42008-01-25 21:08:07 +0100969 pull_rt_task(rq);
970}
971
Steven Rostedt9a897c52008-01-25 21:08:22 +0100972static void post_schedule_rt(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +0100973{
974 /*
975 * If we have more than one rt_task queued, then
976 * see if we can push the other rt_tasks off to other CPUS.
977 * Note we may release the rq lock, and since
978 * the lock was owned by prev, we need to release it
979 * first via finish_lock_switch and then reaquire it here.
980 */
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100981 if (unlikely(rq->rt.overloaded)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +0100982 spin_lock_irq(&rq->lock);
983 push_rt_tasks(rq);
984 spin_unlock_irq(&rq->lock);
985 }
986}
987
Steven Rostedt4642daf2008-01-25 21:08:07 +0100988
Steven Rostedt9a897c52008-01-25 21:08:22 +0100989static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
Steven Rostedt4642daf2008-01-25 21:08:07 +0100990{
Steven Rostedt9a897c52008-01-25 21:08:22 +0100991 if (!task_running(rq, p) &&
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100992 (p->prio >= rq->rt.highest_prio) &&
993 rq->rt.overloaded)
Steven Rostedt4642daf2008-01-25 21:08:07 +0100994 push_rt_tasks(rq);
995}
996
Peter Williams43010652007-08-09 11:16:46 +0200997static unsigned long
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200998load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamse1d14842007-10-24 18:23:51 +0200999 unsigned long max_load_move,
1000 struct sched_domain *sd, enum cpu_idle_type idle,
1001 int *all_pinned, int *this_best_prio)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001002{
Steven Rostedtc7a1e462008-01-25 21:08:07 +01001003 /* don't touch RT tasks */
1004 return 0;
Peter Williamse1d14842007-10-24 18:23:51 +02001005}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001006
Peter Williamse1d14842007-10-24 18:23:51 +02001007static int
1008move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
1009 struct sched_domain *sd, enum cpu_idle_type idle)
1010{
Steven Rostedtc7a1e462008-01-25 21:08:07 +01001011 /* don't touch RT tasks */
1012 return 0;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001013}
Ingo Molnardeeeccd2008-01-25 21:08:15 +01001014
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001015static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
1016{
1017 int weight = cpus_weight(*new_mask);
1018
1019 BUG_ON(!rt_task(p));
1020
1021 /*
1022 * Update the migration status of the RQ if we have an RT task
1023 * which is running AND changing its weight value.
1024 */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001025 if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001026 struct rq *rq = task_rq(p);
1027
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001028 if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001029 rq->rt.rt_nr_migratory++;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001030 } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001031 BUG_ON(!rq->rt.rt_nr_migratory);
1032 rq->rt.rt_nr_migratory--;
1033 }
1034
1035 update_rt_migration(rq);
1036 }
1037
1038 p->cpus_allowed = *new_mask;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001039 p->rt.nr_cpus_allowed = weight;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001040}
Ingo Molnardeeeccd2008-01-25 21:08:15 +01001041
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001042/* Assumes rq->lock is held */
1043static void join_domain_rt(struct rq *rq)
1044{
1045 if (rq->rt.overloaded)
1046 rt_set_overload(rq);
1047}
1048
1049/* Assumes rq->lock is held */
1050static void leave_domain_rt(struct rq *rq)
1051{
1052 if (rq->rt.overloaded)
1053 rt_clear_overload(rq);
1054}
Steven Rostedtcb469842008-01-25 21:08:22 +01001055
1056/*
1057 * When switch from the rt queue, we bring ourselves to a position
1058 * that we might want to pull RT tasks from other runqueues.
1059 */
1060static void switched_from_rt(struct rq *rq, struct task_struct *p,
1061 int running)
1062{
1063 /*
1064 * If there are other RT tasks then we will reschedule
1065 * and the scheduling of the other RT tasks will handle
1066 * the balancing. But if we are the last RT task
1067 * we may need to handle the pulling of RT tasks
1068 * now.
1069 */
1070 if (!rq->rt.rt_nr_running)
1071 pull_rt_task(rq);
1072}
Steven Rostedte8fa1362008-01-25 21:08:05 +01001073#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001074
Steven Rostedtcb469842008-01-25 21:08:22 +01001075/*
1076 * When switching a task to RT, we may overload the runqueue
1077 * with RT tasks. In this case we try to push them off to
1078 * other runqueues.
1079 */
1080static void switched_to_rt(struct rq *rq, struct task_struct *p,
1081 int running)
1082{
1083 int check_resched = 1;
1084
1085 /*
1086 * If we are already running, then there's nothing
1087 * that needs to be done. But if we are not running
1088 * we may need to preempt the current running task.
1089 * If that current running task is also an RT task
1090 * then see if we can move to another run queue.
1091 */
1092 if (!running) {
1093#ifdef CONFIG_SMP
1094 if (rq->rt.overloaded && push_rt_task(rq) &&
1095 /* Don't resched if we changed runqueues */
1096 rq != task_rq(p))
1097 check_resched = 0;
1098#endif /* CONFIG_SMP */
1099 if (check_resched && p->prio < rq->curr->prio)
1100 resched_task(rq->curr);
1101 }
1102}
1103
1104/*
1105 * Priority of the task has changed. This may cause
1106 * us to initiate a push or pull.
1107 */
1108static void prio_changed_rt(struct rq *rq, struct task_struct *p,
1109 int oldprio, int running)
1110{
1111 if (running) {
1112#ifdef CONFIG_SMP
1113 /*
1114 * If our priority decreases while running, we
1115 * may need to pull tasks to this runqueue.
1116 */
1117 if (oldprio < p->prio)
1118 pull_rt_task(rq);
1119 /*
1120 * If there's a higher priority task waiting to run
1121 * then reschedule.
1122 */
1123 if (p->prio > rq->rt.highest_prio)
1124 resched_task(p);
1125#else
1126 /* For UP simply resched on drop of prio */
1127 if (oldprio < p->prio)
1128 resched_task(p);
1129#endif /* CONFIG_SMP */
1130 } else {
1131 /*
1132 * This task is not running, but if it is
1133 * greater than the current running task
1134 * then reschedule.
1135 */
1136 if (p->prio < rq->curr->prio)
1137 resched_task(rq->curr);
1138 }
1139}
1140
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001141static void watchdog(struct rq *rq, struct task_struct *p)
1142{
1143 unsigned long soft, hard;
1144
1145 if (!p->signal)
1146 return;
1147
1148 soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
1149 hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
1150
1151 if (soft != RLIM_INFINITY) {
1152 unsigned long next;
1153
1154 p->rt.timeout++;
1155 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
Peter Zijlstra5a52dd52008-01-25 21:08:32 +01001156 if (p->rt.timeout > next)
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001157 p->it_sched_expires = p->se.sum_exec_runtime;
1158 }
1159}
Steven Rostedtcb469842008-01-25 21:08:22 +01001160
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001161static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001162{
Peter Zijlstra67e2be02007-12-20 15:01:17 +01001163 update_curr_rt(rq);
1164
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001165 watchdog(rq, p);
1166
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001167 /*
1168 * RR tasks need a special form of timeslice management.
1169 * FIFO tasks have no timeslices.
1170 */
1171 if (p->policy != SCHED_RR)
1172 return;
1173
Peter Zijlstrafa717062008-01-25 21:08:27 +01001174 if (--p->rt.time_slice)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001175 return;
1176
Peter Zijlstrafa717062008-01-25 21:08:27 +01001177 p->rt.time_slice = DEF_TIMESLICE;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001178
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02001179 /*
1180 * Requeue to the end of queue if we are not the only element
1181 * on the queue:
1182 */
Peter Zijlstrafa717062008-01-25 21:08:27 +01001183 if (p->rt.run_list.prev != p->rt.run_list.next) {
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02001184 requeue_task_rt(rq, p);
1185 set_tsk_need_resched(p);
1186 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001187}
1188
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001189static void set_curr_task_rt(struct rq *rq)
1190{
1191 struct task_struct *p = rq->curr;
1192
1193 p->se.exec_start = rq->clock;
1194}
1195
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001196const struct sched_class rt_sched_class = {
1197 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001198 .enqueue_task = enqueue_task_rt,
1199 .dequeue_task = dequeue_task_rt,
1200 .yield_task = yield_task_rt,
Gregory Haskinse7693a32008-01-25 21:08:09 +01001201#ifdef CONFIG_SMP
1202 .select_task_rq = select_task_rq_rt,
1203#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001204
1205 .check_preempt_curr = check_preempt_curr_rt,
1206
1207 .pick_next_task = pick_next_task_rt,
1208 .put_prev_task = put_prev_task_rt,
1209
Peter Williams681f3e62007-10-24 18:23:51 +02001210#ifdef CONFIG_SMP
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001211 .load_balance = load_balance_rt,
Peter Williamse1d14842007-10-24 18:23:51 +02001212 .move_one_task = move_one_task_rt,
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001213 .set_cpus_allowed = set_cpus_allowed_rt,
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001214 .join_domain = join_domain_rt,
1215 .leave_domain = leave_domain_rt,
Steven Rostedt9a897c52008-01-25 21:08:22 +01001216 .pre_schedule = pre_schedule_rt,
1217 .post_schedule = post_schedule_rt,
1218 .task_wake_up = task_wake_up_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01001219 .switched_from = switched_from_rt,
Peter Williams681f3e62007-10-24 18:23:51 +02001220#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001221
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001222 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001223 .task_tick = task_tick_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01001224
1225 .prio_changed = prio_changed_rt,
1226 .switched_to = switched_to_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001227};