blob: 663b2355a3aa772d8bcc8c90b55a3e0e0e3a6e17 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002/*
3 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
4 * policies)
5 */
6
Peter Zijlstra029632f2011-10-25 10:00:11 +02007#include "sched.h"
8
9#include <linux/slab.h>
Steven Rostedtb6366f02015-03-18 14:49:46 -040010#include <linux/irq_work.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020011
Clark Williamsce0dbbb2013-02-07 09:47:04 -060012int sched_rr_timeslice = RR_TIMESLICE;
Shile Zhang975e1552017-01-28 22:00:49 +080013int sysctl_sched_rr_timeslice = (MSEC_PER_SEC / HZ) * RR_TIMESLICE;
Clark Williamsce0dbbb2013-02-07 09:47:04 -060014
Peter Zijlstra029632f2011-10-25 10:00:11 +020015static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
16
17struct rt_bandwidth def_rt_bandwidth;
18
19static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
20{
21 struct rt_bandwidth *rt_b =
22 container_of(timer, struct rt_bandwidth, rt_period_timer);
Peter Zijlstra029632f2011-10-25 10:00:11 +020023 int idle = 0;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020024 int overrun;
Peter Zijlstra029632f2011-10-25 10:00:11 +020025
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020026 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020027 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020028 overrun = hrtimer_forward_now(timer, rt_b->rt_period);
Peter Zijlstra029632f2011-10-25 10:00:11 +020029 if (!overrun)
30 break;
31
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020032 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020033 idle = do_sched_rt_period_timer(rt_b, overrun);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020034 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020035 }
Peter Zijlstra4cfafd32015-05-14 12:23:11 +020036 if (idle)
37 rt_b->rt_period_active = 0;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +020038 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +020039
40 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
41}
42
43void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
44{
45 rt_b->rt_period = ns_to_ktime(period);
46 rt_b->rt_runtime = runtime;
47
48 raw_spin_lock_init(&rt_b->rt_runtime_lock);
49
50 hrtimer_init(&rt_b->rt_period_timer,
51 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
52 rt_b->rt_period_timer.function = sched_rt_period_timer;
53}
54
55static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
56{
57 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
58 return;
59
Peter Zijlstra029632f2011-10-25 10:00:11 +020060 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +020061 if (!rt_b->rt_period_active) {
62 rt_b->rt_period_active = 1;
Steven Rostedtc3a990d2016-02-16 18:37:46 -050063 /*
64 * SCHED_DEADLINE updates the bandwidth, as a run away
65 * RT task with a DL task could hog a CPU. But DL does
66 * not reset the period. If a deadline task was running
67 * without an RT task running, it can cause RT tasks to
68 * throttle when they start up. Kick the timer right away
69 * to update the period.
70 */
71 hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0));
Peter Zijlstra4cfafd32015-05-14 12:23:11 +020072 hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED);
73 }
Peter Zijlstra029632f2011-10-25 10:00:11 +020074 raw_spin_unlock(&rt_b->rt_runtime_lock);
75}
76
Abel Vesa07c54f72015-03-03 13:50:27 +020077void init_rt_rq(struct rt_rq *rt_rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +020078{
79 struct rt_prio_array *array;
80 int i;
81
82 array = &rt_rq->active;
83 for (i = 0; i < MAX_RT_PRIO; i++) {
84 INIT_LIST_HEAD(array->queue + i);
85 __clear_bit(i, array->bitmap);
86 }
87 /* delimiter for bitsearch: */
88 __set_bit(MAX_RT_PRIO, array->bitmap);
89
90#if defined CONFIG_SMP
91 rt_rq->highest_prio.curr = MAX_RT_PRIO;
92 rt_rq->highest_prio.next = MAX_RT_PRIO;
93 rt_rq->rt_nr_migratory = 0;
94 rt_rq->overloaded = 0;
95 plist_head_init(&rt_rq->pushable_tasks);
Steven Rostedtb6366f02015-03-18 14:49:46 -040096#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +040097 /* We start is dequeued state, because no RT tasks are queued */
98 rt_rq->rt_queued = 0;
Peter Zijlstra029632f2011-10-25 10:00:11 +020099
100 rt_rq->rt_time = 0;
101 rt_rq->rt_throttled = 0;
102 rt_rq->rt_runtime = 0;
103 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
104}
105
Gregory Haskins398a1532009-01-14 09:10:04 -0500106#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +0200107static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
108{
109 hrtimer_cancel(&rt_b->rt_period_timer);
110}
Gregory Haskins398a1532009-01-14 09:10:04 -0500111
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200112#define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
113
Peter Zijlstra8f488942009-07-24 12:25:30 +0200114static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
115{
116#ifdef CONFIG_SCHED_DEBUG
117 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
118#endif
119 return container_of(rt_se, struct task_struct, rt);
120}
121
Gregory Haskins398a1532009-01-14 09:10:04 -0500122static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
123{
124 return rt_rq->rq;
125}
126
127static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
128{
129 return rt_se->rt_rq;
130}
131
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400132static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
133{
134 struct rt_rq *rt_rq = rt_se->rt_rq;
135
136 return rt_rq->rq;
137}
138
Peter Zijlstra029632f2011-10-25 10:00:11 +0200139void free_rt_sched_group(struct task_group *tg)
140{
141 int i;
142
143 if (tg->rt_se)
144 destroy_rt_bandwidth(&tg->rt_bandwidth);
145
146 for_each_possible_cpu(i) {
147 if (tg->rt_rq)
148 kfree(tg->rt_rq[i]);
149 if (tg->rt_se)
150 kfree(tg->rt_se[i]);
151 }
152
153 kfree(tg->rt_rq);
154 kfree(tg->rt_se);
155}
156
157void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
158 struct sched_rt_entity *rt_se, int cpu,
159 struct sched_rt_entity *parent)
160{
161 struct rq *rq = cpu_rq(cpu);
162
163 rt_rq->highest_prio.curr = MAX_RT_PRIO;
164 rt_rq->rt_nr_boosted = 0;
165 rt_rq->rq = rq;
166 rt_rq->tg = tg;
167
168 tg->rt_rq[cpu] = rt_rq;
169 tg->rt_se[cpu] = rt_se;
170
171 if (!rt_se)
172 return;
173
174 if (!parent)
175 rt_se->rt_rq = &rq->rt;
176 else
177 rt_se->rt_rq = parent->my_q;
178
179 rt_se->my_q = rt_rq;
180 rt_se->parent = parent;
181 INIT_LIST_HEAD(&rt_se->run_list);
182}
183
184int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
185{
186 struct rt_rq *rt_rq;
187 struct sched_rt_entity *rt_se;
188 int i;
189
190 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
191 if (!tg->rt_rq)
192 goto err;
193 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
194 if (!tg->rt_se)
195 goto err;
196
197 init_rt_bandwidth(&tg->rt_bandwidth,
198 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
199
200 for_each_possible_cpu(i) {
201 rt_rq = kzalloc_node(sizeof(struct rt_rq),
202 GFP_KERNEL, cpu_to_node(i));
203 if (!rt_rq)
204 goto err;
205
206 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
207 GFP_KERNEL, cpu_to_node(i));
208 if (!rt_se)
209 goto err_free_rq;
210
Abel Vesa07c54f72015-03-03 13:50:27 +0200211 init_rt_rq(rt_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200212 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
213 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
214 }
215
216 return 1;
217
218err_free_rq:
219 kfree(rt_rq);
220err:
221 return 0;
222}
223
Gregory Haskins398a1532009-01-14 09:10:04 -0500224#else /* CONFIG_RT_GROUP_SCHED */
225
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200226#define rt_entity_is_task(rt_se) (1)
227
Peter Zijlstra8f488942009-07-24 12:25:30 +0200228static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
229{
230 return container_of(rt_se, struct task_struct, rt);
231}
232
Gregory Haskins398a1532009-01-14 09:10:04 -0500233static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
234{
235 return container_of(rt_rq, struct rq, rt);
236}
237
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400238static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
Gregory Haskins398a1532009-01-14 09:10:04 -0500239{
240 struct task_struct *p = rt_task_of(rt_se);
Kirill Tkhai653d07a2014-03-15 02:14:55 +0400241
242 return task_rq(p);
243}
244
245static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
246{
247 struct rq *rq = rq_of_rt_se(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -0500248
249 return &rq->rt;
250}
251
Peter Zijlstra029632f2011-10-25 10:00:11 +0200252void free_rt_sched_group(struct task_group *tg) { }
253
254int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
255{
256 return 1;
257}
Gregory Haskins398a1532009-01-14 09:10:04 -0500258#endif /* CONFIG_RT_GROUP_SCHED */
259
Steven Rostedt4fd29172008-01-25 21:08:06 +0100260#ifdef CONFIG_SMP
Ingo Molnar84de4272008-01-25 21:08:15 +0100261
Peter Zijlstra8046d682015-06-11 14:46:40 +0200262static void pull_rt_task(struct rq *this_rq);
Peter Zijlstra38033c32014-01-23 20:32:21 +0100263
Peter Zijlstradc877342014-02-12 15:47:29 +0100264static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
265{
266 /* Try to pull RT tasks here if we lower this rq's prio */
267 return rq->rt.highest_prio.curr > prev->prio;
268}
269
Gregory Haskins637f5082008-01-25 21:08:18 +0100270static inline int rt_overloaded(struct rq *rq)
Steven Rostedt4fd29172008-01-25 21:08:06 +0100271{
Gregory Haskins637f5082008-01-25 21:08:18 +0100272 return atomic_read(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100273}
Ingo Molnar84de4272008-01-25 21:08:15 +0100274
Steven Rostedt4fd29172008-01-25 21:08:06 +0100275static inline void rt_set_overload(struct rq *rq)
276{
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -0400277 if (!rq->online)
278 return;
279
Rusty Russellc6c49272008-11-25 02:35:05 +1030280 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100281 /*
282 * Make sure the mask is visible before we set
283 * the overload count. That is checked to determine
284 * if we should look at the mask. It would be a shame
285 * if we looked at the mask, but the mask was not
286 * updated yet.
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200287 *
288 * Matched by the barrier in pull_rt_task().
Steven Rostedt4fd29172008-01-25 21:08:06 +0100289 */
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +0200290 smp_wmb();
Gregory Haskins637f5082008-01-25 21:08:18 +0100291 atomic_inc(&rq->rd->rto_count);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100292}
Ingo Molnar84de4272008-01-25 21:08:15 +0100293
Steven Rostedt4fd29172008-01-25 21:08:06 +0100294static inline void rt_clear_overload(struct rq *rq)
295{
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -0400296 if (!rq->online)
297 return;
298
Steven Rostedt4fd29172008-01-25 21:08:06 +0100299 /* the order here really doesn't matter */
Gregory Haskins637f5082008-01-25 21:08:18 +0100300 atomic_dec(&rq->rd->rto_count);
Rusty Russellc6c49272008-11-25 02:35:05 +1030301 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
Steven Rostedt4fd29172008-01-25 21:08:06 +0100302}
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100303
Gregory Haskins398a1532009-01-14 09:10:04 -0500304static void update_rt_migration(struct rt_rq *rt_rq)
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100305{
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200306 if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
Gregory Haskins398a1532009-01-14 09:10:04 -0500307 if (!rt_rq->overloaded) {
308 rt_set_overload(rq_of_rt_rq(rt_rq));
309 rt_rq->overloaded = 1;
Gregory Haskinscdc8eb92008-01-25 21:08:23 +0100310 }
Gregory Haskins398a1532009-01-14 09:10:04 -0500311 } else if (rt_rq->overloaded) {
312 rt_clear_overload(rq_of_rt_rq(rt_rq));
313 rt_rq->overloaded = 0;
Gregory Haskins637f5082008-01-25 21:08:18 +0100314 }
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100315}
Steven Rostedt4fd29172008-01-25 21:08:06 +0100316
Gregory Haskins398a1532009-01-14 09:10:04 -0500317static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100318{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200319 struct task_struct *p;
320
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200321 if (!rt_entity_is_task(rt_se))
322 return;
323
Peter Zijlstra29baa742012-04-23 12:11:21 +0200324 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200325 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
326
327 rt_rq->rt_nr_total++;
Ingo Molnar4b53a342017-02-05 15:41:03 +0100328 if (p->nr_cpus_allowed > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500329 rt_rq->rt_nr_migratory++;
330
331 update_rt_migration(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100332}
333
Gregory Haskins398a1532009-01-14 09:10:04 -0500334static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
335{
Peter Zijlstra29baa742012-04-23 12:11:21 +0200336 struct task_struct *p;
337
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200338 if (!rt_entity_is_task(rt_se))
339 return;
340
Peter Zijlstra29baa742012-04-23 12:11:21 +0200341 p = rt_task_of(rt_se);
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200342 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
343
344 rt_rq->rt_nr_total--;
Ingo Molnar4b53a342017-02-05 15:41:03 +0100345 if (p->nr_cpus_allowed > 1)
Gregory Haskins398a1532009-01-14 09:10:04 -0500346 rt_rq->rt_nr_migratory--;
347
348 update_rt_migration(rt_rq);
349}
350
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400351static inline int has_pushable_tasks(struct rq *rq)
352{
353 return !plist_head_empty(&rq->rt.pushable_tasks);
354}
355
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +0200356static DEFINE_PER_CPU(struct callback_head, rt_push_head);
357static DEFINE_PER_CPU(struct callback_head, rt_pull_head);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200358
359static void push_rt_tasks(struct rq *);
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +0200360static void pull_rt_task(struct rq *);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200361
362static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100363{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200364 if (!has_pushable_tasks(rq))
365 return;
366
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +0200367 queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
368}
369
370static inline void queue_pull_task(struct rq *rq)
371{
372 queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
Peter Zijlstradc877342014-02-12 15:47:29 +0100373}
374
Gregory Haskins917b6272008-12-29 09:39:53 -0500375static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
376{
377 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
378 plist_node_init(&p->pushable_tasks, p->prio);
379 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400380
381 /* Update the highest prio pushable task */
382 if (p->prio < rq->rt.highest_prio.next)
383 rq->rt.highest_prio.next = p->prio;
Gregory Haskins917b6272008-12-29 09:39:53 -0500384}
385
386static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
387{
388 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
Gregory Haskins917b6272008-12-29 09:39:53 -0500389
Steven Rostedt5181f4a42011-06-16 21:55:23 -0400390 /* Update the new highest prio pushable task */
391 if (has_pushable_tasks(rq)) {
392 p = plist_first_entry(&rq->rt.pushable_tasks,
393 struct task_struct, pushable_tasks);
394 rq->rt.highest_prio.next = p->prio;
395 } else
396 rq->rt.highest_prio.next = MAX_RT_PRIO;
Ingo Molnarbcf08df2008-04-19 12:11:10 +0200397}
398
Gregory Haskins917b6272008-12-29 09:39:53 -0500399#else
400
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100401static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
402{
403}
404
405static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
406{
407}
408
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500409static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100410void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
411{
412}
413
Gregory Haskinsb07430a2009-01-14 08:55:39 -0500414static inline
Peter Zijlstraceacc2c2009-01-16 14:46:40 +0100415void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
416{
417}
Gregory Haskins917b6272008-12-29 09:39:53 -0500418
Peter Zijlstradc877342014-02-12 15:47:29 +0100419static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
420{
421 return false;
422}
423
Peter Zijlstra8046d682015-06-11 14:46:40 +0200424static inline void pull_rt_task(struct rq *this_rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100425{
Peter Zijlstradc877342014-02-12 15:47:29 +0100426}
427
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200428static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100429{
430}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200431#endif /* CONFIG_SMP */
432
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400433static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
434static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
435
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100436static inline int on_rt_rq(struct sched_rt_entity *rt_se)
437{
Peter Zijlstraff77e462016-01-18 15:27:07 +0100438 return rt_se->on_rq;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100439}
440
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100441#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100442
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100443static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100444{
445 if (!rt_rq->tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100446 return RUNTIME_INF;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100447
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200448 return rt_rq->rt_runtime;
449}
450
451static inline u64 sched_rt_period(struct rt_rq *rt_rq)
452{
453 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100454}
455
Cheng Xuec514c42011-05-14 14:20:02 +0800456typedef struct task_group *rt_rq_iter_t;
457
Yong Zhang1c09ab02011-06-28 10:51:31 +0800458static inline struct task_group *next_task_group(struct task_group *tg)
459{
460 do {
461 tg = list_entry_rcu(tg->list.next,
462 typeof(struct task_group), list);
463 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
464
465 if (&tg->list == &task_groups)
466 tg = NULL;
467
468 return tg;
469}
470
471#define for_each_rt_rq(rt_rq, iter, rq) \
472 for (iter = container_of(&task_groups, typeof(*iter), list); \
473 (iter = next_task_group(iter)) && \
474 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
Cheng Xuec514c42011-05-14 14:20:02 +0800475
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100476#define for_each_sched_rt_entity(rt_se) \
477 for (; rt_se; rt_se = rt_se->parent)
478
479static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
480{
481 return rt_se->my_q;
482}
483
Peter Zijlstraff77e462016-01-18 15:27:07 +0100484static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
485static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100486
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100487static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100488{
Dario Faggiolif6121f42008-10-03 17:40:46 +0200489 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
Kirill Tkhai88751252014-06-29 00:03:57 +0400490 struct rq *rq = rq_of_rt_rq(rt_rq);
Yong Zhang74b7eb52010-01-29 14:57:52 +0800491 struct sched_rt_entity *rt_se;
492
Kirill Tkhai88751252014-06-29 00:03:57 +0400493 int cpu = cpu_of(rq);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530494
495 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100496
Dario Faggiolif6121f42008-10-03 17:40:46 +0200497 if (rt_rq->rt_nr_running) {
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400498 if (!rt_se)
499 enqueue_top_rt_rq(rt_rq);
500 else if (!on_rt_rq(rt_se))
Peter Zijlstraff77e462016-01-18 15:27:07 +0100501 enqueue_rt_entity(rt_se, 0);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400502
Gregory Haskinse864c492008-12-29 09:39:49 -0500503 if (rt_rq->highest_prio.curr < curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +0400504 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100505 }
506}
507
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100508static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100509{
Yong Zhang74b7eb52010-01-29 14:57:52 +0800510 struct sched_rt_entity *rt_se;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530511 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
Yong Zhang74b7eb52010-01-29 14:57:52 +0800512
Balbir Singh0c3b9162011-03-03 17:04:35 +0530513 rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100514
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400515 if (!rt_se)
516 dequeue_top_rt_rq(rt_rq);
517 else if (on_rt_rq(rt_se))
Peter Zijlstraff77e462016-01-18 15:27:07 +0100518 dequeue_rt_entity(rt_se, 0);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100519}
520
Kirill Tkhai46383642014-03-15 02:15:07 +0400521static inline int rt_rq_throttled(struct rt_rq *rt_rq)
522{
523 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
524}
525
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100526static int rt_se_boosted(struct sched_rt_entity *rt_se)
527{
528 struct rt_rq *rt_rq = group_rt_rq(rt_se);
529 struct task_struct *p;
530
531 if (rt_rq)
532 return !!rt_rq->rt_nr_boosted;
533
534 p = rt_task_of(rt_se);
535 return p->prio != p->normal_prio;
536}
537
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200538#ifdef CONFIG_SMP
Rusty Russellc6c49272008-11-25 02:35:05 +1030539static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200540{
Nathan Zimmer424c93f2013-05-09 11:24:03 -0500541 return this_rq()->rd->span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200542}
543#else
Rusty Russellc6c49272008-11-25 02:35:05 +1030544static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200545{
Rusty Russellc6c49272008-11-25 02:35:05 +1030546 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200547}
548#endif
549
550static inline
551struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
552{
553 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
554}
555
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200556static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
557{
558 return &rt_rq->tg->rt_bandwidth;
559}
560
Dhaval Giani55e12e52008-06-24 23:39:43 +0530561#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100562
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100563static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100564{
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200565 return rt_rq->rt_runtime;
566}
567
568static inline u64 sched_rt_period(struct rt_rq *rt_rq)
569{
570 return ktime_to_ns(def_rt_bandwidth.rt_period);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100571}
572
Cheng Xuec514c42011-05-14 14:20:02 +0800573typedef struct rt_rq *rt_rq_iter_t;
574
575#define for_each_rt_rq(rt_rq, iter, rq) \
576 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
577
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100578#define for_each_sched_rt_entity(rt_se) \
579 for (; rt_se; rt_se = NULL)
580
581static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
582{
583 return NULL;
584}
585
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100586static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100587{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400588 struct rq *rq = rq_of_rt_rq(rt_rq);
589
590 if (!rt_rq->rt_nr_running)
591 return;
592
593 enqueue_top_rt_rq(rt_rq);
Kirill Tkhai88751252014-06-29 00:03:57 +0400594 resched_curr(rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100595}
596
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100597static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100598{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400599 dequeue_top_rt_rq(rt_rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100600}
601
Kirill Tkhai46383642014-03-15 02:15:07 +0400602static inline int rt_rq_throttled(struct rt_rq *rt_rq)
603{
604 return rt_rq->rt_throttled;
605}
606
Rusty Russellc6c49272008-11-25 02:35:05 +1030607static inline const struct cpumask *sched_rt_period_mask(void)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200608{
Rusty Russellc6c49272008-11-25 02:35:05 +1030609 return cpu_online_mask;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200610}
611
612static inline
613struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
614{
615 return &cpu_rq(cpu)->rt;
616}
617
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200618static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
619{
620 return &def_rt_bandwidth;
621}
622
Dhaval Giani55e12e52008-06-24 23:39:43 +0530623#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100624
Juri Lellifaa59932014-02-21 11:37:15 +0100625bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
626{
627 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
628
629 return (hrtimer_active(&rt_b->rt_period_timer) ||
630 rt_rq->rt_time < rt_b->rt_runtime);
631}
632
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200633#ifdef CONFIG_SMP
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200634/*
635 * We ran out of runtime, see if we can borrow some from our neighbours.
636 */
Juri Lelli269b26a2015-09-02 11:01:36 +0100637static void do_balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200638{
639 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
Shawn Bohreraa7f6732013-01-14 11:55:31 -0600640 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
Juri Lelli269b26a2015-09-02 11:01:36 +0100641 int i, weight;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200642 u64 rt_period;
643
Rusty Russellc6c49272008-11-25 02:35:05 +1030644 weight = cpumask_weight(rd->span);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200645
Thomas Gleixner0986b112009-11-17 15:32:06 +0100646 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200647 rt_period = ktime_to_ns(rt_b->rt_period);
Rusty Russellc6c49272008-11-25 02:35:05 +1030648 for_each_cpu(i, rd->span) {
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200649 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
650 s64 diff;
651
652 if (iter == rt_rq)
653 continue;
654
Thomas Gleixner0986b112009-11-17 15:32:06 +0100655 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200656 /*
657 * Either all rqs have inf runtime and there's nothing to steal
658 * or __disable_runtime() below sets a specific rq to inf to
659 * indicate its been disabled and disalow stealing.
660 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200661 if (iter->rt_runtime == RUNTIME_INF)
662 goto next;
663
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200664 /*
665 * From runqueues with spare time, take 1/n part of their
666 * spare time, but no more than our period.
667 */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200668 diff = iter->rt_runtime - iter->rt_time;
669 if (diff > 0) {
Peter Zijlstra58838cf2008-07-24 12:43:13 +0200670 diff = div_u64((u64)diff, weight);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200671 if (rt_rq->rt_runtime + diff > rt_period)
672 diff = rt_period - rt_rq->rt_runtime;
673 iter->rt_runtime -= diff;
674 rt_rq->rt_runtime += diff;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200675 if (rt_rq->rt_runtime == rt_period) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100676 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200677 break;
678 }
679 }
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200680next:
Thomas Gleixner0986b112009-11-17 15:32:06 +0100681 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200682 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100683 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200684}
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200685
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200686/*
687 * Ensure this RQ takes back all the runtime it lend to its neighbours.
688 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200689static void __disable_runtime(struct rq *rq)
690{
691 struct root_domain *rd = rq->rd;
Cheng Xuec514c42011-05-14 14:20:02 +0800692 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200693 struct rt_rq *rt_rq;
694
695 if (unlikely(!scheduler_running))
696 return;
697
Cheng Xuec514c42011-05-14 14:20:02 +0800698 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200699 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
700 s64 want;
701 int i;
702
Thomas Gleixner0986b112009-11-17 15:32:06 +0100703 raw_spin_lock(&rt_b->rt_runtime_lock);
704 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200705 /*
706 * Either we're all inf and nobody needs to borrow, or we're
707 * already disabled and thus have nothing to do, or we have
708 * exactly the right amount of runtime to take out.
709 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200710 if (rt_rq->rt_runtime == RUNTIME_INF ||
711 rt_rq->rt_runtime == rt_b->rt_runtime)
712 goto balanced;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100713 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200714
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200715 /*
716 * Calculate the difference between what we started out with
717 * and what we current have, that's the amount of runtime
718 * we lend and now have to reclaim.
719 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200720 want = rt_b->rt_runtime - rt_rq->rt_runtime;
721
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200722 /*
723 * Greedy reclaim, take back as much as we can.
724 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030725 for_each_cpu(i, rd->span) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200726 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
727 s64 diff;
728
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200729 /*
730 * Can't reclaim from ourselves or disabled runqueues.
731 */
Peter Zijlstraf1679d02008-08-14 15:49:00 +0200732 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200733 continue;
734
Thomas Gleixner0986b112009-11-17 15:32:06 +0100735 raw_spin_lock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200736 if (want > 0) {
737 diff = min_t(s64, iter->rt_runtime, want);
738 iter->rt_runtime -= diff;
739 want -= diff;
740 } else {
741 iter->rt_runtime -= want;
742 want -= want;
743 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100744 raw_spin_unlock(&iter->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200745
746 if (!want)
747 break;
748 }
749
Thomas Gleixner0986b112009-11-17 15:32:06 +0100750 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200751 /*
752 * We cannot be left wanting - that would mean some runtime
753 * leaked out of the system.
754 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200755 BUG_ON(want);
756balanced:
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200757 /*
758 * Disable all the borrow logic by pretending we have inf
759 * runtime - in which case borrowing doesn't make sense.
760 */
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200761 rt_rq->rt_runtime = RUNTIME_INF;
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -0700762 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100763 raw_spin_unlock(&rt_rq->rt_runtime_lock);
764 raw_spin_unlock(&rt_b->rt_runtime_lock);
Kirill Tkhai99b62562014-06-25 12:19:48 +0400765
766 /* Make rt_rq available for pick_next_task() */
767 sched_rt_rq_enqueue(rt_rq);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200768 }
769}
770
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200771static void __enable_runtime(struct rq *rq)
772{
Cheng Xuec514c42011-05-14 14:20:02 +0800773 rt_rq_iter_t iter;
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200774 struct rt_rq *rt_rq;
775
776 if (unlikely(!scheduler_running))
777 return;
778
Peter Zijlstra78333cd2008-09-23 15:33:43 +0200779 /*
780 * Reset each runqueue's bandwidth settings
781 */
Cheng Xuec514c42011-05-14 14:20:02 +0800782 for_each_rt_rq(rt_rq, iter, rq) {
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200783 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
784
Thomas Gleixner0986b112009-11-17 15:32:06 +0100785 raw_spin_lock(&rt_b->rt_runtime_lock);
786 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200787 rt_rq->rt_runtime = rt_b->rt_runtime;
788 rt_rq->rt_time = 0;
Zhang, Yanminbaf25732008-09-09 11:26:33 +0800789 rt_rq->rt_throttled = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100790 raw_spin_unlock(&rt_rq->rt_runtime_lock);
791 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstra7def2be2008-06-05 14:49:58 +0200792 }
793}
794
Juri Lelli269b26a2015-09-02 11:01:36 +0100795static void balance_runtime(struct rt_rq *rt_rq)
Peter Zijlstraeff65492008-06-19 14:22:26 +0200796{
Peter Zijlstra4a6184c2011-10-06 22:39:14 +0200797 if (!sched_feat(RT_RUNTIME_SHARE))
Juri Lelli269b26a2015-09-02 11:01:36 +0100798 return;
Peter Zijlstra4a6184c2011-10-06 22:39:14 +0200799
Peter Zijlstraeff65492008-06-19 14:22:26 +0200800 if (rt_rq->rt_time > rt_rq->rt_runtime) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100801 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Juri Lelli269b26a2015-09-02 11:01:36 +0100802 do_balance_runtime(rt_rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100803 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200804 }
Peter Zijlstraeff65492008-06-19 14:22:26 +0200805}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530806#else /* !CONFIG_SMP */
Juri Lelli269b26a2015-09-02 11:01:36 +0100807static inline void balance_runtime(struct rt_rq *rt_rq) {}
Dhaval Giani55e12e52008-06-24 23:39:43 +0530808#endif /* CONFIG_SMP */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100809
810static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
811{
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200812 int i, idle = 1, throttled = 0;
Rusty Russellc6c49272008-11-25 02:35:05 +1030813 const struct cpumask *span;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200814
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200815 span = sched_rt_period_mask();
Mike Galbraithe221d022012-08-07 10:02:38 +0200816#ifdef CONFIG_RT_GROUP_SCHED
817 /*
818 * FIXME: isolated CPUs should really leave the root task group,
819 * whether they are isolcpus or were isolated via cpusets, lest
820 * the timer run on a CPU which does not service all runqueues,
821 * potentially leaving other CPUs indefinitely throttled. If
822 * isolation is really required, the user will turn the throttle
823 * off to kill the perturbations it causes anyway. Meanwhile,
824 * this maintains functionality for boot and/or troubleshooting.
825 */
826 if (rt_b == &root_task_group.rt_bandwidth)
827 span = cpu_online_mask;
828#endif
Rusty Russellc6c49272008-11-25 02:35:05 +1030829 for_each_cpu(i, span) {
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200830 int enqueue = 0;
831 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
832 struct rq *rq = rq_of_rt_rq(rt_rq);
Dave Kleikampc249f252017-05-15 14:14:13 -0500833 int skip;
834
835 /*
836 * When span == cpu_online_mask, taking each rq->lock
837 * can be time-consuming. Try to avoid it when possible.
838 */
839 raw_spin_lock(&rt_rq->rt_runtime_lock);
840 skip = !rt_rq->rt_time && !rt_rq->rt_nr_running;
841 raw_spin_unlock(&rt_rq->rt_runtime_lock);
842 if (skip)
843 continue;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200844
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100845 raw_spin_lock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200846 if (rt_rq->rt_time) {
847 u64 runtime;
848
Thomas Gleixner0986b112009-11-17 15:32:06 +0100849 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraeff65492008-06-19 14:22:26 +0200850 if (rt_rq->rt_throttled)
851 balance_runtime(rt_rq);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200852 runtime = rt_rq->rt_runtime;
853 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
854 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
855 rt_rq->rt_throttled = 0;
856 enqueue = 1;
Mike Galbraith61eadef2011-04-29 08:36:50 +0200857
858 /*
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100859 * When we're idle and a woken (rt) task is
860 * throttled check_preempt_curr() will set
861 * skip_update and the time between the wakeup
862 * and this unthrottle will get accounted as
863 * 'runtime'.
Mike Galbraith61eadef2011-04-29 08:36:50 +0200864 */
865 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100866 rq_clock_skip_update(rq, false);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200867 }
868 if (rt_rq->rt_time || rt_rq->rt_nr_running)
869 idle = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +0100870 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Balbir Singh0c3b9162011-03-03 17:04:35 +0530871 } else if (rt_rq->rt_nr_running) {
Peter Zijlstra8a8cde12008-06-19 14:22:28 +0200872 idle = 0;
Balbir Singh0c3b9162011-03-03 17:04:35 +0530873 if (!rt_rq_throttled(rt_rq))
874 enqueue = 1;
875 }
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200876 if (rt_rq->rt_throttled)
877 throttled = 1;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200878
879 if (enqueue)
880 sched_rt_rq_enqueue(rt_rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100881 raw_spin_unlock(&rq->lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200882 }
883
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200884 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
885 return 1;
886
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200887 return idle;
888}
889
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100890static inline int rt_se_prio(struct sched_rt_entity *rt_se)
891{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100892#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100893 struct rt_rq *rt_rq = group_rt_rq(rt_se);
894
895 if (rt_rq)
Gregory Haskinse864c492008-12-29 09:39:49 -0500896 return rt_rq->highest_prio.curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100897#endif
898
899 return rt_task_of(rt_se)->prio;
900}
901
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100902static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100903{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100904 u64 runtime = sched_rt_runtime(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100905
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100906 if (rt_rq->rt_throttled)
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100907 return rt_rq_throttled(rt_rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100908
Shan Hai5b680fd2011-11-29 11:03:56 +0800909 if (runtime >= sched_rt_period(rt_rq))
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200910 return 0;
911
Peter Zijlstrab79f3832008-06-19 14:22:25 +0200912 balance_runtime(rt_rq);
913 runtime = sched_rt_runtime(rt_rq);
914 if (runtime == RUNTIME_INF)
915 return 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200916
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100917 if (rt_rq->rt_time > runtime) {
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200918 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
919
920 /*
921 * Don't actually throttle groups that have no runtime assigned
922 * but accrue some time due to boosting.
923 */
924 if (likely(rt_b->rt_runtime)) {
925 rt_rq->rt_throttled = 1;
John Stultzc2248152014-06-04 16:11:41 -0700926 printk_deferred_once("sched: RT throttling activated\n");
Peter Zijlstra7abc63b2011-10-18 22:03:48 +0200927 } else {
928 /*
929 * In case we did anyway, make it go away,
930 * replenishment is a joke, since it will replenish us
931 * with exactly 0 ns.
932 */
933 rt_rq->rt_time = 0;
934 }
935
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100936 if (rt_rq_throttled(rt_rq)) {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100937 sched_rt_rq_dequeue(rt_rq);
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100938 return 1;
939 }
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100940 }
941
942 return 0;
943}
944
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200945/*
946 * Update the current task's runtime statistics. Skip current tasks that
947 * are not in our scheduling class.
948 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200949static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200950{
951 struct task_struct *curr = rq->curr;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100952 struct sched_rt_entity *rt_se = &curr->rt;
Wen Yange7ad2032018-02-05 11:18:41 +0800953 u64 now = rq_clock_task(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200954 u64 delta_exec;
955
Peter Zijlstra06c3bc62011-02-02 13:19:48 +0100956 if (curr->sched_class != &rt_sched_class)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200957 return;
958
Wen Yange7ad2032018-02-05 11:18:41 +0800959 delta_exec = now - curr->se.exec_start;
Kirill Tkhaifc79e242013-01-30 16:50:36 +0400960 if (unlikely((s64)delta_exec <= 0))
961 return;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200962
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200963 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
Viresh Kumar674e7542017-07-28 12:16:38 +0530964 cpufreq_update_util(rq, SCHED_CPUFREQ_RT);
Wanpeng Li594dd292016-04-22 17:07:24 +0800965
Peter Zijlstra42c62a52011-10-18 22:03:48 +0200966 schedstat_set(curr->se.statistics.exec_max,
967 max(curr->se.statistics.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200968
969 curr->se.sum_exec_runtime += delta_exec;
Frank Mayharf06febc2008-09-12 09:54:39 -0700970 account_group_exec_runtime(curr, delta_exec);
971
Wen Yange7ad2032018-02-05 11:18:41 +0800972 curr->se.exec_start = now;
Tejun Heod2cc5ed2017-09-25 08:12:04 -0700973 cgroup_account_cputime(curr, delta_exec);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100974
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200975 sched_rt_avg_update(rq, delta_exec);
976
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200977 if (!rt_bandwidth_enabled())
978 return;
979
Dhaval Giani354d60c2008-04-19 19:44:59 +0200980 for_each_sched_rt_entity(rt_se) {
Giedrius Rekasius0b079392014-05-25 15:23:31 +0100981 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
Dhaval Giani354d60c2008-04-19 19:44:59 +0200982
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200983 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
Thomas Gleixner0986b112009-11-17 15:32:06 +0100984 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200985 rt_rq->rt_time += delta_exec;
986 if (sched_rt_runtime_exceeded(rt_rq))
Kirill Tkhai88751252014-06-29 00:03:57 +0400987 resched_curr(rq);
Thomas Gleixner0986b112009-11-17 15:32:06 +0100988 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstracc2991c2008-08-19 12:33:03 +0200989 }
Dhaval Giani354d60c2008-04-19 19:44:59 +0200990 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200991}
992
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400993static void
994dequeue_top_rt_rq(struct rt_rq *rt_rq)
995{
996 struct rq *rq = rq_of_rt_rq(rt_rq);
997
998 BUG_ON(&rq->rt != rt_rq);
999
1000 if (!rt_rq->rt_queued)
1001 return;
1002
1003 BUG_ON(!rq->nr_running);
1004
Kirill Tkhai72465442014-05-09 03:00:14 +04001005 sub_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001006 rt_rq->rt_queued = 0;
1007}
1008
1009static void
1010enqueue_top_rt_rq(struct rt_rq *rt_rq)
1011{
1012 struct rq *rq = rq_of_rt_rq(rt_rq);
1013
1014 BUG_ON(&rq->rt != rt_rq);
1015
1016 if (rt_rq->rt_queued)
1017 return;
1018 if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
1019 return;
1020
Kirill Tkhai72465442014-05-09 03:00:14 +04001021 add_nr_running(rq, rt_rq->rt_nr_running);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001022 rt_rq->rt_queued = 1;
1023}
1024
Gregory Haskins398a1532009-01-14 09:10:04 -05001025#if defined CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05001026
Gregory Haskins398a1532009-01-14 09:10:04 -05001027static void
1028inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001029{
Gregory Haskins4d984272008-12-29 09:39:49 -05001030 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins4d984272008-12-29 09:39:49 -05001031
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001032#ifdef CONFIG_RT_GROUP_SCHED
1033 /*
1034 * Change rq's cpupri only if rt_rq is the top queue.
1035 */
1036 if (&rq->rt != rt_rq)
1037 return;
1038#endif
Steven Rostedt5181f4a42011-06-16 21:55:23 -04001039 if (rq->online && prio < prev_prio)
1040 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
Steven Rostedt63489e42008-01-25 21:08:03 +01001041}
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001042
Gregory Haskins398a1532009-01-14 09:10:04 -05001043static void
1044dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
Steven Rostedt63489e42008-01-25 21:08:03 +01001045{
Gregory Haskins4d984272008-12-29 09:39:49 -05001046 struct rq *rq = rq_of_rt_rq(rt_rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001047
Kirill Tkhai757dfca2013-11-27 19:59:13 +04001048#ifdef CONFIG_RT_GROUP_SCHED
1049 /*
1050 * Change rq's cpupri only if rt_rq is the top queue.
1051 */
1052 if (&rq->rt != rt_rq)
1053 return;
1054#endif
Gregory Haskins398a1532009-01-14 09:10:04 -05001055 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1056 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1057}
1058
1059#else /* CONFIG_SMP */
1060
1061static inline
1062void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1063static inline
1064void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1065
1066#endif /* CONFIG_SMP */
1067
Steven Rostedt63489e42008-01-25 21:08:03 +01001068#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001069static void
1070inc_rt_prio(struct rt_rq *rt_rq, int prio)
1071{
1072 int prev_prio = rt_rq->highest_prio.curr;
Steven Rostedt63489e42008-01-25 21:08:03 +01001073
Gregory Haskins398a1532009-01-14 09:10:04 -05001074 if (prio < prev_prio)
1075 rt_rq->highest_prio.curr = prio;
1076
1077 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1078}
1079
1080static void
1081dec_rt_prio(struct rt_rq *rt_rq, int prio)
1082{
1083 int prev_prio = rt_rq->highest_prio.curr;
1084
1085 if (rt_rq->rt_nr_running) {
1086
1087 WARN_ON(prio < prev_prio);
Gregory Haskinse864c492008-12-29 09:39:49 -05001088
1089 /*
Gregory Haskins398a1532009-01-14 09:10:04 -05001090 * This may have been our highest task, and therefore
1091 * we may have some recomputation to do
Gregory Haskinse864c492008-12-29 09:39:49 -05001092 */
Gregory Haskins398a1532009-01-14 09:10:04 -05001093 if (prio == prev_prio) {
Gregory Haskinse864c492008-12-29 09:39:49 -05001094 struct rt_prio_array *array = &rt_rq->active;
1095
1096 rt_rq->highest_prio.curr =
Steven Rostedt764a9d62008-01-25 21:08:04 +01001097 sched_find_first_bit(array->bitmap);
Gregory Haskinse864c492008-12-29 09:39:49 -05001098 }
1099
Steven Rostedt764a9d62008-01-25 21:08:04 +01001100 } else
Gregory Haskinse864c492008-12-29 09:39:49 -05001101 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001102
Gregory Haskins398a1532009-01-14 09:10:04 -05001103 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1104}
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04001105
Gregory Haskins398a1532009-01-14 09:10:04 -05001106#else
1107
1108static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1109static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1110
1111#endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1112
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001113#ifdef CONFIG_RT_GROUP_SCHED
Gregory Haskins398a1532009-01-14 09:10:04 -05001114
1115static void
1116inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1117{
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01001118 if (rt_se_boosted(rt_se))
Steven Rostedt764a9d62008-01-25 21:08:04 +01001119 rt_rq->rt_nr_boosted++;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01001120
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001121 if (rt_rq->tg)
1122 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001123}
1124
1125static void
1126dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1127{
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001128 if (rt_se_boosted(rt_se))
1129 rt_rq->rt_nr_boosted--;
1130
1131 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
Gregory Haskins398a1532009-01-14 09:10:04 -05001132}
1133
1134#else /* CONFIG_RT_GROUP_SCHED */
1135
1136static void
1137inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1138{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001139 start_rt_bandwidth(&def_rt_bandwidth);
Gregory Haskins398a1532009-01-14 09:10:04 -05001140}
1141
1142static inline
1143void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1144
1145#endif /* CONFIG_RT_GROUP_SCHED */
1146
1147static inline
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001148unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1149{
1150 struct rt_rq *group_rq = group_rt_rq(rt_se);
1151
1152 if (group_rq)
1153 return group_rq->rt_nr_running;
1154 else
1155 return 1;
1156}
1157
1158static inline
Frederic Weisbecker01d36d02015-11-04 18:17:10 +01001159unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
1160{
1161 struct rt_rq *group_rq = group_rt_rq(rt_se);
1162 struct task_struct *tsk;
1163
1164 if (group_rq)
1165 return group_rq->rr_nr_running;
1166
1167 tsk = rt_task_of(rt_se);
1168
1169 return (tsk->policy == SCHED_RR) ? 1 : 0;
1170}
1171
1172static inline
Gregory Haskins398a1532009-01-14 09:10:04 -05001173void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1174{
1175 int prio = rt_se_prio(rt_se);
1176
1177 WARN_ON(!rt_prio(prio));
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001178 rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
Frederic Weisbecker01d36d02015-11-04 18:17:10 +01001179 rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
Gregory Haskins398a1532009-01-14 09:10:04 -05001180
1181 inc_rt_prio(rt_rq, prio);
1182 inc_rt_migration(rt_se, rt_rq);
1183 inc_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001184}
1185
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01001186static inline
1187void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1188{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001189 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001190 WARN_ON(!rt_rq->rt_nr_running);
Kirill Tkhai22abdef2014-03-15 02:14:49 +04001191 rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
Frederic Weisbecker01d36d02015-11-04 18:17:10 +01001192 rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001193
Gregory Haskins398a1532009-01-14 09:10:04 -05001194 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1195 dec_rt_migration(rt_se, rt_rq);
1196 dec_rt_group(rt_se, rt_rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001197}
1198
Peter Zijlstraff77e462016-01-18 15:27:07 +01001199/*
1200 * Change rt_se->run_list location unless SAVE && !MOVE
1201 *
1202 * assumes ENQUEUE/DEQUEUE flags match
1203 */
1204static inline bool move_entity(unsigned int flags)
1205{
1206 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
1207 return false;
1208
1209 return true;
1210}
1211
1212static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
1213{
1214 list_del_init(&rt_se->run_list);
1215
1216 if (list_empty(array->queue + rt_se_prio(rt_se)))
1217 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1218
1219 rt_se->on_list = 0;
1220}
1221
1222static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001223{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001224 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1225 struct rt_prio_array *array = &rt_rq->active;
1226 struct rt_rq *group_rq = group_rt_rq(rt_se);
Dmitry Adamushko20b63312008-06-11 00:58:30 +02001227 struct list_head *queue = array->queue + rt_se_prio(rt_se);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001228
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001229 /*
1230 * Don't enqueue the group if its throttled, or when empty.
1231 * The latter is a consequence of the former when a child group
1232 * get throttled and the current group doesn't have any other
1233 * active members.
1234 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01001235 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) {
1236 if (rt_se->on_list)
1237 __delist_rt_entity(rt_se, array);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001238 return;
Peter Zijlstraff77e462016-01-18 15:27:07 +01001239 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001240
Peter Zijlstraff77e462016-01-18 15:27:07 +01001241 if (move_entity(flags)) {
1242 WARN_ON_ONCE(rt_se->on_list);
1243 if (flags & ENQUEUE_HEAD)
1244 list_add(&rt_se->run_list, queue);
1245 else
1246 list_add_tail(&rt_se->run_list, queue);
1247
1248 __set_bit(rt_se_prio(rt_se), array->bitmap);
1249 rt_se->on_list = 1;
1250 }
1251 rt_se->on_rq = 1;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001252
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001253 inc_rt_tasks(rt_se, rt_rq);
1254}
1255
Peter Zijlstraff77e462016-01-18 15:27:07 +01001256static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001257{
1258 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1259 struct rt_prio_array *array = &rt_rq->active;
1260
Peter Zijlstraff77e462016-01-18 15:27:07 +01001261 if (move_entity(flags)) {
1262 WARN_ON_ONCE(!rt_se->on_list);
1263 __delist_rt_entity(rt_se, array);
1264 }
1265 rt_se->on_rq = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001266
1267 dec_rt_tasks(rt_se, rt_rq);
1268}
1269
1270/*
1271 * Because the prio of an upper entry depends on the lower
1272 * entries, we must remove entries top - down.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001273 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01001274static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001275{
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001276 struct sched_rt_entity *back = NULL;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001277
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001278 for_each_sched_rt_entity(rt_se) {
1279 rt_se->back = back;
1280 back = rt_se;
1281 }
1282
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001283 dequeue_top_rt_rq(rt_rq_of_se(back));
1284
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001285 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1286 if (on_rt_rq(rt_se))
Peter Zijlstraff77e462016-01-18 15:27:07 +01001287 __dequeue_rt_entity(rt_se, flags);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001288 }
1289}
1290
Peter Zijlstraff77e462016-01-18 15:27:07 +01001291static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001292{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001293 struct rq *rq = rq_of_rt_se(rt_se);
1294
Peter Zijlstraff77e462016-01-18 15:27:07 +01001295 dequeue_rt_stack(rt_se, flags);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001296 for_each_sched_rt_entity(rt_se)
Peter Zijlstraff77e462016-01-18 15:27:07 +01001297 __enqueue_rt_entity(rt_se, flags);
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001298 enqueue_top_rt_rq(&rq->rt);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001299}
1300
Peter Zijlstraff77e462016-01-18 15:27:07 +01001301static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001302{
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001303 struct rq *rq = rq_of_rt_se(rt_se);
1304
Peter Zijlstraff77e462016-01-18 15:27:07 +01001305 dequeue_rt_stack(rt_se, flags);
Peter Zijlstraad2a3f12008-06-19 09:06:57 +02001306
1307 for_each_sched_rt_entity(rt_se) {
1308 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1309
1310 if (rt_rq && rt_rq->rt_nr_running)
Peter Zijlstraff77e462016-01-18 15:27:07 +01001311 __enqueue_rt_entity(rt_se, flags);
Peter Zijlstra58d6c2d2008-04-19 19:45:00 +02001312 }
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001313 enqueue_top_rt_rq(&rq->rt);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001314}
1315
1316/*
1317 * Adding/removing a task to/from a priority array:
1318 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001319static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001320enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001321{
1322 struct sched_rt_entity *rt_se = &p->rt;
1323
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001324 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001325 rt_se->timeout = 0;
1326
Peter Zijlstraff77e462016-01-18 15:27:07 +01001327 enqueue_rt_entity(rt_se, flags);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001328
Ingo Molnar4b53a342017-02-05 15:41:03 +01001329 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001330 enqueue_pushable_task(rq, p);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001331}
1332
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001333static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001334{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001335 struct sched_rt_entity *rt_se = &p->rt;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001336
1337 update_curr_rt(rq);
Peter Zijlstraff77e462016-01-18 15:27:07 +01001338 dequeue_rt_entity(rt_se, flags);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001339
Gregory Haskins917b6272008-12-29 09:39:53 -05001340 dequeue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001341}
1342
1343/*
Richard Weinberger60686312011-11-12 18:07:57 +01001344 * Put task to the head or the end of the run list without the overhead of
1345 * dequeue followed by enqueue.
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001346 */
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001347static void
1348requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001349{
Ingo Molnar1cdad712008-06-19 09:09:15 +02001350 if (on_rt_rq(rt_se)) {
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001351 struct rt_prio_array *array = &rt_rq->active;
1352 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1353
1354 if (head)
1355 list_move(&rt_se->run_list, queue);
1356 else
1357 list_move_tail(&rt_se->run_list, queue);
Ingo Molnar1cdad712008-06-19 09:09:15 +02001358 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001359}
1360
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001361static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001362{
1363 struct sched_rt_entity *rt_se = &p->rt;
1364 struct rt_rq *rt_rq;
1365
1366 for_each_sched_rt_entity(rt_se) {
1367 rt_rq = rt_rq_of_se(rt_se);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001368 requeue_rt_entity(rt_rq, rt_se, head);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001369 }
1370}
1371
1372static void yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001373{
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001374 requeue_task_rt(rq, rq->curr, 0);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001375}
1376
Gregory Haskinse7693a32008-01-25 21:08:09 +01001377#ifdef CONFIG_SMP
Gregory Haskins318e0892008-01-25 21:08:10 +01001378static int find_lowest_rq(struct task_struct *task);
1379
Peter Zijlstra0017d732010-03-24 18:34:10 +01001380static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01001381select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
Gregory Haskinse7693a32008-01-25 21:08:09 +01001382{
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001383 struct task_struct *curr;
1384 struct rq *rq;
Steven Rostedtc37495f2011-06-16 21:55:22 -04001385
1386 /* For anything but wake ups, just return the task_cpu */
1387 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1388 goto out;
1389
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001390 rq = cpu_rq(cpu);
1391
1392 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07001393 curr = READ_ONCE(rq->curr); /* unlocked access */
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001394
Gregory Haskins318e0892008-01-25 21:08:10 +01001395 /*
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001396 * If the current task on @p's runqueue is an RT task, then
Steven Rostedte1f47d82008-01-25 21:08:12 +01001397 * try to see if we can wake this RT task up on another
1398 * runqueue. Otherwise simply start this RT task
1399 * on its current runqueue.
1400 *
Steven Rostedt43fa5462010-09-20 22:40:03 -04001401 * We want to avoid overloading runqueues. If the woken
1402 * task is a higher priority, then it will stay on this CPU
1403 * and the lower prio task should be moved to another CPU.
1404 * Even though this will probably make the lower prio task
1405 * lose its cache, we do not want to bounce a higher task
1406 * around just because it gave up its CPU, perhaps for a
1407 * lock?
1408 *
1409 * For equal prio tasks, we just let the scheduler sort it out.
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001410 *
Gregory Haskins318e0892008-01-25 21:08:10 +01001411 * Otherwise, just let it ride on the affined RQ and the
1412 * post-schedule router will push the preempted task away
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001413 *
1414 * This test is optimistic, if we get it wrong the load-balancer
1415 * will have to sort it out.
Gregory Haskins318e0892008-01-25 21:08:10 +01001416 */
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001417 if (curr && unlikely(rt_task(curr)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001418 (curr->nr_cpus_allowed < 2 ||
Shawn Bohrer6bfa6872013-10-04 14:24:53 -05001419 curr->prio <= p->prio)) {
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001420 int target = find_lowest_rq(p);
1421
Tim Chen80e3d872014-12-12 15:38:12 -08001422 /*
1423 * Don't bother moving it if the destination CPU is
1424 * not running a lower priority task.
1425 */
1426 if (target != -1 &&
1427 p->prio < cpu_rq(target)->rt.highest_prio.curr)
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001428 cpu = target;
1429 }
1430 rcu_read_unlock();
1431
Steven Rostedtc37495f2011-06-16 21:55:22 -04001432out:
Peter Zijlstra7608dec2011-04-05 17:23:46 +02001433 return cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001434}
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001435
1436static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1437{
Wanpeng Li308a6232014-10-31 06:39:31 +08001438 /*
1439 * Current can't be migrated, useless to reschedule,
1440 * let's hope p can move out.
1441 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001442 if (rq->curr->nr_cpus_allowed == 1 ||
Wanpeng Li308a6232014-10-31 06:39:31 +08001443 !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001444 return;
1445
Wanpeng Li308a6232014-10-31 06:39:31 +08001446 /*
1447 * p is migratable, so let's not schedule it and
1448 * see if it is pushed or pulled somewhere else.
1449 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001450 if (p->nr_cpus_allowed != 1
Rusty Russell13b8bd02009-03-25 15:01:22 +10301451 && cpupri_find(&rq->rd->cpupri, p, NULL))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001452 return;
1453
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001454 /*
1455 * There appears to be other cpus that can accept
1456 * current and none to run 'p', so lets reschedule
1457 * to try and push current away:
1458 */
1459 requeue_task_rt(rq, p, 1);
Kirill Tkhai88751252014-06-29 00:03:57 +04001460 resched_curr(rq);
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001461}
1462
Gregory Haskinse7693a32008-01-25 21:08:09 +01001463#endif /* CONFIG_SMP */
1464
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001465/*
1466 * Preempt the current task with a newly woken task if needed:
1467 */
Peter Zijlstra7d478722009-09-14 19:55:44 +02001468static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001469{
Gregory Haskins45c01e82008-05-12 21:20:41 +02001470 if (p->prio < rq->curr->prio) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001471 resched_curr(rq);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001472 return;
1473 }
1474
1475#ifdef CONFIG_SMP
1476 /*
1477 * If:
1478 *
1479 * - the newly woken task is of equal priority to the current task
1480 * - the newly woken task is non-migratable while current is migratable
1481 * - current will be preempted on the next reschedule
1482 *
1483 * we should check to see if current can readily move to a different
1484 * cpu. If so, we will reschedule to allow the push logic to try
1485 * to move current somewhere else, making room for our non-migratable
1486 * task.
1487 */
Hillf Danton8dd0de82011-06-14 18:36:24 -04001488 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
Dmitry Adamushko7ebefa82008-07-01 23:32:15 +02001489 check_preempt_equal_prio(rq, p);
Gregory Haskins45c01e82008-05-12 21:20:41 +02001490#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001491}
1492
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001493static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1494 struct rt_rq *rt_rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001495{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001496 struct rt_prio_array *array = &rt_rq->active;
1497 struct sched_rt_entity *next = NULL;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001498 struct list_head *queue;
1499 int idx;
1500
1501 idx = sched_find_first_bit(array->bitmap);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001502 BUG_ON(idx >= MAX_RT_PRIO);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001503
1504 queue = array->queue + idx;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001505 next = list_entry(queue->next, struct sched_rt_entity, run_list);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001506
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001507 return next;
1508}
1509
Gregory Haskins917b6272008-12-29 09:39:53 -05001510static struct task_struct *_pick_next_task_rt(struct rq *rq)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001511{
1512 struct sched_rt_entity *rt_se;
1513 struct task_struct *p;
Peter Zijlstra606dba22012-02-11 06:05:00 +01001514 struct rt_rq *rt_rq = &rq->rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001515
1516 do {
1517 rt_se = pick_next_rt_entity(rq, rt_rq);
Dmitry Adamushko326587b2008-01-25 21:08:34 +01001518 BUG_ON(!rt_se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001519 rt_rq = group_rt_rq(rt_se);
1520 } while (rt_rq);
1521
1522 p = rt_task_of(rt_se);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001523 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001524
1525 return p;
1526}
1527
Peter Zijlstra606dba22012-02-11 06:05:00 +01001528static struct task_struct *
Matt Flemingd8ac8972016-09-21 14:38:10 +01001529pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
Gregory Haskins917b6272008-12-29 09:39:53 -05001530{
Peter Zijlstra606dba22012-02-11 06:05:00 +01001531 struct task_struct *p;
1532 struct rt_rq *rt_rq = &rq->rt;
1533
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001534 if (need_pull_rt_task(rq, prev)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001535 /*
1536 * This is OK, because current is on_cpu, which avoids it being
1537 * picked for load-balance and preemption/IRQs are still
1538 * disabled avoiding further scheduler activity on it and we're
1539 * being very careful to re-start the picking loop.
1540 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01001541 rq_unpin_lock(rq, rf);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001542 pull_rt_task(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01001543 rq_repin_lock(rq, rf);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001544 /*
1545 * pull_rt_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001546 * means a dl or stop task can slip in, in which case we need
1547 * to re-start task selection.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001548 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001549 if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001550 rq->dl.dl_nr_running))
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001551 return RETRY_TASK;
1552 }
Peter Zijlstra38033c32014-01-23 20:32:21 +01001553
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001554 /*
1555 * We may dequeue prev's rt_rq in put_prev_task().
1556 * So, we update time before rt_nr_running check.
1557 */
1558 if (prev->sched_class == &rt_sched_class)
1559 update_curr_rt(rq);
1560
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +04001561 if (!rt_rq->rt_queued)
Peter Zijlstra606dba22012-02-11 06:05:00 +01001562 return NULL;
1563
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001564 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001565
1566 p = _pick_next_task_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001567
1568 /* The running task is never eligible for pushing */
Kirill Tkhaif3f17682014-09-12 17:42:01 +04001569 dequeue_pushable_task(rq, p);
Gregory Haskins917b6272008-12-29 09:39:53 -05001570
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02001571 queue_push_tasks(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04001572
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001573 return p;
1574}
1575
Ingo Molnar31ee5292007-08-09 11:16:49 +02001576static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001577{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +02001578 update_curr_rt(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05001579
1580 /*
1581 * The previous task needs to be made eligible for pushing
1582 * if it is still active
1583 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001584 if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
Gregory Haskins917b6272008-12-29 09:39:53 -05001585 enqueue_pushable_task(rq, p);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001586}
1587
Peter Williams681f3e62007-10-24 18:23:51 +02001588#ifdef CONFIG_SMP
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001589
Steven Rostedte8fa1362008-01-25 21:08:05 +01001590/* Only try algorithms three times */
1591#define RT_MAX_TRIES 3
1592
Steven Rostedtf65eda42008-01-25 21:08:07 +01001593static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1594{
1595 if (!task_running(rq, p) &&
Ingo Molnar0c98d342017-02-05 15:38:10 +01001596 cpumask_test_cpu(cpu, &p->cpus_allowed))
Steven Rostedtf65eda42008-01-25 21:08:07 +01001597 return 1;
1598 return 0;
1599}
1600
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001601/*
1602 * Return the highest pushable rq's task, which is suitable to be executed
1603 * on the cpu, NULL otherwise
1604 */
1605static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001606{
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001607 struct plist_head *head = &rq->rt.pushable_tasks;
1608 struct task_struct *p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001609
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001610 if (!has_pushable_tasks(rq))
1611 return NULL;
Peter Zijlstra3d074672010-03-10 17:07:24 +01001612
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001613 plist_for_each_entry(p, head, pushable_tasks) {
1614 if (pick_rt_task(rq, p, cpu))
1615 return p;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001616 }
1617
Kirill Tkhaie23ee742013-06-07 15:37:43 -04001618 return NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001619}
1620
Rusty Russell0e3900e2008-11-25 02:35:13 +10301621static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001622
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001623static int find_lowest_rq(struct task_struct *task)
1624{
1625 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001626 struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001627 int this_cpu = smp_processor_id();
1628 int cpu = task_cpu(task);
1629
Steven Rostedt0da938c2011-06-14 18:36:25 -04001630 /* Make sure the mask is initialized first */
1631 if (unlikely(!lowest_mask))
1632 return -1;
1633
Ingo Molnar4b53a342017-02-05 15:41:03 +01001634 if (task->nr_cpus_allowed == 1)
Gregory Haskins6e0534f2008-05-12 21:21:01 +02001635 return -1; /* No other targets possible */
1636
1637 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
Gregory Haskins06f90db2008-01-25 21:08:13 +01001638 return -1; /* No targets found */
1639
1640 /*
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001641 * At this point we have built a mask of cpus representing the
1642 * lowest priority tasks in the system. Now we want to elect
1643 * the best one based on our affinity and topology.
1644 *
1645 * We prioritize the last cpu that the task executed on since
1646 * it is most likely cache-hot in that location.
1647 */
Rusty Russell96f874e22008-11-25 02:35:14 +10301648 if (cpumask_test_cpu(cpu, lowest_mask))
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001649 return cpu;
1650
1651 /*
1652 * Otherwise, we consult the sched_domains span maps to figure
1653 * out which cpu is logically closest to our hot cache data.
1654 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301655 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1656 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001657
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001658 rcu_read_lock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301659 for_each_domain(cpu, sd) {
1660 if (sd->flags & SD_WAKE_AFFINE) {
1661 int best_cpu;
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001662
Rusty Russelle2c88062009-11-03 14:53:15 +10301663 /*
1664 * "this_cpu" is cheaper to preempt than a
1665 * remote processor.
1666 */
1667 if (this_cpu != -1 &&
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001668 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1669 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301670 return this_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001671 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001672
Rusty Russelle2c88062009-11-03 14:53:15 +10301673 best_cpu = cpumask_first_and(lowest_mask,
1674 sched_domain_span(sd));
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001675 if (best_cpu < nr_cpu_ids) {
1676 rcu_read_unlock();
Rusty Russelle2c88062009-11-03 14:53:15 +10301677 return best_cpu;
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001678 }
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001679 }
1680 }
Xiaotian Fengcd4ae6a2011-04-22 18:53:54 +08001681 rcu_read_unlock();
Gregory Haskins6e1254d2008-01-25 21:08:11 +01001682
1683 /*
1684 * And finally, if there were no matches within the domains
1685 * just give the caller *something* to work with from the compatible
1686 * locations.
1687 */
Rusty Russelle2c88062009-11-03 14:53:15 +10301688 if (this_cpu != -1)
1689 return this_cpu;
1690
1691 cpu = cpumask_any(lowest_mask);
1692 if (cpu < nr_cpu_ids)
1693 return cpu;
1694 return -1;
Gregory Haskins07b40322008-01-25 21:08:10 +01001695}
1696
Steven Rostedte8fa1362008-01-25 21:08:05 +01001697/* Will lock the rq it finds */
Ingo Molnar4df64c02008-01-25 21:08:15 +01001698static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001699{
1700 struct rq *lowest_rq = NULL;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001701 int tries;
Ingo Molnar4df64c02008-01-25 21:08:15 +01001702 int cpu;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001703
1704 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
Gregory Haskins07b40322008-01-25 21:08:10 +01001705 cpu = find_lowest_rq(task);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001706
Gregory Haskins2de0b462008-01-25 21:08:10 +01001707 if ((cpu == -1) || (cpu == rq->cpu))
Steven Rostedte8fa1362008-01-25 21:08:05 +01001708 break;
1709
Gregory Haskins07b40322008-01-25 21:08:10 +01001710 lowest_rq = cpu_rq(cpu);
1711
Tim Chen80e3d872014-12-12 15:38:12 -08001712 if (lowest_rq->rt.highest_prio.curr <= task->prio) {
1713 /*
1714 * Target rq has tasks of equal or higher priority,
1715 * retrying does not release any lock and is unlikely
1716 * to yield a different result.
1717 */
1718 lowest_rq = NULL;
1719 break;
1720 }
1721
Steven Rostedte8fa1362008-01-25 21:08:05 +01001722 /* if the prio of this runqueue changed, try again */
Gregory Haskins07b40322008-01-25 21:08:10 +01001723 if (double_lock_balance(rq, lowest_rq)) {
Steven Rostedte8fa1362008-01-25 21:08:05 +01001724 /*
1725 * We had to unlock the run queue. In
1726 * the mean time, task could have
1727 * migrated already or had its affinity changed.
1728 * Also make sure that it wasn't scheduled on its rq.
1729 */
Gregory Haskins07b40322008-01-25 21:08:10 +01001730 if (unlikely(task_rq(task) != rq ||
Ingo Molnar0c98d342017-02-05 15:38:10 +01001731 !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed) ||
Gregory Haskins07b40322008-01-25 21:08:10 +01001732 task_running(rq, task) ||
Xunlei Pang13b5ab02016-05-09 12:11:31 +08001733 !rt_task(task) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001734 !task_on_rq_queued(task))) {
Ingo Molnar4df64c02008-01-25 21:08:15 +01001735
Peter Zijlstra7f1b4392012-05-17 21:19:46 +02001736 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001737 lowest_rq = NULL;
1738 break;
1739 }
1740 }
1741
1742 /* If this rq is still suitable use it. */
Gregory Haskinse864c492008-12-29 09:39:49 -05001743 if (lowest_rq->rt.highest_prio.curr > task->prio)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001744 break;
1745
1746 /* try again */
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001747 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001748 lowest_rq = NULL;
1749 }
1750
1751 return lowest_rq;
1752}
1753
Gregory Haskins917b6272008-12-29 09:39:53 -05001754static struct task_struct *pick_next_pushable_task(struct rq *rq)
1755{
1756 struct task_struct *p;
1757
1758 if (!has_pushable_tasks(rq))
1759 return NULL;
1760
1761 p = plist_first_entry(&rq->rt.pushable_tasks,
1762 struct task_struct, pushable_tasks);
1763
1764 BUG_ON(rq->cpu != task_cpu(p));
1765 BUG_ON(task_current(rq, p));
Ingo Molnar4b53a342017-02-05 15:41:03 +01001766 BUG_ON(p->nr_cpus_allowed <= 1);
Gregory Haskins917b6272008-12-29 09:39:53 -05001767
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001768 BUG_ON(!task_on_rq_queued(p));
Gregory Haskins917b6272008-12-29 09:39:53 -05001769 BUG_ON(!rt_task(p));
1770
1771 return p;
1772}
1773
Steven Rostedte8fa1362008-01-25 21:08:05 +01001774/*
1775 * If the current CPU has more than one RT task, see if the non
1776 * running task can migrate over to a CPU that is running a task
1777 * of lesser priority.
1778 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001779static int push_rt_task(struct rq *rq)
Steven Rostedte8fa1362008-01-25 21:08:05 +01001780{
1781 struct task_struct *next_task;
1782 struct rq *lowest_rq;
Hillf Danton311e8002011-06-16 21:55:20 -04001783 int ret = 0;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001784
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +01001785 if (!rq->rt.overloaded)
1786 return 0;
1787
Gregory Haskins917b6272008-12-29 09:39:53 -05001788 next_task = pick_next_pushable_task(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001789 if (!next_task)
1790 return 0;
1791
Peter Zijlstra49246272010-10-17 21:46:10 +02001792retry:
Gregory Haskins697f0a42008-01-25 21:08:09 +01001793 if (unlikely(next_task == rq->curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01001794 WARN_ON(1);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001795 return 0;
Steven Rostedtf65eda42008-01-25 21:08:07 +01001796 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01001797
1798 /*
1799 * It's possible that the next_task slipped in of
1800 * higher priority than current. If that's the case
1801 * just reschedule current.
1802 */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001803 if (unlikely(next_task->prio < rq->curr->prio)) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001804 resched_curr(rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001805 return 0;
1806 }
1807
Gregory Haskins697f0a42008-01-25 21:08:09 +01001808 /* We might release rq lock */
Steven Rostedte8fa1362008-01-25 21:08:05 +01001809 get_task_struct(next_task);
1810
1811 /* find_lock_lowest_rq locks the rq if found */
Gregory Haskins697f0a42008-01-25 21:08:09 +01001812 lowest_rq = find_lock_lowest_rq(next_task, rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001813 if (!lowest_rq) {
1814 struct task_struct *task;
1815 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001816 * find_lock_lowest_rq releases rq->lock
Gregory Haskins15635132008-12-29 09:39:53 -05001817 * so it is possible that next_task has migrated.
1818 *
1819 * We need to make sure that the task is still on the same
1820 * run-queue and is also still the next task eligible for
1821 * pushing.
Steven Rostedte8fa1362008-01-25 21:08:05 +01001822 */
Gregory Haskins917b6272008-12-29 09:39:53 -05001823 task = pick_next_pushable_task(rq);
Byungchul Parkde16b912017-05-12 10:05:43 +09001824 if (task == next_task) {
Gregory Haskins15635132008-12-29 09:39:53 -05001825 /*
Hillf Danton311e8002011-06-16 21:55:20 -04001826 * The task hasn't migrated, and is still the next
1827 * eligible task, but we failed to find a run-queue
1828 * to push it to. Do not retry in this case, since
1829 * other cpus will pull from us when ready.
Gregory Haskins15635132008-12-29 09:39:53 -05001830 */
Gregory Haskins15635132008-12-29 09:39:53 -05001831 goto out;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001832 }
Gregory Haskins917b6272008-12-29 09:39:53 -05001833
Gregory Haskins15635132008-12-29 09:39:53 -05001834 if (!task)
1835 /* No more tasks, just exit */
1836 goto out;
1837
Gregory Haskins917b6272008-12-29 09:39:53 -05001838 /*
Gregory Haskins15635132008-12-29 09:39:53 -05001839 * Something has shifted, try again.
Gregory Haskins917b6272008-12-29 09:39:53 -05001840 */
Gregory Haskins15635132008-12-29 09:39:53 -05001841 put_task_struct(next_task);
1842 next_task = task;
1843 goto retry;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001844 }
1845
Gregory Haskins697f0a42008-01-25 21:08:09 +01001846 deactivate_task(rq, next_task, 0);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001847 set_task_cpu(next_task, lowest_rq->cpu);
1848 activate_task(lowest_rq, next_task, 0);
Hillf Danton311e8002011-06-16 21:55:20 -04001849 ret = 1;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001850
Kirill Tkhai88751252014-06-29 00:03:57 +04001851 resched_curr(lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001852
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02001853 double_unlock_balance(rq, lowest_rq);
Steven Rostedte8fa1362008-01-25 21:08:05 +01001854
Steven Rostedte8fa1362008-01-25 21:08:05 +01001855out:
1856 put_task_struct(next_task);
1857
Hillf Danton311e8002011-06-16 21:55:20 -04001858 return ret;
Steven Rostedte8fa1362008-01-25 21:08:05 +01001859}
1860
Steven Rostedte8fa1362008-01-25 21:08:05 +01001861static void push_rt_tasks(struct rq *rq)
1862{
1863 /* push_rt_task will return true if it moved an RT */
1864 while (push_rt_task(rq))
1865 ;
1866}
1867
Steven Rostedtb6366f02015-03-18 14:49:46 -04001868#ifdef HAVE_RT_PUSH_IPI
Steven Rostedtb6366f02015-03-18 14:49:46 -04001869
Steven Rostedt (VMware)3e777f92017-02-28 15:50:30 -05001870/*
1871 * When a high priority task schedules out from a CPU and a lower priority
1872 * task is scheduled in, a check is made to see if there's any RT tasks
1873 * on other CPUs that are waiting to run because a higher priority RT task
1874 * is currently running on its CPU. In this case, the CPU with multiple RT
1875 * tasks queued on it (overloaded) needs to be notified that a CPU has opened
1876 * up that may be able to run one of its non-running queued RT tasks.
1877 *
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001878 * All CPUs with overloaded RT tasks need to be notified as there is currently
1879 * no way to know which of these CPUs have the highest priority task waiting
1880 * to run. Instead of trying to take a spinlock on each of these CPUs,
1881 * which has shown to cause large latency when done on machines with many
1882 * CPUs, sending an IPI to the CPUs to have them push off the overloaded
1883 * RT tasks waiting to run.
Steven Rostedt (VMware)3e777f92017-02-28 15:50:30 -05001884 *
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001885 * Just sending an IPI to each of the CPUs is also an issue, as on large
1886 * count CPU machines, this can cause an IPI storm on a CPU, especially
1887 * if its the only CPU with multiple RT tasks queued, and a large number
1888 * of CPUs scheduling a lower priority task at the same time.
Steven Rostedt (VMware)3e777f92017-02-28 15:50:30 -05001889 *
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001890 * Each root domain has its own irq work function that can iterate over
1891 * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
1892 * tassk must be checked if there's one or many CPUs that are lowering
1893 * their priority, there's a single irq work iterator that will try to
1894 * push off RT tasks that are waiting to run.
Steven Rostedt (VMware)3e777f92017-02-28 15:50:30 -05001895 *
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001896 * When a CPU schedules a lower priority task, it will kick off the
1897 * irq work iterator that will jump to each CPU with overloaded RT tasks.
1898 * As it only takes the first CPU that schedules a lower priority task
1899 * to start the process, the rto_start variable is incremented and if
1900 * the atomic result is one, then that CPU will try to take the rto_lock.
1901 * This prevents high contention on the lock as the process handles all
1902 * CPUs scheduling lower priority tasks.
Steven Rostedt (VMware)3e777f92017-02-28 15:50:30 -05001903 *
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001904 * All CPUs that are scheduling a lower priority task will increment the
1905 * rt_loop_next variable. This will make sure that the irq work iterator
1906 * checks all RT overloaded CPUs whenever a CPU schedules a new lower
1907 * priority task, even if the iterator is in the middle of a scan. Incrementing
1908 * the rt_loop_next will cause the iterator to perform another scan.
Steven Rostedt (VMware)3e777f92017-02-28 15:50:30 -05001909 *
Steven Rostedt (VMware)3e777f92017-02-28 15:50:30 -05001910 */
Steven Rostedt (VMware)ad0f1d92018-01-23 20:45:37 -05001911static int rto_next_cpu(struct root_domain *rd)
Steven Rostedtb6366f02015-03-18 14:49:46 -04001912{
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001913 int next;
Steven Rostedtb6366f02015-03-18 14:49:46 -04001914 int cpu;
1915
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001916 /*
1917 * When starting the IPI RT pushing, the rto_cpu is set to -1,
1918 * rt_next_cpu() will simply return the first CPU found in
1919 * the rto_mask.
1920 *
1921 * If rto_next_cpu() is called with rto_cpu is a valid cpu, it
1922 * will return the next CPU found in the rto_mask.
1923 *
1924 * If there are no more CPUs left in the rto_mask, then a check is made
1925 * against rto_loop and rto_loop_next. rto_loop is only updated with
1926 * the rto_lock held, but any CPU may increment the rto_loop_next
1927 * without any locking.
1928 */
1929 for (;;) {
1930
1931 /* When rto_cpu is -1 this acts like cpumask_first() */
1932 cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
1933
1934 rd->rto_cpu = cpu;
1935
1936 if (cpu < nr_cpu_ids)
1937 return cpu;
1938
1939 rd->rto_cpu = -1;
1940
1941 /*
1942 * ACQUIRE ensures we see the @rto_mask changes
1943 * made prior to the @next value observed.
1944 *
1945 * Matches WMB in rt_set_overload().
1946 */
1947 next = atomic_read_acquire(&rd->rto_loop_next);
1948
1949 if (rd->rto_loop == next)
1950 break;
1951
1952 rd->rto_loop = next;
Steven Rostedtb6366f02015-03-18 14:49:46 -04001953 }
1954
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001955 return -1;
1956}
Steven Rostedtb6366f02015-03-18 14:49:46 -04001957
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001958static inline bool rto_start_trylock(atomic_t *v)
1959{
1960 return !atomic_cmpxchg_acquire(v, 0, 1);
1961}
1962
1963static inline void rto_start_unlock(atomic_t *v)
1964{
1965 atomic_set_release(v, 0);
1966}
1967
1968static void tell_cpu_to_push(struct rq *rq)
1969{
1970 int cpu = -1;
1971
1972 /* Keep the loop going if the IPI is currently active */
1973 atomic_inc(&rq->rd->rto_loop_next);
1974
1975 /* Only one CPU can initiate a loop at a time */
1976 if (!rto_start_trylock(&rq->rd->rto_loop_start))
Steven Rostedtb6366f02015-03-18 14:49:46 -04001977 return;
1978
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001979 raw_spin_lock(&rq->rd->rto_lock);
Steven Rostedtb6366f02015-03-18 14:49:46 -04001980
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001981 /*
1982 * The rto_cpu is updated under the lock, if it has a valid cpu
1983 * then the IPI is still running and will continue due to the
1984 * update to loop_next, and nothing needs to be done here.
1985 * Otherwise it is finishing up and an ipi needs to be sent.
1986 */
1987 if (rq->rd->rto_cpu < 0)
Steven Rostedt (VMware)ad0f1d92018-01-23 20:45:37 -05001988 cpu = rto_next_cpu(rq->rd);
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001989
1990 raw_spin_unlock(&rq->rd->rto_lock);
1991
1992 rto_start_unlock(&rq->rd->rto_loop_start);
1993
Steven Rostedt (VMware)364f5662018-01-23 20:45:38 -05001994 if (cpu >= 0) {
1995 /* Make sure the rd does not get freed while pushing */
1996 sched_get_rd(rq->rd);
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04001997 irq_work_queue_on(&rq->rd->rto_push_work, cpu);
Steven Rostedt (VMware)364f5662018-01-23 20:45:38 -05001998 }
Steven Rostedtb6366f02015-03-18 14:49:46 -04001999}
2000
2001/* Called from hardirq context */
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04002002void rto_push_irq_work_func(struct irq_work *work)
Steven Rostedtb6366f02015-03-18 14:49:46 -04002003{
Steven Rostedt (VMware)ad0f1d92018-01-23 20:45:37 -05002004 struct root_domain *rd =
2005 container_of(work, struct root_domain, rto_push_work);
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04002006 struct rq *rq;
Steven Rostedtb6366f02015-03-18 14:49:46 -04002007 int cpu;
2008
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04002009 rq = this_rq();
Steven Rostedtb6366f02015-03-18 14:49:46 -04002010
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04002011 /*
2012 * We do not need to grab the lock to check for has_pushable_tasks.
2013 * When it gets updated, a check is made if a push is possible.
2014 */
Steven Rostedtb6366f02015-03-18 14:49:46 -04002015 if (has_pushable_tasks(rq)) {
2016 raw_spin_lock(&rq->lock);
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04002017 push_rt_tasks(rq);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002018 raw_spin_unlock(&rq->lock);
2019 }
2020
Steven Rostedt (VMware)ad0f1d92018-01-23 20:45:37 -05002021 raw_spin_lock(&rd->rto_lock);
Steven Rostedt (Red Hat)4bdced52017-10-06 14:05:04 -04002022
Steven Rostedtb6366f02015-03-18 14:49:46 -04002023 /* Pass the IPI to the next rt overloaded queue */
Steven Rostedt (VMware)ad0f1d92018-01-23 20:45:37 -05002024 cpu = rto_next_cpu(rd);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002025
Steven Rostedt (VMware)ad0f1d92018-01-23 20:45:37 -05002026 raw_spin_unlock(&rd->rto_lock);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002027
Steven Rostedt (VMware)364f5662018-01-23 20:45:38 -05002028 if (cpu < 0) {
2029 sched_put_rd(rd);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002030 return;
Steven Rostedt (VMware)364f5662018-01-23 20:45:38 -05002031 }
Steven Rostedtb6366f02015-03-18 14:49:46 -04002032
Steven Rostedtb6366f02015-03-18 14:49:46 -04002033 /* Try the next RT overloaded CPU */
Steven Rostedt (VMware)ad0f1d92018-01-23 20:45:37 -05002034 irq_work_queue_on(&rd->rto_push_work, cpu);
Steven Rostedtb6366f02015-03-18 14:49:46 -04002035}
2036#endif /* HAVE_RT_PUSH_IPI */
2037
Peter Zijlstra8046d682015-06-11 14:46:40 +02002038static void pull_rt_task(struct rq *this_rq)
Steven Rostedtf65eda42008-01-25 21:08:07 +01002039{
Peter Zijlstra8046d682015-06-11 14:46:40 +02002040 int this_cpu = this_rq->cpu, cpu;
2041 bool resched = false;
Gregory Haskinsa8728942008-12-29 09:39:49 -05002042 struct task_struct *p;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002043 struct rq *src_rq;
Steven Rostedtf73c52a2017-12-02 13:04:54 -05002044 int rt_overload_count = rt_overloaded(this_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002045
Steven Rostedtf73c52a2017-12-02 13:04:54 -05002046 if (likely(!rt_overload_count))
Peter Zijlstra8046d682015-06-11 14:46:40 +02002047 return;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002048
Peter Zijlstra7c3f2ab2013-10-15 12:35:07 +02002049 /*
2050 * Match the barrier from rt_set_overloaded; this guarantees that if we
2051 * see overloaded we must also see the rto_mask bit.
2052 */
2053 smp_rmb();
2054
Steven Rostedtf73c52a2017-12-02 13:04:54 -05002055 /* If we are the only overloaded CPU do nothing */
2056 if (rt_overload_count == 1 &&
2057 cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
2058 return;
2059
Steven Rostedtb6366f02015-03-18 14:49:46 -04002060#ifdef HAVE_RT_PUSH_IPI
2061 if (sched_feat(RT_PUSH_IPI)) {
2062 tell_cpu_to_push(this_rq);
Peter Zijlstra8046d682015-06-11 14:46:40 +02002063 return;
Steven Rostedtb6366f02015-03-18 14:49:46 -04002064 }
2065#endif
2066
Rusty Russellc6c49272008-11-25 02:35:05 +10302067 for_each_cpu(cpu, this_rq->rd->rto_mask) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01002068 if (this_cpu == cpu)
2069 continue;
2070
2071 src_rq = cpu_rq(cpu);
Gregory Haskins74ab8e42008-12-29 09:39:50 -05002072
2073 /*
2074 * Don't bother taking the src_rq->lock if the next highest
2075 * task is known to be lower-priority than our current task.
2076 * This may look racy, but if this value is about to go
2077 * logically higher, the src_rq will push this task away.
2078 * And if its going logically lower, we do not care
2079 */
2080 if (src_rq->rt.highest_prio.next >=
2081 this_rq->rt.highest_prio.curr)
2082 continue;
2083
Steven Rostedtf65eda42008-01-25 21:08:07 +01002084 /*
2085 * We can potentially drop this_rq's lock in
2086 * double_lock_balance, and another CPU could
Gregory Haskinsa8728942008-12-29 09:39:49 -05002087 * alter this_rq
Steven Rostedtf65eda42008-01-25 21:08:07 +01002088 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002089 double_lock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002090
2091 /*
Kirill Tkhaie23ee742013-06-07 15:37:43 -04002092 * We can pull only a task, which is pushable
2093 * on its rq, and no others.
Steven Rostedtf65eda42008-01-25 21:08:07 +01002094 */
Kirill Tkhaie23ee742013-06-07 15:37:43 -04002095 p = pick_highest_pushable_task(src_rq, this_cpu);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002096
2097 /*
2098 * Do we have an RT task that preempts
2099 * the to-be-scheduled task?
2100 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002101 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
Steven Rostedtf65eda42008-01-25 21:08:07 +01002102 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002103 WARN_ON(!task_on_rq_queued(p));
Steven Rostedtf65eda42008-01-25 21:08:07 +01002104
2105 /*
2106 * There's a chance that p is higher in priority
2107 * than what's currently running on its cpu.
2108 * This is just that p is wakeing up and hasn't
2109 * had a chance to schedule. We only pull
2110 * p if it is lower in priority than the
Gregory Haskinsa8728942008-12-29 09:39:49 -05002111 * current task on the run queue
Steven Rostedtf65eda42008-01-25 21:08:07 +01002112 */
Gregory Haskinsa8728942008-12-29 09:39:49 -05002113 if (p->prio < src_rq->curr->prio)
Mike Galbraith614ee1f2008-01-25 21:08:30 +01002114 goto skip;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002115
Peter Zijlstra8046d682015-06-11 14:46:40 +02002116 resched = true;
Steven Rostedtf65eda42008-01-25 21:08:07 +01002117
2118 deactivate_task(src_rq, p, 0);
2119 set_task_cpu(p, this_cpu);
2120 activate_task(this_rq, p, 0);
2121 /*
2122 * We continue with the search, just in
2123 * case there's an even higher prio task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002124 * in another runqueue. (low likelihood
Steven Rostedtf65eda42008-01-25 21:08:07 +01002125 * but possible)
Steven Rostedtf65eda42008-01-25 21:08:07 +01002126 */
Steven Rostedtf65eda42008-01-25 21:08:07 +01002127 }
Peter Zijlstra49246272010-10-17 21:46:10 +02002128skip:
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02002129 double_unlock_balance(this_rq, src_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002130 }
2131
Peter Zijlstra8046d682015-06-11 14:46:40 +02002132 if (resched)
2133 resched_curr(this_rq);
Steven Rostedtf65eda42008-01-25 21:08:07 +01002134}
2135
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002136/*
2137 * If we are not running and we are not going to reschedule soon, we should
2138 * try to push tasks away now
2139 */
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002140static void task_woken_rt(struct rq *rq, struct task_struct *p)
Steven Rostedt4642daf2008-01-25 21:08:07 +01002141{
Steven Rostedt9a897c52008-01-25 21:08:22 +01002142 if (!task_running(rq, p) &&
Gregory Haskins8ae121a2008-04-23 07:13:29 -04002143 !test_tsk_need_resched(rq->curr) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01002144 p->nr_cpus_allowed > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01002145 (dl_task(rq->curr) || rt_task(rq->curr)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01002146 (rq->curr->nr_cpus_allowed < 2 ||
Shawn Bohrer3be209a2011-09-12 09:28:04 -05002147 rq->curr->prio <= p->prio))
Steven Rostedt4642daf2008-01-25 21:08:07 +01002148 push_rt_tasks(rq);
2149}
2150
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002151/* Assumes rq->lock is held */
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04002152static void rq_online_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002153{
2154 if (rq->rt.overloaded)
2155 rt_set_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002156
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002157 __enable_runtime(rq);
2158
Gregory Haskinse864c492008-12-29 09:39:49 -05002159 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002160}
2161
2162/* Assumes rq->lock is held */
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04002163static void rq_offline_rt(struct rq *rq)
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002164{
2165 if (rq->rt.overloaded)
2166 rt_clear_overload(rq);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002167
Peter Zijlstra7def2be2008-06-05 14:49:58 +02002168 __disable_runtime(rq);
2169
Gregory Haskins6e0534f2008-05-12 21:21:01 +02002170 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
Ingo Molnarbdd7c812008-01-25 21:08:18 +01002171}
Steven Rostedtcb469842008-01-25 21:08:22 +01002172
2173/*
2174 * When switch from the rt queue, we bring ourselves to a position
2175 * that we might want to pull RT tasks from other runqueues.
2176 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002177static void switched_from_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002178{
2179 /*
2180 * If there are other RT tasks then we will reschedule
2181 * and the scheduling of the other RT tasks will handle
2182 * the balancing. But if we are the last RT task
2183 * we may need to handle the pulling of RT tasks
2184 * now.
2185 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002186 if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
Kirill Tkhai1158ddb2012-11-23 00:02:15 +04002187 return;
2188
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002189 queue_pull_task(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002190}
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302191
Li Zefan11c785b2014-02-08 14:17:45 +08002192void __init init_sched_rt_class(void)
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302193{
2194 unsigned int i;
2195
Peter Zijlstra029632f2011-10-25 10:00:11 +02002196 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -07002197 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
Mike Travis6ca09df2008-12-31 18:08:45 -08002198 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra029632f2011-10-25 10:00:11 +02002199 }
Rusty Russell3d8cbdf2008-11-25 09:58:41 +10302200}
Steven Rostedte8fa1362008-01-25 21:08:05 +01002201#endif /* CONFIG_SMP */
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002202
Steven Rostedtcb469842008-01-25 21:08:22 +01002203/*
2204 * When switching a task to RT, we may overload the runqueue
2205 * with RT tasks. In this case we try to push them off to
2206 * other runqueues.
2207 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002208static void switched_to_rt(struct rq *rq, struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01002209{
Steven Rostedtcb469842008-01-25 21:08:22 +01002210 /*
2211 * If we are already running, then there's nothing
2212 * that needs to be done. But if we are not running
2213 * we may need to preempt the current running task.
2214 * If that current running task is also an RT task
2215 * then see if we can move to another run queue.
2216 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002217 if (task_on_rq_queued(p) && rq->curr != p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002218#ifdef CONFIG_SMP
Ingo Molnar4b53a342017-02-05 15:41:03 +01002219 if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002220 queue_push_tasks(rq);
Sebastian Andrzej Siewior619bd4a2017-01-24 15:40:06 +01002221#endif /* CONFIG_SMP */
Paul E. McKenney2fe25822017-10-13 17:00:18 -07002222 if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
Kirill Tkhai88751252014-06-29 00:03:57 +04002223 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002224 }
2225}
2226
2227/*
2228 * Priority of the task has changed. This may cause
2229 * us to initiate a push or pull.
2230 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002231static void
2232prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01002233{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002234 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002235 return;
2236
2237 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01002238#ifdef CONFIG_SMP
2239 /*
2240 * If our priority decreases while running, we
2241 * may need to pull tasks to this runqueue.
2242 */
2243 if (oldprio < p->prio)
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002244 queue_pull_task(rq);
2245
Steven Rostedtcb469842008-01-25 21:08:22 +01002246 /*
2247 * If there's a higher priority task waiting to run
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002248 * then reschedule.
Steven Rostedtcb469842008-01-25 21:08:22 +01002249 */
Peter Zijlstrafd7a4be2015-06-11 14:46:41 +02002250 if (p->prio > rq->rt.highest_prio.curr)
Kirill Tkhai88751252014-06-29 00:03:57 +04002251 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002252#else
2253 /* For UP simply resched on drop of prio */
2254 if (oldprio < p->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002255 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002256#endif /* CONFIG_SMP */
2257 } else {
2258 /*
2259 * This task is not running, but if it is
2260 * greater than the current running task
2261 * then reschedule.
2262 */
2263 if (p->prio < rq->curr->prio)
Kirill Tkhai88751252014-06-29 00:03:57 +04002264 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01002265 }
2266}
2267
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002268#ifdef CONFIG_POSIX_TIMERS
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002269static void watchdog(struct rq *rq, struct task_struct *p)
2270{
2271 unsigned long soft, hard;
2272
Jiri Slaby78d7d402010-03-05 13:42:54 -08002273 /* max may change after cur was read, this will be fixed next tick */
2274 soft = task_rlimit(p, RLIMIT_RTTIME);
2275 hard = task_rlimit_max(p, RLIMIT_RTTIME);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002276
2277 if (soft != RLIM_INFINITY) {
2278 unsigned long next;
2279
Ying Xue57d2aa02012-07-17 15:03:43 +08002280 if (p->rt.watchdog_stamp != jiffies) {
2281 p->rt.timeout++;
2282 p->rt.watchdog_stamp = jiffies;
2283 }
2284
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002285 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
Peter Zijlstra5a52dd52008-01-25 21:08:32 +01002286 if (p->rt.timeout > next)
Frank Mayharf06febc2008-09-12 09:54:39 -07002287 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002288 }
2289}
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002290#else
2291static inline void watchdog(struct rq *rq, struct task_struct *p) { }
2292#endif
Steven Rostedtcb469842008-01-25 21:08:22 +01002293
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01002294static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002295{
Colin Cross454c7992012-05-16 21:34:23 -07002296 struct sched_rt_entity *rt_se = &p->rt;
2297
Peter Zijlstra67e2be02007-12-20 15:01:17 +01002298 update_curr_rt(rq);
2299
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01002300 watchdog(rq, p);
2301
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002302 /*
2303 * RR tasks need a special form of timeslice management.
2304 * FIFO tasks have no timeslices.
2305 */
2306 if (p->policy != SCHED_RR)
2307 return;
2308
Peter Zijlstrafa717062008-01-25 21:08:27 +01002309 if (--p->rt.time_slice)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002310 return;
2311
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002312 p->rt.time_slice = sched_rr_timeslice;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002313
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002314 /*
Li Bine9aa39b2013-10-21 20:15:43 +08002315 * Requeue to the end of queue if we (and all of our ancestors) are not
2316 * the only element on the queue
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002317 */
Colin Cross454c7992012-05-16 21:34:23 -07002318 for_each_sched_rt_entity(rt_se) {
2319 if (rt_se->run_list.prev != rt_se->run_list.next) {
2320 requeue_task_rt(rq, p, 0);
Kirill Tkhai8aa6f0e2014-09-22 22:36:43 +04002321 resched_curr(rq);
Colin Cross454c7992012-05-16 21:34:23 -07002322 return;
2323 }
Dmitry Adamushko98fbc792007-08-24 20:39:10 +02002324 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002325}
2326
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002327static void set_curr_task_rt(struct rq *rq)
2328{
2329 struct task_struct *p = rq->curr;
2330
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002331 p->se.exec_start = rq_clock_task(rq);
Gregory Haskins917b6272008-12-29 09:39:53 -05002332
2333 /* The running task is never eligible for pushing */
2334 dequeue_pushable_task(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002335}
2336
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07002337static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00002338{
2339 /*
2340 * Time slice is 0 for SCHED_FIFO tasks
2341 */
2342 if (task->policy == SCHED_RR)
Clark Williamsce0dbbb2013-02-07 09:47:04 -06002343 return sched_rr_timeslice;
Peter Williams0d721ce2009-09-21 01:31:53 +00002344 else
2345 return 0;
2346}
2347
Peter Zijlstra029632f2011-10-25 10:00:11 +02002348const struct sched_class rt_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002349 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002350 .enqueue_task = enqueue_task_rt,
2351 .dequeue_task = dequeue_task_rt,
2352 .yield_task = yield_task_rt,
2353
2354 .check_preempt_curr = check_preempt_curr_rt,
2355
2356 .pick_next_task = pick_next_task_rt,
2357 .put_prev_task = put_prev_task_rt,
2358
Peter Williams681f3e62007-10-24 18:23:51 +02002359#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08002360 .select_task_rq = select_task_rq_rt,
2361
Peter Zijlstra6c370672015-05-15 17:43:36 +02002362 .set_cpus_allowed = set_cpus_allowed_common,
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04002363 .rq_online = rq_online_rt,
2364 .rq_offline = rq_offline_rt,
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002365 .task_woken = task_woken_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002366 .switched_from = switched_from_rt,
Peter Williams681f3e62007-10-24 18:23:51 +02002367#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002368
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002369 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002370 .task_tick = task_tick_rt,
Steven Rostedtcb469842008-01-25 21:08:22 +01002371
Peter Williams0d721ce2009-09-21 01:31:53 +00002372 .get_rr_interval = get_rr_interval_rt,
2373
Steven Rostedtcb469842008-01-25 21:08:22 +01002374 .prio_changed = prio_changed_rt,
2375 .switched_to = switched_to_rt,
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002376
2377 .update_curr = update_curr_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02002378};
Peter Zijlstraada18de2008-06-19 14:22:24 +02002379
Nicolas Pitre8887cd92017-06-21 14:22:02 -04002380#ifdef CONFIG_RT_GROUP_SCHED
2381/*
2382 * Ensure that the real time constraints are schedulable.
2383 */
2384static DEFINE_MUTEX(rt_constraints_mutex);
2385
2386/* Must be called with tasklist_lock held */
2387static inline int tg_has_rt_tasks(struct task_group *tg)
2388{
2389 struct task_struct *g, *p;
2390
2391 /*
2392 * Autogroups do not have RT tasks; see autogroup_create().
2393 */
2394 if (task_group_is_autogroup(tg))
2395 return 0;
2396
2397 for_each_process_thread(g, p) {
2398 if (rt_task(p) && task_group(p) == tg)
2399 return 1;
2400 }
2401
2402 return 0;
2403}
2404
2405struct rt_schedulable_data {
2406 struct task_group *tg;
2407 u64 rt_period;
2408 u64 rt_runtime;
2409};
2410
2411static int tg_rt_schedulable(struct task_group *tg, void *data)
2412{
2413 struct rt_schedulable_data *d = data;
2414 struct task_group *child;
2415 unsigned long total, sum = 0;
2416 u64 period, runtime;
2417
2418 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2419 runtime = tg->rt_bandwidth.rt_runtime;
2420
2421 if (tg == d->tg) {
2422 period = d->rt_period;
2423 runtime = d->rt_runtime;
2424 }
2425
2426 /*
2427 * Cannot have more runtime than the period.
2428 */
2429 if (runtime > period && runtime != RUNTIME_INF)
2430 return -EINVAL;
2431
2432 /*
2433 * Ensure we don't starve existing RT tasks.
2434 */
2435 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
2436 return -EBUSY;
2437
2438 total = to_ratio(period, runtime);
2439
2440 /*
2441 * Nobody can have more than the global setting allows.
2442 */
2443 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
2444 return -EINVAL;
2445
2446 /*
2447 * The sum of our children's runtime should not exceed our own.
2448 */
2449 list_for_each_entry_rcu(child, &tg->children, siblings) {
2450 period = ktime_to_ns(child->rt_bandwidth.rt_period);
2451 runtime = child->rt_bandwidth.rt_runtime;
2452
2453 if (child == d->tg) {
2454 period = d->rt_period;
2455 runtime = d->rt_runtime;
2456 }
2457
2458 sum += to_ratio(period, runtime);
2459 }
2460
2461 if (sum > total)
2462 return -EINVAL;
2463
2464 return 0;
2465}
2466
2467static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
2468{
2469 int ret;
2470
2471 struct rt_schedulable_data data = {
2472 .tg = tg,
2473 .rt_period = period,
2474 .rt_runtime = runtime,
2475 };
2476
2477 rcu_read_lock();
2478 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
2479 rcu_read_unlock();
2480
2481 return ret;
2482}
2483
2484static int tg_set_rt_bandwidth(struct task_group *tg,
2485 u64 rt_period, u64 rt_runtime)
2486{
2487 int i, err = 0;
2488
2489 /*
2490 * Disallowing the root group RT runtime is BAD, it would disallow the
2491 * kernel creating (and or operating) RT threads.
2492 */
2493 if (tg == &root_task_group && rt_runtime == 0)
2494 return -EINVAL;
2495
2496 /* No period doesn't make any sense. */
2497 if (rt_period == 0)
2498 return -EINVAL;
2499
2500 mutex_lock(&rt_constraints_mutex);
2501 read_lock(&tasklist_lock);
2502 err = __rt_schedulable(tg, rt_period, rt_runtime);
2503 if (err)
2504 goto unlock;
2505
2506 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2507 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
2508 tg->rt_bandwidth.rt_runtime = rt_runtime;
2509
2510 for_each_possible_cpu(i) {
2511 struct rt_rq *rt_rq = tg->rt_rq[i];
2512
2513 raw_spin_lock(&rt_rq->rt_runtime_lock);
2514 rt_rq->rt_runtime = rt_runtime;
2515 raw_spin_unlock(&rt_rq->rt_runtime_lock);
2516 }
2517 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2518unlock:
2519 read_unlock(&tasklist_lock);
2520 mutex_unlock(&rt_constraints_mutex);
2521
2522 return err;
2523}
2524
2525int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
2526{
2527 u64 rt_runtime, rt_period;
2528
2529 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2530 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
2531 if (rt_runtime_us < 0)
2532 rt_runtime = RUNTIME_INF;
2533
2534 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
2535}
2536
2537long sched_group_rt_runtime(struct task_group *tg)
2538{
2539 u64 rt_runtime_us;
2540
2541 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
2542 return -1;
2543
2544 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
2545 do_div(rt_runtime_us, NSEC_PER_USEC);
2546 return rt_runtime_us;
2547}
2548
2549int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
2550{
2551 u64 rt_runtime, rt_period;
2552
2553 rt_period = rt_period_us * NSEC_PER_USEC;
2554 rt_runtime = tg->rt_bandwidth.rt_runtime;
2555
2556 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
2557}
2558
2559long sched_group_rt_period(struct task_group *tg)
2560{
2561 u64 rt_period_us;
2562
2563 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
2564 do_div(rt_period_us, NSEC_PER_USEC);
2565 return rt_period_us;
2566}
2567
2568static int sched_rt_global_constraints(void)
2569{
2570 int ret = 0;
2571
2572 mutex_lock(&rt_constraints_mutex);
2573 read_lock(&tasklist_lock);
2574 ret = __rt_schedulable(NULL, 0, 0);
2575 read_unlock(&tasklist_lock);
2576 mutex_unlock(&rt_constraints_mutex);
2577
2578 return ret;
2579}
2580
2581int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
2582{
2583 /* Don't accept realtime tasks when there is no way for them to run */
2584 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
2585 return 0;
2586
2587 return 1;
2588}
2589
2590#else /* !CONFIG_RT_GROUP_SCHED */
2591static int sched_rt_global_constraints(void)
2592{
2593 unsigned long flags;
2594 int i;
2595
2596 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
2597 for_each_possible_cpu(i) {
2598 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
2599
2600 raw_spin_lock(&rt_rq->rt_runtime_lock);
2601 rt_rq->rt_runtime = global_rt_runtime();
2602 raw_spin_unlock(&rt_rq->rt_runtime_lock);
2603 }
2604 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
2605
2606 return 0;
2607}
2608#endif /* CONFIG_RT_GROUP_SCHED */
2609
2610static int sched_rt_global_validate(void)
2611{
2612 if (sysctl_sched_rt_period <= 0)
2613 return -EINVAL;
2614
2615 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
2616 (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
2617 return -EINVAL;
2618
2619 return 0;
2620}
2621
2622static void sched_rt_do_global(void)
2623{
2624 def_rt_bandwidth.rt_runtime = global_rt_runtime();
2625 def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
2626}
2627
2628int sched_rt_handler(struct ctl_table *table, int write,
2629 void __user *buffer, size_t *lenp,
2630 loff_t *ppos)
2631{
2632 int old_period, old_runtime;
2633 static DEFINE_MUTEX(mutex);
2634 int ret;
2635
2636 mutex_lock(&mutex);
2637 old_period = sysctl_sched_rt_period;
2638 old_runtime = sysctl_sched_rt_runtime;
2639
2640 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2641
2642 if (!ret && write) {
2643 ret = sched_rt_global_validate();
2644 if (ret)
2645 goto undo;
2646
2647 ret = sched_dl_global_validate();
2648 if (ret)
2649 goto undo;
2650
2651 ret = sched_rt_global_constraints();
2652 if (ret)
2653 goto undo;
2654
2655 sched_rt_do_global();
2656 sched_dl_do_global();
2657 }
2658 if (0) {
2659undo:
2660 sysctl_sched_rt_period = old_period;
2661 sysctl_sched_rt_runtime = old_runtime;
2662 }
2663 mutex_unlock(&mutex);
2664
2665 return ret;
2666}
2667
2668int sched_rr_handler(struct ctl_table *table, int write,
2669 void __user *buffer, size_t *lenp,
2670 loff_t *ppos)
2671{
2672 int ret;
2673 static DEFINE_MUTEX(mutex);
2674
2675 mutex_lock(&mutex);
2676 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2677 /*
2678 * Make sure that internally we keep jiffies.
2679 * Also, writing zero resets the timeslice to default:
2680 */
2681 if (!ret && write) {
2682 sched_rr_timeslice =
2683 sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE :
2684 msecs_to_jiffies(sysctl_sched_rr_timeslice);
2685 }
2686 mutex_unlock(&mutex);
2687 return ret;
2688}
2689
Peter Zijlstraada18de2008-06-19 14:22:24 +02002690#ifdef CONFIG_SCHED_DEBUG
2691extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2692
Peter Zijlstra029632f2011-10-25 10:00:11 +02002693void print_rt_stats(struct seq_file *m, int cpu)
Peter Zijlstraada18de2008-06-19 14:22:24 +02002694{
Cheng Xuec514c42011-05-14 14:20:02 +08002695 rt_rq_iter_t iter;
Peter Zijlstraada18de2008-06-19 14:22:24 +02002696 struct rt_rq *rt_rq;
2697
2698 rcu_read_lock();
Cheng Xuec514c42011-05-14 14:20:02 +08002699 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
Peter Zijlstraada18de2008-06-19 14:22:24 +02002700 print_rt_rq(m, cpu, rt_rq);
2701 rcu_read_unlock();
2702}
Dhaval Giani55e12e52008-06-24 23:39:43 +05302703#endif /* CONFIG_SCHED_DEBUG */