blob: 4eda5f795f04323f994bcc44399f03cf28d50aaa [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 Haskins1f11eb6a2008-06-04 15:04:05 -040015 if (!rq->online)
16 return;
17
Rusty Russellc6c49272008-11-25 02:35:05 +103018 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +010019 /*
20 * Make sure the mask is visible before we set
21 * the overload count. That is checked to determine
22 * if we should look at the mask. It would be a shame
23 * if we looked at the mask, but the mask was not
24 * updated yet.
25 */
26 wmb();
Gregory Haskins637f5082008-01-25 21:08:18 +010027 atomic_inc(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +010028}
Ingo Molnar84de4272008-01-25 21:08:15 +010029
Steven Rostedt4fd29172008-01-25 21:08:06 +010030static inline void rt_clear_overload(struct rq *rq)
31{
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -040032 if (!rq->online)
33 return;
34
Steven Rostedt4fd29172008-01-25 21:08:06 +010035 /* the order here really doesn't matter */
Gregory Haskins637f5082008-01-25 21:08:18 +010036 atomic_dec(&rq->rd->rto_count);
Rusty Russellc6c49272008-11-25 02:35:05 +103037 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +010038}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010039
40static void update_rt_migration(struct rq *rq)
41{
Gregory Haskins637f5082008-01-25 21:08:18 +010042 if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) {
Gregory Haskinscdc8eb92008-01-25 21:08:23 +010043 if (!rq->rt.overloaded) {
44 rt_set_overload(rq);
45 rq->rt.overloaded = 1;
46 }
47 } else if (rq->rt.overloaded) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010048 rt_clear_overload(rq);
Gregory Haskins637f5082008-01-25 21:08:18 +010049 rq->rt.overloaded = 0;
50 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +010051}
Gregory Haskins917b6272008-12-29 09:39:53 -050052
53static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
54{
55 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
56 plist_node_init(&p->pushable_tasks, p->prio);
57 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
58}
59
60static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
61{
62 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
63}
64
65#else
66
Gregory Haskinsb07430a2009-01-14 08:55:39 -050067static inline
68void enqueue_pushable_task(struct rq *rq, struct task_struct *p) {}
69static inline
70void dequeue_pushable_task(struct rq *rq, struct task_struct *p) {}
Gregory Haskins917b6272008-12-29 09:39:53 -050071
Steven Rostedt4fd29172008-01-25 21:08:06 +010072#endif /* CONFIG_SMP */
73
Peter Zijlstra6f505b12008-01-25 21:08:30 +010074static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +010075{
Peter Zijlstra6f505b12008-01-25 21:08:30 +010076 return container_of(rt_se, struct task_struct, rt);
77}
78
79static inline int on_rt_rq(struct sched_rt_entity *rt_se)
80{
81 return !list_empty(&rt_se->run_list);
82}
83
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010084#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +010085
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +010086static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +010087{
88 if (!rt_rq->tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +010089 return RUNTIME_INF;
Peter Zijlstra6f505b12008-01-25 21:08:30 +010090
Peter Zijlstraac086bc2008-04-19 19:44:58 +020091 return rt_rq->rt_runtime;
92}
93
94static inline u64 sched_rt_period(struct rt_rq *rt_rq)
95{
96 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +010097}
98
99#define for_each_leaf_rt_rq(rt_rq, rq) \
Bharata B Rao80f40ee2008-12-15 11:56:48 +0530100 list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100101
102static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
103{
104 return rt_rq->rq;
105}
106
107static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
108{
109 return rt_se->rt_rq;
110}
111
112#define for_each_sched_rt_entity(rt_se) \
113 for (; rt_se; rt_se = rt_se->parent)
114
115static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
116{
117 return rt_se->my_q;
118}
119
120static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
121static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
122
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100123static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100124{
Dario Faggiolif6121f42008-10-03 17:40:46 +0200125 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100126 struct sched_rt_entity *rt_se = rt_rq->rt_se;
127
Dario Faggiolif6121f42008-10-03 17:40:46 +0200128 if (rt_rq->rt_nr_running) {
129 if (rt_se && !on_rt_rq(rt_se))
130 enqueue_rt_entity(rt_se);
Gregory Haskinse864c492008-12-29 09:39:49 -0500131 if (rt_rq->highest_prio.curr < curr->prio)
Peter Zijlstra10203872008-01-25 21:08:32 +0100132 resched_task(curr);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100133 }
134}
135
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100136static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100137{
138 struct sched_rt_entity *rt_se = rt_rq->rt_se;
139
140 if (rt_se && on_rt_rq(rt_se))
141 dequeue_rt_entity(rt_se);
142}
143
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100144static inline int rt_rq_throttled(struct rt_rq *rt_rq)
145{
146 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
147}
148
149static int rt_se_boosted(struct sched_rt_entity *rt_se)
150{
151 struct rt_rq *rt_rq = group_rt_rq(rt_se);
152 struct task_struct *p;
153
154 if (rt_rq)
155 return !!rt_rq->rt_nr_boosted;
156
157 p = rt_task_of(rt_se);
158 return p->prio != p->normal_prio;
159}
160
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200161#ifdef CONFIG_SMP
Rusty Russellc6c49272008-11-25 02:35:05 +1030162static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200163{
164 return cpu_rq(smp_processor_id())->rd->span;
165}
166#else
Rusty Russellc6c49272008-11-25 02:35:05 +1030167static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200168{
Rusty Russellc6c49272008-11-25 02:35:05 +1030169 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200170}
171#endif
172
173static inline
174struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
175{
176 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
177}
178
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200179static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
180{
181 return &rt_rq->tg->rt_bandwidth;
182}
183
Dhaval Giani55e12e52008-06-24 23:39:43 +0530184#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100185
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100186static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100187{
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200188 return rt_rq->rt_runtime;
189}
190
191static inline u64 sched_rt_period(struct rt_rq *rt_rq)
192{
193 return ktime_to_ns(def_rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100194}
195
196#define for_each_leaf_rt_rq(rt_rq, rq) \
197 for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
198
199static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
200{
201 return container_of(rt_rq, struct rq, rt);
202}
203
204static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
205{
206 struct task_struct *p = rt_task_of(rt_se);
207 struct rq *rq = task_rq(p);
208
209 return &rq->rt;
210}
211
212#define for_each_sched_rt_entity(rt_se) \
213 for (; rt_se; rt_se = NULL)
214
215static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
216{
217 return NULL;
218}
219
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100220static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100221{
John Blackwoodf3ade832008-08-26 15:09:43 -0400222 if (rt_rq->rt_nr_running)
223 resched_task(rq_of_rt_rq(rt_rq)->curr);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100224}
225
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100226static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100227{
228}
229
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100230static inline int rt_rq_throttled(struct rt_rq *rt_rq)
231{
232 return rt_rq->rt_throttled;
233}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200234
Rusty Russellc6c49272008-11-25 02:35:05 +1030235static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200236{
Rusty Russellc6c49272008-11-25 02:35:05 +1030237 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200238}
239
240static inline
241struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
242{
243 return &cpu_rq(cpu)->rt;
244}
245
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200246static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
247{
248 return &def_rt_bandwidth;
249}
250
Dhaval Giani55e12e52008-06-24 23:39:43 +0530251#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100252
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200253#ifdef CONFIG_SMP
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200254/*
255 * We ran out of runtime, see if we can borrow some from our neighbours.
256 */
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200257static int do_balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200258{
259 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
260 struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
261 int i, weight, more = 0;
262 u64 rt_period;
263
Rusty Russellc6c49272008-11-25 02:35:05 +1030264 weight = cpumask_weight(rd->span);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200265
266 spin_lock(&rt_b->rt_runtime_lock);
267 rt_period = ktime_to_ns(rt_b->rt_period);
Rusty Russellc6c49272008-11-25 02:35:05 +1030268 for_each_cpu(i, rd->span) {
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200269 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
270 s64 diff;
271
272 if (iter == rt_rq)
273 continue;
274
275 spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200276 /*
277 * Either all rqs have inf runtime and there's nothing to steal
278 * or __disable_runtime() below sets a specific rq to inf to
279 * indicate its been disabled and disalow stealing.
280 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200281 if (iter->rt_runtime == RUNTIME_INF)
282 goto next;
283
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200284 /*
285 * From runqueues with spare time, take 1/n part of their
286 * spare time, but no more than our period.
287 */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200288 diff = iter->rt_runtime - iter->rt_time;
289 if (diff > 0) {
Peter Zijlstra58838cf2008-07-24 12:43:13 +0200290 diff = div_u64((u64)diff, weight);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200291 if (rt_rq->rt_runtime + diff > rt_period)
292 diff = rt_period - rt_rq->rt_runtime;
293 iter->rt_runtime -= diff;
294 rt_rq->rt_runtime += diff;
295 more = 1;
296 if (rt_rq->rt_runtime == rt_period) {
297 spin_unlock(&iter->rt_runtime_lock);
298 break;
299 }
300 }
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200301next:
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200302 spin_unlock(&iter->rt_runtime_lock);
303 }
304 spin_unlock(&rt_b->rt_runtime_lock);
305
306 return more;
307}
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200308
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200309/*
310 * Ensure this RQ takes back all the runtime it lend to its neighbours.
311 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200312static void __disable_runtime(struct rq *rq)
313{
314 struct root_domain *rd = rq->rd;
315 struct rt_rq *rt_rq;
316
317 if (unlikely(!scheduler_running))
318 return;
319
320 for_each_leaf_rt_rq(rt_rq, rq) {
321 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
322 s64 want;
323 int i;
324
325 spin_lock(&rt_b->rt_runtime_lock);
326 spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200327 /*
328 * Either we're all inf and nobody needs to borrow, or we're
329 * already disabled and thus have nothing to do, or we have
330 * exactly the right amount of runtime to take out.
331 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200332 if (rt_rq->rt_runtime == RUNTIME_INF ||
333 rt_rq->rt_runtime == rt_b->rt_runtime)
334 goto balanced;
335 spin_unlock(&rt_rq->rt_runtime_lock);
336
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200337 /*
338 * Calculate the difference between what we started out with
339 * and what we current have, that's the amount of runtime
340 * we lend and now have to reclaim.
341 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200342 want = rt_b->rt_runtime - rt_rq->rt_runtime;
343
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200344 /*
345 * Greedy reclaim, take back as much as we can.
346 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030347 for_each_cpu(i, rd->span) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200348 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
349 s64 diff;
350
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200351 /*
352 * Can't reclaim from ourselves or disabled runqueues.
353 */
Peter Zijlstraf1679d02008-08-14 15:49:00 +0200354 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200355 continue;
356
357 spin_lock(&iter->rt_runtime_lock);
358 if (want > 0) {
359 diff = min_t(s64, iter->rt_runtime, want);
360 iter->rt_runtime -= diff;
361 want -= diff;
362 } else {
363 iter->rt_runtime -= want;
364 want -= want;
365 }
366 spin_unlock(&iter->rt_runtime_lock);
367
368 if (!want)
369 break;
370 }
371
372 spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200373 /*
374 * We cannot be left wanting - that would mean some runtime
375 * leaked out of the system.
376 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200377 BUG_ON(want);
378balanced:
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200379 /*
380 * Disable all the borrow logic by pretending we have inf
381 * runtime - in which case borrowing doesn't make sense.
382 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200383 rt_rq->rt_runtime = RUNTIME_INF;
384 spin_unlock(&rt_rq->rt_runtime_lock);
385 spin_unlock(&rt_b->rt_runtime_lock);
386 }
387}
388
389static void disable_runtime(struct rq *rq)
390{
391 unsigned long flags;
392
393 spin_lock_irqsave(&rq->lock, flags);
394 __disable_runtime(rq);
395 spin_unlock_irqrestore(&rq->lock, flags);
396}
397
398static void __enable_runtime(struct rq *rq)
399{
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200400 struct rt_rq *rt_rq;
401
402 if (unlikely(!scheduler_running))
403 return;
404
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200405 /*
406 * Reset each runqueue's bandwidth settings
407 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200408 for_each_leaf_rt_rq(rt_rq, rq) {
409 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
410
411 spin_lock(&rt_b->rt_runtime_lock);
412 spin_lock(&rt_rq->rt_runtime_lock);
413 rt_rq->rt_runtime = rt_b->rt_runtime;
414 rt_rq->rt_time = 0;
Zhang, Yanminbaf25732008-09-09 11:26:33 +0800415 rt_rq->rt_throttled = 0;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200416 spin_unlock(&rt_rq->rt_runtime_lock);
417 spin_unlock(&rt_b->rt_runtime_lock);
418 }
419}
420
421static void enable_runtime(struct rq *rq)
422{
423 unsigned long flags;
424
425 spin_lock_irqsave(&rq->lock, flags);
426 __enable_runtime(rq);
427 spin_unlock_irqrestore(&rq->lock, flags);
428}
429
Peter Zijlstraeff65492008-06-19 14:22:26 +0200430static int balance_runtime(struct rt_rq *rt_rq)
431{
432 int more = 0;
433
434 if (rt_rq->rt_time > rt_rq->rt_runtime) {
435 spin_unlock(&rt_rq->rt_runtime_lock);
436 more = do_balance_runtime(rt_rq);
437 spin_lock(&rt_rq->rt_runtime_lock);
438 }
439
440 return more;
441}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530442#else /* !CONFIG_SMP */
Peter Zijlstraeff65492008-06-19 14:22:26 +0200443static inline int balance_runtime(struct rt_rq *rt_rq)
444{
445 return 0;
446}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530447#endif /* CONFIG_SMP */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100448
449static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
450{
451 int i, idle = 1;
Rusty Russellc6c49272008-11-25 02:35:05 +1030452 const struct cpumask *span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200453
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200454 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200455 return 1;
456
457 span = sched_rt_period_mask();
Rusty Russellc6c49272008-11-25 02:35:05 +1030458 for_each_cpu(i, span) {
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200459 int enqueue = 0;
460 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
461 struct rq *rq = rq_of_rt_rq(rt_rq);
462
463 spin_lock(&rq->lock);
464 if (rt_rq->rt_time) {
465 u64 runtime;
466
467 spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200468 if (rt_rq->rt_throttled)
469 balance_runtime(rt_rq);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200470 runtime = rt_rq->rt_runtime;
471 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
472 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
473 rt_rq->rt_throttled = 0;
474 enqueue = 1;
475 }
476 if (rt_rq->rt_time || rt_rq->rt_nr_running)
477 idle = 0;
478 spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstra8a8cde12008-06-19 14:22:28 +0200479 } else if (rt_rq->rt_nr_running)
480 idle = 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200481
482 if (enqueue)
483 sched_rt_rq_enqueue(rt_rq);
484 spin_unlock(&rq->lock);
485 }
486
487 return idle;
488}
489
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100490static inline int rt_se_prio(struct sched_rt_entity *rt_se)
491{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100492#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100493 struct rt_rq *rt_rq = group_rt_rq(rt_se);
494
495 if (rt_rq)
Gregory Haskinse864c492008-12-29 09:39:49 -0500496 return rt_rq->highest_prio.curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100497#endif
498
499 return rt_task_of(rt_se)->prio;
500}
501
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100502static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100503{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100504 u64 runtime = sched_rt_runtime(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100505
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100506 if (rt_rq->rt_throttled)
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100507 return rt_rq_throttled(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100508
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200509 if (sched_rt_runtime(rt_rq) >= sched_rt_period(rt_rq))
510 return 0;
511
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200512 balance_runtime(rt_rq);
513 runtime = sched_rt_runtime(rt_rq);
514 if (runtime == RUNTIME_INF)
515 return 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200516
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100517 if (rt_rq->rt_time > runtime) {
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100518 rt_rq->rt_throttled = 1;
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100519 if (rt_rq_throttled(rt_rq)) {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100520 sched_rt_rq_dequeue(rt_rq);
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100521 return 1;
522 }
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100523 }
524
525 return 0;
526}
527
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200528/*
529 * Update the current task's runtime statistics. Skip current tasks that
530 * are not in our scheduling class.
531 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200532static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200533{
534 struct task_struct *curr = rq->curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100535 struct sched_rt_entity *rt_se = &curr->rt;
536 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200537 u64 delta_exec;
538
539 if (!task_has_rt_policy(curr))
540 return;
541
Ingo Molnard2819182007-08-09 11:16:47 +0200542 delta_exec = rq->clock - curr->se.exec_start;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200543 if (unlikely((s64)delta_exec < 0))
544 delta_exec = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200545
546 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200547
548 curr->se.sum_exec_runtime += delta_exec;
Frank Mayharf06febc2008-09-12 09:54:39 -0700549 account_group_exec_runtime(curr, delta_exec);
550
Ingo Molnard2819182007-08-09 11:16:47 +0200551 curr->se.exec_start = rq->clock;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100552 cpuacct_charge(curr, delta_exec);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100553
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200554 if (!rt_bandwidth_enabled())
555 return;
556
Dhaval Giani354d60c2008-04-19 19:44:59 +0200557 for_each_sched_rt_entity(rt_se) {
558 rt_rq = rt_rq_of_se(rt_se);
559
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200560 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
Dimitri Sivaniche113a742008-10-31 08:03:41 -0500561 spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200562 rt_rq->rt_time += delta_exec;
563 if (sched_rt_runtime_exceeded(rt_rq))
564 resched_task(curr);
Dimitri Sivaniche113a742008-10-31 08:03:41 -0500565 spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200566 }
Dhaval Giani354d60c2008-04-19 19:44:59 +0200567 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200568}
569
Gregory Haskinse864c492008-12-29 09:39:49 -0500570#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
571
572static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu);
573
574static inline int next_prio(struct rq *rq)
575{
576 struct task_struct *next = pick_next_highest_task_rt(rq, rq->cpu);
577
578 if (next && rt_prio(next->prio))
579 return next->prio;
580 else
581 return MAX_RT_PRIO;
582}
583#endif
584
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100585static inline
586void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Steven Rostedt63489e42008-01-25 21:08:03 +0100587{
Gregory Haskins4d984272008-12-29 09:39:49 -0500588 int prio = rt_se_prio(rt_se);
589#ifdef CONFIG_SMP
590 struct rq *rq = rq_of_rt_rq(rt_rq);
591#endif
592
593 WARN_ON(!rt_prio(prio));
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100594 rt_rq->rt_nr_running++;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100595#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -0500596 if (prio < rt_rq->highest_prio.curr) {
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -0400597
Gregory Haskinse864c492008-12-29 09:39:49 -0500598 /*
599 * If the new task is higher in priority than anything on the
600 * run-queue, we have a new high that must be published to
601 * the world. We also know that the previous high becomes
602 * our next-highest.
603 */
604 rt_rq->highest_prio.next = rt_rq->highest_prio.curr;
605 rt_rq->highest_prio.curr = prio;
Ingo Molnar1100ac92008-06-05 12:25:37 +0200606#ifdef CONFIG_SMP
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -0400607 if (rq->online)
Gregory Haskins4d984272008-12-29 09:39:49 -0500608 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
Ingo Molnar1100ac92008-06-05 12:25:37 +0200609#endif
Gregory Haskinse864c492008-12-29 09:39:49 -0500610 } else if (prio == rt_rq->highest_prio.curr)
611 /*
612 * If the next task is equal in priority to the highest on
613 * the run-queue, then we implicitly know that the next highest
614 * task cannot be any lower than current
615 */
616 rt_rq->highest_prio.next = prio;
617 else if (prio < rt_rq->highest_prio.next)
618 /*
619 * Otherwise, we need to recompute next-highest
620 */
621 rt_rq->highest_prio.next = next_prio(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100622#endif
Steven Rostedt764a9d62008-01-25 21:08:04 +0100623#ifdef CONFIG_SMP
Gregory Haskins4d984272008-12-29 09:39:49 -0500624 if (rt_se->nr_cpus_allowed > 1)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100625 rq->rt.rt_nr_migratory++;
626
Gregory Haskins4d984272008-12-29 09:39:49 -0500627 update_rt_migration(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100628#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100629#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100630 if (rt_se_boosted(rt_se))
631 rt_rq->rt_nr_boosted++;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200632
633 if (rt_rq->tg)
634 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
635#else
636 start_rt_bandwidth(&def_rt_bandwidth);
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100637#endif
Steven Rostedt63489e42008-01-25 21:08:03 +0100638}
639
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100640static inline
641void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Steven Rostedt63489e42008-01-25 21:08:03 +0100642{
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200643#ifdef CONFIG_SMP
Gregory Haskins4d984272008-12-29 09:39:49 -0500644 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskinse864c492008-12-29 09:39:49 -0500645 int highest_prio = rt_rq->highest_prio.curr;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200646#endif
647
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100648 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
649 WARN_ON(!rt_rq->rt_nr_running);
650 rt_rq->rt_nr_running--;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100651#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100652 if (rt_rq->rt_nr_running) {
Gregory Haskinse864c492008-12-29 09:39:49 -0500653 int prio = rt_se_prio(rt_se);
Steven Rostedt764a9d62008-01-25 21:08:04 +0100654
Gregory Haskinse864c492008-12-29 09:39:49 -0500655 WARN_ON(prio < rt_rq->highest_prio.curr);
656
657 /*
658 * This may have been our highest or next-highest priority
659 * task and therefore we may have some recomputation to do
660 */
661 if (prio == rt_rq->highest_prio.curr) {
662 struct rt_prio_array *array = &rt_rq->active;
663
664 rt_rq->highest_prio.curr =
Steven Rostedt764a9d62008-01-25 21:08:04 +0100665 sched_find_first_bit(array->bitmap);
Gregory Haskinse864c492008-12-29 09:39:49 -0500666 }
667
668 if (prio <= rt_rq->highest_prio.next)
669 rt_rq->highest_prio.next = next_prio(rq);
Steven Rostedt764a9d62008-01-25 21:08:04 +0100670 } else
Gregory Haskinse864c492008-12-29 09:39:49 -0500671 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100672#endif
673#ifdef CONFIG_SMP
Gregory Haskins4d984272008-12-29 09:39:49 -0500674 if (rt_se->nr_cpus_allowed > 1)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100675 rq->rt.rt_nr_migratory--;
676
Gregory Haskinse864c492008-12-29 09:39:49 -0500677 if (rq->online && rt_rq->highest_prio.curr != highest_prio)
678 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -0400679
Gregory Haskins4d984272008-12-29 09:39:49 -0500680 update_rt_migration(rq);
Steven Rostedt764a9d62008-01-25 21:08:04 +0100681#endif /* CONFIG_SMP */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100682#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100683 if (rt_se_boosted(rt_se))
684 rt_rq->rt_nr_boosted--;
685
686 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
687#endif
Steven Rostedt63489e42008-01-25 21:08:03 +0100688}
689
Peter Zijlstraad2a3f12008-06-19 09:06:57 +0200690static void __enqueue_rt_entity(struct sched_rt_entity *rt_se)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200691{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100692 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
693 struct rt_prio_array *array = &rt_rq->active;
694 struct rt_rq *group_rq = group_rt_rq(rt_se);
Dmitry Adamushko20b63312008-06-11 00:58:30 +0200695 struct list_head *queue = array->queue + rt_se_prio(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200696
Peter Zijlstraad2a3f12008-06-19 09:06:57 +0200697 /*
698 * Don't enqueue the group if its throttled, or when empty.
699 * The latter is a consequence of the former when a child group
700 * get throttled and the current group doesn't have any other
701 * active members.
702 */
703 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100704 return;
Steven Rostedt63489e42008-01-25 21:08:03 +0100705
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200706 list_add_tail(&rt_se->run_list, queue);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100707 __set_bit(rt_se_prio(rt_se), array->bitmap);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100708
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100709 inc_rt_tasks(rt_se, rt_rq);
710}
711
Peter Zijlstraad2a3f12008-06-19 09:06:57 +0200712static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100713{
714 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
715 struct rt_prio_array *array = &rt_rq->active;
716
717 list_del_init(&rt_se->run_list);
718 if (list_empty(array->queue + rt_se_prio(rt_se)))
719 __clear_bit(rt_se_prio(rt_se), array->bitmap);
720
721 dec_rt_tasks(rt_se, rt_rq);
722}
723
724/*
725 * Because the prio of an upper entry depends on the lower
726 * entries, we must remove entries top - down.
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100727 */
Peter Zijlstraad2a3f12008-06-19 09:06:57 +0200728static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100729{
Peter Zijlstraad2a3f12008-06-19 09:06:57 +0200730 struct sched_rt_entity *back = NULL;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100731
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +0200732 for_each_sched_rt_entity(rt_se) {
733 rt_se->back = back;
734 back = rt_se;
735 }
736
737 for (rt_se = back; rt_se; rt_se = rt_se->back) {
738 if (on_rt_rq(rt_se))
Peter Zijlstraad2a3f12008-06-19 09:06:57 +0200739 __dequeue_rt_entity(rt_se);
740 }
741}
742
743static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
744{
745 dequeue_rt_stack(rt_se);
746 for_each_sched_rt_entity(rt_se)
747 __enqueue_rt_entity(rt_se);
748}
749
750static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
751{
752 dequeue_rt_stack(rt_se);
753
754 for_each_sched_rt_entity(rt_se) {
755 struct rt_rq *rt_rq = group_rt_rq(rt_se);
756
757 if (rt_rq && rt_rq->rt_nr_running)
758 __enqueue_rt_entity(rt_se);
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +0200759 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200760}
761
762/*
763 * Adding/removing a task to/from a priority array:
764 */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100765static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
766{
767 struct sched_rt_entity *rt_se = &p->rt;
768
769 if (wakeup)
770 rt_se->timeout = 0;
771
Peter Zijlstraad2a3f12008-06-19 09:06:57 +0200772 enqueue_rt_entity(rt_se);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200773
Gregory Haskins917b6272008-12-29 09:39:53 -0500774 if (!task_current(rq, p) && p->rt.nr_cpus_allowed > 1)
775 enqueue_pushable_task(rq, p);
776
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200777 inc_cpu_load(rq, p->se.load.weight);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100778}
779
Ingo Molnarf02231e2007-08-09 11:16:48 +0200780static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200781{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100782 struct sched_rt_entity *rt_se = &p->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200783
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200784 update_curr_rt(rq);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +0200785 dequeue_rt_entity(rt_se);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200786
Gregory Haskins917b6272008-12-29 09:39:53 -0500787 dequeue_pushable_task(rq, p);
788
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200789 dec_cpu_load(rq, p->se.load.weight);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200790}
791
792/*
793 * Put task to the end of the run list without the overhead of dequeue
794 * followed by enqueue.
795 */
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200796static void
797requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200798{
Ingo Molnar1cdad712008-06-19 09:09:15 +0200799 if (on_rt_rq(rt_se)) {
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200800 struct rt_prio_array *array = &rt_rq->active;
801 struct list_head *queue = array->queue + rt_se_prio(rt_se);
802
803 if (head)
804 list_move(&rt_se->run_list, queue);
805 else
806 list_move_tail(&rt_se->run_list, queue);
Ingo Molnar1cdad712008-06-19 09:09:15 +0200807 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200808}
809
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200810static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100811{
812 struct sched_rt_entity *rt_se = &p->rt;
813 struct rt_rq *rt_rq;
814
815 for_each_sched_rt_entity(rt_se) {
816 rt_rq = rt_rq_of_se(rt_se);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200817 requeue_rt_entity(rt_rq, rt_se, head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100818 }
819}
820
821static void yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200822{
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200823 requeue_task_rt(rq, rq->curr, 0);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200824}
825
Gregory Haskinse7693a32008-01-25 21:08:09 +0100826#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +0100827static int find_lowest_rq(struct task_struct *task);
828
Gregory Haskinse7693a32008-01-25 21:08:09 +0100829static int select_task_rq_rt(struct task_struct *p, int sync)
830{
Gregory Haskins318e0892008-01-25 21:08:10 +0100831 struct rq *rq = task_rq(p);
832
833 /*
Steven Rostedte1f47d82008-01-25 21:08:12 +0100834 * If the current task is an RT task, then
835 * try to see if we can wake this RT task up on another
836 * runqueue. Otherwise simply start this RT task
837 * on its current runqueue.
838 *
839 * We want to avoid overloading runqueues. Even if
840 * the RT task is of higher priority than the current RT task.
841 * RT tasks behave differently than other tasks. If
842 * one gets preempted, we try to push it off to another queue.
843 * So trying to keep a preempting RT task on the same
844 * cache hot CPU will force the running RT task to
845 * a cold CPU. So we waste all the cache for the lower
846 * RT task in hopes of saving some of a RT task
847 * that is just being woken and probably will have
848 * cold cache anyway.
Gregory Haskins318e0892008-01-25 21:08:10 +0100849 */
Gregory Haskins17b32792008-01-25 21:08:13 +0100850 if (unlikely(rt_task(rq->curr)) &&
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100851 (p->rt.nr_cpus_allowed > 1)) {
Gregory Haskins318e0892008-01-25 21:08:10 +0100852 int cpu = find_lowest_rq(p);
853
854 return (cpu == -1) ? task_cpu(p) : cpu;
855 }
856
857 /*
858 * Otherwise, just let it ride on the affined RQ and the
859 * post-schedule router will push the preempted task away
860 */
Gregory Haskinse7693a32008-01-25 21:08:09 +0100861 return task_cpu(p);
862}
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200863
864static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
865{
Rusty Russell24600ce2008-11-25 02:35:13 +1030866 cpumask_var_t mask;
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200867
868 if (rq->curr->rt.nr_cpus_allowed == 1)
869 return;
870
Rusty Russell24600ce2008-11-25 02:35:13 +1030871 if (!alloc_cpumask_var(&mask, GFP_ATOMIC))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200872 return;
873
Rusty Russell24600ce2008-11-25 02:35:13 +1030874 if (p->rt.nr_cpus_allowed != 1
875 && cpupri_find(&rq->rd->cpupri, p, mask))
876 goto free;
877
878 if (!cpupri_find(&rq->rd->cpupri, rq->curr, mask))
879 goto free;
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200880
881 /*
882 * There appears to be other cpus that can accept
883 * current and none to run 'p', so lets reschedule
884 * to try and push current away:
885 */
886 requeue_task_rt(rq, p, 1);
887 resched_task(rq->curr);
Rusty Russell24600ce2008-11-25 02:35:13 +1030888free:
889 free_cpumask_var(mask);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200890}
891
Gregory Haskinse7693a32008-01-25 21:08:09 +0100892#endif /* CONFIG_SMP */
893
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200894/*
895 * Preempt the current task with a newly woken task if needed:
896 */
Peter Zijlstra15afe092008-09-20 23:38:02 +0200897static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int sync)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200898{
Gregory Haskins45c01e82008-05-12 21:20:41 +0200899 if (p->prio < rq->curr->prio) {
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200900 resched_task(rq->curr);
Gregory Haskins45c01e82008-05-12 21:20:41 +0200901 return;
902 }
903
904#ifdef CONFIG_SMP
905 /*
906 * If:
907 *
908 * - the newly woken task is of equal priority to the current task
909 * - the newly woken task is non-migratable while current is migratable
910 * - current will be preempted on the next reschedule
911 *
912 * we should check to see if current can readily move to a different
913 * cpu. If so, we will reschedule to allow the push logic to try
914 * to move current somewhere else, making room for our non-migratable
915 * task.
916 */
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +0200917 if (p->prio == rq->curr->prio && !need_resched())
918 check_preempt_equal_prio(rq, p);
Gregory Haskins45c01e82008-05-12 21:20:41 +0200919#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200920}
921
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100922static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
923 struct rt_rq *rt_rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200924{
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100925 struct rt_prio_array *array = &rt_rq->active;
926 struct sched_rt_entity *next = NULL;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200927 struct list_head *queue;
928 int idx;
929
930 idx = sched_find_first_bit(array->bitmap);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100931 BUG_ON(idx >= MAX_RT_PRIO);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200932
933 queue = array->queue + idx;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100934 next = list_entry(queue->next, struct sched_rt_entity, run_list);
Dmitry Adamushko326587b2008-01-25 21:08:34 +0100935
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200936 return next;
937}
938
Gregory Haskins917b6272008-12-29 09:39:53 -0500939static struct task_struct *_pick_next_task_rt(struct rq *rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100940{
941 struct sched_rt_entity *rt_se;
942 struct task_struct *p;
943 struct rt_rq *rt_rq;
944
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100945 rt_rq = &rq->rt;
946
947 if (unlikely(!rt_rq->rt_nr_running))
948 return NULL;
949
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100950 if (rt_rq_throttled(rt_rq))
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100951 return NULL;
952
953 do {
954 rt_se = pick_next_rt_entity(rq, rt_rq);
Dmitry Adamushko326587b2008-01-25 21:08:34 +0100955 BUG_ON(!rt_se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100956 rt_rq = group_rt_rq(rt_se);
957 } while (rt_rq);
958
959 p = rt_task_of(rt_se);
960 p->se.exec_start = rq->clock;
Gregory Haskins917b6272008-12-29 09:39:53 -0500961
962 return p;
963}
964
965static struct task_struct *pick_next_task_rt(struct rq *rq)
966{
967 struct task_struct *p = _pick_next_task_rt(rq);
968
969 /* The running task is never eligible for pushing */
970 if (p)
971 dequeue_pushable_task(rq, p);
972
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100973 return p;
974}
975
Ingo Molnar31ee5292007-08-09 11:16:49 +0200976static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200977{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200978 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200979 p->se.exec_start = 0;
Gregory Haskins917b6272008-12-29 09:39:53 -0500980
981 /*
982 * The previous task needs to be made eligible for pushing
983 * if it is still active
984 */
985 if (p->se.on_rq && p->rt.nr_cpus_allowed > 1)
986 enqueue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200987}
988
Peter Williams681f3e62007-10-24 18:23:51 +0200989#ifdef CONFIG_SMP
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100990
Steven Rostedte8fa1362008-01-25 21:08:05 +0100991/* Only try algorithms three times */
992#define RT_MAX_TRIES 3
993
Steven Rostedte8fa1362008-01-25 21:08:05 +0100994static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
995
Steven Rostedtf65eda42008-01-25 21:08:07 +0100996static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
997{
998 if (!task_running(rq, p) &&
Rusty Russell96f874e22008-11-25 02:35:14 +1030999 (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001000 (p->rt.nr_cpus_allowed > 1))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001001 return 1;
1002 return 0;
1003}
1004
Steven Rostedte8fa1362008-01-25 21:08:05 +01001005/* Return the second highest RT task, NULL otherwise */
Ingo Molnar79064fb2008-01-25 21:08:14 +01001006static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001007{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001008 struct task_struct *next = NULL;
1009 struct sched_rt_entity *rt_se;
1010 struct rt_prio_array *array;
1011 struct rt_rq *rt_rq;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001012 int idx;
1013
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001014 for_each_leaf_rt_rq(rt_rq, rq) {
1015 array = &rt_rq->active;
1016 idx = sched_find_first_bit(array->bitmap);
1017 next_idx:
1018 if (idx >= MAX_RT_PRIO)
1019 continue;
1020 if (next && next->prio < idx)
1021 continue;
1022 list_for_each_entry(rt_se, array->queue + idx, run_list) {
1023 struct task_struct *p = rt_task_of(rt_se);
1024 if (pick_rt_task(rq, p, cpu)) {
1025 next = p;
1026 break;
1027 }
1028 }
1029 if (!next) {
1030 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
1031 goto next_idx;
1032 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01001033 }
1034
Steven Rostedte8fa1362008-01-25 21:08:05 +01001035 return next;
1036}
1037
Rusty Russell0e3900e2008-11-25 02:35:13 +10301038static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001039
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001040static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
1041{
1042 int first;
1043
1044 /* "this_cpu" is cheaper to preempt than a remote processor */
1045 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
1046 return this_cpu;
1047
1048 first = first_cpu(*mask);
1049 if (first != NR_CPUS)
1050 return first;
1051
1052 return -1;
1053}
1054
1055static int find_lowest_rq(struct task_struct *task)
1056{
1057 struct sched_domain *sd;
Rusty Russell96f874e22008-11-25 02:35:14 +10301058 struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001059 int this_cpu = smp_processor_id();
1060 int cpu = task_cpu(task);
1061
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001062 if (task->rt.nr_cpus_allowed == 1)
1063 return -1; /* No other targets possible */
1064
1065 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
Gregory Haskins06f90db2008-01-25 21:08:13 +01001066 return -1; /* No targets found */
1067
1068 /*
Max Krasnyanskye761b772008-07-15 04:43:49 -07001069 * Only consider CPUs that are usable for migration.
1070 * I guess we might want to change cpupri_find() to ignore those
1071 * in the first place.
1072 */
Rusty Russell96f874e22008-11-25 02:35:14 +10301073 cpumask_and(lowest_mask, lowest_mask, cpu_active_mask);
Max Krasnyanskye761b772008-07-15 04:43:49 -07001074
1075 /*
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001076 * At this point we have built a mask of cpus representing the
1077 * lowest priority tasks in the system. Now we want to elect
1078 * the best one based on our affinity and topology.
1079 *
1080 * We prioritize the last cpu that the task executed on since
1081 * it is most likely cache-hot in that location.
1082 */
Rusty Russell96f874e22008-11-25 02:35:14 +10301083 if (cpumask_test_cpu(cpu, lowest_mask))
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001084 return cpu;
1085
1086 /*
1087 * Otherwise, we consult the sched_domains span maps to figure
1088 * out which cpu is logically closest to our hot cache data.
1089 */
1090 if (this_cpu == cpu)
1091 this_cpu = -1; /* Skip this_cpu opt if the same */
1092
1093 for_each_domain(cpu, sd) {
1094 if (sd->flags & SD_WAKE_AFFINE) {
1095 cpumask_t domain_mask;
1096 int best_cpu;
1097
Rusty Russell758b2cd2008-11-25 02:35:04 +10301098 cpumask_and(&domain_mask, sched_domain_span(sd),
1099 lowest_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001100
1101 best_cpu = pick_optimal_cpu(this_cpu,
1102 &domain_mask);
1103 if (best_cpu != -1)
1104 return best_cpu;
1105 }
1106 }
1107
1108 /*
1109 * And finally, if there were no matches within the domains
1110 * just give the caller *something* to work with from the compatible
1111 * locations.
1112 */
1113 return pick_optimal_cpu(this_cpu, lowest_mask);
Gregory Haskins07b40322008-01-25 21:08:10 +01001114}
1115
Steven Rostedte8fa1362008-01-25 21:08:05 +01001116/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +01001117static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001118{
1119 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001120 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +01001121 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001122
1123 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +01001124 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001125
Gregory Haskins2de0b462008-01-25 21:08:10 +01001126 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +01001127 break;
1128
Gregory Haskins07b40322008-01-25 21:08:10 +01001129 lowest_rq = cpu_rq(cpu);
1130
Steven Rostedte8fa1362008-01-25 21:08:05 +01001131 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +01001132 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +01001133 /*
1134 * We had to unlock the run queue. In
1135 * the mean time, task could have
1136 * migrated already or had its affinity changed.
1137 * Also make sure that it wasn't scheduled on its rq.
1138 */
Gregory Haskins07b40322008-01-25 21:08:10 +01001139 if (unlikely(task_rq(task) != rq ||
Rusty Russell96f874e22008-11-25 02:35:14 +10301140 !cpumask_test_cpu(lowest_rq->cpu,
1141 &task->cpus_allowed) ||
Gregory Haskins07b40322008-01-25 21:08:10 +01001142 task_running(rq, task) ||
Steven Rostedte8fa1362008-01-25 21:08:05 +01001143 !task->se.on_rq)) {
Ingo Molnar4df64c02008-01-25 21:08:15 +01001144
Steven Rostedte8fa1362008-01-25 21:08:05 +01001145 spin_unlock(&lowest_rq->lock);
1146 lowest_rq = NULL;
1147 break;
1148 }
1149 }
1150
1151 /* If this rq is still suitable use it. */
Gregory Haskinse864c492008-12-29 09:39:49 -05001152 if (lowest_rq->rt.highest_prio.curr > task->prio)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001153 break;
1154
1155 /* try again */
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001156 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001157 lowest_rq = NULL;
1158 }
1159
1160 return lowest_rq;
1161}
1162
Gregory Haskins917b6272008-12-29 09:39:53 -05001163static inline int has_pushable_tasks(struct rq *rq)
1164{
1165 return !plist_head_empty(&rq->rt.pushable_tasks);
1166}
1167
1168static struct task_struct *pick_next_pushable_task(struct rq *rq)
1169{
1170 struct task_struct *p;
1171
1172 if (!has_pushable_tasks(rq))
1173 return NULL;
1174
1175 p = plist_first_entry(&rq->rt.pushable_tasks,
1176 struct task_struct, pushable_tasks);
1177
1178 BUG_ON(rq->cpu != task_cpu(p));
1179 BUG_ON(task_current(rq, p));
1180 BUG_ON(p->rt.nr_cpus_allowed <= 1);
1181
1182 BUG_ON(!p->se.on_rq);
1183 BUG_ON(!rt_task(p));
1184
1185 return p;
1186}
1187
Steven Rostedte8fa1362008-01-25 21:08:05 +01001188/*
1189 * If the current CPU has more than one RT task, see if the non
1190 * running task can migrate over to a CPU that is running a task
1191 * of lesser priority.
1192 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001193static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001194{
1195 struct task_struct *next_task;
1196 struct rq *lowest_rq;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001197
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +01001198 if (!rq->rt.overloaded)
1199 return 0;
1200
Gregory Haskins917b6272008-12-29 09:39:53 -05001201 next_task = pick_next_pushable_task(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001202 if (!next_task)
1203 return 0;
1204
1205 retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +01001206 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001207 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001208 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001209 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01001210
1211 /*
1212 * It's possible that the next_task slipped in of
1213 * higher priority than current. If that's the case
1214 * just reschedule current.
1215 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001216 if (unlikely(next_task->prio < rq->curr->prio)) {
1217 resched_task(rq->curr);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001218 return 0;
1219 }
1220
Gregory Haskins697f0a42008-01-25 21:08:09 +01001221 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +01001222 get_task_struct(next_task);
1223
1224 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001225 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001226 if (!lowest_rq) {
1227 struct task_struct *task;
1228 /*
Gregory Haskins697f0a42008-01-25 21:08:09 +01001229 * find lock_lowest_rq releases rq->lock
Gregory Haskins15635132008-12-29 09:39:53 -05001230 * so it is possible that next_task has migrated.
1231 *
1232 * We need to make sure that the task is still on the same
1233 * run-queue and is also still the next task eligible for
1234 * pushing.
Steven Rostedte8fa1362008-01-25 21:08:05 +01001235 */
Gregory Haskins917b6272008-12-29 09:39:53 -05001236 task = pick_next_pushable_task(rq);
Gregory Haskins15635132008-12-29 09:39:53 -05001237 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1238 /*
1239 * If we get here, the task hasnt moved at all, but
1240 * it has failed to push. We will not try again,
1241 * since the other cpus will pull from us when they
1242 * are ready.
1243 */
1244 dequeue_pushable_task(rq, next_task);
1245 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001246 }
Gregory Haskins917b6272008-12-29 09:39:53 -05001247
Gregory Haskins15635132008-12-29 09:39:53 -05001248 if (!task)
1249 /* No more tasks, just exit */
1250 goto out;
1251
Gregory Haskins917b6272008-12-29 09:39:53 -05001252 /*
Gregory Haskins15635132008-12-29 09:39:53 -05001253 * Something has shifted, try again.
Gregory Haskins917b6272008-12-29 09:39:53 -05001254 */
Gregory Haskins15635132008-12-29 09:39:53 -05001255 put_task_struct(next_task);
1256 next_task = task;
1257 goto retry;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001258 }
1259
Gregory Haskins697f0a42008-01-25 21:08:09 +01001260 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001261 set_task_cpu(next_task, lowest_rq->cpu);
1262 activate_task(lowest_rq, next_task, 0);
1263
1264 resched_task(lowest_rq->curr);
1265
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001266 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001267
Steven Rostedte8fa1362008-01-25 21:08:05 +01001268out:
1269 put_task_struct(next_task);
1270
Gregory Haskins917b6272008-12-29 09:39:53 -05001271 return 1;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001272}
1273
Steven Rostedte8fa1362008-01-25 21:08:05 +01001274static void push_rt_tasks(struct rq *rq)
1275{
1276 /* push_rt_task will return true if it moved an RT */
1277 while (push_rt_task(rq))
1278 ;
1279}
1280
Steven Rostedtf65eda42008-01-25 21:08:07 +01001281static int pull_rt_task(struct rq *this_rq)
1282{
Ingo Molnar80bf3172008-01-25 21:08:17 +01001283 int this_cpu = this_rq->cpu, ret = 0, cpu;
Gregory Haskinsa8728942008-12-29 09:39:49 -05001284 struct task_struct *p;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001285 struct rq *src_rq;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001286
Gregory Haskins637f5082008-01-25 21:08:18 +01001287 if (likely(!rt_overloaded(this_rq)))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001288 return 0;
1289
Rusty Russellc6c49272008-11-25 02:35:05 +10301290 for_each_cpu(cpu, this_rq->rd->rto_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001291 if (this_cpu == cpu)
1292 continue;
1293
1294 src_rq = cpu_rq(cpu);
Gregory Haskins74ab8e42008-12-29 09:39:50 -05001295
1296 /*
1297 * Don't bother taking the src_rq->lock if the next highest
1298 * task is known to be lower-priority than our current task.
1299 * This may look racy, but if this value is about to go
1300 * logically higher, the src_rq will push this task away.
1301 * And if its going logically lower, we do not care
1302 */
1303 if (src_rq->rt.highest_prio.next >=
1304 this_rq->rt.highest_prio.curr)
1305 continue;
1306
Steven Rostedtf65eda42008-01-25 21:08:07 +01001307 /*
1308 * We can potentially drop this_rq's lock in
1309 * double_lock_balance, and another CPU could
Gregory Haskinsa8728942008-12-29 09:39:49 -05001310 * alter this_rq
Steven Rostedtf65eda42008-01-25 21:08:07 +01001311 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05001312 double_lock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01001313
1314 /*
1315 * Are there still pullable RT tasks?
1316 */
Mike Galbraith614ee1f2008-01-25 21:08:30 +01001317 if (src_rq->rt.rt_nr_running <= 1)
1318 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001319
Steven Rostedtf65eda42008-01-25 21:08:07 +01001320 p = pick_next_highest_task_rt(src_rq, this_cpu);
1321
1322 /*
1323 * Do we have an RT task that preempts
1324 * the to-be-scheduled task?
1325 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05001326 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001327 WARN_ON(p == src_rq->curr);
1328 WARN_ON(!p->se.on_rq);
1329
1330 /*
1331 * There's a chance that p is higher in priority
1332 * than what's currently running on its cpu.
1333 * This is just that p is wakeing up and hasn't
1334 * had a chance to schedule. We only pull
1335 * p if it is lower in priority than the
Gregory Haskinsa8728942008-12-29 09:39:49 -05001336 * current task on the run queue
Steven Rostedtf65eda42008-01-25 21:08:07 +01001337 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05001338 if (p->prio < src_rq->curr->prio)
Mike Galbraith614ee1f2008-01-25 21:08:30 +01001339 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001340
1341 ret = 1;
1342
1343 deactivate_task(src_rq, p, 0);
1344 set_task_cpu(p, this_cpu);
1345 activate_task(this_rq, p, 0);
1346 /*
1347 * We continue with the search, just in
1348 * case there's an even higher prio task
1349 * in another runqueue. (low likelyhood
1350 * but possible)
Steven Rostedtf65eda42008-01-25 21:08:07 +01001351 */
Steven Rostedtf65eda42008-01-25 21:08:07 +01001352 }
Mike Galbraith614ee1f2008-01-25 21:08:30 +01001353 skip:
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001354 double_unlock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01001355 }
1356
1357 return ret;
1358}
1359
Steven Rostedt9a897c52008-01-25 21:08:22 +01001360static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
Steven Rostedtf65eda42008-01-25 21:08:07 +01001361{
1362 /* Try to pull RT tasks here if we lower this rq's prio */
Gregory Haskinse864c492008-12-29 09:39:49 -05001363 if (unlikely(rt_task(prev)) && rq->rt.highest_prio.curr > prev->prio)
Steven Rostedtf65eda42008-01-25 21:08:07 +01001364 pull_rt_task(rq);
1365}
1366
Gregory Haskins967fc042008-12-29 09:39:52 -05001367/*
1368 * assumes rq->lock is held
1369 */
1370static int needs_post_schedule_rt(struct rq *rq)
1371{
Gregory Haskins917b6272008-12-29 09:39:53 -05001372 return has_pushable_tasks(rq);
Gregory Haskins967fc042008-12-29 09:39:52 -05001373}
1374
Steven Rostedt9a897c52008-01-25 21:08:22 +01001375static void post_schedule_rt(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001376{
1377 /*
Gregory Haskins967fc042008-12-29 09:39:52 -05001378 * This is only called if needs_post_schedule_rt() indicates that
1379 * we need to push tasks away
Steven Rostedte8fa1362008-01-25 21:08:05 +01001380 */
Gregory Haskins967fc042008-12-29 09:39:52 -05001381 spin_lock_irq(&rq->lock);
1382 push_rt_tasks(rq);
1383 spin_unlock_irq(&rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001384}
1385
Gregory Haskins8ae121a2008-04-23 07:13:29 -04001386/*
1387 * If we are not running and we are not going to reschedule soon, we should
1388 * try to push tasks away now
1389 */
Steven Rostedt9a897c52008-01-25 21:08:22 +01001390static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
Steven Rostedt4642daf2008-01-25 21:08:07 +01001391{
Steven Rostedt9a897c52008-01-25 21:08:22 +01001392 if (!task_running(rq, p) &&
Gregory Haskins8ae121a2008-04-23 07:13:29 -04001393 !test_tsk_need_resched(rq->curr) &&
Gregory Haskins917b6272008-12-29 09:39:53 -05001394 has_pushable_tasks(rq) &&
Gregory Haskins777c2f32008-12-29 09:39:50 -05001395 p->rt.nr_cpus_allowed > 1)
Steven Rostedt4642daf2008-01-25 21:08:07 +01001396 push_rt_tasks(rq);
1397}
1398
Peter Williams43010652007-08-09 11:16:46 +02001399static unsigned long
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001400load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02001401 unsigned long max_load_move,
1402 struct sched_domain *sd, enum cpu_idle_type idle,
1403 int *all_pinned, int *this_best_prio)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001404{
Steven Rostedtc7a1e462008-01-25 21:08:07 +01001405 /* don't touch RT tasks */
1406 return 0;
Peter Williamse1d14842007-10-24 18:23:51 +02001407}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001408
Peter Williamse1d14842007-10-24 18:23:51 +02001409static int
1410move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
1411 struct sched_domain *sd, enum cpu_idle_type idle)
1412{
Steven Rostedtc7a1e462008-01-25 21:08:07 +01001413 /* don't touch RT tasks */
1414 return 0;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001415}
Ingo Molnardeeeccd2008-01-25 21:08:15 +01001416
Mike Traviscd8ba7c2008-03-26 14:23:49 -07001417static void set_cpus_allowed_rt(struct task_struct *p,
Rusty Russell96f874e22008-11-25 02:35:14 +10301418 const struct cpumask *new_mask)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001419{
Rusty Russell96f874e22008-11-25 02:35:14 +10301420 int weight = cpumask_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001421
1422 BUG_ON(!rt_task(p));
1423
1424 /*
1425 * Update the migration status of the RQ if we have an RT task
1426 * which is running AND changing its weight value.
1427 */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001428 if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001429 struct rq *rq = task_rq(p);
1430
Gregory Haskins917b6272008-12-29 09:39:53 -05001431 if (!task_current(rq, p)) {
1432 /*
1433 * Make sure we dequeue this task from the pushable list
1434 * before going further. It will either remain off of
1435 * the list because we are no longer pushable, or it
1436 * will be requeued.
1437 */
1438 if (p->rt.nr_cpus_allowed > 1)
1439 dequeue_pushable_task(rq, p);
1440
1441 /*
1442 * Requeue if our weight is changing and still > 1
1443 */
1444 if (weight > 1)
1445 enqueue_pushable_task(rq, p);
1446
1447 }
1448
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001449 if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001450 rq->rt.rt_nr_migratory++;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001451 } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001452 BUG_ON(!rq->rt.rt_nr_migratory);
1453 rq->rt.rt_nr_migratory--;
1454 }
1455
1456 update_rt_migration(rq);
1457 }
1458
Rusty Russell96f874e22008-11-25 02:35:14 +10301459 cpumask_copy(&p->cpus_allowed, new_mask);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001460 p->rt.nr_cpus_allowed = weight;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001461}
Ingo Molnardeeeccd2008-01-25 21:08:15 +01001462
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001463/* Assumes rq->lock is held */
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04001464static void rq_online_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001465{
1466 if (rq->rt.overloaded)
1467 rt_set_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001468
Peter Zijlstra7def2be2008-06-05 14:49:58 +02001469 __enable_runtime(rq);
1470
Gregory Haskinse864c492008-12-29 09:39:49 -05001471 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001472}
1473
1474/* Assumes rq->lock is held */
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04001475static void rq_offline_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001476{
1477 if (rq->rt.overloaded)
1478 rt_clear_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001479
Peter Zijlstra7def2be2008-06-05 14:49:58 +02001480 __disable_runtime(rq);
1481
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001482 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001483}
Steven Rostedtcb469842008-01-25 21:08:22 +01001484
1485/*
1486 * When switch from the rt queue, we bring ourselves to a position
1487 * that we might want to pull RT tasks from other runqueues.
1488 */
1489static void switched_from_rt(struct rq *rq, struct task_struct *p,
1490 int running)
1491{
1492 /*
1493 * If there are other RT tasks then we will reschedule
1494 * and the scheduling of the other RT tasks will handle
1495 * the balancing. But if we are the last RT task
1496 * we may need to handle the pulling of RT tasks
1497 * now.
1498 */
1499 if (!rq->rt.rt_nr_running)
1500 pull_rt_task(rq);
1501}
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10301502
1503static inline void init_sched_rt_class(void)
1504{
1505 unsigned int i;
1506
1507 for_each_possible_cpu(i)
Mike Travis6ca09df2008-12-31 18:08:45 -08001508 alloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
1509 GFP_KERNEL, cpu_to_node(i));
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10301510}
Steven Rostedte8fa1362008-01-25 21:08:05 +01001511#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001512
Steven Rostedtcb469842008-01-25 21:08:22 +01001513/*
1514 * When switching a task to RT, we may overload the runqueue
1515 * with RT tasks. In this case we try to push them off to
1516 * other runqueues.
1517 */
1518static void switched_to_rt(struct rq *rq, struct task_struct *p,
1519 int running)
1520{
1521 int check_resched = 1;
1522
1523 /*
1524 * If we are already running, then there's nothing
1525 * that needs to be done. But if we are not running
1526 * we may need to preempt the current running task.
1527 * If that current running task is also an RT task
1528 * then see if we can move to another run queue.
1529 */
1530 if (!running) {
1531#ifdef CONFIG_SMP
1532 if (rq->rt.overloaded && push_rt_task(rq) &&
1533 /* Don't resched if we changed runqueues */
1534 rq != task_rq(p))
1535 check_resched = 0;
1536#endif /* CONFIG_SMP */
1537 if (check_resched && p->prio < rq->curr->prio)
1538 resched_task(rq->curr);
1539 }
1540}
1541
1542/*
1543 * Priority of the task has changed. This may cause
1544 * us to initiate a push or pull.
1545 */
1546static void prio_changed_rt(struct rq *rq, struct task_struct *p,
1547 int oldprio, int running)
1548{
1549 if (running) {
1550#ifdef CONFIG_SMP
1551 /*
1552 * If our priority decreases while running, we
1553 * may need to pull tasks to this runqueue.
1554 */
1555 if (oldprio < p->prio)
1556 pull_rt_task(rq);
1557 /*
1558 * If there's a higher priority task waiting to run
Steven Rostedt6fa46fa2008-03-05 10:00:12 -05001559 * then reschedule. Note, the above pull_rt_task
1560 * can release the rq lock and p could migrate.
1561 * Only reschedule if p is still on the same runqueue.
Steven Rostedtcb469842008-01-25 21:08:22 +01001562 */
Gregory Haskinse864c492008-12-29 09:39:49 -05001563 if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01001564 resched_task(p);
1565#else
1566 /* For UP simply resched on drop of prio */
1567 if (oldprio < p->prio)
1568 resched_task(p);
1569#endif /* CONFIG_SMP */
1570 } else {
1571 /*
1572 * This task is not running, but if it is
1573 * greater than the current running task
1574 * then reschedule.
1575 */
1576 if (p->prio < rq->curr->prio)
1577 resched_task(rq->curr);
1578 }
1579}
1580
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001581static void watchdog(struct rq *rq, struct task_struct *p)
1582{
1583 unsigned long soft, hard;
1584
1585 if (!p->signal)
1586 return;
1587
1588 soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
1589 hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
1590
1591 if (soft != RLIM_INFINITY) {
1592 unsigned long next;
1593
1594 p->rt.timeout++;
1595 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
Peter Zijlstra5a52dd52008-01-25 21:08:32 +01001596 if (p->rt.timeout > next)
Frank Mayharf06febc2008-09-12 09:54:39 -07001597 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001598 }
1599}
Steven Rostedtcb469842008-01-25 21:08:22 +01001600
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001601static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001602{
Peter Zijlstra67e2be02007-12-20 15:01:17 +01001603 update_curr_rt(rq);
1604
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001605 watchdog(rq, p);
1606
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001607 /*
1608 * RR tasks need a special form of timeslice management.
1609 * FIFO tasks have no timeslices.
1610 */
1611 if (p->policy != SCHED_RR)
1612 return;
1613
Peter Zijlstrafa717062008-01-25 21:08:27 +01001614 if (--p->rt.time_slice)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001615 return;
1616
Peter Zijlstrafa717062008-01-25 21:08:27 +01001617 p->rt.time_slice = DEF_TIMESLICE;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001618
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02001619 /*
1620 * Requeue to the end of queue if we are not the only element
1621 * on the queue:
1622 */
Peter Zijlstrafa717062008-01-25 21:08:27 +01001623 if (p->rt.run_list.prev != p->rt.run_list.next) {
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001624 requeue_task_rt(rq, p, 0);
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02001625 set_tsk_need_resched(p);
1626 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001627}
1628
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001629static void set_curr_task_rt(struct rq *rq)
1630{
1631 struct task_struct *p = rq->curr;
1632
1633 p->se.exec_start = rq->clock;
Gregory Haskins917b6272008-12-29 09:39:53 -05001634
1635 /* The running task is never eligible for pushing */
1636 dequeue_pushable_task(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001637}
1638
Harvey Harrison2abdad02008-04-25 10:53:13 -07001639static const struct sched_class rt_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001640 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001641 .enqueue_task = enqueue_task_rt,
1642 .dequeue_task = dequeue_task_rt,
1643 .yield_task = yield_task_rt,
1644
1645 .check_preempt_curr = check_preempt_curr_rt,
1646
1647 .pick_next_task = pick_next_task_rt,
1648 .put_prev_task = put_prev_task_rt,
1649
Peter Williams681f3e62007-10-24 18:23:51 +02001650#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08001651 .select_task_rq = select_task_rq_rt,
1652
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001653 .load_balance = load_balance_rt,
Peter Williamse1d14842007-10-24 18:23:51 +02001654 .move_one_task = move_one_task_rt,
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001655 .set_cpus_allowed = set_cpus_allowed_rt,
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04001656 .rq_online = rq_online_rt,
1657 .rq_offline = rq_offline_rt,
Steven Rostedt9a897c52008-01-25 21:08:22 +01001658 .pre_schedule = pre_schedule_rt,
Gregory Haskins967fc042008-12-29 09:39:52 -05001659 .needs_post_schedule = needs_post_schedule_rt,
Steven Rostedt9a897c52008-01-25 21:08:22 +01001660 .post_schedule = post_schedule_rt,
1661 .task_wake_up = task_wake_up_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01001662 .switched_from = switched_from_rt,
Peter Williams681f3e62007-10-24 18:23:51 +02001663#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001664
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001665 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001666 .task_tick = task_tick_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01001667
1668 .prio_changed = prio_changed_rt,
1669 .switched_to = switched_to_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001670};
Peter Zijlstraada18de2008-06-19 14:22:24 +02001671
1672#ifdef CONFIG_SCHED_DEBUG
1673extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
1674
1675static void print_rt_stats(struct seq_file *m, int cpu)
1676{
1677 struct rt_rq *rt_rq;
1678
1679 rcu_read_lock();
1680 for_each_leaf_rt_rq(rt_rq, cpu_rq(cpu))
1681 print_rt_rq(m, cpu, rt_rq);
1682 rcu_read_unlock();
1683}
Dhaval Giani55e12e52008-06-24 23:39:43 +05301684#endif /* CONFIG_SCHED_DEBUG */
Rusty Russell0e3900e2008-11-25 02:35:13 +10301685