blob: 9ab4d73e9cc95b1ddcd54499e7557fd51a64741f [file] [log] [blame]
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001/*
2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
3 * policies)
4 */
5
Peter Zijlstra029632f2011-10-25 10:00:11 +02006#include "sched.h"
7
8#include <linux/slab.h>
Steven Rostedtb6366f02015-03-18 14:49:46 -04009#include <linux/irq_work.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020010
Clark Williamsce0dbbb2013-02-07 09:47:04 -060011int sched_rr_timeslice = RR_TIMESLICE;
12
Peter Zijlstra029632f2011-10-25 10:00:11 +020013static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
14
15struct rt_bandwidth def_rt_bandwidth;
16
17static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
18{
19 struct rt_bandwidth *rt_b =
20 container_of(timer, struct rt_bandwidth, rt_period_timer);
Peter Zijlstra029632f2011-10-25 10:00:11 +020021 int idle = 0;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020022 int overrun;
Peter Zijlstra029632f2011-10-25 10:00:11 +020023
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020024 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020025 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020026 overrun = hrtimer_forward_now(timer, rt_b->rt_period);
Peter Zijlstra029632f2011-10-25 10:00:11 +020027 if (!overrun)
28 break;
29
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020030 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020031 idle = do_sched_rt_period_timer(rt_b, overrun);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020032 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020033 }
Peter Zijlstra4cfafd32015-05-14 12:23:11 +020034 if (idle)
35 rt_b->rt_period_active = 0;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020036 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020037
38 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
39}
40
41void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
42{
43 rt_b->rt_period = ns_to_ktime(period);
44 rt_b->rt_runtime = runtime;
45
46 raw_spin_lock_init(&rt_b->rt_runtime_lock);
47
48 hrtimer_init(&rt_b->rt_period_timer,
49 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
50 rt_b->rt_period_timer.function = sched_rt_period_timer;
51}
52
53static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
54{
55 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
56 return;
57
Peter Zijlstra029632f2011-10-25 10:00:11 +020058 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +020059 if (!rt_b->rt_period_active) {
60 rt_b->rt_period_active = 1;
Steven Rostedtc3a990d2016-02-16 18:37:46 -050061 /*
62 * SCHED_DEADLINE updates the bandwidth, as a run away
63 * RT task with a DL task could hog a CPU. But DL does
64 * not reset the period. If a deadline task was running
65 * without an RT task running, it can cause RT tasks to
66 * throttle when they start up. Kick the timer right away
67 * to update the period.
68 */
69 hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0));
Peter Zijlstra4cfafd32015-05-14 12:23:11 +020070 hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED);
71 }
Peter Zijlstra029632f2011-10-25 10:00:11 +020072 raw_spin_unlock(&rt_b->rt_runtime_lock);
73}
74
Abel Vesa07c54f72015-03-03 13:50:27 +020075void init_rt_rq(struct rt_rq *rt_rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +020076{
77 struct rt_prio_array *array;
78 int i;
79
80 array = &rt_rq->active;
81 for (i = 0; i < MAX_RT_PRIO; i++) {
82 INIT_LIST_HEAD(array->queue + i);
83 __clear_bit(i, array->bitmap);
84 }
85 /* delimiter for bitsearch: */
86 __set_bit(MAX_RT_PRIO, array->bitmap);
87
88#if defined CONFIG_SMP
89 rt_rq->highest_prio.curr = MAX_RT_PRIO;
90 rt_rq->highest_prio.next = MAX_RT_PRIO;
91 rt_rq->rt_nr_migratory = 0;
92 rt_rq->overloaded = 0;
93 plist_head_init(&rt_rq->pushable_tasks);
Steven Rostedtb6366f02015-03-18 14:49:46 -040094#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +040095 /* We start is dequeued state, because no RT tasks are queued */
96 rt_rq->rt_queued = 0;
Peter Zijlstra029632f2011-10-25 10:00:11 +020097
98 rt_rq->rt_time = 0;
99 rt_rq->rt_throttled = 0;
100 rt_rq->rt_runtime = 0;
101 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
102}
103
Gregory Haskins398a1532009-01-14 09:10:04 -0500104#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +0200105static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
106{
107 hrtimer_cancel(&rt_b->rt_period_timer);
108}
Gregory Haskins398a1532009-01-14 09:10:04 -0500109
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200110#define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
111
Peter Zijlstra8f488942009-07-24 12:25:30 +0200112static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
113{
114#ifdef CONFIG_SCHED_DEBUG
115 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
116#endif
117 return container_of(rt_se, struct task_struct, rt);
118}
119
Gregory Haskins398a1532009-01-14 09:10:04 -0500120static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
121{
122 return rt_rq->rq;
123}
124
125static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
126{
127 return rt_se->rt_rq;
128}
129
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400130static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
131{
132 struct rt_rq *rt_rq = rt_se->rt_rq;
133
134 return rt_rq->rq;
135}
136
Peter Zijlstra029632f2011-10-25 10:00:11 +0200137void free_rt_sched_group(struct task_group *tg)
138{
139 int i;
140
141 if (tg->rt_se)
142 destroy_rt_bandwidth(&tg->rt_bandwidth);
143
144 for_each_possible_cpu(i) {
145 if (tg->rt_rq)
146 kfree(tg->rt_rq[i]);
147 if (tg->rt_se)
148 kfree(tg->rt_se[i]);
149 }
150
151 kfree(tg->rt_rq);
152 kfree(tg->rt_se);
153}
154
155void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
156 struct sched_rt_entity *rt_se, int cpu,
157 struct sched_rt_entity *parent)
158{
159 struct rq *rq = cpu_rq(cpu);
160
161 rt_rq->highest_prio.curr = MAX_RT_PRIO;
162 rt_rq->rt_nr_boosted = 0;
163 rt_rq->rq = rq;
164 rt_rq->tg = tg;
165
166 tg->rt_rq[cpu] = rt_rq;
167 tg->rt_se[cpu] = rt_se;
168
169 if (!rt_se)
170 return;
171
172 if (!parent)
173 rt_se->rt_rq = &rq->rt;
174 else
175 rt_se->rt_rq = parent->my_q;
176
177 rt_se->my_q = rt_rq;
178 rt_se->parent = parent;
179 INIT_LIST_HEAD(&rt_se->run_list);
180}
181
182int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
183{
184 struct rt_rq *rt_rq;
185 struct sched_rt_entity *rt_se;
186 int i;
187
188 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
189 if (!tg->rt_rq)
190 goto err;
191 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
192 if (!tg->rt_se)
193 goto err;
194
195 init_rt_bandwidth(&tg->rt_bandwidth,
196 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
197
198 for_each_possible_cpu(i) {
199 rt_rq = kzalloc_node(sizeof(struct rt_rq),
200 GFP_KERNEL, cpu_to_node(i));
201 if (!rt_rq)
202 goto err;
203
204 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
205 GFP_KERNEL, cpu_to_node(i));
206 if (!rt_se)
207 goto err_free_rq;
208
Abel Vesa07c54f72015-03-03 13:50:27 +0200209 init_rt_rq(rt_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200210 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
211 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
212 }
213
214 return 1;
215
216err_free_rq:
217 kfree(rt_rq);
218err:
219 return 0;
220}
221
Gregory Haskins398a1532009-01-14 09:10:04 -0500222#else /* CONFIG_RT_GROUP_SCHED */
223
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200224#define rt_entity_is_task(rt_se) (1)
225
Peter Zijlstra8f488942009-07-24 12:25:30 +0200226static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
227{
228 return container_of(rt_se, struct task_struct, rt);
229}
230
Gregory Haskins398a1532009-01-14 09:10:04 -0500231static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
232{
233 return container_of(rt_rq, struct rq, rt);
234}
235
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400236static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
Gregory Haskins398a1532009-01-14 09:10:04 -0500237{
238 struct task_struct *p = rt_task_of(rt_se);
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400239
240 return task_rq(p);
241}
242
243static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
244{
245 struct rq *rq = rq_of_rt_se(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -0500246
247 return &rq->rt;
248}
249
Peter Zijlstra029632f2011-10-25 10:00:11 +0200250void free_rt_sched_group(struct task_group *tg) { }
251
252int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
253{
254 return 1;
255}
Gregory Haskins398a1532009-01-14 09:10:04 -0500256#endif /* CONFIG_RT_GROUP_SCHED */
257
Steven Rostedt4fd29172008-01-25 21:08:06 +0100258#ifdef CONFIG_SMP
Ingo Molnar84de4272008-01-25 21:08:15 +0100259
Peter Zijlstra8046d682015-06-11 14:46:40 +0200260static void pull_rt_task(struct rq *this_rq);
Peter Zijlstra38033c32014-01-23 20:32:21 +0100261
Peter Zijlstradc877342014-02-12 15:47:29 +0100262static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
263{
264 /* Try to pull RT tasks here if we lower this rq's prio */
265 return rq->rt.highest_prio.curr > prev->prio;
266}
267
Gregory Haskins637f5082008-01-25 21:08:18 +0100268static inline int rt_overloaded(struct rq *rq)
Steven Rostedt4fd29172008-01-25 21:08:06 +0100269{
Gregory Haskins637f5082008-01-25 21:08:18 +0100270 return atomic_read(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100271}
Ingo Molnar84de4272008-01-25 21:08:15 +0100272
Steven Rostedt4fd29172008-01-25 21:08:06 +0100273static inline void rt_set_overload(struct rq *rq)
274{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400275 if (!rq->online)
276 return;
277
Rusty Russellc6c49272008-11-25 02:35:05 +1030278 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100279 /*
280 * Make sure the mask is visible before we set
281 * the overload count. That is checked to determine
282 * if we should look at the mask. It would be a shame
283 * if we looked at the mask, but the mask was not
284 * updated yet.
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200285 *
286 * Matched by the barrier in pull_rt_task().
Steven Rostedt4fd29172008-01-25 21:08:06 +0100287 */
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200288 smp_wmb();
Gregory Haskins637f5082008-01-25 21:08:18 +0100289 atomic_inc(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100290}
Ingo Molnar84de4272008-01-25 21:08:15 +0100291
Steven Rostedt4fd29172008-01-25 21:08:06 +0100292static inline void rt_clear_overload(struct rq *rq)
293{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400294 if (!rq->online)
295 return;
296
Steven Rostedt4fd29172008-01-25 21:08:06 +0100297 /* the order here really doesn't matter */
Gregory Haskins637f5082008-01-25 21:08:18 +0100298 atomic_dec(&rq->rd->rto_count);
Rusty Russellc6c49272008-11-25 02:35:05 +1030299 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100300}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100301
Gregory Haskins398a1532009-01-14 09:10:04 -0500302static void update_rt_migration(struct rt_rq *rt_rq)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100303{
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200304 if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
Gregory Haskins398a1532009-01-14 09:10:04 -0500305 if (!rt_rq->overloaded) {
306 rt_set_overload(rq_of_rt_rq(rt_rq));
307 rt_rq->overloaded = 1;
Gregory Haskinscdc8eb92008-01-25 21:08:23 +0100308 }
Gregory Haskins398a1532009-01-14 09:10:04 -0500309 } else if (rt_rq->overloaded) {
310 rt_clear_overload(rq_of_rt_rq(rt_rq));
311 rt_rq->overloaded = 0;
Gregory Haskins637f5082008-01-25 21:08:18 +0100312 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100313}
Steven Rostedt4fd29172008-01-25 21:08:06 +0100314
Gregory Haskins398a1532009-01-14 09:10:04 -0500315static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100316{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200317 struct task_struct *p;
318
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200319 if (!rt_entity_is_task(rt_se))
320 return;
321
Peter Zijlstra29baa742012-04-23 12:11:21 +0200322 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200323 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
324
325 rt_rq->rt_nr_total++;
Thomas Gleixner50605ff2016-05-11 14:23:31 +0200326 if (tsk_nr_cpus_allowed(p) > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500327 rt_rq->rt_nr_migratory++;
328
329 update_rt_migration(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100330}
331
Gregory Haskins398a1532009-01-14 09:10:04 -0500332static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
333{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200334 struct task_struct *p;
335
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200336 if (!rt_entity_is_task(rt_se))
337 return;
338
Peter Zijlstra29baa742012-04-23 12:11:21 +0200339 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200340 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
341
342 rt_rq->rt_nr_total--;
Thomas Gleixner50605ff2016-05-11 14:23:31 +0200343 if (tsk_nr_cpus_allowed(p) > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500344 rt_rq->rt_nr_migratory--;
345
346 update_rt_migration(rt_rq);
347}
348
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400349static inline int has_pushable_tasks(struct rq *rq)
350{
351 return !plist_head_empty(&rq->rt.pushable_tasks);
352}
353
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +0200354static DEFINE_PER_CPU(struct callback_head, rt_push_head);
355static DEFINE_PER_CPU(struct callback_head, rt_pull_head);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200356
357static void push_rt_tasks(struct rq *);
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +0200358static void pull_rt_task(struct rq *);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200359
360static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100361{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200362 if (!has_pushable_tasks(rq))
363 return;
364
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +0200365 queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
366}
367
368static inline void queue_pull_task(struct rq *rq)
369{
370 queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
Peter Zijlstradc877342014-02-12 15:47:29 +0100371}
372
Gregory Haskins917b6272008-12-29 09:39:53 -0500373static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
374{
375 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
376 plist_node_init(&p->pushable_tasks, p->prio);
377 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400378
379 /* Update the highest prio pushable task */
380 if (p->prio < rq->rt.highest_prio.next)
381 rq->rt.highest_prio.next = p->prio;
Gregory Haskins917b6272008-12-29 09:39:53 -0500382}
383
384static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
385{
386 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
Gregory Haskins917b6272008-12-29 09:39:53 -0500387
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400388 /* Update the new highest prio pushable task */
389 if (has_pushable_tasks(rq)) {
390 p = plist_first_entry(&rq->rt.pushable_tasks,
391 struct task_struct, pushable_tasks);
392 rq->rt.highest_prio.next = p->prio;
393 } else
394 rq->rt.highest_prio.next = MAX_RT_PRIO;
Ingo Molnarbcf08df2008-04-19 12:11:10 +0200395}
396
Gregory Haskins917b6272008-12-29 09:39:53 -0500397#else
398
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100399static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
400{
401}
402
403static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
404{
405}
406
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500407static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100408void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
409{
410}
411
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500412static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100413void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
414{
415}
Gregory Haskins917b6272008-12-29 09:39:53 -0500416
Peter Zijlstradc877342014-02-12 15:47:29 +0100417static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
418{
419 return false;
420}
421
Peter Zijlstra8046d682015-06-11 14:46:40 +0200422static inline void pull_rt_task(struct rq *this_rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100423{
Peter Zijlstradc877342014-02-12 15:47:29 +0100424}
425
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200426static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100427{
428}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200429#endif /* CONFIG_SMP */
430
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400431static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
432static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
433
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100434static inline int on_rt_rq(struct sched_rt_entity *rt_se)
435{
Peter Zijlstraff77e462016-01-18 15:27:07 +0100436 return rt_se->on_rq;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100437}
438
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100439#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100440
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100441static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100442{
443 if (!rt_rq->tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100444 return RUNTIME_INF;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100445
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200446 return rt_rq->rt_runtime;
447}
448
449static inline u64 sched_rt_period(struct rt_rq *rt_rq)
450{
451 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100452}
453
Cheng Xuec514c42011-05-14 14:20:02 +0800454typedef struct task_group *rt_rq_iter_t;
455
Yong Zhang1c09ab02011-06-28 10:51:31 +0800456static inline struct task_group *next_task_group(struct task_group *tg)
457{
458 do {
459 tg = list_entry_rcu(tg->list.next,
460 typeof(struct task_group), list);
461 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
462
463 if (&tg->list == &task_groups)
464 tg = NULL;
465
466 return tg;
467}
468
469#define for_each_rt_rq(rt_rq, iter, rq) \
470 for (iter = container_of(&task_groups, typeof(*iter), list); \
471 (iter = next_task_group(iter)) && \
472 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
Cheng Xuec514c42011-05-14 14:20:02 +0800473
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100474#define for_each_sched_rt_entity(rt_se) \
475 for (; rt_se; rt_se = rt_se->parent)
476
477static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
478{
479 return rt_se->my_q;
480}
481
Peter Zijlstraff77e462016-01-18 15:27:07 +0100482static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
483static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100484
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100485static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100486{
Dario Faggiolif6121f42008-10-03 17:40:46 +0200487 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
Kirill Tkhai88751252014-06-29 00:03:57 +0400488 struct rq *rq = rq_of_rt_rq(rt_rq);
Yong Zhang74b7eb52010-01-29 14:57:52 +0800489 struct sched_rt_entity *rt_se;
490
Kirill Tkhai88751252014-06-29 00:03:57 +0400491 int cpu = cpu_of(rq);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530492
493 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100494
Dario Faggiolif6121f42008-10-03 17:40:46 +0200495 if (rt_rq->rt_nr_running) {
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400496 if (!rt_se)
497 enqueue_top_rt_rq(rt_rq);
498 else if (!on_rt_rq(rt_se))
Peter Zijlstraff77e462016-01-18 15:27:07 +0100499 enqueue_rt_entity(rt_se, 0);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400500
Gregory Haskinse864c492008-12-29 09:39:49 -0500501 if (rt_rq->highest_prio.curr < curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +0400502 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100503 }
504}
505
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100506static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100507{
Yong Zhang74b7eb52010-01-29 14:57:52 +0800508 struct sched_rt_entity *rt_se;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530509 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
Yong Zhang74b7eb52010-01-29 14:57:52 +0800510
Balbir Singh0c3b9162011-03-03 17:04:35 +0530511 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100512
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400513 if (!rt_se)
514 dequeue_top_rt_rq(rt_rq);
515 else if (on_rt_rq(rt_se))
Peter Zijlstraff77e462016-01-18 15:27:07 +0100516 dequeue_rt_entity(rt_se, 0);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100517}
518
Kirill Tkhai46383642014-03-15 02:15:07 +0400519static inline int rt_rq_throttled(struct rt_rq *rt_rq)
520{
521 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
522}
523
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100524static int rt_se_boosted(struct sched_rt_entity *rt_se)
525{
526 struct rt_rq *rt_rq = group_rt_rq(rt_se);
527 struct task_struct *p;
528
529 if (rt_rq)
530 return !!rt_rq->rt_nr_boosted;
531
532 p = rt_task_of(rt_se);
533 return p->prio != p->normal_prio;
534}
535
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200536#ifdef CONFIG_SMP
Rusty Russellc6c49272008-11-25 02:35:05 +1030537static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200538{
Nathan Zimmer424c93f2013-05-09 11:24:03 -0500539 return this_rq()->rd->span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200540}
541#else
Rusty Russellc6c49272008-11-25 02:35:05 +1030542static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200543{
Rusty Russellc6c49272008-11-25 02:35:05 +1030544 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200545}
546#endif
547
548static inline
549struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
550{
551 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
552}
553
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200554static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
555{
556 return &rt_rq->tg->rt_bandwidth;
557}
558
Dhaval Giani55e12e52008-06-24 23:39:43 +0530559#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100560
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100561static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100562{
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200563 return rt_rq->rt_runtime;
564}
565
566static inline u64 sched_rt_period(struct rt_rq *rt_rq)
567{
568 return ktime_to_ns(def_rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100569}
570
Cheng Xuec514c42011-05-14 14:20:02 +0800571typedef struct rt_rq *rt_rq_iter_t;
572
573#define for_each_rt_rq(rt_rq, iter, rq) \
574 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
575
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100576#define for_each_sched_rt_entity(rt_se) \
577 for (; rt_se; rt_se = NULL)
578
579static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
580{
581 return NULL;
582}
583
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100584static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100585{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400586 struct rq *rq = rq_of_rt_rq(rt_rq);
587
588 if (!rt_rq->rt_nr_running)
589 return;
590
591 enqueue_top_rt_rq(rt_rq);
Kirill Tkhai88751252014-06-29 00:03:57 +0400592 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100593}
594
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100595static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100596{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400597 dequeue_top_rt_rq(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100598}
599
Kirill Tkhai46383642014-03-15 02:15:07 +0400600static inline int rt_rq_throttled(struct rt_rq *rt_rq)
601{
602 return rt_rq->rt_throttled;
603}
604
Rusty Russellc6c49272008-11-25 02:35:05 +1030605static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200606{
Rusty Russellc6c49272008-11-25 02:35:05 +1030607 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200608}
609
610static inline
611struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
612{
613 return &cpu_rq(cpu)->rt;
614}
615
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200616static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
617{
618 return &def_rt_bandwidth;
619}
620
Dhaval Giani55e12e52008-06-24 23:39:43 +0530621#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100622
Juri Lellifaa59932014-02-21 11:37:15 +0100623bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
624{
625 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
626
627 return (hrtimer_active(&rt_b->rt_period_timer) ||
628 rt_rq->rt_time < rt_b->rt_runtime);
629}
630
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200631#ifdef CONFIG_SMP
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200632/*
633 * We ran out of runtime, see if we can borrow some from our neighbours.
634 */
Juri Lelli269b26a2015-09-02 11:01:36 +0100635static void do_balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200636{
637 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
Shawn Bohreraa7f6732013-01-14 11:55:31 -0600638 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
Juri Lelli269b26a2015-09-02 11:01:36 +0100639 int i, weight;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200640 u64 rt_period;
641
Rusty Russellc6c49272008-11-25 02:35:05 +1030642 weight = cpumask_weight(rd->span);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200643
Thomas Gleixner0986b112009-11-17 15:32:06 +0100644 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200645 rt_period = ktime_to_ns(rt_b->rt_period);
Rusty Russellc6c49272008-11-25 02:35:05 +1030646 for_each_cpu(i, rd->span) {
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200647 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
648 s64 diff;
649
650 if (iter == rt_rq)
651 continue;
652
Thomas Gleixner0986b112009-11-17 15:32:06 +0100653 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200654 /*
655 * Either all rqs have inf runtime and there's nothing to steal
656 * or __disable_runtime() below sets a specific rq to inf to
657 * indicate its been disabled and disalow stealing.
658 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200659 if (iter->rt_runtime == RUNTIME_INF)
660 goto next;
661
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200662 /*
663 * From runqueues with spare time, take 1/n part of their
664 * spare time, but no more than our period.
665 */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200666 diff = iter->rt_runtime - iter->rt_time;
667 if (diff > 0) {
Peter Zijlstra58838cf2008-07-24 12:43:13 +0200668 diff = div_u64((u64)diff, weight);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200669 if (rt_rq->rt_runtime + diff > rt_period)
670 diff = rt_period - rt_rq->rt_runtime;
671 iter->rt_runtime -= diff;
672 rt_rq->rt_runtime += diff;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200673 if (rt_rq->rt_runtime == rt_period) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100674 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200675 break;
676 }
677 }
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200678next:
Thomas Gleixner0986b112009-11-17 15:32:06 +0100679 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200680 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100681 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200682}
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200683
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200684/*
685 * Ensure this RQ takes back all the runtime it lend to its neighbours.
686 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200687static void __disable_runtime(struct rq *rq)
688{
689 struct root_domain *rd = rq->rd;
Cheng Xuec514c42011-05-14 14:20:02 +0800690 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200691 struct rt_rq *rt_rq;
692
693 if (unlikely(!scheduler_running))
694 return;
695
Cheng Xuec514c42011-05-14 14:20:02 +0800696 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200697 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
698 s64 want;
699 int i;
700
Thomas Gleixner0986b112009-11-17 15:32:06 +0100701 raw_spin_lock(&rt_b->rt_runtime_lock);
702 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200703 /*
704 * Either we're all inf and nobody needs to borrow, or we're
705 * already disabled and thus have nothing to do, or we have
706 * exactly the right amount of runtime to take out.
707 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200708 if (rt_rq->rt_runtime == RUNTIME_INF ||
709 rt_rq->rt_runtime == rt_b->rt_runtime)
710 goto balanced;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100711 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200712
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200713 /*
714 * Calculate the difference between what we started out with
715 * and what we current have, that's the amount of runtime
716 * we lend and now have to reclaim.
717 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200718 want = rt_b->rt_runtime - rt_rq->rt_runtime;
719
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200720 /*
721 * Greedy reclaim, take back as much as we can.
722 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030723 for_each_cpu(i, rd->span) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200724 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
725 s64 diff;
726
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200727 /*
728 * Can't reclaim from ourselves or disabled runqueues.
729 */
Peter Zijlstraf1679d02008-08-14 15:49:00 +0200730 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200731 continue;
732
Thomas Gleixner0986b112009-11-17 15:32:06 +0100733 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200734 if (want > 0) {
735 diff = min_t(s64, iter->rt_runtime, want);
736 iter->rt_runtime -= diff;
737 want -= diff;
738 } else {
739 iter->rt_runtime -= want;
740 want -= want;
741 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100742 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200743
744 if (!want)
745 break;
746 }
747
Thomas Gleixner0986b112009-11-17 15:32:06 +0100748 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200749 /*
750 * We cannot be left wanting - that would mean some runtime
751 * leaked out of the system.
752 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200753 BUG_ON(want);
754balanced:
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200755 /*
756 * Disable all the borrow logic by pretending we have inf
757 * runtime - in which case borrowing doesn't make sense.
758 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200759 rt_rq->rt_runtime = RUNTIME_INF;
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -0700760 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100761 raw_spin_unlock(&rt_rq->rt_runtime_lock);
762 raw_spin_unlock(&rt_b->rt_runtime_lock);
Kirill Tkhai99b62562014-06-25 12:19:48 +0400763
764 /* Make rt_rq available for pick_next_task() */
765 sched_rt_rq_enqueue(rt_rq);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200766 }
767}
768
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200769static void __enable_runtime(struct rq *rq)
770{
Cheng Xuec514c42011-05-14 14:20:02 +0800771 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200772 struct rt_rq *rt_rq;
773
774 if (unlikely(!scheduler_running))
775 return;
776
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200777 /*
778 * Reset each runqueue's bandwidth settings
779 */
Cheng Xuec514c42011-05-14 14:20:02 +0800780 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200781 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
782
Thomas Gleixner0986b112009-11-17 15:32:06 +0100783 raw_spin_lock(&rt_b->rt_runtime_lock);
784 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200785 rt_rq->rt_runtime = rt_b->rt_runtime;
786 rt_rq->rt_time = 0;
Zhang, Yanminbaf25732008-09-09 11:26:33 +0800787 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100788 raw_spin_unlock(&rt_rq->rt_runtime_lock);
789 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200790 }
791}
792
Juri Lelli269b26a2015-09-02 11:01:36 +0100793static void balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraeff65492008-06-19 14:22:26 +0200794{
Peter Zijlstra4a6184c2011-10-06 22:39:14 +0200795 if (!sched_feat(RT_RUNTIME_SHARE))
Juri Lelli269b26a2015-09-02 11:01:36 +0100796 return;
Peter Zijlstra4a6184c2011-10-06 22:39:14 +0200797
Peter Zijlstraeff65492008-06-19 14:22:26 +0200798 if (rt_rq->rt_time > rt_rq->rt_runtime) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100799 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Juri Lelli269b26a2015-09-02 11:01:36 +0100800 do_balance_runtime(rt_rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100801 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200802 }
Peter Zijlstraeff65492008-06-19 14:22:26 +0200803}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530804#else /* !CONFIG_SMP */
Juri Lelli269b26a2015-09-02 11:01:36 +0100805static inline void balance_runtime(struct rt_rq *rt_rq) {}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530806#endif /* CONFIG_SMP */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100807
808static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
809{
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200810 int i, idle = 1, throttled = 0;
Rusty Russellc6c49272008-11-25 02:35:05 +1030811 const struct cpumask *span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200812
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200813 span = sched_rt_period_mask();
Mike Galbraithe221d022012-08-07 10:02:38 +0200814#ifdef CONFIG_RT_GROUP_SCHED
815 /*
816 * FIXME: isolated CPUs should really leave the root task group,
817 * whether they are isolcpus or were isolated via cpusets, lest
818 * the timer run on a CPU which does not service all runqueues,
819 * potentially leaving other CPUs indefinitely throttled. If
820 * isolation is really required, the user will turn the throttle
821 * off to kill the perturbations it causes anyway. Meanwhile,
822 * this maintains functionality for boot and/or troubleshooting.
823 */
824 if (rt_b == &root_task_group.rt_bandwidth)
825 span = cpu_online_mask;
826#endif
Rusty Russellc6c49272008-11-25 02:35:05 +1030827 for_each_cpu(i, span) {
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200828 int enqueue = 0;
829 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
830 struct rq *rq = rq_of_rt_rq(rt_rq);
831
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100832 raw_spin_lock(&rq->lock);
Davidlohr Bueso3d063252018-04-02 09:49:54 -0700833 update_rq_clock(rq);
834
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
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100891static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100892{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100893 u64 runtime = sched_rt_runtime(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100894
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100895 if (rt_rq->rt_throttled)
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100896 return rt_rq_throttled(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100897
Shan Hai5b680fd2011-11-29 11:03:56 +0800898 if (runtime >= sched_rt_period(rt_rq))
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200899 return 0;
900
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200901 balance_runtime(rt_rq);
902 runtime = sched_rt_runtime(rt_rq);
903 if (runtime == RUNTIME_INF)
904 return 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200905
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100906 if (rt_rq->rt_time > runtime) {
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200907 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
908
909 /*
910 * Don't actually throttle groups that have no runtime assigned
911 * but accrue some time due to boosting.
912 */
913 if (likely(rt_b->rt_runtime)) {
914 rt_rq->rt_throttled = 1;
John Stultzc2248152014-06-04 16:11:41 -0700915 printk_deferred_once("sched: RT throttling activated\n");
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200916 } else {
917 /*
918 * In case we did anyway, make it go away,
919 * replenishment is a joke, since it will replenish us
920 * with exactly 0 ns.
921 */
922 rt_rq->rt_time = 0;
923 }
924
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100925 if (rt_rq_throttled(rt_rq)) {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100926 sched_rt_rq_dequeue(rt_rq);
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100927 return 1;
928 }
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100929 }
930
931 return 0;
932}
933
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200934/*
935 * Update the current task's runtime statistics. Skip current tasks that
936 * are not in our scheduling class.
937 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200938static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200939{
940 struct task_struct *curr = rq->curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100941 struct sched_rt_entity *rt_se = &curr->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200942 u64 delta_exec;
943
Peter Zijlstra06c3bc62011-02-02 13:19:48 +0100944 if (curr->sched_class != &rt_sched_class)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200945 return;
946
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200947 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Kirill Tkhaifc79e242013-01-30 16:50:36 +0400948 if (unlikely((s64)delta_exec <= 0))
949 return;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200950
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200951 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +0200952 cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_RT);
Wanpeng Li594dd292016-04-22 17:07:24 +0800953
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200954 schedstat_set(curr->se.statistics.exec_max,
955 max(curr->se.statistics.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200956
957 curr->se.sum_exec_runtime += delta_exec;
Frank Mayharf06febc2008-09-12 09:54:39 -0700958 account_group_exec_runtime(curr, delta_exec);
959
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200960 curr->se.exec_start = rq_clock_task(rq);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100961 cpuacct_charge(curr, delta_exec);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100962
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200963 sched_rt_avg_update(rq, delta_exec);
964
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200965 if (!rt_bandwidth_enabled())
966 return;
967
Dhaval Giani354d60c2008-04-19 19:44:59 +0200968 for_each_sched_rt_entity(rt_se) {
Giedrius Rekasius0b079392014-05-25 15:23:31 +0100969 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
Dhaval Giani354d60c2008-04-19 19:44:59 +0200970
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200971 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100972 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200973 rt_rq->rt_time += delta_exec;
974 if (sched_rt_runtime_exceeded(rt_rq))
Kirill Tkhai88751252014-06-29 00:03:57 +0400975 resched_curr(rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100976 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200977 }
Dhaval Giani354d60c2008-04-19 19:44:59 +0200978 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200979}
980
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400981static void
982dequeue_top_rt_rq(struct rt_rq *rt_rq)
983{
984 struct rq *rq = rq_of_rt_rq(rt_rq);
985
986 BUG_ON(&rq->rt != rt_rq);
987
988 if (!rt_rq->rt_queued)
989 return;
990
991 BUG_ON(!rq->nr_running);
992
Kirill Tkhai72465442014-05-09 03:00:14 +0400993 sub_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400994 rt_rq->rt_queued = 0;
995}
996
997static void
998enqueue_top_rt_rq(struct rt_rq *rt_rq)
999{
1000 struct rq *rq = rq_of_rt_rq(rt_rq);
1001
1002 BUG_ON(&rq->rt != rt_rq);
1003
1004 if (rt_rq->rt_queued)
1005 return;
1006 if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
1007 return;
1008
Kirill Tkhai72465442014-05-09 03:00:14 +04001009 add_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001010 rt_rq->rt_queued = 1;
1011}
1012
Gregory Haskins398a1532009-01-14 09:10:04 -05001013#if defined CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05001014
Gregory Haskins398a1532009-01-14 09:10:04 -05001015static void
1016inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001017{
Gregory Haskins4d984272008-12-29 09:39:49 -05001018 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins4d984272008-12-29 09:39:49 -05001019
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001020#ifdef CONFIG_RT_GROUP_SCHED
1021 /*
1022 * Change rq's cpupri only if rt_rq is the top queue.
1023 */
1024 if (&rq->rt != rt_rq)
1025 return;
1026#endif
Steven Rostedt5181f4a42011-06-16 21:55:23 -04001027 if (rq->online && prio < prev_prio)
1028 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
Steven Rostedt63489e42008-01-25 21:08:03 +01001029}
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001030
Gregory Haskins398a1532009-01-14 09:10:04 -05001031static void
1032dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001033{
Gregory Haskins4d984272008-12-29 09:39:49 -05001034 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001035
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001036#ifdef CONFIG_RT_GROUP_SCHED
1037 /*
1038 * Change rq's cpupri only if rt_rq is the top queue.
1039 */
1040 if (&rq->rt != rt_rq)
1041 return;
1042#endif
Gregory Haskins398a1532009-01-14 09:10:04 -05001043 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1044 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1045}
1046
1047#else /* CONFIG_SMP */
1048
1049static inline
1050void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1051static inline
1052void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1053
1054#endif /* CONFIG_SMP */
1055
Steven Rostedt63489e42008-01-25 21:08:03 +01001056#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001057static void
1058inc_rt_prio(struct rt_rq *rt_rq, int prio)
1059{
1060 int prev_prio = rt_rq->highest_prio.curr;
Steven Rostedt63489e42008-01-25 21:08:03 +01001061
Gregory Haskins398a1532009-01-14 09:10:04 -05001062 if (prio < prev_prio)
1063 rt_rq->highest_prio.curr = prio;
1064
1065 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1066}
1067
1068static void
1069dec_rt_prio(struct rt_rq *rt_rq, int prio)
1070{
1071 int prev_prio = rt_rq->highest_prio.curr;
1072
1073 if (rt_rq->rt_nr_running) {
1074
1075 WARN_ON(prio < prev_prio);
Gregory Haskinse864c492008-12-29 09:39:49 -05001076
1077 /*
Gregory Haskins398a1532009-01-14 09:10:04 -05001078 * This may have been our highest task, and therefore
1079 * we may have some recomputation to do
Gregory Haskinse864c492008-12-29 09:39:49 -05001080 */
Gregory Haskins398a1532009-01-14 09:10:04 -05001081 if (prio == prev_prio) {
Gregory Haskinse864c492008-12-29 09:39:49 -05001082 struct rt_prio_array *array = &rt_rq->active;
1083
1084 rt_rq->highest_prio.curr =
Steven Rostedt764a9d62008-01-25 21:08:04 +01001085 sched_find_first_bit(array->bitmap);
Gregory Haskinse864c492008-12-29 09:39:49 -05001086 }
1087
Steven Rostedt764a9d62008-01-25 21:08:04 +01001088 } else
Gregory Haskinse864c492008-12-29 09:39:49 -05001089 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001090
Gregory Haskins398a1532009-01-14 09:10:04 -05001091 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1092}
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001093
Gregory Haskins398a1532009-01-14 09:10:04 -05001094#else
1095
1096static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1097static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1098
1099#endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1100
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001101#ifdef CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001102
1103static void
1104inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1105{
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001106 if (rt_se_boosted(rt_se))
Steven Rostedt764a9d62008-01-25 21:08:04 +01001107 rt_rq->rt_nr_boosted++;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01001108
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001109 if (rt_rq->tg)
1110 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001111}
1112
1113static void
1114dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1115{
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001116 if (rt_se_boosted(rt_se))
1117 rt_rq->rt_nr_boosted--;
1118
1119 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
Gregory Haskins398a1532009-01-14 09:10:04 -05001120}
1121
1122#else /* CONFIG_RT_GROUP_SCHED */
1123
1124static void
1125inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1126{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001127 start_rt_bandwidth(&def_rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001128}
1129
1130static inline
1131void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1132
1133#endif /* CONFIG_RT_GROUP_SCHED */
1134
1135static inline
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001136unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1137{
1138 struct rt_rq *group_rq = group_rt_rq(rt_se);
1139
1140 if (group_rq)
1141 return group_rq->rt_nr_running;
1142 else
1143 return 1;
1144}
1145
1146static inline
Frederic Weisbecker01d36d02015-11-04 18:17:10 +01001147unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
1148{
1149 struct rt_rq *group_rq = group_rt_rq(rt_se);
1150 struct task_struct *tsk;
1151
1152 if (group_rq)
1153 return group_rq->rr_nr_running;
1154
1155 tsk = rt_task_of(rt_se);
1156
1157 return (tsk->policy == SCHED_RR) ? 1 : 0;
1158}
1159
1160static inline
Gregory Haskins398a1532009-01-14 09:10:04 -05001161void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1162{
1163 int prio = rt_se_prio(rt_se);
1164
1165 WARN_ON(!rt_prio(prio));
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001166 rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
Frederic Weisbecker01d36d02015-11-04 18:17:10 +01001167 rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -05001168
1169 inc_rt_prio(rt_rq, prio);
1170 inc_rt_migration(rt_se, rt_rq);
1171 inc_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001172}
1173
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001174static inline
1175void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1176{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001177 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001178 WARN_ON(!rt_rq->rt_nr_running);
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001179 rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
Frederic Weisbecker01d36d02015-11-04 18:17:10 +01001180 rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001181
Gregory Haskins398a1532009-01-14 09:10:04 -05001182 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1183 dec_rt_migration(rt_se, rt_rq);
1184 dec_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001185}
1186
Peter Zijlstraff77e462016-01-18 15:27:07 +01001187/*
1188 * Change rt_se->run_list location unless SAVE && !MOVE
1189 *
1190 * assumes ENQUEUE/DEQUEUE flags match
1191 */
1192static inline bool move_entity(unsigned int flags)
1193{
1194 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
1195 return false;
1196
1197 return true;
1198}
1199
1200static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
1201{
1202 list_del_init(&rt_se->run_list);
1203
1204 if (list_empty(array->queue + rt_se_prio(rt_se)))
1205 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1206
1207 rt_se->on_list = 0;
1208}
1209
1210static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001211{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001212 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1213 struct rt_prio_array *array = &rt_rq->active;
1214 struct rt_rq *group_rq = group_rt_rq(rt_se);
Dmitry Adamushko20b63312008-06-11 00:58:30 +02001215 struct list_head *queue = array->queue + rt_se_prio(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001216
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001217 /*
1218 * Don't enqueue the group if its throttled, or when empty.
1219 * The latter is a consequence of the former when a child group
1220 * get throttled and the current group doesn't have any other
1221 * active members.
1222 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01001223 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) {
1224 if (rt_se->on_list)
1225 __delist_rt_entity(rt_se, array);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001226 return;
Peter Zijlstraff77e462016-01-18 15:27:07 +01001227 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001228
Peter Zijlstraff77e462016-01-18 15:27:07 +01001229 if (move_entity(flags)) {
1230 WARN_ON_ONCE(rt_se->on_list);
1231 if (flags & ENQUEUE_HEAD)
1232 list_add(&rt_se->run_list, queue);
1233 else
1234 list_add_tail(&rt_se->run_list, queue);
1235
1236 __set_bit(rt_se_prio(rt_se), array->bitmap);
1237 rt_se->on_list = 1;
1238 }
1239 rt_se->on_rq = 1;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001240
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001241 inc_rt_tasks(rt_se, rt_rq);
1242}
1243
Peter Zijlstraff77e462016-01-18 15:27:07 +01001244static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001245{
1246 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1247 struct rt_prio_array *array = &rt_rq->active;
1248
Peter Zijlstraff77e462016-01-18 15:27:07 +01001249 if (move_entity(flags)) {
1250 WARN_ON_ONCE(!rt_se->on_list);
1251 __delist_rt_entity(rt_se, array);
1252 }
1253 rt_se->on_rq = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001254
1255 dec_rt_tasks(rt_se, rt_rq);
1256}
1257
1258/*
1259 * Because the prio of an upper entry depends on the lower
1260 * entries, we must remove entries top - down.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001261 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01001262static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001263{
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001264 struct sched_rt_entity *back = NULL;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001265
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001266 for_each_sched_rt_entity(rt_se) {
1267 rt_se->back = back;
1268 back = rt_se;
1269 }
1270
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001271 dequeue_top_rt_rq(rt_rq_of_se(back));
1272
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001273 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1274 if (on_rt_rq(rt_se))
Peter Zijlstraff77e462016-01-18 15:27:07 +01001275 __dequeue_rt_entity(rt_se, flags);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001276 }
1277}
1278
Peter Zijlstraff77e462016-01-18 15:27:07 +01001279static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001280{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001281 struct rq *rq = rq_of_rt_se(rt_se);
1282
Peter Zijlstraff77e462016-01-18 15:27:07 +01001283 dequeue_rt_stack(rt_se, flags);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001284 for_each_sched_rt_entity(rt_se)
Peter Zijlstraff77e462016-01-18 15:27:07 +01001285 __enqueue_rt_entity(rt_se, flags);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001286 enqueue_top_rt_rq(&rq->rt);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001287}
1288
Peter Zijlstraff77e462016-01-18 15:27:07 +01001289static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001290{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001291 struct rq *rq = rq_of_rt_se(rt_se);
1292
Peter Zijlstraff77e462016-01-18 15:27:07 +01001293 dequeue_rt_stack(rt_se, flags);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001294
1295 for_each_sched_rt_entity(rt_se) {
1296 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1297
1298 if (rt_rq && rt_rq->rt_nr_running)
Peter Zijlstraff77e462016-01-18 15:27:07 +01001299 __enqueue_rt_entity(rt_se, flags);
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001300 }
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001301 enqueue_top_rt_rq(&rq->rt);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001302}
1303
1304/*
1305 * Adding/removing a task to/from a priority array:
1306 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001307static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001308enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001309{
1310 struct sched_rt_entity *rt_se = &p->rt;
1311
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001312 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001313 rt_se->timeout = 0;
1314
Peter Zijlstraff77e462016-01-18 15:27:07 +01001315 enqueue_rt_entity(rt_se, flags);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001316
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001317 if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001318 enqueue_pushable_task(rq, p);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001319}
1320
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001321static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001322{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001323 struct sched_rt_entity *rt_se = &p->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001324
1325 update_curr_rt(rq);
Peter Zijlstraff77e462016-01-18 15:27:07 +01001326 dequeue_rt_entity(rt_se, flags);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001327
Gregory Haskins917b6272008-12-29 09:39:53 -05001328 dequeue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001329}
1330
1331/*
Richard Weinberger60686312011-11-12 18:07:57 +01001332 * Put task to the head or the end of the run list without the overhead of
1333 * dequeue followed by enqueue.
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001334 */
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001335static void
1336requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001337{
Ingo Molnar1cdad712008-06-19 09:09:15 +02001338 if (on_rt_rq(rt_se)) {
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001339 struct rt_prio_array *array = &rt_rq->active;
1340 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1341
1342 if (head)
1343 list_move(&rt_se->run_list, queue);
1344 else
1345 list_move_tail(&rt_se->run_list, queue);
Ingo Molnar1cdad712008-06-19 09:09:15 +02001346 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001347}
1348
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001349static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001350{
1351 struct sched_rt_entity *rt_se = &p->rt;
1352 struct rt_rq *rt_rq;
1353
1354 for_each_sched_rt_entity(rt_se) {
1355 rt_rq = rt_rq_of_se(rt_se);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001356 requeue_rt_entity(rt_rq, rt_se, head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001357 }
1358}
1359
1360static void yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001361{
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001362 requeue_task_rt(rq, rq->curr, 0);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001363}
1364
Gregory Haskinse7693a32008-01-25 21:08:09 +01001365#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +01001366static int find_lowest_rq(struct task_struct *task);
1367
Peter Zijlstra0017d732010-03-24 18:34:10 +01001368static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01001369select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
Gregory Haskinse7693a32008-01-25 21:08:09 +01001370{
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001371 struct task_struct *curr;
1372 struct rq *rq;
Steven Rostedtc37495f2011-06-16 21:55:22 -04001373
1374 /* For anything but wake ups, just return the task_cpu */
1375 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1376 goto out;
1377
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001378 rq = cpu_rq(cpu);
1379
1380 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07001381 curr = READ_ONCE(rq->curr); /* unlocked access */
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001382
Gregory Haskins318e0892008-01-25 21:08:10 +01001383 /*
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001384 * If the current task on @p's runqueue is an RT task, then
Steven Rostedte1f47d82008-01-25 21:08:12 +01001385 * try to see if we can wake this RT task up on another
1386 * runqueue. Otherwise simply start this RT task
1387 * on its current runqueue.
1388 *
Steven Rostedt43fa5462010-09-20 22:40:03 -04001389 * We want to avoid overloading runqueues. If the woken
1390 * task is a higher priority, then it will stay on this CPU
1391 * and the lower prio task should be moved to another CPU.
1392 * Even though this will probably make the lower prio task
1393 * lose its cache, we do not want to bounce a higher task
1394 * around just because it gave up its CPU, perhaps for a
1395 * lock?
1396 *
1397 * For equal prio tasks, we just let the scheduler sort it out.
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001398 *
Gregory Haskins318e0892008-01-25 21:08:10 +01001399 * Otherwise, just let it ride on the affined RQ and the
1400 * post-schedule router will push the preempted task away
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001401 *
1402 * This test is optimistic, if we get it wrong the load-balancer
1403 * will have to sort it out.
Gregory Haskins318e0892008-01-25 21:08:10 +01001404 */
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001405 if (curr && unlikely(rt_task(curr)) &&
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001406 (tsk_nr_cpus_allowed(curr) < 2 ||
Shawn Bohrer6bfa6872013-10-04 14:24:53 -05001407 curr->prio <= p->prio)) {
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001408 int target = find_lowest_rq(p);
1409
Tim Chen80e3d872014-12-12 15:38:12 -08001410 /*
1411 * Don't bother moving it if the destination CPU is
1412 * not running a lower priority task.
1413 */
1414 if (target != -1 &&
1415 p->prio < cpu_rq(target)->rt.highest_prio.curr)
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001416 cpu = target;
1417 }
1418 rcu_read_unlock();
1419
Steven Rostedtc37495f2011-06-16 21:55:22 -04001420out:
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001421 return cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001422}
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001423
1424static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1425{
Wanpeng Li308a6232014-10-31 06:39:31 +08001426 /*
1427 * Current can't be migrated, useless to reschedule,
1428 * let's hope p can move out.
1429 */
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001430 if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
Wanpeng Li308a6232014-10-31 06:39:31 +08001431 !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001432 return;
1433
Wanpeng Li308a6232014-10-31 06:39:31 +08001434 /*
1435 * p is migratable, so let's not schedule it and
1436 * see if it is pushed or pulled somewhere else.
1437 */
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001438 if (tsk_nr_cpus_allowed(p) != 1
Rusty Russell13b8bd02009-03-25 15:01:22 +10301439 && cpupri_find(&rq->rd->cpupri, p, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001440 return;
1441
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001442 /*
1443 * There appears to be other cpus that can accept
1444 * current and none to run 'p', so lets reschedule
1445 * to try and push current away:
1446 */
1447 requeue_task_rt(rq, p, 1);
Kirill Tkhai88751252014-06-29 00:03:57 +04001448 resched_curr(rq);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001449}
1450
Gregory Haskinse7693a32008-01-25 21:08:09 +01001451#endif /* CONFIG_SMP */
1452
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001453/*
1454 * Preempt the current task with a newly woken task if needed:
1455 */
Peter Zijlstra7d478722009-09-14 19:55:44 +02001456static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001457{
Gregory Haskins45c01e82008-05-12 21:20:41 +02001458 if (p->prio < rq->curr->prio) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001459 resched_curr(rq);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001460 return;
1461 }
1462
1463#ifdef CONFIG_SMP
1464 /*
1465 * If:
1466 *
1467 * - the newly woken task is of equal priority to the current task
1468 * - the newly woken task is non-migratable while current is migratable
1469 * - current will be preempted on the next reschedule
1470 *
1471 * we should check to see if current can readily move to a different
1472 * cpu. If so, we will reschedule to allow the push logic to try
1473 * to move current somewhere else, making room for our non-migratable
1474 * task.
1475 */
Hillf Danton8dd0de82011-06-14 18:36:24 -04001476 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001477 check_preempt_equal_prio(rq, p);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001478#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001479}
1480
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001481static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1482 struct rt_rq *rt_rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001483{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001484 struct rt_prio_array *array = &rt_rq->active;
1485 struct sched_rt_entity *next = NULL;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001486 struct list_head *queue;
1487 int idx;
1488
1489 idx = sched_find_first_bit(array->bitmap);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001490 BUG_ON(idx >= MAX_RT_PRIO);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001491
1492 queue = array->queue + idx;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001493 next = list_entry(queue->next, struct sched_rt_entity, run_list);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001494
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001495 return next;
1496}
1497
Gregory Haskins917b6272008-12-29 09:39:53 -05001498static struct task_struct *_pick_next_task_rt(struct rq *rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001499{
1500 struct sched_rt_entity *rt_se;
1501 struct task_struct *p;
Peter Zijlstra606dba22012-02-11 06:05:00 +01001502 struct rt_rq *rt_rq = &rq->rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001503
1504 do {
1505 rt_se = pick_next_rt_entity(rq, rt_rq);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001506 BUG_ON(!rt_se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001507 rt_rq = group_rt_rq(rt_se);
1508 } while (rt_rq);
1509
1510 p = rt_task_of(rt_se);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001511 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001512
1513 return p;
1514}
1515
Peter Zijlstra606dba22012-02-11 06:05:00 +01001516static struct task_struct *
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001517pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie)
Gregory Haskins917b6272008-12-29 09:39:53 -05001518{
Peter Zijlstra606dba22012-02-11 06:05:00 +01001519 struct task_struct *p;
1520 struct rt_rq *rt_rq = &rq->rt;
1521
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001522 if (need_pull_rt_task(rq, prev)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001523 /*
1524 * This is OK, because current is on_cpu, which avoids it being
1525 * picked for load-balance and preemption/IRQs are still
1526 * disabled avoiding further scheduler activity on it and we're
1527 * being very careful to re-start the picking loop.
1528 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001529 lockdep_unpin_lock(&rq->lock, cookie);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001530 pull_rt_task(rq);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001531 lockdep_repin_lock(&rq->lock, cookie);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001532 /*
1533 * pull_rt_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001534 * means a dl or stop task can slip in, in which case we need
1535 * to re-start task selection.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001536 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001537 if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001538 rq->dl.dl_nr_running))
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001539 return RETRY_TASK;
1540 }
Peter Zijlstra38033c32014-01-23 20:32:21 +01001541
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001542 /*
1543 * We may dequeue prev's rt_rq in put_prev_task().
1544 * So, we update time before rt_nr_running check.
1545 */
1546 if (prev->sched_class == &rt_sched_class)
1547 update_curr_rt(rq);
1548
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001549 if (!rt_rq->rt_queued)
Peter Zijlstra606dba22012-02-11 06:05:00 +01001550 return NULL;
1551
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001552 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001553
1554 p = _pick_next_task_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001555
1556 /* The running task is never eligible for pushing */
Kirill Tkhaif3f17682014-09-12 17:42:01 +04001557 dequeue_pushable_task(rq, p);
Gregory Haskins917b6272008-12-29 09:39:53 -05001558
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02001559 queue_push_tasks(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04001560
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001561 return p;
1562}
1563
Ingo Molnar31ee5292007-08-09 11:16:49 +02001564static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001565{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +02001566 update_curr_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001567
1568 /*
1569 * The previous task needs to be made eligible for pushing
1570 * if it is still active
1571 */
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001572 if (on_rt_rq(&p->rt) && tsk_nr_cpus_allowed(p) > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001573 enqueue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001574}
1575
Peter Williams681f3e62007-10-24 18:23:51 +02001576#ifdef CONFIG_SMP
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001577
Steven Rostedte8fa1362008-01-25 21:08:05 +01001578/* Only try algorithms three times */
1579#define RT_MAX_TRIES 3
1580
Steven Rostedtf65eda42008-01-25 21:08:07 +01001581static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1582{
1583 if (!task_running(rq, p) &&
Kirill Tkhai60334ca2013-01-31 18:56:17 +04001584 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001585 return 1;
1586 return 0;
1587}
1588
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001589/*
1590 * Return the highest pushable rq's task, which is suitable to be executed
1591 * on the cpu, NULL otherwise
1592 */
1593static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001594{
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001595 struct plist_head *head = &rq->rt.pushable_tasks;
1596 struct task_struct *p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001597
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001598 if (!has_pushable_tasks(rq))
1599 return NULL;
Peter Zijlstra3d074672010-03-10 17:07:24 +01001600
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001601 plist_for_each_entry(p, head, pushable_tasks) {
1602 if (pick_rt_task(rq, p, cpu))
1603 return p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001604 }
1605
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001606 return NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001607}
1608
Rusty Russell0e3900e2008-11-25 02:35:13 +10301609static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001610
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001611static int find_lowest_rq(struct task_struct *task)
1612{
1613 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001614 struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001615 int this_cpu = smp_processor_id();
1616 int cpu = task_cpu(task);
1617
Steven Rostedt0da938c2011-06-14 18:36:25 -04001618 /* Make sure the mask is initialized first */
1619 if (unlikely(!lowest_mask))
1620 return -1;
1621
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001622 if (tsk_nr_cpus_allowed(task) == 1)
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001623 return -1; /* No other targets possible */
1624
1625 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
Gregory Haskins06f90db2008-01-25 21:08:13 +01001626 return -1; /* No targets found */
1627
1628 /*
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001629 * At this point we have built a mask of cpus representing the
1630 * lowest priority tasks in the system. Now we want to elect
1631 * the best one based on our affinity and topology.
1632 *
1633 * We prioritize the last cpu that the task executed on since
1634 * it is most likely cache-hot in that location.
1635 */
Rusty Russell96f874e2008-11-25 02:35:14 +10301636 if (cpumask_test_cpu(cpu, lowest_mask))
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001637 return cpu;
1638
1639 /*
1640 * Otherwise, we consult the sched_domains span maps to figure
1641 * out which cpu is logically closest to our hot cache data.
1642 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301643 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1644 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001645
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001646 rcu_read_lock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301647 for_each_domain(cpu, sd) {
1648 if (sd->flags & SD_WAKE_AFFINE) {
1649 int best_cpu;
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001650
Rusty Russelle2c88062009-11-03 14:53:15 +10301651 /*
1652 * "this_cpu" is cheaper to preempt than a
1653 * remote processor.
1654 */
1655 if (this_cpu != -1 &&
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001656 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1657 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301658 return this_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001659 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001660
Rusty Russelle2c88062009-11-03 14:53:15 +10301661 best_cpu = cpumask_first_and(lowest_mask,
1662 sched_domain_span(sd));
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001663 if (best_cpu < nr_cpu_ids) {
1664 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301665 return best_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001666 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001667 }
1668 }
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001669 rcu_read_unlock();
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001670
1671 /*
1672 * And finally, if there were no matches within the domains
1673 * just give the caller *something* to work with from the compatible
1674 * locations.
1675 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301676 if (this_cpu != -1)
1677 return this_cpu;
1678
1679 cpu = cpumask_any(lowest_mask);
1680 if (cpu < nr_cpu_ids)
1681 return cpu;
1682 return -1;
Gregory Haskins07b40322008-01-25 21:08:10 +01001683}
1684
Steven Rostedte8fa1362008-01-25 21:08:05 +01001685/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +01001686static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001687{
1688 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001689 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +01001690 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001691
1692 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +01001693 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001694
Gregory Haskins2de0b462008-01-25 21:08:10 +01001695 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +01001696 break;
1697
Gregory Haskins07b40322008-01-25 21:08:10 +01001698 lowest_rq = cpu_rq(cpu);
1699
Tim Chen80e3d872014-12-12 15:38:12 -08001700 if (lowest_rq->rt.highest_prio.curr <= task->prio) {
1701 /*
1702 * Target rq has tasks of equal or higher priority,
1703 * retrying does not release any lock and is unlikely
1704 * to yield a different result.
1705 */
1706 lowest_rq = NULL;
1707 break;
1708 }
1709
Steven Rostedte8fa1362008-01-25 21:08:05 +01001710 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +01001711 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +01001712 /*
1713 * We had to unlock the run queue. In
1714 * the mean time, task could have
1715 * migrated already or had its affinity changed.
1716 * Also make sure that it wasn't scheduled on its rq.
1717 */
Gregory Haskins07b40322008-01-25 21:08:10 +01001718 if (unlikely(task_rq(task) != rq ||
Rusty Russell96f874e2008-11-25 02:35:14 +10301719 !cpumask_test_cpu(lowest_rq->cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001720 tsk_cpus_allowed(task)) ||
Gregory Haskins07b40322008-01-25 21:08:10 +01001721 task_running(rq, task) ||
Xunlei Pang13b5ab02016-05-09 12:11:31 +08001722 !rt_task(task) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001723 !task_on_rq_queued(task))) {
Ingo Molnar4df64c02008-01-25 21:08:15 +01001724
Peter Zijlstra7f1b4392012-05-17 21:19:46 +02001725 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001726 lowest_rq = NULL;
1727 break;
1728 }
1729 }
1730
1731 /* If this rq is still suitable use it. */
Gregory Haskinse864c492008-12-29 09:39:49 -05001732 if (lowest_rq->rt.highest_prio.curr > task->prio)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001733 break;
1734
1735 /* try again */
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001736 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001737 lowest_rq = NULL;
1738 }
1739
1740 return lowest_rq;
1741}
1742
Gregory Haskins917b6272008-12-29 09:39:53 -05001743static struct task_struct *pick_next_pushable_task(struct rq *rq)
1744{
1745 struct task_struct *p;
1746
1747 if (!has_pushable_tasks(rq))
1748 return NULL;
1749
1750 p = plist_first_entry(&rq->rt.pushable_tasks,
1751 struct task_struct, pushable_tasks);
1752
1753 BUG_ON(rq->cpu != task_cpu(p));
1754 BUG_ON(task_current(rq, p));
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001755 BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
Gregory Haskins917b6272008-12-29 09:39:53 -05001756
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001757 BUG_ON(!task_on_rq_queued(p));
Gregory Haskins917b6272008-12-29 09:39:53 -05001758 BUG_ON(!rt_task(p));
1759
1760 return p;
1761}
1762
Steven Rostedte8fa1362008-01-25 21:08:05 +01001763/*
1764 * If the current CPU has more than one RT task, see if the non
1765 * running task can migrate over to a CPU that is running a task
1766 * of lesser priority.
1767 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001768static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001769{
1770 struct task_struct *next_task;
1771 struct rq *lowest_rq;
Hillf Danton311e8002011-06-16 21:55:20 -04001772 int ret = 0;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001773
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +01001774 if (!rq->rt.overloaded)
1775 return 0;
1776
Gregory Haskins917b6272008-12-29 09:39:53 -05001777 next_task = pick_next_pushable_task(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001778 if (!next_task)
1779 return 0;
1780
Peter Zijlstra49246272010-10-17 21:46:10 +02001781retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +01001782 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001783 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001784 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001785 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01001786
1787 /*
1788 * It's possible that the next_task slipped in of
1789 * higher priority than current. If that's the case
1790 * just reschedule current.
1791 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001792 if (unlikely(next_task->prio < rq->curr->prio)) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001793 resched_curr(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001794 return 0;
1795 }
1796
Gregory Haskins697f0a42008-01-25 21:08:09 +01001797 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +01001798 get_task_struct(next_task);
1799
1800 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001801 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001802 if (!lowest_rq) {
1803 struct task_struct *task;
1804 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001805 * find_lock_lowest_rq releases rq->lock
Gregory Haskins15635132008-12-29 09:39:53 -05001806 * so it is possible that next_task has migrated.
1807 *
1808 * We need to make sure that the task is still on the same
1809 * run-queue and is also still the next task eligible for
1810 * pushing.
Steven Rostedte8fa1362008-01-25 21:08:05 +01001811 */
Gregory Haskins917b6272008-12-29 09:39:53 -05001812 task = pick_next_pushable_task(rq);
Gregory Haskins15635132008-12-29 09:39:53 -05001813 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1814 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001815 * The task hasn't migrated, and is still the next
1816 * eligible task, but we failed to find a run-queue
1817 * to push it to. Do not retry in this case, since
1818 * other cpus will pull from us when ready.
Gregory Haskins15635132008-12-29 09:39:53 -05001819 */
Gregory Haskins15635132008-12-29 09:39:53 -05001820 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001821 }
Gregory Haskins917b6272008-12-29 09:39:53 -05001822
Gregory Haskins15635132008-12-29 09:39:53 -05001823 if (!task)
1824 /* No more tasks, just exit */
1825 goto out;
1826
Gregory Haskins917b6272008-12-29 09:39:53 -05001827 /*
Gregory Haskins15635132008-12-29 09:39:53 -05001828 * Something has shifted, try again.
Gregory Haskins917b6272008-12-29 09:39:53 -05001829 */
Gregory Haskins15635132008-12-29 09:39:53 -05001830 put_task_struct(next_task);
1831 next_task = task;
1832 goto retry;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001833 }
1834
Gregory Haskins697f0a42008-01-25 21:08:09 +01001835 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001836 set_task_cpu(next_task, lowest_rq->cpu);
1837 activate_task(lowest_rq, next_task, 0);
Hillf Danton311e8002011-06-16 21:55:20 -04001838 ret = 1;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001839
Kirill Tkhai88751252014-06-29 00:03:57 +04001840 resched_curr(lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001841
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001842 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001843
Steven Rostedte8fa1362008-01-25 21:08:05 +01001844out:
1845 put_task_struct(next_task);
1846
Hillf Danton311e8002011-06-16 21:55:20 -04001847 return ret;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001848}
1849
Steven Rostedte8fa1362008-01-25 21:08:05 +01001850static void push_rt_tasks(struct rq *rq)
1851{
1852 /* push_rt_task will return true if it moved an RT */
1853 while (push_rt_task(rq))
1854 ;
1855}
1856
Steven Rostedtb6366f02015-03-18 14:49:46 -04001857#ifdef HAVE_RT_PUSH_IPI
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001858
Steven Rostedtb6366f02015-03-18 14:49:46 -04001859/*
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001860 * When a high priority task schedules out from a CPU and a lower priority
1861 * task is scheduled in, a check is made to see if there's any RT tasks
1862 * on other CPUs that are waiting to run because a higher priority RT task
1863 * is currently running on its CPU. In this case, the CPU with multiple RT
1864 * tasks queued on it (overloaded) needs to be notified that a CPU has opened
1865 * up that may be able to run one of its non-running queued RT tasks.
Steven Rostedtb6366f02015-03-18 14:49:46 -04001866 *
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001867 * All CPUs with overloaded RT tasks need to be notified as there is currently
1868 * no way to know which of these CPUs have the highest priority task waiting
1869 * to run. Instead of trying to take a spinlock on each of these CPUs,
1870 * which has shown to cause large latency when done on machines with many
1871 * CPUs, sending an IPI to the CPUs to have them push off the overloaded
1872 * RT tasks waiting to run.
1873 *
1874 * Just sending an IPI to each of the CPUs is also an issue, as on large
1875 * count CPU machines, this can cause an IPI storm on a CPU, especially
1876 * if its the only CPU with multiple RT tasks queued, and a large number
1877 * of CPUs scheduling a lower priority task at the same time.
1878 *
1879 * Each root domain has its own irq work function that can iterate over
1880 * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
1881 * tassk must be checked if there's one or many CPUs that are lowering
1882 * their priority, there's a single irq work iterator that will try to
1883 * push off RT tasks that are waiting to run.
1884 *
1885 * When a CPU schedules a lower priority task, it will kick off the
1886 * irq work iterator that will jump to each CPU with overloaded RT tasks.
1887 * As it only takes the first CPU that schedules a lower priority task
1888 * to start the process, the rto_start variable is incremented and if
1889 * the atomic result is one, then that CPU will try to take the rto_lock.
1890 * This prevents high contention on the lock as the process handles all
1891 * CPUs scheduling lower priority tasks.
1892 *
1893 * All CPUs that are scheduling a lower priority task will increment the
1894 * rt_loop_next variable. This will make sure that the irq work iterator
1895 * checks all RT overloaded CPUs whenever a CPU schedules a new lower
1896 * priority task, even if the iterator is in the middle of a scan. Incrementing
1897 * the rt_loop_next will cause the iterator to perform another scan.
1898 *
Steven Rostedtb6366f02015-03-18 14:49:46 -04001899 */
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05001900static int rto_next_cpu(struct root_domain *rd)
Steven Rostedtb6366f02015-03-18 14:49:46 -04001901{
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001902 int next;
Steven Rostedtb6366f02015-03-18 14:49:46 -04001903 int cpu;
1904
Steven Rostedtb6366f02015-03-18 14:49:46 -04001905 /*
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001906 * When starting the IPI RT pushing, the rto_cpu is set to -1,
1907 * rt_next_cpu() will simply return the first CPU found in
1908 * the rto_mask.
1909 *
1910 * If rto_next_cpu() is called with rto_cpu is a valid cpu, it
1911 * will return the next CPU found in the rto_mask.
1912 *
1913 * If there are no more CPUs left in the rto_mask, then a check is made
1914 * against rto_loop and rto_loop_next. rto_loop is only updated with
1915 * the rto_lock held, but any CPU may increment the rto_loop_next
1916 * without any locking.
Steven Rostedtb6366f02015-03-18 14:49:46 -04001917 */
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001918 for (;;) {
Steven Rostedtb6366f02015-03-18 14:49:46 -04001919
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001920 /* When rto_cpu is -1 this acts like cpumask_first() */
1921 cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
1922
1923 rd->rto_cpu = cpu;
1924
1925 if (cpu < nr_cpu_ids)
1926 return cpu;
1927
1928 rd->rto_cpu = -1;
1929
Steven Rostedtb6366f02015-03-18 14:49:46 -04001930 /*
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001931 * ACQUIRE ensures we see the @rto_mask changes
1932 * made prior to the @next value observed.
1933 *
1934 * Matches WMB in rt_set_overload().
Steven Rostedtb6366f02015-03-18 14:49:46 -04001935 */
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001936 next = atomic_read_acquire(&rd->rto_loop_next);
Steven Rostedtb6366f02015-03-18 14:49:46 -04001937
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001938 if (rd->rto_loop == next)
1939 break;
1940
1941 rd->rto_loop = next;
1942 }
1943
1944 return -1;
Steven Rostedtb6366f02015-03-18 14:49:46 -04001945}
1946
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001947static inline bool rto_start_trylock(atomic_t *v)
Steven Rostedtb6366f02015-03-18 14:49:46 -04001948{
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001949 return !atomic_cmpxchg_acquire(v, 0, 1);
Steven Rostedtb6366f02015-03-18 14:49:46 -04001950}
1951
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001952static inline void rto_start_unlock(atomic_t *v)
1953{
1954 atomic_set_release(v, 0);
1955}
Steven Rostedtb6366f02015-03-18 14:49:46 -04001956
1957static void tell_cpu_to_push(struct rq *rq)
1958{
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001959 int cpu = -1;
Steven Rostedtb6366f02015-03-18 14:49:46 -04001960
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001961 /* Keep the loop going if the IPI is currently active */
1962 atomic_inc(&rq->rd->rto_loop_next);
Steven Rostedtb6366f02015-03-18 14:49:46 -04001963
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001964 /* Only one CPU can initiate a loop at a time */
1965 if (!rto_start_trylock(&rq->rd->rto_loop_start))
Steven Rostedtb6366f02015-03-18 14:49:46 -04001966 return;
1967
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001968 raw_spin_lock(&rq->rd->rto_lock);
Steven Rostedtb6366f02015-03-18 14:49:46 -04001969
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001970 /*
1971 * The rto_cpu is updated under the lock, if it has a valid cpu
1972 * then the IPI is still running and will continue due to the
1973 * update to loop_next, and nothing needs to be done here.
1974 * Otherwise it is finishing up and an ipi needs to be sent.
1975 */
1976 if (rq->rd->rto_cpu < 0)
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05001977 cpu = rto_next_cpu(rq->rd);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001978
1979 raw_spin_unlock(&rq->rd->rto_lock);
1980
1981 rto_start_unlock(&rq->rd->rto_loop_start);
1982
Steven Rostedt (VMware)a384e542018-01-23 20:45:38 -05001983 if (cpu >= 0) {
1984 /* Make sure the rd does not get freed while pushing */
1985 sched_get_rd(rq->rd);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001986 irq_work_queue_on(&rq->rd->rto_push_work, cpu);
Steven Rostedt (VMware)a384e542018-01-23 20:45:38 -05001987 }
Steven Rostedtb6366f02015-03-18 14:49:46 -04001988}
1989
1990/* Called from hardirq context */
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001991void rto_push_irq_work_func(struct irq_work *work)
Steven Rostedtb6366f02015-03-18 14:49:46 -04001992{
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05001993 struct root_domain *rd =
1994 container_of(work, struct root_domain, rto_push_work);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001995 struct rq *rq;
Steven Rostedtb6366f02015-03-18 14:49:46 -04001996 int cpu;
1997
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04001998 rq = this_rq();
Steven Rostedtb6366f02015-03-18 14:49:46 -04001999
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002000 /*
2001 * We do not need to grab the lock to check for has_pushable_tasks.
2002 * When it gets updated, a check is made if a push is possible.
2003 */
Steven Rostedtb6366f02015-03-18 14:49:46 -04002004 if (has_pushable_tasks(rq)) {
2005 raw_spin_lock(&rq->lock);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002006 push_rt_tasks(rq);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002007 raw_spin_unlock(&rq->lock);
2008 }
2009
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002010 raw_spin_lock(&rd->rto_lock);
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -04002011
Steven Rostedtb6366f02015-03-18 14:49:46 -04002012 /* Pass the IPI to the next rt overloaded queue */
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002013 cpu = rto_next_cpu(rd);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002014
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002015 raw_spin_unlock(&rd->rto_lock);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002016
Steven Rostedt (VMware)a384e542018-01-23 20:45:38 -05002017 if (cpu < 0) {
2018 sched_put_rd(rd);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002019 return;
Steven Rostedt (VMware)a384e542018-01-23 20:45:38 -05002020 }
Steven Rostedtb6366f02015-03-18 14:49:46 -04002021
Steven Rostedtb6366f02015-03-18 14:49:46 -04002022 /* Try the next RT overloaded CPU */
Steven Rostedt (VMware)1c679982018-01-23 20:45:37 -05002023 irq_work_queue_on(&rd->rto_push_work, cpu);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002024}
2025#endif /* HAVE_RT_PUSH_IPI */
2026
Peter Zijlstra8046d682015-06-11 14:46:40 +02002027static void pull_rt_task(struct rq *this_rq)
Steven Rostedtf65eda42008-01-25 21:08:07 +01002028{
Peter Zijlstra8046d682015-06-11 14:46:40 +02002029 int this_cpu = this_rq->cpu, cpu;
2030 bool resched = false;
Gregory Haskinsa8728942008-12-29 09:39:49 -05002031 struct task_struct *p;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002032 struct rq *src_rq;
Steven Rostedtd2668172017-12-02 13:04:54 -05002033 int rt_overload_count = rt_overloaded(this_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002034
Steven Rostedtd2668172017-12-02 13:04:54 -05002035 if (likely(!rt_overload_count))
Peter Zijlstra8046d682015-06-11 14:46:40 +02002036 return;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002037
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +02002038 /*
2039 * Match the barrier from rt_set_overloaded; this guarantees that if we
2040 * see overloaded we must also see the rto_mask bit.
2041 */
2042 smp_rmb();
2043
Steven Rostedtd2668172017-12-02 13:04:54 -05002044 /* If we are the only overloaded CPU do nothing */
2045 if (rt_overload_count == 1 &&
2046 cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
2047 return;
2048
Steven Rostedtb6366f02015-03-18 14:49:46 -04002049#ifdef HAVE_RT_PUSH_IPI
2050 if (sched_feat(RT_PUSH_IPI)) {
2051 tell_cpu_to_push(this_rq);
Peter Zijlstra8046d682015-06-11 14:46:40 +02002052 return;
Steven Rostedtb6366f02015-03-18 14:49:46 -04002053 }
2054#endif
2055
Rusty Russellc6c49272008-11-25 02:35:05 +10302056 for_each_cpu(cpu, this_rq->rd->rto_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01002057 if (this_cpu == cpu)
2058 continue;
2059
2060 src_rq = cpu_rq(cpu);
Gregory Haskins74ab8e42008-12-29 09:39:50 -05002061
2062 /*
2063 * Don't bother taking the src_rq->lock if the next highest
2064 * task is known to be lower-priority than our current task.
2065 * This may look racy, but if this value is about to go
2066 * logically higher, the src_rq will push this task away.
2067 * And if its going logically lower, we do not care
2068 */
2069 if (src_rq->rt.highest_prio.next >=
2070 this_rq->rt.highest_prio.curr)
2071 continue;
2072
Steven Rostedtf65eda42008-01-25 21:08:07 +01002073 /*
2074 * We can potentially drop this_rq's lock in
2075 * double_lock_balance, and another CPU could
Gregory Haskinsa8728942008-12-29 09:39:49 -05002076 * alter this_rq
Steven Rostedtf65eda42008-01-25 21:08:07 +01002077 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002078 double_lock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002079
2080 /*
Kirill Tkhaie23ee742013-06-07 15:37:43 -04002081 * We can pull only a task, which is pushable
2082 * on its rq, and no others.
Steven Rostedtf65eda42008-01-25 21:08:07 +01002083 */
Kirill Tkhaie23ee742013-06-07 15:37:43 -04002084 p = pick_highest_pushable_task(src_rq, this_cpu);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002085
2086 /*
2087 * Do we have an RT task that preempts
2088 * the to-be-scheduled task?
2089 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002090 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01002091 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002092 WARN_ON(!task_on_rq_queued(p));
Steven Rostedtf65eda42008-01-25 21:08:07 +01002093
2094 /*
2095 * There's a chance that p is higher in priority
2096 * than what's currently running on its cpu.
2097 * This is just that p is wakeing up and hasn't
2098 * had a chance to schedule. We only pull
2099 * p if it is lower in priority than the
Gregory Haskinsa8728942008-12-29 09:39:49 -05002100 * current task on the run queue
Steven Rostedtf65eda42008-01-25 21:08:07 +01002101 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002102 if (p->prio < src_rq->curr->prio)
Mike Galbraith614ee1f2008-01-25 21:08:30 +01002103 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002104
Peter Zijlstra8046d682015-06-11 14:46:40 +02002105 resched = true;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002106
2107 deactivate_task(src_rq, p, 0);
2108 set_task_cpu(p, this_cpu);
2109 activate_task(this_rq, p, 0);
2110 /*
2111 * We continue with the search, just in
2112 * case there's an even higher prio task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002113 * in another runqueue. (low likelihood
Steven Rostedtf65eda42008-01-25 21:08:07 +01002114 * but possible)
Steven Rostedtf65eda42008-01-25 21:08:07 +01002115 */
Steven Rostedtf65eda42008-01-25 21:08:07 +01002116 }
Peter Zijlstra49246272010-10-17 21:46:10 +02002117skip:
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02002118 double_unlock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002119 }
2120
Peter Zijlstra8046d682015-06-11 14:46:40 +02002121 if (resched)
2122 resched_curr(this_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002123}
2124
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002125/*
2126 * If we are not running and we are not going to reschedule soon, we should
2127 * try to push tasks away now
2128 */
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002129static void task_woken_rt(struct rq *rq, struct task_struct *p)
Steven Rostedt4642daf2008-01-25 21:08:07 +01002130{
Steven Rostedt9a897c52008-01-25 21:08:22 +01002131 if (!task_running(rq, p) &&
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002132 !test_tsk_need_resched(rq->curr) &&
Thomas Gleixner50605ff2016-05-11 14:23:31 +02002133 tsk_nr_cpus_allowed(p) > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01002134 (dl_task(rq->curr) || rt_task(rq->curr)) &&
Thomas Gleixner50605ff2016-05-11 14:23:31 +02002135 (tsk_nr_cpus_allowed(rq->curr) < 2 ||
Shawn Bohrer3be209a2011-09-12 09:28:04 -05002136 rq->curr->prio <= p->prio))
Steven Rostedt4642daf2008-01-25 21:08:07 +01002137 push_rt_tasks(rq);
2138}
2139
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002140/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002141static void rq_online_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002142{
2143 if (rq->rt.overloaded)
2144 rt_set_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002145
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002146 __enable_runtime(rq);
2147
Gregory Haskinse864c492008-12-29 09:39:49 -05002148 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002149}
2150
2151/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002152static void rq_offline_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002153{
2154 if (rq->rt.overloaded)
2155 rt_clear_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002156
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002157 __disable_runtime(rq);
2158
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002159 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002160}
Steven Rostedtcb469842008-01-25 21:08:22 +01002161
2162/*
2163 * When switch from the rt queue, we bring ourselves to a position
2164 * that we might want to pull RT tasks from other runqueues.
2165 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002166static void switched_from_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002167{
2168 /*
2169 * If there are other RT tasks then we will reschedule
2170 * and the scheduling of the other RT tasks will handle
2171 * the balancing. But if we are the last RT task
2172 * we may need to handle the pulling of RT tasks
2173 * now.
2174 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002175 if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
Kirill Tkhai1158ddb2012-11-23 00:02:15 +04002176 return;
2177
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002178 queue_pull_task(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002179}
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302180
Li Zefan11c785b2014-02-08 14:17:45 +08002181void __init init_sched_rt_class(void)
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302182{
2183 unsigned int i;
2184
Peter Zijlstra029632f2011-10-25 10:00:11 +02002185 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -07002186 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
Mike Travis6ca09df2008-12-31 18:08:45 -08002187 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra029632f2011-10-25 10:00:11 +02002188 }
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302189}
Steven Rostedte8fa1362008-01-25 21:08:05 +01002190#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002191
Steven Rostedtcb469842008-01-25 21:08:22 +01002192/*
2193 * When switching a task to RT, we may overload the runqueue
2194 * with RT tasks. In this case we try to push them off to
2195 * other runqueues.
2196 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002197static void switched_to_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002198{
Steven Rostedtcb469842008-01-25 21:08:22 +01002199 /*
2200 * If we are already running, then there's nothing
2201 * that needs to be done. But if we are not running
2202 * we may need to preempt the current running task.
2203 * If that current running task is also an RT task
2204 * then see if we can move to another run queue.
2205 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002206 if (task_on_rq_queued(p) && rq->curr != p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002207#ifdef CONFIG_SMP
Thomas Gleixner50605ff2016-05-11 14:23:31 +02002208 if (tsk_nr_cpus_allowed(p) > 1 && rq->rt.overloaded)
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002209 queue_push_tasks(rq);
Sebastian Andrzej Siewior916c5cfe2017-01-24 15:40:06 +01002210#endif /* CONFIG_SMP */
Paul E. McKenneybac7bb12017-10-13 17:00:18 -07002211 if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
Kirill Tkhai88751252014-06-29 00:03:57 +04002212 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002213 }
2214}
2215
2216/*
2217 * Priority of the task has changed. This may cause
2218 * us to initiate a push or pull.
2219 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002220static void
2221prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01002222{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002223 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002224 return;
2225
2226 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002227#ifdef CONFIG_SMP
2228 /*
2229 * If our priority decreases while running, we
2230 * may need to pull tasks to this runqueue.
2231 */
2232 if (oldprio < p->prio)
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002233 queue_pull_task(rq);
2234
Steven Rostedtcb469842008-01-25 21:08:22 +01002235 /*
2236 * If there's a higher priority task waiting to run
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002237 * then reschedule.
Steven Rostedtcb469842008-01-25 21:08:22 +01002238 */
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002239 if (p->prio > rq->rt.highest_prio.curr)
Kirill Tkhai88751252014-06-29 00:03:57 +04002240 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002241#else
2242 /* For UP simply resched on drop of prio */
2243 if (oldprio < p->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002244 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002245#endif /* CONFIG_SMP */
2246 } else {
2247 /*
2248 * This task is not running, but if it is
2249 * greater than the current running task
2250 * then reschedule.
2251 */
2252 if (p->prio < rq->curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002253 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002254 }
2255}
2256
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002257static void watchdog(struct rq *rq, struct task_struct *p)
2258{
2259 unsigned long soft, hard;
2260
Jiri Slaby78d7d402010-03-05 13:42:54 -08002261 /* max may change after cur was read, this will be fixed next tick */
2262 soft = task_rlimit(p, RLIMIT_RTTIME);
2263 hard = task_rlimit_max(p, RLIMIT_RTTIME);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002264
2265 if (soft != RLIM_INFINITY) {
2266 unsigned long next;
2267
Ying Xue57d2aa02012-07-17 15:03:43 +08002268 if (p->rt.watchdog_stamp != jiffies) {
2269 p->rt.timeout++;
2270 p->rt.watchdog_stamp = jiffies;
2271 }
2272
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002273 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
Peter Zijlstra5a52dd52008-01-25 21:08:32 +01002274 if (p->rt.timeout > next)
Frank Mayharf06febc2008-09-12 09:54:39 -07002275 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002276 }
2277}
Steven Rostedtcb469842008-01-25 21:08:22 +01002278
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002279static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002280{
Colin Cross454c7992012-05-16 21:34:23 -07002281 struct sched_rt_entity *rt_se = &p->rt;
2282
Peter Zijlstra67e2be02007-12-20 15:01:17 +01002283 update_curr_rt(rq);
2284
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002285 watchdog(rq, p);
2286
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002287 /*
2288 * RR tasks need a special form of timeslice management.
2289 * FIFO tasks have no timeslices.
2290 */
2291 if (p->policy != SCHED_RR)
2292 return;
2293
Peter Zijlstrafa717062008-01-25 21:08:27 +01002294 if (--p->rt.time_slice)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002295 return;
2296
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002297 p->rt.time_slice = sched_rr_timeslice;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002298
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002299 /*
Li Bine9aa39b2013-10-21 20:15:43 +08002300 * Requeue to the end of queue if we (and all of our ancestors) are not
2301 * the only element on the queue
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002302 */
Colin Cross454c7992012-05-16 21:34:23 -07002303 for_each_sched_rt_entity(rt_se) {
2304 if (rt_se->run_list.prev != rt_se->run_list.next) {
2305 requeue_task_rt(rq, p, 0);
Kirill Tkhai8aa6f0e2014-09-22 22:36:43 +04002306 resched_curr(rq);
Colin Cross454c7992012-05-16 21:34:23 -07002307 return;
2308 }
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002309 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002310}
2311
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002312static void set_curr_task_rt(struct rq *rq)
2313{
2314 struct task_struct *p = rq->curr;
2315
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002316 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05002317
2318 /* The running task is never eligible for pushing */
2319 dequeue_pushable_task(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002320}
2321
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07002322static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00002323{
2324 /*
2325 * Time slice is 0 for SCHED_FIFO tasks
2326 */
2327 if (task->policy == SCHED_RR)
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002328 return sched_rr_timeslice;
Peter Williams0d721ce2009-09-21 01:31:53 +00002329 else
2330 return 0;
2331}
2332
Peter Zijlstra029632f2011-10-25 10:00:11 +02002333const struct sched_class rt_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002334 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002335 .enqueue_task = enqueue_task_rt,
2336 .dequeue_task = dequeue_task_rt,
2337 .yield_task = yield_task_rt,
2338
2339 .check_preempt_curr = check_preempt_curr_rt,
2340
2341 .pick_next_task = pick_next_task_rt,
2342 .put_prev_task = put_prev_task_rt,
2343
Peter Williams681f3e62007-10-24 18:23:51 +02002344#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08002345 .select_task_rq = select_task_rq_rt,
2346
Peter Zijlstra6c370672015-05-15 17:43:36 +02002347 .set_cpus_allowed = set_cpus_allowed_common,
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002348 .rq_online = rq_online_rt,
2349 .rq_offline = rq_offline_rt,
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002350 .task_woken = task_woken_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002351 .switched_from = switched_from_rt,
Peter Williams681f3e62007-10-24 18:23:51 +02002352#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002353
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002354 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002355 .task_tick = task_tick_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002356
Peter Williams0d721ce2009-09-21 01:31:53 +00002357 .get_rr_interval = get_rr_interval_rt,
2358
Steven Rostedtcb469842008-01-25 21:08:22 +01002359 .prio_changed = prio_changed_rt,
2360 .switched_to = switched_to_rt,
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002361
2362 .update_curr = update_curr_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002363};
Peter Zijlstraada18de2008-06-19 14:22:24 +02002364
2365#ifdef CONFIG_SCHED_DEBUG
2366extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2367
Peter Zijlstra029632f2011-10-25 10:00:11 +02002368void print_rt_stats(struct seq_file *m, int cpu)
Peter Zijlstraada18de2008-06-19 14:22:24 +02002369{
Cheng Xuec514c42011-05-14 14:20:02 +08002370 rt_rq_iter_t iter;
Peter Zijlstraada18de2008-06-19 14:22:24 +02002371 struct rt_rq *rt_rq;
2372
2373 rcu_read_lock();
Cheng Xuec514c42011-05-14 14:20:02 +08002374 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
Peter Zijlstraada18de2008-06-19 14:22:24 +02002375 print_rt_rq(m, cpu, rt_rq);
2376 rcu_read_unlock();
2377}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302378#endif /* CONFIG_SCHED_DEBUG */