blob: ad0241561c3eb202a1dc0962e6dcfc814ce8720d [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
Peter Zijlstra029632f2011-10-25 10:00:11 +02006#include "sched.h"
7
8#include <linux/slab.h>
Steven Rostedtb6366f02015-03-18 14:49:46 -04009#include <linux/irq_work.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020010
Clark Williamsce0dbbb2013-02-07 09:47:04 -060011int sched_rr_timeslice = RR_TIMESLICE;
12
Peter Zijlstra029632f2011-10-25 10:00:11 +020013static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
14
15struct rt_bandwidth def_rt_bandwidth;
16
17static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
18{
19 struct rt_bandwidth *rt_b =
20 container_of(timer, struct rt_bandwidth, rt_period_timer);
21 ktime_t now;
22 int overrun;
23 int idle = 0;
24
25 for (;;) {
26 now = hrtimer_cb_get_time(timer);
27 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
28
29 if (!overrun)
30 break;
31
32 idle = do_sched_rt_period_timer(rt_b, overrun);
33 }
34
35 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
36}
37
38void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
39{
40 rt_b->rt_period = ns_to_ktime(period);
41 rt_b->rt_runtime = runtime;
42
43 raw_spin_lock_init(&rt_b->rt_runtime_lock);
44
45 hrtimer_init(&rt_b->rt_period_timer,
46 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
47 rt_b->rt_period_timer.function = sched_rt_period_timer;
48}
49
50static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
51{
52 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
53 return;
54
55 if (hrtimer_active(&rt_b->rt_period_timer))
56 return;
57
58 raw_spin_lock(&rt_b->rt_runtime_lock);
59 start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period);
60 raw_spin_unlock(&rt_b->rt_runtime_lock);
61}
62
Steven Rostedtb6366f02015-03-18 14:49:46 -040063#ifdef CONFIG_SMP
64static void push_irq_work_func(struct irq_work *work);
65#endif
66
Peter Zijlstra029632f2011-10-25 10:00:11 +020067void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
68{
69 struct rt_prio_array *array;
70 int i;
71
72 array = &rt_rq->active;
73 for (i = 0; i < MAX_RT_PRIO; i++) {
74 INIT_LIST_HEAD(array->queue + i);
75 __clear_bit(i, array->bitmap);
76 }
77 /* delimiter for bitsearch: */
78 __set_bit(MAX_RT_PRIO, array->bitmap);
79
80#if defined CONFIG_SMP
81 rt_rq->highest_prio.curr = MAX_RT_PRIO;
82 rt_rq->highest_prio.next = MAX_RT_PRIO;
83 rt_rq->rt_nr_migratory = 0;
84 rt_rq->overloaded = 0;
85 plist_head_init(&rt_rq->pushable_tasks);
Steven Rostedtb6366f02015-03-18 14:49:46 -040086
87#ifdef HAVE_RT_PUSH_IPI
88 rt_rq->push_flags = 0;
89 rt_rq->push_cpu = nr_cpu_ids;
90 raw_spin_lock_init(&rt_rq->push_lock);
91 init_irq_work(&rt_rq->push_work, push_irq_work_func);
Peter Zijlstra029632f2011-10-25 10:00:11 +020092#endif
Steven Rostedtb6366f02015-03-18 14:49:46 -040093#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +040094 /* We start is dequeued state, because no RT tasks are queued */
95 rt_rq->rt_queued = 0;
Peter Zijlstra029632f2011-10-25 10:00:11 +020096
97 rt_rq->rt_time = 0;
98 rt_rq->rt_throttled = 0;
99 rt_rq->rt_runtime = 0;
100 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
101}
102
Gregory Haskins398a1532009-01-14 09:10:04 -0500103#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +0200104static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
105{
106 hrtimer_cancel(&rt_b->rt_period_timer);
107}
Gregory Haskins398a1532009-01-14 09:10:04 -0500108
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200109#define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
110
Peter Zijlstra8f488942009-07-24 12:25:30 +0200111static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
112{
113#ifdef CONFIG_SCHED_DEBUG
114 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
115#endif
116 return container_of(rt_se, struct task_struct, rt);
117}
118
Gregory Haskins398a1532009-01-14 09:10:04 -0500119static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
120{
121 return rt_rq->rq;
122}
123
124static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
125{
126 return rt_se->rt_rq;
127}
128
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400129static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
130{
131 struct rt_rq *rt_rq = rt_se->rt_rq;
132
133 return rt_rq->rq;
134}
135
Peter Zijlstra029632f2011-10-25 10:00:11 +0200136void free_rt_sched_group(struct task_group *tg)
137{
138 int i;
139
140 if (tg->rt_se)
141 destroy_rt_bandwidth(&tg->rt_bandwidth);
142
143 for_each_possible_cpu(i) {
144 if (tg->rt_rq)
145 kfree(tg->rt_rq[i]);
146 if (tg->rt_se)
147 kfree(tg->rt_se[i]);
148 }
149
150 kfree(tg->rt_rq);
151 kfree(tg->rt_se);
152}
153
154void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
155 struct sched_rt_entity *rt_se, int cpu,
156 struct sched_rt_entity *parent)
157{
158 struct rq *rq = cpu_rq(cpu);
159
160 rt_rq->highest_prio.curr = MAX_RT_PRIO;
161 rt_rq->rt_nr_boosted = 0;
162 rt_rq->rq = rq;
163 rt_rq->tg = tg;
164
165 tg->rt_rq[cpu] = rt_rq;
166 tg->rt_se[cpu] = rt_se;
167
168 if (!rt_se)
169 return;
170
171 if (!parent)
172 rt_se->rt_rq = &rq->rt;
173 else
174 rt_se->rt_rq = parent->my_q;
175
176 rt_se->my_q = rt_rq;
177 rt_se->parent = parent;
178 INIT_LIST_HEAD(&rt_se->run_list);
179}
180
181int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
182{
183 struct rt_rq *rt_rq;
184 struct sched_rt_entity *rt_se;
185 int i;
186
187 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
188 if (!tg->rt_rq)
189 goto err;
190 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
191 if (!tg->rt_se)
192 goto err;
193
194 init_rt_bandwidth(&tg->rt_bandwidth,
195 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
196
197 for_each_possible_cpu(i) {
198 rt_rq = kzalloc_node(sizeof(struct rt_rq),
199 GFP_KERNEL, cpu_to_node(i));
200 if (!rt_rq)
201 goto err;
202
203 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
204 GFP_KERNEL, cpu_to_node(i));
205 if (!rt_se)
206 goto err_free_rq;
207
208 init_rt_rq(rt_rq, cpu_rq(i));
209 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
210 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
211 }
212
213 return 1;
214
215err_free_rq:
216 kfree(rt_rq);
217err:
218 return 0;
219}
220
Gregory Haskins398a1532009-01-14 09:10:04 -0500221#else /* CONFIG_RT_GROUP_SCHED */
222
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200223#define rt_entity_is_task(rt_se) (1)
224
Peter Zijlstra8f488942009-07-24 12:25:30 +0200225static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
226{
227 return container_of(rt_se, struct task_struct, rt);
228}
229
Gregory Haskins398a1532009-01-14 09:10:04 -0500230static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
231{
232 return container_of(rt_rq, struct rq, rt);
233}
234
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400235static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
Gregory Haskins398a1532009-01-14 09:10:04 -0500236{
237 struct task_struct *p = rt_task_of(rt_se);
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400238
239 return task_rq(p);
240}
241
242static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
243{
244 struct rq *rq = rq_of_rt_se(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -0500245
246 return &rq->rt;
247}
248
Peter Zijlstra029632f2011-10-25 10:00:11 +0200249void free_rt_sched_group(struct task_group *tg) { }
250
251int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
252{
253 return 1;
254}
Gregory Haskins398a1532009-01-14 09:10:04 -0500255#endif /* CONFIG_RT_GROUP_SCHED */
256
Steven Rostedt4fd29172008-01-25 21:08:06 +0100257#ifdef CONFIG_SMP
Ingo Molnar84de4272008-01-25 21:08:15 +0100258
Peter Zijlstra38033c32014-01-23 20:32:21 +0100259static int pull_rt_task(struct rq *this_rq);
260
Peter Zijlstradc877342014-02-12 15:47:29 +0100261static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
262{
263 /* Try to pull RT tasks here if we lower this rq's prio */
264 return rq->rt.highest_prio.curr > prev->prio;
265}
266
Gregory Haskins637f5082008-01-25 21:08:18 +0100267static inline int rt_overloaded(struct rq *rq)
Steven Rostedt4fd29172008-01-25 21:08:06 +0100268{
Gregory Haskins637f5082008-01-25 21:08:18 +0100269 return atomic_read(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100270}
Ingo Molnar84de4272008-01-25 21:08:15 +0100271
Steven Rostedt4fd29172008-01-25 21:08:06 +0100272static inline void rt_set_overload(struct rq *rq)
273{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400274 if (!rq->online)
275 return;
276
Rusty Russellc6c49272008-11-25 02:35:05 +1030277 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100278 /*
279 * Make sure the mask is visible before we set
280 * the overload count. That is checked to determine
281 * if we should look at the mask. It would be a shame
282 * if we looked at the mask, but the mask was not
283 * updated yet.
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200284 *
285 * Matched by the barrier in pull_rt_task().
Steven Rostedt4fd29172008-01-25 21:08:06 +0100286 */
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200287 smp_wmb();
Gregory Haskins637f5082008-01-25 21:08:18 +0100288 atomic_inc(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100289}
Ingo Molnar84de4272008-01-25 21:08:15 +0100290
Steven Rostedt4fd29172008-01-25 21:08:06 +0100291static inline void rt_clear_overload(struct rq *rq)
292{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400293 if (!rq->online)
294 return;
295
Steven Rostedt4fd29172008-01-25 21:08:06 +0100296 /* the order here really doesn't matter */
Gregory Haskins637f5082008-01-25 21:08:18 +0100297 atomic_dec(&rq->rd->rto_count);
Rusty Russellc6c49272008-11-25 02:35:05 +1030298 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100299}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100300
Gregory Haskins398a1532009-01-14 09:10:04 -0500301static void update_rt_migration(struct rt_rq *rt_rq)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100302{
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200303 if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
Gregory Haskins398a1532009-01-14 09:10:04 -0500304 if (!rt_rq->overloaded) {
305 rt_set_overload(rq_of_rt_rq(rt_rq));
306 rt_rq->overloaded = 1;
Gregory Haskinscdc8eb92008-01-25 21:08:23 +0100307 }
Gregory Haskins398a1532009-01-14 09:10:04 -0500308 } else if (rt_rq->overloaded) {
309 rt_clear_overload(rq_of_rt_rq(rt_rq));
310 rt_rq->overloaded = 0;
Gregory Haskins637f5082008-01-25 21:08:18 +0100311 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100312}
Steven Rostedt4fd29172008-01-25 21:08:06 +0100313
Gregory Haskins398a1532009-01-14 09:10:04 -0500314static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100315{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200316 struct task_struct *p;
317
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200318 if (!rt_entity_is_task(rt_se))
319 return;
320
Peter Zijlstra29baa742012-04-23 12:11:21 +0200321 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200322 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
323
324 rt_rq->rt_nr_total++;
Peter Zijlstra29baa742012-04-23 12:11:21 +0200325 if (p->nr_cpus_allowed > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500326 rt_rq->rt_nr_migratory++;
327
328 update_rt_migration(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100329}
330
Gregory Haskins398a1532009-01-14 09:10:04 -0500331static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
332{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200333 struct task_struct *p;
334
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200335 if (!rt_entity_is_task(rt_se))
336 return;
337
Peter Zijlstra29baa742012-04-23 12:11:21 +0200338 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200339 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
340
341 rt_rq->rt_nr_total--;
Peter Zijlstra29baa742012-04-23 12:11:21 +0200342 if (p->nr_cpus_allowed > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500343 rt_rq->rt_nr_migratory--;
344
345 update_rt_migration(rt_rq);
346}
347
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400348static inline int has_pushable_tasks(struct rq *rq)
349{
350 return !plist_head_empty(&rq->rt.pushable_tasks);
351}
352
Peter Zijlstradc877342014-02-12 15:47:29 +0100353static inline void set_post_schedule(struct rq *rq)
354{
355 /*
356 * We detect this state here so that we can avoid taking the RQ
357 * lock again later if there is no need to push
358 */
359 rq->post_schedule = has_pushable_tasks(rq);
360}
361
Gregory Haskins917b6272008-12-29 09:39:53 -0500362static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
363{
364 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
365 plist_node_init(&p->pushable_tasks, p->prio);
366 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400367
368 /* Update the highest prio pushable task */
369 if (p->prio < rq->rt.highest_prio.next)
370 rq->rt.highest_prio.next = p->prio;
Gregory Haskins917b6272008-12-29 09:39:53 -0500371}
372
373static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
374{
375 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
Gregory Haskins917b6272008-12-29 09:39:53 -0500376
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400377 /* Update the new highest prio pushable task */
378 if (has_pushable_tasks(rq)) {
379 p = plist_first_entry(&rq->rt.pushable_tasks,
380 struct task_struct, pushable_tasks);
381 rq->rt.highest_prio.next = p->prio;
382 } else
383 rq->rt.highest_prio.next = MAX_RT_PRIO;
Ingo Molnarbcf08df2008-04-19 12:11:10 +0200384}
385
Gregory Haskins917b6272008-12-29 09:39:53 -0500386#else
387
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100388static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
389{
390}
391
392static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
393{
394}
395
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500396static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100397void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
398{
399}
400
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500401static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100402void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
403{
404}
Gregory Haskins917b6272008-12-29 09:39:53 -0500405
Peter Zijlstradc877342014-02-12 15:47:29 +0100406static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
407{
408 return false;
409}
410
411static inline int pull_rt_task(struct rq *this_rq)
412{
413 return 0;
414}
415
416static inline void set_post_schedule(struct rq *rq)
417{
418}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200419#endif /* CONFIG_SMP */
420
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400421static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
422static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
423
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100424static inline int on_rt_rq(struct sched_rt_entity *rt_se)
425{
426 return !list_empty(&rt_se->run_list);
427}
428
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100429#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100430
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100431static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100432{
433 if (!rt_rq->tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100434 return RUNTIME_INF;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100435
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200436 return rt_rq->rt_runtime;
437}
438
439static inline u64 sched_rt_period(struct rt_rq *rt_rq)
440{
441 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100442}
443
Cheng Xuec514c42011-05-14 14:20:02 +0800444typedef struct task_group *rt_rq_iter_t;
445
Yong Zhang1c09ab02011-06-28 10:51:31 +0800446static inline struct task_group *next_task_group(struct task_group *tg)
447{
448 do {
449 tg = list_entry_rcu(tg->list.next,
450 typeof(struct task_group), list);
451 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
452
453 if (&tg->list == &task_groups)
454 tg = NULL;
455
456 return tg;
457}
458
459#define for_each_rt_rq(rt_rq, iter, rq) \
460 for (iter = container_of(&task_groups, typeof(*iter), list); \
461 (iter = next_task_group(iter)) && \
462 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
Cheng Xuec514c42011-05-14 14:20:02 +0800463
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100464#define for_each_sched_rt_entity(rt_se) \
465 for (; rt_se; rt_se = rt_se->parent)
466
467static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
468{
469 return rt_se->my_q;
470}
471
Thomas Gleixner37dad3f2010-01-20 20:59:01 +0000472static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100473static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
474
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100475static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100476{
Dario Faggiolif6121f42008-10-03 17:40:46 +0200477 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
Kirill Tkhai88751252014-06-29 00:03:57 +0400478 struct rq *rq = rq_of_rt_rq(rt_rq);
Yong Zhang74b7eb52010-01-29 14:57:52 +0800479 struct sched_rt_entity *rt_se;
480
Kirill Tkhai88751252014-06-29 00:03:57 +0400481 int cpu = cpu_of(rq);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530482
483 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100484
Dario Faggiolif6121f42008-10-03 17:40:46 +0200485 if (rt_rq->rt_nr_running) {
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400486 if (!rt_se)
487 enqueue_top_rt_rq(rt_rq);
488 else if (!on_rt_rq(rt_se))
Thomas Gleixner37dad3f2010-01-20 20:59:01 +0000489 enqueue_rt_entity(rt_se, false);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400490
Gregory Haskinse864c492008-12-29 09:39:49 -0500491 if (rt_rq->highest_prio.curr < curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +0400492 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100493 }
494}
495
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100496static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100497{
Yong Zhang74b7eb52010-01-29 14:57:52 +0800498 struct sched_rt_entity *rt_se;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530499 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
Yong Zhang74b7eb52010-01-29 14:57:52 +0800500
Balbir Singh0c3b9162011-03-03 17:04:35 +0530501 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100502
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400503 if (!rt_se)
504 dequeue_top_rt_rq(rt_rq);
505 else if (on_rt_rq(rt_se))
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100506 dequeue_rt_entity(rt_se);
507}
508
Kirill Tkhai46383642014-03-15 02:15:07 +0400509static inline int rt_rq_throttled(struct rt_rq *rt_rq)
510{
511 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
512}
513
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100514static int rt_se_boosted(struct sched_rt_entity *rt_se)
515{
516 struct rt_rq *rt_rq = group_rt_rq(rt_se);
517 struct task_struct *p;
518
519 if (rt_rq)
520 return !!rt_rq->rt_nr_boosted;
521
522 p = rt_task_of(rt_se);
523 return p->prio != p->normal_prio;
524}
525
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200526#ifdef CONFIG_SMP
Rusty Russellc6c49272008-11-25 02:35:05 +1030527static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200528{
Nathan Zimmer424c93f2013-05-09 11:24:03 -0500529 return this_rq()->rd->span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200530}
531#else
Rusty Russellc6c49272008-11-25 02:35:05 +1030532static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200533{
Rusty Russellc6c49272008-11-25 02:35:05 +1030534 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200535}
536#endif
537
538static inline
539struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
540{
541 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
542}
543
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200544static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
545{
546 return &rt_rq->tg->rt_bandwidth;
547}
548
Dhaval Giani55e12e52008-06-24 23:39:43 +0530549#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100550
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100551static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100552{
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200553 return rt_rq->rt_runtime;
554}
555
556static inline u64 sched_rt_period(struct rt_rq *rt_rq)
557{
558 return ktime_to_ns(def_rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100559}
560
Cheng Xuec514c42011-05-14 14:20:02 +0800561typedef struct rt_rq *rt_rq_iter_t;
562
563#define for_each_rt_rq(rt_rq, iter, rq) \
564 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
565
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100566#define for_each_sched_rt_entity(rt_se) \
567 for (; rt_se; rt_se = NULL)
568
569static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
570{
571 return NULL;
572}
573
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100574static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100575{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400576 struct rq *rq = rq_of_rt_rq(rt_rq);
577
578 if (!rt_rq->rt_nr_running)
579 return;
580
581 enqueue_top_rt_rq(rt_rq);
Kirill Tkhai88751252014-06-29 00:03:57 +0400582 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100583}
584
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100585static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100586{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400587 dequeue_top_rt_rq(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100588}
589
Kirill Tkhai46383642014-03-15 02:15:07 +0400590static inline int rt_rq_throttled(struct rt_rq *rt_rq)
591{
592 return rt_rq->rt_throttled;
593}
594
Rusty Russellc6c49272008-11-25 02:35:05 +1030595static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200596{
Rusty Russellc6c49272008-11-25 02:35:05 +1030597 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200598}
599
600static inline
601struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
602{
603 return &cpu_rq(cpu)->rt;
604}
605
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200606static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
607{
608 return &def_rt_bandwidth;
609}
610
Dhaval Giani55e12e52008-06-24 23:39:43 +0530611#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100612
Juri Lellifaa59932014-02-21 11:37:15 +0100613bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
614{
615 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
616
617 return (hrtimer_active(&rt_b->rt_period_timer) ||
618 rt_rq->rt_time < rt_b->rt_runtime);
619}
620
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200621#ifdef CONFIG_SMP
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200622/*
623 * We ran out of runtime, see if we can borrow some from our neighbours.
624 */
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200625static int do_balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200626{
627 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
Shawn Bohreraa7f6732013-01-14 11:55:31 -0600628 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200629 int i, weight, more = 0;
630 u64 rt_period;
631
Rusty Russellc6c49272008-11-25 02:35:05 +1030632 weight = cpumask_weight(rd->span);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200633
Thomas Gleixner0986b112009-11-17 15:32:06 +0100634 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200635 rt_period = ktime_to_ns(rt_b->rt_period);
Rusty Russellc6c49272008-11-25 02:35:05 +1030636 for_each_cpu(i, rd->span) {
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200637 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
638 s64 diff;
639
640 if (iter == rt_rq)
641 continue;
642
Thomas Gleixner0986b112009-11-17 15:32:06 +0100643 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200644 /*
645 * Either all rqs have inf runtime and there's nothing to steal
646 * or __disable_runtime() below sets a specific rq to inf to
647 * indicate its been disabled and disalow stealing.
648 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200649 if (iter->rt_runtime == RUNTIME_INF)
650 goto next;
651
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200652 /*
653 * From runqueues with spare time, take 1/n part of their
654 * spare time, but no more than our period.
655 */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200656 diff = iter->rt_runtime - iter->rt_time;
657 if (diff > 0) {
Peter Zijlstra58838cf2008-07-24 12:43:13 +0200658 diff = div_u64((u64)diff, weight);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200659 if (rt_rq->rt_runtime + diff > rt_period)
660 diff = rt_period - rt_rq->rt_runtime;
661 iter->rt_runtime -= diff;
662 rt_rq->rt_runtime += diff;
663 more = 1;
664 if (rt_rq->rt_runtime == rt_period) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100665 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200666 break;
667 }
668 }
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200669next:
Thomas Gleixner0986b112009-11-17 15:32:06 +0100670 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200671 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100672 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200673
674 return more;
675}
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200676
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200677/*
678 * Ensure this RQ takes back all the runtime it lend to its neighbours.
679 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200680static void __disable_runtime(struct rq *rq)
681{
682 struct root_domain *rd = rq->rd;
Cheng Xuec514c42011-05-14 14:20:02 +0800683 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200684 struct rt_rq *rt_rq;
685
686 if (unlikely(!scheduler_running))
687 return;
688
Cheng Xuec514c42011-05-14 14:20:02 +0800689 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200690 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
691 s64 want;
692 int i;
693
Thomas Gleixner0986b112009-11-17 15:32:06 +0100694 raw_spin_lock(&rt_b->rt_runtime_lock);
695 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200696 /*
697 * Either we're all inf and nobody needs to borrow, or we're
698 * already disabled and thus have nothing to do, or we have
699 * exactly the right amount of runtime to take out.
700 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200701 if (rt_rq->rt_runtime == RUNTIME_INF ||
702 rt_rq->rt_runtime == rt_b->rt_runtime)
703 goto balanced;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100704 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200705
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200706 /*
707 * Calculate the difference between what we started out with
708 * and what we current have, that's the amount of runtime
709 * we lend and now have to reclaim.
710 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200711 want = rt_b->rt_runtime - rt_rq->rt_runtime;
712
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200713 /*
714 * Greedy reclaim, take back as much as we can.
715 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030716 for_each_cpu(i, rd->span) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200717 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
718 s64 diff;
719
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200720 /*
721 * Can't reclaim from ourselves or disabled runqueues.
722 */
Peter Zijlstraf1679d02008-08-14 15:49:00 +0200723 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200724 continue;
725
Thomas Gleixner0986b112009-11-17 15:32:06 +0100726 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200727 if (want > 0) {
728 diff = min_t(s64, iter->rt_runtime, want);
729 iter->rt_runtime -= diff;
730 want -= diff;
731 } else {
732 iter->rt_runtime -= want;
733 want -= want;
734 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100735 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200736
737 if (!want)
738 break;
739 }
740
Thomas Gleixner0986b112009-11-17 15:32:06 +0100741 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200742 /*
743 * We cannot be left wanting - that would mean some runtime
744 * leaked out of the system.
745 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200746 BUG_ON(want);
747balanced:
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200748 /*
749 * Disable all the borrow logic by pretending we have inf
750 * runtime - in which case borrowing doesn't make sense.
751 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200752 rt_rq->rt_runtime = RUNTIME_INF;
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -0700753 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100754 raw_spin_unlock(&rt_rq->rt_runtime_lock);
755 raw_spin_unlock(&rt_b->rt_runtime_lock);
Kirill Tkhai99b62562014-06-25 12:19:48 +0400756
757 /* Make rt_rq available for pick_next_task() */
758 sched_rt_rq_enqueue(rt_rq);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200759 }
760}
761
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200762static void __enable_runtime(struct rq *rq)
763{
Cheng Xuec514c42011-05-14 14:20:02 +0800764 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200765 struct rt_rq *rt_rq;
766
767 if (unlikely(!scheduler_running))
768 return;
769
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200770 /*
771 * Reset each runqueue's bandwidth settings
772 */
Cheng Xuec514c42011-05-14 14:20:02 +0800773 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200774 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
775
Thomas Gleixner0986b112009-11-17 15:32:06 +0100776 raw_spin_lock(&rt_b->rt_runtime_lock);
777 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200778 rt_rq->rt_runtime = rt_b->rt_runtime;
779 rt_rq->rt_time = 0;
Zhang, Yanminbaf25732008-09-09 11:26:33 +0800780 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100781 raw_spin_unlock(&rt_rq->rt_runtime_lock);
782 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200783 }
784}
785
Peter Zijlstraeff65492008-06-19 14:22:26 +0200786static int balance_runtime(struct rt_rq *rt_rq)
787{
788 int more = 0;
789
Peter Zijlstra4a6184c2011-10-06 22:39:14 +0200790 if (!sched_feat(RT_RUNTIME_SHARE))
791 return more;
792
Peter Zijlstraeff65492008-06-19 14:22:26 +0200793 if (rt_rq->rt_time > rt_rq->rt_runtime) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100794 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200795 more = do_balance_runtime(rt_rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100796 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200797 }
798
799 return more;
800}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530801#else /* !CONFIG_SMP */
Peter Zijlstraeff65492008-06-19 14:22:26 +0200802static inline int balance_runtime(struct rt_rq *rt_rq)
803{
804 return 0;
805}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530806#endif /* CONFIG_SMP */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100807
808static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
809{
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200810 int i, idle = 1, throttled = 0;
Rusty Russellc6c49272008-11-25 02:35:05 +1030811 const struct cpumask *span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200812
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200813 span = sched_rt_period_mask();
Mike Galbraithe221d022012-08-07 10:02:38 +0200814#ifdef CONFIG_RT_GROUP_SCHED
815 /*
816 * FIXME: isolated CPUs should really leave the root task group,
817 * whether they are isolcpus or were isolated via cpusets, lest
818 * the timer run on a CPU which does not service all runqueues,
819 * potentially leaving other CPUs indefinitely throttled. If
820 * isolation is really required, the user will turn the throttle
821 * off to kill the perturbations it causes anyway. Meanwhile,
822 * this maintains functionality for boot and/or troubleshooting.
823 */
824 if (rt_b == &root_task_group.rt_bandwidth)
825 span = cpu_online_mask;
826#endif
Rusty Russellc6c49272008-11-25 02:35:05 +1030827 for_each_cpu(i, span) {
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200828 int enqueue = 0;
829 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
830 struct rq *rq = rq_of_rt_rq(rt_rq);
831
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100832 raw_spin_lock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200833 if (rt_rq->rt_time) {
834 u64 runtime;
835
Thomas Gleixner0986b112009-11-17 15:32:06 +0100836 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200837 if (rt_rq->rt_throttled)
838 balance_runtime(rt_rq);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200839 runtime = rt_rq->rt_runtime;
840 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
841 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
842 rt_rq->rt_throttled = 0;
843 enqueue = 1;
Mike Galbraith61eadef2011-04-29 08:36:50 +0200844
845 /*
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100846 * When we're idle and a woken (rt) task is
847 * throttled check_preempt_curr() will set
848 * skip_update and the time between the wakeup
849 * and this unthrottle will get accounted as
850 * 'runtime'.
Mike Galbraith61eadef2011-04-29 08:36:50 +0200851 */
852 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100853 rq_clock_skip_update(rq, false);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200854 }
855 if (rt_rq->rt_time || rt_rq->rt_nr_running)
856 idle = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100857 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530858 } else if (rt_rq->rt_nr_running) {
Peter Zijlstra8a8cde12008-06-19 14:22:28 +0200859 idle = 0;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530860 if (!rt_rq_throttled(rt_rq))
861 enqueue = 1;
862 }
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200863 if (rt_rq->rt_throttled)
864 throttled = 1;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200865
866 if (enqueue)
867 sched_rt_rq_enqueue(rt_rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100868 raw_spin_unlock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200869 }
870
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200871 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
872 return 1;
873
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200874 return idle;
875}
876
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100877static inline int rt_se_prio(struct sched_rt_entity *rt_se)
878{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100879#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100880 struct rt_rq *rt_rq = group_rt_rq(rt_se);
881
882 if (rt_rq)
Gregory Haskinse864c492008-12-29 09:39:49 -0500883 return rt_rq->highest_prio.curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100884#endif
885
886 return rt_task_of(rt_se)->prio;
887}
888
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100889static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100890{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100891 u64 runtime = sched_rt_runtime(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100892
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100893 if (rt_rq->rt_throttled)
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100894 return rt_rq_throttled(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100895
Shan Hai5b680fd2011-11-29 11:03:56 +0800896 if (runtime >= sched_rt_period(rt_rq))
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200897 return 0;
898
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200899 balance_runtime(rt_rq);
900 runtime = sched_rt_runtime(rt_rq);
901 if (runtime == RUNTIME_INF)
902 return 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200903
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100904 if (rt_rq->rt_time > runtime) {
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200905 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
906
907 /*
908 * Don't actually throttle groups that have no runtime assigned
909 * but accrue some time due to boosting.
910 */
911 if (likely(rt_b->rt_runtime)) {
912 rt_rq->rt_throttled = 1;
John Stultzc2248152014-06-04 16:11:41 -0700913 printk_deferred_once("sched: RT throttling activated\n");
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200914 } else {
915 /*
916 * In case we did anyway, make it go away,
917 * replenishment is a joke, since it will replenish us
918 * with exactly 0 ns.
919 */
920 rt_rq->rt_time = 0;
921 }
922
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100923 if (rt_rq_throttled(rt_rq)) {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100924 sched_rt_rq_dequeue(rt_rq);
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100925 return 1;
926 }
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100927 }
928
929 return 0;
930}
931
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200932/*
933 * Update the current task's runtime statistics. Skip current tasks that
934 * are not in our scheduling class.
935 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200936static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200937{
938 struct task_struct *curr = rq->curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100939 struct sched_rt_entity *rt_se = &curr->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200940 u64 delta_exec;
941
Peter Zijlstra06c3bc62011-02-02 13:19:48 +0100942 if (curr->sched_class != &rt_sched_class)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200943 return;
944
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200945 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Kirill Tkhaifc79e242013-01-30 16:50:36 +0400946 if (unlikely((s64)delta_exec <= 0))
947 return;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200948
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200949 schedstat_set(curr->se.statistics.exec_max,
950 max(curr->se.statistics.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200951
952 curr->se.sum_exec_runtime += delta_exec;
Frank Mayharf06febc2008-09-12 09:54:39 -0700953 account_group_exec_runtime(curr, delta_exec);
954
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200955 curr->se.exec_start = rq_clock_task(rq);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100956 cpuacct_charge(curr, delta_exec);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100957
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200958 sched_rt_avg_update(rq, delta_exec);
959
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200960 if (!rt_bandwidth_enabled())
961 return;
962
Dhaval Giani354d60c2008-04-19 19:44:59 +0200963 for_each_sched_rt_entity(rt_se) {
Giedrius Rekasius0b079392014-05-25 15:23:31 +0100964 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
Dhaval Giani354d60c2008-04-19 19:44:59 +0200965
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200966 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100967 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200968 rt_rq->rt_time += delta_exec;
969 if (sched_rt_runtime_exceeded(rt_rq))
Kirill Tkhai88751252014-06-29 00:03:57 +0400970 resched_curr(rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100971 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200972 }
Dhaval Giani354d60c2008-04-19 19:44:59 +0200973 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200974}
975
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400976static void
977dequeue_top_rt_rq(struct rt_rq *rt_rq)
978{
979 struct rq *rq = rq_of_rt_rq(rt_rq);
980
981 BUG_ON(&rq->rt != rt_rq);
982
983 if (!rt_rq->rt_queued)
984 return;
985
986 BUG_ON(!rq->nr_running);
987
Kirill Tkhai72465442014-05-09 03:00:14 +0400988 sub_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400989 rt_rq->rt_queued = 0;
990}
991
992static void
993enqueue_top_rt_rq(struct rt_rq *rt_rq)
994{
995 struct rq *rq = rq_of_rt_rq(rt_rq);
996
997 BUG_ON(&rq->rt != rt_rq);
998
999 if (rt_rq->rt_queued)
1000 return;
1001 if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
1002 return;
1003
Kirill Tkhai72465442014-05-09 03:00:14 +04001004 add_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001005 rt_rq->rt_queued = 1;
1006}
1007
Gregory Haskins398a1532009-01-14 09:10:04 -05001008#if defined CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05001009
Gregory Haskins398a1532009-01-14 09:10:04 -05001010static void
1011inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001012{
Gregory Haskins4d984272008-12-29 09:39:49 -05001013 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins4d984272008-12-29 09:39:49 -05001014
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001015#ifdef CONFIG_RT_GROUP_SCHED
1016 /*
1017 * Change rq's cpupri only if rt_rq is the top queue.
1018 */
1019 if (&rq->rt != rt_rq)
1020 return;
1021#endif
Steven Rostedt5181f4a42011-06-16 21:55:23 -04001022 if (rq->online && prio < prev_prio)
1023 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
Steven Rostedt63489e42008-01-25 21:08:03 +01001024}
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001025
Gregory Haskins398a1532009-01-14 09:10:04 -05001026static void
1027dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001028{
Gregory Haskins4d984272008-12-29 09:39:49 -05001029 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001030
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001031#ifdef CONFIG_RT_GROUP_SCHED
1032 /*
1033 * Change rq's cpupri only if rt_rq is the top queue.
1034 */
1035 if (&rq->rt != rt_rq)
1036 return;
1037#endif
Gregory Haskins398a1532009-01-14 09:10:04 -05001038 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1039 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1040}
1041
1042#else /* CONFIG_SMP */
1043
1044static inline
1045void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1046static inline
1047void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1048
1049#endif /* CONFIG_SMP */
1050
Steven Rostedt63489e42008-01-25 21:08:03 +01001051#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001052static void
1053inc_rt_prio(struct rt_rq *rt_rq, int prio)
1054{
1055 int prev_prio = rt_rq->highest_prio.curr;
Steven Rostedt63489e42008-01-25 21:08:03 +01001056
Gregory Haskins398a1532009-01-14 09:10:04 -05001057 if (prio < prev_prio)
1058 rt_rq->highest_prio.curr = prio;
1059
1060 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1061}
1062
1063static void
1064dec_rt_prio(struct rt_rq *rt_rq, int prio)
1065{
1066 int prev_prio = rt_rq->highest_prio.curr;
1067
1068 if (rt_rq->rt_nr_running) {
1069
1070 WARN_ON(prio < prev_prio);
Gregory Haskinse864c492008-12-29 09:39:49 -05001071
1072 /*
Gregory Haskins398a1532009-01-14 09:10:04 -05001073 * This may have been our highest task, and therefore
1074 * we may have some recomputation to do
Gregory Haskinse864c492008-12-29 09:39:49 -05001075 */
Gregory Haskins398a1532009-01-14 09:10:04 -05001076 if (prio == prev_prio) {
Gregory Haskinse864c492008-12-29 09:39:49 -05001077 struct rt_prio_array *array = &rt_rq->active;
1078
1079 rt_rq->highest_prio.curr =
Steven Rostedt764a9d62008-01-25 21:08:04 +01001080 sched_find_first_bit(array->bitmap);
Gregory Haskinse864c492008-12-29 09:39:49 -05001081 }
1082
Steven Rostedt764a9d62008-01-25 21:08:04 +01001083 } else
Gregory Haskinse864c492008-12-29 09:39:49 -05001084 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001085
Gregory Haskins398a1532009-01-14 09:10:04 -05001086 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1087}
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001088
Gregory Haskins398a1532009-01-14 09:10:04 -05001089#else
1090
1091static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1092static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1093
1094#endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1095
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001096#ifdef CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001097
1098static void
1099inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1100{
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001101 if (rt_se_boosted(rt_se))
Steven Rostedt764a9d62008-01-25 21:08:04 +01001102 rt_rq->rt_nr_boosted++;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01001103
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001104 if (rt_rq->tg)
1105 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001106}
1107
1108static void
1109dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1110{
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001111 if (rt_se_boosted(rt_se))
1112 rt_rq->rt_nr_boosted--;
1113
1114 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
Gregory Haskins398a1532009-01-14 09:10:04 -05001115}
1116
1117#else /* CONFIG_RT_GROUP_SCHED */
1118
1119static void
1120inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1121{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001122 start_rt_bandwidth(&def_rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001123}
1124
1125static inline
1126void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1127
1128#endif /* CONFIG_RT_GROUP_SCHED */
1129
1130static inline
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001131unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1132{
1133 struct rt_rq *group_rq = group_rt_rq(rt_se);
1134
1135 if (group_rq)
1136 return group_rq->rt_nr_running;
1137 else
1138 return 1;
1139}
1140
1141static inline
Gregory Haskins398a1532009-01-14 09:10:04 -05001142void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1143{
1144 int prio = rt_se_prio(rt_se);
1145
1146 WARN_ON(!rt_prio(prio));
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001147 rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -05001148
1149 inc_rt_prio(rt_rq, prio);
1150 inc_rt_migration(rt_se, rt_rq);
1151 inc_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001152}
1153
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001154static inline
1155void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1156{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001157 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001158 WARN_ON(!rt_rq->rt_nr_running);
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001159 rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001160
Gregory Haskins398a1532009-01-14 09:10:04 -05001161 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1162 dec_rt_migration(rt_se, rt_rq);
1163 dec_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001164}
1165
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001166static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001167{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001168 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1169 struct rt_prio_array *array = &rt_rq->active;
1170 struct rt_rq *group_rq = group_rt_rq(rt_se);
Dmitry Adamushko20b63312008-06-11 00:58:30 +02001171 struct list_head *queue = array->queue + rt_se_prio(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001172
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001173 /*
1174 * Don't enqueue the group if its throttled, or when empty.
1175 * The latter is a consequence of the former when a child group
1176 * get throttled and the current group doesn't have any other
1177 * active members.
1178 */
1179 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001180 return;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001181
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001182 if (head)
1183 list_add(&rt_se->run_list, queue);
1184 else
1185 list_add_tail(&rt_se->run_list, queue);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001186 __set_bit(rt_se_prio(rt_se), array->bitmap);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001187
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001188 inc_rt_tasks(rt_se, rt_rq);
1189}
1190
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001191static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001192{
1193 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1194 struct rt_prio_array *array = &rt_rq->active;
1195
1196 list_del_init(&rt_se->run_list);
1197 if (list_empty(array->queue + rt_se_prio(rt_se)))
1198 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1199
1200 dec_rt_tasks(rt_se, rt_rq);
1201}
1202
1203/*
1204 * Because the prio of an upper entry depends on the lower
1205 * entries, we must remove entries top - down.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001206 */
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001207static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001208{
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001209 struct sched_rt_entity *back = NULL;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001210
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001211 for_each_sched_rt_entity(rt_se) {
1212 rt_se->back = back;
1213 back = rt_se;
1214 }
1215
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001216 dequeue_top_rt_rq(rt_rq_of_se(back));
1217
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001218 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1219 if (on_rt_rq(rt_se))
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001220 __dequeue_rt_entity(rt_se);
1221 }
1222}
1223
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001224static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001225{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001226 struct rq *rq = rq_of_rt_se(rt_se);
1227
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001228 dequeue_rt_stack(rt_se);
1229 for_each_sched_rt_entity(rt_se)
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001230 __enqueue_rt_entity(rt_se, head);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001231 enqueue_top_rt_rq(&rq->rt);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001232}
1233
1234static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
1235{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001236 struct rq *rq = rq_of_rt_se(rt_se);
1237
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001238 dequeue_rt_stack(rt_se);
1239
1240 for_each_sched_rt_entity(rt_se) {
1241 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1242
1243 if (rt_rq && rt_rq->rt_nr_running)
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001244 __enqueue_rt_entity(rt_se, false);
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001245 }
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001246 enqueue_top_rt_rq(&rq->rt);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001247}
1248
1249/*
1250 * Adding/removing a task to/from a priority array:
1251 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001252static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001253enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001254{
1255 struct sched_rt_entity *rt_se = &p->rt;
1256
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001257 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001258 rt_se->timeout = 0;
1259
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001260 enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001261
Peter Zijlstra29baa742012-04-23 12:11:21 +02001262 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001263 enqueue_pushable_task(rq, p);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001264}
1265
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001266static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001267{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001268 struct sched_rt_entity *rt_se = &p->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001269
1270 update_curr_rt(rq);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001271 dequeue_rt_entity(rt_se);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001272
Gregory Haskins917b6272008-12-29 09:39:53 -05001273 dequeue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001274}
1275
1276/*
Richard Weinberger60686312011-11-12 18:07:57 +01001277 * Put task to the head or the end of the run list without the overhead of
1278 * dequeue followed by enqueue.
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001279 */
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001280static void
1281requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001282{
Ingo Molnar1cdad712008-06-19 09:09:15 +02001283 if (on_rt_rq(rt_se)) {
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001284 struct rt_prio_array *array = &rt_rq->active;
1285 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1286
1287 if (head)
1288 list_move(&rt_se->run_list, queue);
1289 else
1290 list_move_tail(&rt_se->run_list, queue);
Ingo Molnar1cdad712008-06-19 09:09:15 +02001291 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001292}
1293
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001294static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001295{
1296 struct sched_rt_entity *rt_se = &p->rt;
1297 struct rt_rq *rt_rq;
1298
1299 for_each_sched_rt_entity(rt_se) {
1300 rt_rq = rt_rq_of_se(rt_se);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001301 requeue_rt_entity(rt_rq, rt_se, head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001302 }
1303}
1304
1305static void yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001306{
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001307 requeue_task_rt(rq, rq->curr, 0);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001308}
1309
Gregory Haskinse7693a32008-01-25 21:08:09 +01001310#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +01001311static int find_lowest_rq(struct task_struct *task);
1312
Peter Zijlstra0017d732010-03-24 18:34:10 +01001313static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01001314select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
Gregory Haskinse7693a32008-01-25 21:08:09 +01001315{
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001316 struct task_struct *curr;
1317 struct rq *rq;
Steven Rostedtc37495f2011-06-16 21:55:22 -04001318
1319 /* For anything but wake ups, just return the task_cpu */
1320 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1321 goto out;
1322
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001323 rq = cpu_rq(cpu);
1324
1325 rcu_read_lock();
1326 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
1327
Gregory Haskins318e0892008-01-25 21:08:10 +01001328 /*
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001329 * If the current task on @p's runqueue is an RT task, then
Steven Rostedte1f47d82008-01-25 21:08:12 +01001330 * try to see if we can wake this RT task up on another
1331 * runqueue. Otherwise simply start this RT task
1332 * on its current runqueue.
1333 *
Steven Rostedt43fa5462010-09-20 22:40:03 -04001334 * We want to avoid overloading runqueues. If the woken
1335 * task is a higher priority, then it will stay on this CPU
1336 * and the lower prio task should be moved to another CPU.
1337 * Even though this will probably make the lower prio task
1338 * lose its cache, we do not want to bounce a higher task
1339 * around just because it gave up its CPU, perhaps for a
1340 * lock?
1341 *
1342 * For equal prio tasks, we just let the scheduler sort it out.
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001343 *
Gregory Haskins318e0892008-01-25 21:08:10 +01001344 * Otherwise, just let it ride on the affined RQ and the
1345 * post-schedule router will push the preempted task away
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001346 *
1347 * This test is optimistic, if we get it wrong the load-balancer
1348 * will have to sort it out.
Gregory Haskins318e0892008-01-25 21:08:10 +01001349 */
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001350 if (curr && unlikely(rt_task(curr)) &&
Peter Zijlstra29baa742012-04-23 12:11:21 +02001351 (curr->nr_cpus_allowed < 2 ||
Shawn Bohrer6bfa6872013-10-04 14:24:53 -05001352 curr->prio <= p->prio)) {
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001353 int target = find_lowest_rq(p);
1354
Tim Chen80e3d872014-12-12 15:38:12 -08001355 /*
1356 * Don't bother moving it if the destination CPU is
1357 * not running a lower priority task.
1358 */
1359 if (target != -1 &&
1360 p->prio < cpu_rq(target)->rt.highest_prio.curr)
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001361 cpu = target;
1362 }
1363 rcu_read_unlock();
1364
Steven Rostedtc37495f2011-06-16 21:55:22 -04001365out:
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001366 return cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001367}
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001368
1369static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1370{
Wanpeng Li308a6232014-10-31 06:39:31 +08001371 /*
1372 * Current can't be migrated, useless to reschedule,
1373 * let's hope p can move out.
1374 */
1375 if (rq->curr->nr_cpus_allowed == 1 ||
1376 !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001377 return;
1378
Wanpeng Li308a6232014-10-31 06:39:31 +08001379 /*
1380 * p is migratable, so let's not schedule it and
1381 * see if it is pushed or pulled somewhere else.
1382 */
Peter Zijlstra29baa742012-04-23 12:11:21 +02001383 if (p->nr_cpus_allowed != 1
Rusty Russell13b8bd02009-03-25 15:01:22 +10301384 && cpupri_find(&rq->rd->cpupri, p, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001385 return;
1386
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001387 /*
1388 * There appears to be other cpus that can accept
1389 * current and none to run 'p', so lets reschedule
1390 * to try and push current away:
1391 */
1392 requeue_task_rt(rq, p, 1);
Kirill Tkhai88751252014-06-29 00:03:57 +04001393 resched_curr(rq);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001394}
1395
Gregory Haskinse7693a32008-01-25 21:08:09 +01001396#endif /* CONFIG_SMP */
1397
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001398/*
1399 * Preempt the current task with a newly woken task if needed:
1400 */
Peter Zijlstra7d478722009-09-14 19:55:44 +02001401static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001402{
Gregory Haskins45c01e82008-05-12 21:20:41 +02001403 if (p->prio < rq->curr->prio) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001404 resched_curr(rq);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001405 return;
1406 }
1407
1408#ifdef CONFIG_SMP
1409 /*
1410 * If:
1411 *
1412 * - the newly woken task is of equal priority to the current task
1413 * - the newly woken task is non-migratable while current is migratable
1414 * - current will be preempted on the next reschedule
1415 *
1416 * we should check to see if current can readily move to a different
1417 * cpu. If so, we will reschedule to allow the push logic to try
1418 * to move current somewhere else, making room for our non-migratable
1419 * task.
1420 */
Hillf Danton8dd0de82011-06-14 18:36:24 -04001421 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001422 check_preempt_equal_prio(rq, p);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001423#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001424}
1425
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001426static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1427 struct rt_rq *rt_rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001428{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001429 struct rt_prio_array *array = &rt_rq->active;
1430 struct sched_rt_entity *next = NULL;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001431 struct list_head *queue;
1432 int idx;
1433
1434 idx = sched_find_first_bit(array->bitmap);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001435 BUG_ON(idx >= MAX_RT_PRIO);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001436
1437 queue = array->queue + idx;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001438 next = list_entry(queue->next, struct sched_rt_entity, run_list);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001439
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001440 return next;
1441}
1442
Gregory Haskins917b6272008-12-29 09:39:53 -05001443static struct task_struct *_pick_next_task_rt(struct rq *rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001444{
1445 struct sched_rt_entity *rt_se;
1446 struct task_struct *p;
Peter Zijlstra606dba22012-02-11 06:05:00 +01001447 struct rt_rq *rt_rq = &rq->rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001448
1449 do {
1450 rt_se = pick_next_rt_entity(rq, rt_rq);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001451 BUG_ON(!rt_se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001452 rt_rq = group_rt_rq(rt_se);
1453 } while (rt_rq);
1454
1455 p = rt_task_of(rt_se);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001456 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001457
1458 return p;
1459}
1460
Peter Zijlstra606dba22012-02-11 06:05:00 +01001461static struct task_struct *
1462pick_next_task_rt(struct rq *rq, struct task_struct *prev)
Gregory Haskins917b6272008-12-29 09:39:53 -05001463{
Peter Zijlstra606dba22012-02-11 06:05:00 +01001464 struct task_struct *p;
1465 struct rt_rq *rt_rq = &rq->rt;
1466
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001467 if (need_pull_rt_task(rq, prev)) {
Peter Zijlstra38033c32014-01-23 20:32:21 +01001468 pull_rt_task(rq);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001469 /*
1470 * pull_rt_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001471 * means a dl or stop task can slip in, in which case we need
1472 * to re-start task selection.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001473 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001474 if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001475 rq->dl.dl_nr_running))
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001476 return RETRY_TASK;
1477 }
Peter Zijlstra38033c32014-01-23 20:32:21 +01001478
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001479 /*
1480 * We may dequeue prev's rt_rq in put_prev_task().
1481 * So, we update time before rt_nr_running check.
1482 */
1483 if (prev->sched_class == &rt_sched_class)
1484 update_curr_rt(rq);
1485
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001486 if (!rt_rq->rt_queued)
Peter Zijlstra606dba22012-02-11 06:05:00 +01001487 return NULL;
1488
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001489 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001490
1491 p = _pick_next_task_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001492
1493 /* The running task is never eligible for pushing */
Kirill Tkhaif3f17682014-09-12 17:42:01 +04001494 dequeue_pushable_task(rq, p);
Gregory Haskins917b6272008-12-29 09:39:53 -05001495
Peter Zijlstradc877342014-02-12 15:47:29 +01001496 set_post_schedule(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04001497
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001498 return p;
1499}
1500
Ingo Molnar31ee5292007-08-09 11:16:49 +02001501static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001502{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +02001503 update_curr_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001504
1505 /*
1506 * The previous task needs to be made eligible for pushing
1507 * if it is still active
1508 */
Peter Zijlstra29baa742012-04-23 12:11:21 +02001509 if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001510 enqueue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001511}
1512
Peter Williams681f3e62007-10-24 18:23:51 +02001513#ifdef CONFIG_SMP
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001514
Steven Rostedte8fa1362008-01-25 21:08:05 +01001515/* Only try algorithms three times */
1516#define RT_MAX_TRIES 3
1517
Steven Rostedtf65eda42008-01-25 21:08:07 +01001518static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1519{
1520 if (!task_running(rq, p) &&
Kirill Tkhai60334ca2013-01-31 18:56:17 +04001521 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001522 return 1;
1523 return 0;
1524}
1525
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001526/*
1527 * Return the highest pushable rq's task, which is suitable to be executed
1528 * on the cpu, NULL otherwise
1529 */
1530static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001531{
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001532 struct plist_head *head = &rq->rt.pushable_tasks;
1533 struct task_struct *p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001534
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001535 if (!has_pushable_tasks(rq))
1536 return NULL;
Peter Zijlstra3d074672010-03-10 17:07:24 +01001537
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001538 plist_for_each_entry(p, head, pushable_tasks) {
1539 if (pick_rt_task(rq, p, cpu))
1540 return p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001541 }
1542
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001543 return NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001544}
1545
Rusty Russell0e3900e2008-11-25 02:35:13 +10301546static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001547
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001548static int find_lowest_rq(struct task_struct *task)
1549{
1550 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001551 struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001552 int this_cpu = smp_processor_id();
1553 int cpu = task_cpu(task);
1554
Steven Rostedt0da938c2011-06-14 18:36:25 -04001555 /* Make sure the mask is initialized first */
1556 if (unlikely(!lowest_mask))
1557 return -1;
1558
Peter Zijlstra29baa742012-04-23 12:11:21 +02001559 if (task->nr_cpus_allowed == 1)
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001560 return -1; /* No other targets possible */
1561
1562 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
Gregory Haskins06f90db2008-01-25 21:08:13 +01001563 return -1; /* No targets found */
1564
1565 /*
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001566 * At this point we have built a mask of cpus representing the
1567 * lowest priority tasks in the system. Now we want to elect
1568 * the best one based on our affinity and topology.
1569 *
1570 * We prioritize the last cpu that the task executed on since
1571 * it is most likely cache-hot in that location.
1572 */
Rusty Russell96f874e2008-11-25 02:35:14 +10301573 if (cpumask_test_cpu(cpu, lowest_mask))
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001574 return cpu;
1575
1576 /*
1577 * Otherwise, we consult the sched_domains span maps to figure
1578 * out which cpu is logically closest to our hot cache data.
1579 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301580 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1581 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001582
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001583 rcu_read_lock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301584 for_each_domain(cpu, sd) {
1585 if (sd->flags & SD_WAKE_AFFINE) {
1586 int best_cpu;
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001587
Rusty Russelle2c88062009-11-03 14:53:15 +10301588 /*
1589 * "this_cpu" is cheaper to preempt than a
1590 * remote processor.
1591 */
1592 if (this_cpu != -1 &&
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001593 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1594 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301595 return this_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001596 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001597
Rusty Russelle2c88062009-11-03 14:53:15 +10301598 best_cpu = cpumask_first_and(lowest_mask,
1599 sched_domain_span(sd));
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001600 if (best_cpu < nr_cpu_ids) {
1601 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301602 return best_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001603 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001604 }
1605 }
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001606 rcu_read_unlock();
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001607
1608 /*
1609 * And finally, if there were no matches within the domains
1610 * just give the caller *something* to work with from the compatible
1611 * locations.
1612 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301613 if (this_cpu != -1)
1614 return this_cpu;
1615
1616 cpu = cpumask_any(lowest_mask);
1617 if (cpu < nr_cpu_ids)
1618 return cpu;
1619 return -1;
Gregory Haskins07b40322008-01-25 21:08:10 +01001620}
1621
Steven Rostedte8fa1362008-01-25 21:08:05 +01001622/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +01001623static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001624{
1625 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001626 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +01001627 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001628
1629 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +01001630 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001631
Gregory Haskins2de0b462008-01-25 21:08:10 +01001632 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +01001633 break;
1634
Gregory Haskins07b40322008-01-25 21:08:10 +01001635 lowest_rq = cpu_rq(cpu);
1636
Tim Chen80e3d872014-12-12 15:38:12 -08001637 if (lowest_rq->rt.highest_prio.curr <= task->prio) {
1638 /*
1639 * Target rq has tasks of equal or higher priority,
1640 * retrying does not release any lock and is unlikely
1641 * to yield a different result.
1642 */
1643 lowest_rq = NULL;
1644 break;
1645 }
1646
Steven Rostedte8fa1362008-01-25 21:08:05 +01001647 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +01001648 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +01001649 /*
1650 * We had to unlock the run queue. In
1651 * the mean time, task could have
1652 * migrated already or had its affinity changed.
1653 * Also make sure that it wasn't scheduled on its rq.
1654 */
Gregory Haskins07b40322008-01-25 21:08:10 +01001655 if (unlikely(task_rq(task) != rq ||
Rusty Russell96f874e2008-11-25 02:35:14 +10301656 !cpumask_test_cpu(lowest_rq->cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001657 tsk_cpus_allowed(task)) ||
Gregory Haskins07b40322008-01-25 21:08:10 +01001658 task_running(rq, task) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001659 !task_on_rq_queued(task))) {
Ingo Molnar4df64c02008-01-25 21:08:15 +01001660
Peter Zijlstra7f1b4392012-05-17 21:19:46 +02001661 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001662 lowest_rq = NULL;
1663 break;
1664 }
1665 }
1666
1667 /* If this rq is still suitable use it. */
Gregory Haskinse864c492008-12-29 09:39:49 -05001668 if (lowest_rq->rt.highest_prio.curr > task->prio)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001669 break;
1670
1671 /* try again */
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001672 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001673 lowest_rq = NULL;
1674 }
1675
1676 return lowest_rq;
1677}
1678
Gregory Haskins917b6272008-12-29 09:39:53 -05001679static struct task_struct *pick_next_pushable_task(struct rq *rq)
1680{
1681 struct task_struct *p;
1682
1683 if (!has_pushable_tasks(rq))
1684 return NULL;
1685
1686 p = plist_first_entry(&rq->rt.pushable_tasks,
1687 struct task_struct, pushable_tasks);
1688
1689 BUG_ON(rq->cpu != task_cpu(p));
1690 BUG_ON(task_current(rq, p));
Peter Zijlstra29baa742012-04-23 12:11:21 +02001691 BUG_ON(p->nr_cpus_allowed <= 1);
Gregory Haskins917b6272008-12-29 09:39:53 -05001692
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001693 BUG_ON(!task_on_rq_queued(p));
Gregory Haskins917b6272008-12-29 09:39:53 -05001694 BUG_ON(!rt_task(p));
1695
1696 return p;
1697}
1698
Steven Rostedte8fa1362008-01-25 21:08:05 +01001699/*
1700 * If the current CPU has more than one RT task, see if the non
1701 * running task can migrate over to a CPU that is running a task
1702 * of lesser priority.
1703 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001704static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001705{
1706 struct task_struct *next_task;
1707 struct rq *lowest_rq;
Hillf Danton311e8002011-06-16 21:55:20 -04001708 int ret = 0;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001709
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +01001710 if (!rq->rt.overloaded)
1711 return 0;
1712
Gregory Haskins917b6272008-12-29 09:39:53 -05001713 next_task = pick_next_pushable_task(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001714 if (!next_task)
1715 return 0;
1716
Peter Zijlstra49246272010-10-17 21:46:10 +02001717retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +01001718 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001719 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001720 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001721 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01001722
1723 /*
1724 * It's possible that the next_task slipped in of
1725 * higher priority than current. If that's the case
1726 * just reschedule current.
1727 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001728 if (unlikely(next_task->prio < rq->curr->prio)) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001729 resched_curr(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001730 return 0;
1731 }
1732
Gregory Haskins697f0a42008-01-25 21:08:09 +01001733 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +01001734 get_task_struct(next_task);
1735
1736 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001737 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001738 if (!lowest_rq) {
1739 struct task_struct *task;
1740 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001741 * find_lock_lowest_rq releases rq->lock
Gregory Haskins15635132008-12-29 09:39:53 -05001742 * so it is possible that next_task has migrated.
1743 *
1744 * We need to make sure that the task is still on the same
1745 * run-queue and is also still the next task eligible for
1746 * pushing.
Steven Rostedte8fa1362008-01-25 21:08:05 +01001747 */
Gregory Haskins917b6272008-12-29 09:39:53 -05001748 task = pick_next_pushable_task(rq);
Gregory Haskins15635132008-12-29 09:39:53 -05001749 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1750 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001751 * The task hasn't migrated, and is still the next
1752 * eligible task, but we failed to find a run-queue
1753 * to push it to. Do not retry in this case, since
1754 * other cpus will pull from us when ready.
Gregory Haskins15635132008-12-29 09:39:53 -05001755 */
Gregory Haskins15635132008-12-29 09:39:53 -05001756 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001757 }
Gregory Haskins917b6272008-12-29 09:39:53 -05001758
Gregory Haskins15635132008-12-29 09:39:53 -05001759 if (!task)
1760 /* No more tasks, just exit */
1761 goto out;
1762
Gregory Haskins917b6272008-12-29 09:39:53 -05001763 /*
Gregory Haskins15635132008-12-29 09:39:53 -05001764 * Something has shifted, try again.
Gregory Haskins917b6272008-12-29 09:39:53 -05001765 */
Gregory Haskins15635132008-12-29 09:39:53 -05001766 put_task_struct(next_task);
1767 next_task = task;
1768 goto retry;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001769 }
1770
Gregory Haskins697f0a42008-01-25 21:08:09 +01001771 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001772 set_task_cpu(next_task, lowest_rq->cpu);
1773 activate_task(lowest_rq, next_task, 0);
Hillf Danton311e8002011-06-16 21:55:20 -04001774 ret = 1;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001775
Kirill Tkhai88751252014-06-29 00:03:57 +04001776 resched_curr(lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001777
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001778 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001779
Steven Rostedte8fa1362008-01-25 21:08:05 +01001780out:
1781 put_task_struct(next_task);
1782
Hillf Danton311e8002011-06-16 21:55:20 -04001783 return ret;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001784}
1785
Steven Rostedte8fa1362008-01-25 21:08:05 +01001786static void push_rt_tasks(struct rq *rq)
1787{
1788 /* push_rt_task will return true if it moved an RT */
1789 while (push_rt_task(rq))
1790 ;
1791}
1792
Steven Rostedtb6366f02015-03-18 14:49:46 -04001793#ifdef HAVE_RT_PUSH_IPI
1794/*
1795 * The search for the next cpu always starts at rq->cpu and ends
1796 * when we reach rq->cpu again. It will never return rq->cpu.
1797 * This returns the next cpu to check, or nr_cpu_ids if the loop
1798 * is complete.
1799 *
1800 * rq->rt.push_cpu holds the last cpu returned by this function,
1801 * or if this is the first instance, it must hold rq->cpu.
1802 */
1803static int rto_next_cpu(struct rq *rq)
1804{
1805 int prev_cpu = rq->rt.push_cpu;
1806 int cpu;
1807
1808 cpu = cpumask_next(prev_cpu, rq->rd->rto_mask);
1809
1810 /*
1811 * If the previous cpu is less than the rq's CPU, then it already
1812 * passed the end of the mask, and has started from the beginning.
1813 * We end if the next CPU is greater or equal to rq's CPU.
1814 */
1815 if (prev_cpu < rq->cpu) {
1816 if (cpu >= rq->cpu)
1817 return nr_cpu_ids;
1818
1819 } else if (cpu >= nr_cpu_ids) {
1820 /*
1821 * We passed the end of the mask, start at the beginning.
1822 * If the result is greater or equal to the rq's CPU, then
1823 * the loop is finished.
1824 */
1825 cpu = cpumask_first(rq->rd->rto_mask);
1826 if (cpu >= rq->cpu)
1827 return nr_cpu_ids;
1828 }
1829 rq->rt.push_cpu = cpu;
1830
1831 /* Return cpu to let the caller know if the loop is finished or not */
1832 return cpu;
1833}
1834
1835static int find_next_push_cpu(struct rq *rq)
1836{
1837 struct rq *next_rq;
1838 int cpu;
1839
1840 while (1) {
1841 cpu = rto_next_cpu(rq);
1842 if (cpu >= nr_cpu_ids)
1843 break;
1844 next_rq = cpu_rq(cpu);
1845
1846 /* Make sure the next rq can push to this rq */
1847 if (next_rq->rt.highest_prio.next < rq->rt.highest_prio.curr)
1848 break;
1849 }
1850
1851 return cpu;
1852}
1853
1854#define RT_PUSH_IPI_EXECUTING 1
1855#define RT_PUSH_IPI_RESTART 2
1856
1857static void tell_cpu_to_push(struct rq *rq)
1858{
1859 int cpu;
1860
1861 if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) {
1862 raw_spin_lock(&rq->rt.push_lock);
1863 /* Make sure it's still executing */
1864 if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) {
1865 /*
1866 * Tell the IPI to restart the loop as things have
1867 * changed since it started.
1868 */
1869 rq->rt.push_flags |= RT_PUSH_IPI_RESTART;
1870 raw_spin_unlock(&rq->rt.push_lock);
1871 return;
1872 }
1873 raw_spin_unlock(&rq->rt.push_lock);
1874 }
1875
1876 /* When here, there's no IPI going around */
1877
1878 rq->rt.push_cpu = rq->cpu;
1879 cpu = find_next_push_cpu(rq);
1880 if (cpu >= nr_cpu_ids)
1881 return;
1882
1883 rq->rt.push_flags = RT_PUSH_IPI_EXECUTING;
1884
1885 irq_work_queue_on(&rq->rt.push_work, cpu);
1886}
1887
1888/* Called from hardirq context */
1889static void try_to_push_tasks(void *arg)
1890{
1891 struct rt_rq *rt_rq = arg;
1892 struct rq *rq, *src_rq;
1893 int this_cpu;
1894 int cpu;
1895
1896 this_cpu = rt_rq->push_cpu;
1897
1898 /* Paranoid check */
1899 BUG_ON(this_cpu != smp_processor_id());
1900
1901 rq = cpu_rq(this_cpu);
1902 src_rq = rq_of_rt_rq(rt_rq);
1903
1904again:
1905 if (has_pushable_tasks(rq)) {
1906 raw_spin_lock(&rq->lock);
1907 push_rt_task(rq);
1908 raw_spin_unlock(&rq->lock);
1909 }
1910
1911 /* Pass the IPI to the next rt overloaded queue */
1912 raw_spin_lock(&rt_rq->push_lock);
1913 /*
1914 * If the source queue changed since the IPI went out,
1915 * we need to restart the search from that CPU again.
1916 */
1917 if (rt_rq->push_flags & RT_PUSH_IPI_RESTART) {
1918 rt_rq->push_flags &= ~RT_PUSH_IPI_RESTART;
1919 rt_rq->push_cpu = src_rq->cpu;
1920 }
1921
1922 cpu = find_next_push_cpu(src_rq);
1923
1924 if (cpu >= nr_cpu_ids)
1925 rt_rq->push_flags &= ~RT_PUSH_IPI_EXECUTING;
1926 raw_spin_unlock(&rt_rq->push_lock);
1927
1928 if (cpu >= nr_cpu_ids)
1929 return;
1930
1931 /*
1932 * It is possible that a restart caused this CPU to be
1933 * chosen again. Don't bother with an IPI, just see if we
1934 * have more to push.
1935 */
1936 if (unlikely(cpu == rq->cpu))
1937 goto again;
1938
1939 /* Try the next RT overloaded CPU */
1940 irq_work_queue_on(&rt_rq->push_work, cpu);
1941}
1942
1943static void push_irq_work_func(struct irq_work *work)
1944{
1945 struct rt_rq *rt_rq = container_of(work, struct rt_rq, push_work);
1946
1947 try_to_push_tasks(rt_rq);
1948}
1949#endif /* HAVE_RT_PUSH_IPI */
1950
Steven Rostedtf65eda42008-01-25 21:08:07 +01001951static int pull_rt_task(struct rq *this_rq)
1952{
Ingo Molnar80bf3172008-01-25 21:08:17 +01001953 int this_cpu = this_rq->cpu, ret = 0, cpu;
Gregory Haskinsa8728942008-12-29 09:39:49 -05001954 struct task_struct *p;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001955 struct rq *src_rq;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001956
Gregory Haskins637f5082008-01-25 21:08:18 +01001957 if (likely(!rt_overloaded(this_rq)))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001958 return 0;
1959
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +02001960 /*
1961 * Match the barrier from rt_set_overloaded; this guarantees that if we
1962 * see overloaded we must also see the rto_mask bit.
1963 */
1964 smp_rmb();
1965
Steven Rostedtb6366f02015-03-18 14:49:46 -04001966#ifdef HAVE_RT_PUSH_IPI
1967 if (sched_feat(RT_PUSH_IPI)) {
1968 tell_cpu_to_push(this_rq);
1969 return 0;
1970 }
1971#endif
1972
Rusty Russellc6c49272008-11-25 02:35:05 +10301973 for_each_cpu(cpu, this_rq->rd->rto_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001974 if (this_cpu == cpu)
1975 continue;
1976
1977 src_rq = cpu_rq(cpu);
Gregory Haskins74ab8e42008-12-29 09:39:50 -05001978
1979 /*
1980 * Don't bother taking the src_rq->lock if the next highest
1981 * task is known to be lower-priority than our current task.
1982 * This may look racy, but if this value is about to go
1983 * logically higher, the src_rq will push this task away.
1984 * And if its going logically lower, we do not care
1985 */
1986 if (src_rq->rt.highest_prio.next >=
1987 this_rq->rt.highest_prio.curr)
1988 continue;
1989
Steven Rostedtf65eda42008-01-25 21:08:07 +01001990 /*
1991 * We can potentially drop this_rq's lock in
1992 * double_lock_balance, and another CPU could
Gregory Haskinsa8728942008-12-29 09:39:49 -05001993 * alter this_rq
Steven Rostedtf65eda42008-01-25 21:08:07 +01001994 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05001995 double_lock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01001996
1997 /*
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001998 * We can pull only a task, which is pushable
1999 * on its rq, and no others.
Steven Rostedtf65eda42008-01-25 21:08:07 +01002000 */
Kirill Tkhaie23ee742013-06-07 15:37:43 -04002001 p = pick_highest_pushable_task(src_rq, this_cpu);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002002
2003 /*
2004 * Do we have an RT task that preempts
2005 * the to-be-scheduled task?
2006 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002007 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01002008 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002009 WARN_ON(!task_on_rq_queued(p));
Steven Rostedtf65eda42008-01-25 21:08:07 +01002010
2011 /*
2012 * There's a chance that p is higher in priority
2013 * than what's currently running on its cpu.
2014 * This is just that p is wakeing up and hasn't
2015 * had a chance to schedule. We only pull
2016 * p if it is lower in priority than the
Gregory Haskinsa8728942008-12-29 09:39:49 -05002017 * current task on the run queue
Steven Rostedtf65eda42008-01-25 21:08:07 +01002018 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002019 if (p->prio < src_rq->curr->prio)
Mike Galbraith614ee1f2008-01-25 21:08:30 +01002020 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002021
2022 ret = 1;
2023
2024 deactivate_task(src_rq, p, 0);
2025 set_task_cpu(p, this_cpu);
2026 activate_task(this_rq, p, 0);
2027 /*
2028 * We continue with the search, just in
2029 * case there's an even higher prio task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002030 * in another runqueue. (low likelihood
Steven Rostedtf65eda42008-01-25 21:08:07 +01002031 * but possible)
Steven Rostedtf65eda42008-01-25 21:08:07 +01002032 */
Steven Rostedtf65eda42008-01-25 21:08:07 +01002033 }
Peter Zijlstra49246272010-10-17 21:46:10 +02002034skip:
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02002035 double_unlock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002036 }
2037
2038 return ret;
2039}
2040
Steven Rostedt9a897c52008-01-25 21:08:22 +01002041static void post_schedule_rt(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01002042{
Gregory Haskins967fc042008-12-29 09:39:52 -05002043 push_rt_tasks(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002044}
2045
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002046/*
2047 * If we are not running and we are not going to reschedule soon, we should
2048 * try to push tasks away now
2049 */
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002050static void task_woken_rt(struct rq *rq, struct task_struct *p)
Steven Rostedt4642daf2008-01-25 21:08:07 +01002051{
Steven Rostedt9a897c52008-01-25 21:08:22 +01002052 if (!task_running(rq, p) &&
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002053 !test_tsk_need_resched(rq->curr) &&
Gregory Haskins917b6272008-12-29 09:39:53 -05002054 has_pushable_tasks(rq) &&
Peter Zijlstra29baa742012-04-23 12:11:21 +02002055 p->nr_cpus_allowed > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01002056 (dl_task(rq->curr) || rt_task(rq->curr)) &&
Peter Zijlstra29baa742012-04-23 12:11:21 +02002057 (rq->curr->nr_cpus_allowed < 2 ||
Shawn Bohrer3be209a2011-09-12 09:28:04 -05002058 rq->curr->prio <= p->prio))
Steven Rostedt4642daf2008-01-25 21:08:07 +01002059 push_rt_tasks(rq);
2060}
2061
Mike Traviscd8ba7c2008-03-26 14:23:49 -07002062static void set_cpus_allowed_rt(struct task_struct *p,
Rusty Russell96f874e2008-11-25 02:35:14 +10302063 const struct cpumask *new_mask)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002064{
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002065 struct rq *rq;
2066 int weight;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002067
2068 BUG_ON(!rt_task(p));
2069
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002070 if (!task_on_rq_queued(p))
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002071 return;
2072
2073 weight = cpumask_weight(new_mask);
2074
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002075 /*
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002076 * Only update if the process changes its state from whether it
2077 * can migrate or not.
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002078 */
Peter Zijlstra29baa742012-04-23 12:11:21 +02002079 if ((p->nr_cpus_allowed > 1) == (weight > 1))
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002080 return;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002081
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002082 rq = task_rq(p);
Gregory Haskins917b6272008-12-29 09:39:53 -05002083
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002084 /*
2085 * The process used to be able to migrate OR it can now migrate
2086 */
2087 if (weight <= 1) {
2088 if (!task_current(rq, p))
2089 dequeue_pushable_task(rq, p);
2090 BUG_ON(!rq->rt.rt_nr_migratory);
2091 rq->rt.rt_nr_migratory--;
2092 } else {
2093 if (!task_current(rq, p))
2094 enqueue_pushable_task(rq, p);
2095 rq->rt.rt_nr_migratory++;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002096 }
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002097
2098 update_rt_migration(&rq->rt);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002099}
Ingo Molnardeeeccd2008-01-25 21:08:15 +01002100
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002101/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002102static void rq_online_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002103{
2104 if (rq->rt.overloaded)
2105 rt_set_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002106
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002107 __enable_runtime(rq);
2108
Gregory Haskinse864c492008-12-29 09:39:49 -05002109 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002110}
2111
2112/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002113static void rq_offline_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002114{
2115 if (rq->rt.overloaded)
2116 rt_clear_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002117
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002118 __disable_runtime(rq);
2119
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002120 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002121}
Steven Rostedtcb469842008-01-25 21:08:22 +01002122
2123/*
2124 * When switch from the rt queue, we bring ourselves to a position
2125 * that we might want to pull RT tasks from other runqueues.
2126 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002127static void switched_from_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002128{
2129 /*
2130 * If there are other RT tasks then we will reschedule
2131 * and the scheduling of the other RT tasks will handle
2132 * the balancing. But if we are the last RT task
2133 * we may need to handle the pulling of RT tasks
2134 * now.
2135 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002136 if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
Kirill Tkhai1158ddb2012-11-23 00:02:15 +04002137 return;
2138
2139 if (pull_rt_task(rq))
Kirill Tkhai88751252014-06-29 00:03:57 +04002140 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002141}
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302142
Li Zefan11c785b2014-02-08 14:17:45 +08002143void __init init_sched_rt_class(void)
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302144{
2145 unsigned int i;
2146
Peter Zijlstra029632f2011-10-25 10:00:11 +02002147 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -07002148 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
Mike Travis6ca09df2008-12-31 18:08:45 -08002149 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra029632f2011-10-25 10:00:11 +02002150 }
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302151}
Steven Rostedte8fa1362008-01-25 21:08:05 +01002152#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002153
Steven Rostedtcb469842008-01-25 21:08:22 +01002154/*
2155 * When switching a task to RT, we may overload the runqueue
2156 * with RT tasks. In this case we try to push them off to
2157 * other runqueues.
2158 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002159static void switched_to_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002160{
2161 int check_resched = 1;
2162
2163 /*
2164 * If we are already running, then there's nothing
2165 * that needs to be done. But if we are not running
2166 * we may need to preempt the current running task.
2167 * If that current running task is also an RT task
2168 * then see if we can move to another run queue.
2169 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002170 if (task_on_rq_queued(p) && rq->curr != p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002171#ifdef CONFIG_SMP
Kirill V Tkhai10447912014-03-12 06:18:33 -04002172 if (p->nr_cpus_allowed > 1 && rq->rt.overloaded &&
Steven Rostedtcb469842008-01-25 21:08:22 +01002173 /* Don't resched if we changed runqueues */
Kirill V Tkhai10447912014-03-12 06:18:33 -04002174 push_rt_task(rq) && rq != task_rq(p))
Steven Rostedtcb469842008-01-25 21:08:22 +01002175 check_resched = 0;
2176#endif /* CONFIG_SMP */
2177 if (check_resched && p->prio < rq->curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002178 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002179 }
2180}
2181
2182/*
2183 * Priority of the task has changed. This may cause
2184 * us to initiate a push or pull.
2185 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002186static void
2187prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01002188{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002189 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002190 return;
2191
2192 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002193#ifdef CONFIG_SMP
2194 /*
2195 * If our priority decreases while running, we
2196 * may need to pull tasks to this runqueue.
2197 */
2198 if (oldprio < p->prio)
2199 pull_rt_task(rq);
2200 /*
2201 * If there's a higher priority task waiting to run
Steven Rostedt6fa46fa2008-03-05 10:00:12 -05002202 * then reschedule. Note, the above pull_rt_task
2203 * can release the rq lock and p could migrate.
2204 * Only reschedule if p is still on the same runqueue.
Steven Rostedtcb469842008-01-25 21:08:22 +01002205 */
Gregory Haskinse864c492008-12-29 09:39:49 -05002206 if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04002207 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002208#else
2209 /* For UP simply resched on drop of prio */
2210 if (oldprio < p->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002211 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002212#endif /* CONFIG_SMP */
2213 } else {
2214 /*
2215 * This task is not running, but if it is
2216 * greater than the current running task
2217 * then reschedule.
2218 */
2219 if (p->prio < rq->curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002220 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002221 }
2222}
2223
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002224static void watchdog(struct rq *rq, struct task_struct *p)
2225{
2226 unsigned long soft, hard;
2227
Jiri Slaby78d7d402010-03-05 13:42:54 -08002228 /* max may change after cur was read, this will be fixed next tick */
2229 soft = task_rlimit(p, RLIMIT_RTTIME);
2230 hard = task_rlimit_max(p, RLIMIT_RTTIME);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002231
2232 if (soft != RLIM_INFINITY) {
2233 unsigned long next;
2234
Ying Xue57d2aa02012-07-17 15:03:43 +08002235 if (p->rt.watchdog_stamp != jiffies) {
2236 p->rt.timeout++;
2237 p->rt.watchdog_stamp = jiffies;
2238 }
2239
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002240 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
Peter Zijlstra5a52dd52008-01-25 21:08:32 +01002241 if (p->rt.timeout > next)
Frank Mayharf06febc2008-09-12 09:54:39 -07002242 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002243 }
2244}
Steven Rostedtcb469842008-01-25 21:08:22 +01002245
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002246static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002247{
Colin Cross454c7992012-05-16 21:34:23 -07002248 struct sched_rt_entity *rt_se = &p->rt;
2249
Peter Zijlstra67e2be02007-12-20 15:01:17 +01002250 update_curr_rt(rq);
2251
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002252 watchdog(rq, p);
2253
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002254 /*
2255 * RR tasks need a special form of timeslice management.
2256 * FIFO tasks have no timeslices.
2257 */
2258 if (p->policy != SCHED_RR)
2259 return;
2260
Peter Zijlstrafa717062008-01-25 21:08:27 +01002261 if (--p->rt.time_slice)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002262 return;
2263
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002264 p->rt.time_slice = sched_rr_timeslice;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002265
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002266 /*
Li Bine9aa39b2013-10-21 20:15:43 +08002267 * Requeue to the end of queue if we (and all of our ancestors) are not
2268 * the only element on the queue
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002269 */
Colin Cross454c7992012-05-16 21:34:23 -07002270 for_each_sched_rt_entity(rt_se) {
2271 if (rt_se->run_list.prev != rt_se->run_list.next) {
2272 requeue_task_rt(rq, p, 0);
Kirill Tkhai8aa6f0e2014-09-22 22:36:43 +04002273 resched_curr(rq);
Colin Cross454c7992012-05-16 21:34:23 -07002274 return;
2275 }
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002276 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002277}
2278
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002279static void set_curr_task_rt(struct rq *rq)
2280{
2281 struct task_struct *p = rq->curr;
2282
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002283 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05002284
2285 /* The running task is never eligible for pushing */
2286 dequeue_pushable_task(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002287}
2288
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07002289static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00002290{
2291 /*
2292 * Time slice is 0 for SCHED_FIFO tasks
2293 */
2294 if (task->policy == SCHED_RR)
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002295 return sched_rr_timeslice;
Peter Williams0d721ce2009-09-21 01:31:53 +00002296 else
2297 return 0;
2298}
2299
Peter Zijlstra029632f2011-10-25 10:00:11 +02002300const struct sched_class rt_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002301 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002302 .enqueue_task = enqueue_task_rt,
2303 .dequeue_task = dequeue_task_rt,
2304 .yield_task = yield_task_rt,
2305
2306 .check_preempt_curr = check_preempt_curr_rt,
2307
2308 .pick_next_task = pick_next_task_rt,
2309 .put_prev_task = put_prev_task_rt,
2310
Peter Williams681f3e62007-10-24 18:23:51 +02002311#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08002312 .select_task_rq = select_task_rq_rt,
2313
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002314 .set_cpus_allowed = set_cpus_allowed_rt,
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002315 .rq_online = rq_online_rt,
2316 .rq_offline = rq_offline_rt,
Steven Rostedt9a897c52008-01-25 21:08:22 +01002317 .post_schedule = post_schedule_rt,
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002318 .task_woken = task_woken_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002319 .switched_from = switched_from_rt,
Peter Williams681f3e62007-10-24 18:23:51 +02002320#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002321
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002322 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002323 .task_tick = task_tick_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002324
Peter Williams0d721ce2009-09-21 01:31:53 +00002325 .get_rr_interval = get_rr_interval_rt,
2326
Steven Rostedtcb469842008-01-25 21:08:22 +01002327 .prio_changed = prio_changed_rt,
2328 .switched_to = switched_to_rt,
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002329
2330 .update_curr = update_curr_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002331};
Peter Zijlstraada18de2008-06-19 14:22:24 +02002332
2333#ifdef CONFIG_SCHED_DEBUG
2334extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2335
Peter Zijlstra029632f2011-10-25 10:00:11 +02002336void print_rt_stats(struct seq_file *m, int cpu)
Peter Zijlstraada18de2008-06-19 14:22:24 +02002337{
Cheng Xuec514c42011-05-14 14:20:02 +08002338 rt_rq_iter_t iter;
Peter Zijlstraada18de2008-06-19 14:22:24 +02002339 struct rt_rq *rt_rq;
2340
2341 rcu_read_lock();
Cheng Xuec514c42011-05-14 14:20:02 +08002342 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
Peter Zijlstraada18de2008-06-19 14:22:24 +02002343 print_rt_rq(m, cpu, rt_rq);
2344 rcu_read_unlock();
2345}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302346#endif /* CONFIG_SCHED_DEBUG */