blob: 2add019ddbd0816283544c9a8093ce72680cc25d [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>
9
Clark Williamsce0dbbb2013-02-07 09:47:04 -060010int sched_rr_timeslice = RR_TIMESLICE;
11
Peter Zijlstra029632f2011-10-25 10:00:11 +020012static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
13
14struct rt_bandwidth def_rt_bandwidth;
15
16static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
17{
18 struct rt_bandwidth *rt_b =
19 container_of(timer, struct rt_bandwidth, rt_period_timer);
20 ktime_t now;
21 int overrun;
22 int idle = 0;
23
24 for (;;) {
25 now = hrtimer_cb_get_time(timer);
26 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
27
28 if (!overrun)
29 break;
30
31 idle = do_sched_rt_period_timer(rt_b, overrun);
32 }
33
34 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
35}
36
37void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
38{
39 rt_b->rt_period = ns_to_ktime(period);
40 rt_b->rt_runtime = runtime;
41
42 raw_spin_lock_init(&rt_b->rt_runtime_lock);
43
44 hrtimer_init(&rt_b->rt_period_timer,
45 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
46 rt_b->rt_period_timer.function = sched_rt_period_timer;
47}
48
49static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
50{
51 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
52 return;
53
54 if (hrtimer_active(&rt_b->rt_period_timer))
55 return;
56
57 raw_spin_lock(&rt_b->rt_runtime_lock);
58 start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period);
59 raw_spin_unlock(&rt_b->rt_runtime_lock);
60}
61
62void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
63{
64 struct rt_prio_array *array;
65 int i;
66
67 array = &rt_rq->active;
68 for (i = 0; i < MAX_RT_PRIO; i++) {
69 INIT_LIST_HEAD(array->queue + i);
70 __clear_bit(i, array->bitmap);
71 }
72 /* delimiter for bitsearch: */
73 __set_bit(MAX_RT_PRIO, array->bitmap);
74
75#if defined CONFIG_SMP
76 rt_rq->highest_prio.curr = MAX_RT_PRIO;
77 rt_rq->highest_prio.next = MAX_RT_PRIO;
78 rt_rq->rt_nr_migratory = 0;
79 rt_rq->overloaded = 0;
80 plist_head_init(&rt_rq->pushable_tasks);
81#endif
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +040082 /* We start is dequeued state, because no RT tasks are queued */
83 rt_rq->rt_queued = 0;
Peter Zijlstra029632f2011-10-25 10:00:11 +020084
85 rt_rq->rt_time = 0;
86 rt_rq->rt_throttled = 0;
87 rt_rq->rt_runtime = 0;
88 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
89}
90
Gregory Haskins398a1532009-01-14 09:10:04 -050091#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +020092static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
93{
94 hrtimer_cancel(&rt_b->rt_period_timer);
95}
Gregory Haskins398a1532009-01-14 09:10:04 -050096
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +020097#define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
98
Peter Zijlstra8f488942009-07-24 12:25:30 +020099static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
100{
101#ifdef CONFIG_SCHED_DEBUG
102 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
103#endif
104 return container_of(rt_se, struct task_struct, rt);
105}
106
Gregory Haskins398a1532009-01-14 09:10:04 -0500107static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
108{
109 return rt_rq->rq;
110}
111
112static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
113{
114 return rt_se->rt_rq;
115}
116
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400117static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
118{
119 struct rt_rq *rt_rq = rt_se->rt_rq;
120
121 return rt_rq->rq;
122}
123
Peter Zijlstra029632f2011-10-25 10:00:11 +0200124void free_rt_sched_group(struct task_group *tg)
125{
126 int i;
127
128 if (tg->rt_se)
129 destroy_rt_bandwidth(&tg->rt_bandwidth);
130
131 for_each_possible_cpu(i) {
132 if (tg->rt_rq)
133 kfree(tg->rt_rq[i]);
134 if (tg->rt_se)
135 kfree(tg->rt_se[i]);
136 }
137
138 kfree(tg->rt_rq);
139 kfree(tg->rt_se);
140}
141
142void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
143 struct sched_rt_entity *rt_se, int cpu,
144 struct sched_rt_entity *parent)
145{
146 struct rq *rq = cpu_rq(cpu);
147
148 rt_rq->highest_prio.curr = MAX_RT_PRIO;
149 rt_rq->rt_nr_boosted = 0;
150 rt_rq->rq = rq;
151 rt_rq->tg = tg;
152
153 tg->rt_rq[cpu] = rt_rq;
154 tg->rt_se[cpu] = rt_se;
155
156 if (!rt_se)
157 return;
158
159 if (!parent)
160 rt_se->rt_rq = &rq->rt;
161 else
162 rt_se->rt_rq = parent->my_q;
163
164 rt_se->my_q = rt_rq;
165 rt_se->parent = parent;
166 INIT_LIST_HEAD(&rt_se->run_list);
167}
168
169int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
170{
171 struct rt_rq *rt_rq;
172 struct sched_rt_entity *rt_se;
173 int i;
174
175 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
176 if (!tg->rt_rq)
177 goto err;
178 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
179 if (!tg->rt_se)
180 goto err;
181
182 init_rt_bandwidth(&tg->rt_bandwidth,
183 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
184
185 for_each_possible_cpu(i) {
186 rt_rq = kzalloc_node(sizeof(struct rt_rq),
187 GFP_KERNEL, cpu_to_node(i));
188 if (!rt_rq)
189 goto err;
190
191 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
192 GFP_KERNEL, cpu_to_node(i));
193 if (!rt_se)
194 goto err_free_rq;
195
196 init_rt_rq(rt_rq, cpu_rq(i));
197 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
198 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
199 }
200
201 return 1;
202
203err_free_rq:
204 kfree(rt_rq);
205err:
206 return 0;
207}
208
Gregory Haskins398a1532009-01-14 09:10:04 -0500209#else /* CONFIG_RT_GROUP_SCHED */
210
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200211#define rt_entity_is_task(rt_se) (1)
212
Peter Zijlstra8f488942009-07-24 12:25:30 +0200213static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
214{
215 return container_of(rt_se, struct task_struct, rt);
216}
217
Gregory Haskins398a1532009-01-14 09:10:04 -0500218static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
219{
220 return container_of(rt_rq, struct rq, rt);
221}
222
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400223static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
Gregory Haskins398a1532009-01-14 09:10:04 -0500224{
225 struct task_struct *p = rt_task_of(rt_se);
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400226
227 return task_rq(p);
228}
229
230static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
231{
232 struct rq *rq = rq_of_rt_se(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -0500233
234 return &rq->rt;
235}
236
Peter Zijlstra029632f2011-10-25 10:00:11 +0200237void free_rt_sched_group(struct task_group *tg) { }
238
239int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
240{
241 return 1;
242}
Gregory Haskins398a1532009-01-14 09:10:04 -0500243#endif /* CONFIG_RT_GROUP_SCHED */
244
Steven Rostedt4fd29172008-01-25 21:08:06 +0100245#ifdef CONFIG_SMP
Ingo Molnar84de4272008-01-25 21:08:15 +0100246
Peter Zijlstra38033c32014-01-23 20:32:21 +0100247static int pull_rt_task(struct rq *this_rq);
248
Peter Zijlstradc877342014-02-12 15:47:29 +0100249static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
250{
251 /* Try to pull RT tasks here if we lower this rq's prio */
252 return rq->rt.highest_prio.curr > prev->prio;
253}
254
Gregory Haskins637f5082008-01-25 21:08:18 +0100255static inline int rt_overloaded(struct rq *rq)
Steven Rostedt4fd29172008-01-25 21:08:06 +0100256{
Gregory Haskins637f5082008-01-25 21:08:18 +0100257 return atomic_read(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100258}
Ingo Molnar84de4272008-01-25 21:08:15 +0100259
Steven Rostedt4fd29172008-01-25 21:08:06 +0100260static inline void rt_set_overload(struct rq *rq)
261{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400262 if (!rq->online)
263 return;
264
Rusty Russellc6c49272008-11-25 02:35:05 +1030265 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100266 /*
267 * Make sure the mask is visible before we set
268 * the overload count. That is checked to determine
269 * if we should look at the mask. It would be a shame
270 * if we looked at the mask, but the mask was not
271 * updated yet.
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200272 *
273 * Matched by the barrier in pull_rt_task().
Steven Rostedt4fd29172008-01-25 21:08:06 +0100274 */
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200275 smp_wmb();
Gregory Haskins637f5082008-01-25 21:08:18 +0100276 atomic_inc(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100277}
Ingo Molnar84de4272008-01-25 21:08:15 +0100278
Steven Rostedt4fd29172008-01-25 21:08:06 +0100279static inline void rt_clear_overload(struct rq *rq)
280{
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400281 if (!rq->online)
282 return;
283
Steven Rostedt4fd29172008-01-25 21:08:06 +0100284 /* the order here really doesn't matter */
Gregory Haskins637f5082008-01-25 21:08:18 +0100285 atomic_dec(&rq->rd->rto_count);
Rusty Russellc6c49272008-11-25 02:35:05 +1030286 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100287}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100288
Gregory Haskins398a1532009-01-14 09:10:04 -0500289static void update_rt_migration(struct rt_rq *rt_rq)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100290{
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200291 if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
Gregory Haskins398a1532009-01-14 09:10:04 -0500292 if (!rt_rq->overloaded) {
293 rt_set_overload(rq_of_rt_rq(rt_rq));
294 rt_rq->overloaded = 1;
Gregory Haskinscdc8eb92008-01-25 21:08:23 +0100295 }
Gregory Haskins398a1532009-01-14 09:10:04 -0500296 } else if (rt_rq->overloaded) {
297 rt_clear_overload(rq_of_rt_rq(rt_rq));
298 rt_rq->overloaded = 0;
Gregory Haskins637f5082008-01-25 21:08:18 +0100299 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100300}
Steven Rostedt4fd29172008-01-25 21:08:06 +0100301
Gregory Haskins398a1532009-01-14 09:10:04 -0500302static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100303{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200304 struct task_struct *p;
305
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200306 if (!rt_entity_is_task(rt_se))
307 return;
308
Peter Zijlstra29baa742012-04-23 12:11:21 +0200309 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200310 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
311
312 rt_rq->rt_nr_total++;
Peter Zijlstra29baa742012-04-23 12:11:21 +0200313 if (p->nr_cpus_allowed > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500314 rt_rq->rt_nr_migratory++;
315
316 update_rt_migration(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100317}
318
Gregory Haskins398a1532009-01-14 09:10:04 -0500319static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
320{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200321 struct task_struct *p;
322
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200323 if (!rt_entity_is_task(rt_se))
324 return;
325
Peter Zijlstra29baa742012-04-23 12:11:21 +0200326 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200327 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
328
329 rt_rq->rt_nr_total--;
Peter Zijlstra29baa742012-04-23 12:11:21 +0200330 if (p->nr_cpus_allowed > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500331 rt_rq->rt_nr_migratory--;
332
333 update_rt_migration(rt_rq);
334}
335
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400336static inline int has_pushable_tasks(struct rq *rq)
337{
338 return !plist_head_empty(&rq->rt.pushable_tasks);
339}
340
Peter Zijlstradc877342014-02-12 15:47:29 +0100341static inline void set_post_schedule(struct rq *rq)
342{
343 /*
344 * We detect this state here so that we can avoid taking the RQ
345 * lock again later if there is no need to push
346 */
347 rq->post_schedule = has_pushable_tasks(rq);
348}
349
Gregory Haskins917b6272008-12-29 09:39:53 -0500350static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
351{
352 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
353 plist_node_init(&p->pushable_tasks, p->prio);
354 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400355
356 /* Update the highest prio pushable task */
357 if (p->prio < rq->rt.highest_prio.next)
358 rq->rt.highest_prio.next = p->prio;
Gregory Haskins917b6272008-12-29 09:39:53 -0500359}
360
361static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
362{
363 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
Gregory Haskins917b6272008-12-29 09:39:53 -0500364
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400365 /* Update the new highest prio pushable task */
366 if (has_pushable_tasks(rq)) {
367 p = plist_first_entry(&rq->rt.pushable_tasks,
368 struct task_struct, pushable_tasks);
369 rq->rt.highest_prio.next = p->prio;
370 } else
371 rq->rt.highest_prio.next = MAX_RT_PRIO;
Ingo Molnarbcf08df2008-04-19 12:11:10 +0200372}
373
Gregory Haskins917b6272008-12-29 09:39:53 -0500374#else
375
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100376static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
377{
378}
379
380static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
381{
382}
383
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500384static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100385void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
386{
387}
388
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500389static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100390void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
391{
392}
Gregory Haskins917b6272008-12-29 09:39:53 -0500393
Peter Zijlstradc877342014-02-12 15:47:29 +0100394static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
395{
396 return false;
397}
398
399static inline int pull_rt_task(struct rq *this_rq)
400{
401 return 0;
402}
403
404static inline void set_post_schedule(struct rq *rq)
405{
406}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200407#endif /* CONFIG_SMP */
408
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400409static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
410static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
411
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100412static inline int on_rt_rq(struct sched_rt_entity *rt_se)
413{
414 return !list_empty(&rt_se->run_list);
415}
416
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100417#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100418
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100419static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100420{
421 if (!rt_rq->tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100422 return RUNTIME_INF;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100423
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200424 return rt_rq->rt_runtime;
425}
426
427static inline u64 sched_rt_period(struct rt_rq *rt_rq)
428{
429 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100430}
431
Cheng Xuec514c42011-05-14 14:20:02 +0800432typedef struct task_group *rt_rq_iter_t;
433
Yong Zhang1c09ab02011-06-28 10:51:31 +0800434static inline struct task_group *next_task_group(struct task_group *tg)
435{
436 do {
437 tg = list_entry_rcu(tg->list.next,
438 typeof(struct task_group), list);
439 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
440
441 if (&tg->list == &task_groups)
442 tg = NULL;
443
444 return tg;
445}
446
447#define for_each_rt_rq(rt_rq, iter, rq) \
448 for (iter = container_of(&task_groups, typeof(*iter), list); \
449 (iter = next_task_group(iter)) && \
450 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
Cheng Xuec514c42011-05-14 14:20:02 +0800451
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100452#define for_each_sched_rt_entity(rt_se) \
453 for (; rt_se; rt_se = rt_se->parent)
454
455static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
456{
457 return rt_se->my_q;
458}
459
Thomas Gleixner37dad3f2010-01-20 20:59:01 +0000460static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100461static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
462
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100463static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100464{
Dario Faggiolif6121f42008-10-03 17:40:46 +0200465 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
Yong Zhang74b7eb52010-01-29 14:57:52 +0800466 struct sched_rt_entity *rt_se;
467
Balbir Singh0c3b9162011-03-03 17:04:35 +0530468 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
469
470 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100471
Dario Faggiolif6121f42008-10-03 17:40:46 +0200472 if (rt_rq->rt_nr_running) {
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400473 if (!rt_se)
474 enqueue_top_rt_rq(rt_rq);
475 else if (!on_rt_rq(rt_se))
Thomas Gleixner37dad3f2010-01-20 20:59:01 +0000476 enqueue_rt_entity(rt_se, false);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400477
Gregory Haskinse864c492008-12-29 09:39:49 -0500478 if (rt_rq->highest_prio.curr < curr->prio)
Peter Zijlstra10203872008-01-25 21:08:32 +0100479 resched_task(curr);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100480 }
481}
482
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100483static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100484{
Yong Zhang74b7eb52010-01-29 14:57:52 +0800485 struct sched_rt_entity *rt_se;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530486 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
Yong Zhang74b7eb52010-01-29 14:57:52 +0800487
Balbir Singh0c3b9162011-03-03 17:04:35 +0530488 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100489
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400490 if (!rt_se)
491 dequeue_top_rt_rq(rt_rq);
492 else if (on_rt_rq(rt_se))
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100493 dequeue_rt_entity(rt_se);
494}
495
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100496static int rt_se_boosted(struct sched_rt_entity *rt_se)
497{
498 struct rt_rq *rt_rq = group_rt_rq(rt_se);
499 struct task_struct *p;
500
501 if (rt_rq)
502 return !!rt_rq->rt_nr_boosted;
503
504 p = rt_task_of(rt_se);
505 return p->prio != p->normal_prio;
506}
507
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200508#ifdef CONFIG_SMP
Rusty Russellc6c49272008-11-25 02:35:05 +1030509static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200510{
Nathan Zimmer424c93f2013-05-09 11:24:03 -0500511 return this_rq()->rd->span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200512}
513#else
Rusty Russellc6c49272008-11-25 02:35:05 +1030514static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200515{
Rusty Russellc6c49272008-11-25 02:35:05 +1030516 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200517}
518#endif
519
520static inline
521struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
522{
523 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
524}
525
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200526static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
527{
528 return &rt_rq->tg->rt_bandwidth;
529}
530
Dhaval Giani55e12e52008-06-24 23:39:43 +0530531#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100532
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100533static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100534{
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200535 return rt_rq->rt_runtime;
536}
537
538static inline u64 sched_rt_period(struct rt_rq *rt_rq)
539{
540 return ktime_to_ns(def_rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100541}
542
Cheng Xuec514c42011-05-14 14:20:02 +0800543typedef struct rt_rq *rt_rq_iter_t;
544
545#define for_each_rt_rq(rt_rq, iter, rq) \
546 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
547
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100548#define for_each_sched_rt_entity(rt_se) \
549 for (; rt_se; rt_se = NULL)
550
551static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
552{
553 return NULL;
554}
555
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100556static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100557{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400558 struct rq *rq = rq_of_rt_rq(rt_rq);
559
560 if (!rt_rq->rt_nr_running)
561 return;
562
563 enqueue_top_rt_rq(rt_rq);
564 resched_task(rq->curr);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100565}
566
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100567static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100568{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400569 dequeue_top_rt_rq(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100570}
571
Rusty Russellc6c49272008-11-25 02:35:05 +1030572static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200573{
Rusty Russellc6c49272008-11-25 02:35:05 +1030574 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200575}
576
577static inline
578struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
579{
580 return &cpu_rq(cpu)->rt;
581}
582
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200583static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
584{
585 return &def_rt_bandwidth;
586}
587
Dhaval Giani55e12e52008-06-24 23:39:43 +0530588#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100589
Juri Lellifaa59932014-02-21 11:37:15 +0100590bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
591{
592 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
593
594 return (hrtimer_active(&rt_b->rt_period_timer) ||
595 rt_rq->rt_time < rt_b->rt_runtime);
596}
597
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200598#ifdef CONFIG_SMP
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200599/*
600 * We ran out of runtime, see if we can borrow some from our neighbours.
601 */
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200602static int do_balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200603{
604 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
Shawn Bohreraa7f6732013-01-14 11:55:31 -0600605 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200606 int i, weight, more = 0;
607 u64 rt_period;
608
Rusty Russellc6c49272008-11-25 02:35:05 +1030609 weight = cpumask_weight(rd->span);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200610
Thomas Gleixner0986b112009-11-17 15:32:06 +0100611 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200612 rt_period = ktime_to_ns(rt_b->rt_period);
Rusty Russellc6c49272008-11-25 02:35:05 +1030613 for_each_cpu(i, rd->span) {
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200614 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
615 s64 diff;
616
617 if (iter == rt_rq)
618 continue;
619
Thomas Gleixner0986b112009-11-17 15:32:06 +0100620 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200621 /*
622 * Either all rqs have inf runtime and there's nothing to steal
623 * or __disable_runtime() below sets a specific rq to inf to
624 * indicate its been disabled and disalow stealing.
625 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200626 if (iter->rt_runtime == RUNTIME_INF)
627 goto next;
628
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200629 /*
630 * From runqueues with spare time, take 1/n part of their
631 * spare time, but no more than our period.
632 */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200633 diff = iter->rt_runtime - iter->rt_time;
634 if (diff > 0) {
Peter Zijlstra58838cf2008-07-24 12:43:13 +0200635 diff = div_u64((u64)diff, weight);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200636 if (rt_rq->rt_runtime + diff > rt_period)
637 diff = rt_period - rt_rq->rt_runtime;
638 iter->rt_runtime -= diff;
639 rt_rq->rt_runtime += diff;
640 more = 1;
641 if (rt_rq->rt_runtime == rt_period) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100642 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200643 break;
644 }
645 }
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200646next:
Thomas Gleixner0986b112009-11-17 15:32:06 +0100647 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200648 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100649 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200650
651 return more;
652}
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200653
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200654/*
655 * Ensure this RQ takes back all the runtime it lend to its neighbours.
656 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200657static void __disable_runtime(struct rq *rq)
658{
659 struct root_domain *rd = rq->rd;
Cheng Xuec514c42011-05-14 14:20:02 +0800660 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200661 struct rt_rq *rt_rq;
662
663 if (unlikely(!scheduler_running))
664 return;
665
Cheng Xuec514c42011-05-14 14:20:02 +0800666 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200667 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
668 s64 want;
669 int i;
670
Thomas Gleixner0986b112009-11-17 15:32:06 +0100671 raw_spin_lock(&rt_b->rt_runtime_lock);
672 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200673 /*
674 * Either we're all inf and nobody needs to borrow, or we're
675 * already disabled and thus have nothing to do, or we have
676 * exactly the right amount of runtime to take out.
677 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200678 if (rt_rq->rt_runtime == RUNTIME_INF ||
679 rt_rq->rt_runtime == rt_b->rt_runtime)
680 goto balanced;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100681 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200682
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200683 /*
684 * Calculate the difference between what we started out with
685 * and what we current have, that's the amount of runtime
686 * we lend and now have to reclaim.
687 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200688 want = rt_b->rt_runtime - rt_rq->rt_runtime;
689
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200690 /*
691 * Greedy reclaim, take back as much as we can.
692 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030693 for_each_cpu(i, rd->span) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200694 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
695 s64 diff;
696
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200697 /*
698 * Can't reclaim from ourselves or disabled runqueues.
699 */
Peter Zijlstraf1679d02008-08-14 15:49:00 +0200700 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200701 continue;
702
Thomas Gleixner0986b112009-11-17 15:32:06 +0100703 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200704 if (want > 0) {
705 diff = min_t(s64, iter->rt_runtime, want);
706 iter->rt_runtime -= diff;
707 want -= diff;
708 } else {
709 iter->rt_runtime -= want;
710 want -= want;
711 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100712 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200713
714 if (!want)
715 break;
716 }
717
Thomas Gleixner0986b112009-11-17 15:32:06 +0100718 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200719 /*
720 * We cannot be left wanting - that would mean some runtime
721 * leaked out of the system.
722 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200723 BUG_ON(want);
724balanced:
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200725 /*
726 * Disable all the borrow logic by pretending we have inf
727 * runtime - in which case borrowing doesn't make sense.
728 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200729 rt_rq->rt_runtime = RUNTIME_INF;
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -0700730 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100731 raw_spin_unlock(&rt_rq->rt_runtime_lock);
732 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200733 }
734}
735
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200736static void __enable_runtime(struct rq *rq)
737{
Cheng Xuec514c42011-05-14 14:20:02 +0800738 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200739 struct rt_rq *rt_rq;
740
741 if (unlikely(!scheduler_running))
742 return;
743
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200744 /*
745 * Reset each runqueue's bandwidth settings
746 */
Cheng Xuec514c42011-05-14 14:20:02 +0800747 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200748 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
749
Thomas Gleixner0986b112009-11-17 15:32:06 +0100750 raw_spin_lock(&rt_b->rt_runtime_lock);
751 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200752 rt_rq->rt_runtime = rt_b->rt_runtime;
753 rt_rq->rt_time = 0;
Zhang, Yanminbaf25732008-09-09 11:26:33 +0800754 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100755 raw_spin_unlock(&rt_rq->rt_runtime_lock);
756 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200757 }
758}
759
Peter Zijlstraeff65492008-06-19 14:22:26 +0200760static int balance_runtime(struct rt_rq *rt_rq)
761{
762 int more = 0;
763
Peter Zijlstra4a6184c2011-10-06 22:39:14 +0200764 if (!sched_feat(RT_RUNTIME_SHARE))
765 return more;
766
Peter Zijlstraeff65492008-06-19 14:22:26 +0200767 if (rt_rq->rt_time > rt_rq->rt_runtime) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100768 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200769 more = do_balance_runtime(rt_rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100770 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200771 }
772
773 return more;
774}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530775#else /* !CONFIG_SMP */
Peter Zijlstraeff65492008-06-19 14:22:26 +0200776static inline int balance_runtime(struct rt_rq *rt_rq)
777{
778 return 0;
779}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530780#endif /* CONFIG_SMP */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100781
782static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
783{
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200784 int i, idle = 1, throttled = 0;
Rusty Russellc6c49272008-11-25 02:35:05 +1030785 const struct cpumask *span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200786
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200787 span = sched_rt_period_mask();
Mike Galbraithe221d022012-08-07 10:02:38 +0200788#ifdef CONFIG_RT_GROUP_SCHED
789 /*
790 * FIXME: isolated CPUs should really leave the root task group,
791 * whether they are isolcpus or were isolated via cpusets, lest
792 * the timer run on a CPU which does not service all runqueues,
793 * potentially leaving other CPUs indefinitely throttled. If
794 * isolation is really required, the user will turn the throttle
795 * off to kill the perturbations it causes anyway. Meanwhile,
796 * this maintains functionality for boot and/or troubleshooting.
797 */
798 if (rt_b == &root_task_group.rt_bandwidth)
799 span = cpu_online_mask;
800#endif
Rusty Russellc6c49272008-11-25 02:35:05 +1030801 for_each_cpu(i, span) {
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200802 int enqueue = 0;
803 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
804 struct rq *rq = rq_of_rt_rq(rt_rq);
805
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100806 raw_spin_lock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200807 if (rt_rq->rt_time) {
808 u64 runtime;
809
Thomas Gleixner0986b112009-11-17 15:32:06 +0100810 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200811 if (rt_rq->rt_throttled)
812 balance_runtime(rt_rq);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200813 runtime = rt_rq->rt_runtime;
814 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
815 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
816 rt_rq->rt_throttled = 0;
817 enqueue = 1;
Mike Galbraith61eadef2011-04-29 08:36:50 +0200818
819 /*
820 * Force a clock update if the CPU was idle,
821 * lest wakeup -> unthrottle time accumulate.
822 */
823 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
824 rq->skip_clock_update = -1;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200825 }
826 if (rt_rq->rt_time || rt_rq->rt_nr_running)
827 idle = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100828 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530829 } else if (rt_rq->rt_nr_running) {
Peter Zijlstra8a8cde12008-06-19 14:22:28 +0200830 idle = 0;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530831 if (!rt_rq_throttled(rt_rq))
832 enqueue = 1;
833 }
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200834 if (rt_rq->rt_throttled)
835 throttled = 1;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200836
837 if (enqueue)
838 sched_rt_rq_enqueue(rt_rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100839 raw_spin_unlock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200840 }
841
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200842 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
843 return 1;
844
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200845 return idle;
846}
847
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100848static inline int rt_se_prio(struct sched_rt_entity *rt_se)
849{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100850#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100851 struct rt_rq *rt_rq = group_rt_rq(rt_se);
852
853 if (rt_rq)
Gregory Haskinse864c492008-12-29 09:39:49 -0500854 return rt_rq->highest_prio.curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100855#endif
856
857 return rt_task_of(rt_se)->prio;
858}
859
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100860static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100861{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100862 u64 runtime = sched_rt_runtime(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100863
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100864 if (rt_rq->rt_throttled)
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100865 return rt_rq_throttled(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100866
Shan Hai5b680fd2011-11-29 11:03:56 +0800867 if (runtime >= sched_rt_period(rt_rq))
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200868 return 0;
869
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200870 balance_runtime(rt_rq);
871 runtime = sched_rt_runtime(rt_rq);
872 if (runtime == RUNTIME_INF)
873 return 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200874
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100875 if (rt_rq->rt_time > runtime) {
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200876 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
877
878 /*
879 * Don't actually throttle groups that have no runtime assigned
880 * but accrue some time due to boosting.
881 */
882 if (likely(rt_b->rt_runtime)) {
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100883 static bool once = false;
884
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200885 rt_rq->rt_throttled = 1;
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100886
887 if (!once) {
888 once = true;
889 printk_sched("sched: RT throttling activated\n");
890 }
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200891 } else {
892 /*
893 * In case we did anyway, make it go away,
894 * replenishment is a joke, since it will replenish us
895 * with exactly 0 ns.
896 */
897 rt_rq->rt_time = 0;
898 }
899
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100900 if (rt_rq_throttled(rt_rq)) {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100901 sched_rt_rq_dequeue(rt_rq);
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100902 return 1;
903 }
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100904 }
905
906 return 0;
907}
908
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200909/*
910 * Update the current task's runtime statistics. Skip current tasks that
911 * are not in our scheduling class.
912 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200913static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200914{
915 struct task_struct *curr = rq->curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100916 struct sched_rt_entity *rt_se = &curr->rt;
917 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200918 u64 delta_exec;
919
Peter Zijlstra06c3bc62011-02-02 13:19:48 +0100920 if (curr->sched_class != &rt_sched_class)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200921 return;
922
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200923 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Kirill Tkhaifc79e242013-01-30 16:50:36 +0400924 if (unlikely((s64)delta_exec <= 0))
925 return;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200926
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200927 schedstat_set(curr->se.statistics.exec_max,
928 max(curr->se.statistics.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200929
930 curr->se.sum_exec_runtime += delta_exec;
Frank Mayharf06febc2008-09-12 09:54:39 -0700931 account_group_exec_runtime(curr, delta_exec);
932
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200933 curr->se.exec_start = rq_clock_task(rq);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100934 cpuacct_charge(curr, delta_exec);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100935
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200936 sched_rt_avg_update(rq, delta_exec);
937
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200938 if (!rt_bandwidth_enabled())
939 return;
940
Dhaval Giani354d60c2008-04-19 19:44:59 +0200941 for_each_sched_rt_entity(rt_se) {
942 rt_rq = rt_rq_of_se(rt_se);
943
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200944 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100945 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200946 rt_rq->rt_time += delta_exec;
947 if (sched_rt_runtime_exceeded(rt_rq))
948 resched_task(curr);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100949 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200950 }
Dhaval Giani354d60c2008-04-19 19:44:59 +0200951 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200952}
953
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400954static void
955dequeue_top_rt_rq(struct rt_rq *rt_rq)
956{
957 struct rq *rq = rq_of_rt_rq(rt_rq);
958
959 BUG_ON(&rq->rt != rt_rq);
960
961 if (!rt_rq->rt_queued)
962 return;
963
964 BUG_ON(!rq->nr_running);
965
966 rq->nr_running -= rt_rq->rt_nr_running;
967 rt_rq->rt_queued = 0;
968}
969
970static void
971enqueue_top_rt_rq(struct rt_rq *rt_rq)
972{
973 struct rq *rq = rq_of_rt_rq(rt_rq);
974
975 BUG_ON(&rq->rt != rt_rq);
976
977 if (rt_rq->rt_queued)
978 return;
979 if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
980 return;
981
982 rq->nr_running += rt_rq->rt_nr_running;
983 rt_rq->rt_queued = 1;
984}
985
Gregory Haskins398a1532009-01-14 09:10:04 -0500986#if defined CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -0500987
Gregory Haskins398a1532009-01-14 09:10:04 -0500988static void
989inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +0100990{
Gregory Haskins4d984272008-12-29 09:39:49 -0500991 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins4d984272008-12-29 09:39:49 -0500992
Kirill Tkhai757dfca2013-11-27 19:59:13 +0400993#ifdef CONFIG_RT_GROUP_SCHED
994 /*
995 * Change rq's cpupri only if rt_rq is the top queue.
996 */
997 if (&rq->rt != rt_rq)
998 return;
999#endif
Steven Rostedt5181f4a42011-06-16 21:55:23 -04001000 if (rq->online && prio < prev_prio)
1001 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
Steven Rostedt63489e42008-01-25 21:08:03 +01001002}
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001003
Gregory Haskins398a1532009-01-14 09:10:04 -05001004static void
1005dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001006{
Gregory Haskins4d984272008-12-29 09:39:49 -05001007 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001008
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001009#ifdef CONFIG_RT_GROUP_SCHED
1010 /*
1011 * Change rq's cpupri only if rt_rq is the top queue.
1012 */
1013 if (&rq->rt != rt_rq)
1014 return;
1015#endif
Gregory Haskins398a1532009-01-14 09:10:04 -05001016 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1017 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1018}
1019
1020#else /* CONFIG_SMP */
1021
1022static inline
1023void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1024static inline
1025void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1026
1027#endif /* CONFIG_SMP */
1028
Steven Rostedt63489e42008-01-25 21:08:03 +01001029#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001030static void
1031inc_rt_prio(struct rt_rq *rt_rq, int prio)
1032{
1033 int prev_prio = rt_rq->highest_prio.curr;
Steven Rostedt63489e42008-01-25 21:08:03 +01001034
Gregory Haskins398a1532009-01-14 09:10:04 -05001035 if (prio < prev_prio)
1036 rt_rq->highest_prio.curr = prio;
1037
1038 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1039}
1040
1041static void
1042dec_rt_prio(struct rt_rq *rt_rq, int prio)
1043{
1044 int prev_prio = rt_rq->highest_prio.curr;
1045
1046 if (rt_rq->rt_nr_running) {
1047
1048 WARN_ON(prio < prev_prio);
Gregory Haskinse864c492008-12-29 09:39:49 -05001049
1050 /*
Gregory Haskins398a1532009-01-14 09:10:04 -05001051 * This may have been our highest task, and therefore
1052 * we may have some recomputation to do
Gregory Haskinse864c492008-12-29 09:39:49 -05001053 */
Gregory Haskins398a1532009-01-14 09:10:04 -05001054 if (prio == prev_prio) {
Gregory Haskinse864c492008-12-29 09:39:49 -05001055 struct rt_prio_array *array = &rt_rq->active;
1056
1057 rt_rq->highest_prio.curr =
Steven Rostedt764a9d62008-01-25 21:08:04 +01001058 sched_find_first_bit(array->bitmap);
Gregory Haskinse864c492008-12-29 09:39:49 -05001059 }
1060
Steven Rostedt764a9d62008-01-25 21:08:04 +01001061 } else
Gregory Haskinse864c492008-12-29 09:39:49 -05001062 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001063
Gregory Haskins398a1532009-01-14 09:10:04 -05001064 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1065}
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001066
Gregory Haskins398a1532009-01-14 09:10:04 -05001067#else
1068
1069static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1070static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1071
1072#endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1073
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001074#ifdef CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001075
1076static void
1077inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1078{
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001079 if (rt_se_boosted(rt_se))
Steven Rostedt764a9d62008-01-25 21:08:04 +01001080 rt_rq->rt_nr_boosted++;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01001081
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001082 if (rt_rq->tg)
1083 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001084}
1085
1086static void
1087dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1088{
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001089 if (rt_se_boosted(rt_se))
1090 rt_rq->rt_nr_boosted--;
1091
1092 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
Gregory Haskins398a1532009-01-14 09:10:04 -05001093}
1094
1095#else /* CONFIG_RT_GROUP_SCHED */
1096
1097static void
1098inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1099{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001100 start_rt_bandwidth(&def_rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001101}
1102
1103static inline
1104void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1105
1106#endif /* CONFIG_RT_GROUP_SCHED */
1107
1108static inline
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001109unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1110{
1111 struct rt_rq *group_rq = group_rt_rq(rt_se);
1112
1113 if (group_rq)
1114 return group_rq->rt_nr_running;
1115 else
1116 return 1;
1117}
1118
1119static inline
Gregory Haskins398a1532009-01-14 09:10:04 -05001120void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1121{
1122 int prio = rt_se_prio(rt_se);
1123
1124 WARN_ON(!rt_prio(prio));
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001125 rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -05001126
1127 inc_rt_prio(rt_rq, prio);
1128 inc_rt_migration(rt_se, rt_rq);
1129 inc_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001130}
1131
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001132static inline
1133void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1134{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001135 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001136 WARN_ON(!rt_rq->rt_nr_running);
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001137 rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001138
Gregory Haskins398a1532009-01-14 09:10:04 -05001139 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1140 dec_rt_migration(rt_se, rt_rq);
1141 dec_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001142}
1143
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001144static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001145{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001146 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1147 struct rt_prio_array *array = &rt_rq->active;
1148 struct rt_rq *group_rq = group_rt_rq(rt_se);
Dmitry Adamushko20b63312008-06-11 00:58:30 +02001149 struct list_head *queue = array->queue + rt_se_prio(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001150
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001151 /*
1152 * Don't enqueue the group if its throttled, or when empty.
1153 * The latter is a consequence of the former when a child group
1154 * get throttled and the current group doesn't have any other
1155 * active members.
1156 */
1157 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001158 return;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001159
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001160 if (head)
1161 list_add(&rt_se->run_list, queue);
1162 else
1163 list_add_tail(&rt_se->run_list, queue);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001164 __set_bit(rt_se_prio(rt_se), array->bitmap);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001165
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001166 inc_rt_tasks(rt_se, rt_rq);
1167}
1168
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001169static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001170{
1171 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1172 struct rt_prio_array *array = &rt_rq->active;
1173
1174 list_del_init(&rt_se->run_list);
1175 if (list_empty(array->queue + rt_se_prio(rt_se)))
1176 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1177
1178 dec_rt_tasks(rt_se, rt_rq);
1179}
1180
1181/*
1182 * Because the prio of an upper entry depends on the lower
1183 * entries, we must remove entries top - down.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001184 */
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001185static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001186{
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001187 struct sched_rt_entity *back = NULL;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001188
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001189 for_each_sched_rt_entity(rt_se) {
1190 rt_se->back = back;
1191 back = rt_se;
1192 }
1193
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001194 dequeue_top_rt_rq(rt_rq_of_se(back));
1195
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001196 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1197 if (on_rt_rq(rt_se))
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001198 __dequeue_rt_entity(rt_se);
1199 }
1200}
1201
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001202static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001203{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001204 struct rq *rq = rq_of_rt_se(rt_se);
1205
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001206 dequeue_rt_stack(rt_se);
1207 for_each_sched_rt_entity(rt_se)
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001208 __enqueue_rt_entity(rt_se, head);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001209 enqueue_top_rt_rq(&rq->rt);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001210}
1211
1212static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
1213{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001214 struct rq *rq = rq_of_rt_se(rt_se);
1215
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001216 dequeue_rt_stack(rt_se);
1217
1218 for_each_sched_rt_entity(rt_se) {
1219 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1220
1221 if (rt_rq && rt_rq->rt_nr_running)
Thomas Gleixner37dad3f2010-01-20 20:59:01 +00001222 __enqueue_rt_entity(rt_se, false);
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001223 }
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001224 enqueue_top_rt_rq(&rq->rt);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001225}
1226
1227/*
1228 * Adding/removing a task to/from a priority array:
1229 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001230static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001231enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001232{
1233 struct sched_rt_entity *rt_se = &p->rt;
1234
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001235 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001236 rt_se->timeout = 0;
1237
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001238 enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001239
Peter Zijlstra29baa742012-04-23 12:11:21 +02001240 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001241 enqueue_pushable_task(rq, p);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001242}
1243
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001244static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001245{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001246 struct sched_rt_entity *rt_se = &p->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001247
1248 update_curr_rt(rq);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001249 dequeue_rt_entity(rt_se);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001250
Gregory Haskins917b6272008-12-29 09:39:53 -05001251 dequeue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001252}
1253
1254/*
Richard Weinberger60686312011-11-12 18:07:57 +01001255 * Put task to the head or the end of the run list without the overhead of
1256 * dequeue followed by enqueue.
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001257 */
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001258static void
1259requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001260{
Ingo Molnar1cdad712008-06-19 09:09:15 +02001261 if (on_rt_rq(rt_se)) {
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001262 struct rt_prio_array *array = &rt_rq->active;
1263 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1264
1265 if (head)
1266 list_move(&rt_se->run_list, queue);
1267 else
1268 list_move_tail(&rt_se->run_list, queue);
Ingo Molnar1cdad712008-06-19 09:09:15 +02001269 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001270}
1271
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001272static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001273{
1274 struct sched_rt_entity *rt_se = &p->rt;
1275 struct rt_rq *rt_rq;
1276
1277 for_each_sched_rt_entity(rt_se) {
1278 rt_rq = rt_rq_of_se(rt_se);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001279 requeue_rt_entity(rt_rq, rt_se, head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001280 }
1281}
1282
1283static void yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001284{
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001285 requeue_task_rt(rq, rq->curr, 0);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001286}
1287
Gregory Haskinse7693a32008-01-25 21:08:09 +01001288#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +01001289static int find_lowest_rq(struct task_struct *task);
1290
Peter Zijlstra0017d732010-03-24 18:34:10 +01001291static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01001292select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
Gregory Haskinse7693a32008-01-25 21:08:09 +01001293{
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001294 struct task_struct *curr;
1295 struct rq *rq;
Steven Rostedtc37495f2011-06-16 21:55:22 -04001296
Peter Zijlstra29baa742012-04-23 12:11:21 +02001297 if (p->nr_cpus_allowed == 1)
Mike Galbraith76854c72011-11-22 15:18:24 +01001298 goto out;
1299
Steven Rostedtc37495f2011-06-16 21:55:22 -04001300 /* For anything but wake ups, just return the task_cpu */
1301 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1302 goto out;
1303
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001304 rq = cpu_rq(cpu);
1305
1306 rcu_read_lock();
1307 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
1308
Gregory Haskins318e0892008-01-25 21:08:10 +01001309 /*
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001310 * If the current task on @p's runqueue is an RT task, then
Steven Rostedte1f47d82008-01-25 21:08:12 +01001311 * try to see if we can wake this RT task up on another
1312 * runqueue. Otherwise simply start this RT task
1313 * on its current runqueue.
1314 *
Steven Rostedt43fa5462010-09-20 22:40:03 -04001315 * We want to avoid overloading runqueues. If the woken
1316 * task is a higher priority, then it will stay on this CPU
1317 * and the lower prio task should be moved to another CPU.
1318 * Even though this will probably make the lower prio task
1319 * lose its cache, we do not want to bounce a higher task
1320 * around just because it gave up its CPU, perhaps for a
1321 * lock?
1322 *
1323 * For equal prio tasks, we just let the scheduler sort it out.
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001324 *
Gregory Haskins318e0892008-01-25 21:08:10 +01001325 * Otherwise, just let it ride on the affined RQ and the
1326 * post-schedule router will push the preempted task away
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001327 *
1328 * This test is optimistic, if we get it wrong the load-balancer
1329 * will have to sort it out.
Gregory Haskins318e0892008-01-25 21:08:10 +01001330 */
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001331 if (curr && unlikely(rt_task(curr)) &&
Peter Zijlstra29baa742012-04-23 12:11:21 +02001332 (curr->nr_cpus_allowed < 2 ||
Shawn Bohrer6bfa6872013-10-04 14:24:53 -05001333 curr->prio <= p->prio)) {
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001334 int target = find_lowest_rq(p);
1335
1336 if (target != -1)
1337 cpu = target;
1338 }
1339 rcu_read_unlock();
1340
Steven Rostedtc37495f2011-06-16 21:55:22 -04001341out:
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001342 return cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001343}
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001344
1345static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1346{
Peter Zijlstra29baa742012-04-23 12:11:21 +02001347 if (rq->curr->nr_cpus_allowed == 1)
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001348 return;
1349
Peter Zijlstra29baa742012-04-23 12:11:21 +02001350 if (p->nr_cpus_allowed != 1
Rusty Russell13b8bd02009-03-25 15:01:22 +10301351 && cpupri_find(&rq->rd->cpupri, p, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001352 return;
1353
Rusty Russell13b8bd02009-03-25 15:01:22 +10301354 if (!cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1355 return;
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001356
1357 /*
1358 * There appears to be other cpus that can accept
1359 * current and none to run 'p', so lets reschedule
1360 * to try and push current away:
1361 */
1362 requeue_task_rt(rq, p, 1);
1363 resched_task(rq->curr);
1364}
1365
Gregory Haskinse7693a32008-01-25 21:08:09 +01001366#endif /* CONFIG_SMP */
1367
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001368/*
1369 * Preempt the current task with a newly woken task if needed:
1370 */
Peter Zijlstra7d478722009-09-14 19:55:44 +02001371static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001372{
Gregory Haskins45c01e82008-05-12 21:20:41 +02001373 if (p->prio < rq->curr->prio) {
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001374 resched_task(rq->curr);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001375 return;
1376 }
1377
1378#ifdef CONFIG_SMP
1379 /*
1380 * If:
1381 *
1382 * - the newly woken task is of equal priority to the current task
1383 * - the newly woken task is non-migratable while current is migratable
1384 * - current will be preempted on the next reschedule
1385 *
1386 * we should check to see if current can readily move to a different
1387 * cpu. If so, we will reschedule to allow the push logic to try
1388 * to move current somewhere else, making room for our non-migratable
1389 * task.
1390 */
Hillf Danton8dd0de82011-06-14 18:36:24 -04001391 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001392 check_preempt_equal_prio(rq, p);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001393#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001394}
1395
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001396static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1397 struct rt_rq *rt_rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001398{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001399 struct rt_prio_array *array = &rt_rq->active;
1400 struct sched_rt_entity *next = NULL;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001401 struct list_head *queue;
1402 int idx;
1403
1404 idx = sched_find_first_bit(array->bitmap);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001405 BUG_ON(idx >= MAX_RT_PRIO);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001406
1407 queue = array->queue + idx;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001408 next = list_entry(queue->next, struct sched_rt_entity, run_list);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001409
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001410 return next;
1411}
1412
Gregory Haskins917b6272008-12-29 09:39:53 -05001413static struct task_struct *_pick_next_task_rt(struct rq *rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001414{
1415 struct sched_rt_entity *rt_se;
1416 struct task_struct *p;
Peter Zijlstra606dba22012-02-11 06:05:00 +01001417 struct rt_rq *rt_rq = &rq->rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001418
1419 do {
1420 rt_se = pick_next_rt_entity(rq, rt_rq);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001421 BUG_ON(!rt_se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001422 rt_rq = group_rt_rq(rt_se);
1423 } while (rt_rq);
1424
1425 p = rt_task_of(rt_se);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001426 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001427
1428 return p;
1429}
1430
Peter Zijlstra606dba22012-02-11 06:05:00 +01001431static struct task_struct *
1432pick_next_task_rt(struct rq *rq, struct task_struct *prev)
Gregory Haskins917b6272008-12-29 09:39:53 -05001433{
Peter Zijlstra606dba22012-02-11 06:05:00 +01001434 struct task_struct *p;
1435 struct rt_rq *rt_rq = &rq->rt;
1436
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001437 if (need_pull_rt_task(rq, prev)) {
Peter Zijlstra38033c32014-01-23 20:32:21 +01001438 pull_rt_task(rq);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001439 /*
1440 * pull_rt_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001441 * means a dl or stop task can slip in, in which case we need
1442 * to re-start task selection.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001443 */
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001444 if (unlikely((rq->stop && rq->stop->on_rq) ||
1445 rq->dl.dl_nr_running))
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001446 return RETRY_TASK;
1447 }
Peter Zijlstra38033c32014-01-23 20:32:21 +01001448
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001449 /*
1450 * We may dequeue prev's rt_rq in put_prev_task().
1451 * So, we update time before rt_nr_running check.
1452 */
1453 if (prev->sched_class == &rt_sched_class)
1454 update_curr_rt(rq);
1455
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001456 if (!rt_rq->rt_queued)
Peter Zijlstra606dba22012-02-11 06:05:00 +01001457 return NULL;
1458
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001459 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001460
1461 p = _pick_next_task_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001462
1463 /* The running task is never eligible for pushing */
1464 if (p)
1465 dequeue_pushable_task(rq, p);
1466
Peter Zijlstradc877342014-02-12 15:47:29 +01001467 set_post_schedule(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04001468
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001469 return p;
1470}
1471
Ingo Molnar31ee5292007-08-09 11:16:49 +02001472static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001473{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +02001474 update_curr_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001475
1476 /*
1477 * The previous task needs to be made eligible for pushing
1478 * if it is still active
1479 */
Peter Zijlstra29baa742012-04-23 12:11:21 +02001480 if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001481 enqueue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001482}
1483
Peter Williams681f3e62007-10-24 18:23:51 +02001484#ifdef CONFIG_SMP
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001485
Steven Rostedte8fa1362008-01-25 21:08:05 +01001486/* Only try algorithms three times */
1487#define RT_MAX_TRIES 3
1488
Steven Rostedtf65eda42008-01-25 21:08:07 +01001489static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1490{
1491 if (!task_running(rq, p) &&
Kirill Tkhai60334ca2013-01-31 18:56:17 +04001492 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001493 return 1;
1494 return 0;
1495}
1496
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001497/*
1498 * Return the highest pushable rq's task, which is suitable to be executed
1499 * on the cpu, NULL otherwise
1500 */
1501static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001502{
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001503 struct plist_head *head = &rq->rt.pushable_tasks;
1504 struct task_struct *p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001505
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001506 if (!has_pushable_tasks(rq))
1507 return NULL;
Peter Zijlstra3d074672010-03-10 17:07:24 +01001508
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001509 plist_for_each_entry(p, head, pushable_tasks) {
1510 if (pick_rt_task(rq, p, cpu))
1511 return p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001512 }
1513
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001514 return NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001515}
1516
Rusty Russell0e3900e2008-11-25 02:35:13 +10301517static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001518
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001519static int find_lowest_rq(struct task_struct *task)
1520{
1521 struct sched_domain *sd;
Rusty Russell96f874e2008-11-25 02:35:14 +10301522 struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001523 int this_cpu = smp_processor_id();
1524 int cpu = task_cpu(task);
1525
Steven Rostedt0da938c2011-06-14 18:36:25 -04001526 /* Make sure the mask is initialized first */
1527 if (unlikely(!lowest_mask))
1528 return -1;
1529
Peter Zijlstra29baa742012-04-23 12:11:21 +02001530 if (task->nr_cpus_allowed == 1)
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001531 return -1; /* No other targets possible */
1532
1533 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
Gregory Haskins06f90db2008-01-25 21:08:13 +01001534 return -1; /* No targets found */
1535
1536 /*
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001537 * At this point we have built a mask of cpus representing the
1538 * lowest priority tasks in the system. Now we want to elect
1539 * the best one based on our affinity and topology.
1540 *
1541 * We prioritize the last cpu that the task executed on since
1542 * it is most likely cache-hot in that location.
1543 */
Rusty Russell96f874e2008-11-25 02:35:14 +10301544 if (cpumask_test_cpu(cpu, lowest_mask))
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001545 return cpu;
1546
1547 /*
1548 * Otherwise, we consult the sched_domains span maps to figure
1549 * out which cpu is logically closest to our hot cache data.
1550 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301551 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1552 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001553
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001554 rcu_read_lock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301555 for_each_domain(cpu, sd) {
1556 if (sd->flags & SD_WAKE_AFFINE) {
1557 int best_cpu;
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001558
Rusty Russelle2c88062009-11-03 14:53:15 +10301559 /*
1560 * "this_cpu" is cheaper to preempt than a
1561 * remote processor.
1562 */
1563 if (this_cpu != -1 &&
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001564 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1565 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301566 return this_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001567 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001568
Rusty Russelle2c88062009-11-03 14:53:15 +10301569 best_cpu = cpumask_first_and(lowest_mask,
1570 sched_domain_span(sd));
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001571 if (best_cpu < nr_cpu_ids) {
1572 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301573 return best_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001574 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001575 }
1576 }
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001577 rcu_read_unlock();
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001578
1579 /*
1580 * And finally, if there were no matches within the domains
1581 * just give the caller *something* to work with from the compatible
1582 * locations.
1583 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301584 if (this_cpu != -1)
1585 return this_cpu;
1586
1587 cpu = cpumask_any(lowest_mask);
1588 if (cpu < nr_cpu_ids)
1589 return cpu;
1590 return -1;
Gregory Haskins07b40322008-01-25 21:08:10 +01001591}
1592
Steven Rostedte8fa1362008-01-25 21:08:05 +01001593/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +01001594static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001595{
1596 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001597 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +01001598 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001599
1600 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +01001601 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001602
Gregory Haskins2de0b462008-01-25 21:08:10 +01001603 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +01001604 break;
1605
Gregory Haskins07b40322008-01-25 21:08:10 +01001606 lowest_rq = cpu_rq(cpu);
1607
Steven Rostedte8fa1362008-01-25 21:08:05 +01001608 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +01001609 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +01001610 /*
1611 * We had to unlock the run queue. In
1612 * the mean time, task could have
1613 * migrated already or had its affinity changed.
1614 * Also make sure that it wasn't scheduled on its rq.
1615 */
Gregory Haskins07b40322008-01-25 21:08:10 +01001616 if (unlikely(task_rq(task) != rq ||
Rusty Russell96f874e2008-11-25 02:35:14 +10301617 !cpumask_test_cpu(lowest_rq->cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001618 tsk_cpus_allowed(task)) ||
Gregory Haskins07b40322008-01-25 21:08:10 +01001619 task_running(rq, task) ||
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02001620 !task->on_rq)) {
Ingo Molnar4df64c02008-01-25 21:08:15 +01001621
Peter Zijlstra7f1b4392012-05-17 21:19:46 +02001622 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001623 lowest_rq = NULL;
1624 break;
1625 }
1626 }
1627
1628 /* If this rq is still suitable use it. */
Gregory Haskinse864c492008-12-29 09:39:49 -05001629 if (lowest_rq->rt.highest_prio.curr > task->prio)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001630 break;
1631
1632 /* try again */
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001633 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001634 lowest_rq = NULL;
1635 }
1636
1637 return lowest_rq;
1638}
1639
Gregory Haskins917b6272008-12-29 09:39:53 -05001640static struct task_struct *pick_next_pushable_task(struct rq *rq)
1641{
1642 struct task_struct *p;
1643
1644 if (!has_pushable_tasks(rq))
1645 return NULL;
1646
1647 p = plist_first_entry(&rq->rt.pushable_tasks,
1648 struct task_struct, pushable_tasks);
1649
1650 BUG_ON(rq->cpu != task_cpu(p));
1651 BUG_ON(task_current(rq, p));
Peter Zijlstra29baa742012-04-23 12:11:21 +02001652 BUG_ON(p->nr_cpus_allowed <= 1);
Gregory Haskins917b6272008-12-29 09:39:53 -05001653
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02001654 BUG_ON(!p->on_rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001655 BUG_ON(!rt_task(p));
1656
1657 return p;
1658}
1659
Steven Rostedte8fa1362008-01-25 21:08:05 +01001660/*
1661 * If the current CPU has more than one RT task, see if the non
1662 * running task can migrate over to a CPU that is running a task
1663 * of lesser priority.
1664 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001665static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001666{
1667 struct task_struct *next_task;
1668 struct rq *lowest_rq;
Hillf Danton311e8002011-06-16 21:55:20 -04001669 int ret = 0;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001670
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +01001671 if (!rq->rt.overloaded)
1672 return 0;
1673
Gregory Haskins917b6272008-12-29 09:39:53 -05001674 next_task = pick_next_pushable_task(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001675 if (!next_task)
1676 return 0;
1677
Peter Zijlstra49246272010-10-17 21:46:10 +02001678retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +01001679 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001680 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001681 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001682 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01001683
1684 /*
1685 * It's possible that the next_task slipped in of
1686 * higher priority than current. If that's the case
1687 * just reschedule current.
1688 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001689 if (unlikely(next_task->prio < rq->curr->prio)) {
1690 resched_task(rq->curr);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001691 return 0;
1692 }
1693
Gregory Haskins697f0a42008-01-25 21:08:09 +01001694 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +01001695 get_task_struct(next_task);
1696
1697 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001698 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001699 if (!lowest_rq) {
1700 struct task_struct *task;
1701 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001702 * find_lock_lowest_rq releases rq->lock
Gregory Haskins15635132008-12-29 09:39:53 -05001703 * so it is possible that next_task has migrated.
1704 *
1705 * We need to make sure that the task is still on the same
1706 * run-queue and is also still the next task eligible for
1707 * pushing.
Steven Rostedte8fa1362008-01-25 21:08:05 +01001708 */
Gregory Haskins917b6272008-12-29 09:39:53 -05001709 task = pick_next_pushable_task(rq);
Gregory Haskins15635132008-12-29 09:39:53 -05001710 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1711 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001712 * The task hasn't migrated, and is still the next
1713 * eligible task, but we failed to find a run-queue
1714 * to push it to. Do not retry in this case, since
1715 * other cpus will pull from us when ready.
Gregory Haskins15635132008-12-29 09:39:53 -05001716 */
Gregory Haskins15635132008-12-29 09:39:53 -05001717 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001718 }
Gregory Haskins917b6272008-12-29 09:39:53 -05001719
Gregory Haskins15635132008-12-29 09:39:53 -05001720 if (!task)
1721 /* No more tasks, just exit */
1722 goto out;
1723
Gregory Haskins917b6272008-12-29 09:39:53 -05001724 /*
Gregory Haskins15635132008-12-29 09:39:53 -05001725 * Something has shifted, try again.
Gregory Haskins917b6272008-12-29 09:39:53 -05001726 */
Gregory Haskins15635132008-12-29 09:39:53 -05001727 put_task_struct(next_task);
1728 next_task = task;
1729 goto retry;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001730 }
1731
Gregory Haskins697f0a42008-01-25 21:08:09 +01001732 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001733 set_task_cpu(next_task, lowest_rq->cpu);
1734 activate_task(lowest_rq, next_task, 0);
Hillf Danton311e8002011-06-16 21:55:20 -04001735 ret = 1;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001736
1737 resched_task(lowest_rq->curr);
1738
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001739 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001740
Steven Rostedte8fa1362008-01-25 21:08:05 +01001741out:
1742 put_task_struct(next_task);
1743
Hillf Danton311e8002011-06-16 21:55:20 -04001744 return ret;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001745}
1746
Steven Rostedte8fa1362008-01-25 21:08:05 +01001747static void push_rt_tasks(struct rq *rq)
1748{
1749 /* push_rt_task will return true if it moved an RT */
1750 while (push_rt_task(rq))
1751 ;
1752}
1753
Steven Rostedtf65eda42008-01-25 21:08:07 +01001754static int pull_rt_task(struct rq *this_rq)
1755{
Ingo Molnar80bf3172008-01-25 21:08:17 +01001756 int this_cpu = this_rq->cpu, ret = 0, cpu;
Gregory Haskinsa8728942008-12-29 09:39:49 -05001757 struct task_struct *p;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001758 struct rq *src_rq;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001759
Gregory Haskins637f5082008-01-25 21:08:18 +01001760 if (likely(!rt_overloaded(this_rq)))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001761 return 0;
1762
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +02001763 /*
1764 * Match the barrier from rt_set_overloaded; this guarantees that if we
1765 * see overloaded we must also see the rto_mask bit.
1766 */
1767 smp_rmb();
1768
Rusty Russellc6c49272008-11-25 02:35:05 +10301769 for_each_cpu(cpu, this_rq->rd->rto_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001770 if (this_cpu == cpu)
1771 continue;
1772
1773 src_rq = cpu_rq(cpu);
Gregory Haskins74ab8e42008-12-29 09:39:50 -05001774
1775 /*
1776 * Don't bother taking the src_rq->lock if the next highest
1777 * task is known to be lower-priority than our current task.
1778 * This may look racy, but if this value is about to go
1779 * logically higher, the src_rq will push this task away.
1780 * And if its going logically lower, we do not care
1781 */
1782 if (src_rq->rt.highest_prio.next >=
1783 this_rq->rt.highest_prio.curr)
1784 continue;
1785
Steven Rostedtf65eda42008-01-25 21:08:07 +01001786 /*
1787 * We can potentially drop this_rq's lock in
1788 * double_lock_balance, and another CPU could
Gregory Haskinsa8728942008-12-29 09:39:49 -05001789 * alter this_rq
Steven Rostedtf65eda42008-01-25 21:08:07 +01001790 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05001791 double_lock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01001792
1793 /*
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001794 * We can pull only a task, which is pushable
1795 * on its rq, and no others.
Steven Rostedtf65eda42008-01-25 21:08:07 +01001796 */
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001797 p = pick_highest_pushable_task(src_rq, this_cpu);
Steven Rostedtf65eda42008-01-25 21:08:07 +01001798
1799 /*
1800 * Do we have an RT task that preempts
1801 * the to-be-scheduled task?
1802 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05001803 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001804 WARN_ON(p == src_rq->curr);
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02001805 WARN_ON(!p->on_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01001806
1807 /*
1808 * There's a chance that p is higher in priority
1809 * than what's currently running on its cpu.
1810 * This is just that p is wakeing up and hasn't
1811 * had a chance to schedule. We only pull
1812 * p if it is lower in priority than the
Gregory Haskinsa8728942008-12-29 09:39:49 -05001813 * current task on the run queue
Steven Rostedtf65eda42008-01-25 21:08:07 +01001814 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05001815 if (p->prio < src_rq->curr->prio)
Mike Galbraith614ee1f2008-01-25 21:08:30 +01001816 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001817
1818 ret = 1;
1819
1820 deactivate_task(src_rq, p, 0);
1821 set_task_cpu(p, this_cpu);
1822 activate_task(this_rq, p, 0);
1823 /*
1824 * We continue with the search, just in
1825 * case there's an even higher prio task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001826 * in another runqueue. (low likelihood
Steven Rostedtf65eda42008-01-25 21:08:07 +01001827 * but possible)
Steven Rostedtf65eda42008-01-25 21:08:07 +01001828 */
Steven Rostedtf65eda42008-01-25 21:08:07 +01001829 }
Peter Zijlstra49246272010-10-17 21:46:10 +02001830skip:
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001831 double_unlock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01001832 }
1833
1834 return ret;
1835}
1836
Steven Rostedt9a897c52008-01-25 21:08:22 +01001837static void post_schedule_rt(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001838{
Gregory Haskins967fc042008-12-29 09:39:52 -05001839 push_rt_tasks(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001840}
1841
Gregory Haskins8ae121a2008-04-23 07:13:29 -04001842/*
1843 * If we are not running and we are not going to reschedule soon, we should
1844 * try to push tasks away now
1845 */
Peter Zijlstraefbbd052009-12-16 18:04:40 +01001846static void task_woken_rt(struct rq *rq, struct task_struct *p)
Steven Rostedt4642daf2008-01-25 21:08:07 +01001847{
Steven Rostedt9a897c52008-01-25 21:08:22 +01001848 if (!task_running(rq, p) &&
Gregory Haskins8ae121a2008-04-23 07:13:29 -04001849 !test_tsk_need_resched(rq->curr) &&
Gregory Haskins917b6272008-12-29 09:39:53 -05001850 has_pushable_tasks(rq) &&
Peter Zijlstra29baa742012-04-23 12:11:21 +02001851 p->nr_cpus_allowed > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01001852 (dl_task(rq->curr) || rt_task(rq->curr)) &&
Peter Zijlstra29baa742012-04-23 12:11:21 +02001853 (rq->curr->nr_cpus_allowed < 2 ||
Shawn Bohrer3be209a2011-09-12 09:28:04 -05001854 rq->curr->prio <= p->prio))
Steven Rostedt4642daf2008-01-25 21:08:07 +01001855 push_rt_tasks(rq);
1856}
1857
Mike Traviscd8ba7c2008-03-26 14:23:49 -07001858static void set_cpus_allowed_rt(struct task_struct *p,
Rusty Russell96f874e2008-11-25 02:35:14 +10301859 const struct cpumask *new_mask)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001860{
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04001861 struct rq *rq;
1862 int weight;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001863
1864 BUG_ON(!rt_task(p));
1865
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04001866 if (!p->on_rq)
1867 return;
1868
1869 weight = cpumask_weight(new_mask);
1870
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001871 /*
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04001872 * Only update if the process changes its state from whether it
1873 * can migrate or not.
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001874 */
Peter Zijlstra29baa742012-04-23 12:11:21 +02001875 if ((p->nr_cpus_allowed > 1) == (weight > 1))
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04001876 return;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001877
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04001878 rq = task_rq(p);
Gregory Haskins917b6272008-12-29 09:39:53 -05001879
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04001880 /*
1881 * The process used to be able to migrate OR it can now migrate
1882 */
1883 if (weight <= 1) {
1884 if (!task_current(rq, p))
1885 dequeue_pushable_task(rq, p);
1886 BUG_ON(!rq->rt.rt_nr_migratory);
1887 rq->rt.rt_nr_migratory--;
1888 } else {
1889 if (!task_current(rq, p))
1890 enqueue_pushable_task(rq, p);
1891 rq->rt.rt_nr_migratory++;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001892 }
Kirill Tkhai8d3d5ad2012-04-11 09:06:04 +04001893
1894 update_rt_migration(&rq->rt);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001895}
Ingo Molnardeeeccd2008-01-25 21:08:15 +01001896
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001897/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001898static void rq_online_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001899{
1900 if (rq->rt.overloaded)
1901 rt_set_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001902
Peter Zijlstra7def2be2008-06-05 14:49:58 +02001903 __enable_runtime(rq);
1904
Gregory Haskinse864c492008-12-29 09:39:49 -05001905 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001906}
1907
1908/* Assumes rq->lock is held */
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001909static void rq_offline_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001910{
1911 if (rq->rt.overloaded)
1912 rt_clear_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001913
Peter Zijlstra7def2be2008-06-05 14:49:58 +02001914 __disable_runtime(rq);
1915
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001916 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01001917}
Steven Rostedtcb469842008-01-25 21:08:22 +01001918
1919/*
1920 * When switch from the rt queue, we bring ourselves to a position
1921 * that we might want to pull RT tasks from other runqueues.
1922 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001923static void switched_from_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01001924{
1925 /*
1926 * If there are other RT tasks then we will reschedule
1927 * and the scheduling of the other RT tasks will handle
1928 * the balancing. But if we are the last RT task
1929 * we may need to handle the pulling of RT tasks
1930 * now.
1931 */
Kirill Tkhai1158ddb2012-11-23 00:02:15 +04001932 if (!p->on_rq || rq->rt.rt_nr_running)
1933 return;
1934
1935 if (pull_rt_task(rq))
1936 resched_task(rq->curr);
Steven Rostedtcb469842008-01-25 21:08:22 +01001937}
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10301938
Li Zefan11c785b2014-02-08 14:17:45 +08001939void __init init_sched_rt_class(void)
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10301940{
1941 unsigned int i;
1942
Peter Zijlstra029632f2011-10-25 10:00:11 +02001943 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -07001944 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
Mike Travis6ca09df2008-12-31 18:08:45 -08001945 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra029632f2011-10-25 10:00:11 +02001946 }
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10301947}
Steven Rostedte8fa1362008-01-25 21:08:05 +01001948#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001949
Steven Rostedtcb469842008-01-25 21:08:22 +01001950/*
1951 * When switching a task to RT, we may overload the runqueue
1952 * with RT tasks. In this case we try to push them off to
1953 * other runqueues.
1954 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001955static void switched_to_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01001956{
1957 int check_resched = 1;
1958
1959 /*
1960 * If we are already running, then there's nothing
1961 * that needs to be done. But if we are not running
1962 * we may need to preempt the current running task.
1963 * If that current running task is also an RT task
1964 * then see if we can move to another run queue.
1965 */
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02001966 if (p->on_rq && rq->curr != p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01001967#ifdef CONFIG_SMP
Kirill V Tkhai10447912014-03-12 06:18:33 -04001968 if (p->nr_cpus_allowed > 1 && rq->rt.overloaded &&
Steven Rostedtcb469842008-01-25 21:08:22 +01001969 /* Don't resched if we changed runqueues */
Kirill V Tkhai10447912014-03-12 06:18:33 -04001970 push_rt_task(rq) && rq != task_rq(p))
Steven Rostedtcb469842008-01-25 21:08:22 +01001971 check_resched = 0;
1972#endif /* CONFIG_SMP */
1973 if (check_resched && p->prio < rq->curr->prio)
1974 resched_task(rq->curr);
1975 }
1976}
1977
1978/*
1979 * Priority of the task has changed. This may cause
1980 * us to initiate a push or pull.
1981 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001982static void
1983prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01001984{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02001985 if (!p->on_rq)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001986 return;
1987
1988 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01001989#ifdef CONFIG_SMP
1990 /*
1991 * If our priority decreases while running, we
1992 * may need to pull tasks to this runqueue.
1993 */
1994 if (oldprio < p->prio)
1995 pull_rt_task(rq);
1996 /*
1997 * If there's a higher priority task waiting to run
Steven Rostedt6fa46fa2008-03-05 10:00:12 -05001998 * then reschedule. Note, the above pull_rt_task
1999 * can release the rq lock and p could migrate.
2000 * Only reschedule if p is still on the same runqueue.
Steven Rostedtcb469842008-01-25 21:08:22 +01002001 */
Gregory Haskinse864c492008-12-29 09:39:49 -05002002 if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002003 resched_task(p);
2004#else
2005 /* For UP simply resched on drop of prio */
2006 if (oldprio < p->prio)
2007 resched_task(p);
2008#endif /* CONFIG_SMP */
2009 } else {
2010 /*
2011 * This task is not running, but if it is
2012 * greater than the current running task
2013 * then reschedule.
2014 */
2015 if (p->prio < rq->curr->prio)
2016 resched_task(rq->curr);
2017 }
2018}
2019
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002020static void watchdog(struct rq *rq, struct task_struct *p)
2021{
2022 unsigned long soft, hard;
2023
Jiri Slaby78d7d402010-03-05 13:42:54 -08002024 /* max may change after cur was read, this will be fixed next tick */
2025 soft = task_rlimit(p, RLIMIT_RTTIME);
2026 hard = task_rlimit_max(p, RLIMIT_RTTIME);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002027
2028 if (soft != RLIM_INFINITY) {
2029 unsigned long next;
2030
Ying Xue57d2aa02012-07-17 15:03:43 +08002031 if (p->rt.watchdog_stamp != jiffies) {
2032 p->rt.timeout++;
2033 p->rt.watchdog_stamp = jiffies;
2034 }
2035
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002036 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
Peter Zijlstra5a52dd52008-01-25 21:08:32 +01002037 if (p->rt.timeout > next)
Frank Mayharf06febc2008-09-12 09:54:39 -07002038 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002039 }
2040}
Steven Rostedtcb469842008-01-25 21:08:22 +01002041
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002042static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002043{
Colin Cross454c7992012-05-16 21:34:23 -07002044 struct sched_rt_entity *rt_se = &p->rt;
2045
Peter Zijlstra67e2be02007-12-20 15:01:17 +01002046 update_curr_rt(rq);
2047
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002048 watchdog(rq, p);
2049
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002050 /*
2051 * RR tasks need a special form of timeslice management.
2052 * FIFO tasks have no timeslices.
2053 */
2054 if (p->policy != SCHED_RR)
2055 return;
2056
Peter Zijlstrafa717062008-01-25 21:08:27 +01002057 if (--p->rt.time_slice)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002058 return;
2059
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002060 p->rt.time_slice = sched_rr_timeslice;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002061
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002062 /*
Li Bine9aa39b2013-10-21 20:15:43 +08002063 * Requeue to the end of queue if we (and all of our ancestors) are not
2064 * the only element on the queue
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002065 */
Colin Cross454c7992012-05-16 21:34:23 -07002066 for_each_sched_rt_entity(rt_se) {
2067 if (rt_se->run_list.prev != rt_se->run_list.next) {
2068 requeue_task_rt(rq, p, 0);
2069 set_tsk_need_resched(p);
2070 return;
2071 }
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002072 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002073}
2074
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002075static void set_curr_task_rt(struct rq *rq)
2076{
2077 struct task_struct *p = rq->curr;
2078
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002079 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05002080
2081 /* The running task is never eligible for pushing */
2082 dequeue_pushable_task(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002083}
2084
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07002085static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00002086{
2087 /*
2088 * Time slice is 0 for SCHED_FIFO tasks
2089 */
2090 if (task->policy == SCHED_RR)
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002091 return sched_rr_timeslice;
Peter Williams0d721ce2009-09-21 01:31:53 +00002092 else
2093 return 0;
2094}
2095
Peter Zijlstra029632f2011-10-25 10:00:11 +02002096const struct sched_class rt_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002097 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002098 .enqueue_task = enqueue_task_rt,
2099 .dequeue_task = dequeue_task_rt,
2100 .yield_task = yield_task_rt,
2101
2102 .check_preempt_curr = check_preempt_curr_rt,
2103
2104 .pick_next_task = pick_next_task_rt,
2105 .put_prev_task = put_prev_task_rt,
2106
Peter Williams681f3e62007-10-24 18:23:51 +02002107#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08002108 .select_task_rq = select_task_rq_rt,
2109
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01002110 .set_cpus_allowed = set_cpus_allowed_rt,
Gregory Haskins1f11eb62008-06-04 15:04:05 -04002111 .rq_online = rq_online_rt,
2112 .rq_offline = rq_offline_rt,
Steven Rostedt9a897c52008-01-25 21:08:22 +01002113 .post_schedule = post_schedule_rt,
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002114 .task_woken = task_woken_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002115 .switched_from = switched_from_rt,
Peter Williams681f3e62007-10-24 18:23:51 +02002116#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002117
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002118 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002119 .task_tick = task_tick_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002120
Peter Williams0d721ce2009-09-21 01:31:53 +00002121 .get_rr_interval = get_rr_interval_rt,
2122
Steven Rostedtcb469842008-01-25 21:08:22 +01002123 .prio_changed = prio_changed_rt,
2124 .switched_to = switched_to_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002125};
Peter Zijlstraada18de2008-06-19 14:22:24 +02002126
2127#ifdef CONFIG_SCHED_DEBUG
2128extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2129
Peter Zijlstra029632f2011-10-25 10:00:11 +02002130void print_rt_stats(struct seq_file *m, int cpu)
Peter Zijlstraada18de2008-06-19 14:22:24 +02002131{
Cheng Xuec514c42011-05-14 14:20:02 +08002132 rt_rq_iter_t iter;
Peter Zijlstraada18de2008-06-19 14:22:24 +02002133 struct rt_rq *rt_rq;
2134
2135 rcu_read_lock();
Cheng Xuec514c42011-05-14 14:20:02 +08002136 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
Peter Zijlstraada18de2008-06-19 14:22:24 +02002137 print_rt_rq(m, cpu, rt_rq);
2138 rcu_read_unlock();
2139}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302140#endif /* CONFIG_SCHED_DEBUG */