Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
bb44e5d1 IM |
2 | /* |
3 | * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR | |
4 | * policies) | |
5 | */ | |
029632fb PZ |
6 | #include "sched.h" |
7 | ||
ce0dbbbb | 8 | int sched_rr_timeslice = RR_TIMESLICE; |
975e155e | 9 | int sysctl_sched_rr_timeslice = (MSEC_PER_SEC / HZ) * RR_TIMESLICE; |
ce0dbbbb | 10 | |
029632fb PZ |
11 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); |
12 | ||
13 | struct rt_bandwidth def_rt_bandwidth; | |
14 | ||
15 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) | |
16 | { | |
17 | struct rt_bandwidth *rt_b = | |
18 | container_of(timer, struct rt_bandwidth, rt_period_timer); | |
029632fb | 19 | int idle = 0; |
77a4d1a1 | 20 | int overrun; |
029632fb | 21 | |
77a4d1a1 | 22 | raw_spin_lock(&rt_b->rt_runtime_lock); |
029632fb | 23 | for (;;) { |
77a4d1a1 | 24 | overrun = hrtimer_forward_now(timer, rt_b->rt_period); |
029632fb PZ |
25 | if (!overrun) |
26 | break; | |
27 | ||
77a4d1a1 | 28 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
029632fb | 29 | idle = do_sched_rt_period_timer(rt_b, overrun); |
77a4d1a1 | 30 | raw_spin_lock(&rt_b->rt_runtime_lock); |
029632fb | 31 | } |
4cfafd30 PZ |
32 | if (idle) |
33 | rt_b->rt_period_active = 0; | |
77a4d1a1 | 34 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
029632fb PZ |
35 | |
36 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
37 | } | |
38 | ||
39 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) | |
40 | { | |
41 | rt_b->rt_period = ns_to_ktime(period); | |
42 | rt_b->rt_runtime = runtime; | |
43 | ||
44 | raw_spin_lock_init(&rt_b->rt_runtime_lock); | |
45 | ||
46 | hrtimer_init(&rt_b->rt_period_timer, | |
47 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
48 | rt_b->rt_period_timer.function = sched_rt_period_timer; | |
49 | } | |
50 | ||
51 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) | |
52 | { | |
53 | if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) | |
54 | return; | |
55 | ||
029632fb | 56 | raw_spin_lock(&rt_b->rt_runtime_lock); |
4cfafd30 PZ |
57 | if (!rt_b->rt_period_active) { |
58 | rt_b->rt_period_active = 1; | |
c3a990dc SR |
59 | /* |
60 | * SCHED_DEADLINE updates the bandwidth, as a run away | |
61 | * RT task with a DL task could hog a CPU. But DL does | |
62 | * not reset the period. If a deadline task was running | |
63 | * without an RT task running, it can cause RT tasks to | |
64 | * throttle when they start up. Kick the timer right away | |
65 | * to update the period. | |
66 | */ | |
67 | hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0)); | |
4cfafd30 PZ |
68 | hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED); |
69 | } | |
029632fb PZ |
70 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
71 | } | |
72 | ||
07c54f7a | 73 | void init_rt_rq(struct rt_rq *rt_rq) |
029632fb PZ |
74 | { |
75 | struct rt_prio_array *array; | |
76 | int i; | |
77 | ||
78 | array = &rt_rq->active; | |
79 | for (i = 0; i < MAX_RT_PRIO; i++) { | |
80 | INIT_LIST_HEAD(array->queue + i); | |
81 | __clear_bit(i, array->bitmap); | |
82 | } | |
83 | /* delimiter for bitsearch: */ | |
84 | __set_bit(MAX_RT_PRIO, array->bitmap); | |
85 | ||
86 | #if defined CONFIG_SMP | |
87 | rt_rq->highest_prio.curr = MAX_RT_PRIO; | |
88 | rt_rq->highest_prio.next = MAX_RT_PRIO; | |
89 | rt_rq->rt_nr_migratory = 0; | |
90 | rt_rq->overloaded = 0; | |
91 | plist_head_init(&rt_rq->pushable_tasks); | |
b6366f04 | 92 | #endif /* CONFIG_SMP */ |
f4ebcbc0 KT |
93 | /* We start is dequeued state, because no RT tasks are queued */ |
94 | rt_rq->rt_queued = 0; | |
029632fb PZ |
95 | |
96 | rt_rq->rt_time = 0; | |
97 | rt_rq->rt_throttled = 0; | |
98 | rt_rq->rt_runtime = 0; | |
99 | raw_spin_lock_init(&rt_rq->rt_runtime_lock); | |
100 | } | |
101 | ||
8f48894f | 102 | #ifdef CONFIG_RT_GROUP_SCHED |
029632fb PZ |
103 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) |
104 | { | |
105 | hrtimer_cancel(&rt_b->rt_period_timer); | |
106 | } | |
8f48894f PZ |
107 | |
108 | #define rt_entity_is_task(rt_se) (!(rt_se)->my_q) | |
109 | ||
398a153b GH |
110 | static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) |
111 | { | |
8f48894f PZ |
112 | #ifdef CONFIG_SCHED_DEBUG |
113 | WARN_ON_ONCE(!rt_entity_is_task(rt_se)); | |
114 | #endif | |
398a153b GH |
115 | return container_of(rt_se, struct task_struct, rt); |
116 | } | |
117 | ||
398a153b GH |
118 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) |
119 | { | |
120 | return rt_rq->rq; | |
121 | } | |
122 | ||
123 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) | |
124 | { | |
125 | return rt_se->rt_rq; | |
126 | } | |
127 | ||
653d07a6 KT |
128 | static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se) |
129 | { | |
130 | struct rt_rq *rt_rq = rt_se->rt_rq; | |
131 | ||
132 | return rt_rq->rq; | |
133 | } | |
134 | ||
029632fb PZ |
135 | void free_rt_sched_group(struct task_group *tg) |
136 | { | |
137 | int i; | |
138 | ||
139 | if (tg->rt_se) | |
140 | destroy_rt_bandwidth(&tg->rt_bandwidth); | |
141 | ||
142 | for_each_possible_cpu(i) { | |
143 | if (tg->rt_rq) | |
144 | kfree(tg->rt_rq[i]); | |
145 | if (tg->rt_se) | |
146 | kfree(tg->rt_se[i]); | |
147 | } | |
148 | ||
149 | kfree(tg->rt_rq); | |
150 | kfree(tg->rt_se); | |
151 | } | |
152 | ||
153 | void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, | |
154 | struct sched_rt_entity *rt_se, int cpu, | |
155 | struct sched_rt_entity *parent) | |
156 | { | |
157 | struct rq *rq = cpu_rq(cpu); | |
158 | ||
159 | rt_rq->highest_prio.curr = MAX_RT_PRIO; | |
160 | rt_rq->rt_nr_boosted = 0; | |
161 | rt_rq->rq = rq; | |
162 | rt_rq->tg = tg; | |
163 | ||
164 | tg->rt_rq[cpu] = rt_rq; | |
165 | tg->rt_se[cpu] = rt_se; | |
166 | ||
167 | if (!rt_se) | |
168 | return; | |
169 | ||
170 | if (!parent) | |
171 | rt_se->rt_rq = &rq->rt; | |
172 | else | |
173 | rt_se->rt_rq = parent->my_q; | |
174 | ||
175 | rt_se->my_q = rt_rq; | |
176 | rt_se->parent = parent; | |
177 | INIT_LIST_HEAD(&rt_se->run_list); | |
178 | } | |
179 | ||
180 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
181 | { | |
182 | struct rt_rq *rt_rq; | |
183 | struct sched_rt_entity *rt_se; | |
184 | int i; | |
185 | ||
186 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); | |
187 | if (!tg->rt_rq) | |
188 | goto err; | |
189 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); | |
190 | if (!tg->rt_se) | |
191 | goto err; | |
192 | ||
193 | init_rt_bandwidth(&tg->rt_bandwidth, | |
194 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); | |
195 | ||
196 | for_each_possible_cpu(i) { | |
197 | rt_rq = kzalloc_node(sizeof(struct rt_rq), | |
198 | GFP_KERNEL, cpu_to_node(i)); | |
199 | if (!rt_rq) | |
200 | goto err; | |
201 | ||
202 | rt_se = kzalloc_node(sizeof(struct sched_rt_entity), | |
203 | GFP_KERNEL, cpu_to_node(i)); | |
204 | if (!rt_se) | |
205 | goto err_free_rq; | |
206 | ||
07c54f7a | 207 | init_rt_rq(rt_rq); |
029632fb PZ |
208 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; |
209 | init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]); | |
210 | } | |
211 | ||
212 | return 1; | |
213 | ||
214 | err_free_rq: | |
215 | kfree(rt_rq); | |
216 | err: | |
217 | return 0; | |
218 | } | |
219 | ||
398a153b GH |
220 | #else /* CONFIG_RT_GROUP_SCHED */ |
221 | ||
a1ba4d8b PZ |
222 | #define rt_entity_is_task(rt_se) (1) |
223 | ||
8f48894f PZ |
224 | static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) |
225 | { | |
226 | return container_of(rt_se, struct task_struct, rt); | |
227 | } | |
228 | ||
398a153b GH |
229 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) |
230 | { | |
231 | return container_of(rt_rq, struct rq, rt); | |
232 | } | |
233 | ||
653d07a6 | 234 | static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se) |
398a153b GH |
235 | { |
236 | struct task_struct *p = rt_task_of(rt_se); | |
653d07a6 KT |
237 | |
238 | return task_rq(p); | |
239 | } | |
240 | ||
241 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) | |
242 | { | |
243 | struct rq *rq = rq_of_rt_se(rt_se); | |
398a153b GH |
244 | |
245 | return &rq->rt; | |
246 | } | |
247 | ||
029632fb PZ |
248 | void free_rt_sched_group(struct task_group *tg) { } |
249 | ||
250 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
251 | { | |
252 | return 1; | |
253 | } | |
398a153b GH |
254 | #endif /* CONFIG_RT_GROUP_SCHED */ |
255 | ||
4fd29176 | 256 | #ifdef CONFIG_SMP |
84de4274 | 257 | |
8046d680 | 258 | static void pull_rt_task(struct rq *this_rq); |
38033c37 | 259 | |
dc877341 PZ |
260 | static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev) |
261 | { | |
262 | /* Try to pull RT tasks here if we lower this rq's prio */ | |
263 | return rq->rt.highest_prio.curr > prev->prio; | |
264 | } | |
265 | ||
637f5085 | 266 | static inline int rt_overloaded(struct rq *rq) |
4fd29176 | 267 | { |
637f5085 | 268 | return atomic_read(&rq->rd->rto_count); |
4fd29176 | 269 | } |
84de4274 | 270 | |
4fd29176 SR |
271 | static inline void rt_set_overload(struct rq *rq) |
272 | { | |
1f11eb6a GH |
273 | if (!rq->online) |
274 | return; | |
275 | ||
c6c4927b | 276 | cpumask_set_cpu(rq->cpu, rq->rd->rto_mask); |
4fd29176 SR |
277 | /* |
278 | * Make sure the mask is visible before we set | |
279 | * the overload count. That is checked to determine | |
280 | * if we should look at the mask. It would be a shame | |
281 | * if we looked at the mask, but the mask was not | |
282 | * updated yet. | |
7c3f2ab7 PZ |
283 | * |
284 | * Matched by the barrier in pull_rt_task(). | |
4fd29176 | 285 | */ |
7c3f2ab7 | 286 | smp_wmb(); |
637f5085 | 287 | atomic_inc(&rq->rd->rto_count); |
4fd29176 | 288 | } |
84de4274 | 289 | |
4fd29176 SR |
290 | static inline void rt_clear_overload(struct rq *rq) |
291 | { | |
1f11eb6a GH |
292 | if (!rq->online) |
293 | return; | |
294 | ||
4fd29176 | 295 | /* the order here really doesn't matter */ |
637f5085 | 296 | atomic_dec(&rq->rd->rto_count); |
c6c4927b | 297 | cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask); |
4fd29176 | 298 | } |
73fe6aae | 299 | |
398a153b | 300 | static void update_rt_migration(struct rt_rq *rt_rq) |
73fe6aae | 301 | { |
a1ba4d8b | 302 | if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) { |
398a153b GH |
303 | if (!rt_rq->overloaded) { |
304 | rt_set_overload(rq_of_rt_rq(rt_rq)); | |
305 | rt_rq->overloaded = 1; | |
cdc8eb98 | 306 | } |
398a153b GH |
307 | } else if (rt_rq->overloaded) { |
308 | rt_clear_overload(rq_of_rt_rq(rt_rq)); | |
309 | rt_rq->overloaded = 0; | |
637f5085 | 310 | } |
73fe6aae | 311 | } |
4fd29176 | 312 | |
398a153b GH |
313 | static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
314 | { | |
29baa747 PZ |
315 | struct task_struct *p; |
316 | ||
a1ba4d8b PZ |
317 | if (!rt_entity_is_task(rt_se)) |
318 | return; | |
319 | ||
29baa747 | 320 | p = rt_task_of(rt_se); |
a1ba4d8b PZ |
321 | rt_rq = &rq_of_rt_rq(rt_rq)->rt; |
322 | ||
323 | rt_rq->rt_nr_total++; | |
4b53a341 | 324 | if (p->nr_cpus_allowed > 1) |
398a153b GH |
325 | rt_rq->rt_nr_migratory++; |
326 | ||
327 | update_rt_migration(rt_rq); | |
328 | } | |
329 | ||
330 | static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
331 | { | |
29baa747 PZ |
332 | struct task_struct *p; |
333 | ||
a1ba4d8b PZ |
334 | if (!rt_entity_is_task(rt_se)) |
335 | return; | |
336 | ||
29baa747 | 337 | p = rt_task_of(rt_se); |
a1ba4d8b PZ |
338 | rt_rq = &rq_of_rt_rq(rt_rq)->rt; |
339 | ||
340 | rt_rq->rt_nr_total--; | |
4b53a341 | 341 | if (p->nr_cpus_allowed > 1) |
398a153b GH |
342 | rt_rq->rt_nr_migratory--; |
343 | ||
344 | update_rt_migration(rt_rq); | |
345 | } | |
346 | ||
5181f4a4 SR |
347 | static inline int has_pushable_tasks(struct rq *rq) |
348 | { | |
349 | return !plist_head_empty(&rq->rt.pushable_tasks); | |
350 | } | |
351 | ||
fd7a4bed PZ |
352 | static DEFINE_PER_CPU(struct callback_head, rt_push_head); |
353 | static DEFINE_PER_CPU(struct callback_head, rt_pull_head); | |
e3fca9e7 PZ |
354 | |
355 | static void push_rt_tasks(struct rq *); | |
fd7a4bed | 356 | static void pull_rt_task(struct rq *); |
e3fca9e7 PZ |
357 | |
358 | static inline void queue_push_tasks(struct rq *rq) | |
dc877341 | 359 | { |
e3fca9e7 PZ |
360 | if (!has_pushable_tasks(rq)) |
361 | return; | |
362 | ||
fd7a4bed PZ |
363 | queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks); |
364 | } | |
365 | ||
366 | static inline void queue_pull_task(struct rq *rq) | |
367 | { | |
368 | queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task); | |
dc877341 PZ |
369 | } |
370 | ||
917b627d GH |
371 | static void enqueue_pushable_task(struct rq *rq, struct task_struct *p) |
372 | { | |
373 | plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); | |
374 | plist_node_init(&p->pushable_tasks, p->prio); | |
375 | plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks); | |
5181f4a4 SR |
376 | |
377 | /* Update the highest prio pushable task */ | |
378 | if (p->prio < rq->rt.highest_prio.next) | |
379 | rq->rt.highest_prio.next = p->prio; | |
917b627d GH |
380 | } |
381 | ||
382 | static void dequeue_pushable_task(struct rq *rq, struct task_struct *p) | |
383 | { | |
384 | plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); | |
917b627d | 385 | |
5181f4a4 SR |
386 | /* Update the new highest prio pushable task */ |
387 | if (has_pushable_tasks(rq)) { | |
388 | p = plist_first_entry(&rq->rt.pushable_tasks, | |
389 | struct task_struct, pushable_tasks); | |
390 | rq->rt.highest_prio.next = p->prio; | |
391 | } else | |
392 | rq->rt.highest_prio.next = MAX_RT_PRIO; | |
bcf08df3 IM |
393 | } |
394 | ||
917b627d GH |
395 | #else |
396 | ||
ceacc2c1 | 397 | static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p) |
fa85ae24 | 398 | { |
6f505b16 PZ |
399 | } |
400 | ||
ceacc2c1 PZ |
401 | static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p) |
402 | { | |
403 | } | |
404 | ||
b07430ac | 405 | static inline |
ceacc2c1 PZ |
406 | void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
407 | { | |
408 | } | |
409 | ||
398a153b | 410 | static inline |
ceacc2c1 PZ |
411 | void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
412 | { | |
413 | } | |
917b627d | 414 | |
dc877341 PZ |
415 | static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev) |
416 | { | |
417 | return false; | |
418 | } | |
419 | ||
8046d680 | 420 | static inline void pull_rt_task(struct rq *this_rq) |
dc877341 | 421 | { |
dc877341 PZ |
422 | } |
423 | ||
e3fca9e7 | 424 | static inline void queue_push_tasks(struct rq *rq) |
dc877341 PZ |
425 | { |
426 | } | |
4fd29176 SR |
427 | #endif /* CONFIG_SMP */ |
428 | ||
f4ebcbc0 KT |
429 | static void enqueue_top_rt_rq(struct rt_rq *rt_rq); |
430 | static void dequeue_top_rt_rq(struct rt_rq *rt_rq); | |
431 | ||
6f505b16 PZ |
432 | static inline int on_rt_rq(struct sched_rt_entity *rt_se) |
433 | { | |
ff77e468 | 434 | return rt_se->on_rq; |
6f505b16 PZ |
435 | } |
436 | ||
052f1dc7 | 437 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 438 | |
9f0c1e56 | 439 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) |
6f505b16 PZ |
440 | { |
441 | if (!rt_rq->tg) | |
9f0c1e56 | 442 | return RUNTIME_INF; |
6f505b16 | 443 | |
ac086bc2 PZ |
444 | return rt_rq->rt_runtime; |
445 | } | |
446 | ||
447 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) | |
448 | { | |
449 | return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period); | |
6f505b16 PZ |
450 | } |
451 | ||
ec514c48 CX |
452 | typedef struct task_group *rt_rq_iter_t; |
453 | ||
1c09ab0d YZ |
454 | static inline struct task_group *next_task_group(struct task_group *tg) |
455 | { | |
456 | do { | |
457 | tg = list_entry_rcu(tg->list.next, | |
458 | typeof(struct task_group), list); | |
459 | } while (&tg->list != &task_groups && task_group_is_autogroup(tg)); | |
460 | ||
461 | if (&tg->list == &task_groups) | |
462 | tg = NULL; | |
463 | ||
464 | return tg; | |
465 | } | |
466 | ||
467 | #define for_each_rt_rq(rt_rq, iter, rq) \ | |
468 | for (iter = container_of(&task_groups, typeof(*iter), list); \ | |
469 | (iter = next_task_group(iter)) && \ | |
470 | (rt_rq = iter->rt_rq[cpu_of(rq)]);) | |
ec514c48 | 471 | |
6f505b16 PZ |
472 | #define for_each_sched_rt_entity(rt_se) \ |
473 | for (; rt_se; rt_se = rt_se->parent) | |
474 | ||
475 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) | |
476 | { | |
477 | return rt_se->my_q; | |
478 | } | |
479 | ||
ff77e468 PZ |
480 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags); |
481 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags); | |
6f505b16 | 482 | |
9f0c1e56 | 483 | static void sched_rt_rq_enqueue(struct rt_rq *rt_rq) |
6f505b16 | 484 | { |
f6121f4f | 485 | struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr; |
8875125e | 486 | struct rq *rq = rq_of_rt_rq(rt_rq); |
74b7eb58 YZ |
487 | struct sched_rt_entity *rt_se; |
488 | ||
8875125e | 489 | int cpu = cpu_of(rq); |
0c3b9168 BS |
490 | |
491 | rt_se = rt_rq->tg->rt_se[cpu]; | |
6f505b16 | 492 | |
f6121f4f | 493 | if (rt_rq->rt_nr_running) { |
f4ebcbc0 KT |
494 | if (!rt_se) |
495 | enqueue_top_rt_rq(rt_rq); | |
496 | else if (!on_rt_rq(rt_se)) | |
ff77e468 | 497 | enqueue_rt_entity(rt_se, 0); |
f4ebcbc0 | 498 | |
e864c499 | 499 | if (rt_rq->highest_prio.curr < curr->prio) |
8875125e | 500 | resched_curr(rq); |
6f505b16 PZ |
501 | } |
502 | } | |
503 | ||
9f0c1e56 | 504 | static void sched_rt_rq_dequeue(struct rt_rq *rt_rq) |
6f505b16 | 505 | { |
74b7eb58 | 506 | struct sched_rt_entity *rt_se; |
0c3b9168 | 507 | int cpu = cpu_of(rq_of_rt_rq(rt_rq)); |
74b7eb58 | 508 | |
0c3b9168 | 509 | rt_se = rt_rq->tg->rt_se[cpu]; |
6f505b16 | 510 | |
f4ebcbc0 KT |
511 | if (!rt_se) |
512 | dequeue_top_rt_rq(rt_rq); | |
513 | else if (on_rt_rq(rt_se)) | |
ff77e468 | 514 | dequeue_rt_entity(rt_se, 0); |
6f505b16 PZ |
515 | } |
516 | ||
46383648 KT |
517 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) |
518 | { | |
519 | return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted; | |
520 | } | |
521 | ||
23b0fdfc PZ |
522 | static int rt_se_boosted(struct sched_rt_entity *rt_se) |
523 | { | |
524 | struct rt_rq *rt_rq = group_rt_rq(rt_se); | |
525 | struct task_struct *p; | |
526 | ||
527 | if (rt_rq) | |
528 | return !!rt_rq->rt_nr_boosted; | |
529 | ||
530 | p = rt_task_of(rt_se); | |
531 | return p->prio != p->normal_prio; | |
532 | } | |
533 | ||
d0b27fa7 | 534 | #ifdef CONFIG_SMP |
c6c4927b | 535 | static inline const struct cpumask *sched_rt_period_mask(void) |
d0b27fa7 | 536 | { |
424c93fe | 537 | return this_rq()->rd->span; |
d0b27fa7 | 538 | } |
6f505b16 | 539 | #else |
c6c4927b | 540 | static inline const struct cpumask *sched_rt_period_mask(void) |
d0b27fa7 | 541 | { |
c6c4927b | 542 | return cpu_online_mask; |
d0b27fa7 PZ |
543 | } |
544 | #endif | |
6f505b16 | 545 | |
d0b27fa7 PZ |
546 | static inline |
547 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) | |
6f505b16 | 548 | { |
d0b27fa7 PZ |
549 | return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu]; |
550 | } | |
9f0c1e56 | 551 | |
ac086bc2 PZ |
552 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) |
553 | { | |
554 | return &rt_rq->tg->rt_bandwidth; | |
555 | } | |
556 | ||
55e12e5e | 557 | #else /* !CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 PZ |
558 | |
559 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) | |
560 | { | |
ac086bc2 PZ |
561 | return rt_rq->rt_runtime; |
562 | } | |
563 | ||
564 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) | |
565 | { | |
566 | return ktime_to_ns(def_rt_bandwidth.rt_period); | |
6f505b16 PZ |
567 | } |
568 | ||
ec514c48 CX |
569 | typedef struct rt_rq *rt_rq_iter_t; |
570 | ||
571 | #define for_each_rt_rq(rt_rq, iter, rq) \ | |
572 | for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL) | |
573 | ||
6f505b16 PZ |
574 | #define for_each_sched_rt_entity(rt_se) \ |
575 | for (; rt_se; rt_se = NULL) | |
576 | ||
577 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) | |
578 | { | |
579 | return NULL; | |
580 | } | |
581 | ||
9f0c1e56 | 582 | static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq) |
6f505b16 | 583 | { |
f4ebcbc0 KT |
584 | struct rq *rq = rq_of_rt_rq(rt_rq); |
585 | ||
586 | if (!rt_rq->rt_nr_running) | |
587 | return; | |
588 | ||
589 | enqueue_top_rt_rq(rt_rq); | |
8875125e | 590 | resched_curr(rq); |
6f505b16 PZ |
591 | } |
592 | ||
9f0c1e56 | 593 | static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq) |
6f505b16 | 594 | { |
f4ebcbc0 | 595 | dequeue_top_rt_rq(rt_rq); |
6f505b16 PZ |
596 | } |
597 | ||
46383648 KT |
598 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) |
599 | { | |
600 | return rt_rq->rt_throttled; | |
601 | } | |
602 | ||
c6c4927b | 603 | static inline const struct cpumask *sched_rt_period_mask(void) |
d0b27fa7 | 604 | { |
c6c4927b | 605 | return cpu_online_mask; |
d0b27fa7 PZ |
606 | } |
607 | ||
608 | static inline | |
609 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) | |
610 | { | |
611 | return &cpu_rq(cpu)->rt; | |
612 | } | |
613 | ||
ac086bc2 PZ |
614 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) |
615 | { | |
616 | return &def_rt_bandwidth; | |
617 | } | |
618 | ||
55e12e5e | 619 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 | 620 | |
faa59937 JL |
621 | bool sched_rt_bandwidth_account(struct rt_rq *rt_rq) |
622 | { | |
623 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); | |
624 | ||
625 | return (hrtimer_active(&rt_b->rt_period_timer) || | |
626 | rt_rq->rt_time < rt_b->rt_runtime); | |
627 | } | |
628 | ||
ac086bc2 | 629 | #ifdef CONFIG_SMP |
78333cdd PZ |
630 | /* |
631 | * We ran out of runtime, see if we can borrow some from our neighbours. | |
632 | */ | |
269b26a5 | 633 | static void do_balance_runtime(struct rt_rq *rt_rq) |
ac086bc2 PZ |
634 | { |
635 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); | |
aa7f6730 | 636 | struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd; |
269b26a5 | 637 | int i, weight; |
ac086bc2 PZ |
638 | u64 rt_period; |
639 | ||
c6c4927b | 640 | weight = cpumask_weight(rd->span); |
ac086bc2 | 641 | |
0986b11b | 642 | raw_spin_lock(&rt_b->rt_runtime_lock); |
ac086bc2 | 643 | rt_period = ktime_to_ns(rt_b->rt_period); |
c6c4927b | 644 | for_each_cpu(i, rd->span) { |
ac086bc2 PZ |
645 | struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); |
646 | s64 diff; | |
647 | ||
648 | if (iter == rt_rq) | |
649 | continue; | |
650 | ||
0986b11b | 651 | raw_spin_lock(&iter->rt_runtime_lock); |
78333cdd PZ |
652 | /* |
653 | * Either all rqs have inf runtime and there's nothing to steal | |
654 | * or __disable_runtime() below sets a specific rq to inf to | |
655 | * indicate its been disabled and disalow stealing. | |
656 | */ | |
7def2be1 PZ |
657 | if (iter->rt_runtime == RUNTIME_INF) |
658 | goto next; | |
659 | ||
78333cdd PZ |
660 | /* |
661 | * From runqueues with spare time, take 1/n part of their | |
662 | * spare time, but no more than our period. | |
663 | */ | |
ac086bc2 PZ |
664 | diff = iter->rt_runtime - iter->rt_time; |
665 | if (diff > 0) { | |
58838cf3 | 666 | diff = div_u64((u64)diff, weight); |
ac086bc2 PZ |
667 | if (rt_rq->rt_runtime + diff > rt_period) |
668 | diff = rt_period - rt_rq->rt_runtime; | |
669 | iter->rt_runtime -= diff; | |
670 | rt_rq->rt_runtime += diff; | |
ac086bc2 | 671 | if (rt_rq->rt_runtime == rt_period) { |
0986b11b | 672 | raw_spin_unlock(&iter->rt_runtime_lock); |
ac086bc2 PZ |
673 | break; |
674 | } | |
675 | } | |
7def2be1 | 676 | next: |
0986b11b | 677 | raw_spin_unlock(&iter->rt_runtime_lock); |
ac086bc2 | 678 | } |
0986b11b | 679 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
ac086bc2 | 680 | } |
7def2be1 | 681 | |
78333cdd PZ |
682 | /* |
683 | * Ensure this RQ takes back all the runtime it lend to its neighbours. | |
684 | */ | |
7def2be1 PZ |
685 | static void __disable_runtime(struct rq *rq) |
686 | { | |
687 | struct root_domain *rd = rq->rd; | |
ec514c48 | 688 | rt_rq_iter_t iter; |
7def2be1 PZ |
689 | struct rt_rq *rt_rq; |
690 | ||
691 | if (unlikely(!scheduler_running)) | |
692 | return; | |
693 | ||
ec514c48 | 694 | for_each_rt_rq(rt_rq, iter, rq) { |
7def2be1 PZ |
695 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
696 | s64 want; | |
697 | int i; | |
698 | ||
0986b11b TG |
699 | raw_spin_lock(&rt_b->rt_runtime_lock); |
700 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
78333cdd PZ |
701 | /* |
702 | * Either we're all inf and nobody needs to borrow, or we're | |
703 | * already disabled and thus have nothing to do, or we have | |
704 | * exactly the right amount of runtime to take out. | |
705 | */ | |
7def2be1 PZ |
706 | if (rt_rq->rt_runtime == RUNTIME_INF || |
707 | rt_rq->rt_runtime == rt_b->rt_runtime) | |
708 | goto balanced; | |
0986b11b | 709 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
7def2be1 | 710 | |
78333cdd PZ |
711 | /* |
712 | * Calculate the difference between what we started out with | |
713 | * and what we current have, that's the amount of runtime | |
714 | * we lend and now have to reclaim. | |
715 | */ | |
7def2be1 PZ |
716 | want = rt_b->rt_runtime - rt_rq->rt_runtime; |
717 | ||
78333cdd PZ |
718 | /* |
719 | * Greedy reclaim, take back as much as we can. | |
720 | */ | |
c6c4927b | 721 | for_each_cpu(i, rd->span) { |
7def2be1 PZ |
722 | struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); |
723 | s64 diff; | |
724 | ||
78333cdd PZ |
725 | /* |
726 | * Can't reclaim from ourselves or disabled runqueues. | |
727 | */ | |
f1679d08 | 728 | if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF) |
7def2be1 PZ |
729 | continue; |
730 | ||
0986b11b | 731 | raw_spin_lock(&iter->rt_runtime_lock); |
7def2be1 PZ |
732 | if (want > 0) { |
733 | diff = min_t(s64, iter->rt_runtime, want); | |
734 | iter->rt_runtime -= diff; | |
735 | want -= diff; | |
736 | } else { | |
737 | iter->rt_runtime -= want; | |
738 | want -= want; | |
739 | } | |
0986b11b | 740 | raw_spin_unlock(&iter->rt_runtime_lock); |
7def2be1 PZ |
741 | |
742 | if (!want) | |
743 | break; | |
744 | } | |
745 | ||
0986b11b | 746 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
78333cdd PZ |
747 | /* |
748 | * We cannot be left wanting - that would mean some runtime | |
749 | * leaked out of the system. | |
750 | */ | |
7def2be1 PZ |
751 | BUG_ON(want); |
752 | balanced: | |
78333cdd PZ |
753 | /* |
754 | * Disable all the borrow logic by pretending we have inf | |
755 | * runtime - in which case borrowing doesn't make sense. | |
756 | */ | |
7def2be1 | 757 | rt_rq->rt_runtime = RUNTIME_INF; |
a4c96ae3 | 758 | rt_rq->rt_throttled = 0; |
0986b11b TG |
759 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
760 | raw_spin_unlock(&rt_b->rt_runtime_lock); | |
99b62567 KT |
761 | |
762 | /* Make rt_rq available for pick_next_task() */ | |
763 | sched_rt_rq_enqueue(rt_rq); | |
7def2be1 PZ |
764 | } |
765 | } | |
766 | ||
7def2be1 PZ |
767 | static void __enable_runtime(struct rq *rq) |
768 | { | |
ec514c48 | 769 | rt_rq_iter_t iter; |
7def2be1 PZ |
770 | struct rt_rq *rt_rq; |
771 | ||
772 | if (unlikely(!scheduler_running)) | |
773 | return; | |
774 | ||
78333cdd PZ |
775 | /* |
776 | * Reset each runqueue's bandwidth settings | |
777 | */ | |
ec514c48 | 778 | for_each_rt_rq(rt_rq, iter, rq) { |
7def2be1 PZ |
779 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
780 | ||
0986b11b TG |
781 | raw_spin_lock(&rt_b->rt_runtime_lock); |
782 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
7def2be1 PZ |
783 | rt_rq->rt_runtime = rt_b->rt_runtime; |
784 | rt_rq->rt_time = 0; | |
baf25731 | 785 | rt_rq->rt_throttled = 0; |
0986b11b TG |
786 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
787 | raw_spin_unlock(&rt_b->rt_runtime_lock); | |
7def2be1 PZ |
788 | } |
789 | } | |
790 | ||
269b26a5 | 791 | static void balance_runtime(struct rt_rq *rt_rq) |
eff6549b | 792 | { |
4a6184ce | 793 | if (!sched_feat(RT_RUNTIME_SHARE)) |
269b26a5 | 794 | return; |
4a6184ce | 795 | |
eff6549b | 796 | if (rt_rq->rt_time > rt_rq->rt_runtime) { |
0986b11b | 797 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
269b26a5 | 798 | do_balance_runtime(rt_rq); |
0986b11b | 799 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
eff6549b | 800 | } |
eff6549b | 801 | } |
55e12e5e | 802 | #else /* !CONFIG_SMP */ |
269b26a5 | 803 | static inline void balance_runtime(struct rt_rq *rt_rq) {} |
55e12e5e | 804 | #endif /* CONFIG_SMP */ |
ac086bc2 | 805 | |
eff6549b PZ |
806 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun) |
807 | { | |
42c62a58 | 808 | int i, idle = 1, throttled = 0; |
c6c4927b | 809 | const struct cpumask *span; |
eff6549b | 810 | |
eff6549b | 811 | span = sched_rt_period_mask(); |
e221d028 MG |
812 | #ifdef CONFIG_RT_GROUP_SCHED |
813 | /* | |
814 | * FIXME: isolated CPUs should really leave the root task group, | |
815 | * whether they are isolcpus or were isolated via cpusets, lest | |
816 | * the timer run on a CPU which does not service all runqueues, | |
817 | * potentially leaving other CPUs indefinitely throttled. If | |
818 | * isolation is really required, the user will turn the throttle | |
819 | * off to kill the perturbations it causes anyway. Meanwhile, | |
820 | * this maintains functionality for boot and/or troubleshooting. | |
821 | */ | |
822 | if (rt_b == &root_task_group.rt_bandwidth) | |
823 | span = cpu_online_mask; | |
824 | #endif | |
c6c4927b | 825 | for_each_cpu(i, span) { |
eff6549b PZ |
826 | int enqueue = 0; |
827 | struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i); | |
828 | struct rq *rq = rq_of_rt_rq(rt_rq); | |
c249f255 DK |
829 | int skip; |
830 | ||
831 | /* | |
832 | * When span == cpu_online_mask, taking each rq->lock | |
833 | * can be time-consuming. Try to avoid it when possible. | |
834 | */ | |
835 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
836 | skip = !rt_rq->rt_time && !rt_rq->rt_nr_running; | |
837 | raw_spin_unlock(&rt_rq->rt_runtime_lock); | |
838 | if (skip) | |
839 | continue; | |
eff6549b | 840 | |
05fa785c | 841 | raw_spin_lock(&rq->lock); |
eff6549b PZ |
842 | if (rt_rq->rt_time) { |
843 | u64 runtime; | |
844 | ||
0986b11b | 845 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
eff6549b PZ |
846 | if (rt_rq->rt_throttled) |
847 | balance_runtime(rt_rq); | |
848 | runtime = rt_rq->rt_runtime; | |
849 | rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime); | |
850 | if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) { | |
851 | rt_rq->rt_throttled = 0; | |
852 | enqueue = 1; | |
61eadef6 MG |
853 | |
854 | /* | |
9edfbfed PZ |
855 | * When we're idle and a woken (rt) task is |
856 | * throttled check_preempt_curr() will set | |
857 | * skip_update and the time between the wakeup | |
858 | * and this unthrottle will get accounted as | |
859 | * 'runtime'. | |
61eadef6 MG |
860 | */ |
861 | if (rt_rq->rt_nr_running && rq->curr == rq->idle) | |
9edfbfed | 862 | rq_clock_skip_update(rq, false); |
eff6549b PZ |
863 | } |
864 | if (rt_rq->rt_time || rt_rq->rt_nr_running) | |
865 | idle = 0; | |
0986b11b | 866 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
0c3b9168 | 867 | } else if (rt_rq->rt_nr_running) { |
6c3df255 | 868 | idle = 0; |
0c3b9168 BS |
869 | if (!rt_rq_throttled(rt_rq)) |
870 | enqueue = 1; | |
871 | } | |
42c62a58 PZ |
872 | if (rt_rq->rt_throttled) |
873 | throttled = 1; | |
eff6549b PZ |
874 | |
875 | if (enqueue) | |
876 | sched_rt_rq_enqueue(rt_rq); | |
05fa785c | 877 | raw_spin_unlock(&rq->lock); |
eff6549b PZ |
878 | } |
879 | ||
42c62a58 PZ |
880 | if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)) |
881 | return 1; | |
882 | ||
eff6549b PZ |
883 | return idle; |
884 | } | |
ac086bc2 | 885 | |
6f505b16 PZ |
886 | static inline int rt_se_prio(struct sched_rt_entity *rt_se) |
887 | { | |
052f1dc7 | 888 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 PZ |
889 | struct rt_rq *rt_rq = group_rt_rq(rt_se); |
890 | ||
891 | if (rt_rq) | |
e864c499 | 892 | return rt_rq->highest_prio.curr; |
6f505b16 PZ |
893 | #endif |
894 | ||
895 | return rt_task_of(rt_se)->prio; | |
896 | } | |
897 | ||
9f0c1e56 | 898 | static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq) |
6f505b16 | 899 | { |
9f0c1e56 | 900 | u64 runtime = sched_rt_runtime(rt_rq); |
fa85ae24 | 901 | |
fa85ae24 | 902 | if (rt_rq->rt_throttled) |
23b0fdfc | 903 | return rt_rq_throttled(rt_rq); |
fa85ae24 | 904 | |
5b680fd6 | 905 | if (runtime >= sched_rt_period(rt_rq)) |
ac086bc2 PZ |
906 | return 0; |
907 | ||
b79f3833 PZ |
908 | balance_runtime(rt_rq); |
909 | runtime = sched_rt_runtime(rt_rq); | |
910 | if (runtime == RUNTIME_INF) | |
911 | return 0; | |
ac086bc2 | 912 | |
9f0c1e56 | 913 | if (rt_rq->rt_time > runtime) { |
7abc63b1 PZ |
914 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
915 | ||
916 | /* | |
917 | * Don't actually throttle groups that have no runtime assigned | |
918 | * but accrue some time due to boosting. | |
919 | */ | |
920 | if (likely(rt_b->rt_runtime)) { | |
921 | rt_rq->rt_throttled = 1; | |
c224815d | 922 | printk_deferred_once("sched: RT throttling activated\n"); |
7abc63b1 PZ |
923 | } else { |
924 | /* | |
925 | * In case we did anyway, make it go away, | |
926 | * replenishment is a joke, since it will replenish us | |
927 | * with exactly 0 ns. | |
928 | */ | |
929 | rt_rq->rt_time = 0; | |
930 | } | |
931 | ||
23b0fdfc | 932 | if (rt_rq_throttled(rt_rq)) { |
9f0c1e56 | 933 | sched_rt_rq_dequeue(rt_rq); |
23b0fdfc PZ |
934 | return 1; |
935 | } | |
fa85ae24 PZ |
936 | } |
937 | ||
938 | return 0; | |
939 | } | |
940 | ||
bb44e5d1 IM |
941 | /* |
942 | * Update the current task's runtime statistics. Skip current tasks that | |
943 | * are not in our scheduling class. | |
944 | */ | |
a9957449 | 945 | static void update_curr_rt(struct rq *rq) |
bb44e5d1 IM |
946 | { |
947 | struct task_struct *curr = rq->curr; | |
6f505b16 | 948 | struct sched_rt_entity *rt_se = &curr->rt; |
bb44e5d1 | 949 | u64 delta_exec; |
a7711602 | 950 | u64 now; |
bb44e5d1 | 951 | |
06c3bc65 | 952 | if (curr->sched_class != &rt_sched_class) |
bb44e5d1 IM |
953 | return; |
954 | ||
a7711602 | 955 | now = rq_clock_task(rq); |
e7ad2031 | 956 | delta_exec = now - curr->se.exec_start; |
fc79e240 KT |
957 | if (unlikely((s64)delta_exec <= 0)) |
958 | return; | |
6cfb0d5d | 959 | |
58919e83 | 960 | /* Kick cpufreq (see the comment in kernel/sched/sched.h). */ |
674e7541 | 961 | cpufreq_update_util(rq, SCHED_CPUFREQ_RT); |
594dd290 | 962 | |
42c62a58 PZ |
963 | schedstat_set(curr->se.statistics.exec_max, |
964 | max(curr->se.statistics.exec_max, delta_exec)); | |
bb44e5d1 IM |
965 | |
966 | curr->se.sum_exec_runtime += delta_exec; | |
f06febc9 FM |
967 | account_group_exec_runtime(curr, delta_exec); |
968 | ||
e7ad2031 | 969 | curr->se.exec_start = now; |
d2cc5ed6 | 970 | cgroup_account_cputime(curr, delta_exec); |
fa85ae24 | 971 | |
e9e9250b PZ |
972 | sched_rt_avg_update(rq, delta_exec); |
973 | ||
0b148fa0 PZ |
974 | if (!rt_bandwidth_enabled()) |
975 | return; | |
976 | ||
354d60c2 | 977 | for_each_sched_rt_entity(rt_se) { |
0b07939c | 978 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
354d60c2 | 979 | |
cc2991cf | 980 | if (sched_rt_runtime(rt_rq) != RUNTIME_INF) { |
0986b11b | 981 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
cc2991cf PZ |
982 | rt_rq->rt_time += delta_exec; |
983 | if (sched_rt_runtime_exceeded(rt_rq)) | |
8875125e | 984 | resched_curr(rq); |
0986b11b | 985 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
cc2991cf | 986 | } |
354d60c2 | 987 | } |
bb44e5d1 IM |
988 | } |
989 | ||
f4ebcbc0 KT |
990 | static void |
991 | dequeue_top_rt_rq(struct rt_rq *rt_rq) | |
992 | { | |
993 | struct rq *rq = rq_of_rt_rq(rt_rq); | |
994 | ||
995 | BUG_ON(&rq->rt != rt_rq); | |
996 | ||
997 | if (!rt_rq->rt_queued) | |
998 | return; | |
999 | ||
1000 | BUG_ON(!rq->nr_running); | |
1001 | ||
72465447 | 1002 | sub_nr_running(rq, rt_rq->rt_nr_running); |
f4ebcbc0 KT |
1003 | rt_rq->rt_queued = 0; |
1004 | } | |
1005 | ||
1006 | static void | |
1007 | enqueue_top_rt_rq(struct rt_rq *rt_rq) | |
1008 | { | |
1009 | struct rq *rq = rq_of_rt_rq(rt_rq); | |
1010 | ||
1011 | BUG_ON(&rq->rt != rt_rq); | |
1012 | ||
1013 | if (rt_rq->rt_queued) | |
1014 | return; | |
1015 | if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running) | |
1016 | return; | |
1017 | ||
72465447 | 1018 | add_nr_running(rq, rt_rq->rt_nr_running); |
f4ebcbc0 KT |
1019 | rt_rq->rt_queued = 1; |
1020 | } | |
1021 | ||
398a153b | 1022 | #if defined CONFIG_SMP |
e864c499 | 1023 | |
398a153b GH |
1024 | static void |
1025 | inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) | |
63489e45 | 1026 | { |
4d984277 | 1027 | struct rq *rq = rq_of_rt_rq(rt_rq); |
1f11eb6a | 1028 | |
757dfcaa KT |
1029 | #ifdef CONFIG_RT_GROUP_SCHED |
1030 | /* | |
1031 | * Change rq's cpupri only if rt_rq is the top queue. | |
1032 | */ | |
1033 | if (&rq->rt != rt_rq) | |
1034 | return; | |
1035 | #endif | |
5181f4a4 SR |
1036 | if (rq->online && prio < prev_prio) |
1037 | cpupri_set(&rq->rd->cpupri, rq->cpu, prio); | |
398a153b | 1038 | } |
73fe6aae | 1039 | |
398a153b GH |
1040 | static void |
1041 | dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) | |
1042 | { | |
1043 | struct rq *rq = rq_of_rt_rq(rt_rq); | |
d0b27fa7 | 1044 | |
757dfcaa KT |
1045 | #ifdef CONFIG_RT_GROUP_SCHED |
1046 | /* | |
1047 | * Change rq's cpupri only if rt_rq is the top queue. | |
1048 | */ | |
1049 | if (&rq->rt != rt_rq) | |
1050 | return; | |
1051 | #endif | |
398a153b GH |
1052 | if (rq->online && rt_rq->highest_prio.curr != prev_prio) |
1053 | cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr); | |
63489e45 SR |
1054 | } |
1055 | ||
398a153b GH |
1056 | #else /* CONFIG_SMP */ |
1057 | ||
6f505b16 | 1058 | static inline |
398a153b GH |
1059 | void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} |
1060 | static inline | |
1061 | void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} | |
1062 | ||
1063 | #endif /* CONFIG_SMP */ | |
6e0534f2 | 1064 | |
052f1dc7 | 1065 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
398a153b GH |
1066 | static void |
1067 | inc_rt_prio(struct rt_rq *rt_rq, int prio) | |
1068 | { | |
1069 | int prev_prio = rt_rq->highest_prio.curr; | |
1070 | ||
1071 | if (prio < prev_prio) | |
1072 | rt_rq->highest_prio.curr = prio; | |
1073 | ||
1074 | inc_rt_prio_smp(rt_rq, prio, prev_prio); | |
1075 | } | |
1076 | ||
1077 | static void | |
1078 | dec_rt_prio(struct rt_rq *rt_rq, int prio) | |
1079 | { | |
1080 | int prev_prio = rt_rq->highest_prio.curr; | |
1081 | ||
6f505b16 | 1082 | if (rt_rq->rt_nr_running) { |
764a9d6f | 1083 | |
398a153b | 1084 | WARN_ON(prio < prev_prio); |
764a9d6f | 1085 | |
e864c499 | 1086 | /* |
398a153b GH |
1087 | * This may have been our highest task, and therefore |
1088 | * we may have some recomputation to do | |
e864c499 | 1089 | */ |
398a153b | 1090 | if (prio == prev_prio) { |
e864c499 GH |
1091 | struct rt_prio_array *array = &rt_rq->active; |
1092 | ||
1093 | rt_rq->highest_prio.curr = | |
764a9d6f | 1094 | sched_find_first_bit(array->bitmap); |
e864c499 GH |
1095 | } |
1096 | ||
764a9d6f | 1097 | } else |
e864c499 | 1098 | rt_rq->highest_prio.curr = MAX_RT_PRIO; |
73fe6aae | 1099 | |
398a153b GH |
1100 | dec_rt_prio_smp(rt_rq, prio, prev_prio); |
1101 | } | |
1f11eb6a | 1102 | |
398a153b GH |
1103 | #else |
1104 | ||
1105 | static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {} | |
1106 | static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {} | |
1107 | ||
1108 | #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */ | |
6e0534f2 | 1109 | |
052f1dc7 | 1110 | #ifdef CONFIG_RT_GROUP_SCHED |
398a153b GH |
1111 | |
1112 | static void | |
1113 | inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1114 | { | |
1115 | if (rt_se_boosted(rt_se)) | |
1116 | rt_rq->rt_nr_boosted++; | |
1117 | ||
1118 | if (rt_rq->tg) | |
1119 | start_rt_bandwidth(&rt_rq->tg->rt_bandwidth); | |
1120 | } | |
1121 | ||
1122 | static void | |
1123 | dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1124 | { | |
23b0fdfc PZ |
1125 | if (rt_se_boosted(rt_se)) |
1126 | rt_rq->rt_nr_boosted--; | |
1127 | ||
1128 | WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted); | |
398a153b GH |
1129 | } |
1130 | ||
1131 | #else /* CONFIG_RT_GROUP_SCHED */ | |
1132 | ||
1133 | static void | |
1134 | inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1135 | { | |
1136 | start_rt_bandwidth(&def_rt_bandwidth); | |
1137 | } | |
1138 | ||
1139 | static inline | |
1140 | void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {} | |
1141 | ||
1142 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
1143 | ||
22abdef3 KT |
1144 | static inline |
1145 | unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se) | |
1146 | { | |
1147 | struct rt_rq *group_rq = group_rt_rq(rt_se); | |
1148 | ||
1149 | if (group_rq) | |
1150 | return group_rq->rt_nr_running; | |
1151 | else | |
1152 | return 1; | |
1153 | } | |
1154 | ||
01d36d0a FW |
1155 | static inline |
1156 | unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se) | |
1157 | { | |
1158 | struct rt_rq *group_rq = group_rt_rq(rt_se); | |
1159 | struct task_struct *tsk; | |
1160 | ||
1161 | if (group_rq) | |
1162 | return group_rq->rr_nr_running; | |
1163 | ||
1164 | tsk = rt_task_of(rt_se); | |
1165 | ||
1166 | return (tsk->policy == SCHED_RR) ? 1 : 0; | |
1167 | } | |
1168 | ||
398a153b GH |
1169 | static inline |
1170 | void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1171 | { | |
1172 | int prio = rt_se_prio(rt_se); | |
1173 | ||
1174 | WARN_ON(!rt_prio(prio)); | |
22abdef3 | 1175 | rt_rq->rt_nr_running += rt_se_nr_running(rt_se); |
01d36d0a | 1176 | rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se); |
398a153b GH |
1177 | |
1178 | inc_rt_prio(rt_rq, prio); | |
1179 | inc_rt_migration(rt_se, rt_rq); | |
1180 | inc_rt_group(rt_se, rt_rq); | |
1181 | } | |
1182 | ||
1183 | static inline | |
1184 | void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | |
1185 | { | |
1186 | WARN_ON(!rt_prio(rt_se_prio(rt_se))); | |
1187 | WARN_ON(!rt_rq->rt_nr_running); | |
22abdef3 | 1188 | rt_rq->rt_nr_running -= rt_se_nr_running(rt_se); |
01d36d0a | 1189 | rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se); |
398a153b GH |
1190 | |
1191 | dec_rt_prio(rt_rq, rt_se_prio(rt_se)); | |
1192 | dec_rt_migration(rt_se, rt_rq); | |
1193 | dec_rt_group(rt_se, rt_rq); | |
63489e45 SR |
1194 | } |
1195 | ||
ff77e468 PZ |
1196 | /* |
1197 | * Change rt_se->run_list location unless SAVE && !MOVE | |
1198 | * | |
1199 | * assumes ENQUEUE/DEQUEUE flags match | |
1200 | */ | |
1201 | static inline bool move_entity(unsigned int flags) | |
1202 | { | |
1203 | if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE) | |
1204 | return false; | |
1205 | ||
1206 | return true; | |
1207 | } | |
1208 | ||
1209 | static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array) | |
1210 | { | |
1211 | list_del_init(&rt_se->run_list); | |
1212 | ||
1213 | if (list_empty(array->queue + rt_se_prio(rt_se))) | |
1214 | __clear_bit(rt_se_prio(rt_se), array->bitmap); | |
1215 | ||
1216 | rt_se->on_list = 0; | |
1217 | } | |
1218 | ||
1219 | static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) | |
bb44e5d1 | 1220 | { |
6f505b16 PZ |
1221 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
1222 | struct rt_prio_array *array = &rt_rq->active; | |
1223 | struct rt_rq *group_rq = group_rt_rq(rt_se); | |
20b6331b | 1224 | struct list_head *queue = array->queue + rt_se_prio(rt_se); |
bb44e5d1 | 1225 | |
ad2a3f13 PZ |
1226 | /* |
1227 | * Don't enqueue the group if its throttled, or when empty. | |
1228 | * The latter is a consequence of the former when a child group | |
1229 | * get throttled and the current group doesn't have any other | |
1230 | * active members. | |
1231 | */ | |
ff77e468 PZ |
1232 | if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) { |
1233 | if (rt_se->on_list) | |
1234 | __delist_rt_entity(rt_se, array); | |
6f505b16 | 1235 | return; |
ff77e468 | 1236 | } |
63489e45 | 1237 | |
ff77e468 PZ |
1238 | if (move_entity(flags)) { |
1239 | WARN_ON_ONCE(rt_se->on_list); | |
1240 | if (flags & ENQUEUE_HEAD) | |
1241 | list_add(&rt_se->run_list, queue); | |
1242 | else | |
1243 | list_add_tail(&rt_se->run_list, queue); | |
1244 | ||
1245 | __set_bit(rt_se_prio(rt_se), array->bitmap); | |
1246 | rt_se->on_list = 1; | |
1247 | } | |
1248 | rt_se->on_rq = 1; | |
78f2c7db | 1249 | |
6f505b16 PZ |
1250 | inc_rt_tasks(rt_se, rt_rq); |
1251 | } | |
1252 | ||
ff77e468 | 1253 | static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) |
6f505b16 PZ |
1254 | { |
1255 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); | |
1256 | struct rt_prio_array *array = &rt_rq->active; | |
1257 | ||
ff77e468 PZ |
1258 | if (move_entity(flags)) { |
1259 | WARN_ON_ONCE(!rt_se->on_list); | |
1260 | __delist_rt_entity(rt_se, array); | |
1261 | } | |
1262 | rt_se->on_rq = 0; | |
6f505b16 PZ |
1263 | |
1264 | dec_rt_tasks(rt_se, rt_rq); | |
1265 | } | |
1266 | ||
1267 | /* | |
1268 | * Because the prio of an upper entry depends on the lower | |
1269 | * entries, we must remove entries top - down. | |
6f505b16 | 1270 | */ |
ff77e468 | 1271 | static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags) |
6f505b16 | 1272 | { |
ad2a3f13 | 1273 | struct sched_rt_entity *back = NULL; |
6f505b16 | 1274 | |
58d6c2d7 PZ |
1275 | for_each_sched_rt_entity(rt_se) { |
1276 | rt_se->back = back; | |
1277 | back = rt_se; | |
1278 | } | |
1279 | ||
f4ebcbc0 KT |
1280 | dequeue_top_rt_rq(rt_rq_of_se(back)); |
1281 | ||
58d6c2d7 PZ |
1282 | for (rt_se = back; rt_se; rt_se = rt_se->back) { |
1283 | if (on_rt_rq(rt_se)) | |
ff77e468 | 1284 | __dequeue_rt_entity(rt_se, flags); |
ad2a3f13 PZ |
1285 | } |
1286 | } | |
1287 | ||
ff77e468 | 1288 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) |
ad2a3f13 | 1289 | { |
f4ebcbc0 KT |
1290 | struct rq *rq = rq_of_rt_se(rt_se); |
1291 | ||
ff77e468 | 1292 | dequeue_rt_stack(rt_se, flags); |
ad2a3f13 | 1293 | for_each_sched_rt_entity(rt_se) |
ff77e468 | 1294 | __enqueue_rt_entity(rt_se, flags); |
f4ebcbc0 | 1295 | enqueue_top_rt_rq(&rq->rt); |
ad2a3f13 PZ |
1296 | } |
1297 | ||
ff77e468 | 1298 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) |
ad2a3f13 | 1299 | { |
f4ebcbc0 KT |
1300 | struct rq *rq = rq_of_rt_se(rt_se); |
1301 | ||
ff77e468 | 1302 | dequeue_rt_stack(rt_se, flags); |
ad2a3f13 PZ |
1303 | |
1304 | for_each_sched_rt_entity(rt_se) { | |
1305 | struct rt_rq *rt_rq = group_rt_rq(rt_se); | |
1306 | ||
1307 | if (rt_rq && rt_rq->rt_nr_running) | |
ff77e468 | 1308 | __enqueue_rt_entity(rt_se, flags); |
58d6c2d7 | 1309 | } |
f4ebcbc0 | 1310 | enqueue_top_rt_rq(&rq->rt); |
bb44e5d1 IM |
1311 | } |
1312 | ||
1313 | /* | |
1314 | * Adding/removing a task to/from a priority array: | |
1315 | */ | |
ea87bb78 | 1316 | static void |
371fd7e7 | 1317 | enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags) |
6f505b16 PZ |
1318 | { |
1319 | struct sched_rt_entity *rt_se = &p->rt; | |
1320 | ||
371fd7e7 | 1321 | if (flags & ENQUEUE_WAKEUP) |
6f505b16 PZ |
1322 | rt_se->timeout = 0; |
1323 | ||
ff77e468 | 1324 | enqueue_rt_entity(rt_se, flags); |
c09595f6 | 1325 | |
4b53a341 | 1326 | if (!task_current(rq, p) && p->nr_cpus_allowed > 1) |
917b627d | 1327 | enqueue_pushable_task(rq, p); |
6f505b16 PZ |
1328 | } |
1329 | ||
371fd7e7 | 1330 | static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags) |
bb44e5d1 | 1331 | { |
6f505b16 | 1332 | struct sched_rt_entity *rt_se = &p->rt; |
bb44e5d1 | 1333 | |
f1e14ef6 | 1334 | update_curr_rt(rq); |
ff77e468 | 1335 | dequeue_rt_entity(rt_se, flags); |
c09595f6 | 1336 | |
917b627d | 1337 | dequeue_pushable_task(rq, p); |
bb44e5d1 IM |
1338 | } |
1339 | ||
1340 | /* | |
60686317 RW |
1341 | * Put task to the head or the end of the run list without the overhead of |
1342 | * dequeue followed by enqueue. | |
bb44e5d1 | 1343 | */ |
7ebefa8c DA |
1344 | static void |
1345 | requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head) | |
6f505b16 | 1346 | { |
1cdad715 | 1347 | if (on_rt_rq(rt_se)) { |
7ebefa8c DA |
1348 | struct rt_prio_array *array = &rt_rq->active; |
1349 | struct list_head *queue = array->queue + rt_se_prio(rt_se); | |
1350 | ||
1351 | if (head) | |
1352 | list_move(&rt_se->run_list, queue); | |
1353 | else | |
1354 | list_move_tail(&rt_se->run_list, queue); | |
1cdad715 | 1355 | } |
6f505b16 PZ |
1356 | } |
1357 | ||
7ebefa8c | 1358 | static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head) |
bb44e5d1 | 1359 | { |
6f505b16 PZ |
1360 | struct sched_rt_entity *rt_se = &p->rt; |
1361 | struct rt_rq *rt_rq; | |
bb44e5d1 | 1362 | |
6f505b16 PZ |
1363 | for_each_sched_rt_entity(rt_se) { |
1364 | rt_rq = rt_rq_of_se(rt_se); | |
7ebefa8c | 1365 | requeue_rt_entity(rt_rq, rt_se, head); |
6f505b16 | 1366 | } |
bb44e5d1 IM |
1367 | } |
1368 | ||
6f505b16 | 1369 | static void yield_task_rt(struct rq *rq) |
bb44e5d1 | 1370 | { |
7ebefa8c | 1371 | requeue_task_rt(rq, rq->curr, 0); |
bb44e5d1 IM |
1372 | } |
1373 | ||
e7693a36 | 1374 | #ifdef CONFIG_SMP |
318e0893 GH |
1375 | static int find_lowest_rq(struct task_struct *task); |
1376 | ||
0017d735 | 1377 | static int |
ac66f547 | 1378 | select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags) |
e7693a36 | 1379 | { |
7608dec2 PZ |
1380 | struct task_struct *curr; |
1381 | struct rq *rq; | |
c37495fd SR |
1382 | |
1383 | /* For anything but wake ups, just return the task_cpu */ | |
1384 | if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK) | |
1385 | goto out; | |
1386 | ||
7608dec2 PZ |
1387 | rq = cpu_rq(cpu); |
1388 | ||
1389 | rcu_read_lock(); | |
316c1608 | 1390 | curr = READ_ONCE(rq->curr); /* unlocked access */ |
7608dec2 | 1391 | |
318e0893 | 1392 | /* |
7608dec2 | 1393 | * If the current task on @p's runqueue is an RT task, then |
e1f47d89 SR |
1394 | * try to see if we can wake this RT task up on another |
1395 | * runqueue. Otherwise simply start this RT task | |
1396 | * on its current runqueue. | |
1397 | * | |
43fa5460 SR |
1398 | * We want to avoid overloading runqueues. If the woken |
1399 | * task is a higher priority, then it will stay on this CPU | |
1400 | * and the lower prio task should be moved to another CPU. | |
1401 | * Even though this will probably make the lower prio task | |
1402 | * lose its cache, we do not want to bounce a higher task | |
1403 | * around just because it gave up its CPU, perhaps for a | |
1404 | * lock? | |
1405 | * | |
1406 | * For equal prio tasks, we just let the scheduler sort it out. | |
7608dec2 PZ |
1407 | * |
1408 | * Otherwise, just let it ride on the affined RQ and the | |
1409 | * post-schedule router will push the preempted task away | |
1410 | * | |
1411 | * This test is optimistic, if we get it wrong the load-balancer | |
1412 | * will have to sort it out. | |
318e0893 | 1413 | */ |
7608dec2 | 1414 | if (curr && unlikely(rt_task(curr)) && |
4b53a341 | 1415 | (curr->nr_cpus_allowed < 2 || |
6bfa687c | 1416 | curr->prio <= p->prio)) { |
7608dec2 | 1417 | int target = find_lowest_rq(p); |
318e0893 | 1418 | |
80e3d87b TC |
1419 | /* |
1420 | * Don't bother moving it if the destination CPU is | |
1421 | * not running a lower priority task. | |
1422 | */ | |
1423 | if (target != -1 && | |
1424 | p->prio < cpu_rq(target)->rt.highest_prio.curr) | |
7608dec2 | 1425 | cpu = target; |
318e0893 | 1426 | } |
7608dec2 | 1427 | rcu_read_unlock(); |
318e0893 | 1428 | |
c37495fd | 1429 | out: |
7608dec2 | 1430 | return cpu; |
e7693a36 | 1431 | } |
7ebefa8c DA |
1432 | |
1433 | static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p) | |
1434 | { | |
308a623a WL |
1435 | /* |
1436 | * Current can't be migrated, useless to reschedule, | |
1437 | * let's hope p can move out. | |
1438 | */ | |
4b53a341 | 1439 | if (rq->curr->nr_cpus_allowed == 1 || |
308a623a | 1440 | !cpupri_find(&rq->rd->cpupri, rq->curr, NULL)) |
7ebefa8c DA |
1441 | return; |
1442 | ||
308a623a WL |
1443 | /* |
1444 | * p is migratable, so let's not schedule it and | |
1445 | * see if it is pushed or pulled somewhere else. | |
1446 | */ | |
4b53a341 | 1447 | if (p->nr_cpus_allowed != 1 |
13b8bd0a RR |
1448 | && cpupri_find(&rq->rd->cpupri, p, NULL)) |
1449 | return; | |
24600ce8 | 1450 | |
7ebefa8c | 1451 | /* |
97fb7a0a IM |
1452 | * There appear to be other CPUs that can accept |
1453 | * the current task but none can run 'p', so lets reschedule | |
1454 | * to try and push the current task away: | |
7ebefa8c DA |
1455 | */ |
1456 | requeue_task_rt(rq, p, 1); | |
8875125e | 1457 | resched_curr(rq); |
7ebefa8c DA |
1458 | } |
1459 | ||
e7693a36 GH |
1460 | #endif /* CONFIG_SMP */ |
1461 | ||
bb44e5d1 IM |
1462 | /* |
1463 | * Preempt the current task with a newly woken task if needed: | |
1464 | */ | |
7d478721 | 1465 | static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags) |
bb44e5d1 | 1466 | { |
45c01e82 | 1467 | if (p->prio < rq->curr->prio) { |
8875125e | 1468 | resched_curr(rq); |
45c01e82 GH |
1469 | return; |
1470 | } | |
1471 | ||
1472 | #ifdef CONFIG_SMP | |
1473 | /* | |
1474 | * If: | |
1475 | * | |
1476 | * - the newly woken task is of equal priority to the current task | |
1477 | * - the newly woken task is non-migratable while current is migratable | |
1478 | * - current will be preempted on the next reschedule | |
1479 | * | |
1480 | * we should check to see if current can readily move to a different | |
1481 | * cpu. If so, we will reschedule to allow the push logic to try | |
1482 | * to move current somewhere else, making room for our non-migratable | |
1483 | * task. | |
1484 | */ | |
8dd0de8b | 1485 | if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr)) |
7ebefa8c | 1486 | check_preempt_equal_prio(rq, p); |
45c01e82 | 1487 | #endif |
bb44e5d1 IM |
1488 | } |
1489 | ||
6f505b16 PZ |
1490 | static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq, |
1491 | struct rt_rq *rt_rq) | |
bb44e5d1 | 1492 | { |
6f505b16 PZ |
1493 | struct rt_prio_array *array = &rt_rq->active; |
1494 | struct sched_rt_entity *next = NULL; | |
bb44e5d1 IM |
1495 | struct list_head *queue; |
1496 | int idx; | |
1497 | ||
1498 | idx = sched_find_first_bit(array->bitmap); | |
6f505b16 | 1499 | BUG_ON(idx >= MAX_RT_PRIO); |
bb44e5d1 IM |
1500 | |
1501 | queue = array->queue + idx; | |
6f505b16 | 1502 | next = list_entry(queue->next, struct sched_rt_entity, run_list); |
326587b8 | 1503 | |
6f505b16 PZ |
1504 | return next; |
1505 | } | |
bb44e5d1 | 1506 | |
917b627d | 1507 | static struct task_struct *_pick_next_task_rt(struct rq *rq) |
6f505b16 PZ |
1508 | { |
1509 | struct sched_rt_entity *rt_se; | |
1510 | struct task_struct *p; | |
606dba2e | 1511 | struct rt_rq *rt_rq = &rq->rt; |
6f505b16 PZ |
1512 | |
1513 | do { | |
1514 | rt_se = pick_next_rt_entity(rq, rt_rq); | |
326587b8 | 1515 | BUG_ON(!rt_se); |
6f505b16 PZ |
1516 | rt_rq = group_rt_rq(rt_se); |
1517 | } while (rt_rq); | |
1518 | ||
1519 | p = rt_task_of(rt_se); | |
78becc27 | 1520 | p->se.exec_start = rq_clock_task(rq); |
917b627d GH |
1521 | |
1522 | return p; | |
1523 | } | |
1524 | ||
606dba2e | 1525 | static struct task_struct * |
d8ac8971 | 1526 | pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
917b627d | 1527 | { |
606dba2e PZ |
1528 | struct task_struct *p; |
1529 | struct rt_rq *rt_rq = &rq->rt; | |
1530 | ||
37e117c0 | 1531 | if (need_pull_rt_task(rq, prev)) { |
cbce1a68 PZ |
1532 | /* |
1533 | * This is OK, because current is on_cpu, which avoids it being | |
1534 | * picked for load-balance and preemption/IRQs are still | |
1535 | * disabled avoiding further scheduler activity on it and we're | |
1536 | * being very careful to re-start the picking loop. | |
1537 | */ | |
d8ac8971 | 1538 | rq_unpin_lock(rq, rf); |
38033c37 | 1539 | pull_rt_task(rq); |
d8ac8971 | 1540 | rq_repin_lock(rq, rf); |
37e117c0 PZ |
1541 | /* |
1542 | * pull_rt_task() can drop (and re-acquire) rq->lock; this | |
a1d9a323 KT |
1543 | * means a dl or stop task can slip in, in which case we need |
1544 | * to re-start task selection. | |
37e117c0 | 1545 | */ |
da0c1e65 | 1546 | if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) || |
a1d9a323 | 1547 | rq->dl.dl_nr_running)) |
37e117c0 PZ |
1548 | return RETRY_TASK; |
1549 | } | |
38033c37 | 1550 | |
734ff2a7 KT |
1551 | /* |
1552 | * We may dequeue prev's rt_rq in put_prev_task(). | |
1553 | * So, we update time before rt_nr_running check. | |
1554 | */ | |
1555 | if (prev->sched_class == &rt_sched_class) | |
1556 | update_curr_rt(rq); | |
1557 | ||
f4ebcbc0 | 1558 | if (!rt_rq->rt_queued) |
606dba2e PZ |
1559 | return NULL; |
1560 | ||
3f1d2a31 | 1561 | put_prev_task(rq, prev); |
606dba2e PZ |
1562 | |
1563 | p = _pick_next_task_rt(rq); | |
917b627d GH |
1564 | |
1565 | /* The running task is never eligible for pushing */ | |
f3f1768f | 1566 | dequeue_pushable_task(rq, p); |
917b627d | 1567 | |
e3fca9e7 | 1568 | queue_push_tasks(rq); |
3f029d3c | 1569 | |
6f505b16 | 1570 | return p; |
bb44e5d1 IM |
1571 | } |
1572 | ||
31ee529c | 1573 | static void put_prev_task_rt(struct rq *rq, struct task_struct *p) |
bb44e5d1 | 1574 | { |
f1e14ef6 | 1575 | update_curr_rt(rq); |
917b627d GH |
1576 | |
1577 | /* | |
1578 | * The previous task needs to be made eligible for pushing | |
1579 | * if it is still active | |
1580 | */ | |
4b53a341 | 1581 | if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1) |
917b627d | 1582 | enqueue_pushable_task(rq, p); |
bb44e5d1 IM |
1583 | } |
1584 | ||
681f3e68 | 1585 | #ifdef CONFIG_SMP |
6f505b16 | 1586 | |
e8fa1362 SR |
1587 | /* Only try algorithms three times */ |
1588 | #define RT_MAX_TRIES 3 | |
1589 | ||
f65eda4f SR |
1590 | static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu) |
1591 | { | |
1592 | if (!task_running(rq, p) && | |
0c98d344 | 1593 | cpumask_test_cpu(cpu, &p->cpus_allowed)) |
f65eda4f | 1594 | return 1; |
97fb7a0a | 1595 | |
f65eda4f SR |
1596 | return 0; |
1597 | } | |
1598 | ||
e23ee747 KT |
1599 | /* |
1600 | * Return the highest pushable rq's task, which is suitable to be executed | |
97fb7a0a | 1601 | * on the CPU, NULL otherwise |
e23ee747 KT |
1602 | */ |
1603 | static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu) | |
e8fa1362 | 1604 | { |
e23ee747 KT |
1605 | struct plist_head *head = &rq->rt.pushable_tasks; |
1606 | struct task_struct *p; | |
3d07467b | 1607 | |
e23ee747 KT |
1608 | if (!has_pushable_tasks(rq)) |
1609 | return NULL; | |
3d07467b | 1610 | |
e23ee747 KT |
1611 | plist_for_each_entry(p, head, pushable_tasks) { |
1612 | if (pick_rt_task(rq, p, cpu)) | |
1613 | return p; | |
f65eda4f SR |
1614 | } |
1615 | ||
e23ee747 | 1616 | return NULL; |
e8fa1362 SR |
1617 | } |
1618 | ||
0e3900e6 | 1619 | static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask); |
e8fa1362 | 1620 | |
6e1254d2 GH |
1621 | static int find_lowest_rq(struct task_struct *task) |
1622 | { | |
1623 | struct sched_domain *sd; | |
4ba29684 | 1624 | struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask); |
6e1254d2 GH |
1625 | int this_cpu = smp_processor_id(); |
1626 | int cpu = task_cpu(task); | |
06f90dbd | 1627 | |
0da938c4 SR |
1628 | /* Make sure the mask is initialized first */ |
1629 | if (unlikely(!lowest_mask)) | |
1630 | return -1; | |
1631 | ||
4b53a341 | 1632 | if (task->nr_cpus_allowed == 1) |
6e0534f2 | 1633 | return -1; /* No other targets possible */ |
6e1254d2 | 1634 | |
6e0534f2 GH |
1635 | if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask)) |
1636 | return -1; /* No targets found */ | |
6e1254d2 GH |
1637 | |
1638 | /* | |
97fb7a0a | 1639 | * At this point we have built a mask of CPUs representing the |
6e1254d2 GH |
1640 | * lowest priority tasks in the system. Now we want to elect |
1641 | * the best one based on our affinity and topology. | |
1642 | * | |
97fb7a0a | 1643 | * We prioritize the last CPU that the task executed on since |
6e1254d2 GH |
1644 | * it is most likely cache-hot in that location. |
1645 | */ | |
96f874e2 | 1646 | if (cpumask_test_cpu(cpu, lowest_mask)) |
6e1254d2 GH |
1647 | return cpu; |
1648 | ||
1649 | /* | |
1650 | * Otherwise, we consult the sched_domains span maps to figure | |
97fb7a0a | 1651 | * out which CPU is logically closest to our hot cache data. |
6e1254d2 | 1652 | */ |
e2c88063 RR |
1653 | if (!cpumask_test_cpu(this_cpu, lowest_mask)) |
1654 | this_cpu = -1; /* Skip this_cpu opt if not among lowest */ | |
6e1254d2 | 1655 | |
cd4ae6ad | 1656 | rcu_read_lock(); |
e2c88063 RR |
1657 | for_each_domain(cpu, sd) { |
1658 | if (sd->flags & SD_WAKE_AFFINE) { | |
1659 | int best_cpu; | |
6e1254d2 | 1660 | |
e2c88063 RR |
1661 | /* |
1662 | * "this_cpu" is cheaper to preempt than a | |
1663 | * remote processor. | |
1664 | */ | |
1665 | if (this_cpu != -1 && | |
cd4ae6ad XF |
1666 | cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { |
1667 | rcu_read_unlock(); | |
e2c88063 | 1668 | return this_cpu; |
cd4ae6ad | 1669 | } |
e2c88063 RR |
1670 | |
1671 | best_cpu = cpumask_first_and(lowest_mask, | |
1672 | sched_domain_span(sd)); | |
cd4ae6ad XF |
1673 | if (best_cpu < nr_cpu_ids) { |
1674 | rcu_read_unlock(); | |
e2c88063 | 1675 | return best_cpu; |
cd4ae6ad | 1676 | } |
6e1254d2 GH |
1677 | } |
1678 | } | |
cd4ae6ad | 1679 | rcu_read_unlock(); |
6e1254d2 GH |
1680 | |
1681 | /* | |
1682 | * And finally, if there were no matches within the domains | |
1683 | * just give the caller *something* to work with from the compatible | |
1684 | * locations. | |
1685 | */ | |
e2c88063 RR |
1686 | if (this_cpu != -1) |
1687 | return this_cpu; | |
1688 | ||
1689 | cpu = cpumask_any(lowest_mask); | |
1690 | if (cpu < nr_cpu_ids) | |
1691 | return cpu; | |
97fb7a0a | 1692 | |
e2c88063 | 1693 | return -1; |
07b4032c GH |
1694 | } |
1695 | ||
1696 | /* Will lock the rq it finds */ | |
4df64c0b | 1697 | static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) |
07b4032c GH |
1698 | { |
1699 | struct rq *lowest_rq = NULL; | |
07b4032c | 1700 | int tries; |
4df64c0b | 1701 | int cpu; |
e8fa1362 | 1702 | |
07b4032c GH |
1703 | for (tries = 0; tries < RT_MAX_TRIES; tries++) { |
1704 | cpu = find_lowest_rq(task); | |
1705 | ||
2de0b463 | 1706 | if ((cpu == -1) || (cpu == rq->cpu)) |
e8fa1362 SR |
1707 | break; |
1708 | ||
07b4032c GH |
1709 | lowest_rq = cpu_rq(cpu); |
1710 | ||
80e3d87b TC |
1711 | if (lowest_rq->rt.highest_prio.curr <= task->prio) { |
1712 | /* | |
1713 | * Target rq has tasks of equal or higher priority, | |
1714 | * retrying does not release any lock and is unlikely | |
1715 | * to yield a different result. | |
1716 | */ | |
1717 | lowest_rq = NULL; | |
1718 | break; | |
1719 | } | |
1720 | ||
e8fa1362 | 1721 | /* if the prio of this runqueue changed, try again */ |
07b4032c | 1722 | if (double_lock_balance(rq, lowest_rq)) { |
e8fa1362 SR |
1723 | /* |
1724 | * We had to unlock the run queue. In | |
1725 | * the mean time, task could have | |
1726 | * migrated already or had its affinity changed. | |
1727 | * Also make sure that it wasn't scheduled on its rq. | |
1728 | */ | |
07b4032c | 1729 | if (unlikely(task_rq(task) != rq || |
0c98d344 | 1730 | !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed) || |
07b4032c | 1731 | task_running(rq, task) || |
13b5ab02 | 1732 | !rt_task(task) || |
da0c1e65 | 1733 | !task_on_rq_queued(task))) { |
4df64c0b | 1734 | |
7f1b4393 | 1735 | double_unlock_balance(rq, lowest_rq); |
e8fa1362 SR |
1736 | lowest_rq = NULL; |
1737 | break; | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | /* If this rq is still suitable use it. */ | |
e864c499 | 1742 | if (lowest_rq->rt.highest_prio.curr > task->prio) |
e8fa1362 SR |
1743 | break; |
1744 | ||
1745 | /* try again */ | |
1b12bbc7 | 1746 | double_unlock_balance(rq, lowest_rq); |
e8fa1362 SR |
1747 | lowest_rq = NULL; |
1748 | } | |
1749 | ||
1750 | return lowest_rq; | |
1751 | } | |
1752 | ||
917b627d GH |
1753 | static struct task_struct *pick_next_pushable_task(struct rq *rq) |
1754 | { | |
1755 | struct task_struct *p; | |
1756 | ||
1757 | if (!has_pushable_tasks(rq)) | |
1758 | return NULL; | |
1759 | ||
1760 | p = plist_first_entry(&rq->rt.pushable_tasks, | |
1761 | struct task_struct, pushable_tasks); | |
1762 | ||
1763 | BUG_ON(rq->cpu != task_cpu(p)); | |
1764 | BUG_ON(task_current(rq, p)); | |
4b53a341 | 1765 | BUG_ON(p->nr_cpus_allowed <= 1); |
917b627d | 1766 | |
da0c1e65 | 1767 | BUG_ON(!task_on_rq_queued(p)); |
917b627d GH |
1768 | BUG_ON(!rt_task(p)); |
1769 | ||
1770 | return p; | |
1771 | } | |
1772 | ||
e8fa1362 SR |
1773 | /* |
1774 | * If the current CPU has more than one RT task, see if the non | |
1775 | * running task can migrate over to a CPU that is running a task | |
1776 | * of lesser priority. | |
1777 | */ | |
697f0a48 | 1778 | static int push_rt_task(struct rq *rq) |
e8fa1362 SR |
1779 | { |
1780 | struct task_struct *next_task; | |
1781 | struct rq *lowest_rq; | |
311e800e | 1782 | int ret = 0; |
e8fa1362 | 1783 | |
a22d7fc1 GH |
1784 | if (!rq->rt.overloaded) |
1785 | return 0; | |
1786 | ||
917b627d | 1787 | next_task = pick_next_pushable_task(rq); |
e8fa1362 SR |
1788 | if (!next_task) |
1789 | return 0; | |
1790 | ||
49246274 | 1791 | retry: |
697f0a48 | 1792 | if (unlikely(next_task == rq->curr)) { |
f65eda4f | 1793 | WARN_ON(1); |
e8fa1362 | 1794 | return 0; |
f65eda4f | 1795 | } |
e8fa1362 SR |
1796 | |
1797 | /* | |
1798 | * It's possible that the next_task slipped in of | |
1799 | * higher priority than current. If that's the case | |
1800 | * just reschedule current. | |
1801 | */ | |
697f0a48 | 1802 | if (unlikely(next_task->prio < rq->curr->prio)) { |
8875125e | 1803 | resched_curr(rq); |
e8fa1362 SR |
1804 | return 0; |
1805 | } | |
1806 | ||
697f0a48 | 1807 | /* We might release rq lock */ |
e8fa1362 SR |
1808 | get_task_struct(next_task); |
1809 | ||
1810 | /* find_lock_lowest_rq locks the rq if found */ | |
697f0a48 | 1811 | lowest_rq = find_lock_lowest_rq(next_task, rq); |
e8fa1362 SR |
1812 | if (!lowest_rq) { |
1813 | struct task_struct *task; | |
1814 | /* | |
311e800e | 1815 | * find_lock_lowest_rq releases rq->lock |
1563513d GH |
1816 | * so it is possible that next_task has migrated. |
1817 | * | |
1818 | * We need to make sure that the task is still on the same | |
1819 | * run-queue and is also still the next task eligible for | |
1820 | * pushing. | |
e8fa1362 | 1821 | */ |
917b627d | 1822 | task = pick_next_pushable_task(rq); |
de16b91e | 1823 | if (task == next_task) { |
1563513d | 1824 | /* |
311e800e HD |
1825 | * The task hasn't migrated, and is still the next |
1826 | * eligible task, but we failed to find a run-queue | |
1827 | * to push it to. Do not retry in this case, since | |
97fb7a0a | 1828 | * other CPUs will pull from us when ready. |
1563513d | 1829 | */ |
1563513d | 1830 | goto out; |
e8fa1362 | 1831 | } |
917b627d | 1832 | |
1563513d GH |
1833 | if (!task) |
1834 | /* No more tasks, just exit */ | |
1835 | goto out; | |
1836 | ||
917b627d | 1837 | /* |
1563513d | 1838 | * Something has shifted, try again. |
917b627d | 1839 | */ |
1563513d GH |
1840 | put_task_struct(next_task); |
1841 | next_task = task; | |
1842 | goto retry; | |
e8fa1362 SR |
1843 | } |
1844 | ||
697f0a48 | 1845 | deactivate_task(rq, next_task, 0); |
e8fa1362 SR |
1846 | set_task_cpu(next_task, lowest_rq->cpu); |
1847 | activate_task(lowest_rq, next_task, 0); | |
311e800e | 1848 | ret = 1; |
e8fa1362 | 1849 | |
8875125e | 1850 | resched_curr(lowest_rq); |
e8fa1362 | 1851 | |
1b12bbc7 | 1852 | double_unlock_balance(rq, lowest_rq); |
e8fa1362 | 1853 | |
e8fa1362 SR |
1854 | out: |
1855 | put_task_struct(next_task); | |
1856 | ||
311e800e | 1857 | return ret; |
e8fa1362 SR |
1858 | } |
1859 | ||
e8fa1362 SR |
1860 | static void push_rt_tasks(struct rq *rq) |
1861 | { | |
1862 | /* push_rt_task will return true if it moved an RT */ | |
1863 | while (push_rt_task(rq)) | |
1864 | ; | |
1865 | } | |
1866 | ||
b6366f04 | 1867 | #ifdef HAVE_RT_PUSH_IPI |
4bdced5c | 1868 | |
b6366f04 | 1869 | /* |
4bdced5c SRRH |
1870 | * When a high priority task schedules out from a CPU and a lower priority |
1871 | * task is scheduled in, a check is made to see if there's any RT tasks | |
1872 | * on other CPUs that are waiting to run because a higher priority RT task | |
1873 | * is currently running on its CPU. In this case, the CPU with multiple RT | |
1874 | * tasks queued on it (overloaded) needs to be notified that a CPU has opened | |
1875 | * up that may be able to run one of its non-running queued RT tasks. | |
1876 | * | |
1877 | * All CPUs with overloaded RT tasks need to be notified as there is currently | |
1878 | * no way to know which of these CPUs have the highest priority task waiting | |
1879 | * to run. Instead of trying to take a spinlock on each of these CPUs, | |
1880 | * which has shown to cause large latency when done on machines with many | |
1881 | * CPUs, sending an IPI to the CPUs to have them push off the overloaded | |
1882 | * RT tasks waiting to run. | |
1883 | * | |
1884 | * Just sending an IPI to each of the CPUs is also an issue, as on large | |
1885 | * count CPU machines, this can cause an IPI storm on a CPU, especially | |
1886 | * if its the only CPU with multiple RT tasks queued, and a large number | |
1887 | * of CPUs scheduling a lower priority task at the same time. | |
1888 | * | |
1889 | * Each root domain has its own irq work function that can iterate over | |
1890 | * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT | |
1891 | * tassk must be checked if there's one or many CPUs that are lowering | |
1892 | * their priority, there's a single irq work iterator that will try to | |
1893 | * push off RT tasks that are waiting to run. | |
1894 | * | |
1895 | * When a CPU schedules a lower priority task, it will kick off the | |
1896 | * irq work iterator that will jump to each CPU with overloaded RT tasks. | |
1897 | * As it only takes the first CPU that schedules a lower priority task | |
1898 | * to start the process, the rto_start variable is incremented and if | |
1899 | * the atomic result is one, then that CPU will try to take the rto_lock. | |
1900 | * This prevents high contention on the lock as the process handles all | |
1901 | * CPUs scheduling lower priority tasks. | |
1902 | * | |
1903 | * All CPUs that are scheduling a lower priority task will increment the | |
1904 | * rt_loop_next variable. This will make sure that the irq work iterator | |
1905 | * checks all RT overloaded CPUs whenever a CPU schedules a new lower | |
1906 | * priority task, even if the iterator is in the middle of a scan. Incrementing | |
1907 | * the rt_loop_next will cause the iterator to perform another scan. | |
b6366f04 | 1908 | * |
b6366f04 | 1909 | */ |
ad0f1d9d | 1910 | static int rto_next_cpu(struct root_domain *rd) |
b6366f04 | 1911 | { |
4bdced5c | 1912 | int next; |
b6366f04 SR |
1913 | int cpu; |
1914 | ||
b6366f04 | 1915 | /* |
4bdced5c SRRH |
1916 | * When starting the IPI RT pushing, the rto_cpu is set to -1, |
1917 | * rt_next_cpu() will simply return the first CPU found in | |
1918 | * the rto_mask. | |
1919 | * | |
97fb7a0a | 1920 | * If rto_next_cpu() is called with rto_cpu is a valid CPU, it |
4bdced5c SRRH |
1921 | * will return the next CPU found in the rto_mask. |
1922 | * | |
1923 | * If there are no more CPUs left in the rto_mask, then a check is made | |
1924 | * against rto_loop and rto_loop_next. rto_loop is only updated with | |
1925 | * the rto_lock held, but any CPU may increment the rto_loop_next | |
1926 | * without any locking. | |
b6366f04 | 1927 | */ |
4bdced5c | 1928 | for (;;) { |
b6366f04 | 1929 | |
4bdced5c SRRH |
1930 | /* When rto_cpu is -1 this acts like cpumask_first() */ |
1931 | cpu = cpumask_next(rd->rto_cpu, rd->rto_mask); | |
b6366f04 | 1932 | |
4bdced5c | 1933 | rd->rto_cpu = cpu; |
b6366f04 | 1934 | |
4bdced5c SRRH |
1935 | if (cpu < nr_cpu_ids) |
1936 | return cpu; | |
b6366f04 | 1937 | |
4bdced5c SRRH |
1938 | rd->rto_cpu = -1; |
1939 | ||
1940 | /* | |
1941 | * ACQUIRE ensures we see the @rto_mask changes | |
1942 | * made prior to the @next value observed. | |
1943 | * | |
1944 | * Matches WMB in rt_set_overload(). | |
1945 | */ | |
1946 | next = atomic_read_acquire(&rd->rto_loop_next); | |
b6366f04 | 1947 | |
4bdced5c | 1948 | if (rd->rto_loop == next) |
b6366f04 | 1949 | break; |
4bdced5c SRRH |
1950 | |
1951 | rd->rto_loop = next; | |
b6366f04 SR |
1952 | } |
1953 | ||
4bdced5c | 1954 | return -1; |
b6366f04 SR |
1955 | } |
1956 | ||
4bdced5c SRRH |
1957 | static inline bool rto_start_trylock(atomic_t *v) |
1958 | { | |
1959 | return !atomic_cmpxchg_acquire(v, 0, 1); | |
1960 | } | |
b6366f04 | 1961 | |
4bdced5c | 1962 | static inline void rto_start_unlock(atomic_t *v) |
b6366f04 | 1963 | { |
4bdced5c SRRH |
1964 | atomic_set_release(v, 0); |
1965 | } | |
b6366f04 | 1966 | |
4bdced5c SRRH |
1967 | static void tell_cpu_to_push(struct rq *rq) |
1968 | { | |
1969 | int cpu = -1; | |
b6366f04 | 1970 | |
4bdced5c SRRH |
1971 | /* Keep the loop going if the IPI is currently active */ |
1972 | atomic_inc(&rq->rd->rto_loop_next); | |
b6366f04 | 1973 | |
4bdced5c SRRH |
1974 | /* Only one CPU can initiate a loop at a time */ |
1975 | if (!rto_start_trylock(&rq->rd->rto_loop_start)) | |
b6366f04 SR |
1976 | return; |
1977 | ||
4bdced5c | 1978 | raw_spin_lock(&rq->rd->rto_lock); |
b6366f04 | 1979 | |
4bdced5c | 1980 | /* |
97fb7a0a | 1981 | * The rto_cpu is updated under the lock, if it has a valid CPU |
4bdced5c SRRH |
1982 | * then the IPI is still running and will continue due to the |
1983 | * update to loop_next, and nothing needs to be done here. | |
1984 | * Otherwise it is finishing up and an ipi needs to be sent. | |
1985 | */ | |
1986 | if (rq->rd->rto_cpu < 0) | |
ad0f1d9d | 1987 | cpu = rto_next_cpu(rq->rd); |
4bdced5c SRRH |
1988 | |
1989 | raw_spin_unlock(&rq->rd->rto_lock); | |
1990 | ||
1991 | rto_start_unlock(&rq->rd->rto_loop_start); | |
1992 | ||
364f5665 SRV |
1993 | if (cpu >= 0) { |
1994 | /* Make sure the rd does not get freed while pushing */ | |
1995 | sched_get_rd(rq->rd); | |
4bdced5c | 1996 | irq_work_queue_on(&rq->rd->rto_push_work, cpu); |
364f5665 | 1997 | } |
b6366f04 SR |
1998 | } |
1999 | ||
2000 | /* Called from hardirq context */ | |
4bdced5c | 2001 | void rto_push_irq_work_func(struct irq_work *work) |
b6366f04 | 2002 | { |
ad0f1d9d SRV |
2003 | struct root_domain *rd = |
2004 | container_of(work, struct root_domain, rto_push_work); | |
4bdced5c | 2005 | struct rq *rq; |
b6366f04 SR |
2006 | int cpu; |
2007 | ||
4bdced5c | 2008 | rq = this_rq(); |
b6366f04 | 2009 | |
4bdced5c SRRH |
2010 | /* |
2011 | * We do not need to grab the lock to check for has_pushable_tasks. | |
2012 | * When it gets updated, a check is made if a push is possible. | |
2013 | */ | |
b6366f04 SR |
2014 | if (has_pushable_tasks(rq)) { |
2015 | raw_spin_lock(&rq->lock); | |
4bdced5c | 2016 | push_rt_tasks(rq); |
b6366f04 SR |
2017 | raw_spin_unlock(&rq->lock); |
2018 | } | |
2019 | ||
ad0f1d9d | 2020 | raw_spin_lock(&rd->rto_lock); |
b6366f04 | 2021 | |
4bdced5c | 2022 | /* Pass the IPI to the next rt overloaded queue */ |
ad0f1d9d | 2023 | cpu = rto_next_cpu(rd); |
b6366f04 | 2024 | |
ad0f1d9d | 2025 | raw_spin_unlock(&rd->rto_lock); |
b6366f04 | 2026 | |
364f5665 SRV |
2027 | if (cpu < 0) { |
2028 | sched_put_rd(rd); | |
b6366f04 | 2029 | return; |
364f5665 | 2030 | } |
b6366f04 | 2031 | |
b6366f04 | 2032 | /* Try the next RT overloaded CPU */ |
ad0f1d9d | 2033 | irq_work_queue_on(&rd->rto_push_work, cpu); |
b6366f04 SR |
2034 | } |
2035 | #endif /* HAVE_RT_PUSH_IPI */ | |
2036 | ||
8046d680 | 2037 | static void pull_rt_task(struct rq *this_rq) |
f65eda4f | 2038 | { |
8046d680 PZ |
2039 | int this_cpu = this_rq->cpu, cpu; |
2040 | bool resched = false; | |
a8728944 | 2041 | struct task_struct *p; |
f65eda4f | 2042 | struct rq *src_rq; |
f73c52a5 | 2043 | int rt_overload_count = rt_overloaded(this_rq); |
f65eda4f | 2044 | |
f73c52a5 | 2045 | if (likely(!rt_overload_count)) |
8046d680 | 2046 | return; |
f65eda4f | 2047 | |
7c3f2ab7 PZ |
2048 | /* |
2049 | * Match the barrier from rt_set_overloaded; this guarantees that if we | |
2050 | * see overloaded we must also see the rto_mask bit. | |
2051 | */ | |
2052 | smp_rmb(); | |
2053 | ||
f73c52a5 SR |
2054 | /* If we are the only overloaded CPU do nothing */ |
2055 | if (rt_overload_count == 1 && | |
2056 | cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask)) | |
2057 | return; | |
2058 | ||
b6366f04 SR |
2059 | #ifdef HAVE_RT_PUSH_IPI |
2060 | if (sched_feat(RT_PUSH_IPI)) { | |
2061 | tell_cpu_to_push(this_rq); | |
8046d680 | 2062 | return; |
b6366f04 SR |
2063 | } |
2064 | #endif | |
2065 | ||
c6c4927b | 2066 | for_each_cpu(cpu, this_rq->rd->rto_mask) { |
f65eda4f SR |
2067 | if (this_cpu == cpu) |
2068 | continue; | |
2069 | ||
2070 | src_rq = cpu_rq(cpu); | |
74ab8e4f GH |
2071 | |
2072 | /* | |
2073 | * Don't bother taking the src_rq->lock if the next highest | |
2074 | * task is known to be lower-priority than our current task. | |
2075 | * This may look racy, but if this value is about to go | |
2076 | * logically higher, the src_rq will push this task away. | |
2077 | * And if its going logically lower, we do not care | |
2078 | */ | |
2079 | if (src_rq->rt.highest_prio.next >= | |
2080 | this_rq->rt.highest_prio.curr) | |
2081 | continue; | |
2082 | ||
f65eda4f SR |
2083 | /* |
2084 | * We can potentially drop this_rq's lock in | |
2085 | * double_lock_balance, and another CPU could | |
a8728944 | 2086 | * alter this_rq |
f65eda4f | 2087 | */ |
a8728944 | 2088 | double_lock_balance(this_rq, src_rq); |
f65eda4f SR |
2089 | |
2090 | /* | |
e23ee747 KT |
2091 | * We can pull only a task, which is pushable |
2092 | * on its rq, and no others. | |
f65eda4f | 2093 | */ |
e23ee747 | 2094 | p = pick_highest_pushable_task(src_rq, this_cpu); |
f65eda4f SR |
2095 | |
2096 | /* | |
2097 | * Do we have an RT task that preempts | |
2098 | * the to-be-scheduled task? | |
2099 | */ | |
a8728944 | 2100 | if (p && (p->prio < this_rq->rt.highest_prio.curr)) { |
f65eda4f | 2101 | WARN_ON(p == src_rq->curr); |
da0c1e65 | 2102 | WARN_ON(!task_on_rq_queued(p)); |
f65eda4f SR |
2103 | |
2104 | /* | |
2105 | * There's a chance that p is higher in priority | |
97fb7a0a | 2106 | * than what's currently running on its CPU. |
f65eda4f SR |
2107 | * This is just that p is wakeing up and hasn't |
2108 | * had a chance to schedule. We only pull | |
2109 | * p if it is lower in priority than the | |
a8728944 | 2110 | * current task on the run queue |
f65eda4f | 2111 | */ |
a8728944 | 2112 | if (p->prio < src_rq->curr->prio) |
614ee1f6 | 2113 | goto skip; |
f65eda4f | 2114 | |
8046d680 | 2115 | resched = true; |
f65eda4f SR |
2116 | |
2117 | deactivate_task(src_rq, p, 0); | |
2118 | set_task_cpu(p, this_cpu); | |
2119 | activate_task(this_rq, p, 0); | |
2120 | /* | |
2121 | * We continue with the search, just in | |
2122 | * case there's an even higher prio task | |
25985edc | 2123 | * in another runqueue. (low likelihood |
f65eda4f | 2124 | * but possible) |
f65eda4f | 2125 | */ |
f65eda4f | 2126 | } |
49246274 | 2127 | skip: |
1b12bbc7 | 2128 | double_unlock_balance(this_rq, src_rq); |
f65eda4f SR |
2129 | } |
2130 | ||
8046d680 PZ |
2131 | if (resched) |
2132 | resched_curr(this_rq); | |
f65eda4f SR |
2133 | } |
2134 | ||
8ae121ac GH |
2135 | /* |
2136 | * If we are not running and we are not going to reschedule soon, we should | |
2137 | * try to push tasks away now | |
2138 | */ | |
efbbd05a | 2139 | static void task_woken_rt(struct rq *rq, struct task_struct *p) |
4642dafd | 2140 | { |
9a897c5a | 2141 | if (!task_running(rq, p) && |
8ae121ac | 2142 | !test_tsk_need_resched(rq->curr) && |
4b53a341 | 2143 | p->nr_cpus_allowed > 1 && |
1baca4ce | 2144 | (dl_task(rq->curr) || rt_task(rq->curr)) && |
4b53a341 | 2145 | (rq->curr->nr_cpus_allowed < 2 || |
3be209a8 | 2146 | rq->curr->prio <= p->prio)) |
4642dafd SR |
2147 | push_rt_tasks(rq); |
2148 | } | |
2149 | ||
bdd7c81b | 2150 | /* Assumes rq->lock is held */ |
1f11eb6a | 2151 | static void rq_online_rt(struct rq *rq) |
bdd7c81b IM |
2152 | { |
2153 | if (rq->rt.overloaded) | |
2154 | rt_set_overload(rq); | |
6e0534f2 | 2155 | |
7def2be1 PZ |
2156 | __enable_runtime(rq); |
2157 | ||
e864c499 | 2158 | cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr); |
bdd7c81b IM |
2159 | } |
2160 | ||
2161 | /* Assumes rq->lock is held */ | |
1f11eb6a | 2162 | static void rq_offline_rt(struct rq *rq) |
bdd7c81b IM |
2163 | { |
2164 | if (rq->rt.overloaded) | |
2165 | rt_clear_overload(rq); | |
6e0534f2 | 2166 | |
7def2be1 PZ |
2167 | __disable_runtime(rq); |
2168 | ||
6e0534f2 | 2169 | cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID); |
bdd7c81b | 2170 | } |
cb469845 SR |
2171 | |
2172 | /* | |
2173 | * When switch from the rt queue, we bring ourselves to a position | |
2174 | * that we might want to pull RT tasks from other runqueues. | |
2175 | */ | |
da7a735e | 2176 | static void switched_from_rt(struct rq *rq, struct task_struct *p) |
cb469845 SR |
2177 | { |
2178 | /* | |
2179 | * If there are other RT tasks then we will reschedule | |
2180 | * and the scheduling of the other RT tasks will handle | |
2181 | * the balancing. But if we are the last RT task | |
2182 | * we may need to handle the pulling of RT tasks | |
2183 | * now. | |
2184 | */ | |
da0c1e65 | 2185 | if (!task_on_rq_queued(p) || rq->rt.rt_nr_running) |
1158ddb5 KT |
2186 | return; |
2187 | ||
fd7a4bed | 2188 | queue_pull_task(rq); |
cb469845 | 2189 | } |
3d8cbdf8 | 2190 | |
11c785b7 | 2191 | void __init init_sched_rt_class(void) |
3d8cbdf8 RR |
2192 | { |
2193 | unsigned int i; | |
2194 | ||
029632fb | 2195 | for_each_possible_cpu(i) { |
eaa95840 | 2196 | zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i), |
6ca09dfc | 2197 | GFP_KERNEL, cpu_to_node(i)); |
029632fb | 2198 | } |
3d8cbdf8 | 2199 | } |
cb469845 SR |
2200 | #endif /* CONFIG_SMP */ |
2201 | ||
2202 | /* | |
2203 | * When switching a task to RT, we may overload the runqueue | |
2204 | * with RT tasks. In this case we try to push them off to | |
2205 | * other runqueues. | |
2206 | */ | |
da7a735e | 2207 | static void switched_to_rt(struct rq *rq, struct task_struct *p) |
cb469845 | 2208 | { |
cb469845 SR |
2209 | /* |
2210 | * If we are already running, then there's nothing | |
2211 | * that needs to be done. But if we are not running | |
2212 | * we may need to preempt the current running task. | |
2213 | * If that current running task is also an RT task | |
2214 | * then see if we can move to another run queue. | |
2215 | */ | |
da0c1e65 | 2216 | if (task_on_rq_queued(p) && rq->curr != p) { |
cb469845 | 2217 | #ifdef CONFIG_SMP |
4b53a341 | 2218 | if (p->nr_cpus_allowed > 1 && rq->rt.overloaded) |
fd7a4bed | 2219 | queue_push_tasks(rq); |
619bd4a7 | 2220 | #endif /* CONFIG_SMP */ |
2fe25826 | 2221 | if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq))) |
8875125e | 2222 | resched_curr(rq); |
cb469845 SR |
2223 | } |
2224 | } | |
2225 | ||
2226 | /* | |
2227 | * Priority of the task has changed. This may cause | |
2228 | * us to initiate a push or pull. | |
2229 | */ | |
da7a735e PZ |
2230 | static void |
2231 | prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 2232 | { |
da0c1e65 | 2233 | if (!task_on_rq_queued(p)) |
da7a735e PZ |
2234 | return; |
2235 | ||
2236 | if (rq->curr == p) { | |
cb469845 SR |
2237 | #ifdef CONFIG_SMP |
2238 | /* | |
2239 | * If our priority decreases while running, we | |
2240 | * may need to pull tasks to this runqueue. | |
2241 | */ | |
2242 | if (oldprio < p->prio) | |
fd7a4bed PZ |
2243 | queue_pull_task(rq); |
2244 | ||
cb469845 SR |
2245 | /* |
2246 | * If there's a higher priority task waiting to run | |
fd7a4bed | 2247 | * then reschedule. |
cb469845 | 2248 | */ |
fd7a4bed | 2249 | if (p->prio > rq->rt.highest_prio.curr) |
8875125e | 2250 | resched_curr(rq); |
cb469845 SR |
2251 | #else |
2252 | /* For UP simply resched on drop of prio */ | |
2253 | if (oldprio < p->prio) | |
8875125e | 2254 | resched_curr(rq); |
e8fa1362 | 2255 | #endif /* CONFIG_SMP */ |
cb469845 SR |
2256 | } else { |
2257 | /* | |
2258 | * This task is not running, but if it is | |
2259 | * greater than the current running task | |
2260 | * then reschedule. | |
2261 | */ | |
2262 | if (p->prio < rq->curr->prio) | |
8875125e | 2263 | resched_curr(rq); |
cb469845 SR |
2264 | } |
2265 | } | |
2266 | ||
b18b6a9c | 2267 | #ifdef CONFIG_POSIX_TIMERS |
78f2c7db PZ |
2268 | static void watchdog(struct rq *rq, struct task_struct *p) |
2269 | { | |
2270 | unsigned long soft, hard; | |
2271 | ||
78d7d407 JS |
2272 | /* max may change after cur was read, this will be fixed next tick */ |
2273 | soft = task_rlimit(p, RLIMIT_RTTIME); | |
2274 | hard = task_rlimit_max(p, RLIMIT_RTTIME); | |
78f2c7db PZ |
2275 | |
2276 | if (soft != RLIM_INFINITY) { | |
2277 | unsigned long next; | |
2278 | ||
57d2aa00 YX |
2279 | if (p->rt.watchdog_stamp != jiffies) { |
2280 | p->rt.timeout++; | |
2281 | p->rt.watchdog_stamp = jiffies; | |
2282 | } | |
2283 | ||
78f2c7db | 2284 | next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ); |
5a52dd50 | 2285 | if (p->rt.timeout > next) |
f06febc9 | 2286 | p->cputime_expires.sched_exp = p->se.sum_exec_runtime; |
78f2c7db PZ |
2287 | } |
2288 | } | |
b18b6a9c NP |
2289 | #else |
2290 | static inline void watchdog(struct rq *rq, struct task_struct *p) { } | |
2291 | #endif | |
bb44e5d1 | 2292 | |
d84b3131 FW |
2293 | /* |
2294 | * scheduler tick hitting a task of our scheduling class. | |
2295 | * | |
2296 | * NOTE: This function can be called remotely by the tick offload that | |
2297 | * goes along full dynticks. Therefore no local assumption can be made | |
2298 | * and everything must be accessed through the @rq and @curr passed in | |
2299 | * parameters. | |
2300 | */ | |
8f4d37ec | 2301 | static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued) |
bb44e5d1 | 2302 | { |
454c7999 CC |
2303 | struct sched_rt_entity *rt_se = &p->rt; |
2304 | ||
67e2be02 PZ |
2305 | update_curr_rt(rq); |
2306 | ||
78f2c7db PZ |
2307 | watchdog(rq, p); |
2308 | ||
bb44e5d1 IM |
2309 | /* |
2310 | * RR tasks need a special form of timeslice management. | |
2311 | * FIFO tasks have no timeslices. | |
2312 | */ | |
2313 | if (p->policy != SCHED_RR) | |
2314 | return; | |
2315 | ||
fa717060 | 2316 | if (--p->rt.time_slice) |
bb44e5d1 IM |
2317 | return; |
2318 | ||
ce0dbbbb | 2319 | p->rt.time_slice = sched_rr_timeslice; |
bb44e5d1 | 2320 | |
98fbc798 | 2321 | /* |
e9aa39bb LB |
2322 | * Requeue to the end of queue if we (and all of our ancestors) are not |
2323 | * the only element on the queue | |
98fbc798 | 2324 | */ |
454c7999 CC |
2325 | for_each_sched_rt_entity(rt_se) { |
2326 | if (rt_se->run_list.prev != rt_se->run_list.next) { | |
2327 | requeue_task_rt(rq, p, 0); | |
8aa6f0eb | 2328 | resched_curr(rq); |
454c7999 CC |
2329 | return; |
2330 | } | |
98fbc798 | 2331 | } |
bb44e5d1 IM |
2332 | } |
2333 | ||
83b699ed SV |
2334 | static void set_curr_task_rt(struct rq *rq) |
2335 | { | |
2336 | struct task_struct *p = rq->curr; | |
2337 | ||
78becc27 | 2338 | p->se.exec_start = rq_clock_task(rq); |
917b627d GH |
2339 | |
2340 | /* The running task is never eligible for pushing */ | |
2341 | dequeue_pushable_task(rq, p); | |
83b699ed SV |
2342 | } |
2343 | ||
6d686f45 | 2344 | static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task) |
0d721cea PW |
2345 | { |
2346 | /* | |
2347 | * Time slice is 0 for SCHED_FIFO tasks | |
2348 | */ | |
2349 | if (task->policy == SCHED_RR) | |
ce0dbbbb | 2350 | return sched_rr_timeslice; |
0d721cea PW |
2351 | else |
2352 | return 0; | |
2353 | } | |
2354 | ||
029632fb | 2355 | const struct sched_class rt_sched_class = { |
5522d5d5 | 2356 | .next = &fair_sched_class, |
bb44e5d1 IM |
2357 | .enqueue_task = enqueue_task_rt, |
2358 | .dequeue_task = dequeue_task_rt, | |
2359 | .yield_task = yield_task_rt, | |
2360 | ||
2361 | .check_preempt_curr = check_preempt_curr_rt, | |
2362 | ||
2363 | .pick_next_task = pick_next_task_rt, | |
2364 | .put_prev_task = put_prev_task_rt, | |
2365 | ||
681f3e68 | 2366 | #ifdef CONFIG_SMP |
4ce72a2c LZ |
2367 | .select_task_rq = select_task_rq_rt, |
2368 | ||
6c37067e | 2369 | .set_cpus_allowed = set_cpus_allowed_common, |
1f11eb6a GH |
2370 | .rq_online = rq_online_rt, |
2371 | .rq_offline = rq_offline_rt, | |
efbbd05a | 2372 | .task_woken = task_woken_rt, |
cb469845 | 2373 | .switched_from = switched_from_rt, |
681f3e68 | 2374 | #endif |
bb44e5d1 | 2375 | |
83b699ed | 2376 | .set_curr_task = set_curr_task_rt, |
bb44e5d1 | 2377 | .task_tick = task_tick_rt, |
cb469845 | 2378 | |
0d721cea PW |
2379 | .get_rr_interval = get_rr_interval_rt, |
2380 | ||
cb469845 SR |
2381 | .prio_changed = prio_changed_rt, |
2382 | .switched_to = switched_to_rt, | |
6e998916 SG |
2383 | |
2384 | .update_curr = update_curr_rt, | |
bb44e5d1 | 2385 | }; |
ada18de2 | 2386 | |
8887cd99 NP |
2387 | #ifdef CONFIG_RT_GROUP_SCHED |
2388 | /* | |
2389 | * Ensure that the real time constraints are schedulable. | |
2390 | */ | |
2391 | static DEFINE_MUTEX(rt_constraints_mutex); | |
2392 | ||
2393 | /* Must be called with tasklist_lock held */ | |
2394 | static inline int tg_has_rt_tasks(struct task_group *tg) | |
2395 | { | |
2396 | struct task_struct *g, *p; | |
2397 | ||
2398 | /* | |
2399 | * Autogroups do not have RT tasks; see autogroup_create(). | |
2400 | */ | |
2401 | if (task_group_is_autogroup(tg)) | |
2402 | return 0; | |
2403 | ||
2404 | for_each_process_thread(g, p) { | |
2405 | if (rt_task(p) && task_group(p) == tg) | |
2406 | return 1; | |
2407 | } | |
2408 | ||
2409 | return 0; | |
2410 | } | |
2411 | ||
2412 | struct rt_schedulable_data { | |
2413 | struct task_group *tg; | |
2414 | u64 rt_period; | |
2415 | u64 rt_runtime; | |
2416 | }; | |
2417 | ||
2418 | static int tg_rt_schedulable(struct task_group *tg, void *data) | |
2419 | { | |
2420 | struct rt_schedulable_data *d = data; | |
2421 | struct task_group *child; | |
2422 | unsigned long total, sum = 0; | |
2423 | u64 period, runtime; | |
2424 | ||
2425 | period = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
2426 | runtime = tg->rt_bandwidth.rt_runtime; | |
2427 | ||
2428 | if (tg == d->tg) { | |
2429 | period = d->rt_period; | |
2430 | runtime = d->rt_runtime; | |
2431 | } | |
2432 | ||
2433 | /* | |
2434 | * Cannot have more runtime than the period. | |
2435 | */ | |
2436 | if (runtime > period && runtime != RUNTIME_INF) | |
2437 | return -EINVAL; | |
2438 | ||
2439 | /* | |
2440 | * Ensure we don't starve existing RT tasks. | |
2441 | */ | |
2442 | if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg)) | |
2443 | return -EBUSY; | |
2444 | ||
2445 | total = to_ratio(period, runtime); | |
2446 | ||
2447 | /* | |
2448 | * Nobody can have more than the global setting allows. | |
2449 | */ | |
2450 | if (total > to_ratio(global_rt_period(), global_rt_runtime())) | |
2451 | return -EINVAL; | |
2452 | ||
2453 | /* | |
2454 | * The sum of our children's runtime should not exceed our own. | |
2455 | */ | |
2456 | list_for_each_entry_rcu(child, &tg->children, siblings) { | |
2457 | period = ktime_to_ns(child->rt_bandwidth.rt_period); | |
2458 | runtime = child->rt_bandwidth.rt_runtime; | |
2459 | ||
2460 | if (child == d->tg) { | |
2461 | period = d->rt_period; | |
2462 | runtime = d->rt_runtime; | |
2463 | } | |
2464 | ||
2465 | sum += to_ratio(period, runtime); | |
2466 | } | |
2467 | ||
2468 | if (sum > total) | |
2469 | return -EINVAL; | |
2470 | ||
2471 | return 0; | |
2472 | } | |
2473 | ||
2474 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) | |
2475 | { | |
2476 | int ret; | |
2477 | ||
2478 | struct rt_schedulable_data data = { | |
2479 | .tg = tg, | |
2480 | .rt_period = period, | |
2481 | .rt_runtime = runtime, | |
2482 | }; | |
2483 | ||
2484 | rcu_read_lock(); | |
2485 | ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data); | |
2486 | rcu_read_unlock(); | |
2487 | ||
2488 | return ret; | |
2489 | } | |
2490 | ||
2491 | static int tg_set_rt_bandwidth(struct task_group *tg, | |
2492 | u64 rt_period, u64 rt_runtime) | |
2493 | { | |
2494 | int i, err = 0; | |
2495 | ||
2496 | /* | |
2497 | * Disallowing the root group RT runtime is BAD, it would disallow the | |
2498 | * kernel creating (and or operating) RT threads. | |
2499 | */ | |
2500 | if (tg == &root_task_group && rt_runtime == 0) | |
2501 | return -EINVAL; | |
2502 | ||
2503 | /* No period doesn't make any sense. */ | |
2504 | if (rt_period == 0) | |
2505 | return -EINVAL; | |
2506 | ||
2507 | mutex_lock(&rt_constraints_mutex); | |
2508 | read_lock(&tasklist_lock); | |
2509 | err = __rt_schedulable(tg, rt_period, rt_runtime); | |
2510 | if (err) | |
2511 | goto unlock; | |
2512 | ||
2513 | raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); | |
2514 | tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); | |
2515 | tg->rt_bandwidth.rt_runtime = rt_runtime; | |
2516 | ||
2517 | for_each_possible_cpu(i) { | |
2518 | struct rt_rq *rt_rq = tg->rt_rq[i]; | |
2519 | ||
2520 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
2521 | rt_rq->rt_runtime = rt_runtime; | |
2522 | raw_spin_unlock(&rt_rq->rt_runtime_lock); | |
2523 | } | |
2524 | raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); | |
2525 | unlock: | |
2526 | read_unlock(&tasklist_lock); | |
2527 | mutex_unlock(&rt_constraints_mutex); | |
2528 | ||
2529 | return err; | |
2530 | } | |
2531 | ||
2532 | int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) | |
2533 | { | |
2534 | u64 rt_runtime, rt_period; | |
2535 | ||
2536 | rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
2537 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; | |
2538 | if (rt_runtime_us < 0) | |
2539 | rt_runtime = RUNTIME_INF; | |
2540 | ||
2541 | return tg_set_rt_bandwidth(tg, rt_period, rt_runtime); | |
2542 | } | |
2543 | ||
2544 | long sched_group_rt_runtime(struct task_group *tg) | |
2545 | { | |
2546 | u64 rt_runtime_us; | |
2547 | ||
2548 | if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF) | |
2549 | return -1; | |
2550 | ||
2551 | rt_runtime_us = tg->rt_bandwidth.rt_runtime; | |
2552 | do_div(rt_runtime_us, NSEC_PER_USEC); | |
2553 | return rt_runtime_us; | |
2554 | } | |
2555 | ||
2556 | int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us) | |
2557 | { | |
2558 | u64 rt_runtime, rt_period; | |
2559 | ||
2560 | rt_period = rt_period_us * NSEC_PER_USEC; | |
2561 | rt_runtime = tg->rt_bandwidth.rt_runtime; | |
2562 | ||
2563 | return tg_set_rt_bandwidth(tg, rt_period, rt_runtime); | |
2564 | } | |
2565 | ||
2566 | long sched_group_rt_period(struct task_group *tg) | |
2567 | { | |
2568 | u64 rt_period_us; | |
2569 | ||
2570 | rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
2571 | do_div(rt_period_us, NSEC_PER_USEC); | |
2572 | return rt_period_us; | |
2573 | } | |
2574 | ||
2575 | static int sched_rt_global_constraints(void) | |
2576 | { | |
2577 | int ret = 0; | |
2578 | ||
2579 | mutex_lock(&rt_constraints_mutex); | |
2580 | read_lock(&tasklist_lock); | |
2581 | ret = __rt_schedulable(NULL, 0, 0); | |
2582 | read_unlock(&tasklist_lock); | |
2583 | mutex_unlock(&rt_constraints_mutex); | |
2584 | ||
2585 | return ret; | |
2586 | } | |
2587 | ||
2588 | int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk) | |
2589 | { | |
2590 | /* Don't accept realtime tasks when there is no way for them to run */ | |
2591 | if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0) | |
2592 | return 0; | |
2593 | ||
2594 | return 1; | |
2595 | } | |
2596 | ||
2597 | #else /* !CONFIG_RT_GROUP_SCHED */ | |
2598 | static int sched_rt_global_constraints(void) | |
2599 | { | |
2600 | unsigned long flags; | |
2601 | int i; | |
2602 | ||
2603 | raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); | |
2604 | for_each_possible_cpu(i) { | |
2605 | struct rt_rq *rt_rq = &cpu_rq(i)->rt; | |
2606 | ||
2607 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
2608 | rt_rq->rt_runtime = global_rt_runtime(); | |
2609 | raw_spin_unlock(&rt_rq->rt_runtime_lock); | |
2610 | } | |
2611 | raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); | |
2612 | ||
2613 | return 0; | |
2614 | } | |
2615 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
2616 | ||
2617 | static int sched_rt_global_validate(void) | |
2618 | { | |
2619 | if (sysctl_sched_rt_period <= 0) | |
2620 | return -EINVAL; | |
2621 | ||
2622 | if ((sysctl_sched_rt_runtime != RUNTIME_INF) && | |
2623 | (sysctl_sched_rt_runtime > sysctl_sched_rt_period)) | |
2624 | return -EINVAL; | |
2625 | ||
2626 | return 0; | |
2627 | } | |
2628 | ||
2629 | static void sched_rt_do_global(void) | |
2630 | { | |
2631 | def_rt_bandwidth.rt_runtime = global_rt_runtime(); | |
2632 | def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period()); | |
2633 | } | |
2634 | ||
2635 | int sched_rt_handler(struct ctl_table *table, int write, | |
2636 | void __user *buffer, size_t *lenp, | |
2637 | loff_t *ppos) | |
2638 | { | |
2639 | int old_period, old_runtime; | |
2640 | static DEFINE_MUTEX(mutex); | |
2641 | int ret; | |
2642 | ||
2643 | mutex_lock(&mutex); | |
2644 | old_period = sysctl_sched_rt_period; | |
2645 | old_runtime = sysctl_sched_rt_runtime; | |
2646 | ||
2647 | ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
2648 | ||
2649 | if (!ret && write) { | |
2650 | ret = sched_rt_global_validate(); | |
2651 | if (ret) | |
2652 | goto undo; | |
2653 | ||
2654 | ret = sched_dl_global_validate(); | |
2655 | if (ret) | |
2656 | goto undo; | |
2657 | ||
2658 | ret = sched_rt_global_constraints(); | |
2659 | if (ret) | |
2660 | goto undo; | |
2661 | ||
2662 | sched_rt_do_global(); | |
2663 | sched_dl_do_global(); | |
2664 | } | |
2665 | if (0) { | |
2666 | undo: | |
2667 | sysctl_sched_rt_period = old_period; | |
2668 | sysctl_sched_rt_runtime = old_runtime; | |
2669 | } | |
2670 | mutex_unlock(&mutex); | |
2671 | ||
2672 | return ret; | |
2673 | } | |
2674 | ||
2675 | int sched_rr_handler(struct ctl_table *table, int write, | |
2676 | void __user *buffer, size_t *lenp, | |
2677 | loff_t *ppos) | |
2678 | { | |
2679 | int ret; | |
2680 | static DEFINE_MUTEX(mutex); | |
2681 | ||
2682 | mutex_lock(&mutex); | |
2683 | ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
2684 | /* | |
2685 | * Make sure that internally we keep jiffies. | |
2686 | * Also, writing zero resets the timeslice to default: | |
2687 | */ | |
2688 | if (!ret && write) { | |
2689 | sched_rr_timeslice = | |
2690 | sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE : | |
2691 | msecs_to_jiffies(sysctl_sched_rr_timeslice); | |
2692 | } | |
2693 | mutex_unlock(&mutex); | |
97fb7a0a | 2694 | |
8887cd99 NP |
2695 | return ret; |
2696 | } | |
2697 | ||
ada18de2 PZ |
2698 | #ifdef CONFIG_SCHED_DEBUG |
2699 | extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq); | |
2700 | ||
029632fb | 2701 | void print_rt_stats(struct seq_file *m, int cpu) |
ada18de2 | 2702 | { |
ec514c48 | 2703 | rt_rq_iter_t iter; |
ada18de2 PZ |
2704 | struct rt_rq *rt_rq; |
2705 | ||
2706 | rcu_read_lock(); | |
ec514c48 | 2707 | for_each_rt_rq(rt_rq, iter, cpu_rq(cpu)) |
ada18de2 PZ |
2708 | print_rt_rq(m, cpu, rt_rq); |
2709 | rcu_read_unlock(); | |
2710 | } | |
55e12e5e | 2711 | #endif /* CONFIG_SCHED_DEBUG */ |