blob: 03c967312c3e1403b54a33aff644e5f8de93c389 [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
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -070011#include "walt.h"
12
Clark Williamsce0dbbb2013-02-07 09:47:04 -060013int sched_rr_timeslice = RR_TIMESLICE;
14
Peter Zijlstra029632f2011-10-25 10:00:11 +020015static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
16
17struct rt_bandwidth def_rt_bandwidth;
18
19static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
20{
21 struct rt_bandwidth *rt_b =
22 container_of(timer, struct rt_bandwidth, rt_period_timer);
Peter Zijlstra029632f2011-10-25 10:00:11 +020023 int idle = 0;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020024 int overrun;
Peter Zijlstra029632f2011-10-25 10:00:11 +020025
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020026 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020027 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020028 overrun = hrtimer_forward_now(timer, rt_b->rt_period);
Peter Zijlstra029632f2011-10-25 10:00:11 +020029 if (!overrun)
30 break;
31
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020032 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020033 idle = do_sched_rt_period_timer(rt_b, overrun);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020034 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020035 }
Peter Zijlstra4cfafd32015-05-14 12:23:11 +020036 if (idle)
37 rt_b->rt_period_active = 0;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020038 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020039
40 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
41}
42
43void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
44{
45 rt_b->rt_period = ns_to_ktime(period);
46 rt_b->rt_runtime = runtime;
47
48 raw_spin_lock_init(&rt_b->rt_runtime_lock);
49
50 hrtimer_init(&rt_b->rt_period_timer,
51 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
52 rt_b->rt_period_timer.function = sched_rt_period_timer;
53}
54
55static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
56{
57 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
58 return;
59
Peter Zijlstra029632f2011-10-25 10:00:11 +020060 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +020061 if (!rt_b->rt_period_active) {
62 rt_b->rt_period_active = 1;
Steven Rostedtc3a990d2016-02-16 18:37:46 -050063 /*
64 * SCHED_DEADLINE updates the bandwidth, as a run away
65 * RT task with a DL task could hog a CPU. But DL does
66 * not reset the period. If a deadline task was running
67 * without an RT task running, it can cause RT tasks to
68 * throttle when they start up. Kick the timer right away
69 * to update the period.
70 */
71 hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0));
Peter Zijlstra4cfafd32015-05-14 12:23:11 +020072 hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED);
73 }
Peter Zijlstra029632f2011-10-25 10:00:11 +020074 raw_spin_unlock(&rt_b->rt_runtime_lock);
75}
76
Abel Vesa07c54f72015-03-03 13:50:27 +020077void init_rt_rq(struct rt_rq *rt_rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +020078{
79 struct rt_prio_array *array;
80 int i;
81
82 array = &rt_rq->active;
83 for (i = 0; i < MAX_RT_PRIO; i++) {
84 INIT_LIST_HEAD(array->queue + i);
85 __clear_bit(i, array->bitmap);
86 }
87 /* delimiter for bitsearch: */
88 __set_bit(MAX_RT_PRIO, array->bitmap);
89
90#if defined CONFIG_SMP
91 rt_rq->highest_prio.curr = MAX_RT_PRIO;
92 rt_rq->highest_prio.next = MAX_RT_PRIO;
93 rt_rq->rt_nr_migratory = 0;
94 rt_rq->overloaded = 0;
95 plist_head_init(&rt_rq->pushable_tasks);
Steven Rostedtb6366f02015-03-18 14:49:46 -040096#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +040097 /* We start is dequeued state, because no RT tasks are queued */
98 rt_rq->rt_queued = 0;
Peter Zijlstra029632f2011-10-25 10:00:11 +020099
100 rt_rq->rt_time = 0;
101 rt_rq->rt_throttled = 0;
102 rt_rq->rt_runtime = 0;
103 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
104}
105
Gregory Haskins398a1532009-01-14 09:10:04 -0500106#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +0200107static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
108{
109 hrtimer_cancel(&rt_b->rt_period_timer);
110}
Gregory Haskins398a1532009-01-14 09:10:04 -0500111
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200112#define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
113
Peter Zijlstra8f488942009-07-24 12:25:30 +0200114static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
115{
116#ifdef CONFIG_SCHED_DEBUG
117 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
118#endif
119 return container_of(rt_se, struct task_struct, rt);
120}
121
Gregory Haskins398a1532009-01-14 09:10:04 -0500122static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
123{
124 return rt_rq->rq;
125}
126
127static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
128{
129 return rt_se->rt_rq;
130}
131
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400132static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
133{
134 struct rt_rq *rt_rq = rt_se->rt_rq;
135
136 return rt_rq->rq;
137}
138
Peter Zijlstra029632f2011-10-25 10:00:11 +0200139void free_rt_sched_group(struct task_group *tg)
140{
141 int i;
142
143 if (tg->rt_se)
144 destroy_rt_bandwidth(&tg->rt_bandwidth);
145
146 for_each_possible_cpu(i) {
147 if (tg->rt_rq)
148 kfree(tg->rt_rq[i]);
149 if (tg->rt_se)
150 kfree(tg->rt_se[i]);
151 }
152
153 kfree(tg->rt_rq);
154 kfree(tg->rt_se);
155}
156
157void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
158 struct sched_rt_entity *rt_se, int cpu,
159 struct sched_rt_entity *parent)
160{
161 struct rq *rq = cpu_rq(cpu);
162
163 rt_rq->highest_prio.curr = MAX_RT_PRIO;
164 rt_rq->rt_nr_boosted = 0;
165 rt_rq->rq = rq;
166 rt_rq->tg = tg;
167
168 tg->rt_rq[cpu] = rt_rq;
169 tg->rt_se[cpu] = rt_se;
170
171 if (!rt_se)
172 return;
173
174 if (!parent)
175 rt_se->rt_rq = &rq->rt;
176 else
177 rt_se->rt_rq = parent->my_q;
178
179 rt_se->my_q = rt_rq;
180 rt_se->parent = parent;
181 INIT_LIST_HEAD(&rt_se->run_list);
182}
183
184int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
185{
186 struct rt_rq *rt_rq;
187 struct sched_rt_entity *rt_se;
188 int i;
189
190 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
191 if (!tg->rt_rq)
192 goto err;
193 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
194 if (!tg->rt_se)
195 goto err;
196
197 init_rt_bandwidth(&tg->rt_bandwidth,
198 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
199
200 for_each_possible_cpu(i) {
201 rt_rq = kzalloc_node(sizeof(struct rt_rq),
202 GFP_KERNEL, cpu_to_node(i));
203 if (!rt_rq)
204 goto err;
205
206 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
207 GFP_KERNEL, cpu_to_node(i));
208 if (!rt_se)
209 goto err_free_rq;
210
Abel Vesa07c54f72015-03-03 13:50:27 +0200211 init_rt_rq(rt_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200212 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
213 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
214 }
215
216 return 1;
217
218err_free_rq:
219 kfree(rt_rq);
220err:
221 return 0;
222}
223
Gregory Haskins398a1532009-01-14 09:10:04 -0500224#else /* CONFIG_RT_GROUP_SCHED */
225
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200226#define rt_entity_is_task(rt_se) (1)
227
Peter Zijlstra8f488942009-07-24 12:25:30 +0200228static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
229{
230 return container_of(rt_se, struct task_struct, rt);
231}
232
Gregory Haskins398a1532009-01-14 09:10:04 -0500233static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
234{
235 return container_of(rt_rq, struct rq, rt);
236}
237
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400238static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
Gregory Haskins398a1532009-01-14 09:10:04 -0500239{
240 struct task_struct *p = rt_task_of(rt_se);
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400241
242 return task_rq(p);
243}
244
245static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
246{
247 struct rq *rq = rq_of_rt_se(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -0500248
249 return &rq->rt;
250}
251
Peter Zijlstra029632f2011-10-25 10:00:11 +0200252void free_rt_sched_group(struct task_group *tg) { }
253
254int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
255{
256 return 1;
257}
Gregory Haskins398a1532009-01-14 09:10:04 -0500258#endif /* CONFIG_RT_GROUP_SCHED */
259
Steven Rostedt4fd29172008-01-25 21:08:06 +0100260#ifdef CONFIG_SMP
Ingo Molnar84de4272008-01-25 21:08:15 +0100261
Peter Zijlstra8046d682015-06-11 14:46:40 +0200262static void pull_rt_task(struct rq *this_rq);
Peter Zijlstra38033c32014-01-23 20:32:21 +0100263
Peter Zijlstradc877342014-02-12 15:47:29 +0100264static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
265{
266 /* Try to pull RT tasks here if we lower this rq's prio */
267 return rq->rt.highest_prio.curr > prev->prio;
268}
269
Gregory Haskins637f5082008-01-25 21:08:18 +0100270static inline int rt_overloaded(struct rq *rq)
Steven Rostedt4fd29172008-01-25 21:08:06 +0100271{
Gregory Haskins637f5082008-01-25 21:08:18 +0100272 return atomic_read(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100273}
Ingo Molnar84de4272008-01-25 21:08:15 +0100274
Steven Rostedt4fd29172008-01-25 21:08:06 +0100275static inline void rt_set_overload(struct rq *rq)
276{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400277 if (!rq->online)
278 return;
279
Rusty Russellc6c49272008-11-25 02:35:05 +1030280 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100281 /*
282 * Make sure the mask is visible before we set
283 * the overload count. That is checked to determine
284 * if we should look at the mask. It would be a shame
285 * if we looked at the mask, but the mask was not
286 * updated yet.
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200287 *
288 * Matched by the barrier in pull_rt_task().
Steven Rostedt4fd29172008-01-25 21:08:06 +0100289 */
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200290 smp_wmb();
Gregory Haskins637f5082008-01-25 21:08:18 +0100291 atomic_inc(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100292}
Ingo Molnar84de4272008-01-25 21:08:15 +0100293
Steven Rostedt4fd29172008-01-25 21:08:06 +0100294static inline void rt_clear_overload(struct rq *rq)
295{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400296 if (!rq->online)
297 return;
298
Steven Rostedt4fd29172008-01-25 21:08:06 +0100299 /* the order here really doesn't matter */
Gregory Haskins637f5082008-01-25 21:08:18 +0100300 atomic_dec(&rq->rd->rto_count);
Rusty Russellc6c49272008-11-25 02:35:05 +1030301 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100302}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100303
Gregory Haskins398a1532009-01-14 09:10:04 -0500304static void update_rt_migration(struct rt_rq *rt_rq)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100305{
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200306 if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
Gregory Haskins398a1532009-01-14 09:10:04 -0500307 if (!rt_rq->overloaded) {
308 rt_set_overload(rq_of_rt_rq(rt_rq));
309 rt_rq->overloaded = 1;
Gregory Haskinscdc8eb92008-01-25 21:08:23 +0100310 }
Gregory Haskins398a1532009-01-14 09:10:04 -0500311 } else if (rt_rq->overloaded) {
312 rt_clear_overload(rq_of_rt_rq(rt_rq));
313 rt_rq->overloaded = 0;
Gregory Haskins637f5082008-01-25 21:08:18 +0100314 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100315}
Steven Rostedt4fd29172008-01-25 21:08:06 +0100316
Gregory Haskins398a1532009-01-14 09:10:04 -0500317static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100318{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200319 struct task_struct *p;
320
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200321 if (!rt_entity_is_task(rt_se))
322 return;
323
Peter Zijlstra29baa742012-04-23 12:11:21 +0200324 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200325 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
326
327 rt_rq->rt_nr_total++;
Thomas Gleixner50605ff2016-05-11 14:23:31 +0200328 if (tsk_nr_cpus_allowed(p) > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500329 rt_rq->rt_nr_migratory++;
330
331 update_rt_migration(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100332}
333
Gregory Haskins398a1532009-01-14 09:10:04 -0500334static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
335{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200336 struct task_struct *p;
337
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200338 if (!rt_entity_is_task(rt_se))
339 return;
340
Peter Zijlstra29baa742012-04-23 12:11:21 +0200341 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200342 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
343
344 rt_rq->rt_nr_total--;
Thomas Gleixner50605ff2016-05-11 14:23:31 +0200345 if (tsk_nr_cpus_allowed(p) > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500346 rt_rq->rt_nr_migratory--;
347
348 update_rt_migration(rt_rq);
349}
350
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400351static inline int has_pushable_tasks(struct rq *rq)
352{
353 return !plist_head_empty(&rq->rt.pushable_tasks);
354}
355
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +0200356static DEFINE_PER_CPU(struct callback_head, rt_push_head);
357static DEFINE_PER_CPU(struct callback_head, rt_pull_head);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200358
359static void push_rt_tasks(struct rq *);
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +0200360static void pull_rt_task(struct rq *);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200361
362static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100363{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200364 if (!has_pushable_tasks(rq))
365 return;
366
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +0200367 queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
368}
369
370static inline void queue_pull_task(struct rq *rq)
371{
372 queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
Peter Zijlstradc877342014-02-12 15:47:29 +0100373}
374
Gregory Haskins917b6272008-12-29 09:39:53 -0500375static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
376{
377 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
378 plist_node_init(&p->pushable_tasks, p->prio);
379 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400380
381 /* Update the highest prio pushable task */
382 if (p->prio < rq->rt.highest_prio.next)
383 rq->rt.highest_prio.next = p->prio;
Gregory Haskins917b6272008-12-29 09:39:53 -0500384}
385
386static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
387{
388 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
Gregory Haskins917b6272008-12-29 09:39:53 -0500389
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400390 /* Update the new highest prio pushable task */
391 if (has_pushable_tasks(rq)) {
392 p = plist_first_entry(&rq->rt.pushable_tasks,
393 struct task_struct, pushable_tasks);
394 rq->rt.highest_prio.next = p->prio;
395 } else
396 rq->rt.highest_prio.next = MAX_RT_PRIO;
Ingo Molnarbcf08df2008-04-19 12:11:10 +0200397}
398
Gregory Haskins917b6272008-12-29 09:39:53 -0500399#else
400
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100401static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
402{
403}
404
405static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
406{
407}
408
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500409static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100410void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
411{
412}
413
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500414static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100415void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
416{
417}
Gregory Haskins917b6272008-12-29 09:39:53 -0500418
Peter Zijlstradc877342014-02-12 15:47:29 +0100419static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
420{
421 return false;
422}
423
Peter Zijlstra8046d682015-06-11 14:46:40 +0200424static inline void pull_rt_task(struct rq *this_rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100425{
Peter Zijlstradc877342014-02-12 15:47:29 +0100426}
427
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200428static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100429{
430}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200431#endif /* CONFIG_SMP */
432
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400433static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
434static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
435
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100436static inline int on_rt_rq(struct sched_rt_entity *rt_se)
437{
Peter Zijlstraff77e462016-01-18 15:27:07 +0100438 return rt_se->on_rq;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100439}
440
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100441#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100442
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100443static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100444{
445 if (!rt_rq->tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100446 return RUNTIME_INF;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100447
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200448 return rt_rq->rt_runtime;
449}
450
451static inline u64 sched_rt_period(struct rt_rq *rt_rq)
452{
453 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100454}
455
Cheng Xuec514c42011-05-14 14:20:02 +0800456typedef struct task_group *rt_rq_iter_t;
457
Yong Zhang1c09ab02011-06-28 10:51:31 +0800458static inline struct task_group *next_task_group(struct task_group *tg)
459{
460 do {
461 tg = list_entry_rcu(tg->list.next,
462 typeof(struct task_group), list);
463 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
464
465 if (&tg->list == &task_groups)
466 tg = NULL;
467
468 return tg;
469}
470
471#define for_each_rt_rq(rt_rq, iter, rq) \
472 for (iter = container_of(&task_groups, typeof(*iter), list); \
473 (iter = next_task_group(iter)) && \
474 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
Cheng Xuec514c42011-05-14 14:20:02 +0800475
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100476#define for_each_sched_rt_entity(rt_se) \
477 for (; rt_se; rt_se = rt_se->parent)
478
479static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
480{
481 return rt_se->my_q;
482}
483
Peter Zijlstraff77e462016-01-18 15:27:07 +0100484static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
485static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100486
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100487static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100488{
Dario Faggiolif6121f42008-10-03 17:40:46 +0200489 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
Kirill Tkhai88751252014-06-29 00:03:57 +0400490 struct rq *rq = rq_of_rt_rq(rt_rq);
Yong Zhang74b7eb52010-01-29 14:57:52 +0800491 struct sched_rt_entity *rt_se;
492
Kirill Tkhai88751252014-06-29 00:03:57 +0400493 int cpu = cpu_of(rq);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530494
495 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100496
Dario Faggiolif6121f42008-10-03 17:40:46 +0200497 if (rt_rq->rt_nr_running) {
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400498 if (!rt_se)
499 enqueue_top_rt_rq(rt_rq);
500 else if (!on_rt_rq(rt_se))
Peter Zijlstraff77e462016-01-18 15:27:07 +0100501 enqueue_rt_entity(rt_se, 0);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400502
Gregory Haskinse864c492008-12-29 09:39:49 -0500503 if (rt_rq->highest_prio.curr < curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +0400504 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100505 }
506}
507
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100508static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100509{
Yong Zhang74b7eb52010-01-29 14:57:52 +0800510 struct sched_rt_entity *rt_se;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530511 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
Yong Zhang74b7eb52010-01-29 14:57:52 +0800512
Balbir Singh0c3b9162011-03-03 17:04:35 +0530513 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100514
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400515 if (!rt_se)
516 dequeue_top_rt_rq(rt_rq);
517 else if (on_rt_rq(rt_se))
Peter Zijlstraff77e462016-01-18 15:27:07 +0100518 dequeue_rt_entity(rt_se, 0);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100519}
520
Kirill Tkhai46383642014-03-15 02:15:07 +0400521static inline int rt_rq_throttled(struct rt_rq *rt_rq)
522{
523 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
524}
525
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100526static int rt_se_boosted(struct sched_rt_entity *rt_se)
527{
528 struct rt_rq *rt_rq = group_rt_rq(rt_se);
529 struct task_struct *p;
530
531 if (rt_rq)
532 return !!rt_rq->rt_nr_boosted;
533
534 p = rt_task_of(rt_se);
535 return p->prio != p->normal_prio;
536}
537
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200538#ifdef CONFIG_SMP
Rusty Russellc6c49272008-11-25 02:35:05 +1030539static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200540{
Nathan Zimmer424c93f2013-05-09 11:24:03 -0500541 return this_rq()->rd->span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200542}
543#else
Rusty Russellc6c49272008-11-25 02:35:05 +1030544static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200545{
Rusty Russellc6c49272008-11-25 02:35:05 +1030546 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200547}
548#endif
549
550static inline
551struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
552{
553 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
554}
555
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200556static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
557{
558 return &rt_rq->tg->rt_bandwidth;
559}
560
Dhaval Giani55e12e52008-06-24 23:39:43 +0530561#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100562
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100563static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100564{
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200565 return rt_rq->rt_runtime;
566}
567
568static inline u64 sched_rt_period(struct rt_rq *rt_rq)
569{
570 return ktime_to_ns(def_rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100571}
572
Cheng Xuec514c42011-05-14 14:20:02 +0800573typedef struct rt_rq *rt_rq_iter_t;
574
575#define for_each_rt_rq(rt_rq, iter, rq) \
576 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
577
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100578#define for_each_sched_rt_entity(rt_se) \
579 for (; rt_se; rt_se = NULL)
580
581static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
582{
583 return NULL;
584}
585
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100586static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100587{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400588 struct rq *rq = rq_of_rt_rq(rt_rq);
589
590 if (!rt_rq->rt_nr_running)
591 return;
592
593 enqueue_top_rt_rq(rt_rq);
Kirill Tkhai88751252014-06-29 00:03:57 +0400594 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100595}
596
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100597static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100598{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400599 dequeue_top_rt_rq(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100600}
601
Kirill Tkhai46383642014-03-15 02:15:07 +0400602static inline int rt_rq_throttled(struct rt_rq *rt_rq)
603{
604 return rt_rq->rt_throttled;
605}
606
Rusty Russellc6c49272008-11-25 02:35:05 +1030607static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200608{
Rusty Russellc6c49272008-11-25 02:35:05 +1030609 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200610}
611
612static inline
613struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
614{
615 return &cpu_rq(cpu)->rt;
616}
617
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200618static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
619{
620 return &def_rt_bandwidth;
621}
622
Dhaval Giani55e12e52008-06-24 23:39:43 +0530623#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100624
Juri Lellifaa59932014-02-21 11:37:15 +0100625bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
626{
627 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
628
629 return (hrtimer_active(&rt_b->rt_period_timer) ||
630 rt_rq->rt_time < rt_b->rt_runtime);
631}
632
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200633#ifdef CONFIG_SMP
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200634/*
635 * We ran out of runtime, see if we can borrow some from our neighbours.
636 */
Juri Lelli269b26a2015-09-02 11:01:36 +0100637static void do_balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200638{
639 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
Shawn Bohreraa7f6732013-01-14 11:55:31 -0600640 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
Juri Lelli269b26a2015-09-02 11:01:36 +0100641 int i, weight;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200642 u64 rt_period;
643
Rusty Russellc6c49272008-11-25 02:35:05 +1030644 weight = cpumask_weight(rd->span);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200645
Thomas Gleixner0986b112009-11-17 15:32:06 +0100646 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200647 rt_period = ktime_to_ns(rt_b->rt_period);
Rusty Russellc6c49272008-11-25 02:35:05 +1030648 for_each_cpu(i, rd->span) {
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200649 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
650 s64 diff;
651
652 if (iter == rt_rq)
653 continue;
654
Thomas Gleixner0986b112009-11-17 15:32:06 +0100655 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200656 /*
657 * Either all rqs have inf runtime and there's nothing to steal
658 * or __disable_runtime() below sets a specific rq to inf to
659 * indicate its been disabled and disalow stealing.
660 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200661 if (iter->rt_runtime == RUNTIME_INF)
662 goto next;
663
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200664 /*
665 * From runqueues with spare time, take 1/n part of their
666 * spare time, but no more than our period.
667 */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200668 diff = iter->rt_runtime - iter->rt_time;
669 if (diff > 0) {
Peter Zijlstra58838cf2008-07-24 12:43:13 +0200670 diff = div_u64((u64)diff, weight);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200671 if (rt_rq->rt_runtime + diff > rt_period)
672 diff = rt_period - rt_rq->rt_runtime;
673 iter->rt_runtime -= diff;
674 rt_rq->rt_runtime += diff;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200675 if (rt_rq->rt_runtime == rt_period) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100676 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200677 break;
678 }
679 }
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200680next:
Thomas Gleixner0986b112009-11-17 15:32:06 +0100681 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200682 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100683 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200684}
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200685
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200686/*
687 * Ensure this RQ takes back all the runtime it lend to its neighbours.
688 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200689static void __disable_runtime(struct rq *rq)
690{
691 struct root_domain *rd = rq->rd;
Cheng Xuec514c42011-05-14 14:20:02 +0800692 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200693 struct rt_rq *rt_rq;
694
695 if (unlikely(!scheduler_running))
696 return;
697
Cheng Xuec514c42011-05-14 14:20:02 +0800698 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200699 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
700 s64 want;
701 int i;
702
Thomas Gleixner0986b112009-11-17 15:32:06 +0100703 raw_spin_lock(&rt_b->rt_runtime_lock);
704 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200705 /*
706 * Either we're all inf and nobody needs to borrow, or we're
707 * already disabled and thus have nothing to do, or we have
708 * exactly the right amount of runtime to take out.
709 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200710 if (rt_rq->rt_runtime == RUNTIME_INF ||
711 rt_rq->rt_runtime == rt_b->rt_runtime)
712 goto balanced;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100713 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200714
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200715 /*
716 * Calculate the difference between what we started out with
717 * and what we current have, that's the amount of runtime
718 * we lend and now have to reclaim.
719 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200720 want = rt_b->rt_runtime - rt_rq->rt_runtime;
721
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200722 /*
723 * Greedy reclaim, take back as much as we can.
724 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030725 for_each_cpu(i, rd->span) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200726 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
727 s64 diff;
728
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200729 /*
730 * Can't reclaim from ourselves or disabled runqueues.
731 */
Peter Zijlstraf1679d02008-08-14 15:49:00 +0200732 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200733 continue;
734
Thomas Gleixner0986b112009-11-17 15:32:06 +0100735 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200736 if (want > 0) {
737 diff = min_t(s64, iter->rt_runtime, want);
738 iter->rt_runtime -= diff;
739 want -= diff;
740 } else {
741 iter->rt_runtime -= want;
742 want -= want;
743 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100744 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200745
746 if (!want)
747 break;
748 }
749
Thomas Gleixner0986b112009-11-17 15:32:06 +0100750 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200751 /*
752 * We cannot be left wanting - that would mean some runtime
753 * leaked out of the system.
754 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200755 BUG_ON(want);
756balanced:
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200757 /*
758 * Disable all the borrow logic by pretending we have inf
759 * runtime - in which case borrowing doesn't make sense.
760 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200761 rt_rq->rt_runtime = RUNTIME_INF;
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -0700762 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100763 raw_spin_unlock(&rt_rq->rt_runtime_lock);
764 raw_spin_unlock(&rt_b->rt_runtime_lock);
Kirill Tkhai99b62562014-06-25 12:19:48 +0400765
766 /* Make rt_rq available for pick_next_task() */
767 sched_rt_rq_enqueue(rt_rq);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200768 }
769}
770
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200771static void __enable_runtime(struct rq *rq)
772{
Cheng Xuec514c42011-05-14 14:20:02 +0800773 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200774 struct rt_rq *rt_rq;
775
776 if (unlikely(!scheduler_running))
777 return;
778
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200779 /*
780 * Reset each runqueue's bandwidth settings
781 */
Cheng Xuec514c42011-05-14 14:20:02 +0800782 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200783 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
784
Thomas Gleixner0986b112009-11-17 15:32:06 +0100785 raw_spin_lock(&rt_b->rt_runtime_lock);
786 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200787 rt_rq->rt_runtime = rt_b->rt_runtime;
788 rt_rq->rt_time = 0;
Zhang, Yanminbaf25732008-09-09 11:26:33 +0800789 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100790 raw_spin_unlock(&rt_rq->rt_runtime_lock);
791 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200792 }
793}
794
Juri Lelli269b26a2015-09-02 11:01:36 +0100795static void balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraeff65492008-06-19 14:22:26 +0200796{
Peter Zijlstra4a6184c2011-10-06 22:39:14 +0200797 if (!sched_feat(RT_RUNTIME_SHARE))
Juri Lelli269b26a2015-09-02 11:01:36 +0100798 return;
Peter Zijlstra4a6184c2011-10-06 22:39:14 +0200799
Peter Zijlstraeff65492008-06-19 14:22:26 +0200800 if (rt_rq->rt_time > rt_rq->rt_runtime) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100801 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Juri Lelli269b26a2015-09-02 11:01:36 +0100802 do_balance_runtime(rt_rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100803 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200804 }
Peter Zijlstraeff65492008-06-19 14:22:26 +0200805}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530806#else /* !CONFIG_SMP */
Juri Lelli269b26a2015-09-02 11:01:36 +0100807static inline void balance_runtime(struct rt_rq *rt_rq) {}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530808#endif /* CONFIG_SMP */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100809
810static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
811{
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200812 int i, idle = 1, throttled = 0;
Rusty Russellc6c49272008-11-25 02:35:05 +1030813 const struct cpumask *span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200814
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200815 span = sched_rt_period_mask();
Mike Galbraithe221d022012-08-07 10:02:38 +0200816#ifdef CONFIG_RT_GROUP_SCHED
817 /*
818 * FIXME: isolated CPUs should really leave the root task group,
819 * whether they are isolcpus or were isolated via cpusets, lest
820 * the timer run on a CPU which does not service all runqueues,
821 * potentially leaving other CPUs indefinitely throttled. If
822 * isolation is really required, the user will turn the throttle
823 * off to kill the perturbations it causes anyway. Meanwhile,
824 * this maintains functionality for boot and/or troubleshooting.
825 */
826 if (rt_b == &root_task_group.rt_bandwidth)
827 span = cpu_online_mask;
828#endif
Rusty Russellc6c49272008-11-25 02:35:05 +1030829 for_each_cpu(i, span) {
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200830 int enqueue = 0;
831 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
832 struct rq *rq = rq_of_rt_rq(rt_rq);
833
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100834 raw_spin_lock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200835 if (rt_rq->rt_time) {
836 u64 runtime;
837
Thomas Gleixner0986b112009-11-17 15:32:06 +0100838 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200839 if (rt_rq->rt_throttled)
840 balance_runtime(rt_rq);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200841 runtime = rt_rq->rt_runtime;
842 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
843 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
844 rt_rq->rt_throttled = 0;
845 enqueue = 1;
Mike Galbraith61eadef2011-04-29 08:36:50 +0200846
847 /*
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100848 * When we're idle and a woken (rt) task is
849 * throttled check_preempt_curr() will set
850 * skip_update and the time between the wakeup
851 * and this unthrottle will get accounted as
852 * 'runtime'.
Mike Galbraith61eadef2011-04-29 08:36:50 +0200853 */
854 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100855 rq_clock_skip_update(rq, false);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200856 }
857 if (rt_rq->rt_time || rt_rq->rt_nr_running)
858 idle = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100859 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530860 } else if (rt_rq->rt_nr_running) {
Peter Zijlstra8a8cde12008-06-19 14:22:28 +0200861 idle = 0;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530862 if (!rt_rq_throttled(rt_rq))
863 enqueue = 1;
864 }
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200865 if (rt_rq->rt_throttled)
866 throttled = 1;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200867
868 if (enqueue)
869 sched_rt_rq_enqueue(rt_rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100870 raw_spin_unlock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200871 }
872
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200873 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
874 return 1;
875
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200876 return idle;
877}
878
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100879static inline int rt_se_prio(struct sched_rt_entity *rt_se)
880{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100881#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100882 struct rt_rq *rt_rq = group_rt_rq(rt_se);
883
884 if (rt_rq)
Gregory Haskinse864c492008-12-29 09:39:49 -0500885 return rt_rq->highest_prio.curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100886#endif
887
888 return rt_task_of(rt_se)->prio;
889}
890
Matt Wagantall989a7682014-06-17 21:43:35 -0700891static void dump_throttled_rt_tasks(struct rt_rq *rt_rq)
892{
893 struct rt_prio_array *array = &rt_rq->active;
894 struct sched_rt_entity *rt_se;
895 char buf[500];
896 char *pos = buf;
897 char *end = buf + sizeof(buf);
898 int idx;
899
900 pos += snprintf(pos, sizeof(buf),
901 "sched: RT throttling activated for rt_rq %p (cpu %d)\n",
902 rt_rq, cpu_of(rq_of_rt_rq(rt_rq)));
903
904 if (bitmap_empty(array->bitmap, MAX_RT_PRIO))
905 goto out;
906
907 pos += snprintf(pos, end - pos, "potential CPU hogs:\n");
908 idx = sched_find_first_bit(array->bitmap);
909 while (idx < MAX_RT_PRIO) {
910 list_for_each_entry(rt_se, array->queue + idx, run_list) {
911 struct task_struct *p;
912
913 if (!rt_entity_is_task(rt_se))
914 continue;
915
916 p = rt_task_of(rt_se);
917 if (pos < end)
918 pos += snprintf(pos, end - pos, "\t%s (%d)\n",
919 p->comm, p->pid);
920 }
921 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx + 1);
922 }
923out:
Matt Wagantall345eb972014-06-19 14:23:33 -0700924#ifdef CONFIG_PANIC_ON_RT_THROTTLING
925 /*
926 * Use pr_err() in the BUG() case since printk_sched() will
927 * not get flushed and deadlock is not a concern.
928 */
929 pr_err("%s", buf);
930 BUG();
931#else
Matt Wagantall989a7682014-06-17 21:43:35 -0700932 printk_deferred("%s", buf);
Matt Wagantall345eb972014-06-19 14:23:33 -0700933#endif
Matt Wagantall989a7682014-06-17 21:43:35 -0700934}
935
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100936static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100937{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100938 u64 runtime = sched_rt_runtime(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100939
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100940 if (rt_rq->rt_throttled)
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100941 return rt_rq_throttled(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100942
Shan Hai5b680fd2011-11-29 11:03:56 +0800943 if (runtime >= sched_rt_period(rt_rq))
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200944 return 0;
945
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200946 balance_runtime(rt_rq);
947 runtime = sched_rt_runtime(rt_rq);
948 if (runtime == RUNTIME_INF)
949 return 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200950
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100951 if (rt_rq->rt_time > runtime) {
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200952 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
953
954 /*
955 * Don't actually throttle groups that have no runtime assigned
956 * but accrue some time due to boosting.
957 */
958 if (likely(rt_b->rt_runtime)) {
Matt Wagantall989a7682014-06-17 21:43:35 -0700959 static bool once = false;
960
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200961 rt_rq->rt_throttled = 1;
Matt Wagantall989a7682014-06-17 21:43:35 -0700962
963 if (!once) {
964 once = true;
965 dump_throttled_rt_tasks(rt_rq);
966 }
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200967 } else {
968 /*
969 * In case we did anyway, make it go away,
970 * replenishment is a joke, since it will replenish us
971 * with exactly 0 ns.
972 */
973 rt_rq->rt_time = 0;
974 }
975
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100976 if (rt_rq_throttled(rt_rq)) {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100977 sched_rt_rq_dequeue(rt_rq);
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100978 return 1;
979 }
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100980 }
981
982 return 0;
983}
984
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200985/*
986 * Update the current task's runtime statistics. Skip current tasks that
987 * are not in our scheduling class.
988 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200989static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200990{
991 struct task_struct *curr = rq->curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100992 struct sched_rt_entity *rt_se = &curr->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200993 u64 delta_exec;
994
Peter Zijlstra06c3bc62011-02-02 13:19:48 +0100995 if (curr->sched_class != &rt_sched_class)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200996 return;
997
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200998 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Kirill Tkhaifc79e242013-01-30 16:50:36 +0400999 if (unlikely((s64)delta_exec <= 0))
1000 return;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001001
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02001002 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02001003 cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_RT);
Wanpeng Li594dd292016-04-22 17:07:24 +08001004
Peter Zijlstra42c62a52011-10-18 22:03:48 +02001005 schedstat_set(curr->se.statistics.exec_max,
1006 max(curr->se.statistics.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001007
1008 curr->se.sum_exec_runtime += delta_exec;
Frank Mayharf06febc2008-09-12 09:54:39 -07001009 account_group_exec_runtime(curr, delta_exec);
1010
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001011 curr->se.exec_start = rq_clock_task(rq);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001012 cpuacct_charge(curr, delta_exec);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01001013
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001014 sched_rt_avg_update(rq, delta_exec);
1015
Peter Zijlstra0b148fa2008-08-19 12:33:04 +02001016 if (!rt_bandwidth_enabled())
1017 return;
1018
Dhaval Giani354d60c2008-04-19 19:44:59 +02001019 for_each_sched_rt_entity(rt_se) {
Giedrius Rekasius0b079392014-05-25 15:23:31 +01001020 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
Dhaval Giani354d60c2008-04-19 19:44:59 +02001021
Peter Zijlstracc2991c2008-08-19 12:33:03 +02001022 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
Thomas Gleixner0986b112009-11-17 15:32:06 +01001023 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +02001024 rt_rq->rt_time += delta_exec;
1025 if (sched_rt_runtime_exceeded(rt_rq))
Kirill Tkhai88751252014-06-29 00:03:57 +04001026 resched_curr(rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +01001027 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +02001028 }
Dhaval Giani354d60c2008-04-19 19:44:59 +02001029 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001030}
1031
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001032static void
1033dequeue_top_rt_rq(struct rt_rq *rt_rq)
1034{
1035 struct rq *rq = rq_of_rt_rq(rt_rq);
1036
1037 BUG_ON(&rq->rt != rt_rq);
1038
1039 if (!rt_rq->rt_queued)
1040 return;
1041
1042 BUG_ON(!rq->nr_running);
1043
Kirill Tkhai72465442014-05-09 03:00:14 +04001044 sub_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001045 rt_rq->rt_queued = 0;
1046}
1047
1048static void
1049enqueue_top_rt_rq(struct rt_rq *rt_rq)
1050{
1051 struct rq *rq = rq_of_rt_rq(rt_rq);
1052
1053 BUG_ON(&rq->rt != rt_rq);
1054
1055 if (rt_rq->rt_queued)
1056 return;
1057 if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
1058 return;
1059
Kirill Tkhai72465442014-05-09 03:00:14 +04001060 add_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001061 rt_rq->rt_queued = 1;
1062}
1063
Gregory Haskins398a1532009-01-14 09:10:04 -05001064#if defined CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05001065
Gregory Haskins398a1532009-01-14 09:10:04 -05001066static void
1067inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001068{
Gregory Haskins4d984272008-12-29 09:39:49 -05001069 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins4d984272008-12-29 09:39:49 -05001070
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001071#ifdef CONFIG_RT_GROUP_SCHED
1072 /*
1073 * Change rq's cpupri only if rt_rq is the top queue.
1074 */
1075 if (&rq->rt != rt_rq)
1076 return;
1077#endif
Steven Rostedt5181f4a42011-06-16 21:55:23 -04001078 if (rq->online && prio < prev_prio)
1079 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
Steven Rostedt63489e42008-01-25 21:08:03 +01001080}
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001081
Gregory Haskins398a1532009-01-14 09:10:04 -05001082static void
1083dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001084{
Gregory Haskins4d984272008-12-29 09:39:49 -05001085 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001086
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001087#ifdef CONFIG_RT_GROUP_SCHED
1088 /*
1089 * Change rq's cpupri only if rt_rq is the top queue.
1090 */
1091 if (&rq->rt != rt_rq)
1092 return;
1093#endif
Gregory Haskins398a1532009-01-14 09:10:04 -05001094 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1095 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1096}
1097
1098#else /* CONFIG_SMP */
1099
1100static inline
1101void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1102static inline
1103void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1104
1105#endif /* CONFIG_SMP */
1106
Steven Rostedt63489e42008-01-25 21:08:03 +01001107#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001108static void
1109inc_rt_prio(struct rt_rq *rt_rq, int prio)
1110{
1111 int prev_prio = rt_rq->highest_prio.curr;
Steven Rostedt63489e42008-01-25 21:08:03 +01001112
Gregory Haskins398a1532009-01-14 09:10:04 -05001113 if (prio < prev_prio)
1114 rt_rq->highest_prio.curr = prio;
1115
1116 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1117}
1118
1119static void
1120dec_rt_prio(struct rt_rq *rt_rq, int prio)
1121{
1122 int prev_prio = rt_rq->highest_prio.curr;
1123
1124 if (rt_rq->rt_nr_running) {
1125
1126 WARN_ON(prio < prev_prio);
Gregory Haskinse864c492008-12-29 09:39:49 -05001127
1128 /*
Gregory Haskins398a1532009-01-14 09:10:04 -05001129 * This may have been our highest task, and therefore
1130 * we may have some recomputation to do
Gregory Haskinse864c492008-12-29 09:39:49 -05001131 */
Gregory Haskins398a1532009-01-14 09:10:04 -05001132 if (prio == prev_prio) {
Gregory Haskinse864c492008-12-29 09:39:49 -05001133 struct rt_prio_array *array = &rt_rq->active;
1134
1135 rt_rq->highest_prio.curr =
Steven Rostedt764a9d62008-01-25 21:08:04 +01001136 sched_find_first_bit(array->bitmap);
Gregory Haskinse864c492008-12-29 09:39:49 -05001137 }
1138
Steven Rostedt764a9d62008-01-25 21:08:04 +01001139 } else
Gregory Haskinse864c492008-12-29 09:39:49 -05001140 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001141
Gregory Haskins398a1532009-01-14 09:10:04 -05001142 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1143}
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001144
Gregory Haskins398a1532009-01-14 09:10:04 -05001145#else
1146
1147static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1148static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1149
1150#endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1151
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001152#ifdef CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001153
1154static void
1155inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1156{
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001157 if (rt_se_boosted(rt_se))
Steven Rostedt764a9d62008-01-25 21:08:04 +01001158 rt_rq->rt_nr_boosted++;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01001159
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001160 if (rt_rq->tg)
1161 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001162}
1163
1164static void
1165dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1166{
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001167 if (rt_se_boosted(rt_se))
1168 rt_rq->rt_nr_boosted--;
1169
1170 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
Gregory Haskins398a1532009-01-14 09:10:04 -05001171}
1172
1173#else /* CONFIG_RT_GROUP_SCHED */
1174
1175static void
1176inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1177{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001178 start_rt_bandwidth(&def_rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001179}
1180
1181static inline
1182void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1183
1184#endif /* CONFIG_RT_GROUP_SCHED */
1185
1186static inline
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001187unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1188{
1189 struct rt_rq *group_rq = group_rt_rq(rt_se);
1190
1191 if (group_rq)
1192 return group_rq->rt_nr_running;
1193 else
1194 return 1;
1195}
1196
1197static inline
Frederic Weisbecker01d36d02015-11-04 18:17:10 +01001198unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
1199{
1200 struct rt_rq *group_rq = group_rt_rq(rt_se);
1201 struct task_struct *tsk;
1202
1203 if (group_rq)
1204 return group_rq->rr_nr_running;
1205
1206 tsk = rt_task_of(rt_se);
1207
1208 return (tsk->policy == SCHED_RR) ? 1 : 0;
1209}
1210
1211static inline
Gregory Haskins398a1532009-01-14 09:10:04 -05001212void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1213{
1214 int prio = rt_se_prio(rt_se);
1215
1216 WARN_ON(!rt_prio(prio));
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001217 rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
Frederic Weisbecker01d36d02015-11-04 18:17:10 +01001218 rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -05001219
1220 inc_rt_prio(rt_rq, prio);
1221 inc_rt_migration(rt_se, rt_rq);
1222 inc_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001223}
1224
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001225static inline
1226void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1227{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001228 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001229 WARN_ON(!rt_rq->rt_nr_running);
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001230 rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
Frederic Weisbecker01d36d02015-11-04 18:17:10 +01001231 rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001232
Gregory Haskins398a1532009-01-14 09:10:04 -05001233 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1234 dec_rt_migration(rt_se, rt_rq);
1235 dec_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001236}
1237
Peter Zijlstraff77e462016-01-18 15:27:07 +01001238/*
1239 * Change rt_se->run_list location unless SAVE && !MOVE
1240 *
1241 * assumes ENQUEUE/DEQUEUE flags match
1242 */
1243static inline bool move_entity(unsigned int flags)
1244{
1245 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
1246 return false;
1247
1248 return true;
1249}
1250
1251static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
1252{
1253 list_del_init(&rt_se->run_list);
1254
1255 if (list_empty(array->queue + rt_se_prio(rt_se)))
1256 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1257
1258 rt_se->on_list = 0;
1259}
1260
1261static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001262{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001263 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1264 struct rt_prio_array *array = &rt_rq->active;
1265 struct rt_rq *group_rq = group_rt_rq(rt_se);
Dmitry Adamushko20b63312008-06-11 00:58:30 +02001266 struct list_head *queue = array->queue + rt_se_prio(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001267
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001268 /*
1269 * Don't enqueue the group if its throttled, or when empty.
1270 * The latter is a consequence of the former when a child group
1271 * get throttled and the current group doesn't have any other
1272 * active members.
1273 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01001274 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) {
1275 if (rt_se->on_list)
1276 __delist_rt_entity(rt_se, array);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001277 return;
Peter Zijlstraff77e462016-01-18 15:27:07 +01001278 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001279
Peter Zijlstraff77e462016-01-18 15:27:07 +01001280 if (move_entity(flags)) {
1281 WARN_ON_ONCE(rt_se->on_list);
1282 if (flags & ENQUEUE_HEAD)
1283 list_add(&rt_se->run_list, queue);
1284 else
1285 list_add_tail(&rt_se->run_list, queue);
1286
1287 __set_bit(rt_se_prio(rt_se), array->bitmap);
1288 rt_se->on_list = 1;
1289 }
1290 rt_se->on_rq = 1;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001291
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001292 inc_rt_tasks(rt_se, rt_rq);
1293}
1294
Peter Zijlstraff77e462016-01-18 15:27:07 +01001295static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001296{
1297 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1298 struct rt_prio_array *array = &rt_rq->active;
1299
Peter Zijlstraff77e462016-01-18 15:27:07 +01001300 if (move_entity(flags)) {
1301 WARN_ON_ONCE(!rt_se->on_list);
1302 __delist_rt_entity(rt_se, array);
1303 }
1304 rt_se->on_rq = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001305
1306 dec_rt_tasks(rt_se, rt_rq);
1307}
1308
1309/*
1310 * Because the prio of an upper entry depends on the lower
1311 * entries, we must remove entries top - down.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001312 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01001313static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001314{
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001315 struct sched_rt_entity *back = NULL;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001316
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001317 for_each_sched_rt_entity(rt_se) {
1318 rt_se->back = back;
1319 back = rt_se;
1320 }
1321
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001322 dequeue_top_rt_rq(rt_rq_of_se(back));
1323
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001324 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1325 if (on_rt_rq(rt_se))
Peter Zijlstraff77e462016-01-18 15:27:07 +01001326 __dequeue_rt_entity(rt_se, flags);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001327 }
1328}
1329
Peter Zijlstraff77e462016-01-18 15:27:07 +01001330static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001331{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001332 struct rq *rq = rq_of_rt_se(rt_se);
1333
Peter Zijlstraff77e462016-01-18 15:27:07 +01001334 dequeue_rt_stack(rt_se, flags);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001335 for_each_sched_rt_entity(rt_se)
Peter Zijlstraff77e462016-01-18 15:27:07 +01001336 __enqueue_rt_entity(rt_se, flags);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001337 enqueue_top_rt_rq(&rq->rt);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001338}
1339
Peter Zijlstraff77e462016-01-18 15:27:07 +01001340static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001341{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001342 struct rq *rq = rq_of_rt_se(rt_se);
1343
Peter Zijlstraff77e462016-01-18 15:27:07 +01001344 dequeue_rt_stack(rt_se, flags);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001345
1346 for_each_sched_rt_entity(rt_se) {
1347 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1348
1349 if (rt_rq && rt_rq->rt_nr_running)
Peter Zijlstraff77e462016-01-18 15:27:07 +01001350 __enqueue_rt_entity(rt_se, flags);
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001351 }
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001352 enqueue_top_rt_rq(&rq->rt);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001353}
1354
1355/*
1356 * Adding/removing a task to/from a priority array:
1357 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001358static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001359enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001360{
1361 struct sched_rt_entity *rt_se = &p->rt;
1362
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001363 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001364 rt_se->timeout = 0;
1365
Peter Zijlstraff77e462016-01-18 15:27:07 +01001366 enqueue_rt_entity(rt_se, flags);
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07001367 walt_inc_cumulative_runnable_avg(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001368
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001369 if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001370 enqueue_pushable_task(rq, p);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001371}
1372
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001373static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001374{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001375 struct sched_rt_entity *rt_se = &p->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001376
1377 update_curr_rt(rq);
Peter Zijlstraff77e462016-01-18 15:27:07 +01001378 dequeue_rt_entity(rt_se, flags);
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07001379 walt_dec_cumulative_runnable_avg(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001380
Gregory Haskins917b6272008-12-29 09:39:53 -05001381 dequeue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001382}
1383
1384/*
Richard Weinberger60686312011-11-12 18:07:57 +01001385 * Put task to the head or the end of the run list without the overhead of
1386 * dequeue followed by enqueue.
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001387 */
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001388static void
1389requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001390{
Ingo Molnar1cdad712008-06-19 09:09:15 +02001391 if (on_rt_rq(rt_se)) {
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001392 struct rt_prio_array *array = &rt_rq->active;
1393 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1394
1395 if (head)
1396 list_move(&rt_se->run_list, queue);
1397 else
1398 list_move_tail(&rt_se->run_list, queue);
Ingo Molnar1cdad712008-06-19 09:09:15 +02001399 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001400}
1401
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001402static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001403{
1404 struct sched_rt_entity *rt_se = &p->rt;
1405 struct rt_rq *rt_rq;
1406
1407 for_each_sched_rt_entity(rt_se) {
1408 rt_rq = rt_rq_of_se(rt_se);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001409 requeue_rt_entity(rt_rq, rt_se, head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001410 }
1411}
1412
1413static void yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001414{
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001415 requeue_task_rt(rq, rq->curr, 0);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001416}
1417
Gregory Haskinse7693a32008-01-25 21:08:09 +01001418#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +01001419static int find_lowest_rq(struct task_struct *task);
1420
Peter Zijlstra0017d732010-03-24 18:34:10 +01001421static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01001422select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
Gregory Haskinse7693a32008-01-25 21:08:09 +01001423{
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001424 struct task_struct *curr;
1425 struct rq *rq;
Steven Rostedtc37495f2011-06-16 21:55:22 -04001426
1427 /* For anything but wake ups, just return the task_cpu */
1428 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1429 goto out;
1430
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001431 rq = cpu_rq(cpu);
1432
1433 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07001434 curr = READ_ONCE(rq->curr); /* unlocked access */
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001435
Gregory Haskins318e0892008-01-25 21:08:10 +01001436 /*
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001437 * If the current task on @p's runqueue is an RT task, then
Steven Rostedte1f47d82008-01-25 21:08:12 +01001438 * try to see if we can wake this RT task up on another
1439 * runqueue. Otherwise simply start this RT task
1440 * on its current runqueue.
1441 *
Steven Rostedt43fa5462010-09-20 22:40:03 -04001442 * We want to avoid overloading runqueues. If the woken
1443 * task is a higher priority, then it will stay on this CPU
1444 * and the lower prio task should be moved to another CPU.
1445 * Even though this will probably make the lower prio task
1446 * lose its cache, we do not want to bounce a higher task
1447 * around just because it gave up its CPU, perhaps for a
1448 * lock?
1449 *
1450 * For equal prio tasks, we just let the scheduler sort it out.
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001451 *
Gregory Haskins318e0892008-01-25 21:08:10 +01001452 * Otherwise, just let it ride on the affined RQ and the
1453 * post-schedule router will push the preempted task away
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001454 *
1455 * This test is optimistic, if we get it wrong the load-balancer
1456 * will have to sort it out.
Gregory Haskins318e0892008-01-25 21:08:10 +01001457 */
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001458 if (curr && unlikely(rt_task(curr)) &&
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001459 (tsk_nr_cpus_allowed(curr) < 2 ||
Shawn Bohrer6bfa6872013-10-04 14:24:53 -05001460 curr->prio <= p->prio)) {
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001461 int target = find_lowest_rq(p);
1462
Tim Chen80e3d872014-12-12 15:38:12 -08001463 /*
1464 * Don't bother moving it if the destination CPU is
1465 * not running a lower priority task.
1466 */
1467 if (target != -1 &&
1468 p->prio < cpu_rq(target)->rt.highest_prio.curr)
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001469 cpu = target;
1470 }
1471 rcu_read_unlock();
1472
Steven Rostedtc37495f2011-06-16 21:55:22 -04001473out:
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001474 return cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001475}
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001476
1477static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1478{
Wanpeng Li308a6232014-10-31 06:39:31 +08001479 /*
1480 * Current can't be migrated, useless to reschedule,
1481 * let's hope p can move out.
1482 */
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001483 if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
Wanpeng Li308a6232014-10-31 06:39:31 +08001484 !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001485 return;
1486
Wanpeng Li308a6232014-10-31 06:39:31 +08001487 /*
1488 * p is migratable, so let's not schedule it and
1489 * see if it is pushed or pulled somewhere else.
1490 */
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001491 if (tsk_nr_cpus_allowed(p) != 1
Rusty Russell13b8bd02009-03-25 15:01:22 +10301492 && cpupri_find(&rq->rd->cpupri, p, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001493 return;
1494
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001495 /*
1496 * There appears to be other cpus that can accept
1497 * current and none to run 'p', so lets reschedule
1498 * to try and push current away:
1499 */
1500 requeue_task_rt(rq, p, 1);
Kirill Tkhai88751252014-06-29 00:03:57 +04001501 resched_curr(rq);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001502}
1503
Gregory Haskinse7693a32008-01-25 21:08:09 +01001504#endif /* CONFIG_SMP */
1505
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001506/*
1507 * Preempt the current task with a newly woken task if needed:
1508 */
Peter Zijlstra7d478722009-09-14 19:55:44 +02001509static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001510{
Gregory Haskins45c01e82008-05-12 21:20:41 +02001511 if (p->prio < rq->curr->prio) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001512 resched_curr(rq);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001513 return;
1514 }
1515
1516#ifdef CONFIG_SMP
1517 /*
1518 * If:
1519 *
1520 * - the newly woken task is of equal priority to the current task
1521 * - the newly woken task is non-migratable while current is migratable
1522 * - current will be preempted on the next reschedule
1523 *
1524 * we should check to see if current can readily move to a different
1525 * cpu. If so, we will reschedule to allow the push logic to try
1526 * to move current somewhere else, making room for our non-migratable
1527 * task.
1528 */
Hillf Danton8dd0de82011-06-14 18:36:24 -04001529 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001530 check_preempt_equal_prio(rq, p);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001531#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001532}
1533
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001534static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1535 struct rt_rq *rt_rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001536{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001537 struct rt_prio_array *array = &rt_rq->active;
1538 struct sched_rt_entity *next = NULL;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001539 struct list_head *queue;
1540 int idx;
1541
1542 idx = sched_find_first_bit(array->bitmap);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001543 BUG_ON(idx >= MAX_RT_PRIO);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001544
1545 queue = array->queue + idx;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001546 next = list_entry(queue->next, struct sched_rt_entity, run_list);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001547
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001548 return next;
1549}
1550
Gregory Haskins917b6272008-12-29 09:39:53 -05001551static struct task_struct *_pick_next_task_rt(struct rq *rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001552{
1553 struct sched_rt_entity *rt_se;
1554 struct task_struct *p;
Peter Zijlstra606dba22012-02-11 06:05:00 +01001555 struct rt_rq *rt_rq = &rq->rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001556
1557 do {
1558 rt_se = pick_next_rt_entity(rq, rt_rq);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001559 BUG_ON(!rt_se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001560 rt_rq = group_rt_rq(rt_se);
1561 } while (rt_rq);
1562
1563 p = rt_task_of(rt_se);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001564 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001565
1566 return p;
1567}
1568
Peter Zijlstra606dba22012-02-11 06:05:00 +01001569static struct task_struct *
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001570pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie)
Gregory Haskins917b6272008-12-29 09:39:53 -05001571{
Peter Zijlstra606dba22012-02-11 06:05:00 +01001572 struct task_struct *p;
1573 struct rt_rq *rt_rq = &rq->rt;
1574
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001575 if (need_pull_rt_task(rq, prev)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001576 /*
1577 * This is OK, because current is on_cpu, which avoids it being
1578 * picked for load-balance and preemption/IRQs are still
1579 * disabled avoiding further scheduler activity on it and we're
1580 * being very careful to re-start the picking loop.
1581 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001582 lockdep_unpin_lock(&rq->lock, cookie);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001583 pull_rt_task(rq);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001584 lockdep_repin_lock(&rq->lock, cookie);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001585 /*
1586 * pull_rt_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001587 * means a dl or stop task can slip in, in which case we need
1588 * to re-start task selection.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001589 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001590 if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001591 rq->dl.dl_nr_running))
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001592 return RETRY_TASK;
1593 }
Peter Zijlstra38033c32014-01-23 20:32:21 +01001594
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001595 /*
1596 * We may dequeue prev's rt_rq in put_prev_task().
1597 * So, we update time before rt_nr_running check.
1598 */
1599 if (prev->sched_class == &rt_sched_class)
1600 update_curr_rt(rq);
1601
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001602 if (!rt_rq->rt_queued)
Peter Zijlstra606dba22012-02-11 06:05:00 +01001603 return NULL;
1604
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001605 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001606
1607 p = _pick_next_task_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001608
1609 /* The running task is never eligible for pushing */
Kirill Tkhaif3f17682014-09-12 17:42:01 +04001610 dequeue_pushable_task(rq, p);
Gregory Haskins917b6272008-12-29 09:39:53 -05001611
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02001612 queue_push_tasks(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04001613
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001614 return p;
1615}
1616
Ingo Molnar31ee5292007-08-09 11:16:49 +02001617static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001618{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +02001619 update_curr_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001620
1621 /*
1622 * The previous task needs to be made eligible for pushing
1623 * if it is still active
1624 */
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001625 if (on_rt_rq(&p->rt) && tsk_nr_cpus_allowed(p) > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001626 enqueue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001627}
1628
Peter Williams681f3e62007-10-24 18:23:51 +02001629#ifdef CONFIG_SMP
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001630
Steven Rostedte8fa1362008-01-25 21:08:05 +01001631/* Only try algorithms three times */
1632#define RT_MAX_TRIES 3
1633
Steven Rostedtf65eda42008-01-25 21:08:07 +01001634static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1635{
1636 if (!task_running(rq, p) &&
Kirill Tkhai60334ca2013-01-31 18:56:17 +04001637 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001638 return 1;
1639 return 0;
1640}
1641
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001642/*
1643 * Return the highest pushable rq's task, which is suitable to be executed
1644 * on the cpu, NULL otherwise
1645 */
1646static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001647{
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001648 struct plist_head *head = &rq->rt.pushable_tasks;
1649 struct task_struct *p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001650
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001651 if (!has_pushable_tasks(rq))
1652 return NULL;
Peter Zijlstra3d074672010-03-10 17:07:24 +01001653
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001654 plist_for_each_entry(p, head, pushable_tasks) {
1655 if (pick_rt_task(rq, p, cpu))
1656 return p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001657 }
1658
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001659 return NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001660}
1661
Rusty Russell0e3900e2008-11-25 02:35:13 +10301662static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001663
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001664static int find_lowest_rq(struct task_struct *task)
1665{
1666 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001667 struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001668 int this_cpu = smp_processor_id();
1669 int cpu = task_cpu(task);
1670
Steven Rostedt0da938c2011-06-14 18:36:25 -04001671 /* Make sure the mask is initialized first */
1672 if (unlikely(!lowest_mask))
1673 return -1;
1674
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001675 if (tsk_nr_cpus_allowed(task) == 1)
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001676 return -1; /* No other targets possible */
1677
1678 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
Gregory Haskins06f90db2008-01-25 21:08:13 +01001679 return -1; /* No targets found */
1680
1681 /*
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001682 * At this point we have built a mask of cpus representing the
1683 * lowest priority tasks in the system. Now we want to elect
1684 * the best one based on our affinity and topology.
1685 *
1686 * We prioritize the last cpu that the task executed on since
1687 * it is most likely cache-hot in that location.
1688 */
Rusty Russell96f874e2008-11-25 02:35:14 +10301689 if (cpumask_test_cpu(cpu, lowest_mask))
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001690 return cpu;
1691
1692 /*
1693 * Otherwise, we consult the sched_domains span maps to figure
1694 * out which cpu is logically closest to our hot cache data.
1695 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301696 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1697 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001698
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001699 rcu_read_lock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301700 for_each_domain(cpu, sd) {
1701 if (sd->flags & SD_WAKE_AFFINE) {
1702 int best_cpu;
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001703
Rusty Russelle2c88062009-11-03 14:53:15 +10301704 /*
1705 * "this_cpu" is cheaper to preempt than a
1706 * remote processor.
1707 */
1708 if (this_cpu != -1 &&
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001709 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1710 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301711 return this_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001712 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001713
Rusty Russelle2c88062009-11-03 14:53:15 +10301714 best_cpu = cpumask_first_and(lowest_mask,
1715 sched_domain_span(sd));
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001716 if (best_cpu < nr_cpu_ids) {
1717 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301718 return best_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001719 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001720 }
1721 }
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001722 rcu_read_unlock();
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001723
1724 /*
1725 * And finally, if there were no matches within the domains
1726 * just give the caller *something* to work with from the compatible
1727 * locations.
1728 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301729 if (this_cpu != -1)
1730 return this_cpu;
1731
1732 cpu = cpumask_any(lowest_mask);
1733 if (cpu < nr_cpu_ids)
1734 return cpu;
1735 return -1;
Gregory Haskins07b40322008-01-25 21:08:10 +01001736}
1737
Steven Rostedte8fa1362008-01-25 21:08:05 +01001738/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +01001739static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001740{
1741 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001742 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +01001743 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001744
1745 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +01001746 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001747
Gregory Haskins2de0b462008-01-25 21:08:10 +01001748 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +01001749 break;
1750
Gregory Haskins07b40322008-01-25 21:08:10 +01001751 lowest_rq = cpu_rq(cpu);
1752
Tim Chen80e3d872014-12-12 15:38:12 -08001753 if (lowest_rq->rt.highest_prio.curr <= task->prio) {
1754 /*
1755 * Target rq has tasks of equal or higher priority,
1756 * retrying does not release any lock and is unlikely
1757 * to yield a different result.
1758 */
1759 lowest_rq = NULL;
1760 break;
1761 }
1762
Steven Rostedte8fa1362008-01-25 21:08:05 +01001763 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +01001764 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +01001765 /*
1766 * We had to unlock the run queue. In
1767 * the mean time, task could have
1768 * migrated already or had its affinity changed.
1769 * Also make sure that it wasn't scheduled on its rq.
1770 */
Gregory Haskins07b40322008-01-25 21:08:10 +01001771 if (unlikely(task_rq(task) != rq ||
Rusty Russell96f874e2008-11-25 02:35:14 +10301772 !cpumask_test_cpu(lowest_rq->cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001773 tsk_cpus_allowed(task)) ||
Gregory Haskins07b40322008-01-25 21:08:10 +01001774 task_running(rq, task) ||
Xunlei Pang13b5ab02016-05-09 12:11:31 +08001775 !rt_task(task) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001776 !task_on_rq_queued(task))) {
Ingo Molnar4df64c02008-01-25 21:08:15 +01001777
Peter Zijlstra7f1b4392012-05-17 21:19:46 +02001778 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001779 lowest_rq = NULL;
1780 break;
1781 }
1782 }
1783
1784 /* If this rq is still suitable use it. */
Gregory Haskinse864c492008-12-29 09:39:49 -05001785 if (lowest_rq->rt.highest_prio.curr > task->prio)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001786 break;
1787
1788 /* try again */
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001789 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001790 lowest_rq = NULL;
1791 }
1792
1793 return lowest_rq;
1794}
1795
Gregory Haskins917b6272008-12-29 09:39:53 -05001796static struct task_struct *pick_next_pushable_task(struct rq *rq)
1797{
1798 struct task_struct *p;
1799
1800 if (!has_pushable_tasks(rq))
1801 return NULL;
1802
1803 p = plist_first_entry(&rq->rt.pushable_tasks,
1804 struct task_struct, pushable_tasks);
1805
1806 BUG_ON(rq->cpu != task_cpu(p));
1807 BUG_ON(task_current(rq, p));
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001808 BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
Gregory Haskins917b6272008-12-29 09:39:53 -05001809
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001810 BUG_ON(!task_on_rq_queued(p));
Gregory Haskins917b6272008-12-29 09:39:53 -05001811 BUG_ON(!rt_task(p));
1812
1813 return p;
1814}
1815
Steven Rostedte8fa1362008-01-25 21:08:05 +01001816/*
1817 * If the current CPU has more than one RT task, see if the non
1818 * running task can migrate over to a CPU that is running a task
1819 * of lesser priority.
1820 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001821static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001822{
1823 struct task_struct *next_task;
1824 struct rq *lowest_rq;
Hillf Danton311e8002011-06-16 21:55:20 -04001825 int ret = 0;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001826
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +01001827 if (!rq->rt.overloaded)
1828 return 0;
1829
Gregory Haskins917b6272008-12-29 09:39:53 -05001830 next_task = pick_next_pushable_task(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001831 if (!next_task)
1832 return 0;
1833
Peter Zijlstra49246272010-10-17 21:46:10 +02001834retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +01001835 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001836 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001837 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001838 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01001839
1840 /*
1841 * It's possible that the next_task slipped in of
1842 * higher priority than current. If that's the case
1843 * just reschedule current.
1844 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001845 if (unlikely(next_task->prio < rq->curr->prio)) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001846 resched_curr(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001847 return 0;
1848 }
1849
Gregory Haskins697f0a42008-01-25 21:08:09 +01001850 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +01001851 get_task_struct(next_task);
1852
1853 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001854 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001855 if (!lowest_rq) {
1856 struct task_struct *task;
1857 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001858 * find_lock_lowest_rq releases rq->lock
Gregory Haskins15635132008-12-29 09:39:53 -05001859 * so it is possible that next_task has migrated.
1860 *
1861 * We need to make sure that the task is still on the same
1862 * run-queue and is also still the next task eligible for
1863 * pushing.
Steven Rostedte8fa1362008-01-25 21:08:05 +01001864 */
Gregory Haskins917b6272008-12-29 09:39:53 -05001865 task = pick_next_pushable_task(rq);
Gregory Haskins15635132008-12-29 09:39:53 -05001866 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1867 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001868 * The task hasn't migrated, and is still the next
1869 * eligible task, but we failed to find a run-queue
1870 * to push it to. Do not retry in this case, since
1871 * other cpus will pull from us when ready.
Gregory Haskins15635132008-12-29 09:39:53 -05001872 */
Gregory Haskins15635132008-12-29 09:39:53 -05001873 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001874 }
Gregory Haskins917b6272008-12-29 09:39:53 -05001875
Gregory Haskins15635132008-12-29 09:39:53 -05001876 if (!task)
1877 /* No more tasks, just exit */
1878 goto out;
1879
Gregory Haskins917b6272008-12-29 09:39:53 -05001880 /*
Gregory Haskins15635132008-12-29 09:39:53 -05001881 * Something has shifted, try again.
Gregory Haskins917b6272008-12-29 09:39:53 -05001882 */
Gregory Haskins15635132008-12-29 09:39:53 -05001883 put_task_struct(next_task);
1884 next_task = task;
1885 goto retry;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001886 }
1887
Gregory Haskins697f0a42008-01-25 21:08:09 +01001888 deactivate_task(rq, next_task, 0);
Olav Haugan77ba2b92015-08-05 08:45:21 -07001889 next_task->on_rq = TASK_ON_RQ_MIGRATING;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001890 set_task_cpu(next_task, lowest_rq->cpu);
Olav Haugan77ba2b92015-08-05 08:45:21 -07001891 next_task->on_rq = TASK_ON_RQ_QUEUED;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001892 activate_task(lowest_rq, next_task, 0);
Hillf Danton311e8002011-06-16 21:55:20 -04001893 ret = 1;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001894
Kirill Tkhai88751252014-06-29 00:03:57 +04001895 resched_curr(lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001896
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001897 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001898
Steven Rostedte8fa1362008-01-25 21:08:05 +01001899out:
1900 put_task_struct(next_task);
1901
Hillf Danton311e8002011-06-16 21:55:20 -04001902 return ret;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001903}
1904
Steven Rostedte8fa1362008-01-25 21:08:05 +01001905static void push_rt_tasks(struct rq *rq)
1906{
1907 /* push_rt_task will return true if it moved an RT */
1908 while (push_rt_task(rq))
1909 ;
1910}
1911
Steven Rostedtb6366f02015-03-18 14:49:46 -04001912#ifdef HAVE_RT_PUSH_IPI
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001913
Steven Rostedtb6366f02015-03-18 14:49:46 -04001914/*
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001915 * When a high priority task schedules out from a CPU and a lower priority
1916 * task is scheduled in, a check is made to see if there's any RT tasks
1917 * on other CPUs that are waiting to run because a higher priority RT task
1918 * is currently running on its CPU. In this case, the CPU with multiple RT
1919 * tasks queued on it (overloaded) needs to be notified that a CPU has opened
1920 * up that may be able to run one of its non-running queued RT tasks.
Steven Rostedtb6366f02015-03-18 14:49:46 -04001921 *
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001922 * All CPUs with overloaded RT tasks need to be notified as there is currently
1923 * no way to know which of these CPUs have the highest priority task waiting
1924 * to run. Instead of trying to take a spinlock on each of these CPUs,
1925 * which has shown to cause large latency when done on machines with many
1926 * CPUs, sending an IPI to the CPUs to have them push off the overloaded
1927 * RT tasks waiting to run.
1928 *
1929 * Just sending an IPI to each of the CPUs is also an issue, as on large
1930 * count CPU machines, this can cause an IPI storm on a CPU, especially
1931 * if its the only CPU with multiple RT tasks queued, and a large number
1932 * of CPUs scheduling a lower priority task at the same time.
1933 *
1934 * Each root domain has its own irq work function that can iterate over
1935 * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
1936 * tassk must be checked if there's one or many CPUs that are lowering
1937 * their priority, there's a single irq work iterator that will try to
1938 * push off RT tasks that are waiting to run.
1939 *
1940 * When a CPU schedules a lower priority task, it will kick off the
1941 * irq work iterator that will jump to each CPU with overloaded RT tasks.
1942 * As it only takes the first CPU that schedules a lower priority task
1943 * to start the process, the rto_start variable is incremented and if
1944 * the atomic result is one, then that CPU will try to take the rto_lock.
1945 * This prevents high contention on the lock as the process handles all
1946 * CPUs scheduling lower priority tasks.
1947 *
1948 * All CPUs that are scheduling a lower priority task will increment the
1949 * rt_loop_next variable. This will make sure that the irq work iterator
1950 * checks all RT overloaded CPUs whenever a CPU schedules a new lower
1951 * priority task, even if the iterator is in the middle of a scan. Incrementing
1952 * the rt_loop_next will cause the iterator to perform another scan.
1953 *
Steven Rostedtb6366f02015-03-18 14:49:46 -04001954 */
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05001955static int rto_next_cpu(struct root_domain *rd)
Steven Rostedtb6366f02015-03-18 14:49:46 -04001956{
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001957 int next;
Steven Rostedtb6366f02015-03-18 14:49:46 -04001958 int cpu;
1959
Steven Rostedtb6366f02015-03-18 14:49:46 -04001960 /*
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001961 * When starting the IPI RT pushing, the rto_cpu is set to -1,
1962 * rt_next_cpu() will simply return the first CPU found in
1963 * the rto_mask.
1964 *
1965 * If rto_next_cpu() is called with rto_cpu is a valid cpu, it
1966 * will return the next CPU found in the rto_mask.
1967 *
1968 * If there are no more CPUs left in the rto_mask, then a check is made
1969 * against rto_loop and rto_loop_next. rto_loop is only updated with
1970 * the rto_lock held, but any CPU may increment the rto_loop_next
1971 * without any locking.
Steven Rostedtb6366f02015-03-18 14:49:46 -04001972 */
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001973 for (;;) {
Steven Rostedtb6366f02015-03-18 14:49:46 -04001974
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001975 /* When rto_cpu is -1 this acts like cpumask_first() */
1976 cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
1977
1978 rd->rto_cpu = cpu;
1979
1980 if (cpu < nr_cpu_ids)
1981 return cpu;
1982
1983 rd->rto_cpu = -1;
1984
Steven Rostedtb6366f02015-03-18 14:49:46 -04001985 /*
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001986 * ACQUIRE ensures we see the @rto_mask changes
1987 * made prior to the @next value observed.
1988 *
1989 * Matches WMB in rt_set_overload().
Steven Rostedtb6366f02015-03-18 14:49:46 -04001990 */
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001991 next = atomic_read_acquire(&rd->rto_loop_next);
Steven Rostedtb6366f02015-03-18 14:49:46 -04001992
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001993 if (rd->rto_loop == next)
1994 break;
1995
1996 rd->rto_loop = next;
1997 }
1998
1999 return -1;
Steven Rostedtb6366f02015-03-18 14:49:46 -04002000}
2001
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002002static inline bool rto_start_trylock(atomic_t *v)
Steven Rostedtb6366f02015-03-18 14:49:46 -04002003{
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002004 return !atomic_cmpxchg_acquire(v, 0, 1);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002005}
2006
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002007static inline void rto_start_unlock(atomic_t *v)
2008{
2009 atomic_set_release(v, 0);
2010}
Steven Rostedtb6366f02015-03-18 14:49:46 -04002011
2012static void tell_cpu_to_push(struct rq *rq)
2013{
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002014 int cpu = -1;
Steven Rostedtb6366f02015-03-18 14:49:46 -04002015
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002016 /* Keep the loop going if the IPI is currently active */
2017 atomic_inc(&rq->rd->rto_loop_next);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002018
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002019 /* Only one CPU can initiate a loop at a time */
2020 if (!rto_start_trylock(&rq->rd->rto_loop_start))
Steven Rostedtb6366f02015-03-18 14:49:46 -04002021 return;
2022
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002023 raw_spin_lock(&rq->rd->rto_lock);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002024
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002025 /*
2026 * The rto_cpu is updated under the lock, if it has a valid cpu
2027 * then the IPI is still running and will continue due to the
2028 * update to loop_next, and nothing needs to be done here.
2029 * Otherwise it is finishing up and an ipi needs to be sent.
2030 */
2031 if (rq->rd->rto_cpu < 0)
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002032 cpu = rto_next_cpu(rq->rd);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002033
2034 raw_spin_unlock(&rq->rd->rto_lock);
2035
2036 rto_start_unlock(&rq->rd->rto_loop_start);
2037
Steven Rostedt (VMware)a384e542018-01-23 20:45:38 -05002038 if (cpu >= 0) {
2039 /* Make sure the rd does not get freed while pushing */
2040 sched_get_rd(rq->rd);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002041 irq_work_queue_on(&rq->rd->rto_push_work, cpu);
Steven Rostedt (VMware)a384e542018-01-23 20:45:38 -05002042 }
Steven Rostedtb6366f02015-03-18 14:49:46 -04002043}
2044
2045/* Called from hardirq context */
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002046void rto_push_irq_work_func(struct irq_work *work)
Steven Rostedtb6366f02015-03-18 14:49:46 -04002047{
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002048 struct root_domain *rd =
2049 container_of(work, struct root_domain, rto_push_work);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002050 struct rq *rq;
Steven Rostedtb6366f02015-03-18 14:49:46 -04002051 int cpu;
2052
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002053 rq = this_rq();
Steven Rostedtb6366f02015-03-18 14:49:46 -04002054
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002055 /*
2056 * We do not need to grab the lock to check for has_pushable_tasks.
2057 * When it gets updated, a check is made if a push is possible.
2058 */
Steven Rostedtb6366f02015-03-18 14:49:46 -04002059 if (has_pushable_tasks(rq)) {
2060 raw_spin_lock(&rq->lock);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002061 push_rt_tasks(rq);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002062 raw_spin_unlock(&rq->lock);
2063 }
2064
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002065 raw_spin_lock(&rd->rto_lock);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002066
Steven Rostedtb6366f02015-03-18 14:49:46 -04002067 /* Pass the IPI to the next rt overloaded queue */
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002068 cpu = rto_next_cpu(rd);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002069
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002070 raw_spin_unlock(&rd->rto_lock);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002071
Steven Rostedt (VMware)a384e542018-01-23 20:45:38 -05002072 if (cpu < 0) {
2073 sched_put_rd(rd);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002074 return;
Steven Rostedt (VMware)a384e542018-01-23 20:45:38 -05002075 }
Steven Rostedtb6366f02015-03-18 14:49:46 -04002076
Steven Rostedtb6366f02015-03-18 14:49:46 -04002077 /* Try the next RT overloaded CPU */
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002078 irq_work_queue_on(&rd->rto_push_work, cpu);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002079}
2080#endif /* HAVE_RT_PUSH_IPI */
2081
Peter Zijlstra8046d682015-06-11 14:46:40 +02002082static void pull_rt_task(struct rq *this_rq)
Steven Rostedtf65eda42008-01-25 21:08:07 +01002083{
Peter Zijlstra8046d682015-06-11 14:46:40 +02002084 int this_cpu = this_rq->cpu, cpu;
2085 bool resched = false;
Gregory Haskinsa8728942008-12-29 09:39:49 -05002086 struct task_struct *p;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002087 struct rq *src_rq;
Steven Rostedtd2668172017-12-02 13:04:54 -05002088 int rt_overload_count = rt_overloaded(this_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002089
Steven Rostedtd2668172017-12-02 13:04:54 -05002090 if (likely(!rt_overload_count))
Peter Zijlstra8046d682015-06-11 14:46:40 +02002091 return;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002092
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +02002093 /*
2094 * Match the barrier from rt_set_overloaded; this guarantees that if we
2095 * see overloaded we must also see the rto_mask bit.
2096 */
2097 smp_rmb();
2098
Steven Rostedtd2668172017-12-02 13:04:54 -05002099 /* If we are the only overloaded CPU do nothing */
2100 if (rt_overload_count == 1 &&
2101 cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
2102 return;
2103
Steven Rostedtb6366f02015-03-18 14:49:46 -04002104#ifdef HAVE_RT_PUSH_IPI
2105 if (sched_feat(RT_PUSH_IPI)) {
2106 tell_cpu_to_push(this_rq);
Peter Zijlstra8046d682015-06-11 14:46:40 +02002107 return;
Steven Rostedtb6366f02015-03-18 14:49:46 -04002108 }
2109#endif
2110
Rusty Russellc6c49272008-11-25 02:35:05 +10302111 for_each_cpu(cpu, this_rq->rd->rto_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01002112 if (this_cpu == cpu)
2113 continue;
2114
2115 src_rq = cpu_rq(cpu);
Gregory Haskins74ab8e42008-12-29 09:39:50 -05002116
2117 /*
2118 * Don't bother taking the src_rq->lock if the next highest
2119 * task is known to be lower-priority than our current task.
2120 * This may look racy, but if this value is about to go
2121 * logically higher, the src_rq will push this task away.
2122 * And if its going logically lower, we do not care
2123 */
2124 if (src_rq->rt.highest_prio.next >=
2125 this_rq->rt.highest_prio.curr)
2126 continue;
2127
Steven Rostedtf65eda42008-01-25 21:08:07 +01002128 /*
2129 * We can potentially drop this_rq's lock in
2130 * double_lock_balance, and another CPU could
Gregory Haskinsa8728942008-12-29 09:39:49 -05002131 * alter this_rq
Steven Rostedtf65eda42008-01-25 21:08:07 +01002132 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002133 double_lock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002134
2135 /*
Kirill Tkhaie23ee742013-06-07 15:37:43 -04002136 * We can pull only a task, which is pushable
2137 * on its rq, and no others.
Steven Rostedtf65eda42008-01-25 21:08:07 +01002138 */
Kirill Tkhaie23ee742013-06-07 15:37:43 -04002139 p = pick_highest_pushable_task(src_rq, this_cpu);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002140
2141 /*
2142 * Do we have an RT task that preempts
2143 * the to-be-scheduled task?
2144 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002145 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01002146 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002147 WARN_ON(!task_on_rq_queued(p));
Steven Rostedtf65eda42008-01-25 21:08:07 +01002148
2149 /*
2150 * There's a chance that p is higher in priority
2151 * than what's currently running on its cpu.
2152 * This is just that p is wakeing up and hasn't
2153 * had a chance to schedule. We only pull
2154 * p if it is lower in priority than the
Gregory Haskinsa8728942008-12-29 09:39:49 -05002155 * current task on the run queue
Steven Rostedtf65eda42008-01-25 21:08:07 +01002156 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002157 if (p->prio < src_rq->curr->prio)
Mike Galbraith614ee1f2008-01-25 21:08:30 +01002158 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002159
Peter Zijlstra8046d682015-06-11 14:46:40 +02002160 resched = true;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002161
2162 deactivate_task(src_rq, p, 0);
Olav Haugan77ba2b92015-08-05 08:45:21 -07002163 p->on_rq = TASK_ON_RQ_MIGRATING;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002164 set_task_cpu(p, this_cpu);
Olav Haugan77ba2b92015-08-05 08:45:21 -07002165 p->on_rq = TASK_ON_RQ_QUEUED;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002166 activate_task(this_rq, p, 0);
2167 /*
2168 * We continue with the search, just in
2169 * case there's an even higher prio task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002170 * in another runqueue. (low likelihood
Steven Rostedtf65eda42008-01-25 21:08:07 +01002171 * but possible)
Steven Rostedtf65eda42008-01-25 21:08:07 +01002172 */
Steven Rostedtf65eda42008-01-25 21:08:07 +01002173 }
Peter Zijlstra49246272010-10-17 21:46:10 +02002174skip:
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02002175 double_unlock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002176 }
2177
Peter Zijlstra8046d682015-06-11 14:46:40 +02002178 if (resched)
2179 resched_curr(this_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002180}
2181
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002182/*
2183 * If we are not running and we are not going to reschedule soon, we should
2184 * try to push tasks away now
2185 */
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002186static void task_woken_rt(struct rq *rq, struct task_struct *p)
Steven Rostedt4642daf2008-01-25 21:08:07 +01002187{
Steven Rostedt9a897c52008-01-25 21:08:22 +01002188 if (!task_running(rq, p) &&
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002189 !test_tsk_need_resched(rq->curr) &&
Thomas Gleixner50605ff2016-05-11 14:23:31 +02002190 tsk_nr_cpus_allowed(p) > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01002191 (dl_task(rq->curr) || rt_task(rq->curr)) &&
Thomas Gleixner50605ff2016-05-11 14:23:31 +02002192 (tsk_nr_cpus_allowed(rq->curr) < 2 ||
Shawn Bohrer3be209a2011-09-12 09:28:04 -05002193 rq->curr->prio <= p->prio))
Steven Rostedt4642daf2008-01-25 21:08:07 +01002194 push_rt_tasks(rq);
2195}
2196
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002197/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002198static void rq_online_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002199{
2200 if (rq->rt.overloaded)
2201 rt_set_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002202
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002203 __enable_runtime(rq);
2204
Gregory Haskinse864c492008-12-29 09:39:49 -05002205 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002206}
2207
2208/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002209static void rq_offline_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002210{
2211 if (rq->rt.overloaded)
2212 rt_clear_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002213
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002214 __disable_runtime(rq);
2215
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002216 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002217}
Steven Rostedtcb469842008-01-25 21:08:22 +01002218
2219/*
2220 * When switch from the rt queue, we bring ourselves to a position
2221 * that we might want to pull RT tasks from other runqueues.
2222 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002223static void switched_from_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002224{
2225 /*
2226 * If there are other RT tasks then we will reschedule
2227 * and the scheduling of the other RT tasks will handle
2228 * the balancing. But if we are the last RT task
2229 * we may need to handle the pulling of RT tasks
2230 * now.
2231 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002232 if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
Kirill Tkhai1158ddb2012-11-23 00:02:15 +04002233 return;
2234
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002235 queue_pull_task(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002236}
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302237
Li Zefan11c785b2014-02-08 14:17:45 +08002238void __init init_sched_rt_class(void)
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302239{
2240 unsigned int i;
2241
Peter Zijlstra029632f2011-10-25 10:00:11 +02002242 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -07002243 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
Mike Travis6ca09df2008-12-31 18:08:45 -08002244 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra029632f2011-10-25 10:00:11 +02002245 }
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302246}
Steven Rostedte8fa1362008-01-25 21:08:05 +01002247#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002248
Steven Rostedtcb469842008-01-25 21:08:22 +01002249/*
2250 * When switching a task to RT, we may overload the runqueue
2251 * with RT tasks. In this case we try to push them off to
2252 * other runqueues.
2253 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002254static void switched_to_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002255{
Steven Rostedtcb469842008-01-25 21:08:22 +01002256 /*
2257 * If we are already running, then there's nothing
2258 * that needs to be done. But if we are not running
2259 * we may need to preempt the current running task.
2260 * If that current running task is also an RT task
2261 * then see if we can move to another run queue.
2262 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002263 if (task_on_rq_queued(p) && rq->curr != p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002264#ifdef CONFIG_SMP
Thomas Gleixner50605ff2016-05-11 14:23:31 +02002265 if (tsk_nr_cpus_allowed(p) > 1 && rq->rt.overloaded)
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002266 queue_push_tasks(rq);
Sebastian Andrzej Siewior916c5cfe2017-01-24 15:40:06 +01002267#endif /* CONFIG_SMP */
Paul E. McKenneybac7bb12017-10-13 17:00:18 -07002268 if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
Kirill Tkhai88751252014-06-29 00:03:57 +04002269 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002270 }
2271}
2272
2273/*
2274 * Priority of the task has changed. This may cause
2275 * us to initiate a push or pull.
2276 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002277static void
2278prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01002279{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002280 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002281 return;
2282
2283 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002284#ifdef CONFIG_SMP
2285 /*
2286 * If our priority decreases while running, we
2287 * may need to pull tasks to this runqueue.
2288 */
2289 if (oldprio < p->prio)
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002290 queue_pull_task(rq);
2291
Steven Rostedtcb469842008-01-25 21:08:22 +01002292 /*
2293 * If there's a higher priority task waiting to run
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002294 * then reschedule.
Steven Rostedtcb469842008-01-25 21:08:22 +01002295 */
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002296 if (p->prio > rq->rt.highest_prio.curr)
Kirill Tkhai88751252014-06-29 00:03:57 +04002297 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002298#else
2299 /* For UP simply resched on drop of prio */
2300 if (oldprio < p->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002301 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002302#endif /* CONFIG_SMP */
2303 } else {
2304 /*
2305 * This task is not running, but if it is
2306 * greater than the current running task
2307 * then reschedule.
2308 */
2309 if (p->prio < rq->curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002310 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002311 }
2312}
2313
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002314static void watchdog(struct rq *rq, struct task_struct *p)
2315{
2316 unsigned long soft, hard;
2317
Jiri Slaby78d7d402010-03-05 13:42:54 -08002318 /* max may change after cur was read, this will be fixed next tick */
2319 soft = task_rlimit(p, RLIMIT_RTTIME);
2320 hard = task_rlimit_max(p, RLIMIT_RTTIME);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002321
2322 if (soft != RLIM_INFINITY) {
2323 unsigned long next;
2324
Ying Xue57d2aa02012-07-17 15:03:43 +08002325 if (p->rt.watchdog_stamp != jiffies) {
2326 p->rt.timeout++;
2327 p->rt.watchdog_stamp = jiffies;
2328 }
2329
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002330 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
Peter Zijlstra5a52dd52008-01-25 21:08:32 +01002331 if (p->rt.timeout > next)
Frank Mayharf06febc2008-09-12 09:54:39 -07002332 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002333 }
2334}
Steven Rostedtcb469842008-01-25 21:08:22 +01002335
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002336static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002337{
Colin Cross454c7992012-05-16 21:34:23 -07002338 struct sched_rt_entity *rt_se = &p->rt;
2339
Peter Zijlstra67e2be02007-12-20 15:01:17 +01002340 update_curr_rt(rq);
2341
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002342 watchdog(rq, p);
2343
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002344 /*
2345 * RR tasks need a special form of timeslice management.
2346 * FIFO tasks have no timeslices.
2347 */
2348 if (p->policy != SCHED_RR)
2349 return;
2350
Peter Zijlstrafa717062008-01-25 21:08:27 +01002351 if (--p->rt.time_slice)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002352 return;
2353
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002354 p->rt.time_slice = sched_rr_timeslice;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002355
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002356 /*
Li Bine9aa39b2013-10-21 20:15:43 +08002357 * Requeue to the end of queue if we (and all of our ancestors) are not
2358 * the only element on the queue
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002359 */
Colin Cross454c7992012-05-16 21:34:23 -07002360 for_each_sched_rt_entity(rt_se) {
2361 if (rt_se->run_list.prev != rt_se->run_list.next) {
2362 requeue_task_rt(rq, p, 0);
Kirill Tkhai8aa6f0e2014-09-22 22:36:43 +04002363 resched_curr(rq);
Colin Cross454c7992012-05-16 21:34:23 -07002364 return;
2365 }
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002366 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002367}
2368
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002369static void set_curr_task_rt(struct rq *rq)
2370{
2371 struct task_struct *p = rq->curr;
2372
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002373 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05002374
2375 /* The running task is never eligible for pushing */
2376 dequeue_pushable_task(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002377}
2378
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07002379static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00002380{
2381 /*
2382 * Time slice is 0 for SCHED_FIFO tasks
2383 */
2384 if (task->policy == SCHED_RR)
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002385 return sched_rr_timeslice;
Peter Williams0d721ce2009-09-21 01:31:53 +00002386 else
2387 return 0;
2388}
2389
Peter Zijlstra029632f2011-10-25 10:00:11 +02002390const struct sched_class rt_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002391 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002392 .enqueue_task = enqueue_task_rt,
2393 .dequeue_task = dequeue_task_rt,
2394 .yield_task = yield_task_rt,
2395
2396 .check_preempt_curr = check_preempt_curr_rt,
2397
2398 .pick_next_task = pick_next_task_rt,
2399 .put_prev_task = put_prev_task_rt,
2400
Peter Williams681f3e62007-10-24 18:23:51 +02002401#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08002402 .select_task_rq = select_task_rq_rt,
2403
Peter Zijlstra6c370672015-05-15 17:43:36 +02002404 .set_cpus_allowed = set_cpus_allowed_common,
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002405 .rq_online = rq_online_rt,
2406 .rq_offline = rq_offline_rt,
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002407 .task_woken = task_woken_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002408 .switched_from = switched_from_rt,
Peter Williams681f3e62007-10-24 18:23:51 +02002409#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002410
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002411 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002412 .task_tick = task_tick_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002413
Peter Williams0d721ce2009-09-21 01:31:53 +00002414 .get_rr_interval = get_rr_interval_rt,
2415
Steven Rostedtcb469842008-01-25 21:08:22 +01002416 .prio_changed = prio_changed_rt,
2417 .switched_to = switched_to_rt,
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002418
2419 .update_curr = update_curr_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002420};
Peter Zijlstraada18de2008-06-19 14:22:24 +02002421
2422#ifdef CONFIG_SCHED_DEBUG
2423extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2424
Peter Zijlstra029632f2011-10-25 10:00:11 +02002425void print_rt_stats(struct seq_file *m, int cpu)
Peter Zijlstraada18de2008-06-19 14:22:24 +02002426{
Cheng Xuec514c42011-05-14 14:20:02 +08002427 rt_rq_iter_t iter;
Peter Zijlstraada18de2008-06-19 14:22:24 +02002428 struct rt_rq *rt_rq;
2429
2430 rcu_read_lock();
Cheng Xuec514c42011-05-14 14:20:02 +08002431 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
Peter Zijlstraada18de2008-06-19 14:22:24 +02002432 print_rt_rq(m, cpu, rt_rq);
2433 rcu_read_unlock();
2434}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302435#endif /* CONFIG_SCHED_DEBUG */