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> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 37 | #include <linux/freezer.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> |
Jay Lan | 8f0ab51 | 2006-09-30 23:28:59 -0700 | [diff] [blame] | 52 | #include <linux/tsacct_kern.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> |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 55 | #include <linux/reciprocal_div.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 57 | #include <asm/tlb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <asm/unistd.h> |
| 59 | |
| 60 | /* |
Alexey Dobriyan | b035b6d | 2007-02-10 01:45:10 -0800 | [diff] [blame] | 61 | * Scheduler clock - returns current time in nanosec units. |
| 62 | * This is default implementation. |
| 63 | * Architectures and sub-architectures can override this. |
| 64 | */ |
| 65 | unsigned long long __attribute__((weak)) sched_clock(void) |
| 66 | { |
| 67 | return (unsigned long long)jiffies * (1000000000 / HZ); |
| 68 | } |
| 69 | |
| 70 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | * Convert user-nice values [ -20 ... 0 ... 19 ] |
| 72 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], |
| 73 | * and back. |
| 74 | */ |
| 75 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) |
| 76 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) |
| 77 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) |
| 78 | |
| 79 | /* |
| 80 | * 'User priority' is the nice value converted to something we |
| 81 | * can work with better when scaling various scheduler parameters, |
| 82 | * it's a [ 0 ... 39 ] range. |
| 83 | */ |
| 84 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) |
| 85 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) |
| 86 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) |
| 87 | |
| 88 | /* |
| 89 | * Some helpers for converting nanosecond timing to jiffy resolution |
| 90 | */ |
| 91 | #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ)) |
| 92 | #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ)) |
| 93 | |
| 94 | /* |
| 95 | * These are the 'tuning knobs' of the scheduler: |
| 96 | * |
| 97 | * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger), |
| 98 | * default timeslice is 100 msecs, maximum timeslice is 800 msecs. |
| 99 | * Timeslices get refilled after they expire. |
| 100 | */ |
| 101 | #define MIN_TIMESLICE max(5 * HZ / 1000, 1) |
| 102 | #define DEF_TIMESLICE (100 * HZ / 1000) |
| 103 | #define ON_RUNQUEUE_WEIGHT 30 |
| 104 | #define CHILD_PENALTY 95 |
| 105 | #define PARENT_PENALTY 100 |
| 106 | #define EXIT_WEIGHT 3 |
| 107 | #define PRIO_BONUS_RATIO 25 |
| 108 | #define MAX_BONUS (MAX_USER_PRIO * PRIO_BONUS_RATIO / 100) |
| 109 | #define INTERACTIVE_DELTA 2 |
| 110 | #define MAX_SLEEP_AVG (DEF_TIMESLICE * MAX_BONUS) |
| 111 | #define STARVATION_LIMIT (MAX_SLEEP_AVG) |
| 112 | #define NS_MAX_SLEEP_AVG (JIFFIES_TO_NS(MAX_SLEEP_AVG)) |
| 113 | |
| 114 | /* |
| 115 | * If a task is 'interactive' then we reinsert it in the active |
| 116 | * array after it has expired its current timeslice. (it will not |
| 117 | * continue to run immediately, it will still roundrobin with |
| 118 | * other interactive tasks.) |
| 119 | * |
| 120 | * This part scales the interactivity limit depending on niceness. |
| 121 | * |
| 122 | * We scale it linearly, offset by the INTERACTIVE_DELTA delta. |
| 123 | * Here are a few examples of different nice levels: |
| 124 | * |
| 125 | * TASK_INTERACTIVE(-20): [1,1,1,1,1,1,1,1,1,0,0] |
| 126 | * TASK_INTERACTIVE(-10): [1,1,1,1,1,1,1,0,0,0,0] |
| 127 | * TASK_INTERACTIVE( 0): [1,1,1,1,0,0,0,0,0,0,0] |
| 128 | * TASK_INTERACTIVE( 10): [1,1,0,0,0,0,0,0,0,0,0] |
| 129 | * TASK_INTERACTIVE( 19): [0,0,0,0,0,0,0,0,0,0,0] |
| 130 | * |
| 131 | * (the X axis represents the possible -5 ... 0 ... +5 dynamic |
| 132 | * priority range a task can explore, a value of '1' means the |
| 133 | * task is rated interactive.) |
| 134 | * |
| 135 | * Ie. nice +19 tasks can never get 'interactive' enough to be |
| 136 | * reinserted into the active array. And only heavily CPU-hog nice -20 |
| 137 | * tasks will be expired. Default nice 0 tasks are somewhere between, |
| 138 | * it takes some effort for them to get interactive, but it's not |
| 139 | * too hard. |
| 140 | */ |
| 141 | |
| 142 | #define CURRENT_BONUS(p) \ |
| 143 | (NS_TO_JIFFIES((p)->sleep_avg) * MAX_BONUS / \ |
| 144 | MAX_SLEEP_AVG) |
| 145 | |
| 146 | #define GRANULARITY (10 * HZ / 1000 ? : 1) |
| 147 | |
| 148 | #ifdef CONFIG_SMP |
| 149 | #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ |
| 150 | (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)) * \ |
| 151 | num_online_cpus()) |
| 152 | #else |
| 153 | #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ |
| 154 | (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1))) |
| 155 | #endif |
| 156 | |
| 157 | #define SCALE(v1,v1_max,v2_max) \ |
| 158 | (v1) * (v2_max) / (v1_max) |
| 159 | |
| 160 | #define DELTA(p) \ |
Martin Andersson | 013d386 | 2006-03-27 01:15:18 -0800 | [diff] [blame] | 161 | (SCALE(TASK_NICE(p) + 20, 40, MAX_BONUS) - 20 * MAX_BONUS / 40 + \ |
| 162 | INTERACTIVE_DELTA) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | #define TASK_INTERACTIVE(p) \ |
| 165 | ((p)->prio <= (p)->static_prio - DELTA(p)) |
| 166 | |
| 167 | #define INTERACTIVE_SLEEP(p) \ |
| 168 | (JIFFIES_TO_NS(MAX_SLEEP_AVG * \ |
| 169 | (MAX_BONUS / 2 + DELTA((p)) + 1) / MAX_BONUS - 1)) |
| 170 | |
| 171 | #define TASK_PREEMPTS_CURR(p, rq) \ |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 172 | ((p)->prio < (rq)->curr->prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | #define SCALE_PRIO(x, prio) \ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 175 | max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE) |
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 | static unsigned int static_prio_timeslice(int static_prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 179 | if (static_prio < NICE_TO_PRIO(0)) |
| 180 | return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | else |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 182 | return SCALE_PRIO(DEF_TIMESLICE, static_prio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 184 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 185 | #ifdef CONFIG_SMP |
| 186 | /* |
| 187 | * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) |
| 188 | * Since cpu_power is a 'constant', we can use a reciprocal divide. |
| 189 | */ |
| 190 | static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load) |
| 191 | { |
| 192 | return reciprocal_divide(load, sg->reciprocal_cpu_power); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Each time a sched group cpu_power is changed, |
| 197 | * we must compute its reciprocal value |
| 198 | */ |
| 199 | static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val) |
| 200 | { |
| 201 | sg->__cpu_power += val; |
| 202 | sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power); |
| 203 | } |
| 204 | #endif |
| 205 | |
Borislav Petkov | 91fcdd4 | 2006-10-19 23:28:29 -0700 | [diff] [blame] | 206 | /* |
| 207 | * task_timeslice() scales user-nice values [ -20 ... 0 ... 19 ] |
| 208 | * to time slice values: [800ms ... 100ms ... 5ms] |
| 209 | * |
| 210 | * The higher a thread's priority, the bigger timeslices |
| 211 | * it gets during one round of execution. But even the lowest |
| 212 | * priority thread gets MIN_TIMESLICE worth of execution time. |
| 213 | */ |
| 214 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 215 | static inline unsigned int task_timeslice(struct task_struct *p) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 216 | { |
| 217 | return static_prio_timeslice(p->static_prio); |
| 218 | } |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | /* |
| 221 | * These are the runqueue data structures: |
| 222 | */ |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | struct prio_array { |
| 225 | unsigned int nr_active; |
Steven Rostedt | d444886 | 2006-06-27 02:54:29 -0700 | [diff] [blame] | 226 | DECLARE_BITMAP(bitmap, MAX_PRIO+1); /* include 1 bit for delimiter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | struct list_head queue[MAX_PRIO]; |
| 228 | }; |
| 229 | |
| 230 | /* |
| 231 | * This is the main, per-CPU runqueue data structure. |
| 232 | * |
| 233 | * Locking rule: those places that want to lock multiple runqueues |
| 234 | * (such as the load balancing or the thread migration code), lock |
| 235 | * acquire operations must be ordered by ascending &runqueue. |
| 236 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 237 | struct rq { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | spinlock_t lock; |
| 239 | |
| 240 | /* |
| 241 | * nr_running and cpu_load should be in the same cacheline because |
| 242 | * remote CPUs use both these fields when doing load calculation. |
| 243 | */ |
| 244 | unsigned long nr_running; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 245 | unsigned long raw_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | #ifdef CONFIG_SMP |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 247 | unsigned long cpu_load[3]; |
Siddha, Suresh B | bdecea3 | 2007-05-08 00:32:48 -0700 | [diff] [blame] | 248 | unsigned char idle_at_tick; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 249 | #ifdef CONFIG_NO_HZ |
| 250 | unsigned char in_nohz_recently; |
| 251 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | #endif |
| 253 | unsigned long long nr_switches; |
| 254 | |
| 255 | /* |
| 256 | * This is part of a global counter where only the total sum |
| 257 | * over all CPUs matters. A task can increase this counter on |
| 258 | * one CPU and if it got migrated afterwards it may decrease |
| 259 | * it on another CPU. Always updated under the runqueue lock: |
| 260 | */ |
| 261 | unsigned long nr_uninterruptible; |
| 262 | |
| 263 | unsigned long expired_timestamp; |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 264 | /* Cached timestamp set by update_cpu_clock() */ |
| 265 | unsigned long long most_recent_timestamp; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 266 | struct task_struct *curr, *idle; |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 267 | unsigned long next_balance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | struct mm_struct *prev_mm; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 269 | struct prio_array *active, *expired, arrays[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | int best_expired_prio; |
| 271 | atomic_t nr_iowait; |
| 272 | |
| 273 | #ifdef CONFIG_SMP |
| 274 | struct sched_domain *sd; |
| 275 | |
| 276 | /* For active balancing */ |
| 277 | int active_balance; |
| 278 | int push_cpu; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 279 | int cpu; /* cpu of this runqueue */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 281 | struct task_struct *migration_thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | struct list_head migration_queue; |
| 283 | #endif |
| 284 | |
| 285 | #ifdef CONFIG_SCHEDSTATS |
| 286 | /* latency stats */ |
| 287 | struct sched_info rq_sched_info; |
| 288 | |
| 289 | /* sys_sched_yield() stats */ |
| 290 | unsigned long yld_exp_empty; |
| 291 | unsigned long yld_act_empty; |
| 292 | unsigned long yld_both_empty; |
| 293 | unsigned long yld_cnt; |
| 294 | |
| 295 | /* schedule() stats */ |
| 296 | unsigned long sched_switch; |
| 297 | unsigned long sched_cnt; |
| 298 | unsigned long sched_goidle; |
| 299 | |
| 300 | /* try_to_wake_up() stats */ |
| 301 | unsigned long ttwu_cnt; |
| 302 | unsigned long ttwu_local; |
| 303 | #endif |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 304 | struct lock_class_key rq_lock_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | }; |
| 306 | |
Siddha, Suresh B | c339662 | 2007-05-08 00:33:09 -0700 | [diff] [blame] | 307 | static DEFINE_PER_CPU(struct rq, runqueues) ____cacheline_aligned_in_smp; |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 308 | static DEFINE_MUTEX(sched_hotcpu_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 310 | static inline int cpu_of(struct rq *rq) |
| 311 | { |
| 312 | #ifdef CONFIG_SMP |
| 313 | return rq->cpu; |
| 314 | #else |
| 315 | return 0; |
| 316 | #endif |
| 317 | } |
| 318 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 319 | /* |
| 320 | * 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] | 321 | * See detach_destroy_domains: synchronize_sched for details. |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 322 | * |
| 323 | * The domain tree of any CPU may only be accessed from within |
| 324 | * preempt-disabled sections. |
| 325 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 326 | #define for_each_domain(cpu, __sd) \ |
| 327 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
| 330 | #define this_rq() (&__get_cpu_var(runqueues)) |
| 331 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 332 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | #ifndef prepare_arch_switch |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 335 | # define prepare_arch_switch(next) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | #endif |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 337 | #ifndef finish_arch_switch |
| 338 | # define finish_arch_switch(prev) do { } while (0) |
| 339 | #endif |
| 340 | |
| 341 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 342 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 343 | { |
| 344 | return rq->curr == p; |
| 345 | } |
| 346 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 347 | 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] | 348 | { |
| 349 | } |
| 350 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 351 | 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] | 352 | { |
Ingo Molnar | da04c03 | 2005-09-13 11:17:59 +0200 | [diff] [blame] | 353 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 354 | /* this is a valid case when another task releases the spinlock */ |
| 355 | rq->lock.owner = current; |
| 356 | #endif |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 357 | /* |
| 358 | * If we are tracking spinlock dependencies then we have to |
| 359 | * fix up the runqueue lock - which gets 'carried over' from |
| 360 | * prev into current: |
| 361 | */ |
| 362 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); |
| 363 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 364 | spin_unlock_irq(&rq->lock); |
| 365 | } |
| 366 | |
| 367 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 368 | static inline int task_running(struct rq *rq, struct task_struct *p) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 369 | { |
| 370 | #ifdef CONFIG_SMP |
| 371 | return p->oncpu; |
| 372 | #else |
| 373 | return rq->curr == p; |
| 374 | #endif |
| 375 | } |
| 376 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 377 | 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] | 378 | { |
| 379 | #ifdef CONFIG_SMP |
| 380 | /* |
| 381 | * We can optimise this out completely for !SMP, because the |
| 382 | * SMP rebalancing from interrupt is the only thing that cares |
| 383 | * here. |
| 384 | */ |
| 385 | next->oncpu = 1; |
| 386 | #endif |
| 387 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 388 | spin_unlock_irq(&rq->lock); |
| 389 | #else |
| 390 | spin_unlock(&rq->lock); |
| 391 | #endif |
| 392 | } |
| 393 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 394 | 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] | 395 | { |
| 396 | #ifdef CONFIG_SMP |
| 397 | /* |
| 398 | * After ->oncpu is cleared, the task can be moved to a different CPU. |
| 399 | * We must ensure this doesn't happen until the switch is completely |
| 400 | * finished. |
| 401 | */ |
| 402 | smp_wmb(); |
| 403 | prev->oncpu = 0; |
| 404 | #endif |
| 405 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
| 406 | local_irq_enable(); |
| 407 | #endif |
| 408 | } |
| 409 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 412 | * __task_rq_lock - lock the runqueue a given task resides on. |
| 413 | * Must be called interrupts disabled. |
| 414 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 415 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 416 | __acquires(rq->lock) |
| 417 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 418 | struct rq *rq; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 419 | |
| 420 | repeat_lock_task: |
| 421 | rq = task_rq(p); |
| 422 | spin_lock(&rq->lock); |
| 423 | if (unlikely(rq != task_rq(p))) { |
| 424 | spin_unlock(&rq->lock); |
| 425 | goto repeat_lock_task; |
| 426 | } |
| 427 | return rq; |
| 428 | } |
| 429 | |
| 430 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | * task_rq_lock - lock the runqueue a given task resides on and disable |
| 432 | * interrupts. Note the ordering: we can safely lookup the task_rq without |
| 433 | * explicitly disabling preemption. |
| 434 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 435 | 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] | 436 | __acquires(rq->lock) |
| 437 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 438 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | repeat_lock_task: |
| 441 | local_irq_save(*flags); |
| 442 | rq = task_rq(p); |
| 443 | spin_lock(&rq->lock); |
| 444 | if (unlikely(rq != task_rq(p))) { |
| 445 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 446 | goto repeat_lock_task; |
| 447 | } |
| 448 | return rq; |
| 449 | } |
| 450 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 451 | static inline void __task_rq_unlock(struct rq *rq) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 452 | __releases(rq->lock) |
| 453 | { |
| 454 | spin_unlock(&rq->lock); |
| 455 | } |
| 456 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 457 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | __releases(rq->lock) |
| 459 | { |
| 460 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 461 | } |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* |
Robert P. J. Day | cc2a73b | 2006-12-10 02:20:00 -0800 | [diff] [blame] | 464 | * this_rq_lock - lock this runqueue and disable interrupts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 466 | static inline struct rq *this_rq_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | __acquires(rq->lock) |
| 468 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 469 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | local_irq_disable(); |
| 472 | rq = this_rq(); |
| 473 | spin_lock(&rq->lock); |
| 474 | |
| 475 | return rq; |
| 476 | } |
| 477 | |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame^] | 478 | #include "sched_stats.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | /* |
| 481 | * Adding/removing a task to/from a priority array: |
| 482 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 483 | static void dequeue_task(struct task_struct *p, struct prio_array *array) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
| 485 | array->nr_active--; |
| 486 | list_del(&p->run_list); |
| 487 | if (list_empty(array->queue + p->prio)) |
| 488 | __clear_bit(p->prio, array->bitmap); |
| 489 | } |
| 490 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 491 | static void enqueue_task(struct task_struct *p, struct prio_array *array) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
| 493 | sched_info_queued(p); |
| 494 | list_add_tail(&p->run_list, array->queue + p->prio); |
| 495 | __set_bit(p->prio, array->bitmap); |
| 496 | array->nr_active++; |
| 497 | p->array = array; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Put task to the end of the run list without the overhead of dequeue |
| 502 | * followed by enqueue. |
| 503 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 504 | static void requeue_task(struct task_struct *p, struct prio_array *array) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
| 506 | list_move_tail(&p->run_list, array->queue + p->prio); |
| 507 | } |
| 508 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 509 | static inline void |
| 510 | enqueue_task_head(struct task_struct *p, struct prio_array *array) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
| 512 | list_add(&p->run_list, array->queue + p->prio); |
| 513 | __set_bit(p->prio, array->bitmap); |
| 514 | array->nr_active++; |
| 515 | p->array = array; |
| 516 | } |
| 517 | |
| 518 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 519 | * __normal_prio - return the priority that is based on the static |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | * priority but is modified by bonuses/penalties. |
| 521 | * |
| 522 | * We scale the actual sleep average [0 .... MAX_SLEEP_AVG] |
| 523 | * into the -5 ... 0 ... +5 bonus/penalty range. |
| 524 | * |
| 525 | * We use 25% of the full 0...39 priority range so that: |
| 526 | * |
| 527 | * 1) nice +19 interactive tasks do not preempt nice 0 CPU hogs. |
| 528 | * 2) nice -20 CPU hogs do not get preempted by nice 0 tasks. |
| 529 | * |
| 530 | * Both properties are important to certain workloads. |
| 531 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 532 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 533 | static inline int __normal_prio(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
| 535 | int bonus, prio; |
| 536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | bonus = CURRENT_BONUS(p) - MAX_BONUS / 2; |
| 538 | |
| 539 | prio = p->static_prio - bonus; |
| 540 | if (prio < MAX_RT_PRIO) |
| 541 | prio = MAX_RT_PRIO; |
| 542 | if (prio > MAX_PRIO-1) |
| 543 | prio = MAX_PRIO-1; |
| 544 | return prio; |
| 545 | } |
| 546 | |
| 547 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 548 | * To aid in avoiding the subversion of "niceness" due to uneven distribution |
| 549 | * of tasks with abnormal "nice" values across CPUs the contribution that |
| 550 | * each task makes to its run queue's load is weighted according to its |
| 551 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a |
| 552 | * scaled version of the new time slice allocation that they receive on time |
| 553 | * slice expiry etc. |
| 554 | */ |
| 555 | |
| 556 | /* |
| 557 | * Assume: static_prio_timeslice(NICE_TO_PRIO(0)) == DEF_TIMESLICE |
| 558 | * If static_prio_timeslice() is ever changed to break this assumption then |
| 559 | * this code will need modification |
| 560 | */ |
| 561 | #define TIME_SLICE_NICE_ZERO DEF_TIMESLICE |
| 562 | #define LOAD_WEIGHT(lp) \ |
| 563 | (((lp) * SCHED_LOAD_SCALE) / TIME_SLICE_NICE_ZERO) |
| 564 | #define PRIO_TO_LOAD_WEIGHT(prio) \ |
| 565 | LOAD_WEIGHT(static_prio_timeslice(prio)) |
| 566 | #define RTPRIO_TO_LOAD_WEIGHT(rp) \ |
| 567 | (PRIO_TO_LOAD_WEIGHT(MAX_RT_PRIO) + LOAD_WEIGHT(rp)) |
| 568 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 569 | static void set_load_weight(struct task_struct *p) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 570 | { |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 571 | if (has_rt_policy(p)) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 572 | #ifdef CONFIG_SMP |
| 573 | if (p == task_rq(p)->migration_thread) |
| 574 | /* |
| 575 | * The migration thread does the actual balancing. |
| 576 | * Giving its load any weight will skew balancing |
| 577 | * adversely. |
| 578 | */ |
| 579 | p->load_weight = 0; |
| 580 | else |
| 581 | #endif |
| 582 | p->load_weight = RTPRIO_TO_LOAD_WEIGHT(p->rt_priority); |
| 583 | } else |
| 584 | p->load_weight = PRIO_TO_LOAD_WEIGHT(p->static_prio); |
| 585 | } |
| 586 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 587 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 588 | inc_raw_weighted_load(struct rq *rq, const struct task_struct *p) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 589 | { |
| 590 | rq->raw_weighted_load += p->load_weight; |
| 591 | } |
| 592 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 593 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 594 | dec_raw_weighted_load(struct rq *rq, const struct task_struct *p) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 595 | { |
| 596 | rq->raw_weighted_load -= p->load_weight; |
| 597 | } |
| 598 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 599 | 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] | 600 | { |
| 601 | rq->nr_running++; |
| 602 | inc_raw_weighted_load(rq, p); |
| 603 | } |
| 604 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 605 | 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] | 606 | { |
| 607 | rq->nr_running--; |
| 608 | dec_raw_weighted_load(rq, p); |
| 609 | } |
| 610 | |
| 611 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 612 | * Calculate the expected normal priority: i.e. priority |
| 613 | * without taking RT-inheritance into account. Might be |
| 614 | * boosted by interactivity modifiers. Changes upon fork, |
| 615 | * setprio syscalls, and whenever the interactivity |
| 616 | * estimator recalculates. |
| 617 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 618 | static inline int normal_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 619 | { |
| 620 | int prio; |
| 621 | |
| 622 | if (has_rt_policy(p)) |
| 623 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
| 624 | else |
| 625 | prio = __normal_prio(p); |
| 626 | return prio; |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Calculate the current priority, i.e. the priority |
| 631 | * taken into account by the scheduler. This value might |
| 632 | * be boosted by RT tasks, or might be boosted by |
| 633 | * interactivity modifiers. Will be RT if the task got |
| 634 | * RT-boosted. If not then it returns p->normal_prio. |
| 635 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 636 | static int effective_prio(struct task_struct *p) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 637 | { |
| 638 | p->normal_prio = normal_prio(p); |
| 639 | /* |
| 640 | * If we are RT tasks or we were boosted to RT priority, |
| 641 | * keep the priority unchanged. Otherwise, update priority |
| 642 | * to the normal priority: |
| 643 | */ |
| 644 | if (!rt_prio(p->prio)) |
| 645 | return p->normal_prio; |
| 646 | return p->prio; |
| 647 | } |
| 648 | |
| 649 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | * __activate_task - move a task to the runqueue. |
| 651 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 652 | static void __activate_task(struct task_struct *p, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 654 | struct prio_array *target = rq->active; |
Con Kolivas | d425b27 | 2006-03-31 02:31:29 -0800 | [diff] [blame] | 655 | |
Linus Torvalds | f1adad7 | 2006-05-21 18:54:09 -0700 | [diff] [blame] | 656 | if (batch_task(p)) |
Con Kolivas | d425b27 | 2006-03-31 02:31:29 -0800 | [diff] [blame] | 657 | target = rq->expired; |
| 658 | enqueue_task(p, target); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 659 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /* |
| 663 | * __activate_idle_task - move idle task to the _front_ of runqueue. |
| 664 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 665 | 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] | 666 | { |
| 667 | enqueue_task_head(p, rq->active); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 668 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 671 | /* |
| 672 | * Recalculate p->normal_prio and p->prio after having slept, |
| 673 | * updating the sleep-average too: |
| 674 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 675 | 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] | 676 | { |
| 677 | /* Caller must always ensure 'now >= p->timestamp' */ |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 678 | unsigned long sleep_time = now - p->timestamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
Con Kolivas | d425b27 | 2006-03-31 02:31:29 -0800 | [diff] [blame] | 680 | if (batch_task(p)) |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 681 | sleep_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
| 683 | if (likely(sleep_time > 0)) { |
| 684 | /* |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 685 | * This ceiling is set to the lowest priority that would allow |
| 686 | * a task to be reinserted into the active array on timeslice |
| 687 | * completion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | */ |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 689 | unsigned long ceiling = INTERACTIVE_SLEEP(p); |
Con Kolivas | e72ff0b | 2006-03-31 02:31:26 -0800 | [diff] [blame] | 690 | |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 691 | if (p->mm && sleep_time > ceiling && p->sleep_avg < ceiling) { |
| 692 | /* |
| 693 | * Prevents user tasks from achieving best priority |
| 694 | * with one single large enough sleep. |
| 695 | */ |
| 696 | p->sleep_avg = ceiling; |
| 697 | /* |
| 698 | * Using INTERACTIVE_SLEEP() as a ceiling places a |
| 699 | * nice(0) task 1ms sleep away from promotion, and |
| 700 | * gives it 700ms to round-robin with no chance of |
| 701 | * being demoted. This is more than generous, so |
| 702 | * mark this sleep as non-interactive to prevent the |
| 703 | * on-runqueue bonus logic from intervening should |
| 704 | * this task not receive cpu immediately. |
| 705 | */ |
| 706 | p->sleep_type = SLEEP_NONINTERACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } else { |
| 708 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | * Tasks waking from uninterruptible sleep are |
| 710 | * limited in their sleep_avg rise as they |
| 711 | * are likely to be waiting on I/O |
| 712 | */ |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 713 | if (p->sleep_type == SLEEP_NONINTERACTIVE && p->mm) { |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 714 | if (p->sleep_avg >= ceiling) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | sleep_time = 0; |
| 716 | else if (p->sleep_avg + sleep_time >= |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 717 | ceiling) { |
| 718 | p->sleep_avg = ceiling; |
| 719 | sleep_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * This code gives a bonus to interactive tasks. |
| 725 | * |
| 726 | * The boost works by updating the 'average sleep time' |
| 727 | * value here, based on ->timestamp. The more time a |
| 728 | * task spends sleeping, the higher the average gets - |
| 729 | * and the higher the priority boost gets as well. |
| 730 | */ |
| 731 | p->sleep_avg += sleep_time; |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
Con Kolivas | 72d2854 | 2006-06-27 02:54:30 -0700 | [diff] [blame] | 734 | if (p->sleep_avg > NS_MAX_SLEEP_AVG) |
| 735 | p->sleep_avg = NS_MAX_SLEEP_AVG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Chen Shang | a3464a1 | 2005-06-25 14:57:31 -0700 | [diff] [blame] | 738 | return effective_prio(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* |
| 742 | * activate_task - move a task to the runqueue and do priority recalculation |
| 743 | * |
| 744 | * Update all the scheduling statistics stuff. (sleep average |
| 745 | * calculation, priority modifiers, etc.) |
| 746 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 747 | 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] | 748 | { |
| 749 | unsigned long long now; |
| 750 | |
Chen, Kenneth W | 62ab616 | 2006-12-10 02:20:36 -0800 | [diff] [blame] | 751 | if (rt_task(p)) |
| 752 | goto out; |
| 753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | now = sched_clock(); |
| 755 | #ifdef CONFIG_SMP |
| 756 | if (!local) { |
| 757 | /* Compensate for drifting sched_clock */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 758 | struct rq *this_rq = this_rq(); |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 759 | now = (now - this_rq->most_recent_timestamp) |
| 760 | + rq->most_recent_timestamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | } |
| 762 | #endif |
| 763 | |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 764 | /* |
| 765 | * Sleep time is in units of nanosecs, so shift by 20 to get a |
| 766 | * milliseconds-range estimation of the amount of time that the task |
| 767 | * spent sleeping: |
| 768 | */ |
| 769 | if (unlikely(prof_on == SLEEP_PROFILING)) { |
| 770 | if (p->state == TASK_UNINTERRUPTIBLE) |
| 771 | profile_hits(SLEEP_PROFILING, (void *)get_wchan(p), |
| 772 | (now - p->timestamp) >> 20); |
| 773 | } |
| 774 | |
Chen, Kenneth W | 62ab616 | 2006-12-10 02:20:36 -0800 | [diff] [blame] | 775 | p->prio = recalc_task_prio(p, now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | /* |
| 778 | * This checks to make sure it's not an uninterruptible task |
| 779 | * that is now waking up. |
| 780 | */ |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 781 | if (p->sleep_type == SLEEP_NORMAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | /* |
| 783 | * Tasks which were woken up by interrupts (ie. hw events) |
| 784 | * are most likely of interactive nature. So we give them |
| 785 | * the credit of extending their sleep time to the period |
| 786 | * of time they spend on the runqueue, waiting for execution |
| 787 | * on a CPU, first time around: |
| 788 | */ |
| 789 | if (in_interrupt()) |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 790 | p->sleep_type = SLEEP_INTERRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | else { |
| 792 | /* |
| 793 | * Normal first-time wakeups get a credit too for |
| 794 | * on-runqueue time, but it will be weighted down: |
| 795 | */ |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 796 | p->sleep_type = SLEEP_INTERACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | p->timestamp = now; |
Chen, Kenneth W | 62ab616 | 2006-12-10 02:20:36 -0800 | [diff] [blame] | 800 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | __activate_task(p, rq); |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * deactivate_task - remove a task from the runqueue. |
| 806 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 807 | static void deactivate_task(struct task_struct *p, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 809 | dec_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | dequeue_task(p, p->array); |
| 811 | p->array = NULL; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * resched_task - mark a task 'to be rescheduled now'. |
| 816 | * |
| 817 | * On UP this means the setting of the need_resched flag, on SMP it |
| 818 | * might also involve a cross-CPU call to trigger the scheduler on |
| 819 | * the target CPU. |
| 820 | */ |
| 821 | #ifdef CONFIG_SMP |
Andi Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 822 | |
| 823 | #ifndef tsk_is_polling |
| 824 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) |
| 825 | #endif |
| 826 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 827 | static void resched_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 829 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | assert_spin_locked(&task_rq(p)->lock); |
| 832 | |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 833 | if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED))) |
| 834 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 836 | set_tsk_thread_flag(p, TIF_NEED_RESCHED); |
| 837 | |
| 838 | cpu = task_cpu(p); |
| 839 | if (cpu == smp_processor_id()) |
| 840 | return; |
| 841 | |
Andi Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 842 | /* NEED_RESCHED must be visible before we test polling */ |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 843 | smp_mb(); |
Andi Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 844 | if (!tsk_is_polling(p)) |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 845 | smp_send_reschedule(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 847 | |
| 848 | static void resched_cpu(int cpu) |
| 849 | { |
| 850 | struct rq *rq = cpu_rq(cpu); |
| 851 | unsigned long flags; |
| 852 | |
| 853 | if (!spin_trylock_irqsave(&rq->lock, flags)) |
| 854 | return; |
| 855 | resched_task(cpu_curr(cpu)); |
| 856 | spin_unlock_irqrestore(&rq->lock, flags); |
| 857 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | #else |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 859 | static inline void resched_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 861 | assert_spin_locked(&task_rq(p)->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | set_tsk_need_resched(p); |
| 863 | } |
| 864 | #endif |
| 865 | |
| 866 | /** |
| 867 | * task_curr - is this task currently executing on a CPU? |
| 868 | * @p: the task in question. |
| 869 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 870 | inline int task_curr(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { |
| 872 | return cpu_curr(task_cpu(p)) == p; |
| 873 | } |
| 874 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 875 | /* Used instead of source_load when we know the type == 0 */ |
| 876 | unsigned long weighted_cpuload(const int cpu) |
| 877 | { |
| 878 | return cpu_rq(cpu)->raw_weighted_load; |
| 879 | } |
| 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | #ifdef CONFIG_SMP |
Ingo Molnar | c65cc87 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 882 | |
| 883 | void set_task_cpu(struct task_struct *p, unsigned int cpu) |
| 884 | { |
| 885 | task_thread_info(p)->cpu = cpu; |
| 886 | } |
| 887 | |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 888 | struct migration_req { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 891 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | int dest_cpu; |
| 893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | struct completion done; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 895 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | |
| 897 | /* |
| 898 | * The task's runqueue lock must be held. |
| 899 | * Returns true if you have to wait for migration thread. |
| 900 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 901 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 902 | 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] | 903 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 904 | struct rq *rq = task_rq(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
| 906 | /* |
| 907 | * If the task is not on a runqueue (and not running), then |
| 908 | * it is sufficient to simply update the task's cpu field. |
| 909 | */ |
| 910 | if (!p->array && !task_running(rq, p)) { |
| 911 | set_task_cpu(p, dest_cpu); |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | init_completion(&req->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | req->task = p; |
| 917 | req->dest_cpu = dest_cpu; |
| 918 | list_add(&req->list, &rq->migration_queue); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | return 1; |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * wait_task_inactive - wait for a thread to unschedule. |
| 925 | * |
| 926 | * The caller must ensure that the task *will* unschedule sometime soon, |
| 927 | * else this function might spin for a *long* time. This function can't |
| 928 | * be called with interrupts off, or it may introduce deadlock with |
| 929 | * smp_call_function() if an IPI is sent by the same process we are |
| 930 | * waiting to become inactive. |
| 931 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 932 | void wait_task_inactive(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | { |
| 934 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 935 | struct rq *rq; |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 936 | struct prio_array *array; |
| 937 | int running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
| 939 | repeat: |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 940 | /* |
| 941 | * We do the initial early heuristics without holding |
| 942 | * any task-queue locks at all. We'll only try to get |
| 943 | * the runqueue lock when things look like they will |
| 944 | * work out! |
| 945 | */ |
| 946 | rq = task_rq(p); |
| 947 | |
| 948 | /* |
| 949 | * If the task is actively running on another CPU |
| 950 | * still, just relax and busy-wait without holding |
| 951 | * any locks. |
| 952 | * |
| 953 | * NOTE! Since we don't hold any locks, it's not |
| 954 | * even sure that "rq" stays as the right runqueue! |
| 955 | * But we don't care, since "task_running()" will |
| 956 | * return false if the runqueue has changed and p |
| 957 | * is actually now running somewhere else! |
| 958 | */ |
| 959 | while (task_running(rq, p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | cpu_relax(); |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 961 | |
| 962 | /* |
| 963 | * Ok, time to look more closely! We need the rq |
| 964 | * lock now, to be *sure*. If we're wrong, we'll |
| 965 | * just go back and repeat. |
| 966 | */ |
| 967 | rq = task_rq_lock(p, &flags); |
| 968 | running = task_running(rq, p); |
| 969 | array = p->array; |
| 970 | task_rq_unlock(rq, &flags); |
| 971 | |
| 972 | /* |
| 973 | * Was it really running after all now that we |
| 974 | * checked with the proper locks actually held? |
| 975 | * |
| 976 | * Oops. Go back and try again.. |
| 977 | */ |
| 978 | if (unlikely(running)) { |
| 979 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | goto repeat; |
| 981 | } |
Linus Torvalds | fa490cf | 2007-06-18 09:34:40 -0700 | [diff] [blame] | 982 | |
| 983 | /* |
| 984 | * It's not enough that it's not actively running, |
| 985 | * it must be off the runqueue _entirely_, and not |
| 986 | * preempted! |
| 987 | * |
| 988 | * So if it wa still runnable (but just not actively |
| 989 | * running right now), it's preempted, and we should |
| 990 | * yield - it could be a while. |
| 991 | */ |
| 992 | if (unlikely(array)) { |
| 993 | yield(); |
| 994 | goto repeat; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Ahh, all good. It wasn't running, and it wasn't |
| 999 | * runnable, which means that it will never become |
| 1000 | * running in the future either. We're all done! |
| 1001 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /*** |
| 1005 | * kick_process - kick a running thread to enter/exit the kernel |
| 1006 | * @p: the to-be-kicked thread |
| 1007 | * |
| 1008 | * Cause a process which is running on another CPU to enter |
| 1009 | * kernel-mode, without any delay. (to get signals handled.) |
| 1010 | * |
| 1011 | * NOTE: this function doesnt have to take the runqueue lock, |
| 1012 | * because all it wants to ensure is that the remote task enters |
| 1013 | * the kernel. If the IPI races and the task has been migrated |
| 1014 | * to another CPU then no harm is done and the purpose has been |
| 1015 | * achieved as well. |
| 1016 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1017 | void kick_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | { |
| 1019 | int cpu; |
| 1020 | |
| 1021 | preempt_disable(); |
| 1022 | cpu = task_cpu(p); |
| 1023 | if ((cpu != smp_processor_id()) && task_curr(p)) |
| 1024 | smp_send_reschedule(cpu); |
| 1025 | preempt_enable(); |
| 1026 | } |
| 1027 | |
| 1028 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1029 | * Return a low guess at the load of a migration-source cpu weighted |
| 1030 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | * |
| 1032 | * We want to under-estimate the load of migration sources, to |
| 1033 | * balance conservatively. |
| 1034 | */ |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1035 | static inline unsigned long source_load(int cpu, int type) |
| 1036 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1037 | struct rq *rq = cpu_rq(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1038 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1039 | if (type == 0) |
| 1040 | return rq->raw_weighted_load; |
| 1041 | |
| 1042 | return min(rq->cpu_load[type-1], rq->raw_weighted_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1046 | * Return a high guess at the load of a migration-target cpu weighted |
| 1047 | * according to the scheduling class and "nice" value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | */ |
Con Kolivas | b910472 | 2005-11-08 21:38:55 -0800 | [diff] [blame] | 1049 | static inline unsigned long target_load(int cpu, int type) |
| 1050 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1051 | struct rq *rq = cpu_rq(cpu); |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 1052 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1053 | if (type == 0) |
| 1054 | return rq->raw_weighted_load; |
| 1055 | |
| 1056 | return max(rq->cpu_load[type-1], rq->raw_weighted_load); |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * Return the average load per task on the cpu's run queue |
| 1061 | */ |
| 1062 | static inline unsigned long cpu_avg_load_per_task(int cpu) |
| 1063 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1064 | struct rq *rq = cpu_rq(cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1065 | unsigned long n = rq->nr_running; |
| 1066 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 1067 | return n ? rq->raw_weighted_load / n : SCHED_LOAD_SCALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1070 | /* |
| 1071 | * find_idlest_group finds and returns the least busy CPU group within the |
| 1072 | * domain. |
| 1073 | */ |
| 1074 | static struct sched_group * |
| 1075 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) |
| 1076 | { |
| 1077 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; |
| 1078 | unsigned long min_load = ULONG_MAX, this_load = 0; |
| 1079 | int load_idx = sd->forkexec_idx; |
| 1080 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
| 1081 | |
| 1082 | do { |
| 1083 | unsigned long load, avg_load; |
| 1084 | int local_group; |
| 1085 | int i; |
| 1086 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1087 | /* Skip over this group if it has no CPUs allowed */ |
| 1088 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) |
| 1089 | goto nextgroup; |
| 1090 | |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1091 | local_group = cpu_isset(this_cpu, group->cpumask); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1092 | |
| 1093 | /* Tally up the load of all CPUs in the group */ |
| 1094 | avg_load = 0; |
| 1095 | |
| 1096 | for_each_cpu_mask(i, group->cpumask) { |
| 1097 | /* Bias balancing toward cpus of our domain */ |
| 1098 | if (local_group) |
| 1099 | load = source_load(i, load_idx); |
| 1100 | else |
| 1101 | load = target_load(i, load_idx); |
| 1102 | |
| 1103 | avg_load += load; |
| 1104 | } |
| 1105 | |
| 1106 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 1107 | avg_load = sg_div_cpu_power(group, |
| 1108 | avg_load * SCHED_LOAD_SCALE); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1109 | |
| 1110 | if (local_group) { |
| 1111 | this_load = avg_load; |
| 1112 | this = group; |
| 1113 | } else if (avg_load < min_load) { |
| 1114 | min_load = avg_load; |
| 1115 | idlest = group; |
| 1116 | } |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1117 | nextgroup: |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1118 | group = group->next; |
| 1119 | } while (group != sd->groups); |
| 1120 | |
| 1121 | if (!idlest || 100*this_load < imbalance*min_load) |
| 1122 | return NULL; |
| 1123 | return idlest; |
| 1124 | } |
| 1125 | |
| 1126 | /* |
Satoru Takeuchi | 0feaece | 2006-10-03 01:14:10 -0700 | [diff] [blame] | 1127 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1128 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 1129 | static int |
| 1130 | 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] | 1131 | { |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1132 | cpumask_t tmp; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1133 | unsigned long load, min_load = ULONG_MAX; |
| 1134 | int idlest = -1; |
| 1135 | int i; |
| 1136 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1137 | /* Traverse only the allowed CPUs */ |
| 1138 | cpus_and(tmp, group->cpumask, p->cpus_allowed); |
| 1139 | |
| 1140 | for_each_cpu_mask(i, tmp) { |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1141 | load = weighted_cpuload(i); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1142 | |
| 1143 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 1144 | min_load = load; |
| 1145 | idlest = i; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | return idlest; |
| 1150 | } |
| 1151 | |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1152 | /* |
| 1153 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 1154 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 1155 | * SD_BALANCE_EXEC. |
| 1156 | * |
| 1157 | * Balance, ie. select the least loaded group. |
| 1158 | * |
| 1159 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 1160 | * |
| 1161 | * preempt must be disabled. |
| 1162 | */ |
| 1163 | static int sched_balance_self(int cpu, int flag) |
| 1164 | { |
| 1165 | struct task_struct *t = current; |
| 1166 | struct sched_domain *tmp, *sd = NULL; |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1167 | |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1168 | for_each_domain(cpu, tmp) { |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 1169 | /* |
| 1170 | * If power savings logic is enabled for a domain, stop there. |
| 1171 | */ |
| 1172 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
| 1173 | break; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1174 | if (tmp->flags & flag) |
| 1175 | sd = tmp; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1176 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1177 | |
| 1178 | while (sd) { |
| 1179 | cpumask_t span; |
| 1180 | struct sched_group *group; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1181 | int new_cpu, weight; |
| 1182 | |
| 1183 | if (!(sd->flags & flag)) { |
| 1184 | sd = sd->child; |
| 1185 | continue; |
| 1186 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1187 | |
| 1188 | span = sd->span; |
| 1189 | group = find_idlest_group(sd, t, cpu); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1190 | if (!group) { |
| 1191 | sd = sd->child; |
| 1192 | continue; |
| 1193 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1194 | |
M.Baris Demiray | da5a552 | 2005-09-10 00:26:09 -0700 | [diff] [blame] | 1195 | new_cpu = find_idlest_cpu(group, t, cpu); |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1196 | if (new_cpu == -1 || new_cpu == cpu) { |
| 1197 | /* Now try balancing at a lower domain level of cpu */ |
| 1198 | sd = sd->child; |
| 1199 | continue; |
| 1200 | } |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1201 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 1202 | /* Now try balancing at a lower domain level of new_cpu */ |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1203 | cpu = new_cpu; |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1204 | sd = NULL; |
| 1205 | weight = cpus_weight(span); |
| 1206 | for_each_domain(cpu, tmp) { |
| 1207 | if (weight <= cpus_weight(tmp->span)) |
| 1208 | break; |
| 1209 | if (tmp->flags & flag) |
| 1210 | sd = tmp; |
| 1211 | } |
| 1212 | /* while loop will break here if sd == NULL */ |
| 1213 | } |
| 1214 | |
| 1215 | return cpu; |
| 1216 | } |
| 1217 | |
| 1218 | #endif /* CONFIG_SMP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
| 1220 | /* |
| 1221 | * wake_idle() will wake a task on an idle cpu if task->cpu is |
| 1222 | * not idle and an idle cpu is available. The span of cpus to |
| 1223 | * search starts with cpus closest then further out as needed, |
| 1224 | * so we always favor a closer, idle cpu. |
| 1225 | * |
| 1226 | * Returns the CPU we should wake onto. |
| 1227 | */ |
| 1228 | #if defined(ARCH_HAS_SCHED_WAKE_IDLE) |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1229 | static int wake_idle(int cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | { |
| 1231 | cpumask_t tmp; |
| 1232 | struct sched_domain *sd; |
| 1233 | int i; |
| 1234 | |
Siddha, Suresh B | 4953198 | 2007-05-08 00:33:01 -0700 | [diff] [blame] | 1235 | /* |
| 1236 | * If it is idle, then it is the best cpu to run this task. |
| 1237 | * |
| 1238 | * This cpu is also the best, if it has more than one task already. |
| 1239 | * Siblings must be also busy(in most cases) as they didn't already |
| 1240 | * pickup the extra load from this cpu and hence we need not check |
| 1241 | * sibling runqueue info. This will avoid the checks and cache miss |
| 1242 | * penalities associated with that. |
| 1243 | */ |
| 1244 | if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | return cpu; |
| 1246 | |
| 1247 | for_each_domain(cpu, sd) { |
| 1248 | if (sd->flags & SD_WAKE_IDLE) { |
Nick Piggin | e0f364f | 2005-06-25 14:57:06 -0700 | [diff] [blame] | 1249 | cpus_and(tmp, sd->span, p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | for_each_cpu_mask(i, tmp) { |
| 1251 | if (idle_cpu(i)) |
| 1252 | return i; |
| 1253 | } |
| 1254 | } |
Nick Piggin | e0f364f | 2005-06-25 14:57:06 -0700 | [diff] [blame] | 1255 | else |
| 1256 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | } |
| 1258 | return cpu; |
| 1259 | } |
| 1260 | #else |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1261 | static inline int wake_idle(int cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | { |
| 1263 | return cpu; |
| 1264 | } |
| 1265 | #endif |
| 1266 | |
| 1267 | /*** |
| 1268 | * try_to_wake_up - wake up a thread |
| 1269 | * @p: the to-be-woken-up thread |
| 1270 | * @state: the mask of task states that can be woken |
| 1271 | * @sync: do a synchronous wakeup? |
| 1272 | * |
| 1273 | * Put it on the run-queue if it's not already there. The "current" |
| 1274 | * thread is always on the run-queue (except when the actual |
| 1275 | * re-schedule is in progress), and as such you're allowed to do |
| 1276 | * the simpler "current->state = TASK_RUNNING" to mark yourself |
| 1277 | * runnable without the overhead of this. |
| 1278 | * |
| 1279 | * returns failure only if the task is already active. |
| 1280 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1281 | 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] | 1282 | { |
| 1283 | int cpu, this_cpu, success = 0; |
| 1284 | unsigned long flags; |
| 1285 | long old_state; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1286 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | #ifdef CONFIG_SMP |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1288 | struct sched_domain *sd, *this_sd = NULL; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1289 | unsigned long load, this_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | int new_cpu; |
| 1291 | #endif |
| 1292 | |
| 1293 | rq = task_rq_lock(p, &flags); |
| 1294 | old_state = p->state; |
| 1295 | if (!(old_state & state)) |
| 1296 | goto out; |
| 1297 | |
| 1298 | if (p->array) |
| 1299 | goto out_running; |
| 1300 | |
| 1301 | cpu = task_cpu(p); |
| 1302 | this_cpu = smp_processor_id(); |
| 1303 | |
| 1304 | #ifdef CONFIG_SMP |
| 1305 | if (unlikely(task_running(rq, p))) |
| 1306 | goto out_activate; |
| 1307 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1308 | new_cpu = cpu; |
| 1309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | schedstat_inc(rq, ttwu_cnt); |
| 1311 | if (cpu == this_cpu) { |
| 1312 | schedstat_inc(rq, ttwu_local); |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1313 | goto out_set_cpu; |
| 1314 | } |
| 1315 | |
| 1316 | for_each_domain(this_cpu, sd) { |
| 1317 | if (cpu_isset(cpu, sd->span)) { |
| 1318 | schedstat_inc(sd, ttwu_wake_remote); |
| 1319 | this_sd = sd; |
| 1320 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | } |
| 1322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1324 | if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | goto out_set_cpu; |
| 1326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | /* |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1328 | * Check for affine wakeup and passive balancing possibilities. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | */ |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1330 | if (this_sd) { |
| 1331 | int idx = this_sd->wake_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | unsigned int imbalance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1334 | imbalance = 100 + (this_sd->imbalance_pct - 100) / 2; |
| 1335 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1336 | load = source_load(cpu, idx); |
| 1337 | this_load = target_load(this_cpu, idx); |
| 1338 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 1339 | new_cpu = this_cpu; /* Wake to this CPU if we can */ |
| 1340 | |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1341 | if (this_sd->flags & SD_WAKE_AFFINE) { |
| 1342 | unsigned long tl = this_load; |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 1343 | unsigned long tl_per_task; |
| 1344 | |
| 1345 | tl_per_task = cpu_avg_load_per_task(this_cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | /* |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1348 | * If sync wakeup then subtract the (maximum possible) |
| 1349 | * effect of the currently running task from the load |
| 1350 | * of the current CPU: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | */ |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1352 | if (sync) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1353 | tl -= current->load_weight; |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1354 | |
| 1355 | if ((tl <= load && |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1356 | tl + target_load(cpu, idx) <= tl_per_task) || |
| 1357 | 100*(tl + p->load_weight) <= imbalance*load) { |
Nick Piggin | a3f21bc | 2005-06-25 14:57:15 -0700 | [diff] [blame] | 1358 | /* |
| 1359 | * This domain has SD_WAKE_AFFINE and |
| 1360 | * p is cache cold in this domain, and |
| 1361 | * there is no bad imbalance. |
| 1362 | */ |
| 1363 | schedstat_inc(this_sd, ttwu_move_affine); |
| 1364 | goto out_set_cpu; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | /* |
| 1369 | * Start passive balancing when half the imbalance_pct |
| 1370 | * limit is reached. |
| 1371 | */ |
| 1372 | if (this_sd->flags & SD_WAKE_BALANCE) { |
| 1373 | if (imbalance*this_load <= 100*load) { |
| 1374 | schedstat_inc(this_sd, ttwu_move_balance); |
| 1375 | goto out_set_cpu; |
| 1376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */ |
| 1381 | out_set_cpu: |
| 1382 | new_cpu = wake_idle(new_cpu, p); |
| 1383 | if (new_cpu != cpu) { |
| 1384 | set_task_cpu(p, new_cpu); |
| 1385 | task_rq_unlock(rq, &flags); |
| 1386 | /* might preempt at this point */ |
| 1387 | rq = task_rq_lock(p, &flags); |
| 1388 | old_state = p->state; |
| 1389 | if (!(old_state & state)) |
| 1390 | goto out; |
| 1391 | if (p->array) |
| 1392 | goto out_running; |
| 1393 | |
| 1394 | this_cpu = smp_processor_id(); |
| 1395 | cpu = task_cpu(p); |
| 1396 | } |
| 1397 | |
| 1398 | out_activate: |
| 1399 | #endif /* CONFIG_SMP */ |
| 1400 | if (old_state == TASK_UNINTERRUPTIBLE) { |
| 1401 | rq->nr_uninterruptible--; |
| 1402 | /* |
| 1403 | * Tasks on involuntary sleep don't earn |
| 1404 | * sleep_avg beyond just interactive state. |
| 1405 | */ |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 1406 | p->sleep_type = SLEEP_NONINTERACTIVE; |
Con Kolivas | e7c38cb | 2006-03-31 02:31:25 -0800 | [diff] [blame] | 1407 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
| 1409 | /* |
Ingo Molnar | d79fc0f | 2005-09-10 00:26:12 -0700 | [diff] [blame] | 1410 | * Tasks that have marked their sleep as noninteractive get |
Con Kolivas | e7c38cb | 2006-03-31 02:31:25 -0800 | [diff] [blame] | 1411 | * woken up with their sleep average not weighted in an |
| 1412 | * interactive way. |
Ingo Molnar | d79fc0f | 2005-09-10 00:26:12 -0700 | [diff] [blame] | 1413 | */ |
Con Kolivas | e7c38cb | 2006-03-31 02:31:25 -0800 | [diff] [blame] | 1414 | if (old_state & TASK_NONINTERACTIVE) |
| 1415 | p->sleep_type = SLEEP_NONINTERACTIVE; |
| 1416 | |
| 1417 | |
| 1418 | activate_task(p, rq, cpu == this_cpu); |
Ingo Molnar | d79fc0f | 2005-09-10 00:26:12 -0700 | [diff] [blame] | 1419 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | * Sync wakeups (i.e. those types of wakeups where the waker |
| 1421 | * has indicated that it will leave the CPU in short order) |
| 1422 | * don't trigger a preemption, if the woken up task will run on |
| 1423 | * this cpu. (in this case the 'I will reschedule' promise of |
| 1424 | * the waker guarantees that the freshly woken up task is going |
| 1425 | * to be considered on this CPU.) |
| 1426 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | if (!sync || cpu != this_cpu) { |
| 1428 | if (TASK_PREEMPTS_CURR(p, rq)) |
| 1429 | resched_task(rq->curr); |
| 1430 | } |
| 1431 | success = 1; |
| 1432 | |
| 1433 | out_running: |
| 1434 | p->state = TASK_RUNNING; |
| 1435 | out: |
| 1436 | task_rq_unlock(rq, &flags); |
| 1437 | |
| 1438 | return success; |
| 1439 | } |
| 1440 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1441 | int fastcall wake_up_process(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | { |
| 1443 | return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED | |
| 1444 | TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0); |
| 1445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | EXPORT_SYMBOL(wake_up_process); |
| 1447 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1448 | int fastcall wake_up_state(struct task_struct *p, unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | { |
| 1450 | return try_to_wake_up(p, state, 0); |
| 1451 | } |
| 1452 | |
Peter Williams | bc94763 | 2006-12-19 12:48:50 +1000 | [diff] [blame] | 1453 | static void task_running_tick(struct rq *rq, struct task_struct *p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | /* |
| 1455 | * Perform scheduler related setup for a newly forked process p. |
| 1456 | * p is forked by current. |
| 1457 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1458 | void fastcall sched_fork(struct task_struct *p, int clone_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | { |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1460 | int cpu = get_cpu(); |
| 1461 | |
| 1462 | #ifdef CONFIG_SMP |
| 1463 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); |
| 1464 | #endif |
| 1465 | set_task_cpu(p, cpu); |
| 1466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | /* |
| 1468 | * We mark the process as running here, but have not actually |
| 1469 | * inserted it onto the runqueue yet. This guarantees that |
| 1470 | * nobody will actually run it, and a signal or other external |
| 1471 | * event cannot wake it up and insert it on the runqueue either. |
| 1472 | */ |
| 1473 | p->state = TASK_RUNNING; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1474 | |
| 1475 | /* |
| 1476 | * Make sure we do not leak PI boosting priority to the child: |
| 1477 | */ |
| 1478 | p->prio = current->normal_prio; |
| 1479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | INIT_LIST_HEAD(&p->run_list); |
| 1481 | p->array = NULL; |
Chandra Seetharaman | 52f17b6 | 2006-07-14 00:24:38 -0700 | [diff] [blame] | 1482 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
| 1483 | if (unlikely(sched_info_on())) |
| 1484 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | #endif |
Chen, Kenneth W | d6077cb | 2006-02-14 13:53:10 -0800 | [diff] [blame] | 1486 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1487 | p->oncpu = 0; |
| 1488 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | #ifdef CONFIG_PREEMPT |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1490 | /* Want to start with kernel preemption disabled. */ |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 1491 | task_thread_info(p)->preempt_count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | #endif |
| 1493 | /* |
| 1494 | * Share the timeslice between parent and child, thus the |
| 1495 | * total amount of pending timeslices in the system doesn't change, |
| 1496 | * resulting in more scheduling fairness. |
| 1497 | */ |
| 1498 | local_irq_disable(); |
| 1499 | p->time_slice = (current->time_slice + 1) >> 1; |
| 1500 | /* |
| 1501 | * The remainder of the first timeslice might be recovered by |
| 1502 | * the parent if the child exits early enough. |
| 1503 | */ |
| 1504 | p->first_time_slice = 1; |
| 1505 | current->time_slice >>= 1; |
| 1506 | p->timestamp = sched_clock(); |
| 1507 | if (unlikely(!current->time_slice)) { |
| 1508 | /* |
| 1509 | * This case is rare, it happens when the parent has only |
| 1510 | * a single jiffy left from its timeslice. Taking the |
| 1511 | * runqueue lock is not a problem. |
| 1512 | */ |
| 1513 | current->time_slice = 1; |
Peter Williams | bc94763 | 2006-12-19 12:48:50 +1000 | [diff] [blame] | 1514 | task_running_tick(cpu_rq(cpu), current); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1515 | } |
| 1516 | local_irq_enable(); |
| 1517 | put_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * wake_up_new_task - wake up a newly created task for the first time. |
| 1522 | * |
| 1523 | * This function will do some initial scheduler statistics housekeeping |
| 1524 | * that must be done for every newly created context, then puts the task |
| 1525 | * on the runqueue and wakes it. |
| 1526 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1527 | 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] | 1528 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1529 | struct rq *rq, *this_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | unsigned long flags; |
| 1531 | int this_cpu, cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
| 1533 | rq = task_rq_lock(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | BUG_ON(p->state != TASK_RUNNING); |
Nick Piggin | 147cbb4 | 2005-06-25 14:57:19 -0700 | [diff] [blame] | 1535 | this_cpu = smp_processor_id(); |
| 1536 | cpu = task_cpu(p); |
| 1537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | /* |
| 1539 | * We decrease the sleep average of forking parents |
| 1540 | * and children as well, to keep max-interactive tasks |
| 1541 | * from forking tasks that are max-interactive. The parent |
| 1542 | * (current) is done further down, under its lock. |
| 1543 | */ |
| 1544 | p->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(p) * |
| 1545 | CHILD_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); |
| 1546 | |
| 1547 | p->prio = effective_prio(p); |
| 1548 | |
| 1549 | if (likely(cpu == this_cpu)) { |
| 1550 | if (!(clone_flags & CLONE_VM)) { |
| 1551 | /* |
| 1552 | * The VM isn't cloned, so we're in a good position to |
| 1553 | * do child-runs-first in anticipation of an exec. This |
| 1554 | * usually avoids a lot of COW overhead. |
| 1555 | */ |
| 1556 | if (unlikely(!current->array)) |
| 1557 | __activate_task(p, rq); |
| 1558 | else { |
| 1559 | p->prio = current->prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1560 | p->normal_prio = current->normal_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | list_add_tail(&p->run_list, ¤t->run_list); |
| 1562 | p->array = current->array; |
| 1563 | p->array->nr_active++; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1564 | inc_nr_running(p, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | } |
| 1566 | set_need_resched(); |
| 1567 | } else |
| 1568 | /* Run child last */ |
| 1569 | __activate_task(p, rq); |
| 1570 | /* |
| 1571 | * We skip the following code due to cpu == this_cpu |
| 1572 | * |
| 1573 | * task_rq_unlock(rq, &flags); |
| 1574 | * this_rq = task_rq_lock(current, &flags); |
| 1575 | */ |
| 1576 | this_rq = rq; |
| 1577 | } else { |
| 1578 | this_rq = cpu_rq(this_cpu); |
| 1579 | |
| 1580 | /* |
| 1581 | * Not the local CPU - must adjust timestamp. This should |
| 1582 | * get optimised away in the !CONFIG_SMP case. |
| 1583 | */ |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 1584 | p->timestamp = (p->timestamp - this_rq->most_recent_timestamp) |
| 1585 | + rq->most_recent_timestamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | __activate_task(p, rq); |
| 1587 | if (TASK_PREEMPTS_CURR(p, rq)) |
| 1588 | resched_task(rq->curr); |
| 1589 | |
| 1590 | /* |
| 1591 | * Parent and child are on different CPUs, now get the |
| 1592 | * parent runqueue to update the parent's ->sleep_avg: |
| 1593 | */ |
| 1594 | task_rq_unlock(rq, &flags); |
| 1595 | this_rq = task_rq_lock(current, &flags); |
| 1596 | } |
| 1597 | current->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(current) * |
| 1598 | PARENT_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); |
| 1599 | task_rq_unlock(this_rq, &flags); |
| 1600 | } |
| 1601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | /** |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1603 | * prepare_task_switch - prepare to switch tasks |
| 1604 | * @rq: the runqueue preparing to switch |
| 1605 | * @next: the task we are going to switch to. |
| 1606 | * |
| 1607 | * This is called with the rq lock held and interrupts off. It must |
| 1608 | * be paired with a subsequent finish_task_switch after the context |
| 1609 | * switch. |
| 1610 | * |
| 1611 | * prepare_task_switch sets up locking and calls architecture specific |
| 1612 | * hooks. |
| 1613 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1614 | 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] | 1615 | { |
| 1616 | prepare_lock_switch(rq, next); |
| 1617 | prepare_arch_switch(next); |
| 1618 | } |
| 1619 | |
| 1620 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | * finish_task_switch - clean up after a task-switch |
Jeff Garzik | 344baba | 2005-09-07 01:15:17 -0400 | [diff] [blame] | 1622 | * @rq: runqueue associated with task-switch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | * @prev: the thread we just switched away from. |
| 1624 | * |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1625 | * finish_task_switch must be called after the context switch, paired |
| 1626 | * with a prepare_task_switch call before the context switch. |
| 1627 | * finish_task_switch will reconcile locking set up by prepare_task_switch, |
| 1628 | * and do any other architecture-specific cleanup actions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | * |
| 1630 | * Note that we may have delayed dropping an mm in context_switch(). If |
| 1631 | * so, we finish that here outside of the runqueue lock. (Doing it |
| 1632 | * with the lock held can cause deadlocks; see schedule() for |
| 1633 | * details.) |
| 1634 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1635 | 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] | 1636 | __releases(rq->lock) |
| 1637 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | struct mm_struct *mm = rq->prev_mm; |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 1639 | long prev_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | |
| 1641 | rq->prev_mm = NULL; |
| 1642 | |
| 1643 | /* |
| 1644 | * A task struct has one reference for the use as "current". |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 1645 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 1646 | * schedule one last time. The schedule call will never return, and |
| 1647 | * the scheduled task must drop that reference. |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 1648 | * The test for TASK_DEAD must occur while the runqueue locks are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | * still held, otherwise prev could be scheduled on another cpu, die |
| 1650 | * there before we look at prev->state, and then the reference would |
| 1651 | * be dropped twice. |
| 1652 | * Manfred Spraul <manfred@colorfullife.com> |
| 1653 | */ |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 1654 | prev_state = prev->state; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1655 | finish_arch_switch(prev); |
| 1656 | finish_lock_switch(rq, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | if (mm) |
| 1658 | mmdrop(mm); |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 1659 | if (unlikely(prev_state == TASK_DEAD)) { |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 1660 | /* |
| 1661 | * Remove function-return probe instances associated with this |
| 1662 | * task and put them back on the free list. |
| 1663 | */ |
| 1664 | kprobe_flush_task(prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | put_task_struct(prev); |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 1666 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /** |
| 1670 | * schedule_tail - first thing a freshly forked thread must call. |
| 1671 | * @prev: the thread we just switched away from. |
| 1672 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1673 | asmlinkage void schedule_tail(struct task_struct *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | __releases(rq->lock) |
| 1675 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1676 | struct rq *rq = this_rq(); |
| 1677 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 1678 | finish_task_switch(rq, prev); |
| 1679 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW |
| 1680 | /* In this case, finish_task_switch does not reenable preemption */ |
| 1681 | preempt_enable(); |
| 1682 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | if (current->set_child_tid) |
| 1684 | put_user(current->pid, current->set_child_tid); |
| 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * context_switch - switch to the new MM and the new |
| 1689 | * thread's register state. |
| 1690 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1691 | static inline struct task_struct * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1692 | context_switch(struct rq *rq, struct task_struct *prev, |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1693 | struct task_struct *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | { |
| 1695 | struct mm_struct *mm = next->mm; |
| 1696 | struct mm_struct *oldmm = prev->active_mm; |
| 1697 | |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 1698 | /* |
| 1699 | * For paravirt, this is coupled with an exit in switch_to to |
| 1700 | * combine the page table reload and the switch backend into |
| 1701 | * one hypercall. |
| 1702 | */ |
| 1703 | arch_enter_lazy_cpu_mode(); |
| 1704 | |
Nick Piggin | beed33a | 2006-10-11 01:21:52 -0700 | [diff] [blame] | 1705 | if (!mm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | next->active_mm = oldmm; |
| 1707 | atomic_inc(&oldmm->mm_count); |
| 1708 | enter_lazy_tlb(oldmm, next); |
| 1709 | } else |
| 1710 | switch_mm(oldmm, mm, next); |
| 1711 | |
Nick Piggin | beed33a | 2006-10-11 01:21:52 -0700 | [diff] [blame] | 1712 | if (!prev->mm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | prev->active_mm = NULL; |
| 1714 | WARN_ON(rq->prev_mm); |
| 1715 | rq->prev_mm = oldmm; |
| 1716 | } |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 1717 | /* |
| 1718 | * Since the runqueue lock will be released by the next |
| 1719 | * task (which is an invalid locking op but in the case |
| 1720 | * of the scheduler it's an obvious special-case), so we |
| 1721 | * do an early lockdep release here: |
| 1722 | */ |
| 1723 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 1724 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Ingo Molnar | 3a5f5e4 | 2006-07-14 00:24:27 -0700 | [diff] [blame] | 1725 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
| 1727 | /* Here we just switch the register state and the stack. */ |
| 1728 | switch_to(prev, next, prev); |
| 1729 | |
| 1730 | return prev; |
| 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * nr_running, nr_uninterruptible and nr_context_switches: |
| 1735 | * |
| 1736 | * externally visible scheduler statistics: current number of runnable |
| 1737 | * threads, current number of uninterruptible-sleeping threads, total |
| 1738 | * number of context switches performed since bootup. |
| 1739 | */ |
| 1740 | unsigned long nr_running(void) |
| 1741 | { |
| 1742 | unsigned long i, sum = 0; |
| 1743 | |
| 1744 | for_each_online_cpu(i) |
| 1745 | sum += cpu_rq(i)->nr_running; |
| 1746 | |
| 1747 | return sum; |
| 1748 | } |
| 1749 | |
| 1750 | unsigned long nr_uninterruptible(void) |
| 1751 | { |
| 1752 | unsigned long i, sum = 0; |
| 1753 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 1754 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | sum += cpu_rq(i)->nr_uninterruptible; |
| 1756 | |
| 1757 | /* |
| 1758 | * Since we read the counters lockless, it might be slightly |
| 1759 | * inaccurate. Do not allow it to go below zero though: |
| 1760 | */ |
| 1761 | if (unlikely((long)sum < 0)) |
| 1762 | sum = 0; |
| 1763 | |
| 1764 | return sum; |
| 1765 | } |
| 1766 | |
| 1767 | unsigned long long nr_context_switches(void) |
| 1768 | { |
Steven Rostedt | cc94abf | 2006-06-27 02:54:31 -0700 | [diff] [blame] | 1769 | int i; |
| 1770 | unsigned long long sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 1772 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | sum += cpu_rq(i)->nr_switches; |
| 1774 | |
| 1775 | return sum; |
| 1776 | } |
| 1777 | |
| 1778 | unsigned long nr_iowait(void) |
| 1779 | { |
| 1780 | unsigned long i, sum = 0; |
| 1781 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 1782 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
| 1784 | |
| 1785 | return sum; |
| 1786 | } |
| 1787 | |
Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 1788 | unsigned long nr_active(void) |
| 1789 | { |
| 1790 | unsigned long i, running = 0, uninterruptible = 0; |
| 1791 | |
| 1792 | for_each_online_cpu(i) { |
| 1793 | running += cpu_rq(i)->nr_running; |
| 1794 | uninterruptible += cpu_rq(i)->nr_uninterruptible; |
| 1795 | } |
| 1796 | |
| 1797 | if (unlikely((long)uninterruptible < 0)) |
| 1798 | uninterruptible = 0; |
| 1799 | |
| 1800 | return running + uninterruptible; |
| 1801 | } |
| 1802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | #ifdef CONFIG_SMP |
| 1804 | |
| 1805 | /* |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 1806 | * Is this task likely cache-hot: |
| 1807 | */ |
| 1808 | static inline int |
| 1809 | task_hot(struct task_struct *p, unsigned long long now, struct sched_domain *sd) |
| 1810 | { |
| 1811 | return (long long)(now - p->last_ran) < (long long)sd->cache_hot_time; |
| 1812 | } |
| 1813 | |
| 1814 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | * double_rq_lock - safely lock two runqueues |
| 1816 | * |
| 1817 | * Note this does not disable interrupts like task_rq_lock, |
| 1818 | * you need to do so manually before calling. |
| 1819 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1820 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | __acquires(rq1->lock) |
| 1822 | __acquires(rq2->lock) |
| 1823 | { |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 1824 | BUG_ON(!irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | if (rq1 == rq2) { |
| 1826 | spin_lock(&rq1->lock); |
| 1827 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 1828 | } else { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1829 | if (rq1 < rq2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | spin_lock(&rq1->lock); |
| 1831 | spin_lock(&rq2->lock); |
| 1832 | } else { |
| 1833 | spin_lock(&rq2->lock); |
| 1834 | spin_lock(&rq1->lock); |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * double_rq_unlock - safely unlock two runqueues |
| 1841 | * |
| 1842 | * Note this does not restore interrupts like task_rq_unlock, |
| 1843 | * you need to do so manually after calling. |
| 1844 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1845 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | __releases(rq1->lock) |
| 1847 | __releases(rq2->lock) |
| 1848 | { |
| 1849 | spin_unlock(&rq1->lock); |
| 1850 | if (rq1 != rq2) |
| 1851 | spin_unlock(&rq2->lock); |
| 1852 | else |
| 1853 | __release(rq2->lock); |
| 1854 | } |
| 1855 | |
| 1856 | /* |
| 1857 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 1858 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1859 | static void double_lock_balance(struct rq *this_rq, struct rq *busiest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | __releases(this_rq->lock) |
| 1861 | __acquires(busiest->lock) |
| 1862 | __acquires(this_rq->lock) |
| 1863 | { |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 1864 | if (unlikely(!irqs_disabled())) { |
| 1865 | /* printk() doesn't work good under rq->lock */ |
| 1866 | spin_unlock(&this_rq->lock); |
| 1867 | BUG_ON(1); |
| 1868 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | if (unlikely(!spin_trylock(&busiest->lock))) { |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 1870 | if (busiest < this_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | spin_unlock(&this_rq->lock); |
| 1872 | spin_lock(&busiest->lock); |
| 1873 | spin_lock(&this_rq->lock); |
| 1874 | } else |
| 1875 | spin_lock(&busiest->lock); |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | * If dest_cpu is allowed for this process, migrate the task to it. |
| 1881 | * This is accomplished by forcing the cpu_allowed mask to only |
| 1882 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then |
| 1883 | * the cpu_allowed mask is restored. |
| 1884 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1885 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1887 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1889 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | |
| 1891 | rq = task_rq_lock(p, &flags); |
| 1892 | if (!cpu_isset(dest_cpu, p->cpus_allowed) |
| 1893 | || unlikely(cpu_is_offline(dest_cpu))) |
| 1894 | goto out; |
| 1895 | |
| 1896 | /* force the process onto the specified CPU */ |
| 1897 | if (migrate_task(p, dest_cpu, &req)) { |
| 1898 | /* Need to wait for migration thread (might exit: take ref). */ |
| 1899 | struct task_struct *mt = rq->migration_thread; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | get_task_struct(mt); |
| 1902 | task_rq_unlock(rq, &flags); |
| 1903 | wake_up_process(mt); |
| 1904 | put_task_struct(mt); |
| 1905 | wait_for_completion(&req.done); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | return; |
| 1908 | } |
| 1909 | out: |
| 1910 | task_rq_unlock(rq, &flags); |
| 1911 | } |
| 1912 | |
| 1913 | /* |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1914 | * sched_exec - execve() is a valuable balancing opportunity, because at |
| 1915 | * this point the task has the smallest effective memory and cache footprint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | */ |
| 1917 | void sched_exec(void) |
| 1918 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | int new_cpu, this_cpu = get_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1920 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | put_cpu(); |
Nick Piggin | 476d139 | 2005-06-25 14:57:29 -0700 | [diff] [blame] | 1922 | if (new_cpu != this_cpu) |
| 1923 | sched_migrate_task(current, new_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | /* |
| 1927 | * pull_task - move a task from a remote runqueue to the local runqueue. |
| 1928 | * Both runqueues must be locked. |
| 1929 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1930 | static void pull_task(struct rq *src_rq, struct prio_array *src_array, |
| 1931 | struct task_struct *p, struct rq *this_rq, |
| 1932 | struct prio_array *this_array, int this_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | { |
| 1934 | dequeue_task(p, src_array); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1935 | dec_nr_running(p, src_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | set_task_cpu(p, this_cpu); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1937 | inc_nr_running(p, this_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | enqueue_task(p, this_array); |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 1939 | p->timestamp = (p->timestamp - src_rq->most_recent_timestamp) |
| 1940 | + this_rq->most_recent_timestamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | /* |
| 1942 | * Note that idle threads have a prio of MAX_PRIO, for this test |
| 1943 | * to be always true for them. |
| 1944 | */ |
| 1945 | if (TASK_PREEMPTS_CURR(p, this_rq)) |
| 1946 | resched_task(this_rq->curr); |
| 1947 | } |
| 1948 | |
| 1949 | /* |
| 1950 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 1951 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1952 | static |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1953 | int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 1954 | struct sched_domain *sd, enum cpu_idle_type idle, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 1955 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | { |
| 1957 | /* |
| 1958 | * We do not migrate tasks that are: |
| 1959 | * 1) running (obviously), or |
| 1960 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 1961 | * 3) are cache-hot on their current CPU. |
| 1962 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | if (!cpu_isset(this_cpu, p->cpus_allowed)) |
| 1964 | return 0; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1965 | *all_pinned = 0; |
| 1966 | |
| 1967 | if (task_running(rq, p)) |
| 1968 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | |
| 1970 | /* |
| 1971 | * Aggressive migration if: |
Nick Piggin | cafb20c | 2005-06-25 14:57:17 -0700 | [diff] [blame] | 1972 | * 1) task is cache cold, or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | * 2) too many balance attempts have failed. |
| 1974 | */ |
| 1975 | |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 1976 | if (sd->nr_balance_failed > sd->cache_nice_tries) { |
| 1977 | #ifdef CONFIG_SCHEDSTATS |
| 1978 | if (task_hot(p, rq->most_recent_timestamp, sd)) |
| 1979 | schedstat_inc(sd, lb_hot_gained[idle]); |
| 1980 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | return 1; |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 1982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 1984 | if (task_hot(p, rq->most_recent_timestamp, sd)) |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1985 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | return 1; |
| 1987 | } |
| 1988 | |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 1989 | #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] | 1990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | /* |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 1992 | * move_tasks tries to move up to max_nr_move tasks and max_load_move weighted |
| 1993 | * load from busiest to this_rq, as part of a balancing operation within |
| 1994 | * "domain". Returns the number of tasks moved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | * |
| 1996 | * Called with both runqueues locked. |
| 1997 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 1998 | 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] | 1999 | unsigned long max_nr_move, unsigned long max_load_move, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2000 | struct sched_domain *sd, enum cpu_idle_type idle, |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2001 | int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2003 | int idx, pulled = 0, pinned = 0, this_best_prio, best_prio, |
| 2004 | best_prio_seen, skip_for_load; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2005 | struct prio_array *array, *dst_array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | struct list_head *head, *curr; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2007 | struct task_struct *tmp; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2008 | long rem_load_move; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2010 | if (max_nr_move == 0 || max_load_move == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | goto out; |
| 2012 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2013 | rem_load_move = max_load_move; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2014 | pinned = 1; |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2015 | this_best_prio = rq_best_prio(this_rq); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2016 | best_prio = rq_best_prio(busiest); |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2017 | /* |
| 2018 | * Enable handling of the case where there is more than one task |
| 2019 | * with the best priority. If the current running task is one |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2020 | * 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] | 2021 | * and therefore it's safe to override the skip (based on load) of |
| 2022 | * any task we find with that prio. |
| 2023 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2024 | best_prio_seen = best_prio == busiest->curr->prio; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | /* |
| 2027 | * We first consider expired tasks. Those will likely not be |
| 2028 | * executed in the near future, and they are most likely to |
| 2029 | * be cache-cold, thus switching CPUs has the least effect |
| 2030 | * on them. |
| 2031 | */ |
| 2032 | if (busiest->expired->nr_active) { |
| 2033 | array = busiest->expired; |
| 2034 | dst_array = this_rq->expired; |
| 2035 | } else { |
| 2036 | array = busiest->active; |
| 2037 | dst_array = this_rq->active; |
| 2038 | } |
| 2039 | |
| 2040 | new_array: |
| 2041 | /* Start searching at priority 0: */ |
| 2042 | idx = 0; |
| 2043 | skip_bitmap: |
| 2044 | if (!idx) |
| 2045 | idx = sched_find_first_bit(array->bitmap); |
| 2046 | else |
| 2047 | idx = find_next_bit(array->bitmap, MAX_PRIO, idx); |
| 2048 | if (idx >= MAX_PRIO) { |
| 2049 | if (array == busiest->expired && busiest->active->nr_active) { |
| 2050 | array = busiest->active; |
| 2051 | dst_array = this_rq->active; |
| 2052 | goto new_array; |
| 2053 | } |
| 2054 | goto out; |
| 2055 | } |
| 2056 | |
| 2057 | head = array->queue + idx; |
| 2058 | curr = head->prev; |
| 2059 | skip_queue: |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 2060 | tmp = list_entry(curr, struct task_struct, run_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
| 2062 | curr = curr->prev; |
| 2063 | |
Peter Williams | 50ddd96 | 2006-06-27 02:54:36 -0700 | [diff] [blame] | 2064 | /* |
| 2065 | * To help distribute high priority tasks accross CPUs we don't |
| 2066 | * skip a task if it will be the highest priority task (i.e. smallest |
| 2067 | * prio value) on its new queue regardless of its load weight |
| 2068 | */ |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2069 | skip_for_load = tmp->load_weight > rem_load_move; |
| 2070 | if (skip_for_load && idx < this_best_prio) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2071 | skip_for_load = !best_prio_seen && idx == best_prio; |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2072 | if (skip_for_load || |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2073 | !can_migrate_task(tmp, busiest, this_cpu, sd, idle, &pinned)) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2074 | |
| 2075 | best_prio_seen |= idx == best_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | if (curr != head) |
| 2077 | goto skip_queue; |
| 2078 | idx++; |
| 2079 | goto skip_bitmap; |
| 2080 | } |
| 2081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu); |
| 2083 | pulled++; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2084 | rem_load_move -= tmp->load_weight; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2086 | /* |
| 2087 | * We only want to steal up to the prescribed number of tasks |
| 2088 | * and the prescribed amount of weighted load. |
| 2089 | */ |
| 2090 | if (pulled < max_nr_move && rem_load_move > 0) { |
Peter Williams | 615052d | 2006-06-27 02:54:37 -0700 | [diff] [blame] | 2091 | if (idx < this_best_prio) |
| 2092 | this_best_prio = idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | if (curr != head) |
| 2094 | goto skip_queue; |
| 2095 | idx++; |
| 2096 | goto skip_bitmap; |
| 2097 | } |
| 2098 | out: |
| 2099 | /* |
| 2100 | * Right now, this is the only place pull_task() is called, |
| 2101 | * so we can safely collect pull_task() stats here rather than |
| 2102 | * inside pull_task(). |
| 2103 | */ |
| 2104 | schedstat_add(sd, lb_gained[idle], pulled); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2105 | |
| 2106 | if (all_pinned) |
| 2107 | *all_pinned = pinned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | return pulled; |
| 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | * find_busiest_group finds and returns the busiest CPU group within the |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2113 | * domain. It calculates and returns the amount of weighted load which |
| 2114 | * should be moved to restore balance via the imbalance parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | */ |
| 2116 | static struct sched_group * |
| 2117 | find_busiest_group(struct sched_domain *sd, int this_cpu, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2118 | unsigned long *imbalance, enum cpu_idle_type idle, int *sd_idle, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2119 | cpumask_t *cpus, int *balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | { |
| 2121 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; |
| 2122 | 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] | 2123 | unsigned long max_pull; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2124 | unsigned long busiest_load_per_task, busiest_nr_running; |
| 2125 | unsigned long this_load_per_task, this_nr_running; |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2126 | int load_idx; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2127 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2128 | int power_savings_balance = 1; |
| 2129 | unsigned long leader_nr_running = 0, min_load_per_task = 0; |
| 2130 | unsigned long min_nr_running = ULONG_MAX; |
| 2131 | struct sched_group *group_min = NULL, *group_leader = NULL; |
| 2132 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | |
| 2134 | max_load = this_load = total_load = total_pwr = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2135 | busiest_load_per_task = busiest_nr_running = 0; |
| 2136 | this_load_per_task = this_nr_running = 0; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2137 | if (idle == CPU_NOT_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2138 | load_idx = sd->busy_idx; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2139 | else if (idle == CPU_NEWLY_IDLE) |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2140 | load_idx = sd->newidle_idx; |
| 2141 | else |
| 2142 | load_idx = sd->idle_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | |
| 2144 | do { |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2145 | unsigned long load, group_capacity; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | int local_group; |
| 2147 | int i; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2148 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2149 | unsigned long sum_nr_running, sum_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | |
| 2151 | local_group = cpu_isset(this_cpu, group->cpumask); |
| 2152 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2153 | if (local_group) |
| 2154 | balance_cpu = first_cpu(group->cpumask); |
| 2155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | /* Tally up the load of all CPUs in the group */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2157 | sum_weighted_load = sum_nr_running = avg_load = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | |
| 2159 | for_each_cpu_mask(i, group->cpumask) { |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2160 | struct rq *rq; |
| 2161 | |
| 2162 | if (!cpu_isset(i, *cpus)) |
| 2163 | continue; |
| 2164 | |
| 2165 | rq = cpu_rq(i); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2166 | |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2167 | if (*sd_idle && !idle_cpu(i)) |
| 2168 | *sd_idle = 0; |
| 2169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | /* Bias balancing toward cpus of our domain */ |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2171 | if (local_group) { |
| 2172 | if (idle_cpu(i) && !first_idle_cpu) { |
| 2173 | first_idle_cpu = 1; |
| 2174 | balance_cpu = i; |
| 2175 | } |
| 2176 | |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2177 | load = target_load(i, load_idx); |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2178 | } else |
Nick Piggin | a200057 | 2006-02-10 01:51:02 -0800 | [diff] [blame] | 2179 | load = source_load(i, load_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | |
| 2181 | avg_load += load; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2182 | sum_nr_running += rq->nr_running; |
| 2183 | sum_weighted_load += rq->raw_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2186 | /* |
| 2187 | * First idle cpu or the first cpu(busiest) in this sched group |
| 2188 | * is eligible for doing load balancing at this and above |
| 2189 | * domains. |
| 2190 | */ |
| 2191 | if (local_group && balance_cpu != this_cpu && balance) { |
| 2192 | *balance = 0; |
| 2193 | goto ret; |
| 2194 | } |
| 2195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | total_load += avg_load; |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2197 | total_pwr += group->__cpu_power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | |
| 2199 | /* Adjust by relative CPU power of the group */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2200 | avg_load = sg_div_cpu_power(group, |
| 2201 | avg_load * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2203 | group_capacity = group->__cpu_power / SCHED_LOAD_SCALE; |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | if (local_group) { |
| 2206 | this_load = avg_load; |
| 2207 | this = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2208 | this_nr_running = sum_nr_running; |
| 2209 | this_load_per_task = sum_weighted_load; |
| 2210 | } else if (avg_load > max_load && |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2211 | sum_nr_running > group_capacity) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | max_load = avg_load; |
| 2213 | busiest = group; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2214 | busiest_nr_running = sum_nr_running; |
| 2215 | busiest_load_per_task = sum_weighted_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2217 | |
| 2218 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2219 | /* |
| 2220 | * Busy processors will not participate in power savings |
| 2221 | * balance. |
| 2222 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2223 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2224 | goto group_next; |
| 2225 | |
| 2226 | /* |
| 2227 | * If the local group is idle or completely loaded |
| 2228 | * no need to do power savings balance at this domain |
| 2229 | */ |
| 2230 | if (local_group && (this_nr_running >= group_capacity || |
| 2231 | !this_nr_running)) |
| 2232 | power_savings_balance = 0; |
| 2233 | |
| 2234 | /* |
| 2235 | * If a group is already running at full capacity or idle, |
| 2236 | * don't include that group in power savings calculations |
| 2237 | */ |
| 2238 | if (!power_savings_balance || sum_nr_running >= group_capacity |
| 2239 | || !sum_nr_running) |
| 2240 | goto group_next; |
| 2241 | |
| 2242 | /* |
| 2243 | * Calculate the group which has the least non-idle load. |
| 2244 | * This is the group from where we need to pick up the load |
| 2245 | * for saving power |
| 2246 | */ |
| 2247 | if ((sum_nr_running < min_nr_running) || |
| 2248 | (sum_nr_running == min_nr_running && |
| 2249 | first_cpu(group->cpumask) < |
| 2250 | first_cpu(group_min->cpumask))) { |
| 2251 | group_min = group; |
| 2252 | min_nr_running = sum_nr_running; |
| 2253 | min_load_per_task = sum_weighted_load / |
| 2254 | sum_nr_running; |
| 2255 | } |
| 2256 | |
| 2257 | /* |
| 2258 | * Calculate the group which is almost near its |
| 2259 | * capacity but still has some space to pick up some load |
| 2260 | * from other group and save more power |
| 2261 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2262 | if (sum_nr_running <= group_capacity - 1) { |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2263 | if (sum_nr_running > leader_nr_running || |
| 2264 | (sum_nr_running == leader_nr_running && |
| 2265 | first_cpu(group->cpumask) > |
| 2266 | first_cpu(group_leader->cpumask))) { |
| 2267 | group_leader = group; |
| 2268 | leader_nr_running = sum_nr_running; |
| 2269 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2270 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2271 | group_next: |
| 2272 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | group = group->next; |
| 2274 | } while (group != sd->groups); |
| 2275 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2276 | if (!busiest || this_load >= max_load || busiest_nr_running == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | goto out_balanced; |
| 2278 | |
| 2279 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; |
| 2280 | |
| 2281 | if (this_load >= avg_load || |
| 2282 | 100*max_load <= sd->imbalance_pct*this_load) |
| 2283 | goto out_balanced; |
| 2284 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2285 | busiest_load_per_task /= busiest_nr_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | /* |
| 2287 | * We're trying to get all the cpus to the average_load, so we don't |
| 2288 | * want to push ourselves above the average load, nor do we wish to |
| 2289 | * reduce the max loaded cpu below the average load, as either of these |
| 2290 | * actions would just result in more rebalancing later, and ping-pong |
| 2291 | * tasks around. Thus we look for the minimum possible imbalance. |
| 2292 | * Negative imbalances (*we* are more loaded than anyone else) will |
| 2293 | * be counted as no imbalance for these purposes -- we can't fix that |
| 2294 | * by pulling tasks to us. Be careful of negative numbers as they'll |
| 2295 | * appear as very large values with unsigned longs. |
| 2296 | */ |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2297 | if (max_load <= busiest_load_per_task) |
| 2298 | goto out_balanced; |
| 2299 | |
| 2300 | /* |
| 2301 | * In the presence of smp nice balancing, certain scenarios can have |
| 2302 | * max load less than avg load(as we skip the groups at or below |
| 2303 | * its cpu_power, while calculating max_load..) |
| 2304 | */ |
| 2305 | if (max_load < avg_load) { |
| 2306 | *imbalance = 0; |
| 2307 | goto small_imbalance; |
| 2308 | } |
Siddha, Suresh B | 0c117f1 | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2309 | |
| 2310 | /* 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] | 2311 | 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] | 2312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | /* How much load to actually move to equalise the imbalance */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2314 | *imbalance = min(max_pull * busiest->__cpu_power, |
| 2315 | (avg_load - this_load) * this->__cpu_power) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | / SCHED_LOAD_SCALE; |
| 2317 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2318 | /* |
| 2319 | * if *imbalance is less than the average load per runnable task |
| 2320 | * there is no gaurantee that any tasks will be moved so we'll have |
| 2321 | * a think about bumping its value to force at least one task to be |
| 2322 | * moved |
| 2323 | */ |
| 2324 | if (*imbalance < busiest_load_per_task) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2325 | unsigned long tmp, pwr_now, pwr_move; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2326 | unsigned int imbn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2328 | small_imbalance: |
| 2329 | pwr_move = pwr_now = 0; |
| 2330 | imbn = 2; |
| 2331 | if (this_nr_running) { |
| 2332 | this_load_per_task /= this_nr_running; |
| 2333 | if (busiest_load_per_task > this_load_per_task) |
| 2334 | imbn = 1; |
| 2335 | } else |
| 2336 | this_load_per_task = SCHED_LOAD_SCALE; |
| 2337 | |
| 2338 | if (max_load - this_load >= busiest_load_per_task * imbn) { |
| 2339 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | return busiest; |
| 2341 | } |
| 2342 | |
| 2343 | /* |
| 2344 | * OK, we don't have enough imbalance to justify moving tasks, |
| 2345 | * however we may be able to increase total CPU power used by |
| 2346 | * moving them. |
| 2347 | */ |
| 2348 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2349 | pwr_now += busiest->__cpu_power * |
| 2350 | min(busiest_load_per_task, max_load); |
| 2351 | pwr_now += this->__cpu_power * |
| 2352 | min(this_load_per_task, this_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | pwr_now /= SCHED_LOAD_SCALE; |
| 2354 | |
| 2355 | /* Amount of load we'd subtract */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2356 | tmp = sg_div_cpu_power(busiest, |
| 2357 | busiest_load_per_task * SCHED_LOAD_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | if (max_load > tmp) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2359 | pwr_move += busiest->__cpu_power * |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2360 | min(busiest_load_per_task, max_load - tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | |
| 2362 | /* Amount of load we'd add */ |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2363 | if (max_load * busiest->__cpu_power < |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 2364 | busiest_load_per_task * SCHED_LOAD_SCALE) |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2365 | tmp = sg_div_cpu_power(this, |
| 2366 | max_load * busiest->__cpu_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | else |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 2368 | tmp = sg_div_cpu_power(this, |
| 2369 | busiest_load_per_task * SCHED_LOAD_SCALE); |
| 2370 | pwr_move += this->__cpu_power * |
| 2371 | min(this_load_per_task, this_load + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | pwr_move /= SCHED_LOAD_SCALE; |
| 2373 | |
| 2374 | /* Move if we gain throughput */ |
| 2375 | if (pwr_move <= pwr_now) |
| 2376 | goto out_balanced; |
| 2377 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2378 | *imbalance = busiest_load_per_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | } |
| 2380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | return busiest; |
| 2382 | |
| 2383 | out_balanced: |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2384 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2385 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2386 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2388 | if (this == group_leader && group_leader != group_min) { |
| 2389 | *imbalance = min_load_per_task; |
| 2390 | return group_min; |
| 2391 | } |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2392 | #endif |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2393 | ret: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | *imbalance = 0; |
| 2395 | return NULL; |
| 2396 | } |
| 2397 | |
| 2398 | /* |
| 2399 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 2400 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2401 | static struct rq * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2402 | find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2403 | unsigned long imbalance, cpumask_t *cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2405 | struct rq *busiest = NULL, *rq; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2406 | unsigned long max_load = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | int i; |
| 2408 | |
| 2409 | for_each_cpu_mask(i, group->cpumask) { |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2410 | |
| 2411 | if (!cpu_isset(i, *cpus)) |
| 2412 | continue; |
| 2413 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2414 | rq = cpu_rq(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2416 | if (rq->nr_running == 1 && rq->raw_weighted_load > imbalance) |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2417 | continue; |
| 2418 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2419 | if (rq->raw_weighted_load > max_load) { |
| 2420 | max_load = rq->raw_weighted_load; |
| 2421 | busiest = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | return busiest; |
| 2426 | } |
| 2427 | |
| 2428 | /* |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 2429 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 2430 | * so long as it is large enough. |
| 2431 | */ |
| 2432 | #define MAX_PINNED_INTERVAL 512 |
| 2433 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2434 | static inline unsigned long minus_1_or_zero(unsigned long n) |
| 2435 | { |
| 2436 | return n > 0 ? n - 1 : 0; |
| 2437 | } |
| 2438 | |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 2439 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 2441 | * tasks if there is an imbalance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2443 | static int load_balance(int this_cpu, struct rq *this_rq, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2444 | struct sched_domain *sd, enum cpu_idle_type idle, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2445 | int *balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2447 | int nr_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | struct sched_group *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | unsigned long imbalance; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2450 | struct rq *busiest; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2451 | cpumask_t cpus = CPU_MASK_ALL; |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 2452 | unsigned long flags; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2453 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 2454 | /* |
| 2455 | * When power savings policy is enabled for the parent domain, idle |
| 2456 | * sibling can pick up load irrespective of busy siblings. In this case, |
| 2457 | * let the state of idle sibling percolate up as IDLE, instead of |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2458 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 2459 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2460 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 2461 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2462 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | schedstat_inc(sd, lb_cnt[idle]); |
| 2465 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2466 | redo: |
| 2467 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2468 | &cpus, balance); |
| 2469 | |
Chen, Kenneth W | 0606671 | 2006-12-10 02:20:35 -0800 | [diff] [blame] | 2470 | if (*balance == 0) |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2471 | goto out_balanced; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | if (!group) { |
| 2474 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 2475 | goto out_balanced; |
| 2476 | } |
| 2477 | |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2478 | busiest = find_busiest_queue(group, idle, imbalance, &cpus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | if (!busiest) { |
| 2480 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 2481 | goto out_balanced; |
| 2482 | } |
| 2483 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 2484 | BUG_ON(busiest == this_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | |
| 2486 | schedstat_add(sd, lb_imbalance[idle], imbalance); |
| 2487 | |
| 2488 | nr_moved = 0; |
| 2489 | if (busiest->nr_running > 1) { |
| 2490 | /* |
| 2491 | * Attempt to move tasks. If find_busiest_group has found |
| 2492 | * an imbalance but busiest->nr_running <= 1, the group is |
| 2493 | * still unbalanced. nr_moved simply stays zero, so it is |
| 2494 | * correctly treated as an imbalance. |
| 2495 | */ |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 2496 | local_irq_save(flags); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 2497 | double_rq_lock(this_rq, busiest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | nr_moved = move_tasks(this_rq, this_cpu, busiest, |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2499 | minus_1_or_zero(busiest->nr_running), |
| 2500 | imbalance, sd, idle, &all_pinned); |
Nick Piggin | e17224b | 2005-09-10 00:26:18 -0700 | [diff] [blame] | 2501 | double_rq_unlock(this_rq, busiest); |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 2502 | local_irq_restore(flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2503 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2504 | /* |
| 2505 | * some other cpu did the load balance for us. |
| 2506 | */ |
| 2507 | if (nr_moved && this_cpu != smp_processor_id()) |
| 2508 | resched_cpu(this_cpu); |
| 2509 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2510 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2511 | if (unlikely(all_pinned)) { |
| 2512 | cpu_clear(cpu_of(busiest), cpus); |
| 2513 | if (!cpus_empty(cpus)) |
| 2514 | goto redo; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2515 | goto out_balanced; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2516 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | if (!nr_moved) { |
| 2520 | schedstat_inc(sd, lb_failed[idle]); |
| 2521 | sd->nr_balance_failed++; |
| 2522 | |
| 2523 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 2525 | spin_lock_irqsave(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2526 | |
| 2527 | /* don't kick the migration_thread, if the curr |
| 2528 | * task on busiest cpu can't be moved to this_cpu |
| 2529 | */ |
| 2530 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 2531 | spin_unlock_irqrestore(&busiest->lock, flags); |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2532 | all_pinned = 1; |
| 2533 | goto out_one_pinned; |
| 2534 | } |
| 2535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | if (!busiest->active_balance) { |
| 2537 | busiest->active_balance = 1; |
| 2538 | busiest->push_cpu = this_cpu; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2539 | active_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | } |
Christoph Lameter | fe2eea3 | 2006-12-10 02:20:21 -0800 | [diff] [blame] | 2541 | spin_unlock_irqrestore(&busiest->lock, flags); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2542 | if (active_balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | wake_up_process(busiest->migration_thread); |
| 2544 | |
| 2545 | /* |
| 2546 | * We've kicked active balancing, reset the failure |
| 2547 | * counter. |
| 2548 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2549 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2551 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | sd->nr_balance_failed = 0; |
| 2553 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2554 | if (likely(!active_balance)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | /* We were unbalanced, so reset the balancing interval */ |
| 2556 | sd->balance_interval = sd->min_interval; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2557 | } else { |
| 2558 | /* |
| 2559 | * If we've begun active balancing, start to back off. This |
| 2560 | * case may not be covered by the all_pinned logic if there |
| 2561 | * is only 1 task on the busy runqueue (because we don't call |
| 2562 | * move_tasks). |
| 2563 | */ |
| 2564 | if (sd->balance_interval < sd->max_interval) |
| 2565 | sd->balance_interval *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 2568 | if (!nr_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 2569 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2570 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | return nr_moved; |
| 2572 | |
| 2573 | out_balanced: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | schedstat_inc(sd, lb_balanced[idle]); |
| 2575 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2576 | sd->nr_balance_failed = 0; |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2577 | |
| 2578 | out_one_pinned: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | /* tune up the balancing interval */ |
Nick Piggin | 77391d7 | 2005-06-25 14:57:30 -0700 | [diff] [blame] | 2580 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
| 2581 | (sd->balance_interval < sd->max_interval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | sd->balance_interval *= 2; |
| 2583 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2584 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 2585 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2586 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | return 0; |
| 2588 | } |
| 2589 | |
| 2590 | /* |
| 2591 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 2592 | * tasks if there is an imbalance. |
| 2593 | * |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2594 | * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | * this_rq is locked. |
| 2596 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2597 | static int |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2598 | 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] | 2599 | { |
| 2600 | struct sched_group *group; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2601 | struct rq *busiest = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | unsigned long imbalance; |
| 2603 | int nr_moved = 0; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2604 | int sd_idle = 0; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2605 | cpumask_t cpus = CPU_MASK_ALL; |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2606 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 2607 | /* |
| 2608 | * When power savings policy is enabled for the parent domain, idle |
| 2609 | * sibling can pick up load irrespective of busy siblings. In this case, |
| 2610 | * let the state of idle sibling percolate up as IDLE, instead of |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2611 | * portraying it as CPU_NOT_IDLE. |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 2612 | */ |
| 2613 | if (sd->flags & SD_SHARE_CPUPOWER && |
| 2614 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2615 | sd_idle = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2617 | schedstat_inc(sd, lb_cnt[CPU_NEWLY_IDLE]); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2618 | redo: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2619 | group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2620 | &sd_idle, &cpus, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | if (!group) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2622 | schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2623 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | } |
| 2625 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2626 | busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2627 | &cpus); |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 2628 | if (!busiest) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2629 | schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2630 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | } |
| 2632 | |
Nick Piggin | db935db | 2005-06-25 14:57:11 -0700 | [diff] [blame] | 2633 | BUG_ON(busiest == this_rq); |
| 2634 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2635 | schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 2636 | |
| 2637 | nr_moved = 0; |
| 2638 | if (busiest->nr_running > 1) { |
| 2639 | /* Attempt to move tasks */ |
| 2640 | double_lock_balance(this_rq, busiest); |
| 2641 | nr_moved = move_tasks(this_rq, this_cpu, busiest, |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2642 | minus_1_or_zero(busiest->nr_running), |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2643 | imbalance, sd, CPU_NEWLY_IDLE, NULL); |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 2644 | spin_unlock(&busiest->lock); |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 2645 | |
| 2646 | if (!nr_moved) { |
| 2647 | cpu_clear(cpu_of(busiest), cpus); |
| 2648 | if (!cpus_empty(cpus)) |
| 2649 | goto redo; |
| 2650 | } |
Nick Piggin | d6d5cfa | 2005-09-10 00:26:16 -0700 | [diff] [blame] | 2651 | } |
| 2652 | |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2653 | if (!nr_moved) { |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2654 | schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 2655 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 2656 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2657 | return -1; |
| 2658 | } else |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2659 | sd->nr_balance_failed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | return nr_moved; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2662 | |
| 2663 | out_balanced: |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2664 | schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2665 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 2666 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2667 | return -1; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2668 | sd->nr_balance_failed = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2669 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2670 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | /* |
| 2674 | * idle_balance is called by schedule() if this_cpu is about to become |
| 2675 | * idle. Attempts to pull tasks from other CPUs. |
| 2676 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2677 | static void idle_balance(int this_cpu, struct rq *this_rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | { |
| 2679 | struct sched_domain *sd; |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 2680 | int pulled_task = 0; |
| 2681 | unsigned long next_balance = jiffies + 60 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | |
| 2683 | for_each_domain(this_cpu, sd) { |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 2684 | unsigned long interval; |
| 2685 | |
| 2686 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 2687 | continue; |
| 2688 | |
| 2689 | if (sd->flags & SD_BALANCE_NEWIDLE) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2690 | /* If we've pulled tasks over stop searching: */ |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 2691 | pulled_task = load_balance_newidle(this_cpu, |
Christoph Lameter | 92c4ca5 | 2007-06-23 17:16:33 -0700 | [diff] [blame] | 2692 | this_rq, sd); |
| 2693 | |
| 2694 | interval = msecs_to_jiffies(sd->balance_interval); |
| 2695 | if (time_after(next_balance, sd->last_balance + interval)) |
| 2696 | next_balance = sd->last_balance + interval; |
| 2697 | if (pulled_task) |
| 2698 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | } |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 2700 | if (!pulled_task) |
| 2701 | /* |
| 2702 | * We are going idle. next_balance may be set based on |
| 2703 | * a busy processor. So reset next_balance. |
| 2704 | */ |
| 2705 | this_rq->next_balance = next_balance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | /* |
| 2709 | * active_load_balance is run by migration threads. It pushes running tasks |
| 2710 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be |
| 2711 | * running on each physical CPU where possible, and avoids physical / |
| 2712 | * logical imbalances. |
| 2713 | * |
| 2714 | * Called with busiest_rq locked. |
| 2715 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2716 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2718 | int target_cpu = busiest_rq->push_cpu; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 2719 | struct sched_domain *sd; |
| 2720 | struct rq *target_rq; |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2721 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2722 | /* Is there any task to move? */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2723 | if (busiest_rq->nr_running <= 1) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2724 | return; |
| 2725 | |
| 2726 | target_rq = cpu_rq(target_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | |
| 2728 | /* |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2729 | * This condition is "impossible", if it occurs |
| 2730 | * we need to fix it. Originally reported by |
| 2731 | * Bjorn Helgaas on a 128-cpu setup. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2733 | BUG_ON(busiest_rq == target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2735 | /* move a task from busiest_rq to target_rq */ |
| 2736 | double_lock_balance(busiest_rq, target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2738 | /* Search for an sd spanning us and the target CPU. */ |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2739 | for_each_domain(target_cpu, sd) { |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2740 | if ((sd->flags & SD_LOAD_BALANCE) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2741 | cpu_isset(busiest_cpu, sd->span)) |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2742 | break; |
Chen, Kenneth W | c96d145 | 2006-06-27 02:54:28 -0700 | [diff] [blame] | 2743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2745 | if (likely(sd)) { |
| 2746 | schedstat_inc(sd, alb_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2748 | if (move_tasks(target_rq, target_cpu, busiest_rq, 1, |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2749 | RTPRIO_TO_LOAD_WEIGHT(100), sd, CPU_IDLE, |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2750 | NULL)) |
| 2751 | schedstat_inc(sd, alb_pushed); |
| 2752 | else |
| 2753 | schedstat_inc(sd, alb_failed); |
| 2754 | } |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame] | 2755 | spin_unlock(&target_rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | } |
| 2757 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 2758 | static void update_load(struct rq *this_rq) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2759 | { |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 2760 | unsigned long this_load; |
Nick Piggin | ff91691 | 2007-02-12 00:53:51 -0800 | [diff] [blame] | 2761 | unsigned int i, scale; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 2763 | this_load = this_rq->raw_weighted_load; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2764 | |
| 2765 | /* Update our load: */ |
Nick Piggin | ff91691 | 2007-02-12 00:53:51 -0800 | [diff] [blame] | 2766 | for (i = 0, scale = 1; i < 3; i++, scale += scale) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2767 | unsigned long old_load, new_load; |
| 2768 | |
Nick Piggin | ff91691 | 2007-02-12 00:53:51 -0800 | [diff] [blame] | 2769 | /* scale is effectively 1 << i now, and >> i divides by scale */ |
| 2770 | |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2771 | old_load = this_rq->cpu_load[i]; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 2772 | new_load = this_load; |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2773 | /* |
| 2774 | * Round up the averaging division if load is increasing. This |
| 2775 | * prevents us from getting stuck on 9 if the load is 10, for |
| 2776 | * example. |
| 2777 | */ |
| 2778 | if (new_load > old_load) |
| 2779 | new_load += scale-1; |
Nick Piggin | ff91691 | 2007-02-12 00:53:51 -0800 | [diff] [blame] | 2780 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 2781 | } |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 2782 | } |
| 2783 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2784 | #ifdef CONFIG_NO_HZ |
| 2785 | static struct { |
| 2786 | atomic_t load_balancer; |
| 2787 | cpumask_t cpu_mask; |
| 2788 | } nohz ____cacheline_aligned = { |
| 2789 | .load_balancer = ATOMIC_INIT(-1), |
| 2790 | .cpu_mask = CPU_MASK_NONE, |
| 2791 | }; |
| 2792 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 2793 | /* |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2794 | * This routine will try to nominate the ilb (idle load balancing) |
| 2795 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle |
| 2796 | * load balancing on behalf of all those cpus. If all the cpus in the system |
| 2797 | * go into this tickless mode, then there will be no ilb owner (as there is |
| 2798 | * no need for one) and all the cpus will sleep till the next wakeup event |
| 2799 | * arrives... |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 2800 | * |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2801 | * For the ilb owner, tick is not stopped. And this tick will be used |
| 2802 | * for idle load balancing. ilb owner will still be part of |
| 2803 | * nohz.cpu_mask.. |
| 2804 | * |
| 2805 | * While stopping the tick, this cpu will become the ilb owner if there |
| 2806 | * is no other owner. And will be the owner till that cpu becomes busy |
| 2807 | * or if all cpus in the system stop their ticks at which point |
| 2808 | * there is no need for ilb owner. |
| 2809 | * |
| 2810 | * When the ilb owner becomes busy, it nominates another owner, during the |
| 2811 | * next busy scheduler_tick() |
| 2812 | */ |
| 2813 | int select_nohz_load_balancer(int stop_tick) |
| 2814 | { |
| 2815 | int cpu = smp_processor_id(); |
| 2816 | |
| 2817 | if (stop_tick) { |
| 2818 | cpu_set(cpu, nohz.cpu_mask); |
| 2819 | cpu_rq(cpu)->in_nohz_recently = 1; |
| 2820 | |
| 2821 | /* |
| 2822 | * If we are going offline and still the leader, give up! |
| 2823 | */ |
| 2824 | if (cpu_is_offline(cpu) && |
| 2825 | atomic_read(&nohz.load_balancer) == cpu) { |
| 2826 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 2827 | BUG(); |
| 2828 | return 0; |
| 2829 | } |
| 2830 | |
| 2831 | /* time for ilb owner also to sleep */ |
| 2832 | if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 2833 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 2834 | atomic_set(&nohz.load_balancer, -1); |
| 2835 | return 0; |
| 2836 | } |
| 2837 | |
| 2838 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 2839 | /* make me the ilb owner */ |
| 2840 | if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) |
| 2841 | return 1; |
| 2842 | } else if (atomic_read(&nohz.load_balancer) == cpu) |
| 2843 | return 1; |
| 2844 | } else { |
| 2845 | if (!cpu_isset(cpu, nohz.cpu_mask)) |
| 2846 | return 0; |
| 2847 | |
| 2848 | cpu_clear(cpu, nohz.cpu_mask); |
| 2849 | |
| 2850 | if (atomic_read(&nohz.load_balancer) == cpu) |
| 2851 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) |
| 2852 | BUG(); |
| 2853 | } |
| 2854 | return 0; |
| 2855 | } |
| 2856 | #endif |
| 2857 | |
| 2858 | static DEFINE_SPINLOCK(balancing); |
| 2859 | |
| 2860 | /* |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 2861 | * It checks each scheduling domain to see if it is due to be balanced, |
| 2862 | * and initiates a balancing operation if so. |
| 2863 | * |
| 2864 | * Balancing parameters are set up in arch_init_sched_domains. |
| 2865 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2866 | static inline void rebalance_domains(int cpu, enum cpu_idle_type idle) |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 2867 | { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2868 | int balance = 1; |
| 2869 | struct rq *rq = cpu_rq(cpu); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 2870 | unsigned long interval; |
| 2871 | struct sched_domain *sd; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2872 | /* Earliest time when we have to do rebalance again */ |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 2873 | unsigned long next_balance = jiffies + 60*HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2875 | for_each_domain(cpu, sd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 2877 | continue; |
| 2878 | |
| 2879 | interval = sd->balance_interval; |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2880 | if (idle != CPU_IDLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | interval *= sd->busy_factor; |
| 2882 | |
| 2883 | /* scale ms to jiffies */ |
| 2884 | interval = msecs_to_jiffies(interval); |
| 2885 | if (unlikely(!interval)) |
| 2886 | interval = 1; |
| 2887 | |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 2888 | if (sd->flags & SD_SERIALIZE) { |
| 2889 | if (!spin_trylock(&balancing)) |
| 2890 | goto out; |
| 2891 | } |
| 2892 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 2893 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2894 | if (load_balance(cpu, rq, sd, idle, &balance)) { |
Siddha, Suresh B | fa3b6dd | 2005-09-10 00:26:21 -0700 | [diff] [blame] | 2895 | /* |
| 2896 | * We've pulled tasks over so either we're no |
Nick Piggin | 5969fe0 | 2005-09-10 00:26:19 -0700 | [diff] [blame] | 2897 | * longer idle, or one of our SMT siblings is |
| 2898 | * not idle. |
| 2899 | */ |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2900 | idle = CPU_NOT_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | } |
Christoph Lameter | 1bd77f2 | 2006-12-10 02:20:27 -0800 | [diff] [blame] | 2902 | sd->last_balance = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | } |
Christoph Lameter | 08c183f | 2006-12-10 02:20:29 -0800 | [diff] [blame] | 2904 | if (sd->flags & SD_SERIALIZE) |
| 2905 | spin_unlock(&balancing); |
| 2906 | out: |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 2907 | if (time_after(next_balance, sd->last_balance + interval)) |
| 2908 | next_balance = sd->last_balance + interval; |
Siddha, Suresh B | 783609c | 2006-12-10 02:20:33 -0800 | [diff] [blame] | 2909 | |
| 2910 | /* |
| 2911 | * Stop the load balance at this level. There is another |
| 2912 | * CPU in our sched group which is doing load balancing more |
| 2913 | * actively. |
| 2914 | */ |
| 2915 | if (!balance) |
| 2916 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | } |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2918 | rq->next_balance = next_balance; |
| 2919 | } |
| 2920 | |
| 2921 | /* |
| 2922 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
| 2923 | * In CONFIG_NO_HZ case, the idle load balance owner will do the |
| 2924 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 2925 | */ |
| 2926 | static void run_rebalance_domains(struct softirq_action *h) |
| 2927 | { |
| 2928 | int local_cpu = smp_processor_id(); |
| 2929 | struct rq *local_rq = cpu_rq(local_cpu); |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2930 | enum cpu_idle_type idle = local_rq->idle_at_tick ? CPU_IDLE : CPU_NOT_IDLE; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2931 | |
| 2932 | rebalance_domains(local_cpu, idle); |
| 2933 | |
| 2934 | #ifdef CONFIG_NO_HZ |
| 2935 | /* |
| 2936 | * If this cpu is the owner for idle load balancing, then do the |
| 2937 | * balancing on behalf of the other idle cpus whose ticks are |
| 2938 | * stopped. |
| 2939 | */ |
| 2940 | if (local_rq->idle_at_tick && |
| 2941 | atomic_read(&nohz.load_balancer) == local_cpu) { |
| 2942 | cpumask_t cpus = nohz.cpu_mask; |
| 2943 | struct rq *rq; |
| 2944 | int balance_cpu; |
| 2945 | |
| 2946 | cpu_clear(local_cpu, cpus); |
| 2947 | for_each_cpu_mask(balance_cpu, cpus) { |
| 2948 | /* |
| 2949 | * If this cpu gets work to do, stop the load balancing |
| 2950 | * work being done for other cpus. Next load |
| 2951 | * balancing owner will pick it up. |
| 2952 | */ |
| 2953 | if (need_resched()) |
| 2954 | break; |
| 2955 | |
Ingo Molnar | d15bcfd | 2007-07-09 18:51:57 +0200 | [diff] [blame] | 2956 | rebalance_domains(balance_cpu, CPU_IDLE); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 2957 | |
| 2958 | rq = cpu_rq(balance_cpu); |
| 2959 | if (time_after(local_rq->next_balance, rq->next_balance)) |
| 2960 | local_rq->next_balance = rq->next_balance; |
| 2961 | } |
| 2962 | } |
| 2963 | #endif |
| 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. |
| 2968 | * |
| 2969 | * In case of CONFIG_NO_HZ, this is the place where we nominate a new |
| 2970 | * idle load balancing owner or decide to stop the periodic load balancing, |
| 2971 | * if the whole system is idle. |
| 2972 | */ |
| 2973 | static inline void trigger_load_balance(int cpu) |
| 2974 | { |
| 2975 | struct rq *rq = cpu_rq(cpu); |
| 2976 | #ifdef CONFIG_NO_HZ |
| 2977 | /* |
| 2978 | * If we were in the nohz mode recently and busy at the current |
| 2979 | * scheduler tick, then check if we need to nominate new idle |
| 2980 | * load balancer. |
| 2981 | */ |
| 2982 | if (rq->in_nohz_recently && !rq->idle_at_tick) { |
| 2983 | rq->in_nohz_recently = 0; |
| 2984 | |
| 2985 | if (atomic_read(&nohz.load_balancer) == cpu) { |
| 2986 | cpu_clear(cpu, nohz.cpu_mask); |
| 2987 | atomic_set(&nohz.load_balancer, -1); |
| 2988 | } |
| 2989 | |
| 2990 | if (atomic_read(&nohz.load_balancer) == -1) { |
| 2991 | /* |
| 2992 | * simple selection for now: Nominate the |
| 2993 | * first cpu in the nohz list to be the next |
| 2994 | * ilb owner. |
| 2995 | * |
| 2996 | * TBD: Traverse the sched domains and nominate |
| 2997 | * the nearest cpu in the nohz.cpu_mask. |
| 2998 | */ |
| 2999 | int ilb = first_cpu(nohz.cpu_mask); |
| 3000 | |
| 3001 | if (ilb != NR_CPUS) |
| 3002 | resched_cpu(ilb); |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | /* |
| 3007 | * If this cpu is idle and doing idle load balancing for all the |
| 3008 | * cpus with ticks stopped, is it time for that to stop? |
| 3009 | */ |
| 3010 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && |
| 3011 | cpus_weight(nohz.cpu_mask) == num_online_cpus()) { |
| 3012 | resched_cpu(cpu); |
| 3013 | return; |
| 3014 | } |
| 3015 | |
| 3016 | /* |
| 3017 | * If this cpu is idle and the idle load balancing is done by |
| 3018 | * someone else, then no need raise the SCHED_SOFTIRQ |
| 3019 | */ |
| 3020 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && |
| 3021 | cpu_isset(cpu, nohz.cpu_mask)) |
| 3022 | return; |
| 3023 | #endif |
| 3024 | if (time_after_eq(jiffies, rq->next_balance)) |
| 3025 | raise_softirq(SCHED_SOFTIRQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | } |
| 3027 | #else |
| 3028 | /* |
| 3029 | * on UP we do not need to balance between CPUs: |
| 3030 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3031 | static inline void idle_balance(int cpu, struct rq *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | { |
| 3033 | } |
| 3034 | #endif |
| 3035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
| 3037 | |
| 3038 | EXPORT_PER_CPU_SYMBOL(kstat); |
| 3039 | |
| 3040 | /* |
| 3041 | * This is called on clock ticks and on context switches. |
| 3042 | * Bank in p->sched_time the ns elapsed since the last tick or switch. |
| 3043 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3044 | static inline void |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3045 | 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] | 3046 | { |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 3047 | p->sched_time += now - p->last_ran; |
| 3048 | p->last_ran = rq->most_recent_timestamp = now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | * Return current->sched_time plus any more ns on the sched_clock |
| 3053 | * that have not yet been banked. |
| 3054 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3055 | unsigned long long current_sched_time(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | { |
| 3057 | unsigned long long ns; |
| 3058 | unsigned long flags; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | local_irq_save(flags); |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 3061 | ns = p->sched_time + sched_clock() - p->last_ran; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | local_irq_restore(flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | return ns; |
| 3065 | } |
| 3066 | |
| 3067 | /* |
Linus Torvalds | f1adad7 | 2006-05-21 18:54:09 -0700 | [diff] [blame] | 3068 | * We place interactive tasks back into the active array, if possible. |
| 3069 | * |
| 3070 | * To guarantee that this does not starve expired tasks we ignore the |
| 3071 | * interactivity of a task if the first expired task had to wait more |
| 3072 | * than a 'reasonable' amount of time. This deadline timeout is |
| 3073 | * load-dependent, as the frequency of array switched decreases with |
| 3074 | * increasing number of running tasks. We also ignore the interactivity |
| 3075 | * if a better static_prio task has expired: |
| 3076 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3077 | static inline int expired_starving(struct rq *rq) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3078 | { |
| 3079 | if (rq->curr->static_prio > rq->best_expired_prio) |
| 3080 | return 1; |
| 3081 | if (!STARVATION_LIMIT || !rq->expired_timestamp) |
| 3082 | return 0; |
| 3083 | if (jiffies - rq->expired_timestamp > STARVATION_LIMIT * rq->nr_running) |
| 3084 | return 1; |
| 3085 | return 0; |
| 3086 | } |
Linus Torvalds | f1adad7 | 2006-05-21 18:54:09 -0700 | [diff] [blame] | 3087 | |
| 3088 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | * Account user cpu time to a process. |
| 3090 | * @p: the process that the cpu time gets accounted to |
| 3091 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 3092 | * @cputime: the cpu time spent in user space since the last update |
| 3093 | */ |
| 3094 | void account_user_time(struct task_struct *p, cputime_t cputime) |
| 3095 | { |
| 3096 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3097 | cputime64_t tmp; |
| 3098 | |
| 3099 | p->utime = cputime_add(p->utime, cputime); |
| 3100 | |
| 3101 | /* Add user time to cpustat. */ |
| 3102 | tmp = cputime_to_cputime64(cputime); |
| 3103 | if (TASK_NICE(p) > 0) |
| 3104 | cpustat->nice = cputime64_add(cpustat->nice, tmp); |
| 3105 | else |
| 3106 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 3107 | } |
| 3108 | |
| 3109 | /* |
| 3110 | * Account system cpu time to a process. |
| 3111 | * @p: the process that the cpu time gets accounted to |
| 3112 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 3113 | * @cputime: the cpu time spent in kernel space since the last update |
| 3114 | */ |
| 3115 | void account_system_time(struct task_struct *p, int hardirq_offset, |
| 3116 | cputime_t cputime) |
| 3117 | { |
| 3118 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3119 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | cputime64_t tmp; |
| 3121 | |
| 3122 | p->stime = cputime_add(p->stime, cputime); |
| 3123 | |
| 3124 | /* Add system time to cpustat. */ |
| 3125 | tmp = cputime_to_cputime64(cputime); |
| 3126 | if (hardirq_count() - hardirq_offset) |
| 3127 | cpustat->irq = cputime64_add(cpustat->irq, tmp); |
| 3128 | else if (softirq_count()) |
| 3129 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); |
| 3130 | else if (p != rq->idle) |
| 3131 | cpustat->system = cputime64_add(cpustat->system, tmp); |
| 3132 | else if (atomic_read(&rq->nr_iowait) > 0) |
| 3133 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 3134 | else |
| 3135 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 3136 | /* Account for system time used */ |
| 3137 | acct_update_integrals(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | /* |
| 3141 | * Account for involuntary wait time. |
| 3142 | * @p: the process from which the cpu time has been stolen |
| 3143 | * @steal: the cpu time spent in involuntary wait |
| 3144 | */ |
| 3145 | void account_steal_time(struct task_struct *p, cputime_t steal) |
| 3146 | { |
| 3147 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 3148 | cputime64_t tmp = cputime_to_cputime64(steal); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3149 | struct rq *rq = this_rq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | |
| 3151 | if (p == rq->idle) { |
| 3152 | p->stime = cputime_add(p->stime, steal); |
| 3153 | if (atomic_read(&rq->nr_iowait) > 0) |
| 3154 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 3155 | else |
| 3156 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 3157 | } else |
| 3158 | cpustat->steal = cputime64_add(cpustat->steal, tmp); |
| 3159 | } |
| 3160 | |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3161 | static void task_running_tick(struct rq *rq, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | if (p->array != rq->active) { |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3164 | /* Task has expired but was not scheduled yet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | set_tsk_need_resched(p); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3166 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | } |
| 3168 | spin_lock(&rq->lock); |
| 3169 | /* |
| 3170 | * The task was running during this tick - update the |
| 3171 | * time slice counter. Note: we do not update a thread's |
| 3172 | * priority until it either goes to sleep or uses up its |
| 3173 | * timeslice. This makes it possible for interactive tasks |
| 3174 | * to use up their timeslices at their highest priority levels. |
| 3175 | */ |
| 3176 | if (rt_task(p)) { |
| 3177 | /* |
| 3178 | * RR tasks need a special form of timeslice management. |
| 3179 | * FIFO tasks have no timeslices. |
| 3180 | */ |
| 3181 | if ((p->policy == SCHED_RR) && !--p->time_slice) { |
| 3182 | p->time_slice = task_timeslice(p); |
| 3183 | p->first_time_slice = 0; |
| 3184 | set_tsk_need_resched(p); |
| 3185 | |
| 3186 | /* put it at the end of the queue: */ |
| 3187 | requeue_task(p, rq->active); |
| 3188 | } |
| 3189 | goto out_unlock; |
| 3190 | } |
| 3191 | if (!--p->time_slice) { |
| 3192 | dequeue_task(p, rq->active); |
| 3193 | set_tsk_need_resched(p); |
| 3194 | p->prio = effective_prio(p); |
| 3195 | p->time_slice = task_timeslice(p); |
| 3196 | p->first_time_slice = 0; |
| 3197 | |
| 3198 | if (!rq->expired_timestamp) |
| 3199 | rq->expired_timestamp = jiffies; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3200 | if (!TASK_INTERACTIVE(p) || expired_starving(rq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | enqueue_task(p, rq->expired); |
| 3202 | if (p->static_prio < rq->best_expired_prio) |
| 3203 | rq->best_expired_prio = p->static_prio; |
| 3204 | } else |
| 3205 | enqueue_task(p, rq->active); |
| 3206 | } else { |
| 3207 | /* |
| 3208 | * Prevent a too long timeslice allowing a task to monopolize |
| 3209 | * the CPU. We do this by splitting up the timeslice into |
| 3210 | * smaller pieces. |
| 3211 | * |
| 3212 | * Note: this does not mean the task's timeslices expire or |
| 3213 | * get lost in any way, they just might be preempted by |
| 3214 | * another task of equal priority. (one with higher |
| 3215 | * priority would have preempted this task already.) We |
| 3216 | * requeue this task to the end of the list on this priority |
| 3217 | * level, which is in essence a round-robin of tasks with |
| 3218 | * equal priority. |
| 3219 | * |
| 3220 | * This only applies to tasks in the interactive |
| 3221 | * delta range with at least TIMESLICE_GRANULARITY to requeue. |
| 3222 | */ |
| 3223 | if (TASK_INTERACTIVE(p) && !((task_timeslice(p) - |
| 3224 | p->time_slice) % TIMESLICE_GRANULARITY(p)) && |
| 3225 | (p->time_slice >= TIMESLICE_GRANULARITY(p)) && |
| 3226 | (p->array == rq->active)) { |
| 3227 | |
| 3228 | requeue_task(p, rq->active); |
| 3229 | set_tsk_need_resched(p); |
| 3230 | } |
| 3231 | } |
| 3232 | out_unlock: |
| 3233 | spin_unlock(&rq->lock); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | /* |
| 3237 | * This function gets called by the timer code, with HZ frequency. |
| 3238 | * We call it with interrupts disabled. |
| 3239 | * |
| 3240 | * It also gets called by the fork code, when changing the parent's |
| 3241 | * timeslices. |
| 3242 | */ |
| 3243 | void scheduler_tick(void) |
| 3244 | { |
| 3245 | unsigned long long now = sched_clock(); |
| 3246 | struct task_struct *p = current; |
| 3247 | int cpu = smp_processor_id(); |
Siddha, Suresh B | bdecea3 | 2007-05-08 00:32:48 -0700 | [diff] [blame] | 3248 | int idle_at_tick = idle_cpu(cpu); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3249 | struct rq *rq = cpu_rq(cpu); |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3250 | |
| 3251 | update_cpu_clock(p, rq, now); |
| 3252 | |
Siddha, Suresh B | bdecea3 | 2007-05-08 00:32:48 -0700 | [diff] [blame] | 3253 | if (!idle_at_tick) |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3254 | task_running_tick(rq, p); |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 3255 | #ifdef CONFIG_SMP |
Christoph Lameter | 7835b98 | 2006-12-10 02:20:22 -0800 | [diff] [blame] | 3256 | update_load(rq); |
Siddha, Suresh B | bdecea3 | 2007-05-08 00:32:48 -0700 | [diff] [blame] | 3257 | rq->idle_at_tick = idle_at_tick; |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 3258 | trigger_load_balance(cpu); |
Christoph Lameter | e418e1c | 2006-12-10 02:20:23 -0800 | [diff] [blame] | 3259 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
| 3263 | |
| 3264 | void fastcall add_preempt_count(int val) |
| 3265 | { |
| 3266 | /* |
| 3267 | * Underflow? |
| 3268 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3269 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
| 3270 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | preempt_count() += val; |
| 3272 | /* |
| 3273 | * Spinlock count overflowing soon? |
| 3274 | */ |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 3275 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
| 3276 | PREEMPT_MASK - 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | } |
| 3278 | EXPORT_SYMBOL(add_preempt_count); |
| 3279 | |
| 3280 | void fastcall sub_preempt_count(int val) |
| 3281 | { |
| 3282 | /* |
| 3283 | * Underflow? |
| 3284 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3285 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
| 3286 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | /* |
| 3288 | * Is the spinlock portion underflowing? |
| 3289 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 3290 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
| 3291 | !(preempt_count() & PREEMPT_MASK))) |
| 3292 | return; |
| 3293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | preempt_count() -= val; |
| 3295 | } |
| 3296 | EXPORT_SYMBOL(sub_preempt_count); |
| 3297 | |
| 3298 | #endif |
| 3299 | |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 3300 | static inline int interactive_sleep(enum sleep_type sleep_type) |
| 3301 | { |
| 3302 | return (sleep_type == SLEEP_INTERACTIVE || |
| 3303 | sleep_type == SLEEP_INTERRUPTED); |
| 3304 | } |
| 3305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | /* |
| 3307 | * schedule() is the main scheduler function. |
| 3308 | */ |
| 3309 | asmlinkage void __sched schedule(void) |
| 3310 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3311 | struct task_struct *prev, *next; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3312 | struct prio_array *array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | struct list_head *queue; |
| 3314 | unsigned long long now; |
| 3315 | unsigned long run_time; |
Chen Shang | a3464a1 | 2005-06-25 14:57:31 -0700 | [diff] [blame] | 3316 | int cpu, idx, new_prio; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3317 | long *switch_count; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3318 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | |
| 3320 | /* |
| 3321 | * Test if we are atomic. Since do_exit() needs to call into |
| 3322 | * schedule() atomically, we ignore that path for now. |
| 3323 | * Otherwise, whine if we are scheduling when we should not be. |
| 3324 | */ |
Andreas Mohr | 77e4bfb | 2006-03-27 01:15:20 -0800 | [diff] [blame] | 3325 | if (unlikely(in_atomic() && !current->exit_state)) { |
| 3326 | printk(KERN_ERR "BUG: scheduling while atomic: " |
| 3327 | "%s/0x%08x/%d\n", |
| 3328 | current->comm, preempt_count(), current->pid); |
Peter Zijlstra | a4c410f | 2006-12-06 20:37:21 -0800 | [diff] [blame] | 3329 | debug_show_held_locks(current); |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 3330 | if (irqs_disabled()) |
| 3331 | print_irqtrace_events(current); |
Andreas Mohr | 77e4bfb | 2006-03-27 01:15:20 -0800 | [diff] [blame] | 3332 | dump_stack(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | } |
| 3334 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| 3335 | |
| 3336 | need_resched: |
| 3337 | preempt_disable(); |
| 3338 | prev = current; |
| 3339 | release_kernel_lock(prev); |
| 3340 | need_resched_nonpreemptible: |
| 3341 | rq = this_rq(); |
| 3342 | |
| 3343 | /* |
| 3344 | * The idle thread is not allowed to schedule! |
| 3345 | * Remove this check after it has been exercised a bit. |
| 3346 | */ |
| 3347 | if (unlikely(prev == rq->idle) && prev->state != TASK_RUNNING) { |
| 3348 | printk(KERN_ERR "bad: scheduling from the idle thread!\n"); |
| 3349 | dump_stack(); |
| 3350 | } |
| 3351 | |
| 3352 | schedstat_inc(rq, sched_cnt); |
| 3353 | now = sched_clock(); |
Ingo Molnar | 238628e | 2005-04-18 10:58:36 -0700 | [diff] [blame] | 3354 | if (likely((long long)(now - prev->timestamp) < NS_MAX_SLEEP_AVG)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | run_time = now - prev->timestamp; |
Ingo Molnar | 238628e | 2005-04-18 10:58:36 -0700 | [diff] [blame] | 3356 | if (unlikely((long long)(now - prev->timestamp) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | run_time = 0; |
| 3358 | } else |
| 3359 | run_time = NS_MAX_SLEEP_AVG; |
| 3360 | |
| 3361 | /* |
| 3362 | * Tasks charged proportionately less run_time at high sleep_avg to |
| 3363 | * delay them losing their interactive status |
| 3364 | */ |
| 3365 | run_time /= (CURRENT_BONUS(prev) ? : 1); |
| 3366 | |
| 3367 | spin_lock_irq(&rq->lock); |
| 3368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | switch_count = &prev->nivcsw; |
| 3370 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
| 3371 | switch_count = &prev->nvcsw; |
| 3372 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
| 3373 | unlikely(signal_pending(prev)))) |
| 3374 | prev->state = TASK_RUNNING; |
| 3375 | else { |
| 3376 | if (prev->state == TASK_UNINTERRUPTIBLE) |
| 3377 | rq->nr_uninterruptible++; |
| 3378 | deactivate_task(prev, rq); |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | cpu = smp_processor_id(); |
| 3383 | if (unlikely(!rq->nr_running)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | idle_balance(cpu, rq); |
| 3385 | if (!rq->nr_running) { |
| 3386 | next = rq->idle; |
| 3387 | rq->expired_timestamp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | goto switch_tasks; |
| 3389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | array = rq->active; |
| 3393 | if (unlikely(!array->nr_active)) { |
| 3394 | /* |
| 3395 | * Switch the active and expired arrays. |
| 3396 | */ |
| 3397 | schedstat_inc(rq, sched_switch); |
| 3398 | rq->active = rq->expired; |
| 3399 | rq->expired = array; |
| 3400 | array = rq->active; |
| 3401 | rq->expired_timestamp = 0; |
| 3402 | rq->best_expired_prio = MAX_PRIO; |
| 3403 | } |
| 3404 | |
| 3405 | idx = sched_find_first_bit(array->bitmap); |
| 3406 | queue = array->queue + idx; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3407 | next = list_entry(queue->next, struct task_struct, run_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 3409 | if (!rt_task(next) && interactive_sleep(next->sleep_type)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | unsigned long long delta = now - next->timestamp; |
Ingo Molnar | 238628e | 2005-04-18 10:58:36 -0700 | [diff] [blame] | 3411 | if (unlikely((long long)(now - next->timestamp) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | delta = 0; |
| 3413 | |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 3414 | if (next->sleep_type == SLEEP_INTERACTIVE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | delta = delta * (ON_RUNQUEUE_WEIGHT * 128 / 100) / 128; |
| 3416 | |
| 3417 | array = next->array; |
Chen Shang | a3464a1 | 2005-06-25 14:57:31 -0700 | [diff] [blame] | 3418 | new_prio = recalc_task_prio(next, next->timestamp + delta); |
| 3419 | |
| 3420 | if (unlikely(next->prio != new_prio)) { |
| 3421 | dequeue_task(next, array); |
| 3422 | next->prio = new_prio; |
| 3423 | enqueue_task(next, array); |
Con Kolivas | 7c4bb1f | 2006-03-31 02:31:29 -0800 | [diff] [blame] | 3424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | } |
Con Kolivas | 3dee386 | 2006-03-31 02:31:23 -0800 | [diff] [blame] | 3426 | next->sleep_type = SLEEP_NORMAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | switch_tasks: |
| 3428 | if (next == rq->idle) |
| 3429 | schedstat_inc(rq, sched_goidle); |
| 3430 | prefetch(next); |
Chen, Kenneth W | 383f283 | 2005-09-09 13:02:02 -0700 | [diff] [blame] | 3431 | prefetch_stack(next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | clear_tsk_need_resched(prev); |
| 3433 | rcu_qsctr_inc(task_cpu(prev)); |
| 3434 | |
| 3435 | update_cpu_clock(prev, rq, now); |
| 3436 | |
| 3437 | prev->sleep_avg -= run_time; |
| 3438 | if ((long)prev->sleep_avg <= 0) |
| 3439 | prev->sleep_avg = 0; |
| 3440 | prev->timestamp = prev->last_ran = now; |
| 3441 | |
| 3442 | sched_info_switch(prev, next); |
| 3443 | if (likely(prev != next)) { |
Thomas Gleixner | c1e16aa | 2007-02-28 20:12:19 -0800 | [diff] [blame] | 3444 | next->timestamp = next->last_ran = now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | rq->nr_switches++; |
| 3446 | rq->curr = next; |
| 3447 | ++*switch_count; |
| 3448 | |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 3449 | prepare_task_switch(rq, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | prev = context_switch(rq, prev, next); |
| 3451 | barrier(); |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 3452 | /* |
| 3453 | * this_rq must be evaluated again because prev may have moved |
| 3454 | * CPUs since it called schedule(), thus the 'rq' on its stack |
| 3455 | * frame will be invalid. |
| 3456 | */ |
| 3457 | finish_task_switch(this_rq(), prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | } else |
| 3459 | spin_unlock_irq(&rq->lock); |
| 3460 | |
| 3461 | prev = current; |
| 3462 | if (unlikely(reacquire_kernel_lock(prev) < 0)) |
| 3463 | goto need_resched_nonpreemptible; |
| 3464 | preempt_enable_no_resched(); |
| 3465 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 3466 | goto need_resched; |
| 3467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | EXPORT_SYMBOL(schedule); |
| 3469 | |
| 3470 | #ifdef CONFIG_PREEMPT |
| 3471 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 3472 | * this is the entry point to schedule() from in-kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | * off of preempt_enable. Kernel preemptions off return from interrupt |
| 3474 | * occur there and call schedule directly. |
| 3475 | */ |
| 3476 | asmlinkage void __sched preempt_schedule(void) |
| 3477 | { |
| 3478 | struct thread_info *ti = current_thread_info(); |
| 3479 | #ifdef CONFIG_PREEMPT_BKL |
| 3480 | struct task_struct *task = current; |
| 3481 | int saved_lock_depth; |
| 3482 | #endif |
| 3483 | /* |
| 3484 | * If there is a non-zero preempt_count or interrupts are disabled, |
| 3485 | * we do not want to preempt the current task. Just return.. |
| 3486 | */ |
Nick Piggin | beed33a | 2006-10-11 01:21:52 -0700 | [diff] [blame] | 3487 | if (likely(ti->preempt_count || irqs_disabled())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | return; |
| 3489 | |
| 3490 | need_resched: |
| 3491 | add_preempt_count(PREEMPT_ACTIVE); |
| 3492 | /* |
| 3493 | * We keep the big kernel semaphore locked, but we |
| 3494 | * clear ->lock_depth so that schedule() doesnt |
| 3495 | * auto-release the semaphore: |
| 3496 | */ |
| 3497 | #ifdef CONFIG_PREEMPT_BKL |
| 3498 | saved_lock_depth = task->lock_depth; |
| 3499 | task->lock_depth = -1; |
| 3500 | #endif |
| 3501 | schedule(); |
| 3502 | #ifdef CONFIG_PREEMPT_BKL |
| 3503 | task->lock_depth = saved_lock_depth; |
| 3504 | #endif |
| 3505 | sub_preempt_count(PREEMPT_ACTIVE); |
| 3506 | |
| 3507 | /* we could miss a preemption opportunity between schedule and now */ |
| 3508 | barrier(); |
| 3509 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 3510 | goto need_resched; |
| 3511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | EXPORT_SYMBOL(preempt_schedule); |
| 3513 | |
| 3514 | /* |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 3515 | * this is the entry point to schedule() from kernel preemption |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | * off of irq context. |
| 3517 | * Note, that this is called and return with irqs disabled. This will |
| 3518 | * protect us against recursive calling from irq. |
| 3519 | */ |
| 3520 | asmlinkage void __sched preempt_schedule_irq(void) |
| 3521 | { |
| 3522 | struct thread_info *ti = current_thread_info(); |
| 3523 | #ifdef CONFIG_PREEMPT_BKL |
| 3524 | struct task_struct *task = current; |
| 3525 | int saved_lock_depth; |
| 3526 | #endif |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 3527 | /* Catch callers which need to be fixed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
| 3529 | |
| 3530 | need_resched: |
| 3531 | add_preempt_count(PREEMPT_ACTIVE); |
| 3532 | /* |
| 3533 | * We keep the big kernel semaphore locked, but we |
| 3534 | * clear ->lock_depth so that schedule() doesnt |
| 3535 | * auto-release the semaphore: |
| 3536 | */ |
| 3537 | #ifdef CONFIG_PREEMPT_BKL |
| 3538 | saved_lock_depth = task->lock_depth; |
| 3539 | task->lock_depth = -1; |
| 3540 | #endif |
| 3541 | local_irq_enable(); |
| 3542 | schedule(); |
| 3543 | local_irq_disable(); |
| 3544 | #ifdef CONFIG_PREEMPT_BKL |
| 3545 | task->lock_depth = saved_lock_depth; |
| 3546 | #endif |
| 3547 | sub_preempt_count(PREEMPT_ACTIVE); |
| 3548 | |
| 3549 | /* we could miss a preemption opportunity between schedule and now */ |
| 3550 | barrier(); |
| 3551 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 3552 | goto need_resched; |
| 3553 | } |
| 3554 | |
| 3555 | #endif /* CONFIG_PREEMPT */ |
| 3556 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3557 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
| 3558 | void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3560 | return try_to_wake_up(curr->private, mode, sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | EXPORT_SYMBOL(default_wake_function); |
| 3563 | |
| 3564 | /* |
| 3565 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
| 3566 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve |
| 3567 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
| 3568 | * |
| 3569 | * There are circumstances in which we can try to wake a task which has already |
| 3570 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns |
| 3571 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
| 3572 | */ |
| 3573 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, |
| 3574 | int nr_exclusive, int sync, void *key) |
| 3575 | { |
| 3576 | struct list_head *tmp, *next; |
| 3577 | |
| 3578 | list_for_each_safe(tmp, next, &q->task_list) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3579 | wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list); |
| 3580 | unsigned flags = curr->flags; |
| 3581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | if (curr->func(curr, mode, sync, key) && |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3583 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | break; |
| 3585 | } |
| 3586 | } |
| 3587 | |
| 3588 | /** |
| 3589 | * __wake_up - wake up threads blocked on a waitqueue. |
| 3590 | * @q: the waitqueue |
| 3591 | * @mode: which threads |
| 3592 | * @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] | 3593 | * @key: is directly passed to the wakeup function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | */ |
| 3595 | void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode, |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3596 | int nr_exclusive, void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | { |
| 3598 | unsigned long flags; |
| 3599 | |
| 3600 | spin_lock_irqsave(&q->lock, flags); |
| 3601 | __wake_up_common(q, mode, nr_exclusive, 0, key); |
| 3602 | spin_unlock_irqrestore(&q->lock, flags); |
| 3603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | EXPORT_SYMBOL(__wake_up); |
| 3605 | |
| 3606 | /* |
| 3607 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. |
| 3608 | */ |
| 3609 | void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
| 3610 | { |
| 3611 | __wake_up_common(q, mode, 1, 0, NULL); |
| 3612 | } |
| 3613 | |
| 3614 | /** |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 3615 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | * @q: the waitqueue |
| 3617 | * @mode: which threads |
| 3618 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
| 3619 | * |
| 3620 | * The sync wakeup differs that the waker knows that it will schedule |
| 3621 | * away soon, so while the target thread will be woken up, it will not |
| 3622 | * be migrated to another CPU - ie. the two threads are 'synchronized' |
| 3623 | * with each other. This can prevent needless bouncing between CPUs. |
| 3624 | * |
| 3625 | * On UP it can prevent extra preemption. |
| 3626 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3627 | void fastcall |
| 3628 | __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] | 3629 | { |
| 3630 | unsigned long flags; |
| 3631 | int sync = 1; |
| 3632 | |
| 3633 | if (unlikely(!q)) |
| 3634 | return; |
| 3635 | |
| 3636 | if (unlikely(!nr_exclusive)) |
| 3637 | sync = 0; |
| 3638 | |
| 3639 | spin_lock_irqsave(&q->lock, flags); |
| 3640 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); |
| 3641 | spin_unlock_irqrestore(&q->lock, flags); |
| 3642 | } |
| 3643 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ |
| 3644 | |
| 3645 | void fastcall complete(struct completion *x) |
| 3646 | { |
| 3647 | unsigned long flags; |
| 3648 | |
| 3649 | spin_lock_irqsave(&x->wait.lock, flags); |
| 3650 | x->done++; |
| 3651 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, |
| 3652 | 1, 0, NULL); |
| 3653 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 3654 | } |
| 3655 | EXPORT_SYMBOL(complete); |
| 3656 | |
| 3657 | void fastcall complete_all(struct completion *x) |
| 3658 | { |
| 3659 | unsigned long flags; |
| 3660 | |
| 3661 | spin_lock_irqsave(&x->wait.lock, flags); |
| 3662 | x->done += UINT_MAX/2; |
| 3663 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, |
| 3664 | 0, 0, NULL); |
| 3665 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 3666 | } |
| 3667 | EXPORT_SYMBOL(complete_all); |
| 3668 | |
| 3669 | void fastcall __sched wait_for_completion(struct completion *x) |
| 3670 | { |
| 3671 | might_sleep(); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | spin_lock_irq(&x->wait.lock); |
| 3674 | if (!x->done) { |
| 3675 | DECLARE_WAITQUEUE(wait, current); |
| 3676 | |
| 3677 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3678 | __add_wait_queue_tail(&x->wait, &wait); |
| 3679 | do { |
| 3680 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 3681 | spin_unlock_irq(&x->wait.lock); |
| 3682 | schedule(); |
| 3683 | spin_lock_irq(&x->wait.lock); |
| 3684 | } while (!x->done); |
| 3685 | __remove_wait_queue(&x->wait, &wait); |
| 3686 | } |
| 3687 | x->done--; |
| 3688 | spin_unlock_irq(&x->wait.lock); |
| 3689 | } |
| 3690 | EXPORT_SYMBOL(wait_for_completion); |
| 3691 | |
| 3692 | unsigned long fastcall __sched |
| 3693 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
| 3694 | { |
| 3695 | might_sleep(); |
| 3696 | |
| 3697 | spin_lock_irq(&x->wait.lock); |
| 3698 | if (!x->done) { |
| 3699 | DECLARE_WAITQUEUE(wait, current); |
| 3700 | |
| 3701 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3702 | __add_wait_queue_tail(&x->wait, &wait); |
| 3703 | do { |
| 3704 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 3705 | spin_unlock_irq(&x->wait.lock); |
| 3706 | timeout = schedule_timeout(timeout); |
| 3707 | spin_lock_irq(&x->wait.lock); |
| 3708 | if (!timeout) { |
| 3709 | __remove_wait_queue(&x->wait, &wait); |
| 3710 | goto out; |
| 3711 | } |
| 3712 | } while (!x->done); |
| 3713 | __remove_wait_queue(&x->wait, &wait); |
| 3714 | } |
| 3715 | x->done--; |
| 3716 | out: |
| 3717 | spin_unlock_irq(&x->wait.lock); |
| 3718 | return timeout; |
| 3719 | } |
| 3720 | EXPORT_SYMBOL(wait_for_completion_timeout); |
| 3721 | |
| 3722 | int fastcall __sched wait_for_completion_interruptible(struct completion *x) |
| 3723 | { |
| 3724 | int ret = 0; |
| 3725 | |
| 3726 | might_sleep(); |
| 3727 | |
| 3728 | spin_lock_irq(&x->wait.lock); |
| 3729 | if (!x->done) { |
| 3730 | DECLARE_WAITQUEUE(wait, current); |
| 3731 | |
| 3732 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3733 | __add_wait_queue_tail(&x->wait, &wait); |
| 3734 | do { |
| 3735 | if (signal_pending(current)) { |
| 3736 | ret = -ERESTARTSYS; |
| 3737 | __remove_wait_queue(&x->wait, &wait); |
| 3738 | goto out; |
| 3739 | } |
| 3740 | __set_current_state(TASK_INTERRUPTIBLE); |
| 3741 | spin_unlock_irq(&x->wait.lock); |
| 3742 | schedule(); |
| 3743 | spin_lock_irq(&x->wait.lock); |
| 3744 | } while (!x->done); |
| 3745 | __remove_wait_queue(&x->wait, &wait); |
| 3746 | } |
| 3747 | x->done--; |
| 3748 | out: |
| 3749 | spin_unlock_irq(&x->wait.lock); |
| 3750 | |
| 3751 | return ret; |
| 3752 | } |
| 3753 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
| 3754 | |
| 3755 | unsigned long fastcall __sched |
| 3756 | wait_for_completion_interruptible_timeout(struct completion *x, |
| 3757 | unsigned long timeout) |
| 3758 | { |
| 3759 | might_sleep(); |
| 3760 | |
| 3761 | spin_lock_irq(&x->wait.lock); |
| 3762 | if (!x->done) { |
| 3763 | DECLARE_WAITQUEUE(wait, current); |
| 3764 | |
| 3765 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3766 | __add_wait_queue_tail(&x->wait, &wait); |
| 3767 | do { |
| 3768 | if (signal_pending(current)) { |
| 3769 | timeout = -ERESTARTSYS; |
| 3770 | __remove_wait_queue(&x->wait, &wait); |
| 3771 | goto out; |
| 3772 | } |
| 3773 | __set_current_state(TASK_INTERRUPTIBLE); |
| 3774 | spin_unlock_irq(&x->wait.lock); |
| 3775 | timeout = schedule_timeout(timeout); |
| 3776 | spin_lock_irq(&x->wait.lock); |
| 3777 | if (!timeout) { |
| 3778 | __remove_wait_queue(&x->wait, &wait); |
| 3779 | goto out; |
| 3780 | } |
| 3781 | } while (!x->done); |
| 3782 | __remove_wait_queue(&x->wait, &wait); |
| 3783 | } |
| 3784 | x->done--; |
| 3785 | out: |
| 3786 | spin_unlock_irq(&x->wait.lock); |
| 3787 | return timeout; |
| 3788 | } |
| 3789 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
| 3790 | |
| 3791 | |
| 3792 | #define SLEEP_ON_VAR \ |
| 3793 | unsigned long flags; \ |
| 3794 | wait_queue_t wait; \ |
| 3795 | init_waitqueue_entry(&wait, current); |
| 3796 | |
| 3797 | #define SLEEP_ON_HEAD \ |
| 3798 | spin_lock_irqsave(&q->lock,flags); \ |
| 3799 | __add_wait_queue(q, &wait); \ |
| 3800 | spin_unlock(&q->lock); |
| 3801 | |
| 3802 | #define SLEEP_ON_TAIL \ |
| 3803 | spin_lock_irq(&q->lock); \ |
| 3804 | __remove_wait_queue(q, &wait); \ |
| 3805 | spin_unlock_irqrestore(&q->lock, flags); |
| 3806 | |
| 3807 | void fastcall __sched interruptible_sleep_on(wait_queue_head_t *q) |
| 3808 | { |
| 3809 | SLEEP_ON_VAR |
| 3810 | |
| 3811 | current->state = TASK_INTERRUPTIBLE; |
| 3812 | |
| 3813 | SLEEP_ON_HEAD |
| 3814 | schedule(); |
| 3815 | SLEEP_ON_TAIL |
| 3816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3817 | EXPORT_SYMBOL(interruptible_sleep_on); |
| 3818 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 3819 | long fastcall __sched |
| 3820 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | { |
| 3822 | SLEEP_ON_VAR |
| 3823 | |
| 3824 | current->state = TASK_INTERRUPTIBLE; |
| 3825 | |
| 3826 | SLEEP_ON_HEAD |
| 3827 | timeout = schedule_timeout(timeout); |
| 3828 | SLEEP_ON_TAIL |
| 3829 | |
| 3830 | return timeout; |
| 3831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
| 3833 | |
| 3834 | void fastcall __sched sleep_on(wait_queue_head_t *q) |
| 3835 | { |
| 3836 | SLEEP_ON_VAR |
| 3837 | |
| 3838 | current->state = TASK_UNINTERRUPTIBLE; |
| 3839 | |
| 3840 | SLEEP_ON_HEAD |
| 3841 | schedule(); |
| 3842 | SLEEP_ON_TAIL |
| 3843 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | EXPORT_SYMBOL(sleep_on); |
| 3845 | |
| 3846 | long fastcall __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
| 3847 | { |
| 3848 | SLEEP_ON_VAR |
| 3849 | |
| 3850 | current->state = TASK_UNINTERRUPTIBLE; |
| 3851 | |
| 3852 | SLEEP_ON_HEAD |
| 3853 | timeout = schedule_timeout(timeout); |
| 3854 | SLEEP_ON_TAIL |
| 3855 | |
| 3856 | return timeout; |
| 3857 | } |
| 3858 | |
| 3859 | EXPORT_SYMBOL(sleep_on_timeout); |
| 3860 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3861 | #ifdef CONFIG_RT_MUTEXES |
| 3862 | |
| 3863 | /* |
| 3864 | * rt_mutex_setprio - set the current priority of a task |
| 3865 | * @p: task |
| 3866 | * @prio: prio value (kernel-internal form) |
| 3867 | * |
| 3868 | * This function changes the 'effective' priority of a task. It does |
| 3869 | * not touch ->normal_prio like __setscheduler(). |
| 3870 | * |
| 3871 | * Used by the rt_mutex code to implement priority inheritance logic. |
| 3872 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3873 | void rt_mutex_setprio(struct task_struct *p, int prio) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3874 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3875 | struct prio_array *array; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3876 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3877 | struct rq *rq; |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 3878 | int oldprio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3879 | |
| 3880 | BUG_ON(prio < 0 || prio > MAX_PRIO); |
| 3881 | |
| 3882 | rq = task_rq_lock(p, &flags); |
| 3883 | |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 3884 | oldprio = p->prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3885 | array = p->array; |
| 3886 | if (array) |
| 3887 | dequeue_task(p, array); |
| 3888 | p->prio = prio; |
| 3889 | |
| 3890 | if (array) { |
| 3891 | /* |
| 3892 | * If changing to an RT priority then queue it |
| 3893 | * in the active array! |
| 3894 | */ |
| 3895 | if (rt_task(p)) |
| 3896 | array = rq->active; |
| 3897 | enqueue_task(p, array); |
| 3898 | /* |
| 3899 | * Reschedule if we are currently running on this runqueue and |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 3900 | * our priority decreased, or if we are not currently running on |
| 3901 | * this runqueue and our priority is higher than the current's |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3902 | */ |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 3903 | if (task_running(rq, p)) { |
| 3904 | if (p->prio > oldprio) |
| 3905 | resched_task(rq->curr); |
| 3906 | } else if (TASK_PREEMPTS_CURR(p, rq)) |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3907 | resched_task(rq->curr); |
| 3908 | } |
| 3909 | task_rq_unlock(rq, &flags); |
| 3910 | } |
| 3911 | |
| 3912 | #endif |
| 3913 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3914 | void set_user_nice(struct task_struct *p, long nice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3916 | struct prio_array *array; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3917 | int old_prio, delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 3919 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | |
| 3921 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) |
| 3922 | return; |
| 3923 | /* |
| 3924 | * We have to be careful, if called from sys_setpriority(), |
| 3925 | * the task might be in the middle of scheduling on another CPU. |
| 3926 | */ |
| 3927 | rq = task_rq_lock(p, &flags); |
| 3928 | /* |
| 3929 | * The RT priorities are set via sched_setscheduler(), but we still |
| 3930 | * allow the 'normal' nice value to be set - but as expected |
| 3931 | * it wont have any effect on scheduling until the task is |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 3932 | * not SCHED_NORMAL/SCHED_BATCH: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3934 | if (has_rt_policy(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | p->static_prio = NICE_TO_PRIO(nice); |
| 3936 | goto out_unlock; |
| 3937 | } |
| 3938 | array = p->array; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3939 | if (array) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | dequeue_task(p, array); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3941 | dec_raw_weighted_load(rq, p); |
| 3942 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | p->static_prio = NICE_TO_PRIO(nice); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3945 | set_load_weight(p); |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 3946 | old_prio = p->prio; |
| 3947 | p->prio = effective_prio(p); |
| 3948 | delta = p->prio - old_prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | |
| 3950 | if (array) { |
| 3951 | enqueue_task(p, array); |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 3952 | inc_raw_weighted_load(rq, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | /* |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 3954 | * If the task increased its priority or is running and |
| 3955 | * lowered its priority, then reschedule its CPU: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | */ |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 3957 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | resched_task(rq->curr); |
| 3959 | } |
| 3960 | out_unlock: |
| 3961 | task_rq_unlock(rq, &flags); |
| 3962 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | EXPORT_SYMBOL(set_user_nice); |
| 3964 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3965 | /* |
| 3966 | * can_nice - check if a task can reduce its nice value |
| 3967 | * @p: task |
| 3968 | * @nice: nice value |
| 3969 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 3970 | int can_nice(const struct task_struct *p, const int nice) |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3971 | { |
Matt Mackall | 024f474 | 2005-08-18 11:24:19 -0700 | [diff] [blame] | 3972 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
| 3973 | int nice_rlim = 20 - nice; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3974 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3975 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
| 3976 | capable(CAP_SYS_NICE)); |
| 3977 | } |
| 3978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | #ifdef __ARCH_WANT_SYS_NICE |
| 3980 | |
| 3981 | /* |
| 3982 | * sys_nice - change the priority of the current process. |
| 3983 | * @increment: priority increment |
| 3984 | * |
| 3985 | * sys_setpriority is a more generic, but much slower function that |
| 3986 | * does similar things. |
| 3987 | */ |
| 3988 | asmlinkage long sys_nice(int increment) |
| 3989 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 3990 | long nice, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | |
| 3992 | /* |
| 3993 | * Setpriority might change our priority at the same moment. |
| 3994 | * We don't have to worry. Conceptually one call occurs first |
| 3995 | * and we have a single winner. |
| 3996 | */ |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3997 | if (increment < -40) |
| 3998 | increment = -40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | if (increment > 40) |
| 4000 | increment = 40; |
| 4001 | |
| 4002 | nice = PRIO_TO_NICE(current->static_prio) + increment; |
| 4003 | if (nice < -20) |
| 4004 | nice = -20; |
| 4005 | if (nice > 19) |
| 4006 | nice = 19; |
| 4007 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 4008 | if (increment < 0 && !can_nice(current, nice)) |
| 4009 | return -EPERM; |
| 4010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | retval = security_task_setnice(current, nice); |
| 4012 | if (retval) |
| 4013 | return retval; |
| 4014 | |
| 4015 | set_user_nice(current, nice); |
| 4016 | return 0; |
| 4017 | } |
| 4018 | |
| 4019 | #endif |
| 4020 | |
| 4021 | /** |
| 4022 | * task_prio - return the priority value of a given task. |
| 4023 | * @p: the task in question. |
| 4024 | * |
| 4025 | * This is the priority value as seen by users in /proc. |
| 4026 | * RT tasks are offset by -200. Normal tasks are centered |
| 4027 | * around 0, value goes from -16 to +15. |
| 4028 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4029 | int task_prio(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | { |
| 4031 | return p->prio - MAX_RT_PRIO; |
| 4032 | } |
| 4033 | |
| 4034 | /** |
| 4035 | * task_nice - return the nice value of a given task. |
| 4036 | * @p: the task in question. |
| 4037 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4038 | int task_nice(const struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | { |
| 4040 | return TASK_NICE(p); |
| 4041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | EXPORT_SYMBOL_GPL(task_nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | |
| 4044 | /** |
| 4045 | * idle_cpu - is a given cpu idle currently? |
| 4046 | * @cpu: the processor in question. |
| 4047 | */ |
| 4048 | int idle_cpu(int cpu) |
| 4049 | { |
| 4050 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; |
| 4051 | } |
| 4052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4053 | /** |
| 4054 | * idle_task - return the idle task for a given cpu. |
| 4055 | * @cpu: the processor in question. |
| 4056 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4057 | struct task_struct *idle_task(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | { |
| 4059 | return cpu_rq(cpu)->idle; |
| 4060 | } |
| 4061 | |
| 4062 | /** |
| 4063 | * find_process_by_pid - find a process with a matching PID value. |
| 4064 | * @pid: the pid in question. |
| 4065 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4066 | static inline struct task_struct *find_process_by_pid(pid_t pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | { |
| 4068 | return pid ? find_task_by_pid(pid) : current; |
| 4069 | } |
| 4070 | |
| 4071 | /* Actually do priority change: must hold rq lock. */ |
| 4072 | static void __setscheduler(struct task_struct *p, int policy, int prio) |
| 4073 | { |
| 4074 | BUG_ON(p->array); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | p->policy = policy; |
| 4077 | p->rt_priority = prio; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4078 | p->normal_prio = normal_prio(p); |
| 4079 | /* we are holding p->pi_lock already */ |
| 4080 | p->prio = rt_mutex_getprio(p); |
| 4081 | /* |
| 4082 | * SCHED_BATCH tasks are treated as perpetual CPU hogs: |
| 4083 | */ |
| 4084 | if (policy == SCHED_BATCH) |
| 4085 | p->sleep_avg = 0; |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 4086 | set_load_weight(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4090 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | * @p: the task in question. |
| 4092 | * @policy: new policy. |
| 4093 | * @param: structure containing the new RT priority. |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4094 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4095 | * NOTE that the task may be already dead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4097 | int sched_setscheduler(struct task_struct *p, int policy, |
| 4098 | struct sched_param *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4100 | int retval, oldprio, oldpolicy = -1; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4101 | struct prio_array *array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4103 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | |
Steven Rostedt | 66e5393 | 2006-06-27 02:54:44 -0700 | [diff] [blame] | 4105 | /* may grab non-irq protected spin_locks */ |
| 4106 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | recheck: |
| 4108 | /* double check policy once rq lock held */ |
| 4109 | if (policy < 0) |
| 4110 | policy = oldpolicy = p->policy; |
| 4111 | else if (policy != SCHED_FIFO && policy != SCHED_RR && |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4112 | policy != SCHED_NORMAL && policy != SCHED_BATCH) |
| 4113 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | /* |
| 4115 | * Valid priorities for SCHED_FIFO and SCHED_RR are |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4116 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL and |
| 4117 | * SCHED_BATCH is 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | */ |
| 4119 | if (param->sched_priority < 0 || |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4120 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
Steven Rostedt | d46523e | 2005-07-25 16:28:39 -0400 | [diff] [blame] | 4121 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | return -EINVAL; |
Oleg Nesterov | 57a6f51c | 2006-09-29 02:00:49 -0700 | [diff] [blame] | 4123 | if (is_rt_policy(policy) != (param->sched_priority != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | return -EINVAL; |
| 4125 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4126 | /* |
| 4127 | * Allow unprivileged RT tasks to decrease priority: |
| 4128 | */ |
| 4129 | if (!capable(CAP_SYS_NICE)) { |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4130 | if (is_rt_policy(policy)) { |
| 4131 | unsigned long rlim_rtprio; |
| 4132 | unsigned long flags; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4133 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4134 | if (!lock_task_sighand(p, &flags)) |
| 4135 | return -ESRCH; |
| 4136 | rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; |
| 4137 | unlock_task_sighand(p, &flags); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4138 | |
Oleg Nesterov | 8dc3e90 | 2006-09-29 02:00:50 -0700 | [diff] [blame] | 4139 | /* can't set/change the rt policy */ |
| 4140 | if (policy != p->policy && !rlim_rtprio) |
| 4141 | return -EPERM; |
| 4142 | |
| 4143 | /* can't increase priority */ |
| 4144 | if (param->sched_priority > p->rt_priority && |
| 4145 | param->sched_priority > rlim_rtprio) |
| 4146 | return -EPERM; |
| 4147 | } |
| 4148 | |
Olivier Croquette | 37e4ab3 | 2005-06-25 14:57:32 -0700 | [diff] [blame] | 4149 | /* can't change other user's priorities */ |
| 4150 | if ((current->euid != p->euid) && |
| 4151 | (current->euid != p->uid)) |
| 4152 | return -EPERM; |
| 4153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | |
| 4155 | retval = security_task_setscheduler(p, policy, param); |
| 4156 | if (retval) |
| 4157 | return retval; |
| 4158 | /* |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4159 | * make sure no PI-waiters arrive (or leave) while we are |
| 4160 | * changing the priority of the task: |
| 4161 | */ |
| 4162 | spin_lock_irqsave(&p->pi_lock, flags); |
| 4163 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | * To be able to change p->policy safely, the apropriate |
| 4165 | * runqueue lock must be held. |
| 4166 | */ |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4167 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | /* recheck policy now with rq lock held */ |
| 4169 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| 4170 | policy = oldpolicy = -1; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4171 | __task_rq_unlock(rq); |
| 4172 | spin_unlock_irqrestore(&p->pi_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | goto recheck; |
| 4174 | } |
| 4175 | array = p->array; |
| 4176 | if (array) |
| 4177 | deactivate_task(p, rq); |
| 4178 | oldprio = p->prio; |
| 4179 | __setscheduler(p, policy, param->sched_priority); |
| 4180 | if (array) { |
| 4181 | __activate_task(p, rq); |
| 4182 | /* |
| 4183 | * Reschedule if we are currently running on this runqueue and |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4184 | * our priority decreased, or if we are not currently running on |
| 4185 | * this runqueue and our priority is higher than the current's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | */ |
Andrew Morton | d5f9f94 | 2007-05-08 20:27:06 -0700 | [diff] [blame] | 4187 | if (task_running(rq, p)) { |
| 4188 | if (p->prio > oldprio) |
| 4189 | resched_task(rq->curr); |
| 4190 | } else if (TASK_PREEMPTS_CURR(p, rq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | resched_task(rq->curr); |
| 4192 | } |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4193 | __task_rq_unlock(rq); |
| 4194 | spin_unlock_irqrestore(&p->pi_lock, flags); |
| 4195 | |
Thomas Gleixner | 95e02ca | 2006-06-27 02:55:02 -0700 | [diff] [blame] | 4196 | rt_mutex_adjust_pi(p); |
| 4197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | return 0; |
| 4199 | } |
| 4200 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
| 4201 | |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4202 | static int |
| 4203 | 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] | 4204 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | struct sched_param lparam; |
| 4206 | struct task_struct *p; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4207 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | |
| 4209 | if (!param || pid < 0) |
| 4210 | return -EINVAL; |
| 4211 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| 4212 | return -EFAULT; |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4213 | |
| 4214 | rcu_read_lock(); |
| 4215 | retval = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | p = find_process_by_pid(pid); |
Oleg Nesterov | 5fe1d75 | 2006-09-29 02:00:48 -0700 | [diff] [blame] | 4217 | if (p != NULL) |
| 4218 | retval = sched_setscheduler(p, policy, &lparam); |
| 4219 | rcu_read_unlock(); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | return retval; |
| 4222 | } |
| 4223 | |
| 4224 | /** |
| 4225 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| 4226 | * @pid: the pid in question. |
| 4227 | * @policy: new policy. |
| 4228 | * @param: structure containing the new RT priority. |
| 4229 | */ |
| 4230 | asmlinkage long sys_sched_setscheduler(pid_t pid, int policy, |
| 4231 | struct sched_param __user *param) |
| 4232 | { |
Jason Baron | c21761f | 2006-01-18 17:43:03 -0800 | [diff] [blame] | 4233 | /* negative values for policy are not valid */ |
| 4234 | if (policy < 0) |
| 4235 | return -EINVAL; |
| 4236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | return do_sched_setscheduler(pid, policy, param); |
| 4238 | } |
| 4239 | |
| 4240 | /** |
| 4241 | * sys_sched_setparam - set/change the RT priority of a thread |
| 4242 | * @pid: the pid in question. |
| 4243 | * @param: structure containing the new RT priority. |
| 4244 | */ |
| 4245 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) |
| 4246 | { |
| 4247 | return do_sched_setscheduler(pid, -1, param); |
| 4248 | } |
| 4249 | |
| 4250 | /** |
| 4251 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| 4252 | * @pid: the pid in question. |
| 4253 | */ |
| 4254 | asmlinkage long sys_sched_getscheduler(pid_t pid) |
| 4255 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4256 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | int retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | |
| 4259 | if (pid < 0) |
| 4260 | goto out_nounlock; |
| 4261 | |
| 4262 | retval = -ESRCH; |
| 4263 | read_lock(&tasklist_lock); |
| 4264 | p = find_process_by_pid(pid); |
| 4265 | if (p) { |
| 4266 | retval = security_task_getscheduler(p); |
| 4267 | if (!retval) |
| 4268 | retval = p->policy; |
| 4269 | } |
| 4270 | read_unlock(&tasklist_lock); |
| 4271 | |
| 4272 | out_nounlock: |
| 4273 | return retval; |
| 4274 | } |
| 4275 | |
| 4276 | /** |
| 4277 | * sys_sched_getscheduler - get the RT priority of a thread |
| 4278 | * @pid: the pid in question. |
| 4279 | * @param: structure containing the RT priority. |
| 4280 | */ |
| 4281 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) |
| 4282 | { |
| 4283 | struct sched_param lp; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4284 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | int retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | |
| 4287 | if (!param || pid < 0) |
| 4288 | goto out_nounlock; |
| 4289 | |
| 4290 | read_lock(&tasklist_lock); |
| 4291 | p = find_process_by_pid(pid); |
| 4292 | retval = -ESRCH; |
| 4293 | if (!p) |
| 4294 | goto out_unlock; |
| 4295 | |
| 4296 | retval = security_task_getscheduler(p); |
| 4297 | if (retval) |
| 4298 | goto out_unlock; |
| 4299 | |
| 4300 | lp.sched_priority = p->rt_priority; |
| 4301 | read_unlock(&tasklist_lock); |
| 4302 | |
| 4303 | /* |
| 4304 | * This one might sleep, we cannot do it with a spinlock held ... |
| 4305 | */ |
| 4306 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| 4307 | |
| 4308 | out_nounlock: |
| 4309 | return retval; |
| 4310 | |
| 4311 | out_unlock: |
| 4312 | read_unlock(&tasklist_lock); |
| 4313 | return retval; |
| 4314 | } |
| 4315 | |
| 4316 | long sched_setaffinity(pid_t pid, cpumask_t new_mask) |
| 4317 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | cpumask_t cpus_allowed; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4319 | struct task_struct *p; |
| 4320 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 4322 | mutex_lock(&sched_hotcpu_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | read_lock(&tasklist_lock); |
| 4324 | |
| 4325 | p = find_process_by_pid(pid); |
| 4326 | if (!p) { |
| 4327 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 4328 | mutex_unlock(&sched_hotcpu_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | return -ESRCH; |
| 4330 | } |
| 4331 | |
| 4332 | /* |
| 4333 | * It is not safe to call set_cpus_allowed with the |
| 4334 | * tasklist_lock held. We will bump the task_struct's |
| 4335 | * usage count and then drop tasklist_lock. |
| 4336 | */ |
| 4337 | get_task_struct(p); |
| 4338 | read_unlock(&tasklist_lock); |
| 4339 | |
| 4340 | retval = -EPERM; |
| 4341 | if ((current->euid != p->euid) && (current->euid != p->uid) && |
| 4342 | !capable(CAP_SYS_NICE)) |
| 4343 | goto out_unlock; |
| 4344 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 4345 | retval = security_task_setscheduler(p, 0, NULL); |
| 4346 | if (retval) |
| 4347 | goto out_unlock; |
| 4348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | cpus_allowed = cpuset_cpus_allowed(p); |
| 4350 | cpus_and(new_mask, new_mask, cpus_allowed); |
| 4351 | retval = set_cpus_allowed(p, new_mask); |
| 4352 | |
| 4353 | out_unlock: |
| 4354 | put_task_struct(p); |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 4355 | mutex_unlock(&sched_hotcpu_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | return retval; |
| 4357 | } |
| 4358 | |
| 4359 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
| 4360 | cpumask_t *new_mask) |
| 4361 | { |
| 4362 | if (len < sizeof(cpumask_t)) { |
| 4363 | memset(new_mask, 0, sizeof(cpumask_t)); |
| 4364 | } else if (len > sizeof(cpumask_t)) { |
| 4365 | len = sizeof(cpumask_t); |
| 4366 | } |
| 4367 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| 4368 | } |
| 4369 | |
| 4370 | /** |
| 4371 | * sys_sched_setaffinity - set the cpu affinity of a process |
| 4372 | * @pid: pid of the process |
| 4373 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 4374 | * @user_mask_ptr: user-space pointer to the new cpu mask |
| 4375 | */ |
| 4376 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, |
| 4377 | unsigned long __user *user_mask_ptr) |
| 4378 | { |
| 4379 | cpumask_t new_mask; |
| 4380 | int retval; |
| 4381 | |
| 4382 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); |
| 4383 | if (retval) |
| 4384 | return retval; |
| 4385 | |
| 4386 | return sched_setaffinity(pid, new_mask); |
| 4387 | } |
| 4388 | |
| 4389 | /* |
| 4390 | * Represents all cpu's present in the system |
| 4391 | * In systems capable of hotplug, this map could dynamically grow |
| 4392 | * as new cpu's are detected in the system via any platform specific |
| 4393 | * method, such as ACPI for e.g. |
| 4394 | */ |
| 4395 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4396 | cpumask_t cpu_present_map __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | EXPORT_SYMBOL(cpu_present_map); |
| 4398 | |
| 4399 | #ifndef CONFIG_SMP |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4400 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 4401 | EXPORT_SYMBOL(cpu_online_map); |
| 4402 | |
Andi Kleen | 4cef0c6 | 2006-01-11 22:44:57 +0100 | [diff] [blame] | 4403 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 4404 | EXPORT_SYMBOL(cpu_possible_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | #endif |
| 4406 | |
| 4407 | long sched_getaffinity(pid_t pid, cpumask_t *mask) |
| 4408 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4409 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 4412 | mutex_lock(&sched_hotcpu_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | read_lock(&tasklist_lock); |
| 4414 | |
| 4415 | retval = -ESRCH; |
| 4416 | p = find_process_by_pid(pid); |
| 4417 | if (!p) |
| 4418 | goto out_unlock; |
| 4419 | |
David Quigley | e7834f8 | 2006-06-23 02:03:59 -0700 | [diff] [blame] | 4420 | retval = security_task_getscheduler(p); |
| 4421 | if (retval) |
| 4422 | goto out_unlock; |
| 4423 | |
Jack Steiner | 2f7016d | 2006-02-01 03:05:18 -0800 | [diff] [blame] | 4424 | cpus_and(*mask, p->cpus_allowed, cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | |
| 4426 | out_unlock: |
| 4427 | read_unlock(&tasklist_lock); |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 4428 | mutex_unlock(&sched_hotcpu_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | if (retval) |
| 4430 | return retval; |
| 4431 | |
| 4432 | return 0; |
| 4433 | } |
| 4434 | |
| 4435 | /** |
| 4436 | * sys_sched_getaffinity - get the cpu affinity of a process |
| 4437 | * @pid: pid of the process |
| 4438 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 4439 | * @user_mask_ptr: user-space pointer to hold the current cpu mask |
| 4440 | */ |
| 4441 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, |
| 4442 | unsigned long __user *user_mask_ptr) |
| 4443 | { |
| 4444 | int ret; |
| 4445 | cpumask_t mask; |
| 4446 | |
| 4447 | if (len < sizeof(cpumask_t)) |
| 4448 | return -EINVAL; |
| 4449 | |
| 4450 | ret = sched_getaffinity(pid, &mask); |
| 4451 | if (ret < 0) |
| 4452 | return ret; |
| 4453 | |
| 4454 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) |
| 4455 | return -EFAULT; |
| 4456 | |
| 4457 | return sizeof(cpumask_t); |
| 4458 | } |
| 4459 | |
| 4460 | /** |
| 4461 | * sys_sched_yield - yield the current processor to other threads. |
| 4462 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4463 | * This function yields the current CPU by moving the calling thread |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | * to the expired array. If there are no other threads running on this |
| 4465 | * CPU then this function will return. |
| 4466 | */ |
| 4467 | asmlinkage long sys_sched_yield(void) |
| 4468 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4469 | struct rq *rq = this_rq_lock(); |
| 4470 | struct prio_array *array = current->array, *target = rq->expired; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | |
| 4472 | schedstat_inc(rq, yld_cnt); |
| 4473 | /* |
| 4474 | * We implement yielding by moving the task into the expired |
| 4475 | * queue. |
| 4476 | * |
| 4477 | * (special rule: RT tasks will just roundrobin in the active |
| 4478 | * array.) |
| 4479 | */ |
| 4480 | if (rt_task(current)) |
| 4481 | target = rq->active; |
| 4482 | |
Renaud Lienhart | 5927ad7 | 2005-09-10 00:26:20 -0700 | [diff] [blame] | 4483 | if (array->nr_active == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | schedstat_inc(rq, yld_act_empty); |
| 4485 | if (!rq->expired->nr_active) |
| 4486 | schedstat_inc(rq, yld_both_empty); |
| 4487 | } else if (!rq->expired->nr_active) |
| 4488 | schedstat_inc(rq, yld_exp_empty); |
| 4489 | |
| 4490 | if (array != target) { |
| 4491 | dequeue_task(current, array); |
| 4492 | enqueue_task(current, target); |
| 4493 | } else |
| 4494 | /* |
| 4495 | * requeue_task is cheaper so perform that if possible. |
| 4496 | */ |
| 4497 | requeue_task(current, array); |
| 4498 | |
| 4499 | /* |
| 4500 | * Since we are going to call schedule() anyway, there's |
| 4501 | * no need to preempt or enable interrupts: |
| 4502 | */ |
| 4503 | __release(rq->lock); |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 4504 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | _raw_spin_unlock(&rq->lock); |
| 4506 | preempt_enable_no_resched(); |
| 4507 | |
| 4508 | schedule(); |
| 4509 | |
| 4510 | return 0; |
| 4511 | } |
| 4512 | |
Andrew Morton | e7b3840 | 2006-06-30 01:56:00 -0700 | [diff] [blame] | 4513 | static void __cond_resched(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | { |
Ingo Molnar | 8e0a43d | 2006-06-23 02:05:23 -0700 | [diff] [blame] | 4515 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 4516 | __might_sleep(__FILE__, __LINE__); |
| 4517 | #endif |
Ingo Molnar | 5bbcfd9 | 2005-07-07 17:57:04 -0700 | [diff] [blame] | 4518 | /* |
| 4519 | * The BKS might be reacquired before we have dropped |
| 4520 | * PREEMPT_ACTIVE, which could trigger a second |
| 4521 | * cond_resched() call. |
| 4522 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | do { |
| 4524 | add_preempt_count(PREEMPT_ACTIVE); |
| 4525 | schedule(); |
| 4526 | sub_preempt_count(PREEMPT_ACTIVE); |
| 4527 | } while (need_resched()); |
| 4528 | } |
| 4529 | |
| 4530 | int __sched cond_resched(void) |
| 4531 | { |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 4532 | if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && |
| 4533 | system_state == SYSTEM_RUNNING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | __cond_resched(); |
| 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); |
| 4540 | |
| 4541 | /* |
| 4542 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, |
| 4543 | * call schedule, and on return reacquire the lock. |
| 4544 | * |
| 4545 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level |
| 4546 | * operations here to prevent schedule() from being called twice (once via |
| 4547 | * spin_unlock(), once by hand). |
| 4548 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4549 | int cond_resched_lock(spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | { |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4551 | int ret = 0; |
| 4552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | if (need_lockbreak(lock)) { |
| 4554 | spin_unlock(lock); |
| 4555 | cpu_relax(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4556 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | spin_lock(lock); |
| 4558 | } |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 4559 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
Ingo Molnar | 8a25d5d | 2006-07-03 00:24:54 -0700 | [diff] [blame] | 4560 | spin_release(&lock->dep_map, 1, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | _raw_spin_unlock(lock); |
| 4562 | preempt_enable_no_resched(); |
| 4563 | __cond_resched(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4564 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | spin_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | } |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 4567 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | EXPORT_SYMBOL(cond_resched_lock); |
| 4570 | |
| 4571 | int __sched cond_resched_softirq(void) |
| 4572 | { |
| 4573 | BUG_ON(!in_softirq()); |
| 4574 | |
Ingo Molnar | 9414232 | 2006-12-29 16:48:13 -0800 | [diff] [blame] | 4575 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
Thomas Gleixner | 98d82567 | 2007-05-23 13:58:18 -0700 | [diff] [blame] | 4576 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | __cond_resched(); |
| 4578 | local_bh_disable(); |
| 4579 | return 1; |
| 4580 | } |
| 4581 | return 0; |
| 4582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | EXPORT_SYMBOL(cond_resched_softirq); |
| 4584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | /** |
| 4586 | * yield - yield the current processor to other threads. |
| 4587 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 4588 | * This is a shortcut for kernel-space yielding - it marks the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | * thread runnable and calls sys_sched_yield(). |
| 4590 | */ |
| 4591 | void __sched yield(void) |
| 4592 | { |
| 4593 | set_current_state(TASK_RUNNING); |
| 4594 | sys_sched_yield(); |
| 4595 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | EXPORT_SYMBOL(yield); |
| 4597 | |
| 4598 | /* |
| 4599 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
| 4600 | * that process accounting knows that this is a task in IO wait state. |
| 4601 | * |
| 4602 | * But don't do that if it is a deliberate, throttling IO wait (this task |
| 4603 | * has set its backing_dev_info: the queue against which it should throttle) |
| 4604 | */ |
| 4605 | void __sched io_schedule(void) |
| 4606 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4607 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 4609 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | atomic_inc(&rq->nr_iowait); |
| 4611 | schedule(); |
| 4612 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 4613 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | EXPORT_SYMBOL(io_schedule); |
| 4616 | |
| 4617 | long __sched io_schedule_timeout(long timeout) |
| 4618 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4619 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | long ret; |
| 4621 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 4622 | delayacct_blkio_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | atomic_inc(&rq->nr_iowait); |
| 4624 | ret = schedule_timeout(timeout); |
| 4625 | atomic_dec(&rq->nr_iowait); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 4626 | delayacct_blkio_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | return ret; |
| 4628 | } |
| 4629 | |
| 4630 | /** |
| 4631 | * sys_sched_get_priority_max - return maximum RT priority. |
| 4632 | * @policy: scheduling class. |
| 4633 | * |
| 4634 | * this syscall returns the maximum rt_priority that can be used |
| 4635 | * by a given scheduling class. |
| 4636 | */ |
| 4637 | asmlinkage long sys_sched_get_priority_max(int policy) |
| 4638 | { |
| 4639 | int ret = -EINVAL; |
| 4640 | |
| 4641 | switch (policy) { |
| 4642 | case SCHED_FIFO: |
| 4643 | case SCHED_RR: |
| 4644 | ret = MAX_USER_RT_PRIO-1; |
| 4645 | break; |
| 4646 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4647 | case SCHED_BATCH: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | ret = 0; |
| 4649 | break; |
| 4650 | } |
| 4651 | return ret; |
| 4652 | } |
| 4653 | |
| 4654 | /** |
| 4655 | * sys_sched_get_priority_min - return minimum RT priority. |
| 4656 | * @policy: scheduling class. |
| 4657 | * |
| 4658 | * this syscall returns the minimum rt_priority that can be used |
| 4659 | * by a given scheduling class. |
| 4660 | */ |
| 4661 | asmlinkage long sys_sched_get_priority_min(int policy) |
| 4662 | { |
| 4663 | int ret = -EINVAL; |
| 4664 | |
| 4665 | switch (policy) { |
| 4666 | case SCHED_FIFO: |
| 4667 | case SCHED_RR: |
| 4668 | ret = 1; |
| 4669 | break; |
| 4670 | case SCHED_NORMAL: |
Ingo Molnar | b0a9499 | 2006-01-14 13:20:41 -0800 | [diff] [blame] | 4671 | case SCHED_BATCH: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | ret = 0; |
| 4673 | } |
| 4674 | return ret; |
| 4675 | } |
| 4676 | |
| 4677 | /** |
| 4678 | * sys_sched_rr_get_interval - return the default timeslice of a process. |
| 4679 | * @pid: pid of the process. |
| 4680 | * @interval: userspace pointer to the timeslice value. |
| 4681 | * |
| 4682 | * this syscall writes the default timeslice value of a given process |
| 4683 | * into the user-space timespec buffer. A value of '0' means infinity. |
| 4684 | */ |
| 4685 | asmlinkage |
| 4686 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) |
| 4687 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4688 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | int retval = -EINVAL; |
| 4690 | struct timespec t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | |
| 4692 | if (pid < 0) |
| 4693 | goto out_nounlock; |
| 4694 | |
| 4695 | retval = -ESRCH; |
| 4696 | read_lock(&tasklist_lock); |
| 4697 | p = find_process_by_pid(pid); |
| 4698 | if (!p) |
| 4699 | goto out_unlock; |
| 4700 | |
| 4701 | retval = security_task_getscheduler(p); |
| 4702 | if (retval) |
| 4703 | goto out_unlock; |
| 4704 | |
Peter Williams | b78709c | 2006-06-26 16:58:00 +1000 | [diff] [blame] | 4705 | jiffies_to_timespec(p->policy == SCHED_FIFO ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | 0 : task_timeslice(p), &t); |
| 4707 | read_unlock(&tasklist_lock); |
| 4708 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
| 4709 | out_nounlock: |
| 4710 | return retval; |
| 4711 | out_unlock: |
| 4712 | read_unlock(&tasklist_lock); |
| 4713 | return retval; |
| 4714 | } |
| 4715 | |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4716 | static const char stat_nam[] = "RSDTtZX"; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4717 | |
| 4718 | static void show_task(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | unsigned long free = 0; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4721 | unsigned state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4723 | state = p->state ? __ffs(p->state) + 1 : 0; |
Andreas Mohr | 2ed6e34 | 2006-07-10 04:43:52 -0700 | [diff] [blame] | 4724 | printk("%-13.13s %c", p->comm, |
| 4725 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | #if (BITS_PER_LONG == 32) |
| 4727 | if (state == TASK_RUNNING) |
| 4728 | printk(" running "); |
| 4729 | else |
| 4730 | printk(" %08lX ", thread_saved_pc(p)); |
| 4731 | #else |
| 4732 | if (state == TASK_RUNNING) |
| 4733 | printk(" running task "); |
| 4734 | else |
| 4735 | printk(" %016lx ", thread_saved_pc(p)); |
| 4736 | #endif |
| 4737 | #ifdef CONFIG_DEBUG_STACK_USAGE |
| 4738 | { |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 4739 | unsigned long *n = end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4740 | while (!*n) |
| 4741 | n++; |
Al Viro | 10ebffd | 2005-11-13 16:06:56 -0800 | [diff] [blame] | 4742 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | } |
| 4744 | #endif |
Ingo Molnar | 35f6f75 | 2007-04-06 21:18:06 +0200 | [diff] [blame] | 4745 | printk("%5lu %5d %6d", free, p->pid, p->parent->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | if (!p->mm) |
| 4747 | printk(" (L-TLB)\n"); |
| 4748 | else |
| 4749 | printk(" (NOTLB)\n"); |
| 4750 | |
| 4751 | if (state != TASK_RUNNING) |
| 4752 | show_stack(p, NULL); |
| 4753 | } |
| 4754 | |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 4755 | void show_state_filter(unsigned long state_filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4757 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | |
| 4759 | #if (BITS_PER_LONG == 32) |
| 4760 | printk("\n" |
Chris Caputo | 301827a | 2006-12-06 20:39:11 -0800 | [diff] [blame] | 4761 | " free sibling\n"); |
| 4762 | printk(" task PC stack pid father child younger older\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | #else |
| 4764 | printk("\n" |
Chris Caputo | 301827a | 2006-12-06 20:39:11 -0800 | [diff] [blame] | 4765 | " free sibling\n"); |
| 4766 | printk(" task PC stack pid father child younger older\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | #endif |
| 4768 | read_lock(&tasklist_lock); |
| 4769 | do_each_thread(g, p) { |
| 4770 | /* |
| 4771 | * reset the NMI-timeout, listing all files on a slow |
| 4772 | * console might take alot of time: |
| 4773 | */ |
| 4774 | touch_nmi_watchdog(); |
Ingo Molnar | 39bc89f | 2007-04-25 20:50:03 -0700 | [diff] [blame] | 4775 | if (!state_filter || (p->state & state_filter)) |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 4776 | show_task(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | } while_each_thread(g, p); |
| 4778 | |
Jeremy Fitzhardinge | 04c9167 | 2007-05-08 00:28:05 -0700 | [diff] [blame] | 4779 | touch_all_softlockup_watchdogs(); |
| 4780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | read_unlock(&tasklist_lock); |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame] | 4782 | /* |
| 4783 | * Only show locks if all tasks are dumped: |
| 4784 | */ |
| 4785 | if (state_filter == -1) |
| 4786 | debug_show_all_locks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | } |
| 4788 | |
Ingo Molnar | 1df2105 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4789 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) |
| 4790 | { |
| 4791 | /* nothing yet */ |
| 4792 | } |
| 4793 | |
Ingo Molnar | f340c0d | 2005-06-28 16:40:42 +0200 | [diff] [blame] | 4794 | /** |
| 4795 | * init_idle - set up an idle thread for a given CPU |
| 4796 | * @idle: task in question |
| 4797 | * @cpu: cpu the idle task belongs to |
| 4798 | * |
| 4799 | * NOTE: this function does not set the idle thread's NEED_RESCHED |
| 4800 | * flag, to make booting more robust. |
| 4801 | */ |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 4802 | void __cpuinit init_idle(struct task_struct *idle, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4804 | struct rq *rq = cpu_rq(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | unsigned long flags; |
| 4806 | |
Ingo Molnar | 81c29a8 | 2006-03-07 21:55:27 -0800 | [diff] [blame] | 4807 | idle->timestamp = sched_clock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | idle->sleep_avg = 0; |
| 4809 | idle->array = NULL; |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 4810 | idle->prio = idle->normal_prio = MAX_PRIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | idle->state = TASK_RUNNING; |
| 4812 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
| 4813 | set_task_cpu(idle, cpu); |
| 4814 | |
| 4815 | spin_lock_irqsave(&rq->lock, flags); |
| 4816 | rq->curr = rq->idle = idle; |
Nick Piggin | 4866cde | 2005-06-25 14:57:23 -0700 | [diff] [blame] | 4817 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
| 4818 | idle->oncpu = 1; |
| 4819 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4820 | spin_unlock_irqrestore(&rq->lock, flags); |
| 4821 | |
| 4822 | /* Set the preempt count _outside_ the spinlocks! */ |
| 4823 | #if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL) |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 4824 | task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | #else |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 4826 | task_thread_info(idle)->preempt_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | #endif |
| 4828 | } |
| 4829 | |
| 4830 | /* |
| 4831 | * In a system that switches off the HZ timer nohz_cpu_mask |
| 4832 | * indicates which cpus entered this state. This is used |
| 4833 | * in the rcu update to wait only for active cpus. For system |
| 4834 | * which do not switch off the HZ timer nohz_cpu_mask should |
| 4835 | * always be CPU_MASK_NONE. |
| 4836 | */ |
| 4837 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; |
| 4838 | |
| 4839 | #ifdef CONFIG_SMP |
| 4840 | /* |
| 4841 | * This is how migration works: |
| 4842 | * |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4843 | * 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] | 4844 | * runqueue and wake up that CPU's migration thread. |
| 4845 | * 2) we down() the locked semaphore => thread blocks. |
| 4846 | * 3) migration thread wakes up (implicitly it forces the migrated |
| 4847 | * thread off the CPU) |
| 4848 | * 4) it gets the migration request and checks whether the migrated |
| 4849 | * task is still in the wrong runqueue. |
| 4850 | * 5) if it's in the wrong runqueue then the migration thread removes |
| 4851 | * it and puts it into the right queue. |
| 4852 | * 6) migration thread up()s the semaphore. |
| 4853 | * 7) we wake up and the migration is done. |
| 4854 | */ |
| 4855 | |
| 4856 | /* |
| 4857 | * Change a given task's CPU affinity. Migrate the thread to a |
| 4858 | * proper CPU and schedule it away if the CPU it's executing on |
| 4859 | * is removed from the allowed bitmask. |
| 4860 | * |
| 4861 | * NOTE: the caller must have a valid reference to the task, the |
| 4862 | * task must not exit() & deallocate itself prematurely. The |
| 4863 | * call is not atomic; no spinlocks may be held. |
| 4864 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 4865 | int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4867 | struct migration_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4869 | struct rq *rq; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4870 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | |
| 4872 | rq = task_rq_lock(p, &flags); |
| 4873 | if (!cpus_intersects(new_mask, cpu_online_map)) { |
| 4874 | ret = -EINVAL; |
| 4875 | goto out; |
| 4876 | } |
| 4877 | |
| 4878 | p->cpus_allowed = new_mask; |
| 4879 | /* Can the task run on the task's current CPU? If so, we're done */ |
| 4880 | if (cpu_isset(task_cpu(p), new_mask)) |
| 4881 | goto out; |
| 4882 | |
| 4883 | if (migrate_task(p, any_online_cpu(new_mask), &req)) { |
| 4884 | /* Need help from migration thread: drop lock and wait. */ |
| 4885 | task_rq_unlock(rq, &flags); |
| 4886 | wake_up_process(rq->migration_thread); |
| 4887 | wait_for_completion(&req.done); |
| 4888 | tlb_migrate_finish(p->mm); |
| 4889 | return 0; |
| 4890 | } |
| 4891 | out: |
| 4892 | task_rq_unlock(rq, &flags); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 4893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | return ret; |
| 4895 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | EXPORT_SYMBOL_GPL(set_cpus_allowed); |
| 4897 | |
| 4898 | /* |
| 4899 | * Move (not current) task off this cpu, onto dest cpu. We're doing |
| 4900 | * this because either it can't run here any more (set_cpus_allowed() |
| 4901 | * away from this CPU, or CPU going down), or because we're |
| 4902 | * attempting to rebalance this task on exec (sched_exec). |
| 4903 | * |
| 4904 | * So we race with normal scheduler movements, but that's OK, as long |
| 4905 | * as the task is no longer on this CPU. |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4906 | * |
| 4907 | * Returns non-zero if task was successfully migrated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | */ |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4909 | 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] | 4910 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4911 | struct rq *rq_dest, *rq_src; |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4912 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | |
| 4914 | if (unlikely(cpu_is_offline(dest_cpu))) |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4915 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | |
| 4917 | rq_src = cpu_rq(src_cpu); |
| 4918 | rq_dest = cpu_rq(dest_cpu); |
| 4919 | |
| 4920 | double_rq_lock(rq_src, rq_dest); |
| 4921 | /* Already moved. */ |
| 4922 | if (task_cpu(p) != src_cpu) |
| 4923 | goto out; |
| 4924 | /* Affinity changed (again). */ |
| 4925 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) |
| 4926 | goto out; |
| 4927 | |
| 4928 | set_task_cpu(p, dest_cpu); |
| 4929 | if (p->array) { |
| 4930 | /* |
| 4931 | * Sync timestamp with rq_dest's before activating. |
| 4932 | * The same thing could be achieved by doing this step |
| 4933 | * afterwards, and pretending it was a local activate. |
| 4934 | * This way is cleaner and logically correct. |
| 4935 | */ |
Mike Galbraith | b18ec80 | 2006-12-10 02:20:31 -0800 | [diff] [blame] | 4936 | p->timestamp = p->timestamp - rq_src->most_recent_timestamp |
| 4937 | + rq_dest->most_recent_timestamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | deactivate_task(p, rq_src); |
Peter Williams | 0a565f7 | 2006-07-10 04:43:51 -0700 | [diff] [blame] | 4939 | __activate_task(p, rq_dest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | if (TASK_PREEMPTS_CURR(p, rq_dest)) |
| 4941 | resched_task(rq_dest->curr); |
| 4942 | } |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4943 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | out: |
| 4945 | double_rq_unlock(rq_src, rq_dest); |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 4946 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | /* |
| 4950 | * migration_thread - this is a highprio system thread that performs |
| 4951 | * thread migration by bumping thread off CPU then 'pushing' onto |
| 4952 | * another runqueue. |
| 4953 | */ |
Ingo Molnar | 95cdf3b | 2005-09-10 00:26:11 -0700 | [diff] [blame] | 4954 | static int migration_thread(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | int cpu = (long)data; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4957 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | |
| 4959 | rq = cpu_rq(cpu); |
| 4960 | BUG_ON(rq->migration_thread != current); |
| 4961 | |
| 4962 | set_current_state(TASK_INTERRUPTIBLE); |
| 4963 | while (!kthread_should_stop()) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4964 | struct migration_req *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 4967 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | |
| 4969 | spin_lock_irq(&rq->lock); |
| 4970 | |
| 4971 | if (cpu_is_offline(cpu)) { |
| 4972 | spin_unlock_irq(&rq->lock); |
| 4973 | goto wait_to_die; |
| 4974 | } |
| 4975 | |
| 4976 | if (rq->active_balance) { |
| 4977 | active_load_balance(rq, cpu); |
| 4978 | rq->active_balance = 0; |
| 4979 | } |
| 4980 | |
| 4981 | head = &rq->migration_queue; |
| 4982 | |
| 4983 | if (list_empty(head)) { |
| 4984 | spin_unlock_irq(&rq->lock); |
| 4985 | schedule(); |
| 4986 | set_current_state(TASK_INTERRUPTIBLE); |
| 4987 | continue; |
| 4988 | } |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 4989 | req = list_entry(head->next, struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | list_del_init(head->next); |
| 4991 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 4992 | spin_unlock(&rq->lock); |
| 4993 | __migrate_task(req->task, cpu, req->dest_cpu); |
| 4994 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | |
| 4996 | complete(&req->done); |
| 4997 | } |
| 4998 | __set_current_state(TASK_RUNNING); |
| 4999 | return 0; |
| 5000 | |
| 5001 | wait_to_die: |
| 5002 | /* Wait for kthread_stop */ |
| 5003 | set_current_state(TASK_INTERRUPTIBLE); |
| 5004 | while (!kthread_should_stop()) { |
| 5005 | schedule(); |
| 5006 | set_current_state(TASK_INTERRUPTIBLE); |
| 5007 | } |
| 5008 | __set_current_state(TASK_RUNNING); |
| 5009 | return 0; |
| 5010 | } |
| 5011 | |
| 5012 | #ifdef CONFIG_HOTPLUG_CPU |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5013 | /* |
| 5014 | * Figure out where task on dead CPU should go, use force if neccessary. |
| 5015 | * NOTE: interrupts should be disabled by the caller |
| 5016 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5017 | 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] | 5018 | { |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5019 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | cpumask_t mask; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5021 | struct rq *rq; |
| 5022 | int dest_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5024 | restart: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | /* On same node? */ |
| 5026 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5027 | cpus_and(mask, mask, p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | dest_cpu = any_online_cpu(mask); |
| 5029 | |
| 5030 | /* On any allowed CPU? */ |
| 5031 | if (dest_cpu == NR_CPUS) |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5032 | dest_cpu = any_online_cpu(p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | |
| 5034 | /* No more Mr. Nice Guy. */ |
| 5035 | if (dest_cpu == NR_CPUS) { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5036 | rq = task_rq_lock(p, &flags); |
| 5037 | cpus_setall(p->cpus_allowed); |
| 5038 | dest_cpu = any_online_cpu(p->cpus_allowed); |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5039 | task_rq_unlock(rq, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | |
| 5041 | /* |
| 5042 | * Don't tell them about moving exiting tasks or |
| 5043 | * kernel threads (both mm NULL), since they never |
| 5044 | * leave kernel. |
| 5045 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5046 | if (p->mm && printk_ratelimit()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5047 | printk(KERN_INFO "process %d (%s) no " |
| 5048 | "longer affine to cpu%d\n", |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5049 | p->pid, p->comm, dead_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5051 | if (!__migrate_task(p, dead_cpu, dest_cpu)) |
Kirill Korotaev | efc3081 | 2006-06-27 02:54:32 -0700 | [diff] [blame] | 5052 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | /* |
| 5056 | * While a dead CPU has no uninterruptible tasks queued at this point, |
| 5057 | * it might still have a nonzero ->nr_uninterruptible counter, because |
| 5058 | * for performance reasons the counter is not stricly tracking tasks to |
| 5059 | * their home CPUs. So we just add the counter to another CPU's counter, |
| 5060 | * to keep the global sum constant after CPU-down: |
| 5061 | */ |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5062 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5064 | struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | unsigned long flags; |
| 5066 | |
| 5067 | local_irq_save(flags); |
| 5068 | double_rq_lock(rq_src, rq_dest); |
| 5069 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; |
| 5070 | rq_src->nr_uninterruptible = 0; |
| 5071 | double_rq_unlock(rq_src, rq_dest); |
| 5072 | local_irq_restore(flags); |
| 5073 | } |
| 5074 | |
| 5075 | /* Run through task list and migrate tasks from the dead cpu. */ |
| 5076 | static void migrate_live_tasks(int src_cpu) |
| 5077 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5078 | struct task_struct *p, *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | |
| 5080 | write_lock_irq(&tasklist_lock); |
| 5081 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5082 | do_each_thread(t, p) { |
| 5083 | if (p == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | continue; |
| 5085 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5086 | if (task_cpu(p) == src_cpu) |
| 5087 | move_task_off_dead_cpu(src_cpu, p); |
| 5088 | } while_each_thread(t, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | |
| 5090 | write_unlock_irq(&tasklist_lock); |
| 5091 | } |
| 5092 | |
| 5093 | /* Schedules idle task to be the next runnable task on current CPU. |
| 5094 | * 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] | 5095 | * the _front_ of the runqueue. Used by CPU offline code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | */ |
| 5097 | void sched_idle_next(void) |
| 5098 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5099 | int this_cpu = smp_processor_id(); |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5100 | struct rq *rq = cpu_rq(this_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | struct task_struct *p = rq->idle; |
| 5102 | unsigned long flags; |
| 5103 | |
| 5104 | /* cpu has to be offline */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5105 | BUG_ON(cpu_online(this_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5107 | /* |
| 5108 | * Strictly not necessary since rest of the CPUs are stopped by now |
| 5109 | * and interrupts disabled on the current cpu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | */ |
| 5111 | spin_lock_irqsave(&rq->lock, flags); |
| 5112 | |
| 5113 | __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5114 | |
| 5115 | /* Add idle task to the _front_ of its priority queue: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5116 | __activate_idle_task(p, rq); |
| 5117 | |
| 5118 | spin_unlock_irqrestore(&rq->lock, flags); |
| 5119 | } |
| 5120 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5121 | /* |
| 5122 | * 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] | 5123 | * offline. |
| 5124 | */ |
| 5125 | void idle_task_exit(void) |
| 5126 | { |
| 5127 | struct mm_struct *mm = current->active_mm; |
| 5128 | |
| 5129 | BUG_ON(cpu_online(smp_processor_id())); |
| 5130 | |
| 5131 | if (mm != &init_mm) |
| 5132 | switch_mm(mm, &init_mm, current); |
| 5133 | mmdrop(mm); |
| 5134 | } |
| 5135 | |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5136 | /* called under rq->lock with disabled interrupts */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5137 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5139 | struct rq *rq = cpu_rq(dead_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | |
| 5141 | /* Must be exiting, otherwise would be on tasklist. */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5142 | BUG_ON(p->exit_state != EXIT_ZOMBIE && p->exit_state != EXIT_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | |
| 5144 | /* Cannot have done final schedule yet: would have vanished. */ |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 5145 | BUG_ON(p->state == TASK_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5147 | get_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | |
| 5149 | /* |
| 5150 | * Drop lock around migration; if someone else moves it, |
| 5151 | * that's OK. No task can be added to this CPU, so iteration is |
| 5152 | * fine. |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5153 | * NOTE: interrupts should be left disabled --dev@ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | */ |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5155 | spin_unlock(&rq->lock); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5156 | move_task_off_dead_cpu(dead_cpu, p); |
Kirill Korotaev | 054b910 | 2006-12-10 02:20:11 -0800 | [diff] [blame] | 5157 | spin_lock(&rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5159 | put_task_struct(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ |
| 5163 | static void migrate_dead_tasks(unsigned int dead_cpu) |
| 5164 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5165 | struct rq *rq = cpu_rq(dead_cpu); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5166 | unsigned int arr, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | |
| 5168 | for (arr = 0; arr < 2; arr++) { |
| 5169 | for (i = 0; i < MAX_PRIO; i++) { |
| 5170 | struct list_head *list = &rq->arrays[arr].queue[i]; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | while (!list_empty(list)) |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 5173 | migrate_dead(dead_cpu, list_entry(list->next, |
| 5174 | struct task_struct, run_list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | } |
| 5176 | } |
| 5177 | } |
| 5178 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 5179 | |
| 5180 | /* |
| 5181 | * migration_call - callback that gets triggered when a CPU is added. |
| 5182 | * Here we can start up the necessary migration thread for the new CPU. |
| 5183 | */ |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5184 | static int __cpuinit |
| 5185 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | struct task_struct *p; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5188 | int cpu = (long)hcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5190 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | |
| 5192 | switch (action) { |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 5193 | case CPU_LOCK_ACQUIRE: |
| 5194 | mutex_lock(&sched_hotcpu_mutex); |
| 5195 | break; |
| 5196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5198 | case CPU_UP_PREPARE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | p = kthread_create(migration_thread, hcpu, "migration/%d",cpu); |
| 5200 | if (IS_ERR(p)) |
| 5201 | return NOTIFY_BAD; |
| 5202 | p->flags |= PF_NOFREEZE; |
| 5203 | kthread_bind(p, cpu); |
| 5204 | /* Must be high prio: stop_machine expects to yield to it. */ |
| 5205 | rq = task_rq_lock(p, &flags); |
| 5206 | __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1); |
| 5207 | task_rq_unlock(rq, &flags); |
| 5208 | cpu_rq(cpu)->migration_thread = p; |
| 5209 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5212 | case CPU_ONLINE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | /* Strictly unneccessary, as first user will wake it. */ |
| 5214 | wake_up_process(cpu_rq(cpu)->migration_thread); |
| 5215 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | #ifdef CONFIG_HOTPLUG_CPU |
| 5218 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5219 | case CPU_UP_CANCELED_FROZEN: |
Heiko Carstens | fc75cdf | 2006-06-25 05:49:10 -0700 | [diff] [blame] | 5220 | if (!cpu_rq(cpu)->migration_thread) |
| 5221 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
Heiko Carstens | a4c4af7 | 2005-11-07 00:58:38 -0800 | [diff] [blame] | 5223 | kthread_bind(cpu_rq(cpu)->migration_thread, |
| 5224 | any_online_cpu(cpu_online_map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | kthread_stop(cpu_rq(cpu)->migration_thread); |
| 5226 | cpu_rq(cpu)->migration_thread = NULL; |
| 5227 | break; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 5230 | case CPU_DEAD_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | migrate_live_tasks(cpu); |
| 5232 | rq = cpu_rq(cpu); |
| 5233 | kthread_stop(rq->migration_thread); |
| 5234 | rq->migration_thread = NULL; |
| 5235 | /* Idle task back to normal (off runqueue, low prio) */ |
| 5236 | rq = task_rq_lock(rq->idle, &flags); |
| 5237 | deactivate_task(rq->idle, rq); |
| 5238 | rq->idle->static_prio = MAX_PRIO; |
| 5239 | __setscheduler(rq->idle, SCHED_NORMAL, 0); |
| 5240 | migrate_dead_tasks(cpu); |
| 5241 | task_rq_unlock(rq, &flags); |
| 5242 | migrate_nr_uninterruptible(rq); |
| 5243 | BUG_ON(rq->nr_running != 0); |
| 5244 | |
| 5245 | /* No need to migrate the tasks: it was best-effort if |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 5246 | * they didn't take sched_hotcpu_mutex. Just wake up |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | * the requestors. */ |
| 5248 | spin_lock_irq(&rq->lock); |
| 5249 | while (!list_empty(&rq->migration_queue)) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5250 | struct migration_req *req; |
| 5251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | req = list_entry(rq->migration_queue.next, |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5253 | struct migration_req, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | list_del_init(&req->list); |
| 5255 | complete(&req->done); |
| 5256 | } |
| 5257 | spin_unlock_irq(&rq->lock); |
| 5258 | break; |
| 5259 | #endif |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 5260 | case CPU_LOCK_RELEASE: |
| 5261 | mutex_unlock(&sched_hotcpu_mutex); |
| 5262 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | } |
| 5264 | return NOTIFY_OK; |
| 5265 | } |
| 5266 | |
| 5267 | /* Register at highest priority so that task migration (migrate_all_tasks) |
| 5268 | * happens before everything else. |
| 5269 | */ |
Chandra Seetharaman | 26c2143 | 2006-06-27 02:54:10 -0700 | [diff] [blame] | 5270 | static struct notifier_block __cpuinitdata migration_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | .notifier_call = migration_call, |
| 5272 | .priority = 10 |
| 5273 | }; |
| 5274 | |
| 5275 | int __init migration_init(void) |
| 5276 | { |
| 5277 | void *cpu = (void *)(long)smp_processor_id(); |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 5278 | int err; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5279 | |
| 5280 | /* Start one for the boot CPU: */ |
Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 5281 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
| 5282 | BUG_ON(err == NOTIFY_BAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
| 5284 | register_cpu_notifier(&migration_notifier); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | return 0; |
| 5287 | } |
| 5288 | #endif |
| 5289 | |
| 5290 | #ifdef CONFIG_SMP |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 5291 | |
| 5292 | /* Number of possible processor ids */ |
| 5293 | int nr_cpu_ids __read_mostly = NR_CPUS; |
| 5294 | EXPORT_SYMBOL(nr_cpu_ids); |
| 5295 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5296 | #undef SCHED_DOMAIN_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | #ifdef SCHED_DOMAIN_DEBUG |
| 5298 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
| 5299 | { |
| 5300 | int level = 0; |
| 5301 | |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 5302 | if (!sd) { |
| 5303 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); |
| 5304 | return; |
| 5305 | } |
| 5306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
| 5308 | |
| 5309 | do { |
| 5310 | int i; |
| 5311 | char str[NR_CPUS]; |
| 5312 | struct sched_group *group = sd->groups; |
| 5313 | cpumask_t groupmask; |
| 5314 | |
| 5315 | cpumask_scnprintf(str, NR_CPUS, sd->span); |
| 5316 | cpus_clear(groupmask); |
| 5317 | |
| 5318 | printk(KERN_DEBUG); |
| 5319 | for (i = 0; i < level + 1; i++) |
| 5320 | printk(" "); |
| 5321 | printk("domain %d: ", level); |
| 5322 | |
| 5323 | if (!(sd->flags & SD_LOAD_BALANCE)) { |
| 5324 | printk("does not load-balance\n"); |
| 5325 | if (sd->parent) |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 5326 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
| 5327 | " has parent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | break; |
| 5329 | } |
| 5330 | |
| 5331 | printk("span %s\n", str); |
| 5332 | |
| 5333 | if (!cpu_isset(cpu, sd->span)) |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 5334 | printk(KERN_ERR "ERROR: domain->span does not contain " |
| 5335 | "CPU%d\n", cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | if (!cpu_isset(cpu, group->cpumask)) |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 5337 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
| 5338 | " CPU%d\n", cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | |
| 5340 | printk(KERN_DEBUG); |
| 5341 | for (i = 0; i < level + 2; i++) |
| 5342 | printk(" "); |
| 5343 | printk("groups:"); |
| 5344 | do { |
| 5345 | if (!group) { |
| 5346 | printk("\n"); |
| 5347 | printk(KERN_ERR "ERROR: group is NULL\n"); |
| 5348 | break; |
| 5349 | } |
| 5350 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 5351 | if (!group->__cpu_power) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | printk("\n"); |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 5353 | printk(KERN_ERR "ERROR: domain->cpu_power not " |
| 5354 | "set\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | } |
| 5356 | |
| 5357 | if (!cpus_weight(group->cpumask)) { |
| 5358 | printk("\n"); |
| 5359 | printk(KERN_ERR "ERROR: empty group\n"); |
| 5360 | } |
| 5361 | |
| 5362 | if (cpus_intersects(groupmask, group->cpumask)) { |
| 5363 | printk("\n"); |
| 5364 | printk(KERN_ERR "ERROR: repeated CPUs\n"); |
| 5365 | } |
| 5366 | |
| 5367 | cpus_or(groupmask, groupmask, group->cpumask); |
| 5368 | |
| 5369 | cpumask_scnprintf(str, NR_CPUS, group->cpumask); |
| 5370 | printk(" %s", str); |
| 5371 | |
| 5372 | group = group->next; |
| 5373 | } while (group != sd->groups); |
| 5374 | printk("\n"); |
| 5375 | |
| 5376 | if (!cpus_equal(sd->span, groupmask)) |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 5377 | printk(KERN_ERR "ERROR: groups don't span " |
| 5378 | "domain->span\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | |
| 5380 | level++; |
| 5381 | sd = sd->parent; |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 5382 | if (!sd) |
| 5383 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | |
Miguel Ojeda Sandonis | 33859f7 | 2006-12-10 02:20:38 -0800 | [diff] [blame] | 5385 | if (!cpus_subset(groupmask, sd->span)) |
| 5386 | printk(KERN_ERR "ERROR: parent span is not a superset " |
| 5387 | "of domain->span\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | |
| 5389 | } while (sd); |
| 5390 | } |
| 5391 | #else |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5392 | # define sched_domain_debug(sd, cpu) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | #endif |
| 5394 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5395 | static int sd_degenerate(struct sched_domain *sd) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5396 | { |
| 5397 | if (cpus_weight(sd->span) == 1) |
| 5398 | return 1; |
| 5399 | |
| 5400 | /* Following flags need at least 2 groups */ |
| 5401 | if (sd->flags & (SD_LOAD_BALANCE | |
| 5402 | SD_BALANCE_NEWIDLE | |
| 5403 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 5404 | SD_BALANCE_EXEC | |
| 5405 | SD_SHARE_CPUPOWER | |
| 5406 | SD_SHARE_PKG_RESOURCES)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5407 | if (sd->groups != sd->groups->next) |
| 5408 | return 0; |
| 5409 | } |
| 5410 | |
| 5411 | /* Following flags don't use groups */ |
| 5412 | if (sd->flags & (SD_WAKE_IDLE | |
| 5413 | SD_WAKE_AFFINE | |
| 5414 | SD_WAKE_BALANCE)) |
| 5415 | return 0; |
| 5416 | |
| 5417 | return 1; |
| 5418 | } |
| 5419 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5420 | static int |
| 5421 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5422 | { |
| 5423 | unsigned long cflags = sd->flags, pflags = parent->flags; |
| 5424 | |
| 5425 | if (sd_degenerate(parent)) |
| 5426 | return 1; |
| 5427 | |
| 5428 | if (!cpus_equal(sd->span, parent->span)) |
| 5429 | return 0; |
| 5430 | |
| 5431 | /* Does parent contain flags not in child? */ |
| 5432 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ |
| 5433 | if (cflags & SD_WAKE_AFFINE) |
| 5434 | pflags &= ~SD_WAKE_BALANCE; |
| 5435 | /* Flags needing groups don't count if only 1 group in parent */ |
| 5436 | if (parent->groups == parent->groups->next) { |
| 5437 | pflags &= ~(SD_LOAD_BALANCE | |
| 5438 | SD_BALANCE_NEWIDLE | |
| 5439 | SD_BALANCE_FORK | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 5440 | SD_BALANCE_EXEC | |
| 5441 | SD_SHARE_CPUPOWER | |
| 5442 | SD_SHARE_PKG_RESOURCES); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5443 | } |
| 5444 | if (~cflags & pflags) |
| 5445 | return 0; |
| 5446 | |
| 5447 | return 1; |
| 5448 | } |
| 5449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | /* |
| 5451 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
| 5452 | * hold the hotplug lock. |
| 5453 | */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5454 | static void cpu_attach_domain(struct sched_domain *sd, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 5456 | struct rq *rq = cpu_rq(cpu); |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5457 | struct sched_domain *tmp; |
| 5458 | |
| 5459 | /* Remove the sched domains which do not contribute to scheduling. */ |
| 5460 | for (tmp = sd; tmp; tmp = tmp->parent) { |
| 5461 | struct sched_domain *parent = tmp->parent; |
| 5462 | if (!parent) |
| 5463 | break; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 5464 | if (sd_parent_degenerate(tmp, parent)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5465 | tmp->parent = parent->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 5466 | if (parent->parent) |
| 5467 | parent->parent->child = tmp; |
| 5468 | } |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5469 | } |
| 5470 | |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 5471 | if (sd && sd_degenerate(sd)) { |
Suresh Siddha | 245af2c | 2005-06-25 14:57:25 -0700 | [diff] [blame] | 5472 | sd = sd->parent; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 5473 | if (sd) |
| 5474 | sd->child = NULL; |
| 5475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | |
| 5477 | sched_domain_debug(sd, cpu); |
| 5478 | |
Nick Piggin | 674311d | 2005-06-25 14:57:27 -0700 | [diff] [blame] | 5479 | rcu_assign_pointer(rq->sd, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | /* cpus with isolated domains */ |
Tim Chen | 67af63a | 2006-12-22 01:07:50 -0800 | [diff] [blame] | 5483 | static cpumask_t cpu_isolated_map = CPU_MASK_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | |
| 5485 | /* Setup the mask of cpus configured for isolated domains */ |
| 5486 | static int __init isolated_cpu_setup(char *str) |
| 5487 | { |
| 5488 | int ints[NR_CPUS], i; |
| 5489 | |
| 5490 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 5491 | cpus_clear(cpu_isolated_map); |
| 5492 | for (i = 1; i <= ints[0]; i++) |
| 5493 | if (ints[i] < NR_CPUS) |
| 5494 | cpu_set(ints[i], cpu_isolated_map); |
| 5495 | return 1; |
| 5496 | } |
| 5497 | |
| 5498 | __setup ("isolcpus=", isolated_cpu_setup); |
| 5499 | |
| 5500 | /* |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5501 | * init_sched_build_groups takes the cpumask we wish to span, and a pointer |
| 5502 | * to a function which identifies what group(along with sched group) a CPU |
| 5503 | * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS |
| 5504 | * (due to the fact that we keep track of groups covered with a cpumask_t). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | * |
| 5506 | * init_sched_build_groups will build a circular linked list of the groups |
| 5507 | * covered by the given span, and will set each group's ->cpumask correctly, |
| 5508 | * and ->cpu_power to 0. |
| 5509 | */ |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 5510 | static void |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5511 | init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map, |
| 5512 | int (*group_fn)(int cpu, const cpumask_t *cpu_map, |
| 5513 | struct sched_group **sg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | { |
| 5515 | struct sched_group *first = NULL, *last = NULL; |
| 5516 | cpumask_t covered = CPU_MASK_NONE; |
| 5517 | int i; |
| 5518 | |
| 5519 | for_each_cpu_mask(i, span) { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5520 | struct sched_group *sg; |
| 5521 | int group = group_fn(i, cpu_map, &sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | int j; |
| 5523 | |
| 5524 | if (cpu_isset(i, covered)) |
| 5525 | continue; |
| 5526 | |
| 5527 | sg->cpumask = CPU_MASK_NONE; |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 5528 | sg->__cpu_power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | |
| 5530 | for_each_cpu_mask(j, span) { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5531 | if (group_fn(j, cpu_map, NULL) != group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | continue; |
| 5533 | |
| 5534 | cpu_set(j, covered); |
| 5535 | cpu_set(j, sg->cpumask); |
| 5536 | } |
| 5537 | if (!first) |
| 5538 | first = sg; |
| 5539 | if (last) |
| 5540 | last->next = sg; |
| 5541 | last = sg; |
| 5542 | } |
| 5543 | last->next = first; |
| 5544 | } |
| 5545 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5546 | #define SD_NODES_PER_DOMAIN 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5548 | #ifdef CONFIG_NUMA |
akpm@osdl.org | 198e2f1 | 2006-01-12 01:05:30 -0800 | [diff] [blame] | 5549 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5550 | /** |
| 5551 | * find_next_best_node - find the next node to include in a sched_domain |
| 5552 | * @node: node whose sched_domain we're building |
| 5553 | * @used_nodes: nodes already in the sched_domain |
| 5554 | * |
| 5555 | * Find the next node to include in a given scheduling domain. Simply |
| 5556 | * finds the closest node not already in the @used_nodes map. |
| 5557 | * |
| 5558 | * Should use nodemask_t. |
| 5559 | */ |
| 5560 | static int find_next_best_node(int node, unsigned long *used_nodes) |
| 5561 | { |
| 5562 | int i, n, val, min_val, best_node = 0; |
| 5563 | |
| 5564 | min_val = INT_MAX; |
| 5565 | |
| 5566 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 5567 | /* Start at @node */ |
| 5568 | n = (node + i) % MAX_NUMNODES; |
| 5569 | |
| 5570 | if (!nr_cpus_node(n)) |
| 5571 | continue; |
| 5572 | |
| 5573 | /* Skip already used nodes */ |
| 5574 | if (test_bit(n, used_nodes)) |
| 5575 | continue; |
| 5576 | |
| 5577 | /* Simple min distance search */ |
| 5578 | val = node_distance(node, n); |
| 5579 | |
| 5580 | if (val < min_val) { |
| 5581 | min_val = val; |
| 5582 | best_node = n; |
| 5583 | } |
| 5584 | } |
| 5585 | |
| 5586 | set_bit(best_node, used_nodes); |
| 5587 | return best_node; |
| 5588 | } |
| 5589 | |
| 5590 | /** |
| 5591 | * sched_domain_node_span - get a cpumask for a node's sched_domain |
| 5592 | * @node: node whose cpumask we're constructing |
| 5593 | * @size: number of nodes to include in this span |
| 5594 | * |
| 5595 | * Given a node, construct a good cpumask for its sched_domain to span. It |
| 5596 | * should be one that prevents unnecessary balancing, but also spreads tasks |
| 5597 | * out optimally. |
| 5598 | */ |
| 5599 | static cpumask_t sched_domain_node_span(int node) |
| 5600 | { |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5601 | DECLARE_BITMAP(used_nodes, MAX_NUMNODES); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5602 | cpumask_t span, nodemask; |
| 5603 | int i; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5604 | |
| 5605 | cpus_clear(span); |
| 5606 | bitmap_zero(used_nodes, MAX_NUMNODES); |
| 5607 | |
| 5608 | nodemask = node_to_cpumask(node); |
| 5609 | cpus_or(span, span, nodemask); |
| 5610 | set_bit(node, used_nodes); |
| 5611 | |
| 5612 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { |
| 5613 | int next_node = find_next_best_node(node, used_nodes); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5614 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5615 | nodemask = node_to_cpumask(next_node); |
| 5616 | cpus_or(span, span, nodemask); |
| 5617 | } |
| 5618 | |
| 5619 | return span; |
| 5620 | } |
| 5621 | #endif |
| 5622 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 5623 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5624 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5625 | /* |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5626 | * SMT sched-domains: |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5627 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | #ifdef CONFIG_SCHED_SMT |
| 5629 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5630 | static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5631 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5632 | static int cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, |
| 5633 | struct sched_group **sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5635 | if (sg) |
| 5636 | *sg = &per_cpu(sched_group_cpus, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | return cpu; |
| 5638 | } |
| 5639 | #endif |
| 5640 | |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5641 | /* |
| 5642 | * multi-core sched-domains: |
| 5643 | */ |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5644 | #ifdef CONFIG_SCHED_MC |
| 5645 | static DEFINE_PER_CPU(struct sched_domain, core_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5646 | static DEFINE_PER_CPU(struct sched_group, sched_group_core); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5647 | #endif |
| 5648 | |
| 5649 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5650 | static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map, |
| 5651 | struct sched_group **sg) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5652 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5653 | int group; |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 5654 | cpumask_t mask = cpu_sibling_map[cpu]; |
| 5655 | cpus_and(mask, mask, *cpu_map); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5656 | group = first_cpu(mask); |
| 5657 | if (sg) |
| 5658 | *sg = &per_cpu(sched_group_core, group); |
| 5659 | return group; |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5660 | } |
| 5661 | #elif defined(CONFIG_SCHED_MC) |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5662 | static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map, |
| 5663 | struct sched_group **sg) |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5664 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5665 | if (sg) |
| 5666 | *sg = &per_cpu(sched_group_core, cpu); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5667 | return cpu; |
| 5668 | } |
| 5669 | #endif |
| 5670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5672 | static DEFINE_PER_CPU(struct sched_group, sched_group_phys); |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5673 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5674 | static int cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, |
| 5675 | struct sched_group **sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5677 | int group; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 5678 | #ifdef CONFIG_SCHED_MC |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5679 | cpumask_t mask = cpu_coregroup_map(cpu); |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 5680 | cpus_and(mask, mask, *cpu_map); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5681 | group = first_cpu(mask); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5682 | #elif defined(CONFIG_SCHED_SMT) |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 5683 | cpumask_t mask = cpu_sibling_map[cpu]; |
| 5684 | cpus_and(mask, mask, *cpu_map); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5685 | group = first_cpu(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | #else |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5687 | group = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | #endif |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5689 | if (sg) |
| 5690 | *sg = &per_cpu(sched_group_phys, group); |
| 5691 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | #ifdef CONFIG_NUMA |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5695 | /* |
| 5696 | * The init_sched_build_groups can't handle what we want to do with node |
| 5697 | * groups, so roll our own. Now each node has its own list of groups which |
| 5698 | * gets dynamically allocated. |
| 5699 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5701 | static struct sched_group **sched_group_nodes_bycpu[NR_CPUS]; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5702 | |
| 5703 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5704 | static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5705 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5706 | static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map, |
| 5707 | struct sched_group **sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | { |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5709 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu)); |
| 5710 | int group; |
| 5711 | |
| 5712 | cpus_and(nodemask, nodemask, *cpu_map); |
| 5713 | group = first_cpu(nodemask); |
| 5714 | |
| 5715 | if (sg) |
| 5716 | *sg = &per_cpu(sched_group_allnodes, group); |
| 5717 | return group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | } |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5719 | |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 5720 | static void init_numa_sched_groups_power(struct sched_group *group_head) |
| 5721 | { |
| 5722 | struct sched_group *sg = group_head; |
| 5723 | int j; |
| 5724 | |
| 5725 | if (!sg) |
| 5726 | return; |
| 5727 | next_sg: |
| 5728 | for_each_cpu_mask(j, sg->cpumask) { |
| 5729 | struct sched_domain *sd; |
| 5730 | |
| 5731 | sd = &per_cpu(phys_domains, j); |
| 5732 | if (j != first_cpu(sd->groups->cpumask)) { |
| 5733 | /* |
| 5734 | * Only add "power" once for each |
| 5735 | * physical package. |
| 5736 | */ |
| 5737 | continue; |
| 5738 | } |
| 5739 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 5740 | sg_inc_cpu_power(sg, sd->groups->__cpu_power); |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 5741 | } |
| 5742 | sg = sg->next; |
| 5743 | if (sg != group_head) |
| 5744 | goto next_sg; |
| 5745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | #endif |
| 5747 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 5748 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5749 | /* Free memory allocated for various sched_group structures */ |
| 5750 | static void free_sched_groups(const cpumask_t *cpu_map) |
| 5751 | { |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 5752 | int cpu, i; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5753 | |
| 5754 | for_each_cpu_mask(cpu, *cpu_map) { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5755 | struct sched_group **sched_group_nodes |
| 5756 | = sched_group_nodes_bycpu[cpu]; |
| 5757 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5758 | if (!sched_group_nodes) |
| 5759 | continue; |
| 5760 | |
| 5761 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 5762 | cpumask_t nodemask = node_to_cpumask(i); |
| 5763 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; |
| 5764 | |
| 5765 | cpus_and(nodemask, nodemask, *cpu_map); |
| 5766 | if (cpus_empty(nodemask)) |
| 5767 | continue; |
| 5768 | |
| 5769 | if (sg == NULL) |
| 5770 | continue; |
| 5771 | sg = sg->next; |
| 5772 | next_sg: |
| 5773 | oldsg = sg; |
| 5774 | sg = sg->next; |
| 5775 | kfree(oldsg); |
| 5776 | if (oldsg != sched_group_nodes[i]) |
| 5777 | goto next_sg; |
| 5778 | } |
| 5779 | kfree(sched_group_nodes); |
| 5780 | sched_group_nodes_bycpu[cpu] = NULL; |
| 5781 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5782 | } |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 5783 | #else |
| 5784 | static void free_sched_groups(const cpumask_t *cpu_map) |
| 5785 | { |
| 5786 | } |
| 5787 | #endif |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | /* |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 5790 | * Initialize sched groups cpu_power. |
| 5791 | * |
| 5792 | * cpu_power indicates the capacity of sched group, which is used while |
| 5793 | * distributing the load between different sched groups in a sched domain. |
| 5794 | * Typically cpu_power for all the groups in a sched domain will be same unless |
| 5795 | * there are asymmetries in the topology. If there are asymmetries, group |
| 5796 | * having more cpu_power will pickup more load compared to the group having |
| 5797 | * less cpu_power. |
| 5798 | * |
| 5799 | * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents |
| 5800 | * the maximum number of tasks a group can handle in the presence of other idle |
| 5801 | * or lightly loaded groups in the same sched domain. |
| 5802 | */ |
| 5803 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) |
| 5804 | { |
| 5805 | struct sched_domain *child; |
| 5806 | struct sched_group *group; |
| 5807 | |
| 5808 | WARN_ON(!sd || !sd->groups); |
| 5809 | |
| 5810 | if (cpu != first_cpu(sd->groups->cpumask)) |
| 5811 | return; |
| 5812 | |
| 5813 | child = sd->child; |
| 5814 | |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 5815 | sd->groups->__cpu_power = 0; |
| 5816 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 5817 | /* |
| 5818 | * For perf policy, if the groups in child domain share resources |
| 5819 | * (for example cores sharing some portions of the cache hierarchy |
| 5820 | * or SMT), then set this domain groups cpu_power such that each group |
| 5821 | * can handle only one task, when there are other idle groups in the |
| 5822 | * same sched domain. |
| 5823 | */ |
| 5824 | if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) && |
| 5825 | (child->flags & |
| 5826 | (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 5827 | sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 5828 | return; |
| 5829 | } |
| 5830 | |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 5831 | /* |
| 5832 | * add cpu_power of each child group to this groups cpu_power |
| 5833 | */ |
| 5834 | group = child->groups; |
| 5835 | do { |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 5836 | sg_inc_cpu_power(sd->groups, group->__cpu_power); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 5837 | group = group->next; |
| 5838 | } while (group != child->groups); |
| 5839 | } |
| 5840 | |
| 5841 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5842 | * Build sched domains for a given set of cpus and attach the sched domains |
| 5843 | * to the individual cpus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5844 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5845 | static int build_sched_domains(const cpumask_t *cpu_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | { |
| 5847 | int i; |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 5848 | struct sched_domain *sd; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5849 | #ifdef CONFIG_NUMA |
| 5850 | struct sched_group **sched_group_nodes = NULL; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5851 | int sd_allnodes = 0; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5852 | |
| 5853 | /* |
| 5854 | * Allocate the per-node list of sched groups |
| 5855 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5856 | sched_group_nodes = kzalloc(sizeof(struct sched_group*)*MAX_NUMNODES, |
Srivatsa Vaddagiri | d3a5aa9 | 2006-06-27 02:54:39 -0700 | [diff] [blame] | 5857 | GFP_KERNEL); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5858 | if (!sched_group_nodes) { |
| 5859 | printk(KERN_WARNING "Can not alloc sched group node list\n"); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5860 | return -ENOMEM; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5861 | } |
| 5862 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; |
| 5863 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | |
| 5865 | /* |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5866 | * Set up domains for cpus specified by the cpu_map. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5868 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | struct sched_domain *sd = NULL, *p; |
| 5870 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(i)); |
| 5871 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5872 | cpus_and(nodemask, nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | |
| 5874 | #ifdef CONFIG_NUMA |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5875 | if (cpus_weight(*cpu_map) |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5876 | > SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) { |
| 5877 | sd = &per_cpu(allnodes_domains, i); |
| 5878 | *sd = SD_ALLNODES_INIT; |
| 5879 | sd->span = *cpu_map; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5880 | cpu_to_allnodes_group(i, cpu_map, &sd->groups); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5881 | p = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5882 | sd_allnodes = 1; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5883 | } else |
| 5884 | p = NULL; |
| 5885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | sd = &per_cpu(node_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | *sd = SD_NODE_INIT; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5888 | sd->span = sched_domain_node_span(cpu_to_node(i)); |
| 5889 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 5890 | if (p) |
| 5891 | p->child = sd; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5892 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | #endif |
| 5894 | |
| 5895 | p = sd; |
| 5896 | sd = &per_cpu(phys_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | *sd = SD_CPU_INIT; |
| 5898 | sd->span = nodemask; |
| 5899 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 5900 | if (p) |
| 5901 | p->child = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5902 | cpu_to_phys_group(i, cpu_map, &sd->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5904 | #ifdef CONFIG_SCHED_MC |
| 5905 | p = sd; |
| 5906 | sd = &per_cpu(core_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5907 | *sd = SD_MC_INIT; |
| 5908 | sd->span = cpu_coregroup_map(i); |
| 5909 | cpus_and(sd->span, sd->span, *cpu_map); |
| 5910 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 5911 | p->child = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5912 | cpu_to_core_group(i, cpu_map, &sd->groups); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5913 | #endif |
| 5914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | #ifdef CONFIG_SCHED_SMT |
| 5916 | p = sd; |
| 5917 | sd = &per_cpu(cpu_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | *sd = SD_SIBLING_INIT; |
| 5919 | sd->span = cpu_sibling_map[i]; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5920 | cpus_and(sd->span, sd->span, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | sd->parent = p; |
Siddha, Suresh B | 1a84887 | 2006-10-03 01:14:08 -0700 | [diff] [blame] | 5922 | p->child = sd; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5923 | cpu_to_cpu_group(i, cpu_map, &sd->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | #endif |
| 5925 | } |
| 5926 | |
| 5927 | #ifdef CONFIG_SCHED_SMT |
| 5928 | /* Set up CPU (sibling) groups */ |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5929 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | cpumask_t this_sibling_map = cpu_sibling_map[i]; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5931 | cpus_and(this_sibling_map, this_sibling_map, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | if (i != first_cpu(this_sibling_map)) |
| 5933 | continue; |
| 5934 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5935 | init_sched_build_groups(this_sibling_map, cpu_map, &cpu_to_cpu_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | } |
| 5937 | #endif |
| 5938 | |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5939 | #ifdef CONFIG_SCHED_MC |
| 5940 | /* Set up multi-core groups */ |
| 5941 | for_each_cpu_mask(i, *cpu_map) { |
| 5942 | cpumask_t this_core_map = cpu_coregroup_map(i); |
| 5943 | cpus_and(this_core_map, this_core_map, *cpu_map); |
| 5944 | if (i != first_cpu(this_core_map)) |
| 5945 | continue; |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5946 | init_sched_build_groups(this_core_map, cpu_map, &cpu_to_core_group); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 5947 | } |
| 5948 | #endif |
| 5949 | |
| 5950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | /* Set up physical groups */ |
| 5952 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 5953 | cpumask_t nodemask = node_to_cpumask(i); |
| 5954 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 5955 | cpus_and(nodemask, nodemask, *cpu_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | if (cpus_empty(nodemask)) |
| 5957 | continue; |
| 5958 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5959 | init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | } |
| 5961 | |
| 5962 | #ifdef CONFIG_NUMA |
| 5963 | /* Set up node groups */ |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 5964 | if (sd_allnodes) |
| 5965 | init_sched_build_groups(*cpu_map, cpu_map, &cpu_to_allnodes_group); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5966 | |
| 5967 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 5968 | /* Set up node groups */ |
| 5969 | struct sched_group *sg, *prev; |
| 5970 | cpumask_t nodemask = node_to_cpumask(i); |
| 5971 | cpumask_t domainspan; |
| 5972 | cpumask_t covered = CPU_MASK_NONE; |
| 5973 | int j; |
| 5974 | |
| 5975 | cpus_and(nodemask, nodemask, *cpu_map); |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5976 | if (cpus_empty(nodemask)) { |
| 5977 | sched_group_nodes[i] = NULL; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5978 | continue; |
John Hawkes | d1b5513 | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5979 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5980 | |
| 5981 | domainspan = sched_domain_node_span(i); |
| 5982 | cpus_and(domainspan, domainspan, *cpu_map); |
| 5983 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 5984 | sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5985 | if (!sg) { |
| 5986 | printk(KERN_WARNING "Can not alloc domain group for " |
| 5987 | "node %d\n", i); |
| 5988 | goto error; |
| 5989 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5990 | sched_group_nodes[i] = sg; |
| 5991 | for_each_cpu_mask(j, nodemask) { |
| 5992 | struct sched_domain *sd; |
| 5993 | sd = &per_cpu(node_domains, j); |
| 5994 | sd->groups = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5995 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 5996 | sg->__cpu_power = 0; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5997 | sg->cpumask = nodemask; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 5998 | sg->next = sg; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 5999 | cpus_or(covered, covered, nodemask); |
| 6000 | prev = sg; |
| 6001 | |
| 6002 | for (j = 0; j < MAX_NUMNODES; j++) { |
| 6003 | cpumask_t tmp, notcovered; |
| 6004 | int n = (i + j) % MAX_NUMNODES; |
| 6005 | |
| 6006 | cpus_complement(notcovered, covered); |
| 6007 | cpus_and(tmp, notcovered, *cpu_map); |
| 6008 | cpus_and(tmp, tmp, domainspan); |
| 6009 | if (cpus_empty(tmp)) |
| 6010 | break; |
| 6011 | |
| 6012 | nodemask = node_to_cpumask(n); |
| 6013 | cpus_and(tmp, tmp, nodemask); |
| 6014 | if (cpus_empty(tmp)) |
| 6015 | continue; |
| 6016 | |
Srivatsa Vaddagiri | 15f0b67 | 2006-06-27 02:54:40 -0700 | [diff] [blame] | 6017 | sg = kmalloc_node(sizeof(struct sched_group), |
| 6018 | GFP_KERNEL, i); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6019 | if (!sg) { |
| 6020 | printk(KERN_WARNING |
| 6021 | "Can not alloc domain group for node %d\n", j); |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6022 | goto error; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6023 | } |
Eric Dumazet | 5517d86 | 2007-05-08 00:32:57 -0700 | [diff] [blame] | 6024 | sg->__cpu_power = 0; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6025 | sg->cpumask = tmp; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6026 | sg->next = prev->next; |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6027 | cpus_or(covered, covered, tmp); |
| 6028 | prev->next = sg; |
| 6029 | prev = sg; |
| 6030 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | #endif |
| 6033 | |
| 6034 | /* Calculate CPU power for physical packages and nodes */ |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6035 | #ifdef CONFIG_SCHED_SMT |
| 6036 | for_each_cpu_mask(i, *cpu_map) { |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6037 | sd = &per_cpu(cpu_domains, i); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6038 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6039 | } |
| 6040 | #endif |
| 6041 | #ifdef CONFIG_SCHED_MC |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6042 | for_each_cpu_mask(i, *cpu_map) { |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6043 | sd = &per_cpu(core_domains, i); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6044 | init_sched_groups_power(i, sd); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6045 | } |
| 6046 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6048 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | sd = &per_cpu(phys_domains, i); |
Siddha, Suresh B | 89c4710 | 2006-10-03 01:14:09 -0700 | [diff] [blame] | 6050 | init_sched_groups_power(i, sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | } |
| 6052 | |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6053 | #ifdef CONFIG_NUMA |
Siddha, Suresh B | 0806903 | 2006-03-27 01:15:23 -0800 | [diff] [blame] | 6054 | for (i = 0; i < MAX_NUMNODES; i++) |
| 6055 | init_numa_sched_groups_power(sched_group_nodes[i]); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6056 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6057 | if (sd_allnodes) { |
| 6058 | struct sched_group *sg; |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 6059 | |
Siddha, Suresh B | 6711cab | 2006-12-10 02:20:07 -0800 | [diff] [blame] | 6060 | cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg); |
Siddha, Suresh B | f712c0c7 | 2006-07-30 03:02:59 -0700 | [diff] [blame] | 6061 | init_numa_sched_groups_power(sg); |
| 6062 | } |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6063 | #endif |
| 6064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | /* Attach the domains */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6066 | for_each_cpu_mask(i, *cpu_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | struct sched_domain *sd; |
| 6068 | #ifdef CONFIG_SCHED_SMT |
| 6069 | sd = &per_cpu(cpu_domains, i); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 6070 | #elif defined(CONFIG_SCHED_MC) |
| 6071 | sd = &per_cpu(core_domains, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | #else |
| 6073 | sd = &per_cpu(phys_domains, i); |
| 6074 | #endif |
| 6075 | cpu_attach_domain(sd, i); |
| 6076 | } |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6077 | |
| 6078 | return 0; |
| 6079 | |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6080 | #ifdef CONFIG_NUMA |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6081 | error: |
| 6082 | free_sched_groups(cpu_map); |
| 6083 | return -ENOMEM; |
Siddha, Suresh B | a616058 | 2006-10-03 01:14:06 -0700 | [diff] [blame] | 6084 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | } |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6086 | /* |
| 6087 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
| 6088 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6089 | static int arch_init_sched_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6090 | { |
| 6091 | cpumask_t cpu_default_map; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6092 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6094 | /* |
| 6095 | * Setup mask for cpus without special case scheduling requirements. |
| 6096 | * For now this just excludes isolated cpus, but could be used to |
| 6097 | * exclude other special cases in the future. |
| 6098 | */ |
| 6099 | cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map); |
| 6100 | |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6101 | err = build_sched_domains(&cpu_default_map); |
| 6102 | |
| 6103 | return err; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6104 | } |
| 6105 | |
| 6106 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | { |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6108 | free_sched_groups(cpu_map); |
John Hawkes | 9c1cfda | 2005-09-06 15:18:14 -0700 | [diff] [blame] | 6109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6111 | /* |
| 6112 | * Detach sched domains from a group of cpus specified in cpu_map |
| 6113 | * These cpus will now be attached to the NULL domain |
| 6114 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 6115 | static void detach_destroy_domains(const cpumask_t *cpu_map) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6116 | { |
| 6117 | int i; |
| 6118 | |
| 6119 | for_each_cpu_mask(i, *cpu_map) |
| 6120 | cpu_attach_domain(NULL, i); |
| 6121 | synchronize_sched(); |
| 6122 | arch_destroy_sched_domains(cpu_map); |
| 6123 | } |
| 6124 | |
| 6125 | /* |
| 6126 | * Partition sched domains as specified by the cpumasks below. |
| 6127 | * This attaches all cpus from the cpumasks to the NULL domain, |
| 6128 | * waits for a RCU quiescent period, recalculates sched |
| 6129 | * domain information and then attaches them back to the |
| 6130 | * correct sched domains |
| 6131 | * Call with hotplug lock held |
| 6132 | */ |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6133 | int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2) |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6134 | { |
| 6135 | cpumask_t change_map; |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6136 | int err = 0; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6137 | |
| 6138 | cpus_and(*partition1, *partition1, cpu_online_map); |
| 6139 | cpus_and(*partition2, *partition2, cpu_online_map); |
| 6140 | cpus_or(change_map, *partition1, *partition2); |
| 6141 | |
| 6142 | /* Detach sched domains from all of the affected cpus */ |
| 6143 | detach_destroy_domains(&change_map); |
| 6144 | if (!cpus_empty(*partition1)) |
Srivatsa Vaddagiri | 51888ca | 2006-06-27 02:54:38 -0700 | [diff] [blame] | 6145 | err = build_sched_domains(partition1); |
| 6146 | if (!err && !cpus_empty(*partition2)) |
| 6147 | err = build_sched_domains(partition2); |
| 6148 | |
| 6149 | return err; |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6150 | } |
| 6151 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6152 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 6153 | int arch_reinit_sched_domains(void) |
| 6154 | { |
| 6155 | int err; |
| 6156 | |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 6157 | mutex_lock(&sched_hotcpu_mutex); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6158 | detach_destroy_domains(&cpu_online_map); |
| 6159 | err = arch_init_sched_domains(&cpu_online_map); |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 6160 | mutex_unlock(&sched_hotcpu_mutex); |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6161 | |
| 6162 | return err; |
| 6163 | } |
| 6164 | |
| 6165 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) |
| 6166 | { |
| 6167 | int ret; |
| 6168 | |
| 6169 | if (buf[0] != '0' && buf[0] != '1') |
| 6170 | return -EINVAL; |
| 6171 | |
| 6172 | if (smt) |
| 6173 | sched_smt_power_savings = (buf[0] == '1'); |
| 6174 | else |
| 6175 | sched_mc_power_savings = (buf[0] == '1'); |
| 6176 | |
| 6177 | ret = arch_reinit_sched_domains(); |
| 6178 | |
| 6179 | return ret ? ret : count; |
| 6180 | } |
| 6181 | |
| 6182 | int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) |
| 6183 | { |
| 6184 | int err = 0; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6185 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6186 | #ifdef CONFIG_SCHED_SMT |
| 6187 | if (smt_capable()) |
| 6188 | err = sysfs_create_file(&cls->kset.kobj, |
| 6189 | &attr_sched_smt_power_savings.attr); |
| 6190 | #endif |
| 6191 | #ifdef CONFIG_SCHED_MC |
| 6192 | if (!err && mc_capable()) |
| 6193 | err = sysfs_create_file(&cls->kset.kobj, |
| 6194 | &attr_sched_mc_power_savings.attr); |
| 6195 | #endif |
| 6196 | return err; |
| 6197 | } |
| 6198 | #endif |
| 6199 | |
| 6200 | #ifdef CONFIG_SCHED_MC |
| 6201 | static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page) |
| 6202 | { |
| 6203 | return sprintf(page, "%u\n", sched_mc_power_savings); |
| 6204 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6205 | static ssize_t sched_mc_power_savings_store(struct sys_device *dev, |
| 6206 | const char *buf, size_t count) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6207 | { |
| 6208 | return sched_power_savings_store(buf, count, 0); |
| 6209 | } |
| 6210 | SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show, |
| 6211 | sched_mc_power_savings_store); |
| 6212 | #endif |
| 6213 | |
| 6214 | #ifdef CONFIG_SCHED_SMT |
| 6215 | static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page) |
| 6216 | { |
| 6217 | return sprintf(page, "%u\n", sched_smt_power_savings); |
| 6218 | } |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6219 | static ssize_t sched_smt_power_savings_store(struct sys_device *dev, |
| 6220 | const char *buf, size_t count) |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 6221 | { |
| 6222 | return sched_power_savings_store(buf, count, 1); |
| 6223 | } |
| 6224 | SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show, |
| 6225 | sched_smt_power_savings_store); |
| 6226 | #endif |
| 6227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | /* |
| 6229 | * Force a reinitialization of the sched domains hierarchy. The domains |
| 6230 | * and groups cannot be updated in place without racing with the balancing |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 6231 | * code, so we temporarily attach all running cpus to the NULL domain |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6232 | * which will prevent rebalancing while the sched domains are recalculated. |
| 6233 | */ |
| 6234 | static int update_sched_domains(struct notifier_block *nfb, |
| 6235 | unsigned long action, void *hcpu) |
| 6236 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | switch (action) { |
| 6238 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6239 | case CPU_UP_PREPARE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | case CPU_DOWN_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6241 | case CPU_DOWN_PREPARE_FROZEN: |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6242 | detach_destroy_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | return NOTIFY_OK; |
| 6244 | |
| 6245 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6246 | case CPU_UP_CANCELED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6247 | case CPU_DOWN_FAILED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6248 | case CPU_DOWN_FAILED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6250 | case CPU_ONLINE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6252 | case CPU_DEAD_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6253 | /* |
| 6254 | * Fall through and re-initialise the domains. |
| 6255 | */ |
| 6256 | break; |
| 6257 | default: |
| 6258 | return NOTIFY_DONE; |
| 6259 | } |
| 6260 | |
| 6261 | /* The hotplug lock is already held by cpu_up/cpu_down */ |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6262 | arch_init_sched_domains(&cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6263 | |
| 6264 | return NOTIFY_OK; |
| 6265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | |
| 6267 | void __init sched_init_smp(void) |
| 6268 | { |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 6269 | cpumask_t non_isolated_cpus; |
| 6270 | |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 6271 | mutex_lock(&sched_hotcpu_mutex); |
Dinakar Guniguntala | 1a20ff2 | 2005-06-25 14:57:33 -0700 | [diff] [blame] | 6272 | arch_init_sched_domains(&cpu_online_map); |
Nathan Lynch | e5e5673 | 2007-01-10 23:15:28 -0800 | [diff] [blame] | 6273 | cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 6274 | if (cpus_empty(non_isolated_cpus)) |
| 6275 | cpu_set(smp_processor_id(), non_isolated_cpus); |
Gautham R Shenoy | 5be9361 | 2007-05-09 02:34:04 -0700 | [diff] [blame] | 6276 | mutex_unlock(&sched_hotcpu_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
| 6278 | hotcpu_notifier(update_sched_domains, 0); |
Nick Piggin | 5c1e176 | 2006-10-03 01:14:04 -0700 | [diff] [blame] | 6279 | |
| 6280 | /* Move init over to a non-isolated CPU */ |
| 6281 | if (set_cpus_allowed(current, non_isolated_cpus) < 0) |
| 6282 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | } |
| 6284 | #else |
| 6285 | void __init sched_init_smp(void) |
| 6286 | { |
| 6287 | } |
| 6288 | #endif /* CONFIG_SMP */ |
| 6289 | |
| 6290 | int in_sched_functions(unsigned long addr) |
| 6291 | { |
| 6292 | /* Linker adds these: start and end of __sched functions */ |
| 6293 | extern char __sched_text_start[], __sched_text_end[]; |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | return in_lock_functions(addr) || |
| 6296 | (addr >= (unsigned long)__sched_text_start |
| 6297 | && addr < (unsigned long)__sched_text_end); |
| 6298 | } |
| 6299 | |
| 6300 | void __init sched_init(void) |
| 6301 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | int i, j, k; |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 6303 | int highest_cpu = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 6305 | for_each_possible_cpu(i) { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6306 | struct prio_array *array; |
| 6307 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | |
| 6309 | rq = cpu_rq(i); |
| 6310 | spin_lock_init(&rq->lock); |
Ingo Molnar | fcb9937 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 6311 | lockdep_set_class(&rq->lock, &rq->rq_lock_key); |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 6312 | rq->nr_running = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | rq->active = rq->arrays; |
| 6314 | rq->expired = rq->arrays + 1; |
| 6315 | rq->best_expired_prio = MAX_PRIO; |
| 6316 | |
| 6317 | #ifdef CONFIG_SMP |
Nick Piggin | 41c7ce9 | 2005-06-25 14:57:24 -0700 | [diff] [blame] | 6318 | rq->sd = NULL; |
Nick Piggin | 7897986 | 2005-06-25 14:57:13 -0700 | [diff] [blame] | 6319 | for (j = 1; j < 3; j++) |
| 6320 | rq->cpu_load[j] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | rq->active_balance = 0; |
| 6322 | rq->push_cpu = 0; |
Christoph Lameter | 0a2966b | 2006-09-25 23:30:51 -0700 | [diff] [blame] | 6323 | rq->cpu = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | rq->migration_thread = NULL; |
| 6325 | INIT_LIST_HEAD(&rq->migration_queue); |
| 6326 | #endif |
| 6327 | atomic_set(&rq->nr_iowait, 0); |
| 6328 | |
| 6329 | for (j = 0; j < 2; j++) { |
| 6330 | array = rq->arrays + j; |
| 6331 | for (k = 0; k < MAX_PRIO; k++) { |
| 6332 | INIT_LIST_HEAD(array->queue + k); |
| 6333 | __clear_bit(k, array->bitmap); |
| 6334 | } |
| 6335 | // delimiter for bitsearch |
| 6336 | __set_bit(MAX_PRIO, array->bitmap); |
| 6337 | } |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 6338 | highest_cpu = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | } |
| 6340 | |
Peter Williams | 2dd73a4 | 2006-06-27 02:54:34 -0700 | [diff] [blame] | 6341 | set_load_weight(&init_task); |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 6342 | |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 6343 | #ifdef CONFIG_SMP |
Christoph Lameter | 476f353 | 2007-05-06 14:48:58 -0700 | [diff] [blame] | 6344 | nr_cpu_ids = highest_cpu + 1; |
Christoph Lameter | c9819f4 | 2006-12-10 02:20:25 -0800 | [diff] [blame] | 6345 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL); |
| 6346 | #endif |
| 6347 | |
Heiko Carstens | b50f60c | 2006-07-30 03:03:52 -0700 | [diff] [blame] | 6348 | #ifdef CONFIG_RT_MUTEXES |
| 6349 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); |
| 6350 | #endif |
| 6351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | /* |
| 6353 | * The boot idle thread does lazy MMU switching as well: |
| 6354 | */ |
| 6355 | atomic_inc(&init_mm.mm_count); |
| 6356 | enter_lazy_tlb(&init_mm, current); |
| 6357 | |
| 6358 | /* |
| 6359 | * Make us the idle thread. Technically, schedule() should not be |
| 6360 | * called from this thread, however somewhere below it might be, |
| 6361 | * but because we are the idle thread, we just pick up running again |
| 6362 | * when this runqueue becomes "idle". |
| 6363 | */ |
| 6364 | init_idle(current, smp_processor_id()); |
| 6365 | } |
| 6366 | |
| 6367 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 6368 | void __might_sleep(char *file, int line) |
| 6369 | { |
Ingo Molnar | 48f24c4 | 2006-07-03 00:25:40 -0700 | [diff] [blame] | 6370 | #ifdef in_atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | static unsigned long prev_jiffy; /* ratelimiting */ |
| 6372 | |
| 6373 | if ((in_atomic() || irqs_disabled()) && |
| 6374 | system_state == SYSTEM_RUNNING && !oops_in_progress) { |
| 6375 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 6376 | return; |
| 6377 | prev_jiffy = jiffies; |
Ingo Molnar | 91368d7 | 2006-03-23 03:00:54 -0800 | [diff] [blame] | 6378 | printk(KERN_ERR "BUG: sleeping function called from invalid" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6379 | " context at %s:%d\n", file, line); |
| 6380 | printk("in_atomic():%d, irqs_disabled():%d\n", |
| 6381 | in_atomic(), irqs_disabled()); |
Peter Zijlstra | a4c410f | 2006-12-06 20:37:21 -0800 | [diff] [blame] | 6382 | debug_show_held_locks(current); |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 6383 | if (irqs_disabled()) |
| 6384 | print_irqtrace_events(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | dump_stack(); |
| 6386 | } |
| 6387 | #endif |
| 6388 | } |
| 6389 | EXPORT_SYMBOL(__might_sleep); |
| 6390 | #endif |
| 6391 | |
| 6392 | #ifdef CONFIG_MAGIC_SYSRQ |
| 6393 | void normalize_rt_tasks(void) |
| 6394 | { |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6395 | struct prio_array *array; |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 6396 | struct task_struct *g, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6397 | unsigned long flags; |
Ingo Molnar | 70b97a7 | 2006-07-03 00:25:42 -0700 | [diff] [blame] | 6398 | struct rq *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6399 | |
| 6400 | read_lock_irq(&tasklist_lock); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 6401 | |
| 6402 | do_each_thread(g, p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | if (!rt_task(p)) |
| 6404 | continue; |
| 6405 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6406 | spin_lock_irqsave(&p->pi_lock, flags); |
| 6407 | rq = __task_rq_lock(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | |
| 6409 | array = p->array; |
| 6410 | if (array) |
| 6411 | deactivate_task(p, task_rq(p)); |
| 6412 | __setscheduler(p, SCHED_NORMAL, 0); |
| 6413 | if (array) { |
| 6414 | __activate_task(p, task_rq(p)); |
| 6415 | resched_task(rq->curr); |
| 6416 | } |
| 6417 | |
Ingo Molnar | b29739f | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 6418 | __task_rq_unlock(rq); |
| 6419 | spin_unlock_irqrestore(&p->pi_lock, flags); |
Ingo Molnar | a0f98a1 | 2007-06-17 18:37:45 +0200 | [diff] [blame] | 6420 | } while_each_thread(g, p); |
| 6421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6422 | read_unlock_irq(&tasklist_lock); |
| 6423 | } |
| 6424 | |
| 6425 | #endif /* CONFIG_MAGIC_SYSRQ */ |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 6426 | |
| 6427 | #ifdef CONFIG_IA64 |
| 6428 | /* |
| 6429 | * These functions are only useful for the IA64 MCA handling. |
| 6430 | * |
| 6431 | * They can only be called when the whole system has been |
| 6432 | * stopped - every CPU needs to be quiescent, and no scheduling |
| 6433 | * activity can take place. Using them for anything else would |
| 6434 | * be a serious bug, and as a result, they aren't even visible |
| 6435 | * under any other configuration. |
| 6436 | */ |
| 6437 | |
| 6438 | /** |
| 6439 | * curr_task - return the current task for a given cpu. |
| 6440 | * @cpu: the processor in question. |
| 6441 | * |
| 6442 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 6443 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6444 | struct task_struct *curr_task(int cpu) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 6445 | { |
| 6446 | return cpu_curr(cpu); |
| 6447 | } |
| 6448 | |
| 6449 | /** |
| 6450 | * set_curr_task - set the current task for a given cpu. |
| 6451 | * @cpu: the processor in question. |
| 6452 | * @p: the task pointer to set. |
| 6453 | * |
| 6454 | * Description: This function must only be used when non-maskable interrupts |
| 6455 | * are serviced on a separate stack. It allows the architecture to switch the |
| 6456 | * notion of the current task on a cpu in a non-blocking manner. This function |
| 6457 | * must be called with all CPU's synchronized, and interrupts disabled, the |
| 6458 | * and caller must save the original value of the current task (see |
| 6459 | * curr_task() above) and restore that value before reenabling interrupts and |
| 6460 | * re-starting the system. |
| 6461 | * |
| 6462 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! |
| 6463 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 6464 | void set_curr_task(int cpu, struct task_struct *p) |
Linus Torvalds | 1df5c10 | 2005-09-12 07:59:21 -0700 | [diff] [blame] | 6465 | { |
| 6466 | cpu_curr(cpu) = p; |
| 6467 | } |
| 6468 | |
| 6469 | #endif |