blob: b0febf25d8f18bff3da7c89f5139c7ec8bcee08b [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);
Peter Zijlstra029632f2011-10-25 10:00:11 +020021 int idle = 0;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020022 int overrun;
Peter Zijlstra029632f2011-10-25 10:00:11 +020023
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020024 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020025 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020026 overrun = hrtimer_forward_now(timer, rt_b->rt_period);
Peter Zijlstra029632f2011-10-25 10:00:11 +020027 if (!overrun)
28 break;
29
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020030 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020031 idle = do_sched_rt_period_timer(rt_b, overrun);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020032 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020033 }
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020034 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020035
36 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
37}
38
39void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
40{
41 rt_b->rt_period = ns_to_ktime(period);
42 rt_b->rt_runtime = runtime;
43
44 raw_spin_lock_init(&rt_b->rt_runtime_lock);
45
46 hrtimer_init(&rt_b->rt_period_timer,
47 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
48 rt_b->rt_period_timer.function = sched_rt_period_timer;
49}
50
51static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
52{
53 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
54 return;
55
Peter Zijlstra029632f2011-10-25 10:00:11 +020056 raw_spin_lock(&rt_b->rt_runtime_lock);
57 start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period);
58 raw_spin_unlock(&rt_b->rt_runtime_lock);
59}
60
Steven Rostedtb6366f02015-03-18 14:49:46 -040061#ifdef CONFIG_SMP
62static void push_irq_work_func(struct irq_work *work);
63#endif
64
Abel Vesa07c54f72015-03-03 13:50:27 +020065void init_rt_rq(struct rt_rq *rt_rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +020066{
67 struct rt_prio_array *array;
68 int i;
69
70 array = &rt_rq->active;
71 for (i = 0; i < MAX_RT_PRIO; i++) {
72 INIT_LIST_HEAD(array->queue + i);
73 __clear_bit(i, array->bitmap);
74 }
75 /* delimiter for bitsearch: */
76 __set_bit(MAX_RT_PRIO, array->bitmap);
77
78#if defined CONFIG_SMP
79 rt_rq->highest_prio.curr = MAX_RT_PRIO;
80 rt_rq->highest_prio.next = MAX_RT_PRIO;
81 rt_rq->rt_nr_migratory = 0;
82 rt_rq->overloaded = 0;
83 plist_head_init(&rt_rq->pushable_tasks);
Steven Rostedtb6366f02015-03-18 14:49:46 -040084
85#ifdef HAVE_RT_PUSH_IPI
86 rt_rq->push_flags = 0;
87 rt_rq->push_cpu = nr_cpu_ids;
88 raw_spin_lock_init(&rt_rq->push_lock);
89 init_irq_work(&rt_rq->push_work, push_irq_work_func);
Peter Zijlstra029632f2011-10-25 10:00:11 +020090#endif
Steven Rostedtb6366f02015-03-18 14:49:46 -040091#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +040092 /* We start is dequeued state, because no RT tasks are queued */
93 rt_rq->rt_queued = 0;
Peter Zijlstra029632f2011-10-25 10:00:11 +020094
95 rt_rq->rt_time = 0;
96 rt_rq->rt_throttled = 0;
97 rt_rq->rt_runtime = 0;
98 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
99}
100
Gregory Haskins398a1532009-01-14 09:10:04 -0500101#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +0200102static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
103{
104 hrtimer_cancel(&rt_b->rt_period_timer);
105}
Gregory Haskins398a1532009-01-14 09:10:04 -0500106
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200107#define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
108
Peter Zijlstra8f488942009-07-24 12:25:30 +0200109static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
110{
111#ifdef CONFIG_SCHED_DEBUG
112 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
113#endif
114 return container_of(rt_se, struct task_struct, rt);
115}
116
Gregory Haskins398a1532009-01-14 09:10:04 -0500117static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
118{
119 return rt_rq->rq;
120}
121
122static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
123{
124 return rt_se->rt_rq;
125}
126
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400127static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
128{
129 struct rt_rq *rt_rq = rt_se->rt_rq;
130
131 return rt_rq->rq;
132}
133
Peter Zijlstra029632f2011-10-25 10:00:11 +0200134void free_rt_sched_group(struct task_group *tg)
135{
136 int i;
137
138 if (tg->rt_se)
139 destroy_rt_bandwidth(&tg->rt_bandwidth);
140
141 for_each_possible_cpu(i) {
142 if (tg->rt_rq)
143 kfree(tg->rt_rq[i]);
144 if (tg->rt_se)
145 kfree(tg->rt_se[i]);
146 }
147
148 kfree(tg->rt_rq);
149 kfree(tg->rt_se);
150}
151
152void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
153 struct sched_rt_entity *rt_se, int cpu,
154 struct sched_rt_entity *parent)
155{
156 struct rq *rq = cpu_rq(cpu);
157
158 rt_rq->highest_prio.curr = MAX_RT_PRIO;
159 rt_rq->rt_nr_boosted = 0;
160 rt_rq->rq = rq;
161 rt_rq->tg = tg;
162
163 tg->rt_rq[cpu] = rt_rq;
164 tg->rt_se[cpu] = rt_se;
165
166 if (!rt_se)
167 return;
168
169 if (!parent)
170 rt_se->rt_rq = &rq->rt;
171 else
172 rt_se->rt_rq = parent->my_q;
173
174 rt_se->my_q = rt_rq;
175 rt_se->parent = parent;
176 INIT_LIST_HEAD(&rt_se->run_list);
177}
178
179int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
180{
181 struct rt_rq *rt_rq;
182 struct sched_rt_entity *rt_se;
183 int i;
184
185 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
186 if (!tg->rt_rq)
187 goto err;
188 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
189 if (!tg->rt_se)
190 goto err;
191
192 init_rt_bandwidth(&tg->rt_bandwidth,
193 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
194
195 for_each_possible_cpu(i) {
196 rt_rq = kzalloc_node(sizeof(struct rt_rq),
197 GFP_KERNEL, cpu_to_node(i));
198 if (!rt_rq)
199 goto err;
200
201 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
202 GFP_KERNEL, cpu_to_node(i));
203 if (!rt_se)
204 goto err_free_rq;
205
Abel Vesa07c54f72015-03-03 13:50:27 +0200206 init_rt_rq(rt_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200207 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
208 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
209 }
210
211 return 1;
212
213err_free_rq:
214 kfree(rt_rq);
215err:
216 return 0;
217}
218
Gregory Haskins398a1532009-01-14 09:10:04 -0500219#else /* CONFIG_RT_GROUP_SCHED */
220
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200221#define rt_entity_is_task(rt_se) (1)
222
Peter Zijlstra8f488942009-07-24 12:25:30 +0200223static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
224{
225 return container_of(rt_se, struct task_struct, rt);
226}
227
Gregory Haskins398a1532009-01-14 09:10:04 -0500228static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
229{
230 return container_of(rt_rq, struct rq, rt);
231}
232
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400233static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
Gregory Haskins398a1532009-01-14 09:10:04 -0500234{
235 struct task_struct *p = rt_task_of(rt_se);
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400236
237 return task_rq(p);
238}
239
240static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
241{
242 struct rq *rq = rq_of_rt_se(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -0500243
244 return &rq->rt;
245}
246
Peter Zijlstra029632f2011-10-25 10:00:11 +0200247void free_rt_sched_group(struct task_group *tg) { }
248
249int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
250{
251 return 1;
252}
Gregory Haskins398a1532009-01-14 09:10:04 -0500253#endif /* CONFIG_RT_GROUP_SCHED */
254
Steven Rostedt4fd29172008-01-25 21:08:06 +0100255#ifdef CONFIG_SMP
Ingo Molnar84de4272008-01-25 21:08:15 +0100256
Peter Zijlstra38033c32014-01-23 20:32:21 +0100257static int pull_rt_task(struct rq *this_rq);
258
Peter Zijlstradc877342014-02-12 15:47:29 +0100259static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
260{
261 /* Try to pull RT tasks here if we lower this rq's prio */
262 return rq->rt.highest_prio.curr > prev->prio;
263}
264
Gregory Haskins637f5082008-01-25 21:08:18 +0100265static inline int rt_overloaded(struct rq *rq)
Steven Rostedt4fd29172008-01-25 21:08:06 +0100266{
Gregory Haskins637f5082008-01-25 21:08:18 +0100267 return atomic_read(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100268}
Ingo Molnar84de4272008-01-25 21:08:15 +0100269
Steven Rostedt4fd29172008-01-25 21:08:06 +0100270static inline void rt_set_overload(struct rq *rq)
271{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400272 if (!rq->online)
273 return;
274
Rusty Russellc6c49272008-11-25 02:35:05 +1030275 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100276 /*
277 * Make sure the mask is visible before we set
278 * the overload count. That is checked to determine
279 * if we should look at the mask. It would be a shame
280 * if we looked at the mask, but the mask was not
281 * updated yet.
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200282 *
283 * Matched by the barrier in pull_rt_task().
Steven Rostedt4fd29172008-01-25 21:08:06 +0100284 */
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200285 smp_wmb();
Gregory Haskins637f5082008-01-25 21:08:18 +0100286 atomic_inc(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100287}
Ingo Molnar84de4272008-01-25 21:08:15 +0100288
Steven Rostedt4fd29172008-01-25 21:08:06 +0100289static inline void rt_clear_overload(struct rq *rq)
290{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400291 if (!rq->online)
292 return;
293
Steven Rostedt4fd29172008-01-25 21:08:06 +0100294 /* the order here really doesn't matter */
Gregory Haskins637f5082008-01-25 21:08:18 +0100295 atomic_dec(&rq->rd->rto_count);
Rusty Russellc6c49272008-11-25 02:35:05 +1030296 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100297}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100298
Gregory Haskins398a1532009-01-14 09:10:04 -0500299static void update_rt_migration(struct rt_rq *rt_rq)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100300{
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200301 if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
Gregory Haskins398a1532009-01-14 09:10:04 -0500302 if (!rt_rq->overloaded) {
303 rt_set_overload(rq_of_rt_rq(rt_rq));
304 rt_rq->overloaded = 1;
Gregory Haskinscdc8eb92008-01-25 21:08:23 +0100305 }
Gregory Haskins398a1532009-01-14 09:10:04 -0500306 } else if (rt_rq->overloaded) {
307 rt_clear_overload(rq_of_rt_rq(rt_rq));
308 rt_rq->overloaded = 0;
Gregory Haskins637f5082008-01-25 21:08:18 +0100309 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100310}
Steven Rostedt4fd29172008-01-25 21:08:06 +0100311
Gregory Haskins398a1532009-01-14 09:10:04 -0500312static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100313{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200314 struct task_struct *p;
315
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200316 if (!rt_entity_is_task(rt_se))
317 return;
318
Peter Zijlstra29baa742012-04-23 12:11:21 +0200319 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200320 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
321
322 rt_rq->rt_nr_total++;
Peter Zijlstra29baa742012-04-23 12:11:21 +0200323 if (p->nr_cpus_allowed > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500324 rt_rq->rt_nr_migratory++;
325
326 update_rt_migration(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100327}
328
Gregory Haskins398a1532009-01-14 09:10:04 -0500329static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
330{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200331 struct task_struct *p;
332
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200333 if (!rt_entity_is_task(rt_se))
334 return;
335
Peter Zijlstra29baa742012-04-23 12:11:21 +0200336 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200337 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
338
339 rt_rq->rt_nr_total--;
Peter Zijlstra29baa742012-04-23 12:11:21 +0200340 if (p->nr_cpus_allowed > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500341 rt_rq->rt_nr_migratory--;
342
343 update_rt_migration(rt_rq);
344}
345
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400346static inline int has_pushable_tasks(struct rq *rq)
347{
348 return !plist_head_empty(&rq->rt.pushable_tasks);
349}
350
Peter Zijlstradc877342014-02-12 15:47:29 +0100351static inline void set_post_schedule(struct rq *rq)
352{
353 /*
354 * We detect this state here so that we can avoid taking the RQ
355 * lock again later if there is no need to push
356 */
357 rq->post_schedule = has_pushable_tasks(rq);
358}
359
Gregory Haskins917b6272008-12-29 09:39:53 -0500360static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
361{
362 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
363 plist_node_init(&p->pushable_tasks, p->prio);
364 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400365
366 /* Update the highest prio pushable task */
367 if (p->prio < rq->rt.highest_prio.next)
368 rq->rt.highest_prio.next = p->prio;
Gregory Haskins917b6272008-12-29 09:39:53 -0500369}
370
371static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
372{
373 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
Gregory Haskins917b6272008-12-29 09:39:53 -0500374
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400375 /* Update the new highest prio pushable task */
376 if (has_pushable_tasks(rq)) {
377 p = plist_first_entry(&rq->rt.pushable_tasks,
378 struct task_struct, pushable_tasks);
379 rq->rt.highest_prio.next = p->prio;
380 } else
381 rq->rt.highest_prio.next = MAX_RT_PRIO;
Ingo Molnarbcf08df2008-04-19 12:11:10 +0200382}
383
Gregory Haskins917b6272008-12-29 09:39:53 -0500384#else
385
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100386static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
387{
388}
389
390static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
391{
392}
393
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500394static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100395void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
396{
397}
398
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500399static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100400void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
401{
402}
Gregory Haskins917b6272008-12-29 09:39:53 -0500403
Peter Zijlstradc877342014-02-12 15:47:29 +0100404static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
405{
406 return false;
407}
408
409static inline int pull_rt_task(struct rq *this_rq)
410{
411 return 0;
412}
413
414static inline void set_post_schedule(struct rq *rq)
415{
416}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200417#endif /* CONFIG_SMP */
418
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400419static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
420static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
421
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100422static inline int on_rt_rq(struct sched_rt_entity *rt_se)
423{
424 return !list_empty(&rt_se->run_list);
425}
426
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100427#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100428
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100429static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100430{
431 if (!rt_rq->tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100432 return RUNTIME_INF;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100433
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200434 return rt_rq->rt_runtime;
435}
436
437static inline u64 sched_rt_period(struct rt_rq *rt_rq)
438{
439 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100440}
441
Cheng Xuec514c42011-05-14 14:20:02 +0800442typedef struct task_group *rt_rq_iter_t;
443
Yong Zhang1c09ab02011-06-28 10:51:31 +0800444static inline struct task_group *next_task_group(struct task_group *tg)
445{
446 do {
447 tg = list_entry_rcu(tg->list.next,
448 typeof(struct task_group), list);
449 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
450
451 if (&tg->list == &task_groups)
452 tg = NULL;
453
454 return tg;
455}
456
457#define for_each_rt_rq(rt_rq, iter, rq) \
458 for (iter = container_of(&task_groups, typeof(*iter), list); \
459 (iter = next_task_group(iter)) && \
460 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
Cheng Xuec514c42011-05-14 14:20:02 +0800461
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100462#define for_each_sched_rt_entity(rt_se) \
463 for (; rt_se; rt_se = rt_se->parent)
464
465static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
466{
467 return rt_se->my_q;
468}
469
Thomas Gleixner37dad3f2010-01-20 20:59:01 +0000470static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100471static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
472
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100473static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100474{
Dario Faggiolif6121f42008-10-03 17:40:46 +0200475 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
Kirill Tkhai88751252014-06-29 00:03:57 +0400476 struct rq *rq = rq_of_rt_rq(rt_rq);
Yong Zhang74b7eb52010-01-29 14:57:52 +0800477 struct sched_rt_entity *rt_se;
478
Kirill Tkhai88751252014-06-29 00:03:57 +0400479 int cpu = cpu_of(rq);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530480
481 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100482
Dario Faggiolif6121f42008-10-03 17:40:46 +0200483 if (rt_rq->rt_nr_running) {
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400484 if (!rt_se)
485 enqueue_top_rt_rq(rt_rq);
486 else if (!on_rt_rq(rt_se))
Thomas Gleixner37dad3f2010-01-20 20:59:01 +0000487 enqueue_rt_entity(rt_se, false);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400488
Gregory Haskinse864c492008-12-29 09:39:49 -0500489 if (rt_rq->highest_prio.curr < curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +0400490 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100491 }
492}
493
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100494static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100495{
Yong Zhang74b7eb52010-01-29 14:57:52 +0800496 struct sched_rt_entity *rt_se;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530497 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
Yong Zhang74b7eb52010-01-29 14:57:52 +0800498
Balbir Singh0c3b9162011-03-03 17:04:35 +0530499 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100500
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400501 if (!rt_se)
502 dequeue_top_rt_rq(rt_rq);
503 else if (on_rt_rq(rt_se))
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100504 dequeue_rt_entity(rt_se);
505}
506
Kirill Tkhai46383642014-03-15 02:15:07 +0400507static inline int rt_rq_throttled(struct rt_rq *rt_rq)
508{
509 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
510}
511
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100512static int rt_se_boosted(struct sched_rt_entity *rt_se)
513{
514 struct rt_rq *rt_rq = group_rt_rq(rt_se);
515 struct task_struct *p;
516
517 if (rt_rq)
518 return !!rt_rq->rt_nr_boosted;
519
520 p = rt_task_of(rt_se);
521 return p->prio != p->normal_prio;
522}
523
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200524#ifdef CONFIG_SMP
Rusty Russellc6c49272008-11-25 02:35:05 +1030525static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200526{
Nathan Zimmer424c93f2013-05-09 11:24:03 -0500527 return this_rq()->rd->span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200528}
529#else
Rusty Russellc6c49272008-11-25 02:35:05 +1030530static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200531{
Rusty Russellc6c49272008-11-25 02:35:05 +1030532 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200533}
534#endif
535
536static inline
537struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
538{
539 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
540}
541
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200542static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
543{
544 return &rt_rq->tg->rt_bandwidth;
545}
546
Dhaval Giani55e12e52008-06-24 23:39:43 +0530547#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100548
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100549static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100550{
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200551 return rt_rq->rt_runtime;
552}
553
554static inline u64 sched_rt_period(struct rt_rq *rt_rq)
555{
556 return ktime_to_ns(def_rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100557}
558
Cheng Xuec514c42011-05-14 14:20:02 +0800559typedef struct rt_rq *rt_rq_iter_t;
560
561#define for_each_rt_rq(rt_rq, iter, rq) \
562 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
563
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100564#define for_each_sched_rt_entity(rt_se) \
565 for (; rt_se; rt_se = NULL)
566
567static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
568{
569 return NULL;
570}
571
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100572static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100573{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400574 struct rq *rq = rq_of_rt_rq(rt_rq);
575
576 if (!rt_rq->rt_nr_running)
577 return;
578
579 enqueue_top_rt_rq(rt_rq);
Kirill Tkhai88751252014-06-29 00:03:57 +0400580 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100581}
582
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100583static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100584{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400585 dequeue_top_rt_rq(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100586}
587
Kirill Tkhai46383642014-03-15 02:15:07 +0400588static inline int rt_rq_throttled(struct rt_rq *rt_rq)
589{
590 return rt_rq->rt_throttled;
591}
592
Rusty Russellc6c49272008-11-25 02:35:05 +1030593static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200594{
Rusty Russellc6c49272008-11-25 02:35:05 +1030595 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200596}
597
598static inline
599struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
600{
601 return &cpu_rq(cpu)->rt;
602}
603
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200604static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
605{
606 return &def_rt_bandwidth;
607}
608
Dhaval Giani55e12e52008-06-24 23:39:43 +0530609#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100610
Juri Lellifaa59932014-02-21 11:37:15 +0100611bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
612{
613 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
614
615 return (hrtimer_active(&rt_b->rt_period_timer) ||
616 rt_rq->rt_time < rt_b->rt_runtime);
617}
618
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200619#ifdef CONFIG_SMP
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200620/*
621 * We ran out of runtime, see if we can borrow some from our neighbours.
622 */
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200623static int do_balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200624{
625 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
Shawn Bohreraa7f6732013-01-14 11:55:31 -0600626 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200627 int i, weight, more = 0;
628 u64 rt_period;
629
Rusty Russellc6c49272008-11-25 02:35:05 +1030630 weight = cpumask_weight(rd->span);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200631
Thomas Gleixner0986b112009-11-17 15:32:06 +0100632 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200633 rt_period = ktime_to_ns(rt_b->rt_period);
Rusty Russellc6c49272008-11-25 02:35:05 +1030634 for_each_cpu(i, rd->span) {
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200635 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
636 s64 diff;
637
638 if (iter == rt_rq)
639 continue;
640
Thomas Gleixner0986b112009-11-17 15:32:06 +0100641 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200642 /*
643 * Either all rqs have inf runtime and there's nothing to steal
644 * or __disable_runtime() below sets a specific rq to inf to
645 * indicate its been disabled and disalow stealing.
646 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200647 if (iter->rt_runtime == RUNTIME_INF)
648 goto next;
649
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200650 /*
651 * From runqueues with spare time, take 1/n part of their
652 * spare time, but no more than our period.
653 */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200654 diff = iter->rt_runtime - iter->rt_time;
655 if (diff > 0) {
Peter Zijlstra58838cf2008-07-24 12:43:13 +0200656 diff = div_u64((u64)diff, weight);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200657 if (rt_rq->rt_runtime + diff > rt_period)
658 diff = rt_period - rt_rq->rt_runtime;
659 iter->rt_runtime -= diff;
660 rt_rq->rt_runtime += diff;
661 more = 1;
662 if (rt_rq->rt_runtime == rt_period) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100663 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200664 break;
665 }
666 }
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200667next:
Thomas Gleixner0986b112009-11-17 15:32:06 +0100668 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200669 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100670 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200671
672 return more;
673}
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200674
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200675/*
676 * Ensure this RQ takes back all the runtime it lend to its neighbours.
677 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200678static void __disable_runtime(struct rq *rq)
679{
680 struct root_domain *rd = rq->rd;
Cheng Xuec514c42011-05-14 14:20:02 +0800681 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200682 struct rt_rq *rt_rq;
683
684 if (unlikely(!scheduler_running))
685 return;
686
Cheng Xuec514c42011-05-14 14:20:02 +0800687 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200688 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
689 s64 want;
690 int i;
691
Thomas Gleixner0986b112009-11-17 15:32:06 +0100692 raw_spin_lock(&rt_b->rt_runtime_lock);
693 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200694 /*
695 * Either we're all inf and nobody needs to borrow, or we're
696 * already disabled and thus have nothing to do, or we have
697 * exactly the right amount of runtime to take out.
698 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200699 if (rt_rq->rt_runtime == RUNTIME_INF ||
700 rt_rq->rt_runtime == rt_b->rt_runtime)
701 goto balanced;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100702 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200703
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200704 /*
705 * Calculate the difference between what we started out with
706 * and what we current have, that's the amount of runtime
707 * we lend and now have to reclaim.
708 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200709 want = rt_b->rt_runtime - rt_rq->rt_runtime;
710
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200711 /*
712 * Greedy reclaim, take back as much as we can.
713 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030714 for_each_cpu(i, rd->span) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200715 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
716 s64 diff;
717
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200718 /*
719 * Can't reclaim from ourselves or disabled runqueues.
720 */
Peter Zijlstraf1679d02008-08-14 15:49:00 +0200721 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200722 continue;
723
Thomas Gleixner0986b112009-11-17 15:32:06 +0100724 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200725 if (want > 0) {
726 diff = min_t(s64, iter->rt_runtime, want);
727 iter->rt_runtime -= diff;
728 want -= diff;
729 } else {
730 iter->rt_runtime -= want;
731 want -= want;
732 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100733 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200734
735 if (!want)
736 break;
737 }
738
Thomas Gleixner0986b112009-11-17 15:32:06 +0100739 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200740 /*
741 * We cannot be left wanting - that would mean some runtime
742 * leaked out of the system.
743 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200744 BUG_ON(want);
745balanced:
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200746 /*
747 * Disable all the borrow logic by pretending we have inf
748 * runtime - in which case borrowing doesn't make sense.
749 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200750 rt_rq->rt_runtime = RUNTIME_INF;
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -0700751 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100752 raw_spin_unlock(&rt_rq->rt_runtime_lock);
753 raw_spin_unlock(&rt_b->rt_runtime_lock);
Kirill Tkhai99b62562014-06-25 12:19:48 +0400754
755 /* Make rt_rq available for pick_next_task() */
756 sched_rt_rq_enqueue(rt_rq);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200757 }
758}
759
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200760static void __enable_runtime(struct rq *rq)
761{
Cheng Xuec514c42011-05-14 14:20:02 +0800762 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200763 struct rt_rq *rt_rq;
764
765 if (unlikely(!scheduler_running))
766 return;
767
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200768 /*
769 * Reset each runqueue's bandwidth settings
770 */
Cheng Xuec514c42011-05-14 14:20:02 +0800771 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200772 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
773
Thomas Gleixner0986b112009-11-17 15:32:06 +0100774 raw_spin_lock(&rt_b->rt_runtime_lock);
775 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200776 rt_rq->rt_runtime = rt_b->rt_runtime;
777 rt_rq->rt_time = 0;
Zhang, Yanminbaf25732008-09-09 11:26:33 +0800778 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100779 raw_spin_unlock(&rt_rq->rt_runtime_lock);
780 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200781 }
782}
783
Peter Zijlstraeff65492008-06-19 14:22:26 +0200784static int balance_runtime(struct rt_rq *rt_rq)
785{
786 int more = 0;
787
Peter Zijlstra4a6184c2011-10-06 22:39:14 +0200788 if (!sched_feat(RT_RUNTIME_SHARE))
789 return more;
790
Peter Zijlstraeff65492008-06-19 14:22:26 +0200791 if (rt_rq->rt_time > rt_rq->rt_runtime) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100792 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200793 more = do_balance_runtime(rt_rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100794 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200795 }
796
797 return more;
798}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530799#else /* !CONFIG_SMP */
Peter Zijlstraeff65492008-06-19 14:22:26 +0200800static inline int balance_runtime(struct rt_rq *rt_rq)
801{
802 return 0;
803}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530804#endif /* CONFIG_SMP */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100805
806static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
807{
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200808 int i, idle = 1, throttled = 0;
Rusty Russellc6c49272008-11-25 02:35:05 +1030809 const struct cpumask *span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200810
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200811 span = sched_rt_period_mask();
Mike Galbraithe221d022012-08-07 10:02:38 +0200812#ifdef CONFIG_RT_GROUP_SCHED
813 /*
814 * FIXME: isolated CPUs should really leave the root task group,
815 * whether they are isolcpus or were isolated via cpusets, lest
816 * the timer run on a CPU which does not service all runqueues,
817 * potentially leaving other CPUs indefinitely throttled. If
818 * isolation is really required, the user will turn the throttle
819 * off to kill the perturbations it causes anyway. Meanwhile,
820 * this maintains functionality for boot and/or troubleshooting.
821 */
822 if (rt_b == &root_task_group.rt_bandwidth)
823 span = cpu_online_mask;
824#endif
Rusty Russellc6c49272008-11-25 02:35:05 +1030825 for_each_cpu(i, span) {
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200826 int enqueue = 0;
827 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
828 struct rq *rq = rq_of_rt_rq(rt_rq);
829
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100830 raw_spin_lock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200831 if (rt_rq->rt_time) {
832 u64 runtime;
833
Thomas Gleixner0986b112009-11-17 15:32:06 +0100834 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200835 if (rt_rq->rt_throttled)
836 balance_runtime(rt_rq);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200837 runtime = rt_rq->rt_runtime;
838 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
839 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
840 rt_rq->rt_throttled = 0;
841 enqueue = 1;
Mike Galbraith61eadef2011-04-29 08:36:50 +0200842
843 /*
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100844 * When we're idle and a woken (rt) task is
845 * throttled check_preempt_curr() will set
846 * skip_update and the time between the wakeup
847 * and this unthrottle will get accounted as
848 * 'runtime'.
Mike Galbraith61eadef2011-04-29 08:36:50 +0200849 */
850 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100851 rq_clock_skip_update(rq, false);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200852 }
853 if (rt_rq->rt_time || rt_rq->rt_nr_running)
854 idle = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100855 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530856 } else if (rt_rq->rt_nr_running) {
Peter Zijlstra8a8cde12008-06-19 14:22:28 +0200857 idle = 0;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530858 if (!rt_rq_throttled(rt_rq))
859 enqueue = 1;
860 }
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200861 if (rt_rq->rt_throttled)
862 throttled = 1;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200863
864 if (enqueue)
865 sched_rt_rq_enqueue(rt_rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100866 raw_spin_unlock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200867 }
868
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200869 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
870 return 1;
871
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200872 return idle;
873}
874
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100875static inline int rt_se_prio(struct sched_rt_entity *rt_se)
876{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100877#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100878 struct rt_rq *rt_rq = group_rt_rq(rt_se);
879
880 if (rt_rq)
Gregory Haskinse864c492008-12-29 09:39:49 -0500881 return rt_rq->highest_prio.curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100882#endif
883
884 return rt_task_of(rt_se)->prio;
885}
886
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100887static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100888{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100889 u64 runtime = sched_rt_runtime(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100890
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100891 if (rt_rq->rt_throttled)
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100892 return rt_rq_throttled(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100893
Shan Hai5b680fd2011-11-29 11:03:56 +0800894 if (runtime >= sched_rt_period(rt_rq))
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200895 return 0;
896
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200897 balance_runtime(rt_rq);
898 runtime = sched_rt_runtime(rt_rq);
899 if (runtime == RUNTIME_INF)
900 return 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200901
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100902 if (rt_rq->rt_time > runtime) {
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200903 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
904
905 /*
906 * Don't actually throttle groups that have no runtime assigned
907 * but accrue some time due to boosting.
908 */
909 if (likely(rt_b->rt_runtime)) {
910 rt_rq->rt_throttled = 1;
John Stultzc2248152014-06-04 16:11:41 -0700911 printk_deferred_once("sched: RT throttling activated\n");
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200912 } else {
913 /*
914 * In case we did anyway, make it go away,
915 * replenishment is a joke, since it will replenish us
916 * with exactly 0 ns.
917 */
918 rt_rq->rt_time = 0;
919 }
920
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100921 if (rt_rq_throttled(rt_rq)) {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100922 sched_rt_rq_dequeue(rt_rq);
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100923 return 1;
924 }
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100925 }
926
927 return 0;
928}
929
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200930/*
931 * Update the current task's runtime statistics. Skip current tasks that
932 * are not in our scheduling class.
933 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200934static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200935{
936 struct task_struct *curr = rq->curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100937 struct sched_rt_entity *rt_se = &curr->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200938 u64 delta_exec;
939
Peter Zijlstra06c3bc62011-02-02 13:19:48 +0100940 if (curr->sched_class != &rt_sched_class)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200941 return;
942
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200943 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Kirill Tkhaifc79e242013-01-30 16:50:36 +0400944 if (unlikely((s64)delta_exec <= 0))
945 return;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200946
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200947 schedstat_set(curr->se.statistics.exec_max,
948 max(curr->se.statistics.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200949
950 curr->se.sum_exec_runtime += delta_exec;
Frank Mayharf06febc2008-09-12 09:54:39 -0700951 account_group_exec_runtime(curr, delta_exec);
952
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200953 curr->se.exec_start = rq_clock_task(rq);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100954 cpuacct_charge(curr, delta_exec);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100955
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200956 sched_rt_avg_update(rq, delta_exec);
957
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200958 if (!rt_bandwidth_enabled())
959 return;
960
Dhaval Giani354d60c2008-04-19 19:44:59 +0200961 for_each_sched_rt_entity(rt_se) {
Giedrius Rekasius0b079392014-05-25 15:23:31 +0100962 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
Dhaval Giani354d60c2008-04-19 19:44:59 +0200963
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200964 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100965 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200966 rt_rq->rt_time += delta_exec;
967 if (sched_rt_runtime_exceeded(rt_rq))
Kirill Tkhai88751252014-06-29 00:03:57 +0400968 resched_curr(rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100969 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200970 }
Dhaval Giani354d60c2008-04-19 19:44:59 +0200971 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200972}
973
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400974static void
975dequeue_top_rt_rq(struct rt_rq *rt_rq)
976{
977 struct rq *rq = rq_of_rt_rq(rt_rq);
978
979 BUG_ON(&rq->rt != rt_rq);
980
981 if (!rt_rq->rt_queued)
982 return;
983
984 BUG_ON(!rq->nr_running);
985
Kirill Tkhai72465442014-05-09 03:00:14 +0400986 sub_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400987 rt_rq->rt_queued = 0;
988}
989
990static void
991enqueue_top_rt_rq(struct rt_rq *rt_rq)
992{
993 struct rq *rq = rq_of_rt_rq(rt_rq);
994
995 BUG_ON(&rq->rt != rt_rq);
996
997 if (rt_rq->rt_queued)
998 return;
999 if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
1000 return;
1001
Kirill Tkhai72465442014-05-09 03:00:14 +04001002 add_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001003 rt_rq->rt_queued = 1;
1004}
1005
Gregory Haskins398a1532009-01-14 09:10:04 -05001006#if defined CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05001007
Gregory Haskins398a1532009-01-14 09:10:04 -05001008static void
1009inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001010{
Gregory Haskins4d984272008-12-29 09:39:49 -05001011 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins4d984272008-12-29 09:39:49 -05001012
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001013#ifdef CONFIG_RT_GROUP_SCHED
1014 /*
1015 * Change rq's cpupri only if rt_rq is the top queue.
1016 */
1017 if (&rq->rt != rt_rq)
1018 return;
1019#endif
Steven Rostedt5181f4a42011-06-16 21:55:23 -04001020 if (rq->online && prio < prev_prio)
1021 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
Steven Rostedt63489e42008-01-25 21:08:03 +01001022}
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001023
Gregory Haskins398a1532009-01-14 09:10:04 -05001024static void
1025dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001026{
Gregory Haskins4d984272008-12-29 09:39:49 -05001027 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001028
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001029#ifdef CONFIG_RT_GROUP_SCHED
1030 /*
1031 * Change rq's cpupri only if rt_rq is the top queue.
1032 */
1033 if (&rq->rt != rt_rq)
1034 return;
1035#endif
Gregory Haskins398a1532009-01-14 09:10:04 -05001036 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1037 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1038}
1039
1040#else /* CONFIG_SMP */
1041
1042static inline
1043void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1044static inline
1045void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1046
1047#endif /* CONFIG_SMP */
1048
Steven Rostedt63489e42008-01-25 21:08:03 +01001049#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001050static void
1051inc_rt_prio(struct rt_rq *rt_rq, int prio)
1052{
1053 int prev_prio = rt_rq->highest_prio.curr;
Steven Rostedt63489e42008-01-25 21:08:03 +01001054
Gregory Haskins398a1532009-01-14 09:10:04 -05001055 if (prio < prev_prio)
1056 rt_rq->highest_prio.curr = prio;
1057
1058 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1059}
1060
1061static void
1062dec_rt_prio(struct rt_rq *rt_rq, int prio)
1063{
1064 int prev_prio = rt_rq->highest_prio.curr;
1065
1066 if (rt_rq->rt_nr_running) {
1067
1068 WARN_ON(prio < prev_prio);
Gregory Haskinse864c492008-12-29 09:39:49 -05001069
1070 /*
Gregory Haskins398a1532009-01-14 09:10:04 -05001071 * This may have been our highest task, and therefore
1072 * we may have some recomputation to do
Gregory Haskinse864c492008-12-29 09:39:49 -05001073 */
Gregory Haskins398a1532009-01-14 09:10:04 -05001074 if (prio == prev_prio) {
Gregory Haskinse864c492008-12-29 09:39:49 -05001075 struct rt_prio_array *array = &rt_rq->active;
1076
1077 rt_rq->highest_prio.curr =
Steven Rostedt764a9d62008-01-25 21:08:04 +01001078 sched_find_first_bit(array->bitmap);
Gregory Haskinse864c492008-12-29 09:39:49 -05001079 }
1080
Steven Rostedt764a9d62008-01-25 21:08:04 +01001081 } else
Gregory Haskinse864c492008-12-29 09:39:49 -05001082 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001083
Gregory Haskins398a1532009-01-14 09:10:04 -05001084 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1085}
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001086
Gregory Haskins398a1532009-01-14 09:10:04 -05001087#else
1088
1089static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1090static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1091
1092#endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1093
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001094#ifdef CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001095
1096static void
1097inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1098{
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001099 if (rt_se_boosted(rt_se))
Steven Rostedt764a9d62008-01-25 21:08:04 +01001100 rt_rq->rt_nr_boosted++;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01001101
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001102 if (rt_rq->tg)
1103 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001104}
1105
1106static void
1107dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1108{
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001109 if (rt_se_boosted(rt_se))
1110 rt_rq->rt_nr_boosted--;
1111
1112 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
Gregory Haskins398a1532009-01-14 09:10:04 -05001113}
1114
1115#else /* CONFIG_RT_GROUP_SCHED */
1116
1117static void
1118inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1119{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001120 start_rt_bandwidth(&def_rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001121}
1122
1123static inline
1124void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1125
1126#endif /* CONFIG_RT_GROUP_SCHED */
1127
1128static inline
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001129unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1130{
1131 struct rt_rq *group_rq = group_rt_rq(rt_se);
1132
1133 if (group_rq)
1134 return group_rq->rt_nr_running;
1135 else
1136 return 1;
1137}
1138
1139static inline
Gregory Haskins398a1532009-01-14 09:10:04 -05001140void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1141{
1142 int prio = rt_se_prio(rt_se);
1143
1144 WARN_ON(!rt_prio(prio));
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001145 rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -05001146
1147 inc_rt_prio(rt_rq, prio);
1148 inc_rt_migration(rt_se, rt_rq);
1149 inc_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001150}
1151
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001152static inline
1153void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1154{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001155 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001156 WARN_ON(!rt_rq->rt_nr_running);
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001157 rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001158
Gregory Haskins398a1532009-01-14 09:10:04 -05001159 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1160 dec_rt_migration(rt_se, rt_rq);
1161 dec_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001162}
1163
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001164static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001165{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001166 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1167 struct rt_prio_array *array = &rt_rq->active;
1168 struct rt_rq *group_rq = group_rt_rq(rt_se);
Dmitry Adamushko20b63312008-06-11 00:58:30 +02001169 struct list_head *queue = array->queue + rt_se_prio(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001170
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001171 /*
1172 * Don't enqueue the group if its throttled, or when empty.
1173 * The latter is a consequence of the former when a child group
1174 * get throttled and the current group doesn't have any other
1175 * active members.
1176 */
1177 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001178 return;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001179
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001180 if (head)
1181 list_add(&rt_se->run_list, queue);
1182 else
1183 list_add_tail(&rt_se->run_list, queue);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001184 __set_bit(rt_se_prio(rt_se), array->bitmap);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001185
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001186 inc_rt_tasks(rt_se, rt_rq);
1187}
1188
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001189static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001190{
1191 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1192 struct rt_prio_array *array = &rt_rq->active;
1193
1194 list_del_init(&rt_se->run_list);
1195 if (list_empty(array->queue + rt_se_prio(rt_se)))
1196 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1197
1198 dec_rt_tasks(rt_se, rt_rq);
1199}
1200
1201/*
1202 * Because the prio of an upper entry depends on the lower
1203 * entries, we must remove entries top - down.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001204 */
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001205static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001206{
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001207 struct sched_rt_entity *back = NULL;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001208
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001209 for_each_sched_rt_entity(rt_se) {
1210 rt_se->back = back;
1211 back = rt_se;
1212 }
1213
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001214 dequeue_top_rt_rq(rt_rq_of_se(back));
1215
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001216 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1217 if (on_rt_rq(rt_se))
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001218 __dequeue_rt_entity(rt_se);
1219 }
1220}
1221
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001222static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001223{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001224 struct rq *rq = rq_of_rt_se(rt_se);
1225
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001226 dequeue_rt_stack(rt_se);
1227 for_each_sched_rt_entity(rt_se)
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001228 __enqueue_rt_entity(rt_se, head);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001229 enqueue_top_rt_rq(&rq->rt);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001230}
1231
1232static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
1233{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001234 struct rq *rq = rq_of_rt_se(rt_se);
1235
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001236 dequeue_rt_stack(rt_se);
1237
1238 for_each_sched_rt_entity(rt_se) {
1239 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1240
1241 if (rt_rq && rt_rq->rt_nr_running)
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001242 __enqueue_rt_entity(rt_se, false);
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001243 }
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001244 enqueue_top_rt_rq(&rq->rt);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001245}
1246
1247/*
1248 * Adding/removing a task to/from a priority array:
1249 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001250static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001251enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001252{
1253 struct sched_rt_entity *rt_se = &p->rt;
1254
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001255 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001256 rt_se->timeout = 0;
1257
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001258 enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001259
Peter Zijlstra29baa742012-04-23 12:11:21 +02001260 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001261 enqueue_pushable_task(rq, p);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001262}
1263
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001264static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001265{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001266 struct sched_rt_entity *rt_se = &p->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001267
1268 update_curr_rt(rq);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001269 dequeue_rt_entity(rt_se);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001270
Gregory Haskins917b6272008-12-29 09:39:53 -05001271 dequeue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001272}
1273
1274/*
Richard Weinberger60686312011-11-12 18:07:57 +01001275 * Put task to the head or the end of the run list without the overhead of
1276 * dequeue followed by enqueue.
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001277 */
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001278static void
1279requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001280{
Ingo Molnar1cdad712008-06-19 09:09:15 +02001281 if (on_rt_rq(rt_se)) {
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001282 struct rt_prio_array *array = &rt_rq->active;
1283 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1284
1285 if (head)
1286 list_move(&rt_se->run_list, queue);
1287 else
1288 list_move_tail(&rt_se->run_list, queue);
Ingo Molnar1cdad712008-06-19 09:09:15 +02001289 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001290}
1291
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001292static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001293{
1294 struct sched_rt_entity *rt_se = &p->rt;
1295 struct rt_rq *rt_rq;
1296
1297 for_each_sched_rt_entity(rt_se) {
1298 rt_rq = rt_rq_of_se(rt_se);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001299 requeue_rt_entity(rt_rq, rt_se, head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001300 }
1301}
1302
1303static void yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001304{
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001305 requeue_task_rt(rq, rq->curr, 0);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001306}
1307
Gregory Haskinse7693a32008-01-25 21:08:09 +01001308#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +01001309static int find_lowest_rq(struct task_struct *task);
1310
Peter Zijlstra0017d732010-03-24 18:34:10 +01001311static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01001312select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
Gregory Haskinse7693a32008-01-25 21:08:09 +01001313{
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001314 struct task_struct *curr;
1315 struct rq *rq;
Steven Rostedtc37495f2011-06-16 21:55:22 -04001316
1317 /* For anything but wake ups, just return the task_cpu */
1318 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1319 goto out;
1320
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001321 rq = cpu_rq(cpu);
1322
1323 rcu_read_lock();
1324 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
1325
Gregory Haskins318e0892008-01-25 21:08:10 +01001326 /*
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001327 * If the current task on @p's runqueue is an RT task, then
Steven Rostedte1f47d82008-01-25 21:08:12 +01001328 * try to see if we can wake this RT task up on another
1329 * runqueue. Otherwise simply start this RT task
1330 * on its current runqueue.
1331 *
Steven Rostedt43fa5462010-09-20 22:40:03 -04001332 * We want to avoid overloading runqueues. If the woken
1333 * task is a higher priority, then it will stay on this CPU
1334 * and the lower prio task should be moved to another CPU.
1335 * Even though this will probably make the lower prio task
1336 * lose its cache, we do not want to bounce a higher task
1337 * around just because it gave up its CPU, perhaps for a
1338 * lock?
1339 *
1340 * For equal prio tasks, we just let the scheduler sort it out.
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001341 *
Gregory Haskins318e0892008-01-25 21:08:10 +01001342 * Otherwise, just let it ride on the affined RQ and the
1343 * post-schedule router will push the preempted task away
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001344 *
1345 * This test is optimistic, if we get it wrong the load-balancer
1346 * will have to sort it out.
Gregory Haskins318e0892008-01-25 21:08:10 +01001347 */
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001348 if (curr && unlikely(rt_task(curr)) &&
Peter Zijlstra29baa742012-04-23 12:11:21 +02001349 (curr->nr_cpus_allowed < 2 ||
Shawn Bohrer6bfa6872013-10-04 14:24:53 -05001350 curr->prio <= p->prio)) {
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001351 int target = find_lowest_rq(p);
1352
Tim Chen80e3d872014-12-12 15:38:12 -08001353 /*
1354 * Don't bother moving it if the destination CPU is
1355 * not running a lower priority task.
1356 */
1357 if (target != -1 &&
1358 p->prio < cpu_rq(target)->rt.highest_prio.curr)
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001359 cpu = target;
1360 }
1361 rcu_read_unlock();
1362
Steven Rostedtc37495f2011-06-16 21:55:22 -04001363out:
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001364 return cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001365}
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001366
1367static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1368{
Wanpeng Li308a6232014-10-31 06:39:31 +08001369 /*
1370 * Current can't be migrated, useless to reschedule,
1371 * let's hope p can move out.
1372 */
1373 if (rq->curr->nr_cpus_allowed == 1 ||
1374 !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001375 return;
1376
Wanpeng Li308a6232014-10-31 06:39:31 +08001377 /*
1378 * p is migratable, so let's not schedule it and
1379 * see if it is pushed or pulled somewhere else.
1380 */
Peter Zijlstra29baa742012-04-23 12:11:21 +02001381 if (p->nr_cpus_allowed != 1
Rusty Russell13b8bd02009-03-25 15:01:22 +10301382 && cpupri_find(&rq->rd->cpupri, p, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001383 return;
1384
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001385 /*
1386 * There appears to be other cpus that can accept
1387 * current and none to run 'p', so lets reschedule
1388 * to try and push current away:
1389 */
1390 requeue_task_rt(rq, p, 1);
Kirill Tkhai88751252014-06-29 00:03:57 +04001391 resched_curr(rq);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001392}
1393
Gregory Haskinse7693a32008-01-25 21:08:09 +01001394#endif /* CONFIG_SMP */
1395
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001396/*
1397 * Preempt the current task with a newly woken task if needed:
1398 */
Peter Zijlstra7d478722009-09-14 19:55:44 +02001399static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001400{
Gregory Haskins45c01e82008-05-12 21:20:41 +02001401 if (p->prio < rq->curr->prio) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001402 resched_curr(rq);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001403 return;
1404 }
1405
1406#ifdef CONFIG_SMP
1407 /*
1408 * If:
1409 *
1410 * - the newly woken task is of equal priority to the current task
1411 * - the newly woken task is non-migratable while current is migratable
1412 * - current will be preempted on the next reschedule
1413 *
1414 * we should check to see if current can readily move to a different
1415 * cpu. If so, we will reschedule to allow the push logic to try
1416 * to move current somewhere else, making room for our non-migratable
1417 * task.
1418 */
Hillf Danton8dd0de82011-06-14 18:36:24 -04001419 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001420 check_preempt_equal_prio(rq, p);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001421#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001422}
1423
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001424static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1425 struct rt_rq *rt_rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001426{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001427 struct rt_prio_array *array = &rt_rq->active;
1428 struct sched_rt_entity *next = NULL;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001429 struct list_head *queue;
1430 int idx;
1431
1432 idx = sched_find_first_bit(array->bitmap);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001433 BUG_ON(idx >= MAX_RT_PRIO);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001434
1435 queue = array->queue + idx;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001436 next = list_entry(queue->next, struct sched_rt_entity, run_list);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001437
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001438 return next;
1439}
1440
Gregory Haskins917b6272008-12-29 09:39:53 -05001441static struct task_struct *_pick_next_task_rt(struct rq *rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001442{
1443 struct sched_rt_entity *rt_se;
1444 struct task_struct *p;
Peter Zijlstra606dba22012-02-11 06:05:00 +01001445 struct rt_rq *rt_rq = &rq->rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001446
1447 do {
1448 rt_se = pick_next_rt_entity(rq, rt_rq);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001449 BUG_ON(!rt_se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001450 rt_rq = group_rt_rq(rt_se);
1451 } while (rt_rq);
1452
1453 p = rt_task_of(rt_se);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001454 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001455
1456 return p;
1457}
1458
Peter Zijlstra606dba22012-02-11 06:05:00 +01001459static struct task_struct *
1460pick_next_task_rt(struct rq *rq, struct task_struct *prev)
Gregory Haskins917b6272008-12-29 09:39:53 -05001461{
Peter Zijlstra606dba22012-02-11 06:05:00 +01001462 struct task_struct *p;
1463 struct rt_rq *rt_rq = &rq->rt;
1464
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001465 if (need_pull_rt_task(rq, prev)) {
Peter Zijlstra38033c32014-01-23 20:32:21 +01001466 pull_rt_task(rq);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001467 /*
1468 * pull_rt_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001469 * means a dl or stop task can slip in, in which case we need
1470 * to re-start task selection.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001471 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001472 if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001473 rq->dl.dl_nr_running))
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001474 return RETRY_TASK;
1475 }
Peter Zijlstra38033c32014-01-23 20:32:21 +01001476
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001477 /*
1478 * We may dequeue prev's rt_rq in put_prev_task().
1479 * So, we update time before rt_nr_running check.
1480 */
1481 if (prev->sched_class == &rt_sched_class)
1482 update_curr_rt(rq);
1483
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001484 if (!rt_rq->rt_queued)
Peter Zijlstra606dba22012-02-11 06:05:00 +01001485 return NULL;
1486
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001487 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001488
1489 p = _pick_next_task_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001490
1491 /* The running task is never eligible for pushing */
Kirill Tkhaif3f17682014-09-12 17:42:01 +04001492 dequeue_pushable_task(rq, p);
Gregory Haskins917b6272008-12-29 09:39:53 -05001493
Peter Zijlstradc877342014-02-12 15:47:29 +01001494 set_post_schedule(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04001495
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001496 return p;
1497}
1498
Ingo Molnar31ee5292007-08-09 11:16:49 +02001499static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001500{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +02001501 update_curr_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001502
1503 /*
1504 * The previous task needs to be made eligible for pushing
1505 * if it is still active
1506 */
Peter Zijlstra29baa742012-04-23 12:11:21 +02001507 if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001508 enqueue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001509}
1510
Peter Williams681f3e62007-10-24 18:23:51 +02001511#ifdef CONFIG_SMP
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001512
Steven Rostedte8fa1362008-01-25 21:08:05 +01001513/* Only try algorithms three times */
1514#define RT_MAX_TRIES 3
1515
Steven Rostedtf65eda42008-01-25 21:08:07 +01001516static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1517{
1518 if (!task_running(rq, p) &&
Kirill Tkhai60334ca2013-01-31 18:56:17 +04001519 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001520 return 1;
1521 return 0;
1522}
1523
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001524/*
1525 * Return the highest pushable rq's task, which is suitable to be executed
1526 * on the cpu, NULL otherwise
1527 */
1528static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001529{
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001530 struct plist_head *head = &rq->rt.pushable_tasks;
1531 struct task_struct *p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001532
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001533 if (!has_pushable_tasks(rq))
1534 return NULL;
Peter Zijlstra3d074672010-03-10 17:07:24 +01001535
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001536 plist_for_each_entry(p, head, pushable_tasks) {
1537 if (pick_rt_task(rq, p, cpu))
1538 return p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001539 }
1540
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001541 return NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001542}
1543
Rusty Russell0e3900e2008-11-25 02:35:13 +10301544static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001545
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001546static int find_lowest_rq(struct task_struct *task)
1547{
1548 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001549 struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001550 int this_cpu = smp_processor_id();
1551 int cpu = task_cpu(task);
1552
Steven Rostedt0da938c2011-06-14 18:36:25 -04001553 /* Make sure the mask is initialized first */
1554 if (unlikely(!lowest_mask))
1555 return -1;
1556
Peter Zijlstra29baa742012-04-23 12:11:21 +02001557 if (task->nr_cpus_allowed == 1)
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001558 return -1; /* No other targets possible */
1559
1560 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
Gregory Haskins06f90db2008-01-25 21:08:13 +01001561 return -1; /* No targets found */
1562
1563 /*
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001564 * At this point we have built a mask of cpus representing the
1565 * lowest priority tasks in the system. Now we want to elect
1566 * the best one based on our affinity and topology.
1567 *
1568 * We prioritize the last cpu that the task executed on since
1569 * it is most likely cache-hot in that location.
1570 */
Rusty Russell96f874e2008-11-25 02:35:14 +10301571 if (cpumask_test_cpu(cpu, lowest_mask))
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001572 return cpu;
1573
1574 /*
1575 * Otherwise, we consult the sched_domains span maps to figure
1576 * out which cpu is logically closest to our hot cache data.
1577 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301578 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1579 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001580
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001581 rcu_read_lock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301582 for_each_domain(cpu, sd) {
1583 if (sd->flags & SD_WAKE_AFFINE) {
1584 int best_cpu;
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001585
Rusty Russelle2c88062009-11-03 14:53:15 +10301586 /*
1587 * "this_cpu" is cheaper to preempt than a
1588 * remote processor.
1589 */
1590 if (this_cpu != -1 &&
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001591 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1592 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301593 return this_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001594 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001595
Rusty Russelle2c88062009-11-03 14:53:15 +10301596 best_cpu = cpumask_first_and(lowest_mask,
1597 sched_domain_span(sd));
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001598 if (best_cpu < nr_cpu_ids) {
1599 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301600 return best_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001601 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001602 }
1603 }
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001604 rcu_read_unlock();
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001605
1606 /*
1607 * And finally, if there were no matches within the domains
1608 * just give the caller *something* to work with from the compatible
1609 * locations.
1610 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301611 if (this_cpu != -1)
1612 return this_cpu;
1613
1614 cpu = cpumask_any(lowest_mask);
1615 if (cpu < nr_cpu_ids)
1616 return cpu;
1617 return -1;
Gregory Haskins07b40322008-01-25 21:08:10 +01001618}
1619
Steven Rostedte8fa1362008-01-25 21:08:05 +01001620/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +01001621static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001622{
1623 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001624 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +01001625 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001626
1627 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +01001628 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001629
Gregory Haskins2de0b462008-01-25 21:08:10 +01001630 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +01001631 break;
1632
Gregory Haskins07b40322008-01-25 21:08:10 +01001633 lowest_rq = cpu_rq(cpu);
1634
Tim Chen80e3d872014-12-12 15:38:12 -08001635 if (lowest_rq->rt.highest_prio.curr <= task->prio) {
1636 /*
1637 * Target rq has tasks of equal or higher priority,
1638 * retrying does not release any lock and is unlikely
1639 * to yield a different result.
1640 */
1641 lowest_rq = NULL;
1642 break;
1643 }
1644
Steven Rostedte8fa1362008-01-25 21:08:05 +01001645 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +01001646 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +01001647 /*
1648 * We had to unlock the run queue. In
1649 * the mean time, task could have
1650 * migrated already or had its affinity changed.
1651 * Also make sure that it wasn't scheduled on its rq.
1652 */
Gregory Haskins07b40322008-01-25 21:08:10 +01001653 if (unlikely(task_rq(task) != rq ||
Rusty Russell96f874e2008-11-25 02:35:14 +10301654 !cpumask_test_cpu(lowest_rq->cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001655 tsk_cpus_allowed(task)) ||
Gregory Haskins07b40322008-01-25 21:08:10 +01001656 task_running(rq, task) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001657 !task_on_rq_queued(task))) {
Ingo Molnar4df64c02008-01-25 21:08:15 +01001658
Peter Zijlstra7f1b4392012-05-17 21:19:46 +02001659 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001660 lowest_rq = NULL;
1661 break;
1662 }
1663 }
1664
1665 /* If this rq is still suitable use it. */
Gregory Haskinse864c492008-12-29 09:39:49 -05001666 if (lowest_rq->rt.highest_prio.curr > task->prio)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001667 break;
1668
1669 /* try again */
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001670 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001671 lowest_rq = NULL;
1672 }
1673
1674 return lowest_rq;
1675}
1676
Gregory Haskins917b6272008-12-29 09:39:53 -05001677static struct task_struct *pick_next_pushable_task(struct rq *rq)
1678{
1679 struct task_struct *p;
1680
1681 if (!has_pushable_tasks(rq))
1682 return NULL;
1683
1684 p = plist_first_entry(&rq->rt.pushable_tasks,
1685 struct task_struct, pushable_tasks);
1686
1687 BUG_ON(rq->cpu != task_cpu(p));
1688 BUG_ON(task_current(rq, p));
Peter Zijlstra29baa742012-04-23 12:11:21 +02001689 BUG_ON(p->nr_cpus_allowed <= 1);
Gregory Haskins917b6272008-12-29 09:39:53 -05001690
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001691 BUG_ON(!task_on_rq_queued(p));
Gregory Haskins917b6272008-12-29 09:39:53 -05001692 BUG_ON(!rt_task(p));
1693
1694 return p;
1695}
1696
Steven Rostedte8fa1362008-01-25 21:08:05 +01001697/*
1698 * If the current CPU has more than one RT task, see if the non
1699 * running task can migrate over to a CPU that is running a task
1700 * of lesser priority.
1701 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001702static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001703{
1704 struct task_struct *next_task;
1705 struct rq *lowest_rq;
Hillf Danton311e8002011-06-16 21:55:20 -04001706 int ret = 0;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001707
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +01001708 if (!rq->rt.overloaded)
1709 return 0;
1710
Gregory Haskins917b6272008-12-29 09:39:53 -05001711 next_task = pick_next_pushable_task(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001712 if (!next_task)
1713 return 0;
1714
Peter Zijlstra49246272010-10-17 21:46:10 +02001715retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +01001716 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001717 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001718 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001719 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01001720
1721 /*
1722 * It's possible that the next_task slipped in of
1723 * higher priority than current. If that's the case
1724 * just reschedule current.
1725 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001726 if (unlikely(next_task->prio < rq->curr->prio)) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001727 resched_curr(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001728 return 0;
1729 }
1730
Gregory Haskins697f0a42008-01-25 21:08:09 +01001731 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +01001732 get_task_struct(next_task);
1733
1734 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001735 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001736 if (!lowest_rq) {
1737 struct task_struct *task;
1738 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001739 * find_lock_lowest_rq releases rq->lock
Gregory Haskins15635132008-12-29 09:39:53 -05001740 * so it is possible that next_task has migrated.
1741 *
1742 * We need to make sure that the task is still on the same
1743 * run-queue and is also still the next task eligible for
1744 * pushing.
Steven Rostedte8fa1362008-01-25 21:08:05 +01001745 */
Gregory Haskins917b6272008-12-29 09:39:53 -05001746 task = pick_next_pushable_task(rq);
Gregory Haskins15635132008-12-29 09:39:53 -05001747 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1748 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001749 * The task hasn't migrated, and is still the next
1750 * eligible task, but we failed to find a run-queue
1751 * to push it to. Do not retry in this case, since
1752 * other cpus will pull from us when ready.
Gregory Haskins15635132008-12-29 09:39:53 -05001753 */
Gregory Haskins15635132008-12-29 09:39:53 -05001754 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001755 }
Gregory Haskins917b6272008-12-29 09:39:53 -05001756
Gregory Haskins15635132008-12-29 09:39:53 -05001757 if (!task)
1758 /* No more tasks, just exit */
1759 goto out;
1760
Gregory Haskins917b6272008-12-29 09:39:53 -05001761 /*
Gregory Haskins15635132008-12-29 09:39:53 -05001762 * Something has shifted, try again.
Gregory Haskins917b6272008-12-29 09:39:53 -05001763 */
Gregory Haskins15635132008-12-29 09:39:53 -05001764 put_task_struct(next_task);
1765 next_task = task;
1766 goto retry;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001767 }
1768
Gregory Haskins697f0a42008-01-25 21:08:09 +01001769 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001770 set_task_cpu(next_task, lowest_rq->cpu);
1771 activate_task(lowest_rq, next_task, 0);
Hillf Danton311e8002011-06-16 21:55:20 -04001772 ret = 1;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001773
Kirill Tkhai88751252014-06-29 00:03:57 +04001774 resched_curr(lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001775
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001776 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001777
Steven Rostedte8fa1362008-01-25 21:08:05 +01001778out:
1779 put_task_struct(next_task);
1780
Hillf Danton311e8002011-06-16 21:55:20 -04001781 return ret;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001782}
1783
Steven Rostedte8fa1362008-01-25 21:08:05 +01001784static void push_rt_tasks(struct rq *rq)
1785{
1786 /* push_rt_task will return true if it moved an RT */
1787 while (push_rt_task(rq))
1788 ;
1789}
1790
Steven Rostedtb6366f02015-03-18 14:49:46 -04001791#ifdef HAVE_RT_PUSH_IPI
1792/*
1793 * The search for the next cpu always starts at rq->cpu and ends
1794 * when we reach rq->cpu again. It will never return rq->cpu.
1795 * This returns the next cpu to check, or nr_cpu_ids if the loop
1796 * is complete.
1797 *
1798 * rq->rt.push_cpu holds the last cpu returned by this function,
1799 * or if this is the first instance, it must hold rq->cpu.
1800 */
1801static int rto_next_cpu(struct rq *rq)
1802{
1803 int prev_cpu = rq->rt.push_cpu;
1804 int cpu;
1805
1806 cpu = cpumask_next(prev_cpu, rq->rd->rto_mask);
1807
1808 /*
1809 * If the previous cpu is less than the rq's CPU, then it already
1810 * passed the end of the mask, and has started from the beginning.
1811 * We end if the next CPU is greater or equal to rq's CPU.
1812 */
1813 if (prev_cpu < rq->cpu) {
1814 if (cpu >= rq->cpu)
1815 return nr_cpu_ids;
1816
1817 } else if (cpu >= nr_cpu_ids) {
1818 /*
1819 * We passed the end of the mask, start at the beginning.
1820 * If the result is greater or equal to the rq's CPU, then
1821 * the loop is finished.
1822 */
1823 cpu = cpumask_first(rq->rd->rto_mask);
1824 if (cpu >= rq->cpu)
1825 return nr_cpu_ids;
1826 }
1827 rq->rt.push_cpu = cpu;
1828
1829 /* Return cpu to let the caller know if the loop is finished or not */
1830 return cpu;
1831}
1832
1833static int find_next_push_cpu(struct rq *rq)
1834{
1835 struct rq *next_rq;
1836 int cpu;
1837
1838 while (1) {
1839 cpu = rto_next_cpu(rq);
1840 if (cpu >= nr_cpu_ids)
1841 break;
1842 next_rq = cpu_rq(cpu);
1843
1844 /* Make sure the next rq can push to this rq */
1845 if (next_rq->rt.highest_prio.next < rq->rt.highest_prio.curr)
1846 break;
1847 }
1848
1849 return cpu;
1850}
1851
1852#define RT_PUSH_IPI_EXECUTING 1
1853#define RT_PUSH_IPI_RESTART 2
1854
1855static void tell_cpu_to_push(struct rq *rq)
1856{
1857 int cpu;
1858
1859 if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) {
1860 raw_spin_lock(&rq->rt.push_lock);
1861 /* Make sure it's still executing */
1862 if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) {
1863 /*
1864 * Tell the IPI to restart the loop as things have
1865 * changed since it started.
1866 */
1867 rq->rt.push_flags |= RT_PUSH_IPI_RESTART;
1868 raw_spin_unlock(&rq->rt.push_lock);
1869 return;
1870 }
1871 raw_spin_unlock(&rq->rt.push_lock);
1872 }
1873
1874 /* When here, there's no IPI going around */
1875
1876 rq->rt.push_cpu = rq->cpu;
1877 cpu = find_next_push_cpu(rq);
1878 if (cpu >= nr_cpu_ids)
1879 return;
1880
1881 rq->rt.push_flags = RT_PUSH_IPI_EXECUTING;
1882
1883 irq_work_queue_on(&rq->rt.push_work, cpu);
1884}
1885
1886/* Called from hardirq context */
1887static void try_to_push_tasks(void *arg)
1888{
1889 struct rt_rq *rt_rq = arg;
1890 struct rq *rq, *src_rq;
1891 int this_cpu;
1892 int cpu;
1893
1894 this_cpu = rt_rq->push_cpu;
1895
1896 /* Paranoid check */
1897 BUG_ON(this_cpu != smp_processor_id());
1898
1899 rq = cpu_rq(this_cpu);
1900 src_rq = rq_of_rt_rq(rt_rq);
1901
1902again:
1903 if (has_pushable_tasks(rq)) {
1904 raw_spin_lock(&rq->lock);
1905 push_rt_task(rq);
1906 raw_spin_unlock(&rq->lock);
1907 }
1908
1909 /* Pass the IPI to the next rt overloaded queue */
1910 raw_spin_lock(&rt_rq->push_lock);
1911 /*
1912 * If the source queue changed since the IPI went out,
1913 * we need to restart the search from that CPU again.
1914 */
1915 if (rt_rq->push_flags & RT_PUSH_IPI_RESTART) {
1916 rt_rq->push_flags &= ~RT_PUSH_IPI_RESTART;
1917 rt_rq->push_cpu = src_rq->cpu;
1918 }
1919
1920 cpu = find_next_push_cpu(src_rq);
1921
1922 if (cpu >= nr_cpu_ids)
1923 rt_rq->push_flags &= ~RT_PUSH_IPI_EXECUTING;
1924 raw_spin_unlock(&rt_rq->push_lock);
1925
1926 if (cpu >= nr_cpu_ids)
1927 return;
1928
1929 /*
1930 * It is possible that a restart caused this CPU to be
1931 * chosen again. Don't bother with an IPI, just see if we
1932 * have more to push.
1933 */
1934 if (unlikely(cpu == rq->cpu))
1935 goto again;
1936
1937 /* Try the next RT overloaded CPU */
1938 irq_work_queue_on(&rt_rq->push_work, cpu);
1939}
1940
1941static void push_irq_work_func(struct irq_work *work)
1942{
1943 struct rt_rq *rt_rq = container_of(work, struct rt_rq, push_work);
1944
1945 try_to_push_tasks(rt_rq);
1946}
1947#endif /* HAVE_RT_PUSH_IPI */
1948
Steven Rostedtf65eda42008-01-25 21:08:07 +01001949static int pull_rt_task(struct rq *this_rq)
1950{
Ingo Molnar80bf3172008-01-25 21:08:17 +01001951 int this_cpu = this_rq->cpu, ret = 0, cpu;
Gregory Haskinsa8728942008-12-29 09:39:49 -05001952 struct task_struct *p;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001953 struct rq *src_rq;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001954
Gregory Haskins637f5082008-01-25 21:08:18 +01001955 if (likely(!rt_overloaded(this_rq)))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001956 return 0;
1957
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +02001958 /*
1959 * Match the barrier from rt_set_overloaded; this guarantees that if we
1960 * see overloaded we must also see the rto_mask bit.
1961 */
1962 smp_rmb();
1963
Steven Rostedtb6366f02015-03-18 14:49:46 -04001964#ifdef HAVE_RT_PUSH_IPI
1965 if (sched_feat(RT_PUSH_IPI)) {
1966 tell_cpu_to_push(this_rq);
1967 return 0;
1968 }
1969#endif
1970
Rusty Russellc6c49272008-11-25 02:35:05 +10301971 for_each_cpu(cpu, this_rq->rd->rto_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001972 if (this_cpu == cpu)
1973 continue;
1974
1975 src_rq = cpu_rq(cpu);
Gregory Haskins74ab8e42008-12-29 09:39:50 -05001976
1977 /*
1978 * Don't bother taking the src_rq->lock if the next highest
1979 * task is known to be lower-priority than our current task.
1980 * This may look racy, but if this value is about to go
1981 * logically higher, the src_rq will push this task away.
1982 * And if its going logically lower, we do not care
1983 */
1984 if (src_rq->rt.highest_prio.next >=
1985 this_rq->rt.highest_prio.curr)
1986 continue;
1987
Steven Rostedtf65eda42008-01-25 21:08:07 +01001988 /*
1989 * We can potentially drop this_rq's lock in
1990 * double_lock_balance, and another CPU could
Gregory Haskinsa8728942008-12-29 09:39:49 -05001991 * alter this_rq
Steven Rostedtf65eda42008-01-25 21:08:07 +01001992 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05001993 double_lock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01001994
1995 /*
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001996 * We can pull only a task, which is pushable
1997 * on its rq, and no others.
Steven Rostedtf65eda42008-01-25 21:08:07 +01001998 */
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001999 p = pick_highest_pushable_task(src_rq, this_cpu);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002000
2001 /*
2002 * Do we have an RT task that preempts
2003 * the to-be-scheduled task?
2004 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002005 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01002006 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002007 WARN_ON(!task_on_rq_queued(p));
Steven Rostedtf65eda42008-01-25 21:08:07 +01002008
2009 /*
2010 * There's a chance that p is higher in priority
2011 * than what's currently running on its cpu.
2012 * This is just that p is wakeing up and hasn't
2013 * had a chance to schedule. We only pull
2014 * p if it is lower in priority than the
Gregory Haskinsa8728942008-12-29 09:39:49 -05002015 * current task on the run queue
Steven Rostedtf65eda42008-01-25 21:08:07 +01002016 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002017 if (p->prio < src_rq->curr->prio)
Mike Galbraith614ee1f2008-01-25 21:08:30 +01002018 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002019
2020 ret = 1;
2021
2022 deactivate_task(src_rq, p, 0);
2023 set_task_cpu(p, this_cpu);
2024 activate_task(this_rq, p, 0);
2025 /*
2026 * We continue with the search, just in
2027 * case there's an even higher prio task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002028 * in another runqueue. (low likelihood
Steven Rostedtf65eda42008-01-25 21:08:07 +01002029 * but possible)
Steven Rostedtf65eda42008-01-25 21:08:07 +01002030 */
Steven Rostedtf65eda42008-01-25 21:08:07 +01002031 }
Peter Zijlstra49246272010-10-17 21:46:10 +02002032skip:
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02002033 double_unlock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002034 }
2035
2036 return ret;
2037}
2038
Steven Rostedt9a897c52008-01-25 21:08:22 +01002039static void post_schedule_rt(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01002040{
Gregory Haskins967fc042008-12-29 09:39:52 -05002041 push_rt_tasks(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002042}
2043
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002044/*
2045 * If we are not running and we are not going to reschedule soon, we should
2046 * try to push tasks away now
2047 */
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002048static void task_woken_rt(struct rq *rq, struct task_struct *p)
Steven Rostedt4642daf2008-01-25 21:08:07 +01002049{
Steven Rostedt9a897c52008-01-25 21:08:22 +01002050 if (!task_running(rq, p) &&
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002051 !test_tsk_need_resched(rq->curr) &&
Gregory Haskins917b6272008-12-29 09:39:53 -05002052 has_pushable_tasks(rq) &&
Peter Zijlstra29baa742012-04-23 12:11:21 +02002053 p->nr_cpus_allowed > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01002054 (dl_task(rq->curr) || rt_task(rq->curr)) &&
Peter Zijlstra29baa742012-04-23 12:11:21 +02002055 (rq->curr->nr_cpus_allowed < 2 ||
Shawn Bohrer3be209a2011-09-12 09:28:04 -05002056 rq->curr->prio <= p->prio))
Steven Rostedt4642daf2008-01-25 21:08:07 +01002057 push_rt_tasks(rq);
2058}
2059
Mike Traviscd8ba7c2008-03-26 14:23:49 -07002060static void set_cpus_allowed_rt(struct task_struct *p,
Rusty Russell96f874e2008-11-25 02:35:14 +10302061 const struct cpumask *new_mask)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002062{
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002063 struct rq *rq;
2064 int weight;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002065
2066 BUG_ON(!rt_task(p));
2067
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002068 if (!task_on_rq_queued(p))
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002069 return;
2070
2071 weight = cpumask_weight(new_mask);
2072
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002073 /*
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002074 * Only update if the process changes its state from whether it
2075 * can migrate or not.
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002076 */
Peter Zijlstra29baa742012-04-23 12:11:21 +02002077 if ((p->nr_cpus_allowed > 1) == (weight > 1))
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002078 return;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002079
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002080 rq = task_rq(p);
Gregory Haskins917b6272008-12-29 09:39:53 -05002081
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002082 /*
2083 * The process used to be able to migrate OR it can now migrate
2084 */
2085 if (weight <= 1) {
2086 if (!task_current(rq, p))
2087 dequeue_pushable_task(rq, p);
2088 BUG_ON(!rq->rt.rt_nr_migratory);
2089 rq->rt.rt_nr_migratory--;
2090 } else {
2091 if (!task_current(rq, p))
2092 enqueue_pushable_task(rq, p);
2093 rq->rt.rt_nr_migratory++;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002094 }
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04002095
2096 update_rt_migration(&rq->rt);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002097}
Ingo Molnardeeeccd2008-01-25 21:08:15 +01002098
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002099/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002100static void rq_online_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002101{
2102 if (rq->rt.overloaded)
2103 rt_set_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002104
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002105 __enable_runtime(rq);
2106
Gregory Haskinse864c492008-12-29 09:39:49 -05002107 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002108}
2109
2110/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002111static void rq_offline_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002112{
2113 if (rq->rt.overloaded)
2114 rt_clear_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002115
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002116 __disable_runtime(rq);
2117
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002118 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002119}
Steven Rostedtcb469842008-01-25 21:08:22 +01002120
2121/*
2122 * When switch from the rt queue, we bring ourselves to a position
2123 * that we might want to pull RT tasks from other runqueues.
2124 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002125static void switched_from_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002126{
2127 /*
2128 * If there are other RT tasks then we will reschedule
2129 * and the scheduling of the other RT tasks will handle
2130 * the balancing. But if we are the last RT task
2131 * we may need to handle the pulling of RT tasks
2132 * now.
2133 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002134 if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
Kirill Tkhai1158ddb2012-11-23 00:02:15 +04002135 return;
2136
2137 if (pull_rt_task(rq))
Kirill Tkhai88751252014-06-29 00:03:57 +04002138 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002139}
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302140
Li Zefan11c785b2014-02-08 14:17:45 +08002141void __init init_sched_rt_class(void)
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302142{
2143 unsigned int i;
2144
Peter Zijlstra029632f2011-10-25 10:00:11 +02002145 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -07002146 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
Mike Travis6ca09df2008-12-31 18:08:45 -08002147 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra029632f2011-10-25 10:00:11 +02002148 }
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302149}
Steven Rostedte8fa1362008-01-25 21:08:05 +01002150#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002151
Steven Rostedtcb469842008-01-25 21:08:22 +01002152/*
2153 * When switching a task to RT, we may overload the runqueue
2154 * with RT tasks. In this case we try to push them off to
2155 * other runqueues.
2156 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002157static void switched_to_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002158{
2159 int check_resched = 1;
2160
2161 /*
2162 * If we are already running, then there's nothing
2163 * that needs to be done. But if we are not running
2164 * we may need to preempt the current running task.
2165 * If that current running task is also an RT task
2166 * then see if we can move to another run queue.
2167 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002168 if (task_on_rq_queued(p) && rq->curr != p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002169#ifdef CONFIG_SMP
Kirill V Tkhai10447912014-03-12 06:18:33 -04002170 if (p->nr_cpus_allowed > 1 && rq->rt.overloaded &&
Steven Rostedtcb469842008-01-25 21:08:22 +01002171 /* Don't resched if we changed runqueues */
Kirill V Tkhai10447912014-03-12 06:18:33 -04002172 push_rt_task(rq) && rq != task_rq(p))
Steven Rostedtcb469842008-01-25 21:08:22 +01002173 check_resched = 0;
2174#endif /* CONFIG_SMP */
2175 if (check_resched && p->prio < rq->curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002176 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002177 }
2178}
2179
2180/*
2181 * Priority of the task has changed. This may cause
2182 * us to initiate a push or pull.
2183 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002184static void
2185prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01002186{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002187 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002188 return;
2189
2190 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002191#ifdef CONFIG_SMP
2192 /*
2193 * If our priority decreases while running, we
2194 * may need to pull tasks to this runqueue.
2195 */
2196 if (oldprio < p->prio)
2197 pull_rt_task(rq);
2198 /*
2199 * If there's a higher priority task waiting to run
Steven Rostedt6fa46fa2008-03-05 10:00:12 -05002200 * then reschedule. Note, the above pull_rt_task
2201 * can release the rq lock and p could migrate.
2202 * Only reschedule if p is still on the same runqueue.
Steven Rostedtcb469842008-01-25 21:08:22 +01002203 */
Gregory Haskinse864c492008-12-29 09:39:49 -05002204 if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04002205 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002206#else
2207 /* For UP simply resched on drop of prio */
2208 if (oldprio < p->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002209 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002210#endif /* CONFIG_SMP */
2211 } else {
2212 /*
2213 * This task is not running, but if it is
2214 * greater than the current running task
2215 * then reschedule.
2216 */
2217 if (p->prio < rq->curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002218 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002219 }
2220}
2221
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002222static void watchdog(struct rq *rq, struct task_struct *p)
2223{
2224 unsigned long soft, hard;
2225
Jiri Slaby78d7d402010-03-05 13:42:54 -08002226 /* max may change after cur was read, this will be fixed next tick */
2227 soft = task_rlimit(p, RLIMIT_RTTIME);
2228 hard = task_rlimit_max(p, RLIMIT_RTTIME);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002229
2230 if (soft != RLIM_INFINITY) {
2231 unsigned long next;
2232
Ying Xue57d2aa02012-07-17 15:03:43 +08002233 if (p->rt.watchdog_stamp != jiffies) {
2234 p->rt.timeout++;
2235 p->rt.watchdog_stamp = jiffies;
2236 }
2237
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002238 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
Peter Zijlstra5a52dd52008-01-25 21:08:32 +01002239 if (p->rt.timeout > next)
Frank Mayharf06febc2008-09-12 09:54:39 -07002240 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002241 }
2242}
Steven Rostedtcb469842008-01-25 21:08:22 +01002243
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002244static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002245{
Colin Cross454c7992012-05-16 21:34:23 -07002246 struct sched_rt_entity *rt_se = &p->rt;
2247
Peter Zijlstra67e2be02007-12-20 15:01:17 +01002248 update_curr_rt(rq);
2249
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002250 watchdog(rq, p);
2251
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002252 /*
2253 * RR tasks need a special form of timeslice management.
2254 * FIFO tasks have no timeslices.
2255 */
2256 if (p->policy != SCHED_RR)
2257 return;
2258
Peter Zijlstrafa717062008-01-25 21:08:27 +01002259 if (--p->rt.time_slice)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002260 return;
2261
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002262 p->rt.time_slice = sched_rr_timeslice;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002263
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002264 /*
Li Bine9aa39b2013-10-21 20:15:43 +08002265 * Requeue to the end of queue if we (and all of our ancestors) are not
2266 * the only element on the queue
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002267 */
Colin Cross454c7992012-05-16 21:34:23 -07002268 for_each_sched_rt_entity(rt_se) {
2269 if (rt_se->run_list.prev != rt_se->run_list.next) {
2270 requeue_task_rt(rq, p, 0);
Kirill Tkhai8aa6f0e2014-09-22 22:36:43 +04002271 resched_curr(rq);
Colin Cross454c7992012-05-16 21:34:23 -07002272 return;
2273 }
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002274 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002275}
2276
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002277static void set_curr_task_rt(struct rq *rq)
2278{
2279 struct task_struct *p = rq->curr;
2280
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002281 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05002282
2283 /* The running task is never eligible for pushing */
2284 dequeue_pushable_task(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002285}
2286
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07002287static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00002288{
2289 /*
2290 * Time slice is 0 for SCHED_FIFO tasks
2291 */
2292 if (task->policy == SCHED_RR)
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002293 return sched_rr_timeslice;
Peter Williams0d721ce2009-09-21 01:31:53 +00002294 else
2295 return 0;
2296}
2297
Peter Zijlstra029632f2011-10-25 10:00:11 +02002298const struct sched_class rt_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002299 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002300 .enqueue_task = enqueue_task_rt,
2301 .dequeue_task = dequeue_task_rt,
2302 .yield_task = yield_task_rt,
2303
2304 .check_preempt_curr = check_preempt_curr_rt,
2305
2306 .pick_next_task = pick_next_task_rt,
2307 .put_prev_task = put_prev_task_rt,
2308
Peter Williams681f3e62007-10-24 18:23:51 +02002309#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08002310 .select_task_rq = select_task_rq_rt,
2311
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002312 .set_cpus_allowed = set_cpus_allowed_rt,
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002313 .rq_online = rq_online_rt,
2314 .rq_offline = rq_offline_rt,
Steven Rostedt9a897c52008-01-25 21:08:22 +01002315 .post_schedule = post_schedule_rt,
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002316 .task_woken = task_woken_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002317 .switched_from = switched_from_rt,
Peter Williams681f3e62007-10-24 18:23:51 +02002318#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002319
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002320 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002321 .task_tick = task_tick_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002322
Peter Williams0d721ce2009-09-21 01:31:53 +00002323 .get_rr_interval = get_rr_interval_rt,
2324
Steven Rostedtcb469842008-01-25 21:08:22 +01002325 .prio_changed = prio_changed_rt,
2326 .switched_to = switched_to_rt,
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002327
2328 .update_curr = update_curr_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002329};
Peter Zijlstraada18de2008-06-19 14:22:24 +02002330
2331#ifdef CONFIG_SCHED_DEBUG
2332extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2333
Peter Zijlstra029632f2011-10-25 10:00:11 +02002334void print_rt_stats(struct seq_file *m, int cpu)
Peter Zijlstraada18de2008-06-19 14:22:24 +02002335{
Cheng Xuec514c42011-05-14 14:20:02 +08002336 rt_rq_iter_t iter;
Peter Zijlstraada18de2008-06-19 14:22:24 +02002337 struct rt_rq *rt_rq;
2338
2339 rcu_read_lock();
Cheng Xuec514c42011-05-14 14:20:02 +08002340 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
Peter Zijlstraada18de2008-06-19 14:22:24 +02002341 print_rt_rq(m, cpu, rt_rq);
2342 rcu_read_unlock();
2343}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302344#endif /* CONFIG_SCHED_DEBUG */