Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
  3  * policies)
  4  */
  5 
  6 #ifdef CONFIG_SMP
  7 
  8 static inline int rt_overloaded(struct rq *rq)
  9 {
 10         return atomic_read(&rq->rd->rto_count);
 11 }
 12 
 13 static inline void rt_set_overload(struct rq *rq)
 14 {
 15         if (!rq->online)
 16                 return;
 17 
 18         cpu_set(rq->cpu, rq->rd->rto_mask);
 19         /*
 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();
 27         atomic_inc(&rq->rd->rto_count);
 28 }
 29 
 30 static inline void rt_clear_overload(struct rq *rq)
 31 {
 32         if (!rq->online)
 33                 return;
 34 
 35         /* the order here really doesn't matter */
 36         atomic_dec(&rq->rd->rto_count);
 37         cpu_clear(rq->cpu, rq->rd->rto_mask);
 38 }
 39 
 40 static void update_rt_migration(struct rq *rq)
 41 {
 42         if (unlikely(num_online_cpus() == 1))
 43                 return;
 44 
 45         if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) {
 46                 if (!rq->rt.overloaded) {
 47                         rt_set_overload(rq);
 48                         rq->rt.overloaded = 1;
 49                 }
 50         } else if (rq->rt.overloaded) {
 51                 rt_clear_overload(rq);
 52                 rq->rt.overloaded = 0;
 53         }
 54 }
 55 #endif /* CONFIG_SMP */
 56 
 57 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
 58 {
 59         return container_of(rt_se, struct task_struct, rt);
 60 }
 61 
 62 static inline int on_rt_rq(struct sched_rt_entity *rt_se)
 63 {
 64         return !list_empty(&rt_se->run_list);
 65 }
 66 
 67 #ifdef CONFIG_RT_GROUP_SCHED
 68 
 69 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
 70 {
 71         if (!rt_rq->tg)
 72                 return RUNTIME_INF;
 73 
 74         return rt_rq->tg->rt_runtime;
 75 }
 76 
 77 #define for_each_leaf_rt_rq(rt_rq, rq) \
 78         list_for_each_entry(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
 79 
 80 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
 81 {
 82         return rt_rq->rq;
 83 }
 84 
 85 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
 86 {
 87         return rt_se->rt_rq;
 88 }
 89 
 90 #define for_each_sched_rt_entity(rt_se) \
 91         for (; rt_se; rt_se = rt_se->parent)
 92 
 93 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
 94 {
 95         return rt_se->my_q;
 96 }
 97 
 98 static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
 99 static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
100 
101 static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
102 {
103         struct sched_rt_entity *rt_se = rt_rq->rt_se;
104 
105         if (rt_se && !on_rt_rq(rt_se) && rt_rq->rt_nr_running) {
106                 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
107 
108                 enqueue_rt_entity(rt_se);
109                 if (rt_rq->highest_prio < curr->prio)
110                         resched_task(curr);
111         }
112 }
113 
114 static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
115 {
116         struct sched_rt_entity *rt_se = rt_rq->rt_se;
117 
118         if (rt_se && on_rt_rq(rt_se))
119                 dequeue_rt_entity(rt_se);
120 }
121 
122 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
123 {
124         return 0;
125         return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
126 }
127 
128 static int rt_se_boosted(struct sched_rt_entity *rt_se)
129 {
130         struct rt_rq *rt_rq = group_rt_rq(rt_se);
131         struct task_struct *p;
132 
133         if (rt_rq)
134                 return !!rt_rq->rt_nr_boosted;
135 
136         p = rt_task_of(rt_se);
137         return p->prio != p->normal_prio;
138 }
139 
140 #else
141 
142 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
143 {
144         if (sysctl_sched_rt_runtime == -1)
145                 return RUNTIME_INF;
146 
147         return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
148 }
149 
150 #define for_each_leaf_rt_rq(rt_rq, rq) \
151         for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
152 
153 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
154 {
155         return container_of(rt_rq, struct rq, rt);
156 }
157 
158 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
159 {
160         struct task_struct *p = rt_task_of(rt_se);
161         struct rq *rq = task_rq(p);
162 
163         return &rq->rt;
164 }
165 
166 #define for_each_sched_rt_entity(rt_se) \
167         for (; rt_se; rt_se = NULL)
168 
169 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
170 {
171         return NULL;
172 }
173 
174 static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
175 {
176 }
177 
178 static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
179 {
180 }
181 
182 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
183 {
184         return 0;
185         return rt_rq->rt_throttled;
186 }
187 #endif
188 
189 static inline int rt_se_prio(struct sched_rt_entity *rt_se)
190 {
191 #ifdef CONFIG_RT_GROUP_SCHED
192         struct rt_rq *rt_rq = group_rt_rq(rt_se);
193 
194         if (rt_rq)
195                 return rt_rq->highest_prio;
196 #endif
197 
198         return rt_task_of(rt_se)->prio;
199 }
200 
201 static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
202 {
203         u64 runtime = sched_rt_runtime(rt_rq);
204 
205         return 0;
206         if (runtime == RUNTIME_INF)
207                 return 0;
208 
209         if (rt_rq->rt_throttled)
210                 return rt_rq_throttled(rt_rq);
211 
212         if (rt_rq->rt_time > runtime) {
213                 struct rq *rq = rq_of_rt_rq(rt_rq);
214 
215                 rq->rt_throttled = 1;
216                 rt_rq->rt_throttled = 1;
217 
218                 if (rt_rq_throttled(rt_rq)) {
219                         sched_rt_rq_dequeue(rt_rq);
220                         return 1;
221                 }
222         }
223 
224         return 0;
225 }
226 
227 static void update_sched_rt_period(struct rq *rq)
228 {
229         struct rt_rq *rt_rq;
230         u64 period;
231 
232         return;
233         while (rq->clock > rq->rt_period_expire) {
234                 period = (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
235                 rq->rt_period_expire += period;
236 
237                 for_each_leaf_rt_rq(rt_rq, rq) {
238                         u64 runtime = sched_rt_runtime(rt_rq);
239 
240                         rt_rq->rt_time -= min(rt_rq->rt_time, runtime);
241                         if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
242                                 rt_rq->rt_throttled = 0;
243                                 sched_rt_rq_enqueue(rt_rq);
244                         }
245                 }
246 
247                 rq->rt_throttled = 0;
248         }
249 }
250 
251 /*
252  * Update the current task's runtime statistics. Skip current tasks that
253  * are not in our scheduling class.
254  */
255 static void update_curr_rt(struct rq *rq)
256 {
257         struct task_struct *curr = rq->curr;
258         struct sched_rt_entity *rt_se = &curr->rt;
259         struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
260         u64 delta_exec;
261 
262         if (!task_has_rt_policy(curr))
263                 return;
264 
265         delta_exec = rq->clock - curr->se.exec_start;
266         if (unlikely((s64)delta_exec < 0))
267                 delta_exec = 0;
268 
269         schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
270 
271         curr->se.sum_exec_runtime += delta_exec;
272         curr->se.exec_start = rq->clock;
273         cpuacct_charge(curr, delta_exec);
274 
275 //      rt_rq->rt_time += delta_exec;
276         if (sched_rt_runtime_exceeded(rt_rq))
277                 resched_task(curr);
278 }
279 
280 static inline
281 void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
282 {
283         WARN_ON(!rt_prio(rt_se_prio(rt_se)));
284         rt_rq->rt_nr_running++;
285 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
286         if (rt_se_prio(rt_se) < rt_rq->highest_prio) {
287                 struct rq *rq = rq_of_rt_rq(rt_rq);
288                 rt_rq->highest_prio = rt_se_prio(rt_se);
289 
290                 if (rq->online)
291                         cpupri_set(&rq->rd->cpupri, rq->cpu,
292                                    rt_se_prio(rt_se));
293         }
294 #endif
295 #ifdef CONFIG_SMP
296         if (rt_se->nr_cpus_allowed > 1) {
297                 struct rq *rq = rq_of_rt_rq(rt_rq);
298                 rq->rt.rt_nr_migratory++;
299         }
300 
301         update_rt_migration(rq_of_rt_rq(rt_rq));
302 #endif
303 #ifdef CONFIG_RT_GROUP_SCHED
304         if (rt_se_boosted(rt_se))
305                 rt_rq->rt_nr_boosted++;
306 #endif
307 }
308 
309 static inline
310 void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
311 {
312 #ifdef CONFIG_SMP
313         int highest_prio = rt_rq->highest_prio;
314 #endif
315 
316         WARN_ON(!rt_prio(rt_se_prio(rt_se)));
317         WARN_ON(!rt_rq->rt_nr_running);
318         rt_rq->rt_nr_running--;
319 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
320         if (rt_rq->rt_nr_running) {
321                 struct rt_prio_array *array;
322 
323                 WARN_ON(rt_se_prio(rt_se) < rt_rq->highest_prio);
324                 if (rt_se_prio(rt_se) == rt_rq->highest_prio) {
325                         /* recalculate */
326                         array = &rt_rq->active;
327                         rt_rq->highest_prio =
328                                 sched_find_first_bit(array->bitmap);
329                 } /* otherwise leave rq->highest prio alone */
330         } else
331                 rt_rq->highest_prio = MAX_RT_PRIO;
332 #endif
333 #ifdef CONFIG_SMP
334         if (rt_se->nr_cpus_allowed > 1) {
335                 struct rq *rq = rq_of_rt_rq(rt_rq);
336                 BUG_ON(!rq->rt.rt_nr_migratory);
337                 rq->rt.rt_nr_migratory--;
338         }
339 
340         if (rt_rq->highest_prio != highest_prio) {
341                 struct rq *rq = rq_of_rt_rq(rt_rq);
342 
343                 if (rq->online)
344                         cpupri_set(&rq->rd->cpupri, rq->cpu,
345                                    rt_rq->highest_prio);
346         }
347 
348         update_rt_migration(rq_of_rt_rq(rt_rq));
349 #endif /* CONFIG_SMP */
350 #ifdef CONFIG_RT_GROUP_SCHED
351         if (rt_se_boosted(rt_se))
352                 rt_rq->rt_nr_boosted--;
353 
354         WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
355 #endif
356 }
357 
358 static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
359 {
360         struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
361         struct rt_prio_array *array = &rt_rq->active;
362         struct rt_rq *group_rq = group_rt_rq(rt_se);
363 
364         if (group_rq && rt_rq_throttled(group_rq))
365                 return;
366 
367         if (rt_se->nr_cpus_allowed == 1)
368                 list_add_tail(&rt_se->run_list,
369                               array->xqueue + rt_se_prio(rt_se));
370         else
371                 list_add_tail(&rt_se->run_list,
372                               array->squeue + rt_se_prio(rt_se));
373 
374         __set_bit(rt_se_prio(rt_se), array->bitmap);
375 
376         inc_rt_tasks(rt_se, rt_rq);
377 }
378 
379 static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
380 {
381         struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
382         struct rt_prio_array *array = &rt_rq->active;
383 
384         list_del_init(&rt_se->run_list);
385         if (list_empty(array->squeue + rt_se_prio(rt_se))
386             && list_empty(array->xqueue + rt_se_prio(rt_se)))
387                 __clear_bit(rt_se_prio(rt_se), array->bitmap);
388 
389         dec_rt_tasks(rt_se, rt_rq);
390 }
391 
392 /*
393  * Because the prio of an upper entry depends on the lower
394  * entries, we must remove entries top - down.
395  *
396  * XXX: O(1/2 h^2) because we can only walk up, not down the chain.
397  *      doesn't matter much for now, as h=2 for GROUP_SCHED.
398  */
399 static void dequeue_rt_stack(struct task_struct *p)
400 {
401         struct sched_rt_entity *rt_se, *top_se;
402 
403         /*
404          * dequeue all, top - down.
405          */
406         do {
407                 rt_se = &p->rt;
408                 top_se = NULL;
409                 for_each_sched_rt_entity(rt_se) {
410                         if (on_rt_rq(rt_se))
411                                 top_se = rt_se;
412                 }
413                 if (top_se)
414                         dequeue_rt_entity(top_se);
415         } while (top_se);
416 }
417 
418 static inline void incr_rt_nr_uninterruptible(struct task_struct *p,
419                                               struct rq *rq)
420 {
421         rq->rt.rt_nr_uninterruptible++;
422 }
423 
424 static inline void decr_rt_nr_uninterruptible(struct task_struct *p,
425                                               struct rq *rq)
426 {
427         rq->rt.rt_nr_uninterruptible--;
428 }
429 
430 unsigned long rt_nr_running(void)
431 {
432         unsigned long i, sum = 0;
433 
434         for_each_online_cpu(i)
435                 sum += cpu_rq(i)->rt.rt_nr_running;
436 
437         return sum;
438 }
439 
440 unsigned long rt_nr_running_cpu(int cpu)
441 {
442         return cpu_rq(cpu)->rt.rt_nr_running;
443 }
444 
445 unsigned long rt_nr_uninterruptible(void)
446 {
447         unsigned long i, sum = 0;
448 
449         for_each_online_cpu(i)
450                 sum += cpu_rq(i)->rt.rt_nr_uninterruptible;
451 
452         /*
453          * Since we read the counters lockless, it might be slightly
454          * inaccurate. Do not allow it to go below zero though:
455          */
456         if (unlikely((long)sum < 0))
457                 sum = 0;
458 
459         return sum;
460 }
461 
462 unsigned long rt_nr_uninterruptible_cpu(int cpu)
463 {
464         return cpu_rq(cpu)->rt.rt_nr_uninterruptible;
465 }
466 
467 /*
468  * Adding/removing a task to/from a priority array:
469  */
470 static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
471 {
472         struct sched_rt_entity *rt_se = &p->rt;
473 
474         if (wakeup)
475                 rt_se->timeout = 0;
476 
477         dequeue_rt_stack(p);
478 
479         /*
480          * enqueue everybody, bottom - up.
481          */
482         for_each_sched_rt_entity(rt_se)
483                 enqueue_rt_entity(rt_se);
484 
485         if (p->state == TASK_UNINTERRUPTIBLE)
486                 decr_rt_nr_uninterruptible(p, rq);
487 }
488 
489 static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
490 {
491         struct sched_rt_entity *rt_se = &p->rt;
492         struct rt_rq *rt_rq;
493 
494         update_curr_rt(rq);
495 
496         if (p->state == TASK_UNINTERRUPTIBLE)
497                 incr_rt_nr_uninterruptible(p, rq);
498 
499         dequeue_rt_stack(p);
500 
501         /*
502          * re-enqueue all non-empty rt_rq entities.
503          */
504         for_each_sched_rt_entity(rt_se) {
505                 rt_rq = group_rt_rq(rt_se);
506                 if (rt_rq && rt_rq->rt_nr_running)
507                         enqueue_rt_entity(rt_se);
508         }
509 }
510 
511 /*
512  * Put task to the end of the run list without the overhead of dequeue
513  * followed by enqueue.
514  *
515  * Note: We always enqueue the task to the shared-queue, regardless of its
516  * previous position w.r.t. exclusive vs shared.  This is so that exclusive RR
517  * tasks fairly round-robin with all tasks on the runqueue, not just other
518  * exclusive tasks.
519  */
520 static
521 void requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
522 {
523         struct rt_prio_array *array = &rt_rq->active;
524 
525         list_del_init(&rt_se->run_list);
526         list_add_tail(&rt_se->run_list, array->squeue + rt_se_prio(rt_se));
527 }
528 
529 static void requeue_task_rt(struct rq *rq, struct task_struct *p)
530 {
531         struct sched_rt_entity *rt_se = &p->rt;
532         struct rt_rq *rt_rq;
533 
534         for_each_sched_rt_entity(rt_se) {
535                 rt_rq = rt_rq_of_se(rt_se);
536                 requeue_rt_entity(rt_rq, rt_se);
537         }
538 }
539 
540 static void yield_task_rt(struct rq *rq)
541 {
542         requeue_task_rt(rq, rq->curr);
543 }
544 
545 #ifdef CONFIG_SMP
546 static int find_lowest_rq(struct task_struct *task);
547 
548 static int select_task_rq_rt(struct task_struct *p, int sync)
549 {
550         struct rq *rq = task_rq(p);
551 
552         /*
553          * If the current task is an RT task, then
554          * try to see if we can wake this RT task up on another
555          * runqueue. Otherwise simply start this RT task
556          * on its current runqueue.
557          *
558          * We want to avoid overloading runqueues. Even if
559          * the RT task is of higher priority than the current RT task.
560          * RT tasks behave differently than other tasks. If
561          * one gets preempted, we try to push it off to another queue.
562          * So trying to keep a preempting RT task on the same
563          * cache hot CPU will force the running RT task to
564          * a cold CPU. So we waste all the cache for the lower
565          * RT task in hopes of saving some of a RT task
566          * that is just being woken and probably will have
567          * cold cache anyway.
568          */
569         if (unlikely(rt_task(rq->curr)) &&
570             (p->rt.nr_cpus_allowed > 1)) {
571                 int cpu = find_lowest_rq(p);
572 
573                 return (cpu == -1) ? task_cpu(p) : cpu;
574         }
575 
576         /*
577          * Otherwise, just let it ride on the affined RQ and the
578          * post-schedule router will push the preempted task away
579          */
580         return task_cpu(p);
581 }
582 #endif /* CONFIG_SMP */
583 
584 static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
585                                                    struct rt_rq *rt_rq);
586 
587 /*
588  * Preempt the current task with a newly woken task if needed:
589  */
590 static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
591 {
592         if (p->prio < rq->curr->prio) {
593                 resched_task(rq->curr);
594                 return;
595         }
596 
597 #ifdef CONFIG_SMP
598         /*
599          * If:
600          *
601          * - the newly woken task is of equal priority to the current task
602          * - the newly woken task is non-migratable while current is migratable
603          * - current will be preempted on the next reschedule
604          *
605          * we should check to see if current can readily move to a different
606          * cpu.  If so, we will reschedule to allow the push logic to try
607          * to move current somewhere else, making room for our non-migratable
608          * task.
609          */
610         if((p->prio == rq->curr->prio)
611            && p->rt.nr_cpus_allowed == 1
612            && rq->curr->rt.nr_cpus_allowed != 1
613            && pick_next_rt_entity(rq, &rq->rt) != &rq->curr->rt) {
614                 cpumask_t mask;
615 
616                 if (cpupri_find(&rq->rd->cpupri, rq->curr, &mask))
617                         /*
618                          * There appears to be other cpus that can accept
619                          * current, so lets reschedule to try and push it away
620                          */
621                         resched_task(rq->curr);
622         }
623 #endif
624 }
625 
626 static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
627                                                    struct rt_rq *rt_rq)
628 {
629         struct rt_prio_array *array = &rt_rq->active;
630         struct sched_rt_entity *next = NULL;
631         struct list_head *queue;
632         int idx;
633 
634         idx = sched_find_first_bit(array->bitmap);
635         BUG_ON(idx >= MAX_RT_PRIO);
636 
637         queue = array->xqueue + idx;
638         if (!list_empty(queue))
639                 next = list_entry(queue->next, struct sched_rt_entity,
640                                   run_list);
641         else {
642                 queue = array->squeue + idx;
643                 next = list_entry(queue->next, struct sched_rt_entity,
644                                   run_list);
645         }
646 
647         return next;
648 }
649 
650 static struct task_struct *pick_next_task_rt(struct rq *rq)
651 {
652         struct sched_rt_entity *rt_se;
653         struct task_struct *p;
654         struct rt_rq *rt_rq;
655 
656         rt_rq = &rq->rt;
657 
658         if (unlikely(!rt_rq->rt_nr_running))
659                 return NULL;
660 
661         if (rt_rq_throttled(rt_rq))
662                 return NULL;
663 
664         do {
665                 rt_se = pick_next_rt_entity(rq, rt_rq);
666                 BUG_ON(!rt_se);
667                 rt_rq = group_rt_rq(rt_se);
668         } while (rt_rq);
669 
670         p = rt_task_of(rt_se);
671         p->se.exec_start = rq->clock;
672         return p;
673 }
674 
675 static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
676 {
677         update_curr_rt(rq);
678         p->se.exec_start = 0;
679 }
680 
681 #ifdef CONFIG_SMP
682 
683 /* Only try algorithms three times */
684 #define RT_MAX_TRIES 3
685 
686 static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
687 static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
688 
689 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
690 {
691         if (!task_running(rq, p) &&
692             (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
693             (p->rt.nr_cpus_allowed > 1))
694                 return 1;
695         return 0;
696 }
697 
698 /* Return the second highest RT task, NULL otherwise */
699 static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
700 {
701         struct task_struct *next = NULL;
702         struct sched_rt_entity *rt_se;
703         struct rt_prio_array *array;
704         struct rt_rq *rt_rq;
705         int idx;
706 
707         for_each_leaf_rt_rq(rt_rq, rq) {
708                 array = &rt_rq->active;
709                 idx = sched_find_first_bit(array->bitmap);
710  next_idx:
711                 if (idx >= MAX_RT_PRIO)
712                         continue;
713                 if (next && next->prio < idx)
714                         continue;
715                 list_for_each_entry(rt_se, array->squeue + idx, run_list) {
716                         struct task_struct *p = rt_task_of(rt_se);
717                         if (pick_rt_task(rq, p, cpu)) {
718                                 next = p;
719                                 break;
720                         }
721                 }
722                 if (!next) {
723                         idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
724                         goto next_idx;
725                 }
726         }
727 
728         return next;
729 }
730 
731 static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
732 
733 static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
734 {
735         int first;
736 
737         /* "this_cpu" is cheaper to preempt than a remote processor */
738         if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
739                 return this_cpu;
740 
741         first = first_cpu(*mask);
742         if (first != NR_CPUS)
743                 return first;
744 
745         return -1;
746 }
747 
748 static int find_lowest_rq(struct task_struct *task)
749 {
750         struct sched_domain *sd;
751         cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
752         int this_cpu = smp_processor_id();
753         int cpu      = task_cpu(task);
754 
755         if (task->rt.nr_cpus_allowed == 1)
756                 return -1; /* No other targets possible */
757 
758         if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
759                 return -1; /* No targets found */
760 
761         /*
762          * At this point we have built a mask of cpus representing the
763          * lowest priority tasks in the system.  Now we want to elect
764          * the best one based on our affinity and topology.
765          *
766          * We prioritize the last cpu that the task executed on since
767          * it is most likely cache-hot in that location.
768          */
769         if (cpu_isset(cpu, *lowest_mask))
770                 return cpu;
771 
772         /*
773          * Otherwise, we consult the sched_domains span maps to figure
774          * out which cpu is logically closest to our hot cache data.
775          */
776         if (this_cpu == cpu)
777                 this_cpu = -1; /* Skip this_cpu opt if the same */
778 
779         for_each_domain(cpu, sd) {
780                 if (sd->flags & SD_WAKE_AFFINE) {
781                         cpumask_t domain_mask;
782                         int       best_cpu;
783 
784                         cpus_and(domain_mask, sd->span, *lowest_mask);
785 
786                         best_cpu = pick_optimal_cpu(this_cpu,
787                                                     &domain_mask);
788                         if (best_cpu != -1)
789                                 return best_cpu;
790                 }
791         }
792 
793         /*
794          * And finally, if there were no matches within the domains
795          * just give the caller *something* to work with from the compatible
796          * locations.
797          */
798         return pick_optimal_cpu(this_cpu, lowest_mask);
799 }
800 
801 /* Will lock the rq it finds */
802 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
803 {
804         struct rq *lowest_rq = NULL;
805         int tries;
806         int cpu;
807 
808         for (tries = 0; tries < RT_MAX_TRIES; tries++) {
809                 cpu = find_lowest_rq(task);
810 
811                 if ((cpu == -1) || (cpu == rq->cpu))
812                         break;
813 
814                 lowest_rq = cpu_rq(cpu);
815 
816                 /* if the prio of this runqueue changed, try again */
817                 if (double_lock_balance(rq, lowest_rq)) {
818                         /*
819                          * We had to unlock the run queue. In
820                          * the mean time, task could have
821                          * migrated already or had its affinity changed.
822                          * Also make sure that it wasn't scheduled on its rq.
823                          */
824                         if (unlikely(task_rq(task) != rq ||
825                                      !cpu_isset(lowest_rq->cpu,
826                                                 task->cpus_allowed) ||
827                                      task_running(rq, task) ||
828                                      !task->se.on_rq)) {
829 
830                                 spin_unlock(&lowest_rq->lock);
831                                 lowest_rq = NULL;
832                                 break;
833                         }
834                 }
835 
836                 /* If this rq is still suitable use it. */
837                 if (lowest_rq->rt.highest_prio > task->prio)
838                         break;
839 
840                 /* try again */
841                 spin_unlock(&lowest_rq->lock);
842                 lowest_rq = NULL;
843         }
844 
845         return lowest_rq;
846 }
847 
848 /*
849  * If the current CPU has more than one RT task, see if the non
850  * running task can migrate over to a CPU that is running a task
851  * of lesser priority.
852  */
853 static int push_rt_task(struct rq *rq)
854 {
855         struct task_struct *next_task;
856         struct rq *lowest_rq;
857         int ret = 0;
858         int paranoid = RT_MAX_TRIES;
859 
860         if (!rq->rt.overloaded)
861                 return 0;
862 
863         next_task = pick_next_highest_task_rt(rq, -1);
864         if (!next_task)
865                 return 0;
866 
867  retry:
868         if (unlikely(next_task == rq->curr)) {
869                 WARN_ON(1);
870                 return 0;
871         }
872 
873         /*
874          * It's possible that the next_task slipped in of
875          * higher priority than current. If that's the case
876          * just reschedule current.
877          */
878         if (unlikely(next_task->prio < rq->curr->prio)) {
879                 resched_task(rq->curr);
880                 return 0;
881         }
882 
883         /* We might release rq lock */
884         get_task_struct(next_task);
885 
886         /* find_lock_lowest_rq locks the rq if found */
887         lowest_rq = find_lock_lowest_rq(next_task, rq);
888         if (!lowest_rq) {
889                 struct task_struct *task;
890                 /*
891                  * find lock_lowest_rq releases rq->lock
892                  * so it is possible that next_task has changed.
893                  * If it has, then try again.
894                  */
895                 task = pick_next_highest_task_rt(rq, -1);
896                 if (unlikely(task != next_task) && task && paranoid--) {
897                         put_task_struct(next_task);
898                         next_task = task;
899                         goto retry;
900                 }
901                 goto out;
902         }
903 
904         deactivate_task(rq, next_task, 0);
905         set_task_cpu(next_task, lowest_rq->cpu);
906         activate_task(lowest_rq, next_task, 0);
907 
908         resched_task(lowest_rq->curr);
909 
910         schedstat_inc(rq, rto_pushed);
911 
912         spin_unlock(&lowest_rq->lock);
913 
914         ret = 1;
915 out:
916         put_task_struct(next_task);
917 
918         return ret;
919 }
920 
921 /*
922  * TODO: Currently we just use the second highest prio task on
923  *       the queue, and stop when it can't migrate (or there's
924  *       no more RT tasks).  There may be a case where a lower
925  *       priority RT task has a different affinity than the
926  *       higher RT task. In this case the lower RT task could
927  *       possibly be able to migrate where as the higher priority
928  *       RT task could not.  We currently ignore this issue.
929  *       Enhancements are welcome!
930  */
931 static void push_rt_tasks(struct rq *rq)
932 {
933         /* push_rt_task will return true if it moved an RT */
934         while (push_rt_task(rq))
935                 ;
936 }
937 
938 static int pull_rt_task(struct rq *this_rq)
939 {
940         int this_cpu = this_rq->cpu, ret = 0, cpu;
941         struct task_struct *p, *next;
942         struct rq *src_rq;
943 
944         if (likely(!rt_overloaded(this_rq)))
945                 return 0;
946 
947         next = pick_next_task_rt(this_rq);
948 
949         for_each_cpu_mask(cpu, this_rq->rd->rto_mask) {
950                 if (this_cpu == cpu)
951                         continue;
952 
953                 src_rq = cpu_rq(cpu);
954                 /*
955                  * We can potentially drop this_rq's lock in
956                  * double_lock_balance, and another CPU could
957                  * steal our next task - hence we must cause
958                  * the caller to recalculate the next task
959                  * in that case:
960                  */
961                 if (double_lock_balance(this_rq, src_rq)) {
962                         struct task_struct *old_next = next;
963 
964                         next = pick_next_task_rt(this_rq);
965                         if (next != old_next)
966                                 ret = 1;
967                 }
968 
969                 /*
970                  * Are there still pullable RT tasks?
971                  */
972                 if (src_rq->rt.rt_nr_running <= 1)
973                         goto skip;
974 
975                 p = pick_next_highest_task_rt(src_rq, this_cpu);
976 
977                 /*
978                  * Do we have an RT task that preempts
979                  * the to-be-scheduled task?
980                  */
981                 if (p && (!next || (p->prio < next->prio))) {
982                         WARN_ON(p == src_rq->curr);
983                         WARN_ON(!p->se.on_rq);
984 
985                         /*
986                          * There's a chance that p is higher in priority
987                          * than what's currently running on its cpu.
988                          * This is just that p is wakeing up and hasn't
989                          * had a chance to schedule. We only pull
990                          * p if it is lower in priority than the
991                          * current task on the run queue or
992                          * this_rq next task is lower in prio than
993                          * the current task on that rq.
994                          */
995                         if (p->prio < src_rq->curr->prio ||
996                             (next && next->prio < src_rq->curr->prio))
997                                 goto skip;
998 
999                         ret = 1;
1000 
1001                         deactivate_task(src_rq, p, 0);
1002                         set_task_cpu(p, this_cpu);
1003                         activate_task(this_rq, p, 0);
1004                         /*
1005                          * We continue with the search, just in
1006                          * case there's an even higher prio task
1007                          * in another runqueue. (low likelyhood
1008                          * but possible)
1009                          *
1010                          * Update next so that we won't pick a task
1011                          * on another cpu with a priority lower (or equal)
1012                          * than the one we just picked.
1013                          */
1014                         next = p;
1015 
1016                         schedstat_inc(src_rq, rto_pulled);
1017                 }
1018  skip:
1019                 spin_unlock(&src_rq->lock);
1020         }
1021 
1022         return ret;
1023 }
1024 
1025 static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
1026 {
1027         /* Try to pull RT tasks here if we lower this rq's prio */
1028         if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio) {
1029                 pull_rt_task(rq);
1030                 schedstat_inc(rq, rto_schedule);
1031         }
1032 }
1033 
1034 static void post_schedule_rt(struct rq *rq)
1035 {
1036         /*
1037          * If we have more than one rt_task queued, then
1038          * see if we can push the other rt_tasks off to other CPUS.
1039          * Note we may release the rq lock, and since
1040          * the lock was owned by prev, we need to release it
1041          * first via finish_lock_switch and then reaquire it here.
1042          */
1043         if (unlikely(rq->rt.overloaded)) {
1044                 spin_lock(&rq->lock);
1045                 push_rt_tasks(rq);
1046                 schedstat_inc(rq, rto_schedule_tail);
1047                 spin_unlock(&rq->lock);
1048         }
1049 }
1050 
1051 
1052 static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
1053 {
1054         if (!task_running(rq, p) &&
1055             !test_tsk_need_resched(rq->curr) &&
1056             rq->rt.overloaded) {
1057                 push_rt_tasks(rq);
1058                 schedstat_inc(rq, rto_wakeup);
1059         }
1060 }
1061 
1062 static unsigned long
1063 load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
1064                 unsigned long max_load_move,
1065                 struct sched_domain *sd, enum cpu_idle_type idle,
1066                 int *all_pinned, int *this_best_prio)
1067 {
1068         /* don't touch RT tasks */
1069         return 0;
1070 }
1071 
1072 static int
1073 move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
1074                  struct sched_domain *sd, enum cpu_idle_type idle)
1075 {
1076         /* don't touch RT tasks */
1077         return 0;
1078 }
1079 
1080 static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
1081 {
1082         int weight = cpus_weight(*new_mask);
1083 
1084         BUG_ON(!rt_task(p));
1085 
1086         /*
1087          * Update the migration status of the RQ if we have an RT task
1088          * which is running AND changing its weight value.
1089          */
1090         if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
1091                 struct rq *rq = task_rq(p);
1092 
1093                 if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
1094                         rq->rt.rt_nr_migratory++;
1095                 } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
1096                         BUG_ON(!rq->rt.rt_nr_migratory);
1097                         rq->rt.rt_nr_migratory--;
1098                 }
1099 
1100                 update_rt_migration(rq);
1101 
1102                 if (unlikely(weight == 1 || p->rt.nr_cpus_allowed == 1))
1103                         /*
1104                          * If either the new or old weight is a "1", we need
1105                          * to requeue to properly move between shared and
1106                          * exclusive queues.
1107                          */
1108                         requeue_task_rt(rq, p);
1109         }
1110 
1111         p->cpus_allowed    = *new_mask;
1112         p->rt.nr_cpus_allowed = weight;
1113 }
1114 
1115 /* Assumes rq->lock is held */
1116 static void rq_online_rt(struct rq *rq)
1117 {
1118         if (rq->rt.overloaded)
1119                 rt_set_overload(rq);
1120 
1121         cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio);
1122 }
1123 
1124 /* Assumes rq->lock is held */
1125 static void rq_offline_rt(struct rq *rq)
1126 {
1127         if (rq->rt.overloaded)
1128                 rt_clear_overload(rq);
1129 
1130         cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
1131 }
1132 
1133 /*
1134  * When switch from the rt queue, we bring ourselves to a position
1135  * that we might want to pull RT tasks from other runqueues.
1136  */
1137 static void switched_from_rt(struct rq *rq, struct task_struct *p,
1138                            int running)
1139 {
1140         /*
1141          * If there are other RT tasks then we will reschedule
1142          * and the scheduling of the other RT tasks will handle
1143          * the balancing. But if we are the last RT task
1144          * we may need to handle the pulling of RT tasks
1145          * now.
1146          */
1147         if (!rq->rt.rt_nr_running)
1148                 pull_rt_task(rq);
1149 }
1150 #endif /* CONFIG_SMP */
1151 
1152 /*
1153  * When switching a task to RT, we may overload the runqueue
1154  * with RT tasks. In this case we try to push them off to
1155  * other runqueues.
1156  */
1157 static void switched_to_rt(struct rq *rq, struct task_struct *p,
1158                            int running)
1159 {
1160         int check_resched = 1;
1161 
1162         /*
1163          * If we are already running, then there's nothing
1164          * that needs to be done. But if we are not running
1165          * we may need to preempt the current running task.
1166          * If that current running task is also an RT task
1167          * then see if we can move to another run queue.
1168          */
1169         if (!running) {
1170 #ifdef CONFIG_SMP
1171                 if (rq->rt.overloaded && push_rt_task(rq) &&
1172                     /* Don't resched if we changed runqueues */
1173                     rq != task_rq(p))
1174                         check_resched = 0;
1175 #endif /* CONFIG_SMP */
1176                 if (check_resched && p->prio < rq->curr->prio)
1177                         resched_task(rq->curr);
1178         }
1179 }
1180 
1181 /*
1182  * Priority of the task has changed. This may cause
1183  * us to initiate a push or pull.
1184  */
1185 static void prio_changed_rt(struct rq *rq, struct task_struct *p,
1186                             int oldprio, int running)
1187 {
1188         if (running) {
1189 #ifdef CONFIG_SMP
1190                 /*
1191                  * If our priority decreases while running, we
1192                  * may need to pull tasks to this runqueue.
1193                  */
1194                 if (oldprio < p->prio)
1195                         pull_rt_task(rq);
1196                 /*
1197                  * If there's a higher priority task waiting to run
1198                  * then reschedule. Note, the above pull_rt_task
1199                  * can release the rq lock and p could migrate.
1200                  * Only reschedule if p is still on the same runqueue.
1201                  */
1202                 if (p->prio > rq->rt.highest_prio && rq->curr == p)
1203                         resched_task(p);
1204 #else
1205                 /* For UP simply resched on drop of prio */
1206                 if (oldprio < p->prio)
1207                         resched_task(p);
1208 #endif /* CONFIG_SMP */
1209         } else {
1210                 /*
1211                  * This task is not running, but if it is
1212                  * greater than the current running task
1213                  * then reschedule.
1214                  */
1215                 if (p->prio < rq->curr->prio)
1216                         resched_task(rq->curr);
1217         }
1218 }
1219 
1220 static void watchdog(struct rq *rq, struct task_struct *p)
1221 {
1222         unsigned long soft, hard;
1223 
1224         if (!p->signal)
1225                 return;
1226 
1227         soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
1228         hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
1229 
1230         if (soft != RLIM_INFINITY) {
1231                 unsigned long next;
1232 
1233                 p->rt.timeout++;
1234                 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
1235                 if (p->rt.timeout > next)
1236                         p->it_sched_expires = p->se.sum_exec_runtime;
1237         }
1238 }
1239 
1240 static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
1241 {
1242         update_curr_rt(rq);
1243 
1244         watchdog(rq, p);
1245 
1246         /*
1247          * RR tasks need a special form of timeslice management.
1248          * FIFO tasks have no timeslices.
1249          */
1250         if (p->policy != SCHED_RR)
1251                 return;
1252 
1253         if (--p->rt.time_slice)
1254                 return;
1255 
1256         p->rt.time_slice = DEF_TIMESLICE;
1257 
1258         /*
1259          * Requeue to the end of queue if we are not the only element
1260          * on the queue:
1261          */
1262         if (p->rt.run_list.prev != p->rt.run_list.next) {
1263                 requeue_task_rt(rq, p);
1264                 set_tsk_need_resched(p);
1265         }
1266 }
1267 
1268 static void set_curr_task_rt(struct rq *rq)
1269 {
1270         struct task_struct *p = rq->curr;
1271 
1272         p->se.exec_start = rq->clock;
1273 }
1274 
1275 const struct sched_class rt_sched_class = {
1276         .next                   = &fair_sched_class,
1277         .enqueue_task           = enqueue_task_rt,
1278         .dequeue_task           = dequeue_task_rt,
1279         .yield_task             = yield_task_rt,
1280 #ifdef CONFIG_SMP
1281         .select_task_rq         = select_task_rq_rt,
1282 #endif /* CONFIG_SMP */
1283 
1284         .check_preempt_curr     = check_preempt_curr_rt,
1285 
1286         .pick_next_task         = pick_next_task_rt,
1287         .put_prev_task          = put_prev_task_rt,
1288 
1289 #ifdef CONFIG_SMP
1290         .load_balance           = load_balance_rt,
1291         .move_one_task          = move_one_task_rt,
1292         .set_cpus_allowed       = set_cpus_allowed_rt,
1293         .rq_online              = rq_online_rt,
1294         .rq_offline             = rq_offline_rt,
1295         .pre_schedule           = pre_schedule_rt,
1296         .post_schedule          = post_schedule_rt,
1297         .task_wake_up           = task_wake_up_rt,
1298         .switched_from          = switched_from_rt,
1299 #endif
1300 
1301         .set_curr_task          = set_curr_task_rt,
1302         .task_tick              = task_tick_rt,
1303 
1304         .prio_changed           = prio_changed_rt,
1305         .switched_to            = switched_to_rt,
1306 };
1307 
  This page was automatically generated by the LXR engine.