aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/linux/sched.h1
-rw-r--r--kernel/sched/core.c9
-rw-r--r--kernel/sched/deadline.c934
-rw-r--r--kernel/sched/rt.c2
-rw-r--r--kernel/sched/sched.h34
5 files changed, 963 insertions, 17 deletions
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 6c196794fc12..cc66f2615a6d 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1201,6 +1201,7 @@ struct task_struct {
1201 struct list_head tasks; 1201 struct list_head tasks;
1202#ifdef CONFIG_SMP 1202#ifdef CONFIG_SMP
1203 struct plist_node pushable_tasks; 1203 struct plist_node pushable_tasks;
1204 struct rb_node pushable_dl_tasks;
1204#endif 1205#endif
1205 1206
1206 struct mm_struct *mm, *active_mm; 1207 struct mm_struct *mm, *active_mm;
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 203aecdcfccb..548cc04aee45 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1848,6 +1848,7 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
1848 init_task_preempt_count(p); 1848 init_task_preempt_count(p);
1849#ifdef CONFIG_SMP 1849#ifdef CONFIG_SMP
1850 plist_node_init(&p->pushable_tasks, MAX_PRIO); 1850 plist_node_init(&p->pushable_tasks, MAX_PRIO);
1851 RB_CLEAR_NODE(&p->pushable_dl_tasks);
1851#endif 1852#endif
1852 1853
1853 put_cpu(); 1854 put_cpu();
@@ -5040,6 +5041,7 @@ static void free_rootdomain(struct rcu_head *rcu)
5040 struct root_domain *rd = container_of(rcu, struct root_domain, rcu); 5041 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
5041 5042
5042 cpupri_cleanup(&rd->cpupri); 5043 cpupri_cleanup(&rd->cpupri);
5044 free_cpumask_var(rd->dlo_mask);
5043 free_cpumask_var(rd->rto_mask); 5045 free_cpumask_var(rd->rto_mask);
5044 free_cpumask_var(rd->online); 5046 free_cpumask_var(rd->online);
5045 free_cpumask_var(rd->span); 5047 free_cpumask_var(rd->span);
@@ -5091,8 +5093,10 @@ static int init_rootdomain(struct root_domain *rd)
5091 goto out; 5093 goto out;
5092 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL)) 5094 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
5093 goto free_span; 5095 goto free_span;
5094 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL)) 5096 if (!alloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
5095 goto free_online; 5097 goto free_online;
5098 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
5099 goto free_dlo_mask;
5096 5100
5097 if (cpupri_init(&rd->cpupri) != 0) 5101 if (cpupri_init(&rd->cpupri) != 0)
5098 goto free_rto_mask; 5102 goto free_rto_mask;
@@ -5100,6 +5104,8 @@ static int init_rootdomain(struct root_domain *rd)
5100 5104
5101free_rto_mask: 5105free_rto_mask:
5102 free_cpumask_var(rd->rto_mask); 5106 free_cpumask_var(rd->rto_mask);
5107free_dlo_mask:
5108 free_cpumask_var(rd->dlo_mask);
5103free_online: 5109free_online:
5104 free_cpumask_var(rd->online); 5110 free_cpumask_var(rd->online);
5105free_span: 5111free_span:
@@ -6451,6 +6457,7 @@ void __init sched_init_smp(void)
6451 free_cpumask_var(non_isolated_cpus); 6457 free_cpumask_var(non_isolated_cpus);
6452 6458
6453 init_sched_rt_class(); 6459 init_sched_rt_class();
6460 init_sched_dl_class();
6454} 6461}
6455#else 6462#else
6456void __init sched_init_smp(void) 6463void __init sched_init_smp(void)
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 93d82b2a88bd..fcc02c9ca16b 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -10,6 +10,7 @@
10 * miss some of their deadlines), and won't affect any other task. 10 * miss some of their deadlines), and won't affect any other task.
11 * 11 *
12 * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>, 12 * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>,
13 * Juri Lelli <juri.lelli@gmail.com>,
13 * Michael Trimarchi <michael@amarulasolutions.com>, 14 * Michael Trimarchi <michael@amarulasolutions.com>,
14 * Fabio Checconi <fchecconi@gmail.com> 15 * Fabio Checconi <fchecconi@gmail.com>
15 */ 16 */
@@ -20,6 +21,15 @@ static inline int dl_time_before(u64 a, u64 b)
20 return (s64)(a - b) < 0; 21 return (s64)(a - b) < 0;
21} 22}
22 23
24/*
25 * Tells if entity @a should preempt entity @b.
26 */
27static inline
28int dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
29{
30 return dl_time_before(a->deadline, b->deadline);
31}
32
23static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se) 33static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
24{ 34{
25 return container_of(dl_se, struct task_struct, dl); 35 return container_of(dl_se, struct task_struct, dl);
@@ -53,8 +63,168 @@ static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
53void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq) 63void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq)
54{ 64{
55 dl_rq->rb_root = RB_ROOT; 65 dl_rq->rb_root = RB_ROOT;
66
67#ifdef CONFIG_SMP
68 /* zero means no -deadline tasks */
69 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
70
71 dl_rq->dl_nr_migratory = 0;
72 dl_rq->overloaded = 0;
73 dl_rq->pushable_dl_tasks_root = RB_ROOT;
74#endif
75}
76
77#ifdef CONFIG_SMP
78
79static inline int dl_overloaded(struct rq *rq)
80{
81 return atomic_read(&rq->rd->dlo_count);
82}
83
84static inline void dl_set_overload(struct rq *rq)
85{
86 if (!rq->online)
87 return;
88
89 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
90 /*
91 * Must be visible before the overload count is
92 * set (as in sched_rt.c).
93 *
94 * Matched by the barrier in pull_dl_task().
95 */
96 smp_wmb();
97 atomic_inc(&rq->rd->dlo_count);
98}
99
100static inline void dl_clear_overload(struct rq *rq)
101{
102 if (!rq->online)
103 return;
104
105 atomic_dec(&rq->rd->dlo_count);
106 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
107}
108
109static void update_dl_migration(struct dl_rq *dl_rq)
110{
111 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_total > 1) {
112 if (!dl_rq->overloaded) {
113 dl_set_overload(rq_of_dl_rq(dl_rq));
114 dl_rq->overloaded = 1;
115 }
116 } else if (dl_rq->overloaded) {
117 dl_clear_overload(rq_of_dl_rq(dl_rq));
118 dl_rq->overloaded = 0;
119 }
120}
121
122static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
123{
124 struct task_struct *p = dl_task_of(dl_se);
125 dl_rq = &rq_of_dl_rq(dl_rq)->dl;
126
127 dl_rq->dl_nr_total++;
128 if (p->nr_cpus_allowed > 1)
129 dl_rq->dl_nr_migratory++;
130
131 update_dl_migration(dl_rq);
132}
133
134static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
135{
136 struct task_struct *p = dl_task_of(dl_se);
137 dl_rq = &rq_of_dl_rq(dl_rq)->dl;
138
139 dl_rq->dl_nr_total--;
140 if (p->nr_cpus_allowed > 1)
141 dl_rq->dl_nr_migratory--;
142
143 update_dl_migration(dl_rq);
144}
145
146/*
147 * The list of pushable -deadline task is not a plist, like in
148 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
149 */
150static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
151{
152 struct dl_rq *dl_rq = &rq->dl;
153 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
154 struct rb_node *parent = NULL;
155 struct task_struct *entry;
156 int leftmost = 1;
157
158 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
159
160 while (*link) {
161 parent = *link;
162 entry = rb_entry(parent, struct task_struct,
163 pushable_dl_tasks);
164 if (dl_entity_preempt(&p->dl, &entry->dl))
165 link = &parent->rb_left;
166 else {
167 link = &parent->rb_right;
168 leftmost = 0;
169 }
170 }
171
172 if (leftmost)
173 dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
174
175 rb_link_node(&p->pushable_dl_tasks, parent, link);
176 rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
56} 177}
57 178
179static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
180{
181 struct dl_rq *dl_rq = &rq->dl;
182
183 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
184 return;
185
186 if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
187 struct rb_node *next_node;
188
189 next_node = rb_next(&p->pushable_dl_tasks);
190 dl_rq->pushable_dl_tasks_leftmost = next_node;
191 }
192
193 rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
194 RB_CLEAR_NODE(&p->pushable_dl_tasks);
195}
196
197static inline int has_pushable_dl_tasks(struct rq *rq)
198{
199 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
200}
201
202static int push_dl_task(struct rq *rq);
203
204#else
205
206static inline
207void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
208{
209}
210
211static inline
212void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
213{
214}
215
216static inline
217void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
218{
219}
220
221static inline
222void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
223{
224}
225
226#endif /* CONFIG_SMP */
227
58static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags); 228static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
59static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags); 229static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
60static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, 230static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
@@ -309,6 +479,14 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
309 check_preempt_curr_dl(rq, p, 0); 479 check_preempt_curr_dl(rq, p, 0);
310 else 480 else
311 resched_task(rq->curr); 481 resched_task(rq->curr);
482#ifdef CONFIG_SMP
483 /*
484 * Queueing this task back might have overloaded rq,
485 * check if we need to kick someone away.
486 */
487 if (has_pushable_dl_tasks(rq))
488 push_dl_task(rq);
489#endif
312 } 490 }
313unlock: 491unlock:
314 raw_spin_unlock(&rq->lock); 492 raw_spin_unlock(&rq->lock);
@@ -399,6 +577,100 @@ static void update_curr_dl(struct rq *rq)
399 } 577 }
400} 578}
401 579
580#ifdef CONFIG_SMP
581
582static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
583
584static inline u64 next_deadline(struct rq *rq)
585{
586 struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
587
588 if (next && dl_prio(next->prio))
589 return next->dl.deadline;
590 else
591 return 0;
592}
593
594static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
595{
596 struct rq *rq = rq_of_dl_rq(dl_rq);
597
598 if (dl_rq->earliest_dl.curr == 0 ||
599 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
600 /*
601 * If the dl_rq had no -deadline tasks, or if the new task
602 * has shorter deadline than the current one on dl_rq, we
603 * know that the previous earliest becomes our next earliest,
604 * as the new task becomes the earliest itself.
605 */
606 dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr;
607 dl_rq->earliest_dl.curr = deadline;
608 } else if (dl_rq->earliest_dl.next == 0 ||
609 dl_time_before(deadline, dl_rq->earliest_dl.next)) {
610 /*
611 * On the other hand, if the new -deadline task has a
612 * a later deadline than the earliest one on dl_rq, but
613 * it is earlier than the next (if any), we must
614 * recompute the next-earliest.
615 */
616 dl_rq->earliest_dl.next = next_deadline(rq);
617 }
618}
619
620static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
621{
622 struct rq *rq = rq_of_dl_rq(dl_rq);
623
624 /*
625 * Since we may have removed our earliest (and/or next earliest)
626 * task we must recompute them.
627 */
628 if (!dl_rq->dl_nr_running) {
629 dl_rq->earliest_dl.curr = 0;
630 dl_rq->earliest_dl.next = 0;
631 } else {
632 struct rb_node *leftmost = dl_rq->rb_leftmost;
633 struct sched_dl_entity *entry;
634
635 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
636 dl_rq->earliest_dl.curr = entry->deadline;
637 dl_rq->earliest_dl.next = next_deadline(rq);
638 }
639}
640
641#else
642
643static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
644static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
645
646#endif /* CONFIG_SMP */
647
648static inline
649void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
650{
651 int prio = dl_task_of(dl_se)->prio;
652 u64 deadline = dl_se->deadline;
653
654 WARN_ON(!dl_prio(prio));
655 dl_rq->dl_nr_running++;
656
657 inc_dl_deadline(dl_rq, deadline);
658 inc_dl_migration(dl_se, dl_rq);
659}
660
661static inline
662void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
663{
664 int prio = dl_task_of(dl_se)->prio;
665
666 WARN_ON(!dl_prio(prio));
667 WARN_ON(!dl_rq->dl_nr_running);
668 dl_rq->dl_nr_running--;
669
670 dec_dl_deadline(dl_rq, dl_se->deadline);
671 dec_dl_migration(dl_se, dl_rq);
672}
673
402static void __enqueue_dl_entity(struct sched_dl_entity *dl_se) 674static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
403{ 675{
404 struct dl_rq *dl_rq = dl_rq_of_se(dl_se); 676 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
@@ -426,7 +698,7 @@ static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
426 rb_link_node(&dl_se->rb_node, parent, link); 698 rb_link_node(&dl_se->rb_node, parent, link);
427 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root); 699 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
428 700
429 dl_rq->dl_nr_running++; 701 inc_dl_tasks(dl_se, dl_rq);
430} 702}
431 703
432static void __dequeue_dl_entity(struct sched_dl_entity *dl_se) 704static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
@@ -446,7 +718,7 @@ static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
446 rb_erase(&dl_se->rb_node, &dl_rq->rb_root); 718 rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
447 RB_CLEAR_NODE(&dl_se->rb_node); 719 RB_CLEAR_NODE(&dl_se->rb_node);
448 720
449 dl_rq->dl_nr_running--; 721 dec_dl_tasks(dl_se, dl_rq);
450} 722}
451 723
452static void 724static void
@@ -484,12 +756,17 @@ static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
484 return; 756 return;
485 757
486 enqueue_dl_entity(&p->dl, flags); 758 enqueue_dl_entity(&p->dl, flags);
759
760 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
761 enqueue_pushable_dl_task(rq, p);
762
487 inc_nr_running(rq); 763 inc_nr_running(rq);
488} 764}
489 765
490static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) 766static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
491{ 767{
492 dequeue_dl_entity(&p->dl); 768 dequeue_dl_entity(&p->dl);
769 dequeue_pushable_dl_task(rq, p);
493} 770}
494 771
495static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) 772static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
@@ -527,6 +804,74 @@ static void yield_task_dl(struct rq *rq)
527 update_curr_dl(rq); 804 update_curr_dl(rq);
528} 805}
529 806
807#ifdef CONFIG_SMP
808
809static int find_later_rq(struct task_struct *task);
810static int latest_cpu_find(struct cpumask *span,
811 struct task_struct *task,
812 struct cpumask *later_mask);
813
814static int
815select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
816{
817 struct task_struct *curr;
818 struct rq *rq;
819
820 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
821 goto out;
822
823 rq = cpu_rq(cpu);
824
825 rcu_read_lock();
826 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
827
828 /*
829 * If we are dealing with a -deadline task, we must
830 * decide where to wake it up.
831 * If it has a later deadline and the current task
832 * on this rq can't move (provided the waking task
833 * can!) we prefer to send it somewhere else. On the
834 * other hand, if it has a shorter deadline, we
835 * try to make it stay here, it might be important.
836 */
837 if (unlikely(dl_task(curr)) &&
838 (curr->nr_cpus_allowed < 2 ||
839 !dl_entity_preempt(&p->dl, &curr->dl)) &&
840 (p->nr_cpus_allowed > 1)) {
841 int target = find_later_rq(p);
842
843 if (target != -1)
844 cpu = target;
845 }
846 rcu_read_unlock();
847
848out:
849 return cpu;
850}
851
852static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
853{
854 /*
855 * Current can't be migrated, useless to reschedule,
856 * let's hope p can move out.
857 */
858 if (rq->curr->nr_cpus_allowed == 1 ||
859 latest_cpu_find(rq->rd->span, rq->curr, NULL) == -1)
860 return;
861
862 /*
863 * p is migratable, so let's not schedule it and
864 * see if it is pushed or pulled somewhere else.
865 */
866 if (p->nr_cpus_allowed != 1 &&
867 latest_cpu_find(rq->rd->span, p, NULL) != -1)
868 return;
869
870 resched_task(rq->curr);
871}
872
873#endif /* CONFIG_SMP */
874
530/* 875/*
531 * Only called when both the current and waking task are -deadline 876 * Only called when both the current and waking task are -deadline
532 * tasks. 877 * tasks.
@@ -534,8 +879,20 @@ static void yield_task_dl(struct rq *rq)
534static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, 879static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
535 int flags) 880 int flags)
536{ 881{
537 if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline)) 882 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
538 resched_task(rq->curr); 883 resched_task(rq->curr);
884 return;
885 }
886
887#ifdef CONFIG_SMP
888 /*
889 * In the unlikely case current and p have the same deadline
890 * let us try to decide what's the best thing to do...
891 */
892 if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0 &&
893 !need_resched())
894 check_preempt_equal_dl(rq, p);
895#endif /* CONFIG_SMP */
539} 896}
540 897
541#ifdef CONFIG_SCHED_HRTICK 898#ifdef CONFIG_SCHED_HRTICK
@@ -575,16 +932,29 @@ struct task_struct *pick_next_task_dl(struct rq *rq)
575 932
576 p = dl_task_of(dl_se); 933 p = dl_task_of(dl_se);
577 p->se.exec_start = rq_clock_task(rq); 934 p->se.exec_start = rq_clock_task(rq);
935
936 /* Running task will never be pushed. */
937 if (p)
938 dequeue_pushable_dl_task(rq, p);
939
578#ifdef CONFIG_SCHED_HRTICK 940#ifdef CONFIG_SCHED_HRTICK
579 if (hrtick_enabled(rq)) 941 if (hrtick_enabled(rq))
580 start_hrtick_dl(rq, p); 942 start_hrtick_dl(rq, p);
581#endif 943#endif
944
945#ifdef CONFIG_SMP
946 rq->post_schedule = has_pushable_dl_tasks(rq);
947#endif /* CONFIG_SMP */
948
582 return p; 949 return p;
583} 950}
584 951
585static void put_prev_task_dl(struct rq *rq, struct task_struct *p) 952static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
586{ 953{
587 update_curr_dl(rq); 954 update_curr_dl(rq);
955
956 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
957 enqueue_pushable_dl_task(rq, p);
588} 958}
589 959
590static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued) 960static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
@@ -618,16 +988,517 @@ static void set_curr_task_dl(struct rq *rq)
618 struct task_struct *p = rq->curr; 988 struct task_struct *p = rq->curr;
619 989
620 p->se.exec_start = rq_clock_task(rq); 990 p->se.exec_start = rq_clock_task(rq);
991
992 /* You can't push away the running task */
993 dequeue_pushable_dl_task(rq, p);
994}
995
996#ifdef CONFIG_SMP
997
998/* Only try algorithms three times */
999#define DL_MAX_TRIES 3
1000
1001static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1002{
1003 if (!task_running(rq, p) &&
1004 (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
1005 (p->nr_cpus_allowed > 1))
1006 return 1;
1007
1008 return 0;
1009}
1010
1011/* Returns the second earliest -deadline task, NULL otherwise */
1012static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
1013{
1014 struct rb_node *next_node = rq->dl.rb_leftmost;
1015 struct sched_dl_entity *dl_se;
1016 struct task_struct *p = NULL;
1017
1018next_node:
1019 next_node = rb_next(next_node);
1020 if (next_node) {
1021 dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
1022 p = dl_task_of(dl_se);
1023
1024 if (pick_dl_task(rq, p, cpu))
1025 return p;
1026
1027 goto next_node;
1028 }
1029
1030 return NULL;
1031}
1032
1033static int latest_cpu_find(struct cpumask *span,
1034 struct task_struct *task,
1035 struct cpumask *later_mask)
1036{
1037 const struct sched_dl_entity *dl_se = &task->dl;
1038 int cpu, found = -1, best = 0;
1039 u64 max_dl = 0;
1040
1041 for_each_cpu(cpu, span) {
1042 struct rq *rq = cpu_rq(cpu);
1043 struct dl_rq *dl_rq = &rq->dl;
1044
1045 if (cpumask_test_cpu(cpu, &task->cpus_allowed) &&
1046 (!dl_rq->dl_nr_running || dl_time_before(dl_se->deadline,
1047 dl_rq->earliest_dl.curr))) {
1048 if (later_mask)
1049 cpumask_set_cpu(cpu, later_mask);
1050 if (!best && !dl_rq->dl_nr_running) {
1051 best = 1;
1052 found = cpu;
1053 } else if (!best &&
1054 dl_time_before(max_dl,
1055 dl_rq->earliest_dl.curr)) {
1056 max_dl = dl_rq->earliest_dl.curr;
1057 found = cpu;
1058 }
1059 } else if (later_mask)
1060 cpumask_clear_cpu(cpu, later_mask);
1061 }
1062
1063 return found;
1064}
1065
1066static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1067
1068static int find_later_rq(struct task_struct *task)
1069{
1070 struct sched_domain *sd;
1071 struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl);
1072 int this_cpu = smp_processor_id();
1073 int best_cpu, cpu = task_cpu(task);
1074
1075 /* Make sure the mask is initialized first */
1076 if (unlikely(!later_mask))
1077 return -1;
1078
1079 if (task->nr_cpus_allowed == 1)
1080 return -1;
1081
1082 best_cpu = latest_cpu_find(task_rq(task)->rd->span, task, later_mask);
1083 if (best_cpu == -1)
1084 return -1;
1085
1086 /*
1087 * If we are here, some target has been found,
1088 * the most suitable of which is cached in best_cpu.
1089 * This is, among the runqueues where the current tasks
1090 * have later deadlines than the task's one, the rq
1091 * with the latest possible one.
1092 *
1093 * Now we check how well this matches with task's
1094 * affinity and system topology.
1095 *
1096 * The last cpu where the task run is our first
1097 * guess, since it is most likely cache-hot there.
1098 */
1099 if (cpumask_test_cpu(cpu, later_mask))
1100 return cpu;
1101 /*
1102 * Check if this_cpu is to be skipped (i.e., it is
1103 * not in the mask) or not.
1104 */
1105 if (!cpumask_test_cpu(this_cpu, later_mask))
1106 this_cpu = -1;
1107
1108 rcu_read_lock();
1109 for_each_domain(cpu, sd) {
1110 if (sd->flags & SD_WAKE_AFFINE) {
1111
1112 /*
1113 * If possible, preempting this_cpu is
1114 * cheaper than migrating.
1115 */
1116 if (this_cpu != -1 &&
1117 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1118 rcu_read_unlock();
1119 return this_cpu;
1120 }
1121
1122 /*
1123 * Last chance: if best_cpu is valid and is
1124 * in the mask, that becomes our choice.
1125 */
1126 if (best_cpu < nr_cpu_ids &&
1127 cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
1128 rcu_read_unlock();
1129 return best_cpu;
1130 }
1131 }
1132 }
1133 rcu_read_unlock();
1134
1135 /*
1136 * At this point, all our guesses failed, we just return
1137 * 'something', and let the caller sort the things out.
1138 */
1139 if (this_cpu != -1)
1140 return this_cpu;
1141
1142 cpu = cpumask_any(later_mask);
1143 if (cpu < nr_cpu_ids)
1144 return cpu;
1145
1146 return -1;
1147}
1148
1149/* Locks the rq it finds */
1150static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1151{
1152 struct rq *later_rq = NULL;
1153 int tries;
1154 int cpu;
1155
1156 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1157 cpu = find_later_rq(task);
1158
1159 if ((cpu == -1) || (cpu == rq->cpu))
1160 break;
1161
1162 later_rq = cpu_rq(cpu);
1163
1164 /* Retry if something changed. */
1165 if (double_lock_balance(rq, later_rq)) {
1166 if (unlikely(task_rq(task) != rq ||
1167 !cpumask_test_cpu(later_rq->cpu,
1168 &task->cpus_allowed) ||
1169 task_running(rq, task) || !task->on_rq)) {
1170 double_unlock_balance(rq, later_rq);
1171 later_rq = NULL;
1172 break;
1173 }
1174 }
1175
1176 /*
1177 * If the rq we found has no -deadline task, or
1178 * its earliest one has a later deadline than our
1179 * task, the rq is a good one.
1180 */
1181 if (!later_rq->dl.dl_nr_running ||
1182 dl_time_before(task->dl.deadline,
1183 later_rq->dl.earliest_dl.curr))
1184 break;
1185
1186 /* Otherwise we try again. */
1187 double_unlock_balance(rq, later_rq);
1188 later_rq = NULL;
1189 }
1190
1191 return later_rq;
1192}
1193
1194static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1195{
1196 struct task_struct *p;
1197
1198 if (!has_pushable_dl_tasks(rq))
1199 return NULL;
1200
1201 p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
1202 struct task_struct, pushable_dl_tasks);
1203
1204 BUG_ON(rq->cpu != task_cpu(p));
1205 BUG_ON(task_current(rq, p));
1206 BUG_ON(p->nr_cpus_allowed <= 1);
1207
1208 BUG_ON(!p->se.on_rq);
1209 BUG_ON(!dl_task(p));
1210
1211 return p;
1212}
1213
1214/*
1215 * See if the non running -deadline tasks on this rq
1216 * can be sent to some other CPU where they can preempt
1217 * and start executing.
1218 */
1219static int push_dl_task(struct rq *rq)
1220{
1221 struct task_struct *next_task;
1222 struct rq *later_rq;
1223
1224 if (!rq->dl.overloaded)
1225 return 0;
1226
1227 next_task = pick_next_pushable_dl_task(rq);
1228 if (!next_task)
1229 return 0;
1230
1231retry:
1232 if (unlikely(next_task == rq->curr)) {
1233 WARN_ON(1);
1234 return 0;
1235 }
1236
1237 /*
1238 * If next_task preempts rq->curr, and rq->curr
1239 * can move away, it makes sense to just reschedule
1240 * without going further in pushing next_task.
1241 */
1242 if (dl_task(rq->curr) &&
1243 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
1244 rq->curr->nr_cpus_allowed > 1) {
1245 resched_task(rq->curr);
1246 return 0;
1247 }
1248
1249 /* We might release rq lock */
1250 get_task_struct(next_task);
1251
1252 /* Will lock the rq it'll find */
1253 later_rq = find_lock_later_rq(next_task, rq);
1254 if (!later_rq) {
1255 struct task_struct *task;
1256
1257 /*
1258 * We must check all this again, since
1259 * find_lock_later_rq releases rq->lock and it is
1260 * then possible that next_task has migrated.
1261 */
1262 task = pick_next_pushable_dl_task(rq);
1263 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1264 /*
1265 * The task is still there. We don't try
1266 * again, some other cpu will pull it when ready.
1267 */
1268 dequeue_pushable_dl_task(rq, next_task);
1269 goto out;
1270 }
1271
1272 if (!task)
1273 /* No more tasks */
1274 goto out;
1275
1276 put_task_struct(next_task);
1277 next_task = task;
1278 goto retry;
1279 }
1280
1281 deactivate_task(rq, next_task, 0);
1282 set_task_cpu(next_task, later_rq->cpu);
1283 activate_task(later_rq, next_task, 0);
1284
1285 resched_task(later_rq->curr);
1286
1287 double_unlock_balance(rq, later_rq);
1288
1289out:
1290 put_task_struct(next_task);
1291
1292 return 1;
1293}
1294
1295static void push_dl_tasks(struct rq *rq)
1296{
1297 /* Terminates as it moves a -deadline task */
1298 while (push_dl_task(rq))
1299 ;
621} 1300}
622 1301
1302static int pull_dl_task(struct rq *this_rq)
1303{
1304 int this_cpu = this_rq->cpu, ret = 0, cpu;
1305 struct task_struct *p;
1306 struct rq *src_rq;
1307 u64 dmin = LONG_MAX;
1308
1309 if (likely(!dl_overloaded(this_rq)))
1310 return 0;
1311
1312 /*
1313 * Match the barrier from dl_set_overloaded; this guarantees that if we
1314 * see overloaded we must also see the dlo_mask bit.
1315 */
1316 smp_rmb();
1317
1318 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
1319 if (this_cpu == cpu)
1320 continue;
1321
1322 src_rq = cpu_rq(cpu);
1323
1324 /*
1325 * It looks racy, abd it is! However, as in sched_rt.c,
1326 * we are fine with this.
1327 */
1328 if (this_rq->dl.dl_nr_running &&
1329 dl_time_before(this_rq->dl.earliest_dl.curr,
1330 src_rq->dl.earliest_dl.next))
1331 continue;
1332
1333 /* Might drop this_rq->lock */
1334 double_lock_balance(this_rq, src_rq);
1335
1336 /*
1337 * If there are no more pullable tasks on the
1338 * rq, we're done with it.
1339 */
1340 if (src_rq->dl.dl_nr_running <= 1)
1341 goto skip;
1342
1343 p = pick_next_earliest_dl_task(src_rq, this_cpu);
1344
1345 /*
1346 * We found a task to be pulled if:
1347 * - it preempts our current (if there's one),
1348 * - it will preempt the last one we pulled (if any).
1349 */
1350 if (p && dl_time_before(p->dl.deadline, dmin) &&
1351 (!this_rq->dl.dl_nr_running ||
1352 dl_time_before(p->dl.deadline,
1353 this_rq->dl.earliest_dl.curr))) {
1354 WARN_ON(p == src_rq->curr);
1355 WARN_ON(!p->se.on_rq);
1356
1357 /*
1358 * Then we pull iff p has actually an earlier
1359 * deadline than the current task of its runqueue.
1360 */
1361 if (dl_time_before(p->dl.deadline,
1362 src_rq->curr->dl.deadline))
1363 goto skip;
1364
1365 ret = 1;
1366
1367 deactivate_task(src_rq, p, 0);
1368 set_task_cpu(p, this_cpu);
1369 activate_task(this_rq, p, 0);
1370 dmin = p->dl.deadline;
1371
1372 /* Is there any other task even earlier? */
1373 }
1374skip:
1375 double_unlock_balance(this_rq, src_rq);
1376 }
1377
1378 return ret;
1379}
1380
1381static void pre_schedule_dl(struct rq *rq, struct task_struct *prev)
1382{
1383 /* Try to pull other tasks here */
1384 if (dl_task(prev))
1385 pull_dl_task(rq);
1386}
1387
1388static void post_schedule_dl(struct rq *rq)
1389{
1390 push_dl_tasks(rq);
1391}
1392
1393/*
1394 * Since the task is not running and a reschedule is not going to happen
1395 * anytime soon on its runqueue, we try pushing it away now.
1396 */
1397static void task_woken_dl(struct rq *rq, struct task_struct *p)
1398{
1399 if (!task_running(rq, p) &&
1400 !test_tsk_need_resched(rq->curr) &&
1401 has_pushable_dl_tasks(rq) &&
1402 p->nr_cpus_allowed > 1 &&
1403 dl_task(rq->curr) &&
1404 (rq->curr->nr_cpus_allowed < 2 ||
1405 dl_entity_preempt(&rq->curr->dl, &p->dl))) {
1406 push_dl_tasks(rq);
1407 }
1408}
1409
1410static void set_cpus_allowed_dl(struct task_struct *p,
1411 const struct cpumask *new_mask)
1412{
1413 struct rq *rq;
1414 int weight;
1415
1416 BUG_ON(!dl_task(p));
1417
1418 /*
1419 * Update only if the task is actually running (i.e.,
1420 * it is on the rq AND it is not throttled).
1421 */
1422 if (!on_dl_rq(&p->dl))
1423 return;
1424
1425 weight = cpumask_weight(new_mask);
1426
1427 /*
1428 * Only update if the process changes its state from whether it
1429 * can migrate or not.
1430 */
1431 if ((p->nr_cpus_allowed > 1) == (weight > 1))
1432 return;
1433
1434 rq = task_rq(p);
1435
1436 /*
1437 * The process used to be able to migrate OR it can now migrate
1438 */
1439 if (weight <= 1) {
1440 if (!task_current(rq, p))
1441 dequeue_pushable_dl_task(rq, p);
1442 BUG_ON(!rq->dl.dl_nr_migratory);
1443 rq->dl.dl_nr_migratory--;
1444 } else {
1445 if (!task_current(rq, p))
1446 enqueue_pushable_dl_task(rq, p);
1447 rq->dl.dl_nr_migratory++;
1448 }
1449
1450 update_dl_migration(&rq->dl);
1451}
1452
1453/* Assumes rq->lock is held */
1454static void rq_online_dl(struct rq *rq)
1455{
1456 if (rq->dl.overloaded)
1457 dl_set_overload(rq);
1458}
1459
1460/* Assumes rq->lock is held */
1461static void rq_offline_dl(struct rq *rq)
1462{
1463 if (rq->dl.overloaded)
1464 dl_clear_overload(rq);
1465}
1466
1467void init_sched_dl_class(void)
1468{
1469 unsigned int i;
1470
1471 for_each_possible_cpu(i)
1472 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
1473 GFP_KERNEL, cpu_to_node(i));
1474}
1475
1476#endif /* CONFIG_SMP */
1477
623static void switched_from_dl(struct rq *rq, struct task_struct *p) 1478static void switched_from_dl(struct rq *rq, struct task_struct *p)
624{ 1479{
625 if (hrtimer_active(&p->dl.dl_timer)) 1480 if (hrtimer_active(&p->dl.dl_timer) && !dl_policy(p->policy))
626 hrtimer_try_to_cancel(&p->dl.dl_timer); 1481 hrtimer_try_to_cancel(&p->dl.dl_timer);
1482
1483#ifdef CONFIG_SMP
1484 /*
1485 * Since this might be the only -deadline task on the rq,
1486 * this is the right place to try to pull some other one
1487 * from an overloaded cpu, if any.
1488 */
1489 if (!rq->dl.dl_nr_running)
1490 pull_dl_task(rq);
1491#endif
627} 1492}
628 1493
1494/*
1495 * When switching to -deadline, we may overload the rq, then
1496 * we try to push someone off, if possible.
1497 */
629static void switched_to_dl(struct rq *rq, struct task_struct *p) 1498static void switched_to_dl(struct rq *rq, struct task_struct *p)
630{ 1499{
1500 int check_resched = 1;
1501
631 /* 1502 /*
632 * If p is throttled, don't consider the possibility 1503 * If p is throttled, don't consider the possibility
633 * of preempting rq->curr, the check will be done right 1504 * of preempting rq->curr, the check will be done right
@@ -637,26 +1508,53 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p)
637 return; 1508 return;
638 1509
639 if (p->on_rq || rq->curr != p) { 1510 if (p->on_rq || rq->curr != p) {
640 if (task_has_dl_policy(rq->curr)) 1511#ifdef CONFIG_SMP
1512 if (rq->dl.overloaded && push_dl_task(rq) && rq != task_rq(p))
1513 /* Only reschedule if pushing failed */
1514 check_resched = 0;
1515#endif /* CONFIG_SMP */
1516 if (check_resched && task_has_dl_policy(rq->curr))
641 check_preempt_curr_dl(rq, p, 0); 1517 check_preempt_curr_dl(rq, p, 0);
642 else
643 resched_task(rq->curr);
644 } 1518 }
645} 1519}
646 1520
1521/*
1522 * If the scheduling parameters of a -deadline task changed,
1523 * a push or pull operation might be needed.
1524 */
647static void prio_changed_dl(struct rq *rq, struct task_struct *p, 1525static void prio_changed_dl(struct rq *rq, struct task_struct *p,
648 int oldprio) 1526 int oldprio)
649{ 1527{
650 switched_to_dl(rq, p); 1528 if (p->on_rq || rq->curr == p) {
651}
652
653#ifdef CONFIG_SMP 1529#ifdef CONFIG_SMP
654static int 1530 /*
655select_task_rq_dl(struct task_struct *p, int prev_cpu, int sd_flag, int flags) 1531 * This might be too much, but unfortunately
656{ 1532 * we don't have the old deadline value, and
657 return task_cpu(p); 1533 * we can't argue if the task is increasing
1534 * or lowering its prio, so...
1535 */
1536 if (!rq->dl.overloaded)
1537 pull_dl_task(rq);
1538
1539 /*
1540 * If we now have a earlier deadline task than p,
1541 * then reschedule, provided p is still on this
1542 * runqueue.
1543 */
1544 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) &&
1545 rq->curr == p)
1546 resched_task(p);
1547#else
1548 /*
1549 * Again, we don't know if p has a earlier
1550 * or later deadline, so let's blindly set a
1551 * (maybe not needed) rescheduling point.
1552 */
1553 resched_task(p);
1554#endif /* CONFIG_SMP */
1555 } else
1556 switched_to_dl(rq, p);
658} 1557}
659#endif
660 1558
661const struct sched_class dl_sched_class = { 1559const struct sched_class dl_sched_class = {
662 .next = &rt_sched_class, 1560 .next = &rt_sched_class,
@@ -671,6 +1569,12 @@ const struct sched_class dl_sched_class = {
671 1569
672#ifdef CONFIG_SMP 1570#ifdef CONFIG_SMP
673 .select_task_rq = select_task_rq_dl, 1571 .select_task_rq = select_task_rq_dl,
1572 .set_cpus_allowed = set_cpus_allowed_dl,
1573 .rq_online = rq_online_dl,
1574 .rq_offline = rq_offline_dl,
1575 .pre_schedule = pre_schedule_dl,
1576 .post_schedule = post_schedule_dl,
1577 .task_woken = task_woken_dl,
674#endif 1578#endif
675 1579
676 .set_curr_task = set_curr_task_dl, 1580 .set_curr_task = set_curr_task_dl,
diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index 1c4065575fa2..a2740b775b45 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -1738,7 +1738,7 @@ static void task_woken_rt(struct rq *rq, struct task_struct *p)
1738 !test_tsk_need_resched(rq->curr) && 1738 !test_tsk_need_resched(rq->curr) &&
1739 has_pushable_tasks(rq) && 1739 has_pushable_tasks(rq) &&
1740 p->nr_cpus_allowed > 1 && 1740 p->nr_cpus_allowed > 1 &&
1741 rt_task(rq->curr) && 1741 (dl_task(rq->curr) || rt_task(rq->curr)) &&
1742 (rq->curr->nr_cpus_allowed < 2 || 1742 (rq->curr->nr_cpus_allowed < 2 ||
1743 rq->curr->prio <= p->prio)) 1743 rq->curr->prio <= p->prio))
1744 push_rt_tasks(rq); 1744 push_rt_tasks(rq);
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 83eb5390f753..93ea62754f11 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -385,6 +385,31 @@ struct dl_rq {
385 struct rb_node *rb_leftmost; 385 struct rb_node *rb_leftmost;
386 386
387 unsigned long dl_nr_running; 387 unsigned long dl_nr_running;
388
389#ifdef CONFIG_SMP
390 /*
391 * Deadline values of the currently executing and the
392 * earliest ready task on this rq. Caching these facilitates
393 * the decision wether or not a ready but not running task
394 * should migrate somewhere else.
395 */
396 struct {
397 u64 curr;
398 u64 next;
399 } earliest_dl;
400
401 unsigned long dl_nr_migratory;
402 unsigned long dl_nr_total;
403 int overloaded;
404
405 /*
406 * Tasks on this rq that can be pushed away. They are kept in
407 * an rb-tree, ordered by tasks' deadlines, with caching
408 * of the leftmost (earliest deadline) element.
409 */
410 struct rb_root pushable_dl_tasks_root;
411 struct rb_node *pushable_dl_tasks_leftmost;
412#endif
388}; 413};
389 414
390#ifdef CONFIG_SMP 415#ifdef CONFIG_SMP
@@ -405,6 +430,13 @@ struct root_domain {
405 cpumask_var_t online; 430 cpumask_var_t online;
406 431
407 /* 432 /*
433 * The bit corresponding to a CPU gets set here if such CPU has more
434 * than one runnable -deadline task (as it is below for RT tasks).
435 */
436 cpumask_var_t dlo_mask;
437 atomic_t dlo_count;
438
439 /*
408 * The "RT overload" flag: it gets set if a CPU has more than 440 * The "RT overload" flag: it gets set if a CPU has more than
409 * one runnable RT task. 441 * one runnable RT task.
410 */ 442 */
@@ -1095,6 +1127,8 @@ static inline void idle_balance(int cpu, struct rq *rq)
1095extern void sysrq_sched_debug_show(void); 1127extern void sysrq_sched_debug_show(void);
1096extern void sched_init_granularity(void); 1128extern void sched_init_granularity(void);
1097extern void update_max_interval(void); 1129extern void update_max_interval(void);
1130
1131extern void init_sched_dl_class(void);
1098extern void init_sched_rt_class(void); 1132extern void init_sched_rt_class(void);
1099extern void init_sched_fair_class(void); 1133extern void init_sched_fair_class(void);
1100 1134