Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * kernel/sched.c |
| 3 | * |
| 4 | * Kernel scheduler and related syscalls |
| 5 | * |
| 6 | * Copyright (C) 1991-2002 Linus Torvalds |
| 7 | * |
| 8 | * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and |
| 9 | * make semaphores SMP safe |
| 10 | * 1998-11-19 Implemented schedule_timeout() and related stuff |
| 11 | * by Andrea Arcangeli |
| 12 | * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar: |
| 13 | * hybrid priority-list and round-robin design with |
| 14 | * an array-switch method of distributing timeslices |
| 15 | * and per-CPU runqueues. Cleanups and useful suggestions |
| 16 | * by Davide Libenzi, preemptible kernel bits by Robert Love. |
| 17 | * 2003-09-03 Interactivity tuning by Con Kolivas. |
| 18 | * 2004-04-02 Scheduler domains code by Nick Piggin |
| 19 | */ |
| 20 | |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/nmi.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <asm/uaccess.h> |
| 26 | #include <linux/highmem.h> |
| 27 | #include <linux/smp_lock.h> |
| 28 | #include <asm/mmu_context.h> |
| 29 | #include <linux/interrupt.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 30 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/completion.h> |
| 32 | #include <linux/kernel_stat.h> |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 33 | #include <linux/debug_locks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/security.h> |
| 35 | #include <linux/notifier.h> |
| 36 | #include <linux/profile.h> |
| 37 | #include <linux/suspend.h> |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 38 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/blkdev.h> |
| 40 | #include <linux/delay.h> |
| 41 | #include <linux/smp.h> |
| 42 | #include <linux/threads.h> |
| 43 | #include <linux/timer.h> |
| 44 | #include <linux/rcupdate.h> |
| 45 | #include <linux/cpu.h> |
| 46 | #include <linux/cpuset.h> |
| 47 | #include <linux/percpu.h> |
| 48 | #include <linux/kthread.h> |
| 49 | #include <linux/seq_file.h> |
| 50 | #include <linux/syscalls.h> |
| 51 | #include <linux/times.h> |
| 52 | #include <linux/acct.h> |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 53 | #include <linux/kprobes.h> |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 54 | #include <linux/delayacct.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <asm/tlb.h> |
| 56 | |
| 57 | #include <asm/unistd.h> |
| 58 | |
| 59 | /* |
| 60 | * Convert user-nice values [ -20 ... 0 ... 19 ] |
| 61 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], |
| 62 | * and back. |
| 63 | */ |
| 64 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) |
| 65 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) |
| 66 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) |
| 67 | |
| 68 | /* |
| 69 | * 'User priority' is the nice value converted to something we |
| 70 | * can work with better when scaling various scheduler parameters, |
| 71 | * it's a [ 0 ... 39 ] range. |
| 72 | */ |
| 73 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) |
| 74 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) |
| 75 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) |
| 76 | |
| 77 | /* |
| 78 | * Some helpers for converting nanosecond timing to jiffy resolution |
| 79 | */ |
| 80 | #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ)) |
| 81 | #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ)) |
| 82 | |
| 83 | /* |
| 84 | * These are the 'tuning knobs' of the scheduler: |
| 85 | * |
| 86 | * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger), |
| 87 | * default timeslice is 100 msecs, maximum timeslice is 800 msecs. |
| 88 | * Timeslices get refilled after they expire. |
| 89 | */ |
| 90 | #define MIN_TIMESLICE max(5 * HZ / 1000, 1) |
| 91 | #define DEF_TIMESLICE (100 * HZ / 1000) |
| 92 | #define ON_RUNQUEUE_WEIGHT 30 |
| 93 | #define CHILD_PENALTY 95 |
| 94 | #define PARENT_PENALTY 100 |
| 95 | #define EXIT_WEIGHT 3 |
| 96 | #define PRIO_BONUS_RATIO 25 |
| 97 | #define MAX_BONUS (MAX_USER_PRIO * PRIO_BONUS_RATIO / 100) |
| 98 | #define INTERACTIVE_DELTA 2 |
| 99 | #define MAX_SLEEP_AVG (DEF_TIMESLICE * MAX_BONUS) |
| 100 | #define STARVATION_LIMIT (MAX_SLEEP_AVG) |
| 101 | #define NS_MAX_SLEEP_AVG (JIFFIES_TO_NS(MAX_SLEEP_AVG)) |
| 102 | |
| 103 | /* |
| 104 | * If a task is 'interactive' then we reinsert it in the active |
| 105 | * array after it has expired its current timeslice. (it will not |
| 106 | * continue to run immediately, it will still roundrobin with |
| 107 | * other interactive tasks.) |
| 108 | * |
| 109 | * This part scales the interactivity limit depending on niceness. |
| 110 | * |
| 111 | * We scale it linearly, offset by the INTERACTIVE_DELTA delta. |
| 112 | * Here are a few examples of different nice levels: |
| 113 | * |
| 114 | * TASK_INTERACTIVE(-20): [1,1,1,1,1,1,1,1,1,0,0] |
| 115 | * TASK_INTERACTIVE(-10): [1,1,1,1,1,1,1,0,0,0,0] |
| 116 | * TASK_INTERACTIVE( 0): [1,1,1,1,0,0,0,0,0,0,0] |
| 117 | * TASK_INTERACTIVE( 10): [1,1,0,0,0,0,0,0,0,0,0] |
| 118 | * TASK_INTERACTIVE( 19): [0,0,0,0,0,0,0,0,0,0,0] |
| 119 | * |
| 120 | * (the X axis represents the possible -5 ... 0 ... +5 dynamic |
| 121 | * priority range a task can explore, a value of '1' means the |
| 122 | * task is rated interactive.) |
| 123 | * |
| 124 | * Ie. nice +19 tasks can never get 'interactive' enough to be |
| 125 | * reinserted into the active array. And only heavily CPU-hog nice -20 |
| 126 | * tasks will be expired. Default nice 0 tasks are somewhere between, |
| 127 | * it takes some effort for them to get interactive, but it's not |
| 128 | * too hard. |
| 129 | */ |
| 130 | |
| 131 | #define CURRENT_BONUS(p) \ |
| 132 | (NS_TO_JIFFIES((p)->sleep_avg) * MAX_BONUS / \ |
| 133 | MAX_SLEEP_AVG) |
| 134 | |
| 135 | #define GRANULARITY (10 * HZ / 1000 ? : 1) |
| 136 | |
| 137 | #ifdef CONFIG_SMP |
| 138 | #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ |
| 139 | (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)) * \ |
| 140 | num_online_cpus()) |
| 141 | #else |
| 142 | #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ |
| 143 | (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1))) |
| 144 | #endif |
| 145 | |
| 146 | #define SCALE(v1,v1_max,v2_max) \ |
| 147 | (v1) * (v2_max) / (v1_max) |
| 148 | |
| 149 | #define DELTA(p) \ |
Martin Andersson | 013d386 | 2006-03-27 01:15:18 -0800 | [diff] [blame] | 150 | (SCALE(TASK_NICE(p) + 20, 40, MAX_BONUS) - 20 * MAX_BONUS / 40 + \ |
| 151 | INTERACTIVE_DELTA) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | #define TASK_INTERACTIVE(p) \ |
| 154 | ((p)->prio <= (p)->static_prio - DELTA(p)) |
| 155 | |
| 156 | #define INTERACTIVE_SLEEP(p) \ |
| 157 | (JIFFIES_TO_NS(MAX_SLEEP_AVG * \ |
| 158 | (MAX_BONUS / 2 + DELTA((p)) + 1) / MAX_BONUS - 1)) |
| 159 | |
| 160 | #define TASK_PREEMPTS_CURR(p, rq) \ |
| 161 | ((p)->prio < (rq)->curr->prio) |
| 162 | |
| 163 | /* |
| 164 | * task_timeslice() scales user-nice values [ -20 ... 0 ... 19 ] |
| 165 | * to time slice values: [800ms ... 100ms ... 5ms] |
| 166 | * |
| 167 | * The higher a thread's priority, the bigger timeslices |
| 168 | * it gets during one round of execution. But even the lowest |
| 169 | * priority thread gets MIN_TIMESLICE worth of execution time. |
| 170 | */ |
| 171 | |
| 172 | #define SCALE_PRIO(x, prio) \ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 173 | max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 175 | static unsigned int static_prio_timeslice(int static_prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 177 | if (static_prio < NICE_TO_PRIO(0)) |
| 178 | return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | else |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 180 | return SCALE_PRIO(DEF_TIMESLICE, static_prio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 182 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 183 | static inline unsigned int task_timeslice(struct task_struct *p) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 184 | { |
| 185 | return static_prio_timeslice(p->static_prio); |
| 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | /* |
| 189 | * These are the runqueue data structures: |
| 190 | */ |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | struct prio_array { |
| 193 | unsigned int nr_active; |
Steven Rostedt | d444886 | 2006-06-27 02:54:29 -0700 | [diff] [blame] | 194 | DECLARE_BITMAP(bitmap, MAX_PRIO+1); /* include 1 bit for delimiter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | struct list_head queue[MAX_PRIO]; |
| 196 | }; |
| 197 | |
| 198 | /* |
| 199 | * This is the main, per-CPU runqueue data structure. |
| 200 | * |
| 201 | * Locking rule: those places that want to lock multiple runqueues |
| 202 | * (such as the load balancing or the thread migration code), lock |
| 203 | * acquire operations must be ordered by ascending &runqueue. |
| 204 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 205 | struct rq { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | spinlock_t lock; |
| 207 | |
| 208 | /* |
| 209 | * nr_running and cpu_load should be in the same cacheline because |
| 210 | * remote CPUs use both these fields when doing load calculation. |
| 211 | */ |
| 212 | unsigned long nr_running; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 213 | unsigned long raw_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | #ifdef CONFIG_SMP |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 215 | unsigned long cpu_load[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | #endif |
| 217 | unsigned long long nr_switches; |
| 218 | |
| 219 | /* |
| 220 | * This is part of a global counter where only the total sum |
| 221 | * over all CPUs matters. A task can increase this counter on |
| 222 | * one CPU and if it got migrated afterwards it may decrease |
| 223 | * it on another CPU. Always updated under the runqueue lock: |
| 224 | */ |
| 225 | unsigned long nr_uninterruptible; |
| 226 | |
| 227 | unsigned long expired_timestamp; |
| 228 | unsigned long long timestamp_last_tick; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 229 | struct task_struct *curr, *idle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | struct mm_struct *prev_mm; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 231 | struct prio_array *active, *expired, arrays[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | int best_expired_prio; |
| 233 | atomic_t nr_iowait; |
| 234 | |
| 235 | #ifdef CONFIG_SMP |
| 236 | struct sched_domain *sd; |
| 237 | |
| 238 | /* For active balancing */ |
| 239 | int active_balance; |
| 240 | int push_cpu; |
| 241 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 242 | struct task_struct *migration_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | struct list_head migration_queue; |
| 244 | #endif |
| 245 | |
| 246 | #ifdef CONFIG_SCHEDSTATS |
| 247 | /* latency stats */ |
| 248 | struct sched_info rq_sched_info; |
| 249 | |
| 250 | /* sys_sched_yield() stats */ |
| 251 | unsigned long yld_exp_empty; |
| 252 | unsigned long yld_act_empty; |
| 253 | unsigned long yld_both_empty; |
| 254 | unsigned long yld_cnt; |
| 255 | |
| 256 | /* schedule() stats */ |
| 257 | unsigned long sched_switch; |
| 258 | unsigned long sched_cnt; |
| 259 | unsigned long sched_goidle; |
| 260 | |
| 261 | /* try_to_wake_up() stats */ |
| 262 | unsigned long ttwu_cnt; |
| 263 | unsigned long ttwu_local; |
| 264 | #endif |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 265 | struct lock_class_key rq_lock_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | }; |
| 267 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 268 | static DEFINE_PER_CPU(struct rq, runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 270 | /* |
| 271 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 272 | * See detach_destroy_domains: synchronize_sched for details. |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 273 | * |
| 274 | * The domain tree of any CPU may only be accessed from within |
| 275 | * preempt-disabled sections. |
| 276 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 277 | #define for_each_domain(cpu, __sd) \ |
| 278 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
| 281 | #define this_rq() (&__get_cpu_var(runqueues)) |
| 282 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 283 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | #ifndef prepare_arch_switch |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 286 | # define prepare_arch_switch(next) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | #endif |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 288 | #ifndef finish_arch_switch |
| 289 | # define finish_arch_switch(prev) do { } while (0) |
| 290 | #endif |
| 291 | |
| 292 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 293 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 294 | { |
| 295 | return rq->curr == p; |
| 296 | } |
| 297 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 298 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 299 | { |
| 300 | } |
| 301 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 302 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 303 | { |
Ingo Molnar | da04c03 | 2005-09-13 11:17:59 +0200 | [diff] [blame] | 304 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 305 | /* this is a valid case when another task releases the spinlock */ |
| 306 | rq->lock.owner = current; |
| 307 | #endif |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 308 | /* |
| 309 | * If we are tracking spinlock dependencies then we have to |
| 310 | * fix up the runqueue lock - which gets 'carried over' from |
| 311 | * prev into current: |
| 312 | */ |
| 313 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); |
| 314 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 315 | spin_unlock_irq(&rq->lock); |
| 316 | } |
| 317 | |
| 318 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 319 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 320 | { |
| 321 | #ifdef CONFIG_SMP |
| 322 | return p->oncpu; |
| 323 | #else |
| 324 | return rq->curr == p; |
| 325 | #endif |
| 326 | } |
| 327 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 328 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 329 | { |
| 330 | #ifdef CONFIG_SMP |
| 331 | /* |
| 332 | * We can optimise this out completely for !SMP, because the |
| 333 | * SMP rebalancing from interrupt is the only thing that cares |
| 334 | * here. |
| 335 | */ |
| 336 | next->oncpu = 1; |
| 337 | #endif |
| 338 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 339 | spin_unlock_irq(&rq->lock); |
| 340 | #else |
| 341 | spin_unlock(&rq->lock); |
| 342 | #endif |
| 343 | } |
| 344 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 345 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 346 | { |
| 347 | #ifdef CONFIG_SMP |
| 348 | /* |
| 349 | * After ->oncpu is cleared, the task can be moved to a different CPU. |
| 350 | * We must ensure this doesn't happen until the switch is completely |
| 351 | * finished. |
| 352 | */ |
| 353 | smp_wmb(); |
| 354 | prev->oncpu = 0; |
| 355 | #endif |
| 356 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 357 | local_irq_enable(); |
| 358 | #endif |
| 359 | } |
| 360 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 363 | * __task_rq_lock - lock the runqueue a given task resides on. |
| 364 | * Must be called interrupts disabled. |
| 365 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 366 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 367 | __acquires(rq->lock) |
| 368 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 369 | struct rq *rq; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 370 | |
| 371 | repeat_lock_task: |
| 372 | rq = task_rq(p); |
| 373 | spin_lock(&rq->lock); |
| 374 | if (unlikely(rq != task_rq(p))) { |
| 375 | spin_unlock(&rq->lock); |
| 376 | goto repeat_lock_task; |
| 377 | } |
| 378 | return rq; |
| 379 | } |
| 380 | |
| 381 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | * task_rq_lock - lock the runqueue a given task resides on and disable |
| 383 | * interrupts. Note the ordering: we can safely lookup the task_rq without |
| 384 | * explicitly disabling preemption. |
| 385 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 386 | static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | __acquires(rq->lock) |
| 388 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 389 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | repeat_lock_task: |
| 392 | local_irq_save(*flags); |
| 393 | rq = task_rq(p); |
| 394 | spin_lock(&rq->lock); |
| 395 | if (unlikely(rq != task_rq(p))) { |
| 396 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 397 | goto repeat_lock_task; |
| 398 | } |
| 399 | return rq; |
| 400 | } |
| 401 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 402 | static inline void __task_rq_unlock(struct rq *rq) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 403 | __releases(rq->lock) |
| 404 | { |
| 405 | spin_unlock(&rq->lock); |
| 406 | } |
| 407 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 408 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | __releases(rq->lock) |
| 410 | { |
| 411 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 412 | } |
| 413 | |
| 414 | #ifdef CONFIG_SCHEDSTATS |
| 415 | /* |
| 416 | * bump this up when changing the output format or the meaning of an existing |
| 417 | * format, so that tools can adapt (or abort) |
| 418 | */ |
Nick Piggin | 68767a0 | 2005-06-25 14:57:20 -0700 | [diff] [blame] | 419 | #define SCHEDSTAT_VERSION 12 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | static int show_schedstat(struct seq_file *seq, void *v) |
| 422 | { |
| 423 | int cpu; |
| 424 | |
| 425 | seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION); |
| 426 | seq_printf(seq, "timestamp %lu\n", jiffies); |
| 427 | for_each_online_cpu(cpu) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 428 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | #ifdef CONFIG_SMP |
| 430 | struct sched_domain *sd; |
| 431 | int dcnt = 0; |
| 432 | #endif |
| 433 | |
| 434 | /* runqueue-specific stats */ |
| 435 | seq_printf(seq, |
| 436 | "cpu%d %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", |
| 437 | cpu, rq->yld_both_empty, |
| 438 | rq->yld_act_empty, rq->yld_exp_empty, rq->yld_cnt, |
| 439 | rq->sched_switch, rq->sched_cnt, rq->sched_goidle, |
| 440 | rq->ttwu_cnt, rq->ttwu_local, |
| 441 | rq->rq_sched_info.cpu_time, |
| 442 | rq->rq_sched_info.run_delay, rq->rq_sched_info.pcnt); |
| 443 | |
| 444 | seq_printf(seq, "\n"); |
| 445 | |
| 446 | #ifdef CONFIG_SMP |
| 447 | /* domain-specific stats */ |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 448 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | for_each_domain(cpu, sd) { |
| 450 | enum idle_type itype; |
| 451 | char mask_str[NR_CPUS]; |
| 452 | |
| 453 | cpumask_scnprintf(mask_str, NR_CPUS, sd->span); |
| 454 | seq_printf(seq, "domain%d %s", dcnt++, mask_str); |
| 455 | for (itype = SCHED_IDLE; itype < MAX_IDLE_TYPES; |
| 456 | itype++) { |
| 457 | seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu %lu", |
| 458 | sd->lb_cnt[itype], |
| 459 | sd->lb_balanced[itype], |
| 460 | sd->lb_failed[itype], |
| 461 | sd->lb_imbalance[itype], |
| 462 | sd->lb_gained[itype], |
| 463 | sd->lb_hot_gained[itype], |
| 464 | sd->lb_nobusyq[itype], |
| 465 | sd->lb_nobusyg[itype]); |
| 466 | } |
Nick Piggin | 68767a0 | 2005-06-25 14:57:20 -0700 | [diff] [blame] | 467 | seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | sd->alb_cnt, sd->alb_failed, sd->alb_pushed, |
Nick Piggin | 68767a0 | 2005-06-25 14:57:20 -0700 | [diff] [blame] | 469 | sd->sbe_cnt, sd->sbe_balanced, sd->sbe_pushed, |
| 470 | sd->sbf_cnt, sd->sbf_balanced, sd->sbf_pushed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | sd->ttwu_wake_remote, sd->ttwu_move_affine, sd->ttwu_move_balance); |
| 472 | } |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 473 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | #endif |
| 475 | } |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int schedstat_open(struct inode *inode, struct file *file) |
| 480 | { |
| 481 | unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32); |
| 482 | char *buf = kmalloc(size, GFP_KERNEL); |
| 483 | struct seq_file *m; |
| 484 | int res; |
| 485 | |
| 486 | if (!buf) |
| 487 | return -ENOMEM; |
| 488 | res = single_open(file, show_schedstat, NULL); |
| 489 | if (!res) { |
| 490 | m = file->private_data; |
| 491 | m->buf = buf; |
| 492 | m->size = size; |
| 493 | } else |
| 494 | kfree(buf); |
| 495 | return res; |
| 496 | } |
| 497 | |
| 498 | struct file_operations proc_schedstat_operations = { |
| 499 | .open = schedstat_open, |
| 500 | .read = seq_read, |
| 501 | .llseek = seq_lseek, |
| 502 | .release = single_release, |
| 503 | }; |
| 504 | |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 505 | /* |
| 506 | * Expects runqueue lock to be held for atomicity of update |
| 507 | */ |
| 508 | static inline void |
| 509 | rq_sched_info_arrive(struct rq *rq, unsigned long delta_jiffies) |
| 510 | { |
| 511 | if (rq) { |
| 512 | rq->rq_sched_info.run_delay += delta_jiffies; |
| 513 | rq->rq_sched_info.pcnt++; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Expects runqueue lock to be held for atomicity of update |
| 519 | */ |
| 520 | static inline void |
| 521 | rq_sched_info_depart(struct rq *rq, unsigned long delta_jiffies) |
| 522 | { |
| 523 | if (rq) |
| 524 | rq->rq_sched_info.cpu_time += delta_jiffies; |
| 525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | # define schedstat_inc(rq, field) do { (rq)->field++; } while (0) |
| 527 | # define schedstat_add(rq, field, amt) do { (rq)->field += (amt); } while (0) |
| 528 | #else /* !CONFIG_SCHEDSTATS */ |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 529 | static inline void |
| 530 | rq_sched_info_arrive(struct rq *rq, unsigned long delta_jiffies) |
| 531 | {} |
| 532 | static inline void |
| 533 | rq_sched_info_depart(struct rq *rq, unsigned long delta_jiffies) |
| 534 | {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | # define schedstat_inc(rq, field) do { } while (0) |
| 536 | # define schedstat_add(rq, field, amt) do { } while (0) |
| 537 | #endif |
| 538 | |
| 539 | /* |
| 540 | * rq_lock - lock a given runqueue and disable interrupts. |
| 541 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 542 | static inline struct rq *this_rq_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | __acquires(rq->lock) |
| 544 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 545 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | local_irq_disable(); |
| 548 | rq = this_rq(); |
| 549 | spin_lock(&rq->lock); |
| 550 | |
| 551 | return rq; |
| 552 | } |
| 553 | |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 554 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | /* |
| 556 | * Called when a process is dequeued from the active array and given |
| 557 | * the cpu. We should note that with the exception of interactive |
| 558 | * tasks, the expired queue will become the active queue after the active |
| 559 | * queue is empty, without explicitly dequeuing and requeuing tasks in the |
| 560 | * expired queue. (Interactive tasks may be requeued directly to the |
| 561 | * active queue, thus delaying tasks in the expired queue from running; |
| 562 | * see scheduler_tick()). |
| 563 | * |
| 564 | * This function is only called from sched_info_arrive(), rather than |
| 565 | * dequeue_task(). Even though a task may be queued and dequeued multiple |
| 566 | * times as it is shuffled about, we're really interested in knowing how |
| 567 | * long it was from the *first* time it was queued to the time that it |
| 568 | * finally hit a cpu. |
| 569 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 570 | static inline void sched_info_dequeued(struct task_struct *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { |
| 572 | t->sched_info.last_queued = 0; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Called when a task finally hits the cpu. We can now calculate how |
| 577 | * long it was waiting to run. We also note when it began so that we |
| 578 | * can keep stats on how long its timeslice is. |
| 579 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 580 | static void sched_info_arrive(struct task_struct *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 582 | unsigned long now = jiffies, delta_jiffies = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
| 584 | if (t->sched_info.last_queued) |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 585 | delta_jiffies = now - t->sched_info.last_queued; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | sched_info_dequeued(t); |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 587 | t->sched_info.run_delay += delta_jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | t->sched_info.last_arrival = now; |
| 589 | t->sched_info.pcnt++; |
| 590 | |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 591 | rq_sched_info_arrive(task_rq(t), delta_jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Called when a process is queued into either the active or expired |
| 596 | * array. The time is noted and later used to determine how long we |
| 597 | * had to wait for us to reach the cpu. Since the expired queue will |
| 598 | * become the active queue after active queue is empty, without dequeuing |
| 599 | * and requeuing any tasks, we are interested in queuing to either. It |
| 600 | * is unusual but not impossible for tasks to be dequeued and immediately |
| 601 | * requeued in the same or another array: this can happen in sched_yield(), |
| 602 | * set_user_nice(), and even load_balance() as it moves tasks from runqueue |
| 603 | * to runqueue. |
| 604 | * |
| 605 | * This function is only called from enqueue_task(), but also only updates |
| 606 | * the timestamp if it is already not set. It's assumed that |
| 607 | * sched_info_dequeued() will clear that stamp when appropriate. |
| 608 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 609 | static inline void sched_info_queued(struct task_struct *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 611 | if (unlikely(sched_info_on())) |
| 612 | if (!t->sched_info.last_queued) |
| 613 | t->sched_info.last_queued = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Called when a process ceases being the active-running process, either |
| 618 | * voluntarily or involuntarily. Now we can calculate how long we ran. |
| 619 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 620 | static inline void sched_info_depart(struct task_struct *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 622 | unsigned long delta_jiffies = jiffies - t->sched_info.last_arrival; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 624 | t->sched_info.cpu_time += delta_jiffies; |
| 625 | rq_sched_info_depart(task_rq(t), delta_jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Called when tasks are switched involuntarily due, typically, to expiring |
| 630 | * their time slice. (This may also be called when switching to or from |
| 631 | * the idle task.) We are only called when prev != next. |
| 632 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 633 | static inline void |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 634 | __sched_info_switch(struct task_struct *prev, struct task_struct *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 636 | struct rq *rq = task_rq(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * prev now departs the cpu. It's not interesting to record |
| 640 | * stats about how efficient we were at scheduling the idle |
| 641 | * process, however. |
| 642 | */ |
| 643 | if (prev != rq->idle) |
| 644 | sched_info_depart(prev); |
| 645 | |
| 646 | if (next != rq->idle) |
| 647 | sched_info_arrive(next); |
| 648 | } |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 649 | static inline void |
| 650 | sched_info_switch(struct task_struct *prev, struct task_struct *next) |
| 651 | { |
| 652 | if (unlikely(sched_info_on())) |
| 653 | __sched_info_switch(prev, next); |
| 654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | #else |
| 656 | #define sched_info_queued(t) do { } while (0) |
| 657 | #define sched_info_switch(t, next) do { } while (0) |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 658 | #endif /* CONFIG_SCHEDSTATS || CONFIG_TASK_DELAY_ACCT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | /* |
| 661 | * Adding/removing a task to/from a priority array: |
| 662 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 663 | static void dequeue_task(struct task_struct *p, struct prio_array *array) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
| 665 | array->nr_active--; |
| 666 | list_del(&p->run_list); |
| 667 | if (list_empty(array->queue + p->prio)) |
| 668 | __clear_bit(p->prio, array->bitmap); |
| 669 | } |
| 670 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 671 | static void enqueue_task(struct task_struct *p, struct prio_array *array) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
| 673 | sched_info_queued(p); |
| 674 | list_add_tail(&p->run_list, array->queue + p->prio); |
| 675 | __set_bit(p->prio, array->bitmap); |
| 676 | array->nr_active++; |
| 677 | p->array = array; |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Put task to the end of the run list without the overhead of dequeue |
| 682 | * followed by enqueue. |
| 683 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 684 | static void requeue_task(struct task_struct *p, struct prio_array *array) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
| 686 | list_move_tail(&p->run_list, array->queue + p->prio); |
| 687 | } |
| 688 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 689 | static inline void |
| 690 | enqueue_task_head(struct task_struct *p, struct prio_array *array) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
| 692 | list_add(&p->run_list, array->queue + p->prio); |
| 693 | __set_bit(p->prio, array->bitmap); |
| 694 | array->nr_active++; |
| 695 | p->array = array; |
| 696 | } |
| 697 | |
| 698 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 699 | * __normal_prio - return the priority that is based on the static |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | * priority but is modified by bonuses/penalties. |
| 701 | * |
| 702 | * We scale the actual sleep average [0 .... MAX_SLEEP_AVG] |
| 703 | * into the -5 ... 0 ... +5 bonus/penalty range. |
| 704 | * |
| 705 | * We use 25% of the full 0...39 priority range so that: |
| 706 | * |
| 707 | * 1) nice +19 interactive tasks do not preempt nice 0 CPU hogs. |
| 708 | * 2) nice -20 CPU hogs do not get preempted by nice 0 tasks. |
| 709 | * |
| 710 | * Both properties are important to certain workloads. |
| 711 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 712 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 713 | static inline int __normal_prio(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
| 715 | int bonus, prio; |
| 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | bonus = CURRENT_BONUS(p) - MAX_BONUS / 2; |
| 718 | |
| 719 | prio = p->static_prio - bonus; |
| 720 | if (prio < MAX_RT_PRIO) |
| 721 | prio = MAX_RT_PRIO; |
| 722 | if (prio > MAX_PRIO-1) |
| 723 | prio = MAX_PRIO-1; |
| 724 | return prio; |
| 725 | } |
| 726 | |
| 727 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 728 | * To aid in avoiding the subversion of "niceness" due to uneven distribution |
| 729 | * of tasks with abnormal "nice" values across CPUs the contribution that |
| 730 | * each task makes to its run queue's load is weighted according to its |
| 731 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a |
| 732 | * scaled version of the new time slice allocation that they receive on time |
| 733 | * slice expiry etc. |
| 734 | */ |
| 735 | |
| 736 | /* |
| 737 | * Assume: static_prio_timeslice(NICE_TO_PRIO(0)) == DEF_TIMESLICE |
| 738 | * If static_prio_timeslice() is ever changed to break this assumption then |
| 739 | * this code will need modification |
| 740 | */ |
| 741 | #define TIME_SLICE_NICE_ZERO DEF_TIMESLICE |
| 742 | #define LOAD_WEIGHT(lp) \ |
| 743 | (((lp) * SCHED_LOAD_SCALE) / TIME_SLICE_NICE_ZERO) |
| 744 | #define PRIO_TO_LOAD_WEIGHT(prio) \ |
| 745 | LOAD_WEIGHT(static_prio_timeslice(prio)) |
| 746 | #define RTPRIO_TO_LOAD_WEIGHT(rp) \ |
| 747 | (PRIO_TO_LOAD_WEIGHT(MAX_RT_PRIO) + LOAD_WEIGHT(rp)) |
| 748 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 749 | static void set_load_weight(struct task_struct *p) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 750 | { |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 751 | if (has_rt_policy(p)) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 752 | #ifdef CONFIG_SMP |
| 753 | if (p == task_rq(p)->migration_thread) |
| 754 | /* |
| 755 | * The migration thread does the actual balancing. |
| 756 | * Giving its load any weight will skew balancing |
| 757 | * adversely. |
| 758 | */ |
| 759 | p->load_weight = 0; |
| 760 | else |
| 761 | #endif |
| 762 | p->load_weight = RTPRIO_TO_LOAD_WEIGHT(p->rt_priority); |
| 763 | } else |
| 764 | p->load_weight = PRIO_TO_LOAD_WEIGHT(p->static_prio); |
| 765 | } |
| 766 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 767 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 768 | inc_raw_weighted_load(struct rq *rq, const struct task_struct *p) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 769 | { |
| 770 | rq->raw_weighted_load += p->load_weight; |
| 771 | } |
| 772 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 773 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 774 | dec_raw_weighted_load(struct rq *rq, const struct task_struct *p) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 775 | { |
| 776 | rq->raw_weighted_load -= p->load_weight; |
| 777 | } |
| 778 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 779 | static inline void inc_nr_running(struct task_struct *p, struct rq *rq) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 780 | { |
| 781 | rq->nr_running++; |
| 782 | inc_raw_weighted_load(rq, p); |
| 783 | } |
| 784 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 785 | static inline void dec_nr_running(struct task_struct *p, struct rq *rq) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 786 | { |
| 787 | rq->nr_running--; |
| 788 | dec_raw_weighted_load(rq, p); |
| 789 | } |
| 790 | |
| 791 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 792 | * Calculate the expected normal priority: i.e. priority |
| 793 | * without taking RT-inheritance into account. Might be |
| 794 | * boosted by interactivity modifiers. Changes upon fork, |
| 795 | * setprio syscalls, and whenever the interactivity |
| 796 | * estimator recalculates. |
| 797 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 798 | static inline int normal_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 799 | { |
| 800 | int prio; |
| 801 | |
| 802 | if (has_rt_policy(p)) |
| 803 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
| 804 | else |
| 805 | prio = __normal_prio(p); |
| 806 | return prio; |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | * Calculate the current priority, i.e. the priority |
| 811 | * taken into account by the scheduler. This value might |
| 812 | * be boosted by RT tasks, or might be boosted by |
| 813 | * interactivity modifiers. Will be RT if the task got |
| 814 | * RT-boosted. If not then it returns p->normal_prio. |
| 815 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 816 | static int effective_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 817 | { |
| 818 | p->normal_prio = normal_prio(p); |
| 819 | /* |
| 820 | * If we are RT tasks or we were boosted to RT priority, |
| 821 | * keep the priority unchanged. Otherwise, update priority |
| 822 | * to the normal priority: |
| 823 | */ |
| 824 | if (!rt_prio(p->prio)) |
| 825 | return p->normal_prio; |
| 826 | return p->prio; |
| 827 | } |
| 828 | |
| 829 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | * __activate_task - move a task to the runqueue. |
| 831 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 832 | static void __activate_task(struct task_struct *p, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 834 | struct prio_array *target = rq->active; |
Con Kolivas | d425b27 | 2006-03-31 02:31:29 -0800 | [diff] [blame] | 835 | |
Linus Torvalds | f1adad7 | 2006-05-21 18:54:09 -0700 | [diff] [blame] | 836 | if (batch_task(p)) |
Con Kolivas | d425b27 | 2006-03-31 02:31:29 -0800 | [diff] [blame] | 837 | target = rq->expired; |
| 838 | enqueue_task(p, target); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 839 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* |
| 843 | * __activate_idle_task - move idle task to the _front_ of runqueue. |
| 844 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 845 | static inline void __activate_idle_task(struct task_struct *p, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | { |
| 847 | enqueue_task_head(p, rq->active); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 848 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 851 | /* |
| 852 | * Recalculate p->normal_prio and p->prio after having slept, |
| 853 | * updating the sleep-average too: |
| 854 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 855 | static int recalc_task_prio(struct task_struct *p, unsigned long long now) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | { |
| 857 | /* Caller must always ensure 'now >= p->timestamp' */ |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 858 | unsigned long sleep_time = now - p->timestamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
Con Kolivas | d425b27 | 2006-03-31 02:31:29 -0800 | [diff] [blame] | 860 | if (batch_task(p)) |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 861 | sleep_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
| 863 | if (likely(sleep_time > 0)) { |
| 864 | /* |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 865 | * This ceiling is set to the lowest priority that would allow |
| 866 | * a task to be reinserted into the active array on timeslice |
| 867 | * completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | */ |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 869 | unsigned long ceiling = INTERACTIVE_SLEEP(p); |
Con Kolivas | e72ff0b | 2006-03-31 02:31:26 -0800 | [diff] [blame] | 870 | |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 871 | if (p->mm && sleep_time > ceiling && p->sleep_avg < ceiling) { |
| 872 | /* |
| 873 | * Prevents user tasks from achieving best priority |
| 874 | * with one single large enough sleep. |
| 875 | */ |
| 876 | p->sleep_avg = ceiling; |
| 877 | /* |
| 878 | * Using INTERACTIVE_SLEEP() as a ceiling places a |
| 879 | * nice(0) task 1ms sleep away from promotion, and |
| 880 | * gives it 700ms to round-robin with no chance of |
| 881 | * being demoted. This is more than generous, so |
| 882 | * mark this sleep as non-interactive to prevent the |
| 883 | * on-runqueue bonus logic from intervening should |
| 884 | * this task not receive cpu immediately. |
| 885 | */ |
| 886 | p->sleep_type = SLEEP_NONINTERACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } else { |
| 888 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | * Tasks waking from uninterruptible sleep are |
| 890 | * limited in their sleep_avg rise as they |
| 891 | * are likely to be waiting on I/O |
| 892 | */ |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 893 | if (p->sleep_type == SLEEP_NONINTERACTIVE && p->mm) { |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 894 | if (p->sleep_avg >= ceiling) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | sleep_time = 0; |
| 896 | else if (p->sleep_avg + sleep_time >= |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 897 | ceiling) { |
| 898 | p->sleep_avg = ceiling; |
| 899 | sleep_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | /* |
| 904 | * This code gives a bonus to interactive tasks. |
| 905 | * |
| 906 | * The boost works by updating the 'average sleep time' |
| 907 | * value here, based on ->timestamp. The more time a |
| 908 | * task spends sleeping, the higher the average gets - |
| 909 | * and the higher the priority boost gets as well. |
| 910 | */ |
| 911 | p->sleep_avg += sleep_time; |
| 912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 914 | if (p->sleep_avg > NS_MAX_SLEEP_AVG) |
| 915 | p->sleep_avg = NS_MAX_SLEEP_AVG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Chen Shang | a3464a1 | 2005-06-25 14:57:31 -0700 | [diff] [blame] | 918 | return effective_prio(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /* |
| 922 | * activate_task - move a task to the runqueue and do priority recalculation |
| 923 | * |
| 924 | * Update all the scheduling statistics stuff. (sleep average |
| 925 | * calculation, priority modifiers, etc.) |
| 926 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 927 | static void activate_task(struct task_struct *p, struct rq *rq, int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | { |
| 929 | unsigned long long now; |
| 930 | |
| 931 | now = sched_clock(); |
| 932 | #ifdef CONFIG_SMP |
| 933 | if (!local) { |
| 934 | /* Compensate for drifting sched_clock */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 935 | struct rq *this_rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | now = (now - this_rq->timestamp_last_tick) |
| 937 | + rq->timestamp_last_tick; |
| 938 | } |
| 939 | #endif |
| 940 | |
Chen, Kenneth W | a47ab93 | 2005-11-09 15:45:29 -0800 | [diff] [blame] | 941 | if (!rt_task(p)) |
| 942 | p->prio = recalc_task_prio(p, now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
| 944 | /* |
| 945 | * This checks to make sure it's not an uninterruptible task |
| 946 | * that is now waking up. |
| 947 | */ |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 948 | if (p->sleep_type == SLEEP_NORMAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | /* |
| 950 | * Tasks which were woken up by interrupts (ie. hw events) |
| 951 | * are most likely of interactive nature. So we give them |
| 952 | * the credit of extending their sleep time to the period |
| 953 | * of time they spend on the runqueue, waiting for execution |
| 954 | * on a CPU, first time around: |
| 955 | */ |
| 956 | if (in_interrupt()) |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 957 | p->sleep_type = SLEEP_INTERRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | else { |
| 959 | /* |
| 960 | * Normal first-time wakeups get a credit too for |
| 961 | * on-runqueue time, but it will be weighted down: |
| 962 | */ |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 963 | p->sleep_type = SLEEP_INTERACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | p->timestamp = now; |
| 967 | |
| 968 | __activate_task(p, rq); |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * deactivate_task - remove a task from the runqueue. |
| 973 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 974 | static void deactivate_task(struct task_struct *p, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 976 | dec_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | dequeue_task(p, p->array); |
| 978 | p->array = NULL; |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * resched_task - mark a task 'to be rescheduled now'. |
| 983 | * |
| 984 | * On UP this means the setting of the need_resched flag, on SMP it |
| 985 | * might also involve a cross-CPU call to trigger the scheduler on |
| 986 | * the target CPU. |
| 987 | */ |
| 988 | #ifdef CONFIG_SMP |
Andi Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 989 | |
| 990 | #ifndef tsk_is_polling |
| 991 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) |
| 992 | #endif |
| 993 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 994 | static void resched_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | { |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 996 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
| 998 | assert_spin_locked(&task_rq(p)->lock); |
| 999 | |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 1000 | if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED))) |
| 1001 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 1003 | set_tsk_thread_flag(p, TIF_NEED_RESCHED); |
| 1004 | |
| 1005 | cpu = task_cpu(p); |
| 1006 | if (cpu == smp_processor_id()) |
| 1007 | return; |
| 1008 | |
Andi Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 1009 | /* NEED_RESCHED must be visible before we test polling */ |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 1010 | smp_mb(); |
Andi Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 1011 | if (!tsk_is_polling(p)) |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 1012 | smp_send_reschedule(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
| 1014 | #else |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1015 | static inline void resched_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | { |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 1017 | assert_spin_locked(&task_rq(p)->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | set_tsk_need_resched(p); |
| 1019 | } |
| 1020 | #endif |
| 1021 | |
| 1022 | /** |
| 1023 | * task_curr - is this task currently executing on a CPU? |
| 1024 | * @p: the task in question. |
| 1025 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1026 | inline int task_curr(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | { |
| 1028 | return cpu_curr(task_cpu(p)) == p; |
| 1029 | } |
| 1030 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1031 | /* Used instead of source_load when we know the type == 0 */ |
| 1032 | unsigned long weighted_cpuload(const int cpu) |
| 1033 | { |
| 1034 | return cpu_rq(cpu)->raw_weighted_load; |
| 1035 | } |
| 1036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | #ifdef CONFIG_SMP |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1038 | struct migration_req { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1041 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | int dest_cpu; |
| 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | struct completion done; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1045 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
| 1047 | /* |
| 1048 | * The task's runqueue lock must be held. |
| 1049 | * Returns true if you have to wait for migration thread. |
| 1050 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1051 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1052 | migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1054 | struct rq *rq = task_rq(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * If the task is not on a runqueue (and not running), then |
| 1058 | * it is sufficient to simply update the task's cpu field. |
| 1059 | */ |
| 1060 | if (!p->array && !task_running(rq, p)) { |
| 1061 | set_task_cpu(p, dest_cpu); |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | init_completion(&req->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | req->task = p; |
| 1067 | req->dest_cpu = dest_cpu; |
| 1068 | list_add(&req->list, &rq->migration_queue); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 1069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | return 1; |
| 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * wait_task_inactive - wait for a thread to unschedule. |
| 1075 | * |
| 1076 | * The caller must ensure that the task *will* unschedule sometime soon, |
| 1077 | * else this function might spin for a *long* time. This function can't |
| 1078 | * be called with interrupts off, or it may introduce deadlock with |
| 1079 | * smp_call_function() if an IPI is sent by the same process we are |
| 1080 | * waiting to become inactive. |
| 1081 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1082 | void wait_task_inactive(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | { |
| 1084 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1085 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | int preempted; |
| 1087 | |
| 1088 | repeat: |
| 1089 | rq = task_rq_lock(p, &flags); |
| 1090 | /* Must be off runqueue entirely, not preempted. */ |
| 1091 | if (unlikely(p->array || task_running(rq, p))) { |
| 1092 | /* If it's preempted, we yield. It could be a while. */ |
| 1093 | preempted = !task_running(rq, p); |
| 1094 | task_rq_unlock(rq, &flags); |
| 1095 | cpu_relax(); |
| 1096 | if (preempted) |
| 1097 | yield(); |
| 1098 | goto repeat; |
| 1099 | } |
| 1100 | task_rq_unlock(rq, &flags); |
| 1101 | } |
| 1102 | |
| 1103 | /*** |
| 1104 | * kick_process - kick a running thread to enter/exit the kernel |
| 1105 | * @p: the to-be-kicked thread |
| 1106 | * |
| 1107 | * Cause a process which is running on another CPU to enter |
| 1108 | * kernel-mode, without any delay. (to get signals handled.) |
| 1109 | * |
| 1110 | * NOTE: this function doesnt have to take the runqueue lock, |
| 1111 | * because all it wants to ensure is that the remote task enters |
| 1112 | * the kernel. If the IPI races and the task has been migrated |
| 1113 | * to another CPU then no harm is done and the purpose has been |
| 1114 | * achieved as well. |
| 1115 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1116 | void kick_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | { |
| 1118 | int cpu; |
| 1119 | |
| 1120 | preempt_disable(); |
| 1121 | cpu = task_cpu(p); |
| 1122 | if ((cpu != smp_processor_id()) && task_curr(p)) |
| 1123 | smp_send_reschedule(cpu); |
| 1124 | preempt_enable(); |
| 1125 | } |
| 1126 | |
| 1127 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1128 | * Return a low guess at the load of a migration-source cpu weighted |
| 1129 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | * |
| 1131 | * We want to under-estimate the load of migration sources, to |
| 1132 | * balance conservatively. |
| 1133 | */ |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1134 | static inline unsigned long source_load(int cpu, int type) |
| 1135 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1136 | struct rq *rq = cpu_rq(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1137 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1138 | if (type == 0) |
| 1139 | return rq->raw_weighted_load; |
| 1140 | |
| 1141 | return min(rq->cpu_load[type-1], rq->raw_weighted_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1145 | * Return a high guess at the load of a migration-target cpu weighted |
| 1146 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | */ |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1148 | static inline unsigned long target_load(int cpu, int type) |
| 1149 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1150 | struct rq *rq = cpu_rq(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1151 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1152 | if (type == 0) |
| 1153 | return rq->raw_weighted_load; |
| 1154 | |
| 1155 | return max(rq->cpu_load[type-1], rq->raw_weighted_load); |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * Return the average load per task on the cpu's run queue |
| 1160 | */ |
| 1161 | static inline unsigned long cpu_avg_load_per_task(int cpu) |
| 1162 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1163 | struct rq *rq = cpu_rq(cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1164 | unsigned long n = rq->nr_running; |
| 1165 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 1166 | return n ? rq->raw_weighted_load / n : SCHED_LOAD_SCALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1169 | /* |
| 1170 | * find_idlest_group finds and returns the least busy CPU group within the |
| 1171 | * domain. |
| 1172 | */ |
| 1173 | static struct sched_group * |
| 1174 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) |
| 1175 | { |
| 1176 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; |
| 1177 | unsigned long min_load = ULONG_MAX, this_load = 0; |
| 1178 | int load_idx = sd->forkexec_idx; |
| 1179 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
| 1180 | |
| 1181 | do { |
| 1182 | unsigned long load, avg_load; |
| 1183 | int local_group; |
| 1184 | int i; |
| 1185 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1186 | /* Skip over this group if it has no CPUs allowed */ |
| 1187 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) |
| 1188 | goto nextgroup; |
| 1189 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1190 | local_group = cpu_isset(this_cpu, group->cpumask); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1191 | |
| 1192 | /* Tally up the load of all CPUs in the group */ |
| 1193 | avg_load = 0; |
| 1194 | |
| 1195 | for_each_cpu_mask(i, group->cpumask) { |
| 1196 | /* Bias balancing toward cpus of our domain */ |
| 1197 | if (local_group) |
| 1198 | load = source_load(i, load_idx); |
| 1199 | else |
| 1200 | load = target_load(i, load_idx); |
| 1201 | |
| 1202 | avg_load += load; |
| 1203 | } |
| 1204 | |
| 1205 | /* Adjust by relative CPU power of the group */ |
| 1206 | avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power; |
| 1207 | |
| 1208 | if (local_group) { |
| 1209 | this_load = avg_load; |
| 1210 | this = group; |
| 1211 | } else if (avg_load < min_load) { |
| 1212 | min_load = avg_load; |
| 1213 | idlest = group; |
| 1214 | } |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1215 | nextgroup: |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1216 | group = group->next; |
| 1217 | } while (group != sd->groups); |
| 1218 | |
| 1219 | if (!idlest || 100*this_load < imbalance*min_load) |
| 1220 | return NULL; |
| 1221 | return idlest; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * find_idlest_queue - find the idlest runqueue among the cpus in group. |
| 1226 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 1227 | static int |
| 1228 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1229 | { |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1230 | cpumask_t tmp; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1231 | unsigned long load, min_load = ULONG_MAX; |
| 1232 | int idlest = -1; |
| 1233 | int i; |
| 1234 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1235 | /* Traverse only the allowed CPUs */ |
| 1236 | cpus_and(tmp, group->cpumask, p->cpus_allowed); |
| 1237 | |
| 1238 | for_each_cpu_mask(i, tmp) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1239 | load = weighted_cpuload(i); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1240 | |
| 1241 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 1242 | min_load = load; |
| 1243 | idlest = i; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | return idlest; |
| 1248 | } |
| 1249 | |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1250 | /* |
| 1251 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 1252 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 1253 | * SD_BALANCE_EXEC. |
| 1254 | * |
| 1255 | * Balance, ie. select the least loaded group. |
| 1256 | * |
| 1257 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 1258 | * |
| 1259 | * preempt must be disabled. |
| 1260 | */ |
| 1261 | static int sched_balance_self(int cpu, int flag) |
| 1262 | { |
| 1263 | struct task_struct *t = current; |
| 1264 | struct sched_domain *tmp, *sd = NULL; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1265 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1266 | for_each_domain(cpu, tmp) { |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 1267 | /* |
| 1268 | * If power savings logic is enabled for a domain, stop there. |
| 1269 | */ |
| 1270 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
| 1271 | break; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1272 | if (tmp->flags & flag) |
| 1273 | sd = tmp; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1274 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1275 | |
| 1276 | while (sd) { |
| 1277 | cpumask_t span; |
| 1278 | struct sched_group *group; |
| 1279 | int new_cpu; |
| 1280 | int weight; |
| 1281 | |
| 1282 | span = sd->span; |
| 1283 | group = find_idlest_group(sd, t, cpu); |
| 1284 | if (!group) |
| 1285 | goto nextlevel; |
| 1286 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1287 | new_cpu = find_idlest_cpu(group, t, cpu); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1288 | if (new_cpu == -1 || new_cpu == cpu) |
| 1289 | goto nextlevel; |
| 1290 | |
| 1291 | /* Now try balancing at a lower domain level */ |
| 1292 | cpu = new_cpu; |
| 1293 | nextlevel: |
| 1294 | sd = NULL; |
| 1295 | weight = cpus_weight(span); |
| 1296 | for_each_domain(cpu, tmp) { |
| 1297 | if (weight <= cpus_weight(tmp->span)) |
| 1298 | break; |
| 1299 | if (tmp->flags & flag) |
| 1300 | sd = tmp; |
| 1301 | } |
| 1302 | /* while loop will break here if sd == NULL */ |
| 1303 | } |
| 1304 | |
| 1305 | return cpu; |
| 1306 | } |
| 1307 | |
| 1308 | #endif /* CONFIG_SMP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
| 1310 | /* |
| 1311 | * wake_idle() will wake a task on an idle cpu if task->cpu is |
| 1312 | * not idle and an idle cpu is available. The span of cpus to |
| 1313 | * search starts with cpus closest then further out as needed, |
| 1314 | * so we always favor a closer, idle cpu. |
| 1315 | * |
| 1316 | * Returns the CPU we should wake onto. |
| 1317 | */ |
| 1318 | #if defined(ARCH_HAS_SCHED_WAKE_IDLE) |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1319 | static int wake_idle(int cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | { |
| 1321 | cpumask_t tmp; |
| 1322 | struct sched_domain *sd; |
| 1323 | int i; |
| 1324 | |
| 1325 | if (idle_cpu(cpu)) |
| 1326 | return cpu; |
| 1327 | |
| 1328 | for_each_domain(cpu, sd) { |
| 1329 | if (sd->flags & SD_WAKE_IDLE) { |
Nick Piggin | e0f364f | 2005-06-25 14:57:06 -0700 | [diff] [blame] | 1330 | cpus_and(tmp, sd->span, p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | for_each_cpu_mask(i, tmp) { |
| 1332 | if (idle_cpu(i)) |
| 1333 | return i; |
| 1334 | } |
| 1335 | } |
Nick Piggin | e0f364f | 2005-06-25 14:57:06 -0700 | [diff] [blame] | 1336 | else |
| 1337 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | } |
| 1339 | return cpu; |
| 1340 | } |
| 1341 | #else |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1342 | static inline int wake_idle(int cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | { |
| 1344 | return cpu; |
| 1345 | } |
| 1346 | #endif |
| 1347 | |
| 1348 | /*** |
| 1349 | * try_to_wake_up - wake up a thread |
| 1350 | * @p: the to-be-woken-up thread |
| 1351 | * @state: the mask of task states that can be woken |
| 1352 | * @sync: do a synchronous wakeup? |
| 1353 | * |
| 1354 | * Put it on the run-queue if it's not already there. The "current" |
| 1355 | * thread is always on the run-queue (except when the actual |
| 1356 | * re-schedule is in progress), and as such you're allowed to do |
| 1357 | * the simpler "current->state = TASK_RUNNING" to mark yourself |
| 1358 | * runnable without the overhead of this. |
| 1359 | * |
| 1360 | * returns failure only if the task is already active. |
| 1361 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1362 | static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | { |
| 1364 | int cpu, this_cpu, success = 0; |
| 1365 | unsigned long flags; |
| 1366 | long old_state; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1367 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | #ifdef CONFIG_SMP |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1369 | struct sched_domain *sd, *this_sd = NULL; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1370 | unsigned long load, this_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | int new_cpu; |
| 1372 | #endif |
| 1373 | |
| 1374 | rq = task_rq_lock(p, &flags); |
| 1375 | old_state = p->state; |
| 1376 | if (!(old_state & state)) |
| 1377 | goto out; |
| 1378 | |
| 1379 | if (p->array) |
| 1380 | goto out_running; |
| 1381 | |
| 1382 | cpu = task_cpu(p); |
| 1383 | this_cpu = smp_processor_id(); |
| 1384 | |
| 1385 | #ifdef CONFIG_SMP |
| 1386 | if (unlikely(task_running(rq, p))) |
| 1387 | goto out_activate; |
| 1388 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1389 | new_cpu = cpu; |
| 1390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | schedstat_inc(rq, ttwu_cnt); |
| 1392 | if (cpu == this_cpu) { |
| 1393 | schedstat_inc(rq, ttwu_local); |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1394 | goto out_set_cpu; |
| 1395 | } |
| 1396 | |
| 1397 | for_each_domain(this_cpu, sd) { |
| 1398 | if (cpu_isset(cpu, sd->span)) { |
| 1399 | schedstat_inc(sd, ttwu_wake_remote); |
| 1400 | this_sd = sd; |
| 1401 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | } |
| 1403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1405 | if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | goto out_set_cpu; |
| 1407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | /* |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1409 | * Check for affine wakeup and passive balancing possibilities. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | */ |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1411 | if (this_sd) { |
| 1412 | int idx = this_sd->wake_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | unsigned int imbalance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1415 | imbalance = 100 + (this_sd->imbalance_pct - 100) / 2; |
| 1416 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1417 | load = source_load(cpu, idx); |
| 1418 | this_load = target_load(this_cpu, idx); |
| 1419 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1420 | new_cpu = this_cpu; /* Wake to this CPU if we can */ |
| 1421 | |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1422 | if (this_sd->flags & SD_WAKE_AFFINE) { |
| 1423 | unsigned long tl = this_load; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1424 | unsigned long tl_per_task = cpu_avg_load_per_task(this_cpu); |
| 1425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | /* |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1427 | * If sync wakeup then subtract the (maximum possible) |
| 1428 | * effect of the currently running task from the load |
| 1429 | * of the current CPU: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | */ |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1431 | if (sync) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1432 | tl -= current->load_weight; |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1433 | |
| 1434 | if ((tl <= load && |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1435 | tl + target_load(cpu, idx) <= tl_per_task) || |
| 1436 | 100*(tl + p->load_weight) <= imbalance*load) { |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1437 | /* |
| 1438 | * This domain has SD_WAKE_AFFINE and |
| 1439 | * p is cache cold in this domain, and |
| 1440 | * there is no bad imbalance. |
| 1441 | */ |
| 1442 | schedstat_inc(this_sd, ttwu_move_affine); |
| 1443 | goto out_set_cpu; |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * Start passive balancing when half the imbalance_pct |
| 1449 | * limit is reached. |
| 1450 | */ |
| 1451 | if (this_sd->flags & SD_WAKE_BALANCE) { |
| 1452 | if (imbalance*this_load <= 100*load) { |
| 1453 | schedstat_inc(this_sd, ttwu_move_balance); |
| 1454 | goto out_set_cpu; |
| 1455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */ |
| 1460 | out_set_cpu: |
| 1461 | new_cpu = wake_idle(new_cpu, p); |
| 1462 | if (new_cpu != cpu) { |
| 1463 | set_task_cpu(p, new_cpu); |
| 1464 | task_rq_unlock(rq, &flags); |
| 1465 | /* might preempt at this point */ |
| 1466 | rq = task_rq_lock(p, &flags); |
| 1467 | old_state = p->state; |
| 1468 | if (!(old_state & state)) |
| 1469 | goto out; |
| 1470 | if (p->array) |
| 1471 | goto out_running; |
| 1472 | |
| 1473 | this_cpu = smp_processor_id(); |
| 1474 | cpu = task_cpu(p); |
| 1475 | } |
| 1476 | |
| 1477 | out_activate: |
| 1478 | #endif /* CONFIG_SMP */ |
| 1479 | if (old_state == TASK_UNINTERRUPTIBLE) { |
| 1480 | rq->nr_uninterruptible--; |
| 1481 | /* |
| 1482 | * Tasks on involuntary sleep don't earn |
| 1483 | * sleep_avg beyond just interactive state. |
| 1484 | */ |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 1485 | p->sleep_type = SLEEP_NONINTERACTIVE; |
Con Kolivas | e7c38cb | 2006-03-31 02:31:25 -0800 | [diff] [blame] | 1486 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
| 1488 | /* |
Ingo Molnar | d79fc0f | 2005-09-10 00:26:12 -0700 | [diff] [blame] | 1489 | * Tasks that have marked their sleep as noninteractive get |
Con Kolivas | e7c38cb | 2006-03-31 02:31:25 -0800 | [diff] [blame] | 1490 | * woken up with their sleep average not weighted in an |
| 1491 | * interactive way. |
Ingo Molnar | d79fc0f | 2005-09-10 00:26:12 -0700 | [diff] [blame] | 1492 | */ |
Con Kolivas | e7c38cb | 2006-03-31 02:31:25 -0800 | [diff] [blame] | 1493 | if (old_state & TASK_NONINTERACTIVE) |
| 1494 | p->sleep_type = SLEEP_NONINTERACTIVE; |
| 1495 | |
| 1496 | |
| 1497 | activate_task(p, rq, cpu == this_cpu); |
Ingo Molnar | d79fc0f | 2005-09-10 00:26:12 -0700 | [diff] [blame] | 1498 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | * Sync wakeups (i.e. those types of wakeups where the waker |
| 1500 | * has indicated that it will leave the CPU in short order) |
| 1501 | * don't trigger a preemption, if the woken up task will run on |
| 1502 | * this cpu. (in this case the 'I will reschedule' promise of |
| 1503 | * the waker guarantees that the freshly woken up task is going |
| 1504 | * to be considered on this CPU.) |
| 1505 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | if (!sync || cpu != this_cpu) { |
| 1507 | if (TASK_PREEMPTS_CURR(p, rq)) |
| 1508 | resched_task(rq->curr); |
| 1509 | } |
| 1510 | success = 1; |
| 1511 | |
| 1512 | out_running: |
| 1513 | p->state = TASK_RUNNING; |
| 1514 | out: |
| 1515 | task_rq_unlock(rq, &flags); |
| 1516 | |
| 1517 | return success; |
| 1518 | } |
| 1519 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1520 | int fastcall wake_up_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | { |
| 1522 | return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED | |
| 1523 | TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0); |
| 1524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | EXPORT_SYMBOL(wake_up_process); |
| 1526 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1527 | int fastcall wake_up_state(struct task_struct *p, unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | { |
| 1529 | return try_to_wake_up(p, state, 0); |
| 1530 | } |
| 1531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | /* |
| 1533 | * Perform scheduler related setup for a newly forked process p. |
| 1534 | * p is forked by current. |
| 1535 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1536 | void fastcall sched_fork(struct task_struct *p, int clone_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | { |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1538 | int cpu = get_cpu(); |
| 1539 | |
| 1540 | #ifdef CONFIG_SMP |
| 1541 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); |
| 1542 | #endif |
| 1543 | set_task_cpu(p, cpu); |
| 1544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | /* |
| 1546 | * We mark the process as running here, but have not actually |
| 1547 | * inserted it onto the runqueue yet. This guarantees that |
| 1548 | * nobody will actually run it, and a signal or other external |
| 1549 | * event cannot wake it up and insert it on the runqueue either. |
| 1550 | */ |
| 1551 | p->state = TASK_RUNNING; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1552 | |
| 1553 | /* |
| 1554 | * Make sure we do not leak PI boosting priority to the child: |
| 1555 | */ |
| 1556 | p->prio = current->normal_prio; |
| 1557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | INIT_LIST_HEAD(&p->run_list); |
| 1559 | p->array = NULL; |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 1560 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
| 1561 | if (unlikely(sched_info_on())) |
| 1562 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | #endif |
Chen, Kenneth W | d6077cb | 2006-02-14 13:53:10 -0800 | [diff] [blame] | 1564 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1565 | p->oncpu = 0; |
| 1566 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | #ifdef CONFIG_PREEMPT |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1568 | /* Want to start with kernel preemption disabled. */ |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 1569 | task_thread_info(p)->preempt_count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | #endif |
| 1571 | /* |
| 1572 | * Share the timeslice between parent and child, thus the |
| 1573 | * total amount of pending timeslices in the system doesn't change, |
| 1574 | * resulting in more scheduling fairness. |
| 1575 | */ |
| 1576 | local_irq_disable(); |
| 1577 | p->time_slice = (current->time_slice + 1) >> 1; |
| 1578 | /* |
| 1579 | * The remainder of the first timeslice might be recovered by |
| 1580 | * the parent if the child exits early enough. |
| 1581 | */ |
| 1582 | p->first_time_slice = 1; |
| 1583 | current->time_slice >>= 1; |
| 1584 | p->timestamp = sched_clock(); |
| 1585 | if (unlikely(!current->time_slice)) { |
| 1586 | /* |
| 1587 | * This case is rare, it happens when the parent has only |
| 1588 | * a single jiffy left from its timeslice. Taking the |
| 1589 | * runqueue lock is not a problem. |
| 1590 | */ |
| 1591 | current->time_slice = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | scheduler_tick(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1593 | } |
| 1594 | local_irq_enable(); |
| 1595 | put_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | * wake_up_new_task - wake up a newly created task for the first time. |
| 1600 | * |
| 1601 | * This function will do some initial scheduler statistics housekeeping |
| 1602 | * that must be done for every newly created context, then puts the task |
| 1603 | * on the runqueue and wakes it. |
| 1604 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1605 | void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1607 | struct rq *rq, *this_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | unsigned long flags; |
| 1609 | int this_cpu, cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | |
| 1611 | rq = task_rq_lock(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | BUG_ON(p->state != TASK_RUNNING); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1613 | this_cpu = smp_processor_id(); |
| 1614 | cpu = task_cpu(p); |
| 1615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | /* |
| 1617 | * We decrease the sleep average of forking parents |
| 1618 | * and children as well, to keep max-interactive tasks |
| 1619 | * from forking tasks that are max-interactive. The parent |
| 1620 | * (current) is done further down, under its lock. |
| 1621 | */ |
| 1622 | p->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(p) * |
| 1623 | CHILD_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); |
| 1624 | |
| 1625 | p->prio = effective_prio(p); |
| 1626 | |
| 1627 | if (likely(cpu == this_cpu)) { |
| 1628 | if (!(clone_flags & CLONE_VM)) { |
| 1629 | /* |
| 1630 | * The VM isn't cloned, so we're in a good position to |
| 1631 | * do child-runs-first in anticipation of an exec. This |
| 1632 | * usually avoids a lot of COW overhead. |
| 1633 | */ |
| 1634 | if (unlikely(!current->array)) |
| 1635 | __activate_task(p, rq); |
| 1636 | else { |
| 1637 | p->prio = current->prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1638 | p->normal_prio = current->normal_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | list_add_tail(&p->run_list, ¤t->run_list); |
| 1640 | p->array = current->array; |
| 1641 | p->array->nr_active++; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1642 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | } |
| 1644 | set_need_resched(); |
| 1645 | } else |
| 1646 | /* Run child last */ |
| 1647 | __activate_task(p, rq); |
| 1648 | /* |
| 1649 | * We skip the following code due to cpu == this_cpu |
| 1650 | * |
| 1651 | * task_rq_unlock(rq, &flags); |
| 1652 | * this_rq = task_rq_lock(current, &flags); |
| 1653 | */ |
| 1654 | this_rq = rq; |
| 1655 | } else { |
| 1656 | this_rq = cpu_rq(this_cpu); |
| 1657 | |
| 1658 | /* |
| 1659 | * Not the local CPU - must adjust timestamp. This should |
| 1660 | * get optimised away in the !CONFIG_SMP case. |
| 1661 | */ |
| 1662 | p->timestamp = (p->timestamp - this_rq->timestamp_last_tick) |
| 1663 | + rq->timestamp_last_tick; |
| 1664 | __activate_task(p, rq); |
| 1665 | if (TASK_PREEMPTS_CURR(p, rq)) |
| 1666 | resched_task(rq->curr); |
| 1667 | |
| 1668 | /* |
| 1669 | * Parent and child are on different CPUs, now get the |
| 1670 | * parent runqueue to update the parent's ->sleep_avg: |
| 1671 | */ |
| 1672 | task_rq_unlock(rq, &flags); |
| 1673 | this_rq = task_rq_lock(current, &flags); |
| 1674 | } |
| 1675 | current->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(current) * |
| 1676 | PARENT_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); |
| 1677 | task_rq_unlock(this_rq, &flags); |
| 1678 | } |
| 1679 | |
| 1680 | /* |
| 1681 | * Potentially available exiting-child timeslices are |
| 1682 | * retrieved here - this way the parent does not get |
| 1683 | * penalized for creating too many threads. |
| 1684 | * |
| 1685 | * (this cannot be used to 'generate' timeslices |
| 1686 | * artificially, because any timeslice recovered here |
| 1687 | * was given away by the parent in the first place.) |
| 1688 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1689 | void fastcall sched_exit(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | { |
| 1691 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1692 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | |
| 1694 | /* |
| 1695 | * If the child was a (relative-) CPU hog then decrease |
| 1696 | * the sleep_avg of the parent as well. |
| 1697 | */ |
| 1698 | rq = task_rq_lock(p->parent, &flags); |
Oleg Nesterov | 889dfaf | 2005-11-04 18:54:30 +0300 | [diff] [blame] | 1699 | if (p->first_time_slice && task_cpu(p) == task_cpu(p->parent)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | p->parent->time_slice += p->time_slice; |
| 1701 | if (unlikely(p->parent->time_slice > task_timeslice(p))) |
| 1702 | p->parent->time_slice = task_timeslice(p); |
| 1703 | } |
| 1704 | if (p->sleep_avg < p->parent->sleep_avg) |
| 1705 | p->parent->sleep_avg = p->parent->sleep_avg / |
| 1706 | (EXIT_WEIGHT + 1) * EXIT_WEIGHT + p->sleep_avg / |
| 1707 | (EXIT_WEIGHT + 1); |
| 1708 | task_rq_unlock(rq, &flags); |
| 1709 | } |
| 1710 | |
| 1711 | /** |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1712 | * prepare_task_switch - prepare to switch tasks |
| 1713 | * @rq: the runqueue preparing to switch |
| 1714 | * @next: the task we are going to switch to. |
| 1715 | * |
| 1716 | * This is called with the rq lock held and interrupts off. It must |
| 1717 | * be paired with a subsequent finish_task_switch after the context |
| 1718 | * switch. |
| 1719 | * |
| 1720 | * prepare_task_switch sets up locking and calls architecture specific |
| 1721 | * hooks. |
| 1722 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1723 | static inline void prepare_task_switch(struct rq *rq, struct task_struct *next) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1724 | { |
| 1725 | prepare_lock_switch(rq, next); |
| 1726 | prepare_arch_switch(next); |
| 1727 | } |
| 1728 | |
| 1729 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | * finish_task_switch - clean up after a task-switch |
Jeff Garzik | 344baba | 2005-09-07 01:15:17 -0400 | [diff] [blame] | 1731 | * @rq: runqueue associated with task-switch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | * @prev: the thread we just switched away from. |
| 1733 | * |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1734 | * finish_task_switch must be called after the context switch, paired |
| 1735 | * with a prepare_task_switch call before the context switch. |
| 1736 | * finish_task_switch will reconcile locking set up by prepare_task_switch, |
| 1737 | * and do any other architecture-specific cleanup actions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | * |
| 1739 | * Note that we may have delayed dropping an mm in context_switch(). If |
| 1740 | * so, we finish that here outside of the runqueue lock. (Doing it |
| 1741 | * with the lock held can cause deadlocks; see schedule() for |
| 1742 | * details.) |
| 1743 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1744 | static inline void finish_task_switch(struct rq *rq, struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | __releases(rq->lock) |
| 1746 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | struct mm_struct *mm = rq->prev_mm; |
| 1748 | unsigned long prev_task_flags; |
| 1749 | |
| 1750 | rq->prev_mm = NULL; |
| 1751 | |
| 1752 | /* |
| 1753 | * A task struct has one reference for the use as "current". |
| 1754 | * If a task dies, then it sets EXIT_ZOMBIE in tsk->exit_state and |
| 1755 | * calls schedule one last time. The schedule call will never return, |
| 1756 | * and the scheduled task must drop that reference. |
| 1757 | * The test for EXIT_ZOMBIE must occur while the runqueue locks are |
| 1758 | * still held, otherwise prev could be scheduled on another cpu, die |
| 1759 | * there before we look at prev->state, and then the reference would |
| 1760 | * be dropped twice. |
| 1761 | * Manfred Spraul <manfred@colorfullife.com> |
| 1762 | */ |
| 1763 | prev_task_flags = prev->flags; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1764 | finish_arch_switch(prev); |
| 1765 | finish_lock_switch(rq, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | if (mm) |
| 1767 | mmdrop(mm); |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 1768 | if (unlikely(prev_task_flags & PF_DEAD)) { |
| 1769 | /* |
| 1770 | * Remove function-return probe instances associated with this |
| 1771 | * task and put them back on the free list. |
| 1772 | */ |
| 1773 | kprobe_flush_task(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | put_task_struct(prev); |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 1775 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | /** |
| 1779 | * schedule_tail - first thing a freshly forked thread must call. |
| 1780 | * @prev: the thread we just switched away from. |
| 1781 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1782 | asmlinkage void schedule_tail(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | __releases(rq->lock) |
| 1784 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1785 | struct rq *rq = this_rq(); |
| 1786 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1787 | finish_task_switch(rq, prev); |
| 1788 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW |
| 1789 | /* In this case, finish_task_switch does not reenable preemption */ |
| 1790 | preempt_enable(); |
| 1791 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | if (current->set_child_tid) |
| 1793 | put_user(current->pid, current->set_child_tid); |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * context_switch - switch to the new MM and the new |
| 1798 | * thread's register state. |
| 1799 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1800 | static inline struct task_struct * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1801 | context_switch(struct rq *rq, struct task_struct *prev, |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1802 | struct task_struct *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | { |
| 1804 | struct mm_struct *mm = next->mm; |
| 1805 | struct mm_struct *oldmm = prev->active_mm; |
| 1806 | |
| 1807 | if (unlikely(!mm)) { |
| 1808 | next->active_mm = oldmm; |
| 1809 | atomic_inc(&oldmm->mm_count); |
| 1810 | enter_lazy_tlb(oldmm, next); |
| 1811 | } else |
| 1812 | switch_mm(oldmm, mm, next); |
| 1813 | |
| 1814 | if (unlikely(!prev->mm)) { |
| 1815 | prev->active_mm = NULL; |
| 1816 | WARN_ON(rq->prev_mm); |
| 1817 | rq->prev_mm = oldmm; |
| 1818 | } |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 1819 | /* |
| 1820 | * Since the runqueue lock will be released by the next |
| 1821 | * task (which is an invalid locking op but in the case |
| 1822 | * of the scheduler it's an obvious special-case), so we |
| 1823 | * do an early lockdep release here: |
| 1824 | */ |
| 1825 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 1826 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 1827 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | |
| 1829 | /* Here we just switch the register state and the stack. */ |
| 1830 | switch_to(prev, next, prev); |
| 1831 | |
| 1832 | return prev; |
| 1833 | } |
| 1834 | |
| 1835 | /* |
| 1836 | * nr_running, nr_uninterruptible and nr_context_switches: |
| 1837 | * |
| 1838 | * externally visible scheduler statistics: current number of runnable |
| 1839 | * threads, current number of uninterruptible-sleeping threads, total |
| 1840 | * number of context switches performed since bootup. |
| 1841 | */ |
| 1842 | unsigned long nr_running(void) |
| 1843 | { |
| 1844 | unsigned long i, sum = 0; |
| 1845 | |
| 1846 | for_each_online_cpu(i) |
| 1847 | sum += cpu_rq(i)->nr_running; |
| 1848 | |
| 1849 | return sum; |
| 1850 | } |
| 1851 | |
| 1852 | unsigned long nr_uninterruptible(void) |
| 1853 | { |
| 1854 | unsigned long i, sum = 0; |
| 1855 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 1856 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | sum += cpu_rq(i)->nr_uninterruptible; |
| 1858 | |
| 1859 | /* |
| 1860 | * Since we read the counters lockless, it might be slightly |
| 1861 | * inaccurate. Do not allow it to go below zero though: |
| 1862 | */ |
| 1863 | if (unlikely((long)sum < 0)) |
| 1864 | sum = 0; |
| 1865 | |
| 1866 | return sum; |
| 1867 | } |
| 1868 | |
| 1869 | unsigned long long nr_context_switches(void) |
| 1870 | { |
Steven Rostedt | cc94abf | 2006-06-27 02:54:31 -0700 | [diff] [blame] | 1871 | int i; |
| 1872 | unsigned long long sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 1874 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | sum += cpu_rq(i)->nr_switches; |
| 1876 | |
| 1877 | return sum; |
| 1878 | } |
| 1879 | |
| 1880 | unsigned long nr_iowait(void) |
| 1881 | { |
| 1882 | unsigned long i, sum = 0; |
| 1883 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 1884 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
| 1886 | |
| 1887 | return sum; |
| 1888 | } |
| 1889 | |
Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 1890 | unsigned long nr_active(void) |
| 1891 | { |
| 1892 | unsigned long i, running = 0, uninterruptible = 0; |
| 1893 | |
| 1894 | for_each_online_cpu(i) { |
| 1895 | running += cpu_rq(i)->nr_running; |
| 1896 | uninterruptible += cpu_rq(i)->nr_uninterruptible; |
| 1897 | } |
| 1898 | |
| 1899 | if (unlikely((long)uninterruptible < 0)) |
| 1900 | uninterruptible = 0; |
| 1901 | |
| 1902 | return running + uninterruptible; |
| 1903 | } |
| 1904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | #ifdef CONFIG_SMP |
| 1906 | |
| 1907 | /* |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 1908 | * Is this task likely cache-hot: |
| 1909 | */ |
| 1910 | static inline int |
| 1911 | task_hot(struct task_struct *p, unsigned long long now, struct sched_domain *sd) |
| 1912 | { |
| 1913 | return (long long)(now - p->last_ran) < (long long)sd->cache_hot_time; |
| 1914 | } |
| 1915 | |
| 1916 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | * double_rq_lock - safely lock two runqueues |
| 1918 | * |
| 1919 | * Note this does not disable interrupts like task_rq_lock, |
| 1920 | * you need to do so manually before calling. |
| 1921 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1922 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | __acquires(rq1->lock) |
| 1924 | __acquires(rq2->lock) |
| 1925 | { |
| 1926 | if (rq1 == rq2) { |
| 1927 | spin_lock(&rq1->lock); |
| 1928 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 1929 | } else { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1930 | if (rq1 < rq2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | spin_lock(&rq1->lock); |
| 1932 | spin_lock(&rq2->lock); |
| 1933 | } else { |
| 1934 | spin_lock(&rq2->lock); |
| 1935 | spin_lock(&rq1->lock); |
| 1936 | } |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * double_rq_unlock - safely unlock two runqueues |
| 1942 | * |
| 1943 | * Note this does not restore interrupts like task_rq_unlock, |
| 1944 | * you need to do so manually after calling. |
| 1945 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1946 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | __releases(rq1->lock) |
| 1948 | __releases(rq2->lock) |
| 1949 | { |
| 1950 | spin_unlock(&rq1->lock); |
| 1951 | if (rq1 != rq2) |
| 1952 | spin_unlock(&rq2->lock); |
| 1953 | else |
| 1954 | __release(rq2->lock); |
| 1955 | } |
| 1956 | |
| 1957 | /* |
| 1958 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 1959 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1960 | static void double_lock_balance(struct rq *this_rq, struct rq *busiest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | __releases(this_rq->lock) |
| 1962 | __acquires(busiest->lock) |
| 1963 | __acquires(this_rq->lock) |
| 1964 | { |
| 1965 | if (unlikely(!spin_trylock(&busiest->lock))) { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1966 | if (busiest < this_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | spin_unlock(&this_rq->lock); |
| 1968 | spin_lock(&busiest->lock); |
| 1969 | spin_lock(&this_rq->lock); |
| 1970 | } else |
| 1971 | spin_lock(&busiest->lock); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | * If dest_cpu is allowed for this process, migrate the task to it. |
| 1977 | * This is accomplished by forcing the cpu_allowed mask to only |
| 1978 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then |
| 1979 | * the cpu_allowed mask is restored. |
| 1980 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1981 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1983 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1985 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | |
| 1987 | rq = task_rq_lock(p, &flags); |
| 1988 | if (!cpu_isset(dest_cpu, p->cpus_allowed) |
| 1989 | || unlikely(cpu_is_offline(dest_cpu))) |
| 1990 | goto out; |
| 1991 | |
| 1992 | /* force the process onto the specified CPU */ |
| 1993 | if (migrate_task(p, dest_cpu, &req)) { |
| 1994 | /* Need to wait for migration thread (might exit: take ref). */ |
| 1995 | struct task_struct *mt = rq->migration_thread; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | get_task_struct(mt); |
| 1998 | task_rq_unlock(rq, &flags); |
| 1999 | wake_up_process(mt); |
| 2000 | put_task_struct(mt); |
| 2001 | wait_for_completion(&req.done); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | return; |
| 2004 | } |
| 2005 | out: |
| 2006 | task_rq_unlock(rq, &flags); |
| 2007 | } |
| 2008 | |
| 2009 | /* |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2010 | * sched_exec - execve() is a valuable balancing opportunity, because at |
| 2011 | * this point the task has the smallest effective memory and cache footprint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | */ |
| 2013 | void sched_exec(void) |
| 2014 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | int new_cpu, this_cpu = get_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2016 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | put_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 2018 | if (new_cpu != this_cpu) |
| 2019 | sched_migrate_task(current, new_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | /* |
| 2023 | * pull_task - move a task from a remote runqueue to the local runqueue. |
| 2024 | * Both runqueues must be locked. |
| 2025 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2026 | static void pull_task(struct rq *src_rq, struct prio_array *src_array, |
| 2027 | struct task_struct *p, struct rq *this_rq, |
| 2028 | struct prio_array *this_array, int this_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | { |
| 2030 | dequeue_task(p, src_array); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2031 | dec_nr_running(p, src_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | set_task_cpu(p, this_cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2033 | inc_nr_running(p, this_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | enqueue_task(p, this_array); |
| 2035 | p->timestamp = (p->timestamp - src_rq->timestamp_last_tick) |
| 2036 | + this_rq->timestamp_last_tick; |
| 2037 | /* |
| 2038 | * Note that idle threads have a prio of MAX_PRIO, for this test |
| 2039 | * to be always true for them. |
| 2040 | */ |
| 2041 | if (TASK_PREEMPTS_CURR(p, this_rq)) |
| 2042 | resched_task(this_rq->curr); |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 2047 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 2048 | static |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2049 | int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 2050 | struct sched_domain *sd, enum idle_type idle, |
| 2051 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | { |
| 2053 | /* |
| 2054 | * We do not migrate tasks that are: |
| 2055 | * 1) running (obviously), or |
| 2056 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 2057 | * 3) are cache-hot on their current CPU. |
| 2058 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | if (!cpu_isset(this_cpu, p->cpus_allowed)) |
| 2060 | return 0; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2061 | *all_pinned = 0; |
| 2062 | |
| 2063 | if (task_running(rq, p)) |
| 2064 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
| 2066 | /* |
| 2067 | * Aggressive migration if: |
Nick Piggin | cafb20c | 2005-06-25 14:57:17 -0700 | [diff] [blame] | 2068 | * 1) task is cache cold, or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | * 2) too many balance attempts have failed. |
| 2070 | */ |
| 2071 | |
Nick Piggin | cafb20c | 2005-06-25 14:57:17 -0700 | [diff] [blame] | 2072 | if (sd->nr_balance_failed > sd->cache_nice_tries) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | return 1; |
| 2074 | |
| 2075 | if (task_hot(p, rq->timestamp_last_tick, sd)) |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2076 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | return 1; |
| 2078 | } |
| 2079 | |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2080 | #define rq_best_prio(rq) min((rq)->curr->prio, (rq)->best_expired_prio) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2083 | * move_tasks tries to move up to max_nr_move tasks and max_load_move weighted |
| 2084 | * load from busiest to this_rq, as part of a balancing operation within |
| 2085 | * "domain". Returns the number of tasks moved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | * |
| 2087 | * Called with both runqueues locked. |
| 2088 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2089 | static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2090 | unsigned long max_nr_move, unsigned long max_load_move, |
| 2091 | struct sched_domain *sd, enum idle_type idle, |
| 2092 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2094 | int idx, pulled = 0, pinned = 0, this_best_prio, best_prio, |
| 2095 | best_prio_seen, skip_for_load; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2096 | struct prio_array *array, *dst_array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | struct list_head *head, *curr; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2098 | struct task_struct *tmp; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2099 | long rem_load_move; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2101 | if (max_nr_move == 0 || max_load_move == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | goto out; |
| 2103 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2104 | rem_load_move = max_load_move; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2105 | pinned = 1; |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2106 | this_best_prio = rq_best_prio(this_rq); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2107 | best_prio = rq_best_prio(busiest); |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2108 | /* |
| 2109 | * Enable handling of the case where there is more than one task |
| 2110 | * with the best priority. If the current running task is one |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2111 | * of those with prio==best_prio we know it won't be moved |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2112 | * and therefore it's safe to override the skip (based on load) of |
| 2113 | * any task we find with that prio. |
| 2114 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2115 | best_prio_seen = best_prio == busiest->curr->prio; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | /* |
| 2118 | * We first consider expired tasks. Those will likely not be |
| 2119 | * executed in the near future, and they are most likely to |
| 2120 | * be cache-cold, thus switching CPUs has the least effect |
| 2121 | * on them. |
| 2122 | */ |
| 2123 | if (busiest->expired->nr_active) { |
| 2124 | array = busiest->expired; |
| 2125 | dst_array = this_rq->expired; |
| 2126 | } else { |
| 2127 | array = busiest->active; |
| 2128 | dst_array = this_rq->active; |
| 2129 | } |
| 2130 | |
| 2131 | new_array: |
| 2132 | /* Start searching at priority 0: */ |
| 2133 | idx = 0; |
| 2134 | skip_bitmap: |
| 2135 | if (!idx) |
| 2136 | idx = sched_find_first_bit(array->bitmap); |
| 2137 | else |
| 2138 | idx = find_next_bit(array->bitmap, MAX_PRIO, idx); |
| 2139 | if (idx >= MAX_PRIO) { |
| 2140 | if (array == busiest->expired && busiest->active->nr_active) { |
| 2141 | array = busiest->active; |
| 2142 | dst_array = this_rq->active; |
| 2143 | goto new_array; |
| 2144 | } |
| 2145 | goto out; |
| 2146 | } |
| 2147 | |
| 2148 | head = array->queue + idx; |
| 2149 | curr = head->prev; |
| 2150 | skip_queue: |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2151 | tmp = list_entry(curr, struct task_struct, run_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | |
| 2153 | curr = curr->prev; |
| 2154 | |
Peter Williams | 50ddd96 | 2006-06-27 02:54:36 -0700 | [diff] [blame] | 2155 | /* |
| 2156 | * To help distribute high priority tasks accross CPUs we don't |
| 2157 | * skip a task if it will be the highest priority task (i.e. smallest |
| 2158 | * prio value) on its new queue regardless of its load weight |
| 2159 | */ |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2160 | skip_for_load = tmp->load_weight > rem_load_move; |
| 2161 | if (skip_for_load && idx < this_best_prio) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2162 | skip_for_load = !best_prio_seen && idx == best_prio; |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2163 | if (skip_for_load || |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2164 | !can_migrate_task(tmp, busiest, this_cpu, sd, idle, &pinned)) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2165 | |
| 2166 | best_prio_seen |= idx == best_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | if (curr != head) |
| 2168 | goto skip_queue; |
| 2169 | idx++; |
| 2170 | goto skip_bitmap; |
| 2171 | } |
| 2172 | |
| 2173 | #ifdef CONFIG_SCHEDSTATS |
| 2174 | if (task_hot(tmp, busiest->timestamp_last_tick, sd)) |
| 2175 | schedstat_inc(sd, lb_hot_gained[idle]); |
| 2176 | #endif |
| 2177 | |
| 2178 | pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu); |
| 2179 | pulled++; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2180 | rem_load_move -= tmp->load_weight; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2182 | /* |
| 2183 | * We only want to steal up to the prescribed number of tasks |
| 2184 | * and the prescribed amount of weighted load. |
| 2185 | */ |
| 2186 | if (pulled < max_nr_move && rem_load_move > 0) { |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2187 | if (idx < this_best_prio) |
| 2188 | this_best_prio = idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | if (curr != head) |
| 2190 | goto skip_queue; |
| 2191 | idx++; |
| 2192 | goto skip_bitmap; |
| 2193 | } |
| 2194 | out: |
| 2195 | /* |
| 2196 | * Right now, this is the only place pull_task() is called, |
| 2197 | * so we can safely collect pull_task() stats here rather than |
| 2198 | * inside pull_task(). |
| 2199 | */ |
| 2200 | schedstat_add(sd, lb_gained[idle], pulled); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2201 | |
| 2202 | if (all_pinned) |
| 2203 | *all_pinned = pinned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | return pulled; |
| 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | * find_busiest_group finds and returns the busiest CPU group within the |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2209 | * domain. It calculates and returns the amount of weighted load which |
| 2210 | * should be moved to restore balance via the imbalance parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | */ |
| 2212 | static struct sched_group * |
| 2213 | find_busiest_group(struct sched_domain *sd, int this_cpu, |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2214 | unsigned long *imbalance, enum idle_type idle, int *sd_idle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | { |
| 2216 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; |
| 2217 | unsigned long max_load, avg_load, total_load, this_load, total_pwr; |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2218 | unsigned long max_pull; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2219 | unsigned long busiest_load_per_task, busiest_nr_running; |
| 2220 | unsigned long this_load_per_task, this_nr_running; |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2221 | int load_idx; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2222 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2223 | int power_savings_balance = 1; |
| 2224 | unsigned long leader_nr_running = 0, min_load_per_task = 0; |
| 2225 | unsigned long min_nr_running = ULONG_MAX; |
| 2226 | struct sched_group *group_min = NULL, *group_leader = NULL; |
| 2227 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
| 2229 | max_load = this_load = total_load = total_pwr = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2230 | busiest_load_per_task = busiest_nr_running = 0; |
| 2231 | this_load_per_task = this_nr_running = 0; |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2232 | if (idle == NOT_IDLE) |
| 2233 | load_idx = sd->busy_idx; |
| 2234 | else if (idle == NEWLY_IDLE) |
| 2235 | load_idx = sd->newidle_idx; |
| 2236 | else |
| 2237 | load_idx = sd->idle_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | |
| 2239 | do { |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2240 | unsigned long load, group_capacity; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | int local_group; |
| 2242 | int i; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2243 | unsigned long sum_nr_running, sum_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
| 2245 | local_group = cpu_isset(this_cpu, group->cpumask); |
| 2246 | |
| 2247 | /* Tally up the load of all CPUs in the group */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2248 | sum_weighted_load = sum_nr_running = avg_load = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | |
| 2250 | for_each_cpu_mask(i, group->cpumask) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2251 | struct rq *rq = cpu_rq(i); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2252 | |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2253 | if (*sd_idle && !idle_cpu(i)) |
| 2254 | *sd_idle = 0; |
| 2255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | /* Bias balancing toward cpus of our domain */ |
| 2257 | if (local_group) |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2258 | load = target_load(i, load_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | else |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2260 | load = source_load(i, load_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | |
| 2262 | avg_load += load; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2263 | sum_nr_running += rq->nr_running; |
| 2264 | sum_weighted_load += rq->raw_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | total_load += avg_load; |
| 2268 | total_pwr += group->cpu_power; |
| 2269 | |
| 2270 | /* Adjust by relative CPU power of the group */ |
| 2271 | avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power; |
| 2272 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2273 | group_capacity = group->cpu_power / SCHED_LOAD_SCALE; |
| 2274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | if (local_group) { |
| 2276 | this_load = avg_load; |
| 2277 | this = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2278 | this_nr_running = sum_nr_running; |
| 2279 | this_load_per_task = sum_weighted_load; |
| 2280 | } else if (avg_load > max_load && |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2281 | sum_nr_running > group_capacity) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | max_load = avg_load; |
| 2283 | busiest = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2284 | busiest_nr_running = sum_nr_running; |
| 2285 | busiest_load_per_task = sum_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2287 | |
| 2288 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2289 | /* |
| 2290 | * Busy processors will not participate in power savings |
| 2291 | * balance. |
| 2292 | */ |
| 2293 | if (idle == NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
| 2294 | goto group_next; |
| 2295 | |
| 2296 | /* |
| 2297 | * If the local group is idle or completely loaded |
| 2298 | * no need to do power savings balance at this domain |
| 2299 | */ |
| 2300 | if (local_group && (this_nr_running >= group_capacity || |
| 2301 | !this_nr_running)) |
| 2302 | power_savings_balance = 0; |
| 2303 | |
| 2304 | /* |
| 2305 | * If a group is already running at full capacity or idle, |
| 2306 | * don't include that group in power savings calculations |
| 2307 | */ |
| 2308 | if (!power_savings_balance || sum_nr_running >= group_capacity |
| 2309 | || !sum_nr_running) |
| 2310 | goto group_next; |
| 2311 | |
| 2312 | /* |
| 2313 | * Calculate the group which has the least non-idle load. |
| 2314 | * This is the group from where we need to pick up the load |
| 2315 | * for saving power |
| 2316 | */ |
| 2317 | if ((sum_nr_running < min_nr_running) || |
| 2318 | (sum_nr_running == min_nr_running && |
| 2319 | first_cpu(group->cpumask) < |
| 2320 | first_cpu(group_min->cpumask))) { |
| 2321 | group_min = group; |
| 2322 | min_nr_running = sum_nr_running; |
| 2323 | min_load_per_task = sum_weighted_load / |
| 2324 | sum_nr_running; |
| 2325 | } |
| 2326 | |
| 2327 | /* |
| 2328 | * Calculate the group which is almost near its |
| 2329 | * capacity but still has some space to pick up some load |
| 2330 | * from other group and save more power |
| 2331 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2332 | if (sum_nr_running <= group_capacity - 1) { |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2333 | if (sum_nr_running > leader_nr_running || |
| 2334 | (sum_nr_running == leader_nr_running && |
| 2335 | first_cpu(group->cpumask) > |
| 2336 | first_cpu(group_leader->cpumask))) { |
| 2337 | group_leader = group; |
| 2338 | leader_nr_running = sum_nr_running; |
| 2339 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2340 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2341 | group_next: |
| 2342 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | group = group->next; |
| 2344 | } while (group != sd->groups); |
| 2345 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2346 | if (!busiest || this_load >= max_load || busiest_nr_running == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | goto out_balanced; |
| 2348 | |
| 2349 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; |
| 2350 | |
| 2351 | if (this_load >= avg_load || |
| 2352 | 100*max_load <= sd->imbalance_pct*this_load) |
| 2353 | goto out_balanced; |
| 2354 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2355 | busiest_load_per_task /= busiest_nr_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | /* |
| 2357 | * We're trying to get all the cpus to the average_load, so we don't |
| 2358 | * want to push ourselves above the average load, nor do we wish to |
| 2359 | * reduce the max loaded cpu below the average load, as either of these |
| 2360 | * actions would just result in more rebalancing later, and ping-pong |
| 2361 | * tasks around. Thus we look for the minimum possible imbalance. |
| 2362 | * Negative imbalances (*we* are more loaded than anyone else) will |
| 2363 | * be counted as no imbalance for these purposes -- we can't fix that |
| 2364 | * by pulling tasks to us. Be careful of negative numbers as they'll |
| 2365 | * appear as very large values with unsigned longs. |
| 2366 | */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2367 | if (max_load <= busiest_load_per_task) |
| 2368 | goto out_balanced; |
| 2369 | |
| 2370 | /* |
| 2371 | * In the presence of smp nice balancing, certain scenarios can have |
| 2372 | * max load less than avg load(as we skip the groups at or below |
| 2373 | * its cpu_power, while calculating max_load..) |
| 2374 | */ |
| 2375 | if (max_load < avg_load) { |
| 2376 | *imbalance = 0; |
| 2377 | goto small_imbalance; |
| 2378 | } |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2379 | |
| 2380 | /* Don't want to pull so many tasks that a group would go idle */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2381 | max_pull = min(max_load - avg_load, max_load - busiest_load_per_task); |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | /* How much load to actually move to equalise the imbalance */ |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2384 | *imbalance = min(max_pull * busiest->cpu_power, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | (avg_load - this_load) * this->cpu_power) |
| 2386 | / SCHED_LOAD_SCALE; |
| 2387 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2388 | /* |
| 2389 | * if *imbalance is less than the average load per runnable task |
| 2390 | * there is no gaurantee that any tasks will be moved so we'll have |
| 2391 | * a think about bumping its value to force at least one task to be |
| 2392 | * moved |
| 2393 | */ |
| 2394 | if (*imbalance < busiest_load_per_task) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2395 | unsigned long tmp, pwr_now, pwr_move; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2396 | unsigned int imbn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2398 | small_imbalance: |
| 2399 | pwr_move = pwr_now = 0; |
| 2400 | imbn = 2; |
| 2401 | if (this_nr_running) { |
| 2402 | this_load_per_task /= this_nr_running; |
| 2403 | if (busiest_load_per_task > this_load_per_task) |
| 2404 | imbn = 1; |
| 2405 | } else |
| 2406 | this_load_per_task = SCHED_LOAD_SCALE; |
| 2407 | |
| 2408 | if (max_load - this_load >= busiest_load_per_task * imbn) { |
| 2409 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | return busiest; |
| 2411 | } |
| 2412 | |
| 2413 | /* |
| 2414 | * OK, we don't have enough imbalance to justify moving tasks, |
| 2415 | * however we may be able to increase total CPU power used by |
| 2416 | * moving them. |
| 2417 | */ |
| 2418 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2419 | pwr_now += busiest->cpu_power * |
| 2420 | min(busiest_load_per_task, max_load); |
| 2421 | pwr_now += this->cpu_power * |
| 2422 | min(this_load_per_task, this_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | pwr_now /= SCHED_LOAD_SCALE; |
| 2424 | |
| 2425 | /* Amount of load we'd subtract */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2426 | tmp = busiest_load_per_task*SCHED_LOAD_SCALE/busiest->cpu_power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | if (max_load > tmp) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2428 | pwr_move += busiest->cpu_power * |
| 2429 | min(busiest_load_per_task, max_load - tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | |
| 2431 | /* Amount of load we'd add */ |
| 2432 | if (max_load*busiest->cpu_power < |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2433 | busiest_load_per_task*SCHED_LOAD_SCALE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | tmp = max_load*busiest->cpu_power/this->cpu_power; |
| 2435 | else |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2436 | tmp = busiest_load_per_task*SCHED_LOAD_SCALE/this->cpu_power; |
| 2437 | pwr_move += this->cpu_power*min(this_load_per_task, this_load + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | pwr_move /= SCHED_LOAD_SCALE; |
| 2439 | |
| 2440 | /* Move if we gain throughput */ |
| 2441 | if (pwr_move <= pwr_now) |
| 2442 | goto out_balanced; |
| 2443 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2444 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | return busiest; |
| 2448 | |
| 2449 | out_balanced: |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2450 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2451 | if (idle == NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
| 2452 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2454 | if (this == group_leader && group_leader != group_min) { |
| 2455 | *imbalance = min_load_per_task; |
| 2456 | return group_min; |
| 2457 | } |
| 2458 | ret: |
| 2459 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | *imbalance = 0; |
| 2461 | return NULL; |
| 2462 | } |
| 2463 | |
| 2464 | /* |
| 2465 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 2466 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2467 | static struct rq * |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2468 | find_busiest_queue(struct sched_group *group, enum idle_type idle, |
| 2469 | unsigned long imbalance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2471 | struct rq *busiest = NULL, *rq; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2472 | unsigned long max_load = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | int i; |
| 2474 | |
| 2475 | for_each_cpu_mask(i, group->cpumask) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2476 | rq = cpu_rq(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2478 | if (rq->nr_running == 1 && rq->raw_weighted_load > imbalance) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2479 | continue; |
| 2480 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2481 | if (rq->raw_weighted_load > max_load) { |
| 2482 | max_load = rq->raw_weighted_load; |
| 2483 | busiest = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | return busiest; |
| 2488 | } |
| 2489 | |
| 2490 | /* |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 2491 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 2492 | * so long as it is large enough. |
| 2493 | */ |
| 2494 | #define MAX_PINNED_INTERVAL 512 |
| 2495 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2496 | static inline unsigned long minus_1_or_zero(unsigned long n) |
| 2497 | { |
| 2498 | return n > 0 ? n - 1 : 0; |
| 2499 | } |
| 2500 | |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 2501 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 2503 | * tasks if there is an imbalance. |
| 2504 | * |
| 2505 | * Called with this_rq unlocked. |
| 2506 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2507 | static int load_balance(int this_cpu, struct rq *this_rq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | struct sched_domain *sd, enum idle_type idle) |
| 2509 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2510 | int nr_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | struct sched_group *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | unsigned long imbalance; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2513 | struct rq *busiest; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2514 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2515 | if (idle != NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
| 2516 | !sched_smt_power_savings) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2517 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | schedstat_inc(sd, lb_cnt[idle]); |
| 2520 | |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2521 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | if (!group) { |
| 2523 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 2524 | goto out_balanced; |
| 2525 | } |
| 2526 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2527 | busiest = find_busiest_queue(group, idle, imbalance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | if (!busiest) { |
| 2529 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 2530 | goto out_balanced; |
| 2531 | } |
| 2532 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 2533 | BUG_ON(busiest == this_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | |
| 2535 | schedstat_add(sd, lb_imbalance[idle], imbalance); |
| 2536 | |
| 2537 | nr_moved = 0; |
| 2538 | if (busiest->nr_running > 1) { |
| 2539 | /* |
| 2540 | * Attempt to move tasks. If find_busiest_group has found |
| 2541 | * an imbalance but busiest->nr_running <= 1, the group is |
| 2542 | * still unbalanced. nr_moved simply stays zero, so it is |
| 2543 | * correctly treated as an imbalance. |
| 2544 | */ |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 2545 | double_rq_lock(this_rq, busiest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | nr_moved = move_tasks(this_rq, this_cpu, busiest, |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2547 | minus_1_or_zero(busiest->nr_running), |
| 2548 | imbalance, sd, idle, &all_pinned); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 2549 | double_rq_unlock(this_rq, busiest); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2550 | |
| 2551 | /* All tasks on this runqueue were pinned by CPU affinity */ |
| 2552 | if (unlikely(all_pinned)) |
| 2553 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | if (!nr_moved) { |
| 2557 | schedstat_inc(sd, lb_failed[idle]); |
| 2558 | sd->nr_balance_failed++; |
| 2559 | |
| 2560 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | |
| 2562 | spin_lock(&busiest->lock); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2563 | |
| 2564 | /* don't kick the migration_thread, if the curr |
| 2565 | * task on busiest cpu can't be moved to this_cpu |
| 2566 | */ |
| 2567 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { |
| 2568 | spin_unlock(&busiest->lock); |
| 2569 | all_pinned = 1; |
| 2570 | goto out_one_pinned; |
| 2571 | } |
| 2572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | if (!busiest->active_balance) { |
| 2574 | busiest->active_balance = 1; |
| 2575 | busiest->push_cpu = this_cpu; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2576 | active_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | } |
| 2578 | spin_unlock(&busiest->lock); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2579 | if (active_balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | wake_up_process(busiest->migration_thread); |
| 2581 | |
| 2582 | /* |
| 2583 | * We've kicked active balancing, reset the failure |
| 2584 | * counter. |
| 2585 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2586 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2588 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | sd->nr_balance_failed = 0; |
| 2590 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2591 | if (likely(!active_balance)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | /* We were unbalanced, so reset the balancing interval */ |
| 2593 | sd->balance_interval = sd->min_interval; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2594 | } else { |
| 2595 | /* |
| 2596 | * If we've begun active balancing, start to back off. This |
| 2597 | * case may not be covered by the all_pinned logic if there |
| 2598 | * is only 1 task on the busy runqueue (because we don't call |
| 2599 | * move_tasks). |
| 2600 | */ |
| 2601 | if (sd->balance_interval < sd->max_interval) |
| 2602 | sd->balance_interval *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | } |
| 2604 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2605 | if (!nr_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 2606 | !sched_smt_power_savings) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2607 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | return nr_moved; |
| 2609 | |
| 2610 | out_balanced: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | schedstat_inc(sd, lb_balanced[idle]); |
| 2612 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2613 | sd->nr_balance_failed = 0; |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2614 | |
| 2615 | out_one_pinned: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | /* tune up the balancing interval */ |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 2617 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
| 2618 | (sd->balance_interval < sd->max_interval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | sd->balance_interval *= 2; |
| 2620 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2621 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 2622 | !sched_smt_power_savings) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2623 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | return 0; |
| 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 2629 | * tasks if there is an imbalance. |
| 2630 | * |
| 2631 | * Called from schedule when this_rq is about to become idle (NEWLY_IDLE). |
| 2632 | * this_rq is locked. |
| 2633 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2634 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2635 | load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | { |
| 2637 | struct sched_group *group; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2638 | struct rq *busiest = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | unsigned long imbalance; |
| 2640 | int nr_moved = 0; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2641 | int sd_idle = 0; |
| 2642 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2643 | if (sd->flags & SD_SHARE_CPUPOWER && !sched_smt_power_savings) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2644 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | |
| 2646 | schedstat_inc(sd, lb_cnt[NEWLY_IDLE]); |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2647 | group = find_busiest_group(sd, this_cpu, &imbalance, NEWLY_IDLE, &sd_idle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | if (!group) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | schedstat_inc(sd, lb_nobusyg[NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2650 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | } |
| 2652 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2653 | busiest = find_busiest_queue(group, NEWLY_IDLE, imbalance); |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 2654 | if (!busiest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | schedstat_inc(sd, lb_nobusyq[NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2656 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | } |
| 2658 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 2659 | BUG_ON(busiest == this_rq); |
| 2660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | schedstat_add(sd, lb_imbalance[NEWLY_IDLE], imbalance); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 2662 | |
| 2663 | nr_moved = 0; |
| 2664 | if (busiest->nr_running > 1) { |
| 2665 | /* Attempt to move tasks */ |
| 2666 | double_lock_balance(this_rq, busiest); |
| 2667 | nr_moved = move_tasks(this_rq, this_cpu, busiest, |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2668 | minus_1_or_zero(busiest->nr_running), |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2669 | imbalance, sd, NEWLY_IDLE, NULL); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 2670 | spin_unlock(&busiest->lock); |
| 2671 | } |
| 2672 | |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2673 | if (!nr_moved) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | schedstat_inc(sd, lb_failed[NEWLY_IDLE]); |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2675 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER) |
| 2676 | return -1; |
| 2677 | } else |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2678 | sd->nr_balance_failed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | return nr_moved; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2681 | |
| 2682 | out_balanced: |
| 2683 | schedstat_inc(sd, lb_balanced[NEWLY_IDLE]); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2684 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 2685 | !sched_smt_power_savings) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2686 | return -1; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2687 | sd->nr_balance_failed = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2688 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2689 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | /* |
| 2693 | * idle_balance is called by schedule() if this_cpu is about to become |
| 2694 | * idle. Attempts to pull tasks from other CPUs. |
| 2695 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2696 | static void idle_balance(int this_cpu, struct rq *this_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | { |
| 2698 | struct sched_domain *sd; |
| 2699 | |
| 2700 | for_each_domain(this_cpu, sd) { |
| 2701 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2702 | /* If we've pulled tasks over stop searching: */ |
| 2703 | if (load_balance_newidle(this_cpu, this_rq, sd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | } |
| 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | /* |
| 2710 | * active_load_balance is run by migration threads. It pushes running tasks |
| 2711 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be |
| 2712 | * running on each physical CPU where possible, and avoids physical / |
| 2713 | * logical imbalances. |
| 2714 | * |
| 2715 | * Called with busiest_rq locked. |
| 2716 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2717 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2719 | int target_cpu = busiest_rq->push_cpu; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2720 | struct sched_domain *sd; |
| 2721 | struct rq *target_rq; |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2722 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2723 | /* Is there any task to move? */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2724 | if (busiest_rq->nr_running <= 1) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2725 | return; |
| 2726 | |
| 2727 | target_rq = cpu_rq(target_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | |
| 2729 | /* |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2730 | * This condition is "impossible", if it occurs |
| 2731 | * we need to fix it. Originally reported by |
| 2732 | * Bjorn Helgaas on a 128-cpu setup. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2734 | BUG_ON(busiest_rq == target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2736 | /* move a task from busiest_rq to target_rq */ |
| 2737 | double_lock_balance(busiest_rq, target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2739 | /* Search for an sd spanning us and the target CPU. */ |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2740 | for_each_domain(target_cpu, sd) { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2741 | if ((sd->flags & SD_LOAD_BALANCE) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2742 | cpu_isset(busiest_cpu, sd->span)) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2743 | break; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2746 | if (likely(sd)) { |
| 2747 | schedstat_inc(sd, alb_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2749 | if (move_tasks(target_rq, target_cpu, busiest_rq, 1, |
| 2750 | RTPRIO_TO_LOAD_WEIGHT(100), sd, SCHED_IDLE, |
| 2751 | NULL)) |
| 2752 | schedstat_inc(sd, alb_pushed); |
| 2753 | else |
| 2754 | schedstat_inc(sd, alb_failed); |
| 2755 | } |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2756 | spin_unlock(&target_rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | /* |
| 2760 | * rebalance_tick will get called every timer tick, on every CPU. |
| 2761 | * |
| 2762 | * It checks each scheduling domain to see if it is due to be balanced, |
| 2763 | * and initiates a balancing operation if so. |
| 2764 | * |
| 2765 | * Balancing parameters are set up in arch_init_sched_domains. |
| 2766 | */ |
| 2767 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2768 | /* Don't have all balancing operations going off at once: */ |
| 2769 | static inline unsigned long cpu_offset(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2771 | return jiffies + cpu * HZ / NR_CPUS; |
| 2772 | } |
| 2773 | |
| 2774 | static void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2775 | rebalance_tick(int this_cpu, struct rq *this_rq, enum idle_type idle) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2776 | { |
| 2777 | unsigned long this_load, interval, j = cpu_offset(this_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | struct sched_domain *sd; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2779 | int i, scale; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2781 | this_load = this_rq->raw_weighted_load; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2782 | |
| 2783 | /* Update our load: */ |
| 2784 | for (i = 0, scale = 1; i < 3; i++, scale <<= 1) { |
| 2785 | unsigned long old_load, new_load; |
| 2786 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2787 | old_load = this_rq->cpu_load[i]; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2788 | new_load = this_load; |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2789 | /* |
| 2790 | * Round up the averaging division if load is increasing. This |
| 2791 | * prevents us from getting stuck on 9 if the load is 10, for |
| 2792 | * example. |
| 2793 | */ |
| 2794 | if (new_load > old_load) |
| 2795 | new_load += scale-1; |
| 2796 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) / scale; |
| 2797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | |
| 2799 | for_each_domain(this_cpu, sd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 2801 | continue; |
| 2802 | |
| 2803 | interval = sd->balance_interval; |
| 2804 | if (idle != SCHED_IDLE) |
| 2805 | interval *= sd->busy_factor; |
| 2806 | |
| 2807 | /* scale ms to jiffies */ |
| 2808 | interval = msecs_to_jiffies(interval); |
| 2809 | if (unlikely(!interval)) |
| 2810 | interval = 1; |
| 2811 | |
| 2812 | if (j - sd->last_balance >= interval) { |
| 2813 | if (load_balance(this_cpu, this_rq, sd, idle)) { |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2814 | /* |
| 2815 | * We've pulled tasks over so either we're no |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2816 | * longer idle, or one of our SMT siblings is |
| 2817 | * not idle. |
| 2818 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | idle = NOT_IDLE; |
| 2820 | } |
| 2821 | sd->last_balance += interval; |
| 2822 | } |
| 2823 | } |
| 2824 | } |
| 2825 | #else |
| 2826 | /* |
| 2827 | * on UP we do not need to balance between CPUs: |
| 2828 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2829 | static inline void rebalance_tick(int cpu, struct rq *rq, enum idle_type idle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | { |
| 2831 | } |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2832 | static inline void idle_balance(int cpu, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | { |
| 2834 | } |
| 2835 | #endif |
| 2836 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2837 | static inline int wake_priority_sleeper(struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | { |
| 2839 | int ret = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | #ifdef CONFIG_SCHED_SMT |
| 2842 | spin_lock(&rq->lock); |
| 2843 | /* |
| 2844 | * If an SMT sibling task has been put to sleep for priority |
| 2845 | * reasons reschedule the idle task to see if it can now run. |
| 2846 | */ |
| 2847 | if (rq->nr_running) { |
| 2848 | resched_task(rq->idle); |
| 2849 | ret = 1; |
| 2850 | } |
| 2851 | spin_unlock(&rq->lock); |
| 2852 | #endif |
| 2853 | return ret; |
| 2854 | } |
| 2855 | |
| 2856 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
| 2857 | |
| 2858 | EXPORT_PER_CPU_SYMBOL(kstat); |
| 2859 | |
| 2860 | /* |
| 2861 | * This is called on clock ticks and on context switches. |
| 2862 | * Bank in p->sched_time the ns elapsed since the last tick or switch. |
| 2863 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2864 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2865 | update_cpu_clock(struct task_struct *p, struct rq *rq, unsigned long long now) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2867 | p->sched_time += now - max(p->timestamp, rq->timestamp_last_tick); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | /* |
| 2871 | * Return current->sched_time plus any more ns on the sched_clock |
| 2872 | * that have not yet been banked. |
| 2873 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2874 | unsigned long long current_sched_time(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | { |
| 2876 | unsigned long long ns; |
| 2877 | unsigned long flags; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | local_irq_save(flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2880 | ns = max(p->timestamp, task_rq(p)->timestamp_last_tick); |
| 2881 | ns = p->sched_time + sched_clock() - ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | local_irq_restore(flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | return ns; |
| 2885 | } |
| 2886 | |
| 2887 | /* |
Linus Torvalds | f1adad7 | 2006-05-21 18:54:09 -0700 | [diff] [blame] | 2888 | * We place interactive tasks back into the active array, if possible. |
| 2889 | * |
| 2890 | * To guarantee that this does not starve expired tasks we ignore the |
| 2891 | * interactivity of a task if the first expired task had to wait more |
| 2892 | * than a 'reasonable' amount of time. This deadline timeout is |
| 2893 | * load-dependent, as the frequency of array switched decreases with |
| 2894 | * increasing number of running tasks. We also ignore the interactivity |
| 2895 | * if a better static_prio task has expired: |
| 2896 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2897 | static inline int expired_starving(struct rq *rq) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2898 | { |
| 2899 | if (rq->curr->static_prio > rq->best_expired_prio) |
| 2900 | return 1; |
| 2901 | if (!STARVATION_LIMIT || !rq->expired_timestamp) |
| 2902 | return 0; |
| 2903 | if (jiffies - rq->expired_timestamp > STARVATION_LIMIT * rq->nr_running) |
| 2904 | return 1; |
| 2905 | return 0; |
| 2906 | } |
Linus Torvalds | f1adad7 | 2006-05-21 18:54:09 -0700 | [diff] [blame] | 2907 | |
| 2908 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | * Account user cpu time to a process. |
| 2910 | * @p: the process that the cpu time gets accounted to |
| 2911 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 2912 | * @cputime: the cpu time spent in user space since the last update |
| 2913 | */ |
| 2914 | void account_user_time(struct task_struct *p, cputime_t cputime) |
| 2915 | { |
| 2916 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 2917 | cputime64_t tmp; |
| 2918 | |
| 2919 | p->utime = cputime_add(p->utime, cputime); |
| 2920 | |
| 2921 | /* Add user time to cpustat. */ |
| 2922 | tmp = cputime_to_cputime64(cputime); |
| 2923 | if (TASK_NICE(p) > 0) |
| 2924 | cpustat->nice = cputime64_add(cpustat->nice, tmp); |
| 2925 | else |
| 2926 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 2927 | } |
| 2928 | |
| 2929 | /* |
| 2930 | * Account system cpu time to a process. |
| 2931 | * @p: the process that the cpu time gets accounted to |
| 2932 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 2933 | * @cputime: the cpu time spent in kernel space since the last update |
| 2934 | */ |
| 2935 | void account_system_time(struct task_struct *p, int hardirq_offset, |
| 2936 | cputime_t cputime) |
| 2937 | { |
| 2938 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2939 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | cputime64_t tmp; |
| 2941 | |
| 2942 | p->stime = cputime_add(p->stime, cputime); |
| 2943 | |
| 2944 | /* Add system time to cpustat. */ |
| 2945 | tmp = cputime_to_cputime64(cputime); |
| 2946 | if (hardirq_count() - hardirq_offset) |
| 2947 | cpustat->irq = cputime64_add(cpustat->irq, tmp); |
| 2948 | else if (softirq_count()) |
| 2949 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); |
| 2950 | else if (p != rq->idle) |
| 2951 | cpustat->system = cputime64_add(cpustat->system, tmp); |
| 2952 | else if (atomic_read(&rq->nr_iowait) > 0) |
| 2953 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 2954 | else |
| 2955 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 2956 | /* Account for system time used */ |
| 2957 | acct_update_integrals(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | /* |
| 2961 | * Account for involuntary wait time. |
| 2962 | * @p: the process from which the cpu time has been stolen |
| 2963 | * @steal: the cpu time spent in involuntary wait |
| 2964 | */ |
| 2965 | void account_steal_time(struct task_struct *p, cputime_t steal) |
| 2966 | { |
| 2967 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 2968 | cputime64_t tmp = cputime_to_cputime64(steal); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2969 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | |
| 2971 | if (p == rq->idle) { |
| 2972 | p->stime = cputime_add(p->stime, steal); |
| 2973 | if (atomic_read(&rq->nr_iowait) > 0) |
| 2974 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 2975 | else |
| 2976 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 2977 | } else |
| 2978 | cpustat->steal = cputime64_add(cpustat->steal, tmp); |
| 2979 | } |
| 2980 | |
| 2981 | /* |
| 2982 | * This function gets called by the timer code, with HZ frequency. |
| 2983 | * We call it with interrupts disabled. |
| 2984 | * |
| 2985 | * It also gets called by the fork code, when changing the parent's |
| 2986 | * timeslices. |
| 2987 | */ |
| 2988 | void scheduler_tick(void) |
| 2989 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2990 | unsigned long long now = sched_clock(); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2991 | struct task_struct *p = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | int cpu = smp_processor_id(); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2993 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | |
| 2995 | update_cpu_clock(p, rq, now); |
| 2996 | |
| 2997 | rq->timestamp_last_tick = now; |
| 2998 | |
| 2999 | if (p == rq->idle) { |
| 3000 | if (wake_priority_sleeper(rq)) |
| 3001 | goto out; |
| 3002 | rebalance_tick(cpu, rq, SCHED_IDLE); |
| 3003 | return; |
| 3004 | } |
| 3005 | |
| 3006 | /* Task might have expired already, but not scheduled off yet */ |
| 3007 | if (p->array != rq->active) { |
| 3008 | set_tsk_need_resched(p); |
| 3009 | goto out; |
| 3010 | } |
| 3011 | spin_lock(&rq->lock); |
| 3012 | /* |
| 3013 | * The task was running during this tick - update the |
| 3014 | * time slice counter. Note: we do not update a thread's |
| 3015 | * priority until it either goes to sleep or uses up its |
| 3016 | * timeslice. This makes it possible for interactive tasks |
| 3017 | * to use up their timeslices at their highest priority levels. |
| 3018 | */ |
| 3019 | if (rt_task(p)) { |
| 3020 | /* |
| 3021 | * RR tasks need a special form of timeslice management. |
| 3022 | * FIFO tasks have no timeslices. |
| 3023 | */ |
| 3024 | if ((p->policy == SCHED_RR) && !--p->time_slice) { |
| 3025 | p->time_slice = task_timeslice(p); |
| 3026 | p->first_time_slice = 0; |
| 3027 | set_tsk_need_resched(p); |
| 3028 | |
| 3029 | /* put it at the end of the queue: */ |
| 3030 | requeue_task(p, rq->active); |
| 3031 | } |
| 3032 | goto out_unlock; |
| 3033 | } |
| 3034 | if (!--p->time_slice) { |
| 3035 | dequeue_task(p, rq->active); |
| 3036 | set_tsk_need_resched(p); |
| 3037 | p->prio = effective_prio(p); |
| 3038 | p->time_slice = task_timeslice(p); |
| 3039 | p->first_time_slice = 0; |
| 3040 | |
| 3041 | if (!rq->expired_timestamp) |
| 3042 | rq->expired_timestamp = jiffies; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3043 | if (!TASK_INTERACTIVE(p) || expired_starving(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | enqueue_task(p, rq->expired); |
| 3045 | if (p->static_prio < rq->best_expired_prio) |
| 3046 | rq->best_expired_prio = p->static_prio; |
| 3047 | } else |
| 3048 | enqueue_task(p, rq->active); |
| 3049 | } else { |
| 3050 | /* |
| 3051 | * Prevent a too long timeslice allowing a task to monopolize |
| 3052 | * the CPU. We do this by splitting up the timeslice into |
| 3053 | * smaller pieces. |
| 3054 | * |
| 3055 | * Note: this does not mean the task's timeslices expire or |
| 3056 | * get lost in any way, they just might be preempted by |
| 3057 | * another task of equal priority. (one with higher |
| 3058 | * priority would have preempted this task already.) We |
| 3059 | * requeue this task to the end of the list on this priority |
| 3060 | * level, which is in essence a round-robin of tasks with |
| 3061 | * equal priority. |
| 3062 | * |
| 3063 | * This only applies to tasks in the interactive |
| 3064 | * delta range with at least TIMESLICE_GRANULARITY to requeue. |
| 3065 | */ |
| 3066 | if (TASK_INTERACTIVE(p) && !((task_timeslice(p) - |
| 3067 | p->time_slice) % TIMESLICE_GRANULARITY(p)) && |
| 3068 | (p->time_slice >= TIMESLICE_GRANULARITY(p)) && |
| 3069 | (p->array == rq->active)) { |
| 3070 | |
| 3071 | requeue_task(p, rq->active); |
| 3072 | set_tsk_need_resched(p); |
| 3073 | } |
| 3074 | } |
| 3075 | out_unlock: |
| 3076 | spin_unlock(&rq->lock); |
| 3077 | out: |
| 3078 | rebalance_tick(cpu, rq, NOT_IDLE); |
| 3079 | } |
| 3080 | |
| 3081 | #ifdef CONFIG_SCHED_SMT |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3082 | static inline void wakeup_busy_runqueue(struct rq *rq) |
Con Kolivas | fc38ed7 | 2005-09-10 00:26:08 -0700 | [diff] [blame] | 3083 | { |
| 3084 | /* If an SMT runqueue is sleeping due to priority reasons wake it up */ |
| 3085 | if (rq->curr == rq->idle && rq->nr_running) |
| 3086 | resched_task(rq->idle); |
| 3087 | } |
| 3088 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3089 | /* |
| 3090 | * Called with interrupt disabled and this_rq's runqueue locked. |
| 3091 | */ |
| 3092 | static void wake_sleeping_dependent(int this_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | { |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 3094 | struct sched_domain *tmp, *sd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | int i; |
| 3096 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3097 | for_each_domain(this_cpu, tmp) { |
| 3098 | if (tmp->flags & SD_SHARE_CPUPOWER) { |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 3099 | sd = tmp; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3100 | break; |
| 3101 | } |
| 3102 | } |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 3103 | |
| 3104 | if (!sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | return; |
| 3106 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3107 | for_each_cpu_mask(i, sd->span) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3108 | struct rq *smt_rq = cpu_rq(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3110 | if (i == this_cpu) |
| 3111 | continue; |
| 3112 | if (unlikely(!spin_trylock(&smt_rq->lock))) |
| 3113 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3115 | wakeup_busy_runqueue(smt_rq); |
| 3116 | spin_unlock(&smt_rq->lock); |
| 3117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
Ingo Molnar | 67f9a61 | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3120 | /* |
| 3121 | * number of 'lost' timeslices this task wont be able to fully |
| 3122 | * utilize, if another task runs on a sibling. This models the |
| 3123 | * slowdown effect of other tasks running on siblings: |
| 3124 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3125 | static inline unsigned long |
| 3126 | smt_slice(struct task_struct *p, struct sched_domain *sd) |
Ingo Molnar | 67f9a61 | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3127 | { |
| 3128 | return p->time_slice * (100 - sd->per_cpu_gain) / 100; |
| 3129 | } |
| 3130 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3131 | /* |
| 3132 | * To minimise lock contention and not have to drop this_rq's runlock we only |
| 3133 | * trylock the sibling runqueues and bypass those runqueues if we fail to |
| 3134 | * acquire their lock. As we only trylock the normal locking order does not |
| 3135 | * need to be obeyed. |
| 3136 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3137 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3138 | dependent_sleeper(int this_cpu, struct rq *this_rq, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | { |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 3140 | struct sched_domain *tmp, *sd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | int ret = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3143 | /* kernel/rt threads do not participate in dependent sleeping */ |
| 3144 | if (!p->mm || rt_task(p)) |
| 3145 | return 0; |
| 3146 | |
| 3147 | for_each_domain(this_cpu, tmp) { |
| 3148 | if (tmp->flags & SD_SHARE_CPUPOWER) { |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 3149 | sd = tmp; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3150 | break; |
| 3151 | } |
| 3152 | } |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 3153 | |
| 3154 | if (!sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | return 0; |
| 3156 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3157 | for_each_cpu_mask(i, sd->span) { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3158 | struct task_struct *smt_curr; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3159 | struct rq *smt_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3161 | if (i == this_cpu) |
| 3162 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3164 | smt_rq = cpu_rq(i); |
| 3165 | if (unlikely(!spin_trylock(&smt_rq->lock))) |
| 3166 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3168 | smt_curr = smt_rq->curr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3170 | if (!smt_curr->mm) |
| 3171 | goto unlock; |
Con Kolivas | fc38ed7 | 2005-09-10 00:26:08 -0700 | [diff] [blame] | 3172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | /* |
| 3174 | * If a user task with lower static priority than the |
| 3175 | * running task on the SMT sibling is trying to schedule, |
| 3176 | * delay it till there is proportionately less timeslice |
| 3177 | * left of the sibling task to prevent a lower priority |
| 3178 | * task from using an unfair proportion of the |
| 3179 | * physical cpu's resources. -ck |
| 3180 | */ |
Con Kolivas | fc38ed7 | 2005-09-10 00:26:08 -0700 | [diff] [blame] | 3181 | if (rt_task(smt_curr)) { |
| 3182 | /* |
| 3183 | * With real time tasks we run non-rt tasks only |
| 3184 | * per_cpu_gain% of the time. |
| 3185 | */ |
| 3186 | if ((jiffies % DEF_TIMESLICE) > |
| 3187 | (sd->per_cpu_gain * DEF_TIMESLICE / 100)) |
| 3188 | ret = 1; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3189 | } else { |
Ingo Molnar | 67f9a61 | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 3190 | if (smt_curr->static_prio < p->static_prio && |
| 3191 | !TASK_PREEMPTS_CURR(p, smt_rq) && |
| 3192 | smt_slice(smt_curr, sd) > task_timeslice(p)) |
Con Kolivas | fc38ed7 | 2005-09-10 00:26:08 -0700 | [diff] [blame] | 3193 | ret = 1; |
Con Kolivas | fc38ed7 | 2005-09-10 00:26:08 -0700 | [diff] [blame] | 3194 | } |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3195 | unlock: |
| 3196 | spin_unlock(&smt_rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | return ret; |
| 3199 | } |
| 3200 | #else |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3201 | static inline void wake_sleeping_dependent(int this_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | { |
| 3203 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3204 | static inline int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3205 | dependent_sleeper(int this_cpu, struct rq *this_rq, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | { |
| 3207 | return 0; |
| 3208 | } |
| 3209 | #endif |
| 3210 | |
| 3211 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
| 3212 | |
| 3213 | void fastcall add_preempt_count(int val) |
| 3214 | { |
| 3215 | /* |
| 3216 | * Underflow? |
| 3217 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3218 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
| 3219 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3220 | preempt_count() += val; |
| 3221 | /* |
| 3222 | * Spinlock count overflowing soon? |
| 3223 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3224 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= PREEMPT_MASK-10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3225 | } |
| 3226 | EXPORT_SYMBOL(add_preempt_count); |
| 3227 | |
| 3228 | void fastcall sub_preempt_count(int val) |
| 3229 | { |
| 3230 | /* |
| 3231 | * Underflow? |
| 3232 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3233 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
| 3234 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | /* |
| 3236 | * Is the spinlock portion underflowing? |
| 3237 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3238 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
| 3239 | !(preempt_count() & PREEMPT_MASK))) |
| 3240 | return; |
| 3241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | preempt_count() -= val; |
| 3243 | } |
| 3244 | EXPORT_SYMBOL(sub_preempt_count); |
| 3245 | |
| 3246 | #endif |
| 3247 | |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 3248 | static inline int interactive_sleep(enum sleep_type sleep_type) |
| 3249 | { |
| 3250 | return (sleep_type == SLEEP_INTERACTIVE || |
| 3251 | sleep_type == SLEEP_INTERRUPTED); |
| 3252 | } |
| 3253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | /* |
| 3255 | * schedule() is the main scheduler function. |
| 3256 | */ |
| 3257 | asmlinkage void __sched schedule(void) |
| 3258 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3259 | struct task_struct *prev, *next; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3260 | struct prio_array *array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | struct list_head *queue; |
| 3262 | unsigned long long now; |
| 3263 | unsigned long run_time; |
Chen Shang | a3464a1 | 2005-06-25 14:57:31 -0700 | [diff] [blame] | 3264 | int cpu, idx, new_prio; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3265 | long *switch_count; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3266 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | |
| 3268 | /* |
| 3269 | * Test if we are atomic. Since do_exit() needs to call into |
| 3270 | * schedule() atomically, we ignore that path for now. |
| 3271 | * Otherwise, whine if we are scheduling when we should not be. |
| 3272 | */ |
Andreas Mohr | 77e4bfb | 2006-03-27 01:15:20 -0800 | [diff] [blame] | 3273 | if (unlikely(in_atomic() && !current->exit_state)) { |
| 3274 | printk(KERN_ERR "BUG: scheduling while atomic: " |
| 3275 | "%s/0x%08x/%d\n", |
| 3276 | current->comm, preempt_count(), current->pid); |
| 3277 | dump_stack(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | } |
| 3279 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| 3280 | |
| 3281 | need_resched: |
| 3282 | preempt_disable(); |
| 3283 | prev = current; |
| 3284 | release_kernel_lock(prev); |
| 3285 | need_resched_nonpreemptible: |
| 3286 | rq = this_rq(); |
| 3287 | |
| 3288 | /* |
| 3289 | * The idle thread is not allowed to schedule! |
| 3290 | * Remove this check after it has been exercised a bit. |
| 3291 | */ |
| 3292 | if (unlikely(prev == rq->idle) && prev->state != TASK_RUNNING) { |
| 3293 | printk(KERN_ERR "bad: scheduling from the idle thread!\n"); |
| 3294 | dump_stack(); |
| 3295 | } |
| 3296 | |
| 3297 | schedstat_inc(rq, sched_cnt); |
| 3298 | now = sched_clock(); |
Ingo Molnar | 238628e | 2005-04-18 10:58:36 -0700 | [diff] [blame] | 3299 | if (likely((long long)(now - prev->timestamp) < NS_MAX_SLEEP_AVG)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | run_time = now - prev->timestamp; |
Ingo Molnar | 238628e | 2005-04-18 10:58:36 -0700 | [diff] [blame] | 3301 | if (unlikely((long long)(now - prev->timestamp) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | run_time = 0; |
| 3303 | } else |
| 3304 | run_time = NS_MAX_SLEEP_AVG; |
| 3305 | |
| 3306 | /* |
| 3307 | * Tasks charged proportionately less run_time at high sleep_avg to |
| 3308 | * delay them losing their interactive status |
| 3309 | */ |
| 3310 | run_time /= (CURRENT_BONUS(prev) ? : 1); |
| 3311 | |
| 3312 | spin_lock_irq(&rq->lock); |
| 3313 | |
| 3314 | if (unlikely(prev->flags & PF_DEAD)) |
| 3315 | prev->state = EXIT_DEAD; |
| 3316 | |
| 3317 | switch_count = &prev->nivcsw; |
| 3318 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
| 3319 | switch_count = &prev->nvcsw; |
| 3320 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
| 3321 | unlikely(signal_pending(prev)))) |
| 3322 | prev->state = TASK_RUNNING; |
| 3323 | else { |
| 3324 | if (prev->state == TASK_UNINTERRUPTIBLE) |
| 3325 | rq->nr_uninterruptible++; |
| 3326 | deactivate_task(prev, rq); |
| 3327 | } |
| 3328 | } |
| 3329 | |
| 3330 | cpu = smp_processor_id(); |
| 3331 | if (unlikely(!rq->nr_running)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | idle_balance(cpu, rq); |
| 3333 | if (!rq->nr_running) { |
| 3334 | next = rq->idle; |
| 3335 | rq->expired_timestamp = 0; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3336 | wake_sleeping_dependent(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | goto switch_tasks; |
| 3338 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | array = rq->active; |
| 3342 | if (unlikely(!array->nr_active)) { |
| 3343 | /* |
| 3344 | * Switch the active and expired arrays. |
| 3345 | */ |
| 3346 | schedstat_inc(rq, sched_switch); |
| 3347 | rq->active = rq->expired; |
| 3348 | rq->expired = array; |
| 3349 | array = rq->active; |
| 3350 | rq->expired_timestamp = 0; |
| 3351 | rq->best_expired_prio = MAX_PRIO; |
| 3352 | } |
| 3353 | |
| 3354 | idx = sched_find_first_bit(array->bitmap); |
| 3355 | queue = array->queue + idx; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3356 | next = list_entry(queue->next, struct task_struct, run_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 3358 | if (!rt_task(next) && interactive_sleep(next->sleep_type)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | unsigned long long delta = now - next->timestamp; |
Ingo Molnar | 238628e | 2005-04-18 10:58:36 -0700 | [diff] [blame] | 3360 | if (unlikely((long long)(now - next->timestamp) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | delta = 0; |
| 3362 | |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 3363 | if (next->sleep_type == SLEEP_INTERACTIVE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | delta = delta * (ON_RUNQUEUE_WEIGHT * 128 / 100) / 128; |
| 3365 | |
| 3366 | array = next->array; |
Chen Shang | a3464a1 | 2005-06-25 14:57:31 -0700 | [diff] [blame] | 3367 | new_prio = recalc_task_prio(next, next->timestamp + delta); |
| 3368 | |
| 3369 | if (unlikely(next->prio != new_prio)) { |
| 3370 | dequeue_task(next, array); |
| 3371 | next->prio = new_prio; |
| 3372 | enqueue_task(next, array); |
Con Kolivas | 7c4bb1f | 2006-03-31 02:31:29 -0800 | [diff] [blame] | 3373 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | } |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 3375 | next->sleep_type = SLEEP_NORMAL; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 3376 | if (dependent_sleeper(cpu, rq, next)) |
| 3377 | next = rq->idle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | switch_tasks: |
| 3379 | if (next == rq->idle) |
| 3380 | schedstat_inc(rq, sched_goidle); |
| 3381 | prefetch(next); |
Chen, Kenneth W | 383f283 | 2005-09-09 13:02:02 -0700 | [diff] [blame] | 3382 | prefetch_stack(next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | clear_tsk_need_resched(prev); |
| 3384 | rcu_qsctr_inc(task_cpu(prev)); |
| 3385 | |
| 3386 | update_cpu_clock(prev, rq, now); |
| 3387 | |
| 3388 | prev->sleep_avg -= run_time; |
| 3389 | if ((long)prev->sleep_avg <= 0) |
| 3390 | prev->sleep_avg = 0; |
| 3391 | prev->timestamp = prev->last_ran = now; |
| 3392 | |
| 3393 | sched_info_switch(prev, next); |
| 3394 | if (likely(prev != next)) { |
| 3395 | next->timestamp = now; |
| 3396 | rq->nr_switches++; |
| 3397 | rq->curr = next; |
| 3398 | ++*switch_count; |
| 3399 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 3400 | prepare_task_switch(rq, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | prev = context_switch(rq, prev, next); |
| 3402 | barrier(); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 3403 | /* |
| 3404 | * this_rq must be evaluated again because prev may have moved |
| 3405 | * CPUs since it called schedule(), thus the 'rq' on its stack |
| 3406 | * frame will be invalid. |
| 3407 | */ |
| 3408 | finish_task_switch(this_rq(), prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | } else |
| 3410 | spin_unlock_irq(&rq->lock); |
| 3411 | |
| 3412 | prev = current; |
| 3413 | if (unlikely(reacquire_kernel_lock(prev) < 0)) |
| 3414 | goto need_resched_nonpreemptible; |
| 3415 | preempt_enable_no_resched(); |
| 3416 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 3417 | goto need_resched; |
| 3418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | EXPORT_SYMBOL(schedule); |
| 3420 | |
| 3421 | #ifdef CONFIG_PREEMPT |
| 3422 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 3423 | * this is the entry point to schedule() from in-kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | * off of preempt_enable. Kernel preemptions off return from interrupt |
| 3425 | * occur there and call schedule directly. |
| 3426 | */ |
| 3427 | asmlinkage void __sched preempt_schedule(void) |
| 3428 | { |
| 3429 | struct thread_info *ti = current_thread_info(); |
| 3430 | #ifdef CONFIG_PREEMPT_BKL |
| 3431 | struct task_struct *task = current; |
| 3432 | int saved_lock_depth; |
| 3433 | #endif |
| 3434 | /* |
| 3435 | * If there is a non-zero preempt_count or interrupts are disabled, |
| 3436 | * we do not want to preempt the current task. Just return.. |
| 3437 | */ |
| 3438 | if (unlikely(ti->preempt_count || irqs_disabled())) |
| 3439 | return; |
| 3440 | |
| 3441 | need_resched: |
| 3442 | add_preempt_count(PREEMPT_ACTIVE); |
| 3443 | /* |
| 3444 | * We keep the big kernel semaphore locked, but we |
| 3445 | * clear ->lock_depth so that schedule() doesnt |
| 3446 | * auto-release the semaphore: |
| 3447 | */ |
| 3448 | #ifdef CONFIG_PREEMPT_BKL |
| 3449 | saved_lock_depth = task->lock_depth; |
| 3450 | task->lock_depth = -1; |
| 3451 | #endif |
| 3452 | schedule(); |
| 3453 | #ifdef CONFIG_PREEMPT_BKL |
| 3454 | task->lock_depth = saved_lock_depth; |
| 3455 | #endif |
| 3456 | sub_preempt_count(PREEMPT_ACTIVE); |
| 3457 | |
| 3458 | /* we could miss a preemption opportunity between schedule and now */ |
| 3459 | barrier(); |
| 3460 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 3461 | goto need_resched; |
| 3462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | EXPORT_SYMBOL(preempt_schedule); |
| 3464 | |
| 3465 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 3466 | * this is the entry point to schedule() from kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | * off of irq context. |
| 3468 | * Note, that this is called and return with irqs disabled. This will |
| 3469 | * protect us against recursive calling from irq. |
| 3470 | */ |
| 3471 | asmlinkage void __sched preempt_schedule_irq(void) |
| 3472 | { |
| 3473 | struct thread_info *ti = current_thread_info(); |
| 3474 | #ifdef CONFIG_PREEMPT_BKL |
| 3475 | struct task_struct *task = current; |
| 3476 | int saved_lock_depth; |
| 3477 | #endif |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 3478 | /* Catch callers which need to be fixed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
| 3480 | |
| 3481 | need_resched: |
| 3482 | add_preempt_count(PREEMPT_ACTIVE); |
| 3483 | /* |
| 3484 | * We keep the big kernel semaphore locked, but we |
| 3485 | * clear ->lock_depth so that schedule() doesnt |
| 3486 | * auto-release the semaphore: |
| 3487 | */ |
| 3488 | #ifdef CONFIG_PREEMPT_BKL |
| 3489 | saved_lock_depth = task->lock_depth; |
| 3490 | task->lock_depth = -1; |
| 3491 | #endif |
| 3492 | local_irq_enable(); |
| 3493 | schedule(); |
| 3494 | local_irq_disable(); |
| 3495 | #ifdef CONFIG_PREEMPT_BKL |
| 3496 | task->lock_depth = saved_lock_depth; |
| 3497 | #endif |
| 3498 | sub_preempt_count(PREEMPT_ACTIVE); |
| 3499 | |
| 3500 | /* we could miss a preemption opportunity between schedule and now */ |
| 3501 | barrier(); |
| 3502 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 3503 | goto need_resched; |
| 3504 | } |
| 3505 | |
| 3506 | #endif /* CONFIG_PREEMPT */ |
| 3507 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3508 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
| 3509 | void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3511 | return try_to_wake_up(curr->private, mode, sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | EXPORT_SYMBOL(default_wake_function); |
| 3514 | |
| 3515 | /* |
| 3516 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
| 3517 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve |
| 3518 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
| 3519 | * |
| 3520 | * There are circumstances in which we can try to wake a task which has already |
| 3521 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns |
| 3522 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
| 3523 | */ |
| 3524 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, |
| 3525 | int nr_exclusive, int sync, void *key) |
| 3526 | { |
| 3527 | struct list_head *tmp, *next; |
| 3528 | |
| 3529 | list_for_each_safe(tmp, next, &q->task_list) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3530 | wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list); |
| 3531 | unsigned flags = curr->flags; |
| 3532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | if (curr->func(curr, mode, sync, key) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3534 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | break; |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | /** |
| 3540 | * __wake_up - wake up threads blocked on a waitqueue. |
| 3541 | * @q: the waitqueue |
| 3542 | * @mode: which threads |
| 3543 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 3544 | * @key: is directly passed to the wakeup function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | */ |
| 3546 | void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3547 | int nr_exclusive, void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | { |
| 3549 | unsigned long flags; |
| 3550 | |
| 3551 | spin_lock_irqsave(&q->lock, flags); |
| 3552 | __wake_up_common(q, mode, nr_exclusive, 0, key); |
| 3553 | spin_unlock_irqrestore(&q->lock, flags); |
| 3554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | EXPORT_SYMBOL(__wake_up); |
| 3556 | |
| 3557 | /* |
| 3558 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. |
| 3559 | */ |
| 3560 | void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
| 3561 | { |
| 3562 | __wake_up_common(q, mode, 1, 0, NULL); |
| 3563 | } |
| 3564 | |
| 3565 | /** |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 3566 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | * @q: the waitqueue |
| 3568 | * @mode: which threads |
| 3569 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
| 3570 | * |
| 3571 | * The sync wakeup differs that the waker knows that it will schedule |
| 3572 | * away soon, so while the target thread will be woken up, it will not |
| 3573 | * be migrated to another CPU - ie. the two threads are 'synchronized' |
| 3574 | * with each other. This can prevent needless bouncing between CPUs. |
| 3575 | * |
| 3576 | * On UP it can prevent extra preemption. |
| 3577 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3578 | void fastcall |
| 3579 | __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3580 | { |
| 3581 | unsigned long flags; |
| 3582 | int sync = 1; |
| 3583 | |
| 3584 | if (unlikely(!q)) |
| 3585 | return; |
| 3586 | |
| 3587 | if (unlikely(!nr_exclusive)) |
| 3588 | sync = 0; |
| 3589 | |
| 3590 | spin_lock_irqsave(&q->lock, flags); |
| 3591 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); |
| 3592 | spin_unlock_irqrestore(&q->lock, flags); |
| 3593 | } |
| 3594 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ |
| 3595 | |
| 3596 | void fastcall complete(struct completion *x) |
| 3597 | { |
| 3598 | unsigned long flags; |
| 3599 | |
| 3600 | spin_lock_irqsave(&x->wait.lock, flags); |
| 3601 | x->done++; |
| 3602 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, |
| 3603 | 1, 0, NULL); |
| 3604 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 3605 | } |
| 3606 | EXPORT_SYMBOL(complete); |
| 3607 | |
| 3608 | void fastcall complete_all(struct completion *x) |
| 3609 | { |
| 3610 | unsigned long flags; |
| 3611 | |
| 3612 | spin_lock_irqsave(&x->wait.lock, flags); |
| 3613 | x->done += UINT_MAX/2; |
| 3614 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, |
| 3615 | 0, 0, NULL); |
| 3616 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 3617 | } |
| 3618 | EXPORT_SYMBOL(complete_all); |
| 3619 | |
| 3620 | void fastcall __sched wait_for_completion(struct completion *x) |
| 3621 | { |
| 3622 | might_sleep(); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | spin_lock_irq(&x->wait.lock); |
| 3625 | if (!x->done) { |
| 3626 | DECLARE_WAITQUEUE(wait, current); |
| 3627 | |
| 3628 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3629 | __add_wait_queue_tail(&x->wait, &wait); |
| 3630 | do { |
| 3631 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 3632 | spin_unlock_irq(&x->wait.lock); |
| 3633 | schedule(); |
| 3634 | spin_lock_irq(&x->wait.lock); |
| 3635 | } while (!x->done); |
| 3636 | __remove_wait_queue(&x->wait, &wait); |
| 3637 | } |
| 3638 | x->done--; |
| 3639 | spin_unlock_irq(&x->wait.lock); |
| 3640 | } |
| 3641 | EXPORT_SYMBOL(wait_for_completion); |
| 3642 | |
| 3643 | unsigned long fastcall __sched |
| 3644 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
| 3645 | { |
| 3646 | might_sleep(); |
| 3647 | |
| 3648 | spin_lock_irq(&x->wait.lock); |
| 3649 | if (!x->done) { |
| 3650 | DECLARE_WAITQUEUE(wait, current); |
| 3651 | |
| 3652 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3653 | __add_wait_queue_tail(&x->wait, &wait); |
| 3654 | do { |
| 3655 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 3656 | spin_unlock_irq(&x->wait.lock); |
| 3657 | timeout = schedule_timeout(timeout); |
| 3658 | spin_lock_irq(&x->wait.lock); |
| 3659 | if (!timeout) { |
| 3660 | __remove_wait_queue(&x->wait, &wait); |
| 3661 | goto out; |
| 3662 | } |
| 3663 | } while (!x->done); |
| 3664 | __remove_wait_queue(&x->wait, &wait); |
| 3665 | } |
| 3666 | x->done--; |
| 3667 | out: |
| 3668 | spin_unlock_irq(&x->wait.lock); |
| 3669 | return timeout; |
| 3670 | } |
| 3671 | EXPORT_SYMBOL(wait_for_completion_timeout); |
| 3672 | |
| 3673 | int fastcall __sched wait_for_completion_interruptible(struct completion *x) |
| 3674 | { |
| 3675 | int ret = 0; |
| 3676 | |
| 3677 | might_sleep(); |
| 3678 | |
| 3679 | spin_lock_irq(&x->wait.lock); |
| 3680 | if (!x->done) { |
| 3681 | DECLARE_WAITQUEUE(wait, current); |
| 3682 | |
| 3683 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3684 | __add_wait_queue_tail(&x->wait, &wait); |
| 3685 | do { |
| 3686 | if (signal_pending(current)) { |
| 3687 | ret = -ERESTARTSYS; |
| 3688 | __remove_wait_queue(&x->wait, &wait); |
| 3689 | goto out; |
| 3690 | } |
| 3691 | __set_current_state(TASK_INTERRUPTIBLE); |
| 3692 | spin_unlock_irq(&x->wait.lock); |
| 3693 | schedule(); |
| 3694 | spin_lock_irq(&x->wait.lock); |
| 3695 | } while (!x->done); |
| 3696 | __remove_wait_queue(&x->wait, &wait); |
| 3697 | } |
| 3698 | x->done--; |
| 3699 | out: |
| 3700 | spin_unlock_irq(&x->wait.lock); |
| 3701 | |
| 3702 | return ret; |
| 3703 | } |
| 3704 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
| 3705 | |
| 3706 | unsigned long fastcall __sched |
| 3707 | wait_for_completion_interruptible_timeout(struct completion *x, |
| 3708 | unsigned long timeout) |
| 3709 | { |
| 3710 | might_sleep(); |
| 3711 | |
| 3712 | spin_lock_irq(&x->wait.lock); |
| 3713 | if (!x->done) { |
| 3714 | DECLARE_WAITQUEUE(wait, current); |
| 3715 | |
| 3716 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3717 | __add_wait_queue_tail(&x->wait, &wait); |
| 3718 | do { |
| 3719 | if (signal_pending(current)) { |
| 3720 | timeout = -ERESTARTSYS; |
| 3721 | __remove_wait_queue(&x->wait, &wait); |
| 3722 | goto out; |
| 3723 | } |
| 3724 | __set_current_state(TASK_INTERRUPTIBLE); |
| 3725 | spin_unlock_irq(&x->wait.lock); |
| 3726 | timeout = schedule_timeout(timeout); |
| 3727 | spin_lock_irq(&x->wait.lock); |
| 3728 | if (!timeout) { |
| 3729 | __remove_wait_queue(&x->wait, &wait); |
| 3730 | goto out; |
| 3731 | } |
| 3732 | } while (!x->done); |
| 3733 | __remove_wait_queue(&x->wait, &wait); |
| 3734 | } |
| 3735 | x->done--; |
| 3736 | out: |
| 3737 | spin_unlock_irq(&x->wait.lock); |
| 3738 | return timeout; |
| 3739 | } |
| 3740 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
| 3741 | |
| 3742 | |
| 3743 | #define SLEEP_ON_VAR \ |
| 3744 | unsigned long flags; \ |
| 3745 | wait_queue_t wait; \ |
| 3746 | init_waitqueue_entry(&wait, current); |
| 3747 | |
| 3748 | #define SLEEP_ON_HEAD \ |
| 3749 | spin_lock_irqsave(&q->lock,flags); \ |
| 3750 | __add_wait_queue(q, &wait); \ |
| 3751 | spin_unlock(&q->lock); |
| 3752 | |
| 3753 | #define SLEEP_ON_TAIL \ |
| 3754 | spin_lock_irq(&q->lock); \ |
| 3755 | __remove_wait_queue(q, &wait); \ |
| 3756 | spin_unlock_irqrestore(&q->lock, flags); |
| 3757 | |
| 3758 | void fastcall __sched interruptible_sleep_on(wait_queue_head_t *q) |
| 3759 | { |
| 3760 | SLEEP_ON_VAR |
| 3761 | |
| 3762 | current->state = TASK_INTERRUPTIBLE; |
| 3763 | |
| 3764 | SLEEP_ON_HEAD |
| 3765 | schedule(); |
| 3766 | SLEEP_ON_TAIL |
| 3767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | EXPORT_SYMBOL(interruptible_sleep_on); |
| 3769 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3770 | long fastcall __sched |
| 3771 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | { |
| 3773 | SLEEP_ON_VAR |
| 3774 | |
| 3775 | current->state = TASK_INTERRUPTIBLE; |
| 3776 | |
| 3777 | SLEEP_ON_HEAD |
| 3778 | timeout = schedule_timeout(timeout); |
| 3779 | SLEEP_ON_TAIL |
| 3780 | |
| 3781 | return timeout; |
| 3782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
| 3784 | |
| 3785 | void fastcall __sched sleep_on(wait_queue_head_t *q) |
| 3786 | { |
| 3787 | SLEEP_ON_VAR |
| 3788 | |
| 3789 | current->state = TASK_UNINTERRUPTIBLE; |
| 3790 | |
| 3791 | SLEEP_ON_HEAD |
| 3792 | schedule(); |
| 3793 | SLEEP_ON_TAIL |
| 3794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | EXPORT_SYMBOL(sleep_on); |
| 3796 | |
| 3797 | long fastcall __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
| 3798 | { |
| 3799 | SLEEP_ON_VAR |
| 3800 | |
| 3801 | current->state = TASK_UNINTERRUPTIBLE; |
| 3802 | |
| 3803 | SLEEP_ON_HEAD |
| 3804 | timeout = schedule_timeout(timeout); |
| 3805 | SLEEP_ON_TAIL |
| 3806 | |
| 3807 | return timeout; |
| 3808 | } |
| 3809 | |
| 3810 | EXPORT_SYMBOL(sleep_on_timeout); |
| 3811 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3812 | #ifdef CONFIG_RT_MUTEXES |
| 3813 | |
| 3814 | /* |
| 3815 | * rt_mutex_setprio - set the current priority of a task |
| 3816 | * @p: task |
| 3817 | * @prio: prio value (kernel-internal form) |
| 3818 | * |
| 3819 | * This function changes the 'effective' priority of a task. It does |
| 3820 | * not touch ->normal_prio like __setscheduler(). |
| 3821 | * |
| 3822 | * Used by the rt_mutex code to implement priority inheritance logic. |
| 3823 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3824 | void rt_mutex_setprio(struct task_struct *p, int prio) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3825 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3826 | struct prio_array *array; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3827 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3828 | struct rq *rq; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3829 | int oldprio; |
| 3830 | |
| 3831 | BUG_ON(prio < 0 || prio > MAX_PRIO); |
| 3832 | |
| 3833 | rq = task_rq_lock(p, &flags); |
| 3834 | |
| 3835 | oldprio = p->prio; |
| 3836 | array = p->array; |
| 3837 | if (array) |
| 3838 | dequeue_task(p, array); |
| 3839 | p->prio = prio; |
| 3840 | |
| 3841 | if (array) { |
| 3842 | /* |
| 3843 | * If changing to an RT priority then queue it |
| 3844 | * in the active array! |
| 3845 | */ |
| 3846 | if (rt_task(p)) |
| 3847 | array = rq->active; |
| 3848 | enqueue_task(p, array); |
| 3849 | /* |
| 3850 | * Reschedule if we are currently running on this runqueue and |
| 3851 | * our priority decreased, or if we are not currently running on |
| 3852 | * this runqueue and our priority is higher than the current's |
| 3853 | */ |
| 3854 | if (task_running(rq, p)) { |
| 3855 | if (p->prio > oldprio) |
| 3856 | resched_task(rq->curr); |
| 3857 | } else if (TASK_PREEMPTS_CURR(p, rq)) |
| 3858 | resched_task(rq->curr); |
| 3859 | } |
| 3860 | task_rq_unlock(rq, &flags); |
| 3861 | } |
| 3862 | |
| 3863 | #endif |
| 3864 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3865 | void set_user_nice(struct task_struct *p, long nice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3867 | struct prio_array *array; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3868 | int old_prio, delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3870 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | |
| 3872 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) |
| 3873 | return; |
| 3874 | /* |
| 3875 | * We have to be careful, if called from sys_setpriority(), |
| 3876 | * the task might be in the middle of scheduling on another CPU. |
| 3877 | */ |
| 3878 | rq = task_rq_lock(p, &flags); |
| 3879 | /* |
| 3880 | * The RT priorities are set via sched_setscheduler(), but we still |
| 3881 | * allow the 'normal' nice value to be set - but as expected |
| 3882 | * it wont have any effect on scheduling until the task is |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 3883 | * not SCHED_NORMAL/SCHED_BATCH: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3884 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3885 | if (has_rt_policy(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | p->static_prio = NICE_TO_PRIO(nice); |
| 3887 | goto out_unlock; |
| 3888 | } |
| 3889 | array = p->array; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3890 | if (array) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | dequeue_task(p, array); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3892 | dec_raw_weighted_load(rq, p); |
| 3893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | p->static_prio = NICE_TO_PRIO(nice); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3896 | set_load_weight(p); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3897 | old_prio = p->prio; |
| 3898 | p->prio = effective_prio(p); |
| 3899 | delta = p->prio - old_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | |
| 3901 | if (array) { |
| 3902 | enqueue_task(p, array); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3903 | inc_raw_weighted_load(rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | /* |
| 3905 | * If the task increased its priority or is running and |
| 3906 | * lowered its priority, then reschedule its CPU: |
| 3907 | */ |
| 3908 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
| 3909 | resched_task(rq->curr); |
| 3910 | } |
| 3911 | out_unlock: |
| 3912 | task_rq_unlock(rq, &flags); |
| 3913 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | EXPORT_SYMBOL(set_user_nice); |
| 3915 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3916 | /* |
| 3917 | * can_nice - check if a task can reduce its nice value |
| 3918 | * @p: task |
| 3919 | * @nice: nice value |
| 3920 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3921 | int can_nice(const struct task_struct *p, const int nice) |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3922 | { |
Matt Mackall | 024f474 | 2005-08-18 11:24:19 -0700 | [diff] [blame] | 3923 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
| 3924 | int nice_rlim = 20 - nice; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3925 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3926 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
| 3927 | capable(CAP_SYS_NICE)); |
| 3928 | } |
| 3929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | #ifdef __ARCH_WANT_SYS_NICE |
| 3931 | |
| 3932 | /* |
| 3933 | * sys_nice - change the priority of the current process. |
| 3934 | * @increment: priority increment |
| 3935 | * |
| 3936 | * sys_setpriority is a more generic, but much slower function that |
| 3937 | * does similar things. |
| 3938 | */ |
| 3939 | asmlinkage long sys_nice(int increment) |
| 3940 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3941 | long nice, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | |
| 3943 | /* |
| 3944 | * Setpriority might change our priority at the same moment. |
| 3945 | * We don't have to worry. Conceptually one call occurs first |
| 3946 | * and we have a single winner. |
| 3947 | */ |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3948 | if (increment < -40) |
| 3949 | increment = -40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | if (increment > 40) |
| 3951 | increment = 40; |
| 3952 | |
| 3953 | nice = PRIO_TO_NICE(current->static_prio) + increment; |
| 3954 | if (nice < -20) |
| 3955 | nice = -20; |
| 3956 | if (nice > 19) |
| 3957 | nice = 19; |
| 3958 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3959 | if (increment < 0 && !can_nice(current, nice)) |
| 3960 | return -EPERM; |
| 3961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | retval = security_task_setnice(current, nice); |
| 3963 | if (retval) |
| 3964 | return retval; |
| 3965 | |
| 3966 | set_user_nice(current, nice); |
| 3967 | return 0; |
| 3968 | } |
| 3969 | |
| 3970 | #endif |
| 3971 | |
| 3972 | /** |
| 3973 | * task_prio - return the priority value of a given task. |
| 3974 | * @p: the task in question. |
| 3975 | * |
| 3976 | * This is the priority value as seen by users in /proc. |
| 3977 | * RT tasks are offset by -200. Normal tasks are centered |
| 3978 | * around 0, value goes from -16 to +15. |
| 3979 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3980 | int task_prio(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | { |
| 3982 | return p->prio - MAX_RT_PRIO; |
| 3983 | } |
| 3984 | |
| 3985 | /** |
| 3986 | * task_nice - return the nice value of a given task. |
| 3987 | * @p: the task in question. |
| 3988 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3989 | int task_nice(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | { |
| 3991 | return TASK_NICE(p); |
| 3992 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3993 | EXPORT_SYMBOL_GPL(task_nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3994 | |
| 3995 | /** |
| 3996 | * idle_cpu - is a given cpu idle currently? |
| 3997 | * @cpu: the processor in question. |
| 3998 | */ |
| 3999 | int idle_cpu(int cpu) |
| 4000 | { |
| 4001 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; |
| 4002 | } |
| 4003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | /** |
| 4005 | * idle_task - return the idle task for a given cpu. |
| 4006 | * @cpu: the processor in question. |
| 4007 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4008 | struct task_struct *idle_task(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | { |
| 4010 | return cpu_rq(cpu)->idle; |
| 4011 | } |
| 4012 | |
| 4013 | /** |
| 4014 | * find_process_by_pid - find a process with a matching PID value. |
| 4015 | * @pid: the pid in question. |
| 4016 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4017 | static inline struct task_struct *find_process_by_pid(pid_t pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | { |
| 4019 | return pid ? find_task_by_pid(pid) : current; |
| 4020 | } |
| 4021 | |
| 4022 | /* Actually do priority change: must hold rq lock. */ |
| 4023 | static void __setscheduler(struct task_struct *p, int policy, int prio) |
| 4024 | { |
| 4025 | BUG_ON(p->array); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | p->policy = policy; |
| 4028 | p->rt_priority = prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4029 | p->normal_prio = normal_prio(p); |
| 4030 | /* we are holding p->pi_lock already */ |
| 4031 | p->prio = rt_mutex_getprio(p); |
| 4032 | /* |
| 4033 | * SCHED_BATCH tasks are treated as perpetual CPU hogs: |
| 4034 | */ |
| 4035 | if (policy == SCHED_BATCH) |
| 4036 | p->sleep_avg = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 4037 | set_load_weight(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | /** |
| 4041 | * sched_setscheduler - change the scheduling policy and/or RT priority of |
| 4042 | * a thread. |
| 4043 | * @p: the task in question. |
| 4044 | * @policy: new policy. |
| 4045 | * @param: structure containing the new RT priority. |
| 4046 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4047 | int sched_setscheduler(struct task_struct *p, int policy, |
| 4048 | struct sched_param *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4050 | int retval, oldprio, oldpolicy = -1; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4051 | struct prio_array *array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4053 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | |
Steven Rostedt | 66e5393 | 2006-06-27 02:54:44 -0700 | [diff] [blame] | 4055 | /* may grab non-irq protected spin_locks */ |
| 4056 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | recheck: |
| 4058 | /* double check policy once rq lock held */ |
| 4059 | if (policy < 0) |
| 4060 | policy = oldpolicy = p->policy; |
| 4061 | else if (policy != SCHED_FIFO && policy != SCHED_RR && |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4062 | policy != SCHED_NORMAL && policy != SCHED_BATCH) |
| 4063 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | /* |
| 4065 | * Valid priorities for SCHED_FIFO and SCHED_RR are |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4066 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL and |
| 4067 | * SCHED_BATCH is 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | */ |
| 4069 | if (param->sched_priority < 0 || |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4070 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
Steven Rostedt | d46523e | 2005-07-25 16:28:39 -0400 | [diff] [blame] | 4071 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | return -EINVAL; |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4073 | if ((policy == SCHED_NORMAL || policy == SCHED_BATCH) |
| 4074 | != (param->sched_priority == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | return -EINVAL; |
| 4076 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4077 | /* |
| 4078 | * Allow unprivileged RT tasks to decrease priority: |
| 4079 | */ |
| 4080 | if (!capable(CAP_SYS_NICE)) { |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4081 | /* |
| 4082 | * can't change policy, except between SCHED_NORMAL |
| 4083 | * and SCHED_BATCH: |
| 4084 | */ |
| 4085 | if (((policy != SCHED_NORMAL && p->policy != SCHED_BATCH) && |
| 4086 | (policy != SCHED_BATCH && p->policy != SCHED_NORMAL)) && |
| 4087 | !p->signal->rlim[RLIMIT_RTPRIO].rlim_cur) |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4088 | return -EPERM; |
| 4089 | /* can't increase priority */ |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4090 | if ((policy != SCHED_NORMAL && policy != SCHED_BATCH) && |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4091 | param->sched_priority > p->rt_priority && |
| 4092 | param->sched_priority > |
| 4093 | p->signal->rlim[RLIMIT_RTPRIO].rlim_cur) |
| 4094 | return -EPERM; |
| 4095 | /* can't change other user's priorities */ |
| 4096 | if ((current->euid != p->euid) && |
| 4097 | (current->euid != p->uid)) |
| 4098 | return -EPERM; |
| 4099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | |
| 4101 | retval = security_task_setscheduler(p, policy, param); |
| 4102 | if (retval) |
| 4103 | return retval; |
| 4104 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4105 | * make sure no PI-waiters arrive (or leave) while we are |
| 4106 | * changing the priority of the task: |
| 4107 | */ |
| 4108 | spin_lock_irqsave(&p->pi_lock, flags); |
| 4109 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | * To be able to change p->policy safely, the apropriate |
| 4111 | * runqueue lock must be held. |
| 4112 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4113 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | /* recheck policy now with rq lock held */ |
| 4115 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| 4116 | policy = oldpolicy = -1; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4117 | __task_rq_unlock(rq); |
| 4118 | spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | goto recheck; |
| 4120 | } |
| 4121 | array = p->array; |
| 4122 | if (array) |
| 4123 | deactivate_task(p, rq); |
| 4124 | oldprio = p->prio; |
| 4125 | __setscheduler(p, policy, param->sched_priority); |
| 4126 | if (array) { |
| 4127 | __activate_task(p, rq); |
| 4128 | /* |
| 4129 | * Reschedule if we are currently running on this runqueue and |
| 4130 | * our priority decreased, or if we are not currently running on |
| 4131 | * this runqueue and our priority is higher than the current's |
| 4132 | */ |
| 4133 | if (task_running(rq, p)) { |
| 4134 | if (p->prio > oldprio) |
| 4135 | resched_task(rq->curr); |
| 4136 | } else if (TASK_PREEMPTS_CURR(p, rq)) |
| 4137 | resched_task(rq->curr); |
| 4138 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4139 | __task_rq_unlock(rq); |
| 4140 | spin_unlock_irqrestore(&p->pi_lock, flags); |
| 4141 | |
Thomas Gleixner | 95e02ca | 2006-06-27 02:55:02 -0700 | [diff] [blame] | 4142 | rt_mutex_adjust_pi(p); |
| 4143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | return 0; |
| 4145 | } |
| 4146 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
| 4147 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4148 | static int |
| 4149 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | struct sched_param lparam; |
| 4152 | struct task_struct *p; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4153 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | |
| 4155 | if (!param || pid < 0) |
| 4156 | return -EINVAL; |
| 4157 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| 4158 | return -EFAULT; |
| 4159 | read_lock_irq(&tasklist_lock); |
| 4160 | p = find_process_by_pid(pid); |
| 4161 | if (!p) { |
| 4162 | read_unlock_irq(&tasklist_lock); |
| 4163 | return -ESRCH; |
| 4164 | } |
Thomas Gleixner | e74c69f | 2006-06-27 02:55:00 -0700 | [diff] [blame] | 4165 | get_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | read_unlock_irq(&tasklist_lock); |
Thomas Gleixner | e74c69f | 2006-06-27 02:55:00 -0700 | [diff] [blame] | 4167 | retval = sched_setscheduler(p, policy, &lparam); |
| 4168 | put_task_struct(p); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | return retval; |
| 4171 | } |
| 4172 | |
| 4173 | /** |
| 4174 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| 4175 | * @pid: the pid in question. |
| 4176 | * @policy: new policy. |
| 4177 | * @param: structure containing the new RT priority. |
| 4178 | */ |
| 4179 | asmlinkage long sys_sched_setscheduler(pid_t pid, int policy, |
| 4180 | struct sched_param __user *param) |
| 4181 | { |
Jason Baron | c21761f | 2006-01-18 17:43:03 -0800 | [diff] [blame] | 4182 | /* negative values for policy are not valid */ |
| 4183 | if (policy < 0) |
| 4184 | return -EINVAL; |
| 4185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | return do_sched_setscheduler(pid, policy, param); |
| 4187 | } |
| 4188 | |
| 4189 | /** |
| 4190 | * sys_sched_setparam - set/change the RT priority of a thread |
| 4191 | * @pid: the pid in question. |
| 4192 | * @param: structure containing the new RT priority. |
| 4193 | */ |
| 4194 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) |
| 4195 | { |
| 4196 | return do_sched_setscheduler(pid, -1, param); |
| 4197 | } |
| 4198 | |
| 4199 | /** |
| 4200 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| 4201 | * @pid: the pid in question. |
| 4202 | */ |
| 4203 | asmlinkage long sys_sched_getscheduler(pid_t pid) |
| 4204 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4205 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | int retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | |
| 4208 | if (pid < 0) |
| 4209 | goto out_nounlock; |
| 4210 | |
| 4211 | retval = -ESRCH; |
| 4212 | read_lock(&tasklist_lock); |
| 4213 | p = find_process_by_pid(pid); |
| 4214 | if (p) { |
| 4215 | retval = security_task_getscheduler(p); |
| 4216 | if (!retval) |
| 4217 | retval = p->policy; |
| 4218 | } |
| 4219 | read_unlock(&tasklist_lock); |
| 4220 | |
| 4221 | out_nounlock: |
| 4222 | return retval; |
| 4223 | } |
| 4224 | |
| 4225 | /** |
| 4226 | * sys_sched_getscheduler - get the RT priority of a thread |
| 4227 | * @pid: the pid in question. |
| 4228 | * @param: structure containing the RT priority. |
| 4229 | */ |
| 4230 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) |
| 4231 | { |
| 4232 | struct sched_param lp; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4233 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | int retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | |
| 4236 | if (!param || pid < 0) |
| 4237 | goto out_nounlock; |
| 4238 | |
| 4239 | read_lock(&tasklist_lock); |
| 4240 | p = find_process_by_pid(pid); |
| 4241 | retval = -ESRCH; |
| 4242 | if (!p) |
| 4243 | goto out_unlock; |
| 4244 | |
| 4245 | retval = security_task_getscheduler(p); |
| 4246 | if (retval) |
| 4247 | goto out_unlock; |
| 4248 | |
| 4249 | lp.sched_priority = p->rt_priority; |
| 4250 | read_unlock(&tasklist_lock); |
| 4251 | |
| 4252 | /* |
| 4253 | * This one might sleep, we cannot do it with a spinlock held ... |
| 4254 | */ |
| 4255 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| 4256 | |
| 4257 | out_nounlock: |
| 4258 | return retval; |
| 4259 | |
| 4260 | out_unlock: |
| 4261 | read_unlock(&tasklist_lock); |
| 4262 | return retval; |
| 4263 | } |
| 4264 | |
| 4265 | long sched_setaffinity(pid_t pid, cpumask_t new_mask) |
| 4266 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | cpumask_t cpus_allowed; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4268 | struct task_struct *p; |
| 4269 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | |
| 4271 | lock_cpu_hotplug(); |
| 4272 | read_lock(&tasklist_lock); |
| 4273 | |
| 4274 | p = find_process_by_pid(pid); |
| 4275 | if (!p) { |
| 4276 | read_unlock(&tasklist_lock); |
| 4277 | unlock_cpu_hotplug(); |
| 4278 | return -ESRCH; |
| 4279 | } |
| 4280 | |
| 4281 | /* |
| 4282 | * It is not safe to call set_cpus_allowed with the |
| 4283 | * tasklist_lock held. We will bump the task_struct's |
| 4284 | * usage count and then drop tasklist_lock. |
| 4285 | */ |
| 4286 | get_task_struct(p); |
| 4287 | read_unlock(&tasklist_lock); |
| 4288 | |
| 4289 | retval = -EPERM; |
| 4290 | if ((current->euid != p->euid) && (current->euid != p->uid) && |
| 4291 | !capable(CAP_SYS_NICE)) |
| 4292 | goto out_unlock; |
| 4293 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 4294 | retval = security_task_setscheduler(p, 0, NULL); |
| 4295 | if (retval) |
| 4296 | goto out_unlock; |
| 4297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | cpus_allowed = cpuset_cpus_allowed(p); |
| 4299 | cpus_and(new_mask, new_mask, cpus_allowed); |
| 4300 | retval = set_cpus_allowed(p, new_mask); |
| 4301 | |
| 4302 | out_unlock: |
| 4303 | put_task_struct(p); |
| 4304 | unlock_cpu_hotplug(); |
| 4305 | return retval; |
| 4306 | } |
| 4307 | |
| 4308 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
| 4309 | cpumask_t *new_mask) |
| 4310 | { |
| 4311 | if (len < sizeof(cpumask_t)) { |
| 4312 | memset(new_mask, 0, sizeof(cpumask_t)); |
| 4313 | } else if (len > sizeof(cpumask_t)) { |
| 4314 | len = sizeof(cpumask_t); |
| 4315 | } |
| 4316 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| 4317 | } |
| 4318 | |
| 4319 | /** |
| 4320 | * sys_sched_setaffinity - set the cpu affinity of a process |
| 4321 | * @pid: pid of the process |
| 4322 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 4323 | * @user_mask_ptr: user-space pointer to the new cpu mask |
| 4324 | */ |
| 4325 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, |
| 4326 | unsigned long __user *user_mask_ptr) |
| 4327 | { |
| 4328 | cpumask_t new_mask; |
| 4329 | int retval; |
| 4330 | |
| 4331 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); |
| 4332 | if (retval) |
| 4333 | return retval; |
| 4334 | |
| 4335 | return sched_setaffinity(pid, new_mask); |
| 4336 | } |
| 4337 | |
| 4338 | /* |
| 4339 | * Represents all cpu's present in the system |
| 4340 | * In systems capable of hotplug, this map could dynamically grow |
| 4341 | * as new cpu's are detected in the system via any platform specific |
| 4342 | * method, such as ACPI for e.g. |
| 4343 | */ |
| 4344 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4345 | cpumask_t cpu_present_map __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | EXPORT_SYMBOL(cpu_present_map); |
| 4347 | |
| 4348 | #ifndef CONFIG_SMP |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4349 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
| 4350 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | #endif |
| 4352 | |
| 4353 | long sched_getaffinity(pid_t pid, cpumask_t *mask) |
| 4354 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4355 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | |
| 4358 | lock_cpu_hotplug(); |
| 4359 | read_lock(&tasklist_lock); |
| 4360 | |
| 4361 | retval = -ESRCH; |
| 4362 | p = find_process_by_pid(pid); |
| 4363 | if (!p) |
| 4364 | goto out_unlock; |
| 4365 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 4366 | retval = security_task_getscheduler(p); |
| 4367 | if (retval) |
| 4368 | goto out_unlock; |
| 4369 | |
Jack Steiner | 2f7016d | 2006-02-01 03:05:18 -0800 | [diff] [blame] | 4370 | cpus_and(*mask, p->cpus_allowed, cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | |
| 4372 | out_unlock: |
| 4373 | read_unlock(&tasklist_lock); |
| 4374 | unlock_cpu_hotplug(); |
| 4375 | if (retval) |
| 4376 | return retval; |
| 4377 | |
| 4378 | return 0; |
| 4379 | } |
| 4380 | |
| 4381 | /** |
| 4382 | * sys_sched_getaffinity - get the cpu affinity of a process |
| 4383 | * @pid: pid of the process |
| 4384 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 4385 | * @user_mask_ptr: user-space pointer to hold the current cpu mask |
| 4386 | */ |
| 4387 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, |
| 4388 | unsigned long __user *user_mask_ptr) |
| 4389 | { |
| 4390 | int ret; |
| 4391 | cpumask_t mask; |
| 4392 | |
| 4393 | if (len < sizeof(cpumask_t)) |
| 4394 | return -EINVAL; |
| 4395 | |
| 4396 | ret = sched_getaffinity(pid, &mask); |
| 4397 | if (ret < 0) |
| 4398 | return ret; |
| 4399 | |
| 4400 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) |
| 4401 | return -EFAULT; |
| 4402 | |
| 4403 | return sizeof(cpumask_t); |
| 4404 | } |
| 4405 | |
| 4406 | /** |
| 4407 | * sys_sched_yield - yield the current processor to other threads. |
| 4408 | * |
| 4409 | * this function yields the current CPU by moving the calling thread |
| 4410 | * to the expired array. If there are no other threads running on this |
| 4411 | * CPU then this function will return. |
| 4412 | */ |
| 4413 | asmlinkage long sys_sched_yield(void) |
| 4414 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4415 | struct rq *rq = this_rq_lock(); |
| 4416 | struct prio_array *array = current->array, *target = rq->expired; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | |
| 4418 | schedstat_inc(rq, yld_cnt); |
| 4419 | /* |
| 4420 | * We implement yielding by moving the task into the expired |
| 4421 | * queue. |
| 4422 | * |
| 4423 | * (special rule: RT tasks will just roundrobin in the active |
| 4424 | * array.) |
| 4425 | */ |
| 4426 | if (rt_task(current)) |
| 4427 | target = rq->active; |
| 4428 | |
Renaud Lienhart | 5927ad7 | 2005-09-10 00:26:20 -0700 | [diff] [blame] | 4429 | if (array->nr_active == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | schedstat_inc(rq, yld_act_empty); |
| 4431 | if (!rq->expired->nr_active) |
| 4432 | schedstat_inc(rq, yld_both_empty); |
| 4433 | } else if (!rq->expired->nr_active) |
| 4434 | schedstat_inc(rq, yld_exp_empty); |
| 4435 | |
| 4436 | if (array != target) { |
| 4437 | dequeue_task(current, array); |
| 4438 | enqueue_task(current, target); |
| 4439 | } else |
| 4440 | /* |
| 4441 | * requeue_task is cheaper so perform that if possible. |
| 4442 | */ |
| 4443 | requeue_task(current, array); |
| 4444 | |
| 4445 | /* |
| 4446 | * Since we are going to call schedule() anyway, there's |
| 4447 | * no need to preempt or enable interrupts: |
| 4448 | */ |
| 4449 | __release(rq->lock); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 4450 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | _raw_spin_unlock(&rq->lock); |
| 4452 | preempt_enable_no_resched(); |
| 4453 | |
| 4454 | schedule(); |
| 4455 | |
| 4456 | return 0; |
| 4457 | } |
| 4458 | |
Jim Houston | 2d7d253 | 2006-07-30 03:03:39 -0700 | [diff] [blame] | 4459 | static inline int __resched_legal(int expected_preempt_count) |
Andrew Morton | e7b3840 | 2006-06-30 01:56:00 -0700 | [diff] [blame] | 4460 | { |
Jim Houston | 2d7d253 | 2006-07-30 03:03:39 -0700 | [diff] [blame] | 4461 | if (unlikely(preempt_count() != expected_preempt_count)) |
Andrew Morton | e7b3840 | 2006-06-30 01:56:00 -0700 | [diff] [blame] | 4462 | return 0; |
| 4463 | if (unlikely(system_state != SYSTEM_RUNNING)) |
| 4464 | return 0; |
| 4465 | return 1; |
| 4466 | } |
| 4467 | |
| 4468 | static void __cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | { |
Ingo Molnar | 8e0a43d | 2006-06-23 02:05:23 -0700 | [diff] [blame] | 4470 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 4471 | __might_sleep(__FILE__, __LINE__); |
| 4472 | #endif |
Ingo Molnar | 5bbcfd9 | 2005-07-07 17:57:04 -0700 | [diff] [blame] | 4473 | /* |
| 4474 | * The BKS might be reacquired before we have dropped |
| 4475 | * PREEMPT_ACTIVE, which could trigger a second |
| 4476 | * cond_resched() call. |
| 4477 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | do { |
| 4479 | add_preempt_count(PREEMPT_ACTIVE); |
| 4480 | schedule(); |
| 4481 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4482 | } while (need_resched()); |
| 4483 | } |
| 4484 | |
| 4485 | int __sched cond_resched(void) |
| 4486 | { |
Jim Houston | 2d7d253 | 2006-07-30 03:03:39 -0700 | [diff] [blame] | 4487 | if (need_resched() && __resched_legal(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | __cond_resched(); |
| 4489 | return 1; |
| 4490 | } |
| 4491 | return 0; |
| 4492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | EXPORT_SYMBOL(cond_resched); |
| 4494 | |
| 4495 | /* |
| 4496 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, |
| 4497 | * call schedule, and on return reacquire the lock. |
| 4498 | * |
| 4499 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level |
| 4500 | * operations here to prevent schedule() from being called twice (once via |
| 4501 | * spin_unlock(), once by hand). |
| 4502 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4503 | int cond_resched_lock(spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | { |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4505 | int ret = 0; |
| 4506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | if (need_lockbreak(lock)) { |
| 4508 | spin_unlock(lock); |
| 4509 | cpu_relax(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4510 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | spin_lock(lock); |
| 4512 | } |
Jim Houston | 2d7d253 | 2006-07-30 03:03:39 -0700 | [diff] [blame] | 4513 | if (need_resched() && __resched_legal(1)) { |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 4514 | spin_release(&lock->dep_map, 1, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | _raw_spin_unlock(lock); |
| 4516 | preempt_enable_no_resched(); |
| 4517 | __cond_resched(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4518 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | spin_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | } |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4521 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | EXPORT_SYMBOL(cond_resched_lock); |
| 4524 | |
| 4525 | int __sched cond_resched_softirq(void) |
| 4526 | { |
| 4527 | BUG_ON(!in_softirq()); |
| 4528 | |
Jim Houston | 2d7d253 | 2006-07-30 03:03:39 -0700 | [diff] [blame] | 4529 | if (need_resched() && __resched_legal(0)) { |
Ingo Molnar | de30a2b | 2006-07-03 00:24:42 -0700 | [diff] [blame] | 4530 | raw_local_irq_disable(); |
| 4531 | _local_bh_enable(); |
| 4532 | raw_local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | __cond_resched(); |
| 4534 | local_bh_disable(); |
| 4535 | return 1; |
| 4536 | } |
| 4537 | return 0; |
| 4538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | EXPORT_SYMBOL(cond_resched_softirq); |
| 4540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | /** |
| 4542 | * yield - yield the current processor to other threads. |
| 4543 | * |
| 4544 | * this is a shortcut for kernel-space yielding - it marks the |
| 4545 | * thread runnable and calls sys_sched_yield(). |
| 4546 | */ |
| 4547 | void __sched yield(void) |
| 4548 | { |
| 4549 | set_current_state(TASK_RUNNING); |
| 4550 | sys_sched_yield(); |
| 4551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | EXPORT_SYMBOL(yield); |
| 4553 | |
| 4554 | /* |
| 4555 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
| 4556 | * that process accounting knows that this is a task in IO wait state. |
| 4557 | * |
| 4558 | * But don't do that if it is a deliberate, throttling IO wait (this task |
| 4559 | * has set its backing_dev_info: the queue against which it should throttle) |
| 4560 | */ |
| 4561 | void __sched io_schedule(void) |
| 4562 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4563 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4564 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 4565 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | atomic_inc(&rq->nr_iowait); |
| 4567 | schedule(); |
| 4568 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 4569 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | EXPORT_SYMBOL(io_schedule); |
| 4572 | |
| 4573 | long __sched io_schedule_timeout(long timeout) |
| 4574 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4575 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | long ret; |
| 4577 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 4578 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | atomic_inc(&rq->nr_iowait); |
| 4580 | ret = schedule_timeout(timeout); |
| 4581 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 4582 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | return ret; |
| 4584 | } |
| 4585 | |
| 4586 | /** |
| 4587 | * sys_sched_get_priority_max - return maximum RT priority. |
| 4588 | * @policy: scheduling class. |
| 4589 | * |
| 4590 | * this syscall returns the maximum rt_priority that can be used |
| 4591 | * by a given scheduling class. |
| 4592 | */ |
| 4593 | asmlinkage long sys_sched_get_priority_max(int policy) |
| 4594 | { |
| 4595 | int ret = -EINVAL; |
| 4596 | |
| 4597 | switch (policy) { |
| 4598 | case SCHED_FIFO: |
| 4599 | case SCHED_RR: |
| 4600 | ret = MAX_USER_RT_PRIO-1; |
| 4601 | break; |
| 4602 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4603 | case SCHED_BATCH: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | ret = 0; |
| 4605 | break; |
| 4606 | } |
| 4607 | return ret; |
| 4608 | } |
| 4609 | |
| 4610 | /** |
| 4611 | * sys_sched_get_priority_min - return minimum RT priority. |
| 4612 | * @policy: scheduling class. |
| 4613 | * |
| 4614 | * this syscall returns the minimum rt_priority that can be used |
| 4615 | * by a given scheduling class. |
| 4616 | */ |
| 4617 | asmlinkage long sys_sched_get_priority_min(int policy) |
| 4618 | { |
| 4619 | int ret = -EINVAL; |
| 4620 | |
| 4621 | switch (policy) { |
| 4622 | case SCHED_FIFO: |
| 4623 | case SCHED_RR: |
| 4624 | ret = 1; |
| 4625 | break; |
| 4626 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4627 | case SCHED_BATCH: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | ret = 0; |
| 4629 | } |
| 4630 | return ret; |
| 4631 | } |
| 4632 | |
| 4633 | /** |
| 4634 | * sys_sched_rr_get_interval - return the default timeslice of a process. |
| 4635 | * @pid: pid of the process. |
| 4636 | * @interval: userspace pointer to the timeslice value. |
| 4637 | * |
| 4638 | * this syscall writes the default timeslice value of a given process |
| 4639 | * into the user-space timespec buffer. A value of '0' means infinity. |
| 4640 | */ |
| 4641 | asmlinkage |
| 4642 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) |
| 4643 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4644 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | int retval = -EINVAL; |
| 4646 | struct timespec t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | |
| 4648 | if (pid < 0) |
| 4649 | goto out_nounlock; |
| 4650 | |
| 4651 | retval = -ESRCH; |
| 4652 | read_lock(&tasklist_lock); |
| 4653 | p = find_process_by_pid(pid); |
| 4654 | if (!p) |
| 4655 | goto out_unlock; |
| 4656 | |
| 4657 | retval = security_task_getscheduler(p); |
| 4658 | if (retval) |
| 4659 | goto out_unlock; |
| 4660 | |
Peter Williams | b78709c | 2006-06-26 16:58:00 +1000 | [diff] [blame] | 4661 | jiffies_to_timespec(p->policy == SCHED_FIFO ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | 0 : task_timeslice(p), &t); |
| 4663 | read_unlock(&tasklist_lock); |
| 4664 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
| 4665 | out_nounlock: |
| 4666 | return retval; |
| 4667 | out_unlock: |
| 4668 | read_unlock(&tasklist_lock); |
| 4669 | return retval; |
| 4670 | } |
| 4671 | |
| 4672 | static inline struct task_struct *eldest_child(struct task_struct *p) |
| 4673 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4674 | if (list_empty(&p->children)) |
| 4675 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | return list_entry(p->children.next,struct task_struct,sibling); |
| 4677 | } |
| 4678 | |
| 4679 | static inline struct task_struct *older_sibling(struct task_struct *p) |
| 4680 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4681 | if (p->sibling.prev==&p->parent->children) |
| 4682 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | return list_entry(p->sibling.prev,struct task_struct,sibling); |
| 4684 | } |
| 4685 | |
| 4686 | static inline struct task_struct *younger_sibling(struct task_struct *p) |
| 4687 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4688 | if (p->sibling.next==&p->parent->children) |
| 4689 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | return list_entry(p->sibling.next,struct task_struct,sibling); |
| 4691 | } |
| 4692 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4693 | static const char stat_nam[] = "RSDTtZX"; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4694 | |
| 4695 | static void show_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4697 | struct task_struct *relative; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | unsigned long free = 0; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4699 | unsigned state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | state = p->state ? __ffs(p->state) + 1 : 0; |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4702 | printk("%-13.13s %c", p->comm, |
| 4703 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | #if (BITS_PER_LONG == 32) |
| 4705 | if (state == TASK_RUNNING) |
| 4706 | printk(" running "); |
| 4707 | else |
| 4708 | printk(" %08lX ", thread_saved_pc(p)); |
| 4709 | #else |
| 4710 | if (state == TASK_RUNNING) |
| 4711 | printk(" running task "); |
| 4712 | else |
| 4713 | printk(" %016lx ", thread_saved_pc(p)); |
| 4714 | #endif |
| 4715 | #ifdef CONFIG_DEBUG_STACK_USAGE |
| 4716 | { |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 4717 | unsigned long *n = end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | while (!*n) |
| 4719 | n++; |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 4720 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | } |
| 4722 | #endif |
| 4723 | printk("%5lu %5d %6d ", free, p->pid, p->parent->pid); |
| 4724 | if ((relative = eldest_child(p))) |
| 4725 | printk("%5d ", relative->pid); |
| 4726 | else |
| 4727 | printk(" "); |
| 4728 | if ((relative = younger_sibling(p))) |
| 4729 | printk("%7d", relative->pid); |
| 4730 | else |
| 4731 | printk(" "); |
| 4732 | if ((relative = older_sibling(p))) |
| 4733 | printk(" %5d", relative->pid); |
| 4734 | else |
| 4735 | printk(" "); |
| 4736 | if (!p->mm) |
| 4737 | printk(" (L-TLB)\n"); |
| 4738 | else |
| 4739 | printk(" (NOTLB)\n"); |
| 4740 | |
| 4741 | if (state != TASK_RUNNING) |
| 4742 | show_stack(p, NULL); |
| 4743 | } |
| 4744 | |
| 4745 | void show_state(void) |
| 4746 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4747 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | |
| 4749 | #if (BITS_PER_LONG == 32) |
| 4750 | printk("\n" |
| 4751 | " sibling\n"); |
| 4752 | printk(" task PC pid father child younger older\n"); |
| 4753 | #else |
| 4754 | printk("\n" |
| 4755 | " sibling\n"); |
| 4756 | printk(" task PC pid father child younger older\n"); |
| 4757 | #endif |
| 4758 | read_lock(&tasklist_lock); |
| 4759 | do_each_thread(g, p) { |
| 4760 | /* |
| 4761 | * reset the NMI-timeout, listing all files on a slow |
| 4762 | * console might take alot of time: |
| 4763 | */ |
| 4764 | touch_nmi_watchdog(); |
| 4765 | show_task(p); |
| 4766 | } while_each_thread(g, p); |
| 4767 | |
| 4768 | read_unlock(&tasklist_lock); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 4769 | debug_show_all_locks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | } |
| 4771 | |
Ingo Molnar | f340c0d | 2005-06-28 16:40:42 +0200 | [diff] [blame] | 4772 | /** |
| 4773 | * init_idle - set up an idle thread for a given CPU |
| 4774 | * @idle: task in question |
| 4775 | * @cpu: cpu the idle task belongs to |
| 4776 | * |
| 4777 | * NOTE: this function does not set the idle thread's NEED_RESCHED |
| 4778 | * flag, to make booting more robust. |
| 4779 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4780 | void __devinit init_idle(struct task_struct *idle, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4782 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | unsigned long flags; |
| 4784 | |
Ingo Molnar | 81c29a8 | 2006-03-07 21:55:27 -0800 | [diff] [blame] | 4785 | idle->timestamp = sched_clock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4786 | idle->sleep_avg = 0; |
| 4787 | idle->array = NULL; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4788 | idle->prio = idle->normal_prio = MAX_PRIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | idle->state = TASK_RUNNING; |
| 4790 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
| 4791 | set_task_cpu(idle, cpu); |
| 4792 | |
| 4793 | spin_lock_irqsave(&rq->lock, flags); |
| 4794 | rq->curr = rq->idle = idle; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 4795 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
| 4796 | idle->oncpu = 1; |
| 4797 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | spin_unlock_irqrestore(&rq->lock, flags); |
| 4799 | |
| 4800 | /* Set the preempt count _outside_ the spinlocks! */ |
| 4801 | #if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL) |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 4802 | task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | #else |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 4804 | task_thread_info(idle)->preempt_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | #endif |
| 4806 | } |
| 4807 | |
| 4808 | /* |
| 4809 | * In a system that switches off the HZ timer nohz_cpu_mask |
| 4810 | * indicates which cpus entered this state. This is used |
| 4811 | * in the rcu update to wait only for active cpus. For system |
| 4812 | * which do not switch off the HZ timer nohz_cpu_mask should |
| 4813 | * always be CPU_MASK_NONE. |
| 4814 | */ |
| 4815 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; |
| 4816 | |
| 4817 | #ifdef CONFIG_SMP |
| 4818 | /* |
| 4819 | * This is how migration works: |
| 4820 | * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4821 | * 1) we queue a struct migration_req structure in the source CPU's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | * runqueue and wake up that CPU's migration thread. |
| 4823 | * 2) we down() the locked semaphore => thread blocks. |
| 4824 | * 3) migration thread wakes up (implicitly it forces the migrated |
| 4825 | * thread off the CPU) |
| 4826 | * 4) it gets the migration request and checks whether the migrated |
| 4827 | * task is still in the wrong runqueue. |
| 4828 | * 5) if it's in the wrong runqueue then the migration thread removes |
| 4829 | * it and puts it into the right queue. |
| 4830 | * 6) migration thread up()s the semaphore. |
| 4831 | * 7) we wake up and the migration is done. |
| 4832 | */ |
| 4833 | |
| 4834 | /* |
| 4835 | * Change a given task's CPU affinity. Migrate the thread to a |
| 4836 | * proper CPU and schedule it away if the CPU it's executing on |
| 4837 | * is removed from the allowed bitmask. |
| 4838 | * |
| 4839 | * NOTE: the caller must have a valid reference to the task, the |
| 4840 | * task must not exit() & deallocate itself prematurely. The |
| 4841 | * call is not atomic; no spinlocks may be held. |
| 4842 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4843 | int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4845 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4847 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4848 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | |
| 4850 | rq = task_rq_lock(p, &flags); |
| 4851 | if (!cpus_intersects(new_mask, cpu_online_map)) { |
| 4852 | ret = -EINVAL; |
| 4853 | goto out; |
| 4854 | } |
| 4855 | |
| 4856 | p->cpus_allowed = new_mask; |
| 4857 | /* Can the task run on the task's current CPU? If so, we're done */ |
| 4858 | if (cpu_isset(task_cpu(p), new_mask)) |
| 4859 | goto out; |
| 4860 | |
| 4861 | if (migrate_task(p, any_online_cpu(new_mask), &req)) { |
| 4862 | /* Need help from migration thread: drop lock and wait. */ |
| 4863 | task_rq_unlock(rq, &flags); |
| 4864 | wake_up_process(rq->migration_thread); |
| 4865 | wait_for_completion(&req.done); |
| 4866 | tlb_migrate_finish(p->mm); |
| 4867 | return 0; |
| 4868 | } |
| 4869 | out: |
| 4870 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | return ret; |
| 4873 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | EXPORT_SYMBOL_GPL(set_cpus_allowed); |
| 4875 | |
| 4876 | /* |
| 4877 | * Move (not current) task off this cpu, onto dest cpu. We're doing |
| 4878 | * this because either it can't run here any more (set_cpus_allowed() |
| 4879 | * away from this CPU, or CPU going down), or because we're |
| 4880 | * attempting to rebalance this task on exec (sched_exec). |
| 4881 | * |
| 4882 | * So we race with normal scheduler movements, but that's OK, as long |
| 4883 | * as the task is no longer on this CPU. |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4884 | * |
| 4885 | * Returns non-zero if task was successfully migrated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4886 | */ |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4887 | static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4889 | struct rq *rq_dest, *rq_src; |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4890 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | |
| 4892 | if (unlikely(cpu_is_offline(dest_cpu))) |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4893 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | |
| 4895 | rq_src = cpu_rq(src_cpu); |
| 4896 | rq_dest = cpu_rq(dest_cpu); |
| 4897 | |
| 4898 | double_rq_lock(rq_src, rq_dest); |
| 4899 | /* Already moved. */ |
| 4900 | if (task_cpu(p) != src_cpu) |
| 4901 | goto out; |
| 4902 | /* Affinity changed (again). */ |
| 4903 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) |
| 4904 | goto out; |
| 4905 | |
| 4906 | set_task_cpu(p, dest_cpu); |
| 4907 | if (p->array) { |
| 4908 | /* |
| 4909 | * Sync timestamp with rq_dest's before activating. |
| 4910 | * The same thing could be achieved by doing this step |
| 4911 | * afterwards, and pretending it was a local activate. |
| 4912 | * This way is cleaner and logically correct. |
| 4913 | */ |
| 4914 | p->timestamp = p->timestamp - rq_src->timestamp_last_tick |
| 4915 | + rq_dest->timestamp_last_tick; |
| 4916 | deactivate_task(p, rq_src); |
Peter Williams | 0a565f7 | 2006-07-10 04:43:51 -0700 | [diff] [blame] | 4917 | __activate_task(p, rq_dest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | if (TASK_PREEMPTS_CURR(p, rq_dest)) |
| 4919 | resched_task(rq_dest->curr); |
| 4920 | } |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4921 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | out: |
| 4923 | double_rq_unlock(rq_src, rq_dest); |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4924 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | } |
| 4926 | |
| 4927 | /* |
| 4928 | * migration_thread - this is a highprio system thread that performs |
| 4929 | * thread migration by bumping thread off CPU then 'pushing' onto |
| 4930 | * another runqueue. |
| 4931 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4932 | static int migration_thread(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | int cpu = (long)data; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4935 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | |
| 4937 | rq = cpu_rq(cpu); |
| 4938 | BUG_ON(rq->migration_thread != current); |
| 4939 | |
| 4940 | set_current_state(TASK_INTERRUPTIBLE); |
| 4941 | while (!kthread_should_stop()) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4942 | struct migration_req *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 4945 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | |
| 4947 | spin_lock_irq(&rq->lock); |
| 4948 | |
| 4949 | if (cpu_is_offline(cpu)) { |
| 4950 | spin_unlock_irq(&rq->lock); |
| 4951 | goto wait_to_die; |
| 4952 | } |
| 4953 | |
| 4954 | if (rq->active_balance) { |
| 4955 | active_load_balance(rq, cpu); |
| 4956 | rq->active_balance = 0; |
| 4957 | } |
| 4958 | |
| 4959 | head = &rq->migration_queue; |
| 4960 | |
| 4961 | if (list_empty(head)) { |
| 4962 | spin_unlock_irq(&rq->lock); |
| 4963 | schedule(); |
| 4964 | set_current_state(TASK_INTERRUPTIBLE); |
| 4965 | continue; |
| 4966 | } |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4967 | req = list_entry(head->next, struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | list_del_init(head->next); |
| 4969 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 4970 | spin_unlock(&rq->lock); |
| 4971 | __migrate_task(req->task, cpu, req->dest_cpu); |
| 4972 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | |
| 4974 | complete(&req->done); |
| 4975 | } |
| 4976 | __set_current_state(TASK_RUNNING); |
| 4977 | return 0; |
| 4978 | |
| 4979 | wait_to_die: |
| 4980 | /* Wait for kthread_stop */ |
| 4981 | set_current_state(TASK_INTERRUPTIBLE); |
| 4982 | while (!kthread_should_stop()) { |
| 4983 | schedule(); |
| 4984 | set_current_state(TASK_INTERRUPTIBLE); |
| 4985 | } |
| 4986 | __set_current_state(TASK_RUNNING); |
| 4987 | return 0; |
| 4988 | } |
| 4989 | |
| 4990 | #ifdef CONFIG_HOTPLUG_CPU |
| 4991 | /* Figure out where task on dead CPU should go, use force if neccessary. */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4992 | static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | { |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4994 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | cpumask_t mask; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4996 | struct rq *rq; |
| 4997 | int dest_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4999 | restart: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | /* On same node? */ |
| 5001 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5002 | cpus_and(mask, mask, p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5003 | dest_cpu = any_online_cpu(mask); |
| 5004 | |
| 5005 | /* On any allowed CPU? */ |
| 5006 | if (dest_cpu == NR_CPUS) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5007 | dest_cpu = any_online_cpu(p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | |
| 5009 | /* No more Mr. Nice Guy. */ |
| 5010 | if (dest_cpu == NR_CPUS) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5011 | rq = task_rq_lock(p, &flags); |
| 5012 | cpus_setall(p->cpus_allowed); |
| 5013 | dest_cpu = any_online_cpu(p->cpus_allowed); |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5014 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | |
| 5016 | /* |
| 5017 | * Don't tell them about moving exiting tasks or |
| 5018 | * kernel threads (both mm NULL), since they never |
| 5019 | * leave kernel. |
| 5020 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5021 | if (p->mm && printk_ratelimit()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | printk(KERN_INFO "process %d (%s) no " |
| 5023 | "longer affine to cpu%d\n", |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5024 | p->pid, p->comm, dead_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5026 | if (!__migrate_task(p, dead_cpu, dest_cpu)) |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5027 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | /* |
| 5031 | * While a dead CPU has no uninterruptible tasks queued at this point, |
| 5032 | * it might still have a nonzero ->nr_uninterruptible counter, because |
| 5033 | * for performance reasons the counter is not stricly tracking tasks to |
| 5034 | * their home CPUs. So we just add the counter to another CPU's counter, |
| 5035 | * to keep the global sum constant after CPU-down: |
| 5036 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5037 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5039 | struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | unsigned long flags; |
| 5041 | |
| 5042 | local_irq_save(flags); |
| 5043 | double_rq_lock(rq_src, rq_dest); |
| 5044 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; |
| 5045 | rq_src->nr_uninterruptible = 0; |
| 5046 | double_rq_unlock(rq_src, rq_dest); |
| 5047 | local_irq_restore(flags); |
| 5048 | } |
| 5049 | |
| 5050 | /* Run through task list and migrate tasks from the dead cpu. */ |
| 5051 | static void migrate_live_tasks(int src_cpu) |
| 5052 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5053 | struct task_struct *p, *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | |
| 5055 | write_lock_irq(&tasklist_lock); |
| 5056 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5057 | do_each_thread(t, p) { |
| 5058 | if (p == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | continue; |
| 5060 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5061 | if (task_cpu(p) == src_cpu) |
| 5062 | move_task_off_dead_cpu(src_cpu, p); |
| 5063 | } while_each_thread(t, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | |
| 5065 | write_unlock_irq(&tasklist_lock); |
| 5066 | } |
| 5067 | |
| 5068 | /* Schedules idle task to be the next runnable task on current CPU. |
| 5069 | * It does so by boosting its priority to highest possible and adding it to |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5070 | * the _front_ of the runqueue. Used by CPU offline code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | */ |
| 5072 | void sched_idle_next(void) |
| 5073 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5074 | int this_cpu = smp_processor_id(); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5075 | struct rq *rq = cpu_rq(this_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | struct task_struct *p = rq->idle; |
| 5077 | unsigned long flags; |
| 5078 | |
| 5079 | /* cpu has to be offline */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5080 | BUG_ON(cpu_online(this_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5081 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5082 | /* |
| 5083 | * Strictly not necessary since rest of the CPUs are stopped by now |
| 5084 | * and interrupts disabled on the current cpu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | */ |
| 5086 | spin_lock_irqsave(&rq->lock, flags); |
| 5087 | |
| 5088 | __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5089 | |
| 5090 | /* Add idle task to the _front_ of its priority queue: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | __activate_idle_task(p, rq); |
| 5092 | |
| 5093 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5094 | } |
| 5095 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5096 | /* |
| 5097 | * Ensures that the idle task is using init_mm right before its cpu goes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | * offline. |
| 5099 | */ |
| 5100 | void idle_task_exit(void) |
| 5101 | { |
| 5102 | struct mm_struct *mm = current->active_mm; |
| 5103 | |
| 5104 | BUG_ON(cpu_online(smp_processor_id())); |
| 5105 | |
| 5106 | if (mm != &init_mm) |
| 5107 | switch_mm(mm, &init_mm, current); |
| 5108 | mmdrop(mm); |
| 5109 | } |
| 5110 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5111 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5113 | struct rq *rq = cpu_rq(dead_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | |
| 5115 | /* Must be exiting, otherwise would be on tasklist. */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5116 | BUG_ON(p->exit_state != EXIT_ZOMBIE && p->exit_state != EXIT_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | |
| 5118 | /* Cannot have done final schedule yet: would have vanished. */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5119 | BUG_ON(p->flags & PF_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5121 | get_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | |
| 5123 | /* |
| 5124 | * Drop lock around migration; if someone else moves it, |
| 5125 | * that's OK. No task can be added to this CPU, so iteration is |
| 5126 | * fine. |
| 5127 | */ |
| 5128 | spin_unlock_irq(&rq->lock); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5129 | move_task_off_dead_cpu(dead_cpu, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | spin_lock_irq(&rq->lock); |
| 5131 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5132 | put_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ |
| 5136 | static void migrate_dead_tasks(unsigned int dead_cpu) |
| 5137 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5138 | struct rq *rq = cpu_rq(dead_cpu); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5139 | unsigned int arr, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | |
| 5141 | for (arr = 0; arr < 2; arr++) { |
| 5142 | for (i = 0; i < MAX_PRIO; i++) { |
| 5143 | struct list_head *list = &rq->arrays[arr].queue[i]; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | while (!list_empty(list)) |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5146 | migrate_dead(dead_cpu, list_entry(list->next, |
| 5147 | struct task_struct, run_list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | } |
| 5149 | } |
| 5150 | } |
| 5151 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 5152 | |
| 5153 | /* |
| 5154 | * migration_call - callback that gets triggered when a CPU is added. |
| 5155 | * Here we can start up the necessary migration thread for the new CPU. |
| 5156 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5157 | static int __cpuinit |
| 5158 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | struct task_struct *p; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5161 | int cpu = (long)hcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5163 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | |
| 5165 | switch (action) { |
| 5166 | case CPU_UP_PREPARE: |
| 5167 | p = kthread_create(migration_thread, hcpu, "migration/%d",cpu); |
| 5168 | if (IS_ERR(p)) |
| 5169 | return NOTIFY_BAD; |
| 5170 | p->flags |= PF_NOFREEZE; |
| 5171 | kthread_bind(p, cpu); |
| 5172 | /* Must be high prio: stop_machine expects to yield to it. */ |
| 5173 | rq = task_rq_lock(p, &flags); |
| 5174 | __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1); |
| 5175 | task_rq_unlock(rq, &flags); |
| 5176 | cpu_rq(cpu)->migration_thread = p; |
| 5177 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | case CPU_ONLINE: |
| 5180 | /* Strictly unneccessary, as first user will wake it. */ |
| 5181 | wake_up_process(cpu_rq(cpu)->migration_thread); |
| 5182 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | #ifdef CONFIG_HOTPLUG_CPU |
| 5185 | case CPU_UP_CANCELED: |
Heiko Carstens | fc75cdf | 2006-06-25 05:49:10 -0700 | [diff] [blame] | 5186 | if (!cpu_rq(cpu)->migration_thread) |
| 5187 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
Heiko Carstens | a4c4af7 | 2005-11-07 00:58:38 -0800 | [diff] [blame] | 5189 | kthread_bind(cpu_rq(cpu)->migration_thread, |
| 5190 | any_online_cpu(cpu_online_map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | kthread_stop(cpu_rq(cpu)->migration_thread); |
| 5192 | cpu_rq(cpu)->migration_thread = NULL; |
| 5193 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | case CPU_DEAD: |
| 5196 | migrate_live_tasks(cpu); |
| 5197 | rq = cpu_rq(cpu); |
| 5198 | kthread_stop(rq->migration_thread); |
| 5199 | rq->migration_thread = NULL; |
| 5200 | /* Idle task back to normal (off runqueue, low prio) */ |
| 5201 | rq = task_rq_lock(rq->idle, &flags); |
| 5202 | deactivate_task(rq->idle, rq); |
| 5203 | rq->idle->static_prio = MAX_PRIO; |
| 5204 | __setscheduler(rq->idle, SCHED_NORMAL, 0); |
| 5205 | migrate_dead_tasks(cpu); |
| 5206 | task_rq_unlock(rq, &flags); |
| 5207 | migrate_nr_uninterruptible(rq); |
| 5208 | BUG_ON(rq->nr_running != 0); |
| 5209 | |
| 5210 | /* No need to migrate the tasks: it was best-effort if |
| 5211 | * they didn't do lock_cpu_hotplug(). Just wake up |
| 5212 | * the requestors. */ |
| 5213 | spin_lock_irq(&rq->lock); |
| 5214 | while (!list_empty(&rq->migration_queue)) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5215 | struct migration_req *req; |
| 5216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | req = list_entry(rq->migration_queue.next, |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5218 | struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | list_del_init(&req->list); |
| 5220 | complete(&req->done); |
| 5221 | } |
| 5222 | spin_unlock_irq(&rq->lock); |
| 5223 | break; |
| 5224 | #endif |
| 5225 | } |
| 5226 | return NOTIFY_OK; |
| 5227 | } |
| 5228 | |
| 5229 | /* Register at highest priority so that task migration (migrate_all_tasks) |
| 5230 | * happens before everything else. |
| 5231 | */ |
Chandra Seetharaman | 26c2143 | 2006-06-27 02:54:10 -0700 | [diff] [blame] | 5232 | static struct notifier_block __cpuinitdata migration_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | .notifier_call = migration_call, |
| 5234 | .priority = 10 |
| 5235 | }; |
| 5236 | |
| 5237 | int __init migration_init(void) |
| 5238 | { |
| 5239 | void *cpu = (void *)(long)smp_processor_id(); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5240 | |
| 5241 | /* Start one for the boot CPU: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
| 5243 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
| 5244 | register_cpu_notifier(&migration_notifier); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | return 0; |
| 5247 | } |
| 5248 | #endif |
| 5249 | |
| 5250 | #ifdef CONFIG_SMP |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5251 | #undef SCHED_DOMAIN_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | #ifdef SCHED_DOMAIN_DEBUG |
| 5253 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
| 5254 | { |
| 5255 | int level = 0; |
| 5256 | |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 5257 | if (!sd) { |
| 5258 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); |
| 5259 | return; |
| 5260 | } |
| 5261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5262 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
| 5263 | |
| 5264 | do { |
| 5265 | int i; |
| 5266 | char str[NR_CPUS]; |
| 5267 | struct sched_group *group = sd->groups; |
| 5268 | cpumask_t groupmask; |
| 5269 | |
| 5270 | cpumask_scnprintf(str, NR_CPUS, sd->span); |
| 5271 | cpus_clear(groupmask); |
| 5272 | |
| 5273 | printk(KERN_DEBUG); |
| 5274 | for (i = 0; i < level + 1; i++) |
| 5275 | printk(" "); |
| 5276 | printk("domain %d: ", level); |
| 5277 | |
| 5278 | if (!(sd->flags & SD_LOAD_BALANCE)) { |
| 5279 | printk("does not load-balance\n"); |
| 5280 | if (sd->parent) |
| 5281 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain has parent"); |
| 5282 | break; |
| 5283 | } |
| 5284 | |
| 5285 | printk("span %s\n", str); |
| 5286 | |
| 5287 | if (!cpu_isset(cpu, sd->span)) |
| 5288 | printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu); |
| 5289 | if (!cpu_isset(cpu, group->cpumask)) |
| 5290 | printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu); |
| 5291 | |
| 5292 | printk(KERN_DEBUG); |
| 5293 | for (i = 0; i < level + 2; i++) |
| 5294 | printk(" "); |
| 5295 | printk("groups:"); |
| 5296 | do { |
| 5297 | if (!group) { |
| 5298 | printk("\n"); |
| 5299 | printk(KERN_ERR "ERROR: group is NULL\n"); |
| 5300 | break; |
| 5301 | } |
| 5302 | |
| 5303 | if (!group->cpu_power) { |
| 5304 | printk("\n"); |
| 5305 | printk(KERN_ERR "ERROR: domain->cpu_power not set\n"); |
| 5306 | } |
| 5307 | |
| 5308 | if (!cpus_weight(group->cpumask)) { |
| 5309 | printk("\n"); |
| 5310 | printk(KERN_ERR "ERROR: empty group\n"); |
| 5311 | } |
| 5312 | |
| 5313 | if (cpus_intersects(groupmask, group->cpumask)) { |
| 5314 | printk("\n"); |
| 5315 | printk(KERN_ERR "ERROR: repeated CPUs\n"); |
| 5316 | } |
| 5317 | |
| 5318 | cpus_or(groupmask, groupmask, group->cpumask); |
| 5319 | |
| 5320 | cpumask_scnprintf(str, NR_CPUS, group->cpumask); |
| 5321 | printk(" %s", str); |
| 5322 | |
| 5323 | group = group->next; |
| 5324 | } while (group != sd->groups); |
| 5325 | printk("\n"); |
| 5326 | |
| 5327 | if (!cpus_equal(sd->span, groupmask)) |
| 5328 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
| 5329 | |
| 5330 | level++; |
| 5331 | sd = sd->parent; |
| 5332 | |
| 5333 | if (sd) { |
| 5334 | if (!cpus_subset(groupmask, sd->span)) |
| 5335 | printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n"); |
| 5336 | } |
| 5337 | |
| 5338 | } while (sd); |
| 5339 | } |
| 5340 | #else |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5341 | # define sched_domain_debug(sd, cpu) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | #endif |
| 5343 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5344 | static int sd_degenerate(struct sched_domain *sd) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5345 | { |
| 5346 | if (cpus_weight(sd->span) == 1) |
| 5347 | return 1; |
| 5348 | |
| 5349 | /* Following flags need at least 2 groups */ |
| 5350 | if (sd->flags & (SD_LOAD_BALANCE | |
| 5351 | SD_BALANCE_NEWIDLE | |
| 5352 | SD_BALANCE_FORK | |
| 5353 | SD_BALANCE_EXEC)) { |
| 5354 | if (sd->groups != sd->groups->next) |
| 5355 | return 0; |
| 5356 | } |
| 5357 | |
| 5358 | /* Following flags don't use groups */ |
| 5359 | if (sd->flags & (SD_WAKE_IDLE | |
| 5360 | SD_WAKE_AFFINE | |
| 5361 | SD_WAKE_BALANCE)) |
| 5362 | return 0; |
| 5363 | |
| 5364 | return 1; |
| 5365 | } |
| 5366 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5367 | static int |
| 5368 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5369 | { |
| 5370 | unsigned long cflags = sd->flags, pflags = parent->flags; |
| 5371 | |
| 5372 | if (sd_degenerate(parent)) |
| 5373 | return 1; |
| 5374 | |
| 5375 | if (!cpus_equal(sd->span, parent->span)) |
| 5376 | return 0; |
| 5377 | |
| 5378 | /* Does parent contain flags not in child? */ |
| 5379 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ |
| 5380 | if (cflags & SD_WAKE_AFFINE) |
| 5381 | pflags &= ~SD_WAKE_BALANCE; |
| 5382 | /* Flags needing groups don't count if only 1 group in parent */ |
| 5383 | if (parent->groups == parent->groups->next) { |
| 5384 | pflags &= ~(SD_LOAD_BALANCE | |
| 5385 | SD_BALANCE_NEWIDLE | |
| 5386 | SD_BALANCE_FORK | |
| 5387 | SD_BALANCE_EXEC); |
| 5388 | } |
| 5389 | if (~cflags & pflags) |
| 5390 | return 0; |
| 5391 | |
| 5392 | return 1; |
| 5393 | } |
| 5394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | /* |
| 5396 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
| 5397 | * hold the hotplug lock. |
| 5398 | */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5399 | static void cpu_attach_domain(struct sched_domain *sd, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5401 | struct rq *rq = cpu_rq(cpu); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5402 | struct sched_domain *tmp; |
| 5403 | |
| 5404 | /* Remove the sched domains which do not contribute to scheduling. */ |
| 5405 | for (tmp = sd; tmp; tmp = tmp->parent) { |
| 5406 | struct sched_domain *parent = tmp->parent; |
| 5407 | if (!parent) |
| 5408 | break; |
| 5409 | if (sd_parent_degenerate(tmp, parent)) |
| 5410 | tmp->parent = parent->parent; |
| 5411 | } |
| 5412 | |
| 5413 | if (sd && sd_degenerate(sd)) |
| 5414 | sd = sd->parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | |
| 5416 | sched_domain_debug(sd, cpu); |
| 5417 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 5418 | rcu_assign_pointer(rq->sd, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | /* cpus with isolated domains */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5422 | static cpumask_t __devinitdata cpu_isolated_map = CPU_MASK_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | |
| 5424 | /* Setup the mask of cpus configured for isolated domains */ |
| 5425 | static int __init isolated_cpu_setup(char *str) |
| 5426 | { |
| 5427 | int ints[NR_CPUS], i; |
| 5428 | |
| 5429 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 5430 | cpus_clear(cpu_isolated_map); |
| 5431 | for (i = 1; i <= ints[0]; i++) |
| 5432 | if (ints[i] < NR_CPUS) |
| 5433 | cpu_set(ints[i], cpu_isolated_map); |
| 5434 | return 1; |
| 5435 | } |
| 5436 | |
| 5437 | __setup ("isolcpus=", isolated_cpu_setup); |
| 5438 | |
| 5439 | /* |
| 5440 | * init_sched_build_groups takes an array of groups, the cpumask we wish |
| 5441 | * to span, and a pointer to a function which identifies what group a CPU |
| 5442 | * belongs to. The return value of group_fn must be a valid index into the |
| 5443 | * groups[] array, and must be >= 0 and < NR_CPUS (due to the fact that we |
| 5444 | * keep track of groups covered with a cpumask_t). |
| 5445 | * |
| 5446 | * init_sched_build_groups will build a circular linked list of the groups |
| 5447 | * covered by the given span, and will set each group's ->cpumask correctly, |
| 5448 | * and ->cpu_power to 0. |
| 5449 | */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5450 | static void init_sched_build_groups(struct sched_group groups[], cpumask_t span, |
| 5451 | int (*group_fn)(int cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | { |
| 5453 | struct sched_group *first = NULL, *last = NULL; |
| 5454 | cpumask_t covered = CPU_MASK_NONE; |
| 5455 | int i; |
| 5456 | |
| 5457 | for_each_cpu_mask(i, span) { |
| 5458 | int group = group_fn(i); |
| 5459 | struct sched_group *sg = &groups[group]; |
| 5460 | int j; |
| 5461 | |
| 5462 | if (cpu_isset(i, covered)) |
| 5463 | continue; |
| 5464 | |
| 5465 | sg->cpumask = CPU_MASK_NONE; |
| 5466 | sg->cpu_power = 0; |
| 5467 | |
| 5468 | for_each_cpu_mask(j, span) { |
| 5469 | if (group_fn(j) != group) |
| 5470 | continue; |
| 5471 | |
| 5472 | cpu_set(j, covered); |
| 5473 | cpu_set(j, sg->cpumask); |
| 5474 | } |
| 5475 | if (!first) |
| 5476 | first = sg; |
| 5477 | if (last) |
| 5478 | last->next = sg; |
| 5479 | last = sg; |
| 5480 | } |
| 5481 | last->next = first; |
| 5482 | } |
| 5483 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5484 | #define SD_NODES_PER_DOMAIN 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5486 | /* |
| 5487 | * Self-tuning task migration cost measurement between source and target CPUs. |
| 5488 | * |
| 5489 | * This is done by measuring the cost of manipulating buffers of varying |
| 5490 | * sizes. For a given buffer-size here are the steps that are taken: |
| 5491 | * |
| 5492 | * 1) the source CPU reads+dirties a shared buffer |
| 5493 | * 2) the target CPU reads+dirties the same shared buffer |
| 5494 | * |
| 5495 | * We measure how long they take, in the following 4 scenarios: |
| 5496 | * |
| 5497 | * - source: CPU1, target: CPU2 | cost1 |
| 5498 | * - source: CPU2, target: CPU1 | cost2 |
| 5499 | * - source: CPU1, target: CPU1 | cost3 |
| 5500 | * - source: CPU2, target: CPU2 | cost4 |
| 5501 | * |
| 5502 | * We then calculate the cost3+cost4-cost1-cost2 difference - this is |
| 5503 | * the cost of migration. |
| 5504 | * |
| 5505 | * We then start off from a small buffer-size and iterate up to larger |
| 5506 | * buffer sizes, in 5% steps - measuring each buffer-size separately, and |
| 5507 | * doing a maximum search for the cost. (The maximum cost for a migration |
| 5508 | * normally occurs when the working set size is around the effective cache |
| 5509 | * size.) |
| 5510 | */ |
| 5511 | #define SEARCH_SCOPE 2 |
| 5512 | #define MIN_CACHE_SIZE (64*1024U) |
| 5513 | #define DEFAULT_CACHE_SIZE (5*1024*1024U) |
Ingo Molnar | 70b4d63 | 2006-01-30 20:24:38 +0100 | [diff] [blame] | 5514 | #define ITERATIONS 1 |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5515 | #define SIZE_THRESH 130 |
| 5516 | #define COST_THRESH 130 |
| 5517 | |
| 5518 | /* |
| 5519 | * The migration cost is a function of 'domain distance'. Domain |
| 5520 | * distance is the number of steps a CPU has to iterate down its |
| 5521 | * domain tree to share a domain with the other CPU. The farther |
| 5522 | * two CPUs are from each other, the larger the distance gets. |
| 5523 | * |
| 5524 | * Note that we use the distance only to cache measurement results, |
| 5525 | * the distance value is not used numerically otherwise. When two |
| 5526 | * CPUs have the same distance it is assumed that the migration |
| 5527 | * cost is the same. (this is a simplification but quite practical) |
| 5528 | */ |
| 5529 | #define MAX_DOMAIN_DISTANCE 32 |
| 5530 | |
| 5531 | static unsigned long long migration_cost[MAX_DOMAIN_DISTANCE] = |
Ingo Molnar | 4bbf39c | 2006-02-17 13:52:44 -0800 | [diff] [blame] | 5532 | { [ 0 ... MAX_DOMAIN_DISTANCE-1 ] = |
| 5533 | /* |
| 5534 | * Architectures may override the migration cost and thus avoid |
| 5535 | * boot-time calibration. Unit is nanoseconds. Mostly useful for |
| 5536 | * virtualized hardware: |
| 5537 | */ |
| 5538 | #ifdef CONFIG_DEFAULT_MIGRATION_COST |
| 5539 | CONFIG_DEFAULT_MIGRATION_COST |
| 5540 | #else |
| 5541 | -1LL |
| 5542 | #endif |
| 5543 | }; |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5544 | |
| 5545 | /* |
| 5546 | * Allow override of migration cost - in units of microseconds. |
| 5547 | * E.g. migration_cost=1000,2000,3000 will set up a level-1 cost |
| 5548 | * of 1 msec, level-2 cost of 2 msecs and level3 cost of 3 msecs: |
| 5549 | */ |
| 5550 | static int __init migration_cost_setup(char *str) |
| 5551 | { |
| 5552 | int ints[MAX_DOMAIN_DISTANCE+1], i; |
| 5553 | |
| 5554 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 5555 | |
| 5556 | printk("#ints: %d\n", ints[0]); |
| 5557 | for (i = 1; i <= ints[0]; i++) { |
| 5558 | migration_cost[i-1] = (unsigned long long)ints[i]*1000; |
| 5559 | printk("migration_cost[%d]: %Ld\n", i-1, migration_cost[i-1]); |
| 5560 | } |
| 5561 | return 1; |
| 5562 | } |
| 5563 | |
| 5564 | __setup ("migration_cost=", migration_cost_setup); |
| 5565 | |
| 5566 | /* |
| 5567 | * Global multiplier (divisor) for migration-cutoff values, |
| 5568 | * in percentiles. E.g. use a value of 150 to get 1.5 times |
| 5569 | * longer cache-hot cutoff times. |
| 5570 | * |
| 5571 | * (We scale it from 100 to 128 to long long handling easier.) |
| 5572 | */ |
| 5573 | |
| 5574 | #define MIGRATION_FACTOR_SCALE 128 |
| 5575 | |
| 5576 | static unsigned int migration_factor = MIGRATION_FACTOR_SCALE; |
| 5577 | |
| 5578 | static int __init setup_migration_factor(char *str) |
| 5579 | { |
| 5580 | get_option(&str, &migration_factor); |
| 5581 | migration_factor = migration_factor * MIGRATION_FACTOR_SCALE / 100; |
| 5582 | return 1; |
| 5583 | } |
| 5584 | |
| 5585 | __setup("migration_factor=", setup_migration_factor); |
| 5586 | |
| 5587 | /* |
| 5588 | * Estimated distance of two CPUs, measured via the number of domains |
| 5589 | * we have to pass for the two CPUs to be in the same span: |
| 5590 | */ |
| 5591 | static unsigned long domain_distance(int cpu1, int cpu2) |
| 5592 | { |
| 5593 | unsigned long distance = 0; |
| 5594 | struct sched_domain *sd; |
| 5595 | |
| 5596 | for_each_domain(cpu1, sd) { |
| 5597 | WARN_ON(!cpu_isset(cpu1, sd->span)); |
| 5598 | if (cpu_isset(cpu2, sd->span)) |
| 5599 | return distance; |
| 5600 | distance++; |
| 5601 | } |
| 5602 | if (distance >= MAX_DOMAIN_DISTANCE) { |
| 5603 | WARN_ON(1); |
| 5604 | distance = MAX_DOMAIN_DISTANCE-1; |
| 5605 | } |
| 5606 | |
| 5607 | return distance; |
| 5608 | } |
| 5609 | |
| 5610 | static unsigned int migration_debug; |
| 5611 | |
| 5612 | static int __init setup_migration_debug(char *str) |
| 5613 | { |
| 5614 | get_option(&str, &migration_debug); |
| 5615 | return 1; |
| 5616 | } |
| 5617 | |
| 5618 | __setup("migration_debug=", setup_migration_debug); |
| 5619 | |
| 5620 | /* |
| 5621 | * Maximum cache-size that the scheduler should try to measure. |
| 5622 | * Architectures with larger caches should tune this up during |
| 5623 | * bootup. Gets used in the domain-setup code (i.e. during SMP |
| 5624 | * bootup). |
| 5625 | */ |
| 5626 | unsigned int max_cache_size; |
| 5627 | |
| 5628 | static int __init setup_max_cache_size(char *str) |
| 5629 | { |
| 5630 | get_option(&str, &max_cache_size); |
| 5631 | return 1; |
| 5632 | } |
| 5633 | |
| 5634 | __setup("max_cache_size=", setup_max_cache_size); |
| 5635 | |
| 5636 | /* |
| 5637 | * Dirty a big buffer in a hard-to-predict (for the L2 cache) way. This |
| 5638 | * is the operation that is timed, so we try to generate unpredictable |
| 5639 | * cachemisses that still end up filling the L2 cache: |
| 5640 | */ |
| 5641 | static void touch_cache(void *__cache, unsigned long __size) |
| 5642 | { |
| 5643 | unsigned long size = __size/sizeof(long), chunk1 = size/3, |
| 5644 | chunk2 = 2*size/3; |
| 5645 | unsigned long *cache = __cache; |
| 5646 | int i; |
| 5647 | |
| 5648 | for (i = 0; i < size/6; i += 8) { |
| 5649 | switch (i % 6) { |
| 5650 | case 0: cache[i]++; |
| 5651 | case 1: cache[size-1-i]++; |
| 5652 | case 2: cache[chunk1-i]++; |
| 5653 | case 3: cache[chunk1+i]++; |
| 5654 | case 4: cache[chunk2-i]++; |
| 5655 | case 5: cache[chunk2+i]++; |
| 5656 | } |
| 5657 | } |
| 5658 | } |
| 5659 | |
| 5660 | /* |
| 5661 | * Measure the cache-cost of one task migration. Returns in units of nsec. |
| 5662 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5663 | static unsigned long long |
| 5664 | measure_one(void *cache, unsigned long size, int source, int target) |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5665 | { |
| 5666 | cpumask_t mask, saved_mask; |
| 5667 | unsigned long long t0, t1, t2, t3, cost; |
| 5668 | |
| 5669 | saved_mask = current->cpus_allowed; |
| 5670 | |
| 5671 | /* |
| 5672 | * Flush source caches to RAM and invalidate them: |
| 5673 | */ |
| 5674 | sched_cacheflush(); |
| 5675 | |
| 5676 | /* |
| 5677 | * Migrate to the source CPU: |
| 5678 | */ |
| 5679 | mask = cpumask_of_cpu(source); |
| 5680 | set_cpus_allowed(current, mask); |
| 5681 | WARN_ON(smp_processor_id() != source); |
| 5682 | |
| 5683 | /* |
| 5684 | * Dirty the working set: |
| 5685 | */ |
| 5686 | t0 = sched_clock(); |
| 5687 | touch_cache(cache, size); |
| 5688 | t1 = sched_clock(); |
| 5689 | |
| 5690 | /* |
| 5691 | * Migrate to the target CPU, dirty the L2 cache and access |
| 5692 | * the shared buffer. (which represents the working set |
| 5693 | * of a migrated task.) |
| 5694 | */ |
| 5695 | mask = cpumask_of_cpu(target); |
| 5696 | set_cpus_allowed(current, mask); |
| 5697 | WARN_ON(smp_processor_id() != target); |
| 5698 | |
| 5699 | t2 = sched_clock(); |
| 5700 | touch_cache(cache, size); |
| 5701 | t3 = sched_clock(); |
| 5702 | |
| 5703 | cost = t1-t0 + t3-t2; |
| 5704 | |
| 5705 | if (migration_debug >= 2) |
| 5706 | printk("[%d->%d]: %8Ld %8Ld %8Ld => %10Ld.\n", |
| 5707 | source, target, t1-t0, t1-t0, t3-t2, cost); |
| 5708 | /* |
| 5709 | * Flush target caches to RAM and invalidate them: |
| 5710 | */ |
| 5711 | sched_cacheflush(); |
| 5712 | |
| 5713 | set_cpus_allowed(current, saved_mask); |
| 5714 | |
| 5715 | return cost; |
| 5716 | } |
| 5717 | |
| 5718 | /* |
| 5719 | * Measure a series of task migrations and return the average |
| 5720 | * result. Since this code runs early during bootup the system |
| 5721 | * is 'undisturbed' and the average latency makes sense. |
| 5722 | * |
| 5723 | * The algorithm in essence auto-detects the relevant cache-size, |
| 5724 | * so it will properly detect different cachesizes for different |
| 5725 | * cache-hierarchies, depending on how the CPUs are connected. |
| 5726 | * |
| 5727 | * Architectures can prime the upper limit of the search range via |
| 5728 | * max_cache_size, otherwise the search range defaults to 20MB...64K. |
| 5729 | */ |
| 5730 | static unsigned long long |
| 5731 | measure_cost(int cpu1, int cpu2, void *cache, unsigned int size) |
| 5732 | { |
| 5733 | unsigned long long cost1, cost2; |
| 5734 | int i; |
| 5735 | |
| 5736 | /* |
| 5737 | * Measure the migration cost of 'size' bytes, over an |
| 5738 | * average of 10 runs: |
| 5739 | * |
| 5740 | * (We perturb the cache size by a small (0..4k) |
| 5741 | * value to compensate size/alignment related artifacts. |
| 5742 | * We also subtract the cost of the operation done on |
| 5743 | * the same CPU.) |
| 5744 | */ |
| 5745 | cost1 = 0; |
| 5746 | |
| 5747 | /* |
| 5748 | * dry run, to make sure we start off cache-cold on cpu1, |
| 5749 | * and to get any vmalloc pagefaults in advance: |
| 5750 | */ |
| 5751 | measure_one(cache, size, cpu1, cpu2); |
| 5752 | for (i = 0; i < ITERATIONS; i++) |
| 5753 | cost1 += measure_one(cache, size - i*1024, cpu1, cpu2); |
| 5754 | |
| 5755 | measure_one(cache, size, cpu2, cpu1); |
| 5756 | for (i = 0; i < ITERATIONS; i++) |
| 5757 | cost1 += measure_one(cache, size - i*1024, cpu2, cpu1); |
| 5758 | |
| 5759 | /* |
| 5760 | * (We measure the non-migrating [cached] cost on both |
| 5761 | * cpu1 and cpu2, to handle CPUs with different speeds) |
| 5762 | */ |
| 5763 | cost2 = 0; |
| 5764 | |
| 5765 | measure_one(cache, size, cpu1, cpu1); |
| 5766 | for (i = 0; i < ITERATIONS; i++) |
| 5767 | cost2 += measure_one(cache, size - i*1024, cpu1, cpu1); |
| 5768 | |
| 5769 | measure_one(cache, size, cpu2, cpu2); |
| 5770 | for (i = 0; i < ITERATIONS; i++) |
| 5771 | cost2 += measure_one(cache, size - i*1024, cpu2, cpu2); |
| 5772 | |
| 5773 | /* |
| 5774 | * Get the per-iteration migration cost: |
| 5775 | */ |
| 5776 | do_div(cost1, 2*ITERATIONS); |
| 5777 | do_div(cost2, 2*ITERATIONS); |
| 5778 | |
| 5779 | return cost1 - cost2; |
| 5780 | } |
| 5781 | |
| 5782 | static unsigned long long measure_migration_cost(int cpu1, int cpu2) |
| 5783 | { |
| 5784 | unsigned long long max_cost = 0, fluct = 0, avg_fluct = 0; |
| 5785 | unsigned int max_size, size, size_found = 0; |
| 5786 | long long cost = 0, prev_cost; |
| 5787 | void *cache; |
| 5788 | |
| 5789 | /* |
| 5790 | * Search from max_cache_size*5 down to 64K - the real relevant |
| 5791 | * cachesize has to lie somewhere inbetween. |
| 5792 | */ |
| 5793 | if (max_cache_size) { |
| 5794 | max_size = max(max_cache_size * SEARCH_SCOPE, MIN_CACHE_SIZE); |
| 5795 | size = max(max_cache_size / SEARCH_SCOPE, MIN_CACHE_SIZE); |
| 5796 | } else { |
| 5797 | /* |
| 5798 | * Since we have no estimation about the relevant |
| 5799 | * search range |
| 5800 | */ |
| 5801 | max_size = DEFAULT_CACHE_SIZE * SEARCH_SCOPE; |
| 5802 | size = MIN_CACHE_SIZE; |
| 5803 | } |
| 5804 | |
| 5805 | if (!cpu_online(cpu1) || !cpu_online(cpu2)) { |
| 5806 | printk("cpu %d and %d not both online!\n", cpu1, cpu2); |
| 5807 | return 0; |
| 5808 | } |
| 5809 | |
| 5810 | /* |
| 5811 | * Allocate the working set: |
| 5812 | */ |
| 5813 | cache = vmalloc(max_size); |
| 5814 | if (!cache) { |
| 5815 | printk("could not vmalloc %d bytes for cache!\n", 2*max_size); |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 5816 | return 1000000; /* return 1 msec on very small boxen */ |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5817 | } |
| 5818 | |
| 5819 | while (size <= max_size) { |
| 5820 | prev_cost = cost; |
| 5821 | cost = measure_cost(cpu1, cpu2, cache, size); |
| 5822 | |
| 5823 | /* |
| 5824 | * Update the max: |
| 5825 | */ |
| 5826 | if (cost > 0) { |
| 5827 | if (max_cost < cost) { |
| 5828 | max_cost = cost; |
| 5829 | size_found = size; |
| 5830 | } |
| 5831 | } |
| 5832 | /* |
| 5833 | * Calculate average fluctuation, we use this to prevent |
| 5834 | * noise from triggering an early break out of the loop: |
| 5835 | */ |
| 5836 | fluct = abs(cost - prev_cost); |
| 5837 | avg_fluct = (avg_fluct + fluct)/2; |
| 5838 | |
| 5839 | if (migration_debug) |
| 5840 | printk("-> [%d][%d][%7d] %3ld.%ld [%3ld.%ld] (%ld): (%8Ld %8Ld)\n", |
| 5841 | cpu1, cpu2, size, |
| 5842 | (long)cost / 1000000, |
| 5843 | ((long)cost / 100000) % 10, |
| 5844 | (long)max_cost / 1000000, |
| 5845 | ((long)max_cost / 100000) % 10, |
| 5846 | domain_distance(cpu1, cpu2), |
| 5847 | cost, avg_fluct); |
| 5848 | |
| 5849 | /* |
| 5850 | * If we iterated at least 20% past the previous maximum, |
| 5851 | * and the cost has dropped by more than 20% already, |
| 5852 | * (taking fluctuations into account) then we assume to |
| 5853 | * have found the maximum and break out of the loop early: |
| 5854 | */ |
| 5855 | if (size_found && (size*100 > size_found*SIZE_THRESH)) |
| 5856 | if (cost+avg_fluct <= 0 || |
| 5857 | max_cost*100 > (cost+avg_fluct)*COST_THRESH) { |
| 5858 | |
| 5859 | if (migration_debug) |
| 5860 | printk("-> found max.\n"); |
| 5861 | break; |
| 5862 | } |
| 5863 | /* |
Ingo Molnar | 70b4d63 | 2006-01-30 20:24:38 +0100 | [diff] [blame] | 5864 | * Increase the cachesize in 10% steps: |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5865 | */ |
Ingo Molnar | 70b4d63 | 2006-01-30 20:24:38 +0100 | [diff] [blame] | 5866 | size = size * 10 / 9; |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | if (migration_debug) |
| 5870 | printk("[%d][%d] working set size found: %d, cost: %Ld\n", |
| 5871 | cpu1, cpu2, size_found, max_cost); |
| 5872 | |
| 5873 | vfree(cache); |
| 5874 | |
| 5875 | /* |
| 5876 | * A task is considered 'cache cold' if at least 2 times |
| 5877 | * the worst-case cost of migration has passed. |
| 5878 | * |
| 5879 | * (this limit is only listened to if the load-balancing |
| 5880 | * situation is 'nice' - if there is a large imbalance we |
| 5881 | * ignore it for the sake of CPU utilization and |
| 5882 | * processing fairness.) |
| 5883 | */ |
| 5884 | return 2 * max_cost * migration_factor / MIGRATION_FACTOR_SCALE; |
| 5885 | } |
| 5886 | |
| 5887 | static void calibrate_migration_costs(const cpumask_t *cpu_map) |
| 5888 | { |
| 5889 | int cpu1 = -1, cpu2 = -1, cpu, orig_cpu = raw_smp_processor_id(); |
| 5890 | unsigned long j0, j1, distance, max_distance = 0; |
| 5891 | struct sched_domain *sd; |
| 5892 | |
| 5893 | j0 = jiffies; |
| 5894 | |
| 5895 | /* |
| 5896 | * First pass - calculate the cacheflush times: |
| 5897 | */ |
| 5898 | for_each_cpu_mask(cpu1, *cpu_map) { |
| 5899 | for_each_cpu_mask(cpu2, *cpu_map) { |
| 5900 | if (cpu1 == cpu2) |
| 5901 | continue; |
| 5902 | distance = domain_distance(cpu1, cpu2); |
| 5903 | max_distance = max(max_distance, distance); |
| 5904 | /* |
| 5905 | * No result cached yet? |
| 5906 | */ |
| 5907 | if (migration_cost[distance] == -1LL) |
| 5908 | migration_cost[distance] = |
| 5909 | measure_migration_cost(cpu1, cpu2); |
| 5910 | } |
| 5911 | } |
| 5912 | /* |
| 5913 | * Second pass - update the sched domain hierarchy with |
| 5914 | * the new cache-hot-time estimations: |
| 5915 | */ |
| 5916 | for_each_cpu_mask(cpu, *cpu_map) { |
| 5917 | distance = 0; |
| 5918 | for_each_domain(cpu, sd) { |
| 5919 | sd->cache_hot_time = migration_cost[distance]; |
| 5920 | distance++; |
| 5921 | } |
| 5922 | } |
| 5923 | /* |
| 5924 | * Print the matrix: |
| 5925 | */ |
| 5926 | if (migration_debug) |
| 5927 | printk("migration: max_cache_size: %d, cpu: %d MHz:\n", |
| 5928 | max_cache_size, |
| 5929 | #ifdef CONFIG_X86 |
| 5930 | cpu_khz/1000 |
| 5931 | #else |
| 5932 | -1 |
| 5933 | #endif |
| 5934 | ); |
Chuck Ebbert | bd576c9 | 2006-02-04 23:27:42 -0800 | [diff] [blame] | 5935 | if (system_state == SYSTEM_BOOTING) { |
| 5936 | printk("migration_cost="); |
| 5937 | for (distance = 0; distance <= max_distance; distance++) { |
| 5938 | if (distance) |
| 5939 | printk(","); |
| 5940 | printk("%ld", (long)migration_cost[distance] / 1000); |
| 5941 | } |
| 5942 | printk("\n"); |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5943 | } |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5944 | j1 = jiffies; |
| 5945 | if (migration_debug) |
| 5946 | printk("migration: %ld seconds\n", (j1-j0)/HZ); |
| 5947 | |
| 5948 | /* |
| 5949 | * Move back to the original CPU. NUMA-Q gets confused |
| 5950 | * if we migrate to another quad during bootup. |
| 5951 | */ |
| 5952 | if (raw_smp_processor_id() != orig_cpu) { |
| 5953 | cpumask_t mask = cpumask_of_cpu(orig_cpu), |
| 5954 | saved_mask = current->cpus_allowed; |
| 5955 | |
| 5956 | set_cpus_allowed(current, mask); |
| 5957 | set_cpus_allowed(current, saved_mask); |
| 5958 | } |
| 5959 | } |
| 5960 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5961 | #ifdef CONFIG_NUMA |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5962 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5963 | /** |
| 5964 | * find_next_best_node - find the next node to include in a sched_domain |
| 5965 | * @node: node whose sched_domain we're building |
| 5966 | * @used_nodes: nodes already in the sched_domain |
| 5967 | * |
| 5968 | * Find the next node to include in a given scheduling domain. Simply |
| 5969 | * finds the closest node not already in the @used_nodes map. |
| 5970 | * |
| 5971 | * Should use nodemask_t. |
| 5972 | */ |
| 5973 | static int find_next_best_node(int node, unsigned long *used_nodes) |
| 5974 | { |
| 5975 | int i, n, val, min_val, best_node = 0; |
| 5976 | |
| 5977 | min_val = INT_MAX; |
| 5978 | |
| 5979 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 5980 | /* Start at @node */ |
| 5981 | n = (node + i) % MAX_NUMNODES; |
| 5982 | |
| 5983 | if (!nr_cpus_node(n)) |
| 5984 | continue; |
| 5985 | |
| 5986 | /* Skip already used nodes */ |
| 5987 | if (test_bit(n, used_nodes)) |
| 5988 | continue; |
| 5989 | |
| 5990 | /* Simple min distance search */ |
| 5991 | val = node_distance(node, n); |
| 5992 | |
| 5993 | if (val < min_val) { |
| 5994 | min_val = val; |
| 5995 | best_node = n; |
| 5996 | } |
| 5997 | } |
| 5998 | |
| 5999 | set_bit(best_node, used_nodes); |
| 6000 | return best_node; |
| 6001 | } |
| 6002 | |
| 6003 | /** |
| 6004 | * sched_domain_node_span - get a cpumask for a node's sched_domain |
| 6005 | * @node: node whose cpumask we're constructing |
| 6006 | * @size: number of nodes to include in this span |
| 6007 | * |
| 6008 | * Given a node, construct a good cpumask for its sched_domain to span. It |
| 6009 | * should be one that prevents unnecessary balancing, but also spreads tasks |
| 6010 | * out optimally. |
| 6011 | */ |
| 6012 | static cpumask_t sched_domain_node_span(int node) |
| 6013 | { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6014 | DECLARE_BITMAP(used_nodes, MAX_NUMNODES); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6015 | cpumask_t span, nodemask; |
| 6016 | int i; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6017 | |
| 6018 | cpus_clear(span); |
| 6019 | bitmap_zero(used_nodes, MAX_NUMNODES); |
| 6020 | |
| 6021 | nodemask = node_to_cpumask(node); |
| 6022 | cpus_or(span, span, nodemask); |
| 6023 | set_bit(node, used_nodes); |
| 6024 | |
| 6025 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { |
| 6026 | int next_node = find_next_best_node(node, used_nodes); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6027 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6028 | nodemask = node_to_cpumask(next_node); |
| 6029 | cpus_or(span, span, nodemask); |
| 6030 | } |
| 6031 | |
| 6032 | return span; |
| 6033 | } |
| 6034 | #endif |
| 6035 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6036 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6037 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6038 | /* |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6039 | * SMT sched-domains: |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6040 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6041 | #ifdef CONFIG_SCHED_SMT |
| 6042 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); |
| 6043 | static struct sched_group sched_group_cpus[NR_CPUS]; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6044 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6045 | static int cpu_to_cpu_group(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6046 | { |
| 6047 | return cpu; |
| 6048 | } |
| 6049 | #endif |
| 6050 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6051 | /* |
| 6052 | * multi-core sched-domains: |
| 6053 | */ |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6054 | #ifdef CONFIG_SCHED_MC |
| 6055 | static DEFINE_PER_CPU(struct sched_domain, core_domains); |
Srivatsa Vaddagiri | 3693816 | 2006-06-27 02:54:41 -0700 | [diff] [blame] | 6056 | static struct sched_group *sched_group_core_bycpu[NR_CPUS]; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6057 | #endif |
| 6058 | |
| 6059 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) |
| 6060 | static int cpu_to_core_group(int cpu) |
| 6061 | { |
| 6062 | return first_cpu(cpu_sibling_map[cpu]); |
| 6063 | } |
| 6064 | #elif defined(CONFIG_SCHED_MC) |
| 6065 | static int cpu_to_core_group(int cpu) |
| 6066 | { |
| 6067 | return cpu; |
| 6068 | } |
| 6069 | #endif |
| 6070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
Srivatsa Vaddagiri | 3693816 | 2006-06-27 02:54:41 -0700 | [diff] [blame] | 6072 | static struct sched_group *sched_group_phys_bycpu[NR_CPUS]; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6073 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6074 | static int cpu_to_phys_group(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6076 | #ifdef CONFIG_SCHED_MC |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6077 | cpumask_t mask = cpu_coregroup_map(cpu); |
| 6078 | return first_cpu(mask); |
| 6079 | #elif defined(CONFIG_SCHED_SMT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | return first_cpu(cpu_sibling_map[cpu]); |
| 6081 | #else |
| 6082 | return cpu; |
| 6083 | #endif |
| 6084 | } |
| 6085 | |
| 6086 | #ifdef CONFIG_NUMA |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6087 | /* |
| 6088 | * The init_sched_build_groups can't handle what we want to do with node |
| 6089 | * groups, so roll our own. Now each node has its own list of groups which |
| 6090 | * gets dynamically allocated. |
| 6091 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6093 | static struct sched_group **sched_group_nodes_bycpu[NR_CPUS]; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6094 | |
| 6095 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6096 | static struct sched_group *sched_group_allnodes_bycpu[NR_CPUS]; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6097 | |
| 6098 | static int cpu_to_allnodes_group(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | { |
| 6100 | return cpu_to_node(cpu); |
| 6101 | } |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6102 | static void init_numa_sched_groups_power(struct sched_group *group_head) |
| 6103 | { |
| 6104 | struct sched_group *sg = group_head; |
| 6105 | int j; |
| 6106 | |
| 6107 | if (!sg) |
| 6108 | return; |
| 6109 | next_sg: |
| 6110 | for_each_cpu_mask(j, sg->cpumask) { |
| 6111 | struct sched_domain *sd; |
| 6112 | |
| 6113 | sd = &per_cpu(phys_domains, j); |
| 6114 | if (j != first_cpu(sd->groups->cpumask)) { |
| 6115 | /* |
| 6116 | * Only add "power" once for each |
| 6117 | * physical package. |
| 6118 | */ |
| 6119 | continue; |
| 6120 | } |
| 6121 | |
| 6122 | sg->cpu_power += sd->groups->cpu_power; |
| 6123 | } |
| 6124 | sg = sg->next; |
| 6125 | if (sg != group_head) |
| 6126 | goto next_sg; |
| 6127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | #endif |
| 6129 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6130 | /* Free memory allocated for various sched_group structures */ |
| 6131 | static void free_sched_groups(const cpumask_t *cpu_map) |
| 6132 | { |
Srivatsa Vaddagiri | 3693816 | 2006-06-27 02:54:41 -0700 | [diff] [blame] | 6133 | int cpu; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6134 | #ifdef CONFIG_NUMA |
| 6135 | int i; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6136 | |
| 6137 | for_each_cpu_mask(cpu, *cpu_map) { |
| 6138 | struct sched_group *sched_group_allnodes |
| 6139 | = sched_group_allnodes_bycpu[cpu]; |
| 6140 | struct sched_group **sched_group_nodes |
| 6141 | = sched_group_nodes_bycpu[cpu]; |
| 6142 | |
| 6143 | if (sched_group_allnodes) { |
| 6144 | kfree(sched_group_allnodes); |
| 6145 | sched_group_allnodes_bycpu[cpu] = NULL; |
| 6146 | } |
| 6147 | |
| 6148 | if (!sched_group_nodes) |
| 6149 | continue; |
| 6150 | |
| 6151 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6152 | cpumask_t nodemask = node_to_cpumask(i); |
| 6153 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; |
| 6154 | |
| 6155 | cpus_and(nodemask, nodemask, *cpu_map); |
| 6156 | if (cpus_empty(nodemask)) |
| 6157 | continue; |
| 6158 | |
| 6159 | if (sg == NULL) |
| 6160 | continue; |
| 6161 | sg = sg->next; |
| 6162 | next_sg: |
| 6163 | oldsg = sg; |
| 6164 | sg = sg->next; |
| 6165 | kfree(oldsg); |
| 6166 | if (oldsg != sched_group_nodes[i]) |
| 6167 | goto next_sg; |
| 6168 | } |
| 6169 | kfree(sched_group_nodes); |
| 6170 | sched_group_nodes_bycpu[cpu] = NULL; |
| 6171 | } |
| 6172 | #endif |
Srivatsa Vaddagiri | 3693816 | 2006-06-27 02:54:41 -0700 | [diff] [blame] | 6173 | for_each_cpu_mask(cpu, *cpu_map) { |
| 6174 | if (sched_group_phys_bycpu[cpu]) { |
| 6175 | kfree(sched_group_phys_bycpu[cpu]); |
| 6176 | sched_group_phys_bycpu[cpu] = NULL; |
| 6177 | } |
| 6178 | #ifdef CONFIG_SCHED_MC |
| 6179 | if (sched_group_core_bycpu[cpu]) { |
| 6180 | kfree(sched_group_core_bycpu[cpu]); |
| 6181 | sched_group_core_bycpu[cpu] = NULL; |
| 6182 | } |
| 6183 | #endif |
| 6184 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6185 | } |
| 6186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6188 | * Build sched domains for a given set of cpus and attach the sched domains |
| 6189 | * to the individual cpus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6191 | static int build_sched_domains(const cpumask_t *cpu_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | { |
| 6193 | int i; |
Srivatsa Vaddagiri | 3693816 | 2006-06-27 02:54:41 -0700 | [diff] [blame] | 6194 | struct sched_group *sched_group_phys = NULL; |
| 6195 | #ifdef CONFIG_SCHED_MC |
| 6196 | struct sched_group *sched_group_core = NULL; |
| 6197 | #endif |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6198 | #ifdef CONFIG_NUMA |
| 6199 | struct sched_group **sched_group_nodes = NULL; |
| 6200 | struct sched_group *sched_group_allnodes = NULL; |
| 6201 | |
| 6202 | /* |
| 6203 | * Allocate the per-node list of sched groups |
| 6204 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6205 | sched_group_nodes = kzalloc(sizeof(struct sched_group*)*MAX_NUMNODES, |
Srivatsa Vaddagiri | d3a5aa9 | 2006-06-27 02:54:39 -0700 | [diff] [blame] | 6206 | GFP_KERNEL); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6207 | if (!sched_group_nodes) { |
| 6208 | printk(KERN_WARNING "Can not alloc sched group node list\n"); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6209 | return -ENOMEM; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6210 | } |
| 6211 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; |
| 6212 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | |
| 6214 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6215 | * Set up domains for cpus specified by the cpu_map. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6217 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | int group; |
| 6219 | struct sched_domain *sd = NULL, *p; |
| 6220 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(i)); |
| 6221 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6222 | cpus_and(nodemask, nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | |
| 6224 | #ifdef CONFIG_NUMA |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6225 | if (cpus_weight(*cpu_map) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6226 | > SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) { |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6227 | if (!sched_group_allnodes) { |
| 6228 | sched_group_allnodes |
| 6229 | = kmalloc(sizeof(struct sched_group) |
| 6230 | * MAX_NUMNODES, |
| 6231 | GFP_KERNEL); |
| 6232 | if (!sched_group_allnodes) { |
| 6233 | printk(KERN_WARNING |
| 6234 | "Can not alloc allnodes sched group\n"); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6235 | goto error; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6236 | } |
| 6237 | sched_group_allnodes_bycpu[i] |
| 6238 | = sched_group_allnodes; |
| 6239 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6240 | sd = &per_cpu(allnodes_domains, i); |
| 6241 | *sd = SD_ALLNODES_INIT; |
| 6242 | sd->span = *cpu_map; |
| 6243 | group = cpu_to_allnodes_group(i); |
| 6244 | sd->groups = &sched_group_allnodes[group]; |
| 6245 | p = sd; |
| 6246 | } else |
| 6247 | p = NULL; |
| 6248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | sd = &per_cpu(node_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | *sd = SD_NODE_INIT; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6251 | sd->span = sched_domain_node_span(cpu_to_node(i)); |
| 6252 | sd->parent = p; |
| 6253 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | #endif |
| 6255 | |
Srivatsa Vaddagiri | 3693816 | 2006-06-27 02:54:41 -0700 | [diff] [blame] | 6256 | if (!sched_group_phys) { |
| 6257 | sched_group_phys |
| 6258 | = kmalloc(sizeof(struct sched_group) * NR_CPUS, |
| 6259 | GFP_KERNEL); |
| 6260 | if (!sched_group_phys) { |
| 6261 | printk (KERN_WARNING "Can not alloc phys sched" |
| 6262 | "group\n"); |
| 6263 | goto error; |
| 6264 | } |
| 6265 | sched_group_phys_bycpu[i] = sched_group_phys; |
| 6266 | } |
| 6267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | p = sd; |
| 6269 | sd = &per_cpu(phys_domains, i); |
| 6270 | group = cpu_to_phys_group(i); |
| 6271 | *sd = SD_CPU_INIT; |
| 6272 | sd->span = nodemask; |
| 6273 | sd->parent = p; |
| 6274 | sd->groups = &sched_group_phys[group]; |
| 6275 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6276 | #ifdef CONFIG_SCHED_MC |
Srivatsa Vaddagiri | 3693816 | 2006-06-27 02:54:41 -0700 | [diff] [blame] | 6277 | if (!sched_group_core) { |
| 6278 | sched_group_core |
| 6279 | = kmalloc(sizeof(struct sched_group) * NR_CPUS, |
| 6280 | GFP_KERNEL); |
| 6281 | if (!sched_group_core) { |
| 6282 | printk (KERN_WARNING "Can not alloc core sched" |
| 6283 | "group\n"); |
| 6284 | goto error; |
| 6285 | } |
| 6286 | sched_group_core_bycpu[i] = sched_group_core; |
| 6287 | } |
| 6288 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6289 | p = sd; |
| 6290 | sd = &per_cpu(core_domains, i); |
| 6291 | group = cpu_to_core_group(i); |
| 6292 | *sd = SD_MC_INIT; |
| 6293 | sd->span = cpu_coregroup_map(i); |
| 6294 | cpus_and(sd->span, sd->span, *cpu_map); |
| 6295 | sd->parent = p; |
| 6296 | sd->groups = &sched_group_core[group]; |
| 6297 | #endif |
| 6298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6299 | #ifdef CONFIG_SCHED_SMT |
| 6300 | p = sd; |
| 6301 | sd = &per_cpu(cpu_domains, i); |
| 6302 | group = cpu_to_cpu_group(i); |
| 6303 | *sd = SD_SIBLING_INIT; |
| 6304 | sd->span = cpu_sibling_map[i]; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6305 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | sd->parent = p; |
| 6307 | sd->groups = &sched_group_cpus[group]; |
| 6308 | #endif |
| 6309 | } |
| 6310 | |
| 6311 | #ifdef CONFIG_SCHED_SMT |
| 6312 | /* Set up CPU (sibling) groups */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6313 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | cpumask_t this_sibling_map = cpu_sibling_map[i]; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6315 | cpus_and(this_sibling_map, this_sibling_map, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | if (i != first_cpu(this_sibling_map)) |
| 6317 | continue; |
| 6318 | |
| 6319 | init_sched_build_groups(sched_group_cpus, this_sibling_map, |
| 6320 | &cpu_to_cpu_group); |
| 6321 | } |
| 6322 | #endif |
| 6323 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6324 | #ifdef CONFIG_SCHED_MC |
| 6325 | /* Set up multi-core groups */ |
| 6326 | for_each_cpu_mask(i, *cpu_map) { |
| 6327 | cpumask_t this_core_map = cpu_coregroup_map(i); |
| 6328 | cpus_and(this_core_map, this_core_map, *cpu_map); |
| 6329 | if (i != first_cpu(this_core_map)) |
| 6330 | continue; |
| 6331 | init_sched_build_groups(sched_group_core, this_core_map, |
| 6332 | &cpu_to_core_group); |
| 6333 | } |
| 6334 | #endif |
| 6335 | |
| 6336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | /* Set up physical groups */ |
| 6338 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6339 | cpumask_t nodemask = node_to_cpumask(i); |
| 6340 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6341 | cpus_and(nodemask, nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | if (cpus_empty(nodemask)) |
| 6343 | continue; |
| 6344 | |
| 6345 | init_sched_build_groups(sched_group_phys, nodemask, |
| 6346 | &cpu_to_phys_group); |
| 6347 | } |
| 6348 | |
| 6349 | #ifdef CONFIG_NUMA |
| 6350 | /* Set up node groups */ |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6351 | if (sched_group_allnodes) |
| 6352 | init_sched_build_groups(sched_group_allnodes, *cpu_map, |
| 6353 | &cpu_to_allnodes_group); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6354 | |
| 6355 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6356 | /* Set up node groups */ |
| 6357 | struct sched_group *sg, *prev; |
| 6358 | cpumask_t nodemask = node_to_cpumask(i); |
| 6359 | cpumask_t domainspan; |
| 6360 | cpumask_t covered = CPU_MASK_NONE; |
| 6361 | int j; |
| 6362 | |
| 6363 | cpus_and(nodemask, nodemask, *cpu_map); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6364 | if (cpus_empty(nodemask)) { |
| 6365 | sched_group_nodes[i] = NULL; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6366 | continue; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6367 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6368 | |
| 6369 | domainspan = sched_domain_node_span(i); |
| 6370 | cpus_and(domainspan, domainspan, *cpu_map); |
| 6371 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 6372 | sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6373 | if (!sg) { |
| 6374 | printk(KERN_WARNING "Can not alloc domain group for " |
| 6375 | "node %d\n", i); |
| 6376 | goto error; |
| 6377 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6378 | sched_group_nodes[i] = sg; |
| 6379 | for_each_cpu_mask(j, nodemask) { |
| 6380 | struct sched_domain *sd; |
| 6381 | sd = &per_cpu(node_domains, j); |
| 6382 | sd->groups = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6383 | } |
| 6384 | sg->cpu_power = 0; |
| 6385 | sg->cpumask = nodemask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6386 | sg->next = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6387 | cpus_or(covered, covered, nodemask); |
| 6388 | prev = sg; |
| 6389 | |
| 6390 | for (j = 0; j < MAX_NUMNODES; j++) { |
| 6391 | cpumask_t tmp, notcovered; |
| 6392 | int n = (i + j) % MAX_NUMNODES; |
| 6393 | |
| 6394 | cpus_complement(notcovered, covered); |
| 6395 | cpus_and(tmp, notcovered, *cpu_map); |
| 6396 | cpus_and(tmp, tmp, domainspan); |
| 6397 | if (cpus_empty(tmp)) |
| 6398 | break; |
| 6399 | |
| 6400 | nodemask = node_to_cpumask(n); |
| 6401 | cpus_and(tmp, tmp, nodemask); |
| 6402 | if (cpus_empty(tmp)) |
| 6403 | continue; |
| 6404 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 6405 | sg = kmalloc_node(sizeof(struct sched_group), |
| 6406 | GFP_KERNEL, i); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6407 | if (!sg) { |
| 6408 | printk(KERN_WARNING |
| 6409 | "Can not alloc domain group for node %d\n", j); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6410 | goto error; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6411 | } |
| 6412 | sg->cpu_power = 0; |
| 6413 | sg->cpumask = tmp; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6414 | sg->next = prev->next; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6415 | cpus_or(covered, covered, tmp); |
| 6416 | prev->next = sg; |
| 6417 | prev = sg; |
| 6418 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6419 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | #endif |
| 6421 | |
| 6422 | /* Calculate CPU power for physical packages and nodes */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6423 | #ifdef CONFIG_SCHED_SMT |
| 6424 | for_each_cpu_mask(i, *cpu_map) { |
| 6425 | struct sched_domain *sd; |
| 6426 | sd = &per_cpu(cpu_domains, i); |
| 6427 | sd->groups->cpu_power = SCHED_LOAD_SCALE; |
| 6428 | } |
| 6429 | #endif |
| 6430 | #ifdef CONFIG_SCHED_MC |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6431 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | int power; |
| 6433 | struct sched_domain *sd; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6434 | sd = &per_cpu(core_domains, i); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6435 | if (sched_smt_power_savings) |
| 6436 | power = SCHED_LOAD_SCALE * cpus_weight(sd->groups->cpumask); |
| 6437 | else |
| 6438 | power = SCHED_LOAD_SCALE + (cpus_weight(sd->groups->cpumask)-1) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6439 | * SCHED_LOAD_SCALE / 10; |
| 6440 | sd->groups->cpu_power = power; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6441 | } |
| 6442 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6443 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6444 | for_each_cpu_mask(i, *cpu_map) { |
| 6445 | struct sched_domain *sd; |
| 6446 | #ifdef CONFIG_SCHED_MC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | sd = &per_cpu(phys_domains, i); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6448 | if (i != first_cpu(sd->groups->cpumask)) |
| 6449 | continue; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6450 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6451 | sd->groups->cpu_power = 0; |
| 6452 | if (sched_mc_power_savings || sched_smt_power_savings) { |
| 6453 | int j; |
| 6454 | |
| 6455 | for_each_cpu_mask(j, sd->groups->cpumask) { |
| 6456 | struct sched_domain *sd1; |
| 6457 | sd1 = &per_cpu(core_domains, j); |
| 6458 | /* |
| 6459 | * for each core we will add once |
| 6460 | * to the group in physical domain |
| 6461 | */ |
| 6462 | if (j != first_cpu(sd1->groups->cpumask)) |
| 6463 | continue; |
| 6464 | |
| 6465 | if (sched_smt_power_savings) |
| 6466 | sd->groups->cpu_power += sd1->groups->cpu_power; |
| 6467 | else |
| 6468 | sd->groups->cpu_power += SCHED_LOAD_SCALE; |
| 6469 | } |
| 6470 | } else |
| 6471 | /* |
| 6472 | * This has to be < 2 * SCHED_LOAD_SCALE |
| 6473 | * Lets keep it SCHED_LOAD_SCALE, so that |
| 6474 | * while calculating NUMA group's cpu_power |
| 6475 | * we can simply do |
| 6476 | * numa_group->cpu_power += phys_group->cpu_power; |
| 6477 | * |
| 6478 | * See "only add power once for each physical pkg" |
| 6479 | * comment below |
| 6480 | */ |
| 6481 | sd->groups->cpu_power = SCHED_LOAD_SCALE; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6482 | #else |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6483 | int power; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6484 | sd = &per_cpu(phys_domains, i); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6485 | if (sched_smt_power_savings) |
| 6486 | power = SCHED_LOAD_SCALE * cpus_weight(sd->groups->cpumask); |
| 6487 | else |
| 6488 | power = SCHED_LOAD_SCALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | sd->groups->cpu_power = power; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6490 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | } |
| 6492 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6493 | #ifdef CONFIG_NUMA |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6494 | for (i = 0; i < MAX_NUMNODES; i++) |
| 6495 | init_numa_sched_groups_power(sched_group_nodes[i]); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6496 | |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 6497 | if (sched_group_allnodes) { |
| 6498 | int group = cpu_to_allnodes_group(first_cpu(*cpu_map)); |
| 6499 | struct sched_group *sg = &sched_group_allnodes[group]; |
| 6500 | |
| 6501 | init_numa_sched_groups_power(sg); |
| 6502 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6503 | #endif |
| 6504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6505 | /* Attach the domains */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6506 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6507 | struct sched_domain *sd; |
| 6508 | #ifdef CONFIG_SCHED_SMT |
| 6509 | sd = &per_cpu(cpu_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6510 | #elif defined(CONFIG_SCHED_MC) |
| 6511 | sd = &per_cpu(core_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6512 | #else |
| 6513 | sd = &per_cpu(phys_domains, i); |
| 6514 | #endif |
| 6515 | cpu_attach_domain(sd, i); |
| 6516 | } |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 6517 | /* |
| 6518 | * Tune cache-hot values: |
| 6519 | */ |
| 6520 | calibrate_migration_costs(cpu_map); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6521 | |
| 6522 | return 0; |
| 6523 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6524 | error: |
| 6525 | free_sched_groups(cpu_map); |
| 6526 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6527 | } |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6528 | /* |
| 6529 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
| 6530 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6531 | static int arch_init_sched_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6532 | { |
| 6533 | cpumask_t cpu_default_map; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6534 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6536 | /* |
| 6537 | * Setup mask for cpus without special case scheduling requirements. |
| 6538 | * For now this just excludes isolated cpus, but could be used to |
| 6539 | * exclude other special cases in the future. |
| 6540 | */ |
| 6541 | cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map); |
| 6542 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6543 | err = build_sched_domains(&cpu_default_map); |
| 6544 | |
| 6545 | return err; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6546 | } |
| 6547 | |
| 6548 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6550 | free_sched_groups(cpu_map); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6553 | /* |
| 6554 | * Detach sched domains from a group of cpus specified in cpu_map |
| 6555 | * These cpus will now be attached to the NULL domain |
| 6556 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 6557 | static void detach_destroy_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6558 | { |
| 6559 | int i; |
| 6560 | |
| 6561 | for_each_cpu_mask(i, *cpu_map) |
| 6562 | cpu_attach_domain(NULL, i); |
| 6563 | synchronize_sched(); |
| 6564 | arch_destroy_sched_domains(cpu_map); |
| 6565 | } |
| 6566 | |
| 6567 | /* |
| 6568 | * Partition sched domains as specified by the cpumasks below. |
| 6569 | * This attaches all cpus from the cpumasks to the NULL domain, |
| 6570 | * waits for a RCU quiescent period, recalculates sched |
| 6571 | * domain information and then attaches them back to the |
| 6572 | * correct sched domains |
| 6573 | * Call with hotplug lock held |
| 6574 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6575 | int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6576 | { |
| 6577 | cpumask_t change_map; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6578 | int err = 0; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6579 | |
| 6580 | cpus_and(*partition1, *partition1, cpu_online_map); |
| 6581 | cpus_and(*partition2, *partition2, cpu_online_map); |
| 6582 | cpus_or(change_map, *partition1, *partition2); |
| 6583 | |
| 6584 | /* Detach sched domains from all of the affected cpus */ |
| 6585 | detach_destroy_domains(&change_map); |
| 6586 | if (!cpus_empty(*partition1)) |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6587 | err = build_sched_domains(partition1); |
| 6588 | if (!err && !cpus_empty(*partition2)) |
| 6589 | err = build_sched_domains(partition2); |
| 6590 | |
| 6591 | return err; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6592 | } |
| 6593 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6594 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 6595 | int arch_reinit_sched_domains(void) |
| 6596 | { |
| 6597 | int err; |
| 6598 | |
| 6599 | lock_cpu_hotplug(); |
| 6600 | detach_destroy_domains(&cpu_online_map); |
| 6601 | err = arch_init_sched_domains(&cpu_online_map); |
| 6602 | unlock_cpu_hotplug(); |
| 6603 | |
| 6604 | return err; |
| 6605 | } |
| 6606 | |
| 6607 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) |
| 6608 | { |
| 6609 | int ret; |
| 6610 | |
| 6611 | if (buf[0] != '0' && buf[0] != '1') |
| 6612 | return -EINVAL; |
| 6613 | |
| 6614 | if (smt) |
| 6615 | sched_smt_power_savings = (buf[0] == '1'); |
| 6616 | else |
| 6617 | sched_mc_power_savings = (buf[0] == '1'); |
| 6618 | |
| 6619 | ret = arch_reinit_sched_domains(); |
| 6620 | |
| 6621 | return ret ? ret : count; |
| 6622 | } |
| 6623 | |
| 6624 | int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) |
| 6625 | { |
| 6626 | int err = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6627 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6628 | #ifdef CONFIG_SCHED_SMT |
| 6629 | if (smt_capable()) |
| 6630 | err = sysfs_create_file(&cls->kset.kobj, |
| 6631 | &attr_sched_smt_power_savings.attr); |
| 6632 | #endif |
| 6633 | #ifdef CONFIG_SCHED_MC |
| 6634 | if (!err && mc_capable()) |
| 6635 | err = sysfs_create_file(&cls->kset.kobj, |
| 6636 | &attr_sched_mc_power_savings.attr); |
| 6637 | #endif |
| 6638 | return err; |
| 6639 | } |
| 6640 | #endif |
| 6641 | |
| 6642 | #ifdef CONFIG_SCHED_MC |
| 6643 | static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page) |
| 6644 | { |
| 6645 | return sprintf(page, "%u\n", sched_mc_power_savings); |
| 6646 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6647 | static ssize_t sched_mc_power_savings_store(struct sys_device *dev, |
| 6648 | const char *buf, size_t count) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6649 | { |
| 6650 | return sched_power_savings_store(buf, count, 0); |
| 6651 | } |
| 6652 | SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show, |
| 6653 | sched_mc_power_savings_store); |
| 6654 | #endif |
| 6655 | |
| 6656 | #ifdef CONFIG_SCHED_SMT |
| 6657 | static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page) |
| 6658 | { |
| 6659 | return sprintf(page, "%u\n", sched_smt_power_savings); |
| 6660 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6661 | static ssize_t sched_smt_power_savings_store(struct sys_device *dev, |
| 6662 | const char *buf, size_t count) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6663 | { |
| 6664 | return sched_power_savings_store(buf, count, 1); |
| 6665 | } |
| 6666 | SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show, |
| 6667 | sched_smt_power_savings_store); |
| 6668 | #endif |
| 6669 | |
| 6670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6671 | #ifdef CONFIG_HOTPLUG_CPU |
| 6672 | /* |
| 6673 | * Force a reinitialization of the sched domains hierarchy. The domains |
| 6674 | * and groups cannot be updated in place without racing with the balancing |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 6675 | * code, so we temporarily attach all running cpus to the NULL domain |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6676 | * which will prevent rebalancing while the sched domains are recalculated. |
| 6677 | */ |
| 6678 | static int update_sched_domains(struct notifier_block *nfb, |
| 6679 | unsigned long action, void *hcpu) |
| 6680 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6681 | switch (action) { |
| 6682 | case CPU_UP_PREPARE: |
| 6683 | case CPU_DOWN_PREPARE: |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6684 | detach_destroy_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | return NOTIFY_OK; |
| 6686 | |
| 6687 | case CPU_UP_CANCELED: |
| 6688 | case CPU_DOWN_FAILED: |
| 6689 | case CPU_ONLINE: |
| 6690 | case CPU_DEAD: |
| 6691 | /* |
| 6692 | * Fall through and re-initialise the domains. |
| 6693 | */ |
| 6694 | break; |
| 6695 | default: |
| 6696 | return NOTIFY_DONE; |
| 6697 | } |
| 6698 | |
| 6699 | /* The hotplug lock is already held by cpu_up/cpu_down */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6700 | arch_init_sched_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | |
| 6702 | return NOTIFY_OK; |
| 6703 | } |
| 6704 | #endif |
| 6705 | |
| 6706 | void __init sched_init_smp(void) |
| 6707 | { |
| 6708 | lock_cpu_hotplug(); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6709 | arch_init_sched_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6710 | unlock_cpu_hotplug(); |
| 6711 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
| 6712 | hotcpu_notifier(update_sched_domains, 0); |
| 6713 | } |
| 6714 | #else |
| 6715 | void __init sched_init_smp(void) |
| 6716 | { |
| 6717 | } |
| 6718 | #endif /* CONFIG_SMP */ |
| 6719 | |
| 6720 | int in_sched_functions(unsigned long addr) |
| 6721 | { |
| 6722 | /* Linker adds these: start and end of __sched functions */ |
| 6723 | extern char __sched_text_start[], __sched_text_end[]; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | return in_lock_functions(addr) || |
| 6726 | (addr >= (unsigned long)__sched_text_start |
| 6727 | && addr < (unsigned long)__sched_text_end); |
| 6728 | } |
| 6729 | |
| 6730 | void __init sched_init(void) |
| 6731 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6732 | int i, j, k; |
| 6733 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 6734 | for_each_possible_cpu(i) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6735 | struct prio_array *array; |
| 6736 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | |
| 6738 | rq = cpu_rq(i); |
| 6739 | spin_lock_init(&rq->lock); |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 6740 | lockdep_set_class(&rq->lock, &rq->rq_lock_key); |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 6741 | rq->nr_running = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6742 | rq->active = rq->arrays; |
| 6743 | rq->expired = rq->arrays + 1; |
| 6744 | rq->best_expired_prio = MAX_PRIO; |
| 6745 | |
| 6746 | #ifdef CONFIG_SMP |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 6747 | rq->sd = NULL; |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 6748 | for (j = 1; j < 3; j++) |
| 6749 | rq->cpu_load[j] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | rq->active_balance = 0; |
| 6751 | rq->push_cpu = 0; |
| 6752 | rq->migration_thread = NULL; |
| 6753 | INIT_LIST_HEAD(&rq->migration_queue); |
| 6754 | #endif |
| 6755 | atomic_set(&rq->nr_iowait, 0); |
| 6756 | |
| 6757 | for (j = 0; j < 2; j++) { |
| 6758 | array = rq->arrays + j; |
| 6759 | for (k = 0; k < MAX_PRIO; k++) { |
| 6760 | INIT_LIST_HEAD(array->queue + k); |
| 6761 | __clear_bit(k, array->bitmap); |
| 6762 | } |
| 6763 | // delimiter for bitsearch |
| 6764 | __set_bit(MAX_PRIO, array->bitmap); |
| 6765 | } |
| 6766 | } |
| 6767 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 6768 | set_load_weight(&init_task); |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame^] | 6769 | |
| 6770 | #ifdef CONFIG_RT_MUTEXES |
| 6771 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); |
| 6772 | #endif |
| 6773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6774 | /* |
| 6775 | * The boot idle thread does lazy MMU switching as well: |
| 6776 | */ |
| 6777 | atomic_inc(&init_mm.mm_count); |
| 6778 | enter_lazy_tlb(&init_mm, current); |
| 6779 | |
| 6780 | /* |
| 6781 | * Make us the idle thread. Technically, schedule() should not be |
| 6782 | * called from this thread, however somewhere below it might be, |
| 6783 | * but because we are the idle thread, we just pick up running again |
| 6784 | * when this runqueue becomes "idle". |
| 6785 | */ |
| 6786 | init_idle(current, smp_processor_id()); |
| 6787 | } |
| 6788 | |
| 6789 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 6790 | void __might_sleep(char *file, int line) |
| 6791 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6792 | #ifdef in_atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6793 | static unsigned long prev_jiffy; /* ratelimiting */ |
| 6794 | |
| 6795 | if ((in_atomic() || irqs_disabled()) && |
| 6796 | system_state == SYSTEM_RUNNING && !oops_in_progress) { |
| 6797 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 6798 | return; |
| 6799 | prev_jiffy = jiffies; |
Ingo Molnar | 91368d7 | 2006-03-23 03:00:54 -0800 | [diff] [blame] | 6800 | printk(KERN_ERR "BUG: sleeping function called from invalid" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | " context at %s:%d\n", file, line); |
| 6802 | printk("in_atomic():%d, irqs_disabled():%d\n", |
| 6803 | in_atomic(), irqs_disabled()); |
| 6804 | dump_stack(); |
| 6805 | } |
| 6806 | #endif |
| 6807 | } |
| 6808 | EXPORT_SYMBOL(__might_sleep); |
| 6809 | #endif |
| 6810 | |
| 6811 | #ifdef CONFIG_MAGIC_SYSRQ |
| 6812 | void normalize_rt_tasks(void) |
| 6813 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6814 | struct prio_array *array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6815 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6817 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | |
| 6819 | read_lock_irq(&tasklist_lock); |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 6820 | for_each_process(p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6821 | if (!rt_task(p)) |
| 6822 | continue; |
| 6823 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6824 | spin_lock_irqsave(&p->pi_lock, flags); |
| 6825 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | |
| 6827 | array = p->array; |
| 6828 | if (array) |
| 6829 | deactivate_task(p, task_rq(p)); |
| 6830 | __setscheduler(p, SCHED_NORMAL, 0); |
| 6831 | if (array) { |
| 6832 | __activate_task(p, task_rq(p)); |
| 6833 | resched_task(rq->curr); |
| 6834 | } |
| 6835 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6836 | __task_rq_unlock(rq); |
| 6837 | spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | } |
| 6839 | read_unlock_irq(&tasklist_lock); |
| 6840 | } |
| 6841 | |
| 6842 | #endif /* CONFIG_MAGIC_SYSRQ */ |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 6843 | |
| 6844 | #ifdef CONFIG_IA64 |
| 6845 | /* |
| 6846 | * These functions are only useful for the IA64 MCA handling. |
| 6847 | * |
| 6848 | * They can only be called when the whole system has been |
| 6849 | * stopped - every CPU needs to be quiescent, and no scheduling |
| 6850 | * activity can take place. Using them for anything else would |
| 6851 | * be a serious bug, and as a result, they aren't even visible |
| 6852 | * under any other configuration. |
| 6853 | */ |
| 6854 | |
| 6855 | /** |
| 6856 | * curr_task - return the current task for a given cpu. |
| 6857 | * @cpu: the processor in question. |
| 6858 | * |
| 6859 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 6860 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6861 | struct task_struct *curr_task(int cpu) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 6862 | { |
| 6863 | return cpu_curr(cpu); |
| 6864 | } |
| 6865 | |
| 6866 | /** |
| 6867 | * set_curr_task - set the current task for a given cpu. |
| 6868 | * @cpu: the processor in question. |
| 6869 | * @p: the task pointer to set. |
| 6870 | * |
| 6871 | * Description: This function must only be used when non-maskable interrupts |
| 6872 | * are serviced on a separate stack. It allows the architecture to switch the |
| 6873 | * notion of the current task on a cpu in a non-blocking manner. This function |
| 6874 | * must be called with all CPU's synchronized, and interrupts disabled, the |
| 6875 | * and caller must save the original value of the current task (see |
| 6876 | * curr_task() above) and restore that value before reenabling interrupts and |
| 6877 | * re-starting the system. |
| 6878 | * |
| 6879 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 6880 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6881 | void set_curr_task(int cpu, struct task_struct *p) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 6882 | { |
| 6883 | cpu_curr(cpu) = p; |
| 6884 | } |
| 6885 | |
| 6886 | #endif |