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 | |
| 8 | /* |
| 9 | * The "RT overload" flag: it gets set if a CPU has more than |
| 10 | * one runnable RT task. |
| 11 | */ |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 12 | static cpumask_t rt_overload_mask; |
| 13 | static atomic_t rto_count; |
Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 14 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 15 | static inline int rt_overloaded(void) |
| 16 | { |
| 17 | return atomic_read(&rto_count); |
| 18 | } |
Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 19 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 20 | static inline void rt_set_overload(struct rq *rq) |
| 21 | { |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 22 | rq->rt.overloaded = 1; |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 23 | cpu_set(rq->cpu, rt_overload_mask); |
| 24 | /* |
| 25 | * Make sure the mask is visible before we set |
| 26 | * the overload count. That is checked to determine |
| 27 | * if we should look at the mask. It would be a shame |
| 28 | * if we looked at the mask, but the mask was not |
| 29 | * updated yet. |
| 30 | */ |
| 31 | wmb(); |
| 32 | atomic_inc(&rto_count); |
| 33 | } |
Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 34 | |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 35 | static inline void rt_clear_overload(struct rq *rq) |
| 36 | { |
| 37 | /* the order here really doesn't matter */ |
| 38 | atomic_dec(&rto_count); |
| 39 | cpu_clear(rq->cpu, rt_overload_mask); |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 40 | rq->rt.overloaded = 0; |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 41 | } |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 42 | |
| 43 | static void update_rt_migration(struct rq *rq) |
| 44 | { |
| 45 | if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) |
| 46 | rt_set_overload(rq); |
| 47 | else |
| 48 | rt_clear_overload(rq); |
| 49 | } |
Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 50 | #endif /* CONFIG_SMP */ |
| 51 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 52 | /* |
| 53 | * Update the current task's runtime statistics. Skip current tasks that |
| 54 | * are not in our scheduling class. |
| 55 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 56 | static void update_curr_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 57 | { |
| 58 | struct task_struct *curr = rq->curr; |
| 59 | u64 delta_exec; |
| 60 | |
| 61 | if (!task_has_rt_policy(curr)) |
| 62 | return; |
| 63 | |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 64 | delta_exec = rq->clock - curr->se.exec_start; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 65 | if (unlikely((s64)delta_exec < 0)) |
| 66 | delta_exec = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 67 | |
| 68 | 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] | 69 | |
| 70 | curr->se.sum_exec_runtime += delta_exec; |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 71 | curr->se.exec_start = rq->clock; |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 72 | cpuacct_charge(curr, delta_exec); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 75 | static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq) |
| 76 | { |
| 77 | WARN_ON(!rt_task(p)); |
| 78 | rq->rt.rt_nr_running++; |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 79 | #ifdef CONFIG_SMP |
| 80 | if (p->prio < rq->rt.highest_prio) |
| 81 | rq->rt.highest_prio = p->prio; |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 82 | if (p->nr_cpus_allowed > 1) |
| 83 | rq->rt.rt_nr_migratory++; |
| 84 | |
| 85 | update_rt_migration(rq); |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 86 | #endif /* CONFIG_SMP */ |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq) |
| 90 | { |
| 91 | WARN_ON(!rt_task(p)); |
| 92 | WARN_ON(!rq->rt.rt_nr_running); |
| 93 | rq->rt.rt_nr_running--; |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 94 | #ifdef CONFIG_SMP |
| 95 | if (rq->rt.rt_nr_running) { |
| 96 | struct rt_prio_array *array; |
| 97 | |
| 98 | WARN_ON(p->prio < rq->rt.highest_prio); |
| 99 | if (p->prio == rq->rt.highest_prio) { |
| 100 | /* recalculate */ |
| 101 | array = &rq->rt.active; |
| 102 | rq->rt.highest_prio = |
| 103 | sched_find_first_bit(array->bitmap); |
| 104 | } /* otherwise leave rq->highest prio alone */ |
| 105 | } else |
| 106 | rq->rt.highest_prio = MAX_RT_PRIO; |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 107 | if (p->nr_cpus_allowed > 1) |
| 108 | rq->rt.rt_nr_migratory--; |
| 109 | |
| 110 | update_rt_migration(rq); |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 111 | #endif /* CONFIG_SMP */ |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Ingo Molnar | fd390f6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 114 | static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 115 | { |
| 116 | struct rt_prio_array *array = &rq->rt.active; |
| 117 | |
| 118 | list_add_tail(&p->run_list, array->queue + p->prio); |
| 119 | __set_bit(p->prio, array->bitmap); |
Srivatsa Vaddagiri | 58e2d4c | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 120 | inc_cpu_load(rq, p->se.load.weight); |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 121 | |
| 122 | inc_rt_tasks(p, rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Adding/removing a task to/from a priority array: |
| 127 | */ |
Ingo Molnar | f02231e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 128 | 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] | 129 | { |
| 130 | struct rt_prio_array *array = &rq->rt.active; |
| 131 | |
Ingo Molnar | f1e14ef | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 132 | update_curr_rt(rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 133 | |
| 134 | list_del(&p->run_list); |
| 135 | if (list_empty(array->queue + p->prio)) |
| 136 | __clear_bit(p->prio, array->bitmap); |
Srivatsa Vaddagiri | 58e2d4c | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 137 | dec_cpu_load(rq, p->se.load.weight); |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 138 | |
| 139 | dec_rt_tasks(p, rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Put task to the end of the run list without the overhead of dequeue |
| 144 | * followed by enqueue. |
| 145 | */ |
| 146 | static void requeue_task_rt(struct rq *rq, struct task_struct *p) |
| 147 | { |
| 148 | struct rt_prio_array *array = &rq->rt.active; |
| 149 | |
| 150 | list_move_tail(&p->run_list, array->queue + p->prio); |
| 151 | } |
| 152 | |
| 153 | static void |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 154 | yield_task_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 155 | { |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 156 | requeue_task_rt(rq, rq->curr); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 159 | #ifdef CONFIG_SMP |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 160 | static int find_lowest_rq(struct task_struct *task); |
| 161 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 162 | static int select_task_rq_rt(struct task_struct *p, int sync) |
| 163 | { |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 164 | struct rq *rq = task_rq(p); |
| 165 | |
| 166 | /* |
Steven Rostedt | e1f47d8 | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 167 | * If the current task is an RT task, then |
| 168 | * try to see if we can wake this RT task up on another |
| 169 | * runqueue. Otherwise simply start this RT task |
| 170 | * on its current runqueue. |
| 171 | * |
| 172 | * We want to avoid overloading runqueues. Even if |
| 173 | * the RT task is of higher priority than the current RT task. |
| 174 | * RT tasks behave differently than other tasks. If |
| 175 | * one gets preempted, we try to push it off to another queue. |
| 176 | * So trying to keep a preempting RT task on the same |
| 177 | * cache hot CPU will force the running RT task to |
| 178 | * a cold CPU. So we waste all the cache for the lower |
| 179 | * RT task in hopes of saving some of a RT task |
| 180 | * that is just being woken and probably will have |
| 181 | * cold cache anyway. |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 182 | */ |
Gregory Haskins | 17b3279 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 183 | if (unlikely(rt_task(rq->curr)) && |
| 184 | (p->nr_cpus_allowed > 1)) { |
Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 185 | int cpu = find_lowest_rq(p); |
| 186 | |
| 187 | return (cpu == -1) ? task_cpu(p) : cpu; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Otherwise, just let it ride on the affined RQ and the |
| 192 | * post-schedule router will push the preempted task away |
| 193 | */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 194 | return task_cpu(p); |
| 195 | } |
| 196 | #endif /* CONFIG_SMP */ |
| 197 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 198 | /* |
| 199 | * Preempt the current task with a newly woken task if needed: |
| 200 | */ |
| 201 | static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p) |
| 202 | { |
| 203 | if (p->prio < rq->curr->prio) |
| 204 | resched_task(rq->curr); |
| 205 | } |
| 206 | |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 207 | static struct task_struct *pick_next_task_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 208 | { |
| 209 | struct rt_prio_array *array = &rq->rt.active; |
| 210 | struct task_struct *next; |
| 211 | struct list_head *queue; |
| 212 | int idx; |
| 213 | |
| 214 | idx = sched_find_first_bit(array->bitmap); |
| 215 | if (idx >= MAX_RT_PRIO) |
| 216 | return NULL; |
| 217 | |
| 218 | queue = array->queue + idx; |
| 219 | next = list_entry(queue->next, struct task_struct, run_list); |
| 220 | |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 221 | next->se.exec_start = rq->clock; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 222 | |
| 223 | return next; |
| 224 | } |
| 225 | |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 226 | 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] | 227 | { |
Ingo Molnar | f1e14ef | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 228 | update_curr_rt(rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 229 | p->se.exec_start = 0; |
| 230 | } |
| 231 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 232 | #ifdef CONFIG_SMP |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 233 | /* Only try algorithms three times */ |
| 234 | #define RT_MAX_TRIES 3 |
| 235 | |
| 236 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest); |
| 237 | static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep); |
| 238 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 239 | static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu) |
| 240 | { |
| 241 | if (!task_running(rq, p) && |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 242 | (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) && |
| 243 | (p->nr_cpus_allowed > 1)) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 244 | return 1; |
| 245 | return 0; |
| 246 | } |
| 247 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 248 | /* Return the second highest RT task, NULL otherwise */ |
Ingo Molnar | 79064fb | 2008-01-25 21:08:14 +0100 | [diff] [blame] | 249 | 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] | 250 | { |
| 251 | struct rt_prio_array *array = &rq->rt.active; |
| 252 | struct task_struct *next; |
| 253 | struct list_head *queue; |
| 254 | int idx; |
| 255 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 256 | if (likely(rq->rt.rt_nr_running < 2)) |
| 257 | return NULL; |
| 258 | |
| 259 | idx = sched_find_first_bit(array->bitmap); |
| 260 | if (unlikely(idx >= MAX_RT_PRIO)) { |
| 261 | WARN_ON(1); /* rt_nr_running is bad */ |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | queue = array->queue + idx; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 266 | BUG_ON(list_empty(queue)); |
| 267 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 268 | next = list_entry(queue->next, struct task_struct, run_list); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 269 | if (unlikely(pick_rt_task(rq, next, cpu))) |
| 270 | goto out; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 271 | |
| 272 | if (queue->next->next != queue) { |
| 273 | /* same prio task */ |
Ingo Molnar | 79064fb | 2008-01-25 21:08:14 +0100 | [diff] [blame] | 274 | next = list_entry(queue->next->next, struct task_struct, |
| 275 | run_list); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 276 | if (pick_rt_task(rq, next, cpu)) |
| 277 | goto out; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 280 | retry: |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 281 | /* slower, but more flexible */ |
| 282 | idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1); |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 283 | if (unlikely(idx >= MAX_RT_PRIO)) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 284 | return NULL; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 285 | |
| 286 | queue = array->queue + idx; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 287 | BUG_ON(list_empty(queue)); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 288 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 289 | list_for_each_entry(next, queue, run_list) { |
| 290 | if (pick_rt_task(rq, next, cpu)) |
| 291 | goto out; |
| 292 | } |
| 293 | |
| 294 | goto retry; |
| 295 | |
| 296 | out: |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 297 | return next; |
| 298 | } |
| 299 | |
| 300 | static DEFINE_PER_CPU(cpumask_t, local_cpu_mask); |
| 301 | |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 302 | static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask) |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 303 | { |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 304 | int lowest_prio = -1; |
Steven Rostedt | 610bf05 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 305 | int lowest_cpu = -1; |
Gregory Haskins | 06f90db | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 306 | int count = 0; |
Steven Rostedt | 610bf05 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 307 | int cpu; |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 308 | |
Steven Rostedt | 610bf05 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 309 | cpus_and(*lowest_mask, cpu_online_map, task->cpus_allowed); |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * Scan each rq for the lowest prio. |
| 313 | */ |
Steven Rostedt | 610bf05 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 314 | for_each_cpu_mask(cpu, *lowest_mask) { |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 315 | struct rq *rq = cpu_rq(cpu); |
| 316 | |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 317 | /* We look for lowest RT prio or non-rt CPU */ |
| 318 | if (rq->rt.highest_prio >= MAX_RT_PRIO) { |
Steven Rostedt | 610bf05 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 319 | /* |
| 320 | * if we already found a low RT queue |
| 321 | * and now we found this non-rt queue |
| 322 | * clear the mask and set our bit. |
| 323 | * Otherwise just return the queue as is |
| 324 | * and the count==1 will cause the algorithm |
| 325 | * to use the first bit found. |
| 326 | */ |
| 327 | if (lowest_cpu != -1) { |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 328 | cpus_clear(*lowest_mask); |
Steven Rostedt | 610bf05 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 329 | cpu_set(rq->cpu, *lowest_mask); |
| 330 | } |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 331 | return 1; |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* no locking for now */ |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 335 | if ((rq->rt.highest_prio > task->prio) |
| 336 | && (rq->rt.highest_prio >= lowest_prio)) { |
| 337 | if (rq->rt.highest_prio > lowest_prio) { |
| 338 | /* new low - clear old data */ |
| 339 | lowest_prio = rq->rt.highest_prio; |
Steven Rostedt | 610bf05 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 340 | lowest_cpu = cpu; |
| 341 | count = 0; |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 342 | } |
Gregory Haskins | 06f90db | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 343 | count++; |
Steven Rostedt | 610bf05 | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 344 | } else |
| 345 | cpu_clear(cpu, *lowest_mask); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Clear out all the set bits that represent |
| 350 | * runqueues that were of higher prio than |
| 351 | * the lowest_prio. |
| 352 | */ |
| 353 | if (lowest_cpu > 0) { |
| 354 | /* |
| 355 | * Perhaps we could add another cpumask op to |
| 356 | * zero out bits. Like cpu_zero_bits(cpumask, nrbits); |
| 357 | * Then that could be optimized to use memset and such. |
| 358 | */ |
| 359 | for_each_cpu_mask(cpu, *lowest_mask) { |
| 360 | if (cpu >= lowest_cpu) |
| 361 | break; |
| 362 | cpu_clear(cpu, *lowest_mask); |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Gregory Haskins | 06f90db | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 366 | return count; |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask) |
| 370 | { |
| 371 | int first; |
| 372 | |
| 373 | /* "this_cpu" is cheaper to preempt than a remote processor */ |
| 374 | if ((this_cpu != -1) && cpu_isset(this_cpu, *mask)) |
| 375 | return this_cpu; |
| 376 | |
| 377 | first = first_cpu(*mask); |
| 378 | if (first != NR_CPUS) |
| 379 | return first; |
| 380 | |
| 381 | return -1; |
| 382 | } |
| 383 | |
| 384 | static int find_lowest_rq(struct task_struct *task) |
| 385 | { |
| 386 | struct sched_domain *sd; |
| 387 | cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask); |
| 388 | int this_cpu = smp_processor_id(); |
| 389 | int cpu = task_cpu(task); |
Gregory Haskins | 06f90db | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 390 | int count = find_lowest_cpus(task, lowest_mask); |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 391 | |
Gregory Haskins | 06f90db | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 392 | if (!count) |
| 393 | return -1; /* No targets found */ |
| 394 | |
| 395 | /* |
| 396 | * There is no sense in performing an optimal search if only one |
| 397 | * target is found. |
| 398 | */ |
| 399 | if (count == 1) |
| 400 | return first_cpu(*lowest_mask); |
Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 401 | |
| 402 | /* |
| 403 | * At this point we have built a mask of cpus representing the |
| 404 | * lowest priority tasks in the system. Now we want to elect |
| 405 | * the best one based on our affinity and topology. |
| 406 | * |
| 407 | * We prioritize the last cpu that the task executed on since |
| 408 | * it is most likely cache-hot in that location. |
| 409 | */ |
| 410 | if (cpu_isset(cpu, *lowest_mask)) |
| 411 | return cpu; |
| 412 | |
| 413 | /* |
| 414 | * Otherwise, we consult the sched_domains span maps to figure |
| 415 | * out which cpu is logically closest to our hot cache data. |
| 416 | */ |
| 417 | if (this_cpu == cpu) |
| 418 | this_cpu = -1; /* Skip this_cpu opt if the same */ |
| 419 | |
| 420 | for_each_domain(cpu, sd) { |
| 421 | if (sd->flags & SD_WAKE_AFFINE) { |
| 422 | cpumask_t domain_mask; |
| 423 | int best_cpu; |
| 424 | |
| 425 | cpus_and(domain_mask, sd->span, *lowest_mask); |
| 426 | |
| 427 | best_cpu = pick_optimal_cpu(this_cpu, |
| 428 | &domain_mask); |
| 429 | if (best_cpu != -1) |
| 430 | return best_cpu; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * And finally, if there were no matches within the domains |
| 436 | * just give the caller *something* to work with from the compatible |
| 437 | * locations. |
| 438 | */ |
| 439 | return pick_optimal_cpu(this_cpu, lowest_mask); |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 442 | /* Will lock the rq it finds */ |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 443 | 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] | 444 | { |
| 445 | struct rq *lowest_rq = NULL; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 446 | int tries; |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 447 | int cpu; |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 448 | |
| 449 | for (tries = 0; tries < RT_MAX_TRIES; tries++) { |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 450 | cpu = find_lowest_rq(task); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 451 | |
Gregory Haskins | 2de0b46 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 452 | if ((cpu == -1) || (cpu == rq->cpu)) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 453 | break; |
| 454 | |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 455 | lowest_rq = cpu_rq(cpu); |
| 456 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 457 | /* if the prio of this runqueue changed, try again */ |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 458 | if (double_lock_balance(rq, lowest_rq)) { |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 459 | /* |
| 460 | * We had to unlock the run queue. In |
| 461 | * the mean time, task could have |
| 462 | * migrated already or had its affinity changed. |
| 463 | * Also make sure that it wasn't scheduled on its rq. |
| 464 | */ |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 465 | if (unlikely(task_rq(task) != rq || |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 466 | !cpu_isset(lowest_rq->cpu, |
| 467 | task->cpus_allowed) || |
Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 468 | task_running(rq, task) || |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 469 | !task->se.on_rq)) { |
Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 470 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 471 | spin_unlock(&lowest_rq->lock); |
| 472 | lowest_rq = NULL; |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* If this rq is still suitable use it. */ |
| 478 | if (lowest_rq->rt.highest_prio > task->prio) |
| 479 | break; |
| 480 | |
| 481 | /* try again */ |
| 482 | spin_unlock(&lowest_rq->lock); |
| 483 | lowest_rq = NULL; |
| 484 | } |
| 485 | |
| 486 | return lowest_rq; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * If the current CPU has more than one RT task, see if the non |
| 491 | * running task can migrate over to a CPU that is running a task |
| 492 | * of lesser priority. |
| 493 | */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 494 | static int push_rt_task(struct rq *rq) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 495 | { |
| 496 | struct task_struct *next_task; |
| 497 | struct rq *lowest_rq; |
| 498 | int ret = 0; |
| 499 | int paranoid = RT_MAX_TRIES; |
| 500 | |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 501 | if (!rq->rt.overloaded) |
| 502 | return 0; |
| 503 | |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 504 | next_task = pick_next_highest_task_rt(rq, -1); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 505 | if (!next_task) |
| 506 | return 0; |
| 507 | |
| 508 | retry: |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 509 | if (unlikely(next_task == rq->curr)) { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 510 | WARN_ON(1); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 511 | return 0; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 512 | } |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * It's possible that the next_task slipped in of |
| 516 | * higher priority than current. If that's the case |
| 517 | * just reschedule current. |
| 518 | */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 519 | if (unlikely(next_task->prio < rq->curr->prio)) { |
| 520 | resched_task(rq->curr); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 524 | /* We might release rq lock */ |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 525 | get_task_struct(next_task); |
| 526 | |
| 527 | /* find_lock_lowest_rq locks the rq if found */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 528 | lowest_rq = find_lock_lowest_rq(next_task, rq); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 529 | if (!lowest_rq) { |
| 530 | struct task_struct *task; |
| 531 | /* |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 532 | * find lock_lowest_rq releases rq->lock |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 533 | * so it is possible that next_task has changed. |
| 534 | * If it has, then try again. |
| 535 | */ |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 536 | task = pick_next_highest_task_rt(rq, -1); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 537 | if (unlikely(task != next_task) && task && paranoid--) { |
| 538 | put_task_struct(next_task); |
| 539 | next_task = task; |
| 540 | goto retry; |
| 541 | } |
| 542 | goto out; |
| 543 | } |
| 544 | |
Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 545 | deactivate_task(rq, next_task, 0); |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 546 | set_task_cpu(next_task, lowest_rq->cpu); |
| 547 | activate_task(lowest_rq, next_task, 0); |
| 548 | |
| 549 | resched_task(lowest_rq->curr); |
| 550 | |
| 551 | spin_unlock(&lowest_rq->lock); |
| 552 | |
| 553 | ret = 1; |
| 554 | out: |
| 555 | put_task_struct(next_task); |
| 556 | |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * TODO: Currently we just use the second highest prio task on |
| 562 | * the queue, and stop when it can't migrate (or there's |
| 563 | * no more RT tasks). There may be a case where a lower |
| 564 | * priority RT task has a different affinity than the |
| 565 | * higher RT task. In this case the lower RT task could |
| 566 | * possibly be able to migrate where as the higher priority |
| 567 | * RT task could not. We currently ignore this issue. |
| 568 | * Enhancements are welcome! |
| 569 | */ |
| 570 | static void push_rt_tasks(struct rq *rq) |
| 571 | { |
| 572 | /* push_rt_task will return true if it moved an RT */ |
| 573 | while (push_rt_task(rq)) |
| 574 | ; |
| 575 | } |
| 576 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 577 | static int pull_rt_task(struct rq *this_rq) |
| 578 | { |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame^] | 579 | int this_cpu = this_rq->cpu, ret = 0, cpu; |
| 580 | struct task_struct *p, *next; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 581 | struct rq *src_rq; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 582 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 583 | /* |
| 584 | * If cpusets are used, and we have overlapping |
| 585 | * run queue cpusets, then this algorithm may not catch all. |
| 586 | * This is just the price you pay on trying to keep |
| 587 | * dirtying caches down on large SMP machines. |
| 588 | */ |
| 589 | if (likely(!rt_overloaded())) |
| 590 | return 0; |
| 591 | |
| 592 | next = pick_next_task_rt(this_rq); |
| 593 | |
Ingo Molnar | 6e1938d | 2008-01-25 21:08:16 +0100 | [diff] [blame] | 594 | for_each_cpu_mask(cpu, rt_overload_mask) { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 595 | if (this_cpu == cpu) |
| 596 | continue; |
| 597 | |
| 598 | src_rq = cpu_rq(cpu); |
| 599 | if (unlikely(src_rq->rt.rt_nr_running <= 1)) { |
| 600 | /* |
| 601 | * It is possible that overlapping cpusets |
| 602 | * will miss clearing a non overloaded runqueue. |
| 603 | * Clear it now. |
| 604 | */ |
| 605 | if (double_lock_balance(this_rq, src_rq)) { |
| 606 | /* unlocked our runqueue lock */ |
| 607 | struct task_struct *old_next = next; |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame^] | 608 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 609 | next = pick_next_task_rt(this_rq); |
| 610 | if (next != old_next) |
| 611 | ret = 1; |
| 612 | } |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame^] | 613 | if (likely(src_rq->rt.rt_nr_running <= 1)) { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 614 | /* |
| 615 | * Small chance that this_rq->curr changed |
| 616 | * but it's really harmless here. |
| 617 | */ |
| 618 | rt_clear_overload(this_rq); |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame^] | 619 | } else { |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 620 | /* |
| 621 | * Heh, the src_rq is now overloaded, since |
| 622 | * we already have the src_rq lock, go straight |
| 623 | * to pulling tasks from it. |
| 624 | */ |
| 625 | goto try_pulling; |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame^] | 626 | } |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 627 | spin_unlock(&src_rq->lock); |
| 628 | continue; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * We can potentially drop this_rq's lock in |
| 633 | * double_lock_balance, and another CPU could |
| 634 | * steal our next task - hence we must cause |
| 635 | * the caller to recalculate the next task |
| 636 | * in that case: |
| 637 | */ |
| 638 | if (double_lock_balance(this_rq, src_rq)) { |
| 639 | struct task_struct *old_next = next; |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame^] | 640 | |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 641 | next = pick_next_task_rt(this_rq); |
| 642 | if (next != old_next) |
| 643 | ret = 1; |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * Are there still pullable RT tasks? |
| 648 | */ |
| 649 | if (src_rq->rt.rt_nr_running <= 1) { |
| 650 | spin_unlock(&src_rq->lock); |
| 651 | continue; |
| 652 | } |
| 653 | |
| 654 | try_pulling: |
| 655 | p = pick_next_highest_task_rt(src_rq, this_cpu); |
| 656 | |
| 657 | /* |
| 658 | * Do we have an RT task that preempts |
| 659 | * the to-be-scheduled task? |
| 660 | */ |
| 661 | if (p && (!next || (p->prio < next->prio))) { |
| 662 | WARN_ON(p == src_rq->curr); |
| 663 | WARN_ON(!p->se.on_rq); |
| 664 | |
| 665 | /* |
| 666 | * There's a chance that p is higher in priority |
| 667 | * than what's currently running on its cpu. |
| 668 | * This is just that p is wakeing up and hasn't |
| 669 | * had a chance to schedule. We only pull |
| 670 | * p if it is lower in priority than the |
| 671 | * current task on the run queue or |
| 672 | * this_rq next task is lower in prio than |
| 673 | * the current task on that rq. |
| 674 | */ |
| 675 | if (p->prio < src_rq->curr->prio || |
| 676 | (next && next->prio < src_rq->curr->prio)) |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame^] | 677 | goto out; |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 678 | |
| 679 | ret = 1; |
| 680 | |
| 681 | deactivate_task(src_rq, p, 0); |
| 682 | set_task_cpu(p, this_cpu); |
| 683 | activate_task(this_rq, p, 0); |
| 684 | /* |
| 685 | * We continue with the search, just in |
| 686 | * case there's an even higher prio task |
| 687 | * in another runqueue. (low likelyhood |
| 688 | * but possible) |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame^] | 689 | * |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 690 | * Update next so that we won't pick a task |
| 691 | * on another cpu with a priority lower (or equal) |
| 692 | * than the one we just picked. |
| 693 | */ |
| 694 | next = p; |
| 695 | |
| 696 | } |
Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame^] | 697 | out: |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 698 | spin_unlock(&src_rq->lock); |
| 699 | } |
| 700 | |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | static void schedule_balance_rt(struct rq *rq, |
| 705 | struct task_struct *prev) |
| 706 | { |
| 707 | /* Try to pull RT tasks here if we lower this rq's prio */ |
| 708 | if (unlikely(rt_task(prev)) && |
| 709 | rq->rt.highest_prio > prev->prio) |
| 710 | pull_rt_task(rq); |
| 711 | } |
| 712 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 713 | static void schedule_tail_balance_rt(struct rq *rq) |
| 714 | { |
| 715 | /* |
| 716 | * If we have more than one rt_task queued, then |
| 717 | * see if we can push the other rt_tasks off to other CPUS. |
| 718 | * Note we may release the rq lock, and since |
| 719 | * the lock was owned by prev, we need to release it |
| 720 | * first via finish_lock_switch and then reaquire it here. |
| 721 | */ |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 722 | if (unlikely(rq->rt.overloaded)) { |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 723 | spin_lock_irq(&rq->lock); |
| 724 | push_rt_tasks(rq); |
| 725 | spin_unlock_irq(&rq->lock); |
| 726 | } |
| 727 | } |
| 728 | |
Steven Rostedt | 4642daf | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 729 | |
| 730 | static void wakeup_balance_rt(struct rq *rq, struct task_struct *p) |
| 731 | { |
| 732 | if (unlikely(rt_task(p)) && |
| 733 | !task_running(rq, p) && |
Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 734 | (p->prio >= rq->rt.highest_prio) && |
| 735 | rq->rt.overloaded) |
Steven Rostedt | 4642daf | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 736 | push_rt_tasks(rq); |
| 737 | } |
| 738 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 739 | static unsigned long |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 740 | 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] | 741 | unsigned long max_load_move, |
| 742 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 743 | int *all_pinned, int *this_best_prio) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 744 | { |
Steven Rostedt | c7a1e46 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 745 | /* don't touch RT tasks */ |
| 746 | return 0; |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 747 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 748 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 749 | static int |
| 750 | move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 751 | struct sched_domain *sd, enum cpu_idle_type idle) |
| 752 | { |
Steven Rostedt | c7a1e46 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 753 | /* don't touch RT tasks */ |
| 754 | return 0; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 755 | } |
Ingo Molnar | deeeccd | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 756 | |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 757 | static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask) |
| 758 | { |
| 759 | int weight = cpus_weight(*new_mask); |
| 760 | |
| 761 | BUG_ON(!rt_task(p)); |
| 762 | |
| 763 | /* |
| 764 | * Update the migration status of the RQ if we have an RT task |
| 765 | * which is running AND changing its weight value. |
| 766 | */ |
| 767 | if (p->se.on_rq && (weight != p->nr_cpus_allowed)) { |
| 768 | struct rq *rq = task_rq(p); |
| 769 | |
Ingo Molnar | deeeccd | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 770 | if ((p->nr_cpus_allowed <= 1) && (weight > 1)) { |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 771 | rq->rt.rt_nr_migratory++; |
Ingo Molnar | deeeccd | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 772 | } else if ((p->nr_cpus_allowed > 1) && (weight <= 1)) { |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 773 | BUG_ON(!rq->rt.rt_nr_migratory); |
| 774 | rq->rt.rt_nr_migratory--; |
| 775 | } |
| 776 | |
| 777 | update_rt_migration(rq); |
| 778 | } |
| 779 | |
| 780 | p->cpus_allowed = *new_mask; |
| 781 | p->nr_cpus_allowed = weight; |
| 782 | } |
Ingo Molnar | deeeccd | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 783 | |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 784 | #else /* CONFIG_SMP */ |
| 785 | # define schedule_tail_balance_rt(rq) do { } while (0) |
Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 786 | # define schedule_balance_rt(rq, prev) do { } while (0) |
Steven Rostedt | 4642daf | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 787 | # define wakeup_balance_rt(rq, p) do { } while (0) |
Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 788 | #endif /* CONFIG_SMP */ |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 789 | |
| 790 | static void task_tick_rt(struct rq *rq, struct task_struct *p) |
| 791 | { |
Peter Zijlstra | 67e2be0 | 2007-12-20 15:01:17 +0100 | [diff] [blame] | 792 | update_curr_rt(rq); |
| 793 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 794 | /* |
| 795 | * RR tasks need a special form of timeslice management. |
| 796 | * FIFO tasks have no timeslices. |
| 797 | */ |
| 798 | if (p->policy != SCHED_RR) |
| 799 | return; |
| 800 | |
| 801 | if (--p->time_slice) |
| 802 | return; |
| 803 | |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 804 | p->time_slice = DEF_TIMESLICE; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 805 | |
Dmitry Adamushko | 98fbc79 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 806 | /* |
| 807 | * Requeue to the end of queue if we are not the only element |
| 808 | * on the queue: |
| 809 | */ |
| 810 | if (p->run_list.prev != p->run_list.next) { |
| 811 | requeue_task_rt(rq, p); |
| 812 | set_tsk_need_resched(p); |
| 813 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 814 | } |
| 815 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 816 | static void set_curr_task_rt(struct rq *rq) |
| 817 | { |
| 818 | struct task_struct *p = rq->curr; |
| 819 | |
| 820 | p->se.exec_start = rq->clock; |
| 821 | } |
| 822 | |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 823 | const struct sched_class rt_sched_class = { |
| 824 | .next = &fair_sched_class, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 825 | .enqueue_task = enqueue_task_rt, |
| 826 | .dequeue_task = dequeue_task_rt, |
| 827 | .yield_task = yield_task_rt, |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 828 | #ifdef CONFIG_SMP |
| 829 | .select_task_rq = select_task_rq_rt, |
| 830 | #endif /* CONFIG_SMP */ |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 831 | |
| 832 | .check_preempt_curr = check_preempt_curr_rt, |
| 833 | |
| 834 | .pick_next_task = pick_next_task_rt, |
| 835 | .put_prev_task = put_prev_task_rt, |
| 836 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 837 | #ifdef CONFIG_SMP |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 838 | .load_balance = load_balance_rt, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 839 | .move_one_task = move_one_task_rt, |
Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 840 | .set_cpus_allowed = set_cpus_allowed_rt, |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 841 | #endif |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 842 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 843 | .set_curr_task = set_curr_task_rt, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 844 | .task_tick = task_tick_rt, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 845 | }; |