Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR |
| 3 | * policies) |
| 4 | */ |
| 5 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6 | #include "sched.h" |
| 7 | |
| 8 | #include <linux/slab.h> |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 9 | #include <linux/irq_work.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 10 | |
Clark Williams | ce0dbbb | 2013-02-07 09:47:04 -0600 | [diff] [blame] | 11 | int sched_rr_timeslice = RR_TIMESLICE; |
| 12 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 13 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); |
| 14 | |
| 15 | struct rt_bandwidth def_rt_bandwidth; |
| 16 | |
| 17 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) |
| 18 | { |
| 19 | struct rt_bandwidth *rt_b = |
| 20 | container_of(timer, struct rt_bandwidth, rt_period_timer); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 21 | int idle = 0; |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 22 | int overrun; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 23 | |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 24 | raw_spin_lock(&rt_b->rt_runtime_lock); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 25 | for (;;) { |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 26 | overrun = hrtimer_forward_now(timer, rt_b->rt_period); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 27 | if (!overrun) |
| 28 | break; |
| 29 | |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 30 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 31 | idle = do_sched_rt_period_timer(rt_b, overrun); |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 32 | raw_spin_lock(&rt_b->rt_runtime_lock); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 33 | } |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 34 | if (idle) |
| 35 | rt_b->rt_period_active = 0; |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 36 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 37 | |
| 38 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; |
| 39 | } |
| 40 | |
| 41 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) |
| 42 | { |
| 43 | rt_b->rt_period = ns_to_ktime(period); |
| 44 | rt_b->rt_runtime = runtime; |
| 45 | |
| 46 | raw_spin_lock_init(&rt_b->rt_runtime_lock); |
| 47 | |
| 48 | hrtimer_init(&rt_b->rt_period_timer, |
| 49 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 50 | rt_b->rt_period_timer.function = sched_rt_period_timer; |
| 51 | } |
| 52 | |
| 53 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) |
| 54 | { |
| 55 | if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) |
| 56 | return; |
| 57 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 58 | raw_spin_lock(&rt_b->rt_runtime_lock); |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 59 | if (!rt_b->rt_period_active) { |
| 60 | rt_b->rt_period_active = 1; |
Steven Rostedt | c3a990d | 2016-02-16 18:37:46 -0500 | [diff] [blame] | 61 | /* |
| 62 | * SCHED_DEADLINE updates the bandwidth, as a run away |
| 63 | * RT task with a DL task could hog a CPU. But DL does |
| 64 | * not reset the period. If a deadline task was running |
| 65 | * without an RT task running, it can cause RT tasks to |
| 66 | * throttle when they start up. Kick the timer right away |
| 67 | * to update the period. |
| 68 | */ |
| 69 | hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0)); |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 70 | hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED); |
| 71 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 72 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
| 73 | } |
| 74 | |
Abel Vesa | 07c54f7 | 2015-03-03 13:50:27 +0200 | [diff] [blame] | 75 | void init_rt_rq(struct rt_rq *rt_rq) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 76 | { |
| 77 | struct rt_prio_array *array; |
| 78 | int i; |
| 79 | |
| 80 | array = &rt_rq->active; |
| 81 | for (i = 0; i < MAX_RT_PRIO; i++) { |
| 82 | INIT_LIST_HEAD(array->queue + i); |
| 83 | __clear_bit(i, array->bitmap); |
| 84 | } |
| 85 | /* delimiter for bitsearch: */ |
| 86 | __set_bit(MAX_RT_PRIO, array->bitmap); |
| 87 | |
| 88 | #if defined CONFIG_SMP |
| 89 | rt_rq->highest_prio.curr = MAX_RT_PRIO; |
| 90 | rt_rq->highest_prio.next = MAX_RT_PRIO; |
| 91 | rt_rq->rt_nr_migratory = 0; |
| 92 | rt_rq->overloaded = 0; |
| 93 | plist_head_init(&rt_rq->pushable_tasks); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 94 | #endif /* CONFIG_SMP */ |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 95 | /* We start is dequeued state, because no RT tasks are queued */ |
| 96 | rt_rq->rt_queued = 0; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 97 | |
| 98 | rt_rq->rt_time = 0; |
| 99 | rt_rq->rt_throttled = 0; |
| 100 | rt_rq->rt_runtime = 0; |
| 101 | raw_spin_lock_init(&rt_rq->rt_runtime_lock); |
| 102 | } |
| 103 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 104 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 105 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) |
| 106 | { |
| 107 | hrtimer_cancel(&rt_b->rt_period_timer); |
| 108 | } |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 109 | |
Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 110 | #define rt_entity_is_task(rt_se) (!(rt_se)->my_q) |
| 111 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 112 | static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) |
| 113 | { |
| 114 | #ifdef CONFIG_SCHED_DEBUG |
| 115 | WARN_ON_ONCE(!rt_entity_is_task(rt_se)); |
| 116 | #endif |
| 117 | return container_of(rt_se, struct task_struct, rt); |
| 118 | } |
| 119 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 120 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) |
| 121 | { |
| 122 | return rt_rq->rq; |
| 123 | } |
| 124 | |
| 125 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) |
| 126 | { |
| 127 | return rt_se->rt_rq; |
| 128 | } |
| 129 | |
Kirill Tkhai | 653d07a | 2014-03-15 02:14:55 +0400 | [diff] [blame] | 130 | static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se) |
| 131 | { |
| 132 | struct rt_rq *rt_rq = rt_se->rt_rq; |
| 133 | |
| 134 | return rt_rq->rq; |
| 135 | } |
| 136 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 137 | void free_rt_sched_group(struct task_group *tg) |
| 138 | { |
| 139 | int i; |
| 140 | |
| 141 | if (tg->rt_se) |
| 142 | destroy_rt_bandwidth(&tg->rt_bandwidth); |
| 143 | |
| 144 | for_each_possible_cpu(i) { |
| 145 | if (tg->rt_rq) |
| 146 | kfree(tg->rt_rq[i]); |
| 147 | if (tg->rt_se) |
| 148 | kfree(tg->rt_se[i]); |
| 149 | } |
| 150 | |
| 151 | kfree(tg->rt_rq); |
| 152 | kfree(tg->rt_se); |
| 153 | } |
| 154 | |
| 155 | void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, |
| 156 | struct sched_rt_entity *rt_se, int cpu, |
| 157 | struct sched_rt_entity *parent) |
| 158 | { |
| 159 | struct rq *rq = cpu_rq(cpu); |
| 160 | |
| 161 | rt_rq->highest_prio.curr = MAX_RT_PRIO; |
| 162 | rt_rq->rt_nr_boosted = 0; |
| 163 | rt_rq->rq = rq; |
| 164 | rt_rq->tg = tg; |
| 165 | |
| 166 | tg->rt_rq[cpu] = rt_rq; |
| 167 | tg->rt_se[cpu] = rt_se; |
| 168 | |
| 169 | if (!rt_se) |
| 170 | return; |
| 171 | |
| 172 | if (!parent) |
| 173 | rt_se->rt_rq = &rq->rt; |
| 174 | else |
| 175 | rt_se->rt_rq = parent->my_q; |
| 176 | |
| 177 | rt_se->my_q = rt_rq; |
| 178 | rt_se->parent = parent; |
| 179 | INIT_LIST_HEAD(&rt_se->run_list); |
| 180 | } |
| 181 | |
| 182 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) |
| 183 | { |
| 184 | struct rt_rq *rt_rq; |
| 185 | struct sched_rt_entity *rt_se; |
| 186 | int i; |
| 187 | |
| 188 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); |
| 189 | if (!tg->rt_rq) |
| 190 | goto err; |
| 191 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); |
| 192 | if (!tg->rt_se) |
| 193 | goto err; |
| 194 | |
| 195 | init_rt_bandwidth(&tg->rt_bandwidth, |
| 196 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); |
| 197 | |
| 198 | for_each_possible_cpu(i) { |
| 199 | rt_rq = kzalloc_node(sizeof(struct rt_rq), |
| 200 | GFP_KERNEL, cpu_to_node(i)); |
| 201 | if (!rt_rq) |
| 202 | goto err; |
| 203 | |
| 204 | rt_se = kzalloc_node(sizeof(struct sched_rt_entity), |
| 205 | GFP_KERNEL, cpu_to_node(i)); |
| 206 | if (!rt_se) |
| 207 | goto err_free_rq; |
| 208 | |
Abel Vesa | 07c54f7 | 2015-03-03 13:50:27 +0200 | [diff] [blame] | 209 | init_rt_rq(rt_rq); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 210 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; |
| 211 | init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]); |
| 212 | } |
| 213 | |
| 214 | return 1; |
| 215 | |
| 216 | err_free_rq: |
| 217 | kfree(rt_rq); |
| 218 | err: |
| 219 | return 0; |
| 220 | } |
| 221 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 222 | #else /* CONFIG_RT_GROUP_SCHED */ |
| 223 | |
Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 224 | #define rt_entity_is_task(rt_se) (1) |
| 225 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 226 | static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) |
| 227 | { |
| 228 | return container_of(rt_se, struct task_struct, rt); |
| 229 | } |
| 230 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 231 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) |
| 232 | { |
| 233 | return container_of(rt_rq, struct rq, rt); |
| 234 | } |
| 235 | |
Kirill Tkhai | 653d07a | 2014-03-15 02:14:55 +0400 | [diff] [blame] | 236 | static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se) |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 237 | { |
| 238 | struct task_struct *p = rt_task_of(rt_se); |
Kirill Tkhai | 653d07a | 2014-03-15 02:14:55 +0400 | [diff] [blame] | 239 | |
| 240 | return task_rq(p); |
| 241 | } |
| 242 | |
| 243 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) |
| 244 | { |
| 245 | struct rq *rq = rq_of_rt_se(rt_se); |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 246 | |
| 247 | return &rq->rt; |
| 248 | } |
| 249 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 250 | void free_rt_sched_group(struct task_group *tg) { } |
| 251 | |
| 252 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) |
| 253 | { |
| 254 | return 1; |
| 255 | } |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 256 | #endif /* CONFIG_RT_GROUP_SCHED */ |
| 257 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 258 | #ifdef CONFIG_SMP |
Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 259 | |
Peter Zijlstra | 8046d68 | 2015-06-11 14:46:40 +0200 | [diff] [blame] | 260 | static void pull_rt_task(struct rq *this_rq); |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 261 | |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 262 | static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev) |
| 263 | { |
| 264 | /* Try to pull RT tasks here if we lower this rq's prio */ |
| 265 | return rq->rt.highest_prio.curr > prev->prio; |
| 266 | } |
| 267 | |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 268 | static inline int rt_overloaded(struct rq *rq) |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 269 | { |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 270 | return atomic_read(&rq->rd->rto_count); |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 271 | } |
Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 272 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 273 | static inline void rt_set_overload(struct rq *rq) |
| 274 | { |
Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 275 | if (!rq->online) |
| 276 | return; |
| 277 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 278 | cpumask_set_cpu(rq->cpu, rq->rd->rto_mask); |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 279 | /* |
| 280 | * Make sure the mask is visible before we set |
| 281 | * the overload count. That is checked to determine |
| 282 | * if we should look at the mask. It would be a shame |
| 283 | * if we looked at the mask, but the mask was not |
| 284 | * updated yet. |
Peter Zijlstra | 7c3f2ab | 2013-10-15 12:35:07 +0200 | [diff] [blame] | 285 | * |
| 286 | * Matched by the barrier in pull_rt_task(). |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 287 | */ |
Peter Zijlstra | 7c3f2ab | 2013-10-15 12:35:07 +0200 | [diff] [blame] | 288 | smp_wmb(); |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 289 | atomic_inc(&rq->rd->rto_count); |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 290 | } |
Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 291 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 292 | static inline void rt_clear_overload(struct rq *rq) |
| 293 | { |
Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 294 | if (!rq->online) |
| 295 | return; |
| 296 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 297 | /* the order here really doesn't matter */ |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 298 | atomic_dec(&rq->rd->rto_count); |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 299 | cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask); |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 300 | } |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 301 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 302 | static void update_rt_migration(struct rt_rq *rt_rq) |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 303 | { |
Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 304 | if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) { |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 305 | if (!rt_rq->overloaded) { |
| 306 | rt_set_overload(rq_of_rt_rq(rt_rq)); |
| 307 | rt_rq->overloaded = 1; |
Gregory Haskins | cdc8eb9 | 2008-01-25 21:08:23 +0100 | [diff] [blame] | 308 | } |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 309 | } else if (rt_rq->overloaded) { |
| 310 | rt_clear_overload(rq_of_rt_rq(rt_rq)); |
| 311 | rt_rq->overloaded = 0; |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 312 | } |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 313 | } |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 314 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 315 | static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 316 | { |
Peter Zijlstra | 29baa74 | 2012-04-23 12:11:21 +0200 | [diff] [blame] | 317 | struct task_struct *p; |
| 318 | |
Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 319 | if (!rt_entity_is_task(rt_se)) |
| 320 | return; |
| 321 | |
Peter Zijlstra | 29baa74 | 2012-04-23 12:11:21 +0200 | [diff] [blame] | 322 | p = rt_task_of(rt_se); |
Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 323 | rt_rq = &rq_of_rt_rq(rt_rq)->rt; |
| 324 | |
| 325 | rt_rq->rt_nr_total++; |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 326 | if (tsk_nr_cpus_allowed(p) > 1) |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 327 | rt_rq->rt_nr_migratory++; |
| 328 | |
| 329 | update_rt_migration(rt_rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 332 | static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
| 333 | { |
Peter Zijlstra | 29baa74 | 2012-04-23 12:11:21 +0200 | [diff] [blame] | 334 | struct task_struct *p; |
| 335 | |
Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 336 | if (!rt_entity_is_task(rt_se)) |
| 337 | return; |
| 338 | |
Peter Zijlstra | 29baa74 | 2012-04-23 12:11:21 +0200 | [diff] [blame] | 339 | p = rt_task_of(rt_se); |
Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 340 | rt_rq = &rq_of_rt_rq(rt_rq)->rt; |
| 341 | |
| 342 | rt_rq->rt_nr_total--; |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 343 | if (tsk_nr_cpus_allowed(p) > 1) |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 344 | rt_rq->rt_nr_migratory--; |
| 345 | |
| 346 | update_rt_migration(rt_rq); |
| 347 | } |
| 348 | |
Steven Rostedt | 5181f4a4 | 2011-06-16 21:55:23 -0400 | [diff] [blame] | 349 | static inline int has_pushable_tasks(struct rq *rq) |
| 350 | { |
| 351 | return !plist_head_empty(&rq->rt.pushable_tasks); |
| 352 | } |
| 353 | |
Peter Zijlstra | fd7a4be | 2015-06-11 14:46:41 +0200 | [diff] [blame] | 354 | static DEFINE_PER_CPU(struct callback_head, rt_push_head); |
| 355 | static DEFINE_PER_CPU(struct callback_head, rt_pull_head); |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 356 | |
| 357 | static void push_rt_tasks(struct rq *); |
Peter Zijlstra | fd7a4be | 2015-06-11 14:46:41 +0200 | [diff] [blame] | 358 | static void pull_rt_task(struct rq *); |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 359 | |
| 360 | static inline void queue_push_tasks(struct rq *rq) |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 361 | { |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 362 | if (!has_pushable_tasks(rq)) |
| 363 | return; |
| 364 | |
Peter Zijlstra | fd7a4be | 2015-06-11 14:46:41 +0200 | [diff] [blame] | 365 | queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks); |
| 366 | } |
| 367 | |
| 368 | static inline void queue_pull_task(struct rq *rq) |
| 369 | { |
| 370 | queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task); |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 373 | static void enqueue_pushable_task(struct rq *rq, struct task_struct *p) |
| 374 | { |
| 375 | plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); |
| 376 | plist_node_init(&p->pushable_tasks, p->prio); |
| 377 | plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks); |
Steven Rostedt | 5181f4a4 | 2011-06-16 21:55:23 -0400 | [diff] [blame] | 378 | |
| 379 | /* Update the highest prio pushable task */ |
| 380 | if (p->prio < rq->rt.highest_prio.next) |
| 381 | rq->rt.highest_prio.next = p->prio; |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static void dequeue_pushable_task(struct rq *rq, struct task_struct *p) |
| 385 | { |
| 386 | plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 387 | |
Steven Rostedt | 5181f4a4 | 2011-06-16 21:55:23 -0400 | [diff] [blame] | 388 | /* Update the new highest prio pushable task */ |
| 389 | if (has_pushable_tasks(rq)) { |
| 390 | p = plist_first_entry(&rq->rt.pushable_tasks, |
| 391 | struct task_struct, pushable_tasks); |
| 392 | rq->rt.highest_prio.next = p->prio; |
| 393 | } else |
| 394 | rq->rt.highest_prio.next = MAX_RT_PRIO; |
Ingo Molnar | bcf08df | 2008-04-19 12:11:10 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 397 | #else |
| 398 | |
Peter Zijlstra | ceacc2c | 2009-01-16 14:46:40 +0100 | [diff] [blame] | 399 | static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p) |
| 400 | { |
| 401 | } |
| 402 | |
| 403 | static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p) |
| 404 | { |
| 405 | } |
| 406 | |
Gregory Haskins | b07430a | 2009-01-14 08:55:39 -0500 | [diff] [blame] | 407 | static inline |
Peter Zijlstra | ceacc2c | 2009-01-16 14:46:40 +0100 | [diff] [blame] | 408 | void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
| 409 | { |
| 410 | } |
| 411 | |
Gregory Haskins | b07430a | 2009-01-14 08:55:39 -0500 | [diff] [blame] | 412 | static inline |
Peter Zijlstra | ceacc2c | 2009-01-16 14:46:40 +0100 | [diff] [blame] | 413 | void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
| 414 | { |
| 415 | } |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 416 | |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 417 | static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev) |
| 418 | { |
| 419 | return false; |
| 420 | } |
| 421 | |
Peter Zijlstra | 8046d68 | 2015-06-11 14:46:40 +0200 | [diff] [blame] | 422 | static inline void pull_rt_task(struct rq *this_rq) |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 423 | { |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 426 | static inline void queue_push_tasks(struct rq *rq) |
Peter Zijlstra | dc87734 | 2014-02-12 15:47:29 +0100 | [diff] [blame] | 427 | { |
| 428 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 429 | #endif /* CONFIG_SMP */ |
| 430 | |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 431 | static void enqueue_top_rt_rq(struct rt_rq *rt_rq); |
| 432 | static void dequeue_top_rt_rq(struct rt_rq *rt_rq); |
| 433 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 434 | static inline int on_rt_rq(struct sched_rt_entity *rt_se) |
| 435 | { |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 436 | return rt_se->on_rq; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 439 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 440 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 441 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 442 | { |
| 443 | if (!rt_rq->tg) |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 444 | return RUNTIME_INF; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 445 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 446 | return rt_rq->rt_runtime; |
| 447 | } |
| 448 | |
| 449 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) |
| 450 | { |
| 451 | return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 454 | typedef struct task_group *rt_rq_iter_t; |
| 455 | |
Yong Zhang | 1c09ab0 | 2011-06-28 10:51:31 +0800 | [diff] [blame] | 456 | static inline struct task_group *next_task_group(struct task_group *tg) |
| 457 | { |
| 458 | do { |
| 459 | tg = list_entry_rcu(tg->list.next, |
| 460 | typeof(struct task_group), list); |
| 461 | } while (&tg->list != &task_groups && task_group_is_autogroup(tg)); |
| 462 | |
| 463 | if (&tg->list == &task_groups) |
| 464 | tg = NULL; |
| 465 | |
| 466 | return tg; |
| 467 | } |
| 468 | |
| 469 | #define for_each_rt_rq(rt_rq, iter, rq) \ |
| 470 | for (iter = container_of(&task_groups, typeof(*iter), list); \ |
| 471 | (iter = next_task_group(iter)) && \ |
| 472 | (rt_rq = iter->rt_rq[cpu_of(rq)]);) |
Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 473 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 474 | #define for_each_sched_rt_entity(rt_se) \ |
| 475 | for (; rt_se; rt_se = rt_se->parent) |
| 476 | |
| 477 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) |
| 478 | { |
| 479 | return rt_se->my_q; |
| 480 | } |
| 481 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 482 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags); |
| 483 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 484 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 485 | static void sched_rt_rq_enqueue(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 486 | { |
Dario Faggioli | f6121f4 | 2008-10-03 17:40:46 +0200 | [diff] [blame] | 487 | struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr; |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 488 | struct rq *rq = rq_of_rt_rq(rt_rq); |
Yong Zhang | 74b7eb5 | 2010-01-29 14:57:52 +0800 | [diff] [blame] | 489 | struct sched_rt_entity *rt_se; |
| 490 | |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 491 | int cpu = cpu_of(rq); |
Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 492 | |
| 493 | rt_se = rt_rq->tg->rt_se[cpu]; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 494 | |
Dario Faggioli | f6121f4 | 2008-10-03 17:40:46 +0200 | [diff] [blame] | 495 | if (rt_rq->rt_nr_running) { |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 496 | if (!rt_se) |
| 497 | enqueue_top_rt_rq(rt_rq); |
| 498 | else if (!on_rt_rq(rt_se)) |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 499 | enqueue_rt_entity(rt_se, 0); |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 500 | |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 501 | if (rt_rq->highest_prio.curr < curr->prio) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 502 | resched_curr(rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 506 | static void sched_rt_rq_dequeue(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 507 | { |
Yong Zhang | 74b7eb5 | 2010-01-29 14:57:52 +0800 | [diff] [blame] | 508 | struct sched_rt_entity *rt_se; |
Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 509 | int cpu = cpu_of(rq_of_rt_rq(rt_rq)); |
Yong Zhang | 74b7eb5 | 2010-01-29 14:57:52 +0800 | [diff] [blame] | 510 | |
Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 511 | rt_se = rt_rq->tg->rt_se[cpu]; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 512 | |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 513 | if (!rt_se) |
| 514 | dequeue_top_rt_rq(rt_rq); |
| 515 | else if (on_rt_rq(rt_se)) |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 516 | dequeue_rt_entity(rt_se, 0); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Kirill Tkhai | 4638364 | 2014-03-15 02:15:07 +0400 | [diff] [blame] | 519 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) |
| 520 | { |
| 521 | return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted; |
| 522 | } |
| 523 | |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 524 | static int rt_se_boosted(struct sched_rt_entity *rt_se) |
| 525 | { |
| 526 | struct rt_rq *rt_rq = group_rt_rq(rt_se); |
| 527 | struct task_struct *p; |
| 528 | |
| 529 | if (rt_rq) |
| 530 | return !!rt_rq->rt_nr_boosted; |
| 531 | |
| 532 | p = rt_task_of(rt_se); |
| 533 | return p->prio != p->normal_prio; |
| 534 | } |
| 535 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 536 | #ifdef CONFIG_SMP |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 537 | static inline const struct cpumask *sched_rt_period_mask(void) |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 538 | { |
Nathan Zimmer | 424c93f | 2013-05-09 11:24:03 -0500 | [diff] [blame] | 539 | return this_rq()->rd->span; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 540 | } |
| 541 | #else |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 542 | static inline const struct cpumask *sched_rt_period_mask(void) |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 543 | { |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 544 | return cpu_online_mask; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 545 | } |
| 546 | #endif |
| 547 | |
| 548 | static inline |
| 549 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) |
| 550 | { |
| 551 | return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu]; |
| 552 | } |
| 553 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 554 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) |
| 555 | { |
| 556 | return &rt_rq->tg->rt_bandwidth; |
| 557 | } |
| 558 | |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 559 | #else /* !CONFIG_RT_GROUP_SCHED */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 560 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 561 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 562 | { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 563 | return rt_rq->rt_runtime; |
| 564 | } |
| 565 | |
| 566 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) |
| 567 | { |
| 568 | return ktime_to_ns(def_rt_bandwidth.rt_period); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 571 | typedef struct rt_rq *rt_rq_iter_t; |
| 572 | |
| 573 | #define for_each_rt_rq(rt_rq, iter, rq) \ |
| 574 | for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL) |
| 575 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 576 | #define for_each_sched_rt_entity(rt_se) \ |
| 577 | for (; rt_se; rt_se = NULL) |
| 578 | |
| 579 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) |
| 580 | { |
| 581 | return NULL; |
| 582 | } |
| 583 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 584 | static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 585 | { |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 586 | struct rq *rq = rq_of_rt_rq(rt_rq); |
| 587 | |
| 588 | if (!rt_rq->rt_nr_running) |
| 589 | return; |
| 590 | |
| 591 | enqueue_top_rt_rq(rt_rq); |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 592 | resched_curr(rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 593 | } |
| 594 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 595 | static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 596 | { |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 597 | dequeue_top_rt_rq(rt_rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 598 | } |
| 599 | |
Kirill Tkhai | 4638364 | 2014-03-15 02:15:07 +0400 | [diff] [blame] | 600 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) |
| 601 | { |
| 602 | return rt_rq->rt_throttled; |
| 603 | } |
| 604 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 605 | static inline const struct cpumask *sched_rt_period_mask(void) |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 606 | { |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 607 | return cpu_online_mask; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static inline |
| 611 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) |
| 612 | { |
| 613 | return &cpu_rq(cpu)->rt; |
| 614 | } |
| 615 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 616 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) |
| 617 | { |
| 618 | return &def_rt_bandwidth; |
| 619 | } |
| 620 | |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 621 | #endif /* CONFIG_RT_GROUP_SCHED */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 622 | |
Juri Lelli | faa5993 | 2014-02-21 11:37:15 +0100 | [diff] [blame] | 623 | bool sched_rt_bandwidth_account(struct rt_rq *rt_rq) |
| 624 | { |
| 625 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
| 626 | |
| 627 | return (hrtimer_active(&rt_b->rt_period_timer) || |
| 628 | rt_rq->rt_time < rt_b->rt_runtime); |
| 629 | } |
| 630 | |
Peter Zijlstra | b79f383 | 2008-06-19 14:22:25 +0200 | [diff] [blame] | 631 | #ifdef CONFIG_SMP |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 632 | /* |
| 633 | * We ran out of runtime, see if we can borrow some from our neighbours. |
| 634 | */ |
Juri Lelli | 269b26a | 2015-09-02 11:01:36 +0100 | [diff] [blame] | 635 | static void do_balance_runtime(struct rt_rq *rt_rq) |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 636 | { |
| 637 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
Shawn Bohrer | aa7f673 | 2013-01-14 11:55:31 -0600 | [diff] [blame] | 638 | struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd; |
Juri Lelli | 269b26a | 2015-09-02 11:01:36 +0100 | [diff] [blame] | 639 | int i, weight; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 640 | u64 rt_period; |
| 641 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 642 | weight = cpumask_weight(rd->span); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 643 | |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 644 | raw_spin_lock(&rt_b->rt_runtime_lock); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 645 | rt_period = ktime_to_ns(rt_b->rt_period); |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 646 | for_each_cpu(i, rd->span) { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 647 | struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); |
| 648 | s64 diff; |
| 649 | |
| 650 | if (iter == rt_rq) |
| 651 | continue; |
| 652 | |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 653 | raw_spin_lock(&iter->rt_runtime_lock); |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 654 | /* |
| 655 | * Either all rqs have inf runtime and there's nothing to steal |
| 656 | * or __disable_runtime() below sets a specific rq to inf to |
| 657 | * indicate its been disabled and disalow stealing. |
| 658 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 659 | if (iter->rt_runtime == RUNTIME_INF) |
| 660 | goto next; |
| 661 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 662 | /* |
| 663 | * From runqueues with spare time, take 1/n part of their |
| 664 | * spare time, but no more than our period. |
| 665 | */ |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 666 | diff = iter->rt_runtime - iter->rt_time; |
| 667 | if (diff > 0) { |
Peter Zijlstra | 58838cf | 2008-07-24 12:43:13 +0200 | [diff] [blame] | 668 | diff = div_u64((u64)diff, weight); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 669 | if (rt_rq->rt_runtime + diff > rt_period) |
| 670 | diff = rt_period - rt_rq->rt_runtime; |
| 671 | iter->rt_runtime -= diff; |
| 672 | rt_rq->rt_runtime += diff; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 673 | if (rt_rq->rt_runtime == rt_period) { |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 674 | raw_spin_unlock(&iter->rt_runtime_lock); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | } |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 678 | next: |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 679 | raw_spin_unlock(&iter->rt_runtime_lock); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 680 | } |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 681 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 682 | } |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 683 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 684 | /* |
| 685 | * Ensure this RQ takes back all the runtime it lend to its neighbours. |
| 686 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 687 | static void __disable_runtime(struct rq *rq) |
| 688 | { |
| 689 | struct root_domain *rd = rq->rd; |
Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 690 | rt_rq_iter_t iter; |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 691 | struct rt_rq *rt_rq; |
| 692 | |
| 693 | if (unlikely(!scheduler_running)) |
| 694 | return; |
| 695 | |
Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 696 | for_each_rt_rq(rt_rq, iter, rq) { |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 697 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
| 698 | s64 want; |
| 699 | int i; |
| 700 | |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 701 | raw_spin_lock(&rt_b->rt_runtime_lock); |
| 702 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 703 | /* |
| 704 | * Either we're all inf and nobody needs to borrow, or we're |
| 705 | * already disabled and thus have nothing to do, or we have |
| 706 | * exactly the right amount of runtime to take out. |
| 707 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 708 | if (rt_rq->rt_runtime == RUNTIME_INF || |
| 709 | rt_rq->rt_runtime == rt_b->rt_runtime) |
| 710 | goto balanced; |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 711 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 712 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 713 | /* |
| 714 | * Calculate the difference between what we started out with |
| 715 | * and what we current have, that's the amount of runtime |
| 716 | * we lend and now have to reclaim. |
| 717 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 718 | want = rt_b->rt_runtime - rt_rq->rt_runtime; |
| 719 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 720 | /* |
| 721 | * Greedy reclaim, take back as much as we can. |
| 722 | */ |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 723 | for_each_cpu(i, rd->span) { |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 724 | struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); |
| 725 | s64 diff; |
| 726 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 727 | /* |
| 728 | * Can't reclaim from ourselves or disabled runqueues. |
| 729 | */ |
Peter Zijlstra | f1679d0 | 2008-08-14 15:49:00 +0200 | [diff] [blame] | 730 | if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF) |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 731 | continue; |
| 732 | |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 733 | raw_spin_lock(&iter->rt_runtime_lock); |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 734 | if (want > 0) { |
| 735 | diff = min_t(s64, iter->rt_runtime, want); |
| 736 | iter->rt_runtime -= diff; |
| 737 | want -= diff; |
| 738 | } else { |
| 739 | iter->rt_runtime -= want; |
| 740 | want -= want; |
| 741 | } |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 742 | raw_spin_unlock(&iter->rt_runtime_lock); |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 743 | |
| 744 | if (!want) |
| 745 | break; |
| 746 | } |
| 747 | |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 748 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 749 | /* |
| 750 | * We cannot be left wanting - that would mean some runtime |
| 751 | * leaked out of the system. |
| 752 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 753 | BUG_ON(want); |
| 754 | balanced: |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 755 | /* |
| 756 | * Disable all the borrow logic by pretending we have inf |
| 757 | * runtime - in which case borrowing doesn't make sense. |
| 758 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 759 | rt_rq->rt_runtime = RUNTIME_INF; |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 760 | rt_rq->rt_throttled = 0; |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 761 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
| 762 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
Kirill Tkhai | 99b6256 | 2014-06-25 12:19:48 +0400 | [diff] [blame] | 763 | |
| 764 | /* Make rt_rq available for pick_next_task() */ |
| 765 | sched_rt_rq_enqueue(rt_rq); |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 769 | static void __enable_runtime(struct rq *rq) |
| 770 | { |
Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 771 | rt_rq_iter_t iter; |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 772 | struct rt_rq *rt_rq; |
| 773 | |
| 774 | if (unlikely(!scheduler_running)) |
| 775 | return; |
| 776 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 777 | /* |
| 778 | * Reset each runqueue's bandwidth settings |
| 779 | */ |
Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 780 | for_each_rt_rq(rt_rq, iter, rq) { |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 781 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
| 782 | |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 783 | raw_spin_lock(&rt_b->rt_runtime_lock); |
| 784 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 785 | rt_rq->rt_runtime = rt_b->rt_runtime; |
| 786 | rt_rq->rt_time = 0; |
Zhang, Yanmin | baf2573 | 2008-09-09 11:26:33 +0800 | [diff] [blame] | 787 | rt_rq->rt_throttled = 0; |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 788 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
| 789 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
Juri Lelli | 269b26a | 2015-09-02 11:01:36 +0100 | [diff] [blame] | 793 | static void balance_runtime(struct rt_rq *rt_rq) |
Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 794 | { |
Peter Zijlstra | 4a6184c | 2011-10-06 22:39:14 +0200 | [diff] [blame] | 795 | if (!sched_feat(RT_RUNTIME_SHARE)) |
Juri Lelli | 269b26a | 2015-09-02 11:01:36 +0100 | [diff] [blame] | 796 | return; |
Peter Zijlstra | 4a6184c | 2011-10-06 22:39:14 +0200 | [diff] [blame] | 797 | |
Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 798 | if (rt_rq->rt_time > rt_rq->rt_runtime) { |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 799 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
Juri Lelli | 269b26a | 2015-09-02 11:01:36 +0100 | [diff] [blame] | 800 | do_balance_runtime(rt_rq); |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 801 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 802 | } |
Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 803 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 804 | #else /* !CONFIG_SMP */ |
Juri Lelli | 269b26a | 2015-09-02 11:01:36 +0100 | [diff] [blame] | 805 | static inline void balance_runtime(struct rt_rq *rt_rq) {} |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 806 | #endif /* CONFIG_SMP */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 807 | |
| 808 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun) |
| 809 | { |
Peter Zijlstra | 42c62a5 | 2011-10-18 22:03:48 +0200 | [diff] [blame] | 810 | int i, idle = 1, throttled = 0; |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 811 | const struct cpumask *span; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 812 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 813 | span = sched_rt_period_mask(); |
Mike Galbraith | e221d02 | 2012-08-07 10:02:38 +0200 | [diff] [blame] | 814 | #ifdef CONFIG_RT_GROUP_SCHED |
| 815 | /* |
| 816 | * FIXME: isolated CPUs should really leave the root task group, |
| 817 | * whether they are isolcpus or were isolated via cpusets, lest |
| 818 | * the timer run on a CPU which does not service all runqueues, |
| 819 | * potentially leaving other CPUs indefinitely throttled. If |
| 820 | * isolation is really required, the user will turn the throttle |
| 821 | * off to kill the perturbations it causes anyway. Meanwhile, |
| 822 | * this maintains functionality for boot and/or troubleshooting. |
| 823 | */ |
| 824 | if (rt_b == &root_task_group.rt_bandwidth) |
| 825 | span = cpu_online_mask; |
| 826 | #endif |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 827 | for_each_cpu(i, span) { |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 828 | int enqueue = 0; |
| 829 | struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i); |
| 830 | struct rq *rq = rq_of_rt_rq(rt_rq); |
| 831 | |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 832 | raw_spin_lock(&rq->lock); |
Davidlohr Bueso | 3d06325 | 2018-04-02 09:49:54 -0700 | [diff] [blame] | 833 | update_rq_clock(rq); |
| 834 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 835 | if (rt_rq->rt_time) { |
| 836 | u64 runtime; |
| 837 | |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 838 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 839 | if (rt_rq->rt_throttled) |
| 840 | balance_runtime(rt_rq); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 841 | runtime = rt_rq->rt_runtime; |
| 842 | rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime); |
| 843 | if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) { |
| 844 | rt_rq->rt_throttled = 0; |
| 845 | enqueue = 1; |
Mike Galbraith | 61eadef | 2011-04-29 08:36:50 +0200 | [diff] [blame] | 846 | |
| 847 | /* |
Peter Zijlstra | 9edfbfe | 2015-01-05 11:18:11 +0100 | [diff] [blame] | 848 | * When we're idle and a woken (rt) task is |
| 849 | * throttled check_preempt_curr() will set |
| 850 | * skip_update and the time between the wakeup |
| 851 | * and this unthrottle will get accounted as |
| 852 | * 'runtime'. |
Mike Galbraith | 61eadef | 2011-04-29 08:36:50 +0200 | [diff] [blame] | 853 | */ |
| 854 | if (rt_rq->rt_nr_running && rq->curr == rq->idle) |
Peter Zijlstra | 9edfbfe | 2015-01-05 11:18:11 +0100 | [diff] [blame] | 855 | rq_clock_skip_update(rq, false); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 856 | } |
| 857 | if (rt_rq->rt_time || rt_rq->rt_nr_running) |
| 858 | idle = 0; |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 859 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 860 | } else if (rt_rq->rt_nr_running) { |
Peter Zijlstra | 8a8cde1 | 2008-06-19 14:22:28 +0200 | [diff] [blame] | 861 | idle = 0; |
Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 862 | if (!rt_rq_throttled(rt_rq)) |
| 863 | enqueue = 1; |
| 864 | } |
Peter Zijlstra | 42c62a5 | 2011-10-18 22:03:48 +0200 | [diff] [blame] | 865 | if (rt_rq->rt_throttled) |
| 866 | throttled = 1; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 867 | |
| 868 | if (enqueue) |
| 869 | sched_rt_rq_enqueue(rt_rq); |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 870 | raw_spin_unlock(&rq->lock); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 871 | } |
| 872 | |
Peter Zijlstra | 42c62a5 | 2011-10-18 22:03:48 +0200 | [diff] [blame] | 873 | if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)) |
| 874 | return 1; |
| 875 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 876 | return idle; |
| 877 | } |
| 878 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 879 | static inline int rt_se_prio(struct sched_rt_entity *rt_se) |
| 880 | { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 881 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 882 | struct rt_rq *rt_rq = group_rt_rq(rt_se); |
| 883 | |
| 884 | if (rt_rq) |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 885 | return rt_rq->highest_prio.curr; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 886 | #endif |
| 887 | |
| 888 | return rt_task_of(rt_se)->prio; |
| 889 | } |
| 890 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 891 | static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 892 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 893 | u64 runtime = sched_rt_runtime(rt_rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 894 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 895 | if (rt_rq->rt_throttled) |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 896 | return rt_rq_throttled(rt_rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 897 | |
Shan Hai | 5b680fd | 2011-11-29 11:03:56 +0800 | [diff] [blame] | 898 | if (runtime >= sched_rt_period(rt_rq)) |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 899 | return 0; |
| 900 | |
Peter Zijlstra | b79f383 | 2008-06-19 14:22:25 +0200 | [diff] [blame] | 901 | balance_runtime(rt_rq); |
| 902 | runtime = sched_rt_runtime(rt_rq); |
| 903 | if (runtime == RUNTIME_INF) |
| 904 | return 0; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 905 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 906 | if (rt_rq->rt_time > runtime) { |
Peter Zijlstra | 7abc63b | 2011-10-18 22:03:48 +0200 | [diff] [blame] | 907 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
| 908 | |
| 909 | /* |
| 910 | * Don't actually throttle groups that have no runtime assigned |
| 911 | * but accrue some time due to boosting. |
| 912 | */ |
| 913 | if (likely(rt_b->rt_runtime)) { |
| 914 | rt_rq->rt_throttled = 1; |
John Stultz | c224815 | 2014-06-04 16:11:41 -0700 | [diff] [blame] | 915 | printk_deferred_once("sched: RT throttling activated\n"); |
Peter Zijlstra | 7abc63b | 2011-10-18 22:03:48 +0200 | [diff] [blame] | 916 | } else { |
| 917 | /* |
| 918 | * In case we did anyway, make it go away, |
| 919 | * replenishment is a joke, since it will replenish us |
| 920 | * with exactly 0 ns. |
| 921 | */ |
| 922 | rt_rq->rt_time = 0; |
| 923 | } |
| 924 | |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 925 | if (rt_rq_throttled(rt_rq)) { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 926 | sched_rt_rq_dequeue(rt_rq); |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 927 | return 1; |
| 928 | } |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 934 | /* |
| 935 | * Update the current task's runtime statistics. Skip current tasks that |
| 936 | * are not in our scheduling class. |
| 937 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 938 | static void update_curr_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 939 | { |
| 940 | struct task_struct *curr = rq->curr; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 941 | struct sched_rt_entity *rt_se = &curr->rt; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 942 | u64 delta_exec; |
| 943 | |
Peter Zijlstra | 06c3bc6 | 2011-02-02 13:19:48 +0100 | [diff] [blame] | 944 | if (curr->sched_class != &rt_sched_class) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 945 | return; |
| 946 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 947 | delta_exec = rq_clock_task(rq) - curr->se.exec_start; |
Kirill Tkhai | fc79e24 | 2013-01-30 16:50:36 +0400 | [diff] [blame] | 948 | if (unlikely((s64)delta_exec <= 0)) |
| 949 | return; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 950 | |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 951 | /* Kick cpufreq (see the comment in kernel/sched/sched.h). */ |
Rafael J. Wysocki | 12bde33 | 2016-08-10 03:11:17 +0200 | [diff] [blame] | 952 | cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_RT); |
Wanpeng Li | 594dd29 | 2016-04-22 17:07:24 +0800 | [diff] [blame] | 953 | |
Peter Zijlstra | 42c62a5 | 2011-10-18 22:03:48 +0200 | [diff] [blame] | 954 | schedstat_set(curr->se.statistics.exec_max, |
| 955 | max(curr->se.statistics.exec_max, delta_exec)); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 956 | |
| 957 | curr->se.sum_exec_runtime += delta_exec; |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 958 | account_group_exec_runtime(curr, delta_exec); |
| 959 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 960 | curr->se.exec_start = rq_clock_task(rq); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 961 | cpuacct_charge(curr, delta_exec); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 962 | |
Peter Zijlstra | e9e9250 | 2009-09-01 10:34:37 +0200 | [diff] [blame] | 963 | sched_rt_avg_update(rq, delta_exec); |
| 964 | |
Peter Zijlstra | 0b148fa | 2008-08-19 12:33:04 +0200 | [diff] [blame] | 965 | if (!rt_bandwidth_enabled()) |
| 966 | return; |
| 967 | |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 968 | for_each_sched_rt_entity(rt_se) { |
Giedrius Rekasius | 0b07939 | 2014-05-25 15:23:31 +0100 | [diff] [blame] | 969 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 970 | |
Peter Zijlstra | cc2991c | 2008-08-19 12:33:03 +0200 | [diff] [blame] | 971 | if (sched_rt_runtime(rt_rq) != RUNTIME_INF) { |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 972 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | cc2991c | 2008-08-19 12:33:03 +0200 | [diff] [blame] | 973 | rt_rq->rt_time += delta_exec; |
| 974 | if (sched_rt_runtime_exceeded(rt_rq)) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 975 | resched_curr(rq); |
Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 976 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | cc2991c | 2008-08-19 12:33:03 +0200 | [diff] [blame] | 977 | } |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 978 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 979 | } |
| 980 | |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 981 | static void |
| 982 | dequeue_top_rt_rq(struct rt_rq *rt_rq) |
| 983 | { |
| 984 | struct rq *rq = rq_of_rt_rq(rt_rq); |
| 985 | |
| 986 | BUG_ON(&rq->rt != rt_rq); |
| 987 | |
| 988 | if (!rt_rq->rt_queued) |
| 989 | return; |
| 990 | |
| 991 | BUG_ON(!rq->nr_running); |
| 992 | |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 993 | sub_nr_running(rq, rt_rq->rt_nr_running); |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 994 | rt_rq->rt_queued = 0; |
| 995 | } |
| 996 | |
| 997 | static void |
| 998 | enqueue_top_rt_rq(struct rt_rq *rt_rq) |
| 999 | { |
| 1000 | struct rq *rq = rq_of_rt_rq(rt_rq); |
| 1001 | |
| 1002 | BUG_ON(&rq->rt != rt_rq); |
| 1003 | |
| 1004 | if (rt_rq->rt_queued) |
| 1005 | return; |
| 1006 | if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running) |
| 1007 | return; |
| 1008 | |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 1009 | add_nr_running(rq, rt_rq->rt_nr_running); |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 1010 | rt_rq->rt_queued = 1; |
| 1011 | } |
| 1012 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1013 | #if defined CONFIG_SMP |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1014 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1015 | static void |
| 1016 | inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 1017 | { |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1018 | struct rq *rq = rq_of_rt_rq(rt_rq); |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1019 | |
Kirill Tkhai | 757dfca | 2013-11-27 19:59:13 +0400 | [diff] [blame] | 1020 | #ifdef CONFIG_RT_GROUP_SCHED |
| 1021 | /* |
| 1022 | * Change rq's cpupri only if rt_rq is the top queue. |
| 1023 | */ |
| 1024 | if (&rq->rt != rt_rq) |
| 1025 | return; |
| 1026 | #endif |
Steven Rostedt | 5181f4a4 | 2011-06-16 21:55:23 -0400 | [diff] [blame] | 1027 | if (rq->online && prio < prev_prio) |
| 1028 | cpupri_set(&rq->rd->cpupri, rq->cpu, prio); |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 1029 | } |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1030 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1031 | static void |
| 1032 | dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 1033 | { |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1034 | struct rq *rq = rq_of_rt_rq(rt_rq); |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1035 | |
Kirill Tkhai | 757dfca | 2013-11-27 19:59:13 +0400 | [diff] [blame] | 1036 | #ifdef CONFIG_RT_GROUP_SCHED |
| 1037 | /* |
| 1038 | * Change rq's cpupri only if rt_rq is the top queue. |
| 1039 | */ |
| 1040 | if (&rq->rt != rt_rq) |
| 1041 | return; |
| 1042 | #endif |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1043 | if (rq->online && rt_rq->highest_prio.curr != prev_prio) |
| 1044 | cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr); |
| 1045 | } |
| 1046 | |
| 1047 | #else /* CONFIG_SMP */ |
| 1048 | |
| 1049 | static inline |
| 1050 | void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} |
| 1051 | static inline |
| 1052 | void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} |
| 1053 | |
| 1054 | #endif /* CONFIG_SMP */ |
| 1055 | |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 1056 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1057 | static void |
| 1058 | inc_rt_prio(struct rt_rq *rt_rq, int prio) |
| 1059 | { |
| 1060 | int prev_prio = rt_rq->highest_prio.curr; |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 1061 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1062 | if (prio < prev_prio) |
| 1063 | rt_rq->highest_prio.curr = prio; |
| 1064 | |
| 1065 | inc_rt_prio_smp(rt_rq, prio, prev_prio); |
| 1066 | } |
| 1067 | |
| 1068 | static void |
| 1069 | dec_rt_prio(struct rt_rq *rt_rq, int prio) |
| 1070 | { |
| 1071 | int prev_prio = rt_rq->highest_prio.curr; |
| 1072 | |
| 1073 | if (rt_rq->rt_nr_running) { |
| 1074 | |
| 1075 | WARN_ON(prio < prev_prio); |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1076 | |
| 1077 | /* |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1078 | * This may have been our highest task, and therefore |
| 1079 | * we may have some recomputation to do |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1080 | */ |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1081 | if (prio == prev_prio) { |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1082 | struct rt_prio_array *array = &rt_rq->active; |
| 1083 | |
| 1084 | rt_rq->highest_prio.curr = |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 1085 | sched_find_first_bit(array->bitmap); |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1086 | } |
| 1087 | |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 1088 | } else |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1089 | rt_rq->highest_prio.curr = MAX_RT_PRIO; |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1090 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1091 | dec_rt_prio_smp(rt_rq, prio, prev_prio); |
| 1092 | } |
Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 1093 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1094 | #else |
| 1095 | |
| 1096 | static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {} |
| 1097 | static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {} |
| 1098 | |
| 1099 | #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */ |
| 1100 | |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1101 | #ifdef CONFIG_RT_GROUP_SCHED |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1102 | |
| 1103 | static void |
| 1104 | inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
| 1105 | { |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1106 | if (rt_se_boosted(rt_se)) |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 1107 | rt_rq->rt_nr_boosted++; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 1108 | |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 1109 | if (rt_rq->tg) |
| 1110 | start_rt_bandwidth(&rt_rq->tg->rt_bandwidth); |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static void |
| 1114 | dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
| 1115 | { |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 1116 | if (rt_se_boosted(rt_se)) |
| 1117 | rt_rq->rt_nr_boosted--; |
| 1118 | |
| 1119 | WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted); |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | #else /* CONFIG_RT_GROUP_SCHED */ |
| 1123 | |
| 1124 | static void |
| 1125 | inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
| 1126 | { |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1127 | start_rt_bandwidth(&def_rt_bandwidth); |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | static inline |
| 1131 | void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {} |
| 1132 | |
| 1133 | #endif /* CONFIG_RT_GROUP_SCHED */ |
| 1134 | |
| 1135 | static inline |
Kirill Tkhai | 22abdef | 2014-03-15 02:14:49 +0400 | [diff] [blame] | 1136 | unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se) |
| 1137 | { |
| 1138 | struct rt_rq *group_rq = group_rt_rq(rt_se); |
| 1139 | |
| 1140 | if (group_rq) |
| 1141 | return group_rq->rt_nr_running; |
| 1142 | else |
| 1143 | return 1; |
| 1144 | } |
| 1145 | |
| 1146 | static inline |
Frederic Weisbecker | 01d36d0 | 2015-11-04 18:17:10 +0100 | [diff] [blame] | 1147 | unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se) |
| 1148 | { |
| 1149 | struct rt_rq *group_rq = group_rt_rq(rt_se); |
| 1150 | struct task_struct *tsk; |
| 1151 | |
| 1152 | if (group_rq) |
| 1153 | return group_rq->rr_nr_running; |
| 1154 | |
| 1155 | tsk = rt_task_of(rt_se); |
| 1156 | |
| 1157 | return (tsk->policy == SCHED_RR) ? 1 : 0; |
| 1158 | } |
| 1159 | |
| 1160 | static inline |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1161 | void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
| 1162 | { |
| 1163 | int prio = rt_se_prio(rt_se); |
| 1164 | |
| 1165 | WARN_ON(!rt_prio(prio)); |
Kirill Tkhai | 22abdef | 2014-03-15 02:14:49 +0400 | [diff] [blame] | 1166 | rt_rq->rt_nr_running += rt_se_nr_running(rt_se); |
Frederic Weisbecker | 01d36d0 | 2015-11-04 18:17:10 +0100 | [diff] [blame] | 1167 | rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se); |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1168 | |
| 1169 | inc_rt_prio(rt_rq, prio); |
| 1170 | inc_rt_migration(rt_se, rt_rq); |
| 1171 | inc_rt_group(rt_se, rt_rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 1174 | static inline |
| 1175 | void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
| 1176 | { |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1177 | WARN_ON(!rt_prio(rt_se_prio(rt_se))); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1178 | WARN_ON(!rt_rq->rt_nr_running); |
Kirill Tkhai | 22abdef | 2014-03-15 02:14:49 +0400 | [diff] [blame] | 1179 | rt_rq->rt_nr_running -= rt_se_nr_running(rt_se); |
Frederic Weisbecker | 01d36d0 | 2015-11-04 18:17:10 +0100 | [diff] [blame] | 1180 | rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1181 | |
Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1182 | dec_rt_prio(rt_rq, rt_se_prio(rt_se)); |
| 1183 | dec_rt_migration(rt_se, rt_rq); |
| 1184 | dec_rt_group(rt_se, rt_rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1187 | /* |
| 1188 | * Change rt_se->run_list location unless SAVE && !MOVE |
| 1189 | * |
| 1190 | * assumes ENQUEUE/DEQUEUE flags match |
| 1191 | */ |
| 1192 | static inline bool move_entity(unsigned int flags) |
| 1193 | { |
| 1194 | if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE) |
| 1195 | return false; |
| 1196 | |
| 1197 | return true; |
| 1198 | } |
| 1199 | |
| 1200 | static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array) |
| 1201 | { |
| 1202 | list_del_init(&rt_se->run_list); |
| 1203 | |
| 1204 | if (list_empty(array->queue + rt_se_prio(rt_se))) |
| 1205 | __clear_bit(rt_se_prio(rt_se), array->bitmap); |
| 1206 | |
| 1207 | rt_se->on_list = 0; |
| 1208 | } |
| 1209 | |
| 1210 | static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1211 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1212 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
| 1213 | struct rt_prio_array *array = &rt_rq->active; |
| 1214 | struct rt_rq *group_rq = group_rt_rq(rt_se); |
Dmitry Adamushko | 20b6331 | 2008-06-11 00:58:30 +0200 | [diff] [blame] | 1215 | struct list_head *queue = array->queue + rt_se_prio(rt_se); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1216 | |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1217 | /* |
| 1218 | * Don't enqueue the group if its throttled, or when empty. |
| 1219 | * The latter is a consequence of the former when a child group |
| 1220 | * get throttled and the current group doesn't have any other |
| 1221 | * active members. |
| 1222 | */ |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1223 | if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) { |
| 1224 | if (rt_se->on_list) |
| 1225 | __delist_rt_entity(rt_se, array); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1226 | return; |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1227 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1228 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1229 | if (move_entity(flags)) { |
| 1230 | WARN_ON_ONCE(rt_se->on_list); |
| 1231 | if (flags & ENQUEUE_HEAD) |
| 1232 | list_add(&rt_se->run_list, queue); |
| 1233 | else |
| 1234 | list_add_tail(&rt_se->run_list, queue); |
| 1235 | |
| 1236 | __set_bit(rt_se_prio(rt_se), array->bitmap); |
| 1237 | rt_se->on_list = 1; |
| 1238 | } |
| 1239 | rt_se->on_rq = 1; |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1240 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1241 | inc_rt_tasks(rt_se, rt_rq); |
| 1242 | } |
| 1243 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1244 | static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1245 | { |
| 1246 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
| 1247 | struct rt_prio_array *array = &rt_rq->active; |
| 1248 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1249 | if (move_entity(flags)) { |
| 1250 | WARN_ON_ONCE(!rt_se->on_list); |
| 1251 | __delist_rt_entity(rt_se, array); |
| 1252 | } |
| 1253 | rt_se->on_rq = 0; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1254 | |
| 1255 | dec_rt_tasks(rt_se, rt_rq); |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * Because the prio of an upper entry depends on the lower |
| 1260 | * entries, we must remove entries top - down. |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1261 | */ |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1262 | static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1263 | { |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1264 | struct sched_rt_entity *back = NULL; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1265 | |
Peter Zijlstra | 58d6c2d | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1266 | for_each_sched_rt_entity(rt_se) { |
| 1267 | rt_se->back = back; |
| 1268 | back = rt_se; |
| 1269 | } |
| 1270 | |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 1271 | dequeue_top_rt_rq(rt_rq_of_se(back)); |
| 1272 | |
Peter Zijlstra | 58d6c2d | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1273 | for (rt_se = back; rt_se; rt_se = rt_se->back) { |
| 1274 | if (on_rt_rq(rt_se)) |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1275 | __dequeue_rt_entity(rt_se, flags); |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1279 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1280 | { |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 1281 | struct rq *rq = rq_of_rt_se(rt_se); |
| 1282 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1283 | dequeue_rt_stack(rt_se, flags); |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1284 | for_each_sched_rt_entity(rt_se) |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1285 | __enqueue_rt_entity(rt_se, flags); |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 1286 | enqueue_top_rt_rq(&rq->rt); |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1289 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1290 | { |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 1291 | struct rq *rq = rq_of_rt_se(rt_se); |
| 1292 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1293 | dequeue_rt_stack(rt_se, flags); |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1294 | |
| 1295 | for_each_sched_rt_entity(rt_se) { |
| 1296 | struct rt_rq *rt_rq = group_rt_rq(rt_se); |
| 1297 | |
| 1298 | if (rt_rq && rt_rq->rt_nr_running) |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1299 | __enqueue_rt_entity(rt_se, flags); |
Peter Zijlstra | 58d6c2d | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1300 | } |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 1301 | enqueue_top_rt_rq(&rq->rt); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * Adding/removing a task to/from a priority array: |
| 1306 | */ |
Thomas Gleixner | ea87bb7 | 2010-01-20 20:58:57 +0000 | [diff] [blame] | 1307 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1308 | enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1309 | { |
| 1310 | struct sched_rt_entity *rt_se = &p->rt; |
| 1311 | |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1312 | if (flags & ENQUEUE_WAKEUP) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1313 | rt_se->timeout = 0; |
| 1314 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1315 | enqueue_rt_entity(rt_se, flags); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 1316 | |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 1317 | if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1) |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1318 | enqueue_pushable_task(rq, p); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1321 | static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1322 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1323 | struct sched_rt_entity *rt_se = &p->rt; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1324 | |
| 1325 | update_curr_rt(rq); |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1326 | dequeue_rt_entity(rt_se, flags); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 1327 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1328 | dequeue_pushable_task(rq, p); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /* |
Richard Weinberger | 6068631 | 2011-11-12 18:07:57 +0100 | [diff] [blame] | 1332 | * Put task to the head or the end of the run list without the overhead of |
| 1333 | * dequeue followed by enqueue. |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1334 | */ |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1335 | static void |
| 1336 | requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1337 | { |
Ingo Molnar | 1cdad71 | 2008-06-19 09:09:15 +0200 | [diff] [blame] | 1338 | if (on_rt_rq(rt_se)) { |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1339 | struct rt_prio_array *array = &rt_rq->active; |
| 1340 | struct list_head *queue = array->queue + rt_se_prio(rt_se); |
| 1341 | |
| 1342 | if (head) |
| 1343 | list_move(&rt_se->run_list, queue); |
| 1344 | else |
| 1345 | list_move_tail(&rt_se->run_list, queue); |
Ingo Molnar | 1cdad71 | 2008-06-19 09:09:15 +0200 | [diff] [blame] | 1346 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1349 | static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1350 | { |
| 1351 | struct sched_rt_entity *rt_se = &p->rt; |
| 1352 | struct rt_rq *rt_rq; |
| 1353 | |
| 1354 | for_each_sched_rt_entity(rt_se) { |
| 1355 | rt_rq = rt_rq_of_se(rt_se); |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1356 | requeue_rt_entity(rt_rq, rt_se, head); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | static void yield_task_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1361 | { |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1362 | requeue_task_rt(rq, rq->curr, 0); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1363 | } |
| 1364 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1365 | #ifdef CONFIG_SMP |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1366 | static int find_lowest_rq(struct task_struct *task); |
| 1367 | |
Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 1368 | static int |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 1369 | select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags) |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1370 | { |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1371 | struct task_struct *curr; |
| 1372 | struct rq *rq; |
Steven Rostedt | c37495f | 2011-06-16 21:55:22 -0400 | [diff] [blame] | 1373 | |
| 1374 | /* For anything but wake ups, just return the task_cpu */ |
| 1375 | if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK) |
| 1376 | goto out; |
| 1377 | |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1378 | rq = cpu_rq(cpu); |
| 1379 | |
| 1380 | rcu_read_lock(); |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 1381 | curr = READ_ONCE(rq->curr); /* unlocked access */ |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1382 | |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1383 | /* |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1384 | * If the current task on @p's runqueue is an RT task, then |
Steven Rostedt | e1f47d8 | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 1385 | * try to see if we can wake this RT task up on another |
| 1386 | * runqueue. Otherwise simply start this RT task |
| 1387 | * on its current runqueue. |
| 1388 | * |
Steven Rostedt | 43fa546 | 2010-09-20 22:40:03 -0400 | [diff] [blame] | 1389 | * We want to avoid overloading runqueues. If the woken |
| 1390 | * task is a higher priority, then it will stay on this CPU |
| 1391 | * and the lower prio task should be moved to another CPU. |
| 1392 | * Even though this will probably make the lower prio task |
| 1393 | * lose its cache, we do not want to bounce a higher task |
| 1394 | * around just because it gave up its CPU, perhaps for a |
| 1395 | * lock? |
| 1396 | * |
| 1397 | * For equal prio tasks, we just let the scheduler sort it out. |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1398 | * |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1399 | * Otherwise, just let it ride on the affined RQ and the |
| 1400 | * post-schedule router will push the preempted task away |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1401 | * |
| 1402 | * This test is optimistic, if we get it wrong the load-balancer |
| 1403 | * will have to sort it out. |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1404 | */ |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1405 | if (curr && unlikely(rt_task(curr)) && |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 1406 | (tsk_nr_cpus_allowed(curr) < 2 || |
Shawn Bohrer | 6bfa687 | 2013-10-04 14:24:53 -0500 | [diff] [blame] | 1407 | curr->prio <= p->prio)) { |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1408 | int target = find_lowest_rq(p); |
| 1409 | |
Tim Chen | 80e3d87 | 2014-12-12 15:38:12 -0800 | [diff] [blame] | 1410 | /* |
| 1411 | * Don't bother moving it if the destination CPU is |
| 1412 | * not running a lower priority task. |
| 1413 | */ |
| 1414 | if (target != -1 && |
| 1415 | p->prio < cpu_rq(target)->rt.highest_prio.curr) |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1416 | cpu = target; |
| 1417 | } |
| 1418 | rcu_read_unlock(); |
| 1419 | |
Steven Rostedt | c37495f | 2011-06-16 21:55:22 -0400 | [diff] [blame] | 1420 | out: |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1421 | return cpu; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1422 | } |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1423 | |
| 1424 | static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p) |
| 1425 | { |
Wanpeng Li | 308a623 | 2014-10-31 06:39:31 +0800 | [diff] [blame] | 1426 | /* |
| 1427 | * Current can't be migrated, useless to reschedule, |
| 1428 | * let's hope p can move out. |
| 1429 | */ |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 1430 | if (tsk_nr_cpus_allowed(rq->curr) == 1 || |
Wanpeng Li | 308a623 | 2014-10-31 06:39:31 +0800 | [diff] [blame] | 1431 | !cpupri_find(&rq->rd->cpupri, rq->curr, NULL)) |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1432 | return; |
| 1433 | |
Wanpeng Li | 308a623 | 2014-10-31 06:39:31 +0800 | [diff] [blame] | 1434 | /* |
| 1435 | * p is migratable, so let's not schedule it and |
| 1436 | * see if it is pushed or pulled somewhere else. |
| 1437 | */ |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 1438 | if (tsk_nr_cpus_allowed(p) != 1 |
Rusty Russell | 13b8bd0 | 2009-03-25 15:01:22 +1030 | [diff] [blame] | 1439 | && cpupri_find(&rq->rd->cpupri, p, NULL)) |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1440 | return; |
| 1441 | |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1442 | /* |
| 1443 | * There appears to be other cpus that can accept |
| 1444 | * current and none to run 'p', so lets reschedule |
| 1445 | * to try and push current away: |
| 1446 | */ |
| 1447 | requeue_task_rt(rq, p, 1); |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 1448 | resched_curr(rq); |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1449 | } |
| 1450 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1451 | #endif /* CONFIG_SMP */ |
| 1452 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1453 | /* |
| 1454 | * Preempt the current task with a newly woken task if needed: |
| 1455 | */ |
Peter Zijlstra | 7d47872 | 2009-09-14 19:55:44 +0200 | [diff] [blame] | 1456 | static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1457 | { |
Gregory Haskins | 45c01e8 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 1458 | if (p->prio < rq->curr->prio) { |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 1459 | resched_curr(rq); |
Gregory Haskins | 45c01e8 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | |
| 1463 | #ifdef CONFIG_SMP |
| 1464 | /* |
| 1465 | * If: |
| 1466 | * |
| 1467 | * - the newly woken task is of equal priority to the current task |
| 1468 | * - the newly woken task is non-migratable while current is migratable |
| 1469 | * - current will be preempted on the next reschedule |
| 1470 | * |
| 1471 | * we should check to see if current can readily move to a different |
| 1472 | * cpu. If so, we will reschedule to allow the push logic to try |
| 1473 | * to move current somewhere else, making room for our non-migratable |
| 1474 | * task. |
| 1475 | */ |
Hillf Danton | 8dd0de8 | 2011-06-14 18:36:24 -0400 | [diff] [blame] | 1476 | if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr)) |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1477 | check_preempt_equal_prio(rq, p); |
Gregory Haskins | 45c01e8 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 1478 | #endif |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1481 | static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq, |
| 1482 | struct rt_rq *rt_rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1483 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1484 | struct rt_prio_array *array = &rt_rq->active; |
| 1485 | struct sched_rt_entity *next = NULL; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1486 | struct list_head *queue; |
| 1487 | int idx; |
| 1488 | |
| 1489 | idx = sched_find_first_bit(array->bitmap); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1490 | BUG_ON(idx >= MAX_RT_PRIO); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1491 | |
| 1492 | queue = array->queue + idx; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1493 | next = list_entry(queue->next, struct sched_rt_entity, run_list); |
Dmitry Adamushko | 326587b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 1494 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1495 | return next; |
| 1496 | } |
| 1497 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1498 | static struct task_struct *_pick_next_task_rt(struct rq *rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1499 | { |
| 1500 | struct sched_rt_entity *rt_se; |
| 1501 | struct task_struct *p; |
Peter Zijlstra | 606dba2 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 1502 | struct rt_rq *rt_rq = &rq->rt; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1503 | |
| 1504 | do { |
| 1505 | rt_se = pick_next_rt_entity(rq, rt_rq); |
Dmitry Adamushko | 326587b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 1506 | BUG_ON(!rt_se); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1507 | rt_rq = group_rt_rq(rt_se); |
| 1508 | } while (rt_rq); |
| 1509 | |
| 1510 | p = rt_task_of(rt_se); |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 1511 | p->se.exec_start = rq_clock_task(rq); |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1512 | |
| 1513 | return p; |
| 1514 | } |
| 1515 | |
Peter Zijlstra | 606dba2 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 1516 | static struct task_struct * |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 1517 | pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie) |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1518 | { |
Peter Zijlstra | 606dba2 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 1519 | struct task_struct *p; |
| 1520 | struct rt_rq *rt_rq = &rq->rt; |
| 1521 | |
Peter Zijlstra | 37e117c | 2014-02-14 12:25:08 +0100 | [diff] [blame] | 1522 | if (need_pull_rt_task(rq, prev)) { |
Peter Zijlstra | cbce1a6 | 2015-06-11 14:46:54 +0200 | [diff] [blame] | 1523 | /* |
| 1524 | * This is OK, because current is on_cpu, which avoids it being |
| 1525 | * picked for load-balance and preemption/IRQs are still |
| 1526 | * disabled avoiding further scheduler activity on it and we're |
| 1527 | * being very careful to re-start the picking loop. |
| 1528 | */ |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 1529 | lockdep_unpin_lock(&rq->lock, cookie); |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 1530 | pull_rt_task(rq); |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 1531 | lockdep_repin_lock(&rq->lock, cookie); |
Peter Zijlstra | 37e117c | 2014-02-14 12:25:08 +0100 | [diff] [blame] | 1532 | /* |
| 1533 | * pull_rt_task() can drop (and re-acquire) rq->lock; this |
Kirill Tkhai | a1d9a32 | 2014-04-10 17:38:36 +0400 | [diff] [blame] | 1534 | * means a dl or stop task can slip in, in which case we need |
| 1535 | * to re-start task selection. |
Peter Zijlstra | 37e117c | 2014-02-14 12:25:08 +0100 | [diff] [blame] | 1536 | */ |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 1537 | if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) || |
Kirill Tkhai | a1d9a32 | 2014-04-10 17:38:36 +0400 | [diff] [blame] | 1538 | rq->dl.dl_nr_running)) |
Peter Zijlstra | 37e117c | 2014-02-14 12:25:08 +0100 | [diff] [blame] | 1539 | return RETRY_TASK; |
| 1540 | } |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 1541 | |
Kirill Tkhai | 734ff2a | 2014-03-04 19:25:46 +0400 | [diff] [blame] | 1542 | /* |
| 1543 | * We may dequeue prev's rt_rq in put_prev_task(). |
| 1544 | * So, we update time before rt_nr_running check. |
| 1545 | */ |
| 1546 | if (prev->sched_class == &rt_sched_class) |
| 1547 | update_curr_rt(rq); |
| 1548 | |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 1549 | if (!rt_rq->rt_queued) |
Peter Zijlstra | 606dba2 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 1550 | return NULL; |
| 1551 | |
Peter Zijlstra | 3f1d2a3 | 2014-02-12 10:49:30 +0100 | [diff] [blame] | 1552 | put_prev_task(rq, prev); |
Peter Zijlstra | 606dba2 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 1553 | |
| 1554 | p = _pick_next_task_rt(rq); |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1555 | |
| 1556 | /* The running task is never eligible for pushing */ |
Kirill Tkhai | f3f1768 | 2014-09-12 17:42:01 +0400 | [diff] [blame] | 1557 | dequeue_pushable_task(rq, p); |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1558 | |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 1559 | queue_push_tasks(rq); |
Gregory Haskins | 3f029d3 | 2009-07-29 11:08:47 -0400 | [diff] [blame] | 1560 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1561 | return p; |
| 1562 | } |
| 1563 | |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1564 | static void put_prev_task_rt(struct rq *rq, struct task_struct *p) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1565 | { |
Ingo Molnar | f1e14ef | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1566 | update_curr_rt(rq); |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1567 | |
| 1568 | /* |
| 1569 | * The previous task needs to be made eligible for pushing |
| 1570 | * if it is still active |
| 1571 | */ |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 1572 | if (on_rt_rq(&p->rt) && tsk_nr_cpus_allowed(p) > 1) |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1573 | enqueue_pushable_task(rq, p); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1576 | #ifdef CONFIG_SMP |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1577 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1578 | /* Only try algorithms three times */ |
| 1579 | #define RT_MAX_TRIES 3 |
| 1580 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1581 | static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu) |
| 1582 | { |
| 1583 | if (!task_running(rq, p) && |
Kirill Tkhai | 60334ca | 2013-01-31 18:56:17 +0400 | [diff] [blame] | 1584 | cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1585 | return 1; |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
Kirill Tkhai | e23ee74 | 2013-06-07 15:37:43 -0400 | [diff] [blame] | 1589 | /* |
| 1590 | * Return the highest pushable rq's task, which is suitable to be executed |
| 1591 | * on the cpu, NULL otherwise |
| 1592 | */ |
| 1593 | static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1594 | { |
Kirill Tkhai | e23ee74 | 2013-06-07 15:37:43 -0400 | [diff] [blame] | 1595 | struct plist_head *head = &rq->rt.pushable_tasks; |
| 1596 | struct task_struct *p; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1597 | |
Kirill Tkhai | e23ee74 | 2013-06-07 15:37:43 -0400 | [diff] [blame] | 1598 | if (!has_pushable_tasks(rq)) |
| 1599 | return NULL; |
Peter Zijlstra | 3d07467 | 2010-03-10 17:07:24 +0100 | [diff] [blame] | 1600 | |
Kirill Tkhai | e23ee74 | 2013-06-07 15:37:43 -0400 | [diff] [blame] | 1601 | plist_for_each_entry(p, head, pushable_tasks) { |
| 1602 | if (pick_rt_task(rq, p, cpu)) |
| 1603 | return p; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1604 | } |
| 1605 | |
Kirill Tkhai | e23ee74 | 2013-06-07 15:37:43 -0400 | [diff] [blame] | 1606 | return NULL; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1607 | } |
| 1608 | |
Rusty Russell | 0e3900e | 2008-11-25 02:35:13 +1030 | [diff] [blame] | 1609 | static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1610 | |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1611 | static int find_lowest_rq(struct task_struct *task) |
| 1612 | { |
| 1613 | struct sched_domain *sd; |
Christoph Lameter | 4ba2968 | 2014-08-26 19:12:21 -0500 | [diff] [blame] | 1614 | struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask); |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1615 | int this_cpu = smp_processor_id(); |
| 1616 | int cpu = task_cpu(task); |
| 1617 | |
Steven Rostedt | 0da938c | 2011-06-14 18:36:25 -0400 | [diff] [blame] | 1618 | /* Make sure the mask is initialized first */ |
| 1619 | if (unlikely(!lowest_mask)) |
| 1620 | return -1; |
| 1621 | |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 1622 | if (tsk_nr_cpus_allowed(task) == 1) |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1623 | return -1; /* No other targets possible */ |
| 1624 | |
| 1625 | if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask)) |
Gregory Haskins | 06f90db | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 1626 | return -1; /* No targets found */ |
| 1627 | |
| 1628 | /* |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1629 | * At this point we have built a mask of cpus representing the |
| 1630 | * lowest priority tasks in the system. Now we want to elect |
| 1631 | * the best one based on our affinity and topology. |
| 1632 | * |
| 1633 | * We prioritize the last cpu that the task executed on since |
| 1634 | * it is most likely cache-hot in that location. |
| 1635 | */ |
Rusty Russell | 96f874e | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1636 | if (cpumask_test_cpu(cpu, lowest_mask)) |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1637 | return cpu; |
| 1638 | |
| 1639 | /* |
| 1640 | * Otherwise, we consult the sched_domains span maps to figure |
| 1641 | * out which cpu is logically closest to our hot cache data. |
| 1642 | */ |
Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1643 | if (!cpumask_test_cpu(this_cpu, lowest_mask)) |
| 1644 | this_cpu = -1; /* Skip this_cpu opt if not among lowest */ |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1645 | |
Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1646 | rcu_read_lock(); |
Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1647 | for_each_domain(cpu, sd) { |
| 1648 | if (sd->flags & SD_WAKE_AFFINE) { |
| 1649 | int best_cpu; |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1650 | |
Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1651 | /* |
| 1652 | * "this_cpu" is cheaper to preempt than a |
| 1653 | * remote processor. |
| 1654 | */ |
| 1655 | if (this_cpu != -1 && |
Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1656 | cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { |
| 1657 | rcu_read_unlock(); |
Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1658 | return this_cpu; |
Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1659 | } |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1660 | |
Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1661 | best_cpu = cpumask_first_and(lowest_mask, |
| 1662 | sched_domain_span(sd)); |
Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1663 | if (best_cpu < nr_cpu_ids) { |
| 1664 | rcu_read_unlock(); |
Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1665 | return best_cpu; |
Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1666 | } |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1667 | } |
| 1668 | } |
Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1669 | rcu_read_unlock(); |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1670 | |
| 1671 | /* |
| 1672 | * And finally, if there were no matches within the domains |
| 1673 | * just give the caller *something* to work with from the compatible |
| 1674 | * locations. |
| 1675 | */ |
Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1676 | if (this_cpu != -1) |
| 1677 | return this_cpu; |
| 1678 | |
| 1679 | cpu = cpumask_any(lowest_mask); |
| 1680 | if (cpu < nr_cpu_ids) |
| 1681 | return cpu; |
| 1682 | return -1; |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1683 | } |
| 1684 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1685 | /* Will lock the rq it finds */ |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1686 | static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1687 | { |
| 1688 | struct rq *lowest_rq = NULL; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1689 | int tries; |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1690 | int cpu; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1691 | |
| 1692 | for (tries = 0; tries < RT_MAX_TRIES; tries++) { |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1693 | cpu = find_lowest_rq(task); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1694 | |
Gregory Haskins | 2de0b46 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1695 | if ((cpu == -1) || (cpu == rq->cpu)) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1696 | break; |
| 1697 | |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1698 | lowest_rq = cpu_rq(cpu); |
| 1699 | |
Tim Chen | 80e3d87 | 2014-12-12 15:38:12 -0800 | [diff] [blame] | 1700 | if (lowest_rq->rt.highest_prio.curr <= task->prio) { |
| 1701 | /* |
| 1702 | * Target rq has tasks of equal or higher priority, |
| 1703 | * retrying does not release any lock and is unlikely |
| 1704 | * to yield a different result. |
| 1705 | */ |
| 1706 | lowest_rq = NULL; |
| 1707 | break; |
| 1708 | } |
| 1709 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1710 | /* if the prio of this runqueue changed, try again */ |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1711 | if (double_lock_balance(rq, lowest_rq)) { |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1712 | /* |
| 1713 | * We had to unlock the run queue. In |
| 1714 | * the mean time, task could have |
| 1715 | * migrated already or had its affinity changed. |
| 1716 | * Also make sure that it wasn't scheduled on its rq. |
| 1717 | */ |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1718 | if (unlikely(task_rq(task) != rq || |
Rusty Russell | 96f874e | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1719 | !cpumask_test_cpu(lowest_rq->cpu, |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 1720 | tsk_cpus_allowed(task)) || |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1721 | task_running(rq, task) || |
Xunlei Pang | 13b5ab0 | 2016-05-09 12:11:31 +0800 | [diff] [blame] | 1722 | !rt_task(task) || |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 1723 | !task_on_rq_queued(task))) { |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1724 | |
Peter Zijlstra | 7f1b439 | 2012-05-17 21:19:46 +0200 | [diff] [blame] | 1725 | double_unlock_balance(rq, lowest_rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1726 | lowest_rq = NULL; |
| 1727 | break; |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | /* If this rq is still suitable use it. */ |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1732 | if (lowest_rq->rt.highest_prio.curr > task->prio) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1733 | break; |
| 1734 | |
| 1735 | /* try again */ |
Peter Zijlstra | 1b12bbc | 2008-08-11 09:30:22 +0200 | [diff] [blame] | 1736 | double_unlock_balance(rq, lowest_rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1737 | lowest_rq = NULL; |
| 1738 | } |
| 1739 | |
| 1740 | return lowest_rq; |
| 1741 | } |
| 1742 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1743 | static struct task_struct *pick_next_pushable_task(struct rq *rq) |
| 1744 | { |
| 1745 | struct task_struct *p; |
| 1746 | |
| 1747 | if (!has_pushable_tasks(rq)) |
| 1748 | return NULL; |
| 1749 | |
| 1750 | p = plist_first_entry(&rq->rt.pushable_tasks, |
| 1751 | struct task_struct, pushable_tasks); |
| 1752 | |
| 1753 | BUG_ON(rq->cpu != task_cpu(p)); |
| 1754 | BUG_ON(task_current(rq, p)); |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 1755 | BUG_ON(tsk_nr_cpus_allowed(p) <= 1); |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1756 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 1757 | BUG_ON(!task_on_rq_queued(p)); |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1758 | BUG_ON(!rt_task(p)); |
| 1759 | |
| 1760 | return p; |
| 1761 | } |
| 1762 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1763 | /* |
| 1764 | * If the current CPU has more than one RT task, see if the non |
| 1765 | * running task can migrate over to a CPU that is running a task |
| 1766 | * of lesser priority. |
| 1767 | */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1768 | static int push_rt_task(struct rq *rq) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1769 | { |
| 1770 | struct task_struct *next_task; |
| 1771 | struct rq *lowest_rq; |
Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1772 | int ret = 0; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1773 | |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 1774 | if (!rq->rt.overloaded) |
| 1775 | return 0; |
| 1776 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1777 | next_task = pick_next_pushable_task(rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1778 | if (!next_task) |
| 1779 | return 0; |
| 1780 | |
Peter Zijlstra | 4924627 | 2010-10-17 21:46:10 +0200 | [diff] [blame] | 1781 | retry: |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1782 | if (unlikely(next_task == rq->curr)) { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1783 | WARN_ON(1); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1784 | return 0; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1785 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1786 | |
| 1787 | /* |
| 1788 | * It's possible that the next_task slipped in of |
| 1789 | * higher priority than current. If that's the case |
| 1790 | * just reschedule current. |
| 1791 | */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1792 | if (unlikely(next_task->prio < rq->curr->prio)) { |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 1793 | resched_curr(rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1794 | return 0; |
| 1795 | } |
| 1796 | |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1797 | /* We might release rq lock */ |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1798 | get_task_struct(next_task); |
| 1799 | |
| 1800 | /* find_lock_lowest_rq locks the rq if found */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1801 | lowest_rq = find_lock_lowest_rq(next_task, rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1802 | if (!lowest_rq) { |
| 1803 | struct task_struct *task; |
| 1804 | /* |
Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1805 | * find_lock_lowest_rq releases rq->lock |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1806 | * so it is possible that next_task has migrated. |
| 1807 | * |
| 1808 | * We need to make sure that the task is still on the same |
| 1809 | * run-queue and is also still the next task eligible for |
| 1810 | * pushing. |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1811 | */ |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1812 | task = pick_next_pushable_task(rq); |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1813 | if (task_cpu(next_task) == rq->cpu && task == next_task) { |
| 1814 | /* |
Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1815 | * The task hasn't migrated, and is still the next |
| 1816 | * eligible task, but we failed to find a run-queue |
| 1817 | * to push it to. Do not retry in this case, since |
| 1818 | * other cpus will pull from us when ready. |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1819 | */ |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1820 | goto out; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1821 | } |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1822 | |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1823 | if (!task) |
| 1824 | /* No more tasks, just exit */ |
| 1825 | goto out; |
| 1826 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1827 | /* |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1828 | * Something has shifted, try again. |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1829 | */ |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1830 | put_task_struct(next_task); |
| 1831 | next_task = task; |
| 1832 | goto retry; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1833 | } |
| 1834 | |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1835 | deactivate_task(rq, next_task, 0); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1836 | set_task_cpu(next_task, lowest_rq->cpu); |
| 1837 | activate_task(lowest_rq, next_task, 0); |
Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1838 | ret = 1; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1839 | |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 1840 | resched_curr(lowest_rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1841 | |
Peter Zijlstra | 1b12bbc | 2008-08-11 09:30:22 +0200 | [diff] [blame] | 1842 | double_unlock_balance(rq, lowest_rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1843 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1844 | out: |
| 1845 | put_task_struct(next_task); |
| 1846 | |
Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1847 | return ret; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1848 | } |
| 1849 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1850 | static void push_rt_tasks(struct rq *rq) |
| 1851 | { |
| 1852 | /* push_rt_task will return true if it moved an RT */ |
| 1853 | while (push_rt_task(rq)) |
| 1854 | ; |
| 1855 | } |
| 1856 | |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1857 | #ifdef HAVE_RT_PUSH_IPI |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1858 | |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1859 | /* |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1860 | * When a high priority task schedules out from a CPU and a lower priority |
| 1861 | * task is scheduled in, a check is made to see if there's any RT tasks |
| 1862 | * on other CPUs that are waiting to run because a higher priority RT task |
| 1863 | * is currently running on its CPU. In this case, the CPU with multiple RT |
| 1864 | * tasks queued on it (overloaded) needs to be notified that a CPU has opened |
| 1865 | * up that may be able to run one of its non-running queued RT tasks. |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1866 | * |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1867 | * All CPUs with overloaded RT tasks need to be notified as there is currently |
| 1868 | * no way to know which of these CPUs have the highest priority task waiting |
| 1869 | * to run. Instead of trying to take a spinlock on each of these CPUs, |
| 1870 | * which has shown to cause large latency when done on machines with many |
| 1871 | * CPUs, sending an IPI to the CPUs to have them push off the overloaded |
| 1872 | * RT tasks waiting to run. |
| 1873 | * |
| 1874 | * Just sending an IPI to each of the CPUs is also an issue, as on large |
| 1875 | * count CPU machines, this can cause an IPI storm on a CPU, especially |
| 1876 | * if its the only CPU with multiple RT tasks queued, and a large number |
| 1877 | * of CPUs scheduling a lower priority task at the same time. |
| 1878 | * |
| 1879 | * Each root domain has its own irq work function that can iterate over |
| 1880 | * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT |
| 1881 | * tassk must be checked if there's one or many CPUs that are lowering |
| 1882 | * their priority, there's a single irq work iterator that will try to |
| 1883 | * push off RT tasks that are waiting to run. |
| 1884 | * |
| 1885 | * When a CPU schedules a lower priority task, it will kick off the |
| 1886 | * irq work iterator that will jump to each CPU with overloaded RT tasks. |
| 1887 | * As it only takes the first CPU that schedules a lower priority task |
| 1888 | * to start the process, the rto_start variable is incremented and if |
| 1889 | * the atomic result is one, then that CPU will try to take the rto_lock. |
| 1890 | * This prevents high contention on the lock as the process handles all |
| 1891 | * CPUs scheduling lower priority tasks. |
| 1892 | * |
| 1893 | * All CPUs that are scheduling a lower priority task will increment the |
| 1894 | * rt_loop_next variable. This will make sure that the irq work iterator |
| 1895 | * checks all RT overloaded CPUs whenever a CPU schedules a new lower |
| 1896 | * priority task, even if the iterator is in the middle of a scan. Incrementing |
| 1897 | * the rt_loop_next will cause the iterator to perform another scan. |
| 1898 | * |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1899 | */ |
Steven Rostedt (VMware) | 1c67998 | 2018-01-23 20:45:37 -0500 | [diff] [blame] | 1900 | static int rto_next_cpu(struct root_domain *rd) |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1901 | { |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1902 | int next; |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1903 | int cpu; |
| 1904 | |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1905 | /* |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1906 | * When starting the IPI RT pushing, the rto_cpu is set to -1, |
| 1907 | * rt_next_cpu() will simply return the first CPU found in |
| 1908 | * the rto_mask. |
| 1909 | * |
| 1910 | * If rto_next_cpu() is called with rto_cpu is a valid cpu, it |
| 1911 | * will return the next CPU found in the rto_mask. |
| 1912 | * |
| 1913 | * If there are no more CPUs left in the rto_mask, then a check is made |
| 1914 | * against rto_loop and rto_loop_next. rto_loop is only updated with |
| 1915 | * the rto_lock held, but any CPU may increment the rto_loop_next |
| 1916 | * without any locking. |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1917 | */ |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1918 | for (;;) { |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1919 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1920 | /* When rto_cpu is -1 this acts like cpumask_first() */ |
| 1921 | cpu = cpumask_next(rd->rto_cpu, rd->rto_mask); |
| 1922 | |
| 1923 | rd->rto_cpu = cpu; |
| 1924 | |
| 1925 | if (cpu < nr_cpu_ids) |
| 1926 | return cpu; |
| 1927 | |
| 1928 | rd->rto_cpu = -1; |
| 1929 | |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1930 | /* |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1931 | * ACQUIRE ensures we see the @rto_mask changes |
| 1932 | * made prior to the @next value observed. |
| 1933 | * |
| 1934 | * Matches WMB in rt_set_overload(). |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1935 | */ |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1936 | next = atomic_read_acquire(&rd->rto_loop_next); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1937 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1938 | if (rd->rto_loop == next) |
| 1939 | break; |
| 1940 | |
| 1941 | rd->rto_loop = next; |
| 1942 | } |
| 1943 | |
| 1944 | return -1; |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1945 | } |
| 1946 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1947 | static inline bool rto_start_trylock(atomic_t *v) |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1948 | { |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1949 | return !atomic_cmpxchg_acquire(v, 0, 1); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1950 | } |
| 1951 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1952 | static inline void rto_start_unlock(atomic_t *v) |
| 1953 | { |
| 1954 | atomic_set_release(v, 0); |
| 1955 | } |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1956 | |
| 1957 | static void tell_cpu_to_push(struct rq *rq) |
| 1958 | { |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1959 | int cpu = -1; |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1960 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1961 | /* Keep the loop going if the IPI is currently active */ |
| 1962 | atomic_inc(&rq->rd->rto_loop_next); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1963 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1964 | /* Only one CPU can initiate a loop at a time */ |
| 1965 | if (!rto_start_trylock(&rq->rd->rto_loop_start)) |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1966 | return; |
| 1967 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1968 | raw_spin_lock(&rq->rd->rto_lock); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1969 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1970 | /* |
| 1971 | * The rto_cpu is updated under the lock, if it has a valid cpu |
| 1972 | * then the IPI is still running and will continue due to the |
| 1973 | * update to loop_next, and nothing needs to be done here. |
| 1974 | * Otherwise it is finishing up and an ipi needs to be sent. |
| 1975 | */ |
| 1976 | if (rq->rd->rto_cpu < 0) |
Steven Rostedt (VMware) | 1c67998 | 2018-01-23 20:45:37 -0500 | [diff] [blame] | 1977 | cpu = rto_next_cpu(rq->rd); |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1978 | |
| 1979 | raw_spin_unlock(&rq->rd->rto_lock); |
| 1980 | |
| 1981 | rto_start_unlock(&rq->rd->rto_loop_start); |
| 1982 | |
Steven Rostedt (VMware) | a384e54 | 2018-01-23 20:45:38 -0500 | [diff] [blame] | 1983 | if (cpu >= 0) { |
| 1984 | /* Make sure the rd does not get freed while pushing */ |
| 1985 | sched_get_rd(rq->rd); |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1986 | irq_work_queue_on(&rq->rd->rto_push_work, cpu); |
Steven Rostedt (VMware) | a384e54 | 2018-01-23 20:45:38 -0500 | [diff] [blame] | 1987 | } |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* Called from hardirq context */ |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1991 | void rto_push_irq_work_func(struct irq_work *work) |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1992 | { |
Steven Rostedt (VMware) | 1c67998 | 2018-01-23 20:45:37 -0500 | [diff] [blame] | 1993 | struct root_domain *rd = |
| 1994 | container_of(work, struct root_domain, rto_push_work); |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1995 | struct rq *rq; |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1996 | int cpu; |
| 1997 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 1998 | rq = this_rq(); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 1999 | |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 2000 | /* |
| 2001 | * We do not need to grab the lock to check for has_pushable_tasks. |
| 2002 | * When it gets updated, a check is made if a push is possible. |
| 2003 | */ |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2004 | if (has_pushable_tasks(rq)) { |
| 2005 | raw_spin_lock(&rq->lock); |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 2006 | push_rt_tasks(rq); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2007 | raw_spin_unlock(&rq->lock); |
| 2008 | } |
| 2009 | |
Steven Rostedt (VMware) | 1c67998 | 2018-01-23 20:45:37 -0500 | [diff] [blame] | 2010 | raw_spin_lock(&rd->rto_lock); |
Steven Rostedt (Red Hat) | 1c37ff7 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 2011 | |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2012 | /* Pass the IPI to the next rt overloaded queue */ |
Steven Rostedt (VMware) | 1c67998 | 2018-01-23 20:45:37 -0500 | [diff] [blame] | 2013 | cpu = rto_next_cpu(rd); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2014 | |
Steven Rostedt (VMware) | 1c67998 | 2018-01-23 20:45:37 -0500 | [diff] [blame] | 2015 | raw_spin_unlock(&rd->rto_lock); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2016 | |
Steven Rostedt (VMware) | a384e54 | 2018-01-23 20:45:38 -0500 | [diff] [blame] | 2017 | if (cpu < 0) { |
| 2018 | sched_put_rd(rd); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2019 | return; |
Steven Rostedt (VMware) | a384e54 | 2018-01-23 20:45:38 -0500 | [diff] [blame] | 2020 | } |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2021 | |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2022 | /* Try the next RT overloaded CPU */ |
Steven Rostedt (VMware) | 1c67998 | 2018-01-23 20:45:37 -0500 | [diff] [blame] | 2023 | irq_work_queue_on(&rd->rto_push_work, cpu); |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2024 | } |
| 2025 | #endif /* HAVE_RT_PUSH_IPI */ |
| 2026 | |
Peter Zijlstra | 8046d68 | 2015-06-11 14:46:40 +0200 | [diff] [blame] | 2027 | static void pull_rt_task(struct rq *this_rq) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2028 | { |
Peter Zijlstra | 8046d68 | 2015-06-11 14:46:40 +0200 | [diff] [blame] | 2029 | int this_cpu = this_rq->cpu, cpu; |
| 2030 | bool resched = false; |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 2031 | struct task_struct *p; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2032 | struct rq *src_rq; |
Steven Rostedt | d266817 | 2017-12-02 13:04:54 -0500 | [diff] [blame] | 2033 | int rt_overload_count = rt_overloaded(this_rq); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2034 | |
Steven Rostedt | d266817 | 2017-12-02 13:04:54 -0500 | [diff] [blame] | 2035 | if (likely(!rt_overload_count)) |
Peter Zijlstra | 8046d68 | 2015-06-11 14:46:40 +0200 | [diff] [blame] | 2036 | return; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2037 | |
Peter Zijlstra | 7c3f2ab | 2013-10-15 12:35:07 +0200 | [diff] [blame] | 2038 | /* |
| 2039 | * Match the barrier from rt_set_overloaded; this guarantees that if we |
| 2040 | * see overloaded we must also see the rto_mask bit. |
| 2041 | */ |
| 2042 | smp_rmb(); |
| 2043 | |
Steven Rostedt | d266817 | 2017-12-02 13:04:54 -0500 | [diff] [blame] | 2044 | /* If we are the only overloaded CPU do nothing */ |
| 2045 | if (rt_overload_count == 1 && |
| 2046 | cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask)) |
| 2047 | return; |
| 2048 | |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2049 | #ifdef HAVE_RT_PUSH_IPI |
| 2050 | if (sched_feat(RT_PUSH_IPI)) { |
| 2051 | tell_cpu_to_push(this_rq); |
Peter Zijlstra | 8046d68 | 2015-06-11 14:46:40 +0200 | [diff] [blame] | 2052 | return; |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 2053 | } |
| 2054 | #endif |
| 2055 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 2056 | for_each_cpu(cpu, this_rq->rd->rto_mask) { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2057 | if (this_cpu == cpu) |
| 2058 | continue; |
| 2059 | |
| 2060 | src_rq = cpu_rq(cpu); |
Gregory Haskins | 74ab8e4 | 2008-12-29 09:39:50 -0500 | [diff] [blame] | 2061 | |
| 2062 | /* |
| 2063 | * Don't bother taking the src_rq->lock if the next highest |
| 2064 | * task is known to be lower-priority than our current task. |
| 2065 | * This may look racy, but if this value is about to go |
| 2066 | * logically higher, the src_rq will push this task away. |
| 2067 | * And if its going logically lower, we do not care |
| 2068 | */ |
| 2069 | if (src_rq->rt.highest_prio.next >= |
| 2070 | this_rq->rt.highest_prio.curr) |
| 2071 | continue; |
| 2072 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2073 | /* |
| 2074 | * We can potentially drop this_rq's lock in |
| 2075 | * double_lock_balance, and another CPU could |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 2076 | * alter this_rq |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2077 | */ |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 2078 | double_lock_balance(this_rq, src_rq); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2079 | |
| 2080 | /* |
Kirill Tkhai | e23ee74 | 2013-06-07 15:37:43 -0400 | [diff] [blame] | 2081 | * We can pull only a task, which is pushable |
| 2082 | * on its rq, and no others. |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2083 | */ |
Kirill Tkhai | e23ee74 | 2013-06-07 15:37:43 -0400 | [diff] [blame] | 2084 | p = pick_highest_pushable_task(src_rq, this_cpu); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2085 | |
| 2086 | /* |
| 2087 | * Do we have an RT task that preempts |
| 2088 | * the to-be-scheduled task? |
| 2089 | */ |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 2090 | if (p && (p->prio < this_rq->rt.highest_prio.curr)) { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2091 | WARN_ON(p == src_rq->curr); |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2092 | WARN_ON(!task_on_rq_queued(p)); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2093 | |
| 2094 | /* |
| 2095 | * There's a chance that p is higher in priority |
| 2096 | * than what's currently running on its cpu. |
| 2097 | * This is just that p is wakeing up and hasn't |
| 2098 | * had a chance to schedule. We only pull |
| 2099 | * p if it is lower in priority than the |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 2100 | * current task on the run queue |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2101 | */ |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 2102 | if (p->prio < src_rq->curr->prio) |
Mike Galbraith | 614ee1f | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 2103 | goto skip; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2104 | |
Peter Zijlstra | 8046d68 | 2015-06-11 14:46:40 +0200 | [diff] [blame] | 2105 | resched = true; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2106 | |
| 2107 | deactivate_task(src_rq, p, 0); |
| 2108 | set_task_cpu(p, this_cpu); |
| 2109 | activate_task(this_rq, p, 0); |
| 2110 | /* |
| 2111 | * We continue with the search, just in |
| 2112 | * case there's an even higher prio task |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2113 | * in another runqueue. (low likelihood |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2114 | * but possible) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2115 | */ |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2116 | } |
Peter Zijlstra | 4924627 | 2010-10-17 21:46:10 +0200 | [diff] [blame] | 2117 | skip: |
Peter Zijlstra | 1b12bbc | 2008-08-11 09:30:22 +0200 | [diff] [blame] | 2118 | double_unlock_balance(this_rq, src_rq); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2119 | } |
| 2120 | |
Peter Zijlstra | 8046d68 | 2015-06-11 14:46:40 +0200 | [diff] [blame] | 2121 | if (resched) |
| 2122 | resched_curr(this_rq); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2123 | } |
| 2124 | |
Gregory Haskins | 8ae121a | 2008-04-23 07:13:29 -0400 | [diff] [blame] | 2125 | /* |
| 2126 | * If we are not running and we are not going to reschedule soon, we should |
| 2127 | * try to push tasks away now |
| 2128 | */ |
Peter Zijlstra | efbbd05 | 2009-12-16 18:04:40 +0100 | [diff] [blame] | 2129 | static void task_woken_rt(struct rq *rq, struct task_struct *p) |
Steven Rostedt | 4642daf | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2130 | { |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2131 | if (!task_running(rq, p) && |
Gregory Haskins | 8ae121a | 2008-04-23 07:13:29 -0400 | [diff] [blame] | 2132 | !test_tsk_need_resched(rq->curr) && |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 2133 | tsk_nr_cpus_allowed(p) > 1 && |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 2134 | (dl_task(rq->curr) || rt_task(rq->curr)) && |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 2135 | (tsk_nr_cpus_allowed(rq->curr) < 2 || |
Shawn Bohrer | 3be209a | 2011-09-12 09:28:04 -0500 | [diff] [blame] | 2136 | rq->curr->prio <= p->prio)) |
Steven Rostedt | 4642daf | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2137 | push_rt_tasks(rq); |
| 2138 | } |
| 2139 | |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 2140 | /* Assumes rq->lock is held */ |
Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 2141 | static void rq_online_rt(struct rq *rq) |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 2142 | { |
| 2143 | if (rq->rt.overloaded) |
| 2144 | rt_set_overload(rq); |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 2145 | |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 2146 | __enable_runtime(rq); |
| 2147 | |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 2148 | cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr); |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | /* Assumes rq->lock is held */ |
Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 2152 | static void rq_offline_rt(struct rq *rq) |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 2153 | { |
| 2154 | if (rq->rt.overloaded) |
| 2155 | rt_clear_overload(rq); |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 2156 | |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 2157 | __disable_runtime(rq); |
| 2158 | |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 2159 | cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID); |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 2160 | } |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2161 | |
| 2162 | /* |
| 2163 | * When switch from the rt queue, we bring ourselves to a position |
| 2164 | * that we might want to pull RT tasks from other runqueues. |
| 2165 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 2166 | static void switched_from_rt(struct rq *rq, struct task_struct *p) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2167 | { |
| 2168 | /* |
| 2169 | * If there are other RT tasks then we will reschedule |
| 2170 | * and the scheduling of the other RT tasks will handle |
| 2171 | * the balancing. But if we are the last RT task |
| 2172 | * we may need to handle the pulling of RT tasks |
| 2173 | * now. |
| 2174 | */ |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2175 | if (!task_on_rq_queued(p) || rq->rt.rt_nr_running) |
Kirill Tkhai | 1158ddb | 2012-11-23 00:02:15 +0400 | [diff] [blame] | 2176 | return; |
| 2177 | |
Peter Zijlstra | fd7a4be | 2015-06-11 14:46:41 +0200 | [diff] [blame] | 2178 | queue_pull_task(rq); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2179 | } |
Rusty Russell | 3d8cbdf | 2008-11-25 09:58:41 +1030 | [diff] [blame] | 2180 | |
Li Zefan | 11c785b | 2014-02-08 14:17:45 +0800 | [diff] [blame] | 2181 | void __init init_sched_rt_class(void) |
Rusty Russell | 3d8cbdf | 2008-11-25 09:58:41 +1030 | [diff] [blame] | 2182 | { |
| 2183 | unsigned int i; |
| 2184 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2185 | for_each_possible_cpu(i) { |
Yinghai Lu | eaa9584 | 2009-06-06 14:51:36 -0700 | [diff] [blame] | 2186 | zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i), |
Mike Travis | 6ca09df | 2008-12-31 18:08:45 -0800 | [diff] [blame] | 2187 | GFP_KERNEL, cpu_to_node(i)); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2188 | } |
Rusty Russell | 3d8cbdf | 2008-11-25 09:58:41 +1030 | [diff] [blame] | 2189 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 2190 | #endif /* CONFIG_SMP */ |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2191 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2192 | /* |
| 2193 | * When switching a task to RT, we may overload the runqueue |
| 2194 | * with RT tasks. In this case we try to push them off to |
| 2195 | * other runqueues. |
| 2196 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 2197 | static void switched_to_rt(struct rq *rq, struct task_struct *p) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2198 | { |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2199 | /* |
| 2200 | * If we are already running, then there's nothing |
| 2201 | * that needs to be done. But if we are not running |
| 2202 | * we may need to preempt the current running task. |
| 2203 | * If that current running task is also an RT task |
| 2204 | * then see if we can move to another run queue. |
| 2205 | */ |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2206 | if (task_on_rq_queued(p) && rq->curr != p) { |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2207 | #ifdef CONFIG_SMP |
Thomas Gleixner | 50605ff | 2016-05-11 14:23:31 +0200 | [diff] [blame] | 2208 | if (tsk_nr_cpus_allowed(p) > 1 && rq->rt.overloaded) |
Peter Zijlstra | fd7a4be | 2015-06-11 14:46:41 +0200 | [diff] [blame] | 2209 | queue_push_tasks(rq); |
Sebastian Andrzej Siewior | 916c5cfe | 2017-01-24 15:40:06 +0100 | [diff] [blame] | 2210 | #endif /* CONFIG_SMP */ |
Paul E. McKenney | bac7bb1 | 2017-10-13 17:00:18 -0700 | [diff] [blame] | 2211 | if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq))) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 2212 | resched_curr(rq); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | /* |
| 2217 | * Priority of the task has changed. This may cause |
| 2218 | * us to initiate a push or pull. |
| 2219 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 2220 | static void |
| 2221 | prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2222 | { |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 2223 | if (!task_on_rq_queued(p)) |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 2224 | return; |
| 2225 | |
| 2226 | if (rq->curr == p) { |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2227 | #ifdef CONFIG_SMP |
| 2228 | /* |
| 2229 | * If our priority decreases while running, we |
| 2230 | * may need to pull tasks to this runqueue. |
| 2231 | */ |
| 2232 | if (oldprio < p->prio) |
Peter Zijlstra | fd7a4be | 2015-06-11 14:46:41 +0200 | [diff] [blame] | 2233 | queue_pull_task(rq); |
| 2234 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2235 | /* |
| 2236 | * If there's a higher priority task waiting to run |
Peter Zijlstra | fd7a4be | 2015-06-11 14:46:41 +0200 | [diff] [blame] | 2237 | * then reschedule. |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2238 | */ |
Peter Zijlstra | fd7a4be | 2015-06-11 14:46:41 +0200 | [diff] [blame] | 2239 | if (p->prio > rq->rt.highest_prio.curr) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 2240 | resched_curr(rq); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2241 | #else |
| 2242 | /* For UP simply resched on drop of prio */ |
| 2243 | if (oldprio < p->prio) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 2244 | resched_curr(rq); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2245 | #endif /* CONFIG_SMP */ |
| 2246 | } else { |
| 2247 | /* |
| 2248 | * This task is not running, but if it is |
| 2249 | * greater than the current running task |
| 2250 | * then reschedule. |
| 2251 | */ |
| 2252 | if (p->prio < rq->curr->prio) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 2253 | resched_curr(rq); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2254 | } |
| 2255 | } |
| 2256 | |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2257 | static void watchdog(struct rq *rq, struct task_struct *p) |
| 2258 | { |
| 2259 | unsigned long soft, hard; |
| 2260 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 2261 | /* max may change after cur was read, this will be fixed next tick */ |
| 2262 | soft = task_rlimit(p, RLIMIT_RTTIME); |
| 2263 | hard = task_rlimit_max(p, RLIMIT_RTTIME); |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2264 | |
| 2265 | if (soft != RLIM_INFINITY) { |
| 2266 | unsigned long next; |
| 2267 | |
Ying Xue | 57d2aa0 | 2012-07-17 15:03:43 +0800 | [diff] [blame] | 2268 | if (p->rt.watchdog_stamp != jiffies) { |
| 2269 | p->rt.timeout++; |
| 2270 | p->rt.watchdog_stamp = jiffies; |
| 2271 | } |
| 2272 | |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2273 | next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ); |
Peter Zijlstra | 5a52dd5 | 2008-01-25 21:08:32 +0100 | [diff] [blame] | 2274 | if (p->rt.timeout > next) |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 2275 | p->cputime_expires.sched_exp = p->se.sum_exec_runtime; |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2276 | } |
| 2277 | } |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2278 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2279 | static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2280 | { |
Colin Cross | 454c799 | 2012-05-16 21:34:23 -0700 | [diff] [blame] | 2281 | struct sched_rt_entity *rt_se = &p->rt; |
| 2282 | |
Peter Zijlstra | 67e2be0 | 2007-12-20 15:01:17 +0100 | [diff] [blame] | 2283 | update_curr_rt(rq); |
| 2284 | |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2285 | watchdog(rq, p); |
| 2286 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2287 | /* |
| 2288 | * RR tasks need a special form of timeslice management. |
| 2289 | * FIFO tasks have no timeslices. |
| 2290 | */ |
| 2291 | if (p->policy != SCHED_RR) |
| 2292 | return; |
| 2293 | |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 2294 | if (--p->rt.time_slice) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2295 | return; |
| 2296 | |
Clark Williams | ce0dbbb | 2013-02-07 09:47:04 -0600 | [diff] [blame] | 2297 | p->rt.time_slice = sched_rr_timeslice; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2298 | |
Dmitry Adamushko | 98fbc79 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 2299 | /* |
Li Bin | e9aa39b | 2013-10-21 20:15:43 +0800 | [diff] [blame] | 2300 | * Requeue to the end of queue if we (and all of our ancestors) are not |
| 2301 | * the only element on the queue |
Dmitry Adamushko | 98fbc79 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 2302 | */ |
Colin Cross | 454c799 | 2012-05-16 21:34:23 -0700 | [diff] [blame] | 2303 | for_each_sched_rt_entity(rt_se) { |
| 2304 | if (rt_se->run_list.prev != rt_se->run_list.next) { |
| 2305 | requeue_task_rt(rq, p, 0); |
Kirill Tkhai | 8aa6f0e | 2014-09-22 22:36:43 +0400 | [diff] [blame] | 2306 | resched_curr(rq); |
Colin Cross | 454c799 | 2012-05-16 21:34:23 -0700 | [diff] [blame] | 2307 | return; |
| 2308 | } |
Dmitry Adamushko | 98fbc79 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 2309 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 2312 | static void set_curr_task_rt(struct rq *rq) |
| 2313 | { |
| 2314 | struct task_struct *p = rq->curr; |
| 2315 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 2316 | p->se.exec_start = rq_clock_task(rq); |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 2317 | |
| 2318 | /* The running task is never eligible for pushing */ |
| 2319 | dequeue_pushable_task(rq, p); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 2320 | } |
| 2321 | |
H Hartley Sweeten | 6d686f4 | 2010-01-13 20:21:52 -0700 | [diff] [blame] | 2322 | static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task) |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 2323 | { |
| 2324 | /* |
| 2325 | * Time slice is 0 for SCHED_FIFO tasks |
| 2326 | */ |
| 2327 | if (task->policy == SCHED_RR) |
Clark Williams | ce0dbbb | 2013-02-07 09:47:04 -0600 | [diff] [blame] | 2328 | return sched_rr_timeslice; |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 2329 | else |
| 2330 | return 0; |
| 2331 | } |
| 2332 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2333 | const struct sched_class rt_sched_class = { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2334 | .next = &fair_sched_class, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2335 | .enqueue_task = enqueue_task_rt, |
| 2336 | .dequeue_task = dequeue_task_rt, |
| 2337 | .yield_task = yield_task_rt, |
| 2338 | |
| 2339 | .check_preempt_curr = check_preempt_curr_rt, |
| 2340 | |
| 2341 | .pick_next_task = pick_next_task_rt, |
| 2342 | .put_prev_task = put_prev_task_rt, |
| 2343 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2344 | #ifdef CONFIG_SMP |
Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 2345 | .select_task_rq = select_task_rq_rt, |
| 2346 | |
Peter Zijlstra | 6c37067 | 2015-05-15 17:43:36 +0200 | [diff] [blame] | 2347 | .set_cpus_allowed = set_cpus_allowed_common, |
Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 2348 | .rq_online = rq_online_rt, |
| 2349 | .rq_offline = rq_offline_rt, |
Peter Zijlstra | efbbd05 | 2009-12-16 18:04:40 +0100 | [diff] [blame] | 2350 | .task_woken = task_woken_rt, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2351 | .switched_from = switched_from_rt, |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2352 | #endif |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2353 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 2354 | .set_curr_task = set_curr_task_rt, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2355 | .task_tick = task_tick_rt, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2356 | |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 2357 | .get_rr_interval = get_rr_interval_rt, |
| 2358 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2359 | .prio_changed = prio_changed_rt, |
| 2360 | .switched_to = switched_to_rt, |
Stanislaw Gruszka | 6e99891 | 2014-11-12 16:58:44 +0100 | [diff] [blame] | 2361 | |
| 2362 | .update_curr = update_curr_rt, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2363 | }; |
Peter Zijlstra | ada18de | 2008-06-19 14:22:24 +0200 | [diff] [blame] | 2364 | |
| 2365 | #ifdef CONFIG_SCHED_DEBUG |
| 2366 | extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq); |
| 2367 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2368 | void print_rt_stats(struct seq_file *m, int cpu) |
Peter Zijlstra | ada18de | 2008-06-19 14:22:24 +0200 | [diff] [blame] | 2369 | { |
Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 2370 | rt_rq_iter_t iter; |
Peter Zijlstra | ada18de | 2008-06-19 14:22:24 +0200 | [diff] [blame] | 2371 | struct rt_rq *rt_rq; |
| 2372 | |
| 2373 | rcu_read_lock(); |
Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 2374 | for_each_rt_rq(rt_rq, iter, cpu_rq(cpu)) |
Peter Zijlstra | ada18de | 2008-06-19 14:22:24 +0200 | [diff] [blame] | 2375 | print_rt_rq(m, cpu, rt_rq); |
| 2376 | rcu_read_unlock(); |
| 2377 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 2378 | #endif /* CONFIG_SCHED_DEBUG */ |