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 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 6 | #ifdef CONFIG_SMP |
Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 7 | |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 8 | static inline int rt_overloaded(struct rq *rq) |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 9 | { |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 10 | return atomic_read(&rq->rd->rto_count); |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 11 | } |
Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 12 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 13 | static inline void rt_set_overload(struct rq *rq) |
| 14 | { |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 15 | if (!rq->online) |
| 16 | return; |
| 17 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 18 | cpumask_set_cpu(rq->cpu, rq->rd->rto_mask); |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 19 | /* |
| 20 | * Make sure the mask is visible before we set |
| 21 | * the overload count. That is checked to determine |
| 22 | * if we should look at the mask. It would be a shame |
| 23 | * if we looked at the mask, but the mask was not |
| 24 | * updated yet. |
| 25 | */ |
| 26 | wmb(); |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 27 | atomic_inc(&rq->rd->rto_count); |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 28 | } |
Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 29 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 30 | static inline void rt_clear_overload(struct rq *rq) |
| 31 | { |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 32 | if (!rq->online) |
| 33 | return; |
| 34 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 35 | /* the order here really doesn't matter */ |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 36 | atomic_dec(&rq->rd->rto_count); |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 37 | cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask); |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 38 | } |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 39 | |
| 40 | static void update_rt_migration(struct rq *rq) |
| 41 | { |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 42 | if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) { |
Gregory Haskins | cdc8eb9 | 2008-01-25 21:08:23 +0100 | [diff] [blame] | 43 | if (!rq->rt.overloaded) { |
| 44 | rt_set_overload(rq); |
| 45 | rq->rt.overloaded = 1; |
| 46 | } |
| 47 | } else if (rq->rt.overloaded) { |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 48 | rt_clear_overload(rq); |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 49 | rq->rt.overloaded = 0; |
| 50 | } |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 51 | } |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 52 | |
| 53 | static void enqueue_pushable_task(struct rq *rq, struct task_struct *p) |
| 54 | { |
| 55 | plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); |
| 56 | plist_node_init(&p->pushable_tasks, p->prio); |
| 57 | plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks); |
| 58 | } |
| 59 | |
| 60 | static void dequeue_pushable_task(struct rq *rq, struct task_struct *p) |
| 61 | { |
| 62 | plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); |
| 63 | } |
| 64 | |
| 65 | #else |
| 66 | |
Gregory Haskins | b07430a | 2009-01-14 08:55:39 -0500 | [diff] [blame^] | 67 | static inline |
| 68 | void enqueue_pushable_task(struct rq *rq, struct task_struct *p) {} |
| 69 | static inline |
| 70 | void dequeue_pushable_task(struct rq *rq, struct task_struct *p) {} |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 71 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 72 | #endif /* CONFIG_SMP */ |
| 73 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 74 | static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 75 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 76 | return container_of(rt_se, struct task_struct, rt); |
| 77 | } |
| 78 | |
| 79 | static inline int on_rt_rq(struct sched_rt_entity *rt_se) |
| 80 | { |
| 81 | return !list_empty(&rt_se->run_list); |
| 82 | } |
| 83 | |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 84 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 85 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 86 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 87 | { |
| 88 | if (!rt_rq->tg) |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 89 | return RUNTIME_INF; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 90 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 91 | return rt_rq->rt_runtime; |
| 92 | } |
| 93 | |
| 94 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) |
| 95 | { |
| 96 | return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | #define for_each_leaf_rt_rq(rt_rq, rq) \ |
Bharata B Rao | 80f40ee | 2008-12-15 11:56:48 +0530 | [diff] [blame] | 100 | list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 101 | |
| 102 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) |
| 103 | { |
| 104 | return rt_rq->rq; |
| 105 | } |
| 106 | |
| 107 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) |
| 108 | { |
| 109 | return rt_se->rt_rq; |
| 110 | } |
| 111 | |
| 112 | #define for_each_sched_rt_entity(rt_se) \ |
| 113 | for (; rt_se; rt_se = rt_se->parent) |
| 114 | |
| 115 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) |
| 116 | { |
| 117 | return rt_se->my_q; |
| 118 | } |
| 119 | |
| 120 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se); |
| 121 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se); |
| 122 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 123 | static void sched_rt_rq_enqueue(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 124 | { |
Dario Faggioli | f6121f4 | 2008-10-03 17:40:46 +0200 | [diff] [blame] | 125 | struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 126 | struct sched_rt_entity *rt_se = rt_rq->rt_se; |
| 127 | |
Dario Faggioli | f6121f4 | 2008-10-03 17:40:46 +0200 | [diff] [blame] | 128 | if (rt_rq->rt_nr_running) { |
| 129 | if (rt_se && !on_rt_rq(rt_se)) |
| 130 | enqueue_rt_entity(rt_se); |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 131 | if (rt_rq->highest_prio.curr < curr->prio) |
Peter Zijlstra | 1020387 | 2008-01-25 21:08:32 +0100 | [diff] [blame] | 132 | resched_task(curr); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 136 | static void sched_rt_rq_dequeue(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 137 | { |
| 138 | struct sched_rt_entity *rt_se = rt_rq->rt_se; |
| 139 | |
| 140 | if (rt_se && on_rt_rq(rt_se)) |
| 141 | dequeue_rt_entity(rt_se); |
| 142 | } |
| 143 | |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 144 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) |
| 145 | { |
| 146 | return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted; |
| 147 | } |
| 148 | |
| 149 | static int rt_se_boosted(struct sched_rt_entity *rt_se) |
| 150 | { |
| 151 | struct rt_rq *rt_rq = group_rt_rq(rt_se); |
| 152 | struct task_struct *p; |
| 153 | |
| 154 | if (rt_rq) |
| 155 | return !!rt_rq->rt_nr_boosted; |
| 156 | |
| 157 | p = rt_task_of(rt_se); |
| 158 | return p->prio != p->normal_prio; |
| 159 | } |
| 160 | |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 161 | #ifdef CONFIG_SMP |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 162 | static inline const struct cpumask *sched_rt_period_mask(void) |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 163 | { |
| 164 | return cpu_rq(smp_processor_id())->rd->span; |
| 165 | } |
| 166 | #else |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 167 | static inline const struct cpumask *sched_rt_period_mask(void) |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 168 | { |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 169 | return cpu_online_mask; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 170 | } |
| 171 | #endif |
| 172 | |
| 173 | static inline |
| 174 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) |
| 175 | { |
| 176 | return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu]; |
| 177 | } |
| 178 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 179 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) |
| 180 | { |
| 181 | return &rt_rq->tg->rt_bandwidth; |
| 182 | } |
| 183 | |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 184 | #else /* !CONFIG_RT_GROUP_SCHED */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 185 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 186 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 187 | { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 188 | return rt_rq->rt_runtime; |
| 189 | } |
| 190 | |
| 191 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) |
| 192 | { |
| 193 | return ktime_to_ns(def_rt_bandwidth.rt_period); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | #define for_each_leaf_rt_rq(rt_rq, rq) \ |
| 197 | for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL) |
| 198 | |
| 199 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) |
| 200 | { |
| 201 | return container_of(rt_rq, struct rq, rt); |
| 202 | } |
| 203 | |
| 204 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) |
| 205 | { |
| 206 | struct task_struct *p = rt_task_of(rt_se); |
| 207 | struct rq *rq = task_rq(p); |
| 208 | |
| 209 | return &rq->rt; |
| 210 | } |
| 211 | |
| 212 | #define for_each_sched_rt_entity(rt_se) \ |
| 213 | for (; rt_se; rt_se = NULL) |
| 214 | |
| 215 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) |
| 216 | { |
| 217 | return NULL; |
| 218 | } |
| 219 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 220 | static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 221 | { |
John Blackwood | f3ade83 | 2008-08-26 15:09:43 -0400 | [diff] [blame] | 222 | if (rt_rq->rt_nr_running) |
| 223 | resched_task(rq_of_rt_rq(rt_rq)->curr); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 226 | static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 227 | { |
| 228 | } |
| 229 | |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 230 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) |
| 231 | { |
| 232 | return rt_rq->rt_throttled; |
| 233 | } |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 234 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 235 | static inline const struct cpumask *sched_rt_period_mask(void) |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 236 | { |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 237 | return cpu_online_mask; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static inline |
| 241 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) |
| 242 | { |
| 243 | return &cpu_rq(cpu)->rt; |
| 244 | } |
| 245 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 246 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) |
| 247 | { |
| 248 | return &def_rt_bandwidth; |
| 249 | } |
| 250 | |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 251 | #endif /* CONFIG_RT_GROUP_SCHED */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 252 | |
Peter Zijlstra | b79f383 | 2008-06-19 14:22:25 +0200 | [diff] [blame] | 253 | #ifdef CONFIG_SMP |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 254 | /* |
| 255 | * We ran out of runtime, see if we can borrow some from our neighbours. |
| 256 | */ |
Peter Zijlstra | b79f383 | 2008-06-19 14:22:25 +0200 | [diff] [blame] | 257 | static int do_balance_runtime(struct rt_rq *rt_rq) |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 258 | { |
| 259 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
| 260 | struct root_domain *rd = cpu_rq(smp_processor_id())->rd; |
| 261 | int i, weight, more = 0; |
| 262 | u64 rt_period; |
| 263 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 264 | weight = cpumask_weight(rd->span); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 265 | |
| 266 | spin_lock(&rt_b->rt_runtime_lock); |
| 267 | rt_period = ktime_to_ns(rt_b->rt_period); |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 268 | for_each_cpu(i, rd->span) { |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 269 | struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); |
| 270 | s64 diff; |
| 271 | |
| 272 | if (iter == rt_rq) |
| 273 | continue; |
| 274 | |
| 275 | spin_lock(&iter->rt_runtime_lock); |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 276 | /* |
| 277 | * Either all rqs have inf runtime and there's nothing to steal |
| 278 | * or __disable_runtime() below sets a specific rq to inf to |
| 279 | * indicate its been disabled and disalow stealing. |
| 280 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 281 | if (iter->rt_runtime == RUNTIME_INF) |
| 282 | goto next; |
| 283 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 284 | /* |
| 285 | * From runqueues with spare time, take 1/n part of their |
| 286 | * spare time, but no more than our period. |
| 287 | */ |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 288 | diff = iter->rt_runtime - iter->rt_time; |
| 289 | if (diff > 0) { |
Peter Zijlstra | 58838cf | 2008-07-24 12:43:13 +0200 | [diff] [blame] | 290 | diff = div_u64((u64)diff, weight); |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 291 | if (rt_rq->rt_runtime + diff > rt_period) |
| 292 | diff = rt_period - rt_rq->rt_runtime; |
| 293 | iter->rt_runtime -= diff; |
| 294 | rt_rq->rt_runtime += diff; |
| 295 | more = 1; |
| 296 | if (rt_rq->rt_runtime == rt_period) { |
| 297 | spin_unlock(&iter->rt_runtime_lock); |
| 298 | break; |
| 299 | } |
| 300 | } |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 301 | next: |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 302 | spin_unlock(&iter->rt_runtime_lock); |
| 303 | } |
| 304 | spin_unlock(&rt_b->rt_runtime_lock); |
| 305 | |
| 306 | return more; |
| 307 | } |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 308 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 309 | /* |
| 310 | * Ensure this RQ takes back all the runtime it lend to its neighbours. |
| 311 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 312 | static void __disable_runtime(struct rq *rq) |
| 313 | { |
| 314 | struct root_domain *rd = rq->rd; |
| 315 | struct rt_rq *rt_rq; |
| 316 | |
| 317 | if (unlikely(!scheduler_running)) |
| 318 | return; |
| 319 | |
| 320 | for_each_leaf_rt_rq(rt_rq, rq) { |
| 321 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
| 322 | s64 want; |
| 323 | int i; |
| 324 | |
| 325 | spin_lock(&rt_b->rt_runtime_lock); |
| 326 | spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 327 | /* |
| 328 | * Either we're all inf and nobody needs to borrow, or we're |
| 329 | * already disabled and thus have nothing to do, or we have |
| 330 | * exactly the right amount of runtime to take out. |
| 331 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 332 | if (rt_rq->rt_runtime == RUNTIME_INF || |
| 333 | rt_rq->rt_runtime == rt_b->rt_runtime) |
| 334 | goto balanced; |
| 335 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 336 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 337 | /* |
| 338 | * Calculate the difference between what we started out with |
| 339 | * and what we current have, that's the amount of runtime |
| 340 | * we lend and now have to reclaim. |
| 341 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 342 | want = rt_b->rt_runtime - rt_rq->rt_runtime; |
| 343 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 344 | /* |
| 345 | * Greedy reclaim, take back as much as we can. |
| 346 | */ |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 347 | for_each_cpu(i, rd->span) { |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 348 | struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); |
| 349 | s64 diff; |
| 350 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 351 | /* |
| 352 | * Can't reclaim from ourselves or disabled runqueues. |
| 353 | */ |
Peter Zijlstra | f1679d0 | 2008-08-14 15:49:00 +0200 | [diff] [blame] | 354 | if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF) |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 355 | continue; |
| 356 | |
| 357 | spin_lock(&iter->rt_runtime_lock); |
| 358 | if (want > 0) { |
| 359 | diff = min_t(s64, iter->rt_runtime, want); |
| 360 | iter->rt_runtime -= diff; |
| 361 | want -= diff; |
| 362 | } else { |
| 363 | iter->rt_runtime -= want; |
| 364 | want -= want; |
| 365 | } |
| 366 | spin_unlock(&iter->rt_runtime_lock); |
| 367 | |
| 368 | if (!want) |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 373 | /* |
| 374 | * We cannot be left wanting - that would mean some runtime |
| 375 | * leaked out of the system. |
| 376 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 377 | BUG_ON(want); |
| 378 | balanced: |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 379 | /* |
| 380 | * Disable all the borrow logic by pretending we have inf |
| 381 | * runtime - in which case borrowing doesn't make sense. |
| 382 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 383 | rt_rq->rt_runtime = RUNTIME_INF; |
| 384 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 385 | spin_unlock(&rt_b->rt_runtime_lock); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static void disable_runtime(struct rq *rq) |
| 390 | { |
| 391 | unsigned long flags; |
| 392 | |
| 393 | spin_lock_irqsave(&rq->lock, flags); |
| 394 | __disable_runtime(rq); |
| 395 | spin_unlock_irqrestore(&rq->lock, flags); |
| 396 | } |
| 397 | |
| 398 | static void __enable_runtime(struct rq *rq) |
| 399 | { |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 400 | struct rt_rq *rt_rq; |
| 401 | |
| 402 | if (unlikely(!scheduler_running)) |
| 403 | return; |
| 404 | |
Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 405 | /* |
| 406 | * Reset each runqueue's bandwidth settings |
| 407 | */ |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 408 | for_each_leaf_rt_rq(rt_rq, rq) { |
| 409 | struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); |
| 410 | |
| 411 | spin_lock(&rt_b->rt_runtime_lock); |
| 412 | spin_lock(&rt_rq->rt_runtime_lock); |
| 413 | rt_rq->rt_runtime = rt_b->rt_runtime; |
| 414 | rt_rq->rt_time = 0; |
Zhang, Yanmin | baf2573 | 2008-09-09 11:26:33 +0800 | [diff] [blame] | 415 | rt_rq->rt_throttled = 0; |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 416 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 417 | spin_unlock(&rt_b->rt_runtime_lock); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void enable_runtime(struct rq *rq) |
| 422 | { |
| 423 | unsigned long flags; |
| 424 | |
| 425 | spin_lock_irqsave(&rq->lock, flags); |
| 426 | __enable_runtime(rq); |
| 427 | spin_unlock_irqrestore(&rq->lock, flags); |
| 428 | } |
| 429 | |
Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 430 | static int balance_runtime(struct rt_rq *rt_rq) |
| 431 | { |
| 432 | int more = 0; |
| 433 | |
| 434 | if (rt_rq->rt_time > rt_rq->rt_runtime) { |
| 435 | spin_unlock(&rt_rq->rt_runtime_lock); |
| 436 | more = do_balance_runtime(rt_rq); |
| 437 | spin_lock(&rt_rq->rt_runtime_lock); |
| 438 | } |
| 439 | |
| 440 | return more; |
| 441 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 442 | #else /* !CONFIG_SMP */ |
Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 443 | static inline int balance_runtime(struct rt_rq *rt_rq) |
| 444 | { |
| 445 | return 0; |
| 446 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 447 | #endif /* CONFIG_SMP */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 448 | |
| 449 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun) |
| 450 | { |
| 451 | int i, idle = 1; |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 452 | const struct cpumask *span; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 453 | |
Peter Zijlstra | 0b148fa | 2008-08-19 12:33:04 +0200 | [diff] [blame] | 454 | if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 455 | return 1; |
| 456 | |
| 457 | span = sched_rt_period_mask(); |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 458 | for_each_cpu(i, span) { |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 459 | int enqueue = 0; |
| 460 | struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i); |
| 461 | struct rq *rq = rq_of_rt_rq(rt_rq); |
| 462 | |
| 463 | spin_lock(&rq->lock); |
| 464 | if (rt_rq->rt_time) { |
| 465 | u64 runtime; |
| 466 | |
| 467 | spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 468 | if (rt_rq->rt_throttled) |
| 469 | balance_runtime(rt_rq); |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 470 | runtime = rt_rq->rt_runtime; |
| 471 | rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime); |
| 472 | if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) { |
| 473 | rt_rq->rt_throttled = 0; |
| 474 | enqueue = 1; |
| 475 | } |
| 476 | if (rt_rq->rt_time || rt_rq->rt_nr_running) |
| 477 | idle = 0; |
| 478 | spin_unlock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | 8a8cde1 | 2008-06-19 14:22:28 +0200 | [diff] [blame] | 479 | } else if (rt_rq->rt_nr_running) |
| 480 | idle = 0; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 481 | |
| 482 | if (enqueue) |
| 483 | sched_rt_rq_enqueue(rt_rq); |
| 484 | spin_unlock(&rq->lock); |
| 485 | } |
| 486 | |
| 487 | return idle; |
| 488 | } |
| 489 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 490 | static inline int rt_se_prio(struct sched_rt_entity *rt_se) |
| 491 | { |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 492 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 493 | struct rt_rq *rt_rq = group_rt_rq(rt_se); |
| 494 | |
| 495 | if (rt_rq) |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 496 | return rt_rq->highest_prio.curr; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 497 | #endif |
| 498 | |
| 499 | return rt_task_of(rt_se)->prio; |
| 500 | } |
| 501 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 502 | static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 503 | { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 504 | u64 runtime = sched_rt_runtime(rt_rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 505 | |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 506 | if (rt_rq->rt_throttled) |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 507 | return rt_rq_throttled(rt_rq); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 508 | |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 509 | if (sched_rt_runtime(rt_rq) >= sched_rt_period(rt_rq)) |
| 510 | return 0; |
| 511 | |
Peter Zijlstra | b79f383 | 2008-06-19 14:22:25 +0200 | [diff] [blame] | 512 | balance_runtime(rt_rq); |
| 513 | runtime = sched_rt_runtime(rt_rq); |
| 514 | if (runtime == RUNTIME_INF) |
| 515 | return 0; |
Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 516 | |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 517 | if (rt_rq->rt_time > runtime) { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 518 | rt_rq->rt_throttled = 1; |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 519 | if (rt_rq_throttled(rt_rq)) { |
Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 520 | sched_rt_rq_dequeue(rt_rq); |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 521 | return 1; |
| 522 | } |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 528 | /* |
| 529 | * Update the current task's runtime statistics. Skip current tasks that |
| 530 | * are not in our scheduling class. |
| 531 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 532 | static void update_curr_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 533 | { |
| 534 | struct task_struct *curr = rq->curr; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 535 | struct sched_rt_entity *rt_se = &curr->rt; |
| 536 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 537 | u64 delta_exec; |
| 538 | |
| 539 | if (!task_has_rt_policy(curr)) |
| 540 | return; |
| 541 | |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 542 | delta_exec = rq->clock - curr->se.exec_start; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 543 | if (unlikely((s64)delta_exec < 0)) |
| 544 | delta_exec = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 545 | |
| 546 | schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec)); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 547 | |
| 548 | curr->se.sum_exec_runtime += delta_exec; |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 549 | account_group_exec_runtime(curr, delta_exec); |
| 550 | |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 551 | curr->se.exec_start = rq->clock; |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 552 | cpuacct_charge(curr, delta_exec); |
Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 553 | |
Peter Zijlstra | 0b148fa | 2008-08-19 12:33:04 +0200 | [diff] [blame] | 554 | if (!rt_bandwidth_enabled()) |
| 555 | return; |
| 556 | |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 557 | for_each_sched_rt_entity(rt_se) { |
| 558 | rt_rq = rt_rq_of_se(rt_se); |
| 559 | |
Peter Zijlstra | cc2991c | 2008-08-19 12:33:03 +0200 | [diff] [blame] | 560 | if (sched_rt_runtime(rt_rq) != RUNTIME_INF) { |
Dimitri Sivanich | e113a74 | 2008-10-31 08:03:41 -0500 | [diff] [blame] | 561 | spin_lock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | cc2991c | 2008-08-19 12:33:03 +0200 | [diff] [blame] | 562 | rt_rq->rt_time += delta_exec; |
| 563 | if (sched_rt_runtime_exceeded(rt_rq)) |
| 564 | resched_task(curr); |
Dimitri Sivanich | e113a74 | 2008-10-31 08:03:41 -0500 | [diff] [blame] | 565 | spin_unlock(&rt_rq->rt_runtime_lock); |
Peter Zijlstra | cc2991c | 2008-08-19 12:33:03 +0200 | [diff] [blame] | 566 | } |
Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 567 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 568 | } |
| 569 | |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 570 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
| 571 | |
| 572 | static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu); |
| 573 | |
| 574 | static inline int next_prio(struct rq *rq) |
| 575 | { |
| 576 | struct task_struct *next = pick_next_highest_task_rt(rq, rq->cpu); |
| 577 | |
| 578 | if (next && rt_prio(next->prio)) |
| 579 | return next->prio; |
| 580 | else |
| 581 | return MAX_RT_PRIO; |
| 582 | } |
| 583 | #endif |
| 584 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 585 | static inline |
| 586 | void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 587 | { |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 588 | int prio = rt_se_prio(rt_se); |
| 589 | #ifdef CONFIG_SMP |
| 590 | struct rq *rq = rq_of_rt_rq(rt_rq); |
| 591 | #endif |
| 592 | |
| 593 | WARN_ON(!rt_prio(prio)); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 594 | rt_rq->rt_nr_running++; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 595 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 596 | if (prio < rt_rq->highest_prio.curr) { |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 597 | |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 598 | /* |
| 599 | * If the new task is higher in priority than anything on the |
| 600 | * run-queue, we have a new high that must be published to |
| 601 | * the world. We also know that the previous high becomes |
| 602 | * our next-highest. |
| 603 | */ |
| 604 | rt_rq->highest_prio.next = rt_rq->highest_prio.curr; |
| 605 | rt_rq->highest_prio.curr = prio; |
Ingo Molnar | 1100ac9 | 2008-06-05 12:25:37 +0200 | [diff] [blame] | 606 | #ifdef CONFIG_SMP |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 607 | if (rq->online) |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 608 | cpupri_set(&rq->rd->cpupri, rq->cpu, prio); |
Ingo Molnar | 1100ac9 | 2008-06-05 12:25:37 +0200 | [diff] [blame] | 609 | #endif |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 610 | } else if (prio == rt_rq->highest_prio.curr) |
| 611 | /* |
| 612 | * If the next task is equal in priority to the highest on |
| 613 | * the run-queue, then we implicitly know that the next highest |
| 614 | * task cannot be any lower than current |
| 615 | */ |
| 616 | rt_rq->highest_prio.next = prio; |
| 617 | else if (prio < rt_rq->highest_prio.next) |
| 618 | /* |
| 619 | * Otherwise, we need to recompute next-highest |
| 620 | */ |
| 621 | rt_rq->highest_prio.next = next_prio(rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 622 | #endif |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 623 | #ifdef CONFIG_SMP |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 624 | if (rt_se->nr_cpus_allowed > 1) |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 625 | rq->rt.rt_nr_migratory++; |
| 626 | |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 627 | update_rt_migration(rq); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 628 | #endif |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 629 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 630 | if (rt_se_boosted(rt_se)) |
| 631 | rt_rq->rt_nr_boosted++; |
Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 632 | |
| 633 | if (rt_rq->tg) |
| 634 | start_rt_bandwidth(&rt_rq->tg->rt_bandwidth); |
| 635 | #else |
| 636 | start_rt_bandwidth(&def_rt_bandwidth); |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 637 | #endif |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 640 | static inline |
| 641 | void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 642 | { |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 643 | #ifdef CONFIG_SMP |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 644 | struct rq *rq = rq_of_rt_rq(rt_rq); |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 645 | int highest_prio = rt_rq->highest_prio.curr; |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 646 | #endif |
| 647 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 648 | WARN_ON(!rt_prio(rt_se_prio(rt_se))); |
| 649 | WARN_ON(!rt_rq->rt_nr_running); |
| 650 | rt_rq->rt_nr_running--; |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 651 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 652 | if (rt_rq->rt_nr_running) { |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 653 | int prio = rt_se_prio(rt_se); |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 654 | |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 655 | WARN_ON(prio < rt_rq->highest_prio.curr); |
| 656 | |
| 657 | /* |
| 658 | * This may have been our highest or next-highest priority |
| 659 | * task and therefore we may have some recomputation to do |
| 660 | */ |
| 661 | if (prio == rt_rq->highest_prio.curr) { |
| 662 | struct rt_prio_array *array = &rt_rq->active; |
| 663 | |
| 664 | rt_rq->highest_prio.curr = |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 665 | sched_find_first_bit(array->bitmap); |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | if (prio <= rt_rq->highest_prio.next) |
| 669 | rt_rq->highest_prio.next = next_prio(rq); |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 670 | } else |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 671 | rt_rq->highest_prio.curr = MAX_RT_PRIO; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 672 | #endif |
| 673 | #ifdef CONFIG_SMP |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 674 | if (rt_se->nr_cpus_allowed > 1) |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 675 | rq->rt.rt_nr_migratory--; |
| 676 | |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 677 | if (rq->online && rt_rq->highest_prio.curr != highest_prio) |
| 678 | cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr); |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 679 | |
Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 680 | update_rt_migration(rq); |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 681 | #endif /* CONFIG_SMP */ |
Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 682 | #ifdef CONFIG_RT_GROUP_SCHED |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 683 | if (rt_se_boosted(rt_se)) |
| 684 | rt_rq->rt_nr_boosted--; |
| 685 | |
| 686 | WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted); |
| 687 | #endif |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 690 | static void __enqueue_rt_entity(struct sched_rt_entity *rt_se) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 691 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 692 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
| 693 | struct rt_prio_array *array = &rt_rq->active; |
| 694 | struct rt_rq *group_rq = group_rt_rq(rt_se); |
Dmitry Adamushko | 20b6331 | 2008-06-11 00:58:30 +0200 | [diff] [blame] | 695 | struct list_head *queue = array->queue + rt_se_prio(rt_se); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 696 | |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 697 | /* |
| 698 | * Don't enqueue the group if its throttled, or when empty. |
| 699 | * The latter is a consequence of the former when a child group |
| 700 | * get throttled and the current group doesn't have any other |
| 701 | * active members. |
| 702 | */ |
| 703 | if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 704 | return; |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 705 | |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 706 | list_add_tail(&rt_se->run_list, queue); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 707 | __set_bit(rt_se_prio(rt_se), array->bitmap); |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 708 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 709 | inc_rt_tasks(rt_se, rt_rq); |
| 710 | } |
| 711 | |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 712 | static void __dequeue_rt_entity(struct sched_rt_entity *rt_se) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 713 | { |
| 714 | struct rt_rq *rt_rq = rt_rq_of_se(rt_se); |
| 715 | struct rt_prio_array *array = &rt_rq->active; |
| 716 | |
| 717 | list_del_init(&rt_se->run_list); |
| 718 | if (list_empty(array->queue + rt_se_prio(rt_se))) |
| 719 | __clear_bit(rt_se_prio(rt_se), array->bitmap); |
| 720 | |
| 721 | dec_rt_tasks(rt_se, rt_rq); |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Because the prio of an upper entry depends on the lower |
| 726 | * entries, we must remove entries top - down. |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 727 | */ |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 728 | static void dequeue_rt_stack(struct sched_rt_entity *rt_se) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 729 | { |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 730 | struct sched_rt_entity *back = NULL; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 731 | |
Peter Zijlstra | 58d6c2d | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 732 | for_each_sched_rt_entity(rt_se) { |
| 733 | rt_se->back = back; |
| 734 | back = rt_se; |
| 735 | } |
| 736 | |
| 737 | for (rt_se = back; rt_se; rt_se = rt_se->back) { |
| 738 | if (on_rt_rq(rt_se)) |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 739 | __dequeue_rt_entity(rt_se); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se) |
| 744 | { |
| 745 | dequeue_rt_stack(rt_se); |
| 746 | for_each_sched_rt_entity(rt_se) |
| 747 | __enqueue_rt_entity(rt_se); |
| 748 | } |
| 749 | |
| 750 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se) |
| 751 | { |
| 752 | dequeue_rt_stack(rt_se); |
| 753 | |
| 754 | for_each_sched_rt_entity(rt_se) { |
| 755 | struct rt_rq *rt_rq = group_rt_rq(rt_se); |
| 756 | |
| 757 | if (rt_rq && rt_rq->rt_nr_running) |
| 758 | __enqueue_rt_entity(rt_se); |
Peter Zijlstra | 58d6c2d | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 759 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /* |
| 763 | * Adding/removing a task to/from a priority array: |
| 764 | */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 765 | static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup) |
| 766 | { |
| 767 | struct sched_rt_entity *rt_se = &p->rt; |
| 768 | |
| 769 | if (wakeup) |
| 770 | rt_se->timeout = 0; |
| 771 | |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 772 | enqueue_rt_entity(rt_se); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 773 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 774 | if (!task_current(rq, p) && p->rt.nr_cpus_allowed > 1) |
| 775 | enqueue_pushable_task(rq, p); |
| 776 | |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 777 | inc_cpu_load(rq, p->se.load.weight); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 778 | } |
| 779 | |
Ingo Molnar | f02231e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 780 | static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 781 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 782 | struct sched_rt_entity *rt_se = &p->rt; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 783 | |
Ingo Molnar | f1e14ef | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 784 | update_curr_rt(rq); |
Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 785 | dequeue_rt_entity(rt_se); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 786 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 787 | dequeue_pushable_task(rq, p); |
| 788 | |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 789 | dec_cpu_load(rq, p->se.load.weight); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* |
| 793 | * Put task to the end of the run list without the overhead of dequeue |
| 794 | * followed by enqueue. |
| 795 | */ |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 796 | static void |
| 797 | 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] | 798 | { |
Ingo Molnar | 1cdad71 | 2008-06-19 09:09:15 +0200 | [diff] [blame] | 799 | if (on_rt_rq(rt_se)) { |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 800 | struct rt_prio_array *array = &rt_rq->active; |
| 801 | struct list_head *queue = array->queue + rt_se_prio(rt_se); |
| 802 | |
| 803 | if (head) |
| 804 | list_move(&rt_se->run_list, queue); |
| 805 | else |
| 806 | list_move_tail(&rt_se->run_list, queue); |
Ingo Molnar | 1cdad71 | 2008-06-19 09:09:15 +0200 | [diff] [blame] | 807 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 808 | } |
| 809 | |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 810 | 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] | 811 | { |
| 812 | struct sched_rt_entity *rt_se = &p->rt; |
| 813 | struct rt_rq *rt_rq; |
| 814 | |
| 815 | for_each_sched_rt_entity(rt_se) { |
| 816 | rt_rq = rt_rq_of_se(rt_se); |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 817 | requeue_rt_entity(rt_rq, rt_se, head); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | static void yield_task_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 822 | { |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 823 | requeue_task_rt(rq, rq->curr, 0); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 826 | #ifdef CONFIG_SMP |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 827 | static int find_lowest_rq(struct task_struct *task); |
| 828 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 829 | static int select_task_rq_rt(struct task_struct *p, int sync) |
| 830 | { |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 831 | struct rq *rq = task_rq(p); |
| 832 | |
| 833 | /* |
Steven Rostedt | e1f47d8 | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 834 | * If the current task is an RT task, then |
| 835 | * try to see if we can wake this RT task up on another |
| 836 | * runqueue. Otherwise simply start this RT task |
| 837 | * on its current runqueue. |
| 838 | * |
| 839 | * We want to avoid overloading runqueues. Even if |
| 840 | * the RT task is of higher priority than the current RT task. |
| 841 | * RT tasks behave differently than other tasks. If |
| 842 | * one gets preempted, we try to push it off to another queue. |
| 843 | * So trying to keep a preempting RT task on the same |
| 844 | * cache hot CPU will force the running RT task to |
| 845 | * a cold CPU. So we waste all the cache for the lower |
| 846 | * RT task in hopes of saving some of a RT task |
| 847 | * that is just being woken and probably will have |
| 848 | * cold cache anyway. |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 849 | */ |
Gregory Haskins | 17b3279 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 850 | if (unlikely(rt_task(rq->curr)) && |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 851 | (p->rt.nr_cpus_allowed > 1)) { |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 852 | int cpu = find_lowest_rq(p); |
| 853 | |
| 854 | return (cpu == -1) ? task_cpu(p) : cpu; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Otherwise, just let it ride on the affined RQ and the |
| 859 | * post-schedule router will push the preempted task away |
| 860 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 861 | return task_cpu(p); |
| 862 | } |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 863 | |
| 864 | static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p) |
| 865 | { |
Rusty Russell | 24600ce | 2008-11-25 02:35:13 +1030 | [diff] [blame] | 866 | cpumask_var_t mask; |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 867 | |
| 868 | if (rq->curr->rt.nr_cpus_allowed == 1) |
| 869 | return; |
| 870 | |
Rusty Russell | 24600ce | 2008-11-25 02:35:13 +1030 | [diff] [blame] | 871 | if (!alloc_cpumask_var(&mask, GFP_ATOMIC)) |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 872 | return; |
| 873 | |
Rusty Russell | 24600ce | 2008-11-25 02:35:13 +1030 | [diff] [blame] | 874 | if (p->rt.nr_cpus_allowed != 1 |
| 875 | && cpupri_find(&rq->rd->cpupri, p, mask)) |
| 876 | goto free; |
| 877 | |
| 878 | if (!cpupri_find(&rq->rd->cpupri, rq->curr, mask)) |
| 879 | goto free; |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 880 | |
| 881 | /* |
| 882 | * There appears to be other cpus that can accept |
| 883 | * current and none to run 'p', so lets reschedule |
| 884 | * to try and push current away: |
| 885 | */ |
| 886 | requeue_task_rt(rq, p, 1); |
| 887 | resched_task(rq->curr); |
Rusty Russell | 24600ce | 2008-11-25 02:35:13 +1030 | [diff] [blame] | 888 | free: |
| 889 | free_cpumask_var(mask); |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 892 | #endif /* CONFIG_SMP */ |
| 893 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 894 | /* |
| 895 | * Preempt the current task with a newly woken task if needed: |
| 896 | */ |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 897 | static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int sync) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 898 | { |
Gregory Haskins | 45c01e8 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 899 | if (p->prio < rq->curr->prio) { |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 900 | resched_task(rq->curr); |
Gregory Haskins | 45c01e8 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 901 | return; |
| 902 | } |
| 903 | |
| 904 | #ifdef CONFIG_SMP |
| 905 | /* |
| 906 | * If: |
| 907 | * |
| 908 | * - the newly woken task is of equal priority to the current task |
| 909 | * - the newly woken task is non-migratable while current is migratable |
| 910 | * - current will be preempted on the next reschedule |
| 911 | * |
| 912 | * we should check to see if current can readily move to a different |
| 913 | * cpu. If so, we will reschedule to allow the push logic to try |
| 914 | * to move current somewhere else, making room for our non-migratable |
| 915 | * task. |
| 916 | */ |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 917 | if (p->prio == rq->curr->prio && !need_resched()) |
| 918 | check_preempt_equal_prio(rq, p); |
Gregory Haskins | 45c01e8 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 919 | #endif |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 920 | } |
| 921 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 922 | static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq, |
| 923 | struct rt_rq *rt_rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 924 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 925 | struct rt_prio_array *array = &rt_rq->active; |
| 926 | struct sched_rt_entity *next = NULL; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 927 | struct list_head *queue; |
| 928 | int idx; |
| 929 | |
| 930 | idx = sched_find_first_bit(array->bitmap); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 931 | BUG_ON(idx >= MAX_RT_PRIO); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 932 | |
| 933 | queue = array->queue + idx; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 934 | next = list_entry(queue->next, struct sched_rt_entity, run_list); |
Dmitry Adamushko | 326587b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 935 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 936 | return next; |
| 937 | } |
| 938 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 939 | static struct task_struct *_pick_next_task_rt(struct rq *rq) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 940 | { |
| 941 | struct sched_rt_entity *rt_se; |
| 942 | struct task_struct *p; |
| 943 | struct rt_rq *rt_rq; |
| 944 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 945 | rt_rq = &rq->rt; |
| 946 | |
| 947 | if (unlikely(!rt_rq->rt_nr_running)) |
| 948 | return NULL; |
| 949 | |
Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 950 | if (rt_rq_throttled(rt_rq)) |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 951 | return NULL; |
| 952 | |
| 953 | do { |
| 954 | rt_se = pick_next_rt_entity(rq, rt_rq); |
Dmitry Adamushko | 326587b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 955 | BUG_ON(!rt_se); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 956 | rt_rq = group_rt_rq(rt_se); |
| 957 | } while (rt_rq); |
| 958 | |
| 959 | p = rt_task_of(rt_se); |
| 960 | p->se.exec_start = rq->clock; |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 961 | |
| 962 | return p; |
| 963 | } |
| 964 | |
| 965 | static struct task_struct *pick_next_task_rt(struct rq *rq) |
| 966 | { |
| 967 | struct task_struct *p = _pick_next_task_rt(rq); |
| 968 | |
| 969 | /* The running task is never eligible for pushing */ |
| 970 | if (p) |
| 971 | dequeue_pushable_task(rq, p); |
| 972 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 973 | return p; |
| 974 | } |
| 975 | |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 976 | 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] | 977 | { |
Ingo Molnar | f1e14ef | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 978 | update_curr_rt(rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 979 | p->se.exec_start = 0; |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 980 | |
| 981 | /* |
| 982 | * The previous task needs to be made eligible for pushing |
| 983 | * if it is still active |
| 984 | */ |
| 985 | if (p->se.on_rq && p->rt.nr_cpus_allowed > 1) |
| 986 | enqueue_pushable_task(rq, p); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 989 | #ifdef CONFIG_SMP |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 990 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 991 | /* Only try algorithms three times */ |
| 992 | #define RT_MAX_TRIES 3 |
| 993 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 994 | static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep); |
| 995 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 996 | static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu) |
| 997 | { |
| 998 | if (!task_running(rq, p) && |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 999 | (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) && |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1000 | (p->rt.nr_cpus_allowed > 1)) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1001 | return 1; |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1005 | /* Return the second highest RT task, NULL otherwise */ |
Ingo Molnar | 79064fb | 2008-01-25 21:08:14 +0100 | [diff] [blame] | 1006 | static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1007 | { |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1008 | struct task_struct *next = NULL; |
| 1009 | struct sched_rt_entity *rt_se; |
| 1010 | struct rt_prio_array *array; |
| 1011 | struct rt_rq *rt_rq; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1012 | int idx; |
| 1013 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1014 | for_each_leaf_rt_rq(rt_rq, rq) { |
| 1015 | array = &rt_rq->active; |
| 1016 | idx = sched_find_first_bit(array->bitmap); |
| 1017 | next_idx: |
| 1018 | if (idx >= MAX_RT_PRIO) |
| 1019 | continue; |
| 1020 | if (next && next->prio < idx) |
| 1021 | continue; |
| 1022 | list_for_each_entry(rt_se, array->queue + idx, run_list) { |
| 1023 | struct task_struct *p = rt_task_of(rt_se); |
| 1024 | if (pick_rt_task(rq, p, cpu)) { |
| 1025 | next = p; |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | if (!next) { |
| 1030 | idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1); |
| 1031 | goto next_idx; |
| 1032 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1035 | return next; |
| 1036 | } |
| 1037 | |
Rusty Russell | 0e3900e | 2008-11-25 02:35:13 +1030 | [diff] [blame] | 1038 | static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1039 | |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1040 | static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask) |
| 1041 | { |
| 1042 | int first; |
| 1043 | |
| 1044 | /* "this_cpu" is cheaper to preempt than a remote processor */ |
| 1045 | if ((this_cpu != -1) && cpu_isset(this_cpu, *mask)) |
| 1046 | return this_cpu; |
| 1047 | |
| 1048 | first = first_cpu(*mask); |
| 1049 | if (first != NR_CPUS) |
| 1050 | return first; |
| 1051 | |
| 1052 | return -1; |
| 1053 | } |
| 1054 | |
| 1055 | static int find_lowest_rq(struct task_struct *task) |
| 1056 | { |
| 1057 | struct sched_domain *sd; |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1058 | struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask); |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1059 | int this_cpu = smp_processor_id(); |
| 1060 | int cpu = task_cpu(task); |
| 1061 | |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1062 | if (task->rt.nr_cpus_allowed == 1) |
| 1063 | return -1; /* No other targets possible */ |
| 1064 | |
| 1065 | if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask)) |
Gregory Haskins | 06f90db | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 1066 | return -1; /* No targets found */ |
| 1067 | |
| 1068 | /* |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 1069 | * Only consider CPUs that are usable for migration. |
| 1070 | * I guess we might want to change cpupri_find() to ignore those |
| 1071 | * in the first place. |
| 1072 | */ |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1073 | cpumask_and(lowest_mask, lowest_mask, cpu_active_mask); |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 1074 | |
| 1075 | /* |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1076 | * At this point we have built a mask of cpus representing the |
| 1077 | * lowest priority tasks in the system. Now we want to elect |
| 1078 | * the best one based on our affinity and topology. |
| 1079 | * |
| 1080 | * We prioritize the last cpu that the task executed on since |
| 1081 | * it is most likely cache-hot in that location. |
| 1082 | */ |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1083 | if (cpumask_test_cpu(cpu, lowest_mask)) |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1084 | return cpu; |
| 1085 | |
| 1086 | /* |
| 1087 | * Otherwise, we consult the sched_domains span maps to figure |
| 1088 | * out which cpu is logically closest to our hot cache data. |
| 1089 | */ |
| 1090 | if (this_cpu == cpu) |
| 1091 | this_cpu = -1; /* Skip this_cpu opt if the same */ |
| 1092 | |
| 1093 | for_each_domain(cpu, sd) { |
| 1094 | if (sd->flags & SD_WAKE_AFFINE) { |
| 1095 | cpumask_t domain_mask; |
| 1096 | int best_cpu; |
| 1097 | |
Rusty Russell | 758b2cd | 2008-11-25 02:35:04 +1030 | [diff] [blame] | 1098 | cpumask_and(&domain_mask, sched_domain_span(sd), |
| 1099 | lowest_mask); |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1100 | |
| 1101 | best_cpu = pick_optimal_cpu(this_cpu, |
| 1102 | &domain_mask); |
| 1103 | if (best_cpu != -1) |
| 1104 | return best_cpu; |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * And finally, if there were no matches within the domains |
| 1110 | * just give the caller *something* to work with from the compatible |
| 1111 | * locations. |
| 1112 | */ |
| 1113 | return pick_optimal_cpu(this_cpu, lowest_mask); |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1116 | /* Will lock the rq it finds */ |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1117 | 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] | 1118 | { |
| 1119 | struct rq *lowest_rq = NULL; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1120 | int tries; |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1121 | int cpu; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1122 | |
| 1123 | for (tries = 0; tries < RT_MAX_TRIES; tries++) { |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1124 | cpu = find_lowest_rq(task); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1125 | |
Gregory Haskins | 2de0b46 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1126 | if ((cpu == -1) || (cpu == rq->cpu)) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1127 | break; |
| 1128 | |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1129 | lowest_rq = cpu_rq(cpu); |
| 1130 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1131 | /* if the prio of this runqueue changed, try again */ |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1132 | if (double_lock_balance(rq, lowest_rq)) { |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1133 | /* |
| 1134 | * We had to unlock the run queue. In |
| 1135 | * the mean time, task could have |
| 1136 | * migrated already or had its affinity changed. |
| 1137 | * Also make sure that it wasn't scheduled on its rq. |
| 1138 | */ |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1139 | if (unlikely(task_rq(task) != rq || |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1140 | !cpumask_test_cpu(lowest_rq->cpu, |
| 1141 | &task->cpus_allowed) || |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1142 | task_running(rq, task) || |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1143 | !task->se.on_rq)) { |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1144 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1145 | spin_unlock(&lowest_rq->lock); |
| 1146 | lowest_rq = NULL; |
| 1147 | break; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | /* If this rq is still suitable use it. */ |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1152 | if (lowest_rq->rt.highest_prio.curr > task->prio) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1153 | break; |
| 1154 | |
| 1155 | /* try again */ |
Peter Zijlstra | 1b12bbc | 2008-08-11 09:30:22 +0200 | [diff] [blame] | 1156 | double_unlock_balance(rq, lowest_rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1157 | lowest_rq = NULL; |
| 1158 | } |
| 1159 | |
| 1160 | return lowest_rq; |
| 1161 | } |
| 1162 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1163 | static inline int has_pushable_tasks(struct rq *rq) |
| 1164 | { |
| 1165 | return !plist_head_empty(&rq->rt.pushable_tasks); |
| 1166 | } |
| 1167 | |
| 1168 | static struct task_struct *pick_next_pushable_task(struct rq *rq) |
| 1169 | { |
| 1170 | struct task_struct *p; |
| 1171 | |
| 1172 | if (!has_pushable_tasks(rq)) |
| 1173 | return NULL; |
| 1174 | |
| 1175 | p = plist_first_entry(&rq->rt.pushable_tasks, |
| 1176 | struct task_struct, pushable_tasks); |
| 1177 | |
| 1178 | BUG_ON(rq->cpu != task_cpu(p)); |
| 1179 | BUG_ON(task_current(rq, p)); |
| 1180 | BUG_ON(p->rt.nr_cpus_allowed <= 1); |
| 1181 | |
| 1182 | BUG_ON(!p->se.on_rq); |
| 1183 | BUG_ON(!rt_task(p)); |
| 1184 | |
| 1185 | return p; |
| 1186 | } |
| 1187 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1188 | /* |
| 1189 | * If the current CPU has more than one RT task, see if the non |
| 1190 | * running task can migrate over to a CPU that is running a task |
| 1191 | * of lesser priority. |
| 1192 | */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1193 | static int push_rt_task(struct rq *rq) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1194 | { |
| 1195 | struct task_struct *next_task; |
| 1196 | struct rq *lowest_rq; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1197 | |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 1198 | if (!rq->rt.overloaded) |
| 1199 | return 0; |
| 1200 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1201 | next_task = pick_next_pushable_task(rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1202 | if (!next_task) |
| 1203 | return 0; |
| 1204 | |
| 1205 | retry: |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1206 | if (unlikely(next_task == rq->curr)) { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1207 | WARN_ON(1); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1208 | return 0; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1209 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1210 | |
| 1211 | /* |
| 1212 | * It's possible that the next_task slipped in of |
| 1213 | * higher priority than current. If that's the case |
| 1214 | * just reschedule current. |
| 1215 | */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1216 | if (unlikely(next_task->prio < rq->curr->prio)) { |
| 1217 | resched_task(rq->curr); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1218 | return 0; |
| 1219 | } |
| 1220 | |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1221 | /* We might release rq lock */ |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1222 | get_task_struct(next_task); |
| 1223 | |
| 1224 | /* find_lock_lowest_rq locks the rq if found */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1225 | lowest_rq = find_lock_lowest_rq(next_task, rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1226 | if (!lowest_rq) { |
| 1227 | struct task_struct *task; |
| 1228 | /* |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1229 | * find lock_lowest_rq releases rq->lock |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1230 | * so it is possible that next_task has migrated. |
| 1231 | * |
| 1232 | * We need to make sure that the task is still on the same |
| 1233 | * run-queue and is also still the next task eligible for |
| 1234 | * pushing. |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1235 | */ |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1236 | task = pick_next_pushable_task(rq); |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1237 | if (task_cpu(next_task) == rq->cpu && task == next_task) { |
| 1238 | /* |
| 1239 | * If we get here, the task hasnt moved at all, but |
| 1240 | * it has failed to push. We will not try again, |
| 1241 | * since the other cpus will pull from us when they |
| 1242 | * are ready. |
| 1243 | */ |
| 1244 | dequeue_pushable_task(rq, next_task); |
| 1245 | goto out; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1246 | } |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1247 | |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1248 | if (!task) |
| 1249 | /* No more tasks, just exit */ |
| 1250 | goto out; |
| 1251 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1252 | /* |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1253 | * Something has shifted, try again. |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1254 | */ |
Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1255 | put_task_struct(next_task); |
| 1256 | next_task = task; |
| 1257 | goto retry; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1260 | deactivate_task(rq, next_task, 0); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1261 | set_task_cpu(next_task, lowest_rq->cpu); |
| 1262 | activate_task(lowest_rq, next_task, 0); |
| 1263 | |
| 1264 | resched_task(lowest_rq->curr); |
| 1265 | |
Peter Zijlstra | 1b12bbc | 2008-08-11 09:30:22 +0200 | [diff] [blame] | 1266 | double_unlock_balance(rq, lowest_rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1267 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1268 | out: |
| 1269 | put_task_struct(next_task); |
| 1270 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1271 | return 1; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1272 | } |
| 1273 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1274 | static void push_rt_tasks(struct rq *rq) |
| 1275 | { |
| 1276 | /* push_rt_task will return true if it moved an RT */ |
| 1277 | while (push_rt_task(rq)) |
| 1278 | ; |
| 1279 | } |
| 1280 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1281 | static int pull_rt_task(struct rq *this_rq) |
| 1282 | { |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame] | 1283 | int this_cpu = this_rq->cpu, ret = 0, cpu; |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1284 | struct task_struct *p; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1285 | struct rq *src_rq; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1286 | |
Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1287 | if (likely(!rt_overloaded(this_rq))) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1288 | return 0; |
| 1289 | |
Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 1290 | for_each_cpu(cpu, this_rq->rd->rto_mask) { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1291 | if (this_cpu == cpu) |
| 1292 | continue; |
| 1293 | |
| 1294 | src_rq = cpu_rq(cpu); |
Gregory Haskins | 74ab8e4 | 2008-12-29 09:39:50 -0500 | [diff] [blame] | 1295 | |
| 1296 | /* |
| 1297 | * Don't bother taking the src_rq->lock if the next highest |
| 1298 | * task is known to be lower-priority than our current task. |
| 1299 | * This may look racy, but if this value is about to go |
| 1300 | * logically higher, the src_rq will push this task away. |
| 1301 | * And if its going logically lower, we do not care |
| 1302 | */ |
| 1303 | if (src_rq->rt.highest_prio.next >= |
| 1304 | this_rq->rt.highest_prio.curr) |
| 1305 | continue; |
| 1306 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1307 | /* |
| 1308 | * We can potentially drop this_rq's lock in |
| 1309 | * double_lock_balance, and another CPU could |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1310 | * alter this_rq |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1311 | */ |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1312 | double_lock_balance(this_rq, src_rq); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1313 | |
| 1314 | /* |
| 1315 | * Are there still pullable RT tasks? |
| 1316 | */ |
Mike Galbraith | 614ee1f | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1317 | if (src_rq->rt.rt_nr_running <= 1) |
| 1318 | goto skip; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1319 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1320 | p = pick_next_highest_task_rt(src_rq, this_cpu); |
| 1321 | |
| 1322 | /* |
| 1323 | * Do we have an RT task that preempts |
| 1324 | * the to-be-scheduled task? |
| 1325 | */ |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1326 | if (p && (p->prio < this_rq->rt.highest_prio.curr)) { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1327 | WARN_ON(p == src_rq->curr); |
| 1328 | WARN_ON(!p->se.on_rq); |
| 1329 | |
| 1330 | /* |
| 1331 | * There's a chance that p is higher in priority |
| 1332 | * than what's currently running on its cpu. |
| 1333 | * This is just that p is wakeing up and hasn't |
| 1334 | * had a chance to schedule. We only pull |
| 1335 | * p if it is lower in priority than the |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1336 | * current task on the run queue |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1337 | */ |
Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1338 | if (p->prio < src_rq->curr->prio) |
Mike Galbraith | 614ee1f | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1339 | goto skip; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1340 | |
| 1341 | ret = 1; |
| 1342 | |
| 1343 | deactivate_task(src_rq, p, 0); |
| 1344 | set_task_cpu(p, this_cpu); |
| 1345 | activate_task(this_rq, p, 0); |
| 1346 | /* |
| 1347 | * We continue with the search, just in |
| 1348 | * case there's an even higher prio task |
| 1349 | * in another runqueue. (low likelyhood |
| 1350 | * but possible) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1351 | */ |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1352 | } |
Mike Galbraith | 614ee1f | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1353 | skip: |
Peter Zijlstra | 1b12bbc | 2008-08-11 09:30:22 +0200 | [diff] [blame] | 1354 | double_unlock_balance(this_rq, src_rq); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | return ret; |
| 1358 | } |
| 1359 | |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1360 | static void pre_schedule_rt(struct rq *rq, struct task_struct *prev) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1361 | { |
| 1362 | /* Try to pull RT tasks here if we lower this rq's prio */ |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1363 | if (unlikely(rt_task(prev)) && rq->rt.highest_prio.curr > prev->prio) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1364 | pull_rt_task(rq); |
| 1365 | } |
| 1366 | |
Gregory Haskins | 967fc04 | 2008-12-29 09:39:52 -0500 | [diff] [blame] | 1367 | /* |
| 1368 | * assumes rq->lock is held |
| 1369 | */ |
| 1370 | static int needs_post_schedule_rt(struct rq *rq) |
| 1371 | { |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1372 | return has_pushable_tasks(rq); |
Gregory Haskins | 967fc04 | 2008-12-29 09:39:52 -0500 | [diff] [blame] | 1373 | } |
| 1374 | |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1375 | static void post_schedule_rt(struct rq *rq) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1376 | { |
| 1377 | /* |
Gregory Haskins | 967fc04 | 2008-12-29 09:39:52 -0500 | [diff] [blame] | 1378 | * This is only called if needs_post_schedule_rt() indicates that |
| 1379 | * we need to push tasks away |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1380 | */ |
Gregory Haskins | 967fc04 | 2008-12-29 09:39:52 -0500 | [diff] [blame] | 1381 | spin_lock_irq(&rq->lock); |
| 1382 | push_rt_tasks(rq); |
| 1383 | spin_unlock_irq(&rq->lock); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1384 | } |
| 1385 | |
Gregory Haskins | 8ae121a | 2008-04-23 07:13:29 -0400 | [diff] [blame] | 1386 | /* |
| 1387 | * If we are not running and we are not going to reschedule soon, we should |
| 1388 | * try to push tasks away now |
| 1389 | */ |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1390 | static void task_wake_up_rt(struct rq *rq, struct task_struct *p) |
Steven Rostedt | 4642daf | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1391 | { |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1392 | if (!task_running(rq, p) && |
Gregory Haskins | 8ae121a | 2008-04-23 07:13:29 -0400 | [diff] [blame] | 1393 | !test_tsk_need_resched(rq->curr) && |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1394 | has_pushable_tasks(rq) && |
Gregory Haskins | 777c2f3 | 2008-12-29 09:39:50 -0500 | [diff] [blame] | 1395 | p->rt.nr_cpus_allowed > 1) |
Steven Rostedt | 4642daf | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1396 | push_rt_tasks(rq); |
| 1397 | } |
| 1398 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1399 | static unsigned long |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1400 | load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1401 | unsigned long max_load_move, |
| 1402 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 1403 | int *all_pinned, int *this_best_prio) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1404 | { |
Steven Rostedt | c7a1e46 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1405 | /* don't touch RT tasks */ |
| 1406 | return 0; |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1407 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1408 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1409 | static int |
| 1410 | move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1411 | struct sched_domain *sd, enum cpu_idle_type idle) |
| 1412 | { |
Steven Rostedt | c7a1e46 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1413 | /* don't touch RT tasks */ |
| 1414 | return 0; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1415 | } |
Ingo Molnar | deeeccd | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1416 | |
Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 1417 | static void set_cpus_allowed_rt(struct task_struct *p, |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1418 | const struct cpumask *new_mask) |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1419 | { |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1420 | int weight = cpumask_weight(new_mask); |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1421 | |
| 1422 | BUG_ON(!rt_task(p)); |
| 1423 | |
| 1424 | /* |
| 1425 | * Update the migration status of the RQ if we have an RT task |
| 1426 | * which is running AND changing its weight value. |
| 1427 | */ |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1428 | if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) { |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1429 | struct rq *rq = task_rq(p); |
| 1430 | |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1431 | if (!task_current(rq, p)) { |
| 1432 | /* |
| 1433 | * Make sure we dequeue this task from the pushable list |
| 1434 | * before going further. It will either remain off of |
| 1435 | * the list because we are no longer pushable, or it |
| 1436 | * will be requeued. |
| 1437 | */ |
| 1438 | if (p->rt.nr_cpus_allowed > 1) |
| 1439 | dequeue_pushable_task(rq, p); |
| 1440 | |
| 1441 | /* |
| 1442 | * Requeue if our weight is changing and still > 1 |
| 1443 | */ |
| 1444 | if (weight > 1) |
| 1445 | enqueue_pushable_task(rq, p); |
| 1446 | |
| 1447 | } |
| 1448 | |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1449 | if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) { |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1450 | rq->rt.rt_nr_migratory++; |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1451 | } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) { |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1452 | BUG_ON(!rq->rt.rt_nr_migratory); |
| 1453 | rq->rt.rt_nr_migratory--; |
| 1454 | } |
| 1455 | |
| 1456 | update_rt_migration(rq); |
| 1457 | } |
| 1458 | |
Rusty Russell | 96f874e2 | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1459 | cpumask_copy(&p->cpus_allowed, new_mask); |
Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1460 | p->rt.nr_cpus_allowed = weight; |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1461 | } |
Ingo Molnar | deeeccd | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1462 | |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1463 | /* Assumes rq->lock is held */ |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 1464 | static void rq_online_rt(struct rq *rq) |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1465 | { |
| 1466 | if (rq->rt.overloaded) |
| 1467 | rt_set_overload(rq); |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1468 | |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 1469 | __enable_runtime(rq); |
| 1470 | |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1471 | cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr); |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | /* Assumes rq->lock is held */ |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 1475 | static void rq_offline_rt(struct rq *rq) |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1476 | { |
| 1477 | if (rq->rt.overloaded) |
| 1478 | rt_clear_overload(rq); |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1479 | |
Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 1480 | __disable_runtime(rq); |
| 1481 | |
Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1482 | cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID); |
Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1483 | } |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1484 | |
| 1485 | /* |
| 1486 | * When switch from the rt queue, we bring ourselves to a position |
| 1487 | * that we might want to pull RT tasks from other runqueues. |
| 1488 | */ |
| 1489 | static void switched_from_rt(struct rq *rq, struct task_struct *p, |
| 1490 | int running) |
| 1491 | { |
| 1492 | /* |
| 1493 | * If there are other RT tasks then we will reschedule |
| 1494 | * and the scheduling of the other RT tasks will handle |
| 1495 | * the balancing. But if we are the last RT task |
| 1496 | * we may need to handle the pulling of RT tasks |
| 1497 | * now. |
| 1498 | */ |
| 1499 | if (!rq->rt.rt_nr_running) |
| 1500 | pull_rt_task(rq); |
| 1501 | } |
Rusty Russell | 3d8cbdf | 2008-11-25 09:58:41 +1030 | [diff] [blame] | 1502 | |
| 1503 | static inline void init_sched_rt_class(void) |
| 1504 | { |
| 1505 | unsigned int i; |
| 1506 | |
| 1507 | for_each_possible_cpu(i) |
Mike Travis | 6ca09df | 2008-12-31 18:08:45 -0800 | [diff] [blame] | 1508 | alloc_cpumask_var_node(&per_cpu(local_cpu_mask, i), |
| 1509 | GFP_KERNEL, cpu_to_node(i)); |
Rusty Russell | 3d8cbdf | 2008-11-25 09:58:41 +1030 | [diff] [blame] | 1510 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1511 | #endif /* CONFIG_SMP */ |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1512 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1513 | /* |
| 1514 | * When switching a task to RT, we may overload the runqueue |
| 1515 | * with RT tasks. In this case we try to push them off to |
| 1516 | * other runqueues. |
| 1517 | */ |
| 1518 | static void switched_to_rt(struct rq *rq, struct task_struct *p, |
| 1519 | int running) |
| 1520 | { |
| 1521 | int check_resched = 1; |
| 1522 | |
| 1523 | /* |
| 1524 | * If we are already running, then there's nothing |
| 1525 | * that needs to be done. But if we are not running |
| 1526 | * we may need to preempt the current running task. |
| 1527 | * If that current running task is also an RT task |
| 1528 | * then see if we can move to another run queue. |
| 1529 | */ |
| 1530 | if (!running) { |
| 1531 | #ifdef CONFIG_SMP |
| 1532 | if (rq->rt.overloaded && push_rt_task(rq) && |
| 1533 | /* Don't resched if we changed runqueues */ |
| 1534 | rq != task_rq(p)) |
| 1535 | check_resched = 0; |
| 1536 | #endif /* CONFIG_SMP */ |
| 1537 | if (check_resched && p->prio < rq->curr->prio) |
| 1538 | resched_task(rq->curr); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * Priority of the task has changed. This may cause |
| 1544 | * us to initiate a push or pull. |
| 1545 | */ |
| 1546 | static void prio_changed_rt(struct rq *rq, struct task_struct *p, |
| 1547 | int oldprio, int running) |
| 1548 | { |
| 1549 | if (running) { |
| 1550 | #ifdef CONFIG_SMP |
| 1551 | /* |
| 1552 | * If our priority decreases while running, we |
| 1553 | * may need to pull tasks to this runqueue. |
| 1554 | */ |
| 1555 | if (oldprio < p->prio) |
| 1556 | pull_rt_task(rq); |
| 1557 | /* |
| 1558 | * If there's a higher priority task waiting to run |
Steven Rostedt | 6fa46fa | 2008-03-05 10:00:12 -0500 | [diff] [blame] | 1559 | * then reschedule. Note, the above pull_rt_task |
| 1560 | * can release the rq lock and p could migrate. |
| 1561 | * Only reschedule if p is still on the same runqueue. |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1562 | */ |
Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1563 | if (p->prio > rq->rt.highest_prio.curr && rq->curr == p) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1564 | resched_task(p); |
| 1565 | #else |
| 1566 | /* For UP simply resched on drop of prio */ |
| 1567 | if (oldprio < p->prio) |
| 1568 | resched_task(p); |
| 1569 | #endif /* CONFIG_SMP */ |
| 1570 | } else { |
| 1571 | /* |
| 1572 | * This task is not running, but if it is |
| 1573 | * greater than the current running task |
| 1574 | * then reschedule. |
| 1575 | */ |
| 1576 | if (p->prio < rq->curr->prio) |
| 1577 | resched_task(rq->curr); |
| 1578 | } |
| 1579 | } |
| 1580 | |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1581 | static void watchdog(struct rq *rq, struct task_struct *p) |
| 1582 | { |
| 1583 | unsigned long soft, hard; |
| 1584 | |
| 1585 | if (!p->signal) |
| 1586 | return; |
| 1587 | |
| 1588 | soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur; |
| 1589 | hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max; |
| 1590 | |
| 1591 | if (soft != RLIM_INFINITY) { |
| 1592 | unsigned long next; |
| 1593 | |
| 1594 | p->rt.timeout++; |
| 1595 | next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ); |
Peter Zijlstra | 5a52dd5 | 2008-01-25 21:08:32 +0100 | [diff] [blame] | 1596 | if (p->rt.timeout > next) |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1597 | p->cputime_expires.sched_exp = p->se.sum_exec_runtime; |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1598 | } |
| 1599 | } |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1600 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1601 | 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] | 1602 | { |
Peter Zijlstra | 67e2be0 | 2007-12-20 15:01:17 +0100 | [diff] [blame] | 1603 | update_curr_rt(rq); |
| 1604 | |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1605 | watchdog(rq, p); |
| 1606 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1607 | /* |
| 1608 | * RR tasks need a special form of timeslice management. |
| 1609 | * FIFO tasks have no timeslices. |
| 1610 | */ |
| 1611 | if (p->policy != SCHED_RR) |
| 1612 | return; |
| 1613 | |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1614 | if (--p->rt.time_slice) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1615 | return; |
| 1616 | |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1617 | p->rt.time_slice = DEF_TIMESLICE; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1618 | |
Dmitry Adamushko | 98fbc79 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 1619 | /* |
| 1620 | * Requeue to the end of queue if we are not the only element |
| 1621 | * on the queue: |
| 1622 | */ |
Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1623 | if (p->rt.run_list.prev != p->rt.run_list.next) { |
Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1624 | requeue_task_rt(rq, p, 0); |
Dmitry Adamushko | 98fbc79 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 1625 | set_tsk_need_resched(p); |
| 1626 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1629 | static void set_curr_task_rt(struct rq *rq) |
| 1630 | { |
| 1631 | struct task_struct *p = rq->curr; |
| 1632 | |
| 1633 | p->se.exec_start = rq->clock; |
Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1634 | |
| 1635 | /* The running task is never eligible for pushing */ |
| 1636 | dequeue_pushable_task(rq, p); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
Harvey Harrison | 2abdad0 | 2008-04-25 10:53:13 -0700 | [diff] [blame] | 1639 | static const struct sched_class rt_sched_class = { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1640 | .next = &fair_sched_class, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1641 | .enqueue_task = enqueue_task_rt, |
| 1642 | .dequeue_task = dequeue_task_rt, |
| 1643 | .yield_task = yield_task_rt, |
| 1644 | |
| 1645 | .check_preempt_curr = check_preempt_curr_rt, |
| 1646 | |
| 1647 | .pick_next_task = pick_next_task_rt, |
| 1648 | .put_prev_task = put_prev_task_rt, |
| 1649 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1650 | #ifdef CONFIG_SMP |
Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 1651 | .select_task_rq = select_task_rq_rt, |
| 1652 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1653 | .load_balance = load_balance_rt, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1654 | .move_one_task = move_one_task_rt, |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1655 | .set_cpus_allowed = set_cpus_allowed_rt, |
Gregory Haskins | 1f11eb6a | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 1656 | .rq_online = rq_online_rt, |
| 1657 | .rq_offline = rq_offline_rt, |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1658 | .pre_schedule = pre_schedule_rt, |
Gregory Haskins | 967fc04 | 2008-12-29 09:39:52 -0500 | [diff] [blame] | 1659 | .needs_post_schedule = needs_post_schedule_rt, |
Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1660 | .post_schedule = post_schedule_rt, |
| 1661 | .task_wake_up = task_wake_up_rt, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1662 | .switched_from = switched_from_rt, |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1663 | #endif |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1664 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1665 | .set_curr_task = set_curr_task_rt, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1666 | .task_tick = task_tick_rt, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1667 | |
| 1668 | .prio_changed = prio_changed_rt, |
| 1669 | .switched_to = switched_to_rt, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1670 | }; |
Peter Zijlstra | ada18de | 2008-06-19 14:22:24 +0200 | [diff] [blame] | 1671 | |
| 1672 | #ifdef CONFIG_SCHED_DEBUG |
| 1673 | extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq); |
| 1674 | |
| 1675 | static void print_rt_stats(struct seq_file *m, int cpu) |
| 1676 | { |
| 1677 | struct rt_rq *rt_rq; |
| 1678 | |
| 1679 | rcu_read_lock(); |
| 1680 | for_each_leaf_rt_rq(rt_rq, cpu_rq(cpu)) |
| 1681 | print_rt_rq(m, cpu, rt_rq); |
| 1682 | rcu_read_unlock(); |
| 1683 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 1684 | #endif /* CONFIG_SCHED_DEBUG */ |
Rusty Russell | 0e3900e | 2008-11-25 02:35:13 +1030 | [diff] [blame] | 1685 | |