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